a
    [Xh'                     @   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_heightZpadding_box_xZpadding_box_yZpadding_widthZpadding_heightZcontent_box_xZcontent_box_ywidthheight)boxZwhich_rectangle r   a/var/www/viveiro_mudafortebrasil/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_colorNZborder_image_sourcer   noneurl)r   Zmask_border_sourceZ
visibilityhiddenZtransparentZ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>Zbackground_imageZbackground_colorc                    s$   g | ]}t  d  g|R  qS )Zimage_resolution)layout_background_layerr   Zlayer)r   pagestyler   r   r#   R   s   Zbackground_sizeZbackground_clipZbackground_repeatZbackground_originZbackground_positionZbackground_attachmentZimage_rendering)Z
draw.colorr   r
   Zall_childrenlayout_box_backgroundsr'   Zborder_imageZmask_border_imager   alphaany
backgroundzipmapr   r   )r&   r   r"   layout_childrenr'   r   childr    r!   Zimagescolorlayersr   )r   r"   r   r&   r'   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   r4   r   r   r   	<genexpr>q   s   z*layout_background_layer.<locals>.<genexpr>c                 S   s   g | ]}|  qS r   r2   r4   r   r   r   r#   x   s   c                 s   s   | ]}|  V  qd S r6   r7   r4   r   r   r   r8   z       c                 S   s   g | ]}|  qS r   r2   r4   r   r   r   r#      r9   c                 s   s   | ]}|  V  qd S r6   )r   r4   r   r   r   r8      r9   c                 s   s   | ]}|  |  V  qd S r6   )r   r   r4   r   r   r   r8      s   r   r   r   Z	font_sizeFZunused)image	unboundedpainting_areasizepositionrepeatpositioning_areaclipped_boxesfixedZcoverZcontainrightZbottomroundr   auto)r:   r=   r>   r?   r;   r<   r@   rA   )Z
bleed_area
isinstancer   ZTableRowGroupBoxchildrenmaxr   r   r   ZTableRowBoxZTableColumnGroupBoxZTableColumnBoxZ	get_cellsminr   r   r3   Zrounded_padding_boxZrounded_content_boxZget_intrinsic_sizer'   r   ZPageBoxZmargin_widthZmargin_heightr   Zcover_constraint_image_sizingZcontain_constraint_image_sizingZdefault_image_sizingr	   rD   )-r   r&   
resolutionr:   r=   Zclipr?   originr>   
attachmentrA   r<   Ztotal_heightrowr   cellsZmin_xZmax_xZintrinsic_widthZintrinsic_heightZ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_heightZorigin_xZ
position_xZorigin_yZ
position_yZref_xZref_yZrepeat_xZrepeat_yZ	n_repeatsZ	new_widthZ
new_heightr   r   r   r$   ^   s    




















r$   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)r.   r'   c                    s   g | ]}|j  d qS )r<   )_replacer%   rQ   r   r   r#      s   z&layout_backgrounds.<locals>.<listcomp>)r1   )r(   rF   rG   r   Z	MarginBoxZelement_taglowerr+   r   r'   rR   r1   Zcanvas_background)r&   r"   Zroot_boxZ
chosen_boxr/   Zoriginal_backgroundr   rQ   r   layout_backgrounds   s0    	



rT   )TN)__doc__collectionsr   	itertoolsr   Ztinycss2.color4r   Zformatting_structurer    r   percentr	   r
   r   r   r   r(   r$   rT   r   r   r   r   <module>   s$   
  
; 