r500/diagrams/resize_dot_svg.py
2025-11-11 18:34:41 -06:00

32 lines
866 B
Python

import sys
scale = 0.75
def scale_svg(lines):
svg = "".join(lines)
head, viewbox = svg.split("viewBox=\"", maxsplit=1)
viewbox, tail = viewbox.split('"', maxsplit=1)
x, y, width, height = map(float, viewbox.split())
yield head
yield f'viewBox="{x} {y} {width * scale} {height * scale}"'
yield tail
def transform():
with open(sys.argv[1]) as f:
svg_lines = []
for line in f.readlines():
if line.strip().startswith("<svg"):
svg_lines.append(line)
elif svg_lines != []:
svg_lines.append(line)
if line.strip().endswith(">"):
yield from scale_svg(svg_lines)
svg_lines = []
else:
yield line
lines = list(transform())
with open(sys.argv[1], 'w') as f:
f.write(''.join(lines))