
    TAi!=                         S r SSKJr   SSKJr  SSKJr  SrSSK
Jr  SSK
Jr  SS	K
Jr  SS
 jr     SS jrS rS rS rSS jr " S S5      rg! \	 a    Sr NDf = f)z0Plotting functions usefull for visual debugging.    )LTTextLineVerticalNTF   )bbox_from_str)bbox_from_textlines)get_textline_coordsc                    U R                  5       u  p4U R                  5       u  pVU R                  [        X1S   U-
  5      [	        XAS   U-   5      5        U R                  [        XQS   U-
  5      [	        XaS   U-   5      5        g)z,Ensure the ax limits include the input bbox.r      r      N)get_xlimget_ylimset_xlimminmaxset_ylim)axbboxmarginx0x1y0y1s          L/var/www/html/land-ocr/venv/lib/python3.13/site-packages/camelot/plotting.pyextend_axe_limr      sp    [[]FB[[]FBKKBQ&()3r7V3C+DEKKBQ&()3r7V3C+DE    c                     U R                  [        R                  " US   US   4US   US   -
  US   US   -
  UUUSS95        UR                  S5      u  pUS:X  a  [	        US   US   5      n
O'US	:X  a  [        US   US   5      n
OS
US   US   -   -  n
S	SSS.nX   nU	S:X  a  [	        US   US   5      nO'U	S:X  a  [        US   US   5      nOS
US   US   -   -  nU R                  UU
UUSUU	[        USS9S9  g)a\  Utility drawing function to draw a box with an associated text label.

Parameters
----------
ax : matplotlib.axes.Axes
    matplotlib.axes.Axes (optional)
bbox : [type]
    boundingbox
text : string
    The text to be placed inside the box.
color : str, optional
    The color of the box, by default "black"
linewidth : int, optional
    The linewidth of the box, by default 3
linestyle : str, optional
    The matplotlib linestyle, by default "solid"
label_pos : str, optional
    The label postiion, by default "top,left"
fontsize : int, optional
    The fontsize of the text in the box, by default 12
r   r   r	   r
   F)color	linewidth	linestylefill,topbottom      ?center)r!   r"   r$   rightleftblackg?)	facecoloralpha)fontsizer   verticalalignmenthorizontalalignmentr   N)	add_patchpatches	Rectanglesplitr   r   textdict)r   r   r1   r   r   r   	label_posr*   vlabelhlabelylabel_align_swapvlabel_out_of_boxxs                 r   draw_labeled_bboxr:      sQ   > LL!Wd1gGd1gGd1g	

 __S)NFQa!	8	Qa!47T!W$%  (5HM(0Qa!	6	Qa!47T!W$%GG		+"E-  	r   c                     U R                  5       nUR                  USU R                  S   SU R                  S   4S9  g)zDraw the content of the table's source pdf into the passed subplot.

Parameters
----------
table : camelot.core.Table
ax : matplotlib.axes.Axes (optional)
r   r   )extentN)get_pdf_imageimshowpdf_size)tabler   imgs      r   draw_pdfrB   c   s=     


CIIc1ennQ/ENN14EFIGr   c                     U R                   (       ac  SSS.nUR                  5        HI  u  p4U R                   U   =(       d    /  H(  n[        U[        U5      SR	                  X5S9SSSS	S
9  M*     MK     gg)zDraw any user provided constraints (area, region, columns, etc).

Parameters
----------
table : camelot.core.Table
ax : matplotlib.axes.Axes (optional)
table_regionstable_areas)regionareaz{zone_name}: ({zone_str}))	zone_namezone_strpurpledottedr   bottom,right)r   r   r   r3   N)parse_detailsitemsr:   r   format)r@   r   zone_constraintsrH   zone_idrI   s         r   draw_parse_constraintsrR   o   s     %!
 #3"8"8":I!//8>B>!!(+/66"+ 7  #&,
 ? #; r   c                 r   [        U R                  5      nU R                   H  n[        U[        5      (       a  SOSnUR	                  [
        R                  " UR                  UR                  4UR                  UR                  -
  UR                  UR                  -
  USS95        M     [        X5        g)zDraw text, horizontal in blue, vertical in red.

Parameters
----------
table : camelot.core.Table
ax : matplotlib.axes.Axes (optional)
redblueg?r   r)   N)r   	textlines
isinstancer   r-   r.   r/   r   r   r   r   r   )r@   r   r   tr   s        r   	draw_textrZ      s     u/D__#A'9::
qttaddQTTk144!$$;e3	
  2r   c                     Uc%  [         R                  " 5       nUR                  SSS9n[        X5        [	        X5        U$ )zInitialize plot and draw common components.

Parameters
----------
table : camelot.core.Table
ax : matplotlib.axes.Axes (optional)

Returns
-------
ax : matplotlib.axes.Axes
o   equal)aspect)pltfigureadd_subplotrB   rR   )r@   r   figs      r   prepare_plotrc      s;     
zjjl__S_1U5%Ir   c                       \ rS rSrSrSS jrSS jr\SS j5       r\SS j5       r	\SS j5       r
\SS	 j5       r\SS
 j5       r\SS j5       rSrg)PlotMethods   z!Classmethod for plotting methods.Nc                 :   [         (       d  [        S5      eUR                  S:X  a  US;   a  [        SU< 35      eUR                  S:w  a!  US;   a  [        UR                   SU< 35      e[	        X5      nUb  U" X5      nUR                  U5        gU" X5      $ )a2  Plot elements found on PDF page based on kind specified.

Useful for debugging and playing with different
parameters to get the best output.

Parameters
----------
table: camelot.core.Table
    A Camelot Table.
kind : str, optional (default: 'text')
    {'text', 'grid', 'contour', 'joint', 'line',
        'network_table_search'}
    The element type for which a plot should be generated.
filename: str, optional (default: None)
    Absolute path for saving the generated plot.
ax : matplotlib.axes.Axes (optional)

Returns
-------
fig : matplotlib.fig.Figure

z$matplotlib is required for plotting.lattice)textedgez%Lattice flavor does not support kind=)linez flavor does not support kind=N)_HAS_MPLImportErrorflavorNotImplementedErrorgetattrsavefig)selfr@   kindfilenamer   plot_methodrb   s          r   __call__PlotMethods.__call__   s    . xDEE<<9$)=%(MdX&VWW<<9$)9%<<. >thG  d)e(CKK!5%%r   c                 N    [        X5      n[        X5        UR                  5       $ )zGenerate a plot for all text elements present on the PDF page.

Parameters
----------
table : camelot.core.Table
ax : matplotlib.axes.Axes (optional)

Returns
-------
fig : matplotlib.fig.Figure

)rc   rZ   
get_figure)rq   r@   r   s      r   r1   PlotMethods.text   s"     %$%}}r   c                 V   [        X5      nU R                   GH~  nU GHs  nUR                  (       aJ  UR                  UR                  S   UR
                  S   /UR                  S   UR
                  S   /5        UR                  (       aJ  UR                  UR                  S   UR                  S   /UR                  S   UR                  S   /5        UR                  (       aJ  UR                  UR
                  S   UR                  S   /UR
                  S   UR                  S   /5        UR                  (       d  GM)  UR                  UR                  S   UR                  S   /UR                  S   UR                  S   /5        GMv     GM     UR                  5       $ )zGenerate a plot for the detected table grids on the PDF page.

Parameters
----------
table : camelot.core.Table
ax : matplotlib.axes.Axes (optional)

Returns
-------
fig : matplotlib.fig.Figure

r   r   )rc   cellsr&   plotlbltr%   rbrtr!   r"   rx   )r@   r   rowcells       r   gridPlotMethods.grid   s2    %$;;C99GGTWWQZ4twwqz4771:6NO::GGTWWQZ4twwqz4771:6NO88GGTWWQZ4twwqz4771:6NO;;;GGTWWQZ4twwqz4771:6NO   }}r   c           
         U R                   S:H  n[        X5      nU(       d  [        X5        UR                  [        R
                  " U R                  S   U R                  S   4U R                  S   U R                  S   -
  U R                  S   U R                  S   -
  SSS95        U(       d  [        XR                  5        UR                  5       $ )	zGenerate a plot for all table boundaries present on the PDF page.

Parameters
----------
table : camelot.core.Table
ax : matplotlib.axes.Axes (optional)

Returns
-------
fig : matplotlib.fig.Figure

rh   r   r   r	   r
   FrT   )r   r   )	rm   rc   rZ   r-   r.   r/   _bboxr   rx   )r@   r   _for_lattices      r   contourPlotMethods.contour  s     ||y0%$e 
QQ0AQ/AQ/	
 2{{+}}r   c                    [        X5      n[        X5        U R                  S:X  Ga  U R                  S    GH  nUR	                  5       nUR                  [        R                  " UR                  UR                  4UR                  UR                  -
  UR                  UR                  -
  SSS95        [        UR                  R                  5       S S9 GH%  nUR                  U   n[        U5      nUR!                  5       u  pxUR#                  5       u  p[%        ['        S U
5      5      n[%        ['        S	 U5      5      n[)        U5      nUR+                  Xg   US
-   [-        U5       SSSSS9  UR/                  Xg   /[-        U5      -  USSSSSSSSS9
  [1        ['        S U
5      5      nUR+                  US
-
  Xi   [-        U
5       SSSSS9  UR/                  UXi   /[-        U5      -  SSSSSSSSS9
  GM(     GM     UR7                  5       $ U R2                   HA  nUR/                  UR4                  UR4                  /UR                  UR                  /5        MC     UR7                  5       $ )zGenerate a plot for relevant textedges.

Parameters
----------
table : camelot.core.Table
ax : matplotlib.axes.Axes (optional)

Returns
-------
fig : matplotlib.fig.Figure

networknetwork_searchesrT   r#   rV   c                 4    U R                   * U R                  4$ N)r   r   )textlines    r   <lambda>&PlotMethods.textedge.<locals>.<lambda>M  s    8;;,)Dr   )keyc                     U R                   $ r   r   tls    r   r   r   S      RUUr   c                     U R                   $ r   )r   r   s    r   r   r   T  r   r      r"   r$      green)r+   r,   r*   r   solidr   oN   g?)r   r   r   markermarkeredgecolor	fillstyle
markersizer)   c                     U R                   $ r   r   r   s    r   r   r   l  s    r   r%   rU   fullr
   )rc   rZ   rm   rM   most_connected_textliner-   r.   r/   r   r   r   r   sorted_textline_to_alignmentskeysr   max_vmax_hlistmapr   r1   lenr|   r   
_textedgescoordrx   )r@   r   r   most_connected_tlr   
alignmentscoordsalignment_id_htls_halignment_id_vtls_vxsystop_hleft_vtes                   r   ri   PlotMethods.textedge-  s    %$%<<9$ ../AB$+$C$C$E!%%*--/@/C/CD),,/@/C/CC),,/@/C/CC#! !3388:DB ")!@!@!DJ04F,6,<,<,>)N,6,<,<,>)Nc"2E:;Bc"2E:;BGEGG.	u:,*2,4!"%   GG/03r7:%")"#"(/"&#$!   !%5u!=>FGG
.u:,*2,3!"$   GG/03r7:$")"#"(."(#$!  W CL }} &&288,ruubeen= '}}r   c                     [        X5      n/ n/ nU R                  S    H+  nUR                  US   5        UR                  US   5        M-     UR                  X#S5        UR	                  5       $ )zGenerate a plot for all line intersections present on the PDF page.

Parameters
----------
table : camelot.core.Table
ax : matplotlib.axes.Axes (optional)

Returns
-------
fig : matplotlib.fig.Figure

jointsr   r   ro)rc   parseappendr|   rx   )r@   r   x_coordy_coordr   s        r   jointPlotMethods.joint  sg     %$[[*ENN58$NN58$ + 	$'}}r   c                     [        X5      nU R                  u  p#U H%  nUR                  US   US   /US   US   /5        M'     U H%  nUR                  US   US   /US   US   /5        M'     UR                  5       $ )zGenerate a plot for all line segments present on the PDF page.

Parameters
----------
table : camelot.core.Table
ax : matplotlib.axes.Axes (optional)

Returns
-------
fig : matplotlib.fig.Figure

r   r	   r   r
   )rc   	_segmentsr|   rx   )r@   r   vertical
horizontalvhs         r   rj   PlotMethods.line  s     %$$AGGQqT1Q4L1Q41,/ AGGQqT1Q4L1Q41,/ }}r   c                    [        X5      nU R                  c  UR                  5       $ U R                  n[        US   5       H  u  p4US   nUS   nUS   n[        U5       H  u  pU[	        U5      S-
  :H  n
[        UU	SU SU 3SU
(       a  S	OS
U
(       a  SOSSS9  UR                  [        R                  " U	S   U-
  U	S   U-
  4U	S
   U	S   -
  S
U-  -   U	S   U	S   -
  S
U-  -   SSSS95        M     M     [        US   5       HY  u  p;[        UUS   SU 3SSSS9  [        UUS   SU 3SS
SS9  US    H#  nUR                  X/US   S   US   S   /S S!9  M%     M[     UR                  5       $ )"zGenerate a plot illustrating the steps of the network table search.

Parameters
----------
table : camelot.core.Table
ax : matplotlib.axes.Axes (optional)

Returns
-------
fig : matplotlib.fig.Figure
bbox_searches	max_h_gap	max_v_gap
iterationsr   rY   z/irT   r   r	      r   zbottom,left)r   r   r*   r3   r   r
   orangerK   F)r   r   r   col_searches	bbox_fullzbox body + header #r   top,left)r   r   r3   	bbox_bodyz
box body #cyanrL   cols_anchorsr   )r   )
rc   rM   rx   	enumerater   r:   r-   r.   r/   r|   )r@   r   rM   box_idbbox_searchr   r   r   	iterationr   final
col_search
col_anchors                r   network_table_search PlotMethods.network_table_search  s    %$&==?"++#,]?-K#LF#K0I#K0I$\2J#,Z#8	!S_q%88!xr)-#(aa#(Ra+ %%a9,d1g	.ABQ$q')A	M9Q$q')A	M9&"*"	 $9	 $M8 #,M.,I"JF;'%fX.$ ;'VH%( )8
 ,";/2";/2 "   9# #K: }}r    )r1   NNr   )__name__
__module____qualname____firstlineno____doc__ru   r1   staticmethodr   r   ri   r   rj   r   __static_attributes__r   r   r   re   re      s    +'&R"  4  B W Wr  ,  * I Ir   re   )
   )r'   r
   r   r      r   )r   pdfminer.layoutr   matplotlib.patchesr.   matplotlib.pyplotpyplotr_   rk   rl   utilsr   r   r   r   r:   rB   rR   rZ   rc   re   r   r   r   <module>r      s|    6 .(# H   & &F EP	H:&(K KU  Hs   A AA