2022-08-15 11:06:51 -06:00
|
|
|
"""GlyphSets returned by a TTFont."""
|
|
|
|
|
|
|
|
from fontTools.misc.fixedTools import otRound
|
|
|
|
from copy import copy
|
2022-08-12 12:29:01 -06:00
|
|
|
|
2022-08-27 12:25:32 -06:00
|
|
|
|
2022-08-12 12:29:01 -06:00
|
|
|
class _TTGlyphSet(object):
|
|
|
|
|
|
|
|
"""Generic dict-like GlyphSet class that pulls metrics from hmtx and
|
|
|
|
glyph shape from TrueType or CFF.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, ttFont, glyphs, glyphType):
|
|
|
|
"""Construct a new glyphset.
|
|
|
|
|
|
|
|
Args:
|
2022-08-27 12:25:32 -06:00
|
|
|
font (TTFont): The font object (used to get metrics).
|
|
|
|
glyphs (dict): A dictionary mapping glyph names to ``_TTGlyph`` objects.
|
|
|
|
glyphType (class): Either ``_TTGlyphCFF`` or ``_TTGlyphGlyf``.
|
2022-08-12 12:29:01 -06:00
|
|
|
"""
|
|
|
|
self._glyphs = glyphs
|
2022-08-27 12:25:32 -06:00
|
|
|
self._hmtx = ttFont["hmtx"]
|
|
|
|
self._vmtx = ttFont["vmtx"] if "vmtx" in ttFont else None
|
2022-08-12 12:29:01 -06:00
|
|
|
self._glyphType = glyphType
|
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
return list(self._glyphs.keys())
|
|
|
|
|
|
|
|
def has_key(self, glyphName):
|
|
|
|
return glyphName in self._glyphs
|
|
|
|
|
|
|
|
__contains__ = has_key
|
|
|
|
|
|
|
|
def __getitem__(self, glyphName):
|
|
|
|
horizontalMetrics = self._hmtx[glyphName]
|
|
|
|
verticalMetrics = self._vmtx[glyphName] if self._vmtx else None
|
|
|
|
return self._glyphType(
|
2022-08-27 12:25:32 -06:00
|
|
|
self, self._glyphs[glyphName], horizontalMetrics, verticalMetrics
|
|
|
|
)
|
2022-08-12 12:29:01 -06:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._glyphs)
|
|
|
|
|
|
|
|
def get(self, glyphName, default=None):
|
|
|
|
try:
|
|
|
|
return self[glyphName]
|
|
|
|
except KeyError:
|
|
|
|
return default
|
|
|
|
|
2022-08-27 12:25:32 -06:00
|
|
|
|
2022-08-12 12:29:01 -06:00
|
|
|
class _TTGlyph(object):
|
|
|
|
|
|
|
|
"""Wrapper for a TrueType glyph that supports the Pen protocol, meaning
|
|
|
|
that it has .draw() and .drawPoints() methods that take a pen object as
|
|
|
|
their only argument. Additionally there are 'width' and 'lsb' attributes,
|
|
|
|
read from the 'hmtx' table.
|
|
|
|
|
|
|
|
If the font contains a 'vmtx' table, there will also be 'height' and 'tsb'
|
|
|
|
attributes.
|
|
|
|
"""
|
|
|
|
|
2022-08-26 20:33:03 -06:00
|
|
|
def __init__(self, glyphset, glyph, horizontalMetrics=None, verticalMetrics=None):
|
2022-08-12 12:29:01 -06:00
|
|
|
"""Construct a new _TTGlyph.
|
|
|
|
|
|
|
|
Args:
|
2022-08-27 12:25:32 -06:00
|
|
|
glyphset (_TTGlyphSet): A glyphset object used to resolve components.
|
|
|
|
glyph (ttLib.tables._g_l_y_f.Glyph): The glyph object.
|
|
|
|
horizontalMetrics (int, int): The glyph's width and left sidebearing.
|
2022-08-12 12:29:01 -06:00
|
|
|
"""
|
|
|
|
self._glyphset = glyphset
|
|
|
|
self._glyph = glyph
|
2022-08-26 20:33:03 -06:00
|
|
|
if horizontalMetrics:
|
|
|
|
self.width, self.lsb = horizontalMetrics
|
|
|
|
else:
|
|
|
|
self.width, self.lsb = None, None
|
2022-08-12 12:29:01 -06:00
|
|
|
if verticalMetrics:
|
|
|
|
self.height, self.tsb = verticalMetrics
|
|
|
|
else:
|
|
|
|
self.height, self.tsb = None, None
|
|
|
|
|
|
|
|
def draw(self, pen):
|
|
|
|
"""Draw the glyph onto ``pen``. See fontTools.pens.basePen for details
|
|
|
|
how that works.
|
|
|
|
"""
|
|
|
|
self._glyph.draw(pen)
|
|
|
|
|
|
|
|
def drawPoints(self, pen):
|
2022-08-24 14:48:58 +02:00
|
|
|
from fontTools.pens.pointPen import SegmentToPointPen
|
2022-08-27 12:25:32 -06:00
|
|
|
|
2022-08-24 14:48:58 +02:00
|
|
|
self.draw(SegmentToPointPen(pen))
|
2022-08-12 12:29:01 -06:00
|
|
|
|
2022-08-27 12:25:32 -06:00
|
|
|
|
2022-08-12 12:29:01 -06:00
|
|
|
class _TTGlyphCFF(_TTGlyph):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-08-27 12:25:32 -06:00
|
|
|
class _TTGlyphGlyf(_TTGlyph):
|
2022-08-12 12:29:01 -06:00
|
|
|
def draw(self, pen):
|
|
|
|
"""Draw the glyph onto Pen. See fontTools.pens.basePen for details
|
|
|
|
how that works.
|
|
|
|
"""
|
|
|
|
glyfTable = self._glyphset._glyphs
|
|
|
|
glyph = self._glyph
|
|
|
|
offset = self.lsb - glyph.xMin if hasattr(glyph, "xMin") else 0
|
|
|
|
glyph.draw(pen, glyfTable, offset)
|
|
|
|
|
|
|
|
def drawPoints(self, pen):
|
|
|
|
"""Draw the glyph onto PointPen. See fontTools.pens.pointPen
|
|
|
|
for details how that works.
|
|
|
|
"""
|
|
|
|
glyfTable = self._glyphset._glyphs
|
|
|
|
glyph = self._glyph
|
|
|
|
offset = self.lsb - glyph.xMin if hasattr(glyph, "xMin") else 0
|
|
|
|
glyph.drawPoints(pen, glyfTable, offset)
|
|
|
|
|
|
|
|
|
2022-08-22 16:51:48 +01:00
|
|
|
class _TTVarGlyphSet(_TTGlyphSet):
|
2022-08-26 20:33:03 -06:00
|
|
|
def __init__(self, font, glyphs, glyphType, location, normalized):
|
2022-08-12 13:36:34 -06:00
|
|
|
self._ttFont = font
|
2022-08-26 20:33:03 -06:00
|
|
|
self._glyphs = glyphs
|
|
|
|
self._glyphType = glyphType
|
2022-08-22 16:51:48 +01:00
|
|
|
|
2022-08-12 13:43:25 -06:00
|
|
|
if not normalized:
|
2022-08-22 16:51:48 +01:00
|
|
|
from fontTools.varLib.models import normalizeLocation, piecewiseLinearMap
|
|
|
|
|
2022-08-27 12:25:32 -06:00
|
|
|
axes = {
|
|
|
|
a.axisTag: (a.minValue, a.defaultValue, a.maxValue)
|
|
|
|
for a in font["fvar"].axes
|
|
|
|
}
|
2022-08-12 13:43:25 -06:00
|
|
|
location = normalizeLocation(location, axes)
|
2022-08-27 12:25:32 -06:00
|
|
|
if "avar" in font:
|
|
|
|
avar = font["avar"]
|
2022-08-13 15:37:12 -06:00
|
|
|
avarSegments = avar.segments
|
|
|
|
new_location = {}
|
2022-08-22 16:51:48 +01:00
|
|
|
for axis_tag, value in location.items():
|
2022-08-13 15:37:12 -06:00
|
|
|
avarMapping = avarSegments.get(axis_tag, None)
|
|
|
|
if avarMapping is not None:
|
|
|
|
value = piecewiseLinearMap(value, avarMapping)
|
|
|
|
new_location[axis_tag] = value
|
|
|
|
location = new_location
|
|
|
|
del new_location
|
|
|
|
|
2022-08-12 13:43:25 -06:00
|
|
|
self.location = location
|
2022-08-12 13:36:34 -06:00
|
|
|
|
|
|
|
def __getitem__(self, glyphName):
|
2022-08-22 16:51:48 +01:00
|
|
|
if glyphName not in self._glyphs:
|
|
|
|
raise KeyError(glyphName)
|
2022-08-27 12:25:32 -06:00
|
|
|
return self._glyphType(self, glyphName, self.location)
|
2022-08-12 13:36:34 -06:00
|
|
|
|
|
|
|
|
2022-08-15 11:06:51 -06:00
|
|
|
def _setCoordinates(glyph, coord, glyfTable):
|
2022-08-12 13:36:34 -06:00
|
|
|
# Handle phantom points for (left, right, top, bottom) positions.
|
|
|
|
assert len(coord) >= 4
|
2022-08-27 12:25:32 -06:00
|
|
|
if not hasattr(glyph, "xMin"):
|
2022-08-12 13:36:34 -06:00
|
|
|
glyph.recalcBounds(glyfTable)
|
|
|
|
leftSideX = coord[-4][0]
|
|
|
|
rightSideX = coord[-3][0]
|
|
|
|
topSideY = coord[-2][1]
|
|
|
|
bottomSideY = coord[-1][1]
|
|
|
|
|
|
|
|
for _ in range(4):
|
|
|
|
del coord[-1]
|
|
|
|
|
|
|
|
if glyph.isComposite():
|
|
|
|
assert len(coord) == len(glyph.components)
|
2022-08-24 13:30:09 -06:00
|
|
|
glyph.components = [copy(comp) for comp in glyph.components]
|
2022-08-27 12:25:32 -06:00
|
|
|
for p, comp in zip(coord, glyph.components):
|
|
|
|
if hasattr(comp, "x"):
|
|
|
|
comp.x, comp.y = p
|
2022-08-12 13:43:25 -06:00
|
|
|
elif glyph.numberOfContours == 0:
|
2022-08-12 13:36:34 -06:00
|
|
|
assert len(coord) == 0
|
|
|
|
else:
|
|
|
|
assert len(coord) == len(glyph.coordinates)
|
|
|
|
glyph.coordinates = coord
|
|
|
|
|
|
|
|
glyph.recalcBounds(glyfTable)
|
|
|
|
|
2022-08-15 11:06:51 -06:00
|
|
|
horizontalAdvanceWidth = otRound(rightSideX - leftSideX)
|
|
|
|
verticalAdvanceWidth = otRound(topSideY - bottomSideY)
|
|
|
|
leftSideBearing = otRound(glyph.xMin - leftSideX)
|
2022-08-22 16:51:48 +01:00
|
|
|
topSideBearing = otRound(topSideY - glyph.yMax)
|
|
|
|
return (
|
|
|
|
horizontalAdvanceWidth,
|
|
|
|
leftSideBearing,
|
|
|
|
verticalAdvanceWidth,
|
|
|
|
topSideBearing,
|
|
|
|
)
|
2022-08-12 13:36:34 -06:00
|
|
|
|
|
|
|
|
2022-08-22 16:51:48 +01:00
|
|
|
class _TTVarGlyph(_TTGlyph):
|
2022-08-27 12:25:32 -06:00
|
|
|
def __init__(self, glyphSet, glyphName, location):
|
2022-08-26 20:33:03 -06:00
|
|
|
|
2022-08-27 12:25:32 -06:00
|
|
|
super().__init__(glyphSet._glyphs, glyphSet._glyphs[glyphName])
|
2022-08-26 20:33:03 -06:00
|
|
|
|
2022-08-27 12:25:32 -06:00
|
|
|
self._glyphSet = glyphSet
|
|
|
|
self._ttFont = glyphSet._ttFont
|
|
|
|
self._glyphs = glyphSet._glyphs
|
2022-08-12 13:36:34 -06:00
|
|
|
self._glyphName = glyphName
|
|
|
|
self._location = location
|
2022-08-26 20:33:03 -06:00
|
|
|
|
|
|
|
|
|
|
|
class _TTVarGlyphCFF(_TTVarGlyph):
|
|
|
|
def draw(self, pen):
|
2022-08-26 21:00:37 -06:00
|
|
|
varStore = self._glyphs.varStore
|
2022-08-26 20:33:03 -06:00
|
|
|
if varStore is None:
|
|
|
|
blender = None
|
|
|
|
else:
|
2022-08-26 21:00:37 -06:00
|
|
|
from fontTools.varLib.varStore import VarStoreInstancer
|
2022-08-27 12:25:32 -06:00
|
|
|
|
|
|
|
vsInstancer = getattr(self._glyphSet, "vsInstancer", None)
|
|
|
|
if vsInstancer is None:
|
|
|
|
self._glyphSet.vsInstancer = vsInstancer = VarStoreInstancer(
|
|
|
|
varStore.otVarStore, self._ttFont["fvar"].axes, self._location
|
|
|
|
)
|
2022-08-26 20:33:03 -06:00
|
|
|
blender = vsInstancer.interpolateFromDeltas
|
|
|
|
self._glyph.draw(pen, blender)
|
2022-08-26 21:07:10 -06:00
|
|
|
|
2022-08-27 12:25:32 -06:00
|
|
|
self.width = self._ttFont["hmtx"][self._glyphName][0]
|
|
|
|
if "HVAR" in self._ttFont:
|
|
|
|
hvar = self._ttFont["HVAR"].table
|
2022-08-26 21:24:24 -06:00
|
|
|
varidx = self._ttFont.getGlyphID(self._glyphName)
|
|
|
|
if hvar.AdvWidthMap is not None:
|
|
|
|
varidx = hvar.AdvWidthMap.mapping[self._glyphName]
|
2022-08-27 12:25:32 -06:00
|
|
|
vsInstancer = VarStoreInstancer(
|
|
|
|
hvar.VarStore, self._ttFont["fvar"].axes, self._location
|
|
|
|
)
|
2022-08-26 21:24:24 -06:00
|
|
|
delta = vsInstancer[varidx]
|
2022-08-27 12:20:15 -06:00
|
|
|
self.width += delta
|
2022-08-22 16:51:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
class _TTVarGlyphGlyf(_TTVarGlyph):
|
2022-08-12 13:36:34 -06:00
|
|
|
def draw(self, pen):
|
2022-08-25 16:11:24 +02:00
|
|
|
self._drawWithPen(pen, isPointPen=False)
|
2022-08-25 15:21:54 +02:00
|
|
|
|
|
|
|
def drawPoints(self, pen):
|
2022-08-25 16:11:24 +02:00
|
|
|
self._drawWithPen(pen, isPointPen=True)
|
2022-08-25 15:21:54 +02:00
|
|
|
|
|
|
|
def _drawWithPen(self, pen, isPointPen):
|
2022-08-12 13:36:34 -06:00
|
|
|
from fontTools.varLib.iup import iup_delta
|
|
|
|
from fontTools.ttLib.tables._g_l_y_f import GlyphCoordinates
|
|
|
|
from fontTools.varLib.models import supportScalar
|
|
|
|
|
2022-08-27 12:25:32 -06:00
|
|
|
glyf = self._ttFont["glyf"]
|
|
|
|
hMetrics = self._ttFont["hmtx"].metrics
|
|
|
|
vMetrics = getattr(self._ttFont.get("vmtx"), "metrics", None)
|
2022-08-12 13:36:34 -06:00
|
|
|
|
2022-08-27 12:25:32 -06:00
|
|
|
variations = self._ttFont["gvar"].variations[self._glyphName]
|
|
|
|
coordinates, _ = glyf._getCoordinatesAndControls(
|
|
|
|
self._glyphName, hMetrics, vMetrics
|
|
|
|
)
|
2022-08-12 13:36:34 -06:00
|
|
|
origCoords, endPts = None, None
|
|
|
|
for var in variations:
|
|
|
|
scalar = supportScalar(self._location, var.axes)
|
|
|
|
if not scalar:
|
|
|
|
continue
|
|
|
|
delta = var.coordinates
|
|
|
|
if None in delta:
|
|
|
|
if origCoords is None:
|
2022-08-27 12:25:32 -06:00
|
|
|
origCoords, control = glyf._getCoordinatesAndControls(
|
|
|
|
self._glyphName, hMetrics, vMetrics
|
|
|
|
)
|
|
|
|
endPts = (
|
|
|
|
control[1] if control[0] >= 1 else list(range(len(control[1])))
|
|
|
|
)
|
2022-08-12 13:36:34 -06:00
|
|
|
delta = iup_delta(delta, origCoords, endPts)
|
|
|
|
coordinates += GlyphCoordinates(delta) * scalar
|
|
|
|
|
2022-08-27 12:25:32 -06:00
|
|
|
glyph = copy(glyf[self._glyphName]) # Shallow copy
|
2022-08-22 16:51:48 +01:00
|
|
|
width, lsb, height, tsb = _setCoordinates(glyph, coordinates, glyf)
|
|
|
|
self.width = width
|
|
|
|
self.lsb = lsb
|
|
|
|
self.height = height
|
|
|
|
self.tsb = tsb
|
|
|
|
offset = lsb - glyph.xMin if hasattr(glyph, "xMin") else 0
|
2022-08-25 15:21:54 +02:00
|
|
|
if isPointPen:
|
|
|
|
glyph.drawPoints(pen, glyf, offset)
|
|
|
|
else:
|
|
|
|
glyph.draw(pen, glyf, offset)
|