2013-11-27 17:27:45 -05:00
|
|
|
from fontTools.misc.py23 import *
|
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
|
|
|
|
|
2017-05-19 15:12:59 +09:00
|
|
|
dependencies = ['hmtx', 'glyf', 'CFF ']
|
2015-04-26 02:01:01 -04: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)
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def compile(self, ttFont):
|
2017-05-19 15:12:59 +09:00
|
|
|
if ttFont.recalcBBoxes and (ttFont.isLoaded('glyf') or ttFont.isLoaded('CFF ')):
|
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)
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def recalc(self, ttFont):
|
2017-08-16 16:41:09 -07:00
|
|
|
if 'hmtx' in ttFont:
|
|
|
|
hmtxTable = ttFont['hmtx']
|
|
|
|
self.advanceWidthMax = max(adv for adv, _ in hmtxTable.metrics.values())
|
2017-05-22 10:40:15 +09: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
|
2017-05-19 15:12:59 +09:00
|
|
|
elif 'CFF ' in ttFont:
|
|
|
|
topDict = ttFont['CFF '].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]))
|
2015-04-26 02:01:01 -04: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
|
|
|
|
|
|
|
|
else: # No glyph has outlines.
|
|
|
|
self.minLeftSideBearing = 0
|
|
|
|
self.minRightSideBearing = 0
|
|
|
|
self.xMaxExtent = 0
|
2017-05-19 14:42:44 +09: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()
|
2015-04-26 02:01:01 -04: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"]))
|