From 44a79b1b8966fe5d618c363203ac606268c3390a Mon Sep 17 00:00:00 2001 From: Alexandre Duret-Lutz Date: Sat, 19 Oct 2019 15:40:00 +0200 Subject: [PATCH] work around recent GraphViz bug in SVG scaling See https://gitlab.com/graphviz/graphviz/issues/1605. * python/spot/aux.py (str_to_svg): Invert the scale parameters if they are both greater than one. --- python/spot/aux.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/python/spot/aux.py b/python/spot/aux.py index 53d787e89..cd56d212b 100644 --- a/python/spot/aux.py +++ b/python/spot/aux.py @@ -27,6 +27,7 @@ import sys import os import errno import contextlib +import re def extend(*classes): @@ -44,6 +45,21 @@ def extend(*classes): return wrap +# Work around a bug introduced in GraphViz 2.42.x, where the scale +# parameter is inverted. https://gitlab.com/graphviz/graphviz/issues/1605 +# In our case, the scale parameters should both be <= 1, so we can +# detect when that is not the case. +svgscale_regex = re.compile('transform="scale\(([\d.]+) ([\d.]+)\) rotate') + +def _gvfix(matchobj): + xs = float(matchobj.group(1)) + ys = float(matchobj.group(2)) + if xs >= 1 and ys >= 1: + xs = 1/xs + ys = 1/ys + return 'transform="scale({} {}) rotate'.format(xs, ys) + + # Add a small LRU cache so that when we display automata into a # interactive widget, we avoid some repeated calls to dot for # identical inputs. @@ -70,7 +86,8 @@ def str_to_svg(str): ret = dot.wait() if ret: raise subprocess.CalledProcessError(ret, 'dot') - return stdout.decode('utf-8') + out = stdout.decode('utf-8') + return svgscale_regex.sub(_gvfix, out) def ostream_to_svg(ostr):