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.
Metadata
Release files for svgpdtools 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| svgpdtools-0.1.1.tar.gz | 20.7 kB | Without provenance | Details for svgpdtools-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| svgpdtools-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for svgpdtools-0.1.1-py3-none-any.whl |
Total release size: 43.3 kB
Release files / svgpdtools-0.1.1.tar.gz
| Download URL | svgpdtools-0.1.1.tar.gz |
|---|---|
| Size | 20.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e71825f7c1e82f530bc185d2bdf5a6fccc161748721900ad845e449f0fbcca52
|
|
BLAKE2b-256 checksum How to use checksums |
798ee580f604c8537d1cd74a0f130a4290b2cd70e62dba1c55adeb9e9e689c5f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.6
|
Release files / svgpdtools-0.1.1-py3-none-any.whl
| Download URL | svgpdtools-0.1.1-py3-none-any.whl |
|---|---|
| Size | 22.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
14c240f6612df4ffcc09038fa0d88ad0a87fe59a53294de33b78373bb7a09352
|
|
BLAKE2b-256 checksum How to use checksums |
07537352dd5d43c13d685abb3ebd357c72d3d0a8d6708368e7762b6a5b7fec4f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.6
|
Security for svgpdtools 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.