svgload Package

path_parser Module

This is a New BSD License. http://www.opensource.org/licenses/bsd-license.php

Copyright (c) 2008-2009, Jonathan Hartley (tartley@tartley.com) Copyright (c) 2012, Christian Fobel (christian@fobel.net) All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of Jonathan Hartley nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

class svg_model.svgload.path_parser.LoopTracer[source]

Bases: object

Methods

get_point(command)
onBadCommand(action)
onClose(command)
onHorizontalMove(command)
onLine(command)
onMove(command)
onVerticalMove(command)
to_loops(commands) commands : list of tuples, as output from to_tuples() method, eg:
get_point(command)[source]
onBadCommand(action)[source]
onClose(command)[source]
onHorizontalMove(command)[source]
onLine(command)[source]
onMove(command)[source]
onVerticalMove(command)[source]
to_loops(commands)[source]
commands : list of tuples, as output from to_tuples() method, eg:
[(‘M’, 1, 2), (‘L’, 3, 4), (‘L’, 5, 6), (‘z’)]

Interprets the command characters at the start of each tuple to return a list of loops, where each loop is a closed list of verts, and each vert is a pair of ints or floats, eg:

[[1, 2, 3, 4, 5, 6]]

Note that the final point of each loop is eliminated if it is equal to the first. SVG defines commands:

M x,y: move, start a new loop L x,y: line, draw boundary H x: move horizontal V y: move vertical Z: close current loop - join to start point

Lower-case command letters (eg ‘m’) indicate a relative offset. See http://www.w3.org/TR/SVG11/paths.html

exception svg_model.svgload.path_parser.ParseError[source]

Bases: exceptions.Exception

class svg_model.svgload.path_parser.PathDataParser[source]

Bases: object

Methods

get_char(allowed)
get_chars(allowed)
get_number()
to_tuples(data) path_data : string, from an svg path tag’s ‘d’ attribute, eg:
get_char(allowed)[source]
get_chars(allowed)[source]
get_number()[source]
to_tuples(data)[source]
path_data : string, from an svg path tag’s ‘d’ attribute, eg:
‘M 46,74 L 35,12 l 53,-13 z’
returns the same data collected in a list of tuples, eg:
[ (‘M’, 46, 74), (‘L’, 35, 12), (‘l’, 53, -13), (‘z’) ],

The input data may have floats instead of ints, this will be reflected in the output. The input may have its whitespace stripped out, or its commas replaced by whitespace.

class svg_model.svgload.path_parser.PathParser[source]

Bases: object

Methods

get_id(attributes)
parse(tag) returns (id, path)
parse_color(color) color : string, eg: ‘#rrggbb’ or ‘none’
parse_style(style) style : string, eg:
get_id(attributes)[source]
next_id = 1
parse(tag)[source]

returns (id, path) where: ‘id’ is the path tag’s id attribute

‘path’ is a populated instance of SvgPath
>>> from lxml import etree
>>> from lxml.builder import E
>>> path_tag = etree.XML("""
...     <path id="path0"
...         style="fill:#0000ff;stroke:#000000;stroke-width:0.10000000000000001;stroke-miterlimit:4;stroke-dasharray:none"
...         d="M 525.93385,261.47322 L 525.933 85,269.65826 L 534.07239,269.65826 L 534.07239,261.47322 L 525.93385,261.47322" />
... """)
>>> path_parser = PathParser()
>>> id, svg_path = path_parser.parse(path_tag)
>>> id
'path0'
>>> svg_path.color
(0, 0, 255)
>>> len(svg_path.loops)
1
>>> svg_path.loops[0].verts
[(534.07239, 261.47322), (534.07239, 269.65826), (525.933, 85), (525.93385, 261.47322)]

Note that only absolute commands (i.e., uppercase) are currently supported. For example: paths will throw a ParseError exception. For example:

>>> path_tag = E.path(id="path0", d="M 636.0331,256.9345 l 636.0331,256.9345")
>>> print etree.tostring(path_tag)
<path d="M 636.0331,256.9345 l 636.0331,256.9345" id="path0"/>
>>> path_parser.parse(path_tag)
Traceback (most recent call last):
...
ParseError: unsupported svg path command: l
>>>
parse_color(color)[source]

color : string, eg: ‘#rrggbb’ or ‘none’ (where rr, gg, bb are hex digits from 00 to ff) returns a triple of unsigned bytes, eg: (0, 128, 255)

parse_style(style)[source]
style : string, eg:
fill:#ff2a2a;fill-rule:evenodd;stroke:none;stroke-width:1px; stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1

returns color as a triple of unsigned bytes: (r, g, b), or None

svg_parser Module

This is a New BSD License. http://www.opensource.org/licenses/bsd-license.php

Copyright (c) 2008-2009, Jonathan Hartley (tartley@tartley.com) Copyright (c) 2012, Christian Fobel (christian@fobel.net) All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of Jonathan Hartley nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

class svg_model.svgload.svg_parser.Svg[source]

Bases: object

Maintains an ordered list of paths, each one corresponding to a path tag from an SVG file. Creates a pylget Batch containing all these paths, for rendering as a single OpenGL GL_TRIANGLES indexed vert primitive.

Methods

add_path(id, path)
add_to_batch(batch) Adds paths to the given batch object.
all_verts()
get_boundary()
get_bounding_box()
add_path(id, path)[source]
add_to_batch(batch)[source]

Adds paths to the given batch object. They are all added as GL_TRIANGLES, so the batch will aggregate them all into a single OpenGL primitive.

all_verts()[source]
get_boundary()[source]
get_bounding_box()[source]
exception svg_model.svgload.svg_parser.SvgParseError[source]

Bases: exceptions.Exception

class svg_model.svgload.svg_parser.SvgParser[source]

Bases: object

parse(filename) returns an Svg object, populated from the <path> tags in the file.

Methods

parse(xml_root[, on_error]) Parse all <path> elements from xml_root.
parse_file(filename[, on_error])
parse(xml_root, on_error=None)[source]

Parse all <path> elements from xml_root.

Optional on_error arg specifies a callback function to be run when an error occurs during parsing. The specified on_error function must accept 3 arguments:

<svg filename>, <path_tag>, <error message>

An example on_error handler is provided as svg_load.svg_parser.parse_warning(), where all SvgParseErrors are converted to warning messages. See usage below: >>> import re >>> svg_parser = SvgParser() >>> path_tag = etree.XML(“”” ... <path ... xmlns=”http://www.w3.org/2000/svg” ... xmlns:dc=”http://purl.org/dc/elements/1.1/” ... xmlns:cc=”http://creativecommons.org/ns#” ... xmlns:rdf=”http://www.w3.org/1999/02/22-rdf-syntax-ns#” ... xmlns:svg=”http://www.w3.org/2000/svg” ... xmlns:sodipodi=”http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd” ... xmlns:inkscape=”http://www.inkscape.org/namespaces/inkscape” ... id=”path13231” ... d=”M8 4 l-4,4” ... linecap=”square” ... stroke=”#000000” ... stroke-width=”0.25” ... />”“”) >>> with warnings.catch_warnings(record=True) as w: ... svg = svg_parser.parse(path_tag, on_error=parse_warning) >>> print w[-1].category <type ‘exceptions.RuntimeWarning’> >>> match = re.search(r’^Error parsing None:d+, unsupported svg path command: l’, str(w[-1].message)) >>> print match is None False >>> path_tag = etree.XML(“”” ... <path ... xmlns=”http://www.w3.org/2000/svg” xmlns:inkscape=”http://www.inkscape.org/namespaces/inkscape” ... xmlns:dc=”http://purl.org/dc/elements/1.1/” xmlns:cc=”http://creativecommons.org/ns#” ... xmlns:rdf=”http://www.w3.org/1999/02/22-rdf-syntax-ns#” ... xmlns:svg=”http://www.w3.org/2000/svg” ... xmlns:sodipodi=”http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd” ... style=”fill:#0000ff;stroke:#ff0000;stroke-width:0.10000000000000001;stroke-miterlimit:4;stroke-dasharray:none” ... id=”path18327” ... d=”M 636.0331,256.9345 L 636.0331,256.9345” ... inkscape:connector-curvature=”0”/>”“”) >>> with warnings.catch_warnings(record=True) as w: ... svg = svg_parser.parse(path_tag, on_error=parse_warning) >>> print w[-1].category <type ‘exceptions.RuntimeWarning’> >>> match = re.search(r’^Error parsing None:d+, loop needs 3 or more verts’, str(w[-1].message)) >>> print match is None False

parse_file(filename, on_error=None)[source]
svg_model.svgload.svg_parser.parse_warning(*args)[source]