
    @wg                       d dl Z d dlmZ d dl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 ddlmZmZmZmZm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(m)Z)m*Z*m+Z+m,Z, ddl-m.Z/ ddl-m0Z1 ddl-m2Z3 ddl-m4Z5 ddl6m7Z7m8Z8 ddl9m:Z: ddl;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZI 	 d dlJmKZK dZLdZOde
dePdeeP   deFfdZQde
dePdeeFeRf   ddfdZSde
dePddfdZTdePdeeP   deUfd ZV G d! d"      ZWe G d# d$             ZX G d% d&eeX         ZY G d' d(e>      ZZ G d) d*eeZ         Z[d+e>d,eeP   d-eeP   deeeP   eeP   f   fd.Z\y# eM$ r eNZKdZLY w xY w)/    N)	dataclass)Decimal)BytesIO)Path)AnyCallableDictIterableIteratorListLiteralOptionalSequenceSetTupleUnioncastoverload   )build_char_mapbuild_font_width_mapcompute_font_widthget_actual_str_keyunknown_char_map)PdfCommonDocProtocol)OrientationNotFoundError_layout_modecrlf_space_checkget_display_strget_text_operandsmult)CompressedTransformationMatrixTransformationMatrixType_human_readable_byteslogger_warningmatrix_multiply)AnnotationDictionaryAttributes)ImageAttributes)PageAttributes)	Resources)PageSizeNotDefinedErrorPdfReadError_xobj_to_image)ArrayObjectContentStreamDictionaryObjectEncodedStreamObjectFloatObjectIndirectObject
NameObject
NullObjectNumberObject	PdfObjectRectangleObjectStreamObjectTextStringObjectis_null_or_none)ImageFTcropboxselfnamedefaultsreturnc                     | j                  |      }t        |t              r|S t        |      r|D ]  }| j                  |      }| n t        |t              r| j
                  j                  |      }t        |      }t        | ||       |S N)get
isinstancer9   r<   r4   pdf
get_object_set_rectangle)r?   r@   rA   retvalds        B/var/www/horilla/myenv/lib/python3.12/site-packages/pypdf/_page.py_get_rectanglerM   m   s    ;?88D>F&/*v 	AXXa[F!	 &.)$$V,V$F4v&M    valuec                 $    t        |      }|| |<   y rD   r5   )r?   r@   rO   s      rL   rI   rI   }   s    dDDJrN   c                 
    | |= y rD    r?   r@   s     rL   _delete_rectanglerU      s	    T
rN   fallbackc                 4     t         fd fd fd      S )Nc                     t        |       S rD   )rM   )r?   rV   r@   s    rL   <lambda>z,_create_rectangle_accessor.<locals>.<lambda>   s    ^D$9 rN   c                     t        | |      S rD   )rI   )r?   rO   r@   s     rL   rY   z,_create_rectangle_accessor.<locals>.<lambda>   s    N4u= rN   c                     t        |       S rD   )rU   rT   s    rL   rY   z,_create_rectangle_accessor.<locals>.<lambda>   s    &tT2 rN   )property)r@   rV   s   ``rL   _create_rectangle_accessorr]      s    9=2 rN   c            
       Z   e Zd ZdZddefdZedefd       Ze	dedefd       Z
ddZdd	ed
edd fdZ	 ddee   dee   dd fdZdedd fdZdefdZeddee   dedee   fd       Ze	 ddeeef   dedeeef   fd       Z	 ddeeeef   ee   f   dedeeeef   ee   f   fdZy)Transformationu  
    Represent a 2D transformation.

    The transformation between two coordinate systems is represented by a 3-by-3
    transformation matrix matrix with the following form::

        a b 0
        c d 0
        e f 1

    Because a transformation matrix has only six elements that can be changed,
    it is usually specified in PDF as the six-element array [ a b c d e f ].

    Coordinate transformations are expressed as matrix multiplications::

                                 a b 0
     [ x′ y′ 1 ] = [ x y 1 ] ×   c d 0
                                 e f 1


    Example:
        >>> from pypdf import Transformation
        >>> op = Transformation().scale(sx=2, sy=3).translate(tx=10, ty=20)
        >>> page.add_transformation(op)

    ctmc                     || _         y rD   r`   )r?   r`   s     rL   __init__zTransformation.__init__   s	    rN   rB   c                     | j                   d   | j                   d   df| j                   d   | j                   d   df| j                   d   | j                   d   dffS )z
        Return the transformation matrix as a tuple of tuples in the form:

        ((a, b, 0), (c, d, 0), (e, f, 1))
        r   r               rb   r?   s    rL   matrixzTransformation.matrix   s\     XXa[$((1+q)XXa[$((1+q)XXa[$((1+q)
 	
rN   rj   c                 Z    | d   d   | d   d   | d   d   | d   d   | d   d   | d   d   fS )a  
        Compresses the transformation matrix into a tuple of (a, b, c, d, e, f).

        Args:
            matrix: The transformation matrix as a tuple of tuples.

        Returns:
            A tuple representing the transformation matrix as (a, b, c, d, e, f)

        r   r   re   rS   )rj   s    rL   compresszTransformation.compress   sP     1IaL1IaL1IaL1IaL1IaL1IaL
 	
rN   c                 ~    t         j                  t        | j                  |j                              }t        |      S )a  
        Apply one transformation to another.

        Args:
            m: a Transformation to apply.

        Returns:
            A new ``Transformation`` instance

        Example:
            >>> from pypdf import Transformation
            >>> op = Transformation((1, 0, 0, -1, 0, height)) # vertical mirror
            >>> op = Transformation().transform(Transformation((-1, 0, 0, 1, iwidth, 0))) # horizontal mirror
            >>> page.add_transformation(op)

        )r_   rl   r&   rj   )r?   mr`   s      rL   	transformzTransformation.transform   s.    " %%odkk188&LMc""rN   txtyc           	      n    | j                   }t        |d   |d   |d   |d   |d   |z   |d   |z   f      S )z
        Translate the contents of a page.

        Args:
            tx: The translation along the x-axis.
            ty: The translation along the y-axis.

        Returns:
            A new ``Transformation`` instance

        r   r   re   rf   rg   rh   rb   )r`   r_   )r?   rp   rq   rn   s       rL   	translatezTransformation.translate   sE     HH1Q41qtQqT1Q4"9adRi"PQQrN   Nsxsyc                     ||t        d      ||}||}|J |J |ddfd|dfdf}t        j                  t        | j                  |            }t        |      S )a  
        Scale the contents of a page towards the origin of the coordinate system.

        Typically, that is the lower-left corner of the page. That can be
        changed by translating the contents / the page boxes.

        Args:
            sx: The scale factor along the x-axis.
            sy: The scale factor along the y-axis.

        Returns:
            A new Transformation instance with the scaled matrix.

        z!Either sx or sy must be specifiedr   r   r   r   )
ValueErrorr_   rl   r&   rj   )r?   rt   ru   opr`   s        rL   scalezTransformation.scale   s    " :"*@AA:B:B~~~~)+Q
QAJ	'J%%odkk2&FGc""rN   rotationc                 D   t        j                  |      }t        j                  |      t        j                  |      dft        j                  |       t        j                  |      dfdf}t        j                  t        | j                  |            }t	        |      S )z
        Rotate the contents of a page.

        Args:
            rotation: The angle of rotation in degrees.

        Returns:
            A new ``Transformation`` instance with the rotated matrix.

        r   rw   )mathradianscossinr_   rl   r&   rj   )r?   r{   ry   r`   s       rL   rotatezTransformation.rotate  s     <<)XXh(!3Q7hhx  $((8"4a8(

 %%odkk2&FGc""rN   c                 "    d| j                    dS )NzTransformation(ctm=)rb   ri   s    rL   __repr__zTransformation.__repr__%  s    $TXXJa00rN   pt	as_objectc                      y rD   rS   r?   r   r   s      rL   apply_onzTransformation.apply_on(      rN   c                      y rD   rS   r   s      rL   r   zTransformation.apply_on,  s     	rN   c                    |rt         nt        } |t        |d         | j                  d   z  t        |d         | j                  d   z  z   | j                  d   z          |t        |d         | j                  d   z  t        |d         | j                  d   z  z   | j                  d   z         f}t        |t              rt	        |      S |S )a  
        Apply the transformation matrix on the given point.

        Args:
            pt: A tuple or list representing the point in the form (x, y)

        Returns:
            A tuple or list representing the transformed point in the form (x', y')

        r   r   re   rg   rf   rh   )r3   floatr`   rF   list)r?   r   r   typpt1s        rL   r   zTransformation.apply_on2  s     'kEbetxx{*U2a5\DHHQK-GG$((ST+UVbetxx{*U2a5\DHHQK-GG$((ST+UV
 'r40tCy9c9rN   ))r   r   r   r   r   r   )rn   r_   rB   r_   )r   r   NNF)__name__
__module____qualname____doc__r"   rc   r\   r#   rj   staticmethodrl   ro   r   rs   r   rz   r   strr   r   r   boolr   r   r   rS   rN   rL   r_   r_      s   ::  

0 

 

 
1 
6T 
 
(#(RE R5 R9I R  AE#5/#.6uo#	#:#u #)9 #(1# 1 4; 4 DK   9>u%26	ue|	   :%u%tE{23: : 
uUE\"DK/	0	:rN   r_   c                       e Zd ZU dZdZeed<   	 dZeed<   	 dZ	e
e   ed<   	 dZe
e   ed<   	 d	ed
eddfdZdefdZdefdZy)	ImageFilez
    Image within the PDF file. *This object is not designed to be built.*

    This object should not be modified except using :func:`ImageFile.replace` to replace the image with a new one.
     r@   rN   dataNimageindirect_reference	new_imagekwargsrB   c                    t         rt        d      ddlm} ddlm} ddlm}m} | j                  t        d      t        | j                  j                  d      st        d	      t        |t              st        d
      t               } |j                   |dfi |  ||      }|j"                  d   j$                  d   j                  J |j"                  d   j$                  d   j                  j'                         | j                  j                  j(                  | j                  j*                  dz
  <   | j                  t-        || j                  j'                               _	         |t-        || j                  j'                                     \  }	}
}|	J | j.                  d| j.                  j1                  d       |	z   | _        |
| _        || _        y)a  
        Replace the image with a new PIL image.

        Args:
            new_image (PIL.Image.Image): The new PIL image to replace the existing image.
            **kwargs: Additional keyword arguments to pass to `Image.save()`.

        Raises:
            TypeError: If the image is inline or in a PdfReader.
            TypeError: If the image does not belong to a PdfWriter.
            TypeError: If `new_image` is not a PIL Image.

        Note:
            This method replaces the existing image with a new image.
            It is not allowed for inline images or images within a PdfReader.
            The `kwargs` parameter allows passing additional parameters
            to `Image.save()`, such as quality.

        z]pillow is required to do image extraction. It can be installed via 'pip install pypdf[image]'r   )	PdfReaderr-   )r1   r8   NzCannot update an inline image._id_translatedz4Cannot update an image not belonging to a PdfWriter.znew_image shall be a PIL ImagePDFr   .)pil_not_importedImportError_readerr   filtersr.   genericr1   r8   r   	TypeErrorhasattrrG   rF   r=   r   savepagesimagesrH   _objectsidnumr   r@   rfindr   r   )r?   r   r   r   r.   r1   r8   breader	extensionbyte_streamimgs               rL   replacezImageFile.replacee  s   ( E 
 	' 	,8""*<==t..224DERSS)U+<==I	q%*6*1||A%%a(;;GGGLLO""1%88CCE 	##,,T-D-D-J-JQ-NO
  $66 	t..99;	

&4!4#:#:#E#E#GH'
#	; $$$II4		 45	A		
rN   c           	          | j                   j                   d| j                   dt        t	        | j
                               dS )Nz(name=z, data: r   )	__class__r   r@   r$   lenr   ri   s    rL   __str__zImageFile.__str__  s?    ..))*&8DYZ]^b^g^gZhDiCjjkllrN   c                 \    | j                         d d dt        | j                         dz   S )Nz, hash: r   )r   hashr   ri   s    rL   r   zImageFile.__repr__  s,    ||~cr"xTYY/@%BBBrN   )r   r   r   r   r@   r   __annotations__r   bytesr   r   r=   r   r4   r   r   r   r   rS   rN   rL   r   r   I  s     D#N D% "E8E?! 48077 7# 7$ 7rm mC# CrN   r   c            	       h   e Zd ZdZdeg eeeee   f      f   deeeee   ee   f   ge	f   ddfdZ
defdZdeeeee   f      fdZdeeeeee   f   e	f      fd	Zed
eeeee   f   de	fd       Zed
edee	   fd       Zd
eeeeee   ee   f   dee	ee	   f   fdZdee	   fdZdefdZy)VirtualListImagesz
    Provides access to images referenced within a page.
    Only one copy will be returned if the usage is used on the same page multiple times.
    See :func:`PageObject.images` for more details.
    ids_functionget_functionrB   Nc                 .    || _         || _        d| _        y Nr   )r   r   current)r?   r   r   s      rL   rc   zVirtualListImages.__init__  s    
 )(rN   c                 4    t        | j                               S rD   )r   r   ri   s    rL   __len__zVirtualListImages.__len__  s    4$$&''rN   c                 "    | j                         S rD   r   ri   s    rL   keyszVirtualListImages.keys  s      ""rN   c                 R    | j                         D cg c]	  }|| |   f c}S c c}w rD   r   )r?   xs     rL   itemszVirtualListImages.items  s'    &*&7&7&9:DG:::s   $indexc                      y rD   rS   r?   r   s     rL   __getitem__zVirtualListImages.__getitem__  r   rN   c                      y rD   rS   r   s     rL   r   zVirtualListImages.__getitem__  r   rN   c                     | j                         t        |t              rTt        |j	                  t        |              }|D cg c]  }|   	 c}t        |       } |fd| j                        S t        |t        t        t        f      r| j                  |      S t        |t              st        d      t              }|dk  r||z   }|dk  s||k\  rt        d      | j                  |         S c c}w )Nc                       S rD   rS   )lsts   rL   rY   z/VirtualListImages.__getitem__.<locals>.<lambda>  s     rN   zInvalid sequence indices typer   Sequence index out of range)r   rF   slicerangeindicesr   typer   r   r   tupleintr   
IndexError)r?   r   r   r   clslen_selfr   s         @rL   r   zVirtualListImages.__getitem__  s     !eU#U]]3t956G#*+a3q6+Ct*Cd&7&788ec4/0$$U++%%;<<s819u$E19):;;  U,, ,s   C;c              #   L   K   t        t        |             D ]	  }| |     y wrD   r   r   r?   is     rL   __iter__zVirtualListImages.__iter__  '     s4y! 	Aq'M	   "$c                     t        | j                               D cg c]  \  }}d| d|  }}}ddj                  |       dS c c}}w )NImage_=[, ])	enumerater   join)r?   r   nps       rL   r   zVirtualListImages.__str__  sQ    *3D4E4E4G*HI$!QvaS!II499Q<."" Js   A)r   r   r   r   r   r   r   r   r   r   rc   r   r   r   r   r   r   r   r   r   r   r   rS   rN   rL   r   r     si   r4c49n(=#>>? c49eCj&@ ABIMN 
	( (#d5d3i01 #;tE%T#Y"7"BCD ; sCc':!; 	    8I+>  -3sDIuSzAB-	y(9--	.-*(9- # #rN   r   c                   :	    e Zd ZU dZd ed<   	 	 dwdee   dee   ddfdZde	fdZ
def fd	Zedefd
       Ze	 	 	 dxdee   deeedf   deeedf   dd fd       Z	 	 	 dxdee   deee      deee      deeeee   f      fdZ	 dydeeee   ee   f   dee   defdZedefd       ZdededefdZde eef   fdZ!ede	fd       Z"e"jF                  deddfd       Z"dzdZ$de	dd fdZ%	 d{ded ed!ed"e&dee eef   e eef   f   f
d#Z'ed$e(d%e eef   dee   de(fd&       Z)ed'edee   d(e*de(fd)       Z+dee   fd*Z,dee(   fd+Z-d,ede(e.e/f   ddfd-Z0	 d|d.d d/e&d0e&ddfd1Z1	 	 	 	 d}d.d d2ee2ege(f      d(ee*   d0e&d/e&ddfd3Z3	 	 	 	 d}d.d d2ee2ege(f      d(ee*   d0e&d/e&ddfd4Z4d.d d(ee*   ddfd5Z5	 	 d~d.d d(ee*e6f   d0e&d/e&ddf
d6Z7	 d~d.d d7ed0e&d/e&ddf
d8Z8	 	 d~d.d d9ed0e&d/e&ddf
d:Z9	 	 d~d.d d;ed<ed0e&d/e&ddfd=Z:	 dd(ee6e*f   d/e&ddfd>Z;d?ed@eddfdAZ<dBeddfdCZ=dededdfdDZ>ddEe	ddfdFZ?edee	   fdG       Z@defdHZAdIeeee e	ef   f   e eef   eee   f   dJedKedLedeeeef   f
dMZBdNedOeeeeCf      dPee   dQee   dIeeee e	ef   f   e eef   eee   f   dRee	dSf   dKedTe&dUee2eeeeegdf      dLedVe eef   deee&e eef   f   fdWZDdXdYeEj                  dddfdededRee	dSf   dLedZee   d[ee2eeeegdf      d\ee2eeeegdf      dUee2eeeeegdf      defd]ZGde eeHj                  f   fd^ZJ	 	 	 	 	 dd_e&d`edae&dbeeK   dcedefddZLdXdYddddedfdgedRee	ee	dSf   f   dLed[ee2eeeegdf      d\ee2eeeegdf      dUee2eeeeegdf      dheMdi   djedefdkZN	 	 	 	 	 ddle.dRee	dSf   dLed[ee2eeeegdf      d\ee2eeeegdf      dUee2eeeeegdf      defdmZOdeePe   ePe   f   fdnZQ eReEj                  do      ZT	  eRdpeEj                  f      ZU	  eRdqdpeEj                  f      ZV	  eRdrdpeEj                  f      ZW	  eRdsdpeEj                  f      ZX	 edee/   fdt       ZYeYjF                  duee/   ddfdv       ZY xZZS )
PageObjecta#  
    PageObject represents a single page within a PDF file.

    Typically these objects will be created by accessing the
    :attr:`pages<pypdf.PdfReader.pages>` property of the
    :class:`PdfReader<pypdf.PdfReader>` class, but it is
    also possible to create an empty page with the
    :meth:`create_blank_page()<pypdf._page.PageObject.create_blank_page>` static method.

    Args:
        pdf: PDF file the page belongs to.
        indirect_reference: Stores the original indirect reference to
            this object in its source PDF

    original_pageNrG   r   rB   c                     t        j                  |        || _        d | _        || _        t        |      s6|J d       | j                  t        t         |j                                      i | _	        y )Nmypy)
r1   rc   rG   inline_imagesr   r<   updater   rH   _font_width_maps)r?   rG   r   s      rL   rc   zPageObject.__init__  si    
 	!!$'=A"412%19691KK-/A/L/L/NOPPRrN   c                 `    t        t        t        d | j                         D              f      S )z
        Used to detect modified object.

        Note: this function is overloaded to return the same results
        as a DictionaryObject.

        Returns:
            Hash considering type and value.

        c              3   F   K   | ]  \  }}||j                         f  y wrD   )hash_bin).0kvs      rL   	<genexpr>z&PageObject.hash_bin.<locals>.<genexpr>  s     %QDAqq!**,&7%Qs   !)r   r1   r   r   ri   s    rL   r   zPageObject.hash_bin  s*     u%QDJJL%QRS
 	
rN   c                 F    t         |          }|dt        |       z  z  }|S )Ns   %d)superhash_value_dataid)r?   r   r   s     rL   r  zPageObject.hash_value_data  s(    w&(4  rN   c                 B    | j                  t        j                  d      S )a  
        A read-only positive number giving the size of user space units.

        It is in multiples of 1/72 inch. Hence a value of 1 means a user
        space unit is 1/72 inch, and a value of 3 means that a user
        space unit is 3/72 inch.
        r   )rE   PG	USER_UNITri   s    rL   	user_unitzPageObject.user_unit  s     xxa((rN   widthheightc           	         t        |       }|j                  t        t        j                        t        d             |j                  t        t        j
                        t                      |j                  t        t        j                        t                      ||r| jt        | j                        dkD  rR| j                  t        | j                        dz
     }|j                  j                  }|j                  j                  }nt        |j                  t        t        j                        t!        dd||f             |S )aC  
        Return a new blank page.

        If ``width`` or ``height`` is ``None``, try to get the page size
        from the last page of *pdf*.

        Args:
            pdf: PDF file the page is within.
            width: The width of the new page expressed in default user
                space units.
            height: The height of the new page expressed in default user
                space units.

        Returns:
            The new blank page

        Raises:
            PageSizeNotDefinedError: if ``pdf`` is ``None`` or contains
                no page

        z/Pager   r   )r   __setitem__r5   r	  TYPEPARENTr6   	RESOURCESr1   r   r   mediaboxr  r  r+   MEDIABOXr9   )rG   r  r  pagelastpages        rL   create_blank_pagezPageObject.create_blank_page%  s    6 # 	BGG,j.ABBII.
=BLL13C3EF=FN3syy>A#599S^a%78 ))//!**11--r{{#_aE65J%K	
 rN   objancest
call_stackc                 ~   |g }t        |dd       }||v rg S |j                  |       | j                  | j                         | _        || }|g }g }t        j
                  |vs1t        j                  t        t        |t        j
                           vr1| j                  g S t        | j                  j                               S |t        j
                     t        j                     j                         }|D ]~  }t        ||   t              s||   t        j                      dk(  r&|j                  t#        |      dk(  r|n||gz          V|j%                  | j'                  ||   ||gz   |              | j                  J |j%                  t        | j                  j                                      |S )Nr   /Imager   )getattrappendr   _get_inline_imagesr	  r  RESXOBJECTr   r1   r   r   rH   rF   r:   IASUBTYPEr   extend_get_ids_image)r?   r  r  r  _ir   x_objectos           rL   r%  zPageObject._get_ids_imageS  s    JS.5Ib!%!%!8!8!:D;C>F+-<<s"ckkc",,/:
 '
 ++32Xd>P>P>U>U>W9XXr||$S[[1<<> 	WAhqk<8{2::&(2

Fq 01fslC

4..x{FaSL*UV	W !!---

4**//123
rN   r  c           	          |t        t        |       }t        |t              rt	        |      }t        |t
              rt        |      dk(  r|d   }	 t        t        t        t        |t        j                           t        j                           }t        |t              r|d   dk(  rO|d   dk(  rG| j                  | j                         | _        | j                  t        d      | j                  |   S t        t        t        |               }|d d \  }}t!        |dd   | ||d   ||   j"                        }|S |dd  }| j%                  |t        t        |d                  S # t        $ r |d   dk(  r|d   dk(  s Y w xY w)Nr   r   ~r   zNo inline image can be foundre   r@   r   r   r   )r   r1   rF   r   r   r   r   r	  r  r   r!  KeyErrorr   r   r  r.   r   r   
_get_image)	r?   r  r  xobjsimgdr   r   fidss	            rL   r-  zPageObject._get_imagex  s   
 ;'.Cb% bBb$CGqLAB	 $'7R\\9J"KCKK"XE b#!u|2#%%-)-)@)@)BD&%%-"#ABB))"--!$'7r"CDD%)"1X"I{12x	{+ 1g#(9#?#?	A HQR&C??3-=uRU|(LMM-  	qESLRVs] &3	s   A E0 0FFc                 B    t        | j                  | j                        S )aI  
        Read-only property emulating a list of images on a page.

        Get a list of all images on the page. The key can be:
        - A string (for the top object)
        - A tuple (for images within XObject forms)
        - An integer

        Examples:
            * `reader.pages[0].images[0]`        # return fist image
            * `reader.pages[0].images['/I0']`    # return image '/I0'
            * `reader.pages[0].images['/TP1','/Image1']` # return image '/Image1' within '/TP1' Xobject/Form
            * `for img in reader.pages[0].images:` # loops through all objects

        images.keys() and images.items() can be used.

        The ImageFile has the following properties:

            * `.name` : name of the object
            * `.data` : bytes of the object
            * `.image`  : PIL Image Object
            * `.indirect_reference` : object reference

        and the following methods:
            `.replace(new_image: PIL.Image.Image, **kwargs)` :
                replace the image in the pdf with the new image
                applying the saving parameters indicated (such as quality)

        Example usage:

            reader.pages[0].images[0]=replace(Image.open("new_image.jpg", quality = 20)

        Inline images are extracted and named ~0~, ~1~, ..., with the
        indirect_reference set to None.

        )r   r%  r-  ri   s    rL   r   zPageObject.images  s    L !!4!4dooFFrN   r  r  c           	         	 t        i ddddddddd	d
ddddddddddddddddddddd
d
ddddddddt        t        |               }|S # t        t        f$ r_ t        |t               rK	 t        t        | d         d   }t        t        |      |   }Y |S # t        $ r t        d| d|       w xY wY |S w xY w)z%Translate values used in inline imagez/Gz/DeviceGrayz/RGBz
/DeviceRGBz/CMYKz/DeviceCMYK/Iz/Indexedz/AHxz/ASCIIHexDecodez/A85z/ASCII85Decodez/LZW
/LZWDecodez/Fl/FlateDecodez/RL/RunLengthDecodez/CCF/CCITTFaxDecodez/DCT
/DCTDecode)r5  r6  r7  r8  r9  
/Resources/ColorSpacezCannot find resource entry z for )r5   r   r   r   r,  rF   r1   r,   )r?   r  r  ress       rL   _translate_value_inlineimagez'PageObject._translate_value_inlineimage  s   #	R-L ] *	
 - , L > - - L "= !, "= 
  &'8!" %&6#$ #/$2(:'8".-. sA,/ AF  8$ 	R!Z(R/l1CD]SC-s3A6A    R&)DQCuQC'PQQR ) 	Rs$   AA C	;)B''CC	C	c                 F   | j                         }t        |      ri S g }|J d       |j                  D ]7  \  }}|dk(  r|j                  |d   |d   d       n|dv rt	        | d      9 i }t        |      D ]  \  }}|d	   t        |d	         d
}|d   j                         D ]  \  }	}
|	dv rt        |
t              r*t        |
D cg c]  }| j                  |	|       c}      }
n| j                  |	|
      }
t        i ddddddddddddddddddddddddddddddddddddd|	         }	|	|vs|
||	<    t        j                  |      |d <   t        |d          \  }}}t!        d!| d!| ||d"      |d!| d!<    |S c c}w )#zM
        get inline_images
        entries will be identified as ~1~
        Nr   s   INLINE IMAGEsettingsr   )r?  __streamdata__)s   BIs   EIs   IDzK operator met whereas not expected,please share usecase with pypdf dev teamr@  )r@  /Length>   /LrA  z/BPCz/BitsPerComponentz/CSr;  z/Dz/Decodez/DPz/DecodeParmsz/Fz/Filterz/Hz/Heightz/Wz/Widthr4  /Interpolatez/Intentz/IM
/ImageMask)rC  rD  objectr*  r+  )get_contentsr<   
operationsr  r,   r   r   r   rF   r   r/   r=  r5   r2   initialize_from_dictionaryr.   r   )r?   content	imgs_dataparamopefilesnumiiinitr  r  r   r   r   r   s                  rL   r  zPageObject._get_inline_images  s   
 ##%7#I	"*F*"!,, 	JE3o%  !&z!2eFmT --"e ? ? 	4  + .	GC"$%5"6r"234D :,,. ! 1))a&#JKLQ::1a@LA 99!Q?A 3} i ~	
 i i h n "9 | ,-@ &} "9 ' "9  "9!" !(#$ )7&2'( ). D=DGC! D /II$OBxL*8H*F'I{C )Qyk* #'	!EAcU!*S.	^ K Ms   Fc                     | j                  t        j                  d      }t        |t              r|S |j                         S )z
        The visual rotation of the page.

        This number has to be a multiple of 90 degrees: 0, 90, 180, or 270 are
        valid values. This property does not affect ``/Contents``.
        r   )rE   r	  ROTATErF   r   rH   )r?   
rotate_objs     rL   r{   zPageObject.rotationE  s5     XXbii+
'
C8zUj>S>S>UUrN   rc                 x    t        t        |      dz   dz  dz  dz        | t        t        j                        <   y )N-   Z   h  )r7   r   r5   r	  rR  )r?   rT  s     rL   r{   zPageObject.rotationP  s2    &2c!frkb5HB4NRU3U&VZ		"#rN   c                    | j                    }d| _         t        | j                        }t               j	                  t        |j                  |j                  dz  z          t        |j                  |j                  dz  z                j                  |      }|j                  |j                        }|j                  |j                        }|j	                  t        |d   |d          t        |d   |d                }| j                  |d       dD ]  }|| v st        | |         }|j                  |j                        }|j                  |j                        }t        t        |d   |d         t        |d   |d         t!        |d   |d         t!        |d   |d         f      | t#        |      <    y)z
        Apply the rotation of the page to the content and the media/crop/...
        boxes.

        It is recommended to apply this function before page merging.
        r   re   r   F)z	/MediaBox/CropBox	/BleedBox/TrimBox/ArtBoxN)r{   r9   r  r_   rs   r   leftr  bottomr  r   r   
lower_leftupper_rightminadd_transformationmaxr5   )r?   rT  mbtrsfr   pt2r   rrs           rL   transfer_rotation_to_contentz'PageObject.transfer_rotation_to_contentT  s    ]]NT]]+YrwwA-..ryy299q=7P1Q0Q VAY 	 mmBMM*mmBNN+~~s3q63q622SQQ5H4HIe,N 	ADy$T!W-mmBMM2mmBNN3&5CFCF+CFCF+CFCF+CFCF+	'Z]#	rN   anglec                     |dz  dk7  rt        d      t        | j                  |z         | t        t        j
                        <   | S )z
        Rotate a page clockwise by increments of 90 degrees.

        Args:
            angle: Angle to rotate the page. Must be an increment of 90 deg.

        Returns:
            The rotated PageObject

        rW  r   z'Rotation angle must be a multiple of 90)rx   r7   r{   r5   r	  rR  )r?   rj  s     rL   r   zPageObject.rotatew  sA     2:?FGG&24==53H&IZ		"#rN   res1res2resourcenew_res1c                    	 t        | j                  t              sJ | j                  j                  }t	        |d      }dt        dt        t        t        f   ffd}|rBt               j                  |j                  |t                     j                                nt        t        ||         t        t        |j                  |t                     j                               i }D ]  }	 ||	      \  }
}t        |
      }|	|
k7  r|||	<   |sM|r7j!                  |	      j#                  |      |<   	 |   j                  |<   nj!                  |	      |<   t%        j'                               }j)                          |D ]  }|d   |d   <     |fS # t
        t        f$ r d }d}Y zw xY w# t        $ r Y fw xY w)N_add_objectFbase_keyrB   c                     j                  |       }| }d}|v r)j                  |      |k(  r|dfS |  d| }|dz  }|v r)|dfS )a  
            Find a key that either doesn't already exist or has the same value
            (indicated by the bool)

            Args:
                base_key: An index is added to this to get the computed key

            Returns:
                A tuple (computed key, bool) where the boolean indicates
                if there is a resource of the given computed_key with the same
                value.

            r   T-r   F)raw_get)rr  rO   computed_keyidxnew_respage2ress       rL   compute_unique_keyz7PageObject._merge_resources.<locals>.compute_unique_key  sw     $$X.E $LC')??<0E9 (--"*1SE2q ')  &&rN   r   r   )rF   r   r4   rG   r   AssertionErrorAttributeErrorr   r   r   r1   r   rE   rH   r   r5   ru  clonesortedr   clear)r?   rl  rm  rn  ro  rG   is_pdf_writerrz  
rename_reskey
unique_key
same_valuenewnamer   elrx  ry  s                  @@rL   _merge_resourceszPageObject._merge_resources  s   	"d55~FFF))--C#]M	' 	'sDy1A 	'B &(GNN488H.>.@ALLNO+T(^<Gdhhx1A1CDOOQ
 
 	'C%7%<"J
 ,Gj ")
3 '/'7'7'<'B'B3'GGG$+27+;+N+N( (0'7'7'<GG$)CMMO '!#A1'%	'( 
""E / 	"C!M	"t * s#   >F& >G &F=<F= 	GGstreamrenamec                    |s| S t        | |      } | j                  D ]  \  }}t        |t              r:t	        |      D ]+  \  }}t        |t
              s|j                  ||      ||<   - Pt        |t              r?|j                         D ]+  \  }}t        |t
              s|j                  ||      ||<   - t        dt        |              | S )NzType of operands is )r0   rG  rF   r   r   r5   rE   dictr   r,  r   )r  r  rG   operands	_operatorr   ry   s          rL   _content_stream_renamez!PageObject._content_stream_rename  s     Mvs+#)#4#4 
	HHi(D)&x0 9EAr!"j1&,jjR&89 Hd+%^^- 9EAr!"j1&,jjR&89 !5d8n5EFGG
	H rN   contentsr`   c                     |\  }}}}}}t        | |      } | j                  j                  dt        |      t        |      t        |      t        |      t        |      t        |      gdg       | S )zHAdd transformation matrix at the beginning of the given contents stream.r      cm)r0   rG  insertr3   )	r  rG   r`   ar   crK   er0  s	            rL   _add_transformation_matrixz%PageObject._add_transformation_matrix  s{     1aAq 3/""  NNNNNN 
	
 rN   c                     t         j                  | v rg| t         j                     j                         }t        |t              rdj                  d |D              S t        t        |      j                         S y)z
        Return the page contents as bytes.

        Returns:
            The ``/Contents`` object as bytes, or ``None`` if it doesn't exist.

        rN   c              3   X   K   | ]"  }|j                         j                          $ y wrD   )rH   get_datar   r   s     rL   r  z4PageObject._get_contents_as_bytes.<locals>.<genexpr>  s     Ga 7 7 9Gs   (*N)	r	  CONTENTSrH   rF   r   r   r   r2   r  )r?   r  s     rL   _get_contents_as_bytesz!PageObject._get_contents_as_bytes  s`     ;;$r{{#..0C#t$xxG3GGG/5>>@@rN   c                    t         j                  | v rc	 t        t        | j                        j
                  }| t         j                     j                         }t        |t              ryt        ||      S y# t        $ r d}Y Lw xY w)u   
        Access the page contents.

        Returns:
            The ``/Contents`` object, or ``None`` if it does not exist.
            ``/Contents`` is optional, as described in §7.7.3.3 of the PDF Reference.

        N)r	  r  r   r4   r   rG   r|  rH   rF   r6   r0   )r?   rG   r  s      rL   rF  zPageObject.get_contents  sy     ;;$>4+B+BCGG r{{#..0C#z*$S#.. " s   $A7 7BBrI  c                    t        | d      r| j                  || t        t        j                        <   yt        | j                  t        j                  d      t              rG| t        j                     D ]1  }	 t               | j                  |j                  j                  dz
  <   3 t        |t              rDt        t        |            D ]-  }| j                  j                  j                  ||         ||<   / t!        |      rt        j                  | vry| j                  J | t        j                     j                  J t               | j                  j                  j                  | t        j                     j                  j                  dz
  <   | t        j                  = d| _        yt        | j                  t        j                  d      d      sH	 | j                  j                  j                  |      | t        t        j                        <   d| _        y|J d       | t        j                     j                  |_        	 || j                  j                  j                  |j                  j                  dz
  <   d| _        y# t        $ r Y +w xY w# t        $ r& || t        t        j                        <   Y d| _        yw xY w# t        $ r& || t        t        j                        <   Y d| _        yw xY w)z
        Replace the page contents with the new content and nullify old objects
        Args:
            content: new content; if None delete the content field.
        r   Nr   r   )r   r   r5   r	  r  rF   rE   r/   r6   r   r   r|  r   r   rG   rq  r<   r   )r?   rI  r(  r   s       rL   replace_contentszPageObject.replace_contents.  s    t12d6M6M6U,3DBKK()dhhr{{D1;?"++& DNLDMM!"6"6"<"<q"@A g{+3w<( Q!4488DDWQZP
Q 7#{{$&..:::BKK(;;GGG L ''++44%88>>B %4 "3 "++t46JK8040G0G0K0K0W0W1Z,-. " &..&)-*   &8  ''++44..44q8 "U & , " 8 18Z,-  ")8 " 8 18Z,-!8s6   ;.J!?J& :K 	J#"J#&%KK%LLpage2expandoverc                 ,    | j                  |||       y)a  
        Merge the content streams of two pages into one.

        Resource references
        (i.e. fonts) are maintained from both pages. The mediabox/cropbox/etc
        of this page are not altered. The parameter page's content stream will
        be added to the end of this page's content stream, meaning that it will
        be drawn after, or "on top" of this page.

        Args:
            page2: The page to be merged into this one. Should be
                an instance of :class:`PageObject<PageObject>`.
            over: set the page2 content over page1 if True (default) else under
            expand: If True, the current page dimensions will be
                expanded to accommodate the dimensions of the page to be merged.

        )r  r  N)_merge_page)r?   r  r  r  s       rL   
merge_pagezPageObject.merge_pagej  s    ( 	T&9rN   page2transformationc                 	   	 t        | j                  t              sJ t        | j                  j                  d      r| j                  |||||      S 	 t               }i }	 t        t        | t        j                     j                               }	 t        t        |t        j                     j                               }	t               }
| |fD ]J  }t        j                  |v s|t        j                     }t        |t              s:|
j!                  |       L t"        j$                  t"        j&                  t"        j(                  t"        j*                  t"        j,                  t"        j.                  t"        j0                  fD ]:  }| j3                  ||	|      \  }}|s||t5        |      <   |j7                  |       < t        t9        t;        |j=                  t"        j>                  t                     j                               jA                  t;        |	j=                  t"        j>                  t                     j                                                 |t5        t"        j>                        <   t               }| jC                         }|!|jE                          |jG                  |       |jC                         }|tI        |tJ              }|jL                  jO                  dtQ        tR        |jT                  |jV                  |jX                  |jZ                  g      df       |jL                  jO                  dg df       |jL                  jO                  dg df       | ||      }t\        j_                  ||| j                        }|jE                          |r|jG                  |       n|jO                  d|       |r| ja                  ||       | jc                  te        || j                               || t5        t        j                        <   |
| t5        t        j                        <   y # t        t        f$ r Y w xY w# t        $ r t               }Y w xY w# t        $ r t               }	Y w xY w)Nrq  r      rer      Wre      n)3rF   r   r4   r   rG   _merge_page_writerr{  r|  r1   r   r	  r  rH   r,  r/   ANNOTSr$  r   EXT_G_STATEFONTr!  COLOR_SPACEPATTERNSHADING
PROPERTIESr  r5   r   r~  setrE   PROC_SETunionrF  isolate_graphics_stater  r  MERGE_CROP_BOXrG  r  mapr3   r^  r_  r  r  r   r  _expand_mediaboxr  r0   )r?   r  r  r`   r  r  new_resourcesr  original_resourcespage2resources
new_annotsr  annotsr<  new	newrenamenew_content_arrayoriginal_contentpage2contentrects                       rL   r  zPageObject._merge_page  s   		d55~FFF''++] ...T6  )*	4!%&6R\\8J8U8U8W!X	0!"2E",,4G4R4R4TUN !]
5M 	.DyyD biifk2%%f-		. OOHHKKOOKKKKNN
 	)C "22"NCNC 14jo.i(	)" 3>&**3<<GRRT%**3<<GRRTU3
j./ (M,,.'335$$%56))+#5.1D##**# II KK JJ KK	  ##**1r4j9##**1r4j9".2<@%<<fdhhL //1!((6!((L9 !!%-m,=txxHI)6Z%&&0Z		"#w / 		  	4!1!3	4  	0-/N	0s6   AQ !/Q /Q1 QQQ.-Q.1RRc                    t        | j                  t              sJ | j                  j                  }i }t        j
                  | vr$t               | t        t        j
                        <   t        t        | t        j
                     j                               }t        j
                  |vrt               }	n/t        t        |t        j
                     j                               }	t        j                  t        j                  t        j                  t        j                  t        j                  t        j                   t        j"                  fD ]I  }
|
|	v s|
|vrt               |t        |
      <   | j%                  ||	|
d      \  }}|j'                  |       K t        j(                  |	v rt        j(                  |vr$t+               |t        t        j(                        <   t        t*        |t        j(                           }t        t*        |	t        j(                           D ]  }||vs|j-                  |        |j/                          t        j0                  |v rRt        j0                  | vr$t+               | t        t        j0                        <   t        t*        | t        j0                     j                               }|t3               }nt3        |      }t        t*        |t        j0                           D ]  }|j                         }|j5                  |dd      }t        t*        |d         }|j7                  |d   |d   fd      }|j7                  |d   |d	   fd      }t+        t9        |d   |d         t9        |d   |d         t;        |d   |d         t;        |d   |d         f      |t        d      <   d
|v rt        t*        |d
         }t+        |j7                  |d   |d   fd      |j7                  |d   |d	   fd      z   |j7                  |d   |d   fd      z   |j7                  |d   |d   fd      z         |t        d
      <   	 |j                  |d   t        d      <   	 | j                  |t        d      <   |j-                  |j                          t+               }| jA                         }|!|jC                          |j-                  |       |jA                         }|tE        |tF              }|jH                  jK                  dtM        tN        |jP                  |jR                  |jT                  |jV                  g      df       |jH                  jK                  dg df       |jH                  jK                  dg df       | ||      }tX        j[                  ||| j                        }|jC                          |r|j-                  |       n|jK                  d|       |r| j]                  ||       | j_                  |       y # t<        $ r Y w xY w# t>        $ r Y Kw xY w)NF)/Pz/StructParent/ParentT)ignore_fieldsforce_duplicatez/Rectr   r   re   rf   z/QuadPointsrg   rh         z/Popupr  r  r  r  r  )0rF   r   r4   rG   r	  r  r1   r5   r   rH   r   r  r  r!  r  r  r  r  r  r   r  r/   r  sortr  r_   r}  r   rb  rd  r,  r|  rF  r  r  r  rG  r  r  r3   r^  r_  r  r  r   r  r  r  )r?   r  r  r`   r  r  rG   r  r  r  r<  _r  arrr   r  rf  r  aarT  r   rg  qr  r  r  r  s                              rL   r  zPageObject._merge_page_writer  s    $11>BBB%%))<<t#-=-?DBLL)*!"2D4F4Q4Q4ST<<u$-/N!"2E",,4G4R4R4TUN OOHHKKOOKKKKNN
 	)C n$00:J:L&z#7#44&U 9 i(	)" <<>)||#55?J}":cll#;<{$6s||$DEC+~cll'CD "C<JJqM" HHJ99yy$.9mZ		*++tBII'A'A'CDF{%'%c*+uRYY'78 "LLNWW"D$(  
 aj1mmQqT1Q4L$7mmQqT1Q4L$7*5CFCF+CFCF+CFCF+CFCF+	+:g&' !A%[!M*:;A4?qtQqTlD9--1qtd;<--1qtd;< --1qtd;<5Bz-01:<:O:OBxLI!67+/+B+BBz$'(MM""7"78A"H (M,,.'335$$%56))+#5.1D##**# II KK JJ KK	  ##**1r4j9##**1r4j9".2<@%<<fdhhL //1!((6!((L9 !!%-/0c   
 & s$   %X23Y2	X?>X?	YYc           	      N   | j                   j                  j                         | j                   j                  j                         | j                   j                  j                         | j                   j
                  j                         f}|j                   j                  j                         |j                   j                  j                         |j                   j                  j                         |j                   j
                  j                         |j                   j                  j                         |j                   j
                  j                         |j                   j                  j                         |j                   j                  j                         fSt        d D              t        fdt        ddd      D              }t        fdt        ddd      D              }nddd   }ddd   }t        |      t        |      f}t        |      t        |      f}t        |d   |d         t        |d   |d         f}t        |d   |d         t        |d   |d         f}|| j                   _
        || j                   _        y )	Nc              3   2   K   | ]  }t        |        y wrD   r   r  s     rL   r  z.PageObject._expand_mediabox.<locals>.<genexpr>       .Qa.   c              3   ^   K   | ]$  }d    |   z  d   |dz      z  z   d   z    & yw)r   re   r   rg   NrS   r   r   corners2r`   s     rL   r  z.PageObject._expand_mediabox.<locals>.<genexpr>  E       A!$s1vQ'??#a&H   *-r      re   c              3   ^   K   | ]$  }d    |   z  d   |d z      z  z   d   z    & yw)r   rf   rh   NrS   r  s     rL   r  z.PageObject._expand_mediabox.<locals>.<genexpr>  r  r  r   rf   )r  r^  
as_numericr_  righttopr   r   rb  rd  r`  ra  )	r?   r  r`   corners1new_xnew_y	lowerleft
upperrightr  s	     `     @rL   r  zPageObject._expand_mediabox{  s&    MM))+MM  ++-MM**,MM((*	
 NN**,NN!!,,.NN**,NN))+NN  ++-NN))+NN  ++-NN!!,,.	
 ?.#..C q!Q E  q!Q E
 QqUOEQqUOEZU,	%j#e*-
!il3S!iPQl5ST	Z]+Z]+


 $- $.!rN   c                 r    t        t              rj                  | j                  fd||       y)a  
        merge_transformed_page is similar to merge_page, but a transformation
        matrix is applied to the merged stream.

        Args:
          page2: The page to be merged into this one.
          ctm: a 6-element tuple containing the operands of the
                 transformation matrix
          over: set the page2 content over page1 if True (default) else under
          expand: Whether the page should be expanded to fit the dimensions
            of the page to be merged.

        c                 b    t         j                  | j                  t        t                    S rD   )r   r  rG   r   r"   )page2Contentr`   r  s    rL   rY   z3PageObject.merge_transformed_page.<locals>.<lambda>  s%    !F!Feii.Lc)R" rN   N)rF   r_   r`   r  )r?   r  r`   r  r  s    ``  rL   merge_transformed_pagez!PageObject.merge_transformed_page  s9    ( c>*''C 	
rN   rz   c                 `    t               j                  ||      }| j                  ||||       y)a  
        merge_scaled_page is similar to merge_page, but the stream to be merged
        is scaled by applying a transformation matrix.

        Args:
          page2: The page to be merged into this one.
          scale: The scaling factor
          over: set the page2 content over page1 if True (default) else under
          expand: Whether the page should be expanded to fit the
            dimensions of the page to be merged.

        N)r_   rz   r  )r?   r  rz   r  r  ry   s         rL   merge_scaled_pagezPageObject.merge_scaled_page  s-     ##E51##E2tV<rN   r{   c                 ^    t               j                  |      }| j                  ||||       y)a  
        merge_rotated_page is similar to merge_page, but the stream to be merged
        is rotated by applying a transformation matrix.

        Args:
          page2: The page to be merged into this one.
          rotation: The angle of the rotation, in degrees
          over: set the page2 content over page1 if True (default) else under
          expand: Whether the page should be expanded to fit the
            dimensions of the page to be merged.

        N)r_   r   r  )r?   r  r{   r  r  ry   s         rL   merge_rotated_pagezPageObject.merge_rotated_page  s+    & $$X.##E2tV<rN   rp   rq   c                 `    t               j                  ||      }| j                  ||||       y)a  
        mergeTranslatedPage is similar to merge_page, but the stream to be
        merged is translated by applying a transformation matrix.

        Args:
          page2: the page to be merged into this one.
          tx: The translation on X axis
          ty: The translation on Y axis
          over: set the page2 content over page1 if True (default) else under
          expand: Whether the page should be expanded to fit the
            dimensions of the page to be merged.

        N)r_   rs   r  )r?   r  rp   rq   r  r  ry   s          rL   merge_translated_pagez PageObject.merge_translated_page  s-    * ''B/##E2tV<rN   c           	         t        |t              r|j                  }| j                         }|Bt        j                  || j                  |      }|j                          | j                  |       |r| j                  j                  j                         | j                  j                  j                         | j                  j                  j                         | j                  j                  j                         | j                  j                  j                         | j                  j                  j                         | j                  j                  j                         | j                  j                  j                         g}t        d |D              }t!        ddd      D cg c]"  }|d   ||   z  |d   ||dz      z  z   |d   z   $ }}t!        ddd      D cg c]"  }|d   ||   z  |d   ||dz      z  z   |d	   z   $ }}t#        |      t#        |      f}t%        |      t%        |      f}	|| j                  _        |	| j                  _        yyc c}w c c}w )
ab  
        Apply a transformation matrix to the page.

        Args:
            ctm: A 6-element tuple containing the operands of the
                transformation matrix. Alternatively, a
                :py:class:`Transformation<pypdf.Transformation>`
                object can be passed.

        See :doc:`/user/cropping-and-transforming`.

        Nc              3   2   K   | ]  }t        |        y wrD   r  r  s     rL   r  z0PageObject.add_transformation.<locals>.<genexpr>+  r  r  r   r  re   r   rg   rf   rh   )rF   r_   r`   rF  r   r  rG   r  r  r  r^  r  r_  r  r  r   r   rb  rd  r`  ra  )
r?   r`   r  rI  cornersr   r  r  r  r  s
             rL   rc  zPageObject.add_transformation  s   " c>*''C##% ;;GTXXsSG**,!!'*""--/$$//1""--/!!,,.##..0!!,,.##..0$$//1	G .#..C q!Q A#c!fwq1u~&==AFE  q!Q A#c!fwq1u~&==AFE 
 USZ0Ie*c%j1J'0DMM$(2DMM%5 s   /'I ''I%rt   ru   c           	      b   | j                  |dd|ddf       | j                  j                  ||      | _        | j                  j                  ||      | _        | j                  j                  ||      | _        | j
                  j                  ||      | _        | j                  j                  ||      | _        t        j                  | v r| t        j                     }t        |t              r|D ]  }|j                         }t        j                  |v s&|t        j                     }t        |t              sJt        t        |d         |z        |d<   t        t        |d         |z        |d<   t        t        |d         |z        |d<   t        t        |d         |z        |d<    t        j                   | v r| t        j                      }t        |t              r	|d   d   }n|d   }t#        t        |d         |z  t        |d         |z  t        |d         |z  t        |d         |z  f      }	t        |t              r5|	| t%        t        j                            t'        d         t%        d      <   y|	| t%        t        j                            t%        d      <   yy)a]  
        Scale a page by the given factors by applying a transformation matrix
        to its content and updating the page size.

        This updates the mediabox, the cropbox, and the contents
        of the page.

        Args:
            sx: The scaling factor on horizontal axis.
            sy: The scaling factor on vertical axis.

        r   r   re   rf   z/BBoxN)rc  r>   rz   artboxbleedboxtrimboxr  r	  r  rF   r/   rH   ADARectr3   r   VPr9   r5   r7   )
r?   rt   ru   annotations
annotationannotation_obj	rectangleviewportbboxscaled_bboxs
             rL   rz   zPageObject.scale;  sX    	Q2q! 45||))"b1kk''B/++B3||))"b1++B399ryy/K+{3"- QJ%/%:%:%<Nxx>1$2388$<	%i=+6uYq\7JR7O+PIaL+6uYq\7JR7O+PIaL+6uYq\7JR7O+PIaL+6uYq\7JR7O+PIaLQ 55D=BEE{H(K0{7+()$q'NR'$q'NR'$q'NR'$q'NR'	K (K0   Z&'Q8w' @KZ&'
7(;<' rN   factorc                 (    | j                  ||       y)z
        Scale a page by the given factor by applying a transformation matrix to
        its content and updating the page size.

        Args:
            factor: The scaling factor (for both X and Y axis).

        N)rz   )r?   r   s     rL   scale_byzPageObject.scale_byq  s     	

66"rN   c                     |t        | j                  j                        z  }|t        | j                  j                        z  }| j	                  ||       y)z
        Scale a page to the specified dimensions by applying a transformation
        matrix to its content and updating the page size.

        Args:
            width: The new width.
            height: The new height.

        N)r   r  r  r  rz   )r?   r  r  rt   ru   s        rL   scale_tozPageObject.scale_to|  sD     U4==..//eDMM0011

2rrN   levelc                 p   | j                         }|M|j                  |      }	 ||j                  j                  j                  |j                  j
                  dz
  <   yy# t        $ rK | j                  3t        | j                  j                  d      r| j                  |       Y yt        d      w xY w)a   
        Compress the size of this page by joining all content streams and
        applying a FlateDecode filter.

        However, it is possible that this function will perform no action if
        content stream compression becomes "automatic".
        Nr   rq  z Page must be part of a PdfWriter)
rF  flate_encoder   rG   r   r   r|  r   r  rx   )r?   r  rI  content_objs       rL   compress_content_streamsz#PageObject.compress_content_streams  s     ##%!..u5K
I   **..77..44q8  " I**67++//< ))+6$%GHHIs   :A! !AB5)B5c                     | j                   y	 | j                   j                  j                  }|j                  |       S # t        $ r Y yw xY w)z
        Read-only property which returns the page number within the PDF file.

        Returns:
            int : page number; None if the page is not attached to a PDF.

        N)r   rG   r   r   rx   )r?   r   s     rL   page_numberzPageObject.page_number  sO     ""*--1177yy& s   0A   	AAc                 P   d}t        | d   j                         | j                  d      j                  D ]m  \  }}|dk(  r$|d   D cg c]  }t	        |t
              s| }}ng }||j                  d      dz   dj                  |      z   |j                         z   dz   z  }o |d	z  }	 | t        j                     d
   D ]  }||dz   z  }|| t        j                     d
   |   j                         dz   z  }	 | t        j                     d
   |   d   j                         }||dz   z  }	 || t        j                     d
   |   d   j                         j                         dz   z  } 	 |S c c}w # t        $ r Y Tw xY w# t        $ r Y w xY w# t        $ r	 |dz  }Y |S w xY w)Nr   	/Contentsr      TJr   utf-8 
z
=============================
/Fontz	/Encodingz
/ToUnicodezNo Font
)r0   rH   rG   rG  rF   r   decoder   r   r	  r  	Exceptionr  r,  )r?   outrL  ry   r   sfoenc_reprs           rL   _debug_for_extractzPageObject._debug_for_extract  s   $((*DHHg

*	QGC U{ #A=1*Q*<Q==299W%+bggaj83<<>IDPPC	Q 	22	2<<(1 rDy tBLL)'226??ADHH#BLL1':2># hj  8d?*C
R\\*73B7( "C0 
= > !  !   	;C
	sa   E0E0+AF :2E5->F+F 5	F>F  FF 	FF FF F%$F%cmaptext_operands	font_sizespace_widthc                    d}|d   }|| j                   vr^|d   i }d}|}	|	dz  |d<   n1t        d|d   |d         }t        |d   |dz        }t        ||      }	|	dk(  r|}	|||	f| j                   |<   | j                   |   d   }| j                   |   d   }| j                   |   d   }	|r!|D ]  }
|
|k(  r||	z  }|t        ||
      z  } ||z  ||z  |fS )Nr   re   rf   r  defaultr   )r   r   r   r   )r?   r  r  r  r  font_widths	font_namefont_width_map
space_charactual_space_widthchars              rL   _get_acutual_font_widthsz#PageObject._get_acutual_font_widths  s:    a	D111Aw35 
,7",>,By)/T!Wd1gF
!5d1g{Q!O%7
%S"!Q&%0"0>
L^/_D!!),..y9!<**95a8
!229=a@% H:%#55K1.$GG	H
 i'y)@)LLrN   textr  	cm_matrix	tm_matrixorientations.rtl_dirvisitor_textactual_str_sizec           
          t        |||||      \  }}|r||z  }nt        ||||||||	      \  }}| j                  ||||
      \  }|d<   |d<   |dxx   |z  cc<   |||fS )Nr  
str_height
str_widths)r    r   r&  )r?   r'  r  r(  r)  r  r*  r  r+  r,  r  r-  r  is_str_operandsr   s                  rL   
_handle_tjzPageObject._handle_tj  s      *;iD,*@&M!D+MD' ))$y+V 	S_]3_\5R%4%Wo--rN   )r   rW             i@content_keyvisitor_operand_beforevisitor_operand_afterc	                     !"#$% d#d d!i 	 |}	t        t        j                        |	vr/|	d   j                         }	t        t        j                        |	vr/t	        t
        |	t        j                           }
d|
v r(t	        t
        |
d         D ]  }t        |||      |<    di ddf	 t        |t              r||   j                         n|}t        |t              st        ||d      }g d	g g d	$g d	g d	%g d	g d	d
d
"ddddddddt        dt        fddt        dt        t           ddf ! "#$%fd}|j                   D ]  \  }}| |||$       |dk(  r |dg         |d|       n|dk(  r1 |d|d   g        |d|d   g        |dg         |d|dd        nZ|dk(  r |d|d    g        |d |       n<|d!k(  rd"z  }|d   D ]}  }t        |t        t        f      r
 |d|g       t        |t"        t        t$        t&        f      sDt)        t        |            |k\  s\t+        #      dkD  sk#d#   d$k7  st |dd$g        n|d%k(  r #z    #d&          	  d#   d'k7  r d'z    d'd&          	 |
d(   }||d      d)   d*k7  r2 j/                  ||d      |||      # #z    #d&          d#j5                         $j5                         n	 |||       | |||$         #z   #dk7  r #d&           S # t        $ r Y yw xY w# t        $ r Y yw xY w# t,        $ r Y w xY w# t        $ r t1        d+|d    t2               Y w xY w# d#j5                         $j5                         w xY w),a  
        See extract_text for most arguments.

        Args:
            content_key: indicate the default key where to extract data
                None = the object; this allow to reuse the function on XObject
                default = "/Content"

        r   Fr  r  charmapNotInitializedNr         ?        r>  r=  r>  r>  r=  g     @@r>  )r0  r  r/  g      (@r0  rB   c                     | dz  S )Ng     @@rS   )r0  s    rL   compute_strwidthsz3PageObject._extract_text.<locals>.compute_strwidthst  s    &&rN   operatorr  c                    d}d}| dk(  r=g dz   d          dj                         j                         y | dk(  r:z   d          dj                         j                         n| dk(  rj                  
f       n| d	k(  r	 j                         \  
n| d
k(  rz   d          dt	        t        |d         t        |d         t        |d         t        |d         t        |d         t        |d         g      j                         j                         n| dk(  rt        |d         dz  n| dk(  rdt        |d         z   n| dk(  r:t        j                  d   dz  d   dz  z         }t        |d         z  |z  n| dk(  rndk7  rz   d          dj                         j                         	 |d      }|d   
|d   |d   |d   |d   f	 t        |d         n| dk(  rgd}t        |d         }t        |d         }dxx   |d   z  |d   z  z   z  cc<   dxx   |d   z  |d   z  z   z  cc<    	d         }d	d<   n| dk(  rcd}t        |d         t        |d         t        |d         t        |d         t        |d         t        |d         g 	d         }d	d<   n=| dk(  rd}dxx   z  cc<   n(| dk(  r"d}j                  |
	      \  	ny |rU	 t        fff| 	d         	d         \  dk(  r!j                         j                         y y y # t        $ r g dY gw xY w# t        $ r' t        d   
t        d   t        d   d|d    d fY w xY w# t        $ r Y w xY w# t        $ r Y y w xY w)NFr>  s   BTr<  rf   r   s   ET   q   Qr  r   r   re   rg   rh   s   Tzg      Y@   Twr=     TLs   Tfz???   TdTr0  s   Tm   T*   Tjr  r/  )copyr  popr  r!   r   r}   sqrtr,  r   r2  r   r   )rA  r  check_crlf_spacer0  scale_xcharMapTuplerp   rq   TL_actual_str_size_space_width
char_scaler(  cm_prevcm_stackr  cmapsr@  r  memo_cmmemo_tmr*  outputr+  r?   space_scaler'  r)  tm_prevr,  s           rL   process_operationz3PageObject._extract_text.<locals>.process_operationw  sO    &+ #J5 :	$+ wa)L#..*#..*U"$+ wa)L#..*#..* T!!!"#$
 T!? !!!"#$ U"$+ wa)L hqk*hqk*hqk*hqk*hqk*hqk* 
	 $..*#..*U""8A;/%7
U"!E(1+$66U"))IaL!OilAo$EF8A;')3g=U"2:dNF#/$T7GT!WiP#..*#..* $)!#5L#/?L
 %Q$Q $Q	D %hqk 2I U"#'  8A;'8A;'!Yq\ 1B14E EE!Yq\ 1B14E EE./?/MN
14 .U"#' (1+&(1+&(1+&(1+&(1+&(1+&	 //?/MN
14 .U"#' !"U"#' 26//   $3/g/  5E '*"I. '*$!$")*:=*IJ(662D&'7 rz"+.."2"+.."2 "!  U ! ? >I?d   #3A#6L(+(+hqk]+	D ! B 0   sI   ,N* N= 1O0 AO? *N:9N:=,O-,O-0	O<;O<?	P
P   'rH  rI     "rE  r   s   Tcr   re   s   TDrF  rG  r  gffffff?r   r  s   Dorf   r  /XObject/Subtyper  z" impossible to decode XFormObject )r5   r	  r  rH   r   r1   r  r   rF   r   r0   r,  r   r   r   r   rG  r   r7   r3   absr   r   extract_xform_textr%   r   rJ  )&r?   r  rG   r*  r  r6  r7  r8  r,  objrresources_dictr0  rI  r\  r  rA  _confirm_space_widthry   xobjrP  rQ  rR  rS  r(  rT  rU  r  rV  r@  r  rW  rX  rY  r+  rZ  r'  r)  r[  s&   `  `    `          @@@@@@@@@@@@@@@@@@@rL   _extract_textzPageObject._extract_text!  s   (   		DR\\*$6I113 R\\*$6
 ""2D4FGN
 n$*N7,CD ?)![#>a?
 	
 		1;K1MK ++-SV  g}5'g> "@	!?	  >=  >=
#c.F		'% 	'E 	'p	  p	 c p	 t p	  p	  p	  p	 d #*"4"4 L	PHh%1&x9iP4!%,!%2T!!%(1+7!%(1+7!%,!%!"6U"!%8A;,8!%2U"'3d':$"1+ 8B!"sEl3)%"6!"sE<&MNU2Y+?? Y]!"X_)%#78 U"$+ wa)LbzT)$'3( $ ' ' $Q )/)*5DHQK(4@#66 !-('21(  $'3( $ ' ' $Q ) D'nn.G'nn.G!(H5$0%h)YOYL	PZ 	$2:,2wa)Dg	  	 	*  		p " , ! "<Xa[MJ  D'nn.G'nn.Gsb   AM$ ,!M$  AM3 %NAN$	M0/M03	M?>M?	NNN30N62N33N66$Oc                 2   | }i }|	 |t         j                     }d|v r| j                  |d   D ]  }t	        |d|       ^ }}|j                         D 	ci c]V  \  }}|t        |t              r|j                         n/t        |t              r|D 	cg c]  }	|	j                          c}	n|X }
}}}	t        j                  g ||
 ||<    	 |d   j                         }||S # t        $ r i }Y w xY wc c}	w c c}	}}w # t        $ r d}Y /w xY w)z
        Get fonts formatted for "layout" mode text extraction.

        Returns:
            Dict[str, Font]: dictionary of _layout_mode.Font instances keyed by font name

        Nr  r5  r  )r	  r  r,  rG   r   r   rF   r4   rH   r/   r   Font)r?   rc  fontsrd  r!  r  font_dict_objr  r  _v	font_dicts              rL   _layout_mode_fontszPageObject._layout_mode_fonts{  sO    .0$&*2<<&8 .(TXX-A!/!8 KI+9)UD+Q(T= %2$7$7$9! ! !Aq %a8 <<> &a5 9::"bmmo:	!I ! (4'8'8'J$'J	'JE)$KI113' . )  $!#$ ;!  s;   C+ =D
C<+D
D +C98C9<D
DDspace_verticallyscale_weightstrip_rotated
debug_pathfont_height_weightc                    | j                         }|r8ddl}|j                  d      j                  |j	                  |dd       d       t        t        | d   j                         | j                  d	      j                        }t        j                  ||||      }	|	sy
t        j                  |	|      }
t        j                  |	|      }t        j                  |
|||      S )aQ  
        Get text preserving fidelity to source PDF text layout.

        Args:
            space_vertically: include blank lines inferred from y distance + font
                height. Defaults to True.
            scale_weight: multiplier for string length when calculating weighted
                average character width. Defaults to 1.25.
            strip_rotated: Removes text that is rotated w.r.t. to the page from
                layout mode output. Defaults to True.
            debug_path (Path | None): if supplied, must target a directory.
                creates the following files with debug information for layout mode
                functions if supplied:
                  - fonts.json: output of self._layout_mode_fonts
                  - tjs.json: individual text render ops with corresponding transform matrices
                  - bts.json: text render ops left justified and grouped by BT/ET operators
                  - bt_groups.json: BT/ET operations grouped by rendered y-coord (aka lines)
                Defaults to None.
            font_height_weight: multiplier for font height when calculating
                blank lines. Defaults to 1.

        Returns:
            str: multiline string containing page text in a fixed width format that
                closely adheres to the rendered layout in the source pdf.

        r   Nz
fonts.jsonre   c                 0     t        | dt              |       S )Nto_dict)r  r   )r   s    rL   rY   z.PageObject._layout_mode_text.<locals>.<lambda>  s    7Qwq)S7QRS7T rN   )indentr  r  r  r   r   )rn  jsonjoinpath
write_textdumpsiterr0   rH   rG   rG  r   text_show_operationsy_coordinate_groupsfixed_char_widthfixed_width_page)r?   ro  rp  rq  rr  rs  rj  rx  ops	bt_groups	ty_groups
char_widths               rL   _layout_mode_textzPageObject._layout_mode_text  s    D '')-88

!-T   	 ${+668$((GLWW
 !55z
	  44Y
K	!229lK
,,Y
DTVhiirN   plain)r*  r  r7  r8  r,  extraction_modeargsr  r  layoutr   c          
         |dvrt        d| d      |dk(  rdD ]$  }	t               |	   st        d|	 dt               & | j	                  |j                  dd	      |j                  d
d      |j                  dd	      |j                  d      |j                  dd            S t        |      dk\  rt        |d   t              r|t        |      dk\  r0t        |d   t        t        f      r|d   }nt        d|d          t        |      dk\  rt        |d   t        t        f      r|d   }n~t        d|d          t        |d   t        t        f      rC|d   }t        |      dk\  rAt        |d   t        t        f      r|d   }n"t        d|d          t        d|d          t        |t              r|f}| j                  | | j                  ||t        j                   |||      S )a  
        Locate all text drawing commands, in the order they are provided in the
        content stream, and extract the text.

        This works well for some PDF files, but poorly for others, depending on
        the generator used. This will be refined in the future.

        Do not rely on the order of text coming out of this function, as it
        will change if this function is made more sophisticated.

        Arabic and Hebrew are extracted in the correct order.
        If required a custom RTL range of characters can be defined;
        see function set_custom_rtl.

        Additionally you can provide visitor methods to get informed on all
        operations and all text objects.
        For example in some PDF files this can be useful to parse tables.

        Args:
            orientations: list of orientations extract_text will look for
                default = (0, 90, 180, 270)
                note: currently only 0 (up),90 (turned left), 180 (upside down),
                270 (turned right)
                Silently ignored in "layout" mode.
            space_width: force default space width
                if not extracted from font (default: 200)
                Silently ignored in "layout" mode.
            visitor_operand_before: function to be called before processing an operation.
                It has four arguments: operator, operand-arguments,
                current transformation matrix and text matrix.
                Ignored with a warning in "layout" mode.
            visitor_operand_after: function to be called after processing an operation.
                It has four arguments: operator, operand-arguments,
                current transformation matrix and text matrix.
                Ignored with a warning in "layout" mode.
            visitor_text: function to be called when extracting some text at some position.
                It has five arguments: text, current transformation matrix,
                text matrix, font-dictionary and font-size.
                The font-dictionary may be None in case of unknown fonts.
                If not None it may e.g. contain key "/BaseFont" with value "/Arial,Bold".
                Ignored with a warning in "layout" mode.
            extraction_mode (Literal["plain", "layout"]): "plain" for legacy functionality,
                "layout" for experimental layout mode functionality.
                NOTE: orientations, space_width, and visitor_* parameters are NOT respected
                in "layout" mode.

        kwargs:
            layout_mode_space_vertically (bool): include blank lines inferred from
                y distance + font height. Defaults to True.
            layout_mode_scale_weight (float): multiplier for string length when calculating
                weighted average character width. Defaults to 1.25.
            layout_mode_strip_rotated (bool): layout mode does not support rotated text.
                Set to False to include rotated text anyway. If rotated text is discovered,
                layout will be degraded and a warning will result. Defaults to True.
            layout_mode_debug_path (Path | None): if supplied, must target a directory.
                creates the following files with debug information for layout mode
                functions if supplied:

                  - fonts.json: output of self._layout_mode_fonts
                  - tjs.json: individual text render ops with corresponding transform matrices
                  - bts.json: text render ops left justified and grouped by BT/ET operators
                  - bt_groups.json: BT/ET operations grouped by rendered y-coord (aka lines)
            layout_mode_font_height_weight (float): multiplier for font height when calculating
                blank lines. Defaults to 1.

        Returns:
            The extracted text

        r  zInvalid text extraction mode ''r  )r7  r8  r,  z	Argument z is ignored in layout modelayout_mode_space_verticallyTlayout_mode_scale_weight      ?layout_mode_strip_rotatedlayout_mode_debug_pathlayout_mode_font_height_weightr   )ro  rp  rq  rr  rs  r   rf   re   zInvalid positional parameter rg   )rx   localsr%   r   r  rE   r   rF   r   r   r   r   r   rg  rG   r	  r  )
r?   r*  r  r7  r8  r,  r  r  r   visitors
             rL   extract_textzPageObject.extract_text  s   ` "55=o=NaPQQh& 	
 8G$"#G9,FG 	 ))!',JD!Q#ZZ(BDI$jj)DdK!::&>?#)::.NPQ#R *   t9>$q'3't9>!$q'E3<8'+Aw'*GQy(QRRt9>!$q'E3<8&*1g'*GQy(QRRDGeS\2#Awt9>!$q'E3<8&*1g'*GQy(QRR"?Qy IJJlC((?L!!HHKK"!	
 		
rN   xformc           
      F    | j                  || j                  ||d|||      S )a\  
        Extract text from an XObject.

        Args:
            xform:
            orientations:
            space_width:  force default space width (if not extracted from font (default 200)
            visitor_operand_before:
            visitor_operand_after:
            visitor_text:

        Returns:
            The extracted text

        N)rg  rG   )r?   r  r*  r  r7  r8  r,  s          rL   rb  zPageObject.extract_xform_textd	  s3    0 !!HH"!	
 		
rN   c                     | j                         }t        |t              sJ t               }t               }t	        |||      \  }}||z
  }||fS )z
        Get the names of embedded fonts and unembedded fonts.

        Returns:
            A tuple (Set of embedded fonts, set of unembedded fonts)

        )rH   rF   r1   r  _get_fonts_walk)r?   r  rj  embedded
unembeddeds        rL   
_get_fontszPageObject._get_fonts	  sV     oo#/000% U)#uh?xX%
##rN   rS   rZ  r[  r\  r]  c                 2    d| vry t        t        | d         S )N/Annots)r   r/   ri   s    rL   r  zPageObject.annotations	  s    D T)_55rN   rO   c                 >    || t        d      = y|| t        d      <   y)z
        Set the annotations array of the page.

        Typically you do not want to set this value, but append to it.
        If you append to it, remember to add the object first to the writer
        and only add the indirect object.
        Nr  rQ   )r?   rO   s     rL   r  zPageObject.annotations	  s%     =Z	*+*/DI&'rN   r   )NNNrD   )rB   N)T)FT)NNTF)TFr   )r   )Tr  TNr   ))r   rW  r4  rX  r5  NNN)[r   r   r   r   r   r   r   r4   rc   r   r   r   r  r\   r   r  r   r   r   r  r1   r   r   r   r%  r   r   r-  r   r   r8   r=  r	   r  r{   setterri  r   r   r  r0   r  r"   r  r  rF  r2   r/   r  r  r   r  r  r  r_   r  r  r  r  rc  rz   r  r  r	  r  r  r&  r;   r2  r	  r  rg  r   ri  rn  r   r  r   r  rb  r   r  r]   r  r  r>   r  r  r  r  __classcell__)r   s   @rL   r   r     s       /37;S*+S %^4S 
	S
# 
 
 )5 ) ) .2-1.2+*++UGT)*+ eWd*++ 
	+ +^ +/&**.	#&'# c## T#Y'	#
 
eCcN#	$#P +/%N#tCy%*,-%N &'%N 
	%NN %G) %G %GN&c &i &I &PTDi$8 Tl V# V V __W% WD W W!FC L * O#O# O# 	O#
 O# 
tCH~tCH~-	.O#b S#X *+ 
	 * *+ , 
	 0 "h}5 ,:"T=2E{RS:"	:"z GK:!:+/:?C:	:2 IM8<n1n1 &hum/C&DEn1 45	n1
 n1 n1 
n1f IM8<F1F1 &hum/C&DEF1 45	F1
 F1 F1 
F1V)/!)/(01O(P)/	)/^ 

 1>AB
 	

 
 

B TY=!=*/=7;=LP=	=, == = 	=
 = 
=6 == = 	=
 = = 
=6 33>#AAB33 33 
	33j4K 4K5 4KT 4Kl	#u 	# 	#e U t Ic I4 I. Xc]  "$C $L"M#tCH~%&S#XXFV=WW
"M
 "M "M "M 
ueU"	#"MH".". uS"2234". ;	".
 ;". #tCH~%&S#XXFV=WW
". CHo". ". ". xc3S(A4(GHI". ". c5j)". 
sD$sEz**	+".P ):"%'[[QUPTLPXX X CHo	X
 X c]X !)3S#2F2L)M NX  (#sC1Et1K(LMX xc3S(A4(GHIX 
Xt
"Dl.?.?)?$@ "L "&""%)$%;j;j ;j 	;j
 TN;j ";j 
;j@ 5F"QUPTLP6=F
F
 CsCx01F
 	F

 !)3S#2F2L)M NF
  (#sC1Et1K(LMF
 xc3S(A4(GHIF
 !!23F
 F
 
F
V ):"QUPTLP!
"!
 CHo!
 	!

 !)3S#2F2L)M N!
  (#sC1Et1K(LM!
 xc3S(A4(GHI!
 
!
F$E#c(CH"45 $  *"++r:H> )bkk^DG	 *+
BKK7PQHG )j"++5NOG (	J3LMF2 6Xk2 6 6 0+!6 04 0 0rN   r   c                       e Zd Zdeg ef   deegef   ddfdZdefdZededefd       Z	ede
dee   fd	       Z	deee
f   deeee   f   fd
Z	deee
f   ddfdZdee   fdZdefdZy)_VirtualListlength_functionr   rB   Nc                 .    || _         || _        d| _        y r   )r  r   r   )r?   r  r   s      rL   rc   z_VirtualList.__init__	  s    
  /(rN   c                 "    | j                         S rD   )r  ri   s    rL   r   z_VirtualList.__len__	  s    ##%%rN   r   c                      y rD   rS   r   s     rL   r   z_VirtualList.__getitem__	  r   rN   c                      y rD   rS   r   s     rL   r   z_VirtualList.__getitem__	  r   rN   c                 V    t        |t              rBt        |j                  t	                      t               } |j                   fd      S t        |t              st        d      t	               }|dk  r||z   }|dk  s||k\  rt        d       j                  |      S )Nc                     |       S rD   rS   )rw  r   r?   s    rL   rY   z*_VirtualList.__getitem__.<locals>.<lambda>	  s    D4F rN   z!Sequence indices must be integersr   r   )rF   r   r   r   r   r   r   r   r   r   r   )r?   r   r   r   r   s   `   @rL   r   z_VirtualList.__getitem__	  s     eU#U]]3t956Gt*Cw(FGG%%?@@t919u$E19):;;  ''rN   c           	         t        |t              rTt        t        |j	                  t        |                    }|j                          |j                          |D ]  }| |=  y t        |t              st        d      t        |       }|dk  r||z   }|dk  s||k\  rt        d      | |   j                  }|J t        t        |j                               j                  dd       }d}|t        t        |j                               }	 t        t         |d         j#                  |      }t        t         |d         |= d}	 |J |j$                  j&                  |= d|v r+t+        t        t        |d         d	z
        |t-        d      <   t        t        t         |d               dk(  r|j                  }|j                  dd       }|y y # t(        $ r Y |w xY w# t.        $ r |rt1        d
|       Y y w xY w)NzIndex must be integersr   zIndex out of ranger  Tz/KidsFz/Countr   zPage not found in page tree: )rF   r   r   r   r   r   r  reverser   r   r   r   r   r1   rH   rE   r/   r   rG   flattened_pagesr  r7   r5   rx   r,   )	r?   r   rT  r   r   indparentfirstr   s	            rL   __delitem__z_VirtualList.__delitem__	  s   eU#UEMM#d)456AFFHIIK G%%455t919u$E19)1225k,,&*+;S^^=M&N&R&Rt'
  *F,=,=,?@FfWo6<<SAfWo6q9?*?//6 v%3?S&"23a74F:h/0 tK9:a? 33CIt4%   !   &)Fse'LMMs7   8G" G "A,G" 	GG" GG" "G>=G>c              #   L   K   t        t        |             D ]	  }| |     y wrD   r   r   s     rL   r   z_VirtualList.__iter__"
  r   r   c                     t        | j                               D cg c]  }d| d
 }}ddj                  |       dS c c}w )NzPageObject(r   r   r   r   )r   r  r   )r?   r   r   s      rL   r   z_VirtualList.__str__&
  sI    ).t/C/C/E)FGA{1#QGG499Q<."" Hs   A )r   r   r   r   r   r   rc   r   r   r   r   r   r   r  r   r   r   r   rS   rN   rL   r  r  	  s    !"c'* uj01 
	& &      8J+?  (3:&(	z8J//	0("-sEz!2 -t -^(:. # #rN   r  r  fntembc           
      N   ddt         ddffd}d| v rFdt        t         | d         v r1t        t         t        t         | d         d         D ]
  } ||        d| v rdt        t         | d         v r?t        t         t        t         | d         d         j                         D ]
  } ||        d	t        t         | d         v r`t        t         t        t         | d         d	         j                         D ]+  }t        t        t         |j	                                      - d
| v rAt        t
        | d
         D ]+  }t        t        t         |j	                                      - d| v rt        t         t        t         | d         d         j                  d      d	k(  r3t        t        t         t        t         | d         d                fS t        t         t        t         | d         d         D ]  }t        t        t         |              fS )a  
    Get the set of all fonts and all embedded fonts.

    Args:
        obj: Page resources dictionary
        fnt: font
        emb: embedded fonts

    Returns:
        A tuple (fnt, emb)

    If there is a key called 'BaseFont', that is a font that is used in the document.
    If there is a key called 'FontName' and another key in the same dictionary object
    that is called 'FontFilex' (where x is null, 2, or 3), then that fontname is
    embedded.

    We create and add to two sets, fnt = fonts used and emb = fonts embedded.

    )z	/FontFilez
/FontFile2z
/FontFile3r0  rB   Nc           	          t        t         j                                d v r"j                  t        t         d                d v sdd v rt         fdD              sLd v rndt        t        t        t         d         d   j                               v r9t         fdD              r$	 j                  t        t         d                y y y y # t        $ r+ j                  dt        t         d	         z   d
z          Y y w xY w)Nz	/BaseFontz
/CharProcs/FontDescriptorc              3   H   K   | ]  }|t        t        d          v   yw)r  N)r   r1   r   r   r0  s     rL   r  z8_get_fonts_walk.<locals>.process_font.<locals>.<genexpr>O
  s)      JKA.2C0DEEs   "/DescendantFontsr   c              3      K   | ]I  }|t        t        t        t        t        t        d          d   j                               d         v  K yw)r  r   r  N)r   r1   r/   rH   r  s     rL   r  z8_get_fonts_walk.<locals>.process_font.<locals>.<genexpr>Z
  s^      
  (, a0B.CDQGRRT ,-
s   AA(r`  r   )r   r1   rH   addr   anyr/   r,  )r0  r  r  fontkeyss   `rL   process_fontz%_get_fonts_walk.<locals>.process_fontE
  s   !1<<>2!GGDan-. Q!Q& OW 
 #a'%$a(:&;<Q?JJL
  
 &
 
>S!K.12
 (,  >d3*66<=>s   0"C 1D
	D
z/DRr  r:  r_  r  z/APz/Nz/Type)r1   r   valuesr  rH   r/   rE   )r  r  r  r  r0  r   r  r  s    ``    @rL   r  r  +
  s$   0 9H&>( &>T &>P |4(8#e*#EE&-=s5z(J7(ST 	AO	sd+S->?? $'7\9J"KG"Tfh  Q  .L0ABB $'7\9J"KJ"WfhR  %5q||~ FSQR Ck3y>2 	NAD!11<<>BCM	N|!4(8#e*#Ed#KLPP 
 %t,<c%j'I$'OP 8O *D1A3u:,Nt,TU E%5q 93DE8OrN   )]r}   dataclassesr   decimalr   ior   pathlibr   typingr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   _cmapr   r   r   r   r   
_protocolsr   _text_extractionr   r   r   r   r    r!   _utilsr"   r#   r$   r%   r&   	constantsr'   r  r(   r"  r)   r	  r*   r   errorsr+   r,   r   r.   r   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   	PIL.Imager=   r   r   rE  r  r   rM   r   rI   rU   r\   r]   r_   r   r   r   r  r  rS   rN   rL   <module>r     s  <  !      "  -   = , + ' 9 #   "
  C 8C= _   C ou6L0M RV 
C s t S HSM h x: x:v XC XC XCv<#+ <#~f0! f0R?[#8J' [#|b	b	Sb 
Sb 3s8SX	bKN  Es   6E 	E! E!