SVGdraw.py - generate SVG drawings¶
- Tags
Python
This module has been copied from 3rd party resources.
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.com
-
class
SVGdraw.pathdata(x=None, y=None)¶ Bases:
objectclass 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.
-
closepath()¶ ends the path
-
move(x, y)¶ move to absolute
-
relmove(x, y)¶ move to relative
-
line(x, y)¶ line to absolute
-
relline(x, y)¶ line to relative
-
hline(x)¶ horizontal line to absolute
-
relhline(x)¶ horizontal line to relative
-
vline(y)¶ verical line to absolute
-
relvline(y)¶ vertical line to relative
-
bezier(x1, y1, x2, y2, x, y)¶ bezier with xy1 and xy2 to xy absolut
-
relbezier(x1, y1, x2, y2, x, y)¶ bezier with xy1 and xy2 to xy relative
-
smbezier(x2, y2, x, y)¶ smooth bezier with xy2 to xy absolut
-
relsmbezier(x2, y2, x, y)¶ smooth bezier with xy2 to xy relative
-
qbezier(x1, y1, x, y)¶ quadratic bezier with xy1 to xy absolut
-
relqbezier(x1, y1, x, y)¶ quadratic bezier with xy1 to xy relative
-
smqbezier(x, y)¶ smooth quadratic bezier to xy absolut
-
relsmqbezier(x, y)¶ smooth quadratic bezier to xy relative
-
ellarc(rx, ry, xrot, laf, sf, x, y)¶ elliptival arc with rx and ry rotating with xrot using large-arc-flag and sweep-flag to xy absolut
-
relellarc(rx, ry, xrot, laf, sf, x, y)¶ elliptival arc with rx and ry rotating with xrot using large-arc-flag and sweep-flag to xy relative
-
-
class
SVGdraw.SVGelement(type, attributes, elements, text, namespace, **args)¶ Bases:
objectCreates 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.
-
addElement(SVGelement)¶ adds an element to a SVGelement
SVGelement.addElement(SVGelement)
-
-
class
SVGdraw.tspan(text=None, **args)¶ Bases:
SVGdraw.SVGelementts=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)
-
class
SVGdraw.tref(link, **args)¶ Bases:
SVGdraw.SVGelementtr=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)
-
class
SVGdraw.spannedtext(textlist=None)¶ Bases:
objectst=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)
-
class
SVGdraw.rect(x=None, y=None, width=None, height=None, fill=None, stroke=None, stroke_width=None, **args)¶ Bases:
SVGdraw.SVGelementr=rect(width,height,x,y,fill,stroke,stroke_width,**args)
a rectangle is defined by a width and height and a xy pair
-
class
SVGdraw.ellipse(cx=None, cy=None, rx=None, ry=None, fill=None, stroke=None, stroke_width=None, **args)¶ Bases:
SVGdraw.SVGelemente=ellipse(rx,ry,x,y,fill,stroke,stroke_width,**args)
an ellipse is defined as a center and a x and y radius.
-
class
SVGdraw.circle(cx=None, cy=None, r=None, fill=None, stroke=None, stroke_width=None, **args)¶ Bases:
SVGdraw.SVGelementc=circle(x,y,radius,fill,stroke,stroke_width,**args)
The circle creates an element using a x, y and radius values eg
-
class
SVGdraw.point(x, y, fill='black', **args)¶ Bases:
SVGdraw.circlep=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.
-
class
SVGdraw.line(x1=None, y1=None, x2=None, y2=None, stroke=None, stroke_width=None, **args)¶ Bases:
SVGdraw.SVGelementl=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
-
class
SVGdraw.polyline(points, fill=None, stroke=None, stroke_width=None, **args)¶ Bases:
SVGdraw.SVGelementpl=polyline([[x1,y1],[x2,y2],…],fill,stroke,stroke_width,**args)
a polyline is defined by a list of xy pairs
-
class
SVGdraw.polygon(points, fill=None, stroke=None, stroke_width=None, **args)¶ Bases:
SVGdraw.SVGelementpl=polyline([[x1,y1],[x2,y2],…],fill,stroke,stroke_width,**args)
a polygon is defined by a list of xy pairs
-
class
SVGdraw.path(pathdata, fill=None, stroke=None, stroke_width=None, id=None, **args)¶ Bases:
SVGdraw.SVGelementp=path(path,fill,stroke,stroke_width,**args)
a path is defined by a path object and optional width, stroke and fillcolor
-
class
SVGdraw.text(x=None, y=None, text=None, font_size=None, font_family=None, text_anchor=None, font_style=None, **args)¶ Bases:
SVGdraw.SVGelementt=text(x,y,text,font_size,font_family,**args)
a text element can bge used for displaying text on the screen
-
class
SVGdraw.textpath(link, text=None, **args)¶ Bases:
SVGdraw.SVGelementtp=textpath(text,link,**args)
a textpath places a text on a path which is referenced by a link.
-
class
SVGdraw.pattern(x=None, y=None, width=None, height=None, patternUnits=None, **args)¶ Bases:
SVGdraw.SVGelementp=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.
-
class
SVGdraw.title(text=None, **args)¶ Bases:
SVGdraw.SVGelementt=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
-
class
SVGdraw.description(text=None, **args)¶ Bases:
SVGdraw.SVGelementd=description(text,**args)
a description can be added to any element and is used for a tooltip Add this element before adding other elements.
-
class
SVGdraw.lineargradient(x1=None, y1=None, x2=None, y2=None, id=None, **args)¶ Bases:
SVGdraw.SVGelementlg=lineargradient(x1,y1,x2,y2,id,**args)
defines a lineargradient using two xy pairs. stop elements van be added to define the gradient colors.
-
class
SVGdraw.radialgradient(cx=None, cy=None, r=None, fx=None, fy=None, id=None, **args)¶ Bases:
SVGdraw.SVGelementrg=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.
-
class
SVGdraw.stop(offset, stop_color=None, **args)¶ Bases:
SVGdraw.SVGelementst=stop(offset,stop_color,**args)
Puts a stop color at the specified radius
-
class
SVGdraw.style(type, cdata=None, **args)¶ Bases:
SVGdraw.SVGelementst=style(type,cdata=None,**args)
Add a CDATA element to this element for defing in line stylesheets etc..
-
class
SVGdraw.image(url, x=None, y=None, width=None, height=None, **args)¶ Bases:
SVGdraw.SVGelementim=image(url,width,height,x,y,**args)
adds an image to the drawing. Supported formats are .png, .jpg and .svg.
-
class
SVGdraw.cursor(url, **args)¶ Bases:
SVGdraw.SVGelementc=cursor(url,**args)
defines a custom cursor for a element or a drawing
-
class
SVGdraw.marker(id=None, viewBox=None, refx=None, refy=None, markerWidth=None, markerHeight=None, **args)¶ Bases:
SVGdraw.SVGelementm=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.
-
class
SVGdraw.group(id=None, **args)¶ Bases:
SVGdraw.SVGelementg=group(id,**args)
a group is defined by an id and is used to contain elements g.addElement(SVGelement)
-
class
SVGdraw.symbol(id=None, viewBox=None, **args)¶ Bases:
SVGdraw.SVGelementsy=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)
-
class
SVGdraw.defs(**args)¶ Bases:
SVGdraw.SVGelementd=defs(
**args)container for defining elements
-
class
SVGdraw.switch(**args)¶ Bases:
SVGdraw.SVGelementsw=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.
-
class
SVGdraw.use(link, x=None, y=None, width=None, height=None, **args)¶ Bases:
SVGdraw.SVGelementu=use(link,x,y,width,height,``**args``)
references a symbol by linking to its id and its position, height and width
-
class
SVGdraw.link(link='', **args)¶ Bases:
SVGdraw.SVGelementa=link(url,``**args``)
a link is defined by a hyperlink. add elements which have to be linked a.addElement(SVGelement)
-
class
SVGdraw.view(id=None, **args)¶ Bases:
SVGdraw.SVGelementv=view(id,``**args``)
a view can be used to create a view with different attributes
-
class
SVGdraw.script(type, cdata=None, **args)¶ Bases:
SVGdraw.SVGelementsc=script(type,type,cdata,``**args``)
adds a script element which contains CDATA to the SVG drawing
-
class
SVGdraw.animate(attribute, fr=None, to=None, dur=None, **args)¶ Bases:
SVGdraw.SVGelementan=animate(attribute,from,to,during,``**args``)
animates an attribute.
-
class
SVGdraw.animateMotion(pathdata, dur, **args)¶ Bases:
SVGdraw.SVGelementan=animateMotion(pathdata,dur,``**args``)
animates a SVGelement over the given path in dur seconds
-
class
SVGdraw.animateTransform(type=None, fr=None, to=None, dur=None, **args)¶ Bases:
SVGdraw.SVGelementantr=animateTransform(type,from,to,dur,``**args``)
transform an element from and to a value.
-
class
SVGdraw.animateColor(attribute, type=None, fr=None, to=None, dur=None, **args)¶ Bases:
SVGdraw.SVGelementac=animateColor(attribute,type,from,to,dur,``**args``)
Animates the color of a element
-
class
SVGdraw.set(attribute, to=None, dur=None, **args)¶ Bases:
SVGdraw.SVGelementst=set(attribute,to,during,``**args``)
sets an attribute to a value for a
-
class
SVGdraw.svg(viewBox=None, width=None, height=None, **args)¶ Bases:
SVGdraw.SVGelements=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()