
    Wwg%                         d dl Z d dlZd dlmZ d dl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 ddlmZ g d	Z G d
 de      Z G d de      Zd Z G d d      Z G d d      Z G d de      Z G d de      Zy)    N)Enum)Optional   )DictionaryObject
NameObject	PdfObjectStreamObjectpdf_name)BoxConstraints)PdfFileReaderBasePdfFileWriter)ResourceTypeResourceManagementErrorPdfResources
PdfContent
RawContentImportedPdfPagec                       e Zd ZdZ ed      Z	  ed      Z	  ed      Z	  ed      Z	  ed      Z		  ed      Z
	  ed      Zy	)
r   u|   
    Enum listing resources that can be used as keys in a resource dictionary.

    See ISO 32000-1, § 7.8.3 Table 34.
    z
/ExtGStatez/ColorSpacez/Patternz/Shadingz/XObjectz/Fontz/PropertiesN)__name__
__module____qualname____doc__r
   EXT_G_STATECOLOR_SPACEPATTERNSHADINGXOBJECTFONT
PROPERTIES     P/var/www/horilla/myenv/lib/python3.12/site-packages/pyhanko/pdf_utils/content.pyr   r      s~     <(K
 =)K
 z"G
 z"G
 z"G
 GD
 -(Jr"   r   c                       e Zd ZdZy)r   z=
    Used to signal problems with resource dictionaries.
    N)r   r   r   r   r!   r"   r#   r   r   P   s     	r"   r   c                 f    |j                         D ]  \  }}|| v rt        d| d      || |<    | S )NzResource with name z occurs in both dictionaries.)itemsr   )dict1dict2kv2s       r#   _res_merge_helperr+   X   sN     2:)%aS(EF  a Lr"   c                   4    e Zd ZdZd ZdefdZdefdZd Z	y)	r   a8  
    Representation of a PDF resource dictionary.

    This class implements :meth:`__getitem__` with :class:`.ResourceType` keys
    for dynamic access to its attributes.
    To merge two instances of :class:`.PdfResources` into one another,
    the class overrides :meth:`__iadd__`, so you can write.

    .. code-block:: python

        res1 += res2

    *Note:* Merging two resource dictionaries with conflicting resource names
    will produce a :class:`.ResourceManagementError`.

    *Note:* This class is currently only used for new resource dictionaries.
    c                     t               | _        t               | _        t               | _        t               | _        t               | _        t               | _        t               | _        y N)r   ext_g_statecolor_spacepatternshadingxobjectfont
propertiesselfs    r#   __init__zPdfResources.__init__u   sJ    +-+-')')')$&	*,r"   itemc                 J    t        | |j                  j                               S r.   )getattrnamelower)r7   r9   s     r#   __getitem__zPdfResources.__getitem__~   s    tTYY__.//r"   returnc                 b      fd}t         |       D ci c]  \  }}||
 c}}      S c c}}w )zj
        Render this instance of :class:`.PdfResources` to an actual resource
        dictionary.
        c               3   T   K   t         D ]  } |    }|s| j                  |f  y wr.   )r   value)r)   valr7   s     r#   _genz(PdfResources.as_pdf_object.<locals>._gen   s0     ! '1g''3,&'s   (()r   )r7   rD   r)   vs   `   r#   as_pdf_objectzPdfResources.as_pdf_object   s-    	'  $& 9$!QA 9:: 9s   +
c                 @    t         D ]  }t        | |   ||           | S )a(  
        Merge another resource dictionary into this one.
        :param other:
            Another instance of :class:`.PdfResources`
        :return:
            Always returns ``self``
        :raises ResourceManagementError:
            Raised when there is a resource name conflict.
        )r   r+   )r7   otherr)   s      r#   __iadd__zPdfResources.__iadd__   s*      	1Ad1guQx0	1r"   N)
r   r   r   r   r8   r   r>   r   rF   rI   r!   r"   r#   r   r   b   s*    $-0 0;/ ;r"   r   c                       e Zd ZdZdZ	 	 	 	 ddee   dee   dee   fdZ	e
defd       Zd	ed
edefdZdefdZe
defd       ZdefdZdefdZd Z	 ddededefdZy)r   z
    Abstract representation of part of a PDF content stream.

    .. warning::

        Whether :class:`.PdfContent` instances can be reused or not
        is left up to the subclasses.
    N	resourcesboxwriterc                 ^    |xs
 t               | _        |xs
 t               | _        || _        y r.   )r   
_resourcesr   rL   rM   )r7   rK   rL   rM   s       r#   r8   zPdfContent.__init__   s(     )2(C\^#&#:.*:r"   r?   c                 H    | j                   t        d      | j                   S )NzPDF writer is not set)rM   
ValueErrorr6   s    r#   _ensure_writerzPdfContent._ensure_writer   s"    ;;455{{r"   categoryr<   rB   c                 (    || j                   |   |<   y)a2  Set a value in the resource dictionary associated with this content
        fragment.

        :param category:
            The resource category to which the resource belongs.
        :param name:
            The resource's (internal) name.
        :param value:
            The resource's value.
        NrO   )r7   rS   r<   rB   s       r#   set_resourcezPdfContent.set_resource   s     +0!$'r"   c                 .    | xj                   |z  c_         y)zImport resources from another resource dictionary.

        :param resources:
            An instance of :class:`.PdfResources`.
        :raises ResourceManagementError:
            Raised when there is a resource name conflict.
        NrU   )r7   rK   s     r#   import_resourceszPdfContent.import_resources   s     	9$r"   c                     | j                   S )z}
        :return:
            The :class:`.PdfResources` instance associated with this
            content fragment.
        rU   r6   s    r#   rK   zPdfContent.resources   s     r"   c                     t         )z<
        Compile the content to graphics operators.
        )NotImplementedErrorr6   s    r#   renderzPdfContent.render   s
     "!r"   c                     ddl m} | j                         } ||| j                  j                  | j                  j
                  | j                  j                               S )u  
        Render the object to a form XObject to be referenced by another
        content stream. See ISO 32000-1, § 8.8.

        *Note:* Even if :attr:`writer` is set, the resulting form XObject will
        not be registered. This is left up to the caller.

        :return:
            A :class:`~.generic.StreamObject` instance representing
            the resulting form XObject.
        r   )init_xobject_dictionary)command_stream	box_width
box_heightrK   )pyhanko.pdf_utils.writerr^   r\   rL   widthheightrO   rF   )r7   r^   r_   s      r#   as_form_xobjectzPdfContent.as_form_xobject   sI     	E&)hhnnxxoo335	
 	
r"   c                     || _         y)z
        Override the currently registered writer object.

        :param writer:
            An instance of :class:`~.writer.BasePdfFileWriter`.
        N)rM   )r7   rM   s     r#   
set_writerzPdfContent.set_writer  s     r"   page_ixprependc                     t        i | j                               }|j                  ||j                  |      | j                  j                         |      S )a  
        Convenience wrapper around :meth:`.BasePdfFileWriter.add_stream_to_page`
        to turn a :class:`.PdfContent` instance into a page
        content stream.

        :param writer:
            A PDF file writer.
        :param page_ix:
            Index of the page to modify.
            The first page has index `0`.
        :param prepend:
            Prepend the content stream to the list of content streams, as
            opposed to appending it to the end.
            This has the effect of causing the stream to be rendered
            underneath the already existing content on the page.
        :return:
            An :class:`~.generic.IndirectObject` reference to the page object
            that was modified.
        )stream_data)rK   ri   )r	   r\   add_stream_to_page
add_objectrK   rF   )r7   rM   rh   ri   	as_streams        r#   add_to_pagezPdfContent.add_to_page  sR    , !?	((i(nn224	 ) 
 	
r"   )NNN)F)r   r   r   r   rM   r   r   r   r   r8   propertyrR   r   r   r   rV   rX   rK   bytesr\   r	   re   rg   intboolro   r!   r"   r#   r   r      s     F -1(,.2	L) n% *+	  1  0$0,60?H0%, % <  " "
 
, HM
'
25
@D
r"   r   c                   N     e Zd ZdZ	 	 ddedee   dee   f fdZdefdZ	 xZ
S )	r   z,Raw byte sequence to be used as PDF content.datarK   rL   c                 4    t         |   ||       || _        y r.   )superr8   ru   )r7   ru   rK   rL   	__class__s       r#   r8   zRawContent.__init__.  s     	C(	r"   r?   c                     | j                   S r.   )ru   r6   s    r#   r\   zRawContent.render7  s    yyr"   )NN)r   r   r   r   rq   r   r   r   r8   r\   __classcell__rx   s   @r#   r   r   +  sE    6
 -1(,	 L) n%	 r"   r   c                   0     e Zd ZdZd fd	ZdefdZ xZS )r   z,Import a page from another PDF file (lazily)c                 >    || _         || _        t        |           y r.   )	file_namerh   rw   r8   )r7   r~   rh   rx   s      r#   r8   zImportedPdfPage.__init__>  s    "r"   r?   c                    ddl m} | j                  }t        | j                  d      5 }t        |      }|j                  || j                        }d d d        dt        j                  t        j                         j                        z   }| j                  j                  |j                  d      <   |j!                         d   \  }}}	}
t#        t%        ||	z
        t%        ||
z
              | _        |d	z   S # 1 sw Y   xY w)
Nr   r   rb)rh   s   /Importasciiz/BBox)rc   rd   s    Do)rM   r   rR   openr~   r   import_page_as_xobjectrh   binasciihexlifyuuiduuid4rq   rK   r3   decode
get_objectr   absrL   )r7   r   winfrxobjresource_namex1y1x2y2s              r#   r\   zImportedPdfPage.renderC  s    -#22$..$' 	E3c"A++At||+DD	E #X%5%5djjl6H6H%II@D}33G<= *73BB!BGSb\Jv%%	E 	Es   )C==D)r   )r   r   r   r   r8   rq   r\   rz   r{   s   @r#   r   r   ;  s    6
& &r"   r   )r   r   enumr   typingr   genericr   r   r   r	   r
   layoutr   readerr   rM   r   __all__r   rQ   r   r+   r   r   r   r   r!   r"   r#   <module>r      s{         # ! %/4 /d	j 	9 9xJ
 J
Z  &j &r"   