a
    [XhY                     @   s  d Z ddlZddl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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 d	dlmZ d	dlmZm Z  d	dl!m"Z"m#Z#m$Z$ d	dl%m&Z& d	dl'm(Z(m)Z) dd Z*dd Z+dd Z,dd Z-dd Z.dd Z/d d! Z0d"d# Z1ed$d% Z2d&d' Z3d(d) Z4d*d+ Z5d,d- Z6dEd/d0Z7d1d2 Z8d3d4 Z9d5d6 Z:d7d8 Z;d9d: Z<d;d< Z=d=d> Z>d?d@ Z?dAdB Z@dCdD ZAdS )FzLayout for inline-level boxes.    N)inf   )computed_from_cascaded)character_ratiostrut_layout)boxesbuild)can_break_textcreate_layoutsplit_first_line   )AbsolutePlaceholderabsolute_layout)flex_layout)avoid_collisionsfloat_layout)grid_layout)handle_leader)handle_min_max_width)resolve_one_percentageresolve_percentages)inline_min_content_widthshrink_to_fittrailing_whitespace_size)inline_replaced_box_layout)find_in_flow_baselinetable_wrapper_widthc	              
   c   s   t || |du r"t|d|j nd|_t| ||||||||	\}	}
|	r`t| |	| |	j|	j }|	du rldS |	|
fV  |
du rdS |
}d|_d}q(dS )zReturn an iterator of ``(line, resume_at)``.

    ``line`` is a laid-out LineBox with as much content as possible that
    fits in the available width.

    Ntext_indentr   )r   r   widthr   get_next_lineboxr   
position_yheight)contextboxr    bottom_space
skip_stackcontaining_blockabsolute_boxesfixed_boxesfirst_letter_styleline	resume_at r,   ]/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/weasyprint/layout/inline.pyiter_line_boxes   s&    	


r.   c	           +      C   sD  t ||}|dkrdS t|||}||_| jrTt| ||dd|_t|j| \|_}	nd |_|_t	| ||dd\}
}}|j}| j
 }|
 }|_| }|_|j}|
| }|
|j7 }
g }g }g }g }g }t| ||
|||||||||\}}}}}}|j|j |_|_t|r|sd|_qbt| | t	| ||dd\}}	}t| |||du pP|d	}|jd
 dkrx|d9 }||j8 }t|\} }!|!dusJ | dusJ |! |_|!|_| |! |_||! }"d|_d|_|||" ||_|j|krqb|j}| j}#|| _t	| ||dd\}
}}|jd
 dkr8|
|f||fk}$n|
|j |f|| |fk}$|$r|#| _qbq|| || |D ]L}%d|%jjd v r|%d||%j  n |%|j|%j ||j |%j  qzg }&|j|j }'|D ]F}(|'|(_t| |(||||dd\})}*|&|) |*r|(||*f| j|)< q|&r<| jt|&7  _||fS )zReturn ``(line, resume_at)``.continueNNT)r%   
first_liner   F)outerNlast	directionrtlltrinlineZdisplayr%   )skip_first_whitespacefirst_letter_to_boxr    excluded_shapesr   r   r   styler!   r   copy
position_xr   split_inline_boxis_phantom_lineboxremove_last_whitespace
text_alignline_box_verticalitybaseline
margin_topmargin_bottom	translateextendZ	specifiedr   appendbroken_out_of_flowchildrentuple)+r"   lineboxr    r$   r%   r&   r'   r(   r)   _r@   available_widthZcandidate_heightr=   Zoriginal_position_xZoriginal_position_yZoriginal_widthmax_xline_placeholdersZline_absolutesZ
line_fixedwaiting_floatsline_childrenr*   r+   preserved_line_breakfirst_letterlast_letterfloat_widthnew_position_xZnew_available_widthZoffset_xbottomtopZoffset_yZnew_excluded_shapes	conditionplaceholderZfloat_childrenZwaiting_floats_yZwaiting_floatZnew_waiting_floatZwaiting_float_resume_atr,   r,   r-   r   6   s    













r   c                 C   sB  |du rd}d}n|  \\}}t| tjr|du s8J | jd }| j }|t|kr\dS |dv r||d }|r|dr|d7 }|dd }qp|r|diS dS t| tj	tj
fr&|dkr| jsdS t| j| |}|dkr|d7 }|t| jkrdS t| j| d}|s|r"||iS dS |du s>J d|  dS )	zReturn ``skip_stack`` to start just after removable leading spaces.

    See https://www.w3.org/TR/CSS21/text.html#white-space-model

    Nr   white_spacer/   normalnowrappre-line    r   zunexpected skip inside )items
isinstancer   TextBoxr>   textencodelen
startswithLineBox	InlineBoxrM   r;   )r#   r%   indexZnext_skip_stackr_   rh   resultr,   r,   r-   r;      s8    


r;   c                 C   s   g }|}t |tjtjfr:|| |js.dS |jd }qt |tjrT|jd dv sXdS |j	d}|rt
|t
|jkr~dS ||_t| |dd\}}}|dusJ |du sJ |j|j }|j|_n|j}d|_d|_|jjd r|jD ]}	|	j| d	d
 q|D ]}
|
 j|8  _qdS )zRemove in place space characters at the end of a line.

    This also reduces the width and position of the inline parents of the
    modified text.

    Nr7   r_   r`    r    r   TdxZignore_floats)rf   r   rl   rm   rK   rM   rg   r>   rh   rstriprj   split_text_boxr   pango_layoutZfirst_line_directionrI   )r"   r*   Z	ancestorsr#   Znew_textnew_boxresumerP   Zspace_widthchildZancestorr,   r,   r-   rC      s:    


rC   c                 C   sf  |rb| j rbd|d< d}| j d }t|tjrti |dd}|jdrt| j d|| j|g}|g| j dd R | _ qb|j	rbd	}|r|
 \}|r|\}	|j	|	d |_	d}|j	r|j	d }
t|
}|d
vr|rqd}||
7 }|j	dd |_	q|drb|d dkrnt| j d|| jg }t| j d|| j|}|f|_ |g| j R | _ ntt| j d|| jg }d|_t| j d|| jg }|f|_ t| j d|| j|}|f|_ |g| j R | _ t| |rb|rb|\}	| \\}}|	|d |ii}nDt|tjrb|r>|
 \}nd}t|||}|rb|\}	|	|i}|S )z-Create a box for the ::first-letter selector.Zstaticpositionrq   r   N)ZcascadedZparent_styleelementz::first-letterr   F)ZPsZPePiZPfZPoT
floatnone)rM   rf   r   rg   r   Zelement_tagendswithrm   r{   rh   valuesunicodedatacategorylstripZBlockBoxr)   rl   r   Zprocess_text_transformre   	ParentBoxr<   )r#   r%   r)   rW   ry   Zletter_styleZ
letter_boxZcharacter_foundchild_skip_stackrn   Znext_letterr   Ztext_boxZline_boxchild_indexZgrandchild_skip_stackr,   r,   r-   r<     s    






r<   c                 C   s   t |tjr*| }t|| | |_nt |tjr|jrj|j	|j
f}t| || |j	|j|j  }}	}
t| ||||||}|jr||	|
  |_	|_|_ntdt|j d|S )z7Compute the width and the height of the atomic ``box``.Layout for  not handled yet)rf   r   ZReplacedBoxr?   r   margin_heightrF   InlineBlockBoxis_table_wrapperr   r!   r   	min_width	max_widthinline_block_box_layout	TypeErrortype__name__)r"   r#   r@   r%   r&   r'   r(   Zcontaining_sizer   r   r   r,   r,   r-   
atomic_boxb  s"    

r   c           	      C   s   ddl m} t|| |jdkr&d|_|jdkr6d|_|jdkrFd|_|jdkrVd|_t|| | ||_d|_	|| |t
 |d||d dd d
\}}}}}}t||_|S )Nr   )block_container_layoutautor   TF)r$   r%   Zpage_is_emptyr'   r(   Zadjoining_marginsdiscard	max_lines)blockr   r   margin_leftmargin_rightrG   rH   inline_block_widthr@   r    r   inline_block_baselinerF   )	r"   r#   r@   r%   r&   r'   r(   r   rP   r,   r,   r-   r   x  s*    






r   c                 C   s|   | j rL| jD ]<}t|tjr|jr|jd jr|jd jd }|j  S qn"| jd dkrnt| dd}|rn|S | j| 	  S )zReturn the y position of the baseline for an inline block.

    Position is taken from the top of its margin box.

    https://www.w3.org/TR/CSS21/visudet.html#propdef-vertical-align

    r   ZoverflowZvisibleTr3   )
r   rM   rf   r   ZTableBoxrF   r>   r   r    r   )r#   ry   Z	first_rowro   r,   r,   r-   r     s    
r   c                 C   sF   |j | j| j | j | j | j | j  }| j dkrBt|| || _ d S )Nr   )r   r   r   border_left_widthborder_right_widthpadding_leftpadding_rightr   )r#   r"   r&   Zavailable_content_widthr,   r,   r-   r     s    

r   c                 C   s  t || ddd}t|tjr||_|du r4d}n"| \\}}|pHd}|du sVJ t|dk}t| ||| ||d\}}}|du rd}n|di}|jr|jd }|du r|jd }q|j	 d| 
 d }nd }}nt|tjr>|jdkrd|_|jdkrd|_t| |||||||||	|
|\}}}}}}nPt|tjrzt| ||||||}||_d}d}d}d}nt|tjr||_d|_d	D ].}t|d
| dkrt|d
| d qt| |t ||d||d	\}}}}}d}d}d}nt|tjrx||_d|_d	D ].}t|d
| dkrt|d
| d qt| |t ||d||\}}}}}d}d}d}ntdt|j d||||||fS )a  Fit as much content as possible from an inline-level box in a width.

    Return ``(new_box, resume_at, preserved_line_break, first_letter,
    last_letter)``. ``resume_at`` is ``None`` if all of the content
    fits. Otherwise it can be passed as a ``skip_stack`` parameter to resume
    where we left off.

    ``new_box`` is non-empty (unless the box is empty) and as big as possible
    while being narrower than ``available_width``, if possible (may overflow
    is no split is possible.)

    r   leftrightNis_line_startr7   r   Fu   ⺀r\   r   r[   r   margin_r   r   )r   rf   r   rg   r@   re   rj   ru   rh   ri   decoderm   r   r   rA   AtomicInlineLevelBoxr   InlineFlexBoxr    getattrsetattrr   r   InlineGridBoxr   r   r   r   )r"   r#   r@   rR   r$   r%   r&   r'   r(   rS   rT   rU   float_widthsskipr   rw   rV   r+   rW   rX   siderP   r,   r,   r-   split_inline_level  s    






r   c              	   C   s  |  rX||_t|}|| ||||f |jd dkrJ|	| n
|
| n| r||_t| ||j}dd || D }|r|t| |d 8 }||| ks|r|| q t	| |||	|
|d d\}}|r|||f| j
|< ||||f |}t| d}||jd   |7  < |jd d	krJt|tjrb||7 }n|jd d
krb||8 }|D ]d\}}| s|qf|jd d	kr|jd dks|jd d
krf|jd dkrf|j|d qfn2| r |jd d }| j}| j| | | d S )Nrz   absolutec                 S   s   g | ]\}}}|  s|qS r,   
is_floated).0rP   Zchild_r,   r,   r-   
<listcomp>+  s   z'_out_of_flow_layout.<locals>.<listcomp>r7   r:   r   r~   r   r   r5   r8   r6   rs   r   )Zis_absolutely_positionedr@   r   rK   r>   r   r   r   r   r   rL   maxmargin_widthrf   r   rl   is_in_normal_flowrI   Z
is_runningZcurrent_pageZrunning_elements)r"   r#   r&   rn   ry   rM   rU   waiting_childrenrT   r'   r(   rS   r   rR   r@   r$   r^   rY   Znon_floating_children	new_childZfloat_resume_atrs   rP   Z	old_childZrunning_namepager,   r,   r-   _out_of_flow_layout  sf    







r   c                 C   s   |r|  }|r| \}}}| rt|s0q|rF||v rF|| }nd }|j|  d }||jkrt| ||j|||||||||	\}}}}}}|rq|d8 }q\q|
| |d u rt|t	j
sJ n|
|||f ||iS |
r|
d d d d iS d S )Nr   r7   r   )r?   popr   can_break_insider@   r   r   rJ   rf   r   rg   rK   )r"   r#   rR   r$   initial_skip_stackr'   r(   rS   rT   rU   rM   r   Zwaiting_children_copyr   ry   Zoriginal_childr   r   Zchild_resume_atrP   r,   r,   r-   _break_waiting_children]  s8    




r   c           2      C   s  |d9 }|du }|}|}t |tjtjfs.J |j|j |j }|j|j |j	 }|}g }g }d}d }}ddd}d}|j
d dkrg }|rd}n| \\}}t|j|d D ]\}}|| }|j|_| s$t| ||||||||
|||	|||| | r|d }||
vr|| 8 }q|t|jd k}|}g }t| |||||||||	||\} }!}"}#}$}%|j
d	 d
kr|}&||%d 8 }n|}&||%d 8 }|r|&r|!du r||&8 }t| |||||||||	||\} }!}"}#}$}%d}|"rd}d}'|du rd}n$|du rd}n|j
d dv r(d}'|'du rjd||#fv rFd}'n$|#dv rV|#}'nt||# |j
d }'|'r~|| g }|du r|#}|jrd}n|$}| du rt |tjsbJ nt |tjr||| f t | tjo | jo t| jd dk}(| j|   })|)|krN|(sNt| |||||||	|
|||}*|*rN|*}! q|)}||| |f |
| |!dur|| ||!i}! qq|| d}!|j
d	 d
krt|dkrdd |D }+|+d j}|+ddd D ]&}|j||j dd || 7 }q|!du },| dd |D }-|-j!| |, d t |tjrd|-_"|-j}|-j
d	 dkrn|ddd }|D ],}| rr|j|  |-j |-_" qqrnp||-_|j
d dkrd}.n|j
d	 dkr|n|,}.|.r|-jD ]}|j|d q|| |-_"|-j|d dd t#|j
| \}/|-_$|j
d |-_%|/|-j% d  }0|0|-j& |-j' |-_(|0|-j) |-j* |-_+|-j
d dkr|D ]}1t,| |1|-||dd! qv|!durt-t.|!}||k r|di}!|j/rd}d}|-|!||||fS )"z$Same behavior as split_inline_level.g0D   ?NFr   r   rz   relativer   r5   r6   r   r   Trp       r_   )prerb   )TFlangr7   Zsc                 S   s   g | ]\}}}|  r|qS r,   )r   )r   rP   	box_childr,   r,   r-   r   !  s   z$split_inline_box.<locals>.<listcomp>rr   c                 S   s   g | ]\}}}|qS r,   r,   )r   rn   r   rP   r,   r,   r-   r   ,      )startendr8   Zbox_decoration_breakcloner   	font_sizer   r:   )0rf   r   rl   rm   r   r   r   r   r   r   r>   re   	enumeraterM   r    r   r   r   r   rj   r   r	   rJ   Ztrailing_collapsible_spacerg   rK   rh   r   r   r@   r   rI   Zcopy_with_childrenZremove_decorationr   r   rF   r!   border_top_widthpadding_toprG   Zborder_bottom_widthZpadding_bottomrH   r   nextiterZ	is_leader)2r"   r#   r@   rR   r$   r%   r&   r'   r(   rS   rT   rU   Zis_startZinitial_position_xr   Zleft_spacingZright_spacingZcontent_box_leftrM   r   rV   rW   rX   r   Zfloat_resume_indexr   iry   rn   Zis_last_childrQ   Zchild_waiting_floatsr   r+   Z	preservedfirstr4   Znew_float_widthsZend_spacingZ	can_breakZtrailing_whitespacerZ   Zprevious_resume_atZin_flow_childrenZis_endrw   Ztranslation_neededline_heighthalf_leadingZabsolute_boxr,   r,   r-   rA     sX   
























rA   Tc                 C   s@  t |tjsJ |jd }|j |d }|dks8|s<dS t| |j| ||j|d\}}}	}
}}|	dkspJ |dkr|	|j}|
|_
||_||_t|j| \}}|| d }||_||_||_| j|j7  _nd}|	du rd}nN|||	  }||	ko
|d}|r.d	}||v s.J d
|f |	|7 }	||	|fS )av  Keep as much text as possible from a TextBox in a limited width.

    Try not to overflow but always have some text in ``new_box``.

    Return ``(new_box, skip, preserved_line_break)``. ``skip`` is the number of
    UTF-8 bytes to skip form the start of the TextBox for the next line, or
    ``None`` if all of the text fits.

    Also break on preserved line breaks.

    r   Nr   )NNFr   r   Frp   )r}   	   u    u    zDGot %r between two lines. Expected nothing or a preserved line break)rf   r   rg   r>   rh   ri   r   r   justification_spacingZcopy_with_textr   rv   r!   r   rG   rH   rF   strip)r"   r#   rQ   r   r   r   rh   layoutlengthZresume_indexr   r!   rF   r   rP   r   rV   ZbetweenZline_breaksr,   r,   r-   ru   e  sH    
ru   c           
         s   g  t |  dd\}} fdd D }|rTdd |D }|rTt|}t||| }|D ]Z\}}}| rv||j }	n2|jd dkr|| }	n|jd dksJ || }	t||	 qX||fS )	zHandle ``vertical-align`` within a :class:`LineBox`.

    Place all boxes vertically assuming that the baseline of ``box``
    is at `y = 0`.

    Return ``(max_y, min_y)``, the maximum and minimum vertical position
    of margin boxes.

    r   
baseline_yc                    s4   g | ],}|  rd nt| dd\}}|||fqS )r0   r   r   )r   aligned_subtree_verticalityr   subtree	sub_max_y	sub_min_ytop_bottom_subtreesr,   r-   r     s   z(line_box_verticality.<locals>.<listcomp>c                 S   s"   g | ]\}}}|  s|| qS r,   r   r   r,   r,   r-   r     s   vertical_alignr\   r[   )r   r   r   r    r>   translate_subtree)
r#   max_ymin_yZsubtrees_with_min_maxZsub_positionsZhighest_subr   r   r   dyr,   r   r-   rE     s.    



rE   c                 C   sP   t | tjr@|  j|7  _| jd dv rL| jD ]}t|| q.n| j|d d S )Nr   r\   r[   r   )rf   r   rm   r    r>   rM   r   rI   )r#   r   ry   r,   r,   r-   r     s    
r   c                 C   sV   t | ||\}}|| j }||   }|d u s6||k r:|}|d u sJ||krN|}||fS N)inline_box_verticalityrF   r   )r#   r   r   r   r   r\   r[   r,   r,   r-   r     s    
r   c                 C   s  d}d}t | tjtjfs"||fS | jD ]}| sJ| r(|| q(|jd }|dkrb|}n|dkr| jd t	| jd }|||
  d  }	|	|j }n|dkr|| j | j | j | j }	|	|j }nX|d	kr|| j | j | j | j | j }
|
|
  |j }n|d
v r d}n|| }||j }	tjtjtjf}t ||rb|j|	|j d n|	|_|d
v r~|| q(|	|
  }
|du s|	|k r|	}|du s|
|kr|
}t |tjr(t|||\}}|dur||k r|}|dur(||kr(|}q(||fS )zHandle ``vertical-align`` within an :class:`InlineBox`.

    Place all boxes vertically assuming that the baseline of ``box``
    is at `y = baseline_y`.

    Return ``(max_y, min_y)``, the maximum and minimum vertical position
    of margin boxes.

    Nr   rF   Zmiddler   xr   ztext-topztext-bottomr   r   r   )rf   r   rl   rm   rM   r   r   rK   r>   r   r   rF   rG   r   r   r!   r   r   r   rI   r    r   )r#   r   r   r   r   ry   r   Zchild_baseline_yZone_exr\   r[   Z	box_typesZchildren_max_yZchildren_min_yr,   r,   r-   r     sr    







r   c                 C   s   |j |krdS |jd }|r6|jd }|dkr2|n|}|jd dv }|dv rl|dk|jd	 d
kA rhd}nd}|dkrxdS ||j  }|dkr|rt| || dS |dkr|d S |dksJ |S dS )z>Return the line horizontal offset according to ``text-align``.r   Ztext_align_allZtext_align_lastr   r_   r`   r   r   r5   r6   r   r   Zjustifycenterr   N)r   r>   justify_line)r"   r*   rQ   r4   ZalignZ
align_lastZspace_collapseoffsetr,   r,   r-   rD   4  s,    



rD   c                 C   s"   t | }rt| ||| d d S )Nr   )count_expandable_spacesadd_word_spacing)r"   r*   Zextra_width	nb_spacesr,   r,   r-   r   Y  s    r   c                 C   sR   t | tjr$| jd| jd S t | tjtjfrJtdd | jD S dS dS )z;Count expandable spaces (space and nbsp) for justification.rp   r   c                 s   s   | ]}t |V  qd S r   )r   r   ry   r,   r,   r-   	<genexpr>f  r   z*count_expandable_spaces.<locals>.<genexpr>r   N)	rf   r   rg   rh   countrl   rm   sumrM   )r#   r,   r,   r-   r   `  s
    r   c           
      C   s   t |tjrv||_| j|7  _t|}|dkrt|j|j| d |jd}|	  || }||7 }| j
|7  _
||_nt |tjtjfr| j|7  _|}|j}|jd dkr|d d d }|D ]}	|	 rt| |	||}q| j
|| 7  _
n||d |S )Nr   )r   r   r5   r6   r7   )rf   r   rg   r   r@   r   r
   rh   r>   Z
deactivater   rv   rl   rm   rM   r   r   rI   )
r"   r#   r   Z	x_advancer   r   Zextra_spaceZprevious_x_advancerM   ry   r,   r,   r-   r   k  s8    r   c                 C   s   | j D ]|}t|tjrtt|s$ dS dD ]H}t|jd|  dd sh|jd| d sh|jd|  jr(  dS q(q| r dS qdS )	NFr   r   valueZborder__widthZpadding_T)	rM   rf   r   rm   rB   r   r>   r   r   )rO   ry   r   r,   r,   r-   rB     s    
rB   c                 C   sh   | j d dv }t| tjrdS |r@t| tjr@t| j| j d S |rdt| tjrdtdd | j	D S dS )Nr_   )ra   zpre-wraprc   Fr   c                 s   s   | ]}t |V  qd S r   )r   r   r,   r,   r-   r     r   z#can_break_inside.<locals>.<genexpr>)
r>   rf   r   r   rg   r	   rh   r   anyrM   )r#   Z	text_wrapr,   r,   r-   r     s    r   )T)B__doc__r   mathr   cssr   Zcss.computed_valuesr   r   Zformatting_structurer   r   Ztext.line_breakr	   r
   r   r   r   r   Zflexr   r~   r   r   Zgridr   Zleaderr   Zmin_maxr   percentr   r   	preferredr   r   r   Zreplacedr   tabler   r   r.   r   r;   rC   r<   r   r   r   r   r   r   r   rA   ru   rE   r   r   r   rD   r   r   r   rB   r   r,   r,   r,   r-   <module>   sT    ~)3R
	`G4 U
?*N%