3
<WN                 @   s  d Z ddlmZ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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 ddlmZm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)d#d$ Z*d%d& Z+ed'd( Z,d)d* Z-d+d, Z.d-d. Z/d/d0 Z0d1d2 Z1d3d4 Z2d5d6 Z3d7d8 Z4d9d: Z5d;d< Z6d=d> Z7d?d@ Z8dAS )Bz
    weasyprint.layout.inline
    ------------------------

    Line breaking and layout for inline-level boxes.

    :copyright: Copyright 2011-2014 Simon Sapin and contributors, see AUTHORS.
    :license: BSD, see LICENSE for details.

    )divisionunicode_literals   )absolute_layoutAbsolutePlaceholder)avoid_collisionsfloat_layout)image_marker_layout)handle_min_max_widthhandle_min_max_height)resolve_percentagesresolve_one_percentage)shrink_to_fitinline_min_content_widthtrailing_whitespace_size)find_in_flow_baselinetable_wrapper_width   )split_first_line)boxes)strut_layoutex_ratioc       
   	   c   sZ   xTt | |||||||\}}	|r,|j|j }|dkr8dS ||	fV  |	dkrNdS |	}qW dS )a  Return an iterator of ``(line, resume_at)``.

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

    :param linebox: a non-laid-out :class:`LineBox`
    :param position_y: vertical top position of the line box on the page
    :param skip_stack: ``None`` to start at the beginning of ``linebox``,
                       or a ``resume_at`` value to continue just after an
                       already laid-out line.
    :param containing_block: Containing block of the line box:
                             a :class:`BlockContainerBox`
    :param device_size: ``(width, height)`` of the current page.

    N)get_next_linebox
position_yheight)
contextboxr   
skip_stackcontaining_blockdevice_sizeabsolute_boxesfixed_boxesline	resume_at r$   Y/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/weasyprint/layout/inlines.pyiter_line_boxes   s    

r&   c             C   s  t || |dkr"t|d|j nd|_t||}|dkr>dS t| ||dd|_t|j| j\|_	}||_
t| ||dd\}	}}
|j	}| jdd }xV|	|_||_
|	|
 }|	|j7 }	g }g }g }g }t| ||	||||||||\}}}t|o| r d|_	P t| | t|\}}|dk	s$t|dk	s2t| |_||_
|| |_	|| }d|_d|_t| ||
|dkpr|d	}|dks|dkr|j|| |j	|krP |j	}| j}|| _t| ||dd\}	}}
|	|f|j|j
fkr|| _P qW |j| |j| xR|D ]J}|jjjd
r0|jd||j
  n |j|j|j ||j	 |j
  qW g }|j
|j	 }x0|D ](}||_
t| |||||}|j| qlW |r|j|jt| }||fS )zReturn ``(line, resume_at)``.Ntext_indentr   continueT)r   
first_lineF)Zouter)lastinline)NN) r   r   widthr'   skip_first_whitespacer   r   styleenable_hintingr   r   r   excluded_shapes
position_xsplit_inline_boxis_phantom_lineboxremove_last_whitespaceline_box_verticalityAssertionErrorbaseline
margin_topmargin_bottom
text_align	translateextendZ_weasy_specified_display
startswithr   appendcopy_with_childrenchildrentuple)r   lineboxr   r   r   r   r    r!   _r1   available_widthZcandidate_heightr0   max_xline_placeholdersZline_absolutesZ
line_fixedwaiting_floatsr"   r#   preserved_line_breakbottomtopZoffset_yZoffset_xZnew_excluded_shapesplaceholderZfloat_childrenZwaiting_floats_yZwaiting_floatr$   r$   r%   r   ;   s    










r   c             C   s(  |dkrd}d}n|\}}t | tjr|dks2t| jj}t| j}||krPdS |d
kr|x"||k rz| j| dkrz|d7 }qZW |r|dfS dS t | tjtj	fr|dkr| j
 rdS t| j
| |}|dkr|d7 }|t| j
krdS t| j
| d}|s|r
||fS dS |dks$td	|  dS )zReturn the ``skip_stack`` to start just after the remove spaces
    at the beginning of the line.

    See http://www.w3.org/TR/CSS21/text.html#white-space-model
    Nr   r(   normalnowrappre-line r   zunexpected skip inside %s)rL   rM   rN   )
isinstancer   TextBoxr6   r.   white_spacelentextLineBox	InlineBoxr@   r-   )r   r   indexZnext_skip_stackrR   lengthresultr$   r$   r%   r-      s4    
r-   c       	      C   s   g }x4t |tjtjfr8|j| |js,dS |jd	 }qW t |tjoP|jjd
ksVdS |j	j
d}|rt|t|j	kr|dS ||_	t| |ddd\}}}|dk	st|dkst|j|j }|j|_n|j}d|_d|_	x|D ]}| j|8  _qW dS )zRemove in place space characters at the end of a line.

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

    Nr   rL   rM   pre-linerO   r    )rL   rM   rZ   )rP   r   rU   rV   r>   r@   rQ   r.   rR   rT   rstriprS   split_text_boxr6   r,   )	r   r   Z	ancestorsnew_textnew_boxZresumerC   Zspace_widthZancestorr$   r$   r%   r4      s0    


r4   c             C   s   | j j| jj| jj\}}| jdkrt| jdkrt|dk	r>|| _n6| j jdk	rt|dk	rb|| j j | _n| jj| j j | _| jdkr| j jdk	r| j| j j | _n$|dk	r|| _n|\}}td|| _dS )zT
    Compute and set the used width for replaced boxes (inline- or block-level)
    autoNi,  )	replacementget_intrinsic_sizer.   image_resolution	font_sizer   r,   intrinsic_ratiomin)r   r   intrinsic_widthintrinsic_heightdevice_width_device_heightr$   r$   r%   replaced_box_width   s     
rl   c             C   s   | j j| jj| jj\}}| j j}| jjdkrB| jjdkrB|| _n| jjdkr^|r^| j| | _| jjdkr| jjdkr|dk	r|| _nb|dk	r| jjdkr| j| | _n@| jjdkr|dk	r|| _n$| jjdkr|\}}td|d | _dS )zU
    Compute and set the used height for replaced boxes (inline- or block-level)
    ra   N   r   )	rb   rc   r.   rd   re   rf   r   r,   rg   )r   r   rh   ri   rf   rj   rk   r$   r$   r%   replaced_box_height!  s"    rn   c             C   s>   x.d	D ]&}t | d| dkrt| d| d qW t| | dS )
z5Lay out an inline :class:`boxes.ReplacedBox` ``box``.rJ   rightrI   leftZmargin_ra   r   N)rJ   ro   rI   rp   )getattrsetattr inline_replaced_box_width_height)r   r   sider$   r$   r%   inline_replaced_box_layout=  s    
ru   c             C   sR   | j jdkr:| j jdkr:tj| | tj| | t|  nt| | t| | d S )Nra   )r.   r,   r   rl   Zwithout_min_maxrn   min_max_auto_replaced)r   r   r$   r$   r%   rs   E  s    

rs   c             C   s  | j }| j}| j}| j}t|| j}t|| j}||k r<dn||krHdnd||k rVdn||krbdndf}|dkrtd}|dkrd}|dkr|| _ t|| | || _nH|dkr|| _ t|| | || _n"|d	krt|| | || _ || _ n|d
krt|| | || _ || _n|dkrl|| || krP|| _ t||| | | _nt||| | | _ || _n|dkr|| || krt||| | | _ || _n|| _ t||| | | _n.|dkr|| _ || _n|dkr|| _ || _dS )znResolve {min,max}-{width,height} constraints on replaced elements
    that have 'auto' width and heights.
    rg   maxr[   r   gư>N)rw   r[   )rg   r[   )r[   rw   )r[   rg   )rw   rw   )rg   rg   )rg   rw   )rw   rg   )r,   r   	min_width
min_heightrw   	max_width
max_heightrg   )r   r,   r   rx   ry   rz   r{   Z
violationsr$   r$   r%   rv   O  sT    





rv   c          	   C   s   t |tjr8t|ddr"t| n
t|| |j |_nPt |tjrv|j	r^t
| ||j|jf t| |||||||}ntdt|j |S )z7Compute the width and the height of the atomic ``box``.Zis_list_markerFzLayout for %s not handled yet)rP   r   ZReplacedBoxrq   r	   ru   margin_heightr7   InlineBlockBoxZis_table_wrapperr   r,   r   inline_block_box_layout	TypeErrortype__name__)r   r   r1   r   r   r   r    r!   r$   r$   r%   
atomic_box  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\}}	}	}	}	t||_|S )Nr   )block_container_layoutra   r   infT)Zmax_position_yr   r   Zpage_is_emptyr    r!   )Zblocksr   r   margin_leftmargin_rightr8   r9   inline_block_widthr1   r   floatinline_block_baseliner7   )
r   r   r1   r   r   r   r    r!   r   rC   r$   r$   r%   r~     s&    





r~   c             C   s.   | j jdkr t| dd}|r |S | j| j  S )z
    Return the y position of the baseline for an inline block
    from the top of its margin box.

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

    ZvisibleT)r*   )r.   Zoverflowr   r   r|   )r   rY   r$   r$   r%   r     s
    r   c             C   s   | j dkrt|| |j | _ d S )Nra   )r,   r   )r   r   r   r$   r$   r%   r     s    
r   c             C   s  t || t|tjrx||_|dkr*d}n|\}}|p8d}|dksFtt| ||| ||\}}}|dkrnd}q|df}nt|tjr|jdkrd|_|j	dkrd|_	t
| |||||||||	|
\}}}n0t|tjrt| |||||||}||_d}d}|||fS )a  Fit as much content as possible from an inline-level box in a width.

    Return ``(new_box, resume_at)``. ``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.)

    Nr   ra   F)r   rP   r   rQ   r1   r6   r^   rV   r   r   r2   ZAtomicInlineLevelBoxr   )r   r   r1   rE   r   r   r   r    r!   rF   rG   skipr`   rH   r#   r$   r$   r%   split_inline_level  s<    





r   c       !      C   sB  |dk}|}t |tjtjfs"t|j|j |j }|r@||7 }|}g }d}|jj	dkr\g }|rfd}n|\}}x
|j
|D ]\}}|j|_|j r||_t|}|	j| |j| |jj	dkr|j| q||j| q| n|j r||_t| ||j}dd |D }|r$|t| |d 8 }||| ks8|
rD|
j| q|t| |||||}|j| xB|d| D ]2}|j sqn|jjd	krn|j|j d
 qnW |jjd	kr||j 7 }q||jjdkr|||j 8 }q|t| |||||||||	|
\}}}d}|r
d}|dkr(t |tjsbtn:|j }|| }||krT|rT|df}P n|}|j| |dk	r|||f}P q|W d}|j|||dkd}t |tjr||j |_n||_|| |_t|j| j\}|_|jj|_ ||j  d }||j! |j" |_#||j$ |j% |_&|jj	dkr8x|D ]} t'| | || q W |||fS )z$Same behavior as split_inline_level.NFZrelativer   absolutec             S   s   g | ]}|j  s|qS r$   )
is_floated).0Zchild_r$   r$   r%   
<listcomp>4  s    z$split_inline_box.<locals>.<listcomp>r   rp   )Zdxro   T)is_startZis_endg       @r\   )(rP   r   rU   rV   r6   Zpadding_leftr   Zborder_left_widthr.   positionZenumerate_skipr   Zis_absolutely_positionedr1   r   r>   r   r   r,   r   r   is_in_normal_flowr   r;   margin_widthr   rQ   r?   r   r/   r7   re   r   border_top_widthpadding_topr8   Zborder_bottom_widthZpadding_bottomr9   r   )!r   r   r1   rE   r   r   r   r    r!   rF   rG   r   Zinitial_position_xZleft_spacingZcontent_box_leftr@   rH   r   rW   childrK   Zfloat_widthZnon_floating_childrenZ	old_child	new_childr#   Z	preservedr   Znew_position_xr`   line_heighthalf_leadingZabsolute_boxr$   r$   r%   r2     s    






	






r2   c             C   s  t |tjst|jj}|j|d }|dks4| r8d
S t||j| j||\}}}	}
}}|	dksdt|j	j
d}|	dk	r|jd||	 j
d}t|jdd|	 j
d}	t|jdd| j
d}|dkr0|j|}|
|_||_||_t|j| j\}}|| d }||_||_||_| j|j7  _nd}|	dkrDd}n:||	koV|jd}|rv|dksvtd	|f |	|7 }	||	|fS )a^  Keep as much text as possible from a TextBox in a limitied width.
    Try not to overflow but always have some text in ``new_box``

    Return ``(new_box, skip)``. ``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 an preserved whitespace.

    Nr   Futf8g       @rO   
    zDGot %r between two lines. Expected nothing or a preserved line break)NNF)r   r   )rP   r   rQ   r6   r.   re   rT   r   r/   Z
text_bytesdecodeencoderS   Zcopy_with_textr,   pango_layoutr   r   r8   r9   r7   strip)r   r   rD   
line_widthr   re   rT   ZlayoutrX   r#   r,   r   r7   r_   Zbetweenr   rC   r   rH   r$   r$   r%   r^     sD    



r^   c       
         s   g  t |  dd\}} fdd D }|rTdd |D }|rTt|}t||| }x^|D ]V\}}}|j rx||j }	n.|jjdkr|| }	n|jjdkst|| }	t||	 qZW ||fS )a  Handle ``vertical-align`` within an :class:`LineBox` (or of a
    non-align sub-tree).

    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                s<   g | ]4}|j  rdnt| ddgD ]\}}|||fq$qS )Nr   )r   )NN)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 | ]\}}}|j  s|| qS r$   )r   )r   r   r   r   r$   r$   r%   r     s   rJ   rI   )r   rw   r   r   r.   vertical_alignr6   translate_subtree)
r   max_ymin_yZsubtrees_with_min_maxZsub_positionsZhighest_subr   r   r   dyr$   )r   r%   r5     s(    

r5   c             C   sR   t | tjrB|  j|7  _| jjdkrNx&| jD ]}t|| q.W n| j|d d S )NrJ   rI   )r   )rJ   rI   )	rP   r   rV   r   r.   r   r@   r   r;   )r   r   r   r$   r$   r%   r     s    r   c             C   sV   t | ||\}}|| j }|| j  }|d ks6||k r:|}|d ksJ||krN|}||fS )N)inline_box_verticalityr7   r|   )r   r   r   r   r   rJ   rI   r$   r$   r%   r     s    
r   c             C   s  d}d}t | tjtjfs"||fS x| jD ]}|j sN|j r,|j| q,|jj	}|dkrd|}n|dkr| jj
t| j }|||j  d  }	|	|j }n|dkr|| j | j | j | j }	|	|j }nX|dkr|| j | j | j | j | j }
|
|j  |j }n|dkrd	}n|| }||j }	t |tjrR|j|	|j d
 n|	|_|dkrn|j| q,|	|j  }
|dks|	|k r|	}|dks|
|kr|
}t |tjr,t|||\}}|dk	r||k r|}|dk	r,||kr,|}q,W ||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.

    Nr7   Zmiddleg       @ztext-topztext-bottomrJ   rI   r   )r   )rJ   rI   )rJ   rI   )rP   r   rU   rV   r@   r   r   r>   r.   r   re   r   r|   r7   r8   r   r   r   r}   r;   r   r   )r   r   r   r   r   r   r   Zchild_baseline_yZone_exrJ   rI   Zchildren_max_yZchildren_min_yr$   r$   r%   r     sZ    


"



r   c             C   s   |j j}|j jdk}|dkr:|dk|j jdkA r6d}nd}|d	krZ|rZ|j jdkrVdnd}|dkrfd
S ||j }|d	kr|rt| || d
S |dkr|d }n|dkst|S )zgReturn how much the line should be moved horizontally according to
    the `text-align` property.

    rL   rM   pre-line-weasy-start
-weasy-endZrtlrp   ro   Zjustifyr   centerg       @)rL   rM   r   )r   r   )r.   r:   rR   	directionr,   justify_liner6   )r   r"   rD   r*   ZalignZspace_collapseoffsetr$   r$   r%   r:   h  s&    

r:   c             C   s*   t |}|dkrd S t| ||| d d S )Nr   )count_spacesadd_word_spacing)r   r"   Zextra_width	nb_spacesr$   r$   r%   r     s    r   c             C   sF   t | tjr| jjdS t | tjtjfr>tdd | jD S dS d S )NrO   c             s   s   | ]}t |V  qd S )N)r   )r   r   r$   r$   r%   	<genexpr>  s    zcount_spaces.<locals>.<genexpr>r   )	rP   r   rQ   rT   countrU   rV   sumr@   )r   r$   r$   r%   r     s
    r   c       
      C   s   t |tjr| j|7  _|j j|7  _t|}|dkrt| |dd d\}}}|d k	s\t|d ksht||| 7 }|j	|_	|j
|_
nbt |tjtjfr| j|7  _|}x|jD ]}	t| |	||}qW | j	|| 7  _	n|j|d |S )Nr   g    _B)rP   r   rQ   r1   r.   Zword_spacingr   r^   r6   r,   r   rU   rV   r@   r   r;   )
r   r   Zextra_word_spacingZ	x_advancer   r`   r#   rC   Zprevious_x_advancer   r$   r$   r%   r     s*    
r   c             C   s   xz| j D ]p}t|tjrlt|s$dS xRdD ]<}t|jd|  ddsb|jd	|  sb|jd
|  jr*dS q*W q|j rdS qW dS )z8http://www.w3.org/TR/CSS21/visuren.html#phantom-line-boxFrJ   ro   rI   rp   z	margin_%svalueNzborder_%s_widthz
padding_%sT)rJ   ro   rI   rp   )	r@   rP   r   rV   r3   rq   r.   r   r   )rB   r   rt   r$   r$   r%   r3     s    

r3   N)9__doc__
__future__r   r   r   r   r   r   r   r   Zreplacedr	   Zmin_maxr
   r   Zpercentagesr   r   Z	preferredr   r   r   Ztablesr   r   rT   r   Zformatting_structurer   Zcss.computed_valuesr   r   r&   r   r-   r4   rl   rn   ru   rs   rv   r   r~   r   r   r   r2   r^   r5   r   r   r   r:   r   r   r   r3   r$   r$   r$   r%   <module>   sJ   i'++
:3 E-M
