Implement building cvar tables
This commit is contained in:
parent
f0d5db9975
commit
5b39aa7af2
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -855,18 +855,32 @@
|
||||
|
||||
<cvar>
|
||||
<version major="1" minor="0"/>
|
||||
<tuple>
|
||||
<coord axis="cntr" value="1.0"/>
|
||||
<delta cvt="0" value="0"/>
|
||||
<delta cvt="1" value="0"/>
|
||||
</tuple>
|
||||
<tuple>
|
||||
<coord axis="wght" value="-1.0"/>
|
||||
<delta cvt="0" value="-4"/>
|
||||
<delta cvt="1" value="0"/>
|
||||
</tuple>
|
||||
<tuple>
|
||||
<coord axis="wght" value="1.0"/>
|
||||
<delta cvt="0" value="13"/>
|
||||
<delta cvt="1" value="0"/>
|
||||
</tuple>
|
||||
<tuple>
|
||||
<coord axis="wght" value="-1.0"/>
|
||||
<coord axis="cntr" value="1.0"/>
|
||||
<delta cvt="0" value="0"/>
|
||||
<delta cvt="1" value="0"/>
|
||||
</tuple>
|
||||
<tuple>
|
||||
<coord axis="wght" value="1.0"/>
|
||||
<coord axis="cntr" value="1.0"/>
|
||||
<delta cvt="0" value="13"/>
|
||||
<delta cvt="0" value="0"/>
|
||||
<delta cvt="1" value="0"/>
|
||||
</tuple>
|
||||
</cvar>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user