3
?Wɋ                 @   s6  d Z ddlmZmZ ddlZddlZddlZddlZddl	m
Z
 ddlmZ ddlmZ ddlmZ d>Zejdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd?d d!Zd"d# Zd$d% Zd&d' Zd(d) Z d*d+ Z!d@d,d-Z"d.d/ Z#d0d1 Z$d2d3 Z%d4d5 Z&d6d7 Z'd8d9 Z(d:d; Z)d<d= Z*dS )Az
    weasyprint.draw
    ---------------

    Take an "after layout" box tree and draw it onto a cairo context.

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

    )divisionunicode_literalsN   )boxes)StackingContext)show_first_line)xrangetoprightbottomleftc          
   c   s"   | j   z
dV  W d| j  X dS )zASave and restore the context when used with the ``with`` keyword.N)saverestore)context r   O/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/weasyprint/draw.pystacked   s    
r   c             C   s2  || }|dt | d d d   }|| }d|   ko>dk n  rV|| || |fS d|   kohdk n  r|| || |fS d|   kodk n  r||| || fS d|   kodk n  r||| || fS d|   kodk n  r || ||| fS d|   kod	k n  r.|| ||| fS d
S )z%Transform a HSV color to a RGB color.r   <      r   x         i,  ih  N)abs)hue
saturationvaluecxmr   r   r   hsv2rgb(   s    r   c             C   s   t | ||}t| ||}|| }|dkr.d}nX|| krLd|| | d  }n:||krjd||  | d  }n||krd| | | d  }|dkrdn|| }|||fS )z%Transform a RGB color to a HSV color.r   r      r      )maxmin)redgreenblueZcmaxZcmindeltar   r   r   r   r   rgb2hsv;   s    r(   c             C   s<   t | j| j| j\}}}|d }|d }t|||| jf S )zReturn a darker color.g      ?g      ?)r(   r$   r%   r&   r   alpha)colorr   r   r   r   r   r   darkenL   s    r+   c             C   sP   t | j| j| j\}}}dd| d  }|r<dd| d  }t|||| jf S )zReturn a lighter color.r   g      ?g      ?)r(   r$   r%   r&   r   r)   )r*   r   r   r   r   r   r   lightenT   s
    r,   c             C   sL   t j| }t||jj|dd t|| j|dd t|| | t||| dS )zDraw the given PageBox.F)clip_boxN)r   Z	from_pagedraw_backgroundbox
backgroundZcanvas_backgrounddraw_borderdraw_stacking_context)pager   enable_hintingstacking_contextr   r   r   	draw_page]   s    

r6   c             C   s   t | |j| t|tjrt| ||| |jjdkrt| || x\|j	D ]D}x>|j	D ]4}x.|j	D ]$}|jj
dksx|j r`t| || q`W qTW qHW qt| || nt| || d S )NZseparateshow)r.   r0   
isinstancer   ZTableBoxdraw_table_backgroundsstyleborder_collapser1   childrenempty_cellsemptydraw_collapsed_borders)r   r3   r/   r4   	row_grouprowcellr   r   r   draw_box_background_and_borderi   s    rC   c             C   s  t |  |j}|j r|jjr|jj\}}}}|dkr>d}|dkrJd}|dkrZ|j }|dkrj|j }| j|j | |j	 | || ||  | j  |jj
dk r| j  |jry|jj j  W n tjk
r   dS X | j|j t|tjtjtjtjfrt| |j|| t | f |jjdkrFt| |j  | j  x|jD ]}t| || qNW x |jD ]}	t| |j|	| qlW x|j D ]}t| || qW t|tj!rt"| |j|| x|g|j# D ]r}	t$|	dd}
|
rt"| |j|
| t|	tj%rt&| |	 n0x.|	j'D ]$}t|tj(rt"| |j|| qW qW x|j)D ]}t| || qNW x|j*D ]}t| || qlW W dQ R X t+| || |jj
dk r| j,  | j-|jj
 W dQ R X dS )z+Draw a ``stacking_context`` on ``context``.autor   r   NvisibleZoutside_list_marker).r   r/   Zis_absolutely_positionedr:   clipborder_heightborder_width	rectangleborder_box_xborder_box_yZopacityZ
push_groupZtransformation_matrixcopyinvertcairoZ
CairoError	transformr8   r   ZBlockBoxZ	MarginBoxInlineBlockBoxZTableCellBoxrC   r3   Zoverflowrounded_box_pathrounded_padding_boxZnegative_z_contextsr2   Zblock_level_boxesZfloat_contexts	InlineBoxdraw_inline_levelZblocks_and_cellsgetattrZReplacedBoxdraw_replacedboxr<   LineBoxZzero_z_contextsZpositive_z_contextsdraw_outlinesZpop_group_to_sourceZpaint_with_alpha)r   r5   r4   r/   r	   r
   r   r   Zchild_contextblockZ
marker_boxchildr   r   r   r2   z   s    


r2   c             C   sV  |\}}}}}}}}	d|kr d}d|kr,d}d|kr8d	}d|	krDd
}	||||	fdkrh| j |||| dS | j|| xtdd|f|d|f|||fd||	ffD ]\}
\}}\}}| j  | j|| ||  t||}| r| jt|| dt|| d | j|r
dnd| |rdnd| |d|
 t	j
 d d|
 t	j
 d  | j  qW dS )a,  Draw the path of the border radius box.

    ``widths`` is a tuple of the inner widths (top, right, bottom, left) from
    the border box. Radii are adjusted from these values. Default is (0, 0, 0,
    0).

    Inspired by Cairo Cookbook
    http://cairographics.org/cookbook/roundedrectangles/

    r   r!   Nr   r      )r   r   )r   r   )r   r   )r   r   r   r   )r\   )r\   r\   r\   r\   r]   )rI   move_to	enumerater   	translater"   Zscaler#   Zarcmathpir   )r   radiir   ywhtltrbrbliZrxZryZradiusr   r   r   rQ      s0    8
 "$rQ   Tc             C   s   |dkrdS t |  |r&| jtj |rRx|jd jD ]}t| | q8W | j  |jj	dkrt | < |jd j
}|r| j|  | j  | j|j  | j  W dQ R X x t|jD ]}t| ||j qW W dQ R X dS )zDraw the background color and image to a ``cairo.Context``.

    If ``clip_box`` is set to ``False``, the background is not clipped to the
    border box of the background, but only to the painting area.

    Nr   r   r]   r]   )r   set_antialiasrN   ANTIALIAS_NONEZlayersZclipped_boxesrQ   rF   r*   r)   painting_arearI   set_source_rgbapaintreverseddraw_background_imageimage_rendering)r   bgr4   r-   r/   rn   layerr   r   r   r.   	  s&    


r.   c       	      C   s   x:|j D ]0}t| |j| x|jD ]}t| |j| q"W qW xx|jD ]n}t| |j| xZ|jD ]P}t| |j| x<|jD ]2}|jjdks|jjdks|j rxt| |j| qxW q^W qDW dS )z:Draw the background color and image of the table children.Zcollapser7   N)Zcolumn_groupsr.   r0   r<   r:   r;   r=   r>   )	r   r3   tabler4   Zcolumn_groupcolumnr@   rA   rB   r   r   r   r9   ,  s    r9   c             C   s  |j d krd S |j\}}}}|j\}}}	}
|j\}}|j\}}|j\}}|dkrZ|d }nJ|dkrh|}n<|dkstttj|	| }|dkr|	| |d  }d}n|}|dkr|d }nL|d	kr|}n>|dksttj|
| }|dk r|
| |d  }d}n|}t	j
d ||}t	j|}|jdd|| |j  |j j|||| t	j|}|jt	j t| L |js| j|||| | j  | j|| ||  | j| | j  W d Q R X d S )
Nz	no-repeatr   repeatroundspacer   r   )rx   ry   )rx   ry   )imagern   Zpositioning_areapositionrx   sizeAssertionErrorra   floorrN   Z
PDFSurfaceZContextrI   rF   drawZSurfacePatternZ
set_extendZEXTEND_REPEATr   Z	unboundedr`   Z
set_sourcerp   )r   ru   rs   Z
painting_xZ
painting_yZpainting_widthZpainting_heightZpositioning_xZpositioning_yZpositioning_widthZpositioning_height
position_x
position_yZrepeat_xZrepeat_yZimage_widthZimage_heightZrepeat_widthZ	n_repeatsZrepeat_heightZsub_surfaceZsub_contextpatternr   r   r   rr   <  sV    












rr   c             C   s   | ||  |||  fS )z3Increment X and Y coordinates by the given offsets.r   )r   rd   Zoffset_xoffset_yoffsetr   r   r   	xy_offsety  s    r   c             C   sd   | dkr(|dk| dkA }|r t nt|S | d	kr`|d
k| dkA rPt |t|fS t|t |fS |S )Ninsetoutsetr	   r   ridgegroove)r   r   )r	   r   )r   r   )r	   r   )r,   r+   )r:   r*   sideZ
do_lightenr   r   r   styled_color~  s    r   c       
         sX   fdd} j jdkr&|  dS  fddtD }tdd |D rT|  dS  fd	dtD  fd
dttD }t|tdtdfkrttdkrt |d d  |  dS xtt||D ]t\}}}}	|dks| rqt	J t
|	|| j dd | j dd  t |	t|	|| W dQ R X qW |  dS )z+Draw the box border to a ``cairo.Context``.c                 s    j jdkp j jdk} | r҈ j jrddd j jf}x jdd D ]}tx |j j j j j d  }||j j j j	f}t
 j j j jd|| t|| j jt j j j jdd W dQ R X qDW dS )zDraw column borders.rD   r   r   Nr   r   Zcolumn_rule_color)r:   Zcolumn_widthZcolumn_countZcolumn_rule_widthr<   r   r   Z
column_gapr   heightclip_border_segmentZcolumn_rule_styledraw_rect_borderr   	get_color)columnsborder_widthsrZ   r   
border_box)r/   r   r4   r   r   draw_column_border  s(    


z'draw_border.<locals>.draw_column_borderrE   Nc                s   g | ]}t  d | qS )zborder_%s_width)rU   ).0r   )r/   r   r   
<listcomp>  s    zdraw_border.<locals>.<listcomp>c             s   s   | ]}|d kV  qdS )r   Nr   )r   widthr   r   r   	<genexpr>  s    zdraw_border.<locals>.<genexpr>c                s   g | ]} j jd | qS )zborder_%s_color)r:   r   )r   r   )r/   r   r   r     s    c                s(   g | ] \}}| j o" jd |  qS )zborder_%s_style)r)   r:   )r   rk   r   )r/   colorsr   r   r     s   soliddoubler   r   r!   )r   )r   )r:   
visibilitySIDESallr_   setlendraw_rounded_borderzipr   r   rounded_border_boxr   )
r   r/   r4   r   widthsZstylesr   r   r*   r:   r   )r/   r   r   r4   r   r1     s6    

"r1   c       4   	      sZ  |rD|dkrDt jj|d dkrDt jjd| dkrDjtj |\}}	}
}|pVd\\}}\}}\}}\}}|p|d|f \}}}}dd }dd }d	kr|||||\\}}}|| || |\\}}}|}d} d |	nd
kr8|| || |\\}}}|| | | | \\}}}|}d} d ||
 ndkr||| || \\}}}|| | | | \\}}}|}d} d |	| nJdkr|||||\\}}}||| || \\}}}|}d} d |dkrz||d  | | |d   }!}"| |d  | | |d   }#}$|
| | }%|
}&j||
  j|
 d j|| j| |
 | | |  ndkr|  | |d  ||d   }!}"|  | |d  | |d   }#}$|| | }%|}&j|	|  jd|  j|| j| | | | |  jtj	 |dkrN|dkr@|nd| |sT|rZ||!|"}'||#|$}(|%|' |( }&t
|& })|r|r|&|)|)d   n|&|)|)d  d  tt j|'d   }*tt j|(d   }+tt j|% }, fdd}-|-|*|,| ||	|||'\},}.|-|+|,|  ||
 |	| |||(d },|%dkrNxtd|,dD ]}/|/|.7 }/dkrt|| |/  || }0t|| |/d   ||
 | }1| dk r|nd }2|2| }3nddkr:t|	| |/  |	| }2t|	| |/d   |	| | }3| dkr,|nd }0|0| }1j|0|2|1|0 |3|2  qdW nj  |&t
|& t
|& d d   pdxtdtt
|& dD ]}/d	krΈj||/  |	| nzd
krj||
 | |	|/  | nNdkr&j||/  |	| | | n"dkrj||	|/  | qW j  dS )zClip one segment of box border.

    The strategy is to remove the zones not needed because of the style or the
    side before painting.

    dottedr   r   r!   c             S   s8   t | t |kr,t |t |kr,| |fdfS ||fdfS )a  Get the point use for border transition.

        The extra boolean returned is ``True`` if the point is in the padding
        box (ie. the padding box is rounded).

        This point is not specified. We must be sure to be inside the rounded
        padding box, and in the zone defined in the "transition zone" allowed
        by the specification. We chose the corner of the transition zone. It's
        easy to get and gives quite good results, but it seems to be different
        from what other browsers do.

        TF)r   )x1y1x2y2r   r   r   transition_point  s    ,z-clip_border_segment.<locals>.transition_pointc          	   S   sL   | | | |  }t jd | |  dd|d  dt jdd|d       S )a6  Return the length of the half of one ellipsis corner.

        Inspired by [Ramanujan, S., "Modular Equations and Approximations to
        pi" Quart. J. Pure. Appl. Math., vol. 45 (1913-1914), pp. 350-372],
        wonderfully explained by Dr Rob.

        http://mathforum.org/dr.math/faq/formulas/

           r   r[   r   
   r!   )ra   rb   sqrt)abr   r   r   r   corner_half_length  s    
z/clip_border_segment.<locals>.corner_half_lengthr	   r
   r   r   r[   r   dashedg      ?c                s  | s|d dfS xt d| dD ]}|d7 }d  | ||  |  d tj }	|dkr`tntd  | |d |  |  d tj  tj d }
d
krj|| |  j|| || d tj|
   j|| || d tj|	   nbdkrhj| ||  j|| || tj|
   j|| || tj|	   |
 tj d kr |	|
 d  | |d |  |  d tj |	  }|d7 }P q W d tj d |
 |
|	   }||fS )Nr   r   r   g      ?r!   r	   r   r   r
   )r	   r   )r   r
   )rangera   rb   r#   r"   r^   Zline_totan)Zdasheslinewayr   rd   ZpxpyZchlrk   Zangle1Zangle2r   )angler   dashmain_offsetr   r   r   	draw_dotsH  s@    &(

0z&clip_border_segment.<locals>.draw_dotsgư>Nr   r   )r   )r   r   r   r   r]   r]   )r	   r   )r   r
   )r   r   )r	   r   )r   r
   )ra   hypotZuser_to_devicerl   rN   rm   r^   rel_line_toset_fill_ruleFILL_RULE_EVEN_ODDry   intceilr   r   r"   r#   rI   rF   )4r   r4   r:   r   r   r   r   rc   bbxbbybbwbbhZtlhZtlvZtrhZtrvZbrhZbrvZblhZblvbtri   bbrj   r   r   Zpx1Zpy1Zrounded1Zpx2Zpy2Zrounded2r   Za1Zb1Za2Zb2Zline_lengthlengthZchl1Zchl2Zdash_lengthZdashes1Zdashes2r   r   r   rk   r   r   r   r   r   )r   r   r   r   r   r   r     s     





 
 "


'"

"

" (



r   c             C   s   | j tj t| |j  |dkrt| |jd	 | j|d   | j  t| |jd
 t| |j  | j|d   | j  d S |dkrt| |jd t| |jd t| |j  | j|  | j  d S )Nr   r   r   r   r   r   r[   )r   r   g      ?g      ?gUUUUUU?gUUUUUU?)	r   rN   r   rQ   rR   Zrounded_box_ratioro   fillr   )r   r/   r:   r*   r   r   r   r     s"    
r   c             C   s  | j tj |\}}}}|\}	}
}}| j|  |dkr | j||d  ||	d  |||
 d  ||	| d   | j|d   | j  | j||d  ||	d  |||
 d  ||	| d   | j|| ||	 || |
 ||	 |  | j|d   | j  d S |dkr| j||d  ||	d  |||
 d  ||	| d   | j||d d  ||	d d  |||
 d d  ||	| d d   | j|| ||	 || |
 ||	 |  | j|  | j  d S )	Nr   r   r   r   r   r   r[   )r   r   )r   rN   r   rI   ro   r   )r   r/   r   r:   r*   r   r   r   r   r   ri   r   rj   r   r   r   r     s8    

  $
 ((
r   c       	      C   s   |j j}|j jd}|j j}|j jdkr|dkr|jdkr|j | |j | |j d|  |j	 d|  f}xPt
D ]H}t| 6 t| ||||| t| |d|f |t||| W d Q R X qtW t|tjrx&|jD ]}t|tjrt| || qW d S )NZoutline_colorrE   r   r   r!   )r:   Zoutline_widthr   Zoutline_styler   r)   rJ   rK   rH   rG   r   r   r   r   r   r8   r   Z	ParentBoxr<   ZBoxrX   )	r   r/   r4   r   r*   r:   Zoutline_boxr   rZ   r   r   r   rX     s"     

rX   c                s  dd |j D }|j}|o|s"dS dd |j D 
t|jt|t|tks\td |d  g7 
j
d |d   |j\|j d jrt|j d j nd|j d j	rt|j d j }nd}|j
}|r|  nd dk rd|r| d nd t}|  fdd	g d	fd
d		
fdd}	
fdd}	xtD ]}
|	|
d qW xLtD ]@}|d| x.tD ]"}
||
d | |	|
|d  qW qW jtjdd xD ]z}|\}}}}}}|dkr0|dddf}nddd|f}t| 2 t| |||||| t| |||t||| W dQ R X qW dS )z/Draw borders of table cells when they collapse.c             S   s   g | ]}|j D ]
}|jqqS r   )r<   r   )r   r@   rA   r   r   r   r     s    z*draw_collapsed_borders.<locals>.<listcomp>Nc             S   s   g | ]}|j D ]
}|jqqS r   )r<   r   )r   r@   rA   r   r   r   r     s    r   r   c                s8   | t | k r| S | t | kr,|  S |   S d S )N)r   )rd   
horizontal)body_rows_offsetfirst_footer_rowfooter_rows_offsetheader_rowsr   r   
row_number	  s
    z*draw_collapsed_borders.<locals>.row_numberTc       	         s   d}x|D ]\}}|rFd|  ko( k n  otd|  ko@kqt  n.d|  koX kn  r
d|  kopk n  r
|| d}| | | \}\}}}t ||}q
W |d S )Nr   )r   r   )r"   )	Zborder_listZyx_pairsverticalresultrd   r   yy_r   )grid_height
grid_widthr   r   r   half_max_width  s    40z.draw_collapsed_borders.<locals>.half_max_widthc       
         s   |dd}| |  \}\}}}|dks4|j dkr8d S  |  }| || d f|| fgdd }|d  |d | d f|d | fgdd }	j||||d||d  |d|	| ff d S )NF)r   r   r   )r   r   r   )r)   append)
r   rd   r   scorer:   r   r*   Zpos_xZpos_y1Zpos_y2)column_positionsr   horizontal_bordersr   row_positionssegmentsvertical_bordersr   r   add_vertical   s    
$
z,draw_collapsed_borders.<locals>.add_verticalc       
   
      s   |dd}| |  \}\}}}|dks4|j dkr8d S | } |  |d | f|| fg } | d  |d | d f|| d fg }	j||||d|||d  |	| dff d S )NT)r   r   r   r	   r   )r)   r   )
r   rd   r   r   r:   r   r*   Zpos_yZpos_x1Zpos_x2)r   r   r   r   r   r   r   r   r   add_horizontal.  s    
 
z.draw_collapsed_borders.<locals>.add_horizontal)keyr	   r]   r]   r]   r]   r]   r]   r]   )T)r<   column_widthslistr   r   r~   r   Zcollapsed_border_gridZ	is_headerZ	is_footerskipped_rowsr   sortoperator
itemgetterr   r   r   r   )r   rv   r4   Zrow_heightsr   Zfooter_rowsr   Zoriginal_grid_heightr   r   r   rd   Zsegmentr   r:   r   r*   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r?     sl    







r?   c             C   s   |j jdks |jdks |jdkr$dS t| J t| |j  | j  | j|j	 |j
  |jj| |j|j|j j W dQ R X dS )zADraw the given :class:`boxes.ReplacedBox` to a ``cairo.context``.rE   r   N)r:   r   r   r   r   rQ   Zrounded_content_boxrF   r`   Zcontent_box_xZcontent_box_yreplacementr   rs   )r   r/   r   r   r   rV   Z  s     
rV   c             C   s   t |tr.|}t |jtjs tt| || nt| |j| t	| || t |tj
tjfrxl|jD ],}t |tjrt| || qbt| ||| qbW n4t |tjrt| | nt |tjstt| || d S )N)r8   r   r/   r   rP   r~   r2   r.   r0   r1   rS   rW   r<   ZTextBox	draw_textrT   ZInlineReplacedBoxrV   )r   r3   r/   r4   r5   rZ   r   r   r   rT   g  s    
rT   c             C   s   |j jst|j jdkrdS | j|j|j|j  | j|j j	  t
| |j| |j j}|jj }|j jd }|r||dk r|d}d|krt| ||j|j |d  || d|krt| ||j|j |d  || d|krt| ||j|j || dS )	zDDraw ``textbox`` to a ``cairo.Context`` from ``PangoCairo.Context``.rE   N   r   Zoverliner   Z	underlinezline-through)r:   Z	font_sizer~   r   r^   r   r   Zbaselinero   r*   r   Zpango_layoutZtext_decorationZget_font_metricsdraw_text_decorationZascentZunderline_positionZstrikethrough_position)r   textboxr4   valuesZmetrics	thicknessr   r   r   r   }  s4    

r   c             C   sj   t | X |r| jtj | j|jj  | j| | j|j	|j
|  | j|jd | j  W dQ R X dS )z;Draw text-decoration of ``textbox`` to a ``cairo.Context``.r   N)r   rl   rN   rm   ro   r:   r*   Zset_line_widthr^   r   r   r   r   Zstroke)r   r   r   r   r4   r   r   r   r     s    

r   )r	   r
   r   r   )T)NN)+__doc__
__future__r   r   
contextlibra   r   Z	cairocffirN   Zformatting_structurer   Zstackingr   textr   compatr   r   contextmanagerr   r   r(   r+   r,   r6   rC   r2   rQ   r.   r9   rr   r   r   r1   r   r   r   rX   r?   rV   rT   r   r   r   r   r   r   <module>   sF   
	f)
#=H
 Fz#