
    i!=                         d Z ddlmZ 	 ddlmZ ddlmZ dZddl
mZ ddl
mZ dd	l
mZ dd
Z	 	 	 	 	 ddZd Zd Zd ZddZ G d d      Zy# e	$ r dZY @w xY w)z0Plotting functions usefull for visual debugging.    )LTTextLineVerticalNTF   )bbox_from_str)bbox_from_textlines)get_textline_coordsc                    | j                         \  }}| j                         \  }}| j                  t        ||d   |z
        t	        ||d   |z                | j                  t        ||d   |z
        t	        ||d   |z                y)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          C/home/agent/.local/lib/python3.12/site-packages/camelot/plotting.pyextend_axe_limr      s|    [[]FB[[]FBKKBQ&()3r47V3C+DEKKBQ&()3r47V3C+DE    c                    | j                  t        j                  |d   |d   f|d   |d   z
  |d   |d   z
  |||d             |j                  d      \  }}	|dk(  rt	        |d   |d         }
n&|d	k(  rt        |d   |d         }
nd
|d   |d   z   z  }
d	ddd}||   }|	dk(  rt	        |d   |d         }n&|	dk(  rt        |d   |d         }nd
|d   |d   z   z  }| j                  ||
||d||	t        |d             y)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:      sU   > LL!Wd1gGd1gGd1g	

 __S)NFFQa!	8	Qa!47T!W$%  (5HM(0Qa!	6	Qa!47T!W$%GG		+"E-  	r   c                     | j                         }|j                  |d| j                  d   d| j                  d   f       y)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                     | j                   rbddd}|j                         D ]I  \  }}| j                   |   xs g D ].  }t        |t        |      dj	                  ||      dddd	
       0 K yy)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": 	Iw!//8>B !!(+/66"+h 7  #&,
	 r   c           
      j   t        | j                        }| j                  D ]  }t        |t              rdnd}|j	                  t        j                  |j                  |j                  f|j                  |j                  z
  |j                  |j                  z
  |d              t        ||       y)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	

 2tr   c                     |'t        j                         }|j                  dd      }t        | |       t	        | |       |S )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UB5"%Ir   c                       e Zd ZdZddZddZedd       Zedd       Zedd       Z	edd       Z
edd	       Zedd
       Zy)PlotMethodsz!Classmethod for plotting methods.Nc                 $   t         st        d      |j                  dk(  r|dv rt        d|      |j                  dk7  r|dv rt        |j                   d|      t	        | |      }| |||      }|j                  |       y |||      S )a  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__zPlotMethods.__call__   s    . DEE<<9$)=%(MdX&VWW<<9$)9%<<. >thG  dD)eR(CKK!5"%%r   c                 R    t        ||      }t        ||       |j                         S )a  Generate 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)rp   r@   r   s      r   r1   zPlotMethods.text   s&     %$%}}r   c                 &   t        | |      }| j                  D ]f  }|D ]]  }|j                  rJ|j                  |j                  d   |j
                  d   g|j                  d   |j
                  d   g       |j                  rJ|j                  |j                  d   |j                  d   g|j                  d   |j                  d   g       |j                  rJ|j                  |j
                  d   |j                  d   g|j
                  d   |j                  d   g       |j                  s|j                  |j                  d   |j                  d   g|j                  d   |j                  d   g       ` i |j                         S )a  Generate 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"   rv   )r@   r   rowcells       r   gridzPlotMethods.grid   s@    %$;; 		PC P99GGTWWQZ4twwqz4771:6NO::GGTWWQZ4twwqz4771:6NO88GGTWWQZ4twwqz4771:6NO;;GGTWWQZ4twwqz4771:6NOP		P }}r   c           	         | j                   dk(  }t        | |      }|st        | |       |j                  t	        j
                  | j                  d   | j                  d   f| j                  d   | j                  d   z
  | j                  d   | j                  d   z
  dd             |st        || j                         |j                         S )	a  Generate 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

        rg   r   r   r	   r
   FrT   )r   r   )	rl   rc   rZ   r-   r.   r/   _bboxr   rv   )r@   r   _for_lattices      r   contourzPlotMethods.contour  s     ||y0%$eR 
QQ0AQ/AQ/	
 2u{{+}}r   c                    t        | |      }t        | |       | j                  dk(  r| j                  d   D ]  }|j	                         }|j                  t        j                  |j                  |j                  f|j                  |j                  z
  |j                  |j                  z
  dd             t        |j                  j                         d       D ]6  }|j                  |   }t        |      }|j!                         \  }}|j#                         \  }	}
t%        t'        d |
            }t%        t'        d	 |            }t)        |      }|j+                  ||   |d
z   t-        |       dddd       |j/                  ||   gt-        |      z  |dddddddd
       t1        t'        d |
            }|j+                  |d
z
  ||	   t-        |
       dddd       |j/                  |||	   gt-        |      z  dddddddd
       9  |j7                         S | j2                  D ]@  }|j/                  |j4                  |j4                  g|j                  |j                  g       B |j7                         S )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    | j                    | j                  fS N)r   r   )textlines    r   <lambda>z&PlotMethods.textedge.<locals>.<lambda>M  s    8;;,)D r   )keyc                     | j                   S r   r   tls    r   r   z&PlotMethods.textedge.<locals>.<lambda>S  
    RUU r   c                     | j                   S r   )r   r   s    r   r   z&PlotMethods.textedge.<locals>.<lambda>T  r   r      r"   r$      green)r+   r,   r*   r   solidr   oN   g?)r   r   r   markermarkeredgecolor	fillstyle
markersizer)   c                     | j                   S r   r   r   s    r   r   z&PlotMethods.textedge.<locals>.<lambda>l  s
     r   r%   rU   fullr
   )rc   rZ   rl   rM   most_connected_textliner-   r.   r/   r   r   r   r   sorted_textline_to_alignmentskeysr   max_vmax_hlistmapr   r1   lenry   r   
_textedgescoordrv   )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   rh   zPlotMethods.textedge-  s    %$%<<9$ ../AB B$+$C$C$E!%%*--/@/C/CD),,/@/C/CC),,/@/C/CC#! !3388:D 6B ")!@!@!DJ04F,6,<,<,>)NE,6,<,<,>)NEc"2E:;Bc"2E:;BGEGG~.	u:,*2,4!"%   GG/03r7:%")"#"(/"&#$!   !%5u!=>FGG
~.u:,*2,3!"$   GG/03r7:$")"#"(."(#$!  W6BL }} && >288,ruubeen=>}}r   c                     t        | |      }g }g }| j                  d   D ]*  }|j                  |d          |j                  |d          , |j                  ||d       |j	                         S )a	  Generate 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appendry   rv   )r@   r   x_coordy_coordr   s        r   jointzPlotMethods.joint  sp     %$[[* 	%ENN58$NN58$	% 	$'}}r   c                     t        | |      }| j                  \  }}|D ]$  }|j                  |d   |d   g|d   |d   g       & |D ]$  }|j                  |d   |d   g|d   |d   g       & |j                         S )a  Generate 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   	_segmentsry   rv   )r@   r   vertical
horizontalvhs         r   ri   zPlotMethods.line  s     %$$* 	0AGGQqT1Q4L1Q41,/	0 	0AGGQqT1Q4L1Q41,/	0}}r   c                    t        | |      }| j                  |j                         S | j                  }t        |d         D ]  \  }}|d   }|d   }|d   }t        |      D ]  \  }}	|t	        |      dz
  k(  }
t        ||	d| d| d|
rd	nd
|
rdndd       |j                  t        j                  |	d   |z
  |	d   |z
  f|	d
   |	d   z
  d
|z  z   |	d   |	d   z
  d
|z  z   ddd               t        |d         D ]a  \  }}t        ||d   d| ddd       t        ||d   d| dd
d       |d   D ]&  }|j                  ||g|d   d   |d   d   gd !       ( c |j                         S )"a  Generate 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   rv   	enumerater   r:   r-   r.   r/   ry   )r@   r   rM   box_idbbox_searchr   r   r   	iterationr   final
col_search
col_anchors                r   network_table_searchz PlotMethods.network_table_search  s    %$&==?"++#,]?-K#L 	FK#K0I#K0I$\2J#,Z#8 	4!S_q%88!xr)-#(aa#(Ra+ %%a9,d1g	.ABQ$q')A	M9Q$q')A	M9&"*"			8 #,M.,I"J 	FJ;'%fX.$ ;'VH%( )8 

 ,";/2";/2 "  
#	: }}r   )r1   NNr   )__name__
__module____qualname____doc__rt   r1   staticmethodr   r   rh   r   ri   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_   rj   rk   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