only rename glyph element ids when clash actually occurs
This commit is contained in:
parent
155ff60797
commit
247fa84b98
@ -121,10 +121,11 @@ def subset_elements(el: etree.Element, retained_ids: Set[str]) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def remap_glyph_ids(
|
def remap_glyph_ids(
|
||||||
elements: Dict[str, etree.Element], glyph_index_map: Dict[int, int]
|
svg: etree.Element, glyph_index_map: Dict[int, int]
|
||||||
) -> Dict[str, str]:
|
) -> Dict[str, str]:
|
||||||
# Given {old_gid: new_gid} map, rename all elements containing id="glyph{gid}"
|
# Given {old_gid: new_gid} map, rename all elements containing id="glyph{gid}"
|
||||||
# special attributes
|
# special attributes
|
||||||
|
elements = group_elements_by_id(svg)
|
||||||
id_map = {}
|
id_map = {}
|
||||||
for el_id, el in elements.items():
|
for el_id, el in elements.items():
|
||||||
m = GID_RE.match(el_id)
|
m = GID_RE.match(el_id)
|
||||||
@ -230,7 +231,7 @@ def subset_glyphs(self, s) -> bool:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
if not s.options.retain_gids:
|
if not s.options.retain_gids:
|
||||||
id_map = remap_glyph_ids(elements, glyph_index_map)
|
id_map = remap_glyph_ids(svg, glyph_index_map)
|
||||||
update_glyph_href_links(svg, id_map)
|
update_glyph_href_links(svg, id_map)
|
||||||
|
|
||||||
new_doc = etree.tostring(svg, pretty_print=s.options.pretty_svg).decode("utf-8")
|
new_doc = etree.tostring(svg, pretty_print=s.options.pretty_svg).decode("utf-8")
|
||||||
|
@ -168,12 +168,15 @@ COMPLEX_SVG = """\
|
|||||||
</g>
|
</g>
|
||||||
<g id="group1">
|
<g id="group1">
|
||||||
<g id="glyph7">
|
<g id="glyph7">
|
||||||
|
<path id="p2" d="M4,4"/>
|
||||||
|
</g>
|
||||||
|
<g id=".glyph7">
|
||||||
<path d="M4,4"/>
|
<path d="M4,4"/>
|
||||||
</g>
|
</g>
|
||||||
<g id="glyph8">
|
<g id="glyph8">
|
||||||
<g id=".glyph8">
|
<g id=".glyph8">
|
||||||
<path id="p2" d="M5,5"/>
|
<path id="p3" d="M5,5"/>
|
||||||
<path id="p3" d="M6,6"/>
|
<path id="M6,6"/>
|
||||||
</g>
|
</g>
|
||||||
<path d="M7,7"/>
|
<path d="M7,7"/>
|
||||||
</g>
|
</g>
|
||||||
@ -304,27 +307,32 @@ COMPLEX_SVG = """\
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
# 'glyph9' uses a path 'p2' defined inside 'glyph8', the latter is excluded
|
# 'glyph9' uses a path 'p2' defined inside 'glyph7', the latter is excluded
|
||||||
# from our subset, as such its id is renamed with a '.' prefix; but (edge
|
# from our subset, thus gets renamed '.glyph7'; an unrelated element with
|
||||||
# case!) there already is an id=".glyph8" in the doc (for whatever reason,
|
# same id=".glyph7" doesn't clash because it was dropped.
|
||||||
# it's still a valid id), so we append a .{digit} suffix to disambiguate.
|
# Similarly 'glyph10' uses path 'p3' defined inside 'glyph8', also excluded
|
||||||
"7,9",
|
# from subset and prefixed with '.'. But since an id=".glyph8" is already
|
||||||
|
# used in the doc, we append a .{digit} suffix to disambiguate.
|
||||||
|
"9,10",
|
||||||
_lines(
|
_lines(
|
||||||
"""\
|
"""\
|
||||||
<svgDoc endGlyphID="2" startGlyphID="1">
|
<svgDoc endGlyphID="2" startGlyphID="1">
|
||||||
<![CDATA[<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
<![CDATA[<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||||
<g id="group1">
|
<g id="group1">
|
||||||
<g id="glyph1">
|
<g id=".glyph7">
|
||||||
<path d="M4,4"/>
|
<path id="p2" d="M4,4"/>
|
||||||
</g>
|
</g>
|
||||||
<g id=".glyph8.1">
|
<g id=".glyph8.1">
|
||||||
<g id=".glyph8">
|
<g id=".glyph8">
|
||||||
<path id="p2" d="M5,5"/>
|
<path id="p3" d="M5,5"/>
|
||||||
</g>
|
</g>
|
||||||
</g>
|
</g>
|
||||||
<g id="glyph2">
|
<g id="glyph1">
|
||||||
<use xlink:href="#p2"/>
|
<use xlink:href="#p2"/>
|
||||||
</g>
|
</g>
|
||||||
|
<g id="glyph2">
|
||||||
|
<use xlink:href="#p3"/>
|
||||||
|
</g>
|
||||||
</g>
|
</g>
|
||||||
</svg>
|
</svg>
|
||||||
]]>
|
]]>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user