2017-05-03 23:49:51 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-05-14 17:04:34 +01:00
|
|
|
"""T2CharString operator specializer and generalizer.
|
|
|
|
|
|
|
|
PostScript glyph drawing operations can be expressed in multiple different
|
|
|
|
ways. For example, as well as the ``lineto`` operator, there is also a
|
|
|
|
``hlineto`` operator which draws a horizontal line, removing the need to
|
|
|
|
specify a ``dx`` coordinate, and a ``vlineto`` operator which draws a
|
|
|
|
vertical line, removing the need to specify a ``dy`` coordinate. As well
|
|
|
|
as decompiling :class:`fontTools.misc.psCharStrings.T2CharString` objects
|
|
|
|
into lists of operations, this module allows for conversion between general
|
|
|
|
and specific forms of the operation.
|
|
|
|
|
|
|
|
"""
|
2017-05-03 23:49:51 -07:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
from fontTools.cffLib import maxStackLimit
|
2017-05-03 23:49:51 -07:00
|
|
|
|
2017-05-07 22:12:19 -06:00
|
|
|
|
|
|
|
def stringToProgram(string):
|
2021-03-27 10:23:29 -04:00
|
|
|
if isinstance(string, str):
|
2017-05-07 22:12:19 -06:00
|
|
|
string = string.split()
|
|
|
|
program = []
|
|
|
|
for token in string:
|
|
|
|
try:
|
|
|
|
token = int(token)
|
|
|
|
except ValueError:
|
|
|
|
try:
|
|
|
|
token = float(token)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
program.append(token)
|
|
|
|
return program
|
|
|
|
|
2018-11-19 17:30:53 -08:00
|
|
|
|
2017-05-07 22:12:19 -06:00
|
|
|
def programToString(program):
|
|
|
|
return " ".join(str(x) for x in program)
|
|
|
|
|
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
def programToCommands(program, getNumRegions=None):
|
2023-10-25 14:34:27 +03:00
|
|
|
"""Takes a T2CharString program list and returns list of commands.
|
2017-05-03 23:49:51 -07:00
|
|
|
Each command is a two-tuple of commandname,arg-list. The commandname might
|
2017-05-06 13:12:14 -06:00
|
|
|
be empty string if no commandname shall be emitted (used for glyph width,
|
2017-05-04 20:50:15 -07:00
|
|
|
hintmask/cntrmask argument, as well as stray arguments at the end of the
|
2023-10-25 14:34:27 +03:00
|
|
|
program (🤷).
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
'getNumRegions' may be None, or a callable object. It must return the
|
2024-05-14 18:13:51 -06:00
|
|
|
number of regions. 'getNumRegions' takes a single argument, vsindex. It
|
|
|
|
returns the numRegions for the vsindex.
|
2019-07-30 20:25:38 -07:00
|
|
|
The Charstring may or may not start with a width value. If the first
|
|
|
|
non-blend operator has an odd number of arguments, then the first argument is
|
|
|
|
a width, and is popped off. This is complicated with blend operators, as
|
|
|
|
there may be more than one before the first hint or moveto operator, and each
|
|
|
|
one reduces several arguments to just one list argument. We have to sum the
|
|
|
|
number of arguments that are not part of the blend arguments, and all the
|
|
|
|
'numBlends' values. We could instead have said that by definition, if there
|
|
|
|
is a blend operator, there is no width value, since CFF2 Charstrings don't
|
|
|
|
have width values. I discussed this with Behdad, and we are allowing for an
|
|
|
|
initial width value in this case because developers may assemble a CFF2
|
|
|
|
charstring from CFF Charstrings, which could have width values.
|
|
|
|
"""
|
2022-12-13 11:26:36 +00:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
seenWidthOp = False
|
2024-05-14 18:13:51 -06:00
|
|
|
vsIndex = 0
|
2019-07-30 20:25:38 -07:00
|
|
|
lenBlendStack = 0
|
|
|
|
lastBlendIndex = 0
|
2017-05-03 23:49:51 -07:00
|
|
|
commands = []
|
|
|
|
stack = []
|
|
|
|
it = iter(program)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
for token in it:
|
2021-03-27 10:23:29 -04:00
|
|
|
if not isinstance(token, str):
|
2017-05-03 23:49:51 -07:00
|
|
|
stack.append(token)
|
|
|
|
continue
|
2022-12-13 11:26:36 +00:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
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.
|
2019-07-30 20:25:38 -07:00
|
|
|
numBlends = stack[-1]
|
|
|
|
numBlendArgs = numBlends * numSourceFonts + 1
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
# replace first blend op by a list of the blend ops.
|
2019-07-30 20:25:38 -07:00
|
|
|
stack[-numBlendArgs:] = [stack[-numBlendArgs:]]
|
|
|
|
lenBlendStack += numBlends + len(stack) - 1
|
|
|
|
lastBlendIndex = len(stack)
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
# if a blend op exists, this is or will be a CFF2 charstring.
|
|
|
|
continue
|
2022-12-13 11:26:36 +00:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
elif token == "vsindex":
|
|
|
|
vsIndex = stack[-1]
|
|
|
|
assert type(vsIndex) is int
|
2022-12-13 11:26:36 +00:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
elif (not seenWidthOp) and token in {
|
|
|
|
"hstem",
|
|
|
|
"hstemhm",
|
|
|
|
"vstem",
|
|
|
|
"vstemhm",
|
|
|
|
"cntrmask",
|
|
|
|
"hintmask",
|
|
|
|
"hmoveto",
|
|
|
|
"vmoveto",
|
|
|
|
"rmoveto",
|
|
|
|
"endchar",
|
|
|
|
}:
|
|
|
|
seenWidthOp = True
|
2017-05-06 13:12:14 -06:00
|
|
|
parity = token in {"hmoveto", "vmoveto"}
|
2019-07-30 20:25:38 -07:00
|
|
|
if lenBlendStack:
|
|
|
|
# lenBlendStack has the number of args represented by the last blend
|
|
|
|
# arg and all the preceding args. We need to now add the number of
|
|
|
|
# args following the last blend arg.
|
|
|
|
numArgs = lenBlendStack + len(stack[lastBlendIndex:])
|
|
|
|
else:
|
|
|
|
numArgs = len(stack)
|
|
|
|
if numArgs and (numArgs % 2) ^ parity:
|
2017-05-06 13:12:14 -06:00
|
|
|
width = stack.pop(0)
|
|
|
|
commands.append(("", [width]))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-04 20:50:15 -07:00
|
|
|
if token in {"hintmask", "cntrmask"}:
|
2017-05-03 23:49:51 -07:00
|
|
|
if stack:
|
2017-05-06 13:12:14 -06:00
|
|
|
commands.append(("", stack))
|
2017-05-03 23:49:51 -07:00
|
|
|
commands.append((token, []))
|
2017-05-06 13:12:14 -06:00
|
|
|
commands.append(("", [next(it)]))
|
2017-05-03 23:49:51 -07:00
|
|
|
else:
|
2019-07-30 20:25:38 -07:00
|
|
|
commands.append((token, stack))
|
2017-05-03 23:49:51 -07:00
|
|
|
stack = []
|
|
|
|
if stack:
|
2017-05-06 13:12:14 -06:00
|
|
|
commands.append(("", stack))
|
2017-05-03 23:49:51 -07:00
|
|
|
return commands
|
|
|
|
|
2018-10-23 10:18:35 -07:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
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
|
2022-12-13 11:26:36 +00:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
|
2018-11-19 17:30:53 -08:00
|
|
|
def commandsToProgram(commands):
|
2017-05-03 23:49:51 -07:00
|
|
|
"""Takes a commands list as returned by programToCommands() and converts
|
2018-11-19 17:30:53 -08:00
|
|
|
it back to a T2CharString program list."""
|
2017-05-03 23:49:51 -07:00
|
|
|
program = []
|
2018-11-19 17:30:53 -08:00
|
|
|
for op, args in commands:
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
if any(isinstance(arg, list) for arg in args):
|
|
|
|
args = _flattenBlendArgs(args)
|
2018-11-19 17:30:53 -08:00
|
|
|
program.extend(args)
|
2017-05-03 23:49:51 -07:00
|
|
|
if op:
|
|
|
|
program.append(op)
|
|
|
|
return program
|
|
|
|
|
|
|
|
|
|
|
|
def _everyN(el, n):
|
|
|
|
"""Group the list el into groups of size n"""
|
2017-05-05 21:07:43 -06:00
|
|
|
if len(el) % n != 0:
|
|
|
|
raise ValueError(el)
|
2017-05-03 23:49:51 -07:00
|
|
|
for i in range(0, len(el), n):
|
|
|
|
yield el[i : i + n]
|
|
|
|
|
|
|
|
|
2017-05-04 00:36:30 -07:00
|
|
|
class _GeneralizerDecombinerCommandsMap(object):
|
2017-05-03 23:49:51 -07:00
|
|
|
@staticmethod
|
|
|
|
def rmoveto(args):
|
|
|
|
if len(args) != 2:
|
|
|
|
raise ValueError(args)
|
|
|
|
yield ("rmoveto", args)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
@staticmethod
|
|
|
|
def hmoveto(args):
|
|
|
|
if len(args) != 1:
|
2017-05-08 14:09:02 -06:00
|
|
|
raise ValueError(args)
|
2017-05-03 23:49:51 -07:00
|
|
|
yield ("rmoveto", [args[0], 0])
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
@staticmethod
|
|
|
|
def vmoveto(args):
|
|
|
|
if len(args) != 1:
|
2017-05-08 14:09:02 -06:00
|
|
|
raise ValueError(args)
|
2017-05-03 23:49:51 -07:00
|
|
|
yield ("rmoveto", [0, args[0]])
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
@staticmethod
|
|
|
|
def rlineto(args):
|
2017-05-08 14:09:02 -06:00
|
|
|
if not args:
|
|
|
|
raise ValueError(args)
|
2017-05-03 23:49:51 -07:00
|
|
|
for args in _everyN(args, 2):
|
|
|
|
yield ("rlineto", args)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
@staticmethod
|
|
|
|
def hlineto(args):
|
2017-05-08 14:09:02 -06:00
|
|
|
if not args:
|
|
|
|
raise ValueError(args)
|
2017-05-03 23:49:51 -07:00
|
|
|
it = iter(args)
|
2022-12-13 11:26:36 +00:00
|
|
|
try:
|
2017-05-06 23:20:11 -07:00
|
|
|
while True:
|
|
|
|
yield ("rlineto", [next(it), 0])
|
|
|
|
yield ("rlineto", [0, next(it)])
|
|
|
|
except StopIteration:
|
2022-12-13 11:26:36 +00:00
|
|
|
pass
|
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
@staticmethod
|
|
|
|
def vlineto(args):
|
2017-05-08 14:09:02 -06:00
|
|
|
if not args:
|
2017-05-03 23:49:51 -07:00
|
|
|
raise ValueError(args)
|
|
|
|
it = iter(args)
|
2022-12-13 11:26:36 +00:00
|
|
|
try:
|
2017-05-03 23:49:51 -07:00
|
|
|
while True:
|
2017-05-06 23:20:11 -07:00
|
|
|
yield ("rlineto", [0, next(it)])
|
|
|
|
yield ("rlineto", [next(it), 0])
|
2017-05-03 23:49:51 -07:00
|
|
|
except StopIteration:
|
2022-12-13 11:26:36 +00:00
|
|
|
pass
|
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
@staticmethod
|
|
|
|
def rrcurveto(args):
|
2017-05-08 14:09:02 -06:00
|
|
|
if not args:
|
2017-05-04 20:50:15 -07:00
|
|
|
raise ValueError(args)
|
2017-05-03 23:49:51 -07:00
|
|
|
for args in _everyN(args, 6):
|
|
|
|
yield ("rrcurveto", args)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
@staticmethod
|
|
|
|
def hhcurveto(args):
|
|
|
|
if len(args) < 4 or len(args) % 4 > 1:
|
2017-05-04 20:50:15 -07:00
|
|
|
raise ValueError(args)
|
2017-05-03 23:49:51 -07:00
|
|
|
if len(args) % 2 == 1:
|
|
|
|
yield ("rrcurveto", [args[1], args[0], args[2], args[3], args[4], 0])
|
|
|
|
args = args[5:]
|
|
|
|
for args in _everyN(args, 4):
|
|
|
|
yield ("rrcurveto", [args[0], 0, args[1], args[2], args[3], 0])
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
@staticmethod
|
|
|
|
def vvcurveto(args):
|
|
|
|
if len(args) < 4 or len(args) % 4 > 1:
|
2017-05-04 20:50:15 -07:00
|
|
|
raise ValueError(args)
|
2017-05-03 23:49:51 -07:00
|
|
|
if len(args) % 2 == 1:
|
|
|
|
yield ("rrcurveto", [args[0], args[1], args[2], args[3], 0, args[4]])
|
|
|
|
args = args[5:]
|
|
|
|
for args in _everyN(args, 4):
|
|
|
|
yield ("rrcurveto", [0, args[0], args[1], args[2], 0, args[3]])
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
@staticmethod
|
|
|
|
def hvcurveto(args):
|
2017-05-04 20:50:15 -07:00
|
|
|
if len(args) < 4 or len(args) % 8 not in {0, 1, 4, 5}:
|
|
|
|
raise ValueError(args)
|
2017-05-03 23:49:51 -07:00
|
|
|
last_args = None
|
|
|
|
if len(args) % 2 == 1:
|
|
|
|
lastStraight = len(args) % 8 == 5
|
|
|
|
args, last_args = args[:-5], args[-5:]
|
|
|
|
it = _everyN(args, 4)
|
2022-12-13 11:26:36 +00:00
|
|
|
try:
|
2017-05-03 23:49:51 -07:00
|
|
|
while True:
|
|
|
|
args = next(it)
|
|
|
|
yield ("rrcurveto", [args[0], 0, args[1], args[2], 0, args[3]])
|
|
|
|
args = next(it)
|
|
|
|
yield ("rrcurveto", [0, args[0], args[1], args[2], args[3], 0])
|
|
|
|
except StopIteration:
|
2022-12-13 11:26:36 +00:00
|
|
|
pass
|
2017-05-03 23:49:51 -07:00
|
|
|
if last_args:
|
|
|
|
args = last_args
|
|
|
|
if lastStraight:
|
|
|
|
yield ("rrcurveto", [args[0], 0, args[1], args[2], args[4], args[3]])
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
2017-05-03 23:49:51 -07:00
|
|
|
yield ("rrcurveto", [0, args[0], args[1], args[2], args[3], args[4]])
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
@staticmethod
|
|
|
|
def vhcurveto(args):
|
2017-05-04 20:50:15 -07:00
|
|
|
if len(args) < 4 or len(args) % 8 not in {0, 1, 4, 5}:
|
2017-05-03 23:49:51 -07:00
|
|
|
raise ValueError(args)
|
|
|
|
last_args = None
|
|
|
|
if len(args) % 2 == 1:
|
|
|
|
lastStraight = len(args) % 8 == 5
|
|
|
|
args, last_args = args[:-5], args[-5:]
|
|
|
|
it = _everyN(args, 4)
|
2022-12-13 11:26:36 +00:00
|
|
|
try:
|
2017-05-03 23:49:51 -07:00
|
|
|
while True:
|
|
|
|
args = next(it)
|
|
|
|
yield ("rrcurveto", [0, args[0], args[1], args[2], args[3], 0])
|
|
|
|
args = next(it)
|
|
|
|
yield ("rrcurveto", [args[0], 0, args[1], args[2], 0, args[3]])
|
|
|
|
except StopIteration:
|
2022-12-13 11:26:36 +00:00
|
|
|
pass
|
2017-05-03 23:49:51 -07:00
|
|
|
if last_args:
|
|
|
|
args = last_args
|
|
|
|
if lastStraight:
|
|
|
|
yield ("rrcurveto", [0, args[0], args[1], args[2], args[3], args[4]])
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
2017-05-03 23:49:51 -07:00
|
|
|
yield ("rrcurveto", [args[0], 0, args[1], args[2], args[4], args[3]])
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
@staticmethod
|
|
|
|
def rcurveline(args):
|
|
|
|
if len(args) < 8 or len(args) % 6 != 2:
|
|
|
|
raise ValueError(args)
|
|
|
|
args, last_args = args[:-2], args[-2:]
|
|
|
|
for args in _everyN(args, 6):
|
|
|
|
yield ("rrcurveto", args)
|
|
|
|
yield ("rlineto", last_args)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
@staticmethod
|
|
|
|
def rlinecurve(args):
|
|
|
|
if len(args) < 8 or len(args) % 2 != 0:
|
|
|
|
raise ValueError(args)
|
|
|
|
args, last_args = args[:-6], args[-6:]
|
|
|
|
for args in _everyN(args, 2):
|
|
|
|
yield ("rlineto", args)
|
|
|
|
yield ("rrcurveto", last_args)
|
|
|
|
|
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
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
|
2022-12-13 11:26:36 +00:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
# 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]
|
2022-12-13 11:26:36 +00:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
numRegions = len(args) // numBlends - 1
|
|
|
|
if not (numBlends * (numRegions + 1) == len(args)):
|
|
|
|
raise ValueError(blendList)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
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)
|
|
|
|
]
|
2022-08-16 15:29:21 -06:00
|
|
|
blend_args = [a + b + [1] for a, b in zip(defaultArgs, deltaList)]
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
return blend_args
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
|
2017-05-08 14:05:10 -06:00
|
|
|
def generalizeCommands(commands, ignoreErrors=False):
|
2017-05-03 23:49:51 -07:00
|
|
|
result = []
|
2017-05-04 00:36:30 -07:00
|
|
|
mapping = _GeneralizerDecombinerCommandsMap
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
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]
|
2022-12-13 11:26:36 +00:00
|
|
|
)
|
|
|
|
]
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
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
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-06 13:12:14 -06:00
|
|
|
func = getattr(mapping, op, None)
|
2017-05-03 23:49:51 -07:00
|
|
|
if not func:
|
|
|
|
result.append((op, args))
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
for command in func(args):
|
|
|
|
result.append(command)
|
|
|
|
except ValueError:
|
|
|
|
if ignoreErrors:
|
2017-05-04 16:35:47 -07:00
|
|
|
# Store op as data, such that consumers of commands do not have to
|
|
|
|
# deal with incorrect number of arguments.
|
2017-05-06 13:12:14 -06:00
|
|
|
result.append(("", args))
|
|
|
|
result.append(("", [op]))
|
2017-05-03 23:49:51 -07:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
return result
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
def generalizeProgram(program, getNumRegions=None, **kwargs):
|
|
|
|
return commandsToProgram(
|
|
|
|
generalizeCommands(programToCommands(program, getNumRegions), **kwargs)
|
2022-12-13 11:26:36 +00:00
|
|
|
)
|
2017-05-03 23:49:51 -07:00
|
|
|
|
|
|
|
|
2017-05-04 20:50:15 -07:00
|
|
|
def _categorizeVector(v):
|
|
|
|
"""
|
|
|
|
Takes X,Y vector v and returns one of r, h, v, or 0 depending on which
|
2017-05-05 16:55:37 -06:00
|
|
|
of X and/or Y are zero, plus tuple of nonzero ones. If both are zero,
|
|
|
|
it returns a single zero still.
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-04 20:50:15 -07:00
|
|
|
>>> _categorizeVector((0,0))
|
2017-05-05 16:55:37 -06:00
|
|
|
('0', (0,))
|
2017-05-04 20:50:15 -07:00
|
|
|
>>> _categorizeVector((1,0))
|
2017-05-05 16:55:37 -06:00
|
|
|
('h', (1,))
|
2017-05-04 20:50:15 -07:00
|
|
|
>>> _categorizeVector((0,2))
|
2017-05-05 16:55:37 -06:00
|
|
|
('v', (2,))
|
2017-05-04 20:50:15 -07:00
|
|
|
>>> _categorizeVector((1,2))
|
2017-05-05 16:55:37 -06:00
|
|
|
('r', (1, 2))
|
2017-05-04 20:50:15 -07:00
|
|
|
"""
|
2017-05-06 05:02:22 -06:00
|
|
|
if not v[0]:
|
|
|
|
if not v[1]:
|
2017-05-06 04:58:59 -06:00
|
|
|
return "0", v[:1]
|
2017-05-05 16:55:37 -06:00
|
|
|
else:
|
|
|
|
return "v", v[1:]
|
|
|
|
else:
|
2017-05-06 05:02:22 -06:00
|
|
|
if not v[1]:
|
2017-05-05 16:55:37 -06:00
|
|
|
return "h", v[:1]
|
|
|
|
else:
|
|
|
|
return "r", v
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-04 20:50:15 -07:00
|
|
|
|
2017-05-06 03:23:00 -06:00
|
|
|
def _mergeCategories(a, b):
|
|
|
|
if a == "0":
|
|
|
|
return b
|
|
|
|
if b == "0":
|
|
|
|
return a
|
2017-05-05 21:07:43 -06:00
|
|
|
if a == b:
|
|
|
|
return a
|
|
|
|
return None
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-05 21:07:43 -06:00
|
|
|
|
2017-05-06 03:23:00 -06:00
|
|
|
def _negateCategory(a):
|
|
|
|
if a == "h":
|
|
|
|
return "v"
|
|
|
|
if a == "v":
|
|
|
|
return "h"
|
|
|
|
assert a in "0r"
|
|
|
|
return a
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-05 16:55:37 -06:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
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]
|
2024-11-04 13:52:28 -05:00
|
|
|
i += 1
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
if not isinstance(arg, list):
|
|
|
|
new_args.append(arg)
|
|
|
|
stack_use += 1
|
|
|
|
else:
|
|
|
|
prev_stack_use = stack_use
|
|
|
|
# The arg is a tuple of blend values.
|
2022-08-16 15:29:21 -06:00
|
|
|
# These are each (master 0,delta 1..delta n, 1)
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
# Combine as many successive tuples as we can,
|
|
|
|
# up to the max stack limit.
|
2022-08-16 15:29:21 -06:00
|
|
|
num_sources = len(arg) - 1
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
blendlist = [arg]
|
|
|
|
stack_use += 1 + num_sources # 1 for the num_blends arg
|
2024-11-04 14:08:07 -05:00
|
|
|
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# Make sure the stack depth does not exceed (maxstack - 1), so
|
|
|
|
# that subroutinizer can insert subroutine calls at any point.
|
|
|
|
while (
|
|
|
|
(i < num_args)
|
|
|
|
and isinstance(args[i], list)
|
|
|
|
and stack_use + num_sources < maxStackLimit
|
|
|
|
):
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
blendlist.append(args[i])
|
|
|
|
i += 1
|
|
|
|
stack_use += num_sources
|
|
|
|
# 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:
|
2022-08-16 15:29:21 -06:00
|
|
|
assert arg[-1] == 1
|
|
|
|
blend_args.extend(arg[1:-1])
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
blend_args.append(num_blends)
|
|
|
|
new_args.append(blend_args)
|
|
|
|
stack_use = prev_stack_use + num_blends
|
2022-12-13 11:26:36 +00:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
return new_args
|
2022-12-13 11:26:36 +00:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
|
|
|
|
def _addArgs(a, b):
|
|
|
|
if isinstance(b, list):
|
|
|
|
if isinstance(a, list):
|
2022-08-16 15:29:21 -06:00
|
|
|
if len(a) != len(b) or a[-1] != b[-1]:
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
raise ValueError()
|
2022-08-16 15:29:21 -06:00
|
|
|
return [_addArgs(va, vb) for va, vb in zip(a[:-1], b[:-1])] + [a[-1]]
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
else:
|
|
|
|
a, b = b, a
|
|
|
|
if isinstance(a, list):
|
2022-08-16 15:29:21 -06:00
|
|
|
assert a[-1] == 1
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
return [_addArgs(a[0], b)] + a[1:]
|
|
|
|
return a + b
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
|
2024-11-06 18:44:22 -05:00
|
|
|
def _argsStackUse(args):
|
|
|
|
stackLen = 0
|
|
|
|
maxLen = 0
|
|
|
|
for arg in args:
|
|
|
|
if type(arg) is list:
|
|
|
|
# Blended arg
|
|
|
|
maxLen = max(maxLen, stackLen + _argsStackUse(arg))
|
|
|
|
stackLen += arg[-1]
|
|
|
|
else:
|
|
|
|
stackLen += 1
|
|
|
|
return max(stackLen, maxLen)
|
|
|
|
|
|
|
|
|
2017-05-04 20:50:15 -07:00
|
|
|
def specializeCommands(
|
|
|
|
commands,
|
|
|
|
ignoreErrors=False,
|
|
|
|
generalizeFirst=True,
|
|
|
|
preserveTopology=False,
|
|
|
|
maxstack=48,
|
|
|
|
):
|
|
|
|
# We perform several rounds of optimizations. They are carefully ordered and are:
|
|
|
|
#
|
|
|
|
# 0. Generalize commands.
|
|
|
|
# This ensures that they are in our expected simple form, with each line/curve only
|
|
|
|
# having arguments for one segment, and using the generic form (rlineto/rrcurveto).
|
|
|
|
# If caller is sure the input is in this form, they can turn off generalization to
|
|
|
|
# save time.
|
|
|
|
#
|
|
|
|
# 1. Combine successive rmoveto operations.
|
|
|
|
#
|
|
|
|
# 2. Specialize rmoveto/rlineto/rrcurveto operators into horizontal/vertical variants.
|
2017-05-06 01:25:57 -06:00
|
|
|
# We specialize into some, made-up, variants as well, which simplifies following
|
2017-05-04 20:50:15 -07:00
|
|
|
# passes.
|
|
|
|
#
|
2017-05-07 00:14:35 -06:00
|
|
|
# 3. Merge or delete redundant operations, to the extent requested.
|
2017-05-05 16:55:37 -06:00
|
|
|
# OpenType spec declares point numbers in CFF undefined. As such, we happily
|
|
|
|
# change topology. If client relies on point numbers (in GPOS anchors, or for
|
|
|
|
# hinting purposes(what?)) they can turn this off.
|
2017-05-04 20:50:15 -07:00
|
|
|
#
|
|
|
|
# 4. Peephole optimization to revert back some of the h/v variants back into their
|
|
|
|
# original "relative" operator (rline/rrcurveto) if that saves a byte.
|
|
|
|
#
|
2017-05-05 21:07:43 -06:00
|
|
|
# 5. Combine adjacent operators when possible, minding not to go over max stack size.
|
2017-05-04 20:50:15 -07:00
|
|
|
#
|
2017-05-05 21:07:43 -06:00
|
|
|
# 6. Resolve any remaining made-up operators into real operators.
|
2017-05-04 20:50:15 -07:00
|
|
|
#
|
|
|
|
# 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. :-)
|
2017-05-05 16:55:37 -06:00
|
|
|
# A dynamic-programming approach can do the same but would be significantly slower.
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
#
|
|
|
|
# 7. For any args which are blend lists, convert them to a blend command.
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-04 20:50:15 -07:00
|
|
|
# 0. Generalize commands.
|
|
|
|
if generalizeFirst:
|
|
|
|
commands = generalizeCommands(commands, ignoreErrors=ignoreErrors)
|
|
|
|
else:
|
2017-05-20 18:34:20 -07:00
|
|
|
commands = list(commands) # Make copy since we modify in-place later.
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-04 20:50:15 -07:00
|
|
|
# 1. Combine successive rmoveto operations.
|
|
|
|
for i in range(len(commands) - 1, 0, -1):
|
|
|
|
if "rmoveto" == commands[i][0] == commands[i - 1][0]:
|
|
|
|
v1, v2 = commands[i - 1][1], commands[i][1]
|
|
|
|
commands[i - 1] = ("rmoveto", [v1[0] + v2[0], v1[1] + v2[1]])
|
|
|
|
del commands[i]
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-04 20:50:15 -07:00
|
|
|
# 2. Specialize rmoveto/rlineto/rrcurveto operators into horizontal/vertical variants.
|
|
|
|
#
|
2017-05-05 16:55:37 -06:00
|
|
|
# We, in fact, specialize into more, made-up, variants that special-case when both
|
|
|
|
# X and Y components are zero. This simplifies the following optimization passes.
|
|
|
|
# This case is rare, but OCD does not let me skip it.
|
|
|
|
#
|
|
|
|
# After this round, we will have four variants that use the following mnemonics:
|
|
|
|
#
|
2017-05-05 21:07:43 -06:00
|
|
|
# - 'r' for relative, ie. non-zero X and non-zero Y,
|
|
|
|
# - 'h' for horizontal, ie. zero X and non-zero Y,
|
|
|
|
# - 'v' for vertical, ie. non-zero X and zero Y,
|
|
|
|
# - '0' for zeros, ie. zero X and zero Y.
|
2017-05-04 20:50:15 -07:00
|
|
|
#
|
2017-05-05 21:07:43 -06:00
|
|
|
# The '0' pseudo-operators are not part of the spec, but help simplify the following
|
2017-05-05 16:55:37 -06:00
|
|
|
# optimization rounds. We resolve them at the end. So, after this, we will have four
|
2017-05-05 21:07:43 -06:00
|
|
|
# moveto and four lineto variants:
|
|
|
|
#
|
|
|
|
# - 0moveto, 0lineto
|
|
|
|
# - hmoveto, hlineto
|
|
|
|
# - vmoveto, vlineto
|
|
|
|
# - rmoveto, rlineto
|
|
|
|
#
|
|
|
|
# and sixteen curveto variants. For example, a '0hcurveto' operator means a curve
|
|
|
|
# dx0,dy0,dx1,dy1,dx2,dy2,dx3,dy3 where dx0, dx1, and dy3 are zero but not dx3.
|
|
|
|
# An 'rvcurveto' means dx3 is zero but not dx0,dy0,dy3.
|
|
|
|
#
|
|
|
|
# There are nine different variants of curves without the '0'. Those nine map exactly
|
|
|
|
# to the existing curve variants in the spec: rrcurveto, and the four variants hhcurveto,
|
|
|
|
# vvcurveto, hvcurveto, and vhcurveto each cover two cases, one with an odd number of
|
|
|
|
# arguments and one without. Eg. an hhcurveto with an extra argument (odd number of
|
|
|
|
# arguments) is in fact an rhcurveto. The operators in the spec are designed such that
|
2017-05-06 03:23:00 -06:00
|
|
|
# all four of rhcurveto, rvcurveto, hrcurveto, and vrcurveto are encodable for one curve.
|
2017-05-05 21:07:43 -06:00
|
|
|
#
|
|
|
|
# Of the curve types with '0', the 00curveto is equivalent to a lineto variant. The rest
|
|
|
|
# of the curve types with a 0 need to be encoded as a h or v variant. Ie. a '0' can be
|
|
|
|
# thought of a "don't care" and can be used as either an 'h' or a 'v'. As such, we always
|
|
|
|
# encode a number 0 as argument when we use a '0' variant. Later on, we can just substitute
|
|
|
|
# the '0' with either 'h' or 'v' and it works.
|
|
|
|
#
|
2017-05-06 03:23:00 -06:00
|
|
|
# When we get to curve splines however, things become more complicated... XXX finish this.
|
2017-05-05 21:07:43 -06:00
|
|
|
# There's one more complexity with splines. If one side of the spline is not horizontal or
|
|
|
|
# vertical (or zero), ie. if it's 'r', then it limits which spline types we can encode.
|
2017-05-06 03:23:00 -06:00
|
|
|
# Only hhcurveto and vvcurveto operators can encode a spline starting with 'r', and
|
|
|
|
# only hvcurveto and vhcurveto operators can encode a spline ending with 'r'.
|
|
|
|
# This limits our merge opportunities later.
|
2017-05-04 20:50:15 -07:00
|
|
|
#
|
|
|
|
for i in range(len(commands)):
|
|
|
|
op, args = commands[i]
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-05 16:55:37 -06:00
|
|
|
if op in {"rmoveto", "rlineto"}:
|
|
|
|
c, args = _categorizeVector(args)
|
|
|
|
commands[i] = c + op[1:], args
|
2017-05-04 20:50:15 -07:00
|
|
|
continue
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-05 21:07:43 -06:00
|
|
|
if op == "rrcurveto":
|
|
|
|
c1, args1 = _categorizeVector(args[:2])
|
|
|
|
c2, args2 = _categorizeVector(args[-2:])
|
2017-05-06 03:23:00 -06:00
|
|
|
commands[i] = c1 + c2 + "curveto", args1 + args[2:4] + args2
|
2017-05-05 21:07:43 -06:00
|
|
|
continue
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-07 00:14:35 -06:00
|
|
|
# 3. Merge or delete redundant operations, to the extent requested.
|
|
|
|
#
|
|
|
|
# TODO
|
|
|
|
# A 0moveto that comes before all other path operations can be removed.
|
|
|
|
# though I find conflicting evidence for this.
|
|
|
|
#
|
|
|
|
# TODO
|
|
|
|
# "If hstem and vstem hints are both declared at the beginning of a
|
|
|
|
# CharString, and this sequence is followed directly by the hintmask or
|
|
|
|
# cntrmask operators, then the vstem hint operator (or, if applicable,
|
|
|
|
# the vstemhm operator) need not be included."
|
|
|
|
#
|
|
|
|
# "The sequence and form of a CFF2 CharString program may be represented as:
|
|
|
|
# {hs* vs* cm* hm* mt subpath}? {mt subpath}*"
|
|
|
|
#
|
|
|
|
# https://www.microsoft.com/typography/otspec/cff2charstr.htm#section3.1
|
|
|
|
#
|
|
|
|
# For Type2 CharStrings the sequence is:
|
|
|
|
# w? {hs* vs* cm* hm* mt subpath}? {mt subpath}* endchar"
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-07 00:14:35 -06:00
|
|
|
# Some other redundancies change topology (point numbers).
|
2017-05-05 16:55:37 -06:00
|
|
|
if not preserveTopology:
|
|
|
|
for i in range(len(commands) - 1, -1, -1):
|
|
|
|
op, args = commands[i]
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-06 03:23:00 -06:00
|
|
|
# A 00curveto is demoted to a (specialized) lineto.
|
|
|
|
if op == "00curveto":
|
2017-05-05 16:55:37 -06:00
|
|
|
assert len(args) == 4
|
|
|
|
c, args = _categorizeVector(args[1:3])
|
|
|
|
op = c + "lineto"
|
|
|
|
commands[i] = op, args
|
|
|
|
# and then...
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-05 16:55:37 -06:00
|
|
|
# A 0lineto can be deleted.
|
|
|
|
if op == "0lineto":
|
|
|
|
del commands[i]
|
|
|
|
continue
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-05 16:55:37 -06:00
|
|
|
# Merge adjacent hlineto's and vlineto's.
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
# In CFF2 charstrings from variable fonts, each
|
|
|
|
# arg item may be a list of blendable values, one from
|
|
|
|
# each source font.
|
2018-10-23 10:18:35 -07:00
|
|
|
if i and op in {"hlineto", "vlineto"} and (op == commands[i - 1][0]):
|
2017-05-05 16:55:37 -06:00
|
|
|
_, other_args = commands[i - 1]
|
|
|
|
assert len(args) == 1 and len(other_args) == 1
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
try:
|
|
|
|
new_args = [_addArgs(args[0], other_args[0])]
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
commands[i - 1] = (op, new_args)
|
2017-05-05 16:55:37 -06:00
|
|
|
del commands[i]
|
|
|
|
continue
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-05 16:55:37 -06:00
|
|
|
# 4. Peephole optimization to revert back some of the h/v variants back into their
|
|
|
|
# original "relative" operator (rline/rrcurveto) if that saves a byte.
|
|
|
|
for i in range(1, len(commands) - 1):
|
|
|
|
op, args = commands[i]
|
|
|
|
prv, nxt = commands[i - 1][0], commands[i + 1][0]
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-05 16:55:37 -06:00
|
|
|
if op in {"0lineto", "hlineto", "vlineto"} and prv == nxt == "rlineto":
|
|
|
|
assert len(args) == 1
|
|
|
|
args = [0, args[0]] if op[0] == "v" else [args[0], 0]
|
|
|
|
commands[i] = ("rlineto", args)
|
|
|
|
continue
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-06 03:23:00 -06:00
|
|
|
if op[2:] == "curveto" and len(args) == 5 and prv == nxt == "rrcurveto":
|
|
|
|
assert (op[0] == "r") ^ (op[1] == "r")
|
2017-05-05 16:55:37 -06:00
|
|
|
if op[0] == "v":
|
|
|
|
pos = 0
|
|
|
|
elif op[0] != "r":
|
|
|
|
pos = 1
|
2017-05-06 03:23:00 -06:00
|
|
|
elif op[1] == "v":
|
2017-05-05 16:55:37 -06:00
|
|
|
pos = 4
|
|
|
|
else:
|
|
|
|
pos = 5
|
2018-02-18 11:48:22 -08:00
|
|
|
# Insert, while maintaining the type of args (can be tuple or list).
|
|
|
|
args = args[:pos] + type(args)((0,)) + args[pos:]
|
2017-05-05 16:55:37 -06:00
|
|
|
commands[i] = ("rrcurveto", args)
|
|
|
|
continue
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-05 21:07:43 -06:00
|
|
|
# 5. Combine adjacent operators when possible, minding not to go over max stack size.
|
2024-11-12 18:43:03 -07:00
|
|
|
stackUse = _argsStackUse(commands[-1][1]) if commands else 0
|
2017-05-05 16:55:37 -06:00
|
|
|
for i in range(len(commands) - 1, 0, -1):
|
|
|
|
op1, args1 = commands[i - 1]
|
|
|
|
op2, args2 = commands[i]
|
|
|
|
new_op = None
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-05 16:55:37 -06:00
|
|
|
# Merge logic...
|
2017-05-05 17:05:04 -06:00
|
|
|
if {op1, op2} <= {"rlineto", "rrcurveto"}:
|
|
|
|
if op1 == op2:
|
|
|
|
new_op = op1
|
|
|
|
else:
|
|
|
|
if op2 == "rrcurveto" and len(args2) == 6:
|
|
|
|
new_op = "rlinecurve"
|
|
|
|
elif len(args2) == 2:
|
|
|
|
new_op = "rcurveline"
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-07 12:54:31 -06:00
|
|
|
elif (op1, op2) in {("rlineto", "rlinecurve"), ("rrcurveto", "rcurveline")}:
|
|
|
|
new_op = op2
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-05 16:55:37 -06:00
|
|
|
elif {op1, op2} == {"vlineto", "hlineto"}:
|
|
|
|
new_op = op1
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-06 03:23:00 -06:00
|
|
|
elif "curveto" == op1[2:] == op2[2:]:
|
|
|
|
d0, d1 = op1[:2]
|
|
|
|
d2, d3 = op2[:2]
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-06 03:23:00 -06:00
|
|
|
if d1 == "r" or d2 == "r" or d0 == d3 == "r":
|
|
|
|
continue
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-06 03:23:00 -06:00
|
|
|
d = _mergeCategories(d1, d2)
|
|
|
|
if d is None:
|
|
|
|
continue
|
|
|
|
if d0 == "r":
|
|
|
|
d = _mergeCategories(d, d3)
|
|
|
|
if d is None:
|
|
|
|
continue
|
|
|
|
new_op = "r" + d + "curveto"
|
|
|
|
elif d3 == "r":
|
2017-05-06 03:49:06 -06:00
|
|
|
d0 = _mergeCategories(d0, _negateCategory(d))
|
2017-05-06 03:23:00 -06:00
|
|
|
if d0 is None:
|
|
|
|
continue
|
|
|
|
new_op = d0 + "r" + "curveto"
|
|
|
|
else:
|
|
|
|
d0 = _mergeCategories(d0, d3)
|
|
|
|
if d0 is None:
|
|
|
|
continue
|
|
|
|
new_op = d0 + d + "curveto"
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2018-07-25 17:29:27 +09:00
|
|
|
# Make sure the stack depth does not exceed (maxstack - 1), so
|
|
|
|
# that subroutinizer can insert subroutine calls at any point.
|
2024-11-12 18:43:03 -07:00
|
|
|
args1StackUse = _argsStackUse(args1)
|
|
|
|
combinedStackUse = max(args1StackUse, len(args1) + stackUse)
|
|
|
|
if new_op and combinedStackUse < maxstack:
|
2017-05-05 16:55:37 -06:00
|
|
|
commands[i - 1] = (new_op, args1 + args2)
|
|
|
|
del commands[i]
|
2024-11-12 18:43:03 -07:00
|
|
|
stackUse = combinedStackUse
|
|
|
|
else:
|
|
|
|
stackUse = args1StackUse
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-05 21:07:43 -06:00
|
|
|
# 6. Resolve any remaining made-up operators into real operators.
|
2017-05-05 16:55:37 -06:00
|
|
|
for i in range(len(commands)):
|
|
|
|
op, args = commands[i]
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-05 16:55:37 -06:00
|
|
|
if op in {"0moveto", "0lineto"}:
|
|
|
|
commands[i] = "h" + op[1:], args
|
|
|
|
continue
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-06 03:49:06 -06:00
|
|
|
if op[2:] == "curveto" and op[:2] not in {"rr", "hh", "vv", "vh", "hv"}:
|
2017-05-06 03:23:00 -06:00
|
|
|
op0, op1 = op[:2]
|
|
|
|
if (op0 == "r") ^ (op1 == "r"):
|
2017-05-05 21:07:43 -06:00
|
|
|
assert len(args) % 2 == 1
|
2017-05-06 03:23:00 -06:00
|
|
|
if op0 == "0":
|
|
|
|
op0 = "h"
|
|
|
|
if op1 == "0":
|
|
|
|
op1 = "h"
|
|
|
|
if op0 == "r":
|
|
|
|
op0 = op1
|
|
|
|
if op1 == "r":
|
|
|
|
op1 = _negateCategory(op0)
|
2017-05-06 03:49:06 -06:00
|
|
|
assert {op0, op1} <= {"h", "v"}, (op0, op1)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-06 03:23:00 -06:00
|
|
|
if len(args) % 2:
|
2017-05-06 03:49:06 -06:00
|
|
|
if op0 != op1: # vhcurveto / hvcurveto
|
2017-05-06 03:23:00 -06:00
|
|
|
if (op0 == "h") ^ (len(args) % 8 == 1):
|
|
|
|
# Swap last two args order
|
|
|
|
args = args[:-2] + args[-1:] + args[-2:-1]
|
2017-05-06 03:49:06 -06:00
|
|
|
else: # hhcurveto / vvcurveto
|
2017-05-06 04:28:06 -06:00
|
|
|
if op0 == "h": # hhcurveto
|
2017-05-06 03:23:00 -06:00
|
|
|
# Swap first two args order
|
|
|
|
args = args[1:2] + args[:1] + args[2:]
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-06 03:23:00 -06:00
|
|
|
commands[i] = op0 + op1 + "curveto", args
|
2017-05-05 16:55:37 -06:00
|
|
|
continue
|
2022-12-13 11:26:36 +00:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
# 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)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-04 16:35:47 -07:00
|
|
|
return commands
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-04 16:35:47 -07:00
|
|
|
|
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.
2019-04-26 09:33:52 -07:00
|
|
|
def specializeProgram(program, getNumRegions=None, **kwargs):
|
|
|
|
return commandsToProgram(
|
|
|
|
specializeCommands(programToCommands(program, getNumRegions), **kwargs)
|
2022-12-13 11:26:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-05-03 23:49:51 -07:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import sys
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-04 20:50:15 -07:00
|
|
|
if len(sys.argv) == 1:
|
|
|
|
import doctest
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-04 20:50:15 -07:00
|
|
|
sys.exit(doctest.testmod().failed)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2022-08-16 15:29:21 -06:00
|
|
|
import argparse
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2022-08-16 15:29:21 -06:00
|
|
|
parser = argparse.ArgumentParser(
|
2024-05-13 17:17:17 -06:00
|
|
|
"fonttools cffLib.specializer",
|
2022-08-16 15:29:21 -06:00
|
|
|
description="CFF CharString generalizer/specializer",
|
|
|
|
)
|
|
|
|
parser.add_argument("program", metavar="command", nargs="*", help="Commands.")
|
|
|
|
parser.add_argument(
|
|
|
|
"--num-regions",
|
|
|
|
metavar="NumRegions",
|
|
|
|
nargs="*",
|
|
|
|
default=None,
|
|
|
|
help="Number of variable-font regions for blend opertaions.",
|
|
|
|
)
|
2024-10-27 16:41:19 -07:00
|
|
|
parser.add_argument(
|
|
|
|
"--font",
|
|
|
|
metavar="FONTFILE",
|
|
|
|
default=None,
|
|
|
|
help="CFF2 font to specialize.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-o",
|
|
|
|
"--output-file",
|
|
|
|
type=str,
|
|
|
|
help="Output font file name.",
|
|
|
|
)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2022-08-16 15:29:21 -06:00
|
|
|
options = parser.parse_args(sys.argv[1:])
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2024-10-27 16:41:19 -07:00
|
|
|
if options.program:
|
|
|
|
getNumRegions = (
|
|
|
|
None
|
|
|
|
if options.num_regions is None
|
|
|
|
else lambda vsIndex: int(
|
|
|
|
options.num_regions[0 if vsIndex is None else vsIndex]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
program = stringToProgram(options.program)
|
|
|
|
print("Program:")
|
|
|
|
print(programToString(program))
|
|
|
|
commands = programToCommands(program, getNumRegions)
|
|
|
|
print("Commands:")
|
|
|
|
print(commands)
|
|
|
|
program2 = commandsToProgram(commands)
|
|
|
|
print("Program from commands:")
|
|
|
|
print(programToString(program2))
|
|
|
|
assert program == program2
|
|
|
|
print("Generalized program:")
|
|
|
|
print(programToString(generalizeProgram(program, getNumRegions)))
|
|
|
|
print("Specialized program:")
|
|
|
|
print(programToString(specializeProgram(program, getNumRegions)))
|
|
|
|
|
|
|
|
if options.font:
|
|
|
|
from fontTools.ttLib import TTFont
|
|
|
|
|
|
|
|
font = TTFont(options.font)
|
|
|
|
cff2 = font["CFF2"].cff.topDictIndex[0]
|
|
|
|
charstrings = cff2.CharStrings
|
|
|
|
for glyphName in charstrings.keys():
|
|
|
|
charstring = charstrings[glyphName]
|
|
|
|
charstring.decompile()
|
|
|
|
getNumRegions = charstring.private.getNumRegions
|
|
|
|
charstring.program = specializeProgram(
|
|
|
|
charstring.program, getNumRegions, maxstack=maxStackLimit
|
|
|
|
)
|
|
|
|
|
|
|
|
if options.output_file is None:
|
|
|
|
from fontTools.misc.cliTools import makeOutputFileName
|
|
|
|
|
|
|
|
outfile = makeOutputFileName(
|
|
|
|
options.font, overWrite=True, suffix=".specialized"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
outfile = options.output_file
|
|
|
|
if outfile:
|
|
|
|
print("Saving", outfile)
|
|
|
|
font.save(outfile)
|