[OpenFontFormat] Add test.py
This commit is contained in:
parent
a007a28cc1
commit
adfd9b1ba4
@ -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
62
OpenFontFormat/test.py
Executable 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!")
|
Loading…
Reference in New Issue
Block a user