a
    Qhm                     @   s   d Z ddlmZmZmZmZ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 dd
lmZ dZdd Zdd Zdd ZdddZdd Zdd Zd ddZdd Zdd ZdS )!zDraw borders.    )ceilfloorpisqrttan   )boxes)replaced)
percentage)Matrix   )	get_colorstyled_color)stacked)toprightbottomleftc              	   C   s   |j d d dks|jdu r dS | \}}}}}}}}	t||d}
|
| j }
| |||||j d }t|||j|j d |j d |j d	 |j d
  dS )z5Set ``box`` mask border as alpha state on ``stream``.mask_border_sourcer   noneNefmask_border_modemask_border_slicemask_border_repeatmask_border_outsetmask_border_width)stylemask_border_imagerounded_border_boxr   ctmset_alpha_statedraw_border_image)streamboxxywhtltrbrblmatrixZmask_stream r/   O/var/www/sistema_ama/venv/lib/python3.9/site-packages/weasyprint/draw/border.pyset_mask_border   s    
r1   c                    s   j d dkrdS t tjo8 j d dkp8 j d dk}|rV j d rVddd j d f}d	} jD ]}|j d
 dkrd	}qhn
|rd}qht|   j d } j d } j d dkrĈ j d }nt j d  j}|j j d | d  }	|	|j	||j
f}
t| ||d|
| t|t j dd}t| |
||| W d   qh1 sJ0    Y  qh j d d dkr jdurt |  j j d  j d  j d  j d  dS  fddtD }t|dhkrdS  fddtD  fddttD }t|dhdhfv }ttd k}d|v}|rP|rP|rPt|  |d d  dS ttt||}d!D ]}|| \}}}}|dksf|sqft| R t| |||  dd" |  d"d  t|  |t||| W d   n1 s0    Y  qfdS )#zADraw the box borders and column rules to a ``pdf.stream.Stream``.
visibilityvisibleNcolumn_widthautocolumn_countcolumn_rule_widthr   Tcolumn_spanallFcolumn_rule_style
column_gapnormal	font_sizer   r   column_rule_colorborder_image_sourcer   border_image_sliceborder_image_repeatborder_image_outsetborder_image_widthc                    s   g | ]}t  d | dqS )border__width)getattr.0sider%   r/   r0   
<listcomp>K       zdraw_border.<locals>.<listcomp>c                    s    g | ]}t  jd | dqS )rD   _color)r   r   rG   rJ   r/   r0   rK   Q   rL   c                    s,   g | ]$\}}| j o& jd | d qS )rD   _style)alphar   )rH   irI   r%   colorsr/   r0   rK   R   s   soliddoubler   )r      r   r      )r   
isinstancer   BlockContainerBoxchildrenr   r
   width
position_x
position_yheightclip_border_segmentr   r   draw_rect_borderborder_imager#   SIDESset	enumeratelendraw_rounded_bordertuplezipr    )r$   r%   columnsborder_widthsZ	skip_nextchildZ
rule_widthZ
rule_stylegapr[   
border_boxcolorwidthsstylesZsimple_styleZsingle_colorZ
four_sidesvaluesindexrI   rZ   r   r/   rQ   r0   draw_border   s    



2 
rr   c           4         s    jd  jd \}}}	tj|||	dd    d\|dd }
|d }dd }||
d }||
d	 }||
d
 }||
d }|\}}  \}}}}}}}}  \}}}}}} }!}"|| }#|| }$|| |# }%|| |$ }&dd }'|'|d |$}(|'|d	 |%})|'|d
 |&}*|'|d |#}+||+8 }||(8 }||+|) 7 }||(|* 7 }dd },|,|d |$||}$|,|d	 |%||}%|,|d
 |&||}&|,|d |#||}#d fdd	}-|-|||#|$dd||\}.}/|-|| |% ||%|$| d|| |-|| |% || |& |%|&| | ||\}0}1|-||| |& |#|&d| || || k  }2r|-||# |||# |% |$|d| | ||d	 |-||# || |& ||# |% |&|| | | ||d	 || k  }3rH|-|| |% ||$ |%||$ |& | ||| | |d	 |-|||$ |#||$ |& d||| | |d	 |r|2r|3r|-||# ||$ ||# |% ||$ |& ||| | | | |||.p|0|/p|1d dS )zHDraw ``image`` as a border image for ``box`` on ``stream`` as specified.image_resolutionr=   N)specified_widthspecified_heightdefault_widthdefault_heightrV   c                 S   s>   t | ttfrt| |S | jdks&J td| jd | S d S )N%d   )rW   intfloatminunitvalue)	dimension	intrinsicr/   r/   r0   compute_slice_dimension{   s    
z2draw_border_image.<locals>.compute_slice_dimensionr   r   r   rU   c                 S   s,   | j d u r| j| S | j dks"J | jS d S )Npx)r}   r~   )r   Zfrom_borderr/   r/   r0   compute_outset_dimension   s    

z3draw_border_image.<locals>.compute_outset_dimensionc                 S   sR   | dkr|S t | ttfr"| | S | jdkr:| jd | S | jdksHJ | jS d S )Nr5   rx   ry   r   )rW   rz   r{   r}   r~   )r   originalr   Zarea_dimensionr/   r/   r0   compute_width_adjustment   s    
z3draw_border_image.<locals>.compute_width_adjustmentstretchc                    s  d||fv rd}
nd}|
s0|r,|r,|| nd}
|dkrJt || |
 }nj|dkr|t|| |
 }||
 ||  |d  }n8|dkrtdt|| |
 }|||  }
nd}|| }
d||fv rd}nd}|s|r|r|| nd}|	dkrt || | }nn|	dkr2t|| | }|| ||  |d  }n:|	dkr`tdt|| | }|||  }nd}|| }d|
|fv r|
|fS |
 }| }||  }||  }t
 | |||     j| | | || | d j|
|d t	|D ]}t	|D ]}tt |||  }|||  }j||d ||
 || ||     
 jd  W d    n1 s0    Y  qq
W d    n1 s0    Y  |
|fS )	Nr   r   repeatspaceroundr   )adimage_rendering)r   r   maxr   r   	rectangleclipend	transformrangedrawr   )r&   r'   rZ   r]   Zslice_xZslice_yZslice_widthZslice_heightrepeat_xrepeat_yscale_xscale_yZextra_dxZn_repeats_xZextra_dyZn_repeats_yZrendered_widthZrendered_heightoffset_xoffset_yrP   jtranslate_xtranslate_yr%   imageintrinsic_heightintrinsic_widthr$   r/   r0   draw_border_image_region   sz    


Lz3draw_border_image.<locals>.draw_border_image_region)r   )r   )r   r   r   r   )r   r   NN)get_intrinsic_sizer   r	   default_image_sizingborder_widthborder_heightr    rounded_padding_box)4r%   r$   r   Zborder_sliceZborder_repeatZborder_outsetr   rZ   r]   ratioZimage_sliceZshould_fillr   Z	slice_topZslice_rightZslice_bottomZ
slice_leftr   r   r&   r'   r(   r)   r*   r+   r,   r-   r   pypwphZptlptrZpbrZpblZborder_leftZ
border_topZborder_rightZborder_bottomr   Z
outset_topZoutset_rightZoutset_bottomZoutset_leftr   r   Z
scale_leftZ	scale_topZscale_rightZscale_bottomZx_middleZy_middler/   r   r0   r#   n   s    


  M


r#   Nc           3   	      s  |\}}}	}
|pd\\}}\}}\}}\}}|p8d|f \}}}}dd }dd }dkr|||||\\}}}|| || |\\}}}|}d}d |nd	kr|| || |\\}}}|| | | | \\}}}|}d}d
 ||	 ndkrJ||| || \\}}}|| | | | \\}}}|}d}d ||
 nJdkr|||||\\}}}||| || \\}}}|}d}d |dv r6||d
  || |d
   } }!| |d
  || |d
   }"}#|	| | }$|	}% ||	  | || |  ||	 | |  ndv r| | |d
  ||d
   } }!| | |d
  | |d
   }"}#|
| | }$|
}% ||
  | | ||  | ||
 |  |dv r|dkr|nd| |s|r|| |!}&||"|#}'|$|& |' }%t|% }(|rP|rP|%|(|(d
   n|%|(|(d
  d  t|&d
   })t|'d
   }*t|$ }+ fdd},|,|)|+||||||&\}+}-|,|*|+| ||	 ||
 |||'d }+|$dkrtd|+d
D ]}.|.|-7 }.dv rrt|| |.  || }/t|| |.d   ||	 | }0|dk rb|nd }1|1| }2nddv rt|| |.  || }1t|| |.d   ||
 | }2|dkr|nd }/|/| }0|/|1|0|/ |2|1  q nj	dd 
  |%t|% t|% d d
   p0dtdt|% d
D ]}.dkrn||.  || nzd	kr||	 | ||.  | nNdkrƈ||.  ||
 | | n"dkrF|||.  | qFj	dd 
  dS )zClip one segment of box border.

    The strategy is to remove the zones not needed because of the style or the
    side before painting.

    )r   r   r   r   r   rV   c                 S   s8   t | t |kr,t |t |kr,| |fdfS ||fdfS )a  Get the point use for border transition.

        The extra boolean returned is ``True`` if the point is in the padding
        box (ie. the padding box is rounded).

        This point is not specified. We must be sure to be inside the rounded
        padding box, and in the zone defined in the "transition zone" allowed
        by the specification. We chose the corner of the transition zone. It's
        easy to get and gives quite good results, but it seems to be different
        from what other browsers do.

        TF)abs)x1y1x2y2r/   r/   r0   transition_pointI  s    ,
z-clip_border_segment.<locals>.transition_pointc              	   S   sH   | | | |  }t d | |  dd|d  dtdd|d       S )a7  Return the length of the half of one ellipsis corner.

        Inspired by [Ramanujan, S., "Modular Equations and Approximations to
        pi" Quart. J. Pure. Appl. Math., vol. 45 (1913-1914), pp. 350-372],
        wonderfully explained by Dr Rob.

        https://mathforum.org/dr.math/faq/formulas/

           r   rU   r   
   rV   )r   r   )r   br&   r/   r/   r0   corner_half_lengthZ  s    
&z/clip_border_segment.<locals>.corner_half_lengthr   r   r   r   r   rU   r   r   r   r   r   )dotteddashedr         ?c                    s  | s|d dfS t d| dD ]}|d7 }d  | ||  |  d t }	|dkrZtntd  | |d |  |  d t  t d }
dv r|| |  || || d t|
   || || d t|	   n^dv rT| ||  || || t|
   || || t|	   |
 t d kr|	|
 d  | |d |  |  d t |	  }|d7 } qqd t d |
 |
|	   }||fS )Nr   r   r   r   rV   r   r   )r   r   r|   r   move_toline_tor   )Zdasheslinewayr&   r'   r   r   ZchlrP   angle1angle2offsetangledashZmain_offsetrI   r$   r/   r0   	draw_dots  sb    

z&clip_border_segment.<locals>.draw_dotsr   gư>Teven_oddN)r   r   r   r   r   r   r   r|   r   r   r   )3r$   r   rZ   rI   rl   ri   radiibbxbbybbwbbhZtlhZtlvZtrhZtrvZbrhZbrvZblhZblvbtr,   bbr-   r   r   Zpx1Zpy1Zrounded1Zpx2Zpy2Zrounded2r   a1b1a2b2Zline_lengthlengthZchl1Zchl2Zdash_lengthZdashes1Zdashes2r   r   r   rP   r   r   r   r   r/   r   r0   r^   =  s     




 
 "


#

"

"



r^   c                 C   s   |dv r||  |d  t| |  t| |d | jdd |  |d  t| |d t| |  | jdd d S |  | t| |  |dkrt| |d t| |d	 t| |  | jdd d S )
Nridgegroover   r   Tr   r   rT   gUUUUUU?gUUUUUU?)	set_colorrounded_boxr   rounded_box_ratiofillr    )r$   r%   r   rm   r/   r/   r0   re     s"    
re   c                 C   s  |\}}}}|\}	}
}}|dv r|  |d  | j|  | ||d  ||	d  |||
 d  ||	| d   | jdd | ||d  ||	d  |||
 d  ||	| d   | || ||	 || |
 ||	 |  |  |d  | jdd d S |  | | j|  |dkr| ||d  ||	d  |||
 d  ||	| d   | ||d d  ||	d d  |||
 d d  ||	| d d   | || ||	 || |
 ||	 |  | jdd d S )	Nr   r   r   Tr   r   rT   rU   )r   r   r   )r$   r%   rn   r   rm   r   r   r   r   r   r,   r   r-   r/   r/   r0   r_   	  s>    
(


$(r_   c	              
   C   sn  ||ks||ksJ t | : |dvr6| j|dd |dkrR| d| g| n|dkrh| |g| |dkr8| |d  ||kr| ||d  | | ||d  | | ||d  | | ||d  | nZ||krB| |||d   | |||d   | |||d   | |||d   n
|dv rJ| |d	  | j|d
 dd ||kr| ||d  | | ||d  | n2||kr| |||d   | |||d   |   | j|d dd ||kr| ||d  | | ||d  | n2||krB| |||d   | |||d   n|dkr ||ksbJ d}	d| }
| ||d	|
  || d|
  |   | 	  || }| || ||k rB| | | 
||
d	  ||	|
  |d|
 d	  ||	|
  |d	|
  | |d	|
 7 }|	d9 }	qn"| | | || | || |   W d    n1 s`0    Y  d S )Nr   T)stroker      r   rT   rU   r   r   rV   r   wavyg      ?r   )r   r   set_dashset_line_widthr   r   r   r   r   r   curve_to)r$   r   r   r   r   	thicknessr   rm   r   Zupradiusr&   r/   r/   r0   	draw_line'  sv    







 


r   c           	      C   s  |j d }|j d }t|j d}|j d }|j d dkr|r|jr| | | | | | | d|  d|  | d|  d|  f}tD ]Z}t| > t	| |||| t
| |d|f |t||| W d    q1 s0    Y  q|jD ]}t|tjrt| | qd S )	Noutline_widthoutline_offsetoutline_coloroutline_styler2   r3   r   rV   )r   r   rO   border_box_xborder_box_yr   r   ra   r   r^   r_   r   rY   rW   r   Boxdraw_outline)	r$   r%   rZ   r   rm   r   Zoutline_boxrI   rj   r/   r/   r0   r   j  s(    




$
r   c              
   C   s  |\}}}}}}}}	t dd ||||	fD rB| |||| dS d}
| ||d  | | || |d  | | || |d |
  ||| ||d |
  || ||d   | || || |d   | || || |d |
  || |d |
  || || |d  ||  | ||	d  ||  | ||	d |
  || ||| |	d |
  ||| |	d   | |||d   | |||d |
  ||d |
  |||d  | dS )zDraw the path of the border radius box.

    ``widths`` is a tuple of the inner widths (top, right, bottom, left) from
    the border box. Radii are adjusted from these values. Default is (0, 0, 0,
    0).

    c                 s   s   | ]}d |v V  qdS )r   Nr/   )rH   cornerr/   r/   r0   	<genexpr>  rL   zrounded_box.<locals>.<genexpr>Ng?r   r   )r9   r   r   r   r   )r$   r   r&   r'   r(   r)   r*   r+   r,   r-   rr/   r/   r0   r     s.    8>8,r   )NN)r   )__doc__mathr   r   r   r   r   formatting_structurer   layoutr	   Zlayout.percentr
   r.   r   rm   r   r   stackr   ra   r1   rr   r#   r^   re   r_   r   r   r   r/   r/   r/   r0   <module>   s(   Q Q 
 9
C