Debian Codegraph @

scripts/find-nested-components.py

raw symbols SCIP
1 import sys
2 from fontTools.ttLib import TTFont
3 import argparse
4
5 parser = argparse.ArgumentParser(description="Finds deeply nested components")
6 parser.add_argument(
7 "--depth", metavar="depth", type=int, default=2, help="minimum depth to report"
8 )
9 parser.add_argument("font", metavar="FONT", help="the font file to test")
10
11 args = parser.parse_args()
12 font = TTFont(args.font)
13
14 if "glyf" not in font:
15 print("find-nested-components can only be used on TrueType fonts")
16
17 nesting = {}
18
19
20 def get_nesting(glyph):
21 if glyph in nesting:
22 return nesting[glyph]
23 gg = font["glyf"].glyphs[glyph]
24 components = gg.getComponentNames(font["glyf"])
25 if not components:
26 nesting[glyph] = {"depth": 0, "chain": []}
27 return nesting[glyph]
28 depth = 0
29 chain = []
30 for g in components:
31 nested = get_nesting(g)
32 if 1 + nested["depth"] > depth:
33 depth = 1 + nested["depth"]
34 chain = [g] + nested["chain"]
35 nesting[glyph] = {"depth": depth, "chain": chain}
36 return nesting[glyph]
37
38
39 for glyph in font.getGlyphOrder():
40 if glyph not in nesting:
41 nesting[glyph] = get_nesting(glyph)
42
43 for k, v in nesting.items():
44 if v["depth"] < args.depth:
45 continue
46 print("%s has depth %i: %s" % (k, v["depth"], " -> ".join([k] + v["chain"])))