o
    vh.                     @   s   d dl Z d dlmZ d dlmZmZ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mZ dd	lmZ dd
lmZmZ g dZG dd de jZdZ	 eddG dd deZG dd deZdS )    N)	dataclass)ListOptionalTuple)ConfigurationError)contentgenericlayout)pdf_name
pdf_string)BasePdfFileWriterinit_xobject_dictionary   )CoordinateSystem)	TextStampTextStampStyle)
QRPositionQRStampStyleQRStampDEFAULT_QR_SCALEc                   @   s   e Zd ZdZejejjejjdZ	ejejj
ejjdZejejj
ejjdZejejjejjdZedd Zed
ddZd	S )r   z[
    QR positioning constants, with the corresponding default content layout
    rule.
    )x_aligny_align)r   r   c                 C   s   | t jt jfv S N)r   LEFT_OF_TEXTRIGHT_OF_TEXT)self r   N/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/pyhanko/stamp/qr.pyhorizontal_flow'   s   zQRPosition.horizontal_flowreturnc                 C   s@   zt jt jt jt jd|  W S  ty   td| dw )a  
        Convert from a configuration string.

        :param config_str:
            A string: 'left', 'right', 'top', 'bottom'
        :return:
            An :class:`.QRPosition` value.
        :raise ConfigurationError: on unexpected string inputs.
        leftrighttopbottom'zW' is not a valid QR position setting; valid values are 'left', 'right', 'top', 'bottom')r   r   r   
ABOVE_TEXT
BELOW_TEXTlowerKeyErrorr   )cls
config_strr   r   r   from_config+   s   
zQRPosition.from_configN)r   r   )__name__
__module____qualname____doc__r	   SimpleBoxLayoutRuleAxisAlignment	ALIGN_MIN	ALIGN_MIDr   	ALIGN_MAXr   r&   r'   propertyr   classmethodr,   r   r   r   r   r      s,    
r   g?T)frozenc                       s   e Zd ZU dZdZeed< 	 dZeed< 	 dZ	e
e ed< 	 ejZeed< 	 dZe
ej ed	< 	 e fd
dZdedejdeddfddZ  ZS )r   z
    Style for text-based stamps together with a QR code.

    This is exactly the same as a text stamp, except that the text box
    is rendered with a QR code to the left of it.
       innsepz@Digital version available at
this url: %(url)s
Timestamp: %(ts)s
stamp_textNqr_inner_sizeqr_positionqr_inner_contentc                    s>   t  | z|d }t||d< W d S  ty   Y d S w )Nr=   )superprocess_entriesr   r,   r)   )r*   config_dictqr_pos	__class__r   r   r@   z   s   zQRStampStyle.process_entrieswriterboxtext_paramsr   r   c                 C   s:   z| d}W n ty   tdw t|| |||dS )Nurlz7Using a QR stamp style requires a 'url' text parameter.)stylerH   rG   rF   )popr)   r	   LayoutErrorr   )r   rE   rF   rG   rH   r   r   r   create_stamp   s   
zQRStampStyle.create_stamp)r-   r.   r/   r0   r:   int__annotations__r;   strr<   r   r   r   r=   r>   r   
PdfContentr7   r@   r   r	   BoxConstraintsdictrL   __classcell__r   r   rC   r   r   M   s0   
 
	r   c                
       s   e Zd Z		ddedededeej f fddZ	dd	 Z
d
eee eeef f f fddZdd Z fddZejddef fddZ  ZS )r   NrE   rH   rI   rF   c                    s$   t  j||||d || _d | _d S )N)rG   rF   )r?   __init__rH   _qr_size)r   rE   rH   rI   rG   rF   rC   r   r   rT      s   
zQRStamp.__init__c                 C   s   | j }|jd ur|jS |jjS r   )rI   inner_content_layoutr=   value)r   rI   r   r   r   _inner_content_layout_rule   s   
z"QRStamp._inner_content_layout_ruler   c                    s  t   \}\}}|  \}}| jtjjtd|d | j}| j	}|j
}|jd ur-|j}	n"|jrG|jrGtt|j|t|j|}
|
d|  }	nttt| }	|	| }|	d|  }|jjrg|| }t||}n	t||}|| }|jj}tj|j|jtj|tjjd}t ||}|!||	|	}d|j"| |j#| |j$|j%f }|jt&j'krtj|dddd}n)|jt&j(krtj|dddd}n|jt&j)krtj|dddd	}n	tj|dddd
}tj|jj*|jj*|tjjd}|!|||}|d|+ g}|,| |-d |||ffS )Nz/QR)categorynamerW      )r   r   marginsinner_content_scalings   q %g 0 0 %g %g %g cm /QR Do Qr   r    )r"   r!   r#   r$   )r$   r"   r!   r#   )r#   r"   r!   r$      q   Q).r?   _inner_layout_natural_size_qr_xobjectset_resourcer   ResourceTypeXOBJECTr
   rI   rF   r:   r<   width_definedheight_definedminmaxheightwidthrM   roundr   r=   r   rW   r	   r1   r   r   MarginsuniformInnerScaling
NO_SCALINGrQ   fitx_scaley_scalex_posy_posr   r   r   r'   flippedas_cmextendappend)r   text_commands
text_widthtext_heightqr_refnatural_qr_sizerI   	stamp_boxr:   qr_sizemin_dimqr_innunits_scale	qr_padded	inn_width
inn_heightdefault_layoutqr_layout_rule	inner_box
qr_inn_posdraw_qr_command
tb_marginstb_layout_ruletext_inn_poscommandsrC   r   r   r`      s   




	


z"QRStamp._inner_layout_natural_sizec                 C   s   dd l }ddlm}m} | jjd u}|r|jjn|jj}|j	|d}|
| j |  |r:|j||j| jjd}n|j|d}| }|jd|j  |j }	t||	|	}
|
  | j|
|	fS )Nr   )PdfFancyQRImagePdfStreamQRImage)error_correction)image_factoryversioncenter_image)r   r[   )qrcodepyhanko.pdf_utils.qrr   r   rI   r>   	constantsERROR_CORRECT_HERROR_CORRECT_MQRCodeadd_datarH   make
make_imager   render_command_streammodules_countborderbox_sizer   compressrE   
add_object)r   r   r   r   is_fancyerr_corrqrimgcommand_stream	bbox_sizeqr_xobjr   r   r   ra     s,   
zQRStamp._qr_xobjectc                    s   t   }| j|d< |S )NrH   )r?   get_default_text_paramsrH   )r   tprC   r   r   r   6  s   

zQRStamp.get_default_text_paramscoordsr   c                   s   t  j||||d\}\}}|||| || f}ttdtdtdtdtdttttj|tdttdtd	td	t	| j
ii}	| j}
|
d usSJ |
||
|	 |||ffS )
Nr   z/Typez/Annotz/Subtypez/Linkz/Rectz/Az/Sz/URI)r?   applyr   DictionaryObjectr
   ArrayObjectlistmapFloatObjectr   rH   rE   register_annotationr   )r   	dest_pagexyr   page_refwh	link_rect
link_annotwrrC   r   r   r   ;  s&   

zQRStamp.apply)NN)r-   r.   r/   r   rO   r   r   r	   rQ   rT   rX   r   r   bytesrM   r`   ra   r   r   PAGE_DEFAULTr   rS   r   r   rC   r   r      s(    &nr   )enumdataclassesr   typingr   r   r   pyhanko.config.errorsr   pyhanko.pdf_utilsr   r   r	   pyhanko.pdf_utils.genericr
   r   pyhanko.pdf_utils.writerr   r   appearancesr   textr   r   __all__Enumr   r   r   r   r   r   r   r   <module>   s     4H