From ec6b02909736791b46899eab01fac2a43ccb99d7 Mon Sep 17 00:00:00 2001 From: Andras Varga Date: Sun, 10 Sep 2023 10:58:23 +0200 Subject: [PATCH] doc/usman: workaround: wrong viewbox for dot-generated SVG figures See "Some figures in the Simulation Manual appear to be zoomed to their top-left corner" https://github.com/omnetpp/omnetpp/issues/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. --- doc/src/manual/Makefile | 1 + .../tools/update-svg-viewBox-after-dot.py | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 doc/src/manual/tools/update-svg-viewBox-after-dot.py diff --git a/doc/src/manual/Makefile b/doc/src/manual/Makefile index 1f47195ff5..f527e2cc73 100644 --- a/doc/src/manual/Makefile +++ b/doc/src/manual/Makefile @@ -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 --- diff --git a/doc/src/manual/tools/update-svg-viewBox-after-dot.py b/doc/src/manual/tools/update-svg-viewBox-after-dot.py new file mode 100644 index 0000000000..9de721a8e0 --- /dev/null +++ b/doc/src/manual/tools/update-svg-viewBox-after-dot.py @@ -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")