
gbc        	   @   sU  d  Z  d Z d Z d d l Z e d k rg y$ d d l m Z d d l m Z Wqg e j d  qg Xn  d d l	 Z	 e	 j
 d d k s t  e	 j
 d	 d k  r d	 Z d Z e Z n  d
 e	 _ i  d  Z i  d  Z d   Z d   Z d   Z d dv d     YZ d dw d     YZ d e f d     YZ d e f d     YZ d dx d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d  e f d!     YZ d" e f d#     YZ d$ e f d%     YZ  d& e f d'     YZ! d( e f d)     YZ" d* e f d+     YZ# d, e f d-     YZ$ d. e f d/     YZ% d0 e f d1     YZ& d2 e f d3     YZ' d4 e f d5     YZ( d6 e f d7     YZ) d8 e f d9     YZ* d: e f d;     YZ+ d< e f d=     YZ, d> e f d?     YZ- d@ e f dA     YZ. dB e f dC     YZ/ dD e f dE     YZ0 dF e f dG     YZ1 dH e f dI     YZ2 dJ e f dK     YZ3 dL e f dM     YZ4 dN e f dO     YZ5 dP e f dQ     YZ6 dR e f dS     YZ7 dT e f dU     YZ8 dV e f dW     YZ9 dX e f dY     YZ: dZ e f d[     YZ; d\ e f d]     YZ< d^ dy d_     YZ= e> d` k rQe=   Z? e< dz  Z@ e db db dc dc dd  ZA e@ jB eA  e& de  ZC e@ jB eC  e/ df  ZD e d d dg d  ZE eD jB eE  e d d d	 dh  ZF eD jB eF  e d di  ZG x@ eH dj  D]2 ZI eG jJ dk dg d dk  eG jJ di dg d dk  qWe8 eG dk  ZK dl eK jL dm <dn eK jL do <eD jB eK  e@ jB eD  x9 eH dp dq dp  D]% ZI e3 dr eI d  ZM e@ jB eM  qWx_ eH d dq dp  D]K ZI xB eH dg ds dk  D]. ZN e eI eN d	 dh dt du  ZF e@ jB eF  q WqWe? jO e@  e? jP   GHn  d S({   s  Use SVGdraw to generate your SVGdrawings.

SVGdraw uses an object model drawing and a method toXML to create SVG graphics
by using easy to use classes and methods usualy you start by creating a drawing eg

    d=drawing()
    #then you create a SVG root element
    s=svg()
    #then you add some elements eg a circle and add it to the svg root element
    c=circle()
    #you can supply attributes by using named arguments.
    c=circle(fill='red',stroke='blue')
    #or by updating the attributes attribute:
    c.attributes['stroke-width']=1
    s.addElement(c)
    #then you add the svg root element to the drawing
    d.setSVG(s)
    #and finaly you xmlify the drawing
    d.toXml()
    

this results in the svg source of the drawing, which consists of a circle
on a white background. Its as easy as that;)
This module was created using the SVG specification of www.w3c.org and the
O'Reilly (www.oreilly.com) python books as information sources. A svg viewer
is available from www.adobe.coms   1.0i    iN(   t   implementation(   t   PrettyPrints2   PyXML is required for using the dom implementationi   i   i2   c         C   si   |  j  d d  }  |  j  d d  }  |  j  d d  }  x, | j   D] \ } } |  j  | |  }  qC W|  S(   s   Escape &, <, and > in a string of data.

    You can escape other strings of data by passing a dictionary as
    the optional entities parameter.  The keys and values must all be
    strings; each key will be replaced with its corresponding value.
    t   &s   &amp;t   <s   &lt;t   >s   &gt;(   t   replacet   items(   t   datat   entitiest   charst   entity(    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   _escapea   s    c         C   s[   t  |  |  }  d |  k rM d |  k r@ d |  j d d  }  qW d |  }  n
 d |  }  |  S(   s  Escape and quote an attribute value.

    Escape &, <, and > in a string of data, then quote it for use as
    an attribute value.  The " character will be escaped as well, if
    necessary.

    You can escape other strings of data by passing a dictionary as
    the optional entities parameter.  The keys and values must all be
    strings; each key will be replaced with its corresponding value.
    t   "t   's   "%s"s   &quot;s   '%s'(   R   R   (   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt
   _quoteattro   s    
c         C   s6   d } x) |  D]! } | t  |  d d !d 7} q W| S(   s   formats a list of xy pairst    i   is     (   t   str(   t   at   st   e(    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   _xypointlist   s    c         C   s/   d } x" |  D] } | t  |  d 7} q W| S(   s   formats a tupleR   t    (   R   (   R   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   _viewboxlist   s    c         C   s   t  |   d d !S(   s   formats a list of numbersi   i(   R   (   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt
   _pointlist   s    t   pathdatac           B   s   e  Z d  Z d d d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s   class used to create a pathdata object which can be used for a path.
    although most methods are pretty straightforward it might be useful to look at the SVG specification.c         C   sP   g  |  _  | d  k	 rL | d  k	 rL |  j  j d t |  d t |   n  d  S(   Ns   M R   (   t   patht   Nonet   appendR   (   t   selft   xt   y(    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   __init__   s    	c         C   s   |  j  j d  d S(   s   ends the patht   zN(   R   R   (   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt	   closepath   s    c         C   s,   |  j  j d t |  d t |   d S(   s   move to absolutes   M R   N(   R   R   R   (   R   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   move   s    c         C   s,   |  j  j d t |  d t |   d S(   s   move to relatives   m R   N(   R   R   R   (   R   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   relmove   s    c         C   s,   |  j  j d t |  d t |   d S(   s   line to absolutes   L R   N(   R   R   R   (   R   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   line   s    c         C   s,   |  j  j d t |  d t |   d S(   s   line to relatives   l R   N(   R   R   R   (   R   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   relline   s    c         C   s   |  j  j d t |   d S(   s   horizontal line to absolutet   HN(   R   R   R   (   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   hline   s    c         C   s   |  j  j d t |   d S(   s   horizontal line to relativet   hN(   R   R   R   (   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   relhline   s    c         C   s   |  j  j d t |   d S(   s   verical line to absolutet   VN(   R   R   R   (   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   vline   s    c         C   s   |  j  j d t |   d S(   s   vertical line to relativet   vN(   R   R   R   (   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   relvline   s    c         C   sd   |  j  j d t |  d t |  d t |  d t |  d t |  d t |   d S(   s%   bezier with xy1 and xy2 to xy absolutt   Ct   ,R   N(   R   R   R   (   R   t   x1t   y1t   x2t   y2R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   bezier   s    c         C   sd   |  j  j d t |  d t |  d t |  d t |  d t |  d t |   d S(   s&   bezier with xy1 and xy2 to xy relativet   cR/   R   N(   R   R   R   (   R   R0   R1   R2   R3   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt	   relbezier   s    c         C   sH   |  j  j d t |  d t |  d t |  d t |   d S(   s$   smooth bezier with xy2 to xy absolutt   SR/   R   N(   R   R   R   (   R   R2   R3   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   smbezier   s    c         C   sH   |  j  j d t |  d t |  d t |  d t |   d S(   s%   smooth bezier with xy2 to xy relativeR   R/   R   N(   R   R   R   (   R   R2   R3   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   relsmbezier   s    c         C   sH   |  j  j d t |  d t |  d t |  d t |   d S(   s'   quadratic bezier with xy1 to xy absolutt   QR/   R   N(   R   R   R   (   R   R0   R1   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   qbezier   s    c         C   sH   |  j  j d t |  d t |  d t |  d t |   d S(   s(   quadratic bezier with xy1 to xy relativet   qR/   R   N(   R   R   R   (   R   R0   R1   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt
   relqbezier   s    c         C   s,   |  j  j d t |  d t |   d S(   s%   smooth quadratic bezier to xy absolutt   TR/   N(   R   R   R   (   R   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt	   smqbezier   s    c         C   s,   |  j  j d t |  d t |   d S(   s&   smooth quadratic bezier to xy relativet   tR/   N(   R   R   R   (   R   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   relsmqbezier   s    c         C   sr   |  j  j d t |  d t |  d t |  d t |  d t |  d t |  d t |   d S(   sc   elliptival arc with rx and ry rotating with xrot using large-arc-flag and sweep-flag  to xy absolutt   AR/   R   N(   R   R   R   (   R   t   rxt   ryt   xrott   laft   sfR   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   ellarc   s    c         C   sr   |  j  j d t |  d t |  d t |  d t |  d t |  d t |  d t |   d S(   sd   elliptival arc with rx and ry rotating with xrot using large-arc-flag and sweep-flag  to xy relativeR   R/   R   N(   R   R   R   (   R   RC   RD   RE   RF   RG   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt	   relellarc   s    c         C   s   d j  |  j  S(   NR   (   t   joinR   (   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   __repr__   s    N(   t   __name__t
   __module__t   __doc__R   R   R!   R"   R#   R$   R%   R'   R)   R+   R-   R4   R6   R8   R9   R;   R=   R?   RA   RH   RI   RK   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR      s,   																			t
   SVGelementc           B   s;   e  Z d  Z d d d d d d d  Z d   Z d   Z RS(   s  SVGelement(type,attributes,elements,text,namespace,**args)
    Creates a arbitrary svg element and is intended to be subclassed not used on its own.
    This element is the base of every svg element it defines a class which resembles
    a xml-element. The main advantage of this kind of implementation is that you don't
    have to create a toXML method for every different graph object. Every element
    consists of a type, attribute, optional subelements, optional text and an optional
    namespace. Note the elements==None, if elements = None:self.elements=[] construction.
    This is done because if you default to elements=[] every object has a reference
    to the same empty list.R   c   	      K   s   | |  _  | d  k r! i  |  _ n	 | |  _ | d  k rB g  |  _ n	 | |  _ | |  _ | |  _ | |  _ x% | j   D] } | | |  j | <qs Wd  S(   N(   t   typeR   t
   attributest   elementst   textt	   namespacet   cdatat   keys(	   R   RP   RQ   RR   RS   RT   RU   t   argst   arg(    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR      s    						c         C   s   |  j  j |  d S(   sS   adds an element to a SVGelement

        SVGelement.addElement(SVGelement)
        N(   RR   R   (   R   RO   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt
   addElement   s    c   
      K   s  | j  d t  } | r' d } d } n d } d } | j | |  | j d |  j  xO |  j j   D]> } | j d t t |   d t t |  j |    qh W|  j	 r | j d t t |  j	   d	  n  |  j
 s |  j s |  j r| j d
  n  |  j
 r| j |  n  x+ |  j
 D]  } | j | d | d | q(W|  j r| j | | | d d  x4 |  j j   D]# }	 | j | | | d |	  qW| j | | | d d |  n  |  j r)t |  j  t d  k r| j t t |  j    q)| j t |  j   n  |  j
 rY| j | | d |  j d
 |  ni |  j r| j d |  j d
 |  nA |  j r| j | | d |  j d
 |  n | j d |  d  S(   Nt   preserveWhitespaceR   s   
s   	R   R   t   =s    xmlns="s   " R   i   s	   <![CDATA[i   s   ]]>s   </s   />(   t   gett   Falset   writeRP   RQ   RV   R   R   R   RT   RR   RS   RU   t   toXmlt
   splitlines(
   R   t   levelt   ft   kwargst   preservet   NEWLINEt   TABt   attkeyt   elementR$   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR_      sD    	<	'		!$		'		'N(   RL   RM   RN   R   R   RY   R_   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyRO      s   		t   tspanc           B   s#   e  Z d  Z d d  Z d   Z RS(   s   ts=tspan(text='',**args)

    a tspan element can be used for applying formatting to a textsection
    usage:
    ts=tspan('this text is bold')
    ts.attributes['font-weight']='bold'
    st=spannedtext()
    st.addtspan(ts)
    t=text(3,5,st)
    c         K   s2   t  j |  d |  |  j d  k r. | |  _ n  d  S(   NRi   (   RO   R   RS   R   (   R   RS   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   4  s    c         C   s_   d } x1 |  j  j   D]  \ } } | d | | f 7} q W| d 7} | |  j 7} | d 7} | S(   Ns   <tspans    %s="%s"R   s   </tspan>(   RQ   R   RS   (   R   R   t   keyt   value(    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyRK   8  s    

N(   RL   RM   RN   R   R   RK   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyRi   )  s   
t   trefc           B   s    e  Z d  Z d   Z d   Z RS(   s   tr=tref(link='',**args)

    a tref element can be used for referencing text by a link to its id.
    usage:
    tr=tref('#linktotext')
    st=spannedtext()
    st.addtref(tr)
    t=text(3,5,st)
    c         K   s!   t  j |  d i | d 6|  d  S(   NRl   s
   xlink:href(   RO   R   (   R   t   linkRW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   K  s    c         C   sH   d } x1 |  j  j   D]  \ } } | d | | f 7} q W| d 7} | S(   Ns   <trefs    %s="%s"s   />(   RQ   R   (   R   R   Rj   Rk   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyRK   M  s
    
(   RL   RM   RN   R   RK   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyRl   A  s   		t   spannedtextc           B   sA   e  Z d  Z d d  Z d d  Z d   Z d   Z d   Z RS(   s  st=spannedtext(textlist=[])

    a spannedtext can be used for text which consists of text, tspan's and tref's
    You can use it to add to a text element or path element. Don't add it directly
    to a svg or a group element.
    usage:
    
    ts=tspan('this text is bold')
    ts.attributes['font-weight']='bold'
    tr=tref('#linktotext')
    tr.attributes['fill']='red'
    st=spannedtext()
    st.addtspan(ts)
    st.addtref(tr)
    st.addtext('This text is not bold')
    t=text(3,5,st)
    c         C   s%   | d  k r g  |  _ n	 | |  _ d  S(   N(   R   t   textlist(   R   Ro   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   g  s    R   c         C   s   |  j  j |  d  S(   N(   Ro   R   (   R   RS   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   addtextl  s    c         C   s   |  j  j |  d  S(   N(   Ro   R   (   R   Ri   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   addtspann  s    c         C   s   |  j  j |  d  S(   N(   Ro   R   (   R   Rl   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   addtrefp  s    c         C   s.   d } x! |  j  D] } | t |  7} q W| S(   NR   (   Ro   R   (   R   R   Rh   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyRK   r  s    N(	   RL   RM   RN   R   R   Rp   Rq   Rr   RK   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyRn   U  s   		t   rectc           B   s,   e  Z d  Z d d d d d d d d  Z RS(   s   r=rect(width,height,x,y,fill,stroke,stroke_width,**args)
    
    a rectangle is defined by a width and height and a xy pair 
    c   	      K   s  | d  k s | d  k rT | d  k r0 t d  n  | d  k rH t d  qT t d  n  t j |  d i | d 6| d 6|  | d  k r | |  j d <n  | d  k r | |  j d <n  | d  k r | |  j d	 <n  | d  k r | |  j d
 <n  | d  k r| |  j d <n  d  S(   Ns   height is requireds   width is requireds"   both height and width are requiredRs   t   widtht   heightR   R   t   fillt   strokes   stroke-width(   R   t
   ValueErrorRO   R   RQ   (	   R   R   R   Rt   Ru   Rv   Rw   t   stroke_widthRW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   }  s"    $N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyRs   x  s   t   ellipsec           B   s,   e  Z d  Z d d d d d d d d  Z RS(   sv   e=ellipse(rx,ry,x,y,fill,stroke,stroke_width,**args)

    an ellipse is defined as a center and a x and y radius.
    c   	      K   s  | d  k s | d  k rT | d  k r0 t d  n  | d  k rH t d  qT t d  n  t j |  d i | d 6| d 6|  | d  k r | |  j d <n  | d  k r | |  j d <n  | d  k r | |  j d	 <n  | d  k r | |  j d
 <n  | d  k r| |  j d <n  d  S(   Ns   rx is requireds   ry is requireds   both rx and ry are requiredRz   RC   RD   t   cxt   cyRv   Rw   s   stroke-width(   R   Rx   RO   R   RQ   (	   R   R{   R|   RC   RD   Rv   Rw   Ry   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s"    $N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyRz     s   t   circlec           B   s)   e  Z d  Z d d d d d d d  Z RS(   s~   c=circle(x,y,radius,fill,stroke,stroke_width,**args)

    The circle creates an element using a x, y and radius values eg
    c         K   s   | d  k r t d  n  t j |  d i | d 6|  | d  k rQ | |  j d <n  | d  k rm | |  j d <n  | d  k r | |  j d <n  | d  k r | |  j d <n  | d  k r | |  j d <n  d  S(	   Ns   r is requiredR}   t   rR{   R|   Rv   Rw   s   stroke-width(   R   Rx   RO   R   RQ   (   R   R{   R|   R~   Rv   Rw   Ry   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR}     s   t   pointc           B   s   e  Z d  Z d d  Z RS(   s   p=point(x,y,color)
    
    A point is defined as a circle with a size 1 radius. It may be more efficient to use a
    very small rectangle if you use many points because a circle is difficult to render.
    t   blackc         K   s    t  j |  | | d | |  d  S(   Ni   (   R}   R   (   R   R   R   Rv   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    (   RL   RM   RN   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   R$   c           B   s)   e  Z d  Z d d d d d d d  Z RS(   sv   l=line(x1,y1,x2,y2,stroke,stroke_width,**args)
    
    A line is defined by a begin x,y pair and an end x,y pair
    c         K   s   t  j |  d |  | d  k r/ | |  j d <n  | d  k rK | |  j d <n  | d  k rg | |  j d <n  | d  k r | |  j d <n  | d  k r | |  j d <n  | d  k r | |  j d <n  d  S(   NR$   R0   R1   R2   R3   s   stroke-widthRw   (   RO   R   R   RQ   (   R   R0   R1   R2   R3   Rw   Ry   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR$     s   t   polylinec           B   s    e  Z d  Z d d d d  Z RS(   s|   pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args)
    
    a polyline is defined by a list of xy pairs
    c         K   s{   t  j |  d i t |  d 6|  | d  k r? | |  j d <n  | d  k r[ | |  j d <n  | d  k rw | |  j d <n  d  S(   NR   t   pointsRv   s   stroke-widthRw   (   RO   R   R   R   RQ   (   R   R   Rv   Rw   Ry   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    #N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   t   polygonc           B   s    e  Z d  Z d d d d  Z RS(   s{   pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args)
    
    a polygon is defined by a list of xy pairs
    c         K   s{   t  j |  d i t |  d 6|  | d  k r? | |  j d <n  | d  k r[ | |  j d <n  | d  k rw | |  j d <n  d  S(   NR   R   Rv   s   stroke-widthRw   (   RO   R   R   R   RQ   (   R   R   Rv   Rw   Ry   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    #N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   R   c           B   s#   e  Z d  Z d d d d d  Z RS(   s   p=path(path,fill,stroke,stroke_width,**args)

    a path is defined by a path object and optional width, stroke and fillcolor
    c         K   s   t  j |  d i t |  d 6|  | d  k r? | |  j d <n  | d  k r[ | |  j d <n  | d  k rw | |  j d <n  | d  k r | |  j d <n  d  S(   NR   t   dRw   Rv   s   stroke-widtht   id(   RO   R   R   R   RQ   (   R   R   Rv   Rw   Ry   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    #N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   RS   c           B   s2   e  Z d  Z d d d d d d d  Z d   Z RS(   sy   t=text(x,y,text,font_size,font_family,**args)
    
    a text element can bge used for displaying text on the screen
    c         K   s   t  j |  d |  | d  k r/ | |  j d <n  | d  k rK | |  j d <n  | d  k rg | |  j d <n  | d  k r | |  j d <n  | d  k r | |  _ n  | d  k r | |  j d <n  d  S(   NRS   R   R   s	   font-sizes   font-familys   text-anchor(   RO   R   R   RQ   RS   (   R   R   R   RS   t	   font_sizet   font_familyt   text_anchorRW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    c         K   sZ   |  j  j d d   } | d k r= t j |  | | d t n t j |  | | d t d  S(   Ns	   xml:spaceRd   RZ   (   RQ   R\   R   RO   R_   t   TrueR]   (   R   Ra   Rb   Rc   Rd   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR_     s    N(   RL   RM   RN   R   R   R_   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyRS   	  s   t   textpathc           B   s   e  Z d  Z d d  Z RS(   sl   tp=textpath(text,link,**args)

    a textpath places a text on a path which is referenced by a link.   
    c         K   s9   t  j |  d i | d 6|  | d  k r5 | |  _ n  d  S(   Nt   textPaths
   xlink:href(   RO   R   R   RS   (   R   Rm   RS   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   +  s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   &  s   t   patternc           B   s&   e  Z d  Z d d d d d d  Z RS(   s   p=pattern(x,y,width,height,patternUnits,**args)

    A pattern is used to fill or stroke an object using a pre-defined
    graphic object which can be replicated ("tiled") at fixed intervals
    in x and y to cover the areas to be painted.
    c         K   s   t  j |  d |  | d  k r/ | |  j d <n  | d  k rK | |  j d <n  | d  k rg | |  j d <n  | d  k r | |  j d <n  | d  k r | |  j d <n  d  S(   NR   R   R   Rt   Ru   t   patternUnits(   RO   R   R   RQ   (   R   R   R   Rt   Ru   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   7  s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   0  s   t   titlec           B   s   e  Z d  Z d d  Z RS(   s   t=title(text,**args)
    
    a title is a text element. The text is displayed in the title bar
    add at least one to the root svg element
    c         K   s/   t  j |  d |  | d  k r+ | |  _ n  d  S(   NR   (   RO   R   R   RS   (   R   RS   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   J  s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   D  s   t   descriptionc           B   s   e  Z d  Z d d  Z RS(   s   d=description(text,**args)
    
    a description can be added to any element and is used for a tooltip
    Add this element before adding other elements.
    c         K   s/   t  j |  d |  | d  k r+ | |  _ n  d  S(   Nt   desc(   RO   R   R   RS   (   R   RS   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   U  s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   O  s   t   lineargradientc           B   s&   e  Z d  Z d d d d d d  Z RS(   s   lg=lineargradient(x1,y1,x2,y2,id,**args)

    defines a lineargradient using two xy pairs.
    stop elements van be added to define the gradient colors.
    c         K   s   t  j |  d |  | d  k r/ | |  j d <n  | d  k rK | |  j d <n  | d  k rg | |  j d <n  | d  k r | |  j d <n  | d  k r | |  j d <n  d  S(   Nt   linearGradientR0   R1   R2   R3   R   (   RO   R   R   RQ   (   R   R0   R1   R2   R3   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   `  s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   Z  s   t   radialgradientc           B   s)   e  Z d  Z d d d d d d d  Z RS(   s   rg=radialgradient(cx,cy,r,fx,fy,id,**args)

    defines a radial gradient using a outer circle which are defined by a cx,cy and r and by using a focalpoint.
    stop elements van be added to define the gradient colors.
    c         K   s   t  j |  d |  | d  k r/ | |  j d <n  | d  k rK | |  j d <n  | d  k rg | |  j d <n  | d  k r | |  j d <n  | d  k r | |  j d <n  | d  k r | |  j d <n  d  S(   Nt   radialGradientR{   R|   R~   t   fxt   fyR   (   RO   R   R   RQ   (   R   R{   R|   R~   R   R   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   s  s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   m  s   t   stopc           B   s   e  Z d  Z d d  Z RS(   sU   st=stop(offset,stop_color,**args)

    Puts a stop color at the specified radius
    c         K   s=   t  j |  d i | d 6|  | d  k r9 | |  j d <n  d  S(   NR   t   offsets
   stop-color(   RO   R   R   RQ   (   R   R   t
   stop_colorRW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   t   stylec           B   s   e  Z d  Z d d  Z RS(   ss   st=style(type,cdata=None,**args)

    Add a CDATA element to this element for defing in line stylesheets etc..
    c         K   s'   t  j |  d i | d 6d | | d  S(   NR   RP   RU   (   RO   R   (   R   RP   RU   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   t   imagec           B   s#   e  Z d  Z d d d d d  Z RS(   sx   im=image(url,width,height,x,y,**args)

    adds an image to the drawing. Supported formats are .png, .jpg and .svg.
    c         K   s   | d  k s | d  k rT | d  k r0 t d  n  | d  k rH t d  qT t d  n  t j |  d i | d 6| d 6| d 6|  | d  k r | |  j d <n  | d  k r | |  j d	 <n  d  S(
   Ns   height is requireds   width is requireds"   both height and width are requiredR   s
   xlink:hrefRt   Ru   R   R   (   R   Rx   RO   R   RQ   (   R   t   urlR   R   Rt   Ru   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    +N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   t   cursorc           B   s   e  Z d  Z d   Z RS(   sQ   c=cursor(url,**args)

    defines a custom cursor for a element or a drawing
    c         K   s!   t  j |  d i | d 6|  d  S(   NR   s
   xlink:href(   RO   R   (   R   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    (   RL   RM   RN   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   t   markerc           B   s)   e  Z d  Z d d d d d d d  Z RS(   s   m=marker(id,viewbox,refX,refY,markerWidth,markerHeight,**args)
    
    defines a marker which can be used as an endpoint for a line or other pathtypes
    add an element to it which should be used as a marker.
    c         K   s   t  j |  d |  | d  k r/ | |  j d <n  | d  k rQ t |  |  j d <n  | d  k rm | |  j d <n  | d  k r | |  j d <n  | d  k r | |  j d <n  | d  k r | |  j d <n  d  S(   NR   R   t   viewBoxt   refXt   refYt   markerWidtht   markerHeight(   RO   R   R   RQ   R   (   R   R   R   t   refxt   refyR   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   t   groupc           B   s   e  Z d  Z d d  Z RS(   sy   g=group(id,**args)
    
    a group is defined by an id and is used to contain elements
    g.addElement(SVGelement)
    c         K   s3   t  j |  d |  | d  k r/ | |  j d <n  d  S(   Nt   gR   (   RO   R   R   RQ   (   R   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   t   symbolc           B   s   e  Z d  Z d d d  Z RS(   s  sy=symbol(id,viewbox,**args)

    defines a symbol which can be used on different places in your graph using
    the use element. A symbol is not rendered but you can use 'use' elements to
    display it by referencing its id.
    sy.addElement(SVGelement)
    c         K   sU   t  j |  d |  | d  k r/ | |  j d <n  | d  k rQ t |  |  j d <n  d  S(   NR   R   R   (   RO   R   R   RQ   R   (   R   R   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s
    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   t   defsc           B   s   e  Z d  Z d   Z RS(   s8   d=defs(**args)

    container for defining elements
    c         K   s   t  j |  d |  d  S(   NR   (   RO   R   (   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    (   RL   RM   RN   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   t   switchc           B   s   e  Z d  Z d   Z RS(   s   sw=switch(**args)

    Elements added to a switch element which are "switched" by the attributes
    requiredFeatures, requiredExtensions and systemLanguage.
    Refer to the SVG specification for details.
    c         K   s   t  j |  d |  d  S(   NR   (   RO   R   (   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    (   RL   RM   RN   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   t   usec           B   s#   e  Z d  Z d d d d d  Z RS(   s}   u=use(link,x,y,width,height,**args)
    
    references a symbol by linking to its id and its position, height and width
    c         K   s   t  j |  d i | d 6|  | d  k r9 | |  j d <n  | d  k rU | |  j d <n  | d  k rq | |  j d <n  | d  k r | |  j d <n  d  S(   NR   s
   xlink:hrefR   R   Rt   Ru   (   RO   R   R   RQ   (   R   Rm   R   R   Rt   Ru   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   Rm   c           B   s   e  Z d  Z d d  Z RS(   s   a=link(url,**args)

    a link  is defined by a hyperlink. add elements which have to be linked
    a.addElement(SVGelement)
    R   c         K   s!   t  j |  d i | d 6|  d  S(   NR   s
   xlink:href(   RO   R   (   R   Rm   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    (   RL   RM   RN   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyRm     s   t   viewc           B   s   e  Z d  Z d d  Z RS(   sT   v=view(id,**args)

    a view can be used to create a view with different attributesc         K   s3   t  j |  d |  | d  k r/ | |  j d <n  d  S(   NR   R   (   RO   R   R   RQ   (   R   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   t   scriptc           B   s   e  Z d  Z d d  Z RS(   sj   sc=script(type,type,cdata,**args)

    adds a script element which contains CDATA to the SVG drawing

    c         K   s'   t  j |  d i | d 6d | | d  S(   NR   RP   RU   (   RO   R   (   R   RP   RU   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   t   animatec           B   s    e  Z d  Z d d d d  Z RS(   sP   an=animate(attribute,from,to,during,**args)

    animates an attribute.    
    c         K   su   t  j |  d i | d 6|  | d  k r9 | |  j d <n  | d  k rU | |  j d <n  | d  k rq | |  j d <n  d  S(   NR   t   attributeNamet   fromt   tot   dur(   RO   R   R   RQ   (   R   t	   attributet   frR   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   &  s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   !  s   t   animateMotionc           B   s   e  Z d  Z d   Z RS(   sh   an=animateMotion(pathdata,dur,**args)

    animates a SVGelement over the given path in dur seconds
    c         K   sU   t  j |  d |  | d  k r5 t |  |  j d <n  | d  k rQ | |  j d <n  d  S(   NR   R   R   (   RO   R   R   R   RQ   (   R   R   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   4  s
    (   RL   RM   RN   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   /  s   t   animateTransformc           B   s#   e  Z d  Z d d d d d  Z RS(   sf   antr=animateTransform(type,from,to,dur,**args)
    
    transform an element from and to a value.
    c         K   s   t  j |  d i d d 6|  | d  k r9 | |  j d <n  | d  k rU | |  j d <n  | d  k rq | |  j d <n  | d  k r | |  j d <n  d  S(   NR   t	   transformR   RP   R   R   R   (   RO   R   R   RQ   (   R   RP   R   R   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   @  s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   ;  s   t   animateColorc           B   s#   e  Z d  Z d d d d d  Z RS(   s\   ac=animateColor(attribute,type,from,to,dur,**args)

    Animates the color of a element
    c         K   s   t  j |  d i | d 6|  | d  k r9 | |  j d <n  | d  k rU | |  j d <n  | d  k rq | |  j d <n  | d  k r | |  j d <n  d  S(   NR   R   RP   R   R   R   (   RO   R   R   RQ   (   R   R   RP   R   R   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   P  s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   K  s   t   setc           B   s   e  Z d  Z d d d  Z RS(   sS   st=set(attribute,to,during,**args)
    
    sets an attribute to a value for a
    c         K   sY   t  j |  d i | d 6|  | d  k r9 | |  j d <n  | d  k rU | |  j d <n  d  S(   NR   R   R   R   (   RO   R   R   RQ   (   R   R   R   R   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   _  s
    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   Z  s   t   svgc           B   s    e  Z d  Z d d d d  Z RS(   sZ  s=svg(viewbox,width,height,**args)
    
    a svg or element is the root of a drawing add all elements to a svg element.
    You can have different svg elements in one svg file
    s.addElement(SVGelement)

    eg
    d=drawing()
    s=svg((0,0,100,100),'100%','100%')
    c=circle(50,50,20)
    s.addElement(c)
    d.setSVG(s)
    d.toXml()
    c         K   sz   t  j |  d |  | d  k r5 t |  |  j d <n  | d  k rQ | |  j d <n  | d  k rm | |  j d <n  d |  _ d  S(   NR   R   Rt   Ru   s   http://www.w3.org/2000/svg(   RO   R   R   R   RQ   RT   (   R   R   Rt   Ru   RW   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   w  s    N(   RL   RM   RN   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR   h  s   t   drawingc           B   sV   e  Z d  Z d   Z d   Z e d k r< d e d  Z n d e d  Z d   Z RS(   s  d=drawing()

    this is the actual SVG document. It needs a svg element as a root.
    Use the addSVG method to set the svg to the root. Use the toXml method to write the SVG
    source to the screen or to a file
    d=drawing()
    d.addSVG(svg)
    d.toXml(optionalfilename)
    c         C   s   d  |  _ d  S(   N(   R   R   (   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   setSVG  s    i    R   c         C   sL  d d  l  } | j   } | j d  | j d  |  j j d |  | s | r d d  l } | j   } | j d | d d  } | j | j    | j   | j	 d  | j
   S| j   Sn | d d	 k rd d  l } | j d
 | d d d d  } | j | j    | j   n, t | d  } | j | j    | j   d  S(   Nis7   <?xml version="1.0" encoding="UTF-8" standalone="no"?>
s   <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
                         "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" 
                         [<!ATTLIST svg xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink">]>
i    t   fileobjt   modet   wbit   svgzt   filenamet   compressleveli	   t   w(   t	   cStringIOt   StringIOR^   R   R_   t   gzipt   GzipFilet   getvaluet   closet   seekt   readt   file(   R   R   t   compressR   t   xmlR   Rb   t   zf(    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR_     s0    

c   	      C   s  t  j d d d  } t  j d d |  a d   a t |  j t  a | s d d l } | j   } t	 t |  | r d d l
 } | j   } | j d | d d	  } | j | j    | j   | j d
  | j   S| j   Sn y | d d k rid d l
 } d d l } | j   } t	 t |  | j d | d d	 d d  } | j | j    | j   n& t | d  } t	 t |  | j   Wn d | GHn Xd S(   s   drawing.toXml()        ---->to the screen
            drawing.toXml(filename)---->to the file
            writes a svg drawing to the screen or to a file
            compresses if filename ends with svgz or if compress is true
            R   s   -//W3C//DTD SVG 1.0//ENs9   http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd c         S   s   |  j  r$ t j |  j  |  j  } n t j |  j  } |  j ra t j |  j  } | j |  n  x4 |  j j	   D]# } | j
 | t |  j |   qq W|  j r x# |  j D] } t | |  } q Wn  | j |  | S(   s   This recursive function appends elements to an element and sets the attributes
                and type. It stops when alle elements have been appended(   RT   t   roott   createElementNSRP   t   createElementRS   t   createTextNodet   appendChildRQ   RV   t   setAttributeR   RR   t   appender(   Rh   t   elementrootR   t   textnodeR   t   el(    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s    		!	iNR   R   R   i    iR   R   R   i	   R   s   Cannot write SVG file: (   R    t   createDocumentTypet   createDocumentR   R   R   R   R   R   R   R   R   R^   R   R   R   R   t   open(	   R   R   R   t   doctypeR   R   R   Rb   R   (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR_     s>    	

c         C   sq   y d d  l  } Wn t j d  n X|  j   } | j j j j   } y | j |  Wn d  n Xd GHd  S(   Nis$   PyXml is required for validating SVGs*   SVG is not well formed, see messages aboves   SVG well formed(	   t   xml.parsers.xmlproc.xmlvalt
   exceptionst   ImportErrorR_   t   parserst   xmlproct   xmlvalt   XMLValidatort   feed(   R   R   R   t   xv(    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   validate  s    
(	   RL   RM   RN   R   R   t   use_dom_implementationR]   R_   R   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyR     s   			>t   __main__id   ii,  t   cyans   SVGdraw Demot
   animationsi   t   redii   i
   s   auto-reverset   rotatet
   indefinitet   repeatCounti   ix   s   #animationsii   R   g      ?(    (    (    (    (   i    i    id   id   (Q   RN   t   __version__R   R   t   xml.domR    t   xml.dom.extR   R   t   syst   version_infot   AssertionErrorR   R]   R   R   t   setrecursionlimitR   R   R   R   R   R   RO   Ri   Rl   Rn   Rs   Rz   R}   R   R$   R   R   R   RS   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   Rm   R   R   R   R   R   R   R   R   R   RL   R   R   R~   RY   R@   R   R   R5   t   pdt   ranget   iR9   t   anRQ   t   ut   jR   R_   (    (    (    s/   lib/python2.7/site-packages/obitools/SVGdraw.pyt   <module>9   s   					GJ#	

					{	