3
6W                @   s0  d Z ddlmZmZ ddlZddlZddlmZmZm	Z	m
Z
 ddlmZ ddlmZ d	d
 Zdd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd  Zed!d" Zed#d$ Zed%d& Zed'd( Zed)d* Z ed+d, Z!ed-d. Z"ed/d0 Z#ed1d2 Z$ed3d4 Z%ed5d6 Z&ed7d8 Z'ed9d: Z(ed;d< Z)ed=d> Z*ed?d@ Z+edAdB Z,edCdD Z-edEdF Z.edGdH Z/edIdJ Z0edKdL Z1edMdN Z2edOdP Z3edQdR Z4edSdT Z5edUdV Z6edWdX Z7dS )Ya  
    weasyprint.tests.layout
    -----------------------

    Tests for layout, ie. positioning and dimensioning of boxes,
    line breaks, page breaks.

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

    )divisionunicode_literalsN   )FONTSassert_no_logscapture_logsalmost_equal   )boxes)render_pagesc             C   s2   | j \}|jdkst|j \}|jdks,t|j S )u4   Take a ``page``  and return its <body>’s children.htmlbody)childrenelement_tagAssertionError)pager   r    r   \/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/weasyprint/tests/test_layout.pybody_children   s
    r   c             C   s   | j | j| j | j fS )z>Return the (x, y, w, h) rectangle for the outer area of a box.)
position_x
position_ymargin_widthmargin_height)boxr   r   r   
outer_area"   s    r   c              C   s  t d} | d }t|tjs tt|j dks4tt|j dksHtt d\}|j dksbt|j dksrtt d\}|j d	kst|j d	kstt d
\}|j dkst|j dkstt d\}|j dkst|j dkstt d\}|j dkst|j dkstt d\}t|j dks>tt|j dksTtt d\}t|j dksttt|j dkstt d\}|j dkst|j dkst|jdkst|j	dkst|j
dkst|jdkst|j\}|jdkst|jdks t|j	dks0t|j
dks@t|j\}|jdksXt|jdksht|j	dksxt|jdkst|jdkst|jdkst|jdkst|j
dkst|j\}|jdkst|jdkst|j	dks t|j
dkstt d\}|j
d ks*t|jd!ks:t|j\}|jdksRt|jd"ksbt|j	d#ksrtt d$\}|jd%kst|jdkst|jd%kst|jdkstt d&\}|jd'kst|jd'kst|jd(kst|jd%kstt d)\}|j d*ks"t|j d*ks4tt d+\}|j d,ksPt|j d-ksbtt d.\}|j d-ks~t|j d,kstd/S )0z)Test the layout for ``@page`` properties.z<p>r   i  ib  z(<style>@page { size: 2in 10in; }</style>   i  z%<style>@page { size: 242px; }</style>   z&<style>@page { size: letter; }</style>i0  i   z/<style>@page { size: letter portrait; }</style>z0<style>@page { size: letter landscape; }</style>z(<style>@page { size: portrait; }</style>z)<style>@page { size: landscape; }</style>z
        <style>@page { size: 200px 300px; margin: 10px 10% 20% 1in }
               body { margin: 8px }
        </style>
        <p style="margin: 0">
       i,  T      r   `   
   r      D   ph      z
        <style>
            @page { size: 100px; margin: 1px 2px; padding: 4px 8px;
                    border-width: 16px 32px; border-style: solid }
        </style>
        <body>
       :   *      z<style>@page {
        size: 106px 206px; width: 80px; height: 170px;
        padding: 1px; border: 2px solid; margin: auto;
    }</style>   z<style>@page {
        size: 106px 206px; width: 80px; height: 170px;
        padding: 1px; border: 2px solid; margin: 5px 5px auto auto;
    }</style>      z<style>@page {
        size: 4px 10000px; width: 100px; height: 100px;
        padding: 1px; border: 2px solid; margin: 3px;
    }</style>p   zo<style>@page {
        size: 1000px; margin: 100px;
        max-width: 500px; min-height: 1500px;
    }</style>i  i  zo<style>@page {
        size: 1000px; margin: 100px;
        min-width: 1500px; max-height: 500px;
    }</style>N)parse
isinstancer
   ZPageBoxr   intr   r   r   r   widthheightr   r   margin_leftmargin_right
margin_topmargin_bottom)pagesr   r   r   	paragraphr   r   r   test_page_size(   s    






r:   c              C   sz  t d\} | j\}|jdks t|j\}|jdks6t|jdksDt|j}g }x|D ]}t|tjsht|jdksvt|jdkstxn|jD ]d}t|tjst|jdkst|jdkst|j	dkst|j
d	kst|jd	kst|j| qW qTW t|d
ks
t|d jdkst|d jdks2t|d jdksFt|d	 jdksZt|d	 jdksnt|d jdkst|d jdkst|d jdkst|d jdkst|d jdkst|d jdkst|d jdkst|d jdkst|d jdks"t|d jdks6t|d jdksJt|d jdks^t|d jdksrt|d jdkst|d jdkst|d jdkst|d jdkst|d jdkst|d jdkst|d jdkst|d jdkst|d jdks&t|d jdks:t|d jdksNt|d jd ksbt|d jdksvtd!S )"zTest the blocks widths.ai  
        <style>
            @page { margin: 0; size: 120px 2000px }
            body { margin: 0 }
            div { margin: 10px }
            p { padding: 2px; border-width: 1px; border-style: solid }
        </style>
        <div>
          <p></p>
          <p style="width: 50px"></p>
        </div>
        <div style="direction: rtl">
          <p style="width: 50px; direction: rtl"></p>
        </div>
        <div>
          <p style="margin: 0 10px 0 20px"></p>
          <p style="width: 50px; margin-left: 20px; margin-right: auto"></p>
          <p style="width: 50px; margin-left: auto; margin-right: 20px"></p>
          <p style="width: 50px; margin: auto"></p>

          <p style="margin-left: 20px; margin-right: auto"></p>
          <p style="margin-left: auto; margin-right: 20px"></p>
          <p style="margin: auto"></p>

          <p style="width: 200px; margin: auto"></p>

          <p style="min-width: 200px; margin: auto"></p>
          <p style="max-width: 50px; margin: auto"></p>
          <p style="min-width: 50px; margin: auto"></p>

          <p style="width: 70%"></p>
        </div>
    r   r   x   divd   r$   r	   r   r+   r   ^   2      @         r,               J   r"   	   r!   r               F   N)r/   r   r   r   r2   r0   r
   ZBlockBoxZpadding_leftZpadding_rightZborder_left_widthZborder_right_widthappendlenr4   r5   )r   r   r   divsZ
paragraphsr<   r9   r   r   r   test_block_widths   sj     
rR   c           	   C   s   t d\} dd t| D }|ddddddddgks8tt d\} d	d t| D }|d
dddddgksltt d\} dd t| D }|d
d
dd
ddgkstdS )zTest the blocks heights.u  
        <style>
            @page { margin: 0; size: 100px 20000px }
            html, body { margin: 0 }
            div { margin: 4px; border-width: 2px; border-style: solid;
                  padding: 4px }
            /* Only use top margins so that margin collapsing does not change
               the result: */
            p { margin: 16px 0 0; border-width: 4px; border-style: solid;
                padding: 8px; height: 50px }
        </style>
        <div>
          <p></p>
          <!-- These two are not in normal flow: the do not contribute to
            the parent’s height. -->
          <p style="position: absolute"></p>
          <p style="float: left"></p>
        </div>
        <div>
          <p></p>
          <p></p>
          <p></p>
        </div>
        <div style="height: 20px">
          <p></p>
        </div>
        <div style="height: 120px">
          <p></p>
        </div>
        <div style="max-height: 20px">
          <p></p>
        </div>
        <div style="min-height: 120px">
          <p></p>
        </div>
        <div style="min-height: 20px">
          <p></p>
        </div>
        <div style="max-height: 120px">
          <p></p>
        </div>
    c             S   s   g | ]
}|j qS r   )r3   ).0r<   r   r   r   
<listcomp>Q  s    z&test_block_heights.<locals>.<listcomp>Z   r@   rB   r;   a>  
        <style>
            body { height: 200px; font-size: 0; }
        </style>
        <div>
          <img src=pattern.png style="height: 40px">
        </div>
        <div style="height: 10%">
          <img src=pattern.png style="height: 40px">
        </div>
        <div style="max-height: 20px">
          <img src=pattern.png style="height: 40px">
        </div>
        <div style="max-height: 10%">
          <img src=pattern.png style="height: 40px">
        </div>
        <div style="min-height: 20px"></div>
        <div style="min-height: 10%"></div>
    c             S   s   g | ]
}|j qS r   )r3   )rS   r<   r   r   r   rT   g  s    (   a/  
        <style>
            body { font-size: 0; }
        </style>
        <div>
          <img src=pattern.png style="height: 40px">
        </div>
        <div style="height: 10%">
          <img src=pattern.png style="height: 40px">
        </div>
        <div style="max-height: 20px">
          <img src=pattern.png style="height: 40px">
        </div>
        <div style="max-height: 10%">
          <img src=pattern.png style="height: 40px">
        </div>
        <div style="min-height: 20px"></div>
        <div style="min-height: 10%"></div>
    c             S   s   g | ]
}|j qS r   )r3   )rS   r<   r   r   r   rT   ~  s    r   Ni  )r/   r   r   )r   Zheightsr   r   r   test_block_heights$  s    )rW   c              C   s   t d\} | j\}|jdks t|j\}|jdks6t|jdksDtt d\} | j\}|jdksdt|j\}|jdkszt|jdkstdS )z(Test the blocks heights set in percents.z{
        <style>
            html, body { margin: 0 }
            body { height: 50% }
        </style>
        <body>
    r   r   r   z
        <style>
            html, body { margin: 0 }
            html { height: 300px }
            body { height: 50% }
        </style>
        <body>
       N)r/   r   r   r   r3   )r   r   r   r   r   r   test_block_percentage_heights  s    rY   c              C   s  t d\} | j\}|jdks t|j\}|jdks6t|jdksDt|j\}}}}|j\}}}	}}
}}}|jdksvt|jdkst|	jdkstd|	j  k odk n  st|
jdkst|
jdkst|
j dkst|
jdkst|jdkst|jdkst|j d	kst|j\}|jdks2tt|jd
ksFt|jdksVt|j\}}}}|jdkstt|jdkst|j dkst|jdkst|j dkst|jdkst|jdkst|j\}|j\}}|jdkst|jdks
t|jdkst|jdks*t|j\}|jdksBt|jdksRt|j\}}}|jdksnt|jdks~tt d\} | j\}|j\}|j\}|j\}|j}t|d
kst|d j\}|j	dkstdS )z%Test the inline-block elements sizes.a5  
        <style>
            @page { margin: 0; size: 200px 2000px }
            body { margin: 0 }
            div { display: inline-block; }
        </style>
        <div> </div>
        <div>a</div>
        <div style="margin: 10px; height: 100px"></div>
        <div style="margin-left: 10px; margin-top: -50px;
                    padding-right: 20px;"></div>
        <div>
            Ipsum dolor sit amet,
            consectetur adipiscing elit.
            Sed sollicitudin nibh
            et turpis molestie tristique.
        </div>
        <div style="width: 100px; height: 100px;
                    padding-left: 10px; margin-right: 10px;
                    margin-top: -10px; margin-bottom: 50px"></div>
        <div style="font-size: 0">
          <div style="min-width: 10px; height: 10px"></div>
          <div style="width: 10%">
            <div style="width: 10px; height: 10px"></div>
          </div>
        </div>
        <div style="min-width: 185px">foo</div>
        <div style="max-width: 10px
          ">Supercalifragilisticexpialidocious</div>r   r   r   r<   r   rB   r=      r   r;      r!   r	      uL   
        <p style="display: inline-block">Lorem ipsum dolor sit amet …</p>u   Lorem ipsum dolor sit amet …N)
r/   r   r   r   r2   r   r3   rP   r   text)r   r   r   line_1line_2line_3Zline_4div_1_div_2div_3div_4Zdiv_5Zdiv_6Zdiv_7Z
child_lineZchild_div_1Zchild_div_2Z
grandchildZdiv_8Zdiv_9Z
outer_liner9   Zinner_linestext_boxr   r   r   test_inline_block_sizes  sl    
rg   c              C   s   t d\} t| \}|j\}|j\}|j\}}|jdks<t|jdksJt|jdksXt|jdksftt d\} t| \}|j\}|j}|jj	}|jd| kst|j
|j |j |j kst|j|jkst|jdkst|j\}|j\}|jdkstd	S )
zTest the lists.z
        <style>
            body { margin: 0 }
            ul { margin-left: 50px; list-style: inside circle }
        </style>
        <ul>
          <li>abc</li>
        </ul>
    u   ◦r   r"   abcz
        <style>
            body { margin: 0 }
            ul { margin-left: 50px; }
        </style>
        <ul>
          <li>abc</li>
        </ul>
    g      ?u   •N)r/   r   r   r]   r   r4   r5   Zoutside_list_markerstyle	font_sizer   Zpadding_box_xr2   r   )r   Zunordered_listZ	list_itemlinemarkercontentrj   r   r   r   
test_lists  s0    


rn   c              C   sf   t d\} t| \}t|jdks&t|jdks4tt d\} t| \}tj  t|jdksbtdS )z@Test lineboxes with no content other than space-like characters.z<p> </p>r   zY
        <style>
            p { width: 1px }
        </style>
        <p><br>  </p>
    r   N)r/   r   rP   r   r   r3   pytestZxfail)r   r9   r   r   r   test_empty_lineboxC  s    


rp   c              C   sF  t ddti \} | j\}|j\}|j\}tt|jdks@t|j}x|D ]}|jjdks`t|jdksntxf|jD ]\}|jdkst|jjdkstt	|t
jrvx,|jD ]"}|jdkst|jjdkstqW qvW qLW t d	\} | j\}|j\}|j\}|j}g }	x"|D ]}|j\}
|	j|
j qW |	d
ddddgksBtdS )z:Test lineboxes breaks with a lot of text and deep nesting.a  
        <style>
        p { font-size: 13px;
            width: 300px;
            font-family: %(fonts)s;
            background-color: #393939;
            color: #FFFFFF;
            line-height: 1;
            text-decoration: underline overline line-through;}
        </style>
        <p><em>Lorem<strong> Ipsum <span>is very</span>simply</strong><em>
        dummy</em>text of the printing and. naaaa </em> naaaa naaaa naaaa
        naaaa naaaa naaaa naaaa naaaa</p>
    fontsr@   rL   r$   emstrongspanu   <pre>a
bc
d e</pre>abcdeN)rr   r$   )rr   rs   rt   )r/   r   r   rP   listr   ri   rj   r   r0   r
   Z	ParentBoxrO   r]   )r   r   r   r9   linesrk   childZchild_childpretextsrf   r   r   r   test_breaking_lineboxX  s6    


r   c              C   sZ   t ddti \} t| \}t|j}t|dks6tdjdd |D }|dksVtdS )	z Test the creation of line boxes.z
        <style>
            p { width: 165px; font-family:%(fonts)s;}
        </style>
        <p><em>Lorem Ipsum</em>is very <strong>coool</strong></p>
    rq   r	    c             s   s&   | ]}d j dd |j D V  qdS ) c             s   s    | ]}t |tjr|jV  qd S )N)r0   r
   TextBoxr]   )rS   r   r   r   r   	<genexpr>  s    z.test_linebox_text.<locals>.<genexpr>.<genexpr>N)joindescendants)rS   rk   r   r   r   r     s   z$test_linebox_text.<locals>.<genexpr>zLorem Ipsumis very cooolN)r/   r   r   rz   r   rP   r   r   )r   r9   r{   r]   r   r   r   test_linebox_text  s    


r   c        	      C   s   xdD ]\} }d}t |t| d \}t|\}t|j}t||ksJt|d j}|d j}x|D ]z}||jksvt||jkstx4|jD ]*}||jkst||j	7 }||jkstqW ||j |j	kst|j}||j
7 }qdW qW dS )z Test the position of line boxes.   r	   r   r,   r   z
            <style>
                p { width:%(width)spx; font-family:%(fonts)s;
                    line-height: 20px }
            </style>
            <p>this is test for <strong>Weasyprint</strong></p>)rq   r2   Nr   r	   r   r,   r   r,   )r   r   r   )r/   r   r   rz   r   rP   r   r   r   r2   r3   )	r2   Zexpected_linesr   r9   r{   Zref_position_yZref_position_xrk   r   r   r   r   test_linebox_positions  s$    





r   c              C   s   t d\} t| \}|jdks"t|j}tdd |D s>tt|dksNtdd |D dgd ksjtt d	\} t| \}|jd
kst|j}tdd |D stt|dkstdd |D dgd kstdS )zTest <pre> and <br>.z
        <style> pre { line-height: 42px }</style>
        <pre>Lorem ipsum dolor sit amet,
            consectetur adipiscing elit.


            Sed sollicitudin nibh

            et turpis molestie tristique.</pre>
    r}   c             s   s   | ]}t |tjV  qd S )N)r0   r
   LineBox)rS   rk   r   r   r   r     s    z*test_forced_line_breaks.<locals>.<genexpr>rG   c             S   s   g | ]
}|j qS r   )r3   )rS   rk   r   r   r   rT     s    z+test_forced_line_breaks.<locals>.<listcomp>r)   z
        <style> p { line-height: 42px }</style>
        <p>Lorem ipsum dolor sit amet,<br>
            consectetur adipiscing elit.<br><br><br>
            Sed sollicitudin nibh<br>
            <br>

            et turpis molestie tristique.</p>
    r$   c             s   s   | ]}t |tjV  qd S )N)r0   r
   r   )rS   rk   r   r   r   r     s    c             S   s   g | ]
}|j qS r   )r3   )rS   rk   r   r   r   rT     s    N)r/   r   r   r   r   allrP   )r   r}   r{   r   r   r   test_forced_line_breaks  s     	

r   c        "      C   sT  t d} g }xL| D ]D}t|}tdd |D s4ttdd |D sJt|j| ~qW dd |D }|ddgddgdggkstt d} g }xL| D ]D}t|}td	d |D sttd
d |D st|j| ~qW dd |D }|ddgddgdggks
tt d} g }xR| D ]J}t|}tdd |D s@ttdd |D sXt|j| ~qW dd |D }|ddgddgdggkstt d\}}}	}
|jdkst|jdkst|j\}|j\}|j\}|j\}|j\}|jdkst|j	dkst|j\}|jdks&t|jdks6t|j sDt|	jdksTt|	jdksdt|	j\}|j\}|j\}}|jdkst|jdkst|
jdkst|
jdkst|
j\}|j\}|j\}|j\}|j\}|jdkstt d\}}|j\}|j\}|j\}}|j
dks,t|jdks<t|j
dksLt|jdks\t|j\}|j\}|j\}|j
dkst|jdkstt d\}}|j\}|j\}|j\}|j
dkst|jdkst|j\}|j\}|j\}}|j
dkst|jdkst|j
dks"t|jdks2tt d\}}|j\}|j\}|j\}|j
dksft|jdksvt|j\}|j\}|j\}|j\}}|j
dkst|jdkst|j
dkst|jdkstt d\}}}	|j\}|j\}|j\}|jdkst|j\}|j\}|j\}}|jdks8t|jdksHt|	j\}|j\}|j\}|jdksptt d\}}}	|j\}|j\}|j\}|jdkst|j\}|j\}|j\}|j\}|j\}|jdkst|	j\}|j\}|j\}}}|jdks
t|jdkst|jdks*tt d \}}}	t|jd!ksLtt|jd"ks`tt|	jd!ksttt d#\}}|j\}|j\}}|j\}}|j\}|j
dkst|j
d$kst|jd$kst|jd$kst|j\}|j\}|j\}}|j\}|j\} }!|j
dks t|j
d$ks0t|jd$ks@t|jd$ksPtd%S )&zTest the page breaks.a  
        <style>
            @page { size: 100px; margin: 10px }
            body { margin: 0 }
            div { height: 30px; font-size: 20px; }
        </style>
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
    c             S   s   g | ]}|j d kqS )r<   )r   )rS   r<   r   r   r   rT     s    z$test_page_breaks.<locals>.<listcomp>c             S   s   g | ]}|j d kqS )r!   )r   )rS   r<   r   r   r   rT     s    c             S   s   g | ]}d d |D qS )c             S   s   g | ]
}|j qS r   )r   )rS   r<   r   r   r   rT     s    z/test_page_breaks.<locals>.<listcomp>.<listcomp>r   )rS   rQ   r   r   r   rT     s    r!   rV   z
        <style>
            @page { size: 100px; margin: 10px }
            body { margin: 0 }
            div { height: 30px }
        </style>
        <div></div><div></div><div></div><div></div><div></div>
    c             S   s   g | ]}|j d kqS )r<   )r   )rS   r<   r   r   r   rT     s    c             S   s   g | ]}|j d kqS )r!   )r   )rS   r<   r   r   r   rT     s    c             S   s   g | ]}d d |D qS )c             S   s   g | ]
}|j qS r   )r   )rS   r<   r   r   r   rT     s    z/test_page_breaks.<locals>.<listcomp>.<listcomp>r   )rS   rQ   r   r   r   rT     s    a@  
        <style>
            @page { size: 100px; margin: 10px }
            img { height: 30px; display: block }
        </style>
        <body>
            <img src=pattern.png>
            <img src=pattern.png>
            <img src=pattern.png>
            <img src=pattern.png>
            <img src=pattern.png>
    c             S   s   g | ]}|j d kqS )img)r   )rS   r   r   r   r   rT     s    c             S   s   g | ]}|j d kqS )r!   )r   )rS   r   r   r   r   rT     s    c             S   s   g | ]}d d |D qS )c             S   s   g | ]
}|j qS r   )r   )rS   r   r   r   r   rT   !  s    z/test_page_breaks.<locals>.<listcomp>.<listcomp>r   )rS   imagesr   r   r   rT   !  s   a  
        <style>
            @page { margin: 10px }
            @page :left { margin-left: 50px }
            @page :right { margin-right: 50px }

            html { page-break-before: left }
            div { page-break-after: left }
            ul { page-break-before: always }
        </style>
        <div>1</div>
        <p>2</p>
        <p>3</p>
        <article>
            <section>
                <ul><li>4</li></ul>
            </section>
        </article>
    r?   r<   1r$   Zula  
        <style>
            @page { size: 140px; margin: 0 }
            img { height: 25px; vertical-align: top }
            p { orphans: 1; widows: 1 }
        </style>
        <body>
            <img src=pattern.png>
            <div>
                <p><img src=pattern.png><br/><img src=pattern.png><p>
                <p><img src=pattern.png><br/><img src=pattern.png><p>
            </div><!-- page break here -->
            <img src=pattern.png>
    r   r-   r=   az  
        <style>
            @page { size: 140px; margin: 0 }
            img { height: 25px; vertical-align: top }
            p { orphans: 1; widows: 1 }
        </style>
        <body>
            <img src=pattern.png><!-- page break here -->
            <div>
                <p style="page-break-inside: avoid">
                    ><img src=pattern.png><br/><img src=pattern.png></p>
                <p style="page-break-before: avoid; page-break-after: avoid;
                          widows: 2"
                    ><img src=pattern.png><br/><img src=pattern.png></p>
            </div>
            <img src=pattern.png>
    a  
        <style>
            @page { size: 140px; margin: 0 }
            img { height: 25px; vertical-align: top }
            p { orphans: 1; widows: 1 }
        </style>
        <body>
            <img src=pattern.png><!-- page break here -->
            <div>
                <div>
                    <p style="page-break-inside: avoid">
                        ><img src=pattern.png><br/><img src=pattern.png></p>
                    <p style="page-break-before: avoid;
                              page-break-after: avoid;
                              widows: 2"
                        ><img src=pattern.png><br/><img src=pattern.png></p>
                </div>
                <img src=pattern.png>
            </div>
    a  
        <style>
            @page { size: 100px; margin: 0 }
            img { height: 30px; display: block; }
            p { orphans: 1; widows: 1 }
        </style>
        <body>
            <div>
                <img src=pattern.png style="page-break-after: always">
                <section>
                    <img src=pattern.png>
                    <img src=pattern.png>
                </section>
            </div>
            <img src=pattern.png><!-- page break here -->
            <img src=pattern.png>
    rZ   <   aM  
        <style>
            @page { size: 100px; margin: 0 }
            img { height: 30px; display: block; }
            p { orphans: 1; widows: 1 }
        </style>
        <body>
            <div>
                <img src=pattern.png style="page-break-after: always">
                <section>
                    <img src=pattern.png><!-- page break here -->
                    <img src=pattern.png style="page-break-after: avoid">
                </section>
            </div>
            <img src=pattern.png style="page-break-after: avoid">
            <img src=pattern.png>
    a!  
        <style>
            @page {
                @bottom-center { content: counter(page) }
            }
            @page:blank {
                @bottom-center { content: none }
            }
        </style>
        <p style="page-break-after: right">foo</p>
        <p>bar</p>
    r	   r   a  
        <style>
          @page { size: 75px; margin: 0 }
          div { height: 20px }
        </style>
        <body>
          <div></div>
          <section>
            <div></div>
            <div style="page-break-after: avoid">
              <div style="position: absolute"></div>
              <div style="position: fixed"></div>
            </div>
          </section>
          <div></div>
    rB   N)r/   r   r   r   rO   r4   r5   r   r   r]   r   r3   rP   )"r8   	page_divsr   rQ   positions_ypage_imagesr   page_1page_2page_3Zpage_4r   r   r<   rk   r]   p_1p_2ZarticlesectionZulistimg_1img_2Z	outer_divZ	inner_divimg_4img_5Z_divra   rc   re   rd   ZabsoluteZfixedr   r   r   test_page_breaks  sH   


















r   c              C   sp   dd } | dddgkst | dddgks0t | d	ddgksDt | d
ddgksXt | dddgkslt dS )z Test orphans and widows control.c       	      S   s~   t d|  }g }xht|D ]\\}}|j\}|j\}|dkrJ|jdd  }n|j}|rl|\}|jt|j q|jd qW |S )Na  
            <style>
                @page { size: 200px }
                h1 { height: 120px }
                p { line-height: 20px;
                    width: 1px; /* line break at each word */
                    %s }
            </style>
            <h1>Tasty test</h1>
            <!-- There is room for 4 lines after h1 on the fist page -->
            <p>
                one
                two
                three
                four
                five
                six
                seven
            </p>
        r   r   )r/   	enumerater   rO   rP   )	cssr8   Zline_countsir   r   r   r   r9   r   r   r   line_distribution9  s    
z4test_orphans_widows_avoid.<locals>.line_distributionzorphans: 2; widows: 2rC   r@   zorphans: 5; widows: 2r   rG   zorphans: 2; widows: 4zorphans: 4; widows: 4z/orphans: 2; widows: 2; page-break-inside: avoidN)r   )r   r   r   r   test_orphans_widows_avoid6  s    $r   c        
      C   s   xdD ]} t dt| d \}|j\}|j\}|j\}|j}| dkrVt|dksftnt|dksftg }x(|D ] }|j\}|j\}	|j|	j qpW dj|d	kstqW d
S )zTest the inline boxes spliting.'  r=   r!   r   z
            <style>p { font-family:%(fonts)s; width: %(width)spx; }</style>
            <p><strong>WeasyPrint is a free software visual rendering engine
                       for HTML and CSS.</strong></p>
        )rq   r2   r   r   zGWeasyPrint is a free software visual rendering engine for HTML and CSS.N)r   r=   r!   r   )r/   r   r   rP   r   rO   r]   r   )
r2   r   r   r   r9   r{   Z
text_partsrk   rs   r]   r   r   r   test_inlinebox_splitingf  s     

r   c        
      C   s   t ddti } g }xv| D ]n}|j\}|j\}|j\}|j}xJ|D ]B}g }x(|j D ]}	t|	tjrT|j|	j qTW |jdj	| qBW qW t
| dkstdj	|dkstdS )z6Test the linebox text after spliting linebox and page.a
  
        <style>
            div { font-family:%(fonts)s; font-size:22px}
            @page { size: 100px; margin:2px; border:1px solid }
            body { margin: 0 }
        </style>
        <div><span/>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15</div>
    rq   r   r	   r   z11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15N)r/   r   r   r   r0   r
   r   rO   r]   r   rP   r   )
r8   r~   r   r   r   r<   r{   rk   Z
line_textsr|   r   r   r   test_page_and_linebox_breaking  s"    

r   c              C   s   xdD ]} t d|  \}|j\}|j\}|j\}|j\}|j\}|j\}|jdks`td| f t d| jdd	 \}|j\}|j\}|j\}|j\}}	}
|
j\}|j\}|jdkstd| f qW d
S )z/Test various spaces and tabulations processing.ru     a   
  	a a	 z<p><em>%s</em></p>zsource was %rz4<p style="white-space: pre-line">

<em>%s</em></pre>
r   N)ru   r   r   r   )r/   r   r]   r   replace)sourcer   r   r   r$   rk   rr   r]   Z_line1Z_line2Zline3r   r   r   test_whitespace_processing  s$    
r   c              C   sP  dd } xRdd dGD ddddddddg D ],}| |\}}|j dksJt|jdks,tq,W d}| d| \}}|j dks~t|jdkstxzdHD ]r}t }| d$| \}}W d%Q R X t|d&kstd'|d( kstt|tj st|j\}|j	d)kst|qW xBdID ]:}t }| d.| \}}W d%Q R X t|d(kstqW t }t
d/ W d%Q R X t|d&ksxtd'|d( kst| d0\}}|jd1kst|jd(kst|j d1kst|jd1kst| d2\}}|jd1kst|jd(kst|j d1kst|jd1ks"t| d3\}}|jd1ks>t|jd(ksNt|j d1ks^t|jd1ksnt| d4\}}|jd1kst|jd(kst|j d1kst|jd1kst| d5\}}|j d6kst|jd6kstt
d7\}|j\}|j\}|j\}|j\}	}
|jd8ks"t|	j d1ks2t|	jd1ksBt|
j d8ksRt|
jd8ksbt|	jd9ksrt|
jd(kstt
d:\}|j\}|j\}|j\}|jd;kst|jd(kst|jd(kst|j d1kst|jd1kst|j d<kst|j d=kstt
d>\}|j\}|j\}|j\}|jd;ksJt|jd(ksZt|jd(ksjt|j d1kszt|jd1kst|j d<kst|j d=kstt
d?\}|j\}|j\}|j\}|jd;kst|jd(kst|jd(ks t|j d1kst|jd1ks t|j d<ks2t|j d=ksDtt
d@\}|j\}|j\}|j\}|jd;ksvt|jd(kst|jd(kst|j d6kst|jd<kst|j dAkst|j d=kstt
dB\}|j\}|j\}|j\}|j dCkst|jdDkst|jd;ks,t|j dEks<t|jdFksLtd%S )Jz:Test that width, height and ratio of images are respected.c             S   s2   t | \}|j\} | j\}|j\}|j\}||fS )N)r/   r   )r   r   r   rk   r   r   r   r   get_img  s    
ztest_images.<locals>.get_imgc             S   s   g | ]}d | qS )z<img src="%s">r   )rS   urlr   r   r   rT     s    ztest_images.<locals>.<listcomp>pattern.pngpattern.gifblue.jpgpattern.svg3data:image/svg+xml,<svg width='4' height='4'></svg>7DatA:image/svg+xml,<svg width='4px' height='4px'></svg>z<embed src=pattern.png>z<embed src=pattern.svg>z+<embed src=really-a-png.svg type=image/png>z/<embed src=really-a-svg.png type=image/svg+xml>z<object data=pattern.png>z<object data=pattern.svg>z-<object data=really-a-png.svg type=image/png>z1<object data=really-a-svg.png type=image/svg+xml>rC   z<data:image/svg+xml,<svg width='2.54cm' height='0.5in'></svg>z<img src="%s">r    0   nonexistent.png(unknownprotocol://weasyprint.org/foo.png#data:image/unknowntype,Not an image4   datå:image/svg+xml,<svg width="4" height="4"></svg>data:image/png,data:image/jpeg,data:image/svg+xml,data:image/png,Not a PNGdata:image/jpeg,Not a JPEG#data:image/svg+xml,<svg>invalid xmlJdata:image/svg+xml;base64,R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=really-a-png.svgz"<img src='%s' alt='invalid image'>Nr   zWARNING: Failed to load imager   zinvalid imageGdata:image/jpeg;base64,R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=Fdata:image/png;base64,R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=data:image/png;<svg></svg>really-a-svg.pngz<img src='%s'>z2<img src=nonexistent.png><img src=nonexistent.png>zO<body style="font-size: 0">
        <img src="pattern.png" style="width: 40px">rV   zP<body style="font-size: 0">
        <img src="pattern.png" style="height: 40px">zf<body style="font-size: 0"><p style="width: 200px">
        <img src="pattern.png" style="width: 20%">zS<body style="font-size: 0">
        <img src="pattern.png" style="min-width: 40px">z.<img src="pattern.png" style="max-width: 2px">r	   z<body style="font-size: 0">
        <img src="pattern.png" style="width: 40px">
        <img src="pattern.png" style="width: 60px; display: table-cell">
    r   rB   z
        <style>
            @page { size: 100px }
            img { width: 40px; margin: 10px auto; display: block }
        </style>
        <body>
            <img src="pattern.png">
    r   rZ   r!   z
        <style>
            @page { size: 100px }
            img { min-width: 40%; margin: 10px auto; display: block }
        </style>
        <body>
            <img src="pattern.png">
    z
        <style>
            @page { size: 100px }
            img { min-width: 40px; margin: 10px auto; display: block }
        </style>
        <body>
            <img src="pattern.png">
    z
        <style>
            @page { size: 100px }
            img { min-height: 30px; max-width: 2px;
                  margin: 10px auto; display: block }
        </style>
        <body>
            <img src="pattern.png">
    1   z
        <body style="float: left">
        <img style="height: 200px; margin: 10px; display: block" src="
            data:image/svg+xml,
            <svg width='150' height='100'></svg>
        ">
    i@     i,  r   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   )r2   r   r3   r   rP   r0   r
   Z	InlineBoxr   r]   r/   r   r   r   Zcontent_box_xcontent_box_y)r   r   r   r   r   Zlogsr]   r   rk   r   r   r   r   r   test_images  s.   	                




r   c              C   s  t d\} | j\}|j\}|j\}|j\}|j\}}|jdksBt|jdksPt|jdks^t|jdksltd|j  k odk n  st|j|jkstt d\} | j\}|j\}|j\}|j\}|j\}}|jdkst|jdkst|jdkst|jdkst|jd	kst|j|jks*tt d
\} | j\}|j\}|j\}|j\}|j\}}|jdksnt|jdks~t|jdkst|jdkst|jd	kst|j|jkstt d\} | j\}|j\}|j\}|j\}|j\}}	}|j\}|jdkst|jdkst|jdks.t|jdks>t|jd	ksNt|j|jks`tt d\} | j\}|j\}|j\}|j\}|j\}}|jdkst|jdkst|jdkst|jdkst|jdkst|j|jkstt d\} | j\}|j\}|j\}|j\}|j\}}}
|jdks<t|jdksLt|
jdks\t|jdkslt|jdks|t|
jdkst|jdkst|j|jkstt d\} | j\}|j\}|j\}|j\}|j\}}|jdkst|jdkst|jdkst|jdks"t|jdks2t|j|jksDtt d\} | j\}|j\}|j\}|j\}|j\}|jdkst|jdkst|j dkst|j dkst|jdkstt d\} | j\}|j\}|j\}|j\}}|jdkst|j|jkst|j\}}|jdks2t|jdksBt|jdksRt|jdksbt|j\}|j\}}|j\}|j\}}|jdkst|jdkst|jdkst|jd	kst|jdkst|jdkst|jdkst|jdkstt d\} | j\}|j\}|j\}|j\}|j\}}}}|j\}
}|j\}|j\}|j|jksjt|jdkszt|jdkst|jd kst|
jd!kst|jd kst|jd"kstt d#\} | j\}|j\}|j\}|j\}|j\}}|jd$kst|jdkstt d%\} | j\}|j\}|j\}|j\}|j\}|jdks`tt d&\} | j\}|j\}|j\}|j\}|j\}|j\}|jd'kst|jdkstd(S ))z&Test various values of vertical-align.z
        <span>
            <img src="pattern.png" style="width: 40px"
            ><img src="pattern.png" style="width: 60px"
        ></span>rV   r   rB   r   rN   z
        <span>
            <img src="pattern.png" style="width: 40px; vertical-align: -15px"
            ><img src="pattern.png" style="width: 60px"></span>#   K   z
        <span style="line-height: 10px">
            <img src="pattern.png" style="width: 40px; vertical-align: -150%"
            ><img src="pattern.png" style="width: 60px"></span>z
        <span style="line-height: 10px">
            <span style="line-height: 10px; vertical-align: -15px">
                <img src="pattern.png" style="width: 40px"></span>
            <img src="pattern.png" style="width: 60px"></span>z
        <span style="line-height: 12px; font-size: 12px; font-family: 'ahem'">
            <img src="pattern.png" style="width: 40px; vertical-align: middle"
            ><img src="pattern.png" style="width: 60px"></span>gA@gR@a  
        <span style="line-height: 10px">
            <img src="pattern.png" style="width: 60px"
            ><img src="pattern.png" style="width: 40px; vertical-align: super"
            ><img src="pattern.png" style="width: 40px; vertical-align: sub"
        ></span>rK      r#   z
        <body style="line-height: 10px">
            <span>
                <img src="pattern.png" style="vertical-align: text-top"
                ><img src="pattern.png" style="vertical-align: text-bottom"
            ></span>rC   r'   zR<span style="line-height: 1.5">
         <span style="padding: 1px"></span></span>rD   a$  
        <span>
            <img src="pattern.png" style="width: 40px; vertical-align: -15px"
            ><img src="pattern.png" style="width: 60px"
        ></span><div style="display: inline-block; vertical-align: 3px">
            <div>
                <div style="height: 100px">foo</div>
                <div>
                    <img src="pattern.png" style="
                        width: 40px; vertical-align: -15px"
                    ><img src="pattern.png" style="width: 60px"
                ></div>
            </div>
        </div>      g      r=      ab  
        <span style="font-size: 0">
            <img src="pattern.png" style="vertical-align: 26px">
            <img src="pattern.png" style="vertical-align: -10px">
            <span style="vertical-align: top">
                <img src="pattern.png" style="vertical-align: -10px">
                <span style="vertical-align: -10px">
                    <img src="pattern.png" style="vertical-align: bottom">
                </span>
            </span>
            <span style="vertical-align: bottom">
                <img src="pattern.png" style="vertical-align: 6px">
            </span>
        </span>$   rE   rZ   z
        <span style="font-size: 0">
            <img src="pattern.png" style="vertical-align: bottom">
            <img src="pattern.png" style="vertical-align: top; height: 100px">
        </span>
    r    zr
        <span style="font-size: 0; vertical-align: top">
            <img src="pattern.png">
        </span>
    z
        <span style="font-size: 0; vertical-align: top; display: inline-block">
            <img src="pattern.png">
        </span>r   N)r/   r   r3   r   r   r   r   )r   r   r   rk   rt   r   r   span_1span_2_whitespaceimg_3ra   rc   rd   re   Zdiv_lineZ	div_img_1Z	div_img_2Zspan_4Zspan_3r   r   r^   r_   r   r   r   test_vertical_align  s^   











r   c              C   sf   t d\} | j\}|j\}|j\}|j\}|jdks8t|jdksFt|jdksTt|jdksbtdS )z=Test that auto margins are ignored for inline replaced boxes.z
        <style>
            @page { size: 200px }
            img { display: inline; margin: auto; width: 50px }
        </style>
        <body><img src="pattern.png" />r   N)r/   r   r6   r   r5   r7   r4   )r   r   r   rk   r   r   r   r   !test_inline_replaced_auto_margins  s    r   c              C   sf   t d\} | j\}|j\}|j\}|j\}|jdks8t|jdksFt|jdksTt|jdksbtdS )zETest that horizontal auto margins are ignored for empty inline boxes.z
        <style>
            @page { size: 200px }
            span { margin: auto }
        </style>
        <body><span></span>r   N)r/   r   r6   r   r5   r7   r4   )r   r   r   blockrt   r   r   r   test_empty_inline_auto_margins  s    r   c              C   s  t d\} | j\}|j\}|j\}}}}x||fD ]}|jjdksFt|jdksTt|jdksbt|j dksrt|j dkst|j	 dkst|j
 dkst|j dks2tq2W |jjdkst|jdkst|jdkst|j dkst|j dkst|j	 d	kst|j
 d	ks(t|j d
ks:t|jjdksLt|jdks\t|jdkslt|j dks~t|j dkst|j	 dkst|j
 dkst|j dkstdS )zQTest the box-sizing property.

    http://www.w3.org/TR/css3-ui/#box-sizing

    a  
        <style>
            @page { size: 100000px }
            body { width: 10000px; margin: 0 }
            div { width: 10%; height: 1000px;
                  margin: 100px; padding: 10px; border: 1px solid }
            div:nth-child(2) { box-sizing: content-box }
            div:nth-child(3) { box-sizing: padding-box }
            div:nth-child(4) { box-sizing: border-box }
        </style>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    zcontent-boxi  i  i  i  zpadding-boxi  i  i  z
border-boxi  i  i  N)r/   r   ri   Z
box_sizingr   r2   r3   Zpadding_widthZpadding_heightborder_widthborder_heightr   )r   r   r   ra   rc   rd   re   r<   r   r   r   test_box_sizing  s<    r   c        	      C   sP  t d\} | j\}}}}}xBt||||gddddgD ]$\}}|j\}|j\}||ks6tq6W |jdkslt|jdkszt|j dkst|j dkst|jd	kst|jdkst|j d
kst|j dkst|jdkst|jdkst|j dkst|j dkst|jd	ks&t|jdks6t|j d
ksHt|j dksZtt d\} | j\}}|j dkst|jdkst|j	dkst|j
dkst|j dkst|jdkst|jdkst|jdkstt d\} | j\}}|j dkst|jd!ks(t|j	dks8t|j
dksHtt d\} | j\}}|j dksnt|jdks~t|j	dkst|j
dkstt d\} | j\}}|j dkst|jdkst|j	d"kst|j
dkstt d\} | j\}}|j dkst|jdks*t|jdks:t|jdksJtt d\} | j\}}|j dkspt|jdkst|jdkst|jdkstt d\} | j\}}|j dkst|jdkst|jdkst|jdkstt d\} | j\}}|j dkst|jdks,t|jd#ks<t|jd ksLtd S )$Na  
        <style>
            @page {
                @top-left-corner {
                    content: 'top_left';
                    padding: 10px;
                }
                @top-right-corner {
                    content: 'top_right';
                    padding: 10px;
                }
                @bottom-left-corner {
                    content: 'bottom_left';
                    padding: 10px;
                }
                @bottom-right-corner {
                    content: 'bottom_right';
                    padding: 10px;
                }

                size: 1000px;
                margin-top: 10%;
                margin-bottom: 40%;
                margin-left: 20%;
                margin-right: 30%;
            }
        </style>
    top_left	top_rightbottom_leftbottom_rightr   r   r=   i  i,  iX  i  z
        <style>
            @page {
                margin: 100px 200px;
                @bottom-left-corner {
                    content: "";
                    margin: 60px
                }
            }
        </style>
    r   P   rV   z
        <style>
            @page {
                margin: 100px;
                @left-middle {
                    content: "";
                    margin: 10px;
                    width: 130px;
                }
            }
        </style>
    r!      a)  
        <style>
            @page {
                margin: 100px;
                @left-bottom {
                    content: "";
                    margin-left: 10px;
                    margin-right: auto;
                    width: 70px;
                }
            }
        </style>
    rB   rN   a(  
        <style>
            @page {
                margin: 100px;
                @right-top {
                    content: "";
                    margin-right: 10px;
                    margin-left: auto;
                    width: 130px;
                }
            }
        </style>
    rZ   a  
        <style>
            @page {
                margin: 100px;
                @top-left {
                    content: "";
                    margin-top: 10px;
                    margin-bottom: auto;
                }
            }
        </style>
    rU   z
        <style>
            @page {
                margin: 100px;
                @top-center {
                    content: "";
                    margin: auto 0;
                }
            }
        </style>
    z
        <style>
            @page {
                margin: 100px;
                @bottom-right {
                    content: "";
                    margin: auto;
                    height: 70px;
                }
            }
        </style>
    r+   a  
        <style>
            @page {
                margin: 100px;
                @bottom-center {
                    content: "";
                    margin: auto 0;
                    height: 150px;
                }
            }
        </style>
    r?   rX   iii)r/   r   zipr   r   r   r   r   r4   r5   r2   r6   r7   r3   )	r   r   r   r   r   r   Z
margin_boxr]   rk   r   r   r   !test_margin_boxes_fixed_dimension   s    










r   c              C   s  dd } | dt dd ks t| dt dd ks8ttd\}|j\}|j\}|j\}t |jd	ksltt|jd
 tjsttd\}|j\}|j\}|j\}|jdksttd\}|j\}|j\}|j\}|jdksttd\}|j\}|j\}|j\}|jdkstdS )z Unit tests for preferred widths.c             S   s,   t d|  \}|j\}|j\}|j\}|jS )Nu  
            <body style="width: %spx; font-family: ahem">
            <p style="white-space: pre-line; float: left">
                Lorem ipsum dolor sit amet,
                  consectetur elit
            </p>
                       <!--  ^  No-break space here  -->
        )r/   r   r2   )Z
body_widthr   r   r   r9   r   r   r   get_float_width  s    z.test_preferred_widths.<locals>.get_float_widthr!   u   consectetur elitr'   i@B zLorem ipsum dolor sit amet,zD
        <p style="float: left">Lorem <em>ipsum</em> dolor.</p>
    r   r   z
        <style>img { width: 20px }</style>
        <p style="float: left">
            <img src=pattern.png><img src=pattern.png><br>
            <img src=pattern.png></p>
    rV   zb<style>p { font: 20px Ahem }</style>
                     <p style="float: left">XX<br>XX<br>X</p>zc<style>p { font: 20px Ahem }</style>
                     <p style="float: left">XX<br> XX<br>X</p>N)rP   r   r/   r   r0   r
   r   r2   )r   r   r   r   r9   r   r   r   test_preferred_widths  s8    r   c              C   s2  dd } dd }d|dd|dd|ddf }| |dddgksFt d|dd|dd|ddf }| |dddgks|t d	|dd|d
df }| |dd
dgkst d|dd|dd|ddf }| |dddgkst d|ddd|dd|df }| |dddgkst d|ddd|dd|df }| |dddgksPt d|dd|df }| |ddgks|t d|dd }| |dd
dgkst d|ddd }| |dd
dgkst d|dd }| |dd
dgkst d|dd }| |dd
dgkst d}| |ddgks0t d|ddd }| |ddgksVt d|dddd }| |ddgks~t d|ddd }| |ddgkst d|dd|ddf }| |d d!gkst d|d"d|ddf }| |d"dgks t d|d d#|d d$f }| |d%d&gks.t d S )'Nc                s    fdddD }t d  \}|jd jdks4t|jd	d
 }dd |D |ksXtddd	d}x0|D ](}|jd||j d|j    ksjtqjW dd |D S )u   Take some CSS to have inside @page

        Return margin-widths of the sub-sequence of the three margin boxes
        that are generated.

        The containing block’s width is 600px. It starts at x = 100 and ends
        at x = 700.

        c                s   g | ]}|d   kr|qS )z { content: r   )rS   
at_keyword)r   r   r   rT   E  s    zLtest_margin_boxes_variable_dimension.<locals>.get_widths.<locals>.<listcomp>	@top-left@top-center
@top-righta  
            <style>
                @page {
                    size: 800px;
                    margin: 100px;
                    padding: 42px;
                    border: 7px solid;
                    %s
                }
            </style>
        r   r   r   Nc             S   s   g | ]
}|j qS r   )r   )rS   r   r   r   r   rT   U  s    g      ?)z	@top-leftz@top-centerz
@top-rightr=   iX  c             S   s   g | ]}|j  qS r   )r   )rS   r   r   r   r   rT   Z  s    )r   r   r   )r/   r   r   r   r   r   r   )r   Zexpected_at_keywordsr   Zmargin_boxesZoffsetsr   r   )r   r   
get_widths:  s    


z8test_margin_boxes_variable_dimension.<locals>.get_widthsc              W   s   dj dd | D S )Nr   c             s   s   | ]}d | V  qdS )z<url('data:image/svg+xml,<svg width="%i" height="10"></svg>')Nr   )rS   r2   r   r   r   r   ^  s   zGtest_margin_boxes_variable_dimension.<locals>.images.<locals>.<genexpr>)r   )Zwidthsr   r   r   r   \  s    z4test_margin_boxes_variable_dimension.<locals>.imageszn
        @top-left { content: %s }
        @top-center { content: %s }
        @top-right { content: %s }
    r?   r=   z|
        @top-left { content: %s; margin: auto }
        @top-center { content: %s }
        @top-right { content: %s }
    z
        @top-left { content: %s }
        @top-center { content: %s }
        @top-right { content: 'foo'; width: 200px }
    i,  rX   r   r!   r      z|
        @top-left { content: %s; width: 205px }
        @top-center { content: %s }
        @top-right { content: %s }
          z
        @top-left { width: 1000px; margin: 1000px; padding: 1000px;
                    border: 1000px solid }
        @top-center { content: %s }
        @top-right { content: %s }
    z
        @top-left { content: ''; width: 200px }
        @top-center { content: ''; width: 300px }
        @top-right { content: %s }
       r   zf
        @top-left { content: ''; width: 200px }
        @top-right { content: ''; width: 500px }
    i  zX
        @top-left { content: ''; width: 200px }
        @top-right { content: %s }
    i^  i  zX
        @top-left { content: %s }
        @top-right { content: ''; width: 200px }
    i  zJ
        @top-left { content: %s }
        @top-right { content: %s }
    r;      r   i&  r      i  iE  )r   )r   r   r   r   r   r   $test_margin_boxes_variable_dimension8  sL    "
     r   c              C   s^   t d\} | j\}}}}|j\}|j\}|j\}|jdks>t|jdksLt|jdksZtdS )aB  
         3 px ->    +-----+
                    |  1  |
                    +-----+

                43 px ->   +-----+
                53 px ->   |  2  |
                           +-----+

                       83 px ->   +-----+
                                  |  3  |
                       103px ->   +-----+
    u  
        <style>
            @page {
                size: 800px;
                margin: 106px;  /* margin boxes’ content height is 100px */

                @top-left {
                    content: "foo"; line-height: 20px; border: 3px solid;
                    vertical-align: top;
                }
                @top-center {
                    content: "foo"; line-height: 20px; border: 3px solid;
                    vertical-align: middle;
                }
                @top-right {
                    content: "foo"; line-height: 20px; border: 3px solid;
                    vertical-align: bottom;
                }
            }
        </style>
    r@   +   S   N)r/   r   r   r   )r   r   r   Z
top_centerr   r^   r_   r`   r   r   r    test_margin_boxes_vertical_align  s    r   c              C   s   dd } dd }| dd }| dd }| d	d
 }| dd }| dd }|dd }| dd }|dd }	| dd }
dS )u   
    The vertical space between to sibling blocks is the max of their margins,
    not the sum. But that’s only the simplest case...
    c             S   s^   | dddkst | dddks$t | ddd	ks6t | ddd
ksHt | dddksZt | S )N10px15pxr+   z-10pxr,   z-15pxautor!   i)r   )vertical_spacer   r   r   assert_collapsing	  s    z1test_margin_collapsing.<locals>.assert_collapsingc             S   s^   | dddkst | dddks$t | ddd	ks6t | ddd
ksHt | dddksZt | S )Nr   r   r-   z-10pxr,   z-15pxr   r!   r   i)r   )r   r   r   r   assert_NOT_collapsing"	  s    z5test_margin_collapsing.<locals>.assert_NOT_collapsingc       	      S   sJ   t d| |f \}|j\}|j\}|j\}}|j |j }|j }|| S )Na  
            <style>
                p { font: 20px/1 serif } /* block height == 20px */
                #p1 { margin-bottom: %s }
                #p2 { margin-top: %s }
            </style>
            <p id=p1>Lorem ipsum
            <p id=p2>dolor sit amet
        )r/   r   r   r3   )	p1_margin_bottomp2_margin_topr   r   r   p1p2	p1_bottomp2_topr   r   r   vertical_space_1+	  s    
z0test_margin_collapsing.<locals>.vertical_space_1c       
      S   sR   t d| |f \}|j\}|j\}|j\}}|j\}|j |j }|j }	|	| S )Na5  
            <style>
                p { font: 20px/1 serif } /* block height == 20px */
                #p1 { margin-bottom: %s }
                #p2 { margin-top: %s }
            </style>
            <div>
                <p id=p1>Lorem ipsum
            </div>
            <p id=p2>dolor sit amet
        )r/   r   r   r3   )
r  r  r   r   r   r<   r  r  r  r  r   r   r   vertical_space_2>	  s    

z0test_margin_collapsing.<locals>.vertical_space_2c       
      S   sR   t d| |f \}|j\}|j\}|j\}}|j\}|j |j }|j }	|	| S )Na5  
            <style>
                p { font: 20px/1 serif } /* block height == 20px */
                #p1 { margin-bottom: %s }
                #p2 { margin-top: %s }
            </style>
            <p id=p1>Lorem ipsum
            <div>
                <p id=p2>dolor sit amet
            </div>
        )r/   r   r   r3   )
r  r  r   r   r   r  r<   r  r  r  r   r   r   vertical_space_3T	  s    

z0test_margin_collapsing.<locals>.vertical_space_3c             S   sZ   t d| |f \}|j\}|j\}|j\}}|j\}|j\}|j |j }	|j }
|
|	 S )Naf  
            <style>
                p { font: 20px/1 serif } /* block height == 20px */
                #p1 { margin-bottom: %s }
                #p2 { margin-top: %s }
            </style>
            <p id=p1>Lorem ipsum
            <div>
                <div>
                    <p id=p2>dolor sit amet
                </div>
            </div>
        )r/   r   r   r3   )r  r  r   r   r   r  div1div2r  r  r  r   r   r   vertical_space_4j	  s    
z0test_margin_collapsing.<locals>.vertical_space_4c             S   s   t d| |f \}|j\}|j\}|j\}}|j\}|j\}|j |j }	|j }
|j |j ksft|j |j kszt|
|	 S )Naq  
            <style>
                p { font: 20px/1 serif } /* block height == 20px */
                #div1 { margin-top: %s }
                #div2 { margin-top: %s }
            </style>
            <p>Lorem ipsum
            <div id=div1>
                <div id=div2>
                    <p id=p2>dolor sit amet
                </div>
            </div>
        )r/   r   r   r3   Zborder_box_yr   )margin_1margin_2r   r   r   r  r
  r  r  r  r  r   r   r   vertical_space_5	  s    
z0test_margin_collapsing.<locals>.vertical_space_5c             S   sZ   t d| |f \}|j\}|j\}|j\}}|j\}|j\}|j |j }	|j }
|
|	 S )Na  
            <style>
                p { font: 20px/1 serif } /* block height == 20px */
                #div1 { margin-top: %s; overflow: hidden }
                #div2 { margin-top: %s }
            </style>
            <p>Lorem ipsum
            <div id=div1>
                <div id=div2>
                    <p id=p2>dolor sit amet
                </div>
            </div>
        )r/   r   r   r3   )r  r  r   r   r   r  r
  r  r  r  r  r   r   r   vertical_space_6	  s    
z0test_margin_collapsing.<locals>.vertical_space_6c       
      S   sP   t dd| |f  \}|j\}|j\}|j\}}}|j |j }|j }	|	| S )Na^  
            <style>
                p { font: 20px/1 serif } /* block height == 20px */
                #p1 { margin-bottom: %s }
                #p2 { margin-top: %s }
                div { margin-bottom: %s; margin-top: %s }
            </style>
            <p id=p1>Lorem ipsum
            <div></div>
            <p id=p2>dolor sit amet
        r	   )r/   r   r   r3   )
r  r  r   r   r   r  r<   r  r  r  r   r   r   vertical_space_7	  s    
z0test_margin_collapsing.<locals>.vertical_space_7c             S   s6   t d| |f \}|j\}|j\}|j\}|j }|S )Nz
            <html>
                <style>
                    html { margin-top: %s }
                    body { margin-top: %s }
                </style>
                <body>
                    <p>Lorem ipsum
        )r/   r   r   )r  r  r   r   r   r  p1_topr   r   r   vertical_space_8	  s    z0test_margin_collapsing.<locals>.vertical_space_8c             S   s>   t d| |f \}|j\}|j\}|j\}|j\}|j }|S )Nz
            <html>
                <style>
                    body { margin-top: %s }
                    div { margin-top: %s }
                </style>
                <body>
                    <div>
                        <p>Lorem ipsum
        )r/   r   r   )r  r  r   r   r   r<   r  r  r   r   r   vertical_space_9	  s    	z0test_margin_collapsing.<locals>.vertical_space_9Nr   )r   r   r  r  r	  r  r  r  r  r  r  r   r   r   test_margin_collapsing	  s    
	r  c              C   s"  t d\} | j\}|j\}|j\}}}|j\}}}}	}
}|j|jfdksLt|j|jfdks`t|j|jfdkstt|j|jfdkst|j|jfdkst|	j|	jfdkst|
j|
jfdkst|j|jfdkst|j|jfdk st|jdk stt d\} | j\}|j\}|j\}|j\}}}|j\}|j\}}}}}}|j\}|j|jfdksdt|jdkstt|j|jfdkst|j|jfdkst|j|jfdkst|j|jfdkst|j|jfdkst|j|jfdkst|j|jfdkst|jdkstd S ) Na  
        <style>
          p { height: 20px }
        </style>
        <p>1</p>
        <div style="position: relative; top: 10px">
            <p>2</p>
            <p style="position: relative; top: -5px; left: 5px">3</p>
            <p>4</p>
            <p style="position: relative; bottom: 5px; right: 5px">5</p>
            <p style="position: relative">6</p>
            <p>7</p>
        </div>
        <p>8</p>
    r   rZ   r,   -   rN   U   n   r   r[   r;   a  
        <style>
          img { width: 20px }
          body { font-size: 0 } /* Remove spaces */
        </style>
        <body>
        <span><img src=pattern.png></span>
        <span style="position: relative; left: 10px">
            <img src=pattern.png>
            <img src=pattern.png
                 style="position: relative; left: -5px; top: 5px">
            <img src=pattern.png>
            <img src=pattern.png
                 style="position: relative; right: 5px; bottom: 5px">
            <img src=pattern.png style="position: relative">
            <img src=pattern.png>
        </span>
        <span><img src=pattern.png></span>
    )r   r   )r   rZ   )r   rZ   )r,   r  )r   rN   r   )r   r  )r   r  )r   r   )r   r[   )r   r   )rZ   r   )r  r,   )rN   r   r   )r  r   )r  r   )r   r   )r[   r   )r/   r   r   r   r   r3   r2   )r   r   r   r  r<   Zp8r  p3p4p5p6p7rk   span1span2span3img1img2Zimg3Zimg4Zimg5Zimg6Zimg7Zimg8r   r   r   test_relative_positioning	  sF    r#  c              C   s  t d\} | j\}|j\}|j\}|j\}}}|jdks<t|j|jfdksPt|j|jfdksdt|j|jfdksxt|j|jfd kst|j|jfd!kst|j|jfd"kst|j|jfd#kstt d\} | j\}|j\}|j\}|j\}}x@|||fD ]2}|j|jfd$kst|j|jfd%ks tq W t d\} | j\}|j\}|j\}|j\}	}
|
j\}}}|
jdks~t|j|jfd&kst|j|jfd'kst|jdkstt d\} | j\}|j\}|j\}|j\}}}|j|jfd(kst|j|jfd)kst|j|jfd*ks*tt d
\} | j\}|j\}|j\}|j\}}}|j|jfd+ksnt|j|jfd,kst|j|jfd-kstt d\} | j\}|j\}|j\}|j\}}|j|jfd.kst|j|jfd/kst|j |j fd0kst|j|jfd1ks"t|j|jfd2ks8t|j|jfd3ksNt|j|jfd4ksdtt d\} | j\}|j\}|j\}}}|j\}}}}}|j\}|j\}
|
j\}	}}|j|jfd5kst|j|jfd6kst|j|jfd7kst|j|jfd9kst|j|jfd:ks$t|j|jfd;ks:t|	j|	jfd<ksPt|j|jfd=ksft|j|jfd>ks|t|j|jfd?kst|jdkst|j|jfd@kstd S )ANaa  
        <div style="margin: 3px">
            <div style="height: 20px; width: 20px; position: absolute"></div>
            <div style="height: 20px; width: 20px; position: absolute;
                        left: 0"></div>
            <div style="height: 20px; width: 20px; position: absolute;
                        top: 0"></div>
        </div>
    r   rB   r@   z
        <div style="position: relative; width: 20px">
            <div style="height: 20px; width: 20px; position: absolute"></div>
            <div style="height: 20px; width: 20px"></div>
        </div>
    a   
        <body style="font-size: 0">
            <img src=pattern.png>
            <span style="position: relative">
                <span style="position: absolute">2</span>
                <span style="position: absolute">3</span>
                <span>4</span>
            </span>
    rC   z
        <style> img { width: 5px; height: 20px} </style>
        <body style="font-size: 0">
            <img src=pattern.png>
            <span style="position: absolute">2</span>
            <img src=pattern.png>
    r,   z
        <style> img { width: 5px; height: 20px} </style>
        <body style="font-size: 0">
            <img src=pattern.png>
            <span style="position: absolute; display: block">2</span>
            <img src=pattern.png>
    a  
        <div style="position: relative; width: 20px; height: 60px;
                    border: 10px solid; padding-top: 6px; top: 5px; left: 1px">
            <div style="height: 20px; width: 20px; position: absolute;
                        bottom: 50%"></div>
            <div style="height: 20px; width: 20px; position: absolute;
                        top: 13px"></div>
        </div>
    r   r   rV   V   rJ   r   ai  
        <style>
          @page { size: 1000px 2000px }
          html { font-size: 0 }
          p { height: 20px }
        </style>
        <p>1</p>
        <div style="width: 100px">
            <p>2</p>
            <p style="position: absolute; top: -5px; left: 5px">3</p>
            <p style="margin: 3px">4</p>
            <p style="position: absolute; bottom: 5px; right: 15px;
                      width: 50px; height: 10%;
                      padding: 3px; margin: 7px">5
                <span>
                  <img src="pattern.png">
                  <span style="position: absolute"></span>
                  <span style="position: absolute; top: -10px; right: 5px;
                               width: 20px; height: 15px"></span>
                </span>
            </p>
            <p style="margin-top: 8px">6</p>
        </div>
        <p>7</p>
                  ?   G   [   )r   r   )rB   rB   )r@   r@   )rB   rB   )r   r@   )rB   rB   )r@   r   )r   r   )rB   rB   )rC   r   )rC   r   )r   r   )r,   r   )r,   r   )r   r   )r   rB   )r,   r   )r   r,   )rB   r   )rV   r$  )rJ   r   )rB   rB   )rJ   r   )rB   rB   )r   r   )r   rB   )r   rB   r   )r,   r   )r   rV   )r%  r&  )r'  r(  )r)  r(  )r*  r+  )r   r,  )r   r.  )	r/   r   r3   r   r   r   r2   r   r   )r   r   r   r
  r  Zdiv3Zdiv4r<   rk   r   r  r  r   Zspan4r!  rt   r"  r  r  r  r  r  r  r  r   r   r   test_absolute_positioning<
  s    


r/  c              C   s   t d\} | j\}|j\}|j\}|j\}}|jdks:t|j|jfdksNt|j|jfdksbt|j|jfdksvt|j|jfd	kst|j|jfd
kstd S )Nz
        <style>
            img { display: block; position: absolute }
        </style>
        <div style="margin: 10px">
            <img src=pattern.png />
            <img src=pattern.png style="left: 15px" />
        </div>
    r   r!   rC   r+   )r   r   )r!   r!   )rC   rC   )r+   r!   )rC   rC   )r/   r   r3   r   r   r   r2   )r   r   r   r<   r!  r"  r   r   r   test_absolute_images
  s    
r0  c              C   s   t d\} }}| j\}dd |jD ddgks2t|j\}|j\}|j\}dd |jD dgksdt|j\}dd |jD ddgkstd S )Nz
        a
        <div style="page-break-before: always; page-break-after: always">
            <p style="position: fixed">b</p>
        </div>
        c
    c             S   s   g | ]
}|j qS r   )r   )rS   rw   r   r   r   rT   	  s    z*test_fixed_positioning.<locals>.<listcomp>r   r$   c             S   s   g | ]
}|j qS r   )r   )rS   rw   r   r   r   rT     s    c             S   s   g | ]
}|j qS r   )r   )rS   rw   r   r   r   rT     s    )r/   r   r   )r   r   r   r   r   r<   r   r   r   test_fixed_positioning
  s    r1  c              C   sv  t d\} | j\}|j\}|j\}}t|dks4tt|dksDtt d\} | j\}|j\}|j\}}}}|j\}}|j\}	|j\}
t|dkstt|dkstt|	dkstt|dkstt|
dk stt d\} | j\}|j\}dd	 |jD }|ddd d!d"d#d$d%d&d'g
ks$tt d\} | j\}|j\}|j\}}}|j\}}|jdksdt|j|jfd(kszt|j|jfd)kstt d\} | j\}|j\}|j\}}}|j\}}|jdkst|j|jfd*kst|j|jfd+kstt d\} | j\}|j\}|j\}}|j\}|j\}|j\}|j\}}	t|d,ksTtt|d-ksftt|	d.ksxtt d\} | j\}|j\}|j\}}|j\}|j\}}	t|d/kstt|d0kstt|	d1kstt d\}}|j\}|j\}|j\}}|j\}|j\}t|d2ks,tt|d3ks>t|j\}|j\}|j\}}|j\}|j\}t d\} d S )4Nz
        <style>
            div { float: left }
            img { width: 100px; vertical-align: top }
        </style>
        <div><img src=pattern.png /></div>
        <div><img src=pattern.png /></div>r   r=   a  
        <style>
            body { width: 290px }
            div { float: left; width: 100px;  }
            img { width: 60px; vertical-align: top }
        </style>
        <div><img src=pattern.png /><!-- 1 --></div>
        <div><img src=pattern.png /><!-- 2 --></div>
        <div><img src=pattern.png /><!-- 4 --></div>
        <img src=pattern.png /><!-- 3
        --><img src=pattern.png /><!-- 5 -->r   r   uJ  
        <style type="text/css">
            body { width: 200px }
            p { width: 70px; height: 20px }
            .left { float: left }
            .right { float: right }
        </style>
        <p class="left"> ⇦ A 1 </p>
        <p class="left"> ⇦ B 2 </p>
        <p class="left"> ⇦ A 3 </p>
        <p class="right"> B 4 ⇨ </p>
        <p class="left"> ⇦ A 5 </p>
        <p class="right"> B 6 ⇨ </p>
        <p class="right"> B 8 ⇨ </p>
        <p class="left"> ⇦ A 7 </p>
        <p class="left"> ⇦ A 9 </p>
        <p class="left"> ⇦ B 10 </p>
    c             S   s   g | ]}|j |jfqS r   )r   r   )rS   r9   r   r   r   rT   P  s   ztest_floats.<locals>.<listcomp>rN   rB   r   rV   r   a)  
        <style>
            body { width: 100px }
            p { float: left; height: 100px }
            img { width: 60px; vertical-align: top }
        </style>
        <p style="width: 20px"></p>
        <p style="width: 100%"></p>
        <img src=pattern.png /><img src=pattern.png />
    a  
        <style>
            body { width: 100px; font: 60px Ahem; }
            p { float: left; height: 100px }
            img { width: 60px; vertical-align: top }
        </style>
        <p style="width: 20px"></p>
        <p style="width: 100%"></p>
        A B
    a`  
        <style>
            body { width: 90px; font-size: 0 }
            img { vertical-align: top }
        </style>
        <body>
        <span>
            <img src=pattern.png style="width: 50px" />
            <img src=pattern.png style="width: 50px" />
            <img src=pattern.png style="float: left; width: 30px" />
        </span>
    r?   rZ   a5  
        <style>
            body { width: 90px; font-size: 0 }
            img { vertical-align: top }
        </style>
        <body>
        <img src=pattern.png style="width: 50px" />
        <img src=pattern.png style="width: 50px" />
        <img src=pattern.png style="float: left; width: 30px" />
    ak  
        <style>
            body { width: 90px; font-size: 0 }
            img { vertical-align: top }
        </style>
        <body>
        <img src=pattern.png style="float: left; width: 30px" />
        <img src=pattern.png style="width: 50px" />
        <div style="page-break-before: always"></div>
        <img src=pattern.png style="width: 50px" />
    z"<div style="top:100%; float:left">)r   r   r=   r=   )r=   r   r=   r=   )r   r   r=   r   )r=   r   r=   r   )r   r   r   r   )r   r   r=   r   )r=   r   r   r   )r   r   )rN   r   )r   rB   )r   rB   )r   rV   )r   rV   )r   r   )r   r   )r   r   )rN   r   )rB   r   )r   r   )rB   r   )r   r   )r   r   r?   r?   )rZ   r?   r?   r?   )r   r?   rZ   rZ   )r   r   r?   r?   )rZ   r?   r?   r?   )r   r?   rZ   rZ   )r   r   rZ   rZ   )rZ   r   r?   r?   )r/   r   r   r   r   r   )r   r   r   ra   rc   re   Z
anon_blockr`   Zline_5r   r   	positionsr   r   r^   r_   r   r   r   r   r   r   Z	float_imgrk   r<   r   r   r   test_floats  s    



	
	


	





r3  c              C   sz  t d} t| dkstg }xV| D ]N}dd |j D }tdd |D sNttdd |D sdt|j| ~q"W dd |D }|dgdggkstt d	} t| dkstg }xV| D ]N}d
d |j D }tdd |D sttdd |D st|j| ~qW dd |D }|dgdggks.tt d} t| dksHtg }x\| D ]T}dd |j D }tdd |D sttdd |D st|j| ~qRW dd |D }|ddgddgdggkstt d} t| dkstg }xD| D ]<}dd |j D }tdd |D s(t|j| ~qW dd |D }|ddgdggks`tt d} t| dksztg }xD| D ]<}dd |j D }tdd |D st|j| ~qW dd |D }|dgddggkstt d} t| dkstg }xD| D ]<}d d |j D }td!d |D s<t|j| ~qW d"d |D }|dgdgdggksvtd#S )$zATests the page breaks when floated boxes
    do not fit the page.a  
        <style>
            @page { size: 100px; margin: 10px }
            img { height: 45px; width:70px; float: left;}
        </style>
        <body>
            <img src=pattern.png>
                    <!-- page break should be here !!! -->
            <img src=pattern.png>
    r	   c             S   s   g | ]}|j d kr|qS )r   )r   )rS   rx   r   r   r   rT     s    z+test_floats_page_breaks.<locals>.<listcomp>c             S   s   g | ]}|j d kqS )r   )r   )rS   r   r   r   r   rT     s    c             S   s   g | ]}|j d kqS )r!   )r   )rS   r   r   r   r   rT     s    c             S   s   g | ]}d d |D qS )c             S   s   g | ]
}|j qS r   )r   )rS   r   r   r   r   rT     s    z6test_floats_page_breaks.<locals>.<listcomp>.<listcomp>r   )rS   r   r   r   r   rT     s   r!   a  
        <style>
            @page { size: 100px; margin: 10px }
            img { height: 81px; width:70px; float: left;}
        </style>
        <body>
            <img src=pattern.png>
                    <!-- page break should be here !!! -->
            <img src=pattern.png>
    c             S   s   g | ]}|j d kr|qS )r   )r   )rS   rx   r   r   r   rT     s    c             S   s   g | ]}|j d kqS )r   )r   )rS   r   r   r   r   rT     s    c             S   s   g | ]}|j d kqS )r!   )r   )rS   r   r   r   r   rT     s    c             S   s   g | ]}d d |D qS )c             S   s   g | ]
}|j qS r   )r   )rS   r   r   r   r   rT     s    z6test_floats_page_breaks.<locals>.<listcomp>.<listcomp>r   )rS   r   r   r   r   rT     s   a  
        <style>
            @page { size: 100px; margin: 10px }
            img { height: 30px; width:70px; float: left;}
        </style>
        <body>
            <img src=pattern.png>
            <img src=pattern.png>
                    <!-- page break should be here !!! -->
            <img src=pattern.png>
            <img src=pattern.png>
                    <!-- page break should be here !!! -->
            <img src=pattern.png>
    r@   c             S   s   g | ]}|j d kr|qS )r   )r   )rS   rx   r   r   r   rT     s    c             S   s   g | ]}|j d kqS )r   )r   )rS   r   r   r   r   rT     s    c             S   s   g | ]}|j d kqS )r!   )r   )rS   r   r   r   r   rT     s    c             S   s   g | ]}d d |D qS )c             S   s   g | ]
}|j qS r   )r   )rS   r   r   r   r   rT     s    z6test_floats_page_breaks.<locals>.<listcomp>.<listcomp>r   )rS   r   r   r   r   rT     s   rV   a  
        <style>
            @page{
                size: 110px;
                margin: 10px;
                padding: 0;
            }
            .large {
                width: 10px;
                height: 60px;
            }
            .small {
                width: 10px;
                height: 20px;
            }
        </style>
        <body>
            <div class="large"></div>
            <div class="small"></div>
            <div class="large"></div>
    c             S   s   g | ]}|j d kr|qS )r<   )r   )rS   r<   r   r   r   rT   5  s    c             S   s   g | ]}|j d kqS )r<   )r   )rS   r<   r   r   r   rT   6  s    c             S   s   g | ]}d d |D qS )c             S   s   g | ]
}|j qS r   )r   )rS   r<   r   r   r   rT   :  s    z6test_floats_page_breaks.<locals>.<listcomp>.<listcomp>r   )rS   rQ   r   r   r   rT   :  s    rN   a  
        <style>
            @page{
                size: 110px;
                margin: 10px;
                padding: 0;
            }
            .large {
                width: 10px;
                height: 60px;
            }
            .small {
                width: 10px;
                height: 20px;
                page-break-after: avoid;
            }
        </style>
        <body>
            <div class="large"></div>
            <div class="small"></div>
            <div class="large"></div>
    c             S   s   g | ]}|j d kr|qS )r<   )r   )rS   r<   r   r   r   rT   Y  s    c             S   s   g | ]}|j d kqS )r<   )r   )rS   r<   r   r   r   rT   Z  s    c             S   s   g | ]}d d |D qS )c             S   s   g | ]
}|j qS r   )r   )rS   r<   r   r   r   rT   ^  s    z6test_floats_page_breaks.<locals>.<listcomp>.<listcomp>r   )rS   rQ   r   r   r   rT   ^  s    rZ   a  
        <style>
            @page{
                size: 110px;
                margin: 10px;
                padding: 0;
            }
            .large {
                width: 10px;
                height: 80px;
            }
            .small {
                width: 10px;
                height: 20px;
                page-break-after: avoid;
            }
        </style>
        <body>
            <div class="large"></div>
            <div class="small"></div>
            <div class="large"></div>
    c             S   s   g | ]}|j d kr|qS )r<   )r   )rS   r<   r   r   r   rT   }  s    c             S   s   g | ]}|j d kqS )r<   )r   )rS   r<   r   r   r   rT   ~  s    c             S   s   g | ]}d d |D qS )c             S   s   g | ]
}|j qS r   )r   )rS   r<   r   r   r   rT     s    z6test_floats_page_breaks.<locals>.<listcomp>.<listcomp>r   )rS   rQ   r   r   r   rT     s    N)r/   rP   r   r   r   rO   )r8   r   r   r   r   r   rQ   r   r   r   test_floats_page_breaks  s    	

	









r4  c              C   sB   t d\} | j\}|j\}|j\}}}|j}|j}||k s>td S )Nz
        <style>p { float: left }</style>
        <p>Hello, world!</p>
        <p style="font-stretch: semi-condensed">Hello, world!</p>
        <p style="font-stretch: semi-expanded">Hello, world!</p>
    )r/   r   r2   r   )r   r   r   r   r   Zp_3ZnormalZ	condensedr   r   r   test_font_stretch  s    r5  c              C   s  t d\} }| j\}|j\}|j\}|j\}}|jdks<t|jdksJt|jdksXt|jdksft|j dksvt|jdkst|jdkst|jdkst|j	dkst|j
dkst|jdkst|j d	kst|j\}|j\}|j\}|j\}}|jdkst|jdkst|jdks,t|jdks<t|j dksNt|jdks^t|jd
ksnt|jdks~t|jdkst|j
dkst|j	dkst|j d	kstd S )Na\  
        <style>
            @page { size: 100px }
            p { padding: 2px; border: 3px solid; margin: 5px }
            img { height: 40px; vertical-align: top }
        </style>
        <p>
            <img src=pattern.png><br>
            <img src=pattern.png><br>
            <img src=pattern.png><br>
            <img src=pattern.png><br>r   r,   r@   r	   r!   r?   r   rU   rV   )r/   r   r   r   r6   Zborder_top_widthZpadding_topr   r3   r7   Zborder_bottom_widthZpadding_bottomr   )r   r   r   r   r9   r^   r_   r   r   r   test_box_decoration_break  sD    



r6  c              C   sl   dd } | ddkst | ddks(t | ddks8t | ddksHt | dd	ksXt | d
dksht d S )Nc             S   s,   t d|  \}|j\}|j\}|j}t|S )Nz,<html style="width: 5em; font-family: ahem">)r/   r   rP   )r   r   r   r   r{   r   r   r   
line_count  s
    z$test_hyphenation.<locals>.line_countu   <body>hyphénationr   u   <body lang=fr>hyphénationu/   <body style="-weasy-hyphens: auto">hyphénationu7   <body style="-weasy-hyphens: auto" lang=fr>hyphénationu   <body>hyp&shy;hénationr	   u4   <body style="-weasy-hyphens: none">hyp&shy;hénation)r   )r7  r   r   r   test_hyphenation  s    r8  c           	   C   s  d8} d9}d:}df ddg||gd;fdd}|dd|g g  |dd|g g  |d	dd<g g  |dd=d |dd>d |dd?d |ddd@g g dCd |ddDd |ddEd |ddFd |ddGd |ddId |ddKd |ddMd |d dOd |d!dPd |d"dQd |d#dSd |d'dUd |d(dWd |d)dYd |d*d[d |d+d\d |d.d]|| || |gdd0d1d2dgd3 |d4d_||| |gdd5d5dgd3 |d6d`||| |gddddgd3 |d7ddag g  d S )bNr   r   linearc       
         sb   t d|  \}|jj\}|\ |jjdd fdd}d||||f}	t||	s^t||	fd S )Nz<style>@page { background: i  i,  c                s   |   | fS )Nr   )dxdy)scale_xscale_yr   r   <lambda>  s    z6test_linear_gradient.<locals>.layout.<locals>.<lambda>r   )r/   
backgroundlayersimagelayoutr   r   )
gradient_csstype_initr2  colorsscaler   layerresultexpectedr   )r<  r=  r   rB    s    
z$test_linear_gradient.<locals>.layoutzlinear-gradient(blue)solidzrepeating-linear-gradient(blue)z+repeating-linear-gradient(blue, lime 1.5px)      ?zlinear-gradient(blue, lime)r   ,  )rE  z%repeating-linear-gradient(blue, lime)z*repeating-linear-gradient(blue, lime 20px)rB   )rG  z&linear-gradient(to bottom, blue, lime)z#linear-gradient(to top, blue, lime)z%linear-gradient(to right, blue, lime)rX     z$linear-gradient(to left, blue, lime)z(linear-gradient(to top left, blue, lime)X  V  8   r)   z)linear-gradient(to top right, blue, lime)z+linear-gradient(to bottom left, blue, lime)z,linear-gradient(to bottom right, blue, lime)z#linear-gradient(270deg, blue, lime)z$linear-gradient(.75turn, blue, lime)z"linear-gradient(45deg, blue, lime)r-   E  w  z%linear-gradient(.125turn, blue, lime)z%linear-gradient(.375turn, blue, lime)z%linear-gradient(.625turn, blue, lime)z%linear-gradient(.875turn, blue, lime)z#linear-gradient(blue 2em, lime 20%)    r   z7linear-gradient(blue 100px, red, blue, red 160px, lime)r=   g?g?g333333?)rE  rF  r2  z9linear-gradient(blue -100px, blue 0, red -12px, lime 50%)g?z+linear-gradient(blue, blue, red, lime -7px)z6repeating-linear-gradient(blue, blue, lime, lime -7px))r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   )r   rL  rL  r   )r   r   r   rM  )r   r   r   rM  )r   r   r   rB   )r   rL  rL  r   皙?皙?)rU  rV  )r   r   r   rM  )r   rM  r   r   )r   rX   rN  rX   )rN  rX   r   rX   )rO  rP  rQ  rW  )rQ  rP  rO  rX  )rO  rY  rQ  rP  )rQ  rZ  rO  rP  )rN  rX   r   rX   )rN  rX   r   rX   )r-   rR  rS  r[  )r-   rR  rS  r\  )r-   r]  rS  rR  )rS  r^  r-   rR  )rS  rR  r-   r_  )r   rT  r   r   )r   r=   r   rM  )r   r`  r   rX   )r   r   r   r=   )r   rL  rL  r   r   )redlimebluerB  r   r   r   test_linear_gradient  s\    	rd  c           	   C   s
  da} db}dc}df ddg||gdddfdd}|dd|g g  |dd|g g  |d	ded |ddf|| gd |ddgd |ddhdd |ddidd |ddjdkd |ddldmdod |ddpdqdsd |dddtg g dvd" |dddwg g dyd" |d$dzd |d&d{d |d'd|g g  |d(d|g g  |d)d|d}|gd |d*d~dd |d,ddd |d-dd |d.dd |d0dd |d2dd |d4dd |d6ddd |d7ddd |d:ddd |d=dd |d>dd |d@dd |dBdd |dCdd |dFddd |dGddd |dIddd |dKdd |dLdd |dOdd t jdR}|dSd
ddd
| fdd |dTdUddd/| fdd |dVddWdd| fdd |dYdd |dZdd |d\dd |d^d
ddd
| fdd |d_dUdddU| fdd |d`ddWddJ| fdd d S )Nr   r   radialc                s   |dkr,|\}}}	}
||| |	||| |
f}t d|  \}|jj\}|\ |jjdd fdd}|||||f}t||st||fd S )Nre  z<style>@page { background: i  i,  c                s   |   | fS )Nr   )r:  r;  )ctm_scale_xctm_scale_yr   r   r>  7  s    z6test_radial_gradient.<locals>.layout.<locals>.<lambda>)r/   r?  r@  rA  rB  r   r   )rC  rD  rE  r2  rF  r=  	ctm_scaleZcenter_xZcenter_yZradius0Zradius1r   rH  rI  rJ  r   )rf  rg  r   rB  ,  s    

z$test_radial_gradient.<locals>.layoutzradial-gradient(blue)rK  zrepeating-radial-gradient(blue)z"radial-gradient(100px, blue, lime)r   rX   r=   )rE  z;radial-gradient(100px at right 20px bottom 30px, lime, red)|    )rE  rF  z radial-gradient(0 0, blue, lime)Hz>z"radial-gradient(1px 0, blue, lime)    cAg+=)rE  r=  z"radial-gradient(0 1px, blue, lime)g  ļBz0repeating-radial-gradient(20px 40px, blue, lime)rB   rV   rI   )rE  r=  rh           ?rJ   )rh  r*   z6repeating-radial-gradient(42px, blue -20px, lime 10px)r!   z9repeating-radial-gradient(42px, blue -140px, lime -110px)z,radial-gradient(42px, blue -20px, lime -1px)z)radial-gradient(42px, blue -20px, lime 0)z,radial-gradient(42px, blue -20px, lime 20px)z(radial-gradient(100px 120px, blue, lime)r;   z$radial-gradient(25% 40%, blue, lime)z0radial-gradient(circle closest-side, blue, lime)z>radial-gradient(circle closest-side at 150px 50px, blue, lime)r?   z=radial-gradient(circle closest-side at 45px 50px, blue, lime)r  z>radial-gradient(circle closest-side at 420px 50px, blue, lime)  z?radial-gradient(circle closest-side at 420px 281px, blue, lime)  z-radial-gradient(closest-side, blue 20%, lime)z6radial-gradient(closest-side at 300px 20%, blue, lime),  r   z6radial-gradient(closest-side at 10% 230px, blue, lime)r   rN   z1radial-gradient(circle farthest-side, blue, lime)z?radial-gradient(circle farthest-side at 150px 50px, blue, lime)r   z>radial-gradient(circle farthest-side at 45px 50px, blue, lime)c  z?radial-gradient(circle farthest-side at 420px 50px, blue, lime)z@radial-gradient(circle farthest-side at 220px 310px, blue, lime)r   6  z*radial-gradient(farthest-side, blue, lime)z7radial-gradient(farthest-side at 300px 20%, blue, lime)   z7radial-gradient(farthest-side at 10% 230px, blue, lime)h  z2radial-gradient(circle closest-corner, blue, lime)z@radial-gradient(circle closest-corner at 340px 80px, blue, lime)T  r   z=radial-gradient(circle closest-corner at 0 342px, blue, lime)V  r)   r	   z+radial-gradient(closest-corner, blue, lime)z:radial-gradient(closest-corner at 450px 100px, blue, lime)i  z9radial-gradient(closest-corner at 40px 210px, blue, lime)   rU   z3radial-gradient(circle farthest-corner, blue, lime)zCradial-gradient(circle farthest-corner at 300px -100px, blue, lime)  z>radial-gradient(circle farthest-corner at 400px 0, blue, lime)  z,radial-gradient(farthest-corner, blue, lime)z;radial-gradient(farthest-corner at 450px 100px, blue, lime)z:radial-gradient(farthest-corner at 40px 210px, blue, lime))r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   )r   rX   r   r=   )ri  rj  r   r=   )r   rX   r   rk  )r   rX   r   rl  )r   rX   r   rk  )r   rX   r   rB   g       @)r   rX   r   rB   g       @qq?)r{  r   )r   rX   r   rB   g       @(?)r   r|  )r   rn  rn  r   F]tE?)r}  r   )r   rn  rn  r   aa?)r   r~  )r   rX   r!   rV   )r   rX   r!   rV   )r   rX   r   rB   )r   rn  rn  r   )r   rX   r   r=   g333333?)r   rX   r   r=   g333333?)r   rX   r   rX   )rX   r?   r   r?   )r  r?   r   r  )ro  r?   r   rB   )ro  rp  r   rm  )r   rX   rV   r   g      ?)rq  r   r   r=   g333333?)rV   r   r   rV   g      ?)r   rX   r   r   )rX   r?   r   r   )r  r?   r   rr  )ro  r?   r   ro  )r   rs  r   rs  )r   rX   r   r   g      ?)rq  r   r   rq  g?)rV   r   r   ru  gqq?)r   rX   r   r   )rv  r   r   r=   )r   rw  r   r)   g      ?g       @g      @)r   rX   r   r   )rq  r  r   ry  )rz  r   r   ry  g      ?gqq?g?)mathsqrt)ra  rb  rc  rB  Zsqrt2r   r   r   test_radial_gradient&  s    











r  c              C   s  xbt ddD ]T} xNt ddD ]@}td| |f \}|j\}|j\}|j\}t|jdkstqW qW d}xdD ]}xtd	|||d
| f \}|j\}|j\}|j\}t|jdkstt|jd jdkst|jd jd jd j}| st|jd
rd}P qt|d7 }qtW qnW dS )zuTest that no floating point error occurs during shrink to fit.

    See bugs #325 and #288, see commit fac5ee9.

    r   r!   a  
                <style>
                    @page { size: 100000px 100px }
                    p { float: left; margin-left: 0.%iin; font-size: 0.%iem;
                        font-family: "ahem" }
                </style>
                <p>this parrot is dead</p>
            r,   r?   r=     '  z
                <style>
                    @page { size: %i0pt %i0px }
                    p { font-size: %ipt; font-family: "ahem" }
                </style>
                <p>mmm <b>%s a</b></p>
            r   r	   r   N)r   r,   r!   r?   r=   r  r  )r   r	   )ranger/   r   rP   r   r]   endswith)r4   rj   r   r   r   r$   lettersr]   r   r   r   'test_shrink_to_fit_floating_point_error  s0    

r  )8__doc__
__future__r   r   r  ro   Ztesting_utilsr   r   r   r   Zformatting_structurer
   Z
test_boxesr   r/   r   r   r:   rR   rW   rY   rg   rn   rp   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r#  r/  r0  r1  r3  r4  r5  r6  r8  rd  r  r  r   r   r   r   <module>   st   	 |^#r,/'  Y0  p  9 ]< .- dG * 6 @2?