| 1 |
import array |
| 2 |
import io |
| 3 |
import logging |
| 4 |
import os |
| 5 |
import re |
| 6 |
from collections import OrderedDict, defaultdict, deque, namedtuple |
| 7 |
|
| 8 |
import ufoLib2 |
| 9 |
from fontTools.misc.arrayTools import Vector |
| 10 |
from fontTools.misc.fixedTools import otRound |
| 11 |
from fontTools.ttLib import TTFont, TTLibError, newTable |
| 12 |
from fontTools.ttLib.tables._g_l_y_f import ( |
| 13 |
ROUND_XY_TO_GRID, |
| 14 |
SCALED_COMPONENT_OFFSET, |
| 15 |
UNSCALED_COMPONENT_OFFSET, |
| 16 |
USE_MY_METRICS, |
| 17 |
) |
| 18 |
from fontTools.ttLib.tables.ttProgram import Program |
| 19 |
from fontTools.ttLib.tables.TupleVariation import TupleVariation |
| 20 |
from fontTools.varLib import models |
| 21 |
|
| 22 |
import vttLib.transfer |
| 23 |
from vttLib.parser import AssemblyParser, ParseException |
| 24 |
|
| 25 |
try: |
| 26 |
from ._version import version as __version__ # type: ignore |
| 27 |
except ImportError: |
| 28 |
__version__ = "0.0.0+unknown" |
| 29 |
|
| 30 |
log = logging.getLogger(__name__) |
| 31 |
|
| 32 |
|
| 33 |
MAXP_ATTRS = { |
| 34 |
"maxZones", |
| 35 |
"maxTwilightPoints", |
| 36 |
"maxStorage", |
| 37 |
"maxFunctionDefs", |
| 38 |
"maxInstructionDefs", |
| 39 |
"maxStackElements", |
| 40 |
"maxSizeOfInstructions", |
| 41 |
} |
| 42 |
LEGACY_VTT_DATA_FILES = ( |
| 43 |
"com.daltonmaag.vttLib.plist", |
| 44 |
"com.github.fonttools.ttx/T_S_I__0.ttx", |
| 45 |
"com.github.fonttools.ttx/T_S_I__1.ttx", |
| 46 |
"com.github.fonttools.ttx/T_S_I__2.ttx", |
| 47 |
"com.github.fonttools.ttx/T_S_I__3.ttx", |
| 48 |
"com.github.fonttools.ttx/T_S_I__5.ttx", |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
class VTTLibError(TTLibError): |
| 53 |
pass |
| 54 |
|
| 55 |
|
| 56 |
class VTTLibInvalidComposite(VTTLibError): |
| 57 |
pass |
| 58 |
|
| 59 |
|
| 60 |
class VTTLibArgumentError(VTTLibError): |
| 61 |
pass |
| 62 |
|
| 63 |
|
| 64 |
def set_cvt_table(font, data): |
| 65 |
data = re.sub(r"/\*.*?\*/", "", data, flags=re.DOTALL) |
| 66 |
values = array.array("h") |
| 67 |
# control values are defined in VTT Control Program as colon-separated |
| 68 |
# INDEX: VALUE pairs |
| 69 |
for m in re.finditer(r"^\s*([0-9]+)\s*:\s*(-?[0-9]+)", data, re.MULTILINE): |
| 70 |
index, value = int(m.group(1)), int(m.group(2)) |
| 71 |
for _ in range(1 + index - len(values)): |
| 72 |
# missing CV indexes default to zero |
| 73 |
values.append(0) |
| 74 |
values[index] = value |
| 75 |
if len(values): |
| 76 |
if "cvt " not in font: |
| 77 |
font["cvt "] = newTable("cvt ") |
| 78 |
font["cvt "].values = values |
| 79 |
|
| 80 |
|
| 81 |
OffsetComponent = namedtuple( |
| 82 |
"OffsetComponent", |
| 83 |
["index", "x", "y", "round_to_grid", "use_my_metrics", "scaled_offset"], |
| 84 |
) |
| 85 |
AnchorComponent = namedtuple( |
| 86 |
"AnchorComponent", ["index", "first", "second", "use_my_metrics", "scaled_offset"] |
| 87 |
) |
| 88 |
|
| 89 |
JUMP_INSTRUCTIONS = frozenset(["JMPR", "JROT", "JROF"]) |
| 90 |
|
| 91 |
|
| 92 |
class JumpVariable(object): |
| 93 |
def __init__(self, positions=None, to_label=None, from_offset=None): |
| 94 |
if positions: |
| 95 |
self.positions = defaultdict(list, **positions) |
| 96 |
else: |
| 97 |
self.positions = defaultdict(list) |
| 98 |
self.to_label = to_label |
| 99 |
self.from_offset = from_offset |
| 100 |
self.relative_offset = None |
| 101 |
|
| 102 |
def __repr__(self): |
| 103 |
return "{}({})".format( |
| 104 |
type(self).__name__, |
| 105 |
", ".join("{}={}".format(k, v) for k, v in sorted(self.__dict__.items())), |
| 106 |
) |
| 107 |
|
| 108 |
|
| 109 |
def split_functions(fpgm_tokens): |
| 110 |
funcs = [] |
| 111 |
tokens = iter(fpgm_tokens) |
| 112 |
for t in tokens: |
| 113 |
mnemonic = t.mnemonic |
| 114 |
if mnemonic.startswith("FDEF") or mnemonic.startswith("IDEF"): |
| 115 |
body = [t] |
| 116 |
for t in tokens: |
| 117 |
body.append(t) |
| 118 |
if t.mnemonic.startswith("ENDF"): |
| 119 |
break |
| 120 |
funcs.append(body) |
| 121 |
continue |
| 122 |
# some version of VTT has #PUSHON between function definitions |
| 123 |
if t.mnemonic.startswith("#PUSHON"): |
| 124 |
continue |
| 125 |
assert 0, "Unexpected token in fpgm: %s" % t |
| 126 |
return funcs |
| 127 |
|
| 128 |
|
| 129 |
def merge_functions(functions, include=None): |
| 130 |
asm = [] |
| 131 |
for line in "\n".join(functions).splitlines(): |
| 132 |
asm.extend(line.strip().split()) |
| 133 |
funcs = {} |
| 134 |
idefs = {} |
| 135 |
stack = [] |
| 136 |
tokens = iter(asm) |
| 137 |
for token in tokens: |
| 138 |
if token.startswith("PUSH") or token.startswith("NPUSH"): |
| 139 |
for token in tokens: |
| 140 |
try: |
| 141 |
num = int(token) |
| 142 |
stack.append(num) |
| 143 |
except ValueError: |
| 144 |
break |
| 145 |
if token.startswith(("FDEF", "IDEF")): |
| 146 |
is_fdef = token.startswith("FDEF") |
| 147 |
num = stack.pop() |
| 148 |
body = [token] |
| 149 |
for token in tokens: |
| 150 |
body.append(token) |
| 151 |
if token.startswith("ENDF"): |
| 152 |
break |
| 153 |
if is_fdef: |
| 154 |
funcs[num] = body |
| 155 |
else: |
| 156 |
idefs[num] = body |
| 157 |
continue |
| 158 |
assert 0, "Unexpected token in fpgm: %s" % token |
| 159 |
if include is not None: |
| 160 |
include = set(include) |
| 161 |
funcs = {num: body for num, body in funcs.items() if num in include} |
| 162 |
result = ["PUSH[]"] + [str(n) for n in sorted(funcs, reverse=True)] |
| 163 |
for num in sorted(funcs): |
| 164 |
result.extend(funcs[num]) |
| 165 |
result.append("PUSH[]") |
| 166 |
result.extend([str(n) for n in sorted(idefs, reverse=True)]) |
| 167 |
for num in sorted(idefs): |
| 168 |
result.extend(idefs[num]) |
| 169 |
return result |
| 170 |
|
| 171 |
|
| 172 |
def tokenize(data, parseAll=True): |
| 173 |
return AssemblyParser.parseString(data, parseAll=parseAll) |
| 174 |
|
| 175 |
|
| 176 |
def transform(tokens, components=None): |
| 177 |
push_on = True |
| 178 |
push_indexes = [0] |
| 179 |
stream = [deque()] |
| 180 |
pos = 1 |
| 181 |
if components is None: |
| 182 |
components = [] |
| 183 |
jump_labels = {} |
| 184 |
jump_variables = defaultdict(JumpVariable) |
| 185 |
round_to_grid = False |
| 186 |
use_my_metrics = False |
| 187 |
scaled_offset = None |
| 188 |
for t in tokens: |
| 189 |
mnemonic = t.mnemonic |
| 190 |
|
| 191 |
if mnemonic == "OVERLAP": |
| 192 |
# this component flag is ignored by VTT so we ignore it too |
| 193 |
continue |
| 194 |
elif mnemonic == "USEMYMETRICS": |
| 195 |
use_my_metrics = True |
| 196 |
continue |
| 197 |
elif mnemonic == "SCALEDCOMPONENTOFFSET": |
| 198 |
scaled_offset = True |
| 199 |
continue |
| 200 |
elif mnemonic == "UNSCALEDCOMPONENTOFFSET": |
| 201 |
scaled_offset = False |
| 202 |
continue |
| 203 |
elif mnemonic == "OFFSET": |
| 204 |
round_to_grid = t.flags == "1" |
| 205 |
index, x, y = t.stack_items |
| 206 |
component = OffsetComponent( |
| 207 |
index, x, y, round_to_grid, use_my_metrics, scaled_offset |
| 208 |
) |
| 209 |
components.append(component) |
| 210 |
use_my_metrics = round_to_grid = False |
| 211 |
scaled_offset = None |
| 212 |
continue |
| 213 |
elif mnemonic == "ANCHOR": |
| 214 |
index, first, second = t.stack_items |
| 215 |
component = AnchorComponent( |
| 216 |
index, first, second, use_my_metrics, scaled_offset |
| 217 |
) |
| 218 |
components.append(component) |
| 219 |
use_my_metrics = False |
| 220 |
scaled_offset = None |
| 221 |
|
| 222 |
elif mnemonic == "#PUSHON": |
| 223 |
push_on = True |
| 224 |
continue |
| 225 |
elif mnemonic == "#PUSHOFF": |
| 226 |
push_on = False |
| 227 |
continue |
| 228 |
|
| 229 |
elif mnemonic == "#BEGIN": |
| 230 |
# XXX shouldn't these be ignored in #PUSHOFF mode? |
| 231 |
push_indexes.append(pos) |
| 232 |
stream.append(deque()) |
| 233 |
pos += 1 |
| 234 |
continue |
| 235 |
elif mnemonic == "#END": |
| 236 |
pi = push_indexes.pop() |
| 237 |
stack = stream[pi] |
| 238 |
if len(stack): |
| 239 |
stream[pi] = ["PUSH[]"] + list(stack) |
| 240 |
continue |
| 241 |
|
| 242 |
elif mnemonic == "#PUSH": |
| 243 |
assert len(t.stack_items) > 0 |
| 244 |
|
| 245 |
last_type = None |
| 246 |
push_groups = [] |
| 247 |
for item in t.stack_items: |
| 248 |
curr_type = type(item) |
| 249 |
if curr_type is not last_type: |
| 250 |
push_groups.append([item]) |
| 251 |
else: |
| 252 |
push_groups[-1].append(item) |
| 253 |
last_type = curr_type |
| 254 |
|
| 255 |
column = 1 |
| 256 |
for args in push_groups: |
| 257 |
is_variable = isinstance(args[0], str) |
| 258 |
if is_variable: |
| 259 |
# initialize jump variables with the row and column in which |
| 260 |
# they appear in the stream |
| 261 |
for column, item in enumerate(args, start=1): |
| 262 |
jump_variables[item].positions[pos].append(column) |
| 263 |
# we explicitly push words for variable stack items to |
| 264 |
# prevent optimization from breaking relative jump offsets |
| 265 |
# Here '-999' is just an arbitrary word value which will |
| 266 |
# be replaced with the actual relative offset |
| 267 |
args = [-999] * len(args) |
| 268 |
if len(args) > 8: |
| 269 |
stream.append(["NPUSHW[]", len(args)] + args) |
| 270 |
else: |
| 271 |
stream.append(["PUSHW[]"] + args) |
| 272 |
else: |
| 273 |
# let fonttools do the automatic push optimization for |
| 274 |
# normal integer stack items |
| 275 |
stream.append(["PUSH[]"] + args) |
| 276 |
pos += 1 |
| 277 |
|
| 278 |
continue |
| 279 |
|
| 280 |
elif mnemonic.startswith(("DLTC", "DLTP", "DELTAP", "DELTAC")): |
| 281 |
assert push_on |
| 282 |
n = len(t.deltas) |
| 283 |
assert n > 0 |
| 284 |
stack = stream[push_indexes[-1]] |
| 285 |
stack.appendleft(n) |
| 286 |
|
| 287 |
deltas = OrderedDict() |
| 288 |
for point_index, rel_ppem, step_no in reversed(t.deltas): |
| 289 |
deltas.setdefault(point_index, []).append((rel_ppem, step_no)) |
| 290 |
|
| 291 |
for point_index, delta_specs in deltas.items(): |
| 292 |
for rel_ppem, step_no in sorted(delta_specs, reverse=True): |
| 293 |
if mnemonic.startswith(("DELTAP", "DELTAC")): |
| 294 |
# DELTAC1 and DELTAP1: delta_base to delta_base+15 |
| 295 |
if mnemonic.endswith("1"): |
| 296 |
delta_base = 9 |
| 297 |
# DELTAC2 and DELTAP2: delta_base+16 to delta_base+31 |
| 298 |
elif mnemonic.endswith("2"): |
| 299 |
delta_base = 25 |
| 300 |
# DELTAC3 and DELTAP3: delta_base+32 to delta_base+47 |
| 301 |
elif mnemonic.endswith("3"): |
| 302 |
delta_base = 41 |
| 303 |
# subtract the default 'delta base' |
| 304 |
rel_ppem -= delta_base |
| 305 |
stack.appendleft(point_index) |
| 306 |
# -8: 0, ... -1: 7, 1: 8, ... 8: 15 |
| 307 |
selector = (step_no + 7) if step_no > 0 else (step_no + 8) |
| 308 |
stack.appendleft((rel_ppem << 4) | selector) |
| 309 |
if mnemonic.startswith("DLT"): |
| 310 |
mnemonic = mnemonic.replace("DLT", "DELTA") |
| 311 |
elif mnemonic.startswith("#") and mnemonic.endswith(":"): |
| 312 |
# collect goto labels used with relative jump instructions |
| 313 |
label = mnemonic[:-1] |
| 314 |
jump_labels[label] = pos |
| 315 |
continue |
| 316 |
elif mnemonic in JUMP_INSTRUCTIONS: |
| 317 |
# record the current offset of jump instruction and the label which |
| 318 |
# it should jumps to |
| 319 |
variable, label = t.assignment |
| 320 |
jump_variables[variable].to_label = label |
| 321 |
jump_variables[variable].from_offset = pos |
| 322 |
else: |
| 323 |
if push_on: |
| 324 |
for i in reversed(t.stack_items): |
| 325 |
stream[push_indexes[-1]].appendleft(i) |
| 326 |
else: |
| 327 |
assert not t.stack_items or mnemonic.startswith(("PUSH", "NPUSH")) |
| 328 |
|
| 329 |
# While it is true that most instructions cannot take stack |
| 330 |
# parameters in #PUSHOFF mode, you can have PUSHx[] and NPUSHx[] |
| 331 |
# instructions that do have parameters that need to get onto the |
| 332 |
# stack. (ok, technically, they are not parameters to put on the |
| 333 |
# stack but data items in the instruction stream). |
| 334 |
# If the current instruction is a kind of PUSH, emit the |
| 335 |
# instruction with its stack parameters (excluding the count for |
| 336 |
# NPUSHx instructions as fontTools computes that). |
| 337 |
if t.stack_items: |
| 338 |
if mnemonic.startswith("NPUSH"): |
| 339 |
params = t.stack_items[1:] |
| 340 |
elif mnemonic.startswith("PUSH"): |
| 341 |
params = t.stack_items[:] |
| 342 |
stream.append([f"{mnemonic}[]"] + params) |
| 343 |
pos += 1 |
| 344 |
continue |
| 345 |
|
| 346 |
stream.append(["{}[{}]".format(mnemonic, t.flags)]) |
| 347 |
pos += 1 |
| 348 |
|
| 349 |
# calculate the relative offsets of each jump variables |
| 350 |
for variable in jump_variables.values(): |
| 351 |
to_offset = jump_labels[variable.to_label] |
| 352 |
from_offset = variable.from_offset |
| 353 |
assert to_offset != from_offset |
| 354 |
start, end = sorted([from_offset, to_offset]) |
| 355 |
sign = 1 if to_offset > from_offset else -1 |
| 356 |
size = _calc_stream_size(stream[start:end]) |
| 357 |
variable.relative_offset = sign * size |
| 358 |
|
| 359 |
# replace variable push args with the computed relative offsets |
| 360 |
for variable in jump_variables.values(): |
| 361 |
for row, columns in variable.positions.items(): |
| 362 |
for column in columns: |
| 363 |
stream[row][column] = variable.relative_offset |
| 364 |
|
| 365 |
assert len(push_indexes) == 1 and push_indexes[0] == 0, push_indexes |
| 366 |
stack = stream[0] |
| 367 |
if len(stack): |
| 368 |
stream[0] = ["PUSH[]"] + list(stack) |
| 369 |
|
| 370 |
return _concat_stream(stream) |
| 371 |
|
| 372 |
|
| 373 |
def transform_assembly(data, name=None, components=None): |
| 374 |
data = data.strip() |
| 375 |
if not data: |
| 376 |
# input data just contains empty whitespace; skip it |
| 377 |
return "" |
| 378 |
|
| 379 |
tokens = tokenize(data) |
| 380 |
|
| 381 |
if name == "fpgm": |
| 382 |
# we transform each function in the fpgm individually, since different |
| 383 |
# functions may refer to jump variables with the same name, however |
| 384 |
# the relative jumps must occur within the same functions (I think...) |
| 385 |
funcs = [transform(f) for f in split_functions(tokens)] |
| 386 |
return merge_functions(funcs) |
| 387 |
else: |
| 388 |
return transform(tokens, components=components) |
| 389 |
|
| 390 |
|
| 391 |
def _concat_stream(stream): |
| 392 |
return "\n".join(" ".join(str(i) for i in item) for item in stream if item) |
| 393 |
|
| 394 |
|
| 395 |
def _calc_stream_size(stream): |
| 396 |
program = make_ft_program(_concat_stream(stream)) |
| 397 |
return len(program.getBytecode()) |
| 398 |
|
| 399 |
|
| 400 |
def make_ft_program(assembly): |
| 401 |
program = Program() |
| 402 |
program.fromAssembly(assembly) |
| 403 |
# need to compile bytecode for PUSH optimization |
| 404 |
program._assemble() |
| 405 |
del program.assembly |
| 406 |
return program |
| 407 |
|
| 408 |
|
| 409 |
_indentRE = re.compile("^FDEF|IF|ELSE\\[ \\]\t.+") |
| 410 |
_unindentRE = re.compile("^ELSE|ENDF|EIF\\[ \\]\t.+") |
| 411 |
|
| 412 |
|
| 413 |
def pformat_tti(program, preserve=True): |
| 414 |
from fontTools.ttLib.tables.ttProgram import _pushCountPat |
| 415 |
|
| 416 |
assembly = program.getAssembly(preserve=preserve) |
| 417 |
stream = io.StringIO() |
| 418 |
i = 0 |
| 419 |
indent = 0 |
| 420 |
nInstr = len(assembly) |
| 421 |
while i < nInstr: |
| 422 |
instr = assembly[i] |
| 423 |
if _unindentRE.match(instr): |
| 424 |
indent -= 1 |
| 425 |
stream.write(" " * indent) |
| 426 |
stream.write(instr) |
| 427 |
stream.write("\n") |
| 428 |
m = _pushCountPat.match(instr) |
| 429 |
i = i + 1 |
| 430 |
if m: |
| 431 |
nValues = int(m.group(1)) |
| 432 |
line = [] |
| 433 |
j = 0 |
| 434 |
for j in range(nValues): |
| 435 |
if j and not (j % 25): |
| 436 |
stream.write(" ".join(line)) |
| 437 |
stream.write("\n") |
| 438 |
line = [] |
| 439 |
line.append(assembly[i + j]) |
| 440 |
stream.write(" " * indent) |
| 441 |
stream.write(" ".join(line)) |
| 442 |
stream.write("\n") |
| 443 |
i = i + j + 1 |
| 444 |
if _indentRE.match(instr): |
| 445 |
indent += 1 |
| 446 |
return stream.getvalue() |
| 447 |
|
| 448 |
|
| 449 |
def log_program_error(name, error): |
| 450 |
log.error( |
| 451 |
"An error occurred while parsing %sprogram:\n%s\n\n" |
| 452 |
% ('"%s" ' % name if name else "", error.markInputline()) |
| 453 |
) |
| 454 |
|
| 455 |
|
| 456 |
def make_program(vtt_assembly, name=None, components=None): |
| 457 |
try: |
| 458 |
ft_assembly = transform_assembly(vtt_assembly, name=name, components=components) |
| 459 |
except ParseException as e: |
| 460 |
log_program_error(name, e) |
| 461 |
raise VTTLibError(e) |
| 462 |
return make_ft_program(ft_assembly) |
| 463 |
|
| 464 |
|
| 465 |
def make_glyph_program(vtt_assembly, name=None): |
| 466 |
components = [] |
| 467 |
program = make_program(vtt_assembly, name, components) |
| 468 |
return program, components |
| 469 |
|
| 470 |
|
| 471 |
def get_extra_assembly(font, name): |
| 472 |
if name not in ("cvt", "cvt ", "prep", "ppgm", "fpgm"): |
| 473 |
raise ValueError("Invalid name: %r" % name) |
| 474 |
if name == "prep": |
| 475 |
name = "ppgm" |
| 476 |
return get_vtt_program(font, name.strip()) |
| 477 |
|
| 478 |
|
| 479 |
def get_glyph_assembly(font, name): |
| 480 |
return get_vtt_program(font, name, is_glyph=True) |
| 481 |
|
| 482 |
|
| 483 |
def get_glyph_talk(font, name): |
| 484 |
return get_vtt_program(font, name, is_talk=True, is_glyph=True) |
| 485 |
|
| 486 |
|
| 487 |
def get_vtt_program(font, name, is_talk=False, is_glyph=False): |
| 488 |
tag = "TSI3" if is_talk else "TSI1" |
| 489 |
if tag not in font: |
| 490 |
raise VTTLibError("%s table not found" % tag) |
| 491 |
try: |
| 492 |
if is_glyph: |
| 493 |
data = font[tag].glyphPrograms[name] |
| 494 |
else: |
| 495 |
data = font[tag].extraPrograms[name] |
| 496 |
except KeyError: |
| 497 |
raise KeyError( |
| 498 |
"%s program missing from %s: '%s'" |
| 499 |
% ("Glyph" if is_glyph else "Extra", tag, name) |
| 500 |
) |
| 501 |
return data.replace("\r", "\n") |
| 502 |
|
| 503 |
|
| 504 |
def set_extra_assembly(font, name, data): |
| 505 |
if name not in ("cvt", "cvt ", "prep", "ppgm", "fpgm"): |
| 506 |
raise ValueError("Invalid name: %r" % name) |
| 507 |
if name == "prep": |
| 508 |
name = "ppgm" |
| 509 |
set_vtt_program(font, name, data) |
| 510 |
|
| 511 |
|
| 512 |
def set_glyph_assembly(font, name, data): |
| 513 |
set_vtt_program(font, name, data, is_glyph=True) |
| 514 |
|
| 515 |
|
| 516 |
def set_glyph_talk(font, name, data): |
| 517 |
return set_vtt_program(font, name, data, is_talk=True, is_glyph=True) |
| 518 |
|
| 519 |
|
| 520 |
def set_vtt_program(font, name, data, is_talk=False, is_glyph=False): |
| 521 |
tag = "TSI3" if is_talk else "TSI1" |
| 522 |
if tag not in font: |
| 523 |
raise VTTLibError("%s table not found" % tag) |
| 524 |
data = "\r".join(data.splitlines()).rstrip() + "\r" |
| 525 |
if is_glyph: |
| 526 |
font[tag].glyphPrograms[name] = data |
| 527 |
else: |
| 528 |
font[tag].extraPrograms[name] = data |
| 529 |
|
| 530 |
|
| 531 |
def check_composite_info(name, glyph, vtt_components, glyph_order, check_flags=False): |
| 532 |
n_glyf_comps = len(glyph.components) |
| 533 |
n_vtt_comps = len(vtt_components) |
| 534 |
if n_vtt_comps != n_glyf_comps: |
| 535 |
raise VTTLibInvalidComposite( |
| 536 |
"'%s' has incorrect number of components: expected %d, " |
| 537 |
"found %d." % (name, n_glyf_comps, n_vtt_comps) |
| 538 |
) |
| 539 |
for i, comp in enumerate(glyph.components): |
| 540 |
vttcomp = vtt_components[i] |
| 541 |
base_name = comp.glyphName |
| 542 |
index = glyph_order.index(base_name) |
| 543 |
if vttcomp.index != index: |
| 544 |
raise VTTLibInvalidComposite( |
| 545 |
"Component %d in '%s' has incorrect index: " |
| 546 |
"expected %d, found %d." % (i, name, index, vttcomp.index) |
| 547 |
) |
| 548 |
if hasattr(comp, "firstPt"): |
| 549 |
if not hasattr(vttcomp, "first") and hasattr(vttcomp, "x"): |
| 550 |
raise VTTLibInvalidComposite( |
| 551 |
"Component %d in '%s' has incorrect type: " |
| 552 |
"expected ANCHOR[], found OFFSET[]." % (i, name) |
| 553 |
) |
| 554 |
if comp.firstPt != vttcomp.first: |
| 555 |
raise VTTLibInvalidComposite( |
| 556 |
"Component %d in '%s' has wrong anchor point: expected" |
| 557 |
" %d, found %d." % (i, name, comp.firstPt, vttcomp.first) |
| 558 |
) |
| 559 |
if comp.secondPt != vttcomp.second: |
| 560 |
raise VTTLibInvalidComposite( |
| 561 |
"Component %d in '%s' has wrong anchor point: expected" |
| 562 |
" %d, found %d." % (i, name, comp.secondPt, vttcomp.second) |
| 563 |
) |
| 564 |
else: |
| 565 |
assert hasattr(comp, "x") |
| 566 |
if not hasattr(vttcomp, "x") and hasattr(vttcomp, "first"): |
| 567 |
raise VTTLibInvalidComposite( |
| 568 |
"Component %d in '%s' has incorrect type: " |
| 569 |
"expected OFFSET[], found ANCHOR[]." % (i, name) |
| 570 |
) |
| 571 |
if comp.x != vttcomp.x: |
| 572 |
raise VTTLibInvalidComposite( |
| 573 |
"Component %d in '%s' has wrong x offset: expected" |
| 574 |
" %d, found %d." % (i, name, comp.x, vttcomp.x) |
| 575 |
) |
| 576 |
if comp.y != vttcomp.y: |
| 577 |
raise VTTLibInvalidComposite( |
| 578 |
"Component %d in '%s' has wrong y offset: expected" |
| 579 |
" %d, found %d." % (i, name, comp.y, vttcomp.y) |
| 580 |
) |
| 581 |
if check_flags and ( |
| 582 |
(comp.flags & ROUND_XY_TO_GRID and not vttcomp.round_to_grid) |
| 583 |
or (not comp.flags & ROUND_XY_TO_GRID and vttcomp.round_to_grid) |
| 584 |
): |
| 585 |
raise VTTLibInvalidComposite( |
| 586 |
"Component %d in '%s' has wrong 'ROUND_XY_TO_GRID' flag." |
| 587 |
% (i, name) |
| 588 |
) |
| 589 |
if not check_flags: |
| 590 |
continue |
| 591 |
if (comp.flags & USE_MY_METRICS and not vttcomp.use_my_metrics) or ( |
| 592 |
not comp.flags & USE_MY_METRICS and vttcomp.use_my_metrics |
| 593 |
): |
| 594 |
raise VTTLibInvalidComposite( |
| 595 |
"Component %d in '%s' has wrong 'USE_MY_METRICS' flag." % (i, name) |
| 596 |
) |
| 597 |
if (comp.flags & SCALED_COMPONENT_OFFSET and not vttcomp.scaled_offset) or ( |
| 598 |
not comp.flags & SCALED_COMPONENT_OFFSET and vttcomp.scaled_offset |
| 599 |
): |
| 600 |
raise VTTLibInvalidComposite( |
| 601 |
"Component %d in '%s' has wrong 'SCALED_COMPONENT_OFFSET' flag." |
| 602 |
% (i, name) |
| 603 |
) |
| 604 |
if (comp.flags & UNSCALED_COMPONENT_OFFSET and not vttcomp.scaled_offset) or ( |
| 605 |
not comp.flags & UNSCALED_COMPONENT_OFFSET and vttcomp.scaled_offset |
| 606 |
): |
| 607 |
raise VTTLibInvalidComposite( |
| 608 |
"Component %d in '%s' has wrong 'UNSCALED_COMPONENT_OFFSET' flag." |
| 609 |
"flag" % (i, name) |
| 610 |
) |
| 611 |
|
| 612 |
|
| 613 |
_use_my_metrics = r"^USEMYMETRICS\[\][\r\n]?" |
| 614 |
_overlap = r"^OVERLAP\[\][\r\n]?" |
| 615 |
_scaled_component_offset = r"^(?:UN)?SCALEDCOMPONENTOFFSET\[\][\r\n]?" |
| 616 |
_anchor = r"^ANCHOR\[\](?:, *-?[0-9]+){3}[\r\n]?" |
| 617 |
_offset = r"^OFFSET\[[rR]\](?:, *-?[0-9]+){3}[\r\n]?" |
| 618 |
composite_info_RE = re.compile( |
| 619 |
"(%s)|(%s)|(%s)|(%s)|(%s)" |
| 620 |
% (_use_my_metrics, _overlap, _scaled_component_offset, _anchor, _offset), |
| 621 |
re.MULTILINE, |
| 622 |
) |
| 623 |
|
| 624 |
|
| 625 |
def set_components_flags(glyph, components, vtt_version=6): |
| 626 |
assert len(components) == len(glyph.components) |
| 627 |
for i, comp in enumerate(glyph.components): |
| 628 |
vttcomp = components[i] |
| 629 |
if vttcomp.use_my_metrics: |
| 630 |
comp.flags |= USE_MY_METRICS |
| 631 |
else: |
| 632 |
comp.flags &= ~USE_MY_METRICS |
| 633 |
if vttcomp.round_to_grid: |
| 634 |
comp.flags |= ROUND_XY_TO_GRID |
| 635 |
else: |
| 636 |
comp.flags &= ~ROUND_XY_TO_GRID |
| 637 |
if vtt_version < 6 or vttcomp.scaled_offset is None: |
| 638 |
continue |
| 639 |
if vttcomp.scaled_offset: |
| 640 |
comp.flags |= SCALED_COMPONENT_OFFSET |
| 641 |
comp.flags &= ~UNSCALED_COMPONENT_OFFSET |
| 642 |
else: |
| 643 |
comp.flags |= UNSCALED_COMPONENT_OFFSET |
| 644 |
comp.flags &= ~SCALED_COMPONENT_OFFSET |
| 645 |
|
| 646 |
|
| 647 |
def write_composite_info(glyph, glyph_order, data="", vtt_version=6): |
| 648 |
head = "" |
| 649 |
last = 0 |
| 650 |
for m in composite_info_RE.finditer(data): |
| 651 |
start, end = m.span() |
| 652 |
head += data[last:start] |
| 653 |
last = end |
| 654 |
tail = "" |
| 655 |
if last < len(data): |
| 656 |
tail += data[last:] |
| 657 |
instructions = [] |
| 658 |
for comp in glyph.components: |
| 659 |
if comp.flags & USE_MY_METRICS: |
| 660 |
instructions.append("USEMYMETRICS[]\n") |
| 661 |
if vtt_version >= 6: |
| 662 |
if comp.flags & SCALED_COMPONENT_OFFSET: |
| 663 |
instructions.append("SCALEDCOMPONENTOFFSET[]\n") |
| 664 |
if comp.flags & UNSCALED_COMPONENT_OFFSET: |
| 665 |
instructions.append("UNSCALEDCOMPONENTOFFSET[]\n") |
| 666 |
index = glyph_order.index(comp.glyphName) |
| 667 |
if hasattr(comp, "firstPt"): |
| 668 |
instructions.append( |
| 669 |
"ANCHOR[], %d, %d, %d\n" % (index, comp.firstPt, comp.secondPt) |
| 670 |
) |
| 671 |
else: |
| 672 |
flag = "R" if comp.flags & ROUND_XY_TO_GRID else "r" |
| 673 |
instructions.append( |
| 674 |
"OFFSET[%s], %d, %d, %d\n" % (flag, index, comp.x, comp.y) |
| 675 |
) |
| 676 |
return head, "".join(instructions), tail |
| 677 |
|
| 678 |
|
| 679 |
def update_composites(font, glyphs=None, vtt_version=6): |
| 680 |
glyph_order = font.getGlyphOrder() |
| 681 |
if glyphs is None: |
| 682 |
glyphs = glyph_order |
| 683 |
glyf_table = font["glyf"] |
| 684 |
for glyph_name in glyphs: |
| 685 |
glyph = glyf_table[glyph_name] |
| 686 |
vtt_components = [] |
| 687 |
try: |
| 688 |
data = get_glyph_assembly(font, glyph_name) |
| 689 |
except KeyError: |
| 690 |
# the glyph is not in the TSI1 table; create a new one |
| 691 |
data = "" |
| 692 |
else: |
| 693 |
# found glyph in TSI1 table; check it contains any VTT components; |
| 694 |
# 'vtt_components' list is updated in place; we don't care about |
| 695 |
# the return value (i.e. transformed FontTools assembly) here |
| 696 |
try: |
| 697 |
transform_assembly(data, components=vtt_components) |
| 698 |
except ParseException as e: |
| 699 |
log_program_error(glyph_name, e) |
| 700 |
raise VTTLibError(e) |
| 701 |
if not glyph.isComposite(): |
| 702 |
if vtt_components: |
| 703 |
log.warning( |
| 704 |
"Glyph '%s' contains components in VTT assembly but not" |
| 705 |
"in glyf table; drop assembly" % glyph_name |
| 706 |
) |
| 707 |
set_glyph_assembly(font, glyph_name, "") |
| 708 |
continue |
| 709 |
new_data = "".join(write_composite_info(glyph, glyph_order, data, vtt_version)) |
| 710 |
set_glyph_assembly(font, glyph_name, new_data) |
| 711 |
|
| 712 |
|
| 713 |
def compile_instructions(font, ship=True, keep_cvar=False): |
| 714 |
if "glyf" not in font: |
| 715 |
raise VTTLibError("Missing 'glyf' table; not a TrueType font") |
| 716 |
if "TSI1" not in font: |
| 717 |
raise VTTLibError("The font contains no 'TSI1' table") |
| 718 |
if keep_cvar and "cvar" not in font: |
| 719 |
raise VTTLibError( |
| 720 |
"The keep_cvar parameter is set, but the cvar table is missing from the font" |
| 721 |
) |
| 722 |
|
| 723 |
control_program = get_extra_assembly(font, "cvt") |
| 724 |
set_cvt_table(font, control_program) |
| 725 |
if "TSIC" in font and not keep_cvar: |
| 726 |
# Compile TSIC |
| 727 |
tsic = font["TSIC"].table |
| 728 |
cvts = font["cvt "].values |
| 729 |
# Gather the full set of cvts (not just those in TSIC) for all locations |
| 730 |
cvt_sets = [Vector(cvts)] |
| 731 |
for record in tsic.Record: |
| 732 |
cvt_local = {i: v for i, v in zip(record.CVTArray, record.CVTValueArray)} |
| 733 |
cvt_set = [ |
| 734 |
cvt_local[i] if i in cvt_local else cvt for i, cvt in enumerate(cvts) |
| 735 |
] |
| 736 |
cvt_sets.append(Vector(cvt_set)) |
| 737 |
|
| 738 |
# Compute variations |
| 739 |
locs = [{axis_tag: 0.0 for axis_tag in tsic.AxisArray}] # default instance |
| 740 |
locs += [ |
| 741 |
{axis_tag: val for axis_tag, val in zip(tsic.AxisArray, loc.Axis)} |
| 742 |
for loc in tsic.RecordLocations |
| 743 |
] |
| 744 |
model = models.VariationModel(locs) |
| 745 |
variations = [] |
| 746 |
deltas, supports = model.getDeltasAndSupports(cvt_sets) |
| 747 |
for i, (delta, support) in enumerate(zip(deltas[1:], supports[1:])): |
| 748 |
delta = [otRound(d) for d in delta] |
| 749 |
if all(abs(v) <= 0.5 for v in delta): |
| 750 |
continue |
| 751 |
# Remove any deltas that weren't specified in TSIC at this location |
| 752 |
# TODO: Just replace optimizaton with getting rid of 0 deltas? |
| 753 |
tsic_rec_index = model.reverseMapping[i + 1] - 1 # Map back to TSIC records |
| 754 |
delta = [ |
| 755 |
d if j in tsic.Record[tsic_rec_index].CVTArray and d != 0 else None |
| 756 |
for j, d in enumerate(delta) |
| 757 |
] |
| 758 |
var = TupleVariation(support, delta) |
| 759 |
variations.append(var) |
| 760 |
|
| 761 |
if variations: |
| 762 |
cvar = font["cvar"] = newTable("cvar") |
| 763 |
cvar.version = 1 |
| 764 |
cvar.variations = variations |
| 765 |
|
| 766 |
for tag in ("prep", "fpgm"): |
| 767 |
if tag not in font: |
| 768 |
font[tag] = newTable(tag) |
| 769 |
data = get_extra_assembly(font, tag) |
| 770 |
font[tag].program = make_program(data, tag) |
| 771 |
|
| 772 |
glyph_order = font.getGlyphOrder() |
| 773 |
glyf_table = font["glyf"] |
| 774 |
for glyph_name in glyph_order: |
| 775 |
try: |
| 776 |
data = get_glyph_assembly(font, glyph_name) |
| 777 |
except KeyError: |
| 778 |
continue |
| 779 |
program, components = make_glyph_program(data, glyph_name) |
| 780 |
if program or components: |
| 781 |
glyph = glyf_table[glyph_name] |
| 782 |
if components: |
| 783 |
if not glyph.isComposite(): |
| 784 |
log.warning( |
| 785 |
"Glyph '%s' contains components in VTT assembly but " |
| 786 |
"not in glyf table; drop assembly and skip " |
| 787 |
"compilation" % glyph_name |
| 788 |
) |
| 789 |
set_glyph_assembly(font, glyph_name, "") |
| 790 |
else: |
| 791 |
check_composite_info(glyph_name, glyph, components, glyph_order) |
| 792 |
set_components_flags(glyph, components) |
| 793 |
if program: |
| 794 |
glyph.program = program |
| 795 |
|
| 796 |
if ship: |
| 797 |
for tag in ("TSI%s" % i for i in (0, 1, 2, 3, 5, "C")): |
| 798 |
if tag in font: |
| 799 |
del font[tag] |
| 800 |
|
| 801 |
|
| 802 |
comment_re = r"/\*%s\*/[\r\n]*" |
| 803 |
# strip the timestamps |
| 804 |
gui_generated_re = re.compile(comment_re % (r" GUI generated .*?")) |
| 805 |
vtt_compiler_re = re.compile( # keep the VTT version |
| 806 |
comment_re % (r" (VTT [0-9]+\.[0-9][0-9A-Z]* compiler) .*?") |
| 807 |
) |
| 808 |
# strip glyph indexes |
| 809 |
glyph_re = re.compile(comment_re % (r" (?:TT|VTTTalk) glyph [0-9]+.*?")) |
| 810 |
|
| 811 |
|
| 812 |
def normalize_vtt_programs(font): |
| 813 |
for tag in ("cvt", "ppgm", "fpgm"): |
| 814 |
try: |
| 815 |
program = get_extra_assembly(font, tag) |
| 816 |
except KeyError: |
| 817 |
# extra program missing; nothing to normalize |
| 818 |
continue |
| 819 |
program = vtt_compiler_re.sub(r"/* \1 */\n", program) |
| 820 |
set_extra_assembly(font, tag, program) |
| 821 |
|
| 822 |
glyph_order = font.getGlyphOrder() |
| 823 |
for name in glyph_order: |
| 824 |
for is_talk in (True, False): |
| 825 |
try: |
| 826 |
program = get_vtt_program(font, name, is_talk, is_glyph=True) |
| 827 |
except KeyError: |
| 828 |
continue |
| 829 |
if is_talk: |
| 830 |
program = gui_generated_re.sub("", program) |
| 831 |
program = vtt_compiler_re.sub(r"/* \1 */\r", program) |
| 832 |
program = glyph_re.sub("", program) |
| 833 |
set_vtt_program(font, name, program, is_talk, is_glyph=True) |
| 834 |
|
| 835 |
if len(font["TSI3"].extraPrograms): |
| 836 |
# VTT sometimes stores 'reserved' data in TSI3 which isn't needed |
| 837 |
font["TSI3"].extraPrograms = {} |
| 838 |
|
| 839 |
|
| 840 |
def subset_vtt_glyph_programs(font, glyph_names): |
| 841 |
for tag in ("TSI1", "TSI3"): |
| 842 |
programs = font[tag].glyphPrograms |
| 843 |
for name in list(programs.keys()): |
| 844 |
if name not in glyph_names: |
| 845 |
del programs[name] |
| 846 |
|
| 847 |
groups = font["TSI5"].glyphGrouping |
| 848 |
for name in list(groups.keys()): |
| 849 |
if name not in glyph_names: |
| 850 |
del groups[name] |
| 851 |
|
| 852 |
|
| 853 |
def vtt_dump_file(infile, outfile=None, **_): |
| 854 |
"""Write VTT data from a TTF to a TTX dump.""" |
| 855 |
if not os.path.exists(infile): |
| 856 |
raise vttLib.VTTLibArgumentError("'%s' not found" % infile) |
| 857 |
|
| 858 |
if outfile is None: |
| 859 |
outfile = os.path.splitext(infile)[0] + "_VTT_Hinting.ttx" |
| 860 |
|
| 861 |
font = TTFont(infile) |
| 862 |
vttLib.transfer.dump_to_file(font, outfile) |
| 863 |
|
| 864 |
|
| 865 |
def vtt_merge_file(infile, outfile=None, keep_cvar=False, **_): |
| 866 |
"""Write VTT data from a TTX dump to a TTF.""" |
| 867 |
if not os.path.exists(infile): |
| 868 |
raise vttLib.VTTLibArgumentError("Input file '%s' not found" % infile) |
| 869 |
|
| 870 |
if not os.path.exists(outfile): |
| 871 |
raise vttLib.VTTLibArgumentError("Output file '%s' not found" % outfile) |
| 872 |
|
| 873 |
font = TTFont(outfile) |
| 874 |
vttLib.transfer.merge_from_file(font, infile, keep_cvar=keep_cvar) |
| 875 |
font.save(outfile) |
| 876 |
|
| 877 |
|
| 878 |
def vtt_move_ufo_data_to_file(infile, outfile=None, **_): |
| 879 |
"""Write VTT data from UFO data to a TTX dump.""" |
| 880 |
if not os.path.exists(infile) or not os.path.isdir(infile): |
| 881 |
raise vttLib.VTTLibArgumentError( |
| 882 |
"Input UFO '%s' not found or not a directory." % infile |
| 883 |
) |
| 884 |
|
| 885 |
if outfile is None: |
| 886 |
outfile = os.path.splitext(infile)[0] + "_VTT_Hinting.ttx" |
| 887 |
|
| 888 |
ufo = ufoLib2.Font.open(infile) |
| 889 |
vttLib.transfer.copy_from_ufo_data_to_file(ufo, outfile) |
| 890 |
ufo.save() |
| 891 |
|
| 892 |
|
| 893 |
def vtt_compile( |
| 894 |
infile, |
| 895 |
outfile=None, |
| 896 |
ship=False, |
| 897 |
inplace=None, |
| 898 |
force_overwrite=False, |
| 899 |
keep_cvar=False, |
| 900 |
**_, |
| 901 |
): |
| 902 |
if not os.path.exists(infile): |
| 903 |
raise vttLib.VTTLibArgumentError("Input TTF '%s' not found." % infile) |
| 904 |
|
| 905 |
font = TTFont(infile) |
| 906 |
|
| 907 |
if keep_cvar and "cvar" not in font: |
| 908 |
raise vttLib.VTTLibArgumentError( |
| 909 |
"The --keep-cvar option was specified, but no cvar table exists in the font" |
| 910 |
) |
| 911 |
|
| 912 |
if outfile: |
| 913 |
pass |
| 914 |
elif inplace: |
| 915 |
# create (and overwrite exising) backup first |
| 916 |
import shutil |
| 917 |
|
| 918 |
shutil.copyfile(infile, infile + inplace) |
| 919 |
outfile = infile |
| 920 |
elif force_overwrite: |
| 921 |
# save to input file (no backup) |
| 922 |
outfile = infile |
| 923 |
else: |
| 924 |
# create new unique output file |
| 925 |
from fontTools.ttx import makeOutputFileName |
| 926 |
|
| 927 |
outfile = makeOutputFileName(infile, None, ".ttf") |
| 928 |
|
| 929 |
vttLib.compile_instructions(font, ship=ship, keep_cvar=keep_cvar) |
| 930 |
font.save(outfile) |