Exercise 1 solution: rank fonts by x-height ratio ================================================== Save this as rank_fonts.py and run it with: py rank_fonts.py (It needs fontTools: py -m pip install fonttools) """Rank installed fonts by x-height ratio.""" from pathlib import Path from fontTools.ttLib import TTFont FONTS = { "Times New Roman": "times.ttf", "Georgia": "georgia.ttf", "Arial": "arial.ttf", "Consolas": "consola.ttf", "Verdana": "verdana.ttf", "Calibri": "calibri.ttf", "Courier New": "cour.ttf", } FONT_DIR = Path("C:/Windows/Fonts") rows = [] for name, filename in FONTS.items(): path = FONT_DIR / filename if not path.exists(): print(f"skipped {name}: {filename} not found") continue font = TTFont(path) em = font["head"].unitsPerEm rows.append((font["OS/2"].sxHeight / em, name)) print("Largest x-height first:") for rank, (ratio, name) in enumerate(sorted(rows, reverse=True), start=1): print(f"{rank}. {name:<16}{ratio:.3f}") Output on the machine this course was written on (Windows, fonts from C:\Windows\Fonts): Largest x-height first: 1. Verdana 0.545 2. Arial 0.519 3. Consolas 0.490 4. Georgia 0.481 5. Calibri 0.464 6. Times New Roman 0.447 7. Courier New 0.423 What to take from it -------------------- * The ratio is x-height divided by units per em (the OS/2 table's sxHeight over the head table's unitsPerEm). Dividing by the em is what makes fonts comparable: two fonts can use different units per em. * Your numbers may differ if your fonts are different versions, and some fonts may be missing (the script skips them rather than crashing). * The prediction: at the same font-size, Verdana's lowercase letters look the biggest of the seven and Courier New's the smallest. (Your eyes judge "size" by lowercase letters far more than by capitals, because most letters in a line are lowercase.) * The ranking says nothing about quality: a large x-height tends to help legibility at small sizes (check), but a small one is not "wrong".