Skip to main content

Manipulate pathdata of SVG path-element.

Project description

SVG PathData Tools

The svgpdtools module defines utilities to manipulate pathdata (value of d attribute) of a SVG path-element. Also there is a command line interface.

Install

% pip install svgpdtools

Examples

import xml.etree.ElementTree as ET
import svgpdtools as PD

def apply_translates():
    PD.precision(3)
    tree = ET.parse('infile.svg')
    t_parent = PD.Transform.translate(25, 25)
    for path in tree.findall('.//{*}path'):
        pd = PD.pathdata_from_string(path.get('d'))
        t_child = path.get('transform', '')
        if t_child:
            path.set('transform', '')
        pd.transform(t_parent * PD.transform_from_string(t_child))
        path.set('d', str(pd))
    ET.register_namespace('', 'http://www.w3.org/2000/svg')
    tree.write('outfile.svg', encoding='utf-8')

Below is using CLI to get almost equivalent output as the above code.

With the CLI to handle SVG data, this module uses ‘xml.sax.make_parser’ and ‘xml.sax.saxutils.XMLGenerator’. So you should not parse untrusted data.

% svgpdtools normalize --collapse-transform-attribute -f infile.svg |\
      svgpdtools transform -p 3 "translate(25,25)" > outfile.svg

Module contents

svgpdtools.precision(value: int) -> None

Set the max length of fractional part of a real number. The default value is 6. Each coordinate of the pathdata is calculated as a Python float value, and formatted with that length when shown.

svgpdtools.pathdata_from_string(src: str) -> PathData

Convert a pathdata string to a svgpdtools.PathData object. A pathdata string is a value of the d property of the SVG-path element.

svgpdtools.transform_from_string(src: str) -> Transform

Convert a string representation of SVG transfom functions to a svgpdtools.Transform object. The syntax of transform functions are the same as the SVG transform attribute.

class svgpdtools.PathData

UserList of svgpdtools.Command objects. This class has some methods which are major task of the svgpdtools module. transform(), absolutize(), and normalize(), those methods are destructive operations.

class svgpdtools.Transform

This class represents a transform matrix, which is the same as the SVG's presentation attribute transform. Transform functions are provided as static functions. Each function's syntax is also same as the SVG's transform functions.

  • Transform.matrix(a, b, c, d, e, f)
  • Transform.translate(dx, dy)
  • Transform.scale(sx, sy)
  • Transform.rotate(deg, cx, cy)
  • Transform.skewX(deg)
  • Transform.skewY(deg)

For example, the current transformation matrix (CTM) is represent multiple functions in SVG:

<g transform="translate(10,10) rotate(45) translate(-10,-10)">
  ...
</g>

The above transformation of the SVGGElement is equivalent to the following:

import svgpdtools.Transform as T
T1, T2, T3 = T.translate(10,10), T.rotate(45), T.translate(-10,-10)
ctm = T1 * T2 * T3
# or
ctm = T.concat([T1, T2, T3])
# or
ctm = T1.concatenated(T2, T3)

svgpdtools.Transform() is an identity matrix.

Future considerations

  • Change the PathData object to a immutable object.
  • Implement editing functions that common bezier path tools have.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

svgpdtools-0.1.1.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

svgpdtools-0.1.1-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file svgpdtools-0.1.1.tar.gz.

File metadata

  • Download URL: svgpdtools-0.1.1.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for svgpdtools-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e71825f7c1e82f530bc185d2bdf5a6fccc161748721900ad845e449f0fbcca52
MD5 8c7142406b0e8e37f59a5022c401ac63
BLAKE2b-256 798ee580f604c8537d1cd74a0f130a4290b2cd70e62dba1c55adeb9e9e689c5f

See more details on using hashes here.

File details

Details for the file svgpdtools-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: svgpdtools-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for svgpdtools-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 14c240f6612df4ffcc09038fa0d88ad0a87fe59a53294de33b78373bb7a09352
MD5 07c5c093bed15db3bdd93f63af1ba423
BLAKE2b-256 07537352dd5d43c13d685abb3ebd357c72d3d0a8d6708368e7762b6a5b7fec4f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page