a
    [Xh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``.Zmask_border_sourcer   noneNefZmask_border_modeZmask_border_sliceZmask_border_repeatZmask_border_outsetZmask_border_width)styleZmask_border_imagerounded_border_boxr   ZctmZset_alpha_statedraw_border_image)streamboxxywhtltrbrblmatrixZmask_stream r&   [/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/weasyprint/draw/border.pyset_mask_border   s    
r(   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visibleNZcolumn_widthautoZcolumn_countZcolumn_rule_widthr   TZcolumn_spanallFZcolumn_rule_styleZ
column_gapnormal	font_sizer   r   Zcolumn_rule_colorZborder_image_sourcer   Zborder_image_sliceZborder_image_repeatZborder_image_outsetZborder_image_widthc                    s   g | ]}t  d | dqS )border__width)getattr.0sider   r&   r'   
<listcomp>K       zdraw_border.<locals>.<listcomp>c                    s    g | ]}t  jd | dqS )r/   Z_color)r   r   r2   r5   r&   r'   r6   Q   r7   c                    s,   g | ]$\}}| j o& jd | d qS )r/   _style)alphar   )r3   ir4   r   colorsr&   r'   r6   R   s   Zsoliddoubler   )r      r   r      )r   
isinstancer   ZBlockContainerBoxchildrenr   r
   width
position_xZ
position_yheightclip_border_segmentr   r   draw_rect_borderZborder_imager   SIDESset	enumeratelendraw_rounded_bordertuplezipr   )r   r   columnsborder_widthsZ	skip_nextchildZ
rule_widthZ
rule_styleZgaprC   
border_boxcolorwidthsZstylesZsimple_styleZsingle_colorZ
four_sidesvaluesindexr4   rB   r   r&   r;   r'   draw_border   s    



2 
rV   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.Zimage_resolutionr.   N)Zspecified_widthZspecified_heightZdefault_widthZdefault_heightr?   c                 S   s>   t | ttfrt| |S | jdks&J td| jd | S d S )N%d   )r@   intfloatminunitvalue)	dimension	intrinsicr&   r&   r'   compute_slice_dimension{   s    
z2draw_border_image.<locals>.compute_slice_dimensionr   r   r   r>   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&   r'   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 )Nr+   rW   rX   ra   )r@   rY   rZ   r\   r]   )r^   originalr_   Zarea_dimensionr&   r&   r'   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dZimage_rendering)r   r   maxrh   r   	rectangleclipendZ	transformrangeZdrawr   )r   r   rB   rD   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_heightZoffset_xZoffset_yr:   jZtranslate_xZtranslate_yr   imageZintrinsic_heightZintrinsic_widthr   r&   r'   draw_border_image_region   sz    


Lz3draw_border_image.<locals>.draw_border_image_region)rp   )rq   )rp   rq   rr   rs   )re   re   NN)Zget_intrinsic_sizer   r	   Zdefault_image_sizingborder_widthborder_heightr   rounded_padding_box)4r   r   rv   Zborder_sliceZborder_repeatZborder_outsetrx   rB   rD   ZratioZimage_sliceZshould_fillr`   Z	slice_topZslice_rightZslice_bottomZ
slice_leftrp   rq   r   r   r   r    r!   r"   r#   r$   ra   pypwphZptlptrZpbrZpblZborder_leftZ
border_topZborder_rightZborder_bottomrb   Z
outset_topZoutset_rightZoutset_bottomZoutset_leftrd   rw   Z
scale_leftZ	scale_topZscale_rightZscale_bottomZx_middleZy_middler&   ru   r'   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   r?   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&   r'   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   r>   r   
   r?   )r   r   )ri   br   r&   r&   r'   corner_half_lengthZ  s    
&z/clip_border_segment.<locals>.corner_half_lengthr   r   r   r   r   r>   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   r?   r   r   )ro   r   r[   rk   move_toline_tor   )dasheslinewayr   r   ra   r{   Zchlr:   Zangle1Zangle2offsetZangleZdashZmain_offsetr4   r   r&   r'   	draw_dots  sb    

z&clip_border_segment.<locals>.draw_dotsr   gư>TZeven_oddN)r   r   rh   r   r   ro   rk   r[   rl   rm   rn   )3r   r   rB   r4   rQ   rO   radiibbxbbybbwbbhZtlhZtlvZtrhZtrvZbrhZbrvZblhZblvbtr#   bbr$   r   r   Zpx1Zpy1Zrounded1Zpx2Zpy2Zrounded2r   Za1b1Za2b2Zline_lengthlengthZchl1Zchl2Zdash_lengthZdashes1Zdashes2r   r   r   r:   r   r   r   r   r&   r   r'   rE   =  s     




 
 "


#

"

"



rE   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ZridgeZgroover   r   Tr   r   r=   gUUUUUU?gUUUUUU?)	set_colorrounded_boxrz   Zrounded_box_ratiofillr   )r   r   r   rR   r&   r&   r'   rK     s"    
rK   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   r=   r>   )r   rl   r   )r   r   rS   r   rR   r   r   r   r   r   r#   r   r$   r&   r&   r'   rF   	  s>    
(


$(rF   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   r=   r>   r   r   r?   r   Zwavyg      ?r   )r   r   Zset_dashZset_line_widthr   r   r   rl   rm   rn   curve_to)r   r   r   r   r   Z	thicknessr   rR   r   ZupZradiusr   r&   r&   r'   	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 )	NZoutline_widthZoutline_offsetZoutline_colorZoutline_styler)   r*   r   r?   )r   r   r9   Zborder_box_xZborder_box_yrx   ry   rG   r   rE   rF   r   rA   r@   r   ZBoxdraw_outline)	r   r   rB   r   rR   r   Zoutline_boxr4   rP   r&   r&   r'   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&   )r3   Zcornerr&   r&   r'   	<genexpr>  r7   zrounded_box.<locals>.<genexpr>Ng?r   r   )r,   rl   r   r   r   )r   r   r   r   r   r    r!   r"   r#   r$   rr&   r&   r'   r     s.    8>8,r   )NN)r   )__doc__mathr   r   r   r   r   Zformatting_structurer   Zlayoutr	   Zlayout.percentr
   r%   r   rR   r   r   stackr   rG   r(   rV   r   rE   rK   rF   r   r   r   r&   r&   r&   r'   <module>   s(   Q Q 
 9
C