
    #j                        d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z# d dl$m%Z% d dl&m'Z' d d	l(m)Z) d d
l*m+Z+m,Z, d dl-m.Z.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?  ej@        eA          ZB G d de'          ZC G d deC          ZD e
de	ee5          ZE G d deCeeE                   ZF G d deFe5                   ZG G d deFe5                   ZH G d deFe5                   ZI G d deFe5                   ZJdS )    N)Sequence)BinaryIOClassVarGenericTextIOTypeVarcast)utils)ImageWriter)LAParamsLTAnnoLTCharLTComponentLTContainerLTCurveLTFigureLTImageLTItemLTLayoutContainerLTLineLTPageLTRectLTText	LTTextBoxLTTextBoxVerticalLTTextGroup
LTTextLineTextGroupElement)PDFColorSpace)PDFTextDevice)PDFValueError)PDFFontPDFUnicodeNotDefined)PDFGraphicStatePDFResourceManager)PDFPage)	PDFStream)AnyIOMatrixPathSegmentPointRectapply_matrix_ptapply_matrix_rectbbox2strencmake_compat_strmult_matrixc                   $   e Zd ZU eed<   eed<   	 	 d&dedededz  ddfd	Z	d
e
deddfdZd
e
ddfdZdedededdfdZdeddfdZdededdfdZdededededee         ddfdZdedededededed ed!edefd"Zdededefd#Zd$eddfd%ZdS )'PDFLayoutAnalyzercur_itemctm   Nrsrcmgrpagenolaparamsreturnc                 Z    t          j        | |           || _        || _        g | _        d S N)r    __init__r9   r:   _stackselfr8   r9   r:   s       Z/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/pdfminer/converter.pyr>   zPDFLayoutAnalyzer.__init__A   s0     	tW--- /1    pagec                     t          ||j                  \  }}}}ddt          ||z
            t          ||z
            f}t          | j        |          | _        d S )Nr   )r.   mediaboxabsr   r9   r5   )rA   rD   r6   x0y0x1y1rF   s           rB   
begin_pagezPDFLayoutAnalyzer.begin_pageL   sU    ,S$-@@RRq#b2g,,BG5t{H55rC   c                    | j         r)J t          t          | j                                         t          | j        t
                    s)J t          t          | j                                        | j        | j                            | j                   | xj	        dz  c_	        | 
                    | j                   d S )Nr7   )r?   strlen
isinstancer5   r   typer:   analyzer9   receive_layout)rA   rD   s     rB   end_pagezPDFLayoutAnalyzer.end_pageQ   s    ;55C$4$4 5 555$-00JJ#d4=6I6I2J2JJJ0=$M!!$-000qDM*****rC   namebboxmatrixc                     | j                             | j                   t          ||t	          || j                            | _        d S r=   )r?   appendr5   r   r2   r6   )rA   rU   rV   rW   s       rB   begin_figurezPDFLayoutAnalyzer.begin_figureY   s=    4=))) t[-J-JKKrC   _c                 
   | j         }t          | j         t                    s)J t          t	          | j                                         | j                                        | _         | j                             |           d S r=   )r5   rP   r   rN   rQ   r?   popadd)rA   r[   figs      rB   
end_figurezPDFLayoutAnalyzer.end_figure]   sj    m$-22LLCT]8K8K4L4LLL2))#rC   streamc                 :   t          | j        t                    s)J t          t	          | j                                        t          ||| j        j        | j        j        | j        j        | j        j	        f          }| j        
                    |           d S r=   )rP   r5   r   rN   rQ   r   rH   rI   rJ   rK   r^   )rA   rU   ra   items       rB   render_imagezPDFLayoutAnalyzer.render_imagec   s    $-22LLCT]8K8K4L4LLL2]t}/1A4=CST
 

 	$rC   gstatestrokefillevenoddpathc                     d                     d D                       }|dd         dk    rdS |                    d          dk    rct          j        d|          D ]K}|                    d          |                    d                   }                     |||||           LdS fdD             }	 fd	|	D             }
d
 D             } fdD             }d t          ||d          D             }t          |          dk    rA|dd         dk    r3|
d         |
d         k    r!|dd         dz   }|
	                                 |dv rUt          |j        |
d         |
d         ||||j        |j        ||j        
  
        } j                            |           dS |dv r|
\  \  }}\  }}\  }}\  }}}|
d         |
d         k    }||k    o||k    o||k    o||k    p||k    o||k    o||k    o||k    }|rZ|rXt#          |j        g |
d         |
d         R ||||j        |j        ||j        	  	        } j                            |           dS t%          |j        |
||||j        |j        ||j        	  	        } j                            |           dS t%          |j        |
||||j        |j        ||j        	  	        } j                            |           dS )z@Paint paths described in section 4.4 of the PDF reference manual c              3   &   K   | ]}|d          V  dS )r   N ).0xs     rB   	<genexpr>z/PDFLayoutAnalyzer.paint_path.<locals>.<genexpr>u   s&      ++!++++++rC   Nr7   mzm[^m]+r   c                     g | ];}t          t          |d          dk    r
|dd         nd          dd                   <S )r   hN)r	   r+   )rn   pri   s     rB   
<listcomp>z0PDFLayoutAnalyzer.paint_path.<locals>.<listcomp>   sT       IJUadckkAbccFFtAwrss|DD  rC   c                 :    g | ]}t          j        |          S rm   )r-   r6   )rn   ptrA   s     rB   rv   z0PDFLayoutAnalyzer.paint_path.<locals>.<listcomp>   s%    CCCR?48R00CCCrC   c                 8    g | ]}t          |d                    S )r   )rN   )rn   	operations     rB   rv   z0PDFLayoutAnalyzer.paint_path.<locals>.<listcomp>   s$    AAAyYq\**AAArC   c           
      p    g | ]2}fd t          |ddd         |ddd         d          D             3S )c           	      x    g | ]6\  }}t          j        t          |          t          |          f          7S rm   )r-   r6   float)rn   operand1operand2rA   s      rB   rv   z;PDFLayoutAnalyzer.paint_path.<locals>.<listcomp>.<listcomp>   sJ       *( $DHuXh.PQQ  rC   r7   N   Fstrict)zip)rn   rz   rA   s     rB   rv   z0PDFLayoutAnalyzer.paint_path.<locals>.<listcomp>   sv     " " "    .1!!$Q$14a4/ / /  " " "rC   c                 F    g | ]\  }}t          t          |g|R           S rm   )r	   r*   )rn   oru   s      rB   rv   z0PDFLayoutAnalyzer.paint_path.<locals>.<listcomp>   s>          Aq [1'q''**     rC   Fr      rt   lhrs   >   mlmlh)original_pathdashing_style>   mlllhmllll   r   )joincountrefinditerstartend
paint_pathr   rO   r]   r   	linewidthscolorncolordashr5   r^   r   r   )rA   re   rf   rg   rh   ri   shaperq   subpathraw_ptspts	operatorstransformed_pointstransformed_pathlinerH   rI   rJ   rK   x2y2x3y3r[   is_closed_loophas_square_coordinatesrectcurves   `    `                      rB   r   zPDFLayoutAnalyzer.paint_pathl   s    ++d+++++!9 D[[!![E22 H HqwwqzzAEE!HH45gwGGGGH H   NR  G DCCC7CCCCAADAAAI" " " " "&" " "   	+=eLLL      5zzA~~%*"4"4RCF9J9Jcrc
S(			%%
 $FFMM"2"(+   !!$''''',,,<?9R(2rHRhr2!$Q3q6!1"HCrCbBhC28*GBhE28EbER2X ' " -&< -!(*#a&*3q6**(
 
D M%%d+++++#((
 
E M%%e,,,,,$MM$K
 
 !!%(((((rC   fontfontsizescalingrisecidncsgraphicstatec	                    	 |                     |          }	t          |	t                    s$J t          t          |	                                n&# t          $ r |                     ||          }	Y nw xY w|                    |          }
|                    |          }t          ||||||	|
|||
  
        }| j	        
                    |           |j        S r=   )	to_unichrrP   rN   rQ   r#   handle_undefined_char
char_width	char_dispr   r5   r^   adv)rA   rW   r   r   r   r   r   r   r   text	textwidthtextdisprc   s                rB   render_charzPDFLayoutAnalyzer.render_char   s    	9>>#&&DdC((99#d4jj//99(9# 	9 	9 	9--dC88DDD	9OOC((	>>#&&
 
 	$xs   AA  A43A4c                 N    t                               d|d|           d| dS )Nzundefined: , z(cid:))logdebug)rA   r   r   s      rB   r   z'PDFLayoutAnalyzer.handle_undefined_char
  s3    		111#11222s~~~rC   ltpagec                     d S r=   rm   rA   r   s     rB   rS   z PDFLayoutAnalyzer.receive_layout  s    rC   r7   N)__name__
__module____qualname__r   __annotations__r)   r%   intr   r>   r&   rL   rT   rN   r,   rZ   r`   r'   rd   r$   boolr   r*   r   r"   r}   r   r   r   r   rS   rm   rC   rB   r4   r4   =   sZ        	KKK
 $(		2 	2#	2 	2 T/		2
 
	2 	2 	2 	26w 6V 6 6 6 6 6
+W + + + + +L LD L& LT L L L LC D       i  D        {){) {) 	{)
 {) {#{) 
{) {) {) {)z  	
     & 
   B'      V       rC   r4   c            	       N    e Zd Z	 	 ddedededz  ddfdZdeddfd	Zdefd
Z	dS )PDFPageAggregatorr7   Nr8   r9   r:   r;   c                 P    t                               | |||           d | _        d S N)r9   r:   )r4   r>   resultr@   s       rB   r>   zPDFPageAggregator.__init__  s,     	""4("SSS%)rC   r   c                     || _         d S r=   r   r   s     rB   rS   z PDFPageAggregator.receive_layout  s    rC   c                 "    | j         J | j         S r=   r   rA   s    rB   
get_resultzPDFPageAggregator.get_result  s    {&&&{rC   r   )
r   r   r   r%   r   r   r>   r   rS   r   rm   rC   rB   r   r     s         $(	* *#* * T/	*
 
* * * *V     F      rC   r   IOTypec                   \    e Zd Z	 	 	 ddedededededz  d	dfd
Ze	de
d	efd            ZdS )PDFConverterutf-8r7   Nr8   outfpcodecr9   r:   r;   c                     t                               | |||           || _        || _        |                     | j                  | _        d S r   )r4   r>   r   r   _is_binary_streamoutfp_binary)rA   r8   r   r   r9   r:   s         rB   r>   zPDFConverter.__init__)  sM     	""4("SSS"

 224:>>rC   c                     dt          | dd          v rdS t          | d          rdS t          | t          j                  rdS t          | t          j        t          j        f          rdS dS )z"Test if an stream is binary or notbmoderk   TF)getattrhasattrrP   ioBytesIOStringIO
TextIOBase)r   s    rB   r   zPDFConverter._is_binary_stream6  su     '%,,,,4UF## 	5rz** 	4R];<< 	5trC   )r   r7   N)r   r   r   r%   r   rN   r   r   r>   staticmethodr(   r   r   rm   rC   rB   r   r   (  s        
 $(? ?#? ? 	?
 ? T/? 
? ? ? ?  4    \  rC   r   c                        e Zd Z	 	 	 	 	 ddedededed	edz  d
ede	dz  ddf fdZ
deddfdZdeddfdZdededdfdZdededededee         ddfdZ xZS )TextConverterr   r7   NFr8   r   r   r9   r:   
showpagenoimagewriterr;   c                 p    t                                          |||||           || _        || _        d S )Nr   r9   r:   )superr>   r   r   )	rA   r8   r   r   r9   r:   r   r   	__class__s	           rB   r>   zTextConverter.__init__G  s=     	%uVhWWW$&rC   r   c                 &   t          j        || j        d          }| j        rAt	          t
          | j                                      |                                           d S t	          t          | j                                      |           d S )Nignore)
r
   compatible_encode_methodr   r   r	   r   r   writeencoder   rA   r   s     rB   
write_textzTextConverter.write_textU  sw    -dDJII 	14:&&,,T[[]];;;;;$$**400000rC   r   c                      dt           dd f fd j        r                     d|j         d            |                                d           d S )Nrc   r;   c                    t          | t                    r| D ]} |           n<t          | t                    r'                    |                                            t          | t
                    r                    d           d S t          | t                    r#j        j                            |            d S d S d S )N
)	rP   r   r   r   get_textr   r   r   export_image)rc   childrenderrA   s     rB   r   z,TextConverter.receive_layout.<locals>.render]  s    $,, 1! " "EF5MMMM"D&)) 1000$	** 4%%%%%D'** 4t/?/K --d333334 4/K/KrC   zPage r   )r   r   r   pageidrA   r   r   s   ` @rB   rS   zTextConverter.receive_layout\  s    		4 		4D 		4 		4 		4 		4 		4 		4 		4 ? 	7OO5FM555666vrC   rU   ra   c                 P    | j         t                              | ||           d S d S r=   )r   r   rd   )rA   rU   ra   s      rB   rd   zTextConverter.render_imagep  s1    '%%dD&99999 ('rC   re   rf   rg   rh   ri   c                     d S r=   rm   )rA   re   rf   rg   rh   ri   s         rB   r   zTextConverter.paint_patht  s	     	rC   )r   r7   NFN)r   r   r   r%   r(   rN   r   r   r   r   r>   r   r   rS   r'   rd   r$   r   r*   r   __classcell__)r   s   @rB   r   r   F  sj       
 $( *.' '#' ' 	'
 ' T/' ' !4'' 
' ' ' ' ' '1s 1t 1 1 1 1V     (: :i :D : : : :  	
  {# 
       rC   r   c                   2   e Zd ZU dddddddZeeeef                  ed<   d	dd
Zeeeef                  ed<   	 	 	 	 	 	 	 	 	 	 	 	 d@de	de
dedededz  dededededededz  ded eeef         dz  d!eeef         dz  d"dfd#Zd$ed"dfd%ZdAd&ZdAd'Zd$ed"dfd(Zd)ed*ed+ed,ed-ed.ed"dfd/Zd)ed*ed0ed"dfd1Zd0ed*ed+ed,ed-ed.ed"dfd2Zd)ed$ed+ed,ed3ed"dfd4Z	 dBd)ed*ed+ed,ed-ed.ed6ed"dfd7Zd)ed"dfd8Zd$ed9ed:ed"dfd;ZdAd<Zd=ed"dfd>Z dAd?Z!dS )CHTMLConverteryellowmagentacyanredblackgray)figuretextlinetextbox	textgroupr   rD   RECT_COLORSblue)r  charTEXT_COLORSr   r7   N      ?normalT2   r   r8   r   r   r9   r:   scale	fontscale
layoutmoder   
pagemarginr   r   rect_colorstext_colorsr;   c                     t                               | |||||           | j        r| j        st	          d          | j        s| j        rt	          d          |ddi}|ddd}|| _        || _        || _        |	| _        |
| _	        || _
        || _        || _        |r>| j                            | j                   | j                            | j                   | j	        | _        d | _        g | _        |                                  d S )Nr   )Codec is required for a binary I/O outputz1Codec must not be specified for a text I/O outputr  r	  r
  )r   rD   )r   r>   r   r   r!   r  r  r  r   r  r   r  r  updater  r  _yoffset_font
_fontstackwrite_header)rA   r8   r   r   r9   r:   r  r  r  r   r  r   r   r  r  s                  rB   r>   zHTMLConverter.__init__  sH   " 	 	 	
 	
 	
  	MTZ 	M KLLL  	UTZ 	U STTT!7+K$+V<<K
"$$$&&& 	6##D$4555##D$4555#/3
:<rC   r   c                     | j         rGt          t          | j                                      |                    | j                              d S t          t          | j                                      |           d S r=   r   r	   r   r   r   r   r   r   s     rB   r   zHTMLConverter.write  f    : 	14:&&,,T[[-D-DEEEEE$$**400000rC   c                     |                      d           | j        rd| j         d}nd}|                      |           |                      d           d S )Nz<html><head>
z<<meta http-equiv="Content-Type" content="text/html; charset=">
z5<meta http-equiv="Content-Type" content="text/html">
z</head><body>
)r   r   )rA   ss     rB   r"  zHTMLConverter.write_header  sp    

#$$$: 	I,:, , , A
 IA

1

$%%%%%rC   c                     d t          d| j                  D             }dd                    |           d}|                     |           |                     d           d S )Nc                      g | ]}d | d| dS )z
<a href="#">z</a>rm   )rn   is     rB   rv   z.HTMLConverter.write_footer.<locals>.<listcomp>  s,    OOOA/1/////OOOrC   r7   z/<div style="position:absolute; top:0px;">Page: r   </div>
z</body></html>
)ranger9   r   r   )rA   
page_linksr(  s      rB   write_footerzHTMLConverter.write_footer  ss    OOq$+9N9NOOO
5YYz**5 5 5 	
 	

1

%&&&&&rC   c                 J    |                      t          |                     d S r=   )r   r0   r   s     rB   r   zHTMLConverter.write_text  s     

3t99rC   colorborderwidthro   ywrs   c                     | j                             |          }|Td| d| d|| j        z   d| j        |z
  | j        z   d|| j        z   d|| j        z   d}|                     |           d S d S )Nz(<span style="position:absolute; border:  zpx solid; left:px; top:
px; width:px; height:zpx;"></span>
)r  getr  r  r   )	rA   r2  r3  ro   r4  r5  rs   color2r(  s	            rB   
place_rectzHTMLConverter.place_rect  s     !%%e,,9!9 9$/9 9DJ9 9 )TZ79 9 TZ	9 9
 dj.9 9 9  JJqMMMMM rC   rc   c                 b    |                      |||j        |j        |j        |j                   d S r=   )r=  rH   rK   widthheight)rA   r2  r3  rc   s       rB   place_borderzHTMLConverter.place_border  s,    {DGTWdj$+VVVVVrC   c                 
   | j         {| j                             |          }dt          |           d| d|| j        z   d| j        |z
  | j        z   d|| j        z   d|| j        z   d}|                     |           d S d S )Nz
<img src="z
" border="z!" style="position:absolute; left:r8  zpx;" width="
" height="" />
)r   r   r0   r  r  r   )	rA   rc   r3  ro   r4  r5  rs   rU   r(  s	            rB   place_imagezHTMLConverter.place_image  s     '#0066D2SYY 2 2+ 2 2DJ2 2 )TZ72 2 dj.	2 2
 tz>2 2 2  JJqMMMMM ('rC   sizec           	      .   | j                             |          }|xd| d|| j        z   d| j        |z
  | j        z   d|| j        z  | j        z   d	}|                     |           |                     |           |                     d           d S d S )Nz&<span style="position:absolute; color:; left:r8  zpx; font-size:px;"></span>
)r  r;  r  r  r  r   r   )rA   r2  r   ro   r4  rF  r<  r(  s           rB   
place_textzHTMLConverter.place_text  s     !%%e,,GG GDJG G )TZ7G G "DJ.?	G G G  JJqMMMOOD!!!JJ{##### rC   Falsewriting_modec                     | j                             | j                   d | _        d| d| d| d|| j        z   d| j        |z
  | j        z   d|| j        z   d|| j        z   d}|                     |           d S )	Nz'<div style="position:absolute; border: r7  zpx solid; writing-mode:rH  r8  r9  r:  rI  )r!  rY   r   r  r  r   )	rA   r2  r3  ro   r4  r5  rs   rM  r(  s	            rB   	begin_divzHTMLConverter.begin_div#  s     	tz***
,, ,*, ,(, , 
N, , MA%3	, ,
 ^, , $*n, , , 	
 	

1rC   c                     | j         |                     d           | j                                        | _         |                     d           d S )N</span>z</div>)r   r   r!  r]   )rA   r2  s     rB   end_divzHTMLConverter.end_div:  sI    :!JJy!!!_((**


8rC   fontnamer   c                 "   ||f}|| j         k    rj| j         |                     d           |                    d          d         }|                     d| d|| j        z  | j        z   d           || _         |                     |           d S )NrQ  +z<span style="font-family: z; font-size:zpx">)r   r   splitr  r  r   )rA   r   rS  r   r   fontname_without_subset_tags         rB   put_textzHTMLConverter.put_text@  s    (#4:z%

9%%%*2..*=*=b*A'JJJ ;J J%
2T^CJ J J  
 DJrC   c                 0    |                      d           d S )Nz<br>r   r   s    rB   put_newlinezHTMLConverter.put_newlineO  s    

6rC   r   c                      dt           t          z  dd f fddt          dd f fd |            xj         j        z  c_        d S )Nrc   r;   c                     t          | t                    r'                    dd|            | D ]} |           d S d S )Nr  r7   )rP   r   rA  rc   r   rA   
show_groups     rB   r`  z0HTMLConverter.receive_layout.<locals>.show_groupS  sc    $,, &!!+q$777! & &EJu%%%%& && &rC   c           
      d   t          | t                    rxj        | j        z  c_                            dd|            j        rS                    dj        | j        z
  j        z                                  d| j         d| j         d           | D ]} |           | j	        | j	        D ]} |           d S d S t          | t                    r                    dd|            d S t          | t                    rU                    dd| j        | j        | j        | j                   | D ]} |                               d           d S t          | t"                    r0                    | d| j        | j        | j        | j                   d S j        d	k    rt          | t(                    r)                    d
d|            | D ]} |           d S t          | t*                    ra                    dd|                                dt/          | j        dz             | j        | j        d           | D ]} |           d S t          | t2                    rS                    dd|                                d|                                 | j        | j        | j                   d S d S t          | t(                    r3| D ]} |           j        dk    r                                 d S d S t          | t*                    rh                    dd| j        | j        | j        | j        |                                            | D ]} |                               d           d S t          | t2                    rDt=          | j                  }                     |                                 || j                   d S t          | tB                    r)"                    |                                            d S d S )NrD   r7   z*<div style="position:absolute; top:%dpx;">z	<a name="z">Page z</a></div>
r   r  exactr  r     r  loose)#rP   r   r  rK   rA  r   r   r  r   groupsr   r   rO  rH   r?  r@  rR  r   rE  r  r   r   rK  rN   indexr   r   rF  r\  get_writing_moder1   rS  rY  r   r   )rc   r   grouprS  r   rA   r`  s       rB   r   z,HTMLConverter.receive_layout.<locals>.renderY  s   $'' H1(!!&!T222? JJD MDG3tzAD D   JJQDKQQQQQ   " " "EF5MMMM;*!% * *"
5)))) +** *D'** 81!!'1d33333D(++ 61xDGTWdj$+VVV! " "EF5MMMMX&&&&&D'** 11  q$'47DJTTTTTG++dJ// %%j!T:::!% & &u& &i00 %%iD999OO!DJN++   "& & &u& &f-- %%fa666OO	      D*-- 1! " "EF5MMMM?g--$$&&&&& .-D),, 1GGJK))++   " " "EF5MMMMY'''''D&)) 1*4=99dmmooxCCCCCD&)) 1000001 1rC   )r   r   r   r  r  rA   r   r   r`  s   ` @@rB   rS   zHTMLConverter.receive_layoutR  s    	&[+;; 	& 	& 	& 	& 	& 	& 	& 	&J	1 J	1D J	1 J	1 J	1 J	1 J	1 J	1 J	1 J	1X 	v(rC   c                 .    |                                   d S r=   r0  r   s    rB   closezHTMLConverter.close      rC   )r   r7   Nr7   r  r  Tr  Nr   NNr;   N)rL  )"r   r   r   r  r   dictrN   r   r  r%   r(   r   r   r}   r   r   r>   r   r"  r0  r   r=  r   rA  r   rE  rK  rO  rR  rY  r\  r   rS   rl  rm   rC   rB   r  r    s,        - -K$sCx.)    - -K$sCx.)    $("*.-1-13 3#3 3 	3
 3 T/3 3 3 3 3 3 !4'3 3 #s(^d*3 #s(^d*3  
!3 3 3 3j1# 1$ 1 1 1 1
& 
& 
& 
&' ' ' 's t      	
    
   *W# WC W{ Wt W W W W  	
    
   *$$ $ 	$
 $ $ 
$ $ $ $: $   	
     
   .S T    S C 5 T       T)V T) T) T) T) T)l     rC   r  c                       e Zd Z ej        d          Z	 	 	 	 	 ddededed	e	d
e
dz  dedz  deddfdZdeddfdZddZddZdeddfdZdeddfdZddZdS )XMLConverterz[ ---]r   r7   NFr8   r   r   r9   r:   r   stripcontrolr;   c                     t                               | |||||           | j        | j         k    rt	          d          || _        || _        |                                  d S )Nr   r  )r   r>   r   r   r!   r   rr  r"  )rA   r8   r   r   r9   r:   r   rr  s           rB   r>   zXMLConverter.__init__  s~     	 	 	
 	
 	
 TZ00 KLLL&(rC   r   c                     | j         rGt          t          | j                                      |                    | j                              d S t          t          | j                                      |           d S r=   r$  r   s     rB   r   zXMLConverter.write  r%  rC   c                     | j         r|                     d| j          d           n|                     d           |                     d           d S )Nz<?xml version="1.0" encoding="z" ?>
z<?xml version="1.0" ?>
z<pages>
r   r   r   s    rB   r"  zXMLConverter.write_header  sZ    : 	3JJJ
JJJKKKKJJ1222

;rC   c                 0    |                      d           d S )Nz	</pages>
r[  r   s    rB   r0  zXMLConverter.write_footer  s    

<     rC   c                     | j         r| j                            d|          }|                     t	          |                     d S Nrk   )rr  CONTROLsubr   r0   r   s     rB   r   zXMLConverter.write_text  sB     	.<##B--D

3t99rC   r   c                 d     dt           dd f fddt           dd f fd |           d S )Nrc   r;   c                 j   t          | t                    r5                    d| j         dt	          | j                   d           d S t          | t                    rR                    dt	          | j                   d           | D ]} |                               d           d S d S )N<textbox id="" bbox="rD  z<textgroup bbox="r'  z</textgroup>
)rP   r   r   rf  r/   rV   r   r_  s     rB   r`  z/XMLConverter.receive_layout.<locals>.show_group  s    $	** -

SDJSS8K8KSSS     D+.. -

Hx	/B/BHHHIII! & &EJu%%%%

+,,,,,	- -rC   c                 	   t          | t                    rd| j         dt          | j                   d| j         d}                    |           | D ]} |           | j        ?                    d           | j        D ]} |                               d                               d           d S t          | t                    r7d| j	         dt          | j                   d	}                    |           d S t          | t                    r7d
| j	         dt          | j                   d	}                    |           d S t          | t                    rLd| j	         dt          | j                   d|                                  d}                    |           d S t          | t                    r\d| j         dt          | j                   d}                    |           | D ]} |                               d           d S t          | t                    rR                    dt          | j                   d           | D ]} |                               d           d S t          | t                     rxd}t          | t"                    rd}d| j         dt          | j                   d| d}                    |           | D ]} |                               d           d S t          | t&                    rdt)          | j                   dt          | j                   d| j        j         d| j        j         d| j        dd}                    |                               |                                                                d           d S t          | t8                    r-                    d|                                  d           d S t          | t:                    rj        Rj                            |           }                    d t)          |           d!| j          d"| j!         d	           d S                     d#| j          d"| j!         d	           d S tE          tG          d$| f                    )%Nz
<page id="r  z
" rotate="r'  z	<layout>
z
</layout>
z</page>
z<line linewidth="rD  z<rect linewidth="z<curve linewidth="z" pts="z"/>
z<figure name="z
</figure>
z<textline bbox="z</textline>
rk   z wmode="vertical"r~  "z>
z</textbox>
z<text font="z" colourspace="z" ncolour="z" size="z.3fr+  z</text>
z<text>z<image src="z	" width="rC  z<image width="	Unhandled)$rP   r   r   r/   rV   rotater   re  r   r   r   r   get_ptsr   rU   r   r   r   rf  r   r0   rS  r   r   r   rF  r   r   r   r   r   r   r?  r@  AssertionErrorrN   )	rc   r(  r   rh  wmoderU   r   rA   r`  s	         rB   r   z+XMLConverter.receive_layout.<locals>.render  s   $'' U?1 1 1%di001 1#{1 1 1 
 

1! " "EF5MMMM;*JJ|,,,!% * *"
5))))JJ}---

;'''''D&)) F?9"&.9 9%di009 9 9 
 

1D&)) ??9"&.9 9%di009 9 9 
 

1D'** 8?2"&.2 2%di002 2 !LLNN2 2 2  

1D(++ 0?QTYQQ8K8KQQQ

1! " "EF5MMMM

=)))))D*-- *?

Ghty.A.AGGGHHH! " "EF5MMMM

?+++++D),, %?d$566 0/EXDJXX8K8KXXeXXX

1! " "EF5MMMM

>*****D&)) ?/ /// /%di00/ / %)HM/ / !% 1 8	/ /
 "Y./ / /  

1000

;'''''D&)) ?

>DMMOO>>>?????D'** ?#/+88>>DJJ7 #D		7 7"&*7 7 $(;7 7 7     JJRRRt{RRR     %S+t)<%=%=>>>rC   r   ri  s   ` @@rB   rS   zXMLConverter.receive_layout  s    		-V 		- 		- 		- 		- 		- 		- 		- 		-W	? W	?D W	? W	? W	? W	? W	? W	? W	? W	?r 	vrC   c                 .    |                                   d S r=   rk  r   s    rB   rl  zXMLConverter.closeF  rm  rC   )r   r7   NNFrn  )r   r   r   r   compilerz  r%   r(   rN   r   r   r   r   r>   r   r"  r0  r   r   rS   rl  rm   rC   rB   rq  rq    sP       bj899G $(*." #  	
  T/ !4'  
   61# 1$ 1 1 1 1       ! ! ! !s t    
eV e e e e eN     rC   rq  c                       e Zd ZdZ ej        d          Z	 	 	 	 ddeded	e	d
e
dedz  defdZdede	fdZde	ddfdZddZddZde	ddfdZddZdeddfdZddZdS )HOCRConverterzKExtract an hOCR representation from explicit text information within a PDF.z[\x00-\x08\x0b-\x0c\x0e-\x1f]utf8r7   NFr8   r   r   r9   r:   rr  c                     t                               | |||||           || _        d| _        |                                  d S )Nr   F)r   r>   rr  within_charsr"  )rA   r8   r   r   r9   r:   rr  s          rB   r>   zHOCRConverter.__init__]  s]     	 	 	
 	
 	
 )!rC   rV   r;   c                     |\  }}}}t          |          }t          | j        d         |z
            }t          |          }t          | j        d         |z
            }	d| d| d| d|	 S )Nr   zbbox r7  )r   	page_bbox)
rA   rV   in_x0in_y0in_x1in_y1out_x0out_y0out_x1out_y1s
             rB   	bbox_reprzHOCRConverter.bbox_reprr  s~    '+$ueUT^A&.//UT^A&.//:v::::::&:::rC   r   c                     | j         rI|                    | j                   }t          t          | j                                      |           d S t          t          | j                                      |           d S r=   )r   r   r	   r   r   r   r   )rA   r   encoded_texts      rB   r   zHOCRConverter.write{  sk    : 	1;;tz22L4:&&,,\:::::$$**400000rC   c                    | j         r|                     d| j          d           n|                     d           |                     d           |                     d           |                     d           |                     d           |                     d           |                     d	           |                     d
           d S )NzL<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en' charset=''>
zD<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
z<head>
z<title></title>
zE<meta http-equiv='Content-Type' content='text/html;charset=utf-8' />
zA<meta name='ocr-system' content='pdfminer.six HOCR Converter' />
zR  <meta name='ocr-capabilities' content='ocr_page ocr_block ocr_line ocrx_word'/>
z</head>
z<body>
rv  r   s    rB   r"  zHOCRConverter.write_header  s   : 	JJE48JE E E   
 JJW   	

:

&'''

T	
 	
 	
 	

P	
 	
 	
 	

C	
 	
 	
 	

;

:rC   c                 Z    |                      d           |                      d           d S )Nz0<!-- comment in the following line to debug -->
zD<!--script src='https://unpkg.com/hocrjs'></script--></body></html>
r[  r   s    rB   r0  zHOCRConverter.write_footer  s8    

FGGG

S	
 	
 	
 	
 	
rC   c                 t    | j         r| j                            d|          }|                     |           d S ry  )rr  rz  r{  r   r   s     rB   r   zHOCRConverter.write_text  s<     	.<##B--D

4rC   c                 V   t          | j                  dk    rd}d| j        v rd}d| j        v r|dz  }|                     d| j         d| j         d	| d
|                     | j                   d| j         d| j         d| j                                         d           d| _        d S )Nr   rk   Italiczfont-style: italic; Boldzfont-weight: bold; z<span style='font:"z"; font-size:z; z' class='ocrx_word' title='z	; x_font z
; x_fsize '>rQ  F)	rO   working_textworking_fontr   working_sizer  working_bboxstripr  )rA   bold_and_italic_styless     rB   
write_wordzHOCRConverter.write_word  s   t !!A%%%'"4,,,)?&***&*??&JJ6t'8 6 6!.6 6)6 6 ..):;;	6 6
 +6 6  ,6 6 $**,,6 6 6	 	 	 "rC   r   c                 @     dt           dd f fd |           d S )Nrc   r;   c                 z   j         r)t          | t                    r                                 t          | t                    rl| j        _                            d| j         d	                    | j                   d           | D ]} |                               d           d S t          | t                    rX                    d	                    | j                   d           | D ]} |                               d           d S t          | t                    r`                    d| j         d	                    | j                   d           | D ]} |                               d           d S t          | t                    rj         sFd	_         |                                 _        | j        _        | j        _        | j        _        d S t+          |                                                                           d
k    r=                                                     |                                            d S j        d         | j        d         k    s j        | j        k    sj        | j        k    r8                                 | j        _        | j        _        | j        _        xj        |                                 z  c_        j        d
         j        d         | j        d         j        d         f_        d S d S )Nz<div class='ocr_page' id='z	' title='r  r-  z<span class='ocr_line' title='r  rJ  z<div class='ocr_block' id='Tr   r7   r   r   )r  rP   r   r  r   rV   r  r   r   r  r   r   rf  r   r   r  r  rS  r  rF  r  rO   r  )rc   r   
child_liner   rA   s      rB   r   z,HOCRConverter.receive_layout.<locals>.render  s]     "Zf%=%= "!!!$'' 6!%

>;> > #nnTY77> > >   " " "EF5MMMM

:&&&&&D*-- +

RT^^DI5N5NRRR   #' ' 'JF:&&&&

;'''''D),, $

>:> > #nnTY77> > >   " " "EF5MMMM

:&&&&&D&)) ( (,D%(,D%(,	D%(,D%(,	D%%%..0011Q66OO%%%JJt}}///// )!,	!<<,==,	99))),0I),0M),0I)%%8%%)!,)!,	!)!,	)D%%%+ rC   r  r   s   ` @rB   rS   zHOCRConverter.receive_layout  sI    9	 9	D 9	 9	 9	 9	 9	 9	 9	v 	vrC   c                 .    |                                   d S r=   rk  r   s    rB   rl  zHOCRConverter.close  rm  rC   )r  r7   NFrn  )r   r   r   __doc__r   r  rz  r%   r(   rN   r   r   r   r>   r,   r  r   r"  r0  r   r  r   rS   rl  rm   rC   rB   r  r  J  sl       UU  bj9::G $(" #  	
  T/    *;d ;s ; ; ; ;1# 1$ 1 1 1 1   2
 
 
 
s t    
" " " "&<V < < < < <|     rC   r  )Kr   loggingr   collections.abcr   typingr   r   r   r   r   r	   pdfminerr
   pdfminer.imager   pdfminer.layoutr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   pdfminer.pdfcolorr   pdfminer.pdfdevicer    pdfminer.pdfexceptionsr!   pdfminer.pdffontr"   r#   pdfminer.pdfinterpr$   r%   pdfminer.pdfpager&   pdfminer.pdftypesr'   pdfminer.utilsr(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   	getLoggerr   r   r4   r   r   r   r   r  rq  r  rm   rC   rB   <module>r     s%   				  				 $ $ $ $ $ $                      & & & & & &                                         * , + + + + + , , , , , , 0 0 0 0 0 0 : : : : : : : : B B B B B B B B $ $ $ $ $ $ ' ' ' ' ' '                          g!!R R R R R R R Rj    )   & 
68U	3	3    $gfo   <6 6 6 6 6L' 6 6 6rj j j j jL' j j jZ	[ [ [ [ [<& [ [ [|n n n n nL' n n n n nrC   