[OpenFontFormat] Add test.py

This commit is contained in:
germax26 2024-07-28 22:16:59 +10:00
parent a007a28cc1
commit adfd9b1ba4
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM
2 changed files with 62 additions and 40 deletions

View File

@ -1,40 +0,0 @@
#!/usr/bin/env python3
import os
import sys
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}"
def print_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)")
def do_font(file: str):
try:
font = open_font_file(file)
print_font(font)
except AssertionError as err:
print(f"Failed: {file}")
raise err
assert not sys.stdin.isatty()
for line in sys.stdin:
file = line.rstrip('\n')
do_font(file)

62
OpenFontFormat/test.py Executable file
View File

@ -0,0 +1,62 @@
#!/usr/bin/env python3
import os
import sys
assert len(sys.argv) == 2, "usage: python3 test.py <test>"
from OpenFont import FontSpecificNameID, NameID, NameTable_Format_0, OpenFontFile, PredefinedNameID, TrueTypeOutlines, open_font_file, write_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 = 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 "rewrite":
def test_font(font: OpenFontFile):
PATH = "out.ttf"
write_font_file(font, PATH)
open_font_file(PATH)
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):
if file in completed: return
try:
font = open_font_file(file)
test_font(font)
except AssertionError as err:
err.add_note(f"Failed: {file}")
raise err
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)
print("Done!")