o
    Zh0h'                     @   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   sp   |dkr|   |  |  |  fS |dkr$|  |  |  |  fS |dks,J ||  | 	 | j
| jf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)boxwhich_rectangle r   R/var/www/html/rh/venv/lib/python3.10/site-packages/weasyprint/layout/background.pybox_rectangle   s   r    TNc                    sz  ddl m} t  |r  D ]}t| qdu r  jd d dkr<d \}}|dkr9|d _n| _d	 d dkrXd	 \}}|dkrU|d _n| _d
 dkreg }	td}
nd fddd D }	|d}
|
j	dkrt
|	s krd _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_backgroundsr7   border_imagemask_border_imager   alphaany
backgroundzipmapr   r   )r6   r   r.   layout_childrenr7   r!   childr,   r-   imagescolorlayersr   )r   r.   r*   r6   r7   r   rA   #   sP   



	rA   c
           -   
   C   s  g }
d}| |u r|j }g }
nt| tjrEg }
d}| jD ]}|jr7|
dd |jD 7 }
t|tdd |jD }q|  |  |  |g}nt| tj	rm| jrldd | jD }
tdd | jD }|  |  |  |g}n[t| tj
tjfr|  }|rd	d |D }
td
d |D }tdd |D }||  || |  g}n&t| |}|dkr|  g}
n|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r+t|||\}}n&|dkr:t|||\}}n|\} }!t|||t| |t|!|||\}}|\}"}#}$}%|| }&|| }'t|#|&}#t|%|'}%|"dkrr|&|# }#|$dkr{|'|% }%|\}(})|(dkrt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      g | ]}|  qS r   rounded_border_boxr+   cellr   r   r   r/   o       z+layout_background_layer.<locals>.<listcomp>c                 s       | ]}|  V  qd S Nr   rQ   r   r   r   	<genexpr>q   s    
z*layout_background_layer.<locals>.<genexpr>c                 S   rN   r   rO   rQ   r   r   r   r/   x   rS   c                 s   rT   rU   rV   rQ   r   r   r   rW   z       c                 S   rN   r   rO   rQ   r   r   r   r/      s    c                 s   rT   rU   )r   rQ   r   r   r   rW      rX   c                 s   s     | ]}|  |  V  qd S rU   )r   r   rQ   r   r   r   rW      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)r[   r^   r_   r`   r\   r]   ra   rb   )
bleed_area
isinstancer   TableRowGroupBoxchildrenmaxr   r   r   TableRowBoxTableColumnGroupBoxTableColumnBox	get_cellsminr   r    rP   rounded_padding_boxrounded_content_boxget_intrinsic_sizer7   r   PageBoxmargin_widthmargin_heightr   cover_constraint_image_sizingcontain_constraint_image_sizingdefault_image_sizingr	   rh   )-r   r6   
resolutionr[   r^   clipr`   originr_   
attachmentrb   r]   total_heightrowr   cellsmin_xmax_xintrinsic_widthintrinsic_heightratiora   positioning_xpositioning_ypositioning_widthpositioning_height
painting_x
painting_ypainting_widthpainting_heightimage_widthimage_height
size_widthsize_heightorigin_x
position_xorigin_y
position_yref_xref_yrepeat_xrepeat_y	n_repeats	new_width
new_heightr   r   r   r3   ^   s   


















r3   c                    s   t | | | t| jd tjrJ | jd }|}|j dkr5|jdu r5|jD ]}|j dkr4|} nq'|jrct| d | j}t | | |d|j	d | jj
 fdd	| jjD d
| _|| _d|_dS 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)rI   r7   c                    s   g | ]}|j  d qS )r]   )_replacer4   r   r   r   r/      s    
z&layout_backgrounds.<locals>.<listcomp>)rM   )rA   rk   rm   r   	MarginBoxelement_taglowerrF   r    r7   r   rM   canvas_background)r6   r.   root_box
chosen_boxrJ   original_backgroundr   r   r   layout_backgrounds   s2   	





r   )TN)__doc__collectionsr   	itertoolsr   tinycss2.color4r   formatting_structurer    r   percentr	   r
   r   r   r    rA   r3   r   r   r   r   r   <module>   s&    

; 