3
(bW"\                 @   s   d Z ddlmZmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZ d	d
 Zd+ddZd,ddZdd Zdd Zd-ddZd.ddZd/ddZd0ddZd1ddZd2dd Zd3d!d"Zej Zd#d$ Zd4d%d&Zd5d'd(Zd)d* ZdS )6a  
    weasyprint.layout.preferred
    ---------------------------

    Preferred and minimum preferred width, aka. max-content and min-content
    width, aka. the shrink-to-fit algorithm.

    Terms used (max-content width, min-content width) are defined in David
    Baron's unofficial draft (http://dbaron.org/css/intrinsic/).

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

    )divisionunicode_literalsN   )boxes)text   )default_image_sizingc             C   s$   t tt| |dd|t| |ddS )zReturn the shrink-to-fit width of ``box``.

    *Warning:* both available_outer_width and the return value are
    for width of the *content area*, not margin area.

    http://www.w3.org/TR/CSS21/visudet.html#float-width

    F)outer)minmaxmin_content_widthmax_content_width)contextboxZavailable_width r   [/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/weasyprint/layout/preferred.pyshrink_to_fit   s
    	r   Tc             C   s   t |tjtjtjfr:|jr,t| ||d S t| ||S nJt |tjtj	fr\t
| ||ddS t |tjrrt||S tdt|j dS )zsReturn the min-content width for ``box``.

    This is the width by breaking at every line-break opportunity.

    r   T)is_line_startz(min-content width for %s not handled yetN)
isinstancer   BlockContainerBoxTableColumnBoxTableColumnGroupBoxis_table_wrapper"table_and_columns_preferred_widthsblock_min_content_width	InlineBoxLineBoxinline_min_content_widthReplacedBoxreplaced_min_content_width	TypeErrortype__name__)r   r   r	   r   r   r   r   +   s    

r   c             C   s   t |tjtjtjfr:|jr,t| ||d S t| ||S nJt |tjtj	fr\t
| ||ddS t |tjrrt||S tdt|j dS )znReturn the max-content width for ``box``.

    This is the width by only breaking at forced line breaks.

    r   T)r   z(max-content width for %s not handled yetN)r   r   r   r   r   r   r   block_max_content_widthr   r   inline_max_content_widthr   r   r    r!   r"   )r   r   r	   r   r   r   r   C   s    

r   c                sb   |j j}|dks|jdkrB fdd|jD }|r<t|nd}n|jdksPt|j}t|||S )z+Helper to create ``block_*_content_width.``auto%c                s"   g | ]}|j  s |d dqS )T)r	   )is_absolutely_positioned).0child)r   functionr   r   
<listcomp>b   s    z(_block_content_width.<locals>.<listcomp>r   px)stylewidthunitchildrenr   AssertionErrorvalueadjust)r   r   r*   r	   r.   Zchildren_widthsr   )r   r*   r   _block_content_widthZ   s    r4   c             C   sL   | j j}| j j}|jdkr |jnd}|jdkr4|jntd}t|t||S )z;Get box width from given width and box min- and max-widths.r&   r   inf)r-   	min_width	max_widthr/   r2   floatr   r
   )r   r.   r6   r7   r   r   r   min_maxl   s
    r9   c             C   s   d}xh|rddgng |r ddgng  D ]D}| j | }|dkr&|jdkrR||j7 }q&|jdks`t||j7 }q&W |r~|| j j7 }|r|| j j7 }|d	k r|d
|d   S dS dS )z3Add box paddings, borders and margins to ``width``.r   Zmargin_leftZpadding_leftZmargin_rightZpadding_rightr%   r,   r&   d   r   g      Y@N)r-   r/   r2   r1   Zborder_left_widthZborder_right_width)r   r.   leftrightZpercentagesr2   Zstyle_valuer   r   r   margin_widthu   s"    

r=   c             C   s$   t | |}|rt| |||S |S dS )zRespect min/max and adjust width depending on ``outer``.

    If ``outer`` is set to ``True``, return margin width, else return content
    width.

    N)r9   r=   )r   r	   r.   r;   r<   Zfixedr   r   r   r3      s    
r3   c             C   s   t | |t|S )z0Return the min-content width for a ``BlockBox``.)r4   r   )r   r   r	   r   r   r   r      s    r   c             C   s   t | |t|S )z0Return the max-content width for a ``BlockBox``.)r4   r   )r   r   r	   r   r   r   r#      s    r#   Fc          	   C   sZ   t t| |||d|d}|r4t|dkr4|dd= n|d  t| |8  < t||t|S )zReturn the min-content width for an ``InlineBox``.

    The width is calculated from the lines from ``skip_stack``. If
    ``first_line`` is ``True``, only the first line minimum width is
    calculated.

    T)minimum
skip_stackr   N)listinline_line_widthslentrailing_whitespace_sizer3   r   )r   r   r	   r?   
first_liner   widthsr   r   r   r      s    	

r   c             C   s<   t t| |||dd}|d  t| |8  < t||t|S )z2Return the max-content width for an ``InlineBox``.F)r>   r   r@   )rA   rB   rD   r3   r   )r   r   r	   r   rF   r   r   r   r$      s    r$   c          	   c   s  d}|d krd}n|\}}x|j |D ]\}}	|	j r<q(t|	tjrtt| |	||||}
t|
dkrt|	||
d |
d< n0t|	||
d dd|
d< t|	||
d dd|
d< nt|	tj	rX|	j
jdk}|d krd}n|\}}|d kst|	j| pdd  }|r|r|jd	}|r2|d	kr2ddg}
n$ttj||	j
| j|rLdnd d
}
n$|rpdt| |	dg}
nt| |	g}
||
d 7 }t|
dkr|V  t|
dkrx|
dd D ]}|V  qW |
d }|
d dk}d }q(W |V  d S )Nr   r   F)r<   )r;   normalnowrappre-line )r.   r   r@   r@   )rG   rH   rI   r@   r@   r@   )Zenumerate_skipr'   r   r   r   rA   rB   rC   r3   TextBoxr-   white_spacer1   r   lstripZline_widthsenable_hintingr   )r   r   r	   r   r>   r?   current_lineskipindexr)   linesZspace_collapseZ
child_textliner   r   r   rB      sT    


	rB   c             C   s   | j jdkr$| j jjdkr$| j jjnd}| j jdkrL| j jjdkrL| j jjntd}| j jdkrx| j jjdkrx| j jjnd}t|t||S )z}Return the percentage contribution of a cell, column or column group.

    http://dbaron.org/css/intrinsic/#pct-contrib

    r%   r&   r   r5   )	r-   r6   r/   r2   r7   r8   r.   r   r
   )r   r6   r7   r.   r   r   r   _percentage_contribution  s     rT   c       <   	      s"  |j  }tj|}|r|| S d\}d}xX|jD ]N}xH|jD ]>}x0|jD ]&}	t|	j|	j t||	j |}qJW |d7 }q>W q2W fddt|D  d}xD|jD ]:}x4|jD ]*}x|jD ]}	|	 | |	j< qW |d7 }qW qW t	t
  }
|jjdkr,dkr,|jjd dtdd |
D   }nd}dksD|dkrg |_t| |dd}t| |dd}t|d	t| |d	dd
}t|d	t| |d	dd
}g g g g |g f}||f| ||f| d t|< }|| S dg }dg }d}xL|jD ]B}x8|jD ]*}|||< |||< |d7 }|krP qW qP qW dd tD }dd tD dd tD xtD ]}xn||fD ]b}|| rt|| t| || ||< t| t| || |< t| t|| |< qW xj|
| D ]^}	|	r|	jdkrt|| t| |	||< t| t| |	|< t| t|	|< qW qvW xvtdD ]f}g }g }g }xDtD ]6}|| }| }| }xt|
| D ]\}	 fddt|d D }|sqt|}   |  }!|!jd |krqt| | |!j }"|!jd |jjd  }#t||" }$t|" }%t|" }&| ||  }'|%|$ }(|(r|'|( })nd})tdt|   |  |% |# }*tdt|   |  |% |# }+t|*|%|$ },|%r| |% }-nd}-t||| |)|,  d|- |*  }t|| d|- |+  }| dkrtdt|!|& }.fddt| | |!j D }/t|/}0|0dkrdt|/ }-n| |0 }-|.|- }qW |j| |j| |j| qW |}||qjW fddtD fddtD }1tfddtD }2dt d }3|3dkrR|2dkrJd}4ntj}4n|2|3 }4|t| }5|tt|4g|1  }6dd tD }7xtD ]}|| r|| jjdkr|| jjjdkrd	|7|< q|| r|| jjdkr|| jjjdkrd	|7|< qxL|
| D ]@}	|	r |	jdkr |	jjdkr |	jjjdkr d	|7|< P q W qW |jjdkr|jjjdkr|jjj }8}9n|5}8|6}9t|5t|d|8d
}5t|6t|d|9d
}6t |t ||5}:t |t ||6};||7||
f}|5|6f| |:|;f| d t|< }|| S )ac  Return content widths for the auto layout table and its columns.

    The tuple returned is
    ``(table_min_content_width, table_max_content_width,
       column_min_content_widths, column_max_content_widths,
       column_intrinsic_percentages, constrainedness,
       total_horizontal_border_spacing, grid)``

    http://dbaron.org/css/intrinsic/

    r   r   c                s   g | ]}d g  qS )Nr   )r(   i)
grid_widthr   r   r+   4  s    z6table_and_columns_preferred_widths.<locals>.<listcomp>Zseparatec             S   s   g | ]}t |r|qS r   )any)r(   columnr   r   r   r+   B  s    F)r	   T)r	   r.   )FTNc             S   s   g | ]}d qS )r   r   )r(   rU   r   r   r   r+   f  s    c             S   s   g | ]}d qS )r   r   )r(   rU   r   r   r   r+   g  s    c             S   s   g | ]}d qS )r   r   )r(   rU   r   r   r   r+   h  s    c                s   g | ]}  | r|qS r   r   )r(   k)gridjr   r   r+     s    c                s    g | ]} | d kr| qS )r   r   )r(   r[   )intrinsic_percentagesmax_content_widthsr   r   r+     s   c          	      s*   g | ]"\}}t |d t d|  qS )r:   N)r
   sum)r(   rU   Z
percentage)r\   r   r   r+     s   c                s0   g | ](} | r|  | d  p(t d qS )g      Y@r5   )r8   )r(   rU   )r\   r]   r   r   r+     s   c             3   s"   | ]} | d kr| V  qdS )r   Nr   )r(   rU   )r\   r]   r   r   	<genexpr>  s    z5table_and_columns_preferred_widths.<locals>.<genexpr>r:   g      Y@c             S   s   g | ]}d qS )Fr   )r(   rU   r   r   r   r+     s    r%   r&   r,   )r   r   )!Zget_wrapped_tableTABLE_CACHEgetr0   r   Zgrid_xZcolspanZrowspanrangerA   zipr-   Zborder_collapseZborder_spacingrC   r   r#   r3   column_groupsr   r   rT   	enumerateslicer^   r
   appendsysmaxsizer.   r/   r2   r=   )<r   r   r	   tableresultZgrid_heightZ
row_numberZ	row_grouprowcellZzipped_gridZtotal_horizontal_border_spacingr6   r7   Zouter_min_widthZouter_max_widthrd   columnsZcolumn_numberZcolumn_grouprX   Zmin_content_widthsrU   groupsspanZmin_contributionsZmax_contributionsZpercentage_contributionsZmin_contributionZmax_contributionZpercentage_contributionZindexesoriginZorigin_cellZ
cell_sliceZbaseline_border_spacingZbaseline_min_contentZbaseline_max_contentZbaseline_percentageZcontent_width_diffZbaseline_diffZ
diff_ratioZcell_min_widthZcell_max_widthZclamped_cell_widthZratioZdiffZother_columns_contributionsZother_columns_contributions_sumZsmall_percentage_contributionsZ'large_percentage_contribution_numeratorZ)large_percentage_contribution_denominatorZlarge_percentage_contributionZtable_min_content_widthZtable_max_content_widthZconstrainednessZtable_min_widthZtable_max_widthZtable_outer_min_content_widthZtable_outer_max_content_widthr   )rZ   rV   r\   r[   r]   r   r     s   


















 

r   c          	   C   s   | j j}|dks|jdkr| j j}|dks4|jdkr:d}n|jdksHt|j}| j}|j| j j| j j	\}}t
|||jd|ddd\}}n|jdkst|j}t| ||S )z:Return the min-content width for an ``InlineReplacedBox``.r%   r&   r,   i,     )Zdefault_widthZdefault_height)r-   r.   r/   heightr1   r2   replacementZget_intrinsic_sizeZimage_resolution	font_sizer   Zintrinsic_ratior3   )r   r	   r.   rs   imageZiwidthZiheight_r   r   r   r   8  s     r   c       
      C   s  ddl m}m} x*t|tjtjfr:|js.dS |jd	 }qW t|tjoX|j	oX|j
jd
ks^dS |j	jd}|j
jdkst|t|j	krdS | r|| |ddd\}}}|st|j|}|| |ddd\}}}|dk	st|dkst|j|j S ||j	|j
| jdd\}}}}	}}|	S dS )z6Return the size of the trailing whitespace of ``box``.r   )split_text_boxsplit_first_liner   rG   rH   pre-linerJ   Nr@   )rG   rH   rz   )Zinlinesrx   ry   r   r   r   r   r0   rK   r   r-   rL   rstripru   rC   r1   Zcopy_with_textr.   rN   )
r   r   rx   ry   Zstripped_textZold_boxrw   Zstripped_boxZresumer.   r   r   r   rD   N  s0    
rD   )T)T)TT)TT)T)T)TNFF)TF)N)T)T)__doc__
__future__r   r   rh   weakrefZformatting_structurer    r   Zreplacedr   r   r   r   r4   r9   r=   r3   r   r#   r   r$   rB   WeakKeyDictionaryr`   rT   r   r   rD   r   r   r   r   <module>   s4   

	



 

	
=
   
