3
VmWSV                 @   s   d 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 dd
lmZmZ G dd de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edd Zedd Zd$d d!Zd"d# Z dS )%z
    weasyprint.layout.pages
    -----------------------

    Layout for pages and CSS3 margin boxes.

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

    )divisionunicode_literals   )boxesbuild   )absolute_layout)block_level_layoutblock_container_layout)resolve_percentages)min_content_widthmax_content_width)handle_min_max_widthhandle_min_max_heightc               @   sD   e Zd Zedd Zedd Zedd Zedd Zd	d
 ZdS )OrientedBoxc             C   s   | j | j | j S )N)padding_plus_bordermargin_amargin_b)self r   W/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/weasyprint/layout/pages.pysugar   s    zOrientedBox.sugarc             C   s   | j | j S )N)r   inner)r   r   r   r   outer   s    zOrientedBox.outerc             C   s   | j | jdkr| jn| j S )Nauto)r   r   min_content_size)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 )Nr   )r   r   max_content_size)r   r   r   r   outer_max_content_size%   s    z"OrientedBox.outer_max_content_sizec             C   s   t t| j|| j| _d S )N)minmaxr   r   r   )r   	availabler   r   r   shrink_to_fit*   s    zOrientedBox.shrink_to_fitN)	__name__
__module____qualname__propertyr   r   r   r   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 )N)contextboxheightr   
margin_topr   margin_bottomr   Zpadding_topZpadding_bottomZborder_top_widthZborder_bottom_widthr   )r   r(   r)   r   r   r   __init__0   s    zVerticalBox.__init__c             C   s"   | j }| j|_| j|_| j|_d S )N)r)   r   r*   r   r+   r   r,   )r   r)   r   r   r   restore_box_attributes<   s    z"VerticalBox.restore_box_attributesc             C   s   dS )Nr   r   )r   r   r   r   r   C   s    zVerticalBox.min_content_sizec             C   s   dS )Ng    .Ar   )r   r   r   r   r   G   s    zVerticalBox.max_content_sizeN)r#   r$   r%   r-   r.   r&   r   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 )
HorizontalBoxc             C   sN   || _ || _|j| _|j| _|j| _|j|j	 |j
 |j | _d | _d | _d S )N)r(   r)   widthr   margin_leftr   margin_rightr   Zpadding_leftZpadding_rightZborder_left_widthZborder_right_widthr   _min_content_size_max_content_size)r   r(   r)   r   r   r   r-   M   s    zHorizontalBox.__init__c             C   s"   | j }| j|_| j|_| j|_d S )N)r)   r   r0   r   r1   r   r2   )r   r)   r   r   r   r.   Y   s    z$HorizontalBox.restore_box_attributesc             C   s$   | j d krt| j| jdd| _ | j S )NF)r   )r3   r   r(   r)   )r   r   r   r   r   _   s    
zHorizontalBox.min_content_sizec             C   s$   | j d krt| j| jdd| _ | j S )NF)r   )r4   r   r(   r)   )r   r   r   r   r   f   s    
zHorizontalBox.max_content_sizeN)r#   r$   r%   r-   r.   r&   r   r   r   r   r   r   r/   L   s   r/   c             C   s  |rt nt| |}|jtdd |j|j|jgD  }||krn|jdkrNd|_|jdkr^d|_|jdkrnd|_d|j|j|jgkr|rd|_nd|_|j|j|jgj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dkr|jdkr||j |j d  |_|_d|j|j|jgkst|j	  dS )a  
    Compute and set a margin box fixed dimension on ``box``, as described in:
    http://dev.w3.org/csswg/css3-page/#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)
r'   r/   r   sumr   r   r   countAssertionErrorr.   )r(   r)   r   verticalZtop_or_lefttotalr   r   r   compute_fixed_dimensionn   s>    




r=   c                s  |rt nt  fdd|D }|\}}}x,|D ]$}|jdkrDd|_|jdkr0d|_q0W |jjrX|jdkrdt|j|j }||j| kr|j	|_npdt|j
|j
 }	|j|_||j |	 }
|
dkr||	 }|j	|j }|| }|dkst| j|
| | 7  _|jdkr.|j||j d |j  |jdkrx|j||j d |j  n |jdksht|j|j  kodkn  r2||j|j kr|j	|_|j	|_n|j|_|j|_||j |j }
|
dkrx|j	|j }|j	|j }|| }|dkst| j|
| | 7  _| j|
| | 7  _nF|jdkrV|j||j |j  n"|jdkrx|j||j |j  ddd |D kstx|D ]}|j  qW dS )aN  
    Compute and set a margin box fixed dimension on ``box``, as described in:
    http://dev.w3.org/csswg/css3-page/#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 outer_sum:
        The target total outer dimension (max box width or height)

    c                s   g | ]} |qS r   r   )r5   r)   )	box_classr(   r   r   
<listcomp>   s    z.compute_variable_dimension.<locals>.<listcomp>r   r   r   c             S   s   g | ]
}|j qS r   )r   )r5   r)   r   r   r   r?     s    N)r'   r/   r   r   r)   is_generatedr   r    r   r   r   r   r   r:   r"   r   r.   )r(   
side_boxesr;   Z	outer_sumZbox_aZbox_bZbox_cr)   Zac_max_content_sizeZac_min_content_sizer!   Z	weight_acZweight_bZ
weight_sumZweight_aZweight_cr   )r>   r(   r   compute_variable_dimension   sl    








 


rB   c          
   #   sb  fddj }j}j}j}j }j }|| }	|| }
g }xNdd||f|dfdd||f||
fdd||fd|fd	d||f|	|fgD ]\} }}|rdd
dg} \}}nddd	g} \}} fdd|D }tdd |D sqt||| xt|dddgD ]~\}}|j	s,q||_
||_|rZ| j|||j   7  _n| j
|||j   7  _
t||| dk |j| qW qW xd||ddfd|||	dfd||d|
fd|||	|
fgD ]h\}}}}}|||f}|j	sq||_
||_t||dd|k t||dd|k |j| qW x|D ]}t|V  qFW dS )z*Yield laid-out margin boxes for this page.c                s    j j| }|dkr jj }tj| |}|jd	k|_|jrzdg}tj	|j|| j
 }|j|}tj|}tj| t|| |jsd |_|_xd
D ]}|j| qW |S )aJ  
        Return a margin box with resolved percentages, but that may still
        have 'auto' values.

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

        :param at_keyword: which margin box to return, eg. '@top-left'
        :param containing_block: as expected by :func:`resolve_percentages`.

        Nnormalnoner   toprightbottomleft)rC   rD   )rE   rF   rG   rH   )	style_for	page_typestyleZinherit_fromr   Z	MarginBoxcontentr@   r   Zcontent_to_boxesZget_image_from_uricopy_with_childrenZinline_in_blockZprocess_whitespacer   r0   r*   Z_reset_spacing)
at_keywordcontaining_blockrK   r)   Zquote_depthchildrenZside)r(   counter_valuespager   r   make_box  s&    







z#make_margin_boxes.<locals>.make_boxrE   Fr   rG   rH   TrF   middlecenterc                s   g | ]}d |f  qS )z@%s-%sr   )r5   suffix)rO   rS   prefixr   r   r?   `  s   z%make_margin_boxes.<locals>.<listcomp>c             s   s   | ]}|j V  qd S )N)r@   )r5   r)   r   r   r   r7   b  s    z$make_margin_boxes.<locals>.<genexpr>g      ?r   z@top-left-cornerz@top-right-cornerz@bottom-left-cornerz@bottom-right-cornerN)rE   rH   )r+   r,   r1   r2   Zborder_widthZborder_heightanyrB   zipr@   
position_x
position_ymargin_heightZmargin_widthr=   appendmargin_box_content_layout)r(   rR   rQ   r+   r,   r1   r2   Zmax_box_widthZmax_box_heightZ
page_end_xZ
page_end_yZgenerated_boxesr;   rZ   r[   suffixesZfixed_outerZvariable_outerrA   r)   offsetrN   cb_width	cb_heightr   )rO   r(   rQ   rS   rR   rW   r   make_margin_boxes  s|    %









rc   c          
   C   s   t | |tdd|jjdg g d\}}}}}|dks6t|jj}|d
kr|jr|jd }|jd }|j}	|j|j  }
|
|	 }|j	| }|dkr|d	 }x|jD ]}|j
d| qW |S )u<   Layout a margin box’s content once the box has dimensions.infNT)Zmax_position_yZ
skip_stackdevice_sizepage_is_emptyZabsolute_boxesfixed_boxesrT   rG   r   r   r   )rT   rG   )r
   floatrK   sizer:   vertical_alignrP   r[   r\   r*   	translate)r(   rR   r)   	resume_at	next_page_rk   Zfirst_childZ
last_childrE   rG   Zcontent_heightr`   childr   r   r   r^     s&    


r^   c             C   s   || j  }| jdkrH| jdkr$d| _| jdkr4d| _|| j | j | _nn| j| j  ko^dkn  r||| j d  | _| _n:| jdkr|| j | j | _n| jdkr|| j | 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."

    http://dev.w3.org/csswg/css3-page/#page-box-page-rule
    http://www.w3.org/TR/CSS21/visudet.html#blockwidth

    r   r   r   N)r   r   r   r   r.   )r)   Zcontaining_block_size	remainingr   r   r   page_width_or_height  s    





rr   c             C   s   t t|| | d S )N)rr   r/   )r)   r(   Zcontaining_block_widthr   r   r   
page_width  s    rs   c             C   s   t t|| | d S )N)rr   r'   )r)   r(   Zcontaining_block_heightr   r   r   page_height  s    rt   Nc             C   s  | j |}|j|_tj||}|jj}t|| d|_d|_	|\}	}
t
|| |	 t|| |
 |j |_|j |_	|j	|j }|}|r|}|jg }t|tjst| j  d}g }g }t| |||||||||
\}}}}}|stdd |D |_x|D ]}t| ||| qW | j| |j|g}|j }xR|D ]J}|jj}|r2|dkr2x*|D ]"}|\}}| j| | j| qTW q2W |r|}|||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 page_number: integer, start at 1 for the first page
    :param resume_at: as returned by ``make_page()`` for the previous page,
                      or ``None`` for the first page.

    r   Tc             S   s    g | ]}|j jjd kr|j qS )Zfixed)Z_boxrK   position)r5   placeholderr   r   r   r?     s    zmake_page.<locals>.<listcomp>rD   )rI   Zviewport_overflowZoverflowr   ZPageBoxrK   rj   r   rZ   r[   rs   rt   Zcontent_box_xZcontent_box_yr*   rM   
isinstanceZBlockBoxr:   Zcreate_block_formatting_contextr	   rg   r   Zfinish_block_formatting_contextdescendants
string_setr]   )r(   root_boxrJ   rm   content_emptypage_numberrK   rR   re   ra   rb   Zpage_content_bottomZinitial_containing_blockZprevious_resume_atrf   Zadjoining_marginsZpositioned_boxesrn   ro   Zabsolute_boxrx   rp   Zstring_setsry   Zstring_nametextr   r   r   	make_page  sT    








 r~   c             c   s   d}|j j}|dkrd}|dkr&d}n|j jdk}d}d}d	}x~|d
7 }|dkrT|p`|dko`| }|rn|d7 }||rxdnd }	t| ||	|||\}
}}|st|
V  |dkrdS d}| }q@W dS )z5Return a list of laid out pages without margin boxes.Zfirst_rF   TrH   FZltrNrX   r   r   Zblank_
right_pageZ	left_page )rK   Zpage_break_before	directionr~   r:   )r(   rz   rW   Z
page_breakr   rm   rn   r|   r{   rJ   rR   r   r   r   make_all_pages  s4    
r   )N)!__doc__
__future__r   r   Zformatting_structurer   r   Zabsoluter   Zblocksr	   r
   Zpercentagesr   Z	preferredr   r   Zmin_maxr   r   objectr   r'   r/   r=   rB   rc   r^   rr   rs   rt   r~   r   r   r   r   r   <module>   s&   "Jav
E