o
    vh$                     @   s   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
 d dlmZmZmZ d dlmZmZ d d	lmZ d d
lmZ ddlmZmZ ddlmZ dejfddZeddG dd deZG dd dejZdS )    N)hexlify)	dataclass)Optional)ConfigurableMixin)ConfigurationError)contentgenericlayout)IndirectObjectpdf_name)rd)BasePdfFileWriter   )AnnotAppearancesCoordinateSystem)STAMP_ART_CONTENTreturnc                 C   s`   t | ts	td| dkrtS | drt| S ddlm} ddl	m
} || }||d dS )Nz)Background specification must be a string	__stamp__z.pdfr   )Image)PdfImage)writer)
isinstancestrr   r   endswithr   ImportedPdfPagePILr   pyhanko.pdf_utils.imagesr   open)bg_specr   r   img r    P/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/pyhanko/stamp/base.py_get_background_content   s   



r"   T)frozenc                       s   e Zd ZU dZdZeed< 	 dZee	j
 ed< 	 ejejjejjejddZejed< 	 d	Zeed
< 	 e fddZdedejdeddfddZ  ZS )BaseStampStylez&
    Base class for stamp styles.
       border_widthN
background   )x_aligny_alignmarginsbackground_layoutg333333?background_opacityc                    sL   t  | d}z|d }W n	 ty   Y nw |dur$t||d< dS dS )a  
        This implementation of :meth:`process_entries` processes the
        :attr:`background` configuration value.
        This can either be a path to an image file, in which case it will
        be turned into an instance of :class:`~.pdf_utils.images.PdfImage`,
        or the special value ``__stamp__``, which is an alias for
        :const:`~pyhanko.stamp.STAMP_ART_CONTENT`.
        Nr'   )superprocess_entriesKeyErrorr"   )clsconfig_dictr   	__class__r    r!   r/   H   s   zBaseStampStyle.process_entriesr   boxtext_paramsr   	BaseStampc                 C      t NNotImplementedError)selfr   r5   r6   r    r    r!   create_stamp\   s   zBaseStampStyle.create_stamp)__name__
__module____qualname____doc__r&   int__annotations__r'   r   r   
PdfContentr	   SimpleBoxLayoutRuleAxisAlignment	ALIGN_MIDMarginsuniformr,   r-   floatclassmethodr/   r   BoxConstraintsdictr=   __classcell__r    r    r3   r!   r$   $   s2   
 
r$   c                	       s   e Zd Z	ddedeej f fddZdd Zdd	 Z	d
d Z
dejfddZejddedededefddZdefddZ  ZS )r7   Nr   r5   c                    s&   t  j||d || _d| _d | _d S )N)r5   r   F)r.   __init__style_resources_ready
_stamp_ref)r<   r   rP   r5   r3   r    r!   rO   f   s   
zBaseStamp.__init__c              	   C   s   | j j}|| j | }|j}|jr$|jr$| j j	| j|j
|j}n| j jj}tjdd|j|jd}t| j j}| jtjjtdttd|td|id d| |f }| |j |S )Nr   )x_scaley_scalex_posy_posz/BackgroundGSz/CAz/ca)categorynamevalues   q /BackgroundGS gs %s %s Q)rP   r'   
set_writerr   renderr5   width_definedheight_definedr,   fitwidthheightr+   r	   Positioningleftbottomr   FloatObjectr-   set_resourcer   ResourceTypeEXT_G_STATEr   DictionaryObjectas_cmimport_resources	resources)r<   bg
bg_contentbg_boxpositioningr+   opacitycommandr    r    r!   _render_backgroundq   s4   
	zBaseStamp._render_backgroundc                 C   r8   r9   r:   )r<   r    r    r!   _render_inner_content   s   zBaseStamp._render_inner_contentc                 C   sp   dg}|   }| jjr||   |r|| | j}| jj}|r.|d||j|j	f  |d d
|S )N   qs   %g w 0 0 %g %g re S   Q    )rs   rP   r'   appendrr   extendr5   r&   r_   r`   join)r<   command_streaminner_contentbboxr&   r    r    r!   r[      s   


zBaseStamp.renderr   c                 C   s0   | j }|du r| j}|  }|| | _ }|S )a  
        Register the stamp with the writer coupled to this instance, and
        cache the returned reference.

        This works by calling :meth:`.PdfContent.as_form_xobject`.

        :return:
            An indirect reference to the form XObject containing the stamp.
        N)rR   _ensure_writeras_form_xobject
add_object)r<   	stamp_refwr	form_xobjr    r    r!   register   s   
zBaseStamp.register)coords	dest_pagexyr   c             	   C   s   |   }dtt j }dt|t||f }tj|d}tt	dtt	|
d|ii}	| j}
|
dus:J |tjkr[|
j||
tjdddd	 |
||
tjd
d |
||
||	}| jj| jjf}||fS )a  
        Apply a stamp to a particular page in the PDF writer attached to this
        :class:`.BaseStamp` instance.

        :param dest_page:
            Index of the page to which the stamp is to be applied
            (starting at `0`).
        :param x:
            Horizontal position of the stamp's lower left corner on the page.
        :param y:
            Vertical position of the stamp's lower left corner on the page.
        :param coords:
            The coordinate convention to use.
        :return:
            A reference to the affected page object, together with
            a ``(width, height)`` tuple describing the dimensions of the stamp.
        s   /Stamps   q 1 0 0 1 %g %g cm %s Do Q)stream_dataz/XObjectasciiNrt   T)prependru   )r   r   uuiduuid4bytesr   r   StreamObjectrh   r   decoder   r   PAGE_DEFAULTadd_stream_to_pager   r5   r_   r`   )r<   r   r   r   r   r   resource_namestamp_paintstamp_wrapper_streamrk   r   page_refdimsr    r    r!   apply   sF   



zBaseStamp.applyc                 C   s   |   }t|dS )a  
        Turn this stamp into an appearance dictionary for an annotation
        (or a form field widget), after rendering it.
        Only the normal appearance will be defined.

        :return:
            An instance of :class:`.AnnotAppearances`.
        )normal)r   r   )r<   r   r    r    r!   as_appearances  s   
zBaseStamp.as_appearancesr9   )r>   r?   r@   r   r   r	   rL   rO   rr   rs   r[   r   r
   r   r   r   rB   r   r   r   rN   r    r    r3   r!   r7   e   s,    '
?r7   )r   binasciir   dataclassesr   typingr   pyhanko.config.apir   pyhanko.config.errorsr   pyhanko.pdf_utilsr   r   r	   pyhanko.pdf_utils.genericr
   r   pyhanko.pdf_utils.miscr   pyhanko.pdf_utils.writerr   appearancesr   r   artr   rD   r"   r$   r7   r    r    r    r!   <module>   s     @