SVGdraw.py - generate SVG drawings

Author:Fedor Baart & Hans de Wit
Release:$Id$
Date:December 09, 2013
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)

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.

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=None, elements=None, text='', namespace='', cdata=None, **args)

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.

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.tspan(text=None, **args)

Bases: SVGdraw.SVGelement

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)

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.tref(link, **args)

Bases: SVGdraw.SVGelement

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)

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.spannedtext(textlist=None)

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)

class SVGdraw.rect(x=None, y=None, width=None, height=None, fill=None, stroke=None, stroke_width=None, **args)

Bases: SVGdraw.SVGelement

r=rect(width,height,x,y,fill,stroke,stroke_width,**args)

a rectangle is defined by a width and height and a xy pair

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.ellipse(cx=None, cy=None, rx=None, ry=None, fill=None, stroke=None, stroke_width=None, **args)

Bases: SVGdraw.SVGelement

e=ellipse(rx,ry,x,y,fill,stroke,stroke_width,**args)

an ellipse is defined as a center and a x and y radius.

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.circle(cx=None, cy=None, r=None, fill=None, stroke=None, stroke_width=None, **args)

Bases: SVGdraw.SVGelement

c=circle(x,y,radius,fill,stroke,stroke_width,**args)

The circle creates an element using a x, y and radius values eg

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.point(x, y, fill='black', **args)

Bases: SVGdraw.circle

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.

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.line(x1=None, y1=None, x2=None, y2=None, stroke=None, stroke_width=None, **args)

Bases: SVGdraw.SVGelement

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

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.polyline(points, fill=None, stroke=None, stroke_width=None, **args)

Bases: SVGdraw.SVGelement

pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args)

a polyline is defined by a list of xy pairs

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.polygon(points, fill=None, stroke=None, stroke_width=None, **args)

Bases: SVGdraw.SVGelement

pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args)

a polygon is defined by a list of xy pairs

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.path(pathdata, fill=None, stroke=None, stroke_width=None, id=None, **args)

Bases: SVGdraw.SVGelement

p=path(path,fill,stroke,stroke_width,**args)

a path is defined by a path object and optional width, stroke and fillcolor

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.text(x=None, y=None, text=None, font_size=None, font_family=None, text_anchor=None, font_style=None, **args)

Bases: SVGdraw.SVGelement

t=text(x,y,text,font_size,font_family,**args)

a text element can bge used for displaying text on the screen

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.textpath(link, text=None, **args)

Bases: SVGdraw.SVGelement

tp=textpath(text,link,**args)

a textpath places a text on a path which is referenced by a link.

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.pattern(x=None, y=None, width=None, height=None, patternUnits=None, **args)

Bases: SVGdraw.SVGelement

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.

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.title(text=None, **args)

Bases: SVGdraw.SVGelement

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

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.description(text=None, **args)

Bases: SVGdraw.SVGelement

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.

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.lineargradient(x1=None, y1=None, x2=None, y2=None, id=None, **args)

Bases: SVGdraw.SVGelement

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.

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.radialgradient(cx=None, cy=None, r=None, fx=None, fy=None, id=None, **args)

Bases: SVGdraw.SVGelement

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.

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.stop(offset, stop_color=None, **args)

Bases: SVGdraw.SVGelement

st=stop(offset,stop_color,**args)

Puts a stop color at the specified radius

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.style(type, cdata=None, **args)

Bases: SVGdraw.SVGelement

st=style(type,cdata=None,**args)

Add a CDATA element to this element for defing in line stylesheets etc..

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.image(url, x=None, y=None, width=None, height=None, **args)

Bases: SVGdraw.SVGelement

im=image(url,width,height,x,y,**args)

adds an image to the drawing. Supported formats are .png, .jpg and .svg.

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.cursor(url, **args)

Bases: SVGdraw.SVGelement

c=cursor(url,**args)

defines a custom cursor for a element or a drawing

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.marker(id=None, viewBox=None, refx=None, refy=None, markerWidth=None, markerHeight=None, **args)

Bases: SVGdraw.SVGelement

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.

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.group(id=None, **args)

Bases: SVGdraw.SVGelement

g=group(id,**args)

a group is defined by an id and is used to contain elements g.addElement(SVGelement)

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.symbol(id=None, viewBox=None, **args)

Bases: SVGdraw.SVGelement

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)

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.defs(**args)

Bases: SVGdraw.SVGelement

d=defs(**args)

container for defining elements

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.switch(**args)

Bases: SVGdraw.SVGelement

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.

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.use(link, x=None, y=None, width=None, height=None, **args)

Bases: SVGdraw.SVGelement

u=use(link,x,y,width,height,``**args``)

references a symbol by linking to its id and its position, height and width

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

Bases: SVGdraw.SVGelement

a=link(url,``**args``)

a link is defined by a hyperlink. add elements which have to be linked a.addElement(SVGelement)

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.view(id=None, **args)

Bases: SVGdraw.SVGelement

v=view(id,``**args``)

a view can be used to create a view with different attributes

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.script(type, cdata=None, **args)

Bases: SVGdraw.SVGelement

sc=script(type,type,cdata,``**args``)

adds a script element which contains CDATA to the SVG drawing

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.animate(attribute, fr=None, to=None, dur=None, **args)

Bases: SVGdraw.SVGelement

an=animate(attribute,from,to,during,``**args``)

animates an attribute.

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.animateMotion(pathdata, dur, **args)

Bases: SVGdraw.SVGelement

an=animateMotion(pathdata,dur,``**args``)

animates a SVGelement over the given path in dur seconds

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.animateTransform(type=None, fr=None, to=None, dur=None, **args)

Bases: SVGdraw.SVGelement

antr=animateTransform(type,from,to,dur,``**args``)

transform an element from and to a value.

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.animateColor(attribute, type=None, fr=None, to=None, dur=None, **args)

Bases: SVGdraw.SVGelement

ac=animateColor(attribute,type,from,to,dur,``**args``)

Animates the color of a element

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.set(attribute, to=None, dur=None, **args)

Bases: SVGdraw.SVGelement

st=set(attribute,to,during,``**args``)

sets an attribute to a value for a

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.svg(viewBox=None, width=None, height=None, **args)

Bases: SVGdraw.SVGelement

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()

addElement(SVGelement)

adds an element to a SVGelement

SVGelement.addElement(SVGelement)

class SVGdraw.drawing

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)

Previous topic

<no title>

Next topic

SetTools.py - Tools for working on sets

This Page