
     h`D              
       d   d Z 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	 ddl
mZ ddlmZ g dZ ej        e          Z G d	 d
e          Z G d de          Z G d d          Z G d dej                  Z G d dej                  Zej        ej        ej        ej        ej        ej        iZ ed           G d de                      Zdededededef
dZdededededef
dZ  ed           G d d e                      Z! ed           G d! d"e                      Z"dS )#z!Layout utilities (to be expanded)    N)	dataclass)Fraction)OptionalUnion)ConfigurableMixin)ConfigurationError)LayoutErrorBoxSpecificationErrorBoxConstraintsAxisAlignmentMarginsInnerScalingSimpleBoxLayoutRulePositioningc                   (     e Zd ZdZdef fdZ xZS )r	   z+Indicates an error in a layout computation.msgc                 J    || _          t                      j        |g|R   d S N)r   super__init__)selfr   args	__class__s      T/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/pyhanko/pdf_utils/layout.pyr   zLayoutError.__init__   s/    $t$$$$$$    )__name__
__module____qualname____doc__strr   __classcell__r   s   @r   r	   r	      sJ        55%C % % % % % % % % % %r   r	   c                   6     e Zd ZdZddee         f fdZ xZS )r
   z4Raised when a box constraint is over/underspecified.Nr   c                 P    t                                          |pd           d S )Nz%box constraint is over/underspecified)r   )r   r   )r   r   r   s     r   r   zBoxSpecificationError.__init__%   s*    SK$KLLLLLr   r   )r   r   r   r   r   r    r   r!   r"   s   @r   r
   r
   "   s_        >>M MHSM M M M M M M M M M Mr   r
   c                      e Zd ZU dZee         ed<   ee         ed<   ee         ed<   eed<   	 	 	 dde	ee
df         de	ee
df         d	ee         fd
Zd Zedefd            Zej        d             Zedefd            Zedefd            Zej        d             Zedefd            Zedefd            Zedefd            ZdS )r   a)  Represents a box of potentially variable width and height.
    Among other uses, this can be leveraged to produce a variably sized
    box with a fixed aspect ratio.

    If width/height are not defined yet, they can be set by assigning to the
    :attr:`width` and :attr:`height` attributes.
    _width_height_ar_fully_specifiedNwidthheightaspect_ratioc                    |t          |          nd }|t          |          nd }|| _        || _        d}d | _        |||d S |#|!|t          t          ||          | _        d}nV|T|| _        |%t          t          ||z                      | _        n&|$t          t          ||z                      | _        || _        d S )NFT)intr&   r'   r(   r
   r   roundr)   )r   r*   r+   r,   	int_width
int_heightfully_specifieds          r   r   zBoxConstraints.__init__7   s     #("3CJJJ	$*$6S[[[D
!!38LF"z'='++	:66DH"OO%#DH%!%
\(A"B"BCC&"5\)A#B#BCC /r   c                 H   | j         /| j        (t          | j         | j                  | _        d| _        d S | j        b| j        *t          | j        | j        z            | _         d| _        d S | j         ,t          | j         | j        z            | _        d| _        d S d S d S )NT)r&   r'   r   r(   r)   r.   r   s    r   _recalculatezBoxConstraints._recalculateU   s    ;"t|'?T\::DH$(D!!!X!|'!$,"9::(,%%%("4;#9::(,%%% "! )(r   returnc                 ,    | j         | j         S t          )z
        :return:
            The width of the box.
        :raises BoxSpecificationError:
            if the box's width could not be determined.
        )r&   r
   r4   s    r   r*   zBoxConstraints.widtha   s     ;";''r   c                 X    | j         || _         |                                  d S t          r   )r&   r5   r
   r   r*   s     r   r*   zBoxConstraints.widthn   s/    ;DK''r   c                     | j         duS )z
        :return:
            ``True`` if the box currently has a well-defined width,
            ``False`` otherwise.
        N)r&   r4   s    r   width_definedzBoxConstraints.width_definedv   s     {$&&r   c                 ,    | j         | j         S t          )z
        :return:
            The height of the box.
        :raises BoxSpecificationError:
            if the box's height could not be determined.
        )r'   r
   r4   s    r   r+   zBoxConstraints.height   s     <#<''r   c                 X    | j         || _         |                                  d S t          r   )r'   r5   r
   r   r+   s     r   r+   zBoxConstraints.height   s/    <!DL''r   c                     | j         duS )z
        :return:
            ``True`` if the box currently has a well-defined height,
            ``False`` otherwise.
        N)r'   r4   s    r   height_definedzBoxConstraints.height_defined   s     |4''r   c                 ,    | j         | j         S t          )z
        :return:
            The aspect ratio of the box.
        :raises BoxSpecificationError:
            if the box's aspect ratio could not be determined.
        )r(   r
   r4   s    r   r,   zBoxConstraints.aspect_ratio   s     88O''r   c                     | j         duS )z
        :return:
            ``True`` if the box currently has a well-defined aspect ratio,
            ``False`` otherwise.
        N)r(   r4   s    r   aspect_ratio_definedz#BoxConstraints.aspect_ratio_defined   s     xt##r   )NNN)r   r   r   r   r   r.   __annotations__r   boolr   floatr   r5   propertyr*   setterr;   r+   r@   r,   rC    r   r   r   r   )   s          SMc]	(	 *.*.+/	0 0S%%&0 c5$&'0 x(	0 0 0 0<
- 
- 
- 
(s 
( 
( 
( X
( \( ( \( 't ' ' ' X' 
( 
( 
( 
( X
( ]( ( ]( ( ( ( ( X( 
(h 
( 
( 
( X
( $d $ $ $ X$ $ $r   r   c                       e Zd ZdZ ej                    Z	  ej                    Z	  ej                    Z	  ej                    Z		 e
dedd fd            ZdS )r   z(Class representing a scaling convention.
config_strr6   c                     	 t           j        t           j        t           j        t           j        d|                                         S # t          $ r t          d| d          w xY w)a   
        Convert from a configuration string.

        :param config_str:
            A string: 'none', 'stretch-fill', 'stretch-to-fit', 'shrink-to-fit'
        :return:
            An :class:`.InnerScaling` value.
        :raise ConfigurationError: on unexpected string inputs.
        )nonezstretch-fillzstretch-to-fitzshrink-to-fit'zs' is not a valid inner scaling setting; valid values are 'none', 'stretch-fill', 'stretch-to-fit', 'shrink-to-fit'.)r   
NO_SCALINGSTRETCH_FILLSTRETCH_TO_FITSHRINK_TO_FITlowerKeyErrorr   )clsrK   s     r   from_configzInnerScaling.from_config   s    	$/ , 9"."=!-!;	 
   " "  	 	 	$$J $ $ $  	s   AA	 	A'N)r   r   r   r   enumautorO   rP   rQ   rR   classmethodr    rV   rI   r   r   r   r      s        22J49;;L5TY[[N
 DIKKM
 S ^    [  r   r   c                       e Zd ZdZ ej                    Z	  ej                    Z	  ej                    Z	 e	de
dd fd            Ze	de
dd fd            Zed             Zdededefd	Zd
S )r   z;Class representing one-dimensional alignment along an axis.	align_strr6   c                     	 t           j        t           j        t           j        d|                                         S # t
          $ r t          d| d          w xY w)a  
        Convert from a horizontal alignment config string.

        :param align_str:
            A string: 'left', 'mid' or 'right'.
        :return:
            An :class:`.AxisAlignment` value.
        :raise ConfigurationError: on unexpected string inputs.
        )leftmidrightrN   zO' is not a valid horizontal alignment; valid values are 'left', 'mid', 'right'.r   	ALIGN_MIN	ALIGN_MID	ALIGN_MAXrS   rT   r   rU   r[   s     r   from_x_alignzAxisAlignment.from_x_align   s    
	%/$.&0  oo	! !
  	 	 	$6I 6 6 6  		   ;> Ac                     	 t           j        t           j        t           j        d|                                         S # t
          $ r t          d| d          w xY w)a  
        Convert from a vertical alignment config string.

        :param align_str:
            A string: 'bottom', 'mid' or 'top'.
        :return:
            An :class:`.AxisAlignment` value.
        :raise ConfigurationError: on unexpected string inputs.
        )bottomr^   toprN   zM' is not a valid vertical alignment; valid values are 'bottom', 'mid', 'top'.r`   rd   s     r   from_y_alignzAxisAlignment.from_y_align  s    
	'1$.$.  oo	! !
  	 	 	$6I 6 6 6  	rf   c                     t           |          S r   )_alignment_oppositesr4   s    r   flippedzAxisAlignment.flipped#  s    #D))r   container_len	inner_lenc                 6   t                               d|||          }| t          j        k    r||z
  |z
  S | t          j        k    r|S ||k    r)t
                              d| d| d| d| d	           |S | t          j        k    r||z
  dz  }||z   S t          )NlengthzContent box width/height z  is too wide for container size z with margins (, z); post_margin will be ignored   )	r   	effectiver   rc   ra   loggerwarningrb   	TypeError)r   rn   ro   
pre_marginpost_margineffective_max_leninner_offsets          r   alignzAxisAlignment.align'  s     $--mZ
 
 =***
 !9,{::],,,***NNNI N N"/N NN N"-N N N  
 ],,,-	9a?L,,r   N)r   r   r   r   rW   rX   ra   rb   rc   rY   r    re   rj   rG   rm   r.   r|   rI   r   r   r   r      s        EE	I 	I 	I S _    [, S _    [, * * X* -0	     r   r   T)frozenc                   J    e Zd ZU dZeed<   	 eed<   	 eed<   	 eed<   	 d ZdS )r   zP
    Class describing the position and scaling of an object in a container.
    x_posy_posx_scaley_scalec                 <    d| j         | j        | j        | j        fz  S )z
        Convenience method to convert this :class:`.Positioning` into a PDF
        ``cm`` operator.

        :return:
            A byte string representing the ``cm`` operator corresponding
            to this :class:`.Positioning`.
        s   %g 0 0 %g %g %g cm)r   r   r   r   r4   s    r   as_cmzPositioning.as_cm^  s*     %LLJJ	(
 
 	
r   N)r   r   r   r   r.   rD   rF   r   rI   r   r   r   r   L  sa           JJJJJJNNNNNN
 
 
 
 
r   r   	alignmentcontainer_boxinner_nat_widthrx   ry   c                 h    |j         r|                     |j        |||          S ||z   |z   |_        |S r   )r;   r|   r*   )r   r   r   rx   ry   s        r   
_aln_widthr   o  sK     " *k
 
 	
 .
:[Hr   inner_nat_heightc                 h    |j         r|                     |j        |||          S ||z   |z   |_        |S r   )r@   r|   r+   )r   r   r   rx   ry   s        r   _aln_heightr     sL     #  "2J
 
 	
  0*<{Jr   c                        e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed<   e
d             Zed             Zd	 Zd
 Ze
 fd            Z xZS )r   z"Class describing a set of margins.r   r]   r_   ri   rh   c                 &    t          ||||          S )z
        Return a set of uniform margins.

        :param num:
            The uniform margin to apply to all four sides.
        :return:
            ``Margins(num, num, num, num)``
        )r   )rU   nums     r   uniformzMargins.uniform  s     sCc***r   c                 Z    ||z
  |z
  }|dk     rt          d| d| d|  d| d	          |S )z4Internal helper method to compute effective margins.r   z	Margins (rr   z) too wide for container  .)r	   )dim_namern   preposteffs        r   rt   zMargins.effective  sq     c!D(77/C / /4 / // /+/ / /   
r   c                 P    t                               d|| j        | j                  S )a&  
        Compute width without margins.

        :param width:
            The container width.
        :return:
            The width after subtracting the left and right margins.
        :raises LayoutError:
            if the container width is too short to accommodate the margins.
        r*   )r   rt   r]   r_   r9   s     r   effective_widthzMargins.effective_width  s"       %DJGGGr   c                 P    t                               d|| j        | j                  S )a+  
        Compute height without margins.

        :param height:
            The container height.
        :return:
            The height after subtracting the top and bottom margins.
        :raises LayoutError:
            if the container height is too short to accommodate the margins.
        r+   )r   rt   rh   ri   r>   s     r   effective_heightzMargins.effective_height  s"       64;IIIr   c                     t          |t                    rt          t          d|                    }t	                                          |          S )N)r]   r_   ri   rh   )
isinstancelistdictzipr   rV   )rU   config_dictr   s     r   rV   zMargins.from_config  sN     k4(( 	6DD K ww"";///r   )r   r   r   r   r]   r.   rD   r_   ri   rh   rY   r   staticmethodrt   r   r   rV   r!   r"   s   @r   r   r     s         ,,D#MMME3NNNCLLLFCOOO	+ 	+ [	+   \H H HJ J J 0 0 0 0 [0 0 0 0 0r   r   c                       e Zd ZU dZeed<   	 eed<   	  e            Zeed<   	 ej	        Z
eed<   	 ed             Zdedd fd	Zd
edededefdZdS )r   zk
    Class describing alignment, scaling and margin rules for a box
    positioned inside another box.
    x_aligny_alignmarginsinner_content_scalingc                    |                     dt          j                  }t          |t                    rt                              |          }||d<   |                     dt          j                  }t          |t                    rt                              |          }||d<   |                     dd           }|t                              |          |d<   d S d S )Nr   r   r   )	getr   rb   r   r    re   rj   r   rV   )rU   r   r   r   scalings        r   process_entriesz#SimpleBoxLayoutRule.process_entries  s     //)]-DEEgs## 	:#0099G!(I//)]-DEEgs## 	:#0099G!(I//"94@@3?3K3K4 4K/000 r   new_marginsr6   c                 F    t          | j        | j        || j                  S )N)r   r   r   r   )r   r   r   r   )r   r   s     r   substitute_marginsz&SimpleBoxLayoutRule.substitute_margins  s,    "LL"&"<	
 
 
 	
r   r   r   r   c                 D   | j         }| j        }dx}}|t          j        k    r|j        r|j        r|                    |j                  }|                    |j	                  }	|dk    r||z  nd}|dk    r|	|z  nd}|t          j
        k    rt          ||          x}}n#|t          j        k    rt          ||d          x}}t          | j        |||z  |j        |j                  }
t#          | j        |||z  |j        |j                  }t+          |
|||          S )a  
        Position and possibly scale a box within a container, according
        to this layout rule.

        :param container_box:
            :class:`.BoxConstraints` describing the container.
        :param inner_nat_width:
            The inner box's natural width.
        :param inner_nat_height:
            The inner box's natural height.
        :return:
            A :class:`.Positioning` describing the scaling & position of the
            lower left corner of the inner box.
           r   )r   r   r   r   )r   r   r   rO   r;   r@   r   r*   r   r+   rQ   minrR   r   r   r]   r_   r   r   rh   ri   r   )r   r   r   r   r   r   r   r   	eff_width
eff_heightr   r   s               r   fitzSimpleBoxLayoutRule.fit  sp   * ,,'|...+ /, /  //0CDDI 11-2FGGJ 2AA1E1E_,,1  4Dq3H3H...a  ,555$'$9$99''L666 %(!$<$<<'Lg%LM
 
 Lw&NK
 
 ugw
 
 
 	
r   N)r   r   r   r   r   rD   r   r   r   rR   r   rY   r   r   r   r.   r   r   rI   r   r   r   r     s          
   wyyGW    +7*D<DDD   [$
g 
:O 
 
 
 
=
%=
 =
 	=

 
=
 =
 =
 =
 =
 =
r   r   )#r   rW   loggingdataclassesr   	fractionsr   typingr   r   pyhanko.config.apir   pyhanko.config.errorsr   __all__	getLoggerr   ru   
ValueErrorr	   r
   r   Enumr   r   rb   ra   rc   rl   r   r.   r   r   r   r   rI   r   r   <module>r      s-   ' '   ! ! ! ! ! !       " " " " " " " " 0 0 0 0 0 0 4 4 4 4 4 4	 	 	 
	8	$	$% % % % %* % % %M M M M MK M M MH$ H$ H$ H$ H$ H$ H$ H$V, , , , ,49 , , ,^^ ^ ^ ^ ^DI ^ ^ ^F ]4]4]4  $
 
 
 
 
# 
 
 
D!  	
     !  	
      $C0 C0 C0 C0 C0 C0 C0 C0L $r
 r
 r
 r
 r
+ r
 r
 r
 r
 r
r   