18 Commits

Author SHA1 Message Date
Cosimo Lupo
dbe60eae80 [subset/svg] use broader ImportError instead of ModuleNotFoundError
Fixes https://github.com/fonttools/fonttools/issues/2696
2022-07-18 11:32:44 +02:00
Cosimo Lupo
be623e257f Clean up svgDoc compression
This is based on bungeman's https://github.com/fonttools/fonttools/pull/2627

Previously, an entire `SVG ` table would be marked as compressed if any
of the decoded SVG documents in it were compressed. Then on encoding all
SVG documents would be considered for compression. The XML format had no
means to indicate if compression was desired.

Instead, mark each svgDoc with its compression status. When decoding
mark the svgDoc as compressed if the data was compressed. When encoding
try to compress the svgDoc if it is marked as compressed. In the XML
format the data itself is always uncompressed, but allow an optional
`compressed` boolean attribute (defaults to false) to indicate the
svgDoc should be compressed when encoded.

We also try to make sure that older code that relies on docList containing
sequences of three items (doc, startGID, endGID) will continue to work
without modification.
2022-06-09 15:15:05 +01:00
Cosimo Lupo
315705a58f [subset] fix subsetting OT-SVG when glyph id attribute is on the root <svg> element
Fixes https://github.com/fonttools/fonttools/issues/2548
2022-03-18 13:18:53 +00:00
Cosimo Lupo
247fa84b98 only rename glyph element ids when clash actually occurs 2021-11-16 14:28:14 +00:00
Cosimo Lupo
155ff60797 use any([<list comprehension>]) to avoid short-circuit function with side effect 2021-11-16 14:06:24 +00:00
Cosimo Lupo
07455790b1 simplify ranges() generator 2021-11-16 14:06:24 +00:00
Cosimo Lupo
66ec30c073 subset/svg: decorate xpath with lru_cache 2021-11-16 14:06:24 +00:00
Cosimo Lupo
d45da5df4c factor out method to resolve href targets as per review 2021-11-16 12:27:12 +00:00
Cosimo Lupo
27e4052380 encode bytes before fromstring; decode to str after tostring
when decompiled from binary, the SVG.docList contains (unicode) strings, decoded as UTF-8. lxml fromstring accepts either bytes or str, but when given str with the xml header declaring an explicit encoding, it rejects them (since the header is lying). So we encode to bytes before calling fromstring in case the SVG contains an explicit encoding (UTF-8 is the only one allowed anyway). When serializing to XML with tostring, we similarly decode to str as UTF-8. Not only to match SVG decompile (which gives us str), but if we didn't do that, then attempting to dump to XML would fail, because XMLWriter.writecdata expects str, not bytes.
2021-11-15 19:06:38 +00:00
Cosimo Lupo
1d19cc6e23 add comment for reason not to short-circuit with any() 2021-11-15 18:17:19 +00:00
Cosimo Lupo
12ed8b752c simplify and do el.attrib.get('id') as per review 2021-11-15 17:46:42 +00:00
Cosimo Lupo
92470ea361 rename parmeter ids => retained_ids for clarity as per review 2021-11-15 17:41:42 +00:00
Cosimo Lupo
a7216ae766 subset: drop svg doc if subsetted to empty 2021-11-15 17:08:55 +00:00
Cosimo Lupo
d1f2481fa0 fix invalid escape sequence in GID_RE pattern 2021-11-15 17:08:55 +00:00
Cosimo Lupo
a4df567718 subset/svg: support resolving cross-references and pruning elements
With this I can finally follow xlink:href and url(#...) sort of
references within the SVG doc and subset the elements accordingly so
that only those that are reachable from the initial set of glyph
elements are kept.
2021-11-15 16:07:18 +00:00
Cosimo Lupo
0dc2be3e78 require lxml to subset SVG
support for namespaces and xpath is insufficient in built-in ElementTree; supporting both lxml and ElementTree is too complicated, let's simply require lxml to be able to subset SVG for now
2021-11-15 16:07:18 +00:00
Cosimo Lupo
d44af9f2d7 basic subset.svg impl that simply remaps id='glyphXXX'
this drops svg document records when they no longer intersect the subset. It keeps them in their entirety (for now) when they still intersect the subset, only renaming all the id='glyphXXX' to point to the new glyph indices after subsetting. Unused, unreferenced elements are not pruned yet.
2021-11-15 16:07:18 +00:00
Cosimo Lupo
bd47bdae97 subset: don't drop SVG table, add WIP subset/svg.py stub
simply  pass-through for now
2021-11-15 16:07:18 +00:00