
    Wwg^                     V    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
y	)
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y)r	   zfAbstract class providing a general interface for quering objects
    in PDF readers and writers alike.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      R/var/www/horilla/myenv/lib/python3.12/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_view0       !!r   c                     t         )zS
        :return: A reference to the document catalog of this PDF handler.
        r   r   s    r   root_refzPdfHandler.root_ref4   s
    
 "!r   c                 r    | j                   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;   s1    
 }}'')$ 8 8999r   c                     t         r   r   r   s    r   document_idzPdfHandler.document_idD   r   r   c                    | j                   j                  d      }t        |t        j                        sJ |j                         }t        |t        j                        sJ 	 |d   }|d   }dk  r|z   dcxk  r|k  st        d       t        d      fd d||t                     S # t        $ r t        j                         }Y jw xY w)N/Pages
/Resources/Countr   zPage index out of rangec                    |j                         }|d   }	 |j                  d      }| }t        |      D ]  \  }}t	        |t
        j                        st        j                  d      t	        |t
        j                        sJ |j                  |v rt        j                  d      |j                         }	|	d   }
|
dk(  r7|	d   }|cxk  r||z   k  rn n |||||j                  hz        c S ||z  }|
dk(  s|k(  r!r|||fc S 	 |	j                  d      }||fc S |d	z  } t        d
      # t        $ r Y w xY w# t        $ r Y 3w xY w)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>._recurseZ   s   %002IW%D!*!2!2<!@ (K&/o #)"	7!'7+A+AB++J  "'7+A+ABBB$$	1++,MNN((*L	( "%XJ"gHj0HH''#*%):):(;;	    :-K')"g-*$19n#MM%14\1J $+N#::#q(G#)J +,,S  F $, % $%s#   D9 E	9	E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_treeI   s    !YY..x8,g.D.DEEE+668.'*B*BCCC	8+L9N $H-
Q; 7*GW)z)455 *455.	-` -~suEEs  	8$557N	8s   (B8 8CCc                 (    | j                  |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Br   c                 (    | j                  |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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	      s    ) BG"$$":>"  "g66 " " "$4 " " "'++ " " g..   "U5%<0 " "AFFC$Dr   N)rO   typingr    r   r   metadata.modelr   r   __all__r	   rT   r   r   <module>rY      s)    8   , ._D _Dr   