
    Csi%                        d dl Z d dlmZ d dl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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j8                  fdZ ed       G d de	             Z G d dej8                        Zy)    N)hexlify)	dataclass)OptionalTuple)ConfigurableMixin)ConfigurationError)contentgenericlayout)IndirectObjectpdf_name)rd)BasePdfFileWriter   )AnnotAppearancesCoordinateSystem)STAMP_ART_CONTENTreturnc                     t        | t              st        d      | dk(  rt        S | j	                  d      rt        j                  |       S ddlm} ddl	m
} |j                  |       } ||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   imgs       ^/var/www/python-projects/worksol/worksolenv/lib/python3.12/site-packages/pyhanko/stamp/base.py_get_background_contentr$      sh    gs# !LMM +  			&	!&&w//5jj!D))    T)frozenc                       e Zd ZU dZdZeed<   	 dZee	e
e
e
f      ed<   	 dZeej                     ed<   	  ej                  ej                   j"                  ej                   j"                  ej$                  j'                  d            Zej                  ed	<   	 d
Ze
ed<   	 e fd       Zdedej2                  deddfdZ xZS )BaseStampStylez&
    Base class for stamp styles.
       border_widthNborder_color
background   )x_aligny_alignmarginsbackground_layoutg333333?background_opacityc                 t    t         |   |       d}	 |d   }|t        |      |d<   yy# t        $ r Y w xY w)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__s      r#   r5   zBaseStampStyle.process_entriesP   sV     	,	!,/G (?(HK%   		s   + 	77r   boxtext_paramsr   	BaseStampc                     t         NNotImplementedError)selfr   r:   r;   s       r#   create_stampzBaseStampStyle.create_stampd   s
     "!r%   )__name__
__module____qualname____doc__r*   int__annotations__r+   r   r   floatr,   r	   
PdfContentr   SimpleBoxLayoutRuleAxisAlignment	ALIGN_MIDMarginsuniformr1   r2   classmethodr5   r   BoxConstraintsdictrB   __classcell__r9   s   @r#   r(   r(   $   s    L# :>L(5u!456= 04J++,3
 5OF4N4N$$..$$..&&q)5v11 
 !$#
 I I&"!" """ 	"
 
"r%   r(   c            	            e Zd Z	 ddedeej                     f fdZd Zd Z	d Z
dej                  fdZej                  d	d
edededefdZdefdZ xZS )r<   r   r:   c                 R    t         |   ||       || _        d| _        d | _        y )N)r:   r   F)r4   __init__style_resources_ready
_stamp_ref)rA   r   rX   r:   r9   s       r#   rW   zBaseStamp.__init__n   s-     	S0
 %48r%   c                 d   | j                   j                  }|j                  | j                         |j	                         }|j
                  }|j                  rR|j                  rF| j                   j                  j                  | j
                  |j                  |j                        }nM| j                   j                  j                  }t        j                  dd|j                  |j                         }t#        j$                  | j                   j&                        }| j)                  t*        j,                  j.                  t1        d      t#        j2                  t1        d      |t1        d      |i             d|j5                         |fz  }| j7                  |j8                         |S )Nr   )x_scaley_scalex_posy_posz/BackgroundGSz/CAz/ca)categorynamevalues   q /BackgroundGS gs %s %s Q)rX   r,   
set_writerr   renderr:   width_definedheight_definedr1   fitwidthheightr0   r   Positioningleftbottomr
   FloatObjectr2   set_resourcer	   ResourceTypeEXT_G_STATEr   DictionaryObjectas_cmimport_resources	resources)rA   bg
bg_contentbg_boxpositioningr0   opacitycommands           r#   _render_backgroundzBaseStamp._render_backgroundy   sL   ZZ""
dkk"YY[
F$9$9**66::&,,K jj22::G ,,1GLLK
 %%djj&C&CD))55/***%'8E?GD 	 	
 03
 
 	bll+r%   c                     t         r>   r?   )rA   s    r#   _render_inner_contentzBaseStamp._render_inner_content   s    !!r%   c                    dg}| j                         }| j                  j                  r|j                  | j	                                |r|j                  |       | j                  }| j                  j                  }| j                  j                  }|rA|r|j                  d|z         |j                  d||j                  |j                  fz         |j                  d       dj                  |      S )N   qs   %g %g %g RGs   %g w 0 0 %g %g re S   Q    )r}   rX   r,   appendr{   extendr:   r*   r+   rh   ri   join)rA   command_streaminner_contentbboxr*   r+   s         r#   rd   zBaseStamp.render   s    224
 ::  !!$"9"9";< !!-0 xxzz..zz..%%n|&CD!!&T[[9:
 	d#yy((r%   r   c                     | j                   }|4| j                  }| j                         }|j                  |      x| _         }|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.
        )rZ   _ensure_writeras_form_xobject
add_object)rA   	stamp_refwr	form_xobjs       r#   registerzBaseStamp.register   sG     OO	$$B,,.I*,--	*BBDOir%   )coords	dest_pagexyr   c          
      2   | j                         }dt        t        j                         j                        z   }dt        |      t        |      |fz  }t        j                  |      }t        j                  t        d      t        j                  t        |j                  d            |i      i      }	| j                  }
|
J |t        j                  k(  rl|
j                  ||
j                  t        j                  d            d       |
j                  ||
j                  t        j                  d	                   |
j                  ||
j                  |      |	      }| 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asciir   T)prependr   )r   r   uuiduuid4bytesr   r
   StreamObjectrq   r   decoder   r   PAGE_DEFAULTadd_stream_to_pager   r:   rh   ri   )rA   r   r   r   r   r   resource_namestamp_paintstamp_wrapper_streamrt   r   page_refdimss                r#   applyzBaseStamp.apply   sn   2 MMO	!GDJJL,>,>$??3qEqE7
 

  '33L,,$g&>&>m227;<iH'
	 [[~~%222!!((T:  "  !!((T: ((r}}%9:I
 0~r%   c                 :    | j                         }t        |      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   )rA   r   s     r#   as_appearanceszBaseStamp.as_appearances  s     MMO	y11r%   r>   )rC   rD   rE   r   r   r   rQ   rW   r{   r}   rd   r
   r   r   r   r   rG   r   r   r   rS   rT   s   @r#   r<   r<   m   s    
 04		9!	9 f++,		9%N")>'00 . $4#@#@== = 	= !=~2 0 2r%   r<   ) r   binasciir   dataclassesr   typingr   r   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   rJ   r$   r(   r<    r%   r#   <module>r      sw      ! " 0 4 6 6 > % 6 ; "*(:(: *& $E"& E" E"Pq2"" q2r%   