2013-09-17 16:59:39 -04:00
|
|
|
from fontTools.misc import sstruct
|
1999-12-16 21:34:53 +00:00
|
|
|
from fontTools.misc.textTools import safeEval
|
2016-10-16 06:31:52 +01:00
|
|
|
from fontTools.misc.fixedTools import (
|
|
|
|
ensureVersionIsLong as fi2ve,
|
|
|
|
versionToFixed as ve2fi,
|
|
|
|
)
|
2013-11-27 17:27:45 -05:00
|
|
|
from . import DefaultTable
|
2017-05-19 15:12:59 +09:00
|
|
|
import math
|
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
|
|
|
|
hheaFormat = """
|
2002-05-13 18:08:19 +00:00
|
|
|
> # big endian
|
2016-10-16 06:31:52 +01:00
|
|
|
tableVersion: L
|
2002-05-13 18:08:19 +00:00
|
|
|
ascent: h
|
|
|
|
descent: h
|
|
|
|
lineGap: h
|
|
|
|
advanceWidthMax: H
|
|
|
|
minLeftSideBearing: h
|
|
|
|
minRightSideBearing: h
|
|
|
|
xMaxExtent: h
|
|
|
|
caretSlopeRise: h
|
|
|
|
caretSlopeRun: h
|
2003-09-01 15:09:29 +00:00
|
|
|
caretOffset: h
|
2002-05-13 18:08:19 +00:00
|
|
|
reserved0: h
|
|
|
|
reserved1: h
|
|
|
|
reserved2: h
|
|
|
|
reserved3: h
|
|
|
|
metricDataFormat: h
|
|
|
|
numberOfHMetrics: H
|
1999-12-16 21:34:53 +00:00
|
|
|
"""
|
|
|
|
|
2002-05-13 18:08:19 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
class table__h_h_e_a(DefaultTable.DefaultTable):
|
2014-06-02 18:23:45 -04:00
|
|
|
# Note: Keep in sync with table__v_h_e_a
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2020-01-31 04:13:48 +02:00
|
|
|
dependencies = ["hmtx", "glyf", "CFF ", "CFF2"]
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2019-09-05 12:38:45 +01:00
|
|
|
# OpenType spec renamed these, add aliases for compatibility
|
|
|
|
@property
|
|
|
|
def ascender(self):
|
|
|
|
return self.ascent
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2019-09-05 12:38:45 +01:00
|
|
|
@ascender.setter
|
|
|
|
def ascender(self, value):
|
|
|
|
self.ascent = value
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2019-09-05 12:38:45 +01:00
|
|
|
@property
|
|
|
|
def descender(self):
|
|
|
|
return self.descent
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2019-09-05 12:38:45 +01:00
|
|
|
@descender.setter
|
|
|
|
def descender(self, value):
|
|
|
|
self.descent = value
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def decompile(self, data, ttFont):
|
2002-05-13 18:13:03 +00:00
|
|
|
sstruct.unpack(hheaFormat, data, self)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def compile(self, ttFont):
|
2020-01-31 04:13:48 +02:00
|
|
|
if ttFont.recalcBBoxes and (
|
|
|
|
ttFont.isLoaded("glyf")
|
|
|
|
or ttFont.isLoaded("CFF ")
|
|
|
|
or ttFont.isLoaded("CFF2")
|
|
|
|
):
|
1999-12-23 14:44:16 +00:00
|
|
|
self.recalc(ttFont)
|
2016-10-16 06:31:52 +01:00
|
|
|
self.tableVersion = fi2ve(self.tableVersion)
|
1999-12-16 21:34:53 +00:00
|
|
|
return sstruct.pack(hheaFormat, self)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def recalc(self, ttFont):
|
2023-10-06 10:28:58 +01:00
|
|
|
if "hmtx" not in ttFont:
|
|
|
|
return
|
|
|
|
|
|
|
|
hmtxTable = ttFont["hmtx"]
|
|
|
|
self.advanceWidthMax = max(adv for adv, _ in hmtxTable.metrics.values())
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-19 14:42:44 +09:00
|
|
|
boundsWidthDict = {}
|
2013-11-27 02:33:03 -05:00
|
|
|
if "glyf" in ttFont:
|
1999-12-16 21:34:53 +00:00
|
|
|
glyfTable = ttFont["glyf"]
|
|
|
|
for name in ttFont.getGlyphOrder():
|
2013-11-14 17:27:15 -05:00
|
|
|
g = glyfTable[name]
|
2013-11-14 17:22:47 -05:00
|
|
|
if g.numberOfContours == 0:
|
1999-12-16 21:34:53 +00:00
|
|
|
continue
|
2013-11-14 17:22:47 -05:00
|
|
|
if g.numberOfContours < 0 and not hasattr(g, "xMax"):
|
|
|
|
# Composite glyph without extents set.
|
|
|
|
# Calculate those.
|
|
|
|
g.recalcBounds(glyfTable)
|
2017-05-19 14:42:44 +09:00
|
|
|
boundsWidthDict[name] = g.xMax - g.xMin
|
2020-01-31 04:13:48 +02:00
|
|
|
elif "CFF " in ttFont or "CFF2" in ttFont:
|
|
|
|
if "CFF " in ttFont:
|
|
|
|
topDict = ttFont["CFF "].cff.topDictIndex[0]
|
|
|
|
else:
|
|
|
|
topDict = ttFont["CFF2"].cff.topDictIndex[0]
|
2018-01-26 16:53:04 -08:00
|
|
|
charStrings = topDict.CharStrings
|
2017-05-19 15:12:59 +09:00
|
|
|
for name in ttFont.getGlyphOrder():
|
2018-01-26 16:53:04 -08:00
|
|
|
cs = charStrings[name]
|
|
|
|
bounds = cs.calcBounds(charStrings)
|
2017-08-01 10:35:39 +09:00
|
|
|
if bounds is not None:
|
2017-10-05 13:27:13 +01:00
|
|
|
boundsWidthDict[name] = int(
|
|
|
|
math.ceil(bounds[2]) - math.floor(bounds[0])
|
2022-12-13 11:26:36 +00:00
|
|
|
)
|
|
|
|
|
2017-05-22 11:05:19 +09:00
|
|
|
if boundsWidthDict:
|
|
|
|
minLeftSideBearing = float("inf")
|
|
|
|
minRightSideBearing = float("inf")
|
|
|
|
xMaxExtent = -float("inf")
|
|
|
|
for name, boundsWidth in boundsWidthDict.items():
|
|
|
|
advanceWidth, lsb = hmtxTable[name]
|
|
|
|
rsb = advanceWidth - lsb - boundsWidth
|
|
|
|
extent = lsb + boundsWidth
|
|
|
|
minLeftSideBearing = min(minLeftSideBearing, lsb)
|
|
|
|
minRightSideBearing = min(minRightSideBearing, rsb)
|
|
|
|
xMaxExtent = max(xMaxExtent, extent)
|
|
|
|
self.minLeftSideBearing = minLeftSideBearing
|
|
|
|
self.minRightSideBearing = minRightSideBearing
|
|
|
|
self.xMaxExtent = xMaxExtent
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-22 11:05:19 +09:00
|
|
|
else: # No glyph has outlines.
|
|
|
|
self.minLeftSideBearing = 0
|
|
|
|
self.minRightSideBearing = 0
|
|
|
|
self.xMaxExtent = 0
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def toXML(self, writer, ttFont):
|
|
|
|
formatstring, names, fixes = sstruct.getformat(hheaFormat)
|
|
|
|
for name in names:
|
|
|
|
value = getattr(self, name)
|
2016-10-16 06:31:52 +01:00
|
|
|
if name == "tableVersion":
|
|
|
|
value = fi2ve(value)
|
|
|
|
value = "0x%08x" % value
|
1999-12-16 21:34:53 +00:00
|
|
|
writer.simpletag(name, value=value)
|
|
|
|
writer.newline()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2013-11-27 03:19:32 -05:00
|
|
|
def fromXML(self, name, attrs, content, ttFont):
|
2016-10-16 06:31:52 +01:00
|
|
|
if name == "tableVersion":
|
|
|
|
setattr(self, name, ve2fi(attrs["value"]))
|
|
|
|
return
|
1999-12-16 21:34:53 +00:00
|
|
|
setattr(self, name, safeEval(attrs["value"]))
|