As recommended from anthrotype I did: $ black Lib/fontTools/otlLib/builder.py
This commit is contained in:
parent
68fa3bbd47
commit
cf6d25cc9e
@ -11,7 +11,11 @@ from fontTools.ttLib.tables.otBase import (
|
|||||||
)
|
)
|
||||||
from fontTools.ttLib.tables import otBase
|
from fontTools.ttLib.tables import otBase
|
||||||
from fontTools.feaLib.ast import STATNameStatement
|
from fontTools.feaLib.ast import STATNameStatement
|
||||||
from fontTools.otlLib.optimize.gpos import GPOS_COMPACT_MODE_DEFAULT, GPOS_COMPACT_MODE_ENV_KEY, compact_lookup
|
from fontTools.otlLib.optimize.gpos import (
|
||||||
|
GPOS_COMPACT_MODE_DEFAULT,
|
||||||
|
GPOS_COMPACT_MODE_ENV_KEY,
|
||||||
|
compact_lookup,
|
||||||
|
)
|
||||||
from fontTools.otlLib.error import OpenTypeLibError
|
from fontTools.otlLib.error import OpenTypeLibError
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
import logging
|
import logging
|
||||||
@ -962,14 +966,18 @@ class MarkBasePosBuilder(LookupBuilder):
|
|||||||
marks = {}
|
marks = {}
|
||||||
for mark, (mc, anchor) in self.marks.items():
|
for mark, (mc, anchor) in self.marks.items():
|
||||||
if mc not in markClasses:
|
if mc not in markClasses:
|
||||||
raise ValueError("Mark class %s not found for mark glyph %s" % (mc, mark))
|
raise ValueError(
|
||||||
|
"Mark class %s not found for mark glyph %s" % (mc, mark)
|
||||||
|
)
|
||||||
marks[mark] = (markClasses[mc], anchor)
|
marks[mark] = (markClasses[mc], anchor)
|
||||||
bases = {}
|
bases = {}
|
||||||
for glyph, anchors in self.bases.items():
|
for glyph, anchors in self.bases.items():
|
||||||
bases[glyph] = {}
|
bases[glyph] = {}
|
||||||
for mc, anchor in anchors.items():
|
for mc, anchor in anchors.items():
|
||||||
if mc not in markClasses:
|
if mc not in markClasses:
|
||||||
raise ValueError("Mark class %s not found for base glyph %s" % (mc, mark))
|
raise ValueError(
|
||||||
|
"Mark class %s not found for base glyph %s" % (mc, mark)
|
||||||
|
)
|
||||||
bases[glyph][markClasses[mc]] = anchor
|
bases[glyph][markClasses[mc]] = anchor
|
||||||
subtables = buildMarkBasePos(marks, bases, self.glyphMap)
|
subtables = buildMarkBasePos(marks, bases, self.glyphMap)
|
||||||
return self.buildLookup_(subtables)
|
return self.buildLookup_(subtables)
|
||||||
@ -2123,8 +2131,16 @@ def buildPairPosClassesSubtable(pairs, glyphMap, valueFormat1=None, valueFormat2
|
|||||||
for c2 in classes2:
|
for c2 in classes2:
|
||||||
rec2 = ot.Class2Record()
|
rec2 = ot.Class2Record()
|
||||||
val1, val2 = pairs.get((c1, c2), (None, None))
|
val1, val2 = pairs.get((c1, c2), (None, None))
|
||||||
rec2.Value1 = ValueRecord(src=val1, valueFormat=valueFormat1) if valueFormat1 else None
|
rec2.Value1 = (
|
||||||
rec2.Value2 = ValueRecord(src=val2, valueFormat=valueFormat2) if valueFormat2 else None
|
ValueRecord(src=val1, valueFormat=valueFormat1)
|
||||||
|
if valueFormat1
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
rec2.Value2 = (
|
||||||
|
ValueRecord(src=val2, valueFormat=valueFormat2)
|
||||||
|
if valueFormat2
|
||||||
|
else None
|
||||||
|
)
|
||||||
rec1.Class2Record.append(rec2)
|
rec1.Class2Record.append(rec2)
|
||||||
self.Class1Count = len(self.Class1Record)
|
self.Class1Count = len(self.Class1Record)
|
||||||
self.Class2Count = len(classes2)
|
self.Class2Count = len(classes2)
|
||||||
@ -2223,8 +2239,16 @@ def buildPairPosGlyphsSubtable(pairs, glyphMap, valueFormat1=None, valueFormat2=
|
|||||||
for glyph2, val1, val2 in sorted(p[glyph], key=lambda x: glyphMap[x[0]]):
|
for glyph2, val1, val2 in sorted(p[glyph], key=lambda x: glyphMap[x[0]]):
|
||||||
pvr = ot.PairValueRecord()
|
pvr = ot.PairValueRecord()
|
||||||
pvr.SecondGlyph = glyph2
|
pvr.SecondGlyph = glyph2
|
||||||
pvr.Value1 = ValueRecord(src=val1, valueFormat=valueFormat1) if valueFormat1 else None
|
pvr.Value1 = (
|
||||||
pvr.Value2 = ValueRecord(src=val2, valueFormat=valueFormat2) if valueFormat2 else None
|
ValueRecord(src=val1, valueFormat=valueFormat1)
|
||||||
|
if valueFormat1
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
pvr.Value2 = (
|
||||||
|
ValueRecord(src=val2, valueFormat=valueFormat2)
|
||||||
|
if valueFormat2
|
||||||
|
else None
|
||||||
|
)
|
||||||
ps.PairValueRecord.append(pvr)
|
ps.PairValueRecord.append(pvr)
|
||||||
ps.PairValueCount = len(ps.PairValueRecord)
|
ps.PairValueCount = len(ps.PairValueRecord)
|
||||||
self.PairSetCount = len(self.PairSet)
|
self.PairSetCount = len(self.PairSet)
|
||||||
@ -2345,8 +2369,13 @@ def buildSinglePosSubtable(values, glyphMap):
|
|||||||
"""
|
"""
|
||||||
self = ot.SinglePos()
|
self = ot.SinglePos()
|
||||||
self.Coverage = buildCoverage(values.keys(), glyphMap)
|
self.Coverage = buildCoverage(values.keys(), glyphMap)
|
||||||
valueFormat = self.ValueFormat = reduce(int.__or__, [v.getFormat() for v in values.values()], 0)
|
valueFormat = self.ValueFormat = reduce(
|
||||||
valueRecords = [ValueRecord(src=values[g], valueFormat=valueFormat) for g in self.Coverage.glyphs]
|
int.__or__, [v.getFormat() for v in values.values()], 0
|
||||||
|
)
|
||||||
|
valueRecords = [
|
||||||
|
ValueRecord(src=values[g], valueFormat=valueFormat)
|
||||||
|
for g in self.Coverage.glyphs
|
||||||
|
]
|
||||||
if all(v == valueRecords[0] for v in valueRecords):
|
if all(v == valueRecords[0] for v in valueRecords):
|
||||||
self.Format = 1
|
self.Format = 1
|
||||||
if self.ValueFormat != 0:
|
if self.ValueFormat != 0:
|
||||||
@ -2649,7 +2678,9 @@ AXIS_VALUE_NEGATIVE_INFINITY = fixedToFloat(-0x80000000, 16)
|
|||||||
AXIS_VALUE_POSITIVE_INFINITY = fixedToFloat(0x7FFFFFFF, 16)
|
AXIS_VALUE_POSITIVE_INFINITY = fixedToFloat(0x7FFFFFFF, 16)
|
||||||
|
|
||||||
|
|
||||||
def buildStatTable(ttFont, axes, locations=None, elidedFallbackName=2, windowsNames=True, macNames=True):
|
def buildStatTable(
|
||||||
|
ttFont, axes, locations=None, elidedFallbackName=2, windowsNames=True, macNames=True
|
||||||
|
):
|
||||||
"""Add a 'STAT' table to 'ttFont'.
|
"""Add a 'STAT' table to 'ttFont'.
|
||||||
|
|
||||||
'axes' is a list of dictionaries describing axes and their
|
'axes' is a list of dictionaries describing axes and their
|
||||||
@ -2734,17 +2765,23 @@ def buildStatTable(ttFont, axes, locations=None, elidedFallbackName=2, windowsNa
|
|||||||
ttFont["STAT"] = ttLib.newTable("STAT")
|
ttFont["STAT"] = ttLib.newTable("STAT")
|
||||||
statTable = ttFont["STAT"].table = ot.STAT()
|
statTable = ttFont["STAT"].table = ot.STAT()
|
||||||
nameTable = ttFont["name"]
|
nameTable = ttFont["name"]
|
||||||
statTable.ElidedFallbackNameID = _addName(nameTable, elidedFallbackName, windows=windowsNames, mac=macNames)
|
statTable.ElidedFallbackNameID = _addName(
|
||||||
|
nameTable, elidedFallbackName, windows=windowsNames, mac=macNames
|
||||||
|
)
|
||||||
|
|
||||||
# 'locations' contains data for AxisValue Format 4
|
# 'locations' contains data for AxisValue Format 4
|
||||||
axisRecords, axisValues = _buildAxisRecords(axes, nameTable, windowsNames=windowsNames, macNames=macNames)
|
axisRecords, axisValues = _buildAxisRecords(
|
||||||
|
axes, nameTable, windowsNames=windowsNames, macNames=macNames
|
||||||
|
)
|
||||||
if not locations:
|
if not locations:
|
||||||
statTable.Version = 0x00010001
|
statTable.Version = 0x00010001
|
||||||
else:
|
else:
|
||||||
# We'll be adding Format 4 AxisValue records, which
|
# We'll be adding Format 4 AxisValue records, which
|
||||||
# requires a higher table version
|
# requires a higher table version
|
||||||
statTable.Version = 0x00010002
|
statTable.Version = 0x00010002
|
||||||
multiAxisValues = _buildAxisValuesFormat4(locations, axes, nameTable, windowsNames=windowsNames, macNames=macNames)
|
multiAxisValues = _buildAxisValuesFormat4(
|
||||||
|
locations, axes, nameTable, windowsNames=windowsNames, macNames=macNames
|
||||||
|
)
|
||||||
axisValues = multiAxisValues + axisValues
|
axisValues = multiAxisValues + axisValues
|
||||||
|
|
||||||
# Store AxisRecords
|
# Store AxisRecords
|
||||||
@ -2769,7 +2806,9 @@ def _buildAxisRecords(axes, nameTable, windowsNames=True, macNames=True):
|
|||||||
for axisRecordIndex, axisDict in enumerate(axes):
|
for axisRecordIndex, axisDict in enumerate(axes):
|
||||||
axis = ot.AxisRecord()
|
axis = ot.AxisRecord()
|
||||||
axis.AxisTag = axisDict["tag"]
|
axis.AxisTag = axisDict["tag"]
|
||||||
axis.AxisNameID = _addName(nameTable, axisDict["name"], 256, windows=windowsNames, mac=macNames)
|
axis.AxisNameID = _addName(
|
||||||
|
nameTable, axisDict["name"], 256, windows=windowsNames, mac=macNames
|
||||||
|
)
|
||||||
axis.AxisOrdering = axisDict.get("ordering", axisRecordIndex)
|
axis.AxisOrdering = axisDict.get("ordering", axisRecordIndex)
|
||||||
axisRecords.append(axis)
|
axisRecords.append(axis)
|
||||||
|
|
||||||
@ -2777,7 +2816,9 @@ def _buildAxisRecords(axes, nameTable, windowsNames=True, macNames=True):
|
|||||||
axisValRec = ot.AxisValue()
|
axisValRec = ot.AxisValue()
|
||||||
axisValRec.AxisIndex = axisRecordIndex
|
axisValRec.AxisIndex = axisRecordIndex
|
||||||
axisValRec.Flags = axisVal.get("flags", 0)
|
axisValRec.Flags = axisVal.get("flags", 0)
|
||||||
axisValRec.ValueNameID = _addName(nameTable, axisVal["name"], windows=windowsNames, mac=macNames)
|
axisValRec.ValueNameID = _addName(
|
||||||
|
nameTable, axisVal["name"], windows=windowsNames, mac=macNames
|
||||||
|
)
|
||||||
|
|
||||||
if "value" in axisVal:
|
if "value" in axisVal:
|
||||||
axisValRec.Value = axisVal["value"]
|
axisValRec.Value = axisVal["value"]
|
||||||
@ -2802,7 +2843,9 @@ def _buildAxisRecords(axes, nameTable, windowsNames=True, macNames=True):
|
|||||||
return axisRecords, axisValues
|
return axisRecords, axisValues
|
||||||
|
|
||||||
|
|
||||||
def _buildAxisValuesFormat4(locations, axes, nameTable, windowsNames=True, macNames=True):
|
def _buildAxisValuesFormat4(
|
||||||
|
locations, axes, nameTable, windowsNames=True, macNames=True
|
||||||
|
):
|
||||||
axisTagToIndex = {}
|
axisTagToIndex = {}
|
||||||
for axisRecordIndex, axisDict in enumerate(axes):
|
for axisRecordIndex, axisDict in enumerate(axes):
|
||||||
axisTagToIndex[axisDict["tag"]] = axisRecordIndex
|
axisTagToIndex[axisDict["tag"]] = axisRecordIndex
|
||||||
@ -2811,7 +2854,9 @@ def _buildAxisValuesFormat4(locations, axes, nameTable, windowsNames=True, macNa
|
|||||||
for axisLocationDict in locations:
|
for axisLocationDict in locations:
|
||||||
axisValRec = ot.AxisValue()
|
axisValRec = ot.AxisValue()
|
||||||
axisValRec.Format = 4
|
axisValRec.Format = 4
|
||||||
axisValRec.ValueNameID = _addName(nameTable, axisLocationDict["name"], windows=windowsNames, mac=macNames)
|
axisValRec.ValueNameID = _addName(
|
||||||
|
nameTable, axisLocationDict["name"], windows=windowsNames, mac=macNames
|
||||||
|
)
|
||||||
axisValRec.Flags = axisLocationDict.get("flags", 0)
|
axisValRec.Flags = axisLocationDict.get("flags", 0)
|
||||||
axisValueRecords = []
|
axisValueRecords = []
|
||||||
for tag, value in axisLocationDict["location"].items():
|
for tag, value in axisLocationDict["location"].items():
|
||||||
@ -2850,4 +2895,6 @@ def _addName(nameTable, value, minNameID=0, windows=True, mac=True):
|
|||||||
return nameID
|
return nameID
|
||||||
else:
|
else:
|
||||||
raise TypeError("value must be int, str, dict or list")
|
raise TypeError("value must be int, str, dict or list")
|
||||||
return nameTable.addMultilingualName(names, windows=windows, mac=mac, minNameID=minNameID)
|
return nameTable.addMultilingualName(
|
||||||
|
names, windows=windows, mac=mac, minNameID=minNameID
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user