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m	Z	 ddl
mZ dd	lmZmZ dd
lmZ ddlmZ ddlmZmZ dd Zdd ZdddZG dd deZG dd dZdS )aa  Transform a "before layout" box tree into an "after layout" tree.

Break boxes across lines and pages; determine the size and dimension of each
box fragement.

Boxes in the new tree have *used values* in their ``position_x``,
``position_y``, ``width`` and ``height`` attributes, amongst others.

See https://www.w3.org/TR/CSS21/cascade.html#used-value

    defaultdict)partial)inf   )boxesbuild)PROGRESS_LOGGER   )absolute_box_layoutabsolute_layout)layout_backgrounds)block_level_layout)make_all_pagesmake_margin_boxesc                 C   s   g | _ |jd }|dkrd}n&|dkrd}n|dkr"|jd dk}n|d	kr.|jd d
k}n|jd dk}d}d| d d}dgddgidhgf}ddg g d}| j |||||f dS )zoInitialize ``context.page_maker``.

    Collect the pagination's states required for page based counters.

    break_beforerightTleftFrecto	directionltrversortlNanyr   )breakpagepages)content_changedpages_wantedanchorscontent_lookups)
page_makerstylepage_valuesappend)contextroot_box
page_break
right_page	resume_at	next_page
page_stateremake_state r-   P/var/www/html/rh/venv/lib/python3.10/site-packages/weasyprint/layout/__init__.pyinitialize_page_maker   s2   
	

r/   c           	      c   s    |D ]<}|j D ]6}t|tjr| }g }t| |||t dd\}}|V  |r>g }|D ]}t| |||t dd q,|}|s(qqdS )zBLay out and yield fixed boxes of ``pages`` on ``containing_page``.N)bottom_space
skip_stack)fixed_boxes
isinstancer   ReplacedBoxcopyr   r   r   )	r%   r   containing_pager   boxabsolute_boxesabsolute_box_new_absolute_boxesr-   r-   r.   layout_fixed_boxesM   s.   

r<      c           "      c   s   t || g }g }d}t|D ]W}|dkr!td| | |_|}|dkr,|j }tt||| |}t|}d}	d}
|j	D ] }|\}}}}}|d }|g|d< |d rXd}	|d r`||k}
q@|	sg|
sg nqg }g }g }t
|D ]\}}|j	|d  \}}}}}|d }| D ]t}|jr|jd	r|}n|jd
r|}n|}|j|v rd|_n||j |jr|jj D ]$\\}}}|j|kr|dkr|dkr|jsq|| |dkr|j|_q|j}|r|dkr|D ]}|\}}|j| |d  | qqqrt
|D ]d\}}g }|j\}} |t||d| | ||j |t|||d d | ||_|d |_|j	|j d }!|f|_| jrQ| j| f7  _| jtt|||!7  _t||j |V  qdS )a  Lay out the whole document.

    This includes line breaks, page breaks, absolute size and position for all
    boxes. Page based counters might require multiple passes.

    :param root_box:
        Root of the box tree (formatting structure of the HTML). The page boxes
        are created from that tree, this structure is not lost during
        pagination.
    :returns:
        A list of laid out Page objects.

    r   z+Step 5 - Creating layout - Repagination #%dFr
   r   r   Tr   z::beforez::after contentzbookmark-labelnoneN   )r/   ranger	   infor5   	footnoteslistr   lenr!   	enumeratedescendantsbookmark_labelelement_tagendswithelementr$   missing_linktarget_collectorcounter_lookup_itemsitemsparse_again
string_setchildrenextendr<   current_pagetupler   r   get_image_from_uri)"htmlr&   r%   	max_loopsr   original_footnotesactual_total_pagesloopinitial_total_pagesreloop_contentreloop_pages	page_datar:   r+   r,   page_counter_valueswatch_elementswatch_elements_beforewatch_elements_afterir   child	checklistr7   	css_tokenitemstring_setsrR   string_nametextroot_childrenrootfootnote_areastater-   r-   r.   layout_documentf   s   







#


rq   c                   @   s   e Zd ZdZdd ZdS )FakeListu)   List in which you can’t append objects.c                 C   s   d S Nr-   )selfri   r-   r-   r.   r$      s   zFakeList.appendN)__name__
__module____qualname____doc__r$   r-   r-   r-   r.   rr      s    rr   c                   @   s   e Zd Zdd Zdd Zedd Zdd Zd	d
 Zdd Z	dd Z
dddZdddZdd Zdd Zdd Zdd Zdd ZdS ) LayoutContextc                 C   s   || _ t|| d| _|| _|| _|| _g | _g | _i | _g | _	g | _
d | _d | _d | _tdd | _tdd | _d | _d| _i | _d| _i | _i | _i | _i | _d S )N)r%   c                   S      t dd S )Nc                   S      g S rs   r-   r-   r-   r-   r.   <lambda>       :LayoutContext.__init__.<locals>.<lambda>.<locals>.<lambda>r   r-   r-   r-   r.   r|          z(LayoutContext.__init__.<locals>.<lambda>c                   S   rz   )Nc                   S   r{   rs   r-   r-   r-   r-   r.   r|      r}   r~   r   r-   r-   r-   r.   r|      r   F)	style_forr   rW   font_configcounter_stylerN   _excluded_shapes_listsrD   page_footnotescurrent_page_footnotesreported_footnotescurrent_footnote_areaexcluded_shapespage_bottomr   rR   running_elementsrU   forced_breakbroken_out_of_flow	in_columnstrut_layoutsfont_featurestablesdictionaries)rt   r   rW   r   r   rN   r-   r-   r.   __init__   s.   
zLayoutContext.__init__c                 C   s   |  | j| |S rs   )	overflowsr   )rt   r0   
position_yr-   r-   r.   overflows_page   s   zLayoutContext.overflows_pagec                 C   s   || d kS )Ng0D   ?r-   )bottomr   r-   r-   r.   r      s   zLayoutContext.overflowsc                 C   s   g | _ | j| j  d S rs   )r   r   r$   rt   r-   r-   r.   create_block_formatting_context  s   z-LayoutContext.create_block_formatting_contextc                 C   sx   |j d dkr'| jr'| |j }tdd | jD |g }| j|| 7  _| j  | jr7| jd | _d S d | _d S )Nheightautoc                 S   s   g | ]	}|j |  qS r-   )r   margin_height).0shaper-   r-   r.   
<listcomp>  s    zALayoutContext.finish_block_formatting_context.<locals>.<listcomp>)r"   r   content_box_yr   maxr   pop)rt   r&   
box_bottommax_shape_bottomr-   r-   r.   finish_block_formatting_context
  s   

z-LayoutContext.finish_block_formatting_contextc                 C   s   t  | _| j| j d S rs   )rr   r   r   r$   r   r-   r-   r.   create_flex_formatting_context  s   z,LayoutContext.create_flex_formatting_contextc                 C   s*   | j   | j r| j d | _d S d | _d S )Nr   )r   r   r   )rt   r&   r-   r-   r.   finish_flex_formatting_context  s   

z,LayoutContext.finish_flex_formatting_contextfirstc                 C      |  | j|||S )z!Resolve value of string function.)get_string_or_element_forrR   rt   r   namekeywordr-   r-   r.   get_string_set_for#     
z LayoutContext.get_string_set_forc                 C   r   )z"Resolve value of element function.)r   r   r   r-   r-   r.   get_running_element_for(  r   z%LayoutContext.get_running_element_forc                 C   s   | j || v rW|| | j  d }|| | j  d }|dkr|S |dkrK|}|rJ|jd dkr@|jd D ]\}}	||kr?|  S q3|jrI|jd }q%	 n|dkrQ|S |dkrWd	S t| j d
 ddD ]}
|
|| v rr|| |
 d   S q`d	S )aZ  Resolve value of string or element function.

        We'll have something like this that represents all assignments on a
        given page:

        {1: ['First Header'], 3: ['Second Header'],
         4: ['Third Header', '3.5th Header']}

        Value depends on current page.
        https://drafts.csswg.org/css-gcpm/#funcdef-string

        :param dict store:
            Dictionary where the resolved value is stored.
        :param page:
            Current page.
        :param str name:
            Name of the named string or running element.
        :param str keyword:
            Indicates which value of the named string or running element to
            use. Default is the first assignment on the current page else the
            most recent assignment.
        :returns:
            Text for string set, box for running element.

        r   r   r   startrR   r@   lastzfirst-exceptNr
   )rU   r"   rS   rB   )rt   storer   r   r   first_stringlast_stringrL   rk   r:   previous_pager-   r-   r.   r   -  s6   
z'LayoutContext.get_string_or_element_forc                 C   s    | j | | j| |  S )z+Add a footnote to the layout for this page.)rD   remover   r$   _update_footnote_areart   footnoter-   r-   r.   layout_footnotea  s   zLayoutContext.layout_footnotec                 C   sT   || j vr(| j | || jv r| j| n|| jv r"| j| |   dS dS )z@Remove a footnote from the layout and return it to the waitlist.N)rD   r$   r   r   r   r   r   r-   r-   r.   unlayout_footnoteg  s   


zLayoutContext.unlayout_footnotec                 C   s$   | j | | j| |   dS )z0Mark a footnote as being moved to the next page.N)r   r   r   r$   r   r   r-   r-   r.   report_footnotes  s   zLayoutContext.report_footnotec                 C   s   | j jdkr| js|  j| j  7  _| j| j _| j jrZt| j 	 }t
| |t d| j jd }|j| j _| jsB|  j| 8  _|jd }|j|  |j|  |j k}|S d| j _| jsk|  j| j  8  _dS )z9Update the page bottom size and our footnote area height.r   Nr   r   F)r   r   r   r   r   r   rS   r   create_anonymous_boxesdeepcopyr   r   r   r   margin_bottom)rt   ro   
last_childoverflowr-   r-   r.   r   y  s8   



z#LayoutContext._update_footnote_areaN)r   )ru   rv   rw   r   r   staticmethodr   r   r   r   r   r   r   r   r   r   r   r   r-   r-   r-   r.   ry      s     


4ry   N)r=   )rx   collectionsr   	functoolsr   mathr   formatting_structurer   r   loggerr	   absoluter   r   
backgroundr   blockr   r   r   r   r/   r<   rq   rE   rr   ry   r-   r-   r-   r.   <module>   s    4
t