a
    ¥Q•h'  ã                   @   sŠ   d 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
 dd	lmZmZ ed
dƒZeddƒZdd„ Zddd„Zdd„ Zdd„ ZdS )z$Manage background position and size.é    )Ú
namedtuple)Úcycle)Úparse_coloré   )Úboxesé   )Úreplaced)Ú
percentageÚresolve_radii_percentagesÚ
Backgroundzcolor, layers, image_renderingÚBackgroundLayerzXimage, size, position, repeat, unbounded, painting_area, positioning_area, clipped_boxesc                 C   st   |dkr$|   ¡ |  ¡ |  ¡ |  ¡ fS |dkrH|  ¡ |  ¡ |  ¡ |  ¡ fS |dksXJ |ƒ‚|  ¡ |  	¡ | j
| jfS d S )Nú
border-boxúpadding-boxúcontent-box)Úborder_box_xÚborder_box_yÚborder_widthÚborder_heightÚpadding_box_xÚpadding_box_yÚpadding_widthÚpadding_heightÚcontent_box_xÚcontent_box_yÚwidthÚheight)ÚboxZwhich_rectangle© r   úU/var/www/sistema_ama/venv/lib/python3.9/site-packages/weasyprint/layout/background.pyÚbox_rectangle   s    þþþr   TNc                    s€  ddl m} tˆ ƒ |r2ˆ  ¡ D ]}tˆ|ˆƒ q ˆdu r@ˆ j‰ˆd d dkrxˆd \}}|dkrrˆ|d�ˆ _n|ˆ _ˆd	 d dkr°ˆd	 \}}|dkrªˆ|d�ˆ _n|ˆ _ˆd
 dkrÊg }	tdƒ}
n*ˆd ‰‡‡fdd„ˆd D ƒ}	|ˆdƒ}
|
j	dk�rt
|	ƒ�sˆ ˆk�rdˆ _dS ‡ ‡‡fdd„t|	gttˆd ˆd ˆd ˆd ˆd ˆd gƒ¢R Ž D ƒ}t|
|ˆd ƒˆ _dS )z%Fetch and position background images.r   )Ú	get_colorNÚborder_image_sourcer   ÚnoneÚurl)r#   Úmask_border_sourceÚ
visibilityÚhiddenÚtransparentÚimage_orientationc                    s(   g | ] \}}|d kr ˆ |ˆd�n|‘qS )r#   )r#   Úorientationr   )Ú.0Útype_Úvalue)Úget_image_from_urir)   r   r   Ú
<listcomp>G   s   ÿÿz*layout_box_backgrounds.<locals>.<listcomp>Úbackground_imageÚbackground_colorc                    s$   g | ]}t ˆ ˆˆd  g|¢R Ž ‘qS )Úimage_resolution)Úlayout_background_layer©r*   Úlayer)r   ÚpageÚstyler   r   r.   R   s   ÿÚbackground_sizeÚbackground_clipÚbackground_repeatÚbackground_originÚbackground_positionÚbackground_attachmentÚimage_rendering)Ú
draw.colorr    r
   Úall_childrenÚlayout_box_backgroundsr6   Zborder_imageZmask_border_imager   ÚalphaÚanyÚ
backgroundÚzipÚmapr   r   )r5   r   r-   Úlayout_childrenr6   r    Úchildr+   r,   ÚimagesÚcolorÚlayersr   )r   r-   r)   r5   r6   r   r@   #   sP    
ý


ú
þ	r@   c
           -   
   C   sÒ  g }
d}| |u r|j }g }
�n€t| tjƒrŽg }
d}| jD ]8}|jr8|
dd„ |jD ƒ7 }
t|tdd„ |jD ƒƒƒ}q8|  ¡ |  ¡ |  ¡ |g}�nt| tj	ƒrÞ| jrÜdd„ | jD ƒ}
tdd„ | jD ƒƒ}|  ¡ |  ¡ |  ¡ |g}nÀt| tj
tjfƒ�rL|  ¡ }|�ržd	d„ |D ƒ}
td
d„ |D ƒƒ}tdd„ |D ƒƒ}||  ¡ || |  ¡ g}nRt| |ƒ}|dk�rl|  ¡ g}
n2|dk�r‚|  ¡ g}
n|dk�s”J |ƒ‚|  ¡ g}
|d u�rÀ| || jd ¡\}}}|d u �sØd||fv �rðtd d|dddd|
d�S |	dk�r*t| tjƒ�rdd|  ¡ |  ¡ f}n
t|dƒ}n
t| |ƒ}|\}}}}|\}}}}|dk�rjt |||¡\}}nL|dk�rˆt |||¡\}}n.|\} }!t |||t| |ƒt|!|ƒ||¡\}}|\}"}#}$}%|| }&|| }'t|#|&ƒ}#t|%|'ƒ}%|"dk�rø|&|# }#|$dk�r
|'|% }%|\}(})|(dk�rbtdt|| ƒƒ}*||* }+d}#|)dk�r^|d dk�r^||+| 9 }|+}|)dk�r²tdt|| ƒƒ}*||* },d}%|(dk�r®|d dk�r®||,| 9 }|,}t|||f|#|%f|d|||
d�S )N)r   r   r   r   r   c                 S   s   g | ]}|  ¡ ‘qS r   ©Úrounded_border_box©r*   Úcellr   r   r   r.   o   s   z+layout_background_layer.<locals>.<listcomp>c                 s   s   | ]}|  ¡ V  qd S ©N©r   rM   r   r   r   Ú	<genexpr>q   s   z*layout_background_layer.<locals>.<genexpr>c                 S   s   g | ]}|  ¡ ‘qS r   rK   rM   r   r   r   r.   x   s   c                 s   s   | ]}|  ¡ V  qd S rO   rP   rM   r   r   r   rQ   z   ó    c                 S   s   g | ]}|  ¡ ‘qS r   rK   rM   r   r   r   r.   �   rR   c                 s   s   | ]}|  ¡ V  qd S rO   )r   rM   r   r   r   rQ   ‚   rR   c                 s   s   | ]}|  ¡ | ¡  V  qd S rO   )r   r   rM   r   r   r   rQ   ƒ   s   r   r   r   Ú	font_sizeFÚunused)ÚimageÚ	unboundedÚpainting_areaÚsizeÚpositionÚrepeatÚpositioning_areaÚclipped_boxesÚfixedÚcoverÚcontainÚrightÚbottomÚroundr   Úauto)rU   rX   rY   rZ   rV   rW   r[   r\   )Ú
bleed_areaÚ
isinstancer   ÚTableRowGroupBoxÚchildrenÚmaxr   r   r   ÚTableRowBoxÚTableColumnGroupBoxÚTableColumnBoxÚ	get_cellsÚminr   r   rL   Úrounded_padding_boxÚrounded_content_boxÚget_intrinsic_sizer6   r   ÚPageBoxÚmargin_widthÚmargin_heightr   Úcover_constraint_image_sizingÚcontain_constraint_image_sizingÚdefault_image_sizingr	   rb   )-r   r5   Ú
resolutionrU   rX   ÚcliprZ   ÚoriginrY   Ú
attachmentr\   rW   Ztotal_heightÚrowr   ÚcellsZmin_xZmax_xÚintrinsic_widthÚintrinsic_heightÚratior[   Zpositioning_xZpositioning_yZpositioning_widthZpositioning_heightZ
painting_xZ
painting_yZpainting_widthZpainting_heightZimage_widthZimage_heightZ
size_widthZsize_heightÚorigin_xÚ
position_xÚorigin_yÚ
position_yÚref_xÚref_yZrepeat_xZrepeat_yZ	n_repeatsÚ	new_widthÚ
new_heightr   r   r   r2   ^   sà    
ÿÿþÿþÿÿ





ÿ
ý

ÿ

ÿ

ÿ
ü





ør2   c                    sÎ   t | | |ƒ t| jd tjƒr"J ‚| jd }|}|j ¡ dkrj|jdu rj|jD ]}|j ¡ dkrN|} qjqN|jrÄt| dƒ‰ | j}t | | |d|j	d� | jj
‡ fdd	„| jjD ƒd
�| _|| _d|_nd| _dS )zøLayout backgrounds on the page box and on its children.

    This function takes care of the canvas background, taken from the root
    elememt or a <body> child of the root element.

    See https://www.w3.org/TR/CSS21/colors.html#background

    r   ÚhtmlNÚbodyr   F)rF   r6   c                    s   g | ]}|j ˆ d �‘qS )©rW   )Ú_replacer3   rŠ   r   r   r.   ù   s   ÿz&layout_backgrounds.<locals>.<listcomp>)rJ   )r@   re   rg   r   Ú	MarginBoxÚelement_tagÚlowerrC   r   r6   r‹   rJ   Zcanvas_background)r5   r-   Úroot_boxÚ
chosen_boxrG   Zoriginal_backgroundr   rŠ   r   Úlayout_backgroundsÞ   s0    	


þ
þþr‘   )TN)Ú__doc__Úcollectionsr   Ú	itertoolsr   Útinycss2.color4r   Úformatting_structurer   Ú r   Úpercentr	   r
   r   r   r   r@   r2   r‘   r   r   r   r   Ú<module>   s$   
þ  ÿ
; 