Precompile optimizations REs and fix performance issue
This commit is contained in:
parent
39bd912c90
commit
9a17e3efa9
|
@ -169,27 +169,29 @@ class SVGFile(object):
|
||||||
|
|
||||||
self.tree.write(self.filename)
|
self.tree.write(self.filename)
|
||||||
|
|
||||||
|
d = r"(\-?\d+(\.\d+)?)"
|
||||||
|
optimize_patterns = [
|
||||||
|
(re.compile(" " + d + " " + d + r" (M|L) \1 \3 "),
|
||||||
|
r" \1 \3 "), # remove useless moves
|
||||||
|
# compress L parts of paths into V and H
|
||||||
|
(re.compile(" " + d + " " + d + r" L " + d + r" \3 "),
|
||||||
|
r" \1 \3 H \5 "),
|
||||||
|
(re.compile(" " + d + " " + d + r" L \1 " + d + " "),
|
||||||
|
r" \1 \3 V \5 "),
|
||||||
|
(re.compile(r"H " + d + r" L \1 " + d + " "),
|
||||||
|
r"H \1 V \3 "),
|
||||||
|
(re.compile(r"V " + d + " L " + d + " \1 "),
|
||||||
|
r"V \1 H \3 "),
|
||||||
|
]
|
||||||
|
|
||||||
def optimize(self, element):
|
def optimize(self, element):
|
||||||
number = 0
|
number = 0
|
||||||
if element.tag.endswith("}path"):
|
if element.tag.endswith("}path"):
|
||||||
path = element.attrib.get("d", "")
|
path = element.attrib.get("d", "")
|
||||||
d = r"(\-?\d+(\.\d+)?)"
|
|
||||||
while True:
|
while True:
|
||||||
old_number = number
|
old_number = number
|
||||||
for pattern, replacement in (
|
for pattern, replacement in self.optimize_patterns:
|
||||||
(" " + d + " " + d + r" (M|L) \1 \3 ",
|
path, n = pattern.subn(replacement, path)
|
||||||
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
|
number += n
|
||||||
if number == old_number:
|
if number == old_number:
|
||||||
break
|
break
|
||||||
|
|
Loading…
Reference in New Issue