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. 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.
  • Loading branch information
gertjanvanzwieten committed Feb 27, 2024
1 parent f73fbd6 commit 33b01b4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions nutils/_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,15 @@ 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 (.*?)>', insert_clickHandler, graph, count=1, flags=re.DOTALL)
graph = re.sub(b'<svg (.*?)>', lambda svg_match: b'<svg '
+ re.sub(b'(width|height)="(\d+)pt"', lambda size_match:
b'%s="%dpt"' % (size_match.group(1), (int(size_match.group(2))*2)//3), svg_match.group(1), count=2)
+ b''' onload="document.addEventListener('click', clickHandler)">'''
+ clickHandler, graph, count=1, flags=re.DOTALL)
img.write(graph)

insert_clickHandler = rb'''<svg \1 onload="document.addEventListener('click', clickHandler)">

clickHandler = rb'''
<style>
.highlight path { stroke: orange; stroke-width: 2; }
.highlight polygon:first-of-type { fill: orange; }
Expand Down

0 comments on commit 33b01b4

Please sign in to comment.