
    kh<\                        U d Z 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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mZ erddlmZ ddlZdZeej<                  eej@                  f   Z!eee	f   Z" G d d	      Z#d
ede$fdZ%de&defdZ'dede(defdZ)	 	 dZdee$ef   dededefdZ*de(de(de(de(fdZ+de(de(de(de$de$f
dZ,de(de(de(de(de$de$fd Z-ee.e.f   Z/ee.e.e.e.f   Z0ee.e.e.e.e.e.f   Z1eee   eee.e.f   eee.e.e.e.f   eee.e.e.e.e.e.f   f   Z2d!Z3e1e4d"<   dede0fd#Z5d$e1d%e1de1fd&Z6d'e1d(e/de1fd)Z7d'e1d(e/de/fd*Z8d'e1d+e0de0fd,Z9d'e1d(e/de/fd-Z:d.e&de;fd/Z< ed0      Z=d1ee=   dee=   fd2Z>de
e=ge;f   d1ee=   deee=   ee=   f   fd3Z?d4e.d5e.d6e(de@fd7ZAd8ee/   de0fd9ZB	 d[d:ee=   d;e
e=ge.f   d<ee=   dee=   fd=ZCd>e(d:ee=   deee=d?f      fd@ZDd\de$dAe(de(fdBZEdCj                  dD dED              ZGde$defdFZHd.edefdGZIdHe0defdIZJd'e1defdJZKdKdLdMdLde/fdNZL edOdLP      ZM G dQ dReeM         ZNg dSZOg dTZPdUe(defdVZQdUe(defdWZRdXe$de$fdYZSy)]zMiscellaneous Routines.    N)escape)TYPE_CHECKINGAnyBinaryIOCallableDictGenericIterableIteratorListOptionalSetTextIOTupleTypeVarUnioncast)PDFTypeErrorPDFValueError)LTComponentic                   L    e Zd ZdZdedededdfdZdefdZd	e	d
e	de	ddfdZ
y)open_filenamezContext manager that allows opening a filename
    (str or pathlib.PurePath type is supported) and closes it on exit,
    (just like `open`), but does nothing for file-like objects.
    filenameargskwargsreturnNc                 @   t        |t        j                        rt        |      }t        |t              rt	        |g|i || _        d| _        y t        |t        j                        rt        t        |      | _        d| _        y t        dt        |      z        )NTFzUnsupported input type: %s)
isinstancepathlibPurePathstropenfile_handlerclosingioIOBaser   AnyIOr   type)selfr   r   r   s       J/var/www/teggl/fontify/venv/lib/python3.12/site-packages/pdfminer/utils.py__init__zopen_filename.__init__/   s|    h 0 018}Hh$'+H'Ft'Fv'FDDL")), $UH 5D DL;d8nLMM    c                     | j                   S N)r#   r)   s    r*   	__enter__zopen_filename.__enter__;   s       r,   exc_typeexc_valexc_tbc                 R    | j                   r| j                  j                          y y r.   )r$   r#   close)r)   r1   r2   r3   s       r*   __exit__zopen_filename.__exit__>   s     <<##% r,   )__name__
__module____qualname____doc__
FileOrNamer   r+   r'   r0   objectr6    r,   r*   r   r   )   sZ    

N 
NC 
N3 
N4 
N!5 !& && && &T &r,   r   in_strr   c                 t    t        | t              sJ t        t        |                    | j                         S )z'Converts to bytes, encoding to unicode.)r   r!   r(   encode)r>   s    r*   make_compat_bytesrA   C   s,    fc"5CV$55"==?r,   oc                     t        | t              r*t        j                  |       }	 | j	                  |d         S t        |       S # t
        $ r t        |       cY S w xY w)z>Converts everything to string, if bytes guessing the encoding.encoding)r   bytescharset_normalizerdetectdecodeUnicodeDecodeErrorr!   )rB   encs     r*   make_compat_strrK   I   sY    !U ''*	88C
O,, 1v " 	q6M	s   A AAssizec                 b    |dk  r| d | S t        |       |kD  r|dz
  dz  }| d |  d| | d   S | S )N         z ... )len)rL   rM   lengths      r*   shorten_strrT   U   sP    ax$x
1v}(qGV*U1fWX;-00r,   bytesorstringrD   	erractionc                     t        | t              r| S t        | t              sJ t        t        |                    | j	                  ||      S )z^When Py2 str.encode is called, it often means bytes.encode in Py3.

    This does either.
    )r   r!   rE   r(   rH   )rU   rD   rV   s      r*   compatible_encode_methodrX   _   sF     -%mU+ESm1D-EE+)44r,   leftabove
upper_leftc                     | |z   |z
  }t        || z
        }t        ||z
        }t        ||z
        }||k  r||k  r| S ||k  r|S |S r.   )abs)rY   rZ   r[   ppapbpcs          r*   paeth_predictorrb   n   s`     	uz!A	QXB	QYB	Q^	B 
RxB"H	rr,   colorscolumnsbitspercomponentdatac                 8   |dk7  rd| }t        |      | |dz  z  }||z  }g }t        dt        |      |      D ]S  }g }	t        |      D ]0  }
|||
z      }|
|k\  r||	|
|z
     z  }|dz  }|	j                  |       2 |j	                  |	       U t        |      S )zReverse the effect of the TIFF predictor 2

    Documentation: https://www.itu.int/itudoc/itu-t/com16/tiff-fx/docs/tiff6.pdf (Section 14, page 64)
       z Unsupported `bitspercomponent': r      )r   rangerR   appendextendrE   )rc   rd   re   rf   	error_msgbppnbytesbuf
scanline_irawi	new_values               r*   apply_tiff_predictorru      s     167G6HI	I&&
$)
*Cs]FCAs4y&1 
v 	"AZ!^,ICxSS\)	S 	JJy!	" 	

3 :r,   predc                    |dvrd|z  }t        |      ||z  |z  dz  }||z  dz  }g }t        d|z        }	t        dt        |      |dz         D ]  }
||
   }||
dz   |
dz   |z    }g }|dk(  rt        |      }nn|dk(  rJt	        |      D ]:  \  }}||z
  dk  rd}nt        |||z
           }||z   dz  }|j                  |       < n|dk(  r.t        ||	      D ]  \  }}||z   dz  }|j                  |         n|d	k(  r]t	        |      D ]N  \  }}||z
  dk  rd}nt        |||z
           }t        |	|         }|||z   dz  z   dz  }|j                  |       P n|d
k(  rwt	        |      D ]h  \  }}||z
  dk  rd}d}n"t        |||z
           }t        |	||z
           }t        |	|         }t        |||      }||z   dz  }|j                  |       j nt        d|z        |j                  |       |}	 t        |      S )zxReverse the effect of the PNG predictor

    Documentation: http://www.libpng.org/pub/png/spec/1.2/PNG-Filters.html
    )rh      z"Unsupported `bitspercomponent': %drh       r   rx      rQ         zUnsupported predictor value: %d)r   listrj   rR   	enumerateintrk   ziprb   rl   rE   )rv   rc   rd   re   rf   msgro   rn   rp   
line_aboverq   filter_typeline_encodedrr   jsub_x	raw_x_bppraw_xup_xprior_x	average_xpaeth_xprior_x_bpppaeths                           r*   apply_png_predictorr      s    v%25EEC  g 00A5F
#
#q
(C
Cg'(JAs4y&1*5 N
:&JNZ!^f-DE!|$CA &l3 "5s7Q; !I #CCL 1I*c1

5!" A "%\:!> "g3.

5!" A !*, 7 "9s7Q; !I #CCL 1Ijm,"i'&9a%??3F

5!" A (5 
"
7s7Q; !I"#K #CCL 1I"%jS&9":Kjm,'	7KH 5C/

5!
"   AK OPP

3
]N^ :r,   )rx   r   r   rx   r   r   MATRIX_IDENTITYc                     	 | \  }}}}t        |      t        |      t        |      t        |      fS # t        $ r t        d      w xY w)NzCould not parse rectangle)float
ValueErrorr   )rB   x0y0x1y1s        r*   
parse_rectr     sO    9RRRy%)U2Yb	99 97889s	   03 Am1m0c                     | \  }}}}}}|\  }}	}
}}}	 ||z  |
|z  z   |	|z  ||z  z   ||z  |
|z  z   |	|z  ||z  z   ||z  |
|z  z   |z   |	|z  ||z  z   |z   fS r.   r=   )r   r   a1b1c1d1e1f1a0b0c0d0e0f0s                 r*   mult_matrixr     s    !RRR!RRR5
R"r'
R"r'
R"r'
R"r'
R"r'B
R"r'B r,   mvc                 ^    | \  }}}}}}|\  }}	||||||z  |	|z  z   |z   ||z  |	|z  z   |z   fS )zTranslates a matrix by (x, y) inside the projection.

    The matrix is changed so that its origin is at the specified point in its own
    coordinate system. Note that this is different from translating it within the
    original coordinate system.r=   
r   r   abcdefxys
             r*   translate_matrixr   #  sT     Q1aAFQaAq1uq1u}q(!a%!a%-!*;;;r,   c                 V    | \  }}}}}}|\  }}	||z  ||	z  z   |z   ||z  ||	z  z   |z   fS )zApplies a matrix to a point.r=   r   s
             r*   apply_matrix_ptr   .  sJ    Q1aAFQq51q5=1a!ea!ema///r,   rectc           	         |\  }}}}||f}||f}||f}||f}	t        | |      \  }
}t        | |      \  }}t        | |      \  }}t        | |	      \  }}t        |
|||      t        ||||      t        |
|||      t        ||||      fS )a  Applies a matrix to a rectangle.

    Note that the result is not a rotated rectangle, but a rectangle with the same
    orientation that tightly fits the outside of the rotated content.

    :param m: The rotation matrix.
    :param rect: The rectangle coordinates (x0, y0, x1, y1), where x0 < x1 and y0 < y1.
    :returns a rectangle with the same orientation, but that would fit the rotated
        content.
    )r   minmax)r   r   r   r   r   r   left_bottomright_bottom	right_topleft_topleft1bottom1right1bottom2right2top1left2top2s                     r*   apply_matrix_rectr   5  s     RRr(K8LRIBxH&q+6UG'<8VW$Q	2NVT#Ax0MUD 	E5&&)GWdD)E5&&)GWdD)	 r,   c                 J    | \  }}}}}}|\  }}	||z  ||	z  z   ||z  ||	z  z   fS )zCEquivalent to apply_matrix_pt(M, (p,q)) - apply_matrix_pt(M, (0,0))r=   )
r   r   r   r   r   r   r   r   r^   qs
             r*   apply_matrix_normr   S  s@    Q1aAFQq51q5=!a%!a%-''r,   r   c                 .    t        | t        t        f      S r.   )r   r   r   r   s    r*   isnumberr   ]  s    a#u&&r,   _Tobjsc              #   b   K   t               }| D ]  }||v r|j                  |       |  yw)zEliminates duplicated elements.N)setadd)r   doneobjs      r*   uniqr   d  s6     5D $;		s   -/c                 v    g }g }|D ]-  } | |      r|j                  |       |j                  |       / ||fS )z9Split a list into two classes according to the predicate.)rk   )rv   r   tr   r   s        r*   fsplitr   n  sC    
A
A 9HHSMHHSM	
 a4Kr,   v0v1r   c                 P    t        t        |       |z  t        ||z         |z        S )zReturns a discrete range.)rj   r   )r   r   r   s      r*   dranger   z  s$    RAs26{a/00r,   ptsc                     t         t         t          t          f}|\  }}}}| D ]5  \  }}t        ||      }t        ||      }t        ||      }t        ||      }7 ||||fS )z7Compute a minimal rectangle that covers all the points.)INFr   r   )r   limitr   r   r   r   r   r   s           r*   	get_boundr     sq    cTC4(ERR 1QZQZQZQZ	
 r2r>r,   seqfuncmaxobjc                 @    d}| D ]  } ||      }|||k  s||}} |S )z;Picks the object obj where func(obj) has the highest value.Nr=   )r   r   r   maxscorer   scores         r*   pickr     s>     H .S	x%/"'vX. Mr,   n.c              #   |   K   g }|D ]1  }|j                  |       t        |      | k(  s#t        |       g }3 yw)z$Groups every n elements of the list.N)rk   rR   tuple)r   r   rr   s       r*   choplistr     s>     
A 	q6Q;(NA	s   '<<defaultc                 P    t        |       }|s|S t        j                  | dd      S )z7Unpacks variable-length unsigned integers (big endian).bigF)	byteordersigned)rR   r   
from_bytes)rL   r   rS   s      r*   nunpackr     s(    VF~~a5~??r,    c              #   2   K   | ]  }t        |        y wr.   )chr).0r   s     r*   	<genexpr>r     s      D	 FDs   (   r   rx   rQ   r{   r|   rP      rO   rh   	   
                                       r	  i  i  i  i  i  i  i  i      !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /   0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?   @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _   `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~   r   i"   i    i!   i&   i   i   i  iD   i9   i:   i"  i0   i   i   i   i   i   i   i"!  i  i  iA  iR  i`  ix  i}  i1  iB  iS  ia  i~  r   i                                       r                                                                                                                                                                                                                                                      rz   c                 t    | j                  d      rt        | dd dd      S dj                  d | D              S )z+Decodes a PDFDocEncoding string to Unicode.s   rQ   Nzutf-16beignorer   c              3   .   K   | ]  }t         |     y wr.   )PDFDocEncoding)r   r   s     r*   r   zdecode_text.<locals>.<genexpr>  s     4Q~a(4s   )
startswithr!   join)rL   s    r*   decode_textr    s8    ||K 1QR5*h//ww4!444r,   c                 :    t        | t              ryt        |       S )z"Encodes a string for SGML/XML/HTMLr   )r   rE   r   r   s    r*   rJ   rJ     s    !U!9r,   bboxc                 2    | \  }}}}|dd|dd|dd|dS )Nz.3f,r=   )r  r   r   r   r   s        r*   bbox2strr    s5    RRXQr#ha3xqC11r,   c                 J    | \  }}}}}}d|dd|dd|dd|dd|dd|ddS )N[z.2fr  z, (z)]r=   )r   r   r   r   r   r   r   s          r*   
matrix2strr    sM    Q1aAqgQqgQqgQqgS3q3rBBr,   obj1r   obj2c                    t        | j                  |j                        t        | j                  |j                        }}t        | j                  |j                        t        | j
                  |j
                        }}||z
  ||z
  }}|| j                  z
  |j                  z
  || j                  z
  |j                  z
  }	}|dk  r|	dk  rz| j                  | j                  z   dz  | j                  | j
                  z   dz  }}
|j                  |j                  z   dz  |j                  |j
                  z   dz  }}|
|z
  ||z
  fS t        d|      t        d|	      fS )a  A distance function between two TextBoxes.

    Consider the bounding rectangle for obj1 and obj2.
    Return vector between 2 boxes boundaries if they don't overlap, otherwise
    returns vector betweeen boxes centers

             +------+..........+ (x1, y1)
             | obj1 |          :
             +------+www+------+
             :          | obj2 |
    (x0, y0) +..........+------+
    r   rQ   )r   r   r   r   r   r   widthheight)r  r  r   r   r   r   owohiwihxc1yc1xc2yc2s                 r*   vecBetweenBoxesr    s     DGGTWW%s477DGG'<RDGGTWW%s477DGG'<RRbRTZZ$**,b4;;.>.LR	Av"q&ww(A-$''0AQ/Fcww(A-$''0AQ/FcSy#)##1bz3q":%%r,   LTComponentT)boundc                       e Zd ZdZddededdfdZdefdZde	e
   fdZdefd	Zd
edefdZdede	e   fdZdee
   ddfdZd
e
ddfdZd
e
ddfdZdede	e
   fdZy)PlanezA set-like data structure for objects placed on a plane.

    Can efficiently find objects in a certain rectangular area.
    It maintains two parallel lists of objects, each of
    which is sorted by its x or y coordinate.
    r  gridsizer   Nc                     g | _         t               | _        i | _        || _        |\  | _        | _        | _        | _        y r.   )	_seqr   _objs_gridr  r   r   r   r   )r)   r  r  s      r*   r+   zPlane.__init__  s6    (*	(+
68
 /3,$'47DGr,   c                     dt        |       z  S )Nz<Plane objs=%r>)r}   r/   s    r*   __repr__zPlane.__repr__  s     4:--r,   c                 .      fd j                   D        S )Nc              3   @   K   | ]  }|j                   v s|  y wr.   r  )r   r   r)   s     r*   r   z!Plane.__iter__.<locals>.<genexpr>  s     >C4::,=>s   )r  r/   s   `r*   __iter__zPlane.__iter__  s    >tyy>>r,   c                 ,    t        | j                        S r.   )rR   r  r/   s    r*   __len__zPlane.__len__  s    4::r,   r   c                     || j                   v S r.   r  )r)   r   s     r*   __contains__zPlane.__contains__  s    djj  r,   c              #     K   |\  }}}}|| j                   k  s-| j                  |k  s|| j                  k  s| j                  |k  ry t	        | j                   |      }t	        | j                  |      }t        | j                  |      }t        | j                  |      }t        ||| j                        D ]$  }t        ||| j                        D ]  }||f 
 & y wr.   )r   r   r   r   r   r   r   r  )r)   r  r   r   r   r   grid_ygrid_xs           r*   	_getrangezPlane._getrange  s     RR=DGGrMR477]dggm""""RT]]3 	'F R7 'v&&'	's   CCr   c                 4    |D ]  }| j                  |        y r.   )r   )r)   r   r   s      r*   rl   zPlane.extend  s     	CHHSM	r,   c                 t   | j                  |j                  |j                  |j                  |j                  f      D ]B  }|| j
                  vrg }|| j
                  |<   n| j
                  |   }|j                  |       D | j                  j                  |       | j                  j                  |       y)zPlace an object.N)
r  r   r   r   r   r  rk   r  r  r   )r)   r   kr   s       r*   r   z	Plane.add  s     @A 	A

"(* !

1JJqMHHSM	 			

sr,   c                 &   | j                  |j                  |j                  |j                  |j                  f      D ]!  }	 | j
                  |   j                  |       # | j                  j                  |       y# t        t        f$ r Y Qw xY w)zDisplace an object.N)
r  r   r   r   r   r  removeKeyErrorr   r  )r)   r   r  s      r*   r  zPlane.remove   s{     @A 	A

1$$S)	
 	

# j) s   A>>BBc              #   N  K   |\  }}}}t               }| j                  |      D ]|  }|| j                  vr| j                  |   D ]Y  }||v r|j                  |       |j                  |k  s-||j
                  k  s|j                  |k  s||j                  k  rV| [ ~ yw)z)Finds objects that are in a certain area.N)r   r  r  r   r   r   r   r   )	r)   r  r   r   r   r   r   r  r   s	            r*   findz
Plane.find)  s     RRu% 		A

"zz!} $;66R<2<366R<2<			s   B#B%)r  )r7   r8   r9   r:   Rectr   r+   r!   r  r   r  r  r  r<   boolr  Pointr  r
   rl   r   r  r  r=   r,   r*   r  r    s    4T 4S 4$ 4.# .?(<0 ? ! !4 !
'd 
'x 
'8L1 d 
| 
 
, 4  (<"8 r,   r  )rs   r   r   r   )r   lr   valuec                    d| cxk  rdk  sJ  J g }d}| dk7  rt        | d      \  } }|dk(  r6|j                  dt        |          |j                  dt        |dz             n}|dk(  r3|j                  dt        |          |j                  dt        |          nE|dk\  }|r|j                  dt        |          |dz  }|j                  |rdndt        |   |z         |dz  }| dk7  rdj	                  |      S )	z,Format a number as lowercase Roman numerals.r   i  r   r   rx   r|   rP   r   )divmodinsert
ROMAN_ONESROMAN_FIVESr  )r  resultindex	remainder	over_fives        r*   format_int_romanr  =  s   utFE
1*!%,y>MM!Z./MM!Z	23!^MM!Z./MM![/0!QIaU!34Q	MMy!aE1BY1NO
 1*  776?r,   c                    | dkD  sJ g }| dk7  rQt        | dz
  t        t        j                              \  } }|j	                  t        j                  |          | dk7  rQ|j                          dj                  |      S )z5Format a number as lowercase letters a-z, aa-zz, etc.r   rx   r   )r  rR   stringascii_lowercaserk   reverser  )r  r  r  s      r*   format_int_alphar  V  st    199F
1*!%!)S1G1G-HIyf,,Y78 1* NN776?r,   paddedc                     t        |       dk(  r| S | d   dkD  r| S t        |       kD  r| S t        fd|  d D              r| d  S | S )a1  Remove block padding as described in PDF 1.7 section 7.6.2:

    > For an original message length of M, the pad shall consist of 16 -
    (M mod 16) bytes whose value shall also be 16 - (M mod 16).
    > Note that the pad is present when M is evenly divisible by 16;
    it contains 16 bytes of 0x10.
    r   r  c              3   (   K   | ]	  }|k(    y wr.   r=   )r   r   paddings     r*   r   zunpad_aes.<locals>.<genexpr>u  s     
3A1<
3s   N)rR   all)r  r  s    @r*   	unpad_aesr  c  sg     6{aRjG|V

3	!2
33ix  Mr,   )zutf-8r  r.   )r   )Tr:   r%   r   r  htmlr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   pdfminer.pdfexceptionsr   r   pdfminer.layoutr   rF   r   r    r!   r&   r;   r'   r   rE   rA   r<   rK   r   rT   rX   rb   ru   r   r   r  r  MatrixPathSegmentr   __annotations__r   r   r   r   r   r   r  r   r   r   r   rj   r   r   r   r   r   r  r  r  rJ   r  r  r  r  r  r
  r  r  r  r  r=   r,   r*   <module>r$     s    	       & ?+  7##S"))34
fh& &4c e 	v 	# 	3 c c  5$55 5 		5# c s s $14<A
4b
bb b 	b
 b bJ 	eUlUE5%'(	ueUE5%7	8	#J	#ue
	#ueUE
)*	#ueUE5%
78: - ,9# 9$ 9F  6 < <5 <V <0v 0% 0E 0 t  <( (E (e (' '4 ' T]x|  	2$*% 	Xb\ 	eDHdSUhDV>W 	1u 1% 1C 1E 1
	8E? 	t 	  	"
B4;
 RL b\	 (2, 8E"c'N+C @u @s @3 @  DAD DN55 5S 53 3 24 2C 2
C& CS C
&- &} & &4 ~];MGL! M` "
C C 2
C 
C 
e  r,   