-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reporting sizes of SVG diagrams
- Loading branch information
Showing
3 changed files
with
91 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters