a
    [Xh                     @   s\  d Z ddl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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 edg dZG dd dZG dd deZG dd deZ dd Z!dd Z"dd Z#dd Z$d d! Z%d"d# Z&ed$d% Z'ed&d' Z(d(d) Z)d*d+ Z*d,d- Z+d.d/ Z,d0d1 Z-d2d3 Z.dS )4z'Layout for pages and CSS3 margin boxes.    N)
namedtuple)inf   )computed_from_cascaded)boxesbuild)PROGRESS_LOGGER   )absolute_box_layoutabsolute_layout)block_container_layoutblock_level_layout)float_layout)handle_min_max_heighthandle_min_max_width)resolve_percentages)max_content_widthmin_content_widthPageType)sideblanknameindexgroupsc                   @   sJ   e Zd Zedd Zedd Zejdd Zedd Zedd	 Zd
S )OrientedBoxc                 C   s   | j | j | j S N)padding_plus_bordermargin_amargin_bself r!   [/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/weasyprint/layout/page.pysugar   s    zOrientedBox.sugarc                 C   s   | j | j S r   )r#   innerr   r!   r!   r"   outer   s    zOrientedBox.outerc                 C   s    t t| j|| j | j| _d S r   )minmaxmin_content_sizer#   max_content_sizer$   )r    Znew_outer_widthr!   r!   r"   r%      s    c                 C   s   | j | jdkr| jn| j S Nauto)r#   r$   r(   r   r!   r!   r"   outer_min_content_size#   s    z"OrientedBox.outer_min_content_sizec                 C   s   | j | jdkr| jn| j S r*   )r#   r$   r)   r   r!   r!   r"   outer_max_content_size(   s    z"OrientedBox.outer_max_content_sizeN)	__name__
__module____qualname__propertyr#   r%   setterr,   r-   r!   r!   r!   r"   r      s   



r   c                   @   s4   e Zd Zdd Zdd Zedd Zedd Zd	S )
VerticalBoxc                 C   sB   || _ || _|j| _|j| _|j| _|j|j	 |j
 |j | _d S r   )contextboxheightr$   
margin_topr   margin_bottomr   Zpadding_topZpadding_bottomZborder_top_widthZborder_bottom_widthr   r    r4   r5   r!   r!   r"   __init__/   s    
zVerticalBox.__init__c                 C   s"   | j }| j|_| j|_| j|_d S r   )r5   r$   r6   r   r7   r   r8   r    r5   r!   r!   r"   restore_box_attributes;   s    z"VerticalBox.restore_box_attributesc                 C   s   dS )Nr   r!   r   r!   r!   r"   r(   B   s    zVerticalBox.min_content_sizec                 C   s   dS )Ng    .Ar!   r   r!   r!   r"   r)   F   s    zVerticalBox.max_content_sizeNr.   r/   r0   r:   r<   r1   r(   r)   r!   r!   r!   r"   r3   .   s   
r3   c                   @   s4   e Zd Zdd Zdd Zedd Zedd Zd	S )
HorizontalBoxc                 C   sN   || _ || _|j| _|j| _|j| _|j|j	 |j
 |j | _d | _d | _d S r   )r4   r5   widthr$   margin_leftr   margin_rightr   Zpadding_leftZpadding_rightZborder_left_widthZborder_right_widthr   _min_content_size_max_content_sizer9   r!   r!   r"   r:   L   s    
zHorizontalBox.__init__c                 C   s"   | j }| j|_| j|_| j|_d S r   )r5   r$   r?   r   r@   r   rA   r;   r!   r!   r"   r<   X   s    z$HorizontalBox.restore_box_attributesc                 C   s$   | j d u rt| j| jdd| _ | j S NF)r%   )rB   r   r4   r5   r   r!   r!   r"   r(   ^   s
    

zHorizontalBox.min_content_sizec                 C   s$   | j d u rt| j| jdd| _ | j S rD   )rC   r   r4   r5   r   r!   r!   r"   r)   e   s
    

zHorizontalBox.max_content_sizeNr=   r!   r!   r!   r"   r>   K   s   
r>   c                 C   s  |rt nt| |}|jtdd |j|j|jfD  }||krn|jdkrNd|_|jdkr^d|_|jdkrnd|_d|j|j|jfvr|rd|_nd|_|j|j|jgddkr|jdkr||j |j |j |_nH|jdkr||j |j |j |_n$|jdkr||j |j |j |_|jdkrd|jdkr:d|_|jdkrLd|_||j |j |j |_|j|j  krdkrn n||j |j d  |_|_d|j|j|jfvsJ |  dS )a  Compute and set a margin box fixed dimension on ``box``.

    Described in: https://drafts.csswg.org/css-page-3/#margin-constraints

    :param box:
        The margin box to work on
    :param outer:
        The target outer dimension (value of a page margin)
    :param vertical:
        True to set height, margin-top and margin-bottom; False for width,
        margin-left and margin-right
    :param top_or_left:
        True if the margin box in if the top half (for vertical==True) or
        left half (for vertical==False) of the page.
        This determines which margin should be 'auto' if the values are
        over-constrained. (Rule 3 of the algorithm.)
    c                 s   s   | ]}|d kr|V  qdS )r+   Nr!   ).0valuer!   r!   r"   	<genexpr>   s   z*compute_fixed_dimension.<locals>.<genexpr>r+   r   r	   r   N)	r3   r>   r   sumr   r   r$   countr<   )r4   r5   r%   verticalZtop_or_lefttotalr!   r!   r"   compute_fixed_dimensionm   sb    





 rL   c                    s  |rt nt  fdd|D }|\}}}|D ]$}|jdkrBd|_|jdkr.d|_q.|jjs|jdkslJ |j|j  krdkrn n@||j|j kr||j |j }|j}	|j}
|	|
 }|dkrd}|j||	 |  |_	|j||
 |  |_	n||j
|j
 krp||j
 |j
 }|j|j }	|j|j }
|	|
 }|dkrFd}|j||	 |  |_	|j||
 |  |_	nZ||j
 |j
 }|j}	|j}
|	|
 }|dkrd}|j||	 |  |_	|j||
 |  |_	n2|jdkr||j	 |_	n|jdkr^||j	 |_	n\|jdkr&dt|j|j }||j| krt||j | }|j}|}|| }|dkr^d}|j|| |  |_	ndt|j
|j
 }||j
| kr||j
 | }|j|j }|| }|| }|dkrd}|j|| |  |_	nB||j
 | }|j}|}|| }|dkrd}|j|| |  |_	|jdkrB||j	 d |_	|jdkr^||j	 d |_	ddd |D vsvJ |D ]}|  qzdS )	ua  Compute and set a margin box fixed dimension on ``box``

    Described in: https://drafts.csswg.org/css-page-3/#margin-dimension

    :param side_boxes:
        Three boxes on a same side (as opposed to a corner).
        A list of:
        - A @*-left or @*-top margin box
        - A @*-center or @*-middle margin box
        - A @*-right or @*-bottom margin box
    :param vertical:
        ``True`` to set height, margin-top and margin-bottom;
        ``False`` for width, margin-left and margin-right.
    :param available_size:
        The distance between the page box’s left right border edges

    c                    s   g | ]} |qS r!   r!   rE   r5   Z	box_classr4   r!   r"   
<listcomp>       z.compute_variable_dimension.<locals>.<listcomp>r+   r   r	   r   c                 S   s   g | ]
}|j qS r!   )r$   rM   r!   r!   r"   rO   E  rP   N)r3   r>   r   r   r5   is_generatedr$   r-   r)   r%   r,   r(   r'   r<   )r4   
side_boxesrJ   Zavailable_sizeZbox_aZbox_bZbox_cr5   Z
flex_spaceZflex_factor_aZflex_factor_cZflex_factor_sumZac_max_content_sizeZflex_factor_bZflex_factor_acZac_min_content_sizer!   rN   r"   compute_variable_dimension   s   



 
















rS   c                 C   s   d}dD ]^}| | dkr"d| |< qg }| | D ]*\}}|dkrBd}|dkr.| ||f q.t|| |< q|du r|sd	| d
  | d
< dS )zDrop 'pages' counter from style in @page and @margin context.

    Ensure `counter-increment: page` for @page context if not otherwise
    manipulated by the style.

    F)Zcounter_setZcounter_resetcounter_incrementr+   r!   pageTpagesN))rU   r	   rT   )appendtuple)stylepseudo_typeZpage_counter_touchedZpropnameZjustified_valuesr   rF   r!   r!   r"    _standardize_page_based_countersK  s     r[   c              	   #   sH  fddj }j}j}j} } }|| }	|| }
g }dd||f|dfdd||f||
fdd||fd|fd	d||f|	|ffD ]\} }}|rg d
} \}}ng d} \}} fdd|D }tdd |D sqt||| t|g dD ]~\}}|j	sq
||_
||_|rL| j|||   7  _n| j
|||   7  _
t||| dv  || q
qd||ddfd|||	dfd||d|
fd|||	|
ffD ]h\}}}}}|||f}|j	sq||_
||_t||dd|v  t||dd|v  || q|D ]}t|V  q.dS )zYield laid-out margin boxes for this page.

    ``state`` is the actual, up-to-date page-state from
    ``context.page_maker[context.current_page]``.

    c           	         s     j| }|du r&tdi jd}t||  t| |}|d dv|_|jrt	}|\}}}|
t  t||j t|j||| j j j 	|_t| t| t|}t|| |jsd |_|_dD ]}|| q|S )aa  Return a margin box with resolved percentages.

        The margin box may still have 'auto' values.

        Return ``None`` if this margin box should not be generated.

        :param at_keyword:
            Which margin box to return, e.g. '@top-left'
        :param containing_block:
            As expected by :func:`resolve_percentages`.

        N)elementZcascadedZparent_stylecontent)normalZinhibitnoner   )toprightbottomleft)	style_for	page_typer   rY   r[   r   Z	MarginBoxrQ   copydeepcopyrW   setr   update_countersZcontent_to_boxesZget_image_from_uritarget_collectorZcounter_stylechildrenZprocess_whitespaceZprocess_text_transformcreate_anonymous_boxesr   r?   r6   Z_reset_spacing)	
at_keywordcontaining_blockrY   r5   Zmargin_stateZquote_depthZcounter_valuesZcounter_scopesr   )r4   rU   stater!   r"   make_boxl  s6    







z#make_margin_boxes.<locals>.make_boxr`   Fr   rb   rc   Tra   )r`   middlerb   )rc   centerra   c                    s"   g | ]}d  d|  qS )@-r!   )rE   suffix)rn   rp   prefixr!   r"   rO     s   z%make_margin_boxes.<locals>.<listcomp>c                 s   s   | ]}|j V  qd S r   )rQ   rM   r!   r!   r"   rG     rP   z$make_margin_boxes.<locals>.<genexpr>)r   g      ?r	   )r`   rc   z@top-left-cornerz@top-right-cornerz@bottom-left-cornerz@bottom-right-cornerN)r7   r8   r@   rA   Zborder_widthZborder_heightanyrS   ziprQ   
position_x
position_ymargin_heightZmargin_widthrL   rW   margin_box_content_layout)r4   rU   ro   r7   r8   r@   rA   Zmax_box_widthZmax_box_heightZ
page_end_xZ
page_end_yZgenerated_boxesrJ   ry   rz   suffixesZfixed_outerZvariable_outerrR   r5   offsetrm   cb_width	cb_heightr!   )rn   r4   rp   rU   rv   ro   r"   make_margin_boxesd  s    /









r   c                 C   s   g }t | |t dd||dddd
\}}}}}}|du s:J |D ]}t| |||ddd q>|jd }|dv r|jr|jd }	|jd	 }
|	j}|
j|
  }|| }|j| }|d
kr|d }|jD ]}|d| q|S )u<   Layout a margin box’s content once the box has dimensions.NTF)bottom_space
skip_stackpage_is_emptyZabsolute_boxesfixed_boxesadjoining_marginsdiscard	max_linesr   r   r   vertical_align)rq   rb   rq   r   )	r   r   r   rY   rk   rz   r{   r6   	translate)r4   rU   r5   positioned_boxes	resume_at	next_page_absolute_boxr   Zfirst_childZ
last_childr`   rb   Zcontent_heightr~   childr!   r!   r"   r|     s2    





r|   c                 C   s   || j  }| jdkrH| jdkr$d| _| jdkr4d| _|| j | j | _nn| j| j  kr`dkr|n n|| j d  | _| _n:| jdkr|| j | j | _n| jdkr|| j | j | _|   dS )a  Take a :class:`OrientedBox` object and set either width, margin-left
    and margin-right; or height, margin-top and margin-bottom.

    "The width and horizontal margins of the page box are then calculated
     exactly as for a non-replaced block element in normal flow. The height
     and vertical margins of the page box are calculated analogously (instead
     of using the block height formulas). In both cases if the values are
     over-constrained, instead of ignoring any margins, the containing block
     is resized to coincide with the margin edges of the page box."

    https://drafts.csswg.org/css-page-3/#page-box-page-rule
    https://www.w3.org/TR/CSS21/visudet.html#blockwidth

    r+   r   r   N)r   r$   r   r   r<   )r5   Zcontaining_block_size	remainingr!   r!   r"   page_width_or_height	  s    





r   c                 C   s   t t|| | d S r   )r   r>   )r5   r4   Zcontaining_block_widthr!   r!   r"   
page_width(  s    r   c                 C   s   t t|| | d S r   )r   r3   )r5   r4   Zcontaining_block_heightr!   r!   r"   page_height-  s    r   c           6   
   C   s8  |  |}|j|d< t||}|jd }t|| d|_d|_|\}	}
t|| |	 t	|| |
 |
 |_| |_|j|j | _|}|  |d}t||}t|| |
 |_| j|_|jr|}|g }t|tjsJ |   || _g | _|| _| j}g | _t|D ]N\}}| j| | |}|r|dkr| | ||d | _ q\qd}g }g }g }i }| j }|| _|D ]|\}}}| |_| rt | ||||d|\}}n$|! sJ t"| |||d|\}}|| |r|||f||< qt#| |d||||||	\}}}}}}|s0J ||j$ |_$t%&|' }t#| |t( d|j)d||d }|j*|+  d dd	 |D |_,|D ]} t-| | ||ddd
 q| .| ||g|_$t/|d t%0|| |d }!| j1}"| j2}#|#|d  d }$g }%g }&|#d|d  D ]4\}}}}}'|%3|'4dg  |&3|'4dg  q|j5ddD ]}(|(jd })|)r|)|%vr|$d |) |%|) |"6|)|!|d |# |(j7r|"j84|(j7df}*nd}*|*durRd}+t9|*},|,|&v}-|-r|$d |, |&|, |d |*_:|*j;r.|!|*j<kr$t='|!|*_<d|*_;d}+|*j>}.|.rd|.v rLd|$d< |-r|!|*j<krt='|!|*_<|.D ]&}/|!4|/d}0|0durnd}+ qqn|*j?}1|1@ D ]P\}2}3d|3vrq|"jA4|2d}4|4j:}5|5dkr|2|%v rd|#|5 d d< q|+rRd|$d< |*B|! qR|jr.|}|#|d  d }|||fS )a  Take just enough content from the beginning to fill one page.

    Return ``(page, finished)``. ``page`` is a laid out PageBox object
    and ``resume_at`` indicates where in the document to start the next page,
    or is ``None`` if this was the last page.

    :param int page_number:
        Page number, starts at 1 for the first page.
    :param resume_at:
        As returned by ``make_page()`` for the previous page, or ``None`` for
        the first page.

    overflowsizer   z	@footnoteNT)Zdyc                 S   s"   g | ]}|j jd  dkr|j qS )positionfixed)Z_boxrY   )rE   placeholderr!   r!   r"   rO     s   zmake_page.<locals>.<listcomp>r   r	   r   anchorscontent_lookups)Zplaceholdersanchorr]   FrV   pages_wantedcontent_changed)Crd   Zviewport_overflowr   ZPageBoxrY   r   ry   rz   r   r   Zcontent_box_xZcontent_box_yr6   Zpage_bottomZFootnoteAreaBoxr   Zcopy_with_children
isinstanceZBlockLevelBoxZcreate_block_formatting_contextZcurrent_pageZcurrent_page_footnotesZcurrent_footnote_areareported_footnotes	enumerateZ	footnotesrW   Zlayout_footnoteZreport_footnotebroken_out_of_flowvaluesZ
is_floatedr   Zis_absolutely_positionedr
   r   rk   r   rl   rg   r   rU   r   r{   r   r   Zfinish_block_formatting_contextr[   ri   rj   
page_makerextendgetZdescendantsZcache_target_page_countersZmissing_linkZcounter_lookup_itemsidpage_maker_indexpendingZcached_page_counter_valuesrf   missing_countersZmissing_target_countersitemsZtarget_lookup_itemsZparse_again)6r4   root_boxre   r   page_number
page_staterY   rU   Zdevice_sizer   r   Zinitial_containing_blockZfootnote_area_styleZfootnote_areaZprevious_resume_atr   iZreported_footnoter   r   r   r   Zout_of_flow_boxesr   Zcontext_out_of_flowr5   rn   r   Zout_of_flow_boxZout_of_flow_resume_atr   r   r   Zpage_counter_valuesrj   r   remake_stateZcached_anchorsZcached_lookupsZx_remake_stater   r   Zcounter_lookupZcall_parse_againZcounter_lookup_idZrefresh_missing_countersr   Zcounter_nameZcounter_valueZtarget_missingZanchor_nameZmissed_countersitemr   r!   r!   r"   	make_page2  s:   

































r   c                 C   s\   | |  |j| |j|j|jd | D ],\}}|r*|| kr*|j|||j||jd q*dS )zASet style for page types and pseudo-types matching ``page_type``.)rootparentbase_url)rZ   r   r   r   N)Zadd_page_declarationsZset_computed_stylesZetree_elementr   Zget_cascaded_styles)re   htmlrd   r\   rZ   r!   r!   r"   set_page_type_computed_styles  s    
r   c                 C   sD   |sdS |  \\}}| d u s&|| vr*dS |d u r6dS t| | |S )NTF)r   _includes_resume_at)r   page_group_resume_atZpage_child_indexZpage_child_resume_atr!   r!   r"   r   !  s    r   c                 C   s  t | }t| d d  D ]6\}}t||d r@|d  d7  < q| ||  q|s|rd|d dksl|d st|d S | r| d d |d kr|d S t|pdd i }}	|}
t| d \}}|
}|
j| }
|d u rq|}q|r|j	d S |
j	d |d kr| 
|d d|	g |d S t|
tjs4|d S t|
jD ]8\}}| sTq>|d i||< || }|}|}
 qq>|d S qd S )Nr   r	   breakrw   rU   r   r   )lenr   r   poprf   rg   rX   r   rk   rY   rW   r   r   Z	ParentBoxZis_in_normal_flow)page_groupsr   r   r   r   Zpage_groups_lengthr   Z
page_groupZcurrent_resume_atr   Zcurrent_elementZchild_indexZchild_resume_atZparent_elementr   r!   r!   r"   _update_page_groups,  sD    


r   c                 C   s  |j }||  \}}}}	}
t|	}	|d dv r8|d }n<|d dv rp|jd dk}|d dk}||A rjdnd}nd	}t|dkr|p|dkr| p|jo|d	u }|rdnd}t|||||}td
d |D }t|||| |}t	|||j
 |d dkp|d |_d|_| d }t||||||	\}}}|s2J | }| d t|krPd}n<|| d  \}}}}}
||kp||kp||kp||	k}|rddg g d}|d	u|d< ||||	|f}| d t|kr|| n||| d < ||fS )a  Return one laid out page without margin boxes.

    Start with the initial values from ``context.page_maker[index]``.
    The resulting values / initial values for the next page are stored in
    the ``page_maker``.

    As the function's name suggests: the plan is not to make all pages
    repeatedly when a missing counter was resolved, but rather re-make the
    single page where the ``content_changed`` happened.

    r   )rc   ra   )Zrectoverso	directionZltrr   ra   rc   Nc                 s   s   | ]\}}}||fV  qd S r   r!   )rE   r   r   r   r!   r!   r"   rG     rP   zremake_page.<locals>.<genexpr>rw   rU   Fr	   T)r   r   r   r   r   )r   rf   rg   rY   boolr   r   rX   r   r   rd   Zforced_breakZmargin_clearancer   r   rW   )r   r   r4   r   r   r   r   r   Z
right_pager   r   Znext_page_sideZdirection_ltrZbreak_versor   r   r   r   re   r   rU   Zpage_maker_next_changedZnext_resume_atZnext_next_pageZnext_right_pageZnext_page_stater   r   r!   r!   r"   remake_pageg  sh    





	r   c           
      c   s   d}d}g }| j | d }t|dks6|d s6|d rtd|d  d|d< d|d< g |d	< g |d
< t||| ||\}}	| j}|V  n0td|d  | j |d  d }	d}|| V  |d7 }|	du r|s| j d|d  | _ | j  | j  dS qdS )zaReturn a list of laid out pages without margin boxes.

    Re-make pages only if necessary.

    r   Nr   r   r   z"Step 5 - Creating layout - Page %dr	   Fr   r   z/Step 5 - Creating layout - Page %d (up-to-date))r   r   r   infor   r   r   clear)
r4   r   r   rV   r   r   r   r   rU   r   r!   r!   r"   make_all_pages  s:    


r   )/__doc__rf   collectionsr   mathr   cssr   Zformatting_structurer   r   loggerr   absoluter
   r   blockr   r   floatr   Zmin_maxr   r   percentr   	preferredr   r   r   r   r3   r>   rL   rS   r[   r   r|   r   r   r   r   r   r   r   r   r   r!   r!   r!   r"   <module>   sF   "G  

 Z;Q