svgdigitizer.svg

Provides access to the elements of an SVG tracing a plot for the digitiver.

EXAMPLES:

A simple SVG consisting of a path grouped with a label:

>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...   <g>
...     <path d="M 0 100 L 100 0" />
...     <text x="0" y="0">curve: 0</text>
...   </g>
... </svg>'''))

We can access the path by its label:

>>> paths = svg.get_labeled_paths("curve")

We extract the single group with the label “curve” and the single path in this group:

>>> curve = paths[0][0]
>>> curve
Path "curve: 0"

We can query the path for the endpoints of its segments:

>>> curve.points
[(0.0, 100.0), (100.0, 0.0)]

We can recover the specification of that segment as it is encoded in the SVG:

>>> curve.path
Path(Line(start=100j, end=(100+0j)))
class svgdigitizer.svg.LabeledPath(path, label)

A <path> element in an SVG such as a trace of a plot with a text label.

EXAMPLES:

>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...   <g>
...     <path d="M 0 100 L 100 0" />
...     <text x="0" y="0">curve: 0</text>
...   </g>
... </svg>'''))
>>> svg.get_labeled_paths()[0][0]
Path "curve: 0"
property far

Return the end point of this path that is furthest away from the label.

EXAMPLES:

>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...   <g>
...     <path d="M 0 100 L 100 0" />
...     <text x="0" y="100">curve: 0</text>
...   </g>
... </svg>'''))
>>> path = svg.get_labeled_paths()[0][0]
>>> path.far
(100.0, 0.0)
property path

Return the path transformed to the global SVG coordinate system, i.e., with all transform attributes resolved.

Note that we do not resolve CSS style transformations yet.

EXAMPLES:

>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...   <g>
...     <path d="M 0 100 L 100 0" />
...     <text x="0" y="0">curve: 0</text>
...   </g>
... </svg>'''))
>>> svg.get_labeled_paths()[0][0].path
Path(Line(start=100j, end=(100+0j)))

TESTS:

Transformations on the path are resolved:

>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...   <g>
...     <path d="M 0 100 L 100 0" transform="translate(100 200)" />
...     <text x="0" y="0">curve: 0</text>
...   </g>
... </svg>'''))
>>> svg.get_labeled_paths()[0][0].path
Path(Line(start=(100+300j), end=(200+200j)))

Transformations on the containing group are resolved:

>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...   <g transform="translate(-100 -200)">
...     <path d="M 0 100 L 100 0" transform="translate(100 200)" />
...     <text x="0" y="0">curve: 0</text>
...   </g>
... </svg>'''))
>>> svg.get_labeled_paths()[0][0].path
Path(Line(start=100j, end=(100+0j)))
classmethod path_points(path)

Return the points defining this path.

This returns the raw points in the d attribute, ignoring the commands that connect these points, i.e., ignoring whether these points are connected by M commands that do not actually draw anything, or any kind of visible curve.

property points

Return the points defining this path.

This returns the raw points in the d attribute, ignoring the commands that connect these points, i.e., ignoring whether these points are connected by M commands that do not actually draw anything, or any kind of visible curve.

class svgdigitizer.svg.LabeledPaths(label, paths, match)

A collection of paths associated to a single label.

EXAMPLES:

>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...   <g>
...     <path d="M 0 100 L 100 0" />
...     <text x="0" y="0">curve: 0</text>
...   </g>
... </svg>'''))
>>> svg.get_labeled_paths()
[[Path "curve: 0"]]
property label

Return the label associated to the paths.

EXAMPLES:

>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...   <g>
...     <path d="M 0 100 L 100 0" />
...     <text x="0" y="0">curve: 0</text>
...   </g>
... </svg>'''))
>>> svg.get_labeled_paths()[0].label
<text>curve: 0</text>
class svgdigitizer.svg.SVG(svg)

An Scalable Vector Graphics (SVG) document.

Provides access to the objects in an SVG while hiding details such as transformation attributes.

EXAMPLES:

An SVG can be created from a string or from a (file) stream:

>>> svg = SVG(r'''
... <svg>
...     <!-- an empty SVG -->
... </svg>''')
>>> svg
SVG('<?xml version="1.0" ?><svg>\n    <!-- an empty SVG -->\n</svg>')
>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...     <!-- an empty SVG -->
... </svg>'''))
>>> svg
SVG('<?xml version="1.0" ?><svg>\n    <!-- an empty SVG -->\n</svg>')
get_labeled_paths(pattern='')

Return all paths with their corresponding <text> label if it matches pattern.

EXAMPLES:

>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...   <g>
...     <path d="M 0 100 L 100 0" />
...     <text x="0" y="0">curve: 0</text>
...   </g>
... </svg>'''))
>>> svg.get_labeled_paths("curve")
[[Path "curve: 0"]]
>>> svg.get_labeled_paths("text")
[]
>>> svg.get_labeled_paths()
[[Path "curve: 0"]]
get_texts(pattern='')

Return all <text> elements that match pattern.

EXAMPLES:

>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...   <g>
...     <path d="M 0 100 L 100 0" />
...     <text x="0" y="0">curve: 0</text>
...   </g>
... </svg>'''))
>>> svg.get_texts()
[<text>curve: 0</text>]

Named matches are directly available as attributes on the returned texts:

>>> curves = svg.get_texts("curve: (?P<name>.*)")
>>> curves[0].name
'0'
classmethod transform(element)

Return a transformed version of element with all transform attributes applied.

EXAMPLES:

Transformations can be applied to text elements:

>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...   <g transform="translate(100, 10)">
...     <text x="0" y="0" transform="translate(100, 10)">curve: 0</text>
...   </g>
... </svg>'''))
>>> transformed = svg.transform(svg.svg.getElementsByTagName("text")[0])
>>> transformed.toxml()
'<text x="200.0" y="20.0">curve: 0</text>'

Transformations can be applied to paths:

>>> svg = SVG(StringIO(r'''
... <svg>
...   <g transform="translate(100, 10)">
...     <path d="M 0 0 L 1 1" transform="translate(100, 10)" />
...   </g>
... </svg>'''))
>>> svg.transform(svg.svg.getElementsByTagName("path")[0])
Path(Line(start=(200+20j), end=(201+21j)))
class svgdigitizer.svg.Text(label, match)

A <text> element in an SVG such as a label for a path.

EXAMPLES:

>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...   <text x="0" y="0">curve: 0</text>
... </svg>'''))
>>> svg.get_texts()
[<text>curve: 0</text>]

The coordinates of the text in the SVG coordinate system are exposed as .x and .y:

>>> from io import StringIO
>>> svg = SVG(StringIO(r'''
... <svg>
...   <text x="0" y="0" transform="translate(10,100)">curve: 0</text>
... </svg>'''))
>>> text = svg.get_texts()[0]
>>> text.x
10.0
>>> text.y
100.0