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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}nL|dkr,d}n>|dkrD|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.

    Zbreak_beforerightTleftFZrecto	directionZltrZversortlNanyr   )breakpagepages)content_changedpages_wantedZanchorsZcontent_lookups)
page_makerstyleZpage_valuesappend)contextroot_boxZ
page_breakZ
right_pageZ	resume_atZ	next_page
page_stateremake_state r"   _/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/weasyprint/layout/__init__.pyinitialize_page_maker   s2    
	
r$   c           	      c   s   |D ]v}|j D ]j}t|tjr&| }g }t| |||t dd\}}|V  |rg }|D ]}t| |||t dd qV|}qJqqdS )zBLay out and yield fixed boxes of ``pages`` on ``containing_page``.N)bottom_spaceZ
skip_stack)Zfixed_boxes
isinstancer   ZReplacedBoxcopyr   r   r   )	r   r   Zcontaining_pager   boxZabsolute_boxesZabsolute_box_Znew_absolute_boxesr"   r"   r#   layout_fixed_boxesM   s$    

r*      c           "      c   s  t || g }g }d}t|D ]}|dkr@td| | |_|}|dkrV|j }tt||| |}t|}d}	d}
|j	D ]@}|\}}}}}|d }|g|d< |d rd}	|d r~||k}
q~|	s|
s qqg }g }g }t
|D ]<\}}|j	|d  \}}}}}|d }| D ]}|jrn|jd	r6|}n|jd
rJ|}n|}|j|v rbd|_n||j |jr|jj D ]V\\}}}|j|kr|dkr|dkr|jsq|| |dkr|j|_q|j}|r|dkr|D ]&}|\}}|j| |d  | qqqt
|D ]\}}g }|j\}} |t||d| | ||j |t|||d d | ||_|d |_|j	|j d }!|f|_| jr| 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	   infor'   	footnoteslistr   lenr   	enumerateZdescendantsZbookmark_labelZelement_tagendswithelementr   Zmissing_linktarget_collectorZcounter_lookup_itemsitemsZparse_again
string_setchildrenextendr*   current_pagetupler   r   get_image_from_uri)"htmlr   r   Z	max_loopsr   Zoriginal_footnotesZactual_total_pagesZloopZinitial_total_pagesZreloop_contentZreloop_pagesZ	page_datar)   r    r!   Zpage_counter_valuesZwatch_elementsZwatch_elements_beforeZwatch_elements_afterir   childZ	checklistr(   Z	css_tokenitemZstring_setsr:   string_nametextZroot_childrenrootfootnote_areastater"   r"   r#   layout_documentf   s    









"

rI   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"   )selfrC   r"   r"   r#   r      s    zFakeList.appendN)__name__
__module____qualname____doc__r   r"   r"   r"   r#   rJ      s   rJ   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   s   t dd S )Nc                   S   s   g S rK   r"   r"   r"   r"   r#   <lambda>       :LayoutContext.__init__.<locals>.<lambda>.<locals>.<lambda>r   r"   r"   r"   r#   rR      rS   z(LayoutContext.__init__.<locals>.<lambda>c                   S   s   t dd S )Nc                   S   s   g S rK   r"   r"   r"   r"   r#   rR      rS   rT   r   r"   r"   r"   r#   rR      rS   F)	style_forr   r?   font_configcounter_styler8   _excluded_shapes_listsr2   Zpage_footnotescurrent_page_footnotesreported_footnotescurrent_footnote_areaexcluded_shapespage_bottomr   r:   running_elementsr=   Zforced_breakZbroken_out_of_flow	in_columnZstrut_layoutsZfont_featuresZtablesZdictionaries)rL   rU   r?   rV   rW   r8   r"   r"   r#   __init__   s.    zLayoutContext.__init__c                 C   s   |  | j| |S rK   )	overflowsr]   )rL   r%   
position_yr"   r"   r#   overflows_page   s    zLayoutContext.overflows_pagec                 C   s   || d kS )Ng0D   ?r"   )Zbottomrb   r"   r"   r#   ra      s    zLayoutContext.overflowsc                 C   s   g | _ | j| j  d S rK   )r\   rX   r   rL   r"   r"   r#   create_block_formatting_context  s    z-LayoutContext.create_block_formatting_contextc                 C   sv   |j d dkrN| jrN| |j }tdd | jD |g }| j|| 7  _| j  | jrl| jd | _nd | _d S )Nheightautoc                 S   s   g | ]}|j |  qS r"   )rb   margin_height).0shaper"   r"   r#   
<listcomp>  s   zALayoutContext.finish_block_formatting_context.<locals>.<listcomp>)r   r\   Zcontent_box_yrf   maxrX   pop)rL   r   Z
box_bottomZmax_shape_bottomr"   r"   r#   finish_block_formatting_context
  s    
z-LayoutContext.finish_block_formatting_contextc                 C   s   t  | _| j| j d S rK   )rJ   r\   rX   r   rd   r"   r"   r#   create_flex_formatting_context  s    z,LayoutContext.create_flex_formatting_contextc                 C   s(   | j   | j r| j d | _nd | _d S )Nrl   )rX   rn   r\   )rL   r   r"   r"   r#   finish_flex_formatting_context  s    
z,LayoutContext.finish_flex_formatting_contextfirstc                 C   s   |  | j|||S )z!Resolve value of string function.)get_string_or_element_forr:   rL   r   namekeywordr"   r"   r#   get_string_set_for#  s    
z LayoutContext.get_string_set_forc                 C   s   |  | j|||S )z"Resolve value of element function.)rs   r^   rt   r"   r"   r#   get_running_element_for(  s    
z%LayoutContext.get_running_element_forc                 C   s   | j || v r|| | j  d }|| | j  d }|dkr>|S |dkr|}|r|jd dkr|jd D ]\}}	||krf|  S qf|jr|jd }qJqqJn|dkr|S |dkrd	S t| j d
 ddD ]$}
|
|| v r|| |
 d   S qd	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   rl   rr   startr:   r.   lastzfirst-exceptNr
   )r=   r   r;   r0   )rL   storer   ru   rv   Zfirst_stringZlast_stringr7   rD   r)   Zprevious_pager"   r"   r#   rs   -  s.    

z'LayoutContext.get_string_or_element_forc                 C   s    | j | | j| |  S )z+Add a footnote to the layout for this page.)r2   removerY   r   _update_footnote_arearL   Zfootnoter"   r"   r#   layout_footnotea  s    zLayoutContext.layout_footnotec                 C   sP   || j vrL| j | || jv r.| j| n|| jv rD| j| |   dS )z@Remove a footnote from the layout and return it to the waitlist.N)r2   r   rY   r|   rZ   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)rY   r|   rZ   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rt| j 	 }t
| |t d| j jd }|j| j _| js|  j| 8  _|jd }|j|  |j|  |j k}|S d| j _| js|  j| j  8  _dS dS )z9Update the page bottom size and our footnote area height.rg   Nr   rl   F)r[   rf   r_   r]   rh   rY   r;   r   Zcreate_anonymous_boxesdeepcopyr   r   r   rb   Zmargin_bottom)rL   rG   Z
last_childZoverflowr"   r"   r#   r}   y  s8    



z#LayoutContext._update_footnote_areaN)rr   )rr   )rM   rN   rO   r`   rc   staticmethodra   re   ro   rp   rq   rw   rx   rs   r   r   r   r}   r"   r"   r"   r#   rQ      s   


4rQ   N)r+   )rP   collectionsr   	functoolsr   mathr   Zformatting_structurer   r   loggerr	   absoluter   r   Z
backgroundr   blockr   r   r   r   r$   r*   rI   r3   rJ   rQ   r"   r"   r"   r#   <module>   s   4
t