Sparse cff2vf support (#1591)
* Added getter (in the form of a property decorator) for T2Charstring.vsindex. Fixes endless compile loop in some circumstances. Fixed bug in mutator: need to remove vsindex from snapshotted charstrings, plus formatting clean up * Fix for subsetting HVAR tables that have an AdvanceWidthMap when the --retain-gid option is used. Needed to make subset_test.py::test_retain_gids_cff2 tests pass. * in varLib/cffLib.py, add support for sparse sources, and sources with more than one model, and hence more than one VarData element in the VarStore. CFF2 source fonts with multiple FontDicts in the FDArray need some extra work. With sparse fonts, some of the source fonts may have a fewer FontDicts than the default font. The getfd_map function() builds a map from the FontDict indices in the default font to those in each region font. This is needed when building up the blend value lists in the master font FontDict PrivateDicts, in order to fetch PrivateDict values from the correct FontDict in each region font. In specializer.py, add support for CFF2 CharStrings with blend operators. 1) In generalizeCommands, convert a blend op to a list of args that are blend lists for the following regular operator. A blend list as a default font value, followed by the delta tuple. 2) In specializeCommands(), convert these back to blend ops, combining as many successive blend lists as allowed by the stack limit. Add test case for sparse CFF2 sources. The test font has 55 glyphs. 2 glyphs use only 2 sources (weight = 0 and 100). The rest use 4 source fonts: the two end points of the weight axis, and two intermediate masters. The intermediate masters are only 1 design space unit apart, and are used to change glyph design at the point in design space. For the rest, at most 2 glyphs use the same set of source fonts. There are 12 source fonts. Add test case for specializer programToCommands() and commandsToProgram by converting each CharString.program in the font to a command list, and back again, and comparing original and final versions.
This commit is contained in:
parent
9cbfef1972
commit
5b3db36670
@ -4,6 +4,7 @@
|
||||
|
||||
from __future__ import print_function, division, absolute_import
|
||||
from fontTools.misc.py23 import *
|
||||
from fontTools.cffLib import maxStackLimit
|
||||
|
||||
|
||||
def stringToProgram(string):
|
||||
@ -26,14 +27,21 @@ def programToString(program):
|
||||
return ' '.join(str(x) for x in program)
|
||||
|
||||
|
||||
def programToCommands(program):
|
||||
r"""Takes a T2CharString program list and returns list of commands.
|
||||
def programToCommands(program, getNumRegions=None):
|
||||
"""Takes a T2CharString program list and returns list of commands.
|
||||
Each command is a two-tuple of commandname,arg-list. The commandname might
|
||||
be empty string if no commandname shall be emitted (used for glyph width,
|
||||
hintmask/cntrmask argument, as well as stray arguments at the end of the
|
||||
program (¯\_(ツ)_/¯)."""
|
||||
program (¯\_(ツ)_/¯).
|
||||
'getNumRegions' may be None, or a callable object. It must return the
|
||||
number of regions. 'getNumRegions' takes a single argument, vsindex. If
|
||||
the vsindex argument is None, getNumRegions returns the default number
|
||||
of regions for the charstring, else it returns the numRegions for
|
||||
the vsindex."""
|
||||
|
||||
width = None
|
||||
seenWidthOp = False
|
||||
vsIndex = None
|
||||
commands = []
|
||||
stack = []
|
||||
it = iter(program)
|
||||
@ -42,10 +50,37 @@ def programToCommands(program):
|
||||
stack.append(token)
|
||||
continue
|
||||
|
||||
if width is None and token in {'hstem', 'hstemhm', 'vstem', 'vstemhm',
|
||||
'cntrmask', 'hintmask',
|
||||
'hmoveto', 'vmoveto', 'rmoveto',
|
||||
'endchar'}:
|
||||
if token == 'blend':
|
||||
assert getNumRegions is not None
|
||||
numSourceFonts = 1 + getNumRegions(vsIndex)
|
||||
# replace the blend op args on the stack with a single list
|
||||
# containing all the blend op args.
|
||||
numBlendOps = stack[-1] * numSourceFonts + 1
|
||||
# replace first blend op by a list of the blend ops.
|
||||
stack[-numBlendOps:] = [stack[-numBlendOps:]]
|
||||
|
||||
# Check for width.
|
||||
if not seenWidthOp:
|
||||
seenWidthOp = True
|
||||
widthLen = len(stack) - numBlendOps
|
||||
if widthLen and (widthLen % 2):
|
||||
stack.pop(0)
|
||||
elif width is not None:
|
||||
commands.pop(0)
|
||||
width = None
|
||||
# We do NOT add the width to the command list if a blend is seen:
|
||||
# if a blend op exists, this is or will be a CFF2 charstring.
|
||||
continue
|
||||
|
||||
elif token == 'vsindex':
|
||||
vsIndex = stack[-1]
|
||||
assert type(vsIndex) is int
|
||||
|
||||
elif (not seenWidthOp) and token in {'hstem', 'hstemhm', 'vstem', 'vstemhm',
|
||||
'cntrmask', 'hintmask',
|
||||
'hmoveto', 'vmoveto', 'rmoveto',
|
||||
'endchar'}:
|
||||
seenWidthOp = True
|
||||
parity = token in {'hmoveto', 'vmoveto'}
|
||||
if stack and (len(stack) % 2) ^ parity:
|
||||
width = stack.pop(0)
|
||||
@ -64,11 +99,23 @@ def programToCommands(program):
|
||||
return commands
|
||||
|
||||
|
||||
def _flattenBlendArgs(args):
|
||||
token_list = []
|
||||
for arg in args:
|
||||
if isinstance(arg, list):
|
||||
token_list.extend(arg)
|
||||
token_list.append('blend')
|
||||
else:
|
||||
token_list.append(arg)
|
||||
return token_list
|
||||
|
||||
def commandsToProgram(commands):
|
||||
"""Takes a commands list as returned by programToCommands() and converts
|
||||
it back to a T2CharString program list."""
|
||||
program = []
|
||||
for op,args in commands:
|
||||
if any(isinstance(arg, list) for arg in args):
|
||||
args = _flattenBlendArgs(args)
|
||||
program.extend(args)
|
||||
if op:
|
||||
program.append(op)
|
||||
@ -203,11 +250,58 @@ class _GeneralizerDecombinerCommandsMap(object):
|
||||
yield ('rlineto', args)
|
||||
yield ('rrcurveto', last_args)
|
||||
|
||||
def _convertBlendOpToArgs(blendList):
|
||||
# args is list of blend op args. Since we are supporting
|
||||
# recursive blend op calls, some of these args may also
|
||||
# be a list of blend op args, and need to be converted before
|
||||
# we convert the current list.
|
||||
if any([isinstance(arg, list) for arg in blendList]):
|
||||
args = [i for e in blendList for i in
|
||||
(_convertBlendOpToArgs(e) if isinstance(e,list) else [e]) ]
|
||||
else:
|
||||
args = blendList
|
||||
|
||||
# We now know that blendList contains a blend op argument list, even if
|
||||
# some of the args are lists that each contain a blend op argument list.
|
||||
# Convert from:
|
||||
# [default font arg sequence x0,...,xn] + [delta tuple for x0] + ... + [delta tuple for xn]
|
||||
# to:
|
||||
# [ [x0] + [delta tuple for x0],
|
||||
# ...,
|
||||
# [xn] + [delta tuple for xn] ]
|
||||
numBlends = args[-1]
|
||||
# Can't use args.pop() when the args are being used in a nested list
|
||||
# comprehension. See calling context
|
||||
args = args[:-1]
|
||||
|
||||
numRegions = len(args)//numBlends - 1
|
||||
if not (numBlends*(numRegions + 1) == len(args)):
|
||||
raise ValueError(blendList)
|
||||
|
||||
defaultArgs = [[arg] for arg in args[:numBlends]]
|
||||
deltaArgs = args[numBlends:]
|
||||
numDeltaValues = len(deltaArgs)
|
||||
deltaList = [ deltaArgs[i:i + numRegions] for i in range(0, numDeltaValues, numRegions) ]
|
||||
blend_args = [ a + b for a, b in zip(defaultArgs,deltaList)]
|
||||
return blend_args
|
||||
|
||||
def generalizeCommands(commands, ignoreErrors=False):
|
||||
result = []
|
||||
mapping = _GeneralizerDecombinerCommandsMap
|
||||
for op,args in commands:
|
||||
for op, args in commands:
|
||||
# First, generalize any blend args in the arg list.
|
||||
if any([isinstance(arg, list) for arg in args]):
|
||||
try:
|
||||
args = [n for arg in args for n in (_convertBlendOpToArgs(arg) if isinstance(arg, list) else [arg])]
|
||||
except ValueError:
|
||||
if ignoreErrors:
|
||||
# Store op as data, such that consumers of commands do not have to
|
||||
# deal with incorrect number of arguments.
|
||||
result.append(('', args))
|
||||
result.append(('', [op]))
|
||||
else:
|
||||
raise
|
||||
|
||||
func = getattr(mapping, op, None)
|
||||
if not func:
|
||||
result.append((op,args))
|
||||
@ -225,8 +319,8 @@ def generalizeCommands(commands, ignoreErrors=False):
|
||||
raise
|
||||
return result
|
||||
|
||||
def generalizeProgram(program, **kwargs):
|
||||
return commandsToProgram(generalizeCommands(programToCommands(program), **kwargs))
|
||||
def generalizeProgram(program, getNumRegions=None, **kwargs):
|
||||
return commandsToProgram(generalizeCommands(programToCommands(program, getNumRegions), **kwargs))
|
||||
|
||||
|
||||
def _categorizeVector(v):
|
||||
@ -267,6 +361,70 @@ def _negateCategory(a):
|
||||
assert a in '0r'
|
||||
return a
|
||||
|
||||
def _convertToBlendCmds(args):
|
||||
# return a list of blend commands, and
|
||||
# the remaining non-blended args, if any.
|
||||
num_args = len(args)
|
||||
stack_use = 0
|
||||
new_args = []
|
||||
i = 0
|
||||
while i < num_args:
|
||||
arg = args[i]
|
||||
if not isinstance(arg, list):
|
||||
new_args.append(arg)
|
||||
i += 1
|
||||
stack_use += 1
|
||||
else:
|
||||
prev_stack_use = stack_use
|
||||
# The arg is a tuple of blend values.
|
||||
# These are each (master 0,delta 1..delta n)
|
||||
# Combine as many successive tuples as we can,
|
||||
# up to the max stack limit.
|
||||
num_sources = len(arg)
|
||||
blendlist = [arg]
|
||||
i += 1
|
||||
stack_use += 1 + num_sources # 1 for the num_blends arg
|
||||
while (i < num_args) and isinstance(args[i], list):
|
||||
blendlist.append(args[i])
|
||||
i += 1
|
||||
stack_use += num_sources
|
||||
if stack_use + num_sources > maxStackLimit:
|
||||
# if we are here, max stack is the CFF2 max stack.
|
||||
# I use the CFF2 max stack limit here rather than
|
||||
# the 'maxstack' chosen by the client, as the default
|
||||
# maxstack may have been used unintentionally. For all
|
||||
# the other operators, this just produces a little less
|
||||
# optimization, but here it puts a hard (and low) limit
|
||||
# on the number of source fonts that can be used.
|
||||
break
|
||||
# blendList now contains as many single blend tuples as can be
|
||||
# combined without exceeding the CFF2 stack limit.
|
||||
num_blends = len(blendlist)
|
||||
# append the 'num_blends' default font values
|
||||
blend_args = []
|
||||
for arg in blendlist:
|
||||
blend_args.append(arg[0])
|
||||
for arg in blendlist:
|
||||
blend_args.extend(arg[1:])
|
||||
blend_args.append(num_blends)
|
||||
new_args.append(blend_args)
|
||||
stack_use = prev_stack_use + num_blends
|
||||
|
||||
return new_args
|
||||
|
||||
def _addArgs(a, b):
|
||||
if isinstance(b, list):
|
||||
if isinstance(a, list):
|
||||
if len(a) != len(b):
|
||||
raise ValueError()
|
||||
return [_addArgs(va, vb) for va,vb in zip(a, b)]
|
||||
else:
|
||||
a, b = b, a
|
||||
if isinstance(a, list):
|
||||
return [_addArgs(a[0], b)] + a[1:]
|
||||
return a + b
|
||||
|
||||
|
||||
def specializeCommands(commands,
|
||||
ignoreErrors=False,
|
||||
generalizeFirst=True,
|
||||
@ -302,6 +460,8 @@ def specializeCommands(commands,
|
||||
# I have convinced myself that this produces optimal bytecode (except for, possibly
|
||||
# one byte each time maxstack size prohibits combining.) YMMV, but you'd be wrong. :-)
|
||||
# A dynamic-programming approach can do the same but would be significantly slower.
|
||||
#
|
||||
# 7. For any args which are blend lists, convert them to a blend command.
|
||||
|
||||
|
||||
# 0. Generalize commands.
|
||||
@ -417,12 +577,18 @@ def specializeCommands(commands,
|
||||
continue
|
||||
|
||||
# Merge adjacent hlineto's and vlineto's.
|
||||
# In CFF2 charstrings from variable fonts, each
|
||||
# arg item may be a list of blendable values, one from
|
||||
# each source font.
|
||||
if (i and op in {'hlineto', 'vlineto'} and
|
||||
(op == commands[i-1][0]) and
|
||||
(not isinstance(args[0], list))):
|
||||
(op == commands[i-1][0])):
|
||||
_, other_args = commands[i-1]
|
||||
assert len(args) == 1 and len(other_args) == 1
|
||||
commands[i-1] = (op, [other_args[0]+args[0]])
|
||||
try:
|
||||
new_args = [_addArgs(args[0], other_args[0])]
|
||||
except ValueError:
|
||||
continue
|
||||
commands[i-1] = (op, new_args)
|
||||
del commands[i]
|
||||
continue
|
||||
|
||||
@ -534,10 +700,16 @@ def specializeCommands(commands,
|
||||
commands[i] = op0+op1+'curveto', args
|
||||
continue
|
||||
|
||||
# 7. For any series of args which are blend lists, convert the series to a single blend arg.
|
||||
for i in range(len(commands)):
|
||||
op, args = commands[i]
|
||||
if any(isinstance(arg, list) for arg in args):
|
||||
commands[i] = op, _convertToBlendCmds(args)
|
||||
|
||||
return commands
|
||||
|
||||
def specializeProgram(program, **kwargs):
|
||||
return commandsToProgram(specializeCommands(programToCommands(program), **kwargs))
|
||||
def specializeProgram(program, getNumRegions=None, **kwargs):
|
||||
return commandsToProgram(specializeCommands(programToCommands(program, getNumRegions), **kwargs))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@ -554,4 +726,3 @@ if __name__ == '__main__':
|
||||
assert program == program2
|
||||
print("Generalized program:"); print(programToString(generalizeProgram(program)))
|
||||
print("Specialized program:"); print(programToString(specializeProgram(program)))
|
||||
|
||||
|
@ -944,6 +944,16 @@ class T2CharString(object):
|
||||
self.program = program
|
||||
self.private = private
|
||||
self.globalSubrs = globalSubrs if globalSubrs is not None else []
|
||||
self._cur_vsindex = None
|
||||
|
||||
def getNumRegions(self, vsindex=None):
|
||||
pd = self.private
|
||||
assert(pd is not None)
|
||||
if vsindex is not None:
|
||||
self._cur_vsindex = vsindex
|
||||
elif self._cur_vsindex is None:
|
||||
self._cur_vsindex = pd.vsindex if hasattr(pd, 'vsindex') else 0
|
||||
return pd.getNumRegions(self._cur_vsindex)
|
||||
|
||||
def __repr__(self):
|
||||
if self.bytecode is None:
|
||||
|
@ -759,12 +759,11 @@ _DesignSpaceData = namedtuple(
|
||||
|
||||
|
||||
def _add_CFF2(varFont, model, master_fonts):
|
||||
from .cff import (convertCFFtoCFF2, addCFFVarStore, merge_region_fonts)
|
||||
from .cff import (convertCFFtoCFF2, merge_region_fonts)
|
||||
glyphOrder = varFont.getGlyphOrder()
|
||||
convertCFFtoCFF2(varFont)
|
||||
ordered_fonts_list = model.reorderMasters(master_fonts, model.reverseMapping)
|
||||
# re-ordering the master list simplifies building the CFF2 data item lists.
|
||||
addCFFVarStore(varFont, model) # Add VarStore to the CFF2 font.
|
||||
merge_region_fonts(varFont, model, ordered_fonts_list, glyphOrder)
|
||||
|
||||
|
||||
|
522
Lib/fontTools/varLib/cff.py
Normal file → Executable file
522
Lib/fontTools/varLib/cff.py
Normal file → Executable file
@ -1,7 +1,5 @@
|
||||
from collections import namedtuple
|
||||
import os
|
||||
from fontTools.misc.py23 import BytesIO
|
||||
from fontTools.misc.psCharStrings import T2CharString, T2OutlineExtractor
|
||||
from fontTools.pens.t2CharStringPen import T2CharStringPen, t2c_round
|
||||
from fontTools.cffLib import (
|
||||
maxStackLimit,
|
||||
TopDictIndex,
|
||||
@ -14,20 +12,21 @@ from fontTools.cffLib import (
|
||||
FontDict,
|
||||
VarStoreData
|
||||
)
|
||||
from fontTools.cffLib.specializer import (commandsToProgram, specializeCommands)
|
||||
from fontTools.misc.py23 import BytesIO
|
||||
from fontTools.cffLib.specializer import (
|
||||
specializeCommands, commandsToProgram)
|
||||
from fontTools.ttLib import newTable
|
||||
from fontTools import varLib
|
||||
from fontTools.varLib.models import allEqual
|
||||
from fontTools.misc.psCharStrings import T2CharString, T2OutlineExtractor
|
||||
from fontTools.pens.t2CharStringPen import T2CharStringPen, t2c_round
|
||||
|
||||
|
||||
def addCFFVarStore(varFont, varModel):
|
||||
supports = varModel.supports[1:]
|
||||
def addCFFVarStore(varFont, varModel, varDataList, masterSupports):
|
||||
fvarTable = varFont['fvar']
|
||||
axisKeys = [axis.axisTag for axis in fvarTable.axes]
|
||||
varTupleList = varLib.builder.buildVarRegionList(supports, axisKeys)
|
||||
varTupleIndexes = list(range(len(supports)))
|
||||
varDeltasCFFV = varLib.builder.buildVarData(varTupleIndexes, None, False)
|
||||
varStoreCFFV = varLib.builder.buildVarStore(varTupleList, [varDeltasCFFV])
|
||||
varTupleList = varLib.builder.buildVarRegionList(masterSupports, axisKeys)
|
||||
varStoreCFFV = varLib.builder.buildVarStore(varTupleList, varDataList)
|
||||
|
||||
topDict = varFont['CFF2'].cff.topDictIndex[0]
|
||||
topDict.VarStore = VarStoreData(otVarStore=varStoreCFFV)
|
||||
@ -143,16 +142,61 @@ pd_blend_fields = ("BlueValues", "OtherBlues", "FamilyBlues",
|
||||
"StemSnapV")
|
||||
|
||||
|
||||
def merge_PrivateDicts(topDict, region_top_dicts, num_masters, var_model):
|
||||
def get_private(regionFDArrays, fd_index, ri, fd_map):
|
||||
region_fdArray = regionFDArrays[ri]
|
||||
region_fd_map = fd_map[fd_index]
|
||||
if ri in region_fd_map:
|
||||
region_fdIndex = region_fd_map[ri]
|
||||
private = region_fdArray[region_fdIndex].Private
|
||||
else:
|
||||
private = None
|
||||
return private
|
||||
|
||||
|
||||
def merge_PrivateDicts(top_dicts, vsindex_dict, var_model, fd_map):
|
||||
"""
|
||||
I step through the FontDicts in the FDArray of the varfont TopDict.
|
||||
For each varfont FontDict:
|
||||
step through each key in FontDict.Private.
|
||||
For each key, step through each relevant source font Private dict, and
|
||||
build a list of values to blend.
|
||||
The 'relevant' source fonts are selected by first getting the right
|
||||
submodel using model_keys[vsindex]. The indices of the
|
||||
subModel.locations are mapped to source font list indices by
|
||||
assuming the latter order is the same as the order of the
|
||||
var_model.locations. I can then get the index of each subModel
|
||||
location in the list of var_model.locations.
|
||||
"""
|
||||
|
||||
topDict = top_dicts[0]
|
||||
region_top_dicts = top_dicts[1:]
|
||||
if hasattr(region_top_dicts[0], 'FDArray'):
|
||||
regionFDArrays = [fdTopDict.FDArray for fdTopDict in region_top_dicts]
|
||||
else:
|
||||
regionFDArrays = [[fdTopDict] for fdTopDict in region_top_dicts]
|
||||
for fd_index, font_dict in enumerate(topDict.FDArray):
|
||||
private_dict = font_dict.Private
|
||||
pds = [private_dict] + [
|
||||
regionFDArray[fd_index].Private for regionFDArray in regionFDArrays
|
||||
]
|
||||
vsindex = getattr(private_dict, 'vsindex', 0)
|
||||
# At the moment, no PrivateDict has a vsindex key, but let's support
|
||||
# how it should work. See comment at end of
|
||||
# merge_charstrings() - still need to optimize use of vsindex.
|
||||
sub_model, model_keys = vsindex_dict[vsindex]
|
||||
master_indices = []
|
||||
for loc in sub_model.locations[1:]:
|
||||
i = var_model.locations.index(loc) - 1
|
||||
master_indices.append(i)
|
||||
pds = [private_dict]
|
||||
last_pd = private_dict
|
||||
for ri in master_indices:
|
||||
pd = get_private(regionFDArrays, fd_index, ri, fd_map)
|
||||
# If the region font doesn't have this FontDict, just reference
|
||||
# the last one used.
|
||||
if pd is None:
|
||||
pd = last_pd
|
||||
else:
|
||||
last_pd = pd
|
||||
pds.append(pd)
|
||||
num_masters = len(pds)
|
||||
for key, value in private_dict.rawDict.items():
|
||||
if key not in pd_blend_fields:
|
||||
continue
|
||||
@ -192,7 +236,7 @@ def merge_PrivateDicts(topDict, region_top_dicts, num_masters, var_model):
|
||||
if (not any_points_differ) and not allEqual(rel_list):
|
||||
any_points_differ = True
|
||||
prev_val_list = val_list
|
||||
deltas = var_model.getDeltas(rel_list)
|
||||
deltas = sub_model.getDeltas(rel_list)
|
||||
# Convert numbers with no decimal part to an int.
|
||||
deltas = [conv_to_int(delta) for delta in deltas]
|
||||
# For PrivateDict BlueValues, the default font
|
||||
@ -206,61 +250,159 @@ def merge_PrivateDicts(topDict, region_top_dicts, num_masters, var_model):
|
||||
else:
|
||||
values = [pd.rawDict[key] for pd in pds]
|
||||
if not allEqual(values):
|
||||
dataList = var_model.getDeltas(values)
|
||||
dataList = sub_model.getDeltas(values)
|
||||
else:
|
||||
dataList = values[0]
|
||||
private_dict.rawDict[key] = dataList
|
||||
|
||||
|
||||
def getfd_map(varFont, fonts_list):
|
||||
""" Since a subset source font may have fewer FontDicts in their
|
||||
FDArray than the default font, we have to match up the FontDicts in
|
||||
the different fonts . We do this with the FDSelect array, and by
|
||||
assuming that the same glyph will reference matching FontDicts in
|
||||
each source font. We return a mapping from fdIndex in the default
|
||||
font to a dictionary which maps each master list index of each
|
||||
region font to the equivalent fdIndex in the region font."""
|
||||
fd_map = {}
|
||||
default_font = fonts_list[0]
|
||||
region_fonts = fonts_list[1:]
|
||||
num_regions = len(region_fonts)
|
||||
topDict = default_font['CFF '].cff.topDictIndex[0]
|
||||
if not hasattr(topDict, 'FDSelect'):
|
||||
fd_map[0] = [0]*num_regions
|
||||
return fd_map
|
||||
|
||||
gname_mapping = {}
|
||||
default_fdSelect = topDict.FDSelect
|
||||
glyphOrder = default_font.getGlyphOrder()
|
||||
for gid, fdIndex in enumerate(default_fdSelect):
|
||||
gname_mapping[glyphOrder[gid]] = fdIndex
|
||||
if fdIndex not in fd_map:
|
||||
fd_map[fdIndex] = {}
|
||||
for ri, region_font in enumerate(region_fonts):
|
||||
region_glyphOrder = region_font.getGlyphOrder()
|
||||
region_topDict = region_font['CFF '].cff.topDictIndex[0]
|
||||
if not hasattr(region_topDict, 'FDSelect'):
|
||||
# All the glyphs share the same FontDict. Pick any glyph.
|
||||
default_fdIndex = gname_mapping[region_glyphOrder[0]]
|
||||
fd_map[default_fdIndex][ri] = 0
|
||||
else:
|
||||
region_fdSelect = region_topDict.FDSelect
|
||||
for gid, fdIndex in enumerate(region_fdSelect):
|
||||
default_fdIndex = gname_mapping[region_glyphOrder[gid]]
|
||||
region_map = fd_map[default_fdIndex]
|
||||
if ri not in region_map:
|
||||
region_map[ri] = fdIndex
|
||||
return fd_map
|
||||
|
||||
|
||||
CVarData = namedtuple('CVarData', 'varDataList masterSupports vsindex_dict')
|
||||
def merge_region_fonts(varFont, model, ordered_fonts_list, glyphOrder):
|
||||
topDict = varFont['CFF2'].cff.topDictIndex[0]
|
||||
default_charstrings = topDict.CharStrings
|
||||
region_fonts = ordered_fonts_list[1:]
|
||||
region_top_dicts = [
|
||||
ttFont['CFF '].cff.topDictIndex[0] for ttFont in region_fonts
|
||||
]
|
||||
top_dicts = [topDict] + [
|
||||
ttFont['CFF '].cff.topDictIndex[0]
|
||||
for ttFont in ordered_fonts_list[1:]
|
||||
]
|
||||
num_masters = len(model.mapping)
|
||||
merge_PrivateDicts(topDict, region_top_dicts, num_masters, model)
|
||||
merge_charstrings(default_charstrings,
|
||||
glyphOrder,
|
||||
num_masters,
|
||||
region_top_dicts, model)
|
||||
cvData = merge_charstrings(glyphOrder, num_masters, top_dicts, model)
|
||||
fd_map = getfd_map(varFont, ordered_fonts_list)
|
||||
merge_PrivateDicts(top_dicts, cvData.vsindex_dict, model, fd_map)
|
||||
addCFFVarStore(varFont, model, cvData.varDataList,
|
||||
cvData.masterSupports)
|
||||
|
||||
|
||||
def merge_charstrings(default_charstrings,
|
||||
glyphOrder,
|
||||
num_masters,
|
||||
region_top_dicts,
|
||||
var_model):
|
||||
for gname in glyphOrder:
|
||||
default_charstring = default_charstrings[gname]
|
||||
def _get_cs(charstrings, glyphName):
|
||||
if glyphName not in charstrings:
|
||||
return None
|
||||
return charstrings[glyphName]
|
||||
|
||||
|
||||
def merge_charstrings(glyphOrder, num_masters, top_dicts, masterModel):
|
||||
|
||||
vsindex_dict = {}
|
||||
vsindex_by_key = {}
|
||||
varDataList = []
|
||||
masterSupports = []
|
||||
default_charstrings = top_dicts[0].CharStrings
|
||||
for gid, gname in enumerate(glyphOrder):
|
||||
all_cs = [
|
||||
_get_cs(td.CharStrings, gname)
|
||||
for td in top_dicts]
|
||||
if len([gs for gs in all_cs if gs is not None]) == 1:
|
||||
continue
|
||||
model, model_cs = masterModel.getSubModel(all_cs)
|
||||
# create the first pass CFF2 charstring, from
|
||||
# the default charstring.
|
||||
default_charstring = model_cs[0]
|
||||
var_pen = CFF2CharStringMergePen([], gname, num_masters, 0)
|
||||
default_charstring.outlineExtractor = CFFToCFF2OutlineExtractor
|
||||
# We need to override outlineExtractor because these
|
||||
# charstrings do have widths in the 'program'; we need to drop these
|
||||
# values rather than post assertion error for them.
|
||||
default_charstring.outlineExtractor = MergeOutlineExtractor
|
||||
default_charstring.draw(var_pen)
|
||||
for region_idx, region_td in enumerate(region_top_dicts, start=1):
|
||||
region_charstrings = region_td.CharStrings
|
||||
region_charstring = region_charstrings[gname]
|
||||
|
||||
# Add the coordinates from all the other regions to the
|
||||
# blend lists in the CFF2 charstring.
|
||||
region_cs = model_cs[1:]
|
||||
for region_idx, region_charstring in enumerate(region_cs, start=1):
|
||||
var_pen.restart(region_idx)
|
||||
region_charstring.outlineExtractor = MergeOutlineExtractor
|
||||
region_charstring.draw(var_pen)
|
||||
new_charstring = var_pen.getCharString(
|
||||
|
||||
# Collapse each coordinate list to a blend operator and its args.
|
||||
new_cs = var_pen.getCharString(
|
||||
private=default_charstring.private,
|
||||
globalSubrs=default_charstring.globalSubrs,
|
||||
var_model=var_model, optimize=True)
|
||||
default_charstrings[gname] = new_charstring
|
||||
var_model=model, optimize=True)
|
||||
default_charstrings[gname] = new_cs
|
||||
|
||||
if (not var_pen.seen_moveto) or ('blend' not in new_cs.program):
|
||||
# If this is not a marking glyph, or if there are no blend
|
||||
# arguments, then we can use vsindex 0. No need to
|
||||
# check if we need a new vsindex.
|
||||
continue
|
||||
|
||||
# If the charstring required a new model, create
|
||||
# a VarData table to go with, and set vsindex.
|
||||
try:
|
||||
key = tuple(v is not None for v in all_cs)
|
||||
vsindex = vsindex_by_key[key]
|
||||
except KeyError:
|
||||
varTupleIndexes = []
|
||||
for support in model.supports[1:]:
|
||||
if support not in masterSupports:
|
||||
masterSupports.append(support)
|
||||
varTupleIndexes.append(masterSupports.index(support))
|
||||
var_data = varLib.builder.buildVarData(varTupleIndexes, None, False)
|
||||
vsindex = len(vsindex_dict)
|
||||
vsindex_by_key[key] = vsindex
|
||||
vsindex_dict[vsindex] = (model, [key])
|
||||
varDataList.append(var_data)
|
||||
# We do not need to check for an existing new_cs.private.vsindex,
|
||||
# as we know it doesn't exist yet.
|
||||
if vsindex != 0:
|
||||
new_cs.program[:0] = [vsindex, 'vsindex']
|
||||
cvData = CVarData(varDataList=varDataList, masterSupports=masterSupports,
|
||||
vsindex_dict=vsindex_dict)
|
||||
# XXX To do: optimize use of vsindex between the PrivateDicts and
|
||||
# charstrings
|
||||
return cvData
|
||||
|
||||
|
||||
class MergeTypeError(TypeError):
|
||||
def __init__(self, point_type, pt_index, m_index, default_type, glyphName):
|
||||
self.error_msg = [
|
||||
"In glyph '{gname}' "
|
||||
"'{point_type}' at point index {pt_index} in master "
|
||||
"index {m_index} differs from the default font point "
|
||||
"type '{default_type}'"
|
||||
"".format(gname=glyphName,
|
||||
point_type=point_type, pt_index=pt_index,
|
||||
m_index=m_index, default_type=default_type)
|
||||
][0]
|
||||
super(MergeTypeError, self).__init__(self.error_msg)
|
||||
self.error_msg = [
|
||||
"In glyph '{gname}' "
|
||||
"'{point_type}' at point index {pt_index} in master "
|
||||
"index {m_index} differs from the default font point "
|
||||
"type '{default_type}'"
|
||||
"".format(
|
||||
gname=glyphName,
|
||||
point_type=point_type, pt_index=pt_index,
|
||||
m_index=m_index, default_type=default_type)
|
||||
][0]
|
||||
super(MergeTypeError, self).__init__(self.error_msg)
|
||||
|
||||
|
||||
def makeRoundNumberFunc(tolerance):
|
||||
@ -274,10 +416,9 @@ def makeRoundNumberFunc(tolerance):
|
||||
|
||||
|
||||
class CFFToCFF2OutlineExtractor(T2OutlineExtractor):
|
||||
""" This class is used to remove the initial width
|
||||
from the CFF charstring without adding the width
|
||||
to self.nominalWidthX, which is None.
|
||||
"""
|
||||
""" This class is used to remove the initial width from the CFF
|
||||
charstring without trying to add the width to self.nominalWidthX,
|
||||
which is None. """
|
||||
def popallWidth(self, evenOdd=0):
|
||||
args = self.popall()
|
||||
if not self.gotWidth:
|
||||
@ -288,60 +429,127 @@ class CFFToCFF2OutlineExtractor(T2OutlineExtractor):
|
||||
return args
|
||||
|
||||
|
||||
class MergeOutlineExtractor(CFFToCFF2OutlineExtractor):
|
||||
""" Used to extract the charstring commands - including hints - from a
|
||||
CFF charstring in order to merge it as another set of region data
|
||||
into a CFF2 variable font charstring."""
|
||||
|
||||
def __init__(self, pen, localSubrs, globalSubrs,
|
||||
nominalWidthX, defaultWidthX, private=None):
|
||||
super(CFFToCFF2OutlineExtractor, self).__init__(pen, localSubrs,
|
||||
globalSubrs, nominalWidthX, defaultWidthX, private)
|
||||
|
||||
def countHints(self):
|
||||
args = self.popallWidth()
|
||||
self.hintCount = self.hintCount + len(args) // 2
|
||||
return args
|
||||
|
||||
def _hint_op(self, type, args):
|
||||
self.pen.add_hint(type, args)
|
||||
|
||||
def op_hstem(self, index):
|
||||
args = self.countHints()
|
||||
self._hint_op('hstem', args)
|
||||
|
||||
def op_vstem(self, index):
|
||||
args = self.countHints()
|
||||
self._hint_op('vstem', args)
|
||||
|
||||
def op_hstemhm(self, index):
|
||||
args = self.countHints()
|
||||
self._hint_op('hstemhm', args)
|
||||
|
||||
def op_vstemhm(self, index):
|
||||
args = self.countHints()
|
||||
self._hint_op('vstemhm', args)
|
||||
|
||||
def _get_hintmask(self, index):
|
||||
if not self.hintMaskBytes:
|
||||
args = self.countHints()
|
||||
if args:
|
||||
self._hint_op('vstemhm', args)
|
||||
self.hintMaskBytes = (self.hintCount + 7) // 8
|
||||
hintMaskBytes, index = self.callingStack[-1].getBytes(index,
|
||||
self.hintMaskBytes)
|
||||
return index, hintMaskBytes
|
||||
|
||||
def op_hintmask(self, index):
|
||||
index, hintMaskBytes = self._get_hintmask(index)
|
||||
self.pen.add_hintmask('hintmask', [hintMaskBytes])
|
||||
return hintMaskBytes, index
|
||||
|
||||
def op_cntrmask(self, index):
|
||||
index, hintMaskBytes = self._get_hintmask(index)
|
||||
self.pen.add_hintmask('cntrmask', [hintMaskBytes])
|
||||
return hintMaskBytes, index
|
||||
|
||||
|
||||
class CFF2CharStringMergePen(T2CharStringPen):
|
||||
"""Pen to merge Type 2 CharStrings.
|
||||
"""
|
||||
def __init__(self, default_commands,
|
||||
glyphName, num_masters, master_idx, roundTolerance=0.5):
|
||||
def __init__(
|
||||
self, default_commands, glyphName, num_masters, master_idx,
|
||||
roundTolerance=0.5):
|
||||
super(
|
||||
CFF2CharStringMergePen,
|
||||
self).__init__(width=None,
|
||||
glyphSet=None, CFF2=True,
|
||||
roundTolerance=roundTolerance)
|
||||
self).__init__(
|
||||
width=None,
|
||||
glyphSet=None, CFF2=True,
|
||||
roundTolerance=roundTolerance)
|
||||
self.pt_index = 0
|
||||
self._commands = default_commands
|
||||
self.m_index = master_idx
|
||||
self.num_masters = num_masters
|
||||
self.prev_move_idx = 0
|
||||
self.seen_moveto = False
|
||||
self.glyphName = glyphName
|
||||
self.roundNumber = makeRoundNumberFunc(roundTolerance)
|
||||
|
||||
def _p(self, pt):
|
||||
""" Unlike T2CharstringPen, this class stores absolute values.
|
||||
This is to allow the logic in check_and_fix_closepath() to work,
|
||||
where the current or previous absolute point has to be compared to
|
||||
the path start-point.
|
||||
"""
|
||||
self._p0 = pt
|
||||
return list(self._p0)
|
||||
|
||||
def add_point(self, point_type, pt_coords):
|
||||
if self.m_index == 0:
|
||||
self._commands.append([point_type, [pt_coords]])
|
||||
else:
|
||||
cmd = self._commands[self.pt_index]
|
||||
if cmd[0] != point_type:
|
||||
# Fix some issues that show up in some
|
||||
# CFF workflows, even when fonts are
|
||||
# topologically merge compatible.
|
||||
success, pt_coords = self.check_and_fix_flat_curve(
|
||||
cmd, point_type, pt_coords)
|
||||
if not success:
|
||||
success = self.check_and_fix_closepath(
|
||||
cmd, point_type, pt_coords)
|
||||
if success:
|
||||
# We may have incremented self.pt_index
|
||||
cmd = self._commands[self.pt_index]
|
||||
if cmd[0] != point_type:
|
||||
success = False
|
||||
if not success:
|
||||
raise MergeTypeError(point_type,
|
||||
self.pt_index, len(cmd[1]),
|
||||
cmd[0], self.glyphName)
|
||||
raise MergeTypeError(
|
||||
point_type,
|
||||
self.pt_index, len(cmd[1]),
|
||||
cmd[0], self.glyphName)
|
||||
cmd[1].append(pt_coords)
|
||||
self.pt_index += 1
|
||||
|
||||
def add_hint(self, hint_type, args):
|
||||
if self.m_index == 0:
|
||||
self._commands.append([hint_type, [args]])
|
||||
else:
|
||||
cmd = self._commands[self.pt_index]
|
||||
if cmd[0] != hint_type:
|
||||
raise MergeTypeError(hint_type, self.pt_index, len(cmd[1]),
|
||||
cmd[0], self.glyphName)
|
||||
cmd[1].append(args)
|
||||
self.pt_index += 1
|
||||
|
||||
def add_hintmask(self, hint_type, abs_args):
|
||||
# For hintmask, fonttools.cffLib.specializer.py expects
|
||||
# each of these to be represented by two sequential commands:
|
||||
# first holding only the operator name, with an empty arg list,
|
||||
# second with an empty string as the op name, and the mask arg list.
|
||||
if self.m_index == 0:
|
||||
self._commands.append([hint_type, []])
|
||||
self._commands.append(["", [abs_args]])
|
||||
else:
|
||||
cmd = self._commands[self.pt_index]
|
||||
if cmd[0] != hint_type:
|
||||
raise MergeTypeError(hint_type, self.pt_index, len(cmd[1]),
|
||||
cmd[0], self.glyphName)
|
||||
self.pt_index += 1
|
||||
cmd = self._commands[self.pt_index]
|
||||
cmd[1].append(abs_args)
|
||||
self.pt_index += 1
|
||||
|
||||
def _moveTo(self, pt):
|
||||
if not self.seen_moveto:
|
||||
self.seen_moveto = True
|
||||
pt_coords = self._p(pt)
|
||||
self.add_point('rmoveto', pt_coords)
|
||||
# I set prev_move_idx here because add_point()
|
||||
@ -371,7 +579,7 @@ class CFF2CharStringMergePen(T2CharStringPen):
|
||||
def getCommands(self):
|
||||
return self._commands
|
||||
|
||||
def reorder_blend_args(self, commands):
|
||||
def reorder_blend_args(self, commands, get_delta_func, round_func):
|
||||
"""
|
||||
We first re-order the master coordinate values.
|
||||
For a moveto to lineto, the args are now arranged as:
|
||||
@ -380,9 +588,13 @@ class CFF2CharStringMergePen(T2CharStringPen):
|
||||
[ [master_0 x, master_1 x, master_2 x],
|
||||
[master_0 y, master_1 y, master_2 y]
|
||||
]
|
||||
We also make the value relative.
|
||||
If the master values are all the same, we collapse the list to
|
||||
as single value instead of a list.
|
||||
|
||||
We then convert this to:
|
||||
[ [master_0 x] + [x delta tuple] + [numBlends=1]
|
||||
[master_0 y] + [y delta tuple] + [numBlends=1]
|
||||
]
|
||||
"""
|
||||
for cmd in commands:
|
||||
# arg[i] is the set of arguments for this operator from master i.
|
||||
@ -390,108 +602,46 @@ class CFF2CharStringMergePen(T2CharStringPen):
|
||||
m_args = zip(*args)
|
||||
# m_args[n] is now all num_master args for the i'th argument
|
||||
# for this operation.
|
||||
cmd[1] = m_args
|
||||
|
||||
# Now convert from absolute to relative
|
||||
x0 = [0]*self.num_masters
|
||||
y0 = [0]*self.num_masters
|
||||
for cmd in self._commands:
|
||||
is_x = True
|
||||
coords = cmd[1]
|
||||
rel_coords = []
|
||||
for coord in coords:
|
||||
prev_coord = x0 if is_x else y0
|
||||
rel_coord = [pt[0] - pt[1] for pt in zip(coord, prev_coord)]
|
||||
|
||||
if allEqual(rel_coord):
|
||||
rel_coord = rel_coord[0]
|
||||
rel_coords.append(rel_coord)
|
||||
if is_x:
|
||||
x0 = coord
|
||||
else:
|
||||
y0 = coord
|
||||
is_x = not is_x
|
||||
cmd[1] = rel_coords
|
||||
return commands
|
||||
|
||||
@staticmethod
|
||||
def mergeCommandsToProgram(commands, var_model, round_func):
|
||||
"""
|
||||
Takes a commands list as returned by programToCommands() and
|
||||
converts it back to a T2CharString or CFF2Charstring program list. I
|
||||
need to use this rather than specialize.commandsToProgram, as the
|
||||
commands produced by CFF2CharStringMergePen initially contains a
|
||||
list of coordinate values, one for each master, wherever a single
|
||||
coordinate value is expected by the regular logic. The problem with
|
||||
doing using the specialize.py functions is that a commands list is
|
||||
expected to be a op name with its associated argument list. For the
|
||||
commands list here, some of the arguments may need to be converted
|
||||
to a new argument list and opcode.
|
||||
This version will convert each list of master arguments to a blend
|
||||
op and its arguments, and will also combine successive blend ops up
|
||||
to the stack limit.
|
||||
"""
|
||||
program = []
|
||||
for op, args in commands:
|
||||
num_args = len(args)
|
||||
# some of the args may be blend lists, and some may be
|
||||
# single coordinate values.
|
||||
i = 0
|
||||
stack_use = 0
|
||||
while i < num_args:
|
||||
arg = args[i]
|
||||
if not isinstance(arg, list):
|
||||
program.append(arg)
|
||||
i += 1
|
||||
stack_use += 1
|
||||
else:
|
||||
prev_stack_use = stack_use
|
||||
""" The arg is a tuple of blend values.
|
||||
These are each (master 0,master 1..master n)
|
||||
Combine as many successive tuples as we can,
|
||||
up to the max stack limit.
|
||||
"""
|
||||
num_masters = len(arg)
|
||||
blendlist = [arg]
|
||||
i += 1
|
||||
stack_use += 1 + num_masters # 1 for the num_blends arg
|
||||
while (i < num_args) and isinstance(args[i], list):
|
||||
blendlist.append(args[i])
|
||||
i += 1
|
||||
stack_use += num_masters
|
||||
if stack_use + num_masters > maxStackLimit:
|
||||
# if we are here, max stack is is the CFF2 max stack.
|
||||
break
|
||||
num_blends = len(blendlist)
|
||||
# append the 'num_blends' default font values
|
||||
for arg in blendlist:
|
||||
if round_func:
|
||||
arg[0] = round_func(arg[0])
|
||||
program.append(arg[0])
|
||||
for arg in blendlist:
|
||||
deltas = var_model.getDeltas(arg)
|
||||
cmd[1] = list(m_args)
|
||||
lastOp = None
|
||||
for cmd in commands:
|
||||
op = cmd[0]
|
||||
# masks are represented by two cmd's: first has only op names,
|
||||
# second has only args.
|
||||
if lastOp in ['hintmask', 'cntrmask']:
|
||||
coord = list(cmd[1])
|
||||
assert allEqual(coord), (
|
||||
"hintmask values cannot differ between source fonts.")
|
||||
cmd[1] = [coord[0][0]]
|
||||
else:
|
||||
coords = cmd[1]
|
||||
new_coords = []
|
||||
for coord in coords:
|
||||
if allEqual(coord):
|
||||
new_coords.append(coord[0])
|
||||
else:
|
||||
# convert to deltas
|
||||
deltas = get_delta_func(coord)[1:]
|
||||
if round_func:
|
||||
deltas = [round_func(delta) for delta in deltas]
|
||||
# First item in 'deltas' is the default master value;
|
||||
# for CFF2 data, that has already been written.
|
||||
program.extend(deltas[1:])
|
||||
program.append(num_blends)
|
||||
program.append('blend')
|
||||
stack_use = prev_stack_use + num_blends
|
||||
if op:
|
||||
program.append(op)
|
||||
return program
|
||||
coord = [coord[0]] + deltas
|
||||
new_coords.append(coord)
|
||||
cmd[1] = new_coords
|
||||
lastOp = op
|
||||
return commands
|
||||
|
||||
|
||||
def getCharString(self, private=None, globalSubrs=None,
|
||||
var_model=None, optimize=True):
|
||||
def getCharString(
|
||||
self, private=None, globalSubrs=None,
|
||||
var_model=None, optimize=True):
|
||||
commands = self._commands
|
||||
commands = self.reorder_blend_args(commands)
|
||||
commands = self.reorder_blend_args(commands, var_model.getDeltas,
|
||||
self.roundNumber)
|
||||
if optimize:
|
||||
commands = specializeCommands(commands, generalizeFirst=False,
|
||||
maxstack=maxStackLimit)
|
||||
program = self.mergeCommandsToProgram(commands, var_model=var_model,
|
||||
round_func=self.roundNumber)
|
||||
charString = T2CharString(program=program, private=private,
|
||||
globalSubrs=globalSubrs)
|
||||
commands = specializeCommands(
|
||||
commands, generalizeFirst=False,
|
||||
maxstack=maxStackLimit)
|
||||
program = commandsToProgram(commands)
|
||||
charString = T2CharString(
|
||||
program=program, private=private,
|
||||
globalSubrs=globalSubrs)
|
||||
return charString
|
||||
|
@ -65,24 +65,29 @@ def interpolate_cff2_charstrings(topDict, interpolateFromDeltas, glyphOrder):
|
||||
charstrings = topDict.CharStrings
|
||||
for gname in glyphOrder:
|
||||
# Interpolate charstring
|
||||
# e.g replace blend op args with regular args,
|
||||
# and use and discard vsindex op.
|
||||
charstring = charstrings[gname]
|
||||
pd = charstring.private
|
||||
vsindex = pd.vsindex if (hasattr(pd, 'vsindex')) else 0
|
||||
num_regions = pd.getNumRegions(vsindex)
|
||||
numMasters = num_regions + 1
|
||||
new_program = []
|
||||
vsindex = 0
|
||||
last_i = 0
|
||||
for i, token in enumerate(charstring.program):
|
||||
if token == 'blend':
|
||||
if token == 'vsindex':
|
||||
vsindex = charstring.program[i - 1]
|
||||
if last_i != 0:
|
||||
new_program.extend(charstring.program[last_i:i - 1])
|
||||
last_i = i + 1
|
||||
elif token == 'blend':
|
||||
num_regions = charstring.getNumRegions(vsindex)
|
||||
numMasters = 1 + num_regions
|
||||
num_args = charstring.program[i - 1]
|
||||
""" The stack is now:
|
||||
..args for following operations
|
||||
num_args values from the default font
|
||||
num_args tuples, each with numMasters-1 delta values
|
||||
num_blend_args
|
||||
'blend'
|
||||
"""
|
||||
argi = i - (num_args*numMasters + 1)
|
||||
# The program list starting at program[i] is now:
|
||||
# ..args for following operations
|
||||
# num_args values from the default font
|
||||
# num_args tuples, each with numMasters-1 delta values
|
||||
# num_blend_args
|
||||
# 'blend'
|
||||
argi = i - (num_args * numMasters + 1)
|
||||
end_args = tuplei = argi + num_args
|
||||
while argi < end_args:
|
||||
next_ti = tuplei + num_regions
|
||||
|
BIN
Tests/cffLib/data/TestSparseCFF2VF.otf
Normal file
BIN
Tests/cffLib/data/TestSparseCFF2VF.otf
Normal file
Binary file not shown.
@ -1,6 +1,11 @@
|
||||
from __future__ import print_function, division, absolute_import
|
||||
from fontTools.cffLib.specializer import (programToString, stringToProgram,
|
||||
generalizeProgram, specializeProgram)
|
||||
generalizeProgram, specializeProgram,
|
||||
programToCommands, commandsToProgram,
|
||||
generalizeCommands,
|
||||
specializeCommands)
|
||||
from fontTools.ttLib import TTFont
|
||||
import os
|
||||
import unittest
|
||||
|
||||
# TODO
|
||||
@ -913,6 +918,42 @@ class CFFSpecializeProgramTest(unittest.TestCase):
|
||||
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
|
||||
|
||||
|
||||
class CFF2VFTestSpecialize(unittest.TestCase):
|
||||
|
||||
def __init__(self, methodName):
|
||||
unittest.TestCase.__init__(self, methodName)
|
||||
# Python 3 renamed assertRaisesRegexp to assertRaisesRegex,
|
||||
# and fires deprecation warnings if a program uses the old name.
|
||||
if not hasattr(self, "assertRaisesRegex"):
|
||||
self.assertRaisesRegex = self.assertRaisesRegexp
|
||||
|
||||
@staticmethod
|
||||
def get_test_input(test_file_or_folder):
|
||||
path, _ = os.path.split(__file__)
|
||||
return os.path.join(path, "data", test_file_or_folder)
|
||||
|
||||
def test_blend_round_trip(self):
|
||||
otfvf_path = self.get_test_input('TestSparseCFF2VF.otf')
|
||||
ttf_font = TTFont(otfvf_path)
|
||||
fontGlyphList = ttf_font.getGlyphOrder()
|
||||
topDict = ttf_font['CFF2'].cff.topDictIndex[0]
|
||||
charstrings = topDict.CharStrings
|
||||
for glyphName in fontGlyphList:
|
||||
print(glyphName)
|
||||
cs = charstrings[glyphName]
|
||||
cs.decompile()
|
||||
cmds = programToCommands(cs.program, getNumRegions=cs.getNumRegions)
|
||||
cmds_g = generalizeCommands(cmds)
|
||||
cmds = specializeCommands(cmds_g, generalizeFirst=False)
|
||||
program = commandsToProgram(cmds)
|
||||
self.assertEqual(program, cs.program)
|
||||
program = specializeProgram(program, getNumRegions=cs.getNumRegions)
|
||||
self.assertEqual(program, cs.program)
|
||||
program_g = generalizeProgram(program, getNumRegions=cs.getNumRegions)
|
||||
program = commandsToProgram(cmds_g)
|
||||
self.assertEqual(program, program_g)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
sys.exit(unittest.main())
|
||||
|
229
Tests/varLib/data/TestSparseCFF2VF.designspace
Normal file
229
Tests/varLib/data/TestSparseCFF2VF.designspace
Normal file
@ -0,0 +1,229 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<designspace format="3">
|
||||
<axes>
|
||||
<axis default="200" maximum="900" minimum="200" name="weight" tag="wght">
|
||||
<map input="200" output="0" /> <!-- ExtraLight -->
|
||||
<map input="300" output="160" /> <!-- Light -->
|
||||
<map input="350" output="320" /> <!-- Normal -->
|
||||
<map input="400" output="390" /> <!-- Regular -->
|
||||
<map input="500" output="560" /> <!-- Medium -->
|
||||
<map input="700" output="780" /> <!-- Bold -->
|
||||
<map input="900" output="1000" /><!-- Heavy -->
|
||||
</axis>
|
||||
</axes>
|
||||
<sources>
|
||||
<source filename="master_sparse_cff2/MasterSet_Kanji-w0.00.ufo" stylename="w0.00">
|
||||
<info copy="1" />
|
||||
<location>
|
||||
<dimension name="weight" xvalue="0.00" />
|
||||
</location>
|
||||
</source>
|
||||
<source filename="master_sparse_cff2/MasterSet_Kanji-w439.00.ufo" stylename="w439.00">
|
||||
|
||||
<location>
|
||||
<dimension name="weight" xvalue="439.00" />
|
||||
</location>
|
||||
</source>
|
||||
<source filename="master_sparse_cff2/MasterSet_Kanji-w440.00.ufo" stylename="w440.00">
|
||||
|
||||
<location>
|
||||
<dimension name="weight" xvalue="440.00" />
|
||||
</location>
|
||||
</source>
|
||||
<source filename="master_sparse_cff2/MasterSet_Kanji-w599.00.ufo" stylename="w599.00">
|
||||
|
||||
<location>
|
||||
<dimension name="weight" xvalue="599.00" />
|
||||
</location>
|
||||
</source>
|
||||
<source filename="master_sparse_cff2/MasterSet_Kanji-w600.00.ufo" stylename="w600.00">
|
||||
|
||||
<location>
|
||||
<dimension name="weight" xvalue="600.00" />
|
||||
</location>
|
||||
</source>
|
||||
<source filename="master_sparse_cff2/MasterSet_Kanji-w669.00.ufo" stylename="w669.00">
|
||||
|
||||
<location>
|
||||
<dimension name="weight" xvalue="669.00" />
|
||||
</location>
|
||||
</source>
|
||||
<source filename="master_sparse_cff2/MasterSet_Kanji-w670.00.ufo" stylename="w670.00">
|
||||
|
||||
<location>
|
||||
<dimension name="weight" xvalue="670.00" />
|
||||
</location>
|
||||
</source>
|
||||
<source filename="master_sparse_cff2/MasterSet_Kanji-w799.00.ufo" stylename="w799.00">
|
||||
|
||||
<location>
|
||||
<dimension name="weight" xvalue="799.00" />
|
||||
</location>
|
||||
</source>
|
||||
<source filename="master_sparse_cff2/MasterSet_Kanji-w800.00.ufo" stylename="w800.00">
|
||||
|
||||
<location>
|
||||
<dimension name="weight" xvalue="800.00" />
|
||||
</location>
|
||||
</source>
|
||||
<source filename="master_sparse_cff2/MasterSet_Kanji-w889.00.ufo" stylename="w889.00">
|
||||
|
||||
<location>
|
||||
<dimension name="weight" xvalue="889.00" />
|
||||
</location>
|
||||
</source>
|
||||
<source filename="master_sparse_cff2/MasterSet_Kanji-w890.00.ufo" stylename="w890.00">
|
||||
|
||||
<location>
|
||||
<dimension name="weight" xvalue="890.00" />
|
||||
</location>
|
||||
</source>
|
||||
<source filename="master_sparse_cff2/MasterSet_Kanji-w1000.00.ufo" stylename="w1000.00">
|
||||
|
||||
<location>
|
||||
<dimension name="weight" xvalue="1000.00" />
|
||||
</location>
|
||||
</source>
|
||||
</sources>
|
||||
<instances>
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w0.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w0.00" stylename="Kanji-w0.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="0" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w239.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w239.00" stylename="Kanji-w239.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="239" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w240.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w240.00" stylename="Kanji-w240.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="240" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w439.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w439.00" stylename="Kanji-w439.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="439" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w440.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w440.00" stylename="Kanji-w440.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="440" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
|
||||
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w499.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w499.00" stylename="Kanji-w499.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="499" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w500.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w500.00" stylename="Kanji-w500.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="500" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
|
||||
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w599.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w599.00" stylename="Kanji-w599.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="599" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w600.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w600.00" stylename="Kanji-w600.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="600" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
|
||||
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w669.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w669.00" stylename="Kanji-w669.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="669" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w670.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w670.00" stylename="Kanji-w670.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="670" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
|
||||
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w699.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w699.00" stylename="Kanji-w699.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="699" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w700.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w700.00" stylename="Kanji-w700.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="700" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
|
||||
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-799.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-799.00" stylename="Kanji-799.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="799" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w800.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w800.00" stylename="Kanji-w800.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="800" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
|
||||
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w889.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w889.00" stylename="Kanji-w889.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="889" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w890.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w890.00" stylename="Kanji-w890.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="890" />
|
||||
</location>
|
||||
<kerning />
|
||||
<info />
|
||||
</instance>
|
||||
|
||||
|
||||
<instance familyname="SHSansJPVFTest" filename="instances/SHSansJPVFTest-Kanji-w1000.00.otf" postscriptfontname="SHSansJPVFTest-Kanji-w1000.00" stylename="Kanji-w1000.00">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="1000" />
|
||||
</location>
|
||||
</instance>
|
||||
</instances>
|
||||
</designspace>
|
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w0.00.otf
Normal file
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w0.00.otf
Normal file
Binary file not shown.
Binary file not shown.
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w439.00.otf
Normal file
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w439.00.otf
Normal file
Binary file not shown.
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w440.00.otf
Normal file
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w440.00.otf
Normal file
Binary file not shown.
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w599.00.otf
Normal file
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w599.00.otf
Normal file
Binary file not shown.
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w600.00.otf
Normal file
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w600.00.otf
Normal file
Binary file not shown.
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w669.00.otf
Normal file
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w669.00.otf
Normal file
Binary file not shown.
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w670.00.otf
Normal file
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w670.00.otf
Normal file
Binary file not shown.
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w799.00.otf
Normal file
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w799.00.otf
Normal file
Binary file not shown.
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w800.00.otf
Normal file
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w800.00.otf
Normal file
Binary file not shown.
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w889.00.otf
Normal file
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w889.00.otf
Normal file
Binary file not shown.
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w890.00.otf
Normal file
BIN
Tests/varLib/data/master_sparse_cff2/MasterSet_Kanji-w890.00.otf
Normal file
Binary file not shown.
@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ttFont sfntVersion="OTTO" ttLibVersion="3.32">
|
||||
<ttFont sfntVersion="OTTO" ttLibVersion="3.38">
|
||||
|
||||
<fvar>
|
||||
|
||||
@ -67,29 +67,29 @@
|
||||
<BlueValues>
|
||||
<blend value="-12 0 0"/>
|
||||
<blend value="0 0 0"/>
|
||||
<blend value="486 -8 14"/>
|
||||
<blend value="486 -8 -8"/>
|
||||
<blend value="498 0 0"/>
|
||||
<blend value="574 4 -8"/>
|
||||
<blend value="574 4 4"/>
|
||||
<blend value="586 0 0"/>
|
||||
<blend value="638 6 -10"/>
|
||||
<blend value="638 6 6"/>
|
||||
<blend value="650 0 0"/>
|
||||
<blend value="656 2 -2"/>
|
||||
<blend value="656 2 2"/>
|
||||
<blend value="668 0 0"/>
|
||||
<blend value="712 6 -10"/>
|
||||
<blend value="712 6 6"/>
|
||||
<blend value="724 0 0"/>
|
||||
</BlueValues>
|
||||
<OtherBlues>
|
||||
<blend value="-217 -17 29"/>
|
||||
<blend value="-217 -17 -17"/>
|
||||
<blend value="-205 0 0"/>
|
||||
</OtherBlues>
|
||||
<BlueScale value="0.0625"/>
|
||||
<BlueShift value="7"/>
|
||||
<BlueFuzz value="0"/>
|
||||
<StdHW>
|
||||
<blend value="67 -39.0 67.0"/>
|
||||
<blend value="67 -39.0 -39.0"/>
|
||||
</StdHW>
|
||||
<StdVW>
|
||||
<blend value="85 -51.0 87.0"/>
|
||||
<blend value="85 -51.0 -51.0"/>
|
||||
</StdVW>
|
||||
</Private>
|
||||
</FontDict>
|
||||
|
1301
Tests/varLib/data/test_results/TestSparseCFF2VF.ttx
Normal file
1301
Tests/varLib/data/test_results/TestSparseCFF2VF.ttx
Normal file
File diff suppressed because it is too large
Load Diff
@ -244,6 +244,23 @@ class BuildTest(unittest.TestCase):
|
||||
self.expect_ttx(varfont, expected_ttx_path, tables)
|
||||
self.check_ttx_dump(varfont, expected_ttx_path, tables, suffix)
|
||||
|
||||
def test_varlib_build_sparse_CFF2(self):
|
||||
ds_path = self.get_test_input('TestSparseCFF2VF.designspace')
|
||||
suffix = '.otf'
|
||||
expected_ttx_name = 'TestSparseCFF2VF'
|
||||
tables = ["fvar", "CFF2"]
|
||||
|
||||
finder = lambda s: s.replace('.ufo', suffix)
|
||||
varfont, model, _ = build(ds_path, finder)
|
||||
# some data (e.g. counts printed in TTX inline comments) is only
|
||||
# calculated at compile time, so before we can compare the TTX
|
||||
# dumps we need to save to a temporary stream, and realod the font
|
||||
varfont = reload_font(varfont)
|
||||
|
||||
expected_ttx_path = self.get_test_output(expected_ttx_name + '.ttx')
|
||||
self.expect_ttx(varfont, expected_ttx_path, tables)
|
||||
self.check_ttx_dump(varfont, expected_ttx_path, tables, suffix)
|
||||
|
||||
def test_varlib_main_ttf(self):
|
||||
"""Mostly for testing varLib.main()
|
||||
"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user