a
    ¥Q•h¤?  ã                   @   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.

    Úbreak_beforeÚrightTÚleftFÚrectoÚ	directionÚltrÚversoÚrtlNÚanyr   )ÚbreakÚpageÚpages)Úcontent_changedÚpages_wantedZanchorsZcontent_lookups)Ú
page_makerÚstyleÚpage_valuesÚappend)ÚcontextÚroot_boxZ
page_breakZ
right_pageÚ	resume_atÚ	next_pageÚ
page_stateÚremake_state© r)   úS/var/www/sistema_ama/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   ÚReplacedBoxÚcopyr   r   r   )	r#   r   Zcontaining_pager   ÚboxZabsolute_boxesZabsolute_boxÚ_Znew_absolute_boxesr)   r)   r*   Úlayout_fixed_boxesM   s$    
þ
þr2   é   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 r®d}	|d r~||k}
q~|	s|
s qÎq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qât
|ƒ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   Ú	enumerateÚdescendantsÚbookmark_labelÚelement_tagÚendswithÚelementr"   Úmissing_linkÚtarget_collectorZcounter_lookup_itemsÚitemsZparse_againÚ
string_setÚchildrenÚextendr2   Úcurrent_pageÚtupler   r   Úget_image_from_uri)"Úhtmlr$   r#   Z	max_loopsr   Zoriginal_footnotesZactual_total_pagesÚloopZinitial_total_pagesZreloop_contentZreloop_pagesÚ	page_datar1   r'   r(   Zpage_counter_valuesZwatch_elementsZwatch_elements_beforeZwatch_elements_afterÚir   ÚchildZ	checklistr0   Z	css_tokenÚitemZstring_setsrF   Ústring_nameÚtextZroot_childrenÚrootÚfootnote_areaÚstater)   r)   r*   Úlayout_documentf   s”    
ÿ





ÿ
ÿ

"

rW   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)   )ÚselfrQ   r)   r)   r*   r"   Ü   s    zFakeList.appendN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r"   r)   r)   r)   r*   rX   Ú   s   rX   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 rY   r)   r)   r)   r)   r*   Ú<lambda>ð   ó    ú:LayoutContext.__init__.<locals>.<lambda>.<locals>.<lambda>r   r)   r)   r)   r*   r`   ð   ra   z(LayoutContext.__init__.<locals>.<lambda>c                   S   s   t dd„ ƒS )Nc                   S   s   g S rY   r)   r)   r)   r)   r*   r`   ñ   ra   rb   r   r)   r)   r)   r*   r`   ñ   ra   F)Ú	style_forr   rK   Úfont_configÚcounter_stylerD   Ú_excluded_shapes_listsr:   Zpage_footnotesÚcurrent_page_footnotesÚreported_footnotesÚcurrent_footnote_areaÚexcluded_shapesÚpage_bottomr   rF   Úrunning_elementsrI   Zforced_breakZbroken_out_of_flowÚ	in_columnÚstrut_layoutsÚfont_featuresÚtablesÚdictionaries)rZ   rc   rK   rd   re   rD   r)   r)   r*   Ú__init__á   s.    zLayoutContext.__init__c                 C   s   |   | j| |¡S rY   )Ú	overflowsrk   )rZ   r,   Ú
position_yr)   r)   r*   Úoverflows_pageý   s    zLayoutContext.overflows_pagec                 C   s   || d kS )Ng0¸D   ð?r)   )Úbottomrt   r)   r)   r*   rs      s    zLayoutContext.overflowsc                 C   s   g | _ | j | j ¡ d S rY   )rj   rf   r"   ©rZ   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)   )rt   Úmargin_height)Ú.0Úshaper)   r)   r*   Ú
<listcomp>  s   ÿzALayoutContext.finish_block_formatting_context.<locals>.<listcomp>éÿÿÿÿ)r    rj   Úcontent_box_yry   Úmaxrf   Úpop)rZ   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 rY   )rX   rj   rf   r"   rw   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 )Nr   )rf   r‚   rj   )rZ   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_forrF   ©rZ   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.)r‡   rl   rˆ   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 }qJq°qJn|dkr¤|S |dkr°d	S t| j d
 ddƒD ]$}
|
|| v rÂ|| |
 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†   ÚstartrF   r6   Úlastzfirst-exceptNr
   )rI   r    rG   r8   )rZ   Ústorer   r‰   rŠ   Zfirst_stringZlast_stringrB   rR   r1   Zprevious_pager)   r)   r*   r‡   -  s.    

z'LayoutContext.get_string_or_element_forc                 C   s    | j  |¡ | j |¡ |  ¡ S )z+Add a footnote to the layout for this page.)r:   Úremoverg   r"   Ú_update_footnote_area©rZ   Ú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)r:   r"   rg   r�   rh   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)rg   r�   rh   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r´t | 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.rz   Nr   r   F)ri   ry   rm   rk   r{   rg   rG   r   Zcreate_anonymous_boxesÚdeepcopyr   r   r   rt   Úmargin_bottom)rZ   rU   Z
last_childÚoverflowr)   r)   r*   r‘   y  s8    
ÿ
þþ

ÿÿÿz#LayoutContext._update_footnote_areaN)r†   )r†   )r[   r\   r]   rr   ru   Ústaticmethodrs   rx   rƒ   r„   r…   r‹   rŒ   r‡   r”   r•   r–   r‘   r)   r)   r)   r*   r_   à   s   


4r_   N)r3   )r^   Úcollectionsr   Ú	functoolsr   Úmathr   Úformatting_structurer   r   Úloggerr	   Úabsoluter   r   Ú
backgroundr   Úblockr   r   r   r   r+   r2   rW   r;   rX   r_   r)   r)   r)   r*   Ú<module>   s   4
t