
    Wwg`D              
       b   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j2                  ej2                  ej4                  ej6                  ej6                  ej4                  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"y)#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                 4    || _         t        |   |g|  y N)r   super__init__)selfr   args	__class__s      O/var/www/horilla/myenv/lib/python3.12/site-packages/pyhanko/pdf_utils/layout.pyr   zLayoutError.__init__   s    $t$    )__name__
__module____qualname____doc__strr   __classcell__r   s   @r   r	   r	      s    5%C % %r   r	   c                   0     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.r   c                 .    t         |   |xs d       y )Nz%box constraint is over/underspecified)r   )r   r   )r   r   r   s     r   r   zBoxSpecificationError.__init__%   s    SK$KLr   r   )r   r   r   r   r   r    r   r!   r"   s   @r   r
   r
   "   s    >MHSM M Mr   r
   c                   X   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y)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                 r   |t        |      nd }|t        |      nd }|| _        || _        d}d | _        |||y |%|#|t        t        ||      | _        d}|| _        y |K|| _        |$t        t        ||z              | _        || _        y |t        t        ||z              | _        || _        y )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J	$*$6S[D
!!38L"z'='++	:6DH"O !0 %#DH%!%
\(A"BC !0 &"5\)A#BC /r   c                    | j                   9| j                  -t        | j                   | j                        | _        d| _        y | j                  w| j                  /t        | j                  | j                  z        | _         d| _        y | j                   /t        | j                   | j                  z        | _        d| _        y y y )NT)r&   r'   r   r(   r)   r.   r   s    r   _recalculatezBoxConstraints._recalculateU   s    ;;"t||'?T\\:DH$(D!XX!||'!$,,"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                 V    | j                   || _         | j                          y 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                 V    | j                   || _         | j                          y 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     8888O''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     xxt##r   )NNN)r   r   r   r   r   r.   __annotations__r   boolr   floatr   r5   propertyr*   setterr;   r+   r@   r,   rC    r   r   r   r   )   sN    SMc]	(	 *.*.+/	0S%%&0 c5$&'0 x(	0<
- 
(s 
( 
( \\( ( 't ' ' 
( 
( 
( ]]( ( ( ( ( 
(h 
( 
( $d $ $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y)r   z(Class representing a scaling convention.
config_strr6   c                     	 t         j                  t         j                  t         j                  t         j                  d|j                            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   sw    	$// , 9 9"."="=!-!;!;	
  " "  	$J<  # $ 	s   AA A+N)r   r   r   r   enumautorO   rP   rQ   rR   classmethodr    rV   rI   r   r   r   r      sm    2J499;L5TYY[N
 DIIKM
 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y
)r   z;Class representing one-dimensional alignment along an axis.	align_strr6   c                     	 t         j                  t         j                  t         j                  d|j	                            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   sl    
	%//$..&00 oo	! !
  	$I; 5 6 	   A A Ac                     	 t         j                  t         j                  t         j                  d|j	                            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  sl    
	'11$..$.. oo	! !
  	$I; 5 6 	rf   c                     t         |    S r   )_alignment_oppositesr4   s    r   flippedzAxisAlignment.flipped#  s    #D))r   container_len	inner_lenc                 0   t         j                  d|||      }| t        j                  k(  r||z
  |z
  S | t        j                  k(  r|S ||kD  r$t
        j                  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+I; 7""/ 1<r+.L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		I 		I 		I S _  , S _  , * * -0	r   r   T)frozenc                   H    e Zd ZU dZeed<   	 eed<   	 eed<   	 eed<   	 d Zy)r   zP
    Class describing the position and scaling of an object in a container.
    x_posy_posx_scaley_scalec                 d    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^  s2     %LLLLJJJJ	(
 
 	
r   N)r   r   r   r   r.   rD   rF   r   rI   r   r   r   r   L  s.     JJNN
r   r   	alignmentcontainer_boxinner_nat_widthrx   ry   c                 t    |j                   r| j                  |j                  |||      S ||z   |z   |_        |S r   )r;   r|   r*   )r   r   r   rx   ry   s        r   
_aln_widthr   o  sG     ""*k
 	
 .
:[Hr   inner_nat_heightc                 t    |j                   r| j                  |j                  |||      S ||z   |z   |_        |S r   )r@   r|   r+   )r   r   r   rx   ry   s        r   _aln_heightr     sH     ##  "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                 P    ||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  sP     c!D(7C54&(A*Am_A/  
r   c                 Z    t         j                  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#       %DJJGGr   c                 Z    t         j                  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Ir   c                 l    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  s6     k4(6DK w";//r   )r   r   r   r   r]   r.   rD   r_   ri   rh   rY   r   staticmethodrt   r   r   rV   r!   r"   s   @r   r   r     sp    ,D#ME3NCLFCO	+ 	+  HJ 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y)r   zk
    Class describing alignment, scaling and margin rules for a box
    positioned inside another box.
    x_aligny_alignmarginsinner_content_scalingc                    |j                  dt        j                        }t        |t              rt        j                  |      }||d<   |j                  dt        j                        }t        |t              rt        j                  |      }||d<   |j                  dd       }|t        j                  |      |d<   y y )Nr   r   r   )	getr   rb   r   r    re   rj   r   rV   )rU   r   r   r   scalings        r   process_entriesz#SimpleBoxLayoutRule.process_entries  s     //)]-D-DEgs##009G!(I//)]-D-DEgs##009G!(I//"94@3?3K3K4K/0 r   new_marginsr6   c                 \    t        | j                  | j                  || j                        S )N)r   r   r   r   )r   r   r   r   )r   r   s     r   substitute_marginsz&SimpleBoxLayoutRule.substitute_margins  s)    "LLLL"&"<"<	
 	
r   r   r   r   c                    | j                   }| j                  }dx}}|t        j                  k7  r|j                  r|j
                  r|j                  |j                        }|j                  |j                        }	|dk7  r||z  nd}|dk7  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  sS   * ,,,,'|...++,,//0C0CDI 11-2F2FGJ 2AA1E_,1  4Dq3H..a  ,555$'$99'L666 %(!$<<'LLg%LLMM
 LLw&NNKK
 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    
   yGW  +7*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 4	 
		8	$%* %MK MH$ H$V,499 ,^^DII ^F ]44]44]44  $
# 
 
D!  	
  !  	
   $C0 C0 C0L $r
+ r
 r
r   