diff options
Diffstat (limited to 'Lib/fontTools/subset/svg.py')
-rw-r--r-- | Lib/fontTools/subset/svg.py | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/Lib/fontTools/subset/svg.py b/Lib/fontTools/subset/svg.py index e25fb3e6..4ed2cbd2 100644 --- a/Lib/fontTools/subset/svg.py +++ b/Lib/fontTools/subset/svg.py @@ -7,13 +7,14 @@ from typing import Dict, Iterable, Iterator, List, Optional, Set, Tuple try: from lxml import etree -except ModuleNotFoundError: +except ImportError: # lxml is required for subsetting SVG, but we prefer to delay the import error # until subset_glyphs() is called (i.e. if font to subset has an 'SVG ' table) etree = None from fontTools import ttLib from fontTools.subset.util import _add_method +from fontTools.ttLib.tables.S_V_G_ import SVGDocument __all__ = ["subset_glyphs"] @@ -192,7 +193,7 @@ def ranges(ints: Iterable[int]) -> Iterator[Tuple[int, int]]: @_add_method(ttLib.getTableClass("SVG ")) def subset_glyphs(self, s) -> bool: if etree is None: - raise ModuleNotFoundError("No module named 'lxml', required to subset SVG") + raise ImportError("No module named 'lxml', required to subset SVG") # glyph names (before subsetting) glyph_order: List[str] = s.orig_glyph_order @@ -201,10 +202,12 @@ def subset_glyphs(self, s) -> bool: # map from original to new glyph indices (after subsetting) glyph_index_map: Dict[int, int] = s.glyph_index_map - new_docs: List[Tuple[bytes, int, int]] = [] - for doc, start, end in self.docList: + new_docs: List[SVGDocument] = [] + for doc in self.docList: - glyphs = {glyph_order[i] for i in range(start, end + 1)}.intersection(s.glyphs) + glyphs = { + glyph_order[i] for i in range(doc.startGlyphID, doc.endGlyphID + 1) + }.intersection(s.glyphs) if not glyphs: # no intersection: we can drop the whole record continue @@ -212,7 +215,7 @@ def subset_glyphs(self, s) -> bool: svg = etree.fromstring( # encode because fromstring dislikes xml encoding decl if input is str. # SVG xml encoding must be utf-8 as per OT spec. - doc.encode("utf-8"), + doc.data.encode("utf-8"), parser=etree.XMLParser( # Disable libxml2 security restrictions to support very deep trees. # Without this we would get an error like this: @@ -241,7 +244,7 @@ def subset_glyphs(self, s) -> bool: new_gids = (glyph_index_map[i] for i in gids) for start, end in ranges(new_gids): - new_docs.append((new_doc, start, end)) + new_docs.append(SVGDocument(new_doc, start, end, doc.compressed)) self.docList = new_docs |