Merge pull request #1067 from fonttools/varlib-mutator-cvar

Add interpolation of cvt/cvar table
This commit is contained in:
Jens Kutilek 2017-10-10 12:17:06 +02:00 committed by GitHub
commit f874cf6542
11 changed files with 128 additions and 0 deletions

View File

@ -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

View File

@ -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,30 @@ def _add_gvar(font, model, master_ttfs, tolerance=0.5, optimize=True):
gvar.variations[glyph].append(var)
def _add_cvar(font, model, master_ttfs, tolerance=0.5):
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
deltas = model.getDeltas(allCVTs)
supports = model.supports
for i,(delta,support) in enumerate(zip(deltas[1:], supports[1:])):
delta = [int(round(d)) for d in delta]
if all(abs(v) <= tolerance for v in delta):
continue
var = TupleVariation(support, delta)
cvar.variations.append(var)
def _add_HVAR(font, model, master_ttfs, axisTags):
log.info("Generating HVAR")
@ -856,6 +881,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

View File

@ -152,6 +152,21 @@ def main(args=None):
coordinates += GlyphCoordinates(delta) * scalar
_SetCoordinates(varfont, glyphname, coordinates)
# Interpolate cvt
if 'cvar' in varfont:
cvar = varfont['cvar']
cvt = varfont['cvt ']
deltas = {}
for var in cvar.variations:
scalar = supportScalar(loc, var.axes)
if not scalar: continue
for i, c in enumerate(var.coordinates):
if c is not None:
deltas[i] = deltas.get(i, 0) + scalar * c
for i, delta in deltas.items():
cvt[i] += int(round(delta))
print("Removing variable tables")
for tag in ('avar','cvar','fvar','gvar','HVAR','MVAR','VVAR','STAT'):
if tag in varfont:

View File

@ -149,6 +149,11 @@
</cmap_format_4>
</cmap>
<cvt>
<cv index="0" value="470"/>
<cv index="1" value="677"/>
</cvt>
<loca>
<!-- The 'loca' table will be calculated by the compiler -->
</loca>

View File

@ -149,6 +149,11 @@
</cmap_format_4>
</cmap>
<cvt>
<cv index="0" value="474"/>
<cv index="1" value="677"/>
</cvt>
<loca>
<!-- The 'loca' table will be calculated by the compiler -->
</loca>

View File

@ -149,6 +149,11 @@
</cmap_format_4>
</cmap>
<cvt>
<cv index="0" value="487"/>
<cv index="1" value="677"/>
</cvt>
<loca>
<!-- The 'loca' table will be calculated by the compiler -->
</loca>

View File

@ -149,6 +149,11 @@
</cmap_format_4>
</cmap>
<cvt>
<cv index="0" value="487"/>
<cv index="1" value="677"/>
</cvt>
<loca>
<!-- The 'loca' table will be calculated by the compiler -->
</loca>

View File

@ -149,6 +149,11 @@
</cmap_format_4>
</cmap>
<cvt>
<cv index="0" value="474"/>
<cv index="1" value="677"/>
</cvt>
<loca>
<!-- The 'loca' table will be calculated by the compiler -->
</loca>

View File

@ -128,6 +128,11 @@
</cmap_format_4>
</cmap>
<cvt>
<cv index="0" value="474"/>
<cv index="1" value="677"/>
</cvt>
<loca>
<!-- The 'loca' table will be calculated by the compiler -->
</loca>
@ -848,6 +853,20 @@
</ValueRecord>
</MVAR>
<cvar>
<version major="1" minor="0"/>
<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>
</cvar>
<fvar>
<!-- Weight -->

View File

@ -128,6 +128,11 @@
</cmap_format_4>
</cmap>
<cvt>
<cv index="0" value="474"/>
<cv index="1" value="677"/>
</cvt>
<loca>
<!-- The 'loca' table will be calculated by the compiler -->
</loca>

View File

@ -128,6 +128,11 @@
</cmap_format_4>
</cmap>
<cvt>
<cv index="0" value="477"/>
<cv index="1" value="677"/>
</cvt>
<loca>
<!-- The 'loca' table will be calculated by the compiler -->
</loca>