o
    hP                     @   s   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rId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	 d	Z#d
ede$fddZ%de&defddZ'dede(defddZ)		drdee$ef dededefddZ*de(de(de(de(fdd Z+d!e(d"e(d#e(d$e(d%e$de$fd&d'Z,ee-e-f Z.ee-e-e-e-f Z/ee-e-e-e-e-e-f Z0eee eee-e-f eee-e-e-e-f eee-e-e-e-e-e-f f Z1d(Z2e0e3d)< dede/fd*d+Z4d,e0d-e0de0fd.d/Z5d0e0d1e.de0fd2d3Z6d0e0d1e.de.fd4d5Z7d0e0d1e.de.fd6d7Z8d8e&de9fd9d:Z:ed;Z;d<ee; dee; fd=d>Z<d!e
e;ge9f d<ee; deee; ee; f fd?d@Z=dAe-dBe-dCe(de>fdDdEZ?dFee. de/fdGdHZ@	dsdIee; dJe
e;ge-f dKee; dee; fdLdMZAdNe(dIee; deee;dOf  fdPdQZBdtde$dRe(de(fdSdTZCdUDdVdW dXD ZEde$defdYdZZFd8edefd[d\ZGd]e/defd^d_ZHd0e0defd`daZIdbdcdddcde.fdedfZJedgdcdhZKG didj djeeK ZLg dkZMg dlZNdme(defdndoZOdme(defdpdqZPdS )uz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                   @   sR   e Zd ZdZdedededdfddZdefd	d
Zde	de	de	ddfddZ
dS )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                 O   sr   t |tjr
t|}t |tr t|g|R i || _d| _d S t |tjr1t	t
|| _d| _d S tdt| )NTFzUnsupported input type: %s)
isinstancepathlibPurePathstropenfile_handlerclosingioIOBaser   AnyIOr   type)selfr   r   r    r'   A/var/www/html/venv/lib/python3.10/site-packages/pdfminer/utils.py__init__0   s   


zopen_filename.__init__c                 C   s   | j S N)r    r&   r'   r'   r(   	__enter__<   s   zopen_filename.__enter__exc_typeexc_valexc_tbc                 C   s   | j r
| j  d S d S r*   )r!   r    close)r&   r-   r.   r/   r'   r'   r(   __exit__?   s   zopen_filename.__exit__)__name__
__module____qualname____doc__
FileOrNamer   r)   r$   r,   objectr1   r'   r'   r'   r(   r   *   s
    r   in_strr   c                 C   s"   t | tsJ tt| |  S )z'Converts to bytes, encoding to unicode.)r   r   r%   encode)r8   r'   r'   r(   make_compat_bytesD   s   r:   oc                 C   sH   t | tr t| }z| |d W S  ty   t|  Y S w t| S )z>Converts everything to string, if bytes guessing the encoding.encoding)r   bytescharset_normalizerdetectdecodeUnicodeDecodeErrorr   )r;   encr'   r'   r(   make_compat_strJ   s   

rC   ssizec                 C   sP   |dk r
| d | S t | |kr&|d d }| d |  d| | d   S | S )N         z ... )len)rD   rE   lengthr'   r'   r(   shorten_strV   s    rK   utf-8ignorebytesorstringr<   	erractionc                 C   s4   t | tr| S t | tsJ tt| | ||S )z^When Py2 str.encode is called, it often means bytes.encode in Py3.

    This does either.
    )r   r   r=   r%   r@   )rN   r<   rO   r'   r'   r(   compatible_encode_method`   s   
	rP   leftabove
upper_leftc                 C   sT   | | | }t ||  }t || }t || }||kr"||kr"| S ||kr(|S |S r*   )abs)rQ   rR   rS   ppaZpbZpcr'   r'   r(   paeth_predictoro   s   rW   predcolorscolumnsbitspercomponentdatac                 C   s(  |dvrd| }t ||| | d }|| d }g }td| }	tdt||d D ]}
||
 }||
d |
d |  }g }|dkrIt|}n|dkrst|D ] \}}|| dk r^d}nt|||  }|| d@ }|| qQn|dkrt||	D ]\}}|| d@ }|| q|n{|d	krt|D ]*\}}|| dk rd}nt|||  }t|	| }||| d  d@ }|| qnG|d
krt|D ]6\}}|| dk rd}d}nt|||  }t|	||  }t|	| }t|||}|| d@ }|| qnt d| |	| |}	q,t
|S )zxReverse the effect of the PNG predictor

    Documentation: http://www.libpng.org/pub/png/spec/1.2/PNG-Filters.html
    )      z"Unsupported `bitspercomponent': %dr]       r   r^      rH         zUnsupported predictor value: %d)r   listrangerI   	enumerateintappendziprW   extendr=   )rX   rY   rZ   r[   r\   msgnbytesZbppbufZ
line_aboveZ
scanline_iZfilter_typeZline_encodedrawjZsub_xZ	raw_x_bppZraw_xZup_xZprior_xZ	average_xZpaeth_xZprior_x_bppZpaethr'   r'   r(   apply_png_predictor   sf   
	
		
ro   )r^   r   r   r^   r   r   MATRIX_IDENTITYc                 C   sB   z| \}}}}t |t |t |t |fW S  ty    tdw )NzCould not parse rectangle)float
ValueErrorr   )r;   x0y0x1y1r'   r'   r(   
parse_rect   s   rw   m1m0c                 C   s   | \}}}}}}|\}}	}
}}}	 || |
|  |	| ||  || |
|  |	| ||  || |
|  | |	| ||  | fS r*   r'   )rx   ry   Za1b1c1Zd1e1f1Za0Zb0Zc0Zd0Ze0Zf0r'   r'   r(   mult_matrix   s   r~   mvc           
      C   sH   | \}}}}}}|\}}	|||||| |	|  | || |	|  | fS )zTranslates a matrix by (x, y).r'   
r   r   abcdefxyr'   r'   r(   translate_matrix
  s   0r   c           
      C   sB   | \}}}}}}|\}}		 || ||	  | || ||	  | fS r*   r'   r   r'   r'   r(   apply_matrix_pt  s   (r   c           
      C   s8   | \}}}}}}|\}}	|| ||	  || ||	  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   rU   qr'   r'   r(   apply_matrix_norm  s    r   r   c                 C   s   t | ttfS r*   )r   rf   rq   r   r'   r'   r(   isnumber"  s   r   _Tobjsc                 c   s0    t  }| D ]}||v rq|| |V  qdS )zEliminates duplicated elements.N)setadd)r   doneobjr'   r'   r(   uniq)  s   
r   c                 C   s8   g }g }|D ]}| |r| | q| | q||fS )z9Split a list into two classes according to the predicate.)rg   )rX   r   tr   r   r'   r'   r(   fsplit3  s   r   v0v1r   c                 C   s   t t| | t|| | S )zReturns a discrete range.)rd   rf   )r   r   r   r'   r'   r(   drange?  s   r   ptsc                 C   s^   t t t  t  f}|\}}}}| D ]\}}t||}t||}t||}t||}q||||fS )z7Compute a minimal rectangle that covers all the points.)INFminmax)r   limitrs   rt   ru   rv   r   r   r'   r'   r(   	get_boundD  s   


r   seqfuncmaxobjc                 C   s4   d}| D ]}||}|du s||k r||}}q|S )z;Picks the object obj where func(obj) has the highest value.Nr'   )r   r   r   Zmaxscorer   Zscorer'   r'   r(   pickP  s   
r   n.c                 c   s8    g }|D ]}| | t|| krt|V  g }qdS )z$Groups every n elements of the list.N)rg   rI   tuple)r   r   rr   r'   r'   r(   choplist^  s   

r   defaultc                 C   s    t | }|s|S tj| dddS )z7Unpacks variable-length unsigned integers (big endian).bigF)	byteordersigned)rI   rf   
from_bytes)rD   r   rJ   r'   r'   r(   nunpackh  s   r    c                 c   s    | ]}t |V  qd S r*   )chr).0r   r'   r'   r(   	<genexpr>q  s
    
r   (   r   r^   rH   ra   rb   rG      rF   r]   	   
                                       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                                                                                                                                                                                                                                                      r`   c                 C   s2   |  drt| dd ddS ddd | D S )	z+Decodes a PDFDocEncoding string to Unicode.s   rH   Nzutf-16berM   r   c                 s   s    | ]}t | V  qd S r*   )PDFDocEncoding)r   r   r'   r'   r(   r   }  s    zdecode_text.<locals>.<genexpr>)
startswithr   join)rD   r'   r'   r(   decode_textx  s   
r  c                 C   s   t | trdS t| S )z"Encodes a string for SGML/XML/HTMLr   )r   r=   r   r   r'   r'   r(   rB     s   
rB   bboxc                 C   s.   | \}}}}|dd|dd|dd|dS )Nz.3f,r'   )r  rs   rt   ru   rv   r'   r'   r(   bbox2str  s   "r  c                 C   sF   | \}}}}}}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   r'   r'   r(   
matrix2str  s   6r  obj1r   obj2c                 C   s   t | j|jt | j|j}}t| j|jt| j|j}}|| || }}|| j |j || j |j }}	|dk rj|	dk rj| j| j d | j| j d }
}|j|j d |j|j d }}|
| || 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   rH   )r   rs   rt   r   ru   rv   widthheight)r  r  rs   rt   ru   rv   owZohiwZihZxc1Zyc1Zxc2Zyc2r'   r'   r(   vecBetweenBoxes  s   """r  LTComponentT)boundc                   @   s   e Zd ZdZddededdfddZdefd	d
Zde	e
 fddZdefddZdedefddZdede	e fddZdee
 ddfddZde
ddfddZde
ddfddZdede	e
 fddZdS )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   r  gridsizer   Nc                 C   s2   g | _ t | _i | _|| _|\| _| _| _| _d S r*   )	_seqr   _objs_gridr  rs   rt   ru   rv   )r&   r  r  r'   r'   r(   r)     s
   zPlane.__init__c                 C   s   dt |  S )Nz<Plane objs=%r>)rc   r+   r'   r'   r(   __repr__  s   zPlane.__repr__c                    s    fdd j D S )Nc                 3   s    | ]
}| j v r|V  qd S r*   r  )r   r   r+   r'   r(   r     s    z!Plane.__iter__.<locals>.<genexpr>)r  r+   r'   r+   r(   __iter__  s   zPlane.__iter__c                 C   s
   t | jS r*   )rI   r  r+   r'   r'   r(   __len__     
zPlane.__len__r   c                 C   s
   || j v S r*   r  )r&   r   r'   r'   r(   __contains__  r  zPlane.__contains__c                 c   s    |\}}}}|| j ks| j|ks|| jks| j|krd S t| j |}t| j|}t| j|}t| j|}t||| jD ]}t||| jD ]}||fV  qEq<d S r*   )rs   ru   rt   rv   r   r   r   r  )r&   r  rs   rt   ru   rv   Zgrid_yZgrid_xr'   r'   r(   	_getrange  s   (zPlane._getranger   c                 C   s   |D ]}|  | qd S r*   )r   )r&   r   r   r'   r'   r(   ri     s   zPlane.extendc                 C   sj   |  |j|j|j|jfD ]}|| jvrg }|| j|< n| j| }|| q| j| | j	| dS )zPlace an object.N)
r  rs   rt   ru   rv   r  rg   r  r  r   )r&   r   kr   r'   r'   r(   r     s   

z	Plane.addc              
   C   sZ   |  |j|j|j|jfD ]}z
| j| | W q ttfy$   Y qw | j	| dS )zDisplace an object.N)
r  rs   rt   ru   rv   r  removeKeyErrorrr   r  )r&   r   r  r'   r'   r(   r    s   zPlane.removec           	      c   s    |\}}}}t  }| |D ]2}|| jvrq| j| D ]$}||v r#q|| |j|ks<||jks<|j|ks<||jkr=q|V  qqdS )z)Finds objects that are in a certain area.N)r   r  r  r   ru   rs   rv   rt   )	r&   r  rs   rt   ru   rv   r   r  r   r'   r'   r(   find  s   

(z
Plane.find)r   )r2   r3   r4   r5   Rectrf   r)   r   r  r
   r  r  r  r7   boolr  Pointr  r	   ri   r   r  r  r'   r'   r'   r(   r    s    	r  )ir   r   r   )r   lr   valuec                 C   s   d|   k rdk sJ  J g }d}| dkrqt | d\} }|dkr4|dt|  |dt|d   n5|dkrI|dt|  |dt|  n |dk}|r[|dt|  |d8 }||radndt| |  |d7 }| dksd|S )	z,Format a number as lowercase Roman numerals.r   i  r   r   r^   rb   rG   r   )divmodinsert
ROMAN_ONESROMAN_FIVESr  )r  resultindex	remainderZ	over_fiver'   r'   r(   format_int_roman  s&   
r  c                 C   sZ   | dksJ g }| dkr$t | d ttj\} }|tj|  | dks|  d|S )z5Format a number as lowercase letters a-z, aa-zz, etc.r   r^   r   )r  rI   stringascii_lowercaserg   reverser  )r  r  r  r'   r'   r(   format_int_alpha  s   
r  )rL   rM   r*   )r   )Qr5   r"   r   r  htmlr   typingr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   Zpdfminer.pdfexceptionsr   r   Zpdfminer.layoutr   r>   r   r   r   r#   r6   r$   r   r=   r:   r7   rC   rf   rK   rP   rW   ro   rq   r  r  ZMatrixZPathSegmentrp   __annotations__rw   r~   r   r   r   r  r   r   r   r   rd   r   r   r   r   r   r  r  r  rB   r  r  r  r  r  r  r  r  r  r'   r'   r'   r(   <module>   s    H


e
4

&

	  	P