Skip to content

Commit

Permalink
doc/usman: workaround: wrong viewbox for dot-generated SVG figures
Browse files Browse the repository at this point in the history
See "Some figures in the Simulation Manual appear to be zoomed to their top-left corner"
#1048

Since the viewBox correction factor differs from figure to figure, this
fix is not perfect. factor=2.0 seems to be fine for most figures, and
only seriously off (too much) for one.

Both web and pdf renderings look ok.
  • Loading branch information
avarga authored and rhornig committed Sep 13, 2023
1 parent 256bcff commit ec6b029
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/src/manual/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pdf: $(ALLTEX) $(PDFPICS) figures/*.png cover.pdf

%.svg: %.dot
$(DOT) -Tsvg $< >$@
python3 tools/update-svg-viewBox-after-dot.py $@ 2.0

# --- generating html ---

Expand Down
26 changes: 26 additions & 0 deletions doc/src/manual/tools/update-svg-viewBox-after-dot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# In Graphviz 2.x, dot-generated SVGs are faulty (view box too small), this script fixes it.
# See https://github.com/omnetpp/omnetpp/issues/1048, https://gitlab.com/graphviz/graphviz/-/issues/1406

import sys
import re

if len(sys.argv) not in [2, 3]:
print(f"Usage: python {sys.argv[0]} input.svg [viewbox-enhancing-factor]")
sys.exit(1)

input_file = sys.argv[1]
factor = float(sys.argv[2]) if len(sys.argv) == 3 else 2.0

with open(input_file, "r") as file:
svg_text = file.read()

if re.search(r"Generated by graphviz version 2\.\d+\.\d+", svg_text):
# Enlarge viewBox
svg_text = re.sub(r'viewBox="([^"]+)"', lambda match: 'viewBox="' + ' '.join(str(float(value) * factor) for value in match.group(1).split()) + '"', svg_text)

# Save the modified SVG back to the same file
with open(input_file, "w") as file:
file.write(svg_text)
print(f"ViewBox modified and saved to {input_file}")
else:
print(f"Graphviz is NOT of version 2.x, ViewBox is probably OK, not touching it")

0 comments on commit ec6b029

Please sign in to comment.