Optimize SVG paths

Resolves #63
This commit is contained in:
Florian Festi 2019-07-19 21:49:55 +02:00
parent a38254667c
commit 0a55e9ca6b
1 changed files with 31 additions and 0 deletions

View File

@ -61,6 +61,8 @@ class SVGFile(object):
self.symbol_extends = {} self.symbol_extends = {}
def fix(self, metadata=None): def fix(self, metadata=None):
#print("Optimizations:",
self.optimize(self.tree.getroot())
self.getEnvelope() self.getEnvelope()
self.moveOrigin() self.moveOrigin()
self.addMetadata(metadata) self.addMetadata(metadata)
@ -159,6 +161,35 @@ class SVGFile(object):
self.tree.write(self.filename) self.tree.write(self.filename)
def optimize(self, element):
number = 0
if element.tag.endswith("}path"):
path = element.attrib.get("d", "")
d = r"(\-?\d+(\.\d+)?)"
while True:
old_number = number
for pattern, replacement in (
(" " + d + " " + d + r" (M|L) \1 \3 ",
r" \1 \3 "), # remove useless moves
# compress L parts of paths into V and H
(" " + d + " " + d + r" L " + d + r" \3 ",
r" \1 \3 H \5 "),
(" " + d + " " + d + r" L \1 " + d + " ",
r" \1 \3 V \5 "),
(r"H " + d + r" L \1 " + d + " ",
r"H \1 V \3 "),
(r"V " + d + " L " + d + " \1 ",
r"V \1 H \3 "),
):
path, n = re.subn(pattern, replacement, path, 2)
number += n
if number == old_number:
break
element.attrib["d"] = path
for e in element:
number += self.optimize(e)
return number
def addMetadata(self, md): def addMetadata(self, md):
txt = """ txt = """
{name} - {description} {name} - {description}