60 lines
2.0 KiB
Python
Executable File
60 lines
2.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import os
|
|
import sys
|
|
assert len(sys.argv) >= 2, "usage: python3 test.py <test> [OPTIONS]"
|
|
|
|
from OpenFont import FontSpecificNameID, NameID, NameTable_Format_0, OpenFontFile, PredefinedNameID, TrueTypeOutlines, open_font_file
|
|
|
|
def search_names(font: OpenFontFile, nameID: NameID) -> str:
|
|
assert isinstance(font.naming_table, NameTable_Format_0)
|
|
|
|
for nameRecord in font.naming_table.nameRecord:
|
|
if nameRecord.nameID == nameID:
|
|
return nameRecord.string
|
|
|
|
assert False, f"Name not found: {nameID}"
|
|
|
|
|
|
_, test, *options = sys.argv
|
|
|
|
match test:
|
|
case "names":
|
|
def test_font(font: OpenFontFile):
|
|
assert isinstance(font.naming_table, NameTable_Format_0)
|
|
assert isinstance(font.outlines, TrueTypeOutlines)
|
|
|
|
name = search_names(font, PredefinedNameID.FULL_NAME)
|
|
|
|
print(name, f"({font.maximum_profile.numGlyphs} glyphs, {font.naming_table.count} names)")
|
|
if font.font_variations:
|
|
axis_names = [search_names(font, FontSpecificNameID(axis.axisNameID)) for axis in font.font_variations.font_variations.axes]
|
|
num_instances = font.font_variations.font_variations.instanceCount
|
|
print(f"\tAxes: [{', '.join(axis_names)}] ({num_instances} instances)")
|
|
case _:
|
|
assert False, f"Invalid test: '{test}'"
|
|
|
|
COMPLETED_PATH = f"tests/{test}.txt"
|
|
if not os.path.exists(COMPLETED_PATH):
|
|
with open(COMPLETED_PATH, 'w') as f: pass
|
|
with open(COMPLETED_PATH, "r") as f: completed = f.read().split('\n')
|
|
|
|
def do_font(file: str):
|
|
file = file.strip()
|
|
if file in completed: return
|
|
try:
|
|
font = open_font_file(file)
|
|
test_font(font)
|
|
except AssertionError as err:
|
|
if '--raise' in options:
|
|
err.add_note(f"Failed: {file}")
|
|
raise err
|
|
print(f"{file}{':' if '--no-colon' not in options else ''} {err}")
|
|
return
|
|
with open(COMPLETED_PATH, 'a') as f: f.write(file+'\n')
|
|
completed.append(file)
|
|
|
|
assert not sys.stdin.isatty(), f"Do not run this program directly, instead pipe a list of font files into here."
|
|
for line in sys.stdin:
|
|
file = line.rstrip('\n')
|
|
do_font(file)
|