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 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.
ends the path
move to absolute
move to relative
line to absolute
line to relative
horizontal line to absolute
horizontal line to relative
verical line to absolute
vertical line to relative
bezier with xy1 and xy2 to xy absolut
bezier with xy1 and xy2 to xy relative
smooth bezier with xy2 to xy absolut
smooth bezier with xy2 to xy relative
quadratic bezier with xy1 to xy absolut
quadratic bezier with xy1 to xy relative
smooth quadratic bezier to xy absolut
smooth quadratic bezier to xy relative
elliptival arc with rx and ry rotating with xrot using large-arc-flag and sweep-flag to xy absolut
elliptival arc with rx and ry rotating with xrot using large-arc-flag and sweep-flag to xy relative
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.
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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)
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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)
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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)
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
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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.
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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.
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args)
a polyline is defined by a list of xy pairs
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args)
a polygon is defined by a list of xy pairs
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
tp=textpath(text,link,**args)
a textpath places a text on a path which is referenced by a link.
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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.
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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.
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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.
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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.
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
st=stop(offset,stop_color,**args)
Puts a stop color at the specified radius
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
st=style(type,cdata=None,**args)
Add a CDATA element to this element for defing in line stylesheets etc..
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
im=image(url,width,height,x,y,**args)
adds an image to the drawing. Supported formats are .png, .jpg and .svg.
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
c=cursor(url,**args)
defines a custom cursor for a element or a drawing
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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.
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
g=group(id,**args)
a group is defined by an id and is used to contain elements g.addElement(SVGelement)
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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)
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
d=defs(**args)
container for defining elements
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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.
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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
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)
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
v=view(id,``**args``)
a view can be used to create a view with different attributes
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
sc=script(type,type,cdata,``**args``)
adds a script element which contains CDATA to the SVG drawing
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
an=animate(attribute,from,to,during,``**args``)
animates an attribute.
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
an=animateMotion(pathdata,dur,``**args``)
animates a SVGelement over the given path in dur seconds
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
antr=animateTransform(type,from,to,dur,``**args``)
transform an element from and to a value.
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
ac=animateColor(attribute,type,from,to,dur,``**args``)
Animates the color of a element
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
Bases: SVGdraw.SVGelement
st=set(attribute,to,during,``**args``)
sets an attribute to a value for a
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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()
adds an element to a SVGelement
SVGelement.addElement(SVGelement)
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)