Skip to content

Commit

Permalink
scale graphviz svg down to 2/3 of original size
Browse files Browse the repository at this point in the history
This patch modifies the width and height parameters of the graphviz generated
svg image in order to scale it down to 2/3 the original size. This avoids
having to scale the image via the browser which may affect other tabs as well.
Sadly it does not seem possible to instruct the dot executable to generate an
image at the desired scale directly. The regular expression that is used
instead is fairly sensitive to the generated xml, but that is acceptable given
that the only risk is to leave the svg at its original scale.
  • Loading branch information
gertjanvanzwieten committed Feb 28, 2024
1 parent 304528e commit ad33d53
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nutils/_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess
import abc
import html
import re

Metadata = TypeVar('Metadata')
GraphvizColorCallback = Callable[['Node'], Optional[str]]
Expand Down Expand Up @@ -70,6 +71,8 @@ def export_graphviz(self, *, fill_color: Optional[GraphvizColorCallback] = None,
treelog.warning('graphviz failed for error code', status.returncode)
graph = status.stdout
if image_type == 'svg':
graph = re.sub(b'<svg width="(\d+)pt" height="(\d+)pt"', lambda match:
b'<svg width="%dpt" height="%dpt"' % tuple((int(l)*2)//3 for l in match.groups()), graph, count=1)
i = graph.rindex(b'</svg>')
graph = graph[:i] + clickHandler + graph[i:]
img.write(graph)
Expand Down

0 comments on commit ad33d53

Please sign in to comment.