Skip to content

Commit

Permalink
feat: reporting sizes of SVG diagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
gvwilson committed Jul 16, 2023
1 parent d384522 commit 3ba7c37
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 52 deletions.
86 changes: 86 additions & 0 deletions lib/mccole/bin/check_svg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""Report suspicious fonts in diagrams."""

import argparse
import re
import sys
from xml.dom import minidom

EXPECTED = "Verdana:12px"
PAT = {
"font-family": re.compile(r"\bfont-family:\s*(.+?);"),
"font-size": re.compile(r"\bfont-size:\s*(.+?);"),
}


def main():
"""Main driver."""
options = parse_args()
font_problems = []
sizes = {}
for filename in options.files:
doc = minidom.parse(filename).documentElement
size = get_size(doc)
if size not in sizes:
sizes[size] = set()
sizes[size].add(filename)
font_problems.extend(find_fonts(filename, doc))

for key in sorted(sizes.keys()):
if (key[0] != "px") or (key[1] > options.width):
print(f"{key}: {', '.join(sorted(sizes[key]))}")

for problem in font_problems:
print(problem)


def find_fonts(filename, doc):
"""Find all fonts in document."""
seen = recurse(doc, set())
seen = {f"{entry[0]}:{entry[1]}" for entry in seen if entry[0] is not None}
seen -= {EXPECTED}
return [f"{filename}: {', '.join(sorted(seen))}" for s in seen]


def get_attr(node, name):
"""Get the font-size or font-family attribute."""
result = None
if node.hasAttribute(name):
result = node.getAttribute(name)
elif node.hasAttribute("style"):
if m := PAT[name].match(node.getAttribute("style")):
result = m.group(1)
return result


def get_size(doc):
"""Get width and height of document."""
result = (get_attr(doc, "width"), get_attr(doc, "height"))
if result[0].endswith("px"):
result = ("px", int(result[0][:-2]), int(result[1][:-2]))
elif result[0].endswith("pt"):
result = ("pt", int(result[0][:-2]), int(result[1][:-2]))
else:
result = ("raw", int(result[0]), int(result[1]))
return result


def parse_args():
"""Parse arguments."""
parser = argparse.ArgumentParser()
parser.add_argument("--files", nargs=argparse.REMAINDER, help="Files to check")
parser.add_argument("--width", type=int, help="Maximum width to allow in pixels")
return parser.parse_args()


def recurse(node, accum):
"""Recurse through all nodes in SVG."""
if node.nodeType != node.ELEMENT_NODE:
return
accum.add((get_attr(node, "font-family"), get_attr(node, "font-size")))
for child in node.childNodes:
recurse(child, accum)
return accum


if __name__ == "__main__":
main()
47 changes: 0 additions & 47 deletions lib/mccole/bin/check_svg_fonts.py

This file was deleted.

10 changes: 5 additions & 5 deletions lib/mccole/mccole.mk
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ examples:
check-make:
@for d in ${EXAMPLES}; do echo ""; echo $$d; make -C $$d --dry-run; done

## fonts: check fonts in diagrams
.PHONY: fonts
BIN_CHECK_FONTS := ${MCCOLE}/bin/check_svg_fonts.py
fonts:
@python ${BIN_CHECK_FONTS} $(SRC_SVG)
## svg: check SVG diagrams
.PHONY: svg
BIN_CHECK_SVG := ${MCCOLE}/bin/check_svg.py
svg:
@python ${BIN_CHECK_SVG} --width 600 --files $(SRC_SVG)

## spelling: check spelling against known words
.PHONY: spelling
Expand Down

0 comments on commit 3ba7c37

Please sign in to comment.