o
    vh^                     @   sP   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 dZ
d	S )
z2Utilities common to reading and writing PDF files.    )Tuple   )genericmisc)DocumentMetadata)PdfError
PdfHandlerc                   @   s   e Zd ZdZ	ddejdefddZedej	fdd	Z
edefd
dZedejfddZedej	fddZedeeef fddZdd Zdd Zd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                 C      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
    r   Y/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/pyhanko/pdf_utils/rw_common.py
get_object      zPdfHandler.get_objectreturnc                 C   r   )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   r   r   r   trailer_view    r   zPdfHandler.trailer_viewc                 C   r   Nr   r   r   r   r   document_meta_view0      zPdfHandler.document_meta_viewc                 C   r   )zS
        :return: A reference to the document catalog of this PDF handler.
        r   r   r   r   r   root_ref4   s   zPdfHandler.root_refc                 C   s   | j  }t|tjsJ |S )zD
        :return: The document catalog of this PDF handler.
        )r   r   
isinstancer   DictionaryObject)r   rootr   r   r   r   ;   s   
zPdfHandler.rootc                 C   r   r   r   r   r   r   r   document_idD   r   zPdfHandler.document_idc                    s   | j d}t|tjsJ | }t|tjsJ z|d }W n ty-   t }Y nw |d }dk r:| d  krG|k sLtd td fdd  d||t	 S )N/Pages
/Resources/Countr   zPage index out of rangec              	      sB  |  }|d }z|d}W n	 ty   Y nw | }t|D ]}\}}t|tjs.tdt|tjs6J |j	|v r@td|  }	|	d }
|
dkrp|	d }|  kr\|| k rkn n |||||j	hB   S ||7 }q|
dkr|krr|||f  S z|	d}W n	 ty   Y nw ||f  S |d	7 }q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_parentr   r   r4   Z   sR   



z,PdfHandler._walk_page_tree.<locals>._recurse)
r   r!   r   r   r$   r   r   r"   r   set)r   r5   r6   page_tree_root_refpage_tree_rootroot_resources
page_countr   r3   r   _walk_page_treeI   s$   0zPdfHandler._walk_page_treec                 C      | j |d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r6   r<   r   r5   r   r   r   find_page_container   s   zPdfHandler.find_page_containerc                 C   r=   )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.
        Fr>   r?   r@   r   r   r   find_page_for_modification   s   z%PdfHandler.find_page_for_modificationN)F)__name__
__module____qualname____doc__r   	Referenceboolr   propertyr   r   r   r   r   r   r   bytesr   r<   rA   rB   r   r   r   r   r      s*    
CN)rF   typingr    r   r   metadata.modelr   r   __all__r   r   r   r   r   <module>   s    