| 1 |
import io |
| 2 |
import logging |
| 3 |
import os |
| 4 |
|
| 5 |
import fontTools |
| 6 |
import fontTools.ttLib |
| 7 |
import ufoLib2 |
| 8 |
|
| 9 |
import vttLib |
| 10 |
|
| 11 |
logger = logging.Logger(__name__) |
| 12 |
|
| 13 |
|
| 14 |
def dump_to_file(font: fontTools.ttLib.TTFont, path: os.PathLike) -> None: |
| 15 |
"""Dump relevant VTT data to a file. |
| 16 |
|
| 17 |
Relevant are: |
| 18 |
- TSI1 holds the assembly code |
| 19 |
- TSI3 holds the VTT code |
| 20 |
- TSI5 holds glyph group information |
| 21 |
- TSIC holds data for the cvar table. |
| 22 |
- cvar holds deltas for the CV table. |
| 23 |
- maxp holds instruction data computed by VTT, among other data |
| 24 |
|
| 25 |
TSI0 and TSI2 are filled in by fontTools. cvar can be rebuilt from TSIC but |
| 26 |
is dumped for historical reasons. |
| 27 |
""" |
| 28 |
tables_to_dump = ["TSI1", "TSI3", "TSI5", "maxp"] |
| 29 |
|
| 30 |
for table_tag in tables_to_dump: |
| 31 |
if table_tag not in font: |
| 32 |
raise vttLib.VTTLibArgumentError( |
| 33 |
"Table '%s' not found in input font" % table_tag |
| 34 |
) |
| 35 |
|
| 36 |
if "TSIC" in font: # Optional. |
| 37 |
tables_to_dump.append("TSIC") |
| 38 |
if "cvar" in font: # Optional. |
| 39 |
if "cvt " not in font: |
| 40 |
# Fonttools, as of 4.5.0, requires "cvt " to be present before reading |
| 41 |
# "cvar" from XML. Take the easy way out and just dump the CV table. |
| 42 |
control_program = vttLib.get_extra_assembly(font, "cvt") |
| 43 |
vttLib.set_cvt_table(font, control_program) |
| 44 |
tables_to_dump.append("cvt ") |
| 45 |
tables_to_dump.append("cvar") |
| 46 |
|
| 47 |
vttLib.normalize_vtt_programs(font) |
| 48 |
font.saveXML(path, tables=tables_to_dump) |
| 49 |
|
| 50 |
|
| 51 |
def merge_from_file( |
| 52 |
font: fontTools.ttLib.TTFont, path: os.PathLike, keep_cvar: bool = False |
| 53 |
) -> None: |
| 54 |
"""Merge VTT data from TTX dump into TTFont object. |
| 55 |
|
| 56 |
The 'maxp' table is only partially merged, as we want to import only data |
| 57 |
related to TrueType instructions, so it needs to pre-exist. |
| 58 |
""" |
| 59 |
if "maxp" not in font: |
| 60 |
raise vttLib.VTTLibArgumentError("'maxp' table not found in target font.") |
| 61 |
|
| 62 |
TABLES_TO_MERGE = ("TSI0", "TSI1", "TSI2", "TSI3", "TSI5") |
| 63 |
tables_to_merge_optional = ["TSIC"] |
| 64 |
if keep_cvar: |
| 65 |
tables_to_merge_optional.extend(["cvar", "cvt "]) |
| 66 |
|
| 67 |
ttx_dump = fontTools.ttLib.TTFont() |
| 68 |
ttx_dump.importXML(path) # Import here so we can selectively merge maxp into font. |
| 69 |
ttx_dump["TSI0"] = fontTools.ttLib.newTable("TSI0") |
| 70 |
ttx_dump["TSI2"] = fontTools.ttLib.newTable("TSI2") |
| 71 |
|
| 72 |
if keep_cvar and "cvar" not in ttx_dump: |
| 73 |
vttLib.log.warning( |
| 74 |
"The keep_cvar option was specified, but the TTX file did not include a cvar table." |
| 75 |
) |
| 76 |
|
| 77 |
for tsi_table in TABLES_TO_MERGE: |
| 78 |
font[tsi_table] = ttx_dump[tsi_table] |
| 79 |
for tsi_table in tables_to_merge_optional: |
| 80 |
if tsi_table in ttx_dump: |
| 81 |
font[tsi_table] = ttx_dump[tsi_table] |
| 82 |
|
| 83 |
for maxp_attr in vttLib.MAXP_ATTRS: |
| 84 |
setattr(font["maxp"], maxp_attr, getattr(ttx_dump["maxp"], maxp_attr)) |
| 85 |
|
| 86 |
|
| 87 |
def copy_from_ufo_data_to_file(ufo: ufoLib2.Font, path: os.PathLike) -> None: |
| 88 |
"""Dump VTT data stored in a UFO's data/ structure into a file. |
| 89 |
|
| 90 |
This is used to convert data from Legacy Projects to The New Way. |
| 91 |
""" |
| 92 |
font = fontTools.ttLib.TTFont() |
| 93 |
|
| 94 |
font["maxp"] = maxp = fontTools.ttLib.newTable("maxp") |
| 95 |
maxp.tableVersion = 0x00010000 |
| 96 |
maxp.maxComponentDepth = 0 |
| 97 |
maxp.maxComponentElements = max(len(g.components) for g in ufo) |
| 98 |
maxp.maxCompositeContours = 0 |
| 99 |
maxp.maxCompositePoints = 0 |
| 100 |
maxp.maxContours = 0 |
| 101 |
maxp.maxFunctionDefs = 0 |
| 102 |
maxp.maxInstructionDefs = 0 |
| 103 |
maxp.maxPoints = 0 |
| 104 |
maxp.maxSizeOfInstructions = 0 |
| 105 |
maxp.maxStackElements = 0 |
| 106 |
maxp.maxStorage = 0 |
| 107 |
maxp.maxTwilightPoints = 0 |
| 108 |
maxp.maxZones = 1 |
| 109 |
maxp.numGlyphs = len(ufo) |
| 110 |
if ".notdef" not in ufo: |
| 111 |
maxp.numGlyphs += 1 |
| 112 |
|
| 113 |
for key, data in ufo.data.items(): |
| 114 |
if "T_S_I__" in key: |
| 115 |
font.importXML(io.BytesIO(data)) |
| 116 |
if "com.daltonmaag.vttLib" in key: |
| 117 |
data_maxp = fontTools.misc.plistlib.loads(data)["maxp"] |
| 118 |
maxp = font["maxp"] |
| 119 |
for name in vttLib.MAXP_ATTRS: |
| 120 |
if name in data_maxp: |
| 121 |
value = data_maxp[name] |
| 122 |
setattr(maxp, name, value) |
| 123 |
|
| 124 |
dump_to_file(font, path) |