3
ybWD                 @   sn  d Z ddlZyddlZW n  eefk
r8   ddlZY nX ddlmZ ddlm	Z	 ddl
mZmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZ dd	lmZ dd
lmZ ddlmZ ddl mZ ej!ej"ej#dZ$ej!ej%ej#dZ&ej'ej(dZ)ej*ej+dZ,G dd de-Z.G dd de.Z/G dd de/Z0G dd de/Z1G dd de.Z2G dd de.Z3dS )z
Cairo surface creators.

    N   )Tree   )color)apply_filter_after_paintingapply_filter_before_paintinggradient_or_pattern	parse_def
paint_maskprepare_filter)node_format	transform	normalizepainturlsapply_matrix_transform
PointErrorrect)	PATH_TAGS)TAGS)size)units)optimizeSpeedZ
crispEdgesgeometricPrecision)r   optimizeLegibilityr   )r   r   c               @   sl   e Zd ZdZdZedddZdddZedd Z	ed	d
 Z
dd Zdd Zdd Zdd Zdd ZdS )Surfacea)  Abstract base class for CairoSVG surfaces.

    The ``width`` and ``height`` attributes are in device units (pixels for
    PNG, else points).

    The ``context_width`` and ``context_height`` attributes are in user units
    (i.e. in pixels), they represent the size of the active viewport.

    Nc       	      K   s   |j dd}|j dd}|j dd}|j dd}||d< tf |}|dkrTtj }n|}| |||d||j  |dkr~|j S dS )a$  Convert a SVG document to the format for this class.

        Specify the input by passing one of these:

        :param bytestring: The SVG source as a byte-string.
        :param file_obj: A file-like object.
        :param url: A filename.

        And the output with:

        :param write_to: The filename of file-like object where to write the
                         output. If None or not provided, return a byte string.

        Only ``source`` can be passed as a positional argument, other
        parameters are keyword-only.

        dpi`   parent_widthNparent_heightwrite_to
bytestring)popr   ioBytesIOfinishgetvalue)	clsr!   kwargsr   r   r   r    treeoutput r+   Y/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/cairosvg/surface/__init__.pyconvertO   s    

zSurface.convertc       
      C   sD  d| _ || | _| _ddg| _ddg| _d| _|j|d f|i| _|rx|j| _|j	| _	|j
| _
|j| _|j| _|j| _n$i | _i | _	i | _
i | _i | _i | _d | _| _|| _|| _t| d| _d| _t| |\}}}	| j|| j || j \| _ | _| _t j| j | _| jj| j| j | j|||	 | jjdd | j| dS )au  Create the surface from a filename or a file-like object.

        The rendered content is written to ``output`` which can be a filename,
        a file-like object, ``None`` (render in memory but do not write
        anything) or the built-in ``bytes`` as a marker.

        Call the ``.finish()`` method to make sure that the output is
        actually written.

        Nr   id12ptT) cairocontext_widthcontext_heightcursor_positioncursor_d_positiontext_path_widthurlZ
tree_cachemarkersZ	gradientspatternsmaskspathsfilters_old_parent_nodeparent_noder*   r   r   	font_sizestroke_and_fillr   _create_surfacedevice_units_per_user_unitswidthheightZContextcontextscaleset_context_sizemove_to	draw_root)
selfr)   r*   r   Zparent_surfacer   r   rB   rC   viewboxr+   r+   r,   __init__p   sD    


zSurface.__init__c             C   s   d| j tjd   S )zSurface resolution.r   pt)r   r   ZUNITS)rI   r+   r+   r,   points_per_pixel   s    zSurface.points_per_pixelc             C   s   | j S )zRatio between Cairo device units and user units.

        Device units are points for everything but PNG, and pixels for
        PNG. User units are pixels.

        )rM   )rI   r+   r+   r,   rA      s    z#Surface.device_units_per_user_unitsc             C   s   | j | j||}|||fS )z5Create and return ``(cairo_surface, width, height)``.)surface_classr*   )rI   rB   rC   cairo_surfacer+   r+   r,   r@      s    zSurface._create_surfacec             C   s   |r|\}}}}|| | _ | _|| ||  }}	tj }
||	kr~|
j|||	  d d |
j|	|	 |
j| | |	 |  nb||	k r|
jd|||  d  |
j|| |
j| | |	 |  n|
j||	 |
j| |  t| |
 n|| | _ | _dS )z6Set the Cairo context size, set the SVG viewport size.r   r   N)r1   r2   r0   ZMatrix	translaterE   r   )rI   rB   rC   rJ   xyZx_sizeZy_sizeZx_ratioZy_ratioZmatrixr+   r+   r,   rF      s"    zSurface.set_context_sizec             C   s   | j j  dS )zRead the surface content.N)r0   r%   )rI   r+   r+   r,   r%      s    zSurface.finishc             C   s   | j | dS )zDraw the root ``node``.N)draw)rI   noder+   r+   r,   rH      s    zSurface.draw_rootc       !         s   j }t |jdd _ |jdkrBx|jD ]}t | q,W dS d|kr\t |d dksvd|krzt |d dkrzdS dg|_g |_ j _	| _ j
j  t |jd t|jd	}|r|d d
d nd}t|jd}|r|d d
d nd}t|jdd
}|rt || |s<|s<|d
k rF|jrF j
j   j
jt |jddt |jdd |jtkr|jdsd|d< |jd}	|	dkr j
jtj |	dkrʈ j
jtj |jd}
|
dkr j
jtj |
dkr j
jtj t|jddj }|rX fdd|D }t|rXt |jd} j
j|| t|jdd} j
j| t|jd}t |dkrft |d d}t |d
 d}t |d d}t |d d}t |jdd}t |jdd}t |jdd}t |jdd} j
j   j
j!||  j
j"|||| | || |   j
j#   j
j$  t|jd }|rr j%j|d d
d }|rr j
j  |jd!d"krt |jdd}t |jdd}t |jdd}t |jdd} j
j!||  j
j&|| d#|_d$ _' j(| d% _' j
j#  |jd&d'krZ j
j)tj*  j
j$   j
j)tj+ |jt,kryt,|j  | W n t-k
r   Y nX t|jd(d
}t|jd)d
}|d
k r|j r||9 }||9 }|jd*d+d,k}|o|jd-d.d/k} j
j.t/j|jd0tj0  j
j1 }|j.t2j|jd1tj0 |j3t4j|jd1tj5 |j6t7j|jd1tj8  j
j9|  j'r|r|jt,kr j
j  t:|jd2d3\}} t; ||s
|jd4d'kr j
j)tj*  j
j<t=| |   j
j>   j
j#   j
j   j
j?t |jd t:|jd5\}} t; ||sr j
j<t=| |   j
j@   j
j#  n|s j
jA  |r|jd<krx|jD ]} j(| qW |s|s|d
k r@|jr@ j
jB  |r tC || |r"| jDkr"tE ||| n j
jF| |r@tG || |jd;krfddg _Hddg _Id _J|jKsx j
j#   j	 _| _ dS )=zDraw ``node`` and its children.z	font-sizer/   defsNrB   r   rC   r   maskr   filteropacityrQ   rR   zstroke-width1zstroke-linecapZsquareroundzstroke-linejoinZbevelzstroke-dasharray c                s   g | ]}t  |qS r+   )r   ).0Zdash)rI   r+   r,   
<listcomp>  s    z Surface.draw.<locals>.<listcomp>zstroke-dashoffsetzstroke-miterlimit   clipr      z	clip-pathZclipPathUnitsZobjectBoundingBoxgFTz	clip-ruleZevenoddzstroke-opacityzfill-opacitydisplayinlineZnoneZ
visibilityvisibleZhiddenzshape-renderingztext-renderingfillZblackz	fill-rulestrokelinearGradientradialGradientmarkerpatternclipPathtext)rg   rh   ri   rj   rV   rk   rW   )Lr>   r   gettagchildrenr	   ZtangentsZpending_markersr=   r<   rD   saver   r   floatr   Z
push_grouprG   r   Zset_line_capr0   ZLINE_CAP_SQUAREZLINE_CAP_ROUNDZset_line_joinZLINE_JOIN_ROUNDZLINE_JOIN_BEVELr   splitsumZset_dashZset_miter_limitr   lenrP   Z	rectanglerestorer_   r:   rE   r?   rS   Zset_fill_ruleZFILL_RULE_EVEN_ODDZFILL_RULE_WINDINGr   r   Zset_antialiasSHAPE_ANTIALIASZANTIALIAS_DEFAULTZget_font_optionsTEXT_ANTIALIASZset_hint_styleTEXT_HINT_STYLEZHINT_STYLE_DEFAULTZset_hint_metricsTEXT_HINT_METRICSZHINT_METRICS_DEFAULTZset_font_optionsr   r   Zset_source_rgbar   Zfill_preserveZset_line_widthrf   new_pathZpop_group_to_sourcer   r9   r
   Zpaint_with_alphar   r3   r4   r5   root)!rI   rT   Zold_font_sizechildr9   rV   r;   Zfilter_rX   Zline_capZjoin_capZ
dash_arrayZdashesoffsetZmiter_limitZrect_valuestoprightZbottomleftrQ   rR   rB   rC   Z
clip_pathspathZstroke_opacityZfill_opacityrb   rd   Zfont_optionsZpaint_sourceZpaint_colorr+   )rI   r,   rS      s(   

























 



zSurface.draw)N)NNN)__name__
__module____qualname____doc__rN   classmethodr-   rK   propertyrM   rA   r@   rF   r%   rH   rS   r+   r+   r+   r,   r   A   s   	  
2
r   c               @   s   e Zd ZdZdd ZdS )MultipageSurfacez@Abstract base class for surfaces that can handle multiple pages.c             C   s   d | _ d | _dd |jD }|rx|D ]|}t| |\}}}| jj  | j||| || j9 }|| j9 }| jj	||f | j
j|| | j| | jj  | j
j  q&W n
| j| d S )Nc             S   s   g | ]}|j d kr|qS )svg)rn   )r\   r|   r+   r+   r,   r]     s    z.MultipageSurface.draw_root.<locals>.<listcomp>)rB   rC   ro   r   rD   rp   rF   rA   Z
page_sizesappendr0   Zset_sizerS   ru   Z	show_page)rI   rT   Zsvg_childrenZpagerB   rC   rJ   r+   r+   r,   rH     s     





zMultipageSurface.draw_rootN)r   r   r   r   rH   r+   r+   r+   r,   r     s   r   c               @   s   e Zd ZdZejZdS )
PDFSurfacez$A surface that writes in PDF format.N)r   r   r   r   r0   r   rN   r+   r+   r+   r,   r     s   r   c               @   s   e Zd ZdZejZdS )	PSSurfacez+A surface that writes in PostScript format.N)r   r   r   r   r0   r   rN   r+   r+   r+   r,   r     s   r   c                   s,   e Zd ZdZdZdd Z fddZ  ZS )
PNGSurfacez$A surface that writes in PNG format.r   c             C   s*   t |}t |}tjtj||}|||fS )z5Create and return ``(cairo_surface, width, height)``.)intr0   ZImageSurfaceZFORMAT_ARGB32)rI   rB   rC   rO   r+   r+   r,   r@     s    zPNGSurface._create_surfacec                s&   | j dk	r| jj| j  tt| j S )zRead the PNG surface content.N)r*   r0   Zwrite_to_pngsuperr   r%   )rI   )	__class__r+   r,   r%     s    
zPNGSurface.finish)r   r   r   r   rA   r@   r%   __classcell__r+   r+   )r   r,   r     s   r   c               @   s   e Zd ZdZejZdS )
SVGSurfacezA surface that writes in SVG format.

    It may seem pointless to render SVG to SVG, but this can be used
    with ``output=None`` to get a vector-based single page cairo surface.

    N)r   r   r   r   r0   r   rN   r+   r+   r+   r,   r     s   r   )4r   r#   	cairocffir0   ImportErrorOSErrorparserr   Zcolorsr   rU   r   r   r   r	   r
   r   Zhelpersr   r   r   r   r   r   r   r   r   r   Ztagsr   r   r   r[   ZANTIALIAS_FASTZANTIALIAS_NONEZANTIALIAS_BESTrv   ZANTIALIAS_GOODrw   ZHINT_STYLE_NONEZHINT_STYLE_FULLrx   ZHINT_METRICS_OFFZHINT_METRICS_ONry   objectr   r   r   r   r   r   r+   r+   r+   r,   <module>   s>    (



  ^