[OpenFontFormat] Change name of OpenFont class to OpenFontFile

This commit is contained in:
germax26 2024-05-03 22:46:29 +10:00
parent 8fe052ed17
commit 6a33053dfc
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM
2 changed files with 13 additions and 16 deletions

View File

@ -251,6 +251,7 @@ def parse_table_directory_entry(f: BinaryIO) -> TableDirectoryEntry:
length = read_u32(f) length = read_u32(f)
return TableDirectoryEntry(tableTag, checkSum, offset, length) return TableDirectoryEntry(tableTag, checkSum, offset, length)
# TODO: This should just be a dict[TableTag, TableDirectoryEntry]
@dataclass @dataclass
class FontDirectoryByTable: class FontDirectoryByTable:
cmap: Optional[TableDirectoryEntry] = None cmap: Optional[TableDirectoryEntry] = None
@ -5828,7 +5829,7 @@ class FontVariations:
vertical_metrics_variations: Optional[VVARTable] vertical_metrics_variations: Optional[VVARTable]
@dataclass @dataclass
class OpenFont: class OpenFontFile:
# required tables # required tables
character_to_glyph_mapping: CmapTable character_to_glyph_mapping: CmapTable
font_header: HeadTable font_header: HeadTable
@ -5839,8 +5840,9 @@ class OpenFont:
OS2_and_Windows_specific_metrics: OS2Table OS2_and_Windows_specific_metrics: OS2Table
PostScript_information: PostTable PostScript_information: PostTable
# TFF/CFF # TFF/CFF, SVG
outlines: TrueTypeOutlines | CompactFontOutlines outlines: TrueTypeOutlines | CompactFontOutlines
scalar_vector_graphics: Optional[SvgTable]
# optional tables # optional tables
digital_signature: Optional[DSIGTable] digital_signature: Optional[DSIGTable]
@ -5873,7 +5875,7 @@ def parse_at_table_directory_entry_with_length(f: BinaryIO, table: Optional[Tabl
def possibly_parse_at_table_directory_entry_with_length(f: BinaryIO, table: Optional[TableDirectoryEntry], parser: Callable[[BinaryIO, int], SomeTable]) -> Optional[SomeTable]: def possibly_parse_at_table_directory_entry_with_length(f: BinaryIO, table: Optional[TableDirectoryEntry], parser: Callable[[BinaryIO, int], SomeTable]) -> Optional[SomeTable]:
return parse_at_table_directory_entry_with_length(f, table, parser) if table else None return parse_at_table_directory_entry_with_length(f, table, parser) if table else None
def parse_open_font(f: BinaryIO) -> OpenFont: def parse_open_font_file(f: BinaryIO) -> OpenFontFile:
font_directory = parse_font_directory(f) font_directory = parse_font_directory(f)
font_header = parse_at_table_directory_entry(f, font_directory.by_table.head, parse_head_table) font_header = parse_at_table_directory_entry(f, font_directory.by_table.head, parse_head_table)
@ -5886,7 +5888,6 @@ def parse_open_font(f: BinaryIO) -> OpenFont:
PostScript_information = parse_at_table_directory_entry_with_length(f, font_directory.by_table.post, parse_post_table) PostScript_information = parse_at_table_directory_entry_with_length(f, font_directory.by_table.post, parse_post_table)
# optional # optional
digital_signature = possibly_parse_at_table_directory_entry(f, font_directory.by_table.DSIG, parse_DSIG_table) digital_signature = possibly_parse_at_table_directory_entry(f, font_directory.by_table.DSIG, parse_DSIG_table)
horizontal_device_metrics = possibly_parse_at_table_directory_entry(f, font_directory.by_table.hdmx, lambda f: parse_hdmx_table(f, maximum_profile.numGlyphs)) horizontal_device_metrics = possibly_parse_at_table_directory_entry(f, font_directory.by_table.hdmx, lambda f: parse_hdmx_table(f, maximum_profile.numGlyphs))
kerning = possibly_parse_at_table_directory_entry(f, font_directory.by_table.Kern, parse_Kern_table) kerning = possibly_parse_at_table_directory_entry(f, font_directory.by_table.Kern, parse_Kern_table)
@ -5919,7 +5920,6 @@ def parse_open_font(f: BinaryIO) -> OpenFont:
# SVG # SVG
scalar_vector_graphics = possibly_parse_at_table_directory_entry(f, font_directory.by_table.svg, parse_svg_table) scalar_vector_graphics = possibly_parse_at_table_directory_entry(f, font_directory.by_table.svg, parse_svg_table)
assert scalar_vector_graphics is None, "TODO: This"
# Advanced # Advanced
baseline_data = possibly_parse_at_table_directory_entry(f, font_directory.by_table.BASE, parse_BASE_table) baseline_data = possibly_parse_at_table_directory_entry(f, font_directory.by_table.BASE, parse_BASE_table)
@ -5953,8 +5953,8 @@ def parse_open_font(f: BinaryIO) -> OpenFont:
font_variations = FontVariations(axis_variations, CVT_variations, font_variations_, glyph_variations, horizontal_metrics_variations, metrics_variations, style_attributes, vertical_metrics_variations) font_variations = FontVariations(axis_variations, CVT_variations, font_variations_, glyph_variations, horizontal_metrics_variations, metrics_variations, style_attributes, vertical_metrics_variations)
return OpenFont(character_to_glyph_mapping, font_header, horizontal_header, horizontal_metrics, maximum_profile, naming_table, OS2_and_Windows_specific_metrics, PostScript_information, outlines, digital_signature, horizontal_device_metrics, kerning, linear_threshold_data, PCL5_data, vertical_device_metrics, vertical_metrics_header, vertical_metrics, colour_table, colour_palette_table, advanced_features, font_variations) return OpenFontFile(character_to_glyph_mapping, font_header, horizontal_header, horizontal_metrics, maximum_profile, naming_table, OS2_and_Windows_specific_metrics, PostScript_information, outlines, scalar_vector_graphics, digital_signature, horizontal_device_metrics, kerning, linear_threshold_data, PCL5_data, vertical_device_metrics, vertical_metrics_header, vertical_metrics, colour_table, colour_palette_table, advanced_features, font_variations)
def parse_file(file_path: str) -> OpenFont: def open_font(file_path: str) -> OpenFontFile: # as in `open (verb) font (noun)`, not OpenFont
with open(file_path, 'rb') as f: with open(file_path, 'rb') as f:
return parse_open_font(f) return parse_open_font_file(f)

View File

@ -1,8 +1,8 @@
import os import os
from OpenFont import FontSpecificNameID, NameID, NameTable_Format_0, OpenFont, PredefinedNameID, TrueTypeOutlines, parse_file from OpenFont import FontSpecificNameID, NameID, NameTable_Format_0, OpenFontFile, PredefinedNameID, TrueTypeOutlines, open_font
def search_names(font: OpenFont, nameID: NameID) -> str: def search_names(font: OpenFontFile, nameID: NameID) -> str:
assert isinstance(font.naming_table, NameTable_Format_0) assert isinstance(font.naming_table, NameTable_Format_0)
for nameRecord in font.naming_table.nameRecord: for nameRecord in font.naming_table.nameRecord:
@ -11,7 +11,7 @@ def search_names(font: OpenFont, nameID: NameID) -> str:
assert False, f"Name not found: {nameID}" assert False, f"Name not found: {nameID}"
def print_font(font: OpenFont): def print_font(font: OpenFontFile):
assert isinstance(font.naming_table, NameTable_Format_0) assert isinstance(font.naming_table, NameTable_Format_0)
assert isinstance(font.outlines, TrueTypeOutlines) assert isinstance(font.outlines, TrueTypeOutlines)
@ -24,12 +24,9 @@ def print_font(font: OpenFont):
print(f"\tAxes: [{', '.join(axis_names)}] ({num_instances} instances)") print(f"\tAxes: [{', '.join(axis_names)}] ({num_instances} instances)")
path = "examples/" path = "examples/"
# path = "examples/android_fonts/api_level/30"
# path = "examples/android_fonts/api_level/30/NotoNaskhArabic-Bold.ttf"
path = "examples/JetBrainsMono-2.304/"
if os.path.isfile(path): if os.path.isfile(path):
try: try:
font = parse_file(path) font = open_font(path)
except AssertionError as err: except AssertionError as err:
print("Failed:", path) print("Failed:", path)
raise err raise err
@ -42,7 +39,7 @@ else:
case '.ttf': case '.ttf':
file_path = os.path.join(root, file) file_path = os.path.join(root, file)
try: try:
font = parse_file(file_path) font = open_font(file_path)
except AssertionError as err: except AssertionError as err:
print("Failed:", file_path) print("Failed:", file_path)
raise err raise err