
     h]                     ^    d Z ddlmZ ddlmZmZ ddlmZ ddlmZ dgZ	 G d d          Z
d	S )
z2Utilities common to reading and writing PDF files.    )Tuple   )genericmisc)DocumentMetadata)PdfError
PdfHandlerc                       e Zd ZdZ	 ddej        defdZedej	        fd            Z
edefd            Zedej        fd	            Zedej	        fd
            Zedeeef         fd            Zd Zd Zd ZdS )r	   zfAbstract class providing a general interface for quering objects
    in PDF readers and writers alike.Frefas_metadata_streamc                     t           )aO  
        Retrieve the object associated with the provided reference from
        this PDF handler.

        :param ref:
            An instance of :class:`.generic.Reference`.
        :param as_metadata_stream:
            Whether to dereference the object as an XMP metadata stream.
        :return:
            A PDF object.
        NotImplementedError)selfr   r   s      W/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/pyhanko/pdf_utils/rw_common.py
get_objectzPdfHandler.get_object   
     "!    returnc                     t           )a  
        Returns a view of the document trailer of the document represented
        by this :class:`.PdfHandler` instance.

        The view is effectively read-only, in the sense that any writes
        will not be reflected in the actual trailer (if the handler supports
        writing, that is).

        :return:
            A :class:`.generic.DictionaryObject` representing the current state
            of the document trailer.
        r   r   s    r   trailer_viewzPdfHandler.trailer_view   r   r   c                     t           Nr   r   s    r   document_meta_viewzPdfHandler.document_meta_view/       !!r   c                     t           )zS
        :return: A reference to the document catalog of this PDF handler.
        r   r   s    r   root_refzPdfHandler.root_ref3   s
    
 "!r   c                 p    | j                                         }t          |t          j                  sJ |S )zD
        :return: The document catalog of this PDF handler.
        )r   r   
isinstancer   DictionaryObject)r   roots     r   r"   zPdfHandler.root:   s5    
 }''))$ 899999r   c                     t           r   r   r   s    r   document_idzPdfHandler.document_idC   r   r   c                    | j                             d          }t          |t          j                  sJ |                                }t          |t          j                  sJ 	 |d         }n## t          $ r t          j                    }Y nw xY w|d         }dk     r|z   dcxk    r|k     sn t          d          fd d||t                                S )N/Pages
/Resources/Countr   zPage index out of rangec                    |                                 }|d         }	 |                    d          }n# t          $ r Y nw xY w| }t          |          D ]\  }}t	          |t
          j                  st          j        d          t	          |t
          j                  sJ |j	        |v rt          j        d          |                                 }	|	d         }
|
dk    r:|	d         }|cxk    r	||z   k     rn n |||||j	        hz            c S ||z  }|
dk    rA|k    r6r|||fc S 	 |	                    d          }n# t          $ r Y nw xY w||fc S |d	z  }t          d
          )Nz/Kidsr'   z0Page tree node children must be indirect objectszCircular reference in page treez/Typer&   r(   z/Pager   zPage not found)r   raw_getKeyError	enumerater    r   IndirectObjectr   PdfReadError	referencer   )first_page_ixpages_obj_reflast_rsrc_dict	refs_seen	pages_objkidscur_page_ix	kid_indexkid_refkid	node_type
desc_count_recursepage_ixretrieve_parents               r   r<   z,PdfHandler._walk_page_tree.<locals>._recurseY   s   %0022IW%D!*!2!2<!@!@    (K&/oo #) #)"	7!'7+ABB +J   "'7+ABBBBB$	11+,MNNN((**L	(( "%XJ"gHHHHj0HHHHHH'x'#*%):(;;	        :-KK'))"g--* ;$19n#MMMM%14\1J1J#+ % % % $%#*N#::::#q(+,,,s!   5 
AA5E
EE)
r"   r*   r    r   r-   r   r!   r+   r   set)r   r=   r>   page_tree_root_refpage_tree_rootroot_resources
page_countr<   s    ``    @r   _walk_page_treezPdfHandler._walk_page_treeH   s+   !Y..x88,g.DEEEEE+6688.'*BCCCCC	8+L9NN 	8 	8 	8$577NNN	8 $H-
Q;; 7*GW))))z))))4555.	- .	- .	- .	- .	- .	- .	-` x-~suuEEEs   +A4 4BBc                 0    |                      |d          S )a  
        Retrieve the node in the page tree containing the
        page with index ``page_ix``, along with the necessary objects
        to modify it in an incremental update scenario.

        :param page_ix:
            The (zero-indexed) number of the page for which we want to
            retrieve the parent.
            A negative number counts pages from the back of the document,
            with index ``-1`` referring to the last page.
        :return:
            A triple with the ``/Pages`` object (or a reference to it),
            the index of the target page in said ``/Pages`` object, and a
            (possibly inherited) resource dictionary.
        Tr>   rD   r   r=   s     r   find_page_containerzPdfHandler.find_page_container   s      ##GT#BBBr   c                 0    |                      |d          S )a  
        Retrieve the page with index ``page_ix`` from the page tree, along with
        the necessary objects to modify it in an incremental update scenario.

        :param page_ix:
            The (zero-indexed) number of the page to retrieve.
            A negative number counts pages from the back of the document,
            with index ``-1`` referring to the last page.
        :return:
            A tuple with a reference to the page object and a
            (possibly inherited) resource dictionary.
        FrF   rG   rH   s     r   find_page_for_modificationz%PdfHandler.find_page_for_modification   s     ##GU#CCCr   N)F)__name__
__module____qualname____doc__r   	Referenceboolr   propertyr!   r   r   r   r   r"   r   bytesr$   rD   rI   rK    r   r   r	   r	      sc       ) ) BG" "$":>" " " "  "g6 " " " X" "$4 " " " X" "'+ " " " X" g.    X "U5%<0 " " " X"AF AF AFFC C C$D D D D Dr   N)rO   typingr    r   r   metadata.modelr   r   __all__r	   rT   r   r   <module>rY      s    8 8               , , , , , ,      ._D _D _D _D _D _D _D _D _D _Dr   