From 5b39aa7af276dde89e7291ae801cd65498e26ce5 Mon Sep 17 00:00:00 2001 From: Jens Kutilek Date: Mon, 9 Oct 2017 13:08:55 +0200 Subject: [PATCH] Implement building cvar tables --- Lib/fontTools/ttLib/tables/_c_v_t.py | 32 ++++++++++++++++++++ Lib/fontTools/varLib/__init__.py | 31 +++++++++++++++++++ Tests/varLib/data/test_results/BuildMain.ttx | 16 +++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/Lib/fontTools/ttLib/tables/_c_v_t.py b/Lib/fontTools/ttLib/tables/_c_v_t.py index 4fbee7bfc..15094f551 100644 --- a/Lib/fontTools/ttLib/tables/_c_v_t.py +++ b/Lib/fontTools/ttLib/tables/_c_v_t.py @@ -1,4 +1,5 @@ from __future__ import print_function, division, absolute_import +from numbers import Number from fontTools.misc.py23 import * from fontTools.misc.textTools import safeEval from . import DefaultTable @@ -47,3 +48,34 @@ class table__c_v_t(DefaultTable.DefaultTable): def __delitem__(self, index): del self.values[index] + + +class CVTValues(object): + """ This is used in varLib for calculating control value deltas""" + + def __init__(self, values): + self.values = list(values) + + def __getitem__(self, index): + return self.values[index] + + def __len__(self): + return len(self.values) + + def __repr__(self): + return u"CVTValues(%s)" % self.values + + def __isub__(self, other): + if isinstance(other, CVTValues): + assert len(self.values) == len(other) + self.values = [self.values[i] - other.values[i] for i in range(len(self.values))] + return self + if isinstance(other, Number): + self.values = [v - other for v in self.values] + return self + return NotImplemented + + def __mul__(self, other): + if isinstance(other, Number): + return CVTValues([v * other for v in self.values]) + return NotImplemented diff --git a/Lib/fontTools/varLib/__init__.py b/Lib/fontTools/varLib/__init__.py index 98e8f3ec7..102288cce 100644 --- a/Lib/fontTools/varLib/__init__.py +++ b/Lib/fontTools/varLib/__init__.py @@ -23,6 +23,7 @@ from __future__ import unicode_literals from fontTools.misc.py23 import * from fontTools.ttLib import TTFont, newTable from fontTools.ttLib.tables._n_a_m_e import NameRecord +from fontTools.ttLib.tables._c_v_t import CVTValues from fontTools.ttLib.tables._f_v_a_r import Axis, NamedInstance from fontTools.ttLib.tables._g_l_y_f import GlyphCoordinates from fontTools.ttLib.tables.TupleVariation import TupleVariation @@ -502,6 +503,34 @@ def _add_gvar(font, model, master_ttfs, tolerance=0.5, optimize=True): gvar.variations[glyph].append(var) +def _add_cvar(font, model, master_ttfs): + + log.info("Generating cvar") + assert "cvar" not in font + cvar = font["cvar"] = newTable('cvar') + cvar.version = 1 + cvar.variations = [] + + allCVTs = [CVTValues(m["cvt "].values) for m in master_ttfs] + num_cvts0 = len(allCVTs[0]) + + if (any(len(c) != num_cvts0 for c in allCVTs)): + log.warning("Master has incompatible cvt table, not building cvar table.") + del font["cvar"] + return + print(" allCVTs:", allCVTs) + deltas = model.getDeltas(allCVTs) + supports = model.supports + print(" Deltas: ", deltas) + #print(" Supports:", supports) + for i,(delta,support) in enumerate(zip(deltas[1:], supports[1:])): + print(" ", i) + print(" Delta: ", delta) + print(" Support:", support) + delta = [int(round(d)) for d in delta] + var = TupleVariation(support, delta) + cvar.variations.append(var) + def _add_HVAR(font, model, master_ttfs, axisTags): log.info("Generating HVAR") @@ -856,6 +885,8 @@ def build(designspace_filename, master_finder=lambda s:s): _merge_OTL(vf, model, master_fonts, axisTags) if 'glyf' in vf: _add_gvar(vf, model, master_fonts) + if 'cvt ' in vf: + _add_cvar(vf, model, master_fonts) return vf, model, master_ttfs diff --git a/Tests/varLib/data/test_results/BuildMain.ttx b/Tests/varLib/data/test_results/BuildMain.ttx index 2d87963d2..b6fd206d4 100644 --- a/Tests/varLib/data/test_results/BuildMain.ttx +++ b/Tests/varLib/data/test_results/BuildMain.ttx @@ -855,18 +855,32 @@ + + + + + + + + + + + + + - + +