2014-01-14 15:07:50 +08:00
|
|
|
from __future__ import print_function, division, absolute_import
|
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
|
2013-11-27 17:27:45 -05:00
|
|
|
from . import DefaultTable
|
1999-12-16 21:34:53 +00:00
|
|
|
|
|
|
|
vheaFormat = """
|
|
|
|
> # big endian
|
2014-06-02 18:23:45 -04:00
|
|
|
tableVersion: 16.16F
|
|
|
|
ascent: h
|
|
|
|
descent: h
|
|
|
|
lineGap: h
|
|
|
|
advanceHeightMax: H
|
|
|
|
minTopSideBearing: h
|
1999-12-16 21:34:53 +00:00
|
|
|
minBottomSideBearing: h
|
2014-06-02 18:23:45 -04:00
|
|
|
yMaxExtent: h
|
|
|
|
caretSlopeRise: h
|
|
|
|
caretSlopeRun: h
|
|
|
|
reserved0: h
|
|
|
|
reserved1: h
|
|
|
|
reserved2: h
|
|
|
|
reserved3: h
|
|
|
|
reserved4: h
|
|
|
|
metricDataFormat: h
|
|
|
|
numberOfVMetrics: H
|
1999-12-16 21:34:53 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
class table__v_h_e_a(DefaultTable.DefaultTable):
|
2014-06-02 18:23:45 -04:00
|
|
|
|
|
|
|
# Note: Keep in sync with table__h_h_e_a
|
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
dependencies = ['vmtx', 'glyf']
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def decompile(self, data, ttFont):
|
|
|
|
sstruct.unpack(vheaFormat, data, self)
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def compile(self, ttFont):
|
2015-08-17 10:23:03 +01:00
|
|
|
if ttFont.isLoaded('glyf') and ttFont.recalcBBoxes:
|
|
|
|
self.recalc(ttFont)
|
1999-12-16 21:34:53 +00:00
|
|
|
return sstruct.pack(vheaFormat, self)
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def recalc(self, ttFont):
|
|
|
|
vtmxTable = ttFont['vmtx']
|
2013-11-27 02:33:03 -05:00
|
|
|
if 'glyf' in ttFont:
|
1999-12-16 21:34:53 +00:00
|
|
|
glyfTable = ttFont['glyf']
|
2014-06-02 18:23:45 -04:00
|
|
|
INFINITY = 100000
|
|
|
|
advanceHeightMax = 0
|
|
|
|
minTopSideBearing = +INFINITY # arbitrary big number
|
|
|
|
minBottomSideBearing = +INFINITY # arbitrary big number
|
|
|
|
yMaxExtent = -INFINITY # arbitrary big negative number
|
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
for name in ttFont.getGlyphOrder():
|
|
|
|
height, tsb = vtmxTable[name]
|
2014-06-02 18:23:45 -04:00
|
|
|
advanceHeightMax = max(advanceHeightMax, height)
|
1999-12-16 21:34:53 +00:00
|
|
|
g = glyfTable[name]
|
2014-06-02 18:23:45 -04:00
|
|
|
if g.numberOfContours == 0:
|
1999-12-16 21:34:53 +00:00
|
|
|
continue
|
2014-06-02 18:23:45 -04:00
|
|
|
if g.numberOfContours < 0 and not hasattr(g, "yMax"):
|
|
|
|
# Composite glyph without extents set.
|
|
|
|
# Calculate those.
|
|
|
|
g.recalcBounds(glyfTable)
|
1999-12-16 21:34:53 +00:00
|
|
|
minTopSideBearing = min(minTopSideBearing, tsb)
|
2014-06-02 18:23:45 -04:00
|
|
|
bsb = height - tsb - (g.yMax - g.yMin)
|
|
|
|
minBottomSideBearing = min(minBottomSideBearing, bsb)
|
1999-12-16 21:34:53 +00:00
|
|
|
extent = tsb + (g.yMax - g.yMin)
|
|
|
|
yMaxExtent = max(yMaxExtent, extent)
|
2014-06-02 18:23:45 -04:00
|
|
|
|
|
|
|
if yMaxExtent == -INFINITY:
|
|
|
|
# No glyph has outlines.
|
|
|
|
minTopSideBearing = 0
|
|
|
|
minBottomSideBearing = 0
|
|
|
|
yMaxExtent = 0
|
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
self.advanceHeightMax = advanceHeightMax
|
|
|
|
self.minTopSideBearing = minTopSideBearing
|
|
|
|
self.minBottomSideBearing = minBottomSideBearing
|
|
|
|
self.yMaxExtent = yMaxExtent
|
|
|
|
else:
|
|
|
|
# XXX CFF recalc...
|
|
|
|
pass
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def toXML(self, writer, ttFont):
|
|
|
|
formatstring, names, fixes = sstruct.getformat(vheaFormat)
|
|
|
|
for name in names:
|
|
|
|
value = getattr(self, name)
|
|
|
|
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):
|
1999-12-16 21:34:53 +00:00
|
|
|
setattr(self, name, safeEval(attrs["value"]))
|