a
    Qh?                     @   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 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   	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 )Ng0D   ?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 }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   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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.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