a
    Qh'                     @   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rg }	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 }
nt| tjrg }
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rdd | jD }
tdd | jD }|  |  |  |g}nt| tj
tjfrL|  }|rd	d |D }
td
d |D }tdd |D }||  || |  g}nRt| |}|dkrl|  g}
n2|dkr|  g}
n|dksJ ||  g}
|d ur||| jd \}}}|d u sd||fv rt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rt|||\}}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rt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rt| d | j}t | | |d|j	d | jj
 fdd	| jjD d
| _|| _d|_nd| _dS )zLayout 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$   
  
; 