|
|
|
@ -69,6 +69,7 @@ __version__="1.0"
|
|
|
|
|
# Anyway the text based approach is about 60 times faster than using the full dom implementation.
|
|
|
|
|
use_dom_implementation=0
|
|
|
|
|
|
|
|
|
|
from xml.parsers import expat
|
|
|
|
|
|
|
|
|
|
import exceptions
|
|
|
|
|
if use_dom_implementation<>0:
|
|
|
|
@ -226,7 +227,11 @@ class Attribute:
|
|
|
|
|
self.value = value
|
|
|
|
|
self.nsname = nsname
|
|
|
|
|
self.nsref = nsref
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_attr_value(attrs, name):
|
|
|
|
|
return attrs[name]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SVGelement:
|
|
|
|
|
"""SVGelement(type,attributes,elements,text,namespace,**args)
|
|
|
|
|
Creates a arbitrary svg element and is intended to be subclassed not used on its own.
|
|
|
|
@ -266,9 +271,38 @@ class SVGelement:
|
|
|
|
|
def setParent(self, parent):
|
|
|
|
|
self._parent = parent
|
|
|
|
|
|
|
|
|
|
def addAttribute(self, attribute):
|
|
|
|
|
def setAttribute(self, attribute):
|
|
|
|
|
self._attributes[attribute.name] = attribute
|
|
|
|
|
|
|
|
|
|
def delAttribute(self, name):
|
|
|
|
|
if name in self._attributes.keys():
|
|
|
|
|
del self._attributes[name]
|
|
|
|
|
|
|
|
|
|
def hasAttribute(self, name):
|
|
|
|
|
if name in self._attributes.keys():
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
raise Exception("Can't construct a default object.")
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
def construct(name, attributes, text=None, cdata=None):
|
|
|
|
|
try:
|
|
|
|
|
eltClass = elementTable[name]
|
|
|
|
|
except KeyError, e:
|
|
|
|
|
print "Unknown SVG element %s." % e
|
|
|
|
|
return None
|
|
|
|
|
element = eltClass._construct(attributes)
|
|
|
|
|
for attrname, attrvalue in attributes.items():
|
|
|
|
|
element.setAttribute(Attribute(attrname, attrvalue))
|
|
|
|
|
if text:
|
|
|
|
|
element.text = text
|
|
|
|
|
if cdata:
|
|
|
|
|
element.cdata = cdata
|
|
|
|
|
return element
|
|
|
|
|
construct = staticmethod(construct)
|
|
|
|
|
|
|
|
|
|
def toXml(self,level,f):
|
|
|
|
|
f.write('\t'*level)
|
|
|
|
|
f.write('<'+self.type)
|
|
|
|
@ -323,11 +357,16 @@ class tspan(SVGelement):
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
s="<tspan"
|
|
|
|
|
for key, attr in self._attributes.items():
|
|
|
|
|
s+= ' %s="%s"' % (key,attr.value)
|
|
|
|
|
s+= ' %s="%s"' % (key,attr.value)
|
|
|
|
|
s+='>'
|
|
|
|
|
s+=self.text
|
|
|
|
|
s+='</tspan>'
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return tspan()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class tref(SVGelement):
|
|
|
|
|
"""tr=tref(link='',**args)
|
|
|
|
@ -341,14 +380,20 @@ class tref(SVGelement):
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,link,**args):
|
|
|
|
|
SVGelement.__init__(self,'tref',**args)
|
|
|
|
|
self.addAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
|
|
|
|
self.setAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
s="<tref"
|
|
|
|
|
|
|
|
|
|
for key, attr in self._attributes.items():
|
|
|
|
|
s+= ' %s="%s"' % (key,attr.value)
|
|
|
|
|
s+= ' %s="%s"' % (key,attr.value)
|
|
|
|
|
s+='/>'
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
href = get_attr_value(attributes, 'xlink:href')
|
|
|
|
|
if href and href.startswith("xlink:"):
|
|
|
|
|
href = href[len("xlink:"):]
|
|
|
|
|
return tref(href)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
class spannedtext:
|
|
|
|
|
"""st=spannedtext(textlist=[])
|
|
|
|
@ -400,16 +445,23 @@ class rect(SVGelement):
|
|
|
|
|
raise ValueError, 'both height and width are required'
|
|
|
|
|
SVGelement.__init__(self,'rect',{'width':width,'height':height},**args)
|
|
|
|
|
if x<>None:
|
|
|
|
|
self.addAttribute(Attribute('x', x))
|
|
|
|
|
self.setAttribute(Attribute('x', x))
|
|
|
|
|
if y<>None:
|
|
|
|
|
self.addAttribute(Attribute('y', y))
|
|
|
|
|
self.setAttribute(Attribute('y', y))
|
|
|
|
|
if fill<>None:
|
|
|
|
|
self.addAttribute(Attribute('fill', fill))
|
|
|
|
|
self.setAttribute(Attribute('fill', fill))
|
|
|
|
|
if stroke<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke', stroke))
|
|
|
|
|
self.setAttribute(Attribute('stroke', stroke))
|
|
|
|
|
if stroke_width<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
|
|
|
|
|
self.setAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
width = get_attr_value(attributes, 'width')
|
|
|
|
|
height = get_attr_value(attributes, 'height')
|
|
|
|
|
return rect(width=width, height=height)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ellipse(SVGelement):
|
|
|
|
|
"""e=ellipse(rx,ry,x,y,fill,stroke,stroke_width,**args)
|
|
|
|
|
|
|
|
|
@ -425,17 +477,23 @@ class ellipse(SVGelement):
|
|
|
|
|
raise ValueError, 'both rx and ry are required'
|
|
|
|
|
SVGelement.__init__(self,'ellipse',{'rx':rx,'ry':ry},**args)
|
|
|
|
|
if cx<>None:
|
|
|
|
|
self.addAttribute(Attribute('cx', cx))
|
|
|
|
|
self.setAttribute(Attribute('cx', cx))
|
|
|
|
|
if cy<>None:
|
|
|
|
|
self.addAttribute(Attribute('cy', cy))
|
|
|
|
|
self.setAttribute(Attribute('cy', cy))
|
|
|
|
|
if fill<>None:
|
|
|
|
|
self.addAttribute(Attribute('fill', fill))
|
|
|
|
|
self.setAttribute(Attribute('fill', fill))
|
|
|
|
|
if stroke<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke', stroke))
|
|
|
|
|
self.setAttribute(Attribute('stroke', stroke))
|
|
|
|
|
if stroke_width<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
self.setAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
rx = get_attr_value(attributes, 'rx')
|
|
|
|
|
ry = get_attr_value(attributes, 'ry')
|
|
|
|
|
return ellipse(rx=rx, ry=ry)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class circle(SVGelement):
|
|
|
|
|
"""c=circle(x,y,radius,fill,stroke,stroke_width,**args)
|
|
|
|
|
|
|
|
|
@ -446,15 +504,24 @@ class circle(SVGelement):
|
|
|
|
|
raise ValueError, 'r is required'
|
|
|
|
|
SVGelement.__init__(self,'circle',{'r':r},**args)
|
|
|
|
|
if cx<>None:
|
|
|
|
|
self.addAttribute(Attribute('cx', cx))
|
|
|
|
|
self.setAttribute(Attribute('cx', cx))
|
|
|
|
|
if cy<>None:
|
|
|
|
|
self.addAttribute(Attribute('cy', cy))
|
|
|
|
|
self.setAttribute(Attribute('cy', cy))
|
|
|
|
|
if fill<>None:
|
|
|
|
|
self.addAttribute(Attribute('fill', fill))
|
|
|
|
|
self.setAttribute(Attribute('fill', fill))
|
|
|
|
|
if stroke<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke', stroke))
|
|
|
|
|
self.setAttribute(Attribute('stroke', stroke))
|
|
|
|
|
if stroke_width<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
self.setAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
r = get_attr_value(attributes, 'r')
|
|
|
|
|
if int(r) == 1:
|
|
|
|
|
return point()
|
|
|
|
|
else:
|
|
|
|
|
return circle(r=r)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class point(circle):
|
|
|
|
|
"""p=point(x,y,color)
|
|
|
|
@ -462,7 +529,7 @@ class point(circle):
|
|
|
|
|
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.
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,x,y,fill='black',**args):
|
|
|
|
|
def __init__(self,x=None,y=None,fill='black',**args):
|
|
|
|
|
circle.__init__(self,x,y,1,fill,**args)
|
|
|
|
|
|
|
|
|
|
class line(SVGelement):
|
|
|
|
@ -473,17 +540,22 @@ class line(SVGelement):
|
|
|
|
|
def __init__(self,x1=None,y1=None,x2=None,y2=None,stroke=None,stroke_width=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'line',**args)
|
|
|
|
|
if x1<>None:
|
|
|
|
|
self.addAttribute(Attribute('x1', x1))
|
|
|
|
|
self.setAttribute(Attribute('x1', x1))
|
|
|
|
|
if y1<>None:
|
|
|
|
|
self.addAttribute(Attribute('y1', y1))
|
|
|
|
|
self.setAttribute(Attribute('y1', y1))
|
|
|
|
|
if x2<>None:
|
|
|
|
|
self.addAttribute(Attribute('x2', x2))
|
|
|
|
|
self.setAttribute(Attribute('x2', x2))
|
|
|
|
|
if y2<>None:
|
|
|
|
|
self.addAttribute(Attribute('y2', y2))
|
|
|
|
|
self.setAttribute(Attribute('y2', y2))
|
|
|
|
|
if stroke_width<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
self.setAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
if stroke<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke', stroke))
|
|
|
|
|
self.setAttribute(Attribute('stroke', stroke))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return line()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class polyline(SVGelement):
|
|
|
|
|
"""pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args)
|
|
|
|
@ -493,11 +565,17 @@ class polyline(SVGelement):
|
|
|
|
|
def __init__(self,points,fill=None,stroke=None,stroke_width=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'polyline',{'points':_xypointlist(points)},**args)
|
|
|
|
|
if fill<>None:
|
|
|
|
|
self.addAttribute(Attribute('fill', fill))
|
|
|
|
|
self.setAttribute(Attribute('fill', fill))
|
|
|
|
|
if stroke_width<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
self.setAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
if stroke<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke', stroke))
|
|
|
|
|
self.setAttribute(Attribute('stroke', stroke))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
points = get_attr_value(attributes, 'points')
|
|
|
|
|
return polyline(points)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class polygon(SVGelement):
|
|
|
|
|
"""pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args)
|
|
|
|
@ -507,11 +585,17 @@ class polygon(SVGelement):
|
|
|
|
|
def __init__(self,points,fill=None,stroke=None,stroke_width=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'polygon',{'points':_xypointlist(points)},**args)
|
|
|
|
|
if fill<>None:
|
|
|
|
|
self.addAttribute(Attribute('fill', fill))
|
|
|
|
|
self.setAttribute(Attribute('fill', fill))
|
|
|
|
|
if stroke_width<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
self.setAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
if stroke<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke', stroke))
|
|
|
|
|
self.setAttribute(Attribute('stroke', stroke))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
points = get_attr_value(attributes, 'points')
|
|
|
|
|
return polygon(points)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class path(SVGelement):
|
|
|
|
|
"""p=path(path,fill,stroke,stroke_width,**args)
|
|
|
|
@ -521,14 +605,19 @@ class path(SVGelement):
|
|
|
|
|
def __init__(self,pathdata,fill=None,stroke=None,stroke_width=None,id=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'path',{'d':str(pathdata)},**args)
|
|
|
|
|
if stroke<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke', stroke))
|
|
|
|
|
self.setAttribute(Attribute('stroke', stroke))
|
|
|
|
|
if fill<>None:
|
|
|
|
|
self.addAttribute(Attribute('fill', fill))
|
|
|
|
|
self.setAttribute(Attribute('fill', fill))
|
|
|
|
|
if stroke_width<>None:
|
|
|
|
|
self.addAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
self.setAttribute(Attribute('stroke-width', stroke_width))
|
|
|
|
|
if id<>None:
|
|
|
|
|
self.addAttribute(Attribute('id', id))
|
|
|
|
|
self.setAttribute(Attribute('id', id))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
pathdata = get_attr_value(attributes, 'd')
|
|
|
|
|
return path(d)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class text(SVGelement):
|
|
|
|
|
"""t=text(x,y,text,font_size,font_family,**args)
|
|
|
|
@ -538,17 +627,21 @@ class text(SVGelement):
|
|
|
|
|
def __init__(self,x=None,y=None,text=None,font_size=None,font_family=None,text_anchor=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'text',**args)
|
|
|
|
|
if x<>None:
|
|
|
|
|
self.addAttribute(Attribute('x', x))
|
|
|
|
|
self.setAttribute(Attribute('x', x))
|
|
|
|
|
if y<>None:
|
|
|
|
|
self.addAttribute(Attribute('y', y))
|
|
|
|
|
self.setAttribute(Attribute('y', y))
|
|
|
|
|
if font_size<>None:
|
|
|
|
|
self.addAttribute(Attribute('font-size', font_size))
|
|
|
|
|
self.setAttribute(Attribute('font-size', font_size))
|
|
|
|
|
if font_family<>None:
|
|
|
|
|
self.addAttribute(Attribute('font-family', font_family))
|
|
|
|
|
self.setAttribute(Attribute('font-family', font_family))
|
|
|
|
|
if text<>None:
|
|
|
|
|
self.text=text
|
|
|
|
|
if text_anchor<>None:
|
|
|
|
|
self.addAttribute(Attribute('text-anchor', text_anchor))
|
|
|
|
|
self.setAttribute(Attribute('text-anchor', text_anchor))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return text()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class textpath(SVGelement):
|
|
|
|
@ -558,10 +651,18 @@ class textpath(SVGelement):
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,link,text=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'textPath',**args)
|
|
|
|
|
self.addAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
|
|
|
|
self.setAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
|
|
|
|
if text<>None:
|
|
|
|
|
self.text=text
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
href = get_attr_value(attributes, 'xlink:href')
|
|
|
|
|
if href and href.startswith("xlink:"):
|
|
|
|
|
href = href[len("xlink:"):]
|
|
|
|
|
return textpath(href)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class pattern(SVGelement):
|
|
|
|
|
"""p=pattern(x,y,width,height,patternUnits,**args)
|
|
|
|
|
|
|
|
|
@ -572,15 +673,20 @@ class pattern(SVGelement):
|
|
|
|
|
def __init__(self,x=None,y=None,width=None,height=None,patternUnits=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'pattern',**args)
|
|
|
|
|
if x<>None:
|
|
|
|
|
self.addAttribute(Attribute('x', x))
|
|
|
|
|
self.setAttribute(Attribute('x', x))
|
|
|
|
|
if y<>None:
|
|
|
|
|
self.addAttribute(Attribute('y', y))
|
|
|
|
|
self.setAttribute(Attribute('y', y))
|
|
|
|
|
if width<>None:
|
|
|
|
|
self.addAttribute(Attribute('width', width))
|
|
|
|
|
self.setAttribute(Attribute('width', width))
|
|
|
|
|
if height<>None:
|
|
|
|
|
self.addAttribute(Attribute('height', height))
|
|
|
|
|
self.setAttribute(Attribute('height', height))
|
|
|
|
|
if patternUnits<>None:
|
|
|
|
|
self.addAttribute(Attribute('patternUnits', patternUnits))
|
|
|
|
|
self.setAttribute(Attribute('patternUnits', patternUnits))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return pattern()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class title(SVGelement):
|
|
|
|
|
"""t=title(text,**args)
|
|
|
|
@ -593,6 +699,11 @@ class title(SVGelement):
|
|
|
|
|
if text<>None:
|
|
|
|
|
self.text=text
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return title()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class description(SVGelement):
|
|
|
|
|
"""d=description(text,**args)
|
|
|
|
|
|
|
|
|
@ -604,6 +715,11 @@ class description(SVGelement):
|
|
|
|
|
if text<>None:
|
|
|
|
|
self.text=text
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return description()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class lineargradient(SVGelement):
|
|
|
|
|
"""lg=lineargradient(x1,y1,x2,y2,id,**args)
|
|
|
|
|
|
|
|
|
@ -613,15 +729,20 @@ class lineargradient(SVGelement):
|
|
|
|
|
def __init__(self,x1=None,y1=None,x2=None,y2=None,id=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'linearGradient',**args)
|
|
|
|
|
if x1<>None:
|
|
|
|
|
self.addAttribute(Attribute('x1', x1))
|
|
|
|
|
self.setAttribute(Attribute('x1', x1))
|
|
|
|
|
if y1<>None:
|
|
|
|
|
self.addAttribute(Attribute('y1', y1))
|
|
|
|
|
self.setAttribute(Attribute('y1', y1))
|
|
|
|
|
if x2<>None:
|
|
|
|
|
self.addAttribute(Attribute('x2', x2))
|
|
|
|
|
self.setAttribute(Attribute('x2', x2))
|
|
|
|
|
if y2<>None:
|
|
|
|
|
self.addAttribute(Attribute('y2', y2))
|
|
|
|
|
self.setAttribute(Attribute('y2', y2))
|
|
|
|
|
if id<>None:
|
|
|
|
|
self.addAttribute(Attribute('id', id))
|
|
|
|
|
self.setAttribute(Attribute('id', id))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return lineargradient()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class radialgradient(SVGelement):
|
|
|
|
|
"""rg=radialgradient(cx,cy,r,fx,fy,id,**args)
|
|
|
|
@ -632,17 +753,22 @@ class radialgradient(SVGelement):
|
|
|
|
|
def __init__(self,cx=None,cy=None,r=None,fx=None,fy=None,id=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'radialGradient',**args)
|
|
|
|
|
if cx<>None:
|
|
|
|
|
self.addAttribute(Attribute('cx', cx))
|
|
|
|
|
self.setAttribute(Attribute('cx', cx))
|
|
|
|
|
if cy<>None:
|
|
|
|
|
self.addAttribute(Attribute('cy', cy))
|
|
|
|
|
self.setAttribute(Attribute('cy', cy))
|
|
|
|
|
if r<>None:
|
|
|
|
|
self.addAttribute(Attribute('r', r))
|
|
|
|
|
self.setAttribute(Attribute('r', r))
|
|
|
|
|
if fx<>None:
|
|
|
|
|
self.addAttribute(Attribute('fx', fx))
|
|
|
|
|
self.setAttribute(Attribute('fx', fx))
|
|
|
|
|
if fy<>None:
|
|
|
|
|
self.addAttribute(Attribute('fy', fy))
|
|
|
|
|
self.setAttribute(Attribute('fy', fy))
|
|
|
|
|
if id<>None:
|
|
|
|
|
self.addAttribute(Attribute('id', id))
|
|
|
|
|
self.setAttribute(Attribute('id', id))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return radialgradient()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class stop(SVGelement):
|
|
|
|
|
"""st=stop(offset,stop_color,**args)
|
|
|
|
@ -652,7 +778,12 @@ class stop(SVGelement):
|
|
|
|
|
def __init__(self,offset,stop_color=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'stop',{'offset':offset},**args)
|
|
|
|
|
if stop_color<>None:
|
|
|
|
|
self.addAttribute(Attribute('stop-color', stop_color))
|
|
|
|
|
self.setAttribute(Attribute('stop-color', stop_color))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
offset = get_attr_value(attributes, 'offset')
|
|
|
|
|
return stop(offset)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
class style(SVGelement):
|
|
|
|
|
"""st=style(type,cdata=None,**args)
|
|
|
|
@ -661,6 +792,11 @@ class style(SVGelement):
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,type,cdata=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'style',{'type':type},cdata=cdata, **args)
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
type = get_attr_value(attributes, 'type')
|
|
|
|
|
return style(type)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class image(SVGelement):
|
|
|
|
@ -677,12 +813,22 @@ class image(SVGelement):
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError, 'both height and width are required'
|
|
|
|
|
SVGelement.__init__(self,'image',{'width':width,'height':height},**args)
|
|
|
|
|
self.addAttribute(Attribute('href', url, 'xlink', xlinkNSRef))
|
|
|
|
|
self.setAttribute(Attribute('href', url, 'xlink', xlinkNSRef))
|
|
|
|
|
if x<>None:
|
|
|
|
|
self.addAttribute(Attribute('x', x))
|
|
|
|
|
self.setAttribute(Attribute('x', x))
|
|
|
|
|
if y<>None:
|
|
|
|
|
self.addAttribute(Attribute('y', y))
|
|
|
|
|
self.setAttribute(Attribute('y', y))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
href = get_attr_value(attributes, 'xlink:href')
|
|
|
|
|
if href and href.startswith("xlink:"):
|
|
|
|
|
href = href[len("xlink:"):]
|
|
|
|
|
width = get_attr_value(attributes, 'width')
|
|
|
|
|
height = get_attr_value(attributes, 'height')
|
|
|
|
|
return image(href, width=width, height=height)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cursor(SVGelement):
|
|
|
|
|
"""c=cursor(url,**args)
|
|
|
|
|
|
|
|
|
@ -690,7 +836,14 @@ class cursor(SVGelement):
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,url,**args):
|
|
|
|
|
SVGelement.__init__(self,'cursor',**args)
|
|
|
|
|
self.addAttribute(Attribute('href', url, 'xlink', xlinkNSRef))
|
|
|
|
|
self.setAttribute(Attribute('href', url, 'xlink', xlinkNSRef))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
href = get_attr_value(attributes, 'xlink:href')
|
|
|
|
|
if href and href.startswith("xlink:"):
|
|
|
|
|
href = href[len("xlink:"):]
|
|
|
|
|
return cursor(href)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class marker(SVGelement):
|
|
|
|
@ -702,17 +855,22 @@ class marker(SVGelement):
|
|
|
|
|
def __init__(self,id=None,viewBox=None,refx=None,refy=None,markerWidth=None,markerHeight=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'marker',**args)
|
|
|
|
|
if id<>None:
|
|
|
|
|
self.addAttribute(Attribute('id', id))
|
|
|
|
|
self.setAttribute(Attribute('id', id))
|
|
|
|
|
if viewBox<>None:
|
|
|
|
|
self.addAttribute(Attribute('viewBox', _viewboxlist(viewBox)))
|
|
|
|
|
self.setAttribute(Attribute('viewBox', _viewboxlist(viewBox)))
|
|
|
|
|
if refx<>None:
|
|
|
|
|
self.addAttribute(Attribute('refX', refx))
|
|
|
|
|
self.setAttribute(Attribute('refX', refx))
|
|
|
|
|
if refy<>None:
|
|
|
|
|
self.addAttribute(Attribute('refY', refy))
|
|
|
|
|
self.setAttribute(Attribute('refY', refy))
|
|
|
|
|
if markerWidth<>None:
|
|
|
|
|
self.addAttribute(Attribute('markerWidth', markerWidth))
|
|
|
|
|
self.setAttribute(Attribute('markerWidth', markerWidth))
|
|
|
|
|
if markerHeight<>None:
|
|
|
|
|
self.addAttribute(Attribute('markerHeight', markerHeight))
|
|
|
|
|
self.setAttribute(Attribute('markerHeight', markerHeight))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return marker()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class group(SVGelement):
|
|
|
|
|
"""g=group(id,**args)
|
|
|
|
@ -723,7 +881,11 @@ class group(SVGelement):
|
|
|
|
|
def __init__(self,id=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'g',**args)
|
|
|
|
|
if id<>None:
|
|
|
|
|
self.addAttribute(Attribute('id', id))
|
|
|
|
|
self.setAttribute(Attribute('id', id))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return group()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
class symbol(SVGelement):
|
|
|
|
|
"""sy=symbol(id,viewbox,**args)
|
|
|
|
@ -737,9 +899,14 @@ class symbol(SVGelement):
|
|
|
|
|
def __init__(self,id=None,viewBox=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'symbol',**args)
|
|
|
|
|
if id<>None:
|
|
|
|
|
self.addAttribute(Attribute('id', id))
|
|
|
|
|
self.setAttribute(Attribute('id', id))
|
|
|
|
|
if viewBox<>None:
|
|
|
|
|
self.addAttribute(Attribute('viewBox', _viewboxlist(viewBox)))
|
|
|
|
|
self.setAttribute(Attribute('viewBox', _viewboxlist(viewBox)))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return symbol()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class defs(SVGelement):
|
|
|
|
|
"""d=defs(**args)
|
|
|
|
@ -749,6 +916,11 @@ class defs(SVGelement):
|
|
|
|
|
def __init__(self,**args):
|
|
|
|
|
SVGelement.__init__(self,'defs',**args)
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return defs()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class switch(SVGelement):
|
|
|
|
|
"""sw=switch(**args)
|
|
|
|
|
|
|
|
|
@ -759,7 +931,11 @@ class switch(SVGelement):
|
|
|
|
|
def __init__(self,**args):
|
|
|
|
|
SVGelement.__init__(self,'switch',**args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return switch()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class use(SVGelement):
|
|
|
|
|
"""u=use(link,x,y,width,height,**args)
|
|
|
|
|
|
|
|
|
@ -767,18 +943,25 @@ class use(SVGelement):
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,link,x=None,y=None,width=None,height=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'use',**args)
|
|
|
|
|
self.addAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
|
|
|
|
self.setAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
|
|
|
|
if x<>None:
|
|
|
|
|
self.addAttribute(Attribute('x', x))
|
|
|
|
|
self.setAttribute(Attribute('x', x))
|
|
|
|
|
if y<>None:
|
|
|
|
|
self.addAttribute(Attribute('y', y))
|
|
|
|
|
self.setAttribute(Attribute('y', y))
|
|
|
|
|
|
|
|
|
|
if width<>None:
|
|
|
|
|
self.addAttribute(Attribute('width', width))
|
|
|
|
|
self.setAttribute(Attribute('width', width))
|
|
|
|
|
if height<>None:
|
|
|
|
|
self.addAttribute(Attribute('height', height))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.setAttribute(Attribute('height', height))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
href = get_attr_value(attributes, 'xlink:href')
|
|
|
|
|
if href and href.startswith("xlink:"):
|
|
|
|
|
href = href[len("xlink:"):]
|
|
|
|
|
return use(href)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class link(SVGelement):
|
|
|
|
|
"""a=link(url,**args)
|
|
|
|
|
|
|
|
|
@ -787,7 +970,15 @@ class link(SVGelement):
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,link='',**args):
|
|
|
|
|
SVGelement.__init__(self,'a',**args)
|
|
|
|
|
self.addAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
|
|
|
|
self.setAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
href = get_attr_value(attributes, 'xlink:href')
|
|
|
|
|
if href and href.startswith("xlink:"):
|
|
|
|
|
href = href[len("xlink:"):]
|
|
|
|
|
return link(href)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class view(SVGelement):
|
|
|
|
|
"""v=view(id,**args)
|
|
|
|
@ -796,7 +987,12 @@ class view(SVGelement):
|
|
|
|
|
def __init__(self,id=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'view',**args)
|
|
|
|
|
if id<>None:
|
|
|
|
|
self.addAttribute(Attribute('id', id))
|
|
|
|
|
self.setAttribute(Attribute('id', id))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return view()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class script(SVGelement):
|
|
|
|
|
"""sc=script(type,type,cdata,**args)
|
|
|
|
@ -806,6 +1002,12 @@ class script(SVGelement):
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,type,cdata=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'script',{'type':type},cdata=cdata,**args)
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
type = get_attr_value(attributes, 'type')
|
|
|
|
|
return script(type, cdata=cdata)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class animate(SVGelement):
|
|
|
|
|
"""an=animate(attribute,from,to,during,**args)
|
|
|
|
@ -815,23 +1017,34 @@ class animate(SVGelement):
|
|
|
|
|
def __init__(self,attribute,fr=None,to=None,dur=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'animate',{'attributeName':attribute},**args)
|
|
|
|
|
if fr<>None:
|
|
|
|
|
self.addAttribute(Attribute('from', fr))
|
|
|
|
|
self.setAttribute(Attribute('from', fr))
|
|
|
|
|
if to<>None:
|
|
|
|
|
self.addAttribute(Attribute('to', to))
|
|
|
|
|
self.setAttribute(Attribute('to', to))
|
|
|
|
|
if dur<>None:
|
|
|
|
|
self.addAttribute(Attribute('dur', dur))
|
|
|
|
|
self.setAttribute(Attribute('dur', dur))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
attribute = get_attr_value(attributes, 'attributeName')
|
|
|
|
|
return animate(attribute)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class animateMotion(SVGelement):
|
|
|
|
|
"""an=animateMotion(pathdata,dur,**args)
|
|
|
|
|
|
|
|
|
|
animates a SVGelement over the given path in dur seconds
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,pathdata,dur,**args):
|
|
|
|
|
def __init__(self,pathdata=None,dur=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'animateMotion',**args)
|
|
|
|
|
if pathdata<>None:
|
|
|
|
|
self.addAttribute(Attribute('path', str(pathdata)))
|
|
|
|
|
self.setAttribute(Attribute('path', str(pathdata)))
|
|
|
|
|
if dur<>None:
|
|
|
|
|
self.addAttribute(Attribute('dur', dur))
|
|
|
|
|
self.setAttribute(Attribute('dur', dur))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return animateMotion()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class animateTransform(SVGelement):
|
|
|
|
|
"""antr=animateTransform(type,from,to,dur,**args)
|
|
|
|
@ -842,13 +1055,19 @@ class animateTransform(SVGelement):
|
|
|
|
|
SVGelement.__init__(self,'animateTransform',{'attributeName':'transform'},**args)
|
|
|
|
|
#As far as I know the attributeName is always transform
|
|
|
|
|
if type<>None:
|
|
|
|
|
self.addAttribute(Attribute('type', type))
|
|
|
|
|
self.setAttribute(Attribute('type', type))
|
|
|
|
|
if fr<>None:
|
|
|
|
|
self.addAttribute(Attribute('from', fr))
|
|
|
|
|
self.setAttribute(Attribute('from', fr))
|
|
|
|
|
if to<>None:
|
|
|
|
|
self.addAttribute(Attribute('to', to))
|
|
|
|
|
self.setAttribute(Attribute('to', to))
|
|
|
|
|
if dur<>None:
|
|
|
|
|
self.addAttribute(Attribute('dur', dur))
|
|
|
|
|
self.setAttribute(Attribute('dur', dur))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return animateTransform()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class animateColor(SVGelement):
|
|
|
|
|
"""ac=animateColor(attribute,type,from,to,dur,**args)
|
|
|
|
|
|
|
|
|
@ -857,13 +1076,20 @@ class animateColor(SVGelement):
|
|
|
|
|
def __init__(self,attribute,type=None,fr=None,to=None,dur=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'animateColor',{'attributeName':attribute},**args)
|
|
|
|
|
if type<>None:
|
|
|
|
|
self.addAttribute(Attribute('type', type))
|
|
|
|
|
self.setAttribute(Attribute('type', type))
|
|
|
|
|
if fr<>None:
|
|
|
|
|
self.addAttribute(Attribute('from', fr))
|
|
|
|
|
self.setAttribute(Attribute('from', fr))
|
|
|
|
|
if to<>None:
|
|
|
|
|
self.addAttribute(Attribute('to', to))
|
|
|
|
|
self.setAttribute(Attribute('to', to))
|
|
|
|
|
if dur<>None:
|
|
|
|
|
self.addAttribute(Attribute('dur', dur))
|
|
|
|
|
self.setAttribute(Attribute('dur', dur))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
attribute = get_attr_value(attributes, 'attributeName')
|
|
|
|
|
return animateColor(attribute)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class set(SVGelement):
|
|
|
|
|
"""st=set(attribute,to,during,**args)
|
|
|
|
|
|
|
|
|
@ -872,12 +1098,16 @@ class set(SVGelement):
|
|
|
|
|
def __init__(self,attribute,to=None,dur=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'set',{'attributeName':attribute},**args)
|
|
|
|
|
if to<>None:
|
|
|
|
|
self.addAttribute(Attribute('to', to))
|
|
|
|
|
self.setAttribute(Attribute('to', to))
|
|
|
|
|
if dur<>None:
|
|
|
|
|
self.addAttribute(Attribute('dur', dur))
|
|
|
|
|
self.setAttribute(Attribute('dur', dur))
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
attribute = get_attr_value(attributes, 'attributeName')
|
|
|
|
|
return set(attribute)
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class svg(SVGelement):
|
|
|
|
|
"""s=svg(viewbox,width,height,**args)
|
|
|
|
|
|
|
|
|
@ -896,12 +1126,113 @@ class svg(SVGelement):
|
|
|
|
|
def __init__(self,viewBox=None, width=None, height=None,**args):
|
|
|
|
|
SVGelement.__init__(self,'svg',**args)
|
|
|
|
|
if viewBox<>None:
|
|
|
|
|
self.addAttribute(Attribute('viewBox', _viewboxlist(viewBox)))
|
|
|
|
|
self.setAttribute(Attribute('viewBox', _viewboxlist(viewBox)))
|
|
|
|
|
if width<>None:
|
|
|
|
|
self.addAttribute(Attribute('width', width))
|
|
|
|
|
self.setAttribute(Attribute('width', width))
|
|
|
|
|
if height<>None:
|
|
|
|
|
self.addAttribute(Attribute('height', height_))
|
|
|
|
|
self.setAttribute(Attribute('height', height_))
|
|
|
|
|
self.namespace="http://www.w3.org/2000/svg"
|
|
|
|
|
|
|
|
|
|
def _construct(attributes):
|
|
|
|
|
return svg()
|
|
|
|
|
_construct = staticmethod(_construct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ElementHelper(object):
|
|
|
|
|
def __init__(self, name, attrs):
|
|
|
|
|
self.name = name
|
|
|
|
|
self.attrs = attrs
|
|
|
|
|
self.cdata = None
|
|
|
|
|
self.text = None
|
|
|
|
|
|
|
|
|
|
class Stack(object):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self._stack = []
|
|
|
|
|
|
|
|
|
|
def push(self, obj):
|
|
|
|
|
self._stack.append(obj)
|
|
|
|
|
|
|
|
|
|
def pop(self):
|
|
|
|
|
if len(self._stack) == 0:
|
|
|
|
|
return None
|
|
|
|
|
obj = self._stack[-1]
|
|
|
|
|
del self._stack[-1]
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
def peek(self):
|
|
|
|
|
if len(self._stack) == 0:
|
|
|
|
|
return None
|
|
|
|
|
return self._stack[-1]
|
|
|
|
|
|
|
|
|
|
def is_empty(self):
|
|
|
|
|
if len(self._stack) == 0:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def size(self):
|
|
|
|
|
return len(self._stack)
|
|
|
|
|
|
|
|
|
|
class SVGImportParser(object):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self._indent = 0
|
|
|
|
|
self._cdata = 0
|
|
|
|
|
self._stack = Stack()
|
|
|
|
|
self._base = None
|
|
|
|
|
self._debug = False
|
|
|
|
|
|
|
|
|
|
def getBaseElement(self):
|
|
|
|
|
return self._base
|
|
|
|
|
|
|
|
|
|
def log(self, msg):
|
|
|
|
|
if self._debug:
|
|
|
|
|
print " " * self._indent + msg
|
|
|
|
|
|
|
|
|
|
def StartElementHandler(self, name, attrs):
|
|
|
|
|
self.log("<%s>" % name)
|
|
|
|
|
self._indent = self._indent + 1
|
|
|
|
|
for attrname, attrvalue in attrs.items():
|
|
|
|
|
self.log("%s = %s" % (attrname, attrvalue))
|
|
|
|
|
parent = self._stack.peek()
|
|
|
|
|
elt = SVGelement.construct(name, attrs)
|
|
|
|
|
if not self._base:
|
|
|
|
|
self._base = elt
|
|
|
|
|
if parent:
|
|
|
|
|
parent.addElement(elt)
|
|
|
|
|
self._stack.push(elt)
|
|
|
|
|
|
|
|
|
|
def StartCdataSectionHandler(self):
|
|
|
|
|
self._cdata = 1
|
|
|
|
|
|
|
|
|
|
def CharacterDataHandler(self, content):
|
|
|
|
|
if self._cdata:
|
|
|
|
|
self.log("CDATA = '%s'" % content)
|
|
|
|
|
elt = self._stack.peek()
|
|
|
|
|
if elt:
|
|
|
|
|
elt.cdata = elt.cdata + content
|
|
|
|
|
else:
|
|
|
|
|
if len(content) and not content.isspace():
|
|
|
|
|
self.log("Text = '%s'" % content.strip())
|
|
|
|
|
elt = self._stack.peek()
|
|
|
|
|
if elt:
|
|
|
|
|
elt.text = elt.text + content.strip()
|
|
|
|
|
|
|
|
|
|
def EndCdataSectionHandler(self):
|
|
|
|
|
self._cdata = 0
|
|
|
|
|
|
|
|
|
|
def EndElementHandler(self, name):
|
|
|
|
|
self._indent = self._indent - 1
|
|
|
|
|
self.log("</%s>" % name)
|
|
|
|
|
self._stack.pop()
|
|
|
|
|
|
|
|
|
|
def CommentHandler(self, comment):
|
|
|
|
|
self.log("Comment = '%s'" % comment)
|
|
|
|
|
|
|
|
|
|
def StartNamespaceDeclHandler(self, prefix, uri):
|
|
|
|
|
self.log("Namespace = '%s' -> '%s'" % (prefix, uri))
|
|
|
|
|
|
|
|
|
|
def EndNamespaceDeclHandler(self, prefix):
|
|
|
|
|
self.log("Namespace = '%s'" % prefix)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class drawing:
|
|
|
|
|
"""d=drawing()
|
|
|
|
@ -921,6 +1252,30 @@ class drawing:
|
|
|
|
|
self.svg=svg
|
|
|
|
|
#Voeg een element toe aan de grafiek toe.
|
|
|
|
|
|
|
|
|
|
def fromXml(self, xml):
|
|
|
|
|
HANDLER_NAMES = [
|
|
|
|
|
'StartElementHandler', 'EndElementHandler',
|
|
|
|
|
'CharacterDataHandler',
|
|
|
|
|
'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler',
|
|
|
|
|
'CommentHandler',
|
|
|
|
|
'StartCdataSectionHandler', 'EndCdataSectionHandler',
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# Create a parser
|
|
|
|
|
parser = expat.ParserCreate()
|
|
|
|
|
|
|
|
|
|
handler = SVGImportParser()
|
|
|
|
|
for name in HANDLER_NAMES:
|
|
|
|
|
setattr(parser, name, getattr(handler, name))
|
|
|
|
|
parser.returns_unicode = 0
|
|
|
|
|
|
|
|
|
|
# Parse the XML
|
|
|
|
|
parser.Parse(xml)
|
|
|
|
|
base_element = handler.getBaseElement()
|
|
|
|
|
if not base_element or not isinstance(base_element, svg):
|
|
|
|
|
raise Exception("Base element was not SVG.")
|
|
|
|
|
self.setSVG(base_element)
|
|
|
|
|
|
|
|
|
|
if use_dom_implementation==0:
|
|
|
|
|
def toXml(self, filename='',compress=False):
|
|
|
|
|
import cStringIO
|
|
|
|
@ -1032,13 +1387,60 @@ class drawing:
|
|
|
|
|
print "SVG well formed"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__=='__main__':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elementTable = {
|
|
|
|
|
'tspan': tspan,
|
|
|
|
|
'tref': tref,
|
|
|
|
|
'rect': rect,
|
|
|
|
|
'ellipse': ellipse,
|
|
|
|
|
'circle': circle,
|
|
|
|
|
'line': line,
|
|
|
|
|
'polyline': polyline,
|
|
|
|
|
'polygon': polygon,
|
|
|
|
|
'path': path,
|
|
|
|
|
'text': text,
|
|
|
|
|
'textPath': textpath,
|
|
|
|
|
'pattern': pattern,
|
|
|
|
|
'title': title,
|
|
|
|
|
'desc': description,
|
|
|
|
|
'linearGradient': lineargradient,
|
|
|
|
|
'radialGradient': radialgradient,
|
|
|
|
|
'stop': stop,
|
|
|
|
|
'style': style,
|
|
|
|
|
'image': image,
|
|
|
|
|
'cursor': cursor,
|
|
|
|
|
'marker': marker,
|
|
|
|
|
'g': group,
|
|
|
|
|
'symbol': symbol,
|
|
|
|
|
'defs': defs,
|
|
|
|
|
'switch': switch,
|
|
|
|
|
'use': use,
|
|
|
|
|
'a': link,
|
|
|
|
|
'view': view,
|
|
|
|
|
'script': script,
|
|
|
|
|
'animate': animate,
|
|
|
|
|
'animateMotion': animateMotion,
|
|
|
|
|
'animateTransform': animateTransform,
|
|
|
|
|
'animateColor': animateColor,
|
|
|
|
|
'set': set,
|
|
|
|
|
'svg': svg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
d=drawing()
|
|
|
|
|
import sys
|
|
|
|
|
f = open(sys.argv[1], "r")
|
|
|
|
|
fc = f.read()
|
|
|
|
|
d.fromXml(fc)
|
|
|
|
|
print d.toXml()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s=svg((0,0,100,100))
|
|
|
|
|
r=rect(-100,-100,300,300,'cyan')
|
|
|
|
|
s.addElement(r)
|
|
|
|
|
|
|
|
|
|
text = tspan("Foobar")
|
|
|
|
|
s.addElement(text)
|
|
|
|
|
|
|
|
|
|
t=title('SVGdraw Demo')
|
|
|
|
|
s.addElement(t)
|
|
|
|
@ -1052,8 +1454,8 @@ if __name__=='__main__':
|
|
|
|
|
pd.relsmbezier(10,5,0,10)
|
|
|
|
|
pd.relsmbezier(-10,5,0,10)
|
|
|
|
|
an=animateMotion(pd,10)
|
|
|
|
|
an.addAttribute(Attribute('rotate', 'auto-reverse'))
|
|
|
|
|
an.addAttribute(Attribute('repeatCount', "indefinite"))
|
|
|
|
|
an.setAttribute(Attribute('rotate', 'auto-reverse'))
|
|
|
|
|
an.setAttribute(Attribute('repeatCount', "indefinite"))
|
|
|
|
|
g.addElement(an)
|
|
|
|
|
s.addElement(g)
|
|
|
|
|
for i in range(20,120,20):
|
|
|
|
@ -1067,3 +1469,6 @@ if __name__=='__main__':
|
|
|
|
|
|
|
|
|
|
print d.toXml()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__=='__main__':
|
|
|
|
|
main()
|
|
|
|
|