TotalDepth.util.plot.SVGWriter (SVG Writer Module)

An SVG writer.

TODO: Add a float format to reduce the size of the SVG file.

exception TotalDepth.util.plot.SVGWriter.ExceptionSVGWriter

Exception class for SVGWriter.

TotalDepth.util.plot.SVGWriter.DEFAULT_VALUE_FORMAT = '{:.3f}'

Defaults format for points that are specified in inches or such like

TotalDepth.util.plot.SVGWriter.DEFAULT_VALUE_FORMAT_POINTS = '{:.1f}'

Defaults format for points that are specified in pixels

TotalDepth.util.plot.SVGWriter.dimToTxt(theDim)

Converts a Coord.Dim() object to text for SVG units.

class TotalDepth.util.plot.SVGWriter.SVGWriter(theFile, theViewPort, rootAttrs=None)
__init__(theFile, theViewPort, rootAttrs=None)

Initialise the stream with a file and Coord.Box() object. The view port units must be the same for width and depth.

__enter__()

Context manager support.

class TotalDepth.util.plot.SVGWriter.SVGGroup(theXmlStream, attrs=None)

A group element in SVG.

__init__(theXmlStream, attrs=None)

Initialise the group with a stream.

See: http://www.w3.org/TR/2003/REC-SVG11-20030114/struct.html#GElement

Sadly we can’t use **kwargs because of Python restrictions on keyword names. stroke-width is not a valid keyword argument (although stroke_width would be). So instead we pass in an optional dictionary {string : string, …}

class TotalDepth.util.plot.SVGWriter.SVGRect(theXmlStream, thePoint, theBox, attrs=None)

A rectangle in SVG. Initialise the rectangle with a stream, a Coord.Pt() and a Coord.Box() objects.

See: http://www.w3.org/TR/2003/REC-SVG11-20030114/shapes.html#RectElement

Typical attributes:

{'fill' : "blue", 'stroke' : "black", 'stroke-width' : "2", }
__init__(theXmlStream, thePoint, theBox, attrs=None)

Initialise the rectangle with a stream, a Coord.Pt() and a Coord.Box() objects.

See: http://www.w3.org/TR/2003/REC-SVG11-20030114/shapes.html#RectElement

Typical attributes:

{'fill' : "blue", 'stroke' : "black", 'stroke-width' : "2", }
class TotalDepth.util.plot.SVGWriter.SVGCircle(theXmlStream, thePoint, theRadius, attrs=None)

A circle in SVG.Initialise the circle with a stream, a Coord.Pt() and a Coord.Dim() objects.

See: http://www.w3.org/TR/2003/REC-SVG11-20030114/shapes.html#CircleElement

__init__(theXmlStream, thePoint, theRadius, attrs=None)

Initialise the circle with a stream, a Coord.Pt() and a Coord.Dim() objects.

class TotalDepth.util.plot.SVGWriter.SVGElipse(theXmlStream, ptFrom, theRadX, theRadY, attrs=None)

An elipse in SVG.Initialise the circle with a stream, a Coord.Pt() and a Coord.Dim() objects.

See: http://www.w3.org/TR/2003/REC-SVG11-20030114/shapes.html#EllipseElement

__init__(theXmlStream, ptFrom, theRadX, theRadY, attrs=None)

Initialise the circle with a stream, a Coord.Pt() and a Coord.Dim() objects.

class TotalDepth.util.plot.SVGWriter.SVGLine(theXmlStream, ptFrom, ptTo, attrs=None)

A rectangle in SVG. Initialise the line with a stream, and two Coord.Pt() objects.

See: http://www.w3.org/TR/2003/REC-SVG11-20030114/shapes.html#LineElement

__init__(theXmlStream, ptFrom, ptTo, attrs=None)

Initialise the line with a stream, and two Coord.Pt() objects.

class TotalDepth.util.plot.SVGWriter.SVGPointList(theXmlStream, name, pointS, attrs)

An abstract class that takes a list of points, derived by polyline and polygon.

Initialise the element with a stream, a name, and a list of Coord.Pt() objects.

NOTE: The units of the points are ignored, it is up to the caller to convert them to the User Coordinate System.

TODO: Make the caller supply points as numbers not Coord.Pt as this may be faster??? e.g.:

FMT_PAIR_STR = FMT_STR + ',' + FMT_STR
' '.join([self.FMT_PAIR_STR.format(x, y) for x,y in pointS])
__init__(theXmlStream, name, pointS, attrs)

Initialise the element with a stream, a name, and a list of Coord.Pt() objects. NOTE: The units of the points are ignored, it is up to the caller to convert them to the User Coordinate System. TODO: Make the caller supply points as numbers not Coord.Pt as this may be faster??? e.g. FMT_PAIR_STR = FMT_STR + ‘,’ + FMT_STR ‘ ‘.join([self.FMT_PAIR_STR.format(x, y) for x,y in pointS])

class TotalDepth.util.plot.SVGWriter.SVGPolyline(theXmlStream, pointS, attrs=None)

A polyline in SVG. Initialise the polyline with a stream, and a list of Coord.Pt() objects.

NOTE: The units of the points are ignored, it is up to the caller to convert them to the User Coordinate System.

See: http://www.w3.org/TR/2003/REC-SVG11-20030114/shapes.html#PolylineElement

__init__(theXmlStream, pointS, attrs=None)

Initialise the polyline with a stream, and a list of Coord.Pt() objects. NOTE: The units of the points are ignored, it is up to the caller to convert them to the User Coordinate System.

class TotalDepth.util.plot.SVGWriter.SVGPolygon(theXmlStream, pointS, attrs=None)

A polygon in SVG. Initialise the polygon with a stream, and a list of Coord.Pt() objects.

NOTE: The units of the points are ignored, it is up to the caller to convert them to the User Coordinate System.

See: http://www.w3.org/TR/2003/REC-SVG11-20030114/shapes.html#PolygonElement

__init__(theXmlStream, pointS, attrs=None)

Initialise the polygon with a stream, and a list of Coord.Pt() objects. NOTE: The units of the points are ignored, it is up to the caller to convert them to the User Coordinate System.

class TotalDepth.util.plot.SVGWriter.SVGText(theXmlStream, thePoint, theFont, theSize, attrs=None)

Text in SVG. Initialise the text with a stream, a Coord.Pt() and font as a string and size as an integer. If thePoint is None then no location will be specified (for example for use inside a <defs> element).

See: http://www.w3.org/TR/2003/REC-SVG11-20030114/text.html#TextElement

__init__(theXmlStream, thePoint, theFont, theSize, attrs=None)

Initialise the text with a stream, a Coord.Pt() and font as a string and size as an integer. If thePoint is None then no location will be specified (for example for use inside a <defs> element.

Examples

All these examples assume these imports:

import io
from TotalDepth.util import XmlWrite
from TotalDepth.util.plot import SVGWriter
from TotalDepth.util.plot import Coord

Construction

Writing to an in-memory file:

f = io.StringIO()
vp = Coord.Box(
    Coord.Dim(100, 'mm'),
    Coord.Dim(20, 'mm'),
)
with SVGWriter.SVGWriter(myF, myViewPort):
    pass
print(myF.getvalue())
# Prints:
<?xml version='1.0' encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg height="20.000mm" version="1.1" width="100.000mm" xmlns="http://www.w3.org/2000/svg"/>

Writing Objects to SVG

Writing a rectangles to a stream:

myF = io.StringIO()
vp = Coord.Box(Coord.Dim(5, 'cm'), Coord.Dim(4, 'cm'))
with SVGWriter.SVGWriter(myF, vp) as xS:
    with XmlWrite.Element(xS, 'desc'):
        xS.characters('A couple of rectangles')
    myPt = Coord.Pt(Coord.Dim(0.5, 'cm'), Coord.Dim(0.5, 'cm'))
    myBx = Coord.Box(Coord.Dim(2.0, 'cm'), Coord.Dim(1.0, 'cm'))
    with SVGWriter.SVGRect(xS, myPt, myBx):
        pass
    myPt = Coord.Pt(Coord.Dim(0.01, 'cm'), Coord.Dim(0.01, 'cm'))
    myBx = Coord.Box(Coord.Dim(4.98, 'cm'), Coord.Dim(3.98, 'cm'))
    with SVGWriter.SVGRect(xS, myPt, myBx, attrs= {'fill' : "none", 'stroke' : "blue", 'stroke-width' : ".02cm"}):
        pass
print(myF.getvalue())
# Prints:
<?xml version='1.0' encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg height="4.000cm" version="1.1" width="5.000cm" xmlns="http://www.w3.org/2000/svg">
    <desc>A couple of rectangles</desc>
    <rect height="1.000cm" width="2.000cm" x="0.500cm" y="0.500cm"/>
    <rect fill="none" height="3.980cm" stroke="blue" stroke-width=".02cm" width="4.980cm" x="0.010cm" y="0.010cm"/>
</svg>