Reformat with black for ease of further maintenance
This commit is contained in:
parent
4a8617fc92
commit
04f04474fd
@ -1,4 +1,3 @@
|
||||
|
||||
__all__ = ["FontBuilder"]
|
||||
|
||||
"""
|
||||
@ -322,7 +321,6 @@ _OS2Defaults = dict(
|
||||
|
||||
|
||||
class FontBuilder(object):
|
||||
|
||||
def __init__(self, unitsPerEm=None, font=None, isTTF=True):
|
||||
"""Initialize a FontBuilder instance.
|
||||
|
||||
@ -396,7 +394,7 @@ class FontBuilder(object):
|
||||
"""
|
||||
subTables = []
|
||||
highestUnicode = max(cmapping)
|
||||
if highestUnicode > 0xffff:
|
||||
if highestUnicode > 0xFFFF:
|
||||
cmapping_3_1 = dict((k, v) for k, v in cmapping.items() if k < 0x10000)
|
||||
subTable_3_10 = buildCmapSubTable(cmapping, 12, 3, 10)
|
||||
subTables.append(subTable_3_10)
|
||||
@ -409,7 +407,9 @@ class FontBuilder(object):
|
||||
except struct.error:
|
||||
# format 4 overflowed, fall back to format 12
|
||||
if not allowFallback:
|
||||
raise ValueError("cmap format 4 subtable overflowed; sort glyph order by unicode to fix.")
|
||||
raise ValueError(
|
||||
"cmap format 4 subtable overflowed; sort glyph order by unicode to fix."
|
||||
)
|
||||
format = 12
|
||||
subTable_3_1 = buildCmapSubTable(cmapping_3_1, format, 3, 1)
|
||||
subTables.append(subTable_3_1)
|
||||
@ -490,17 +490,33 @@ class FontBuilder(object):
|
||||
"""
|
||||
if "xAvgCharWidth" not in values:
|
||||
gs = self.font.getGlyphSet()
|
||||
widths = [gs[glyphName].width for glyphName in gs.keys() if gs[glyphName].width > 0]
|
||||
widths = [
|
||||
gs[glyphName].width
|
||||
for glyphName in gs.keys()
|
||||
if gs[glyphName].width > 0
|
||||
]
|
||||
values["xAvgCharWidth"] = int(round(sum(widths) / float(len(widths))))
|
||||
self._initTableWithValues("OS/2", _OS2Defaults, values)
|
||||
if not ("ulUnicodeRange1" in values or "ulUnicodeRange2" in values or
|
||||
"ulUnicodeRange3" in values or "ulUnicodeRange3" in values):
|
||||
assert "cmap" in self.font, "the 'cmap' table must be setup before the 'OS/2' table"
|
||||
if not (
|
||||
"ulUnicodeRange1" in values
|
||||
or "ulUnicodeRange2" in values
|
||||
or "ulUnicodeRange3" in values
|
||||
or "ulUnicodeRange3" in values
|
||||
):
|
||||
assert (
|
||||
"cmap" in self.font
|
||||
), "the 'cmap' table must be setup before the 'OS/2' table"
|
||||
self.font["OS/2"].recalcUnicodeRanges(self.font)
|
||||
|
||||
def setupCFF(self, psName, fontInfo, charStringsDict, privateDict):
|
||||
from .cffLib import CFFFontSet, TopDictIndex, TopDict, CharStrings, \
|
||||
GlobalSubrsIndex, PrivateDict
|
||||
from .cffLib import (
|
||||
CFFFontSet,
|
||||
TopDictIndex,
|
||||
TopDict,
|
||||
CharStrings,
|
||||
GlobalSubrsIndex,
|
||||
PrivateDict,
|
||||
)
|
||||
|
||||
assert not self.isTTF
|
||||
self.font.sfntVersion = "OTTO"
|
||||
@ -529,7 +545,9 @@ class FontBuilder(object):
|
||||
scale = 1 / self.font["head"].unitsPerEm
|
||||
topDict.FontMatrix = [scale, 0, 0, scale, 0, 0]
|
||||
|
||||
charStrings = CharStrings(None, topDict.charset, globalSubrs, private, fdSelect, fdArray)
|
||||
charStrings = CharStrings(
|
||||
None, topDict.charset, globalSubrs, private, fdSelect, fdArray
|
||||
)
|
||||
for glyphName, charString in charStringsDict.items():
|
||||
charString.private = private
|
||||
charString.globalSubrs = globalSubrs
|
||||
@ -542,8 +560,16 @@ class FontBuilder(object):
|
||||
self.font["CFF "].cff = fontSet
|
||||
|
||||
def setupCFF2(self, charStringsDict, fdArrayList=None, regions=None):
|
||||
from .cffLib import CFFFontSet, TopDictIndex, TopDict, CharStrings, \
|
||||
GlobalSubrsIndex, PrivateDict, FDArrayIndex, FontDict
|
||||
from .cffLib import (
|
||||
CFFFontSet,
|
||||
TopDictIndex,
|
||||
TopDict,
|
||||
CharStrings,
|
||||
GlobalSubrsIndex,
|
||||
PrivateDict,
|
||||
FDArrayIndex,
|
||||
FontDict,
|
||||
)
|
||||
|
||||
assert not self.isTTF
|
||||
self.font.sfntVersion = "OTTO"
|
||||
@ -659,7 +685,7 @@ class FontBuilder(object):
|
||||
_add_avar(self.font, OrderedDict(enumerate(axes))) # Only values are used
|
||||
|
||||
def setupGvar(self, variations):
|
||||
gvar = self.font["gvar"] = newTable('gvar')
|
||||
gvar = self.font["gvar"] = newTable("gvar")
|
||||
gvar.version = 1
|
||||
gvar.reserved = 0
|
||||
gvar.variations = variations
|
||||
@ -678,7 +704,7 @@ class FontBuilder(object):
|
||||
The `metrics` argument must be a dict, mapping glyph names to
|
||||
`(width, leftSidebearing)` tuples.
|
||||
"""
|
||||
self.setupMetrics('hmtx', metrics)
|
||||
self.setupMetrics("hmtx", metrics)
|
||||
|
||||
def setupVerticalMetrics(self, metrics):
|
||||
"""Create a new `vmtx` table, for horizontal metrics.
|
||||
@ -686,7 +712,7 @@ class FontBuilder(object):
|
||||
The `metrics` argument must be a dict, mapping glyph names to
|
||||
`(height, topSidebearing)` tuples.
|
||||
"""
|
||||
self.setupMetrics('vmtx', metrics)
|
||||
self.setupMetrics("vmtx", metrics)
|
||||
|
||||
def setupMetrics(self, tableTag, metrics):
|
||||
"""See `setupHorizontalMetrics()` and `setupVerticalMetrics()`."""
|
||||
@ -727,8 +753,14 @@ class FontBuilder(object):
|
||||
bag[vorg] = 1
|
||||
else:
|
||||
bag[vorg] += 1
|
||||
defaultVerticalOrigin = sorted(bag, key=lambda vorg: bag[vorg], reverse=True)[0]
|
||||
self._initTableWithValues("VORG", {}, dict(VOriginRecords={}, defaultVertOriginY=defaultVerticalOrigin))
|
||||
defaultVerticalOrigin = sorted(
|
||||
bag, key=lambda vorg: bag[vorg], reverse=True
|
||||
)[0]
|
||||
self._initTableWithValues(
|
||||
"VORG",
|
||||
{},
|
||||
dict(VOriginRecords={}, defaultVertOriginY=defaultVerticalOrigin),
|
||||
)
|
||||
vorgTable = self.font["VORG"]
|
||||
vorgTable.majorVersion = 1
|
||||
vorgTable.minorVersion = 0
|
||||
@ -739,7 +771,7 @@ class FontBuilder(object):
|
||||
"""Create a new `post` table and initialize it with default values,
|
||||
which can be overridden by keyword arguments.
|
||||
"""
|
||||
isCFF2 = 'CFF2' in self.font
|
||||
isCFF2 = "CFF2" in self.font
|
||||
postTable = self._initTableWithValues("post", _postDefaults, values)
|
||||
if (self.isTTF or isCFF2) and keepGlyphNames:
|
||||
postTable.formatType = 2.0
|
||||
@ -782,7 +814,10 @@ class FontBuilder(object):
|
||||
`fontTools.feaLib` for details.
|
||||
"""
|
||||
from .feaLib.builder import addOpenTypeFeaturesFromString
|
||||
addOpenTypeFeaturesFromString(self.font, features, filename=filename, tables=tables)
|
||||
|
||||
addOpenTypeFeaturesFromString(
|
||||
self.font, features, filename=filename, tables=tables
|
||||
)
|
||||
|
||||
def addFeatureVariations(self, conditionalSubstitutions, featureTag="rvrn"):
|
||||
"""Add conditional substitutions to a Variable Font.
|
||||
@ -831,7 +866,7 @@ class FontBuilder(object):
|
||||
paletteTypes=paletteTypes,
|
||||
paletteLabels=paletteLabels,
|
||||
paletteEntryLabels=paletteEntryLabels,
|
||||
nameTable=self.font.get("name")
|
||||
nameTable=self.font.get("name"),
|
||||
)
|
||||
|
||||
def setupStat(self, axes, locations=None, elidedFallbackName=2):
|
||||
@ -841,6 +876,7 @@ class FontBuilder(object):
|
||||
the arguments.
|
||||
"""
|
||||
from .otlLib.builder import buildStatTable
|
||||
|
||||
buildStatTable(self.font, axes, locations, elidedFallbackName)
|
||||
|
||||
|
||||
@ -889,9 +925,9 @@ def addFvar(font, axes, instances):
|
||||
fvar.axes.append(axis)
|
||||
|
||||
for instance in instances:
|
||||
coordinates = instance['location']
|
||||
name = instance['stylename']
|
||||
psname = instance.get('postscriptfontname')
|
||||
coordinates = instance["location"]
|
||||
name = instance["stylename"]
|
||||
psname = instance.get("postscriptfontname")
|
||||
|
||||
inst = NamedInstance()
|
||||
inst.subfamilyNameID = nameTable.addName(name)
|
||||
@ -901,4 +937,4 @@ def addFvar(font, axes, instances):
|
||||
inst.coordinates = coordinates
|
||||
fvar.instances.append(inst)
|
||||
|
||||
font['fvar'] = fvar
|
||||
font["fvar"] = fvar
|
||||
|
Loading…
x
Reference in New Issue
Block a user