2019-03-06 17:43:28 -08:00
|
|
|
""" Partially instantiate a variable font.
|
|
|
|
|
|
|
|
This is similar to fontTools.varLib.mutator, but instead of creating full
|
|
|
|
instances (i.e. static fonts) from variable fonts, it creates "partial"
|
|
|
|
variable fonts, only containing a subset of the variation space.
|
|
|
|
For example, if you wish to pin the width axis to a given location while
|
|
|
|
keeping the rest of the axes, you can do:
|
|
|
|
|
2019-03-21 15:30:48 +00:00
|
|
|
$ fonttools varLib.instancer ./NotoSans-VF.ttf wdth=85
|
2019-03-06 17:43:28 -08:00
|
|
|
|
|
|
|
NOTE: The module is experimental and both the API and the CLI *will* change.
|
|
|
|
"""
|
|
|
|
from __future__ import print_function, division, absolute_import
|
|
|
|
from fontTools.misc.py23 import *
|
2019-03-14 10:59:15 -04:00
|
|
|
from fontTools.misc.fixedTools import floatToFixedToFloat, otRound
|
2019-03-07 19:18:14 -08:00
|
|
|
from fontTools.varLib.models import supportScalar, normalizeValue, piecewiseLinearMap
|
2019-03-06 17:43:28 -08:00
|
|
|
from fontTools.varLib.iup import iup_delta
|
|
|
|
from fontTools.ttLib import TTFont
|
|
|
|
from fontTools.ttLib.tables._g_l_y_f import GlyphCoordinates
|
2019-03-19 10:44:39 -04:00
|
|
|
from fontTools.varLib.varStore import VarStoreInstancer
|
|
|
|
from fontTools.varLib.mvar import MVAR_ENTRIES
|
2019-03-25 13:15:50 +00:00
|
|
|
import collections
|
2019-03-08 16:24:13 -08:00
|
|
|
from copy import deepcopy
|
2019-03-06 17:43:28 -08:00
|
|
|
import logging
|
2019-03-06 21:54:15 -08:00
|
|
|
import os
|
|
|
|
import re
|
2019-03-06 17:43:28 -08:00
|
|
|
|
|
|
|
|
2019-03-21 15:30:48 +00:00
|
|
|
log = logging.getLogger("fontTools.varlib.instancer")
|
2019-03-06 17:43:28 -08:00
|
|
|
|
|
|
|
|
2019-03-22 14:15:53 +00:00
|
|
|
def instantiateTupleVariationStore(variations, location):
|
2019-03-06 17:43:28 -08:00
|
|
|
newVariations = []
|
2019-03-22 14:15:53 +00:00
|
|
|
defaultDeltas = []
|
2019-03-06 17:43:28 -08:00
|
|
|
for var in variations:
|
2019-03-22 14:15:53 +00:00
|
|
|
# Compute the scalar support of the axes to be pinned at the desired location,
|
|
|
|
# excluding any axes that we are not pinning.
|
|
|
|
# If a TupleVariation doesn't mention an axis, it implies that the axis peak
|
|
|
|
# is 0 (i.e. the axis does not participate).
|
|
|
|
support = {axis: var.axes.pop(axis, (-1, 0, +1)) for axis in location}
|
|
|
|
scalar = supportScalar(location, support)
|
|
|
|
if scalar == 0.0:
|
|
|
|
# no influence, drop the TupleVariation
|
2019-03-06 17:43:28 -08:00
|
|
|
continue
|
2019-03-22 14:15:53 +00:00
|
|
|
elif scalar != 1.0:
|
|
|
|
var.scaleDeltas(scalar)
|
|
|
|
if not var.axes:
|
|
|
|
# if no axis is left in the TupleVariation, also drop it; its deltas
|
|
|
|
# will be folded into the neutral
|
|
|
|
defaultDeltas.append(var.coordinates)
|
2019-03-06 17:43:28 -08:00
|
|
|
else:
|
2019-03-22 14:15:53 +00:00
|
|
|
# keep the TupleVariation, and round the scaled deltas to integers
|
2019-03-22 17:30:30 +00:00
|
|
|
if scalar != 1.0:
|
|
|
|
var.roundDeltas()
|
2019-03-22 14:15:53 +00:00
|
|
|
newVariations.append(var)
|
|
|
|
variations[:] = newVariations
|
|
|
|
return defaultDeltas
|
|
|
|
|
|
|
|
|
|
|
|
def setGvarGlyphDeltas(varfont, glyphname, deltasets):
|
|
|
|
glyf = varfont["glyf"]
|
|
|
|
coordinates = glyf.getCoordinates(glyphname, varfont)
|
|
|
|
origCoords = None
|
|
|
|
|
|
|
|
for deltas in deltasets:
|
|
|
|
hasUntouchedPoints = None in deltas
|
|
|
|
if hasUntouchedPoints:
|
|
|
|
if origCoords is None:
|
|
|
|
origCoords, g = glyf.getCoordinatesAndControls(glyphname, varfont)
|
|
|
|
deltas = iup_delta(deltas, origCoords, g.endPts)
|
|
|
|
coordinates += GlyphCoordinates(deltas)
|
|
|
|
|
|
|
|
glyf.setCoordinates(glyphname, coordinates, varfont)
|
|
|
|
|
|
|
|
|
|
|
|
def instantiateGvarGlyph(varfont, glyphname, location):
|
|
|
|
gvar = varfont["gvar"]
|
|
|
|
|
|
|
|
defaultDeltas = instantiateTupleVariationStore(gvar.variations[glyphname], location)
|
|
|
|
if defaultDeltas:
|
|
|
|
setGvarGlyphDeltas(varfont, glyphname, defaultDeltas)
|
|
|
|
|
|
|
|
if not gvar.variations[glyphname]:
|
|
|
|
del gvar.variations[glyphname]
|
2019-03-06 17:43:28 -08:00
|
|
|
|
|
|
|
|
|
|
|
def instantiateGvar(varfont, location):
|
|
|
|
log.info("Instantiating glyf/gvar tables")
|
|
|
|
|
2019-03-07 19:18:14 -08:00
|
|
|
gvar = varfont["gvar"]
|
|
|
|
glyf = varfont["glyf"]
|
2019-03-06 17:43:28 -08:00
|
|
|
# Get list of glyph names in gvar sorted by component depth.
|
|
|
|
# If a composite glyph is processed before its base glyph, the bounds may
|
|
|
|
# be calculated incorrectly because deltas haven't been applied to the
|
|
|
|
# base glyph yet.
|
|
|
|
glyphnames = sorted(
|
|
|
|
gvar.variations.keys(),
|
|
|
|
key=lambda name: (
|
|
|
|
glyf[name].getCompositeMaxpValues(glyf).maxComponentDepth
|
2019-03-07 19:18:14 -08:00
|
|
|
if glyf[name].isComposite()
|
|
|
|
else 0,
|
|
|
|
name,
|
|
|
|
),
|
2019-03-06 17:43:28 -08:00
|
|
|
)
|
|
|
|
for glyphname in glyphnames:
|
2019-03-22 14:15:53 +00:00
|
|
|
instantiateGvarGlyph(varfont, glyphname, location)
|
|
|
|
|
|
|
|
if not gvar.variations:
|
|
|
|
del varfont["gvar"]
|
|
|
|
|
|
|
|
|
2019-03-22 17:32:05 +00:00
|
|
|
def setCvarDeltas(cvt, deltasets):
|
2019-03-22 14:15:53 +00:00
|
|
|
# copy cvt values internally represented as array.array("h") to a list,
|
|
|
|
# accumulating deltas (that may be float since we scaled them) and only
|
|
|
|
# do the rounding to integer once at the end to reduce rounding errors
|
|
|
|
values = list(cvt)
|
|
|
|
for deltas in deltasets:
|
|
|
|
for i, delta in enumerate(deltas):
|
|
|
|
if delta is not None:
|
|
|
|
values[i] += delta
|
|
|
|
for i, v in enumerate(values):
|
|
|
|
cvt[i] = otRound(v)
|
2019-03-06 17:43:28 -08:00
|
|
|
|
|
|
|
|
2019-03-14 10:59:15 -04:00
|
|
|
def instantiateCvar(varfont, location):
|
|
|
|
log.info("Instantiating cvt/cvar tables")
|
|
|
|
cvar = varfont["cvar"]
|
|
|
|
cvt = varfont["cvt "]
|
2019-03-22 14:15:53 +00:00
|
|
|
defaultDeltas = instantiateTupleVariationStore(cvar.variations, location)
|
2019-03-22 17:32:05 +00:00
|
|
|
setCvarDeltas(cvt, defaultDeltas)
|
2019-03-22 14:15:53 +00:00
|
|
|
if not cvar.variations:
|
2019-03-19 10:44:39 -04:00
|
|
|
del varfont["cvar"]
|
|
|
|
|
|
|
|
|
|
|
|
def setMvarDeltas(varfont, location):
|
|
|
|
log.info("Setting MVAR deltas")
|
|
|
|
|
|
|
|
mvar = varfont["MVAR"].table
|
|
|
|
fvar = varfont["fvar"]
|
|
|
|
varStoreInstancer = VarStoreInstancer(mvar.VarStore, fvar.axes, location)
|
|
|
|
records = mvar.ValueRecord
|
2019-03-25 13:15:50 +00:00
|
|
|
# accumulate applicable deltas as floats and only round at the end
|
|
|
|
deltas = collections.defaultdict(float)
|
2019-03-19 10:44:39 -04:00
|
|
|
for rec in records:
|
|
|
|
mvarTag = rec.ValueTag
|
|
|
|
if mvarTag not in MVAR_ENTRIES:
|
|
|
|
continue
|
|
|
|
tableTag, itemName = MVAR_ENTRIES[mvarTag]
|
2019-03-25 13:15:50 +00:00
|
|
|
deltas[(tableTag, itemName)] += varStoreInstancer[rec.VarIdx]
|
|
|
|
|
|
|
|
for (tableTag, itemName), delta in deltas.items():
|
2019-03-19 10:44:39 -04:00
|
|
|
setattr(
|
2019-03-25 13:15:50 +00:00
|
|
|
varfont[tableTag],
|
|
|
|
itemName,
|
|
|
|
getattr(varfont[tableTag], itemName) + otRound(delta),
|
2019-03-19 10:44:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def instantiateMvar(varfont, location):
|
|
|
|
log.info("Instantiating MVAR table")
|
|
|
|
# First instantiate to new position without modifying MVAR table
|
|
|
|
setMvarDeltas(varfont, location)
|
|
|
|
|
2019-03-26 13:48:54 +00:00
|
|
|
varStore = varfont["MVAR"].table.VarStore
|
|
|
|
instantiateItemVariationStore(varStore, varfont["fvar"].axes, location)
|
2019-03-19 10:44:39 -04:00
|
|
|
|
2019-03-26 13:48:54 +00:00
|
|
|
if not varStore.VarRegionList.Region:
|
|
|
|
# Delete table if no more regions left.
|
|
|
|
del varfont["MVAR"]
|
2019-03-19 10:44:39 -04:00
|
|
|
|
|
|
|
|
2019-03-26 13:48:54 +00:00
|
|
|
def instantiateItemVariationStore(varStore, fvarAxes, location):
|
|
|
|
regionsToBeRemoved = set()
|
|
|
|
regionScalars = {}
|
2019-03-19 10:44:39 -04:00
|
|
|
pinnedAxes = set(location.keys())
|
2019-03-25 13:56:27 +00:00
|
|
|
fvarAxisIndices = {
|
|
|
|
axis.axisTag: index
|
2019-03-26 13:48:54 +00:00
|
|
|
for index, axis in enumerate(fvarAxes)
|
2019-03-25 13:56:27 +00:00
|
|
|
if axis.axisTag in pinnedAxes
|
|
|
|
}
|
2019-03-26 13:48:54 +00:00
|
|
|
for regionIndex, region in enumerate(varStore.VarRegionList.Region):
|
2019-03-25 13:41:41 +00:00
|
|
|
# collect set of axisTags which have influence: peak != 0
|
2019-03-19 10:44:39 -04:00
|
|
|
regionAxes = set(
|
2019-03-25 13:41:41 +00:00
|
|
|
axis
|
2019-03-26 13:48:54 +00:00
|
|
|
for axis, (start, peak, end) in region.get_support(fvarAxes).items()
|
2019-03-25 13:41:41 +00:00
|
|
|
if peak != 0
|
2019-03-19 10:44:39 -04:00
|
|
|
)
|
|
|
|
pinnedRegionAxes = regionAxes & pinnedAxes
|
|
|
|
if not pinnedRegionAxes:
|
|
|
|
# A region where none of the axes having effect are pinned
|
|
|
|
continue
|
|
|
|
if len(pinnedRegionAxes) == len(regionAxes):
|
|
|
|
# All the axes having effect in this region are being pinned so
|
|
|
|
# remove it
|
2019-03-26 13:48:54 +00:00
|
|
|
regionsToBeRemoved.add(regionIndex)
|
2019-03-19 10:44:39 -04:00
|
|
|
else:
|
|
|
|
# This region will be retained but the deltas have to be adjusted.
|
|
|
|
pinnedSupport = {
|
2019-03-25 13:41:41 +00:00
|
|
|
axis: support
|
2019-03-26 13:48:54 +00:00
|
|
|
for axis, support in region.get_support(fvarAxes).items()
|
2019-03-25 13:41:41 +00:00
|
|
|
if axis in pinnedRegionAxes
|
2019-03-19 10:44:39 -04:00
|
|
|
}
|
|
|
|
pinnedScalar = supportScalar(location, pinnedSupport)
|
2019-03-26 13:48:54 +00:00
|
|
|
regionScalars[regionIndex] = pinnedScalar
|
2019-03-19 10:44:39 -04:00
|
|
|
|
2019-03-25 13:56:27 +00:00
|
|
|
for axis in pinnedRegionAxes:
|
2019-03-19 10:44:39 -04:00
|
|
|
# For all pinnedRegionAxes make their influence null by setting
|
|
|
|
# PeakCoord to 0.
|
2019-03-25 13:56:27 +00:00
|
|
|
index = fvarAxisIndices[axis]
|
2019-03-19 10:44:39 -04:00
|
|
|
region.VarRegionAxis[index].PeakCoord = 0
|
|
|
|
|
2019-03-26 15:31:20 +00:00
|
|
|
newVarDatas = []
|
2019-03-26 13:48:54 +00:00
|
|
|
for vardata in varStore.VarData:
|
2019-03-26 15:31:20 +00:00
|
|
|
# drop VarData subtable if we remove all the regions referenced by it
|
|
|
|
if regionsToBeRemoved.issuperset(vardata.VarRegionIndex):
|
|
|
|
continue
|
|
|
|
regionToColumnMap = {
|
|
|
|
regionIndex: col for col, regionIndex in enumerate(vardata.VarRegionIndex)
|
|
|
|
}
|
2019-03-26 13:48:54 +00:00
|
|
|
# Apply scalars for regions to be retained.
|
|
|
|
for regionIndex, scalar in regionScalars.items():
|
2019-03-26 15:31:20 +00:00
|
|
|
if regionIndex not in regionToColumnMap:
|
|
|
|
continue
|
|
|
|
column = regionToColumnMap[regionIndex]
|
|
|
|
for row in vardata.Item:
|
|
|
|
row[column] *= otRound(scalar)
|
2019-03-26 13:48:54 +00:00
|
|
|
|
|
|
|
if regionsToBeRemoved:
|
|
|
|
# from each deltaset row, delete columns corresponding to the regions to
|
|
|
|
# be deleted
|
|
|
|
newItems = []
|
|
|
|
varRegionIndex = vardata.VarRegionIndex
|
|
|
|
for item in vardata.Item:
|
|
|
|
newItems.append(
|
|
|
|
[
|
|
|
|
delta
|
|
|
|
for column, delta in enumerate(item)
|
|
|
|
if varRegionIndex[column] not in regionsToBeRemoved
|
|
|
|
]
|
|
|
|
)
|
|
|
|
vardata.Item = newItems
|
2019-03-26 15:31:20 +00:00
|
|
|
vardata.ItemCount = len(newItems)
|
2019-03-26 13:48:54 +00:00
|
|
|
# prune VarRegionIndex from the regions to be deleted
|
|
|
|
vardata.VarRegionIndex = [
|
|
|
|
ri for ri in vardata.VarRegionIndex if ri not in regionsToBeRemoved
|
|
|
|
]
|
2019-03-26 15:31:20 +00:00
|
|
|
newVarDatas.append(vardata)
|
2019-03-19 10:44:39 -04:00
|
|
|
|
2019-03-26 15:31:20 +00:00
|
|
|
varStore.VarData = newVarDatas
|
|
|
|
varStore.VarDataCount = len(varStore.VarData)
|
2019-03-26 13:48:54 +00:00
|
|
|
# remove unused regions from VarRegionList
|
|
|
|
varStore.prune_regions()
|
2019-03-19 10:44:39 -04:00
|
|
|
|
2019-03-14 10:59:15 -04:00
|
|
|
|
2019-03-26 10:14:16 +00:00
|
|
|
def instantiateFeatureVariations(varfont, location):
|
|
|
|
for tableTag in ("GPOS", "GSUB"):
|
|
|
|
if tableTag not in varfont or not hasattr(
|
|
|
|
varfont[tableTag].table, "FeatureVariations"
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
log.info("Instantiating FeatureVariations of %s table", tableTag)
|
|
|
|
_instantiateFeatureVariations(
|
|
|
|
varfont[tableTag].table, varfont["fvar"].axes, location
|
|
|
|
)
|
2019-03-25 16:14:57 -04:00
|
|
|
|
2019-03-26 10:14:16 +00:00
|
|
|
|
|
|
|
def _instantiateFeatureVariations(table, fvarAxes, location):
|
2019-03-25 16:14:57 -04:00
|
|
|
newRecords = []
|
|
|
|
pinnedAxes = set(location.keys())
|
|
|
|
featureVariationApplied = False
|
2019-03-26 10:14:16 +00:00
|
|
|
for record in table.FeatureVariations.FeatureVariationRecord:
|
2019-03-25 16:14:57 -04:00
|
|
|
retainRecord = True
|
|
|
|
applies = True
|
|
|
|
newConditions = []
|
|
|
|
for condition in record.ConditionSet.ConditionTable:
|
|
|
|
axisIdx = condition.AxisIndex
|
2019-03-26 10:14:16 +00:00
|
|
|
axisTag = fvarAxes[axisIdx].axisTag
|
2019-03-25 16:14:57 -04:00
|
|
|
if condition.Format == 1 and axisTag in pinnedAxes:
|
|
|
|
minValue = condition.FilterRangeMinValue
|
|
|
|
maxValue = condition.FilterRangeMaxValue
|
|
|
|
v = location[axisTag]
|
|
|
|
if not (minValue <= v <= maxValue):
|
|
|
|
# condition not met so remove entire record
|
|
|
|
retainRecord = False
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
applies = False
|
|
|
|
newConditions.append(condition)
|
|
|
|
|
|
|
|
if retainRecord and newConditions:
|
|
|
|
record.ConditionSet.ConditionTable = newConditions
|
|
|
|
newRecords.append(record)
|
|
|
|
|
|
|
|
if applies and not featureVariationApplied:
|
|
|
|
assert record.FeatureTableSubstitution.Version == 0x00010000
|
|
|
|
for rec in record.FeatureTableSubstitution.SubstitutionRecord:
|
|
|
|
table.FeatureList.FeatureRecord[rec.FeatureIndex].Feature = rec.Feature
|
|
|
|
# Set variations only once
|
|
|
|
featureVariationApplied = True
|
2019-03-26 10:14:16 +00:00
|
|
|
|
|
|
|
if newRecords:
|
|
|
|
table.FeatureVariations.FeatureVariationRecord = newRecords
|
|
|
|
else:
|
|
|
|
del table.FeatureVariations
|
2019-03-25 16:14:57 -04:00
|
|
|
|
|
|
|
|
2019-03-06 21:54:15 -08:00
|
|
|
def normalize(value, triple, avar_mapping):
|
|
|
|
value = normalizeValue(value, triple)
|
|
|
|
if avar_mapping:
|
|
|
|
value = piecewiseLinearMap(value, avar_mapping)
|
|
|
|
# Quantize to F2Dot14, to avoid surprise interpolations.
|
|
|
|
return floatToFixedToFloat(value, 14)
|
2019-03-06 17:43:28 -08:00
|
|
|
|
2019-03-07 19:18:14 -08:00
|
|
|
|
2019-03-06 21:54:15 -08:00
|
|
|
def normalizeAxisLimits(varfont, axis_limits):
|
2019-03-07 19:18:14 -08:00
|
|
|
fvar = varfont["fvar"]
|
2019-03-06 21:54:15 -08:00
|
|
|
bad_limits = axis_limits.keys() - {a.axisTag for a in fvar.axes}
|
|
|
|
if bad_limits:
|
2019-03-07 19:18:14 -08:00
|
|
|
raise ValueError("Cannot limit: {} not present in fvar".format(bad_limits))
|
2019-03-06 21:54:15 -08:00
|
|
|
|
2019-03-07 19:18:14 -08:00
|
|
|
axes = {
|
|
|
|
a.axisTag: (a.minValue, a.defaultValue, a.maxValue)
|
|
|
|
for a in fvar.axes
|
|
|
|
if a.axisTag in axis_limits
|
|
|
|
}
|
2019-03-06 21:54:15 -08:00
|
|
|
|
|
|
|
avar_segments = {}
|
2019-03-07 19:18:14 -08:00
|
|
|
if "avar" in varfont:
|
|
|
|
avar_segments = varfont["avar"].segments
|
2019-03-06 21:54:15 -08:00
|
|
|
for axis_tag, triple in axes.items():
|
|
|
|
avar_mapping = avar_segments.get(axis_tag, None)
|
2019-03-12 17:59:11 +00:00
|
|
|
value = axis_limits[axis_tag]
|
|
|
|
if isinstance(value, tuple):
|
|
|
|
axis_limits[axis_tag] = tuple(
|
|
|
|
normalize(v, triple, avar_mapping) for v in axis_limits[axis_tag]
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
axis_limits[axis_tag] = normalize(value, triple, avar_mapping)
|
2019-03-06 21:54:15 -08:00
|
|
|
|
2019-03-06 21:58:58 -08:00
|
|
|
|
|
|
|
def sanityCheckVariableTables(varfont):
|
|
|
|
if "fvar" not in varfont:
|
|
|
|
raise ValueError("Missing required table fvar")
|
|
|
|
if "gvar" in varfont:
|
|
|
|
if "glyf" not in varfont:
|
|
|
|
raise ValueError("Can't have gvar without glyf")
|
|
|
|
|
2019-03-07 19:18:14 -08:00
|
|
|
|
2019-03-06 21:54:15 -08:00
|
|
|
def instantiateVariableFont(varfont, axis_limits, inplace=False):
|
2019-03-06 21:58:58 -08:00
|
|
|
sanityCheckVariableTables(varfont)
|
|
|
|
|
2019-03-06 21:54:15 -08:00
|
|
|
if not inplace:
|
|
|
|
varfont = deepcopy(varfont)
|
|
|
|
normalizeAxisLimits(varfont, axis_limits)
|
|
|
|
|
|
|
|
log.info("Normalized limits: %s", axis_limits)
|
2019-03-06 17:43:28 -08:00
|
|
|
|
2019-03-12 17:59:11 +00:00
|
|
|
# TODO Remove this check once ranges are supported
|
|
|
|
if any(isinstance(v, tuple) for v in axis_limits.values()):
|
|
|
|
raise NotImplementedError("Axes range limits are not supported yet")
|
|
|
|
|
2019-03-06 17:43:28 -08:00
|
|
|
if "gvar" in varfont:
|
2019-03-06 21:54:15 -08:00
|
|
|
instantiateGvar(varfont, axis_limits)
|
2019-03-14 10:59:15 -04:00
|
|
|
|
|
|
|
if "cvar" in varfont:
|
|
|
|
instantiateCvar(varfont, axis_limits)
|
2019-03-19 10:44:39 -04:00
|
|
|
|
|
|
|
if "MVAR" in varfont:
|
|
|
|
instantiateMvar(varfont, axis_limits)
|
2019-03-25 16:14:57 -04:00
|
|
|
|
2019-03-26 10:14:16 +00:00
|
|
|
instantiateFeatureVariations(varfont, axis_limits)
|
2019-03-06 17:43:28 -08:00
|
|
|
|
|
|
|
# TODO: actually process HVAR instead of dropping it
|
|
|
|
del varfont["HVAR"]
|
|
|
|
|
|
|
|
return varfont
|
|
|
|
|
|
|
|
|
2019-03-06 21:54:15 -08:00
|
|
|
def parseLimits(limits):
|
|
|
|
result = {}
|
|
|
|
for limit_string in limits:
|
2019-03-07 19:18:14 -08:00
|
|
|
match = re.match(r"^(\w{1,4})=([^:]+)(?:[:](.+))?$", limit_string)
|
2019-03-06 21:54:15 -08:00
|
|
|
if not match:
|
2019-03-22 15:27:58 +00:00
|
|
|
raise ValueError("invalid location format: %r" % limit_string)
|
2019-03-06 21:54:15 -08:00
|
|
|
tag = match.group(1).ljust(4)
|
|
|
|
lbound = float(match.group(2))
|
|
|
|
ubound = lbound
|
|
|
|
if match.group(3):
|
|
|
|
ubound = float(match.group(3))
|
2019-03-12 17:59:11 +00:00
|
|
|
if lbound != ubound:
|
|
|
|
result[tag] = (lbound, ubound)
|
|
|
|
else:
|
|
|
|
result[tag] = lbound
|
2019-03-06 21:54:15 -08:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def parseArgs(args):
|
|
|
|
"""Parse argv.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
3-tuple (infile, outfile, axis_limits)
|
2019-03-12 17:59:11 +00:00
|
|
|
axis_limits is either a Dict[str, int], for pinning variation axes to specific
|
|
|
|
coordinates along those axes; or a Dict[str, Tuple(int, int)], meaning limit
|
|
|
|
this axis to min/max range.
|
2019-03-12 19:02:14 +00:00
|
|
|
Axes locations are in user-space coordinates, as defined in the "fvar" table.
|
2019-03-12 17:59:11 +00:00
|
|
|
"""
|
2019-03-06 17:43:28 -08:00
|
|
|
from fontTools import configLogger
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
2019-03-21 15:30:48 +00:00
|
|
|
"fonttools varLib.instancer",
|
2019-03-07 19:18:14 -08:00
|
|
|
description="Partially instantiate a variable font",
|
2019-03-06 17:43:28 -08:00
|
|
|
)
|
2019-03-07 19:18:14 -08:00
|
|
|
parser.add_argument("input", metavar="INPUT.ttf", help="Input variable TTF file.")
|
2019-03-06 17:43:28 -08:00
|
|
|
parser.add_argument(
|
2019-03-07 19:18:14 -08:00
|
|
|
"locargs",
|
|
|
|
metavar="AXIS=LOC",
|
|
|
|
nargs="*",
|
2019-03-06 17:43:28 -08:00
|
|
|
help="List of space separated locations. A location consist in "
|
2019-03-12 17:59:11 +00:00
|
|
|
"the tag of a variation axis, followed by '=' and a number or"
|
2019-03-07 19:18:14 -08:00
|
|
|
"number:number. E.g.: wdth=100 or wght=75.0:125.0",
|
|
|
|
)
|
2019-03-06 17:43:28 -08:00
|
|
|
parser.add_argument(
|
2019-03-07 19:18:14 -08:00
|
|
|
"-o",
|
|
|
|
"--output",
|
|
|
|
metavar="OUTPUT.ttf",
|
|
|
|
default=None,
|
|
|
|
help="Output instance TTF file (default: INPUT-instance.ttf).",
|
|
|
|
)
|
2019-03-06 17:43:28 -08:00
|
|
|
logging_group = parser.add_mutually_exclusive_group(required=False)
|
|
|
|
logging_group.add_argument(
|
2019-03-07 19:18:14 -08:00
|
|
|
"-v", "--verbose", action="store_true", help="Run more verbosely."
|
|
|
|
)
|
2019-03-06 17:43:28 -08:00
|
|
|
logging_group.add_argument(
|
2019-03-07 19:18:14 -08:00
|
|
|
"-q", "--quiet", action="store_true", help="Turn verbosity off."
|
|
|
|
)
|
2019-03-06 17:43:28 -08:00
|
|
|
options = parser.parse_args(args)
|
|
|
|
|
2019-03-06 21:54:15 -08:00
|
|
|
infile = options.input
|
2019-03-06 17:43:28 -08:00
|
|
|
outfile = (
|
2019-03-08 10:37:11 -08:00
|
|
|
os.path.splitext(infile)[0] + "-instance.ttf"
|
2019-03-07 19:18:14 -08:00
|
|
|
if not options.output
|
|
|
|
else options.output
|
|
|
|
)
|
2019-03-06 17:43:28 -08:00
|
|
|
configLogger(
|
2019-03-07 19:18:14 -08:00
|
|
|
level=("DEBUG" if options.verbose else "ERROR" if options.quiet else "INFO")
|
2019-03-06 17:43:28 -08:00
|
|
|
)
|
|
|
|
|
2019-03-06 21:54:15 -08:00
|
|
|
axis_limits = parseLimits(options.locargs)
|
|
|
|
if len(axis_limits) != len(options.locargs):
|
2019-03-07 19:18:14 -08:00
|
|
|
raise ValueError("Specified multiple limits for the same axis")
|
2019-03-06 21:54:15 -08:00
|
|
|
return (infile, outfile, axis_limits)
|
|
|
|
|
|
|
|
|
|
|
|
def main(args=None):
|
|
|
|
infile, outfile, axis_limits = parseArgs(args)
|
|
|
|
log.info("Restricting axes: %s", axis_limits)
|
2019-03-06 17:43:28 -08:00
|
|
|
|
|
|
|
log.info("Loading variable font")
|
2019-03-06 21:54:15 -08:00
|
|
|
varfont = TTFont(infile)
|
2019-03-06 17:43:28 -08:00
|
|
|
|
2019-03-06 21:54:15 -08:00
|
|
|
instantiateVariableFont(varfont, axis_limits, inplace=True)
|
2019-03-06 17:43:28 -08:00
|
|
|
|
|
|
|
log.info("Saving partial variable font %s", outfile)
|
|
|
|
varfont.save(outfile)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import sys
|
2019-03-07 19:18:14 -08:00
|
|
|
|
2019-03-06 17:43:28 -08:00
|
|
|
sys.exit(main())
|