move stuff to fontTools.svgLib.path sub-package
in case later on we want to add things to svgLib which don't have to do with paths specifically
This commit is contained in:
parent
ecf781cfce
commit
de59719db4
@ -1,58 +1,6 @@
|
|||||||
from __future__ import (
|
from __future__ import print_function, division, absolute_import
|
||||||
print_function, division, absolute_import, unicode_literals)
|
|
||||||
from fontTools.misc.py23 import *
|
from fontTools.misc.py23 import *
|
||||||
|
|
||||||
from fontTools.pens.transformPen import TransformPen
|
from .path import SVGPath, parse_path
|
||||||
from .parser import parse_path
|
|
||||||
|
|
||||||
try:
|
__all__ = ["SVGPath", "parse_path"]
|
||||||
from xml.etree import cElementTree as ElementTree # python 2
|
|
||||||
except ImportError: # pragma nocover
|
|
||||||
from xml.etree import ElementTree # python 3
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = [tostr(s) for s in ("SVGPath", "parse_path")]
|
|
||||||
|
|
||||||
|
|
||||||
class SVGPath(object):
|
|
||||||
""" Parse SVG ``path`` elements from a file or string, and draw them
|
|
||||||
onto a glyph object that supports the FontTools Pen protocol.
|
|
||||||
|
|
||||||
For example, reading from an SVG file and drawing to a Defcon Glyph:
|
|
||||||
|
|
||||||
import defcon
|
|
||||||
glyph = defcon.Glyph()
|
|
||||||
pen = glyph.getPen()
|
|
||||||
svg = SVGPath("path/to/a.svg")
|
|
||||||
svg.draw(pen)
|
|
||||||
|
|
||||||
Or reading from a string containing SVG data, using the alternative
|
|
||||||
'fromstring' (a class method):
|
|
||||||
|
|
||||||
data = '<?xml version="1.0" ...'
|
|
||||||
svg = SVGPath.fromstring(data)
|
|
||||||
svg.draw(pen)
|
|
||||||
|
|
||||||
Both constructors can optionally take a 'transform' matrix (6-float
|
|
||||||
tuple, or a FontTools Transform object) to modify the draw output.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, filename=None, transform=None):
|
|
||||||
if filename is None:
|
|
||||||
self.root = ElementTree.ElementTree()
|
|
||||||
else:
|
|
||||||
tree = ElementTree.parse(filename)
|
|
||||||
self.root = tree.getroot()
|
|
||||||
self.transform = transform
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def fromstring(cls, data, transform=None):
|
|
||||||
self = cls(transform=transform)
|
|
||||||
self.root = ElementTree.fromstring(data)
|
|
||||||
return self
|
|
||||||
|
|
||||||
def draw(self, pen):
|
|
||||||
if self.transform:
|
|
||||||
pen = TransformPen(pen, self.transform)
|
|
||||||
for el in self.root.findall(".//{http://www.w3.org/2000/svg}path[@d]"):
|
|
||||||
parse_path(el.get("d"), pen)
|
|
||||||
|
58
Lib/fontTools/svgLib/path/__init__.py
Normal file
58
Lib/fontTools/svgLib/path/__init__.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
from __future__ import (
|
||||||
|
print_function, division, absolute_import, unicode_literals)
|
||||||
|
from fontTools.misc.py23 import *
|
||||||
|
|
||||||
|
from fontTools.pens.transformPen import TransformPen
|
||||||
|
from .parser import parse_path
|
||||||
|
|
||||||
|
try:
|
||||||
|
from xml.etree import cElementTree as ElementTree # python 2
|
||||||
|
except ImportError: # pragma nocover
|
||||||
|
from xml.etree import ElementTree # python 3
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = [tostr(s) for s in ("SVGPath", "parse_path")]
|
||||||
|
|
||||||
|
|
||||||
|
class SVGPath(object):
|
||||||
|
""" Parse SVG ``path`` elements from a file or string, and draw them
|
||||||
|
onto a glyph object that supports the FontTools Pen protocol.
|
||||||
|
|
||||||
|
For example, reading from an SVG file and drawing to a Defcon Glyph:
|
||||||
|
|
||||||
|
import defcon
|
||||||
|
glyph = defcon.Glyph()
|
||||||
|
pen = glyph.getPen()
|
||||||
|
svg = SVGPath("path/to/a.svg")
|
||||||
|
svg.draw(pen)
|
||||||
|
|
||||||
|
Or reading from a string containing SVG data, using the alternative
|
||||||
|
'fromstring' (a class method):
|
||||||
|
|
||||||
|
data = '<?xml version="1.0" ...'
|
||||||
|
svg = SVGPath.fromstring(data)
|
||||||
|
svg.draw(pen)
|
||||||
|
|
||||||
|
Both constructors can optionally take a 'transform' matrix (6-float
|
||||||
|
tuple, or a FontTools Transform object) to modify the draw output.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, filename=None, transform=None):
|
||||||
|
if filename is None:
|
||||||
|
self.root = ElementTree.ElementTree()
|
||||||
|
else:
|
||||||
|
tree = ElementTree.parse(filename)
|
||||||
|
self.root = tree.getroot()
|
||||||
|
self.transform = transform
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def fromstring(cls, data, transform=None):
|
||||||
|
self = cls(transform=transform)
|
||||||
|
self.root = ElementTree.fromstring(data)
|
||||||
|
return self
|
||||||
|
|
||||||
|
def draw(self, pen):
|
||||||
|
if self.transform:
|
||||||
|
pen = TransformPen(pen, self.transform)
|
||||||
|
for el in self.root.findall(".//{http://www.w3.org/2000/svg}path[@d]"):
|
||||||
|
parse_path(el.get("d"), pen)
|
Loading…
x
Reference in New Issue
Block a user