From 0b92b4f5530fee68432f13075a1ddc866748f9d1 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 19 Nov 2020 23:26:48 +0100 Subject: [PATCH] Cleanup Firefox SVG example. In the SVG parser: simplify the regexes; do not drop "z" codes (they were previously dropped because they are immediately followed by another code, and thus did not match `path_re`; things only worked because the SVG source was careful to explicitly move back to the start point before issuing CLOSEPOLYs) but instead include them explicitly. Don't rely on later in-place modification of `verts` propagating back to `path`, but modify it first. Make the figure background gray (as was the case for the classic style); that makes the white outline visible again (otherwise that white outline doesn't really make sense). Also just generally compress the rest of the code down. --- examples/showcase/firefox.py | 58 ++++++++++++++---------------------- 1 file changed, 23 insertions(+), 35 deletions(-) diff --git a/examples/showcase/firefox.py b/examples/showcase/firefox.py index 0f597812fa8f..713809b8292d 100644 --- a/examples/showcase/firefox.py +++ b/examples/showcase/firefox.py @@ -22,50 +22,38 @@ def svg_parse(path): 'Q': (Path.CURVE3,)*2, 'C': (Path.CURVE4,)*3, 'Z': (Path.CLOSEPOLY,)} - path_re = re.compile(r'([MLHVCSQTAZ])([^MLHVCSQTAZ]+)', re.IGNORECASE) - float_re = re.compile(r'(?:[\s,]*)([+-]?\d+(?:\.\d+)?)') vertices = [] codes = [] - last = (0, 0) - for cmd, values in path_re.findall(path): - points = [float(v) for v in float_re.findall(values)] - points = np.array(points).reshape((len(points)//2, 2)) + cmd_values = re.split("([A-Za-z])", path)[1:] # Split over commands. + for cmd, values in zip(cmd_values[::2], cmd_values[1::2]): + # Numbers are separated either by commas, or by +/- signs (but not at + # the beginning of the string). + points = ([*map(float, re.split(",|(?