
    Fg                        S SK r S SKrS SKJr  S SKJrJrJrJrJ	r	J
r
JrJr  S SKrS SKJr  S SKJr  S SKJrJrJr  S SKJrJr  S SKJrJr  S S	KJrJr  S S
K J!r!J"r"J#r#J$r$J%r%J&r&  S SK'J(r(  S SK)J*r*J+r+J,r,J-r-J.r.J/r/J0r0  S SK1J2r2J3r3  S SK4J5r5J6r6J7r7J8r8J9r9J:r:J;r;J<r<  S SK=J>r>J?r?J@r@JArAJBrBJCrCJDrDJErE  \ R                  " \G5      rH " S S\5      rI " S S\5      rJ\6" S5      rK\6" S5      rL\6" S5      rM\6" S5      rN\6" S5      rO " S S5      rP\\Q\
\Q\Q\Q4   \
\Q\Q\Q\Q4   4   rR " S S5      rS " S S5      rT " S S \9\\7\,4      5      rU\:\,   rV  " S! S"5      rWg)#    N)BytesIO)DictListMappingOptionalSequenceTupleUnioncast)settings)
safe_float)CMapCMapBaseCMapDB)PREDEFINED_COLORSPACEPDFColorSpace)	PDFDevice
PDFTextSeq)PDFExceptionPDFValueError)
PDFCIDFontPDFFontPDFFontErrorPDFTrueTypeFontPDFType1FontPDFType3Font)PDFPage)LITERALS_ASCII85_DECODE	PDFObjRef	PDFStream
dict_value
list_valueresolve1stream_value)PSEOFPSTypeError)KWDLIT	PSKeyword	PSLiteralPSStackParserPSStackTypekeyword_nameliteral_name)MATRIX_IDENTITYMatrixPathSegmentPointRectchoplistmult_matrixapply_matrix_ptc                       \ rS rSrSrg)PDFResourceError8    N__name__
__module____qualname____firstlineno____static_attributes__r:       a/Users/admin/workspace/ai/PDFMathTranslate/myenv/lib/python3.13/site-packages/pdf2zh/pdfinterp.pyr8   r8   8       rA   r8   c                       \ rS rSrSrg)PDFInterpreterError<   r:   Nr;   r:   rA   rB   rE   rE   <   rC   rA   rE   PDFTextFontFormImagec                   V    \ rS rSr% \\S'   \\S'   SS jrS\4S jr	SS jr
SS	 jrS
rg)PDFTextStateG   matrix
linematrixreturnNc                     S U l         SU l        SU l        SU l        SU l        SU l        SU l        SU l        U R                  5         g )Nr   d   )	fontfontsize	charspace	wordspacescalingleadingrenderriseresetselfs    rB   __init__PDFTextState.__init__K   sC    '+	  ! !!	

rA   c                     SU R                   < SU R                  < SU R                  < SU R                  < SU R                  < SU R
                  < SU R                  < SU R                  < S	U R                  < S
U R                  < S3$ )Nz<PDFTextState: font=z, fontsize=z, charspace=z, wordspace=z
, scaling=z
, leading=z	, render=z, rise=z	, matrix=z, linematrix=>)
rT   rU   rV   rW   rX   rY   rZ   r[   rO   rP   r]   s    rB   __repr__PDFTextState.__repr__X   sT     					
rA   c                 n   [        5       nU R                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        U R
                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        U R                  Ul	        U R                  Ul
        U$ N)rM   rT   rU   rV   rW   rX   rY   rZ   r[   rO   rP   r^   objs     rB   copyPDFTextState.copyk   s|    n99}}llll[[
99[[

rA   c                 (    [         U l        SU l        g )Nr   r   )r/   rO   rP   r]   s    rB   r\   PDFTextState.resety   s    % rA   )
rV   rT   rU   rY   rP   rO   rZ   r[   rX   rW   rQ   N)rQ   rM   )r<   r=   r>   r?   r0   __annotations__r2   r_   strrc   ri   r\   r@   r:   rA   rB   rM   rM   G   s(    N	
# 
&!rA   rM   c                   6    \ rS rSrSS jrS	S jrS\4S jrSrg)
PDFGraphicState   rQ   Nc                     SU l         S U l        S U l        S U l        S U l        S U l        S U l        S U l        S U l        g Nr   		linewidthlinecaplinejoin
miterlimitdashintentflatnessscolorncolorr]   s    rB   r_   PDFGraphicState.__init__   sE     !)-*.,059	(,*. (, (,rA   c                 L   [        5       nU R                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        U R
                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        U R                  Ul	        U$ rf   )
rr   rw   rx   ry   rz   r{   r|   r}   r~   r   rg   s     rB   ri   PDFGraphicState.copy   sr    ll}}99[[
}}[[
[[

rA   c                    SU R                   < SU R                  < SU R                  < SU R                  < SU R                  < SU R
                  < SU R                  < SU R                  < S	U R                  < S
3$ )Nz<PDFGraphicState: linewidth=z
, linecap=z, linejoin=z,  miterlimit=z, dash=z	, intent=z, flatness=z,  stroking color=z, non stroking color=rb   rv   r]   s    rB   rc   PDFGraphicState.__repr__   sM     			
rA   )	r{   r}   r|   rx   ry   rw   rz   r   r~   rn   )rQ   rr   )	r<   r=   r>   r?   r_   ri   rp   rc   r@   r:   rA   rB   rr   rr      s    ,
# 
rA   rr   c                       \ rS rSrSrSS\SS4S jjrS\\   SS4S jr	SS	\
S
\S\4S jjrS\S\\
\4   S\4S jrSrg)PDFResourceManager   zRepository of shared resources.

ResourceManager facilitates reuse of shared resources
such as fonts and images so that large objects are not
allocated multiple times.
cachingrQ   Nc                     Xl         0 U l        g rf   )r   _cached_fonts)r^   r   s     rB   r_   PDFResourceManager.__init__   s    46rA   procsc                 >    U H  nU[         L d	  U[        L a  M  M     g rf   )LITERAL_PDFLITERAL_TEXT)r^   r   procs      rB   get_procsetPDFResourceManager.get_procset   s!    D{"dl&:	 rA   cmapnamestrictc                      [         R                  " U5      $ ! [         R                   a    U(       a  e [        5       s $ f = frf   )r   get_cmapCMapNotFoundr   )r^   r   r   s      rB   r   PDFResourceManager.get_cmap   s8    	??8,,"" 	6M	s    &A Aobjidspecc                 (   U(       a   XR                   ;   a  U R                   U   nU$ [        R                  (       a  US   [        La  [	        S5      eSU;   a  [        US   5      nO"[        R                  (       a  [	        S5      eSnUS;   a  [        X5      nOUS:X  a  [        X5      nOUS:X  a  [        X5      nOUS	;   a  [        X5      nOUS
:X  ae  [        US   5      nU(       d   e[        US   5      R                  5       nS H  nXr;   d  M
  [        X'   5      Xg'   M     U R                  S U5      nO.[        R                  (       a  [	        SU-  5      e[        X5      nU(       a  U R                  (       a  X0R                   U'   U$ )NTypezType is not /FontSubtypezFont Subtype is not specified.Type1)r   MMType1TrueTypeType3)CIDFontType0CIDFontType2Type0DescendantFontsr   )Encoding	ToUnicodezInvalid Font spec: %r)r   r   STRICTLITERAL_FONTr   r.   r   r   r   r   r"   r!   ri   r#   get_fontr   )r^   r   r   rT   subtypedfontssubspecks           rB   r   PDFResourceManager.get_font   se   U000%%e,DP K <|3&':;;D &tI7??&'GHH!..#D/J&&t2G##D/<<!$-G##D):$;<v$VAY/4462Ay%-dg%6
 3 }}T73??&'>'EFF#D/,0""5)rA   )r   r   )T)F)r<   r=   r>   r?   __doc__boolr_   r   objectr   rp   r   r   r   r   r   r@   r:   rA   rB   r   r      so    7 7 7&!1 d  d x *f *GCK,@ *W *rA   r   c            	           \ rS rSrS\\   SS4S jrSS jrS\SS4S jr	SS	 jr
SS\S\S\\\4   4S jjrSS jr\" S5      r\" S5      r\" S
5      rS\S\SS4S jrSrg)PDFContentParser   streamsrQ   Nc                 L    Xl         SU l        [        R                  " U S 5        g ru   )r   istreamr+   r_   )r^   r   s     rB   r_   PDFContentParser.__init__   s      	tT*rA   c                 0   U R                   (       d  U R                  [        U R                  5      :  a8  [	        U R                  U R                     5      nU =R                  S-  sl        O[        S5      e[        UR                  5       5      U l         g g )N   zUnexpected EOF, file truncated?)fpr   lenr   r$   r%   r   get_data)r^   strms     rB   fillfpPDFContentParser.fillfp  sf    ww||c$,,//#DLL$>?!=>>dmmo.DG rA   posc                 P    U R                  5         [        R                  " X5        g rf   )r   r+   seek)r^   r   s     rB   r   PDFContentParser.seek  s    4%rA   c                 D   U R                   [        U R                  5      :  a  g  U R                  5         U R                  R                  5       U l        U R                  R                  U R                  5      U l        U R                  (       a  O	S U l        Mt  SU l         g ru   )	charposr   bufr   r   tellbufposreadBUFSIZr]   s    rB   fillbufPDFContentParser.fillbuf  sm    <<#dhh-'KKM'',,.DKww||DKK0DHxxDG  rA      EItargetc                 B   U R                  U5        SnSnU[        U5      ::  Ga	  U R                  5         U(       a  U R                  U R                     n[        U45      nXF-  nU =R                  S-  sl        [        U5      U::  a  UR                  5       (       d!  U[        U5      :  a  U[        X#   45      :X  a  US-  nOWSnOT U R                  R                  US   U R                  5      nX@R                  U R                  US-    -  nUS-   U l        SnU[        U5      ::  a  GM	  US [        U5      S-   *  n[        R                  " SSU5      nX4$ ! [         a8    X@R                  U R                  S  -  n[        U R                  5      U l         Nf = f)Nr   rA   r   s   (\x0d\x0a|[\x0d\x0a])$)r   r   r   r   r   bytesisspaceindex
ValueErrorresub)r^   r   r   idatacicjs           rB   get_inline_data PDFContentParser.get_inline_data!  sh   		#3v;LLNXXdll+2%L	!K1$		3v;eVYL12FAA1vay$,,?AHHT\\AE::D#$q5DLA+ 3v;2 (Fa()vv0#t<{ " 1HHT\\^44D#&txx=DL1s   
AE ?FFc                 >    U R                   " U R                  5       6   g rf   )add_resultspopallr]   s    rB   flushPDFContentParser.flushB  s    $++-(rA   s   BIs   IDtokenc                    X R                   L a  U R                  US5        g X R                  L Ga   U R                  S5      u  p4[	        U5      S-  S:w  a  SU< 3n[        U5      e[        SU5       VVs0 s H  u  pg[        U5      [        U5      _M     nnnSn	UR                  SS 5      n
U
b'  [        U
[        5      (       a  U
/n
U
S   [        ;   a  Sn	U R                  U[	        S5      -   U	S	9u  pU	S:w  a  X-  n[        X5      nU R                  X45        U	S:X  a  U R                  XR                   45        g g U R                  X45        g s  snnf ! [
         a    ["        R$                  (       a  e  g f = f)
Ninline   r   zInvalid dictionary construct: r   Fs   ~>s   ID )r   )
KEYWORD_BI
start_type
KEYWORD_IDend_typer   r&   r4   r.   r#   get
isinstancer*   r   r   r    push
KEYWORD_EIr   r   )r^   r   r   _objs	error_msgr   vdeosfilterr   rh   s                rB   
do_keywordPDFContentParser.do_keywordI  sh   OO#OOC*oo% MM(3	t9q=A%"@ II%i00@HD@QR@Qfq\!_hqk1@QRsD)%!&)44"(ay$;;#"223V3DS2Q%<KD(		3*%%<IIsOO45   IIsl#' S  ?? #s%   AE 7!EB(E E  E>=E>)r   r   r   r   r   r   rn   )r   )r<   r=   r>   r?   r   r   r_   r   intr   r   r   r	   r   r   r'   r   r   r   r)   r   r@   r:   rA   rB   r   r      s    + 0 +T +/& & &
3  %U
BS B) UJUJUJ$c $) $ $rA   r   c                      \ rS rSrSrS\S\SS4S jrSS jrS	\	\
\
4   SS4S
 jrS\SS4S jrS\SS4S jrS\S\\   4S jrS\\\\4   4S jrS\\\\4   SS4S jrSS jrSS jrS\S\S\S\S\S\SS4S jrS\SS4S jrS\SS4S  jrS!\SS4S" jrS#\SS4S$ jrS%\S&\SS4S' jrS(\SS4S) jr S*\SS4S+ jr!S,\SS4S- jr"S.\S/\SS4S0 jr#S.\S/\SS4S1 jr$S2\S3\S4\S5\S6\S7\SS4S8 jr%S4\S5\S6\S7\SS4
S9 jr&S2\S3\S6\S7\SS4
S: jr'SS; jr(S.\S/\S<\S=\SS4
S> jr)SS? jr*SS@ jr+SSA jr,SSB jr-SSC jr.SSD jr/SSE jr0SSF jr1SSG jr2SSH jr3SSI jr4SSJ jr5S,\SS4SK jr6S,\SS4SL jr7SM\SS4SN jr8SM\SS4SO jr9SP\SQ\SR\SS4SS jr:SP\SQ\SR\SS4ST jr;SU\SV\S/\SW\SS4
SX jr<SU\SV\S/\SW\SS4
SY jr=SSZ jr>SS[ jr?SS\ jr@SS] jrAS,\
SS4S^ jrBSS_ jrCSS` jrDSSa jrESSb jrFSc\SS4Sd jrGSc\Se\SS4Sf jrHSc\SS4Sg jrISc\Se\SS4Sh jrJSSi jrKSj\SS4Sk jrLSj\SS4Sl jrMSm\SS4Sn jrNSo\SS4Sp jrOSq\Sr\SS4Ss jrPSt\SS4Su jrQSv\SS4Sw jrRSx\Sy\SS4Sz jrSSx\Sy\SS4S{ jrTS|\SR\SU\S}\S~\S\SS4S jrUSS jrVS\SS4S jrWS\SS4S jrXS\SS4S jrYS\S\S\SS4S jrZSS jr[SS jr\S\SS4S jr]S\SS4S jr^S\_SS4S jr`\a4S	\	\
\
4   S\b\
   S\SS4S jjrcS\b\
   SS4S jrdSreg)PDFPageInterpreterim  z`Processor for the content of a PDF page

Reference: PDF Reference, Appendix A, Operator Summary
rsrcmgrdevicerQ   Nc                 (    Xl         X l        X0l        g rf   )r   r   	obj_patch)r^   r   r   r   s       rB   r_   PDFPageInterpreter.__init__s  s    "rA   c                 d    U R                  U R                  U R                  U R                  5      $ rf   )	__class__r   r   r   r]   s    rB   dupPDFPageInterpreter.dupx  s!    ~~dllDKKHHrA   	resourcesc                    Xl         0 U l        0 U l        0 U l        [        R
                  " 5       U l        U(       d  gS[        S[        [           4S jn[        U5      R                  5        GHU  u  p4US:X  a  [        U5      R                  5        Hv  u  pVSn[        U[        5      (       a  UR                  n[        U5      nU R                  R!                  Xv5      U R                  U'   XPR                  U R                  U   '   Mx     M  US:X  aH  [        U5      R                  5        H)  u  pU" [#        U5      5      n	U	c  M  XR                  U'   M+     M  US:X  a'  U R                  R%                  ['        U5      5        GM  US:X  d  GM%  [        U5      R                  5        H  u  pXR                  U
'   M     GMX     g)	z@Prepare the fonts and XObjects listed in the Resource attribute.Nr   rQ   c           	         [        U [        5      (       a  [        U S   5      nO[        U 5      nUS:X  a?  [        U [        5      (       a*  [        U 5      S:  a  [	        U[        U S   5      S   5      $ US:X  aE  [        U [        5      (       a0  [        U 5      S:  a!  [	        U[        [        U S   5      5      5      $ [        R                  " U5      $ )Nr   ICCBasedr   r   NDeviceN)	r   listr.   r   r   r$   r"   r   r   )r   names     rB   get_colorspace9PDFPageInterpreter.init_resources.<locals>.get_colorspace  s    $%%#DG,#D)z!jt&<&<Ta$T<Q+@+EFF"z$'='=#d)q.$T3z$q'/B+CDD,0066rA   rI   
ColorSpaceProcSetXObject)r  fontmapfontidxobjmapr   ri   csmapr   r   r   r!   itemsr   r   r   r   r   r#   r   r"   )r^   r  r  r   r   r  r   r   csid
colorspacexobjidxobjstrms               rB   init_resources!PDFPageInterpreter.init_resources{  sl   ".0-//D/I/I/K

	7 
	7H],C 
	7 y)//1DAF{$.qM$7$7$9LF E!$	22 $

%d+D+/<<+@+@+MDLL(6<KKV 45 %: l"",Q-"5"5"7JD!/!?J!-+5

4( #8 i((A7i(21(;(;(=$F+3LL( )>% 2rA   ctmc                 p   / U l         Xl        U R                  R                  U R                  5        [	        5       U l        [        5       U l        / U l        / U l	        SU l
        SU l        U R                  (       a9  [        [        U R                  R                  5       5      5      =U l
        U l        gg)z<Initialize the text and graphic states for rendering a page.N)gstackr  r   set_ctmrM   	textstaterr   graphicstatecurpathargstackscsncsr  nextitervalues)r^   r  s     rB   
init_statePDFPageInterpreter.init_state  s     KMDHH%%+-*,)+,0,0::"&tDJJ,=,=,?'@"AADHtx rA   rh   c                 :    U R                   R                  U5        g rf   )r%  appendrg   s     rB   r   PDFPageInterpreter.push  s    S!rA   nc                 `    US:X  a  / $ U R                   U* S  nU R                   S U*  U l         U$ ru   )r%  )r^   r0  xs      rB   popPDFPageInterpreter.pop  s9    6IMM1"#cr*rA   c                     U R                   U R                  R                  5       U R                  R                  5       4$ rf   )r  r"  ri   r#  r]   s    rB   get_current_state$PDFPageInterpreter.get_current_state  s/    $..--/1B1B1G1G1IJJrA   statec                 x    Uu  U l         U l        U l        U R                  R	                  U R                   5        g rf   )r  r"  r#  r   r!  )r^   r8  s     rB   set_current_state$PDFPageInterpreter.set_current_state  s-     9>54>4#4DHH%rA   c                 V    U R                   R                  U R                  5       5        g)zSave graphics stateN)r   r.  r6  r]   s    rB   do_qPDFPageInterpreter.do_q  s    41134rA   c                 z    U R                   (       a*  U R                  U R                   R                  5       5        gg)zRestore graphics stateN)r   r:  r3  r]   s    rB   do_QPDFPageInterpreter.do_Q  s'    ;;""4;;??#45 rA   a1b1c1d1e1f1c                     [        [        [        XX4XV45      U R                  5      U l        U R                  R                  U R                  5        g)z3Concatenate matrix to current transformation matrixN)r5   r   r0   r  r   r!  )r^   rB  rC  rD  rE  rF  rG  s          rB   do_cmPDFPageInterpreter.do_cm  s:     tFRRR,DEtxxPDHH%rA   rw   c                 B    [        [        U5      U R                  l        g)zSet line widthN)r   floatr#  rw   )r^   rw   s     rB   do_wPDFPageInterpreter.do_w  s    &*5)&<#rA   rx   c                 $    XR                   l        g)zSet line cap styleN)r#  rx   )r^   rx   s     rB   do_JPDFPageInterpreter.do_J  s    $+!rA   ry   c                 $    XR                   l        g)zSet line join styleN)r#  ry   )r^   ry   s     rB   do_jPDFPageInterpreter.do_j      %-"rA   rz   c                 $    XR                   l        g)zSet miter limitN)r#  rz   )r^   rz   s     rB   do_MPDFPageInterpreter.do_M  s    '1$rA   r{   phasec                 (    X4U R                   l        g)zSet line dash patternN)r#  r{   )r^   r{   rY  s      rB   do_dPDFPageInterpreter.do_d  s    "&rA   r|   c                 $    XR                   l        g)zSet color rendering intentN)r#  r|   )r^   r|   s     rB   do_riPDFPageInterpreter.do_ri  s    #) rA   r}   c                 $    XR                   l        g)zSet flatness toleranceN)r#  r}   )r^   r}   s     rB   do_iPDFPageInterpreter.do_i  rU  rA   r  c                     g)z7Set parameters from graphics state parameter dictionaryNr:   r^   r  s     rB   do_gsPDFPageInterpreter.do_gs      rA   r2  yc                 x    U R                   R                  S[        [        U5      [        [        U5      45        g)zBegin new subpathmNr$  r.  r   rL  r^   r2  rh  s      rB   do_mPDFPageInterpreter.do_m  '    S$ua.$ua.ABrA   c                 x    U R                   R                  S[        [        U5      [        [        U5      45        g)z$Append straight line segment to pathlNrk  rl  s      rB   do_lPDFPageInterpreter.do_l  ro  rA   x1y1x2y2x3y3c                     U R                   R                  S[        [        U5      [        [        U5      [        [        U5      [        [        U5      [        [        U5      [        [        U5      45        g)z4Append curved segment to path (three control points)r   Nrk  )r^   rt  ru  rv  rw  rx  ry  s          rB   do_cPDFPageInterpreter.do_c	  sT     	UBUBUBUBUBUB
	
rA   c           
          U R                   R                  S[        [        U5      [        [        U5      [        [        U5      [        [        U5      45        g)z8Append curved segment to path (initial point replicated)r   Nrk  )r^   rv  rw  rx  ry  s        rB   do_vPDFPageInterpreter.do_v  ;    $ub/4r?DOT%QS_U	
rA   c           
          U R                   R                  S[        [        U5      [        [        U5      [        [        U5      [        [        U5      45        g)z6Append curved segment to path (final point replicated)rh  Nrk  )r^   rt  ru  rx  ry  s        rB   do_yPDFPageInterpreter.do_y%  r  rA   c                 :    U R                   R                  S5        g)zClose subpathhN)r$  r.  r]   s    rB   do_hPDFPageInterpreter.do_h+  s    F#rA   wr  c                    [        [        U5      n[        [        U5      n[        [        U5      n[        [        U5      nU R                  R                  SX45        U R                  R                  SX-   U45        U R                  R                  SX-   X$-   45        U R                  R                  SXU-   45        U R                  R                  S5        g)zAppend rectangle to pathrj  rq  r  N)r   rL  r$  r.  )r^   r2  rh  r  r  s        rB   do_rePDFPageInterpreter.do_re/  s    NNNNS!K(S!%O,S!%/0S!UO,F#rA   c                    S[         S[        4S jn[        U R                  5      S:X  a  U R                  S   S   S:X  a  U R                  S   S   S:X  a  [	        U R
                  U R                  S   S	S
 5      S   [	        U R
                  U R                  S   S	S
 5      S   :X  a\  U" U R                  R                  5      (       a;  U R                  R                  U R                  SSSU R                  5        / U l        g/ U l        g
)zStroke pathcolorrQ   c                 R    [        U [        5      (       a  [        U 5      S:H  $ U S:H  $ ru   )r   r	   sum)r  s    rB   is_black)PDFPageInterpreter.do_S.<locals>.is_black=  s&    %''5z1}$axrA   r   r   rj  r   rq  NTFr0  )
Colorr   r   r$  r6   r  r#  r~   r   
paint_path)r^   r  s     rB   do_SPDFPageInterpreter.do_S;  s   	 E 	 d 	 
 t||aDLLOA$6$;QPQ@RTW@W\klpltltuy  vB  vB  CD  vE  FH  FI  vJ  ]K  LM  ]N  P_  `d  `h  `h  im  iu  iu  vw  ix  y{  y|  i}  P~  @  PA  ]A  FN  OS  O`  O`  Og  Og  Fh  FhKK""4#4#4dE5$,,WDLDLrA   c                 D    U R                  5         U R                  5         g)zClose and stroke pathN)r  r  r]   s    rB   do_sPDFPageInterpreter.do_sJ      				rA   c                     / U l         g)z+Fill path using nonzero winding number ruleNr$  r]   s    rB   do_fPDFPageInterpreter.do_fO       rA   c                     g)z6Fill path using nonzero winding number rule (obsolete)Nr:   r]   s    rB   do_FPDFPageInterpreter.do_FT  rg  rA   c                     / U l         g)zFill path using even-odd ruleNr  r]   s    rB   do_f_aPDFPageInterpreter.do_f_aW  r  rA   c                     / U l         g)z6Fill and stroke path using nonzero winding number ruleNr  r]   s    rB   do_BPDFPageInterpreter.do_B\  r  rA   c                     / U l         g)z(Fill and stroke path using even-odd ruleNr  r]   s    rB   do_B_aPDFPageInterpreter.do_B_aa  r  rA   c                 D    U R                  5         U R                  5         g)z>Close, fill, and stroke path using nonzero winding number ruleN)r  r  r]   s    rB   do_bPDFPageInterpreter.do_bf  r  rA   c                 D    U R                  5         U R                  5         g)z0Close, fill, and stroke path using even-odd ruleN)r  r  r]   s    rB   do_b_aPDFPageInterpreter.do_b_ak  s    		rA   c                     / U l         g)z$End path without filling or strokingNr  r]   s    rB   do_nPDFPageInterpreter.do_np  s	    rA   c                     g)z3Set clipping path using nonzero winding number ruleNr:   r]   s    rB   do_WPDFPageInterpreter.do_Wt  rg  rA   c                     g)z%Set clipping path using even-odd ruleNr:   r]   s    rB   do_W_aPDFPageInterpreter.do_W_aw  rg  rA   c                      U R                   [        U5         U l        g! [         a&    [        R
                  (       a  [        SU-  5      e gf = f)z?Set color space for stroking operations

Introduced in PDF 1.1
Undefined ColorSpace: %rN)r  r.   r&  KeyErrorr   r   rE   rd  s     rB   do_CSPDFPageInterpreter.do_CSz  sK    
	Mzz,t"45DH 	M)*Dt*KLL 	M     -AAc                      U R                   [        U5         U l        g! [         a&    [        R
                  (       a  [        SU-  5      e gf = f)z*Set color space for nonstroking operationsr  N)r  r.   r'  r  r   r   rE   rd  s     rB   do_csPDFPageInterpreter.do_cs  sK    	Mzz,t"45DH 	M)*Dt*KLL 	Mr  grayc                 j    [        [        U5      U R                  l        U R                  S   U l        g)z&Set gray level for stroking operations
DeviceGrayNr   rL  r#  r~   r  r&  r^   r  s     rB   do_GPDFPageInterpreter.do_G  '    #'t#4 ::l+rA   c                 j    [        [        U5      U R                  l        U R                  S   U l        g)z)Set gray level for nonstroking operationsr  Nr   rL  r#  r   r  r'  r  s     rB   do_gPDFPageInterpreter.do_g  r  rA   rgbc                     [        [        U5      [        [        U5      [        [        U5      4U R                  l        U R                  S   U l        g)z%Set RGB color for stroking operations	DeviceRGBNr  r^   r  r  r  s       rB   do_RGPDFPageInterpreter.do_RG  :    $(NDNDPQN#S ::k*rA   c                     [        [        U5      [        [        U5      [        [        U5      4U R                  l        U R                  S   U l        g)z(Set RGB color for nonstroking operationsr  Nr  r  s       rB   do_rgPDFPageInterpreter.do_rg  r  rA   r   rj  r   c                     [        [        U5      [        [        U5      [        [        U5      [        [        U5      4U R                  l        U R                  S   U l        g)z&Set CMYK color for stroking operations
DeviceCMYKNr  r^   r   rj  rh  r   s        rB   do_KPDFPageInterpreter.do_K  J     NNNN	$
  ::l+rA   c                     [        [        U5      [        [        U5      [        [        U5      [        [        U5      4U R                  l        U R                  S   U l        g)z)Set CMYK color for nonstroking operationsr  Nr  r  s        rB   do_kPDFPageInterpreter.do_k  r  rA   c                     U R                   (       a  U R                   R                  nO"[        R                  (       a  [	        S5      eSnU R                  U5      n[        [        U5      U R                  l	        U$ )z"Set color for stroking operations.No colorspace specified!r   )
r&  ncomponentsr   r   rE   r3  r   r  r#  r~   r^   r0  argss      rB   do_SCNPDFPageInterpreter.do_SCN  W    88$$A)*DEEAXXa[#'t#4 rA   c                     U R                   (       a  U R                   R                  nO"[        R                  (       a  [	        S5      eSnU R                  U5      n[        [        U5      U R                  l	        U$ )$Set color for nonstroking operationsr  r   )
r'  r  r   r   rE   r3  r   r  r#  r   r  s      rB   do_scnPDFPageInterpreter.do_scn  r  rA   c                 "    U R                  5       $ )z!Set color for stroking operations)r  r]   s    rB   do_SCPDFPageInterpreter.do_SC      {{}rA   c                 "    U R                  5       $ )r  )r  r]   s    rB   do_scPDFPageInterpreter.do_sc  r  rA   c                     g)z%Paint area defined by shading patternNr:   rd  s     rB   do_shPDFPageInterpreter.do_sh  rg  rA   c                 8    U R                   R                  5         g)zBegin text object

Initializing the text matrix, Tm, and the text line matrix, Tlm, to
the identity matrix. Text objects cannot be nested; a second BT cannot
appear before an ET.
N)r"  r\   r]   s    rB   do_BTPDFPageInterpreter.do_BT  s     	rA   c                     g)zEnd a text objectNr:   r]   s    rB   do_ETPDFPageInterpreter.do_ET  rg  rA   c                     g)zBegin compatibility sectionNr:   r]   s    rB   do_BXPDFPageInterpreter.do_BX  rg  rA   c                     g)zEnd compatibility sectionNr:   r]   s    rB   do_EXPDFPageInterpreter.do_EX  rg  rA   tagc                 V    U R                   R                  [        [        U5      5        g)zDefine marked-content pointNr   do_tagr   r*   r^   r  s     rB   do_MPPDFPageInterpreter.do_MP  s    4	3/0rA   propsc                 X    U R                   R                  [        [        U5      U5        g)z.Define marked-content point with property listNr
  r^   r  r  s      rB   do_DPPDFPageInterpreter.do_DP  s    4	3/7rA   c                 V    U R                   R                  [        [        U5      5        g)zBegin marked-content sequenceNr   	begin_tagr   r*   r  s     rB   do_BMCPDFPageInterpreter.do_BMC  s    d9c23rA   c                 X    U R                   R                  [        [        U5      U5        g)z0Begin marked-content sequence with property listNr  r  s      rB   do_BDCPDFPageInterpreter.do_BDC  s    d9c2E:rA   c                 8    U R                   R                  5         g)zEnd marked-content sequenceN)r   end_tagr]   s    rB   do_EMCPDFPageInterpreter.do_EMC  s    rA   spacec                 B    [        [        U5      U R                  l        g)zSet character spacing.

Character spacing is used by the Tj, TJ, and ' operators.

:param space: a number expressed in unscaled text space units.
N)r   rL  r"  rV   r^   r   s     rB   do_TcPDFPageInterpreter.do_Tc       $(u#5 rA   c                 B    [        [        U5      U R                  l        g)zSet the word spacing.

Word spacing is used by the Tj, TJ, and ' operators.

:param space: a number expressed in unscaled text space units
N)r   rL  r"  rW   r"  s     rB   do_TwPDFPageInterpreter.do_Tw  r%  rA   scalec                 B    [        [        U5      U R                  l        g)zeSet the horizontal scaling.

:param scale: is a number specifying the percentage of the normal width
N)r   rL  r"  rX   )r^   r)  s     rB   do_TzPDFPageInterpreter.do_Tz  s    
 "&eU!3rA   rY   c                 D    [        [        U5      * U R                  l        g)zSet the text leading.

Text leading is used only by the T*, ', and " operators.

:param leading: a number expressed in unscaled text space units
N)r   rL  r"  rY   )r^   rY   s     rB   do_TLPDFPageInterpreter.do_TL  s     #'ug"6!6rA   r  rU   c                 N    U R                   [        U5         U R                  l        [        [        U5      U R                  l        g! [         aQ    [
        R                  (       a  [        SU-  5      eU R                  R                  S0 5      U R                  l         Nzf = f)zSet the text font

:param fontid: the name of a font resource in the Font subdictionary
    of the current resource dictionary
:param fontsize: size is a number representing a scale factor.
zUndefined Font id: %rN)r  r.   r"  rT   r  r   r   rE   r   r   r   rL  rU   )r^   r  rU   s      rB   do_TfPDFPageInterpreter.do_Tf   s    	B"&,,|F/C"DDNN
 #'uh"7	  	B)*AF*JKK"&,,"7"7b"ADNN	Bs   'A	 	AB$#B$rZ   c                 B    [        [        U5      U R                  l        g)zSet the text rendering modeN)r   r   r"  rZ   )r^   rZ   s     rB   do_TrPDFPageInterpreter.do_Tr/  s     $S& 1rA   r[   c                 B    [        [        U5      U R                  l        g)zPSet the text rise

:param rise: a number expressed in unscaled text space units
N)r   rL  r"  r[   )r^   r[   s     rB   do_TsPDFPageInterpreter.do_Ts3  s    
 #5$/rA   txtyc                 >   [        U5      n[        U5      nUbJ  UbG  U R                  R                  u  pVpxpX5-  XG-  -   U	-   nX6-  XH-  -   U
-   nXVXxX4U R                  l        O)[        R                  (       a  [        SU< SU< S35      eSU R                  l        g)z\Move to the start of the next line

Offset from the start of the current line by (tx , ty).
NzInvalid offset (z, z) for Tdrl   )r   r"  rO   r   r   r   rP   r^   r9  r:  tx_ty_ar  r   r   efe_newf_news                rB   do_TdPDFPageInterpreter.do_Td:  s    
 nn?s!%!6!6Q1Gcg%)EGcg%)E%&1$>DNN!__"22&2& IJJ$*!rA   c                 R   [        U5      n[        U5      nUbJ  UbG  U R                  R                  u  pVpxpX5-  XG-  -   U	-   nX6-  XH-  -   U
-   nXVXxX4U R                  l        O [        R                  (       a  [        S5      eUb  X@R                  l        SU R                  l        g)zMove to the start of the next line.

offset from the start of the current line by (tx , ty). As a side effect, this
operator sets the leading parameter in the text state.
Nz"Invalid offset ({tx}, {ty}) for TDrl   )r   r"  rO   r   r   r   rY   rP   r<  s                rB   do_TDPDFPageInterpreter.do_TDL  s     nn?s!%!6!6Q1Gcg%)EGcg%)E%&1$>DNN!__ DEE?%(NN"$*!rA   r?  r   r@  rA  c           	      j    [        [        XX4XV45      U R                  l        SU R                  l        g)z$Set text matrix and text line matrixrl   N)r   r0   r"  rO   rP   r^   r?  r  r   r   r@  rA  s          rB   do_TmPDFPageInterpreter.do_Tmc  s*     !%VaA!-? @$*!rA   c                     U R                   R                  u  pp4pVUUUUU R                   R                  U-  U-   U R                   R                  U-  U-   4U R                   l        SU R                   l        g)zMove to start of next text linerl   N)r"  rO   rY   rP   rJ  s          rB   do_T_aPDFPageInterpreter.do_T_ap  sn    !^^22qQNN""Q&*NN""Q&*!
 %+!rA   seqc                 B   U R                   R                  c!  [        R                  (       a  [	        S5      egU R
                  c   eU R                  R                  U R                   [        [        U5      U R
                  U R                  R                  5       5        g)z0Show text, allowing individual glyph positioningNzNo font specified!)r"  rT   r   r   rE   r'  r   render_stringr   r   r#  ri   )r^   rP  s     rB   do_TJPDFPageInterpreter.do_TJ}  st    >>&)*>??xx###!!NNS!HH""$		
rA   sc                 (    U R                  U/5        g)z	Show textN)rS  r^   rU  s     rB   do_TjPDFPageInterpreter.do_Tj  s    

A3rA   c                 H    U R                  5         U R                  U/5        g)z@Move to next line and show text

The ' (single quote) operator.
N)rN  rS  rW  s     rB   do__qPDFPageInterpreter.do__q  s    
 	

A3rA   awacc                 l    U R                  U5        U R                  U5        U R                  U/5        g)zaSet word and character spacing, move to next line, and show text

The " (double quote) operator.
N)r'  r#  rS  )r^   r]  r^  rU  s       rB   do__wPDFPageInterpreter.do__w  s'    
 	

2

2

A3rA   c                     g)zBegin inline image objectNr:   r]   s    rB   do_BIPDFPageInterpreter.do_BI  rg  rA   c                     g)zBegin inline image dataNr:   r]   s    rB   do_IDPDFPageInterpreter.do_ID  rg  rA   c                 "   [        U[        5      (       az  SU;   as  SU;   al  [        [        U5      5      nU R                  R                  US[        5        U R                  R                  X!5        U R                  R                  U5        gggg)zEnd inline image objectWHr   r   r   r   N)	r   r    rp   idr   begin_figurer/   render_image
end_figure)r^   rh   iobjids      rB   do_EIPDFPageInterpreter.do_EI  sm    c9%%#*C\FKK$$V\?KKK$$V1KK""6*	 :D*%rA   
xobjid_argc                    [        U5      n [        U R                  U   5      nUR                  S5      nU[        L Ga  SU;   Ga  U R                  5       n[        [        [        US   5      5      n[        [        [        UR                  S[        5      5      5      nUR                  S5      nU(       a  [        U5      n	OU R                   R#                  5       n	U R$                  R'                  X&U5        [)        XpR*                  5      n
UR-                  U	U/U
S9n UR.                  U R$                  l        UR0                  U R$                  l        U R$                  R3                  U5      n[4        R6                  R9                  [4        R:                  " U
SS 5      R=                  S	S	5      5      n[4        R>                  " U
SS 5      * U-  nUR=                  S5      RA                  5       u  nnnnURA                  5       S
   u  nnSU SU SU SU SU SU SU SU 3U RB                  U R                  U   RD                  '   gU[F        L ad  SU;   a^  SU;   aX  U R$                  R'                  US[        5        U R$                  RI                  X#5        U R$                  R3                  U5        gg! [         a&    [        R
                  (       a  [        SU-  5      e gf = f!    g= f)zInvoke named XObjectzUndefined xobject id: %rNr   BBoxr0   	Resourcesr     r   r   q zQ   cm WidthHeightrk  )%r.   r$   r  r  r   r   rE   r   LITERAL_FORMr  r   r3   r"   r0   r/   r!   r  ri   r   rm  r5   r  render_contentsr  r  ro  nplinalginvarrayreshapemattolistr   r   LITERAL_IMAGErn  )r^   rs  r  xobjr   interpreterbboxrO   xobjresr  r  ops_baseops_newctm_invpos_invr?  r  r   r   r@  rA  s                        rB   do_DoPDFPageInterpreter.do_Do  s   j)	V 45D ((9%l"v~((*Kjf67D&*TXXh-P"QRF hh{+G&w/	 NN//1	KK$$V6:FHH-C 00 1 H

#.#5#5"$/$7$7#..v6		bhhs2Aw&7&?&?!&DEAB(0*113!AaNN$Q'!=?zA3aPQsRSTUSVVWXYWZZ[\][^^_`a_bbfgnfo;pt||F399: %'T/h$>NKK$$V\?KKK$$V2KK""6* U  	)*Dv*MNN	Ds   J= "D,K0 =-K-,K-0K4pagec                    UR                   u  p#pEUR                  S:X  a
  SSSSU* U4nO;UR                  S:X  a  SSSSXE4nO#UR                  S:X  a	  SSSSXR* 4nO
SSSSU* U* 4nU R                  R                  X5        U R	                  UR
                  UR                  US9nU R                  U R                  l        U R                  U R                  l        U R                  R                  U5      nSU S	U S
U SU 3U R                  UR                  '   UR                   H  n	SU R                  U	R                  '   M     g )NZ   r   r      i  rw  ry  z
Q 1 0 0 1 rz  r{   )cropboxrotater   
begin_pager  r  contentsr  r  end_pager   	page_xrefr   )
r^   r  x0y0rt  ru  r  r  r  rh   s
             rB   process_pagePDFPageInterpreter.process_page  s2     <<;;"b!QR(C[[Cq!R(C[[CaQC(CaAsRC(Ct)%%dnndmm%M;; LL$$T*)+H:Zt1RDWI'Vt~~&==C&(DNN399% !rA   r   c                 z    U R                  U5        U R                  U5        U R                  [        U5      5      $ )zDRender the content streams.

This method may be called recursively.
)r  r+  executer"   )r^   r  r   r  s       rB   r  "PDFPageInterpreter.render_contents  s2      	I&||Jw/00rA   c           	      Z   Sn [        U5      n  UR                  5       u  nu  pE[        U[        5      (       Ga  [        U5      nSUR                  SS5      R                  SS5      R                  SS5      -  n[        X5      (       Ga;  [        X5      nUR                  R                  S	-
  n	U	(       a  U R                  U	5      n
[        U
5      U	:X  ar  U" U
6   US
   S:X  dd  US;   d^  SR                  U
 Vs/ s H7  n[        U[        5      (       a  US O[        U5      R                  SS5      PM9     sn5      nX, SU S3-  nOU" 5       nUS :X  a  / nUS
   S:X  dd  US;   d^  SR                  U Vs/ s H7  n[        U[        5      (       a  US O[        U5      R                  SS5      PM9     sn5      nX, SU S3-  nO7[         R"                  (       a  SU-  n[%        U5      eOU R'                  U5        GM  ! [         a     g f = f! [         a     U$ f = fs  snf s  snf )Nr  zdo_%s*_a"_w'_qr   r   T)r  r  EIMPDPBMCBDCrz  rA  )BIIDEMCzUnknown operator: %r)r   r%   
nextobjectr   r)   r-   replacehasattrgetattr__code__co_argcountr3  r   joinrL  rp   r   r   rE   r   )r^   r   opsparserr   rh   r  methodfuncnargsr  r2  ptargsr   s                  rB   r  PDFPageInterpreter.execute
  s8   	%g.F $//18A #y))#C( 4<<T#:#B#B3#M#U#U$  4(("40D MM559E#xxt9- $K$(GSLD<`4`"%((qu+vqulm
1U8K8KqeQTUVQWQ_Q_`cdfQg,gqu+v"w #s!D6^ 3 #f $;"$E $Q8I0I!hhmr'smrhiJq4G4G1Q%SQRV^^\_`bMc(cmr'stA3avQ/C__ 6 =I-i88 % 		#E   		  @ 
C" ,w (ts.   H H ;>H#8>H(
HH
H H )r%  r  r  r$  r   r  r  r#  r   r'  r   r  r   r&  r"  r  )rQ   r   rn   )fr<   r=   r>   r?   r   r   r   r_   r  r   r   r  r0   r+  	PDFStackTr   r   r   r3  r	   rM   rr   r6  r:  r=  r@  rI  rM  rP  rS  rW  r[  r^  ra  re  rm  rr  r{  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r#  r'  r+  r.  r1  r4  r7  rD  rG  rK  rN  rS  rX  r[  r`  rc  rf  rq  r  r   r  r/   r   r  r  r@   r:   rA   rB   r   r   m  sz   
# 2 #I #UY #
I)4VV^(< )4 )4VBf B B""	 "d "S T)_ K5)N#O K&V\?:;& 
&56
&& & 	&
 & & & 
&=i =D =,I ,$ ,.Y .4 .2y 2T 2/ /9 / /*I *$ *.Y .4 .F) F FCi CI C$ CCi CI C$ C

 
 	

 
 
 
 

,
y 
i 
Y 
I 
RV 

y 
i 
Y 
I 
RV 
$
$y 
$Y 
$9 
$ 
$t 
$

E




B4	M) 	M 	MM) M M, ,t ,
, ,t ,
+y +Y +9 + +
+y +Y +9 + +
,i ,I ,) ,	 ,d ,,i ,I ,) ,	 ,d ,

4& 4T 4 *(1 1t 18 89 8 84) 4 4;) ;I ;$ ;69 6 669 6 649 4 47Y 74 78I 8 8t 82I 2$ 20) 0 0+	 +y +T +$+	 +y +T +.++ + 	+
 + + + 
++
 
t 
y T y T 	 y Y 4 (&+ +t +/	 /d /b) )T )6 &	1'1 &!1 	1
 
1(+x/ +D +rA   r   )Xloggingr   ior   typingr   r   r   r   r   r	   r
   r   numpyr  pdf2zhr   pdf2zh.castingr   pdf2zh.cmapdbr   r   r   pdf2zh.pdfcolorr   r   pdf2zh.pdfdevicer   r   pdf2zh.pdfexceptionsr   r   pdf2zh.pdffontr   r   r   r   r   r   pdf2zh.pdfpager   pdf2zh.pdftypesr   r   r    r!   r"   r#   r$   pdf2zh.psexceptionsr%   r&   pdf2zh.psparserr'   r(   r)   r*   r+   r,   r-   r.   pdf2zh.utilsr/   r0   r1   r2   r3   r4   r5   r6   	getLoggerr<   logr8   rE   r   r   r   r~  r  rM   rL  r  rr   r   r   r  r   r:   rA   rB   <module>r     sY    	  N N N   % 0 0 @ 2 <  #   3	 	 		 	 	 !	| 		, 	 %j6{6{6{G4! 4!n 			%
	%u
$%'	-
 -
`E EPi$}U9i+?%@A i$X 	"	 6H HrA   