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, num2binary, binary2num
|
2015-01-02 12:53:16 -08:00
|
|
|
from fontTools.misc.timeTools import timestampFromString, timestampToString, timestampNow
|
2015-01-22 11:21:43 -08:00
|
|
|
from fontTools.misc.timeTools import epoch_diff as mac_epoch_diff # For backward compat
|
2013-11-27 17:27:45 -05:00
|
|
|
from . import DefaultTable
|
2016-01-24 14:40:41 +00:00
|
|
|
import logging
|
1999-12-16 21:34:53 +00:00
|
|
|
|
|
|
|
|
2016-01-24 14:40:41 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
headFormat = """
|
|
|
|
> # big endian
|
2003-01-03 21:29:23 +00:00
|
|
|
tableVersion: 16.16F
|
|
|
|
fontRevision: 16.16F
|
|
|
|
checkSumAdjustment: I
|
|
|
|
magicNumber: I
|
|
|
|
flags: H
|
|
|
|
unitsPerEm: H
|
2013-12-06 17:26:09 -05:00
|
|
|
created: Q
|
|
|
|
modified: Q
|
2003-01-03 21:29:23 +00:00
|
|
|
xMin: h
|
|
|
|
yMin: h
|
|
|
|
xMax: h
|
|
|
|
yMax: h
|
|
|
|
macStyle: H
|
|
|
|
lowestRecPPEM: H
|
|
|
|
fontDirectionHint: h
|
|
|
|
indexToLocFormat: h
|
|
|
|
glyphDataFormat: h
|
1999-12-16 21:34:53 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
class table__h_e_a_d(DefaultTable.DefaultTable):
|
2015-04-26 02:01:01 -04:00
|
|
|
|
2017-05-19 11:37:01 +09:00
|
|
|
dependencies = ['maxp', 'loca', 'CFF ']
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def decompile(self, data, ttFont):
|
2000-02-21 21:30:32 +00:00
|
|
|
dummy, rest = sstruct.unpack2(headFormat, data, self)
|
|
|
|
if rest:
|
|
|
|
# this is quite illegal, but there seem to be fonts out there that do this
|
2016-01-24 14:40:41 +00:00
|
|
|
log.warning("extra bytes at the end of 'head' table")
|
2000-02-21 21:30:32 +00:00
|
|
|
assert rest == "\0\0"
|
2015-01-02 13:08:57 -08:00
|
|
|
|
|
|
|
# For timestamp fields, ignore the top four bytes. Some fonts have
|
|
|
|
# bogus values there. Since till 2038 those bytes only can be zero,
|
|
|
|
# ignore them.
|
|
|
|
#
|
|
|
|
# https://github.com/behdad/fonttools/issues/99#issuecomment-66776810
|
|
|
|
for stamp in 'created', 'modified':
|
|
|
|
value = getattr(self, stamp)
|
|
|
|
if value > 0xFFFFFFFF:
|
2016-01-24 14:40:41 +00:00
|
|
|
log.warning("'%s' timestamp out of range; ignoring top bytes", stamp)
|
2015-01-02 13:08:57 -08:00
|
|
|
value &= 0xFFFFFFFF
|
|
|
|
setattr(self, stamp, value)
|
2015-01-03 23:04:10 +01:00
|
|
|
if value < 0x7C259DC0: # January 1, 1970 00:00:00
|
2016-01-24 14:40:41 +00:00
|
|
|
log.warning("'%s' timestamp seems very low; regarding as unix timestamp", stamp)
|
2015-01-03 23:04:10 +01:00
|
|
|
value += 0x7C259DC0
|
|
|
|
setattr(self, stamp, value)
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def compile(self, ttFont):
|
2017-05-19 11:37:01 +09:00
|
|
|
if ttFont.recalcBBoxes:
|
|
|
|
# For TT-flavored fonts, xMin, yMin, xMax and yMax are set in table__m_a_x_p.recalc().
|
|
|
|
if 'CFF ' in ttFont:
|
|
|
|
topDict = ttFont['CFF '].cff.topDictIndex[0]
|
|
|
|
self.xMin, self.yMin, self.xMax, self.yMax = topDict.FontBBox
|
2014-05-01 15:13:22 -07:00
|
|
|
if ttFont.recalcTimestamp:
|
2015-01-02 13:02:36 -08:00
|
|
|
self.modified = timestampNow()
|
1999-12-16 21:34:53 +00:00
|
|
|
data = sstruct.pack(headFormat, self)
|
|
|
|
return data
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def toXML(self, writer, ttFont):
|
|
|
|
writer.comment("Most of this table will be recalculated by the compiler")
|
|
|
|
writer.newline()
|
|
|
|
formatstring, names, fixes = sstruct.getformat(headFormat)
|
|
|
|
for name in names:
|
|
|
|
value = getattr(self, name)
|
|
|
|
if name in ("created", "modified"):
|
2015-01-02 12:53:16 -08:00
|
|
|
value = timestampToString(value)
|
1999-12-16 21:34:53 +00:00
|
|
|
if name in ("magicNumber", "checkSumAdjustment"):
|
2004-12-24 16:07:01 +00:00
|
|
|
if value < 0:
|
2013-11-27 03:37:29 -05:00
|
|
|
value = value + 0x100000000
|
1999-12-16 21:34:53 +00:00
|
|
|
value = hex(value)
|
2003-01-10 22:34:13 +00:00
|
|
|
if value[-1:] == "L":
|
|
|
|
value = value[:-1]
|
2001-08-15 09:26:15 +00:00
|
|
|
elif name in ("macStyle", "flags"):
|
1999-12-16 21:34:53 +00:00
|
|
|
value = num2binary(value, 16)
|
|
|
|
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
|
|
|
value = attrs["value"]
|
|
|
|
if name in ("created", "modified"):
|
2015-01-02 12:53:16 -08:00
|
|
|
value = timestampFromString(value)
|
2001-08-15 09:26:15 +00:00
|
|
|
elif name in ("macStyle", "flags"):
|
1999-12-16 21:34:53 +00:00
|
|
|
value = binary2num(value)
|
|
|
|
else:
|
2013-11-27 04:00:15 -05:00
|
|
|
value = safeEval(value)
|
1999-12-16 21:34:53 +00:00
|
|
|
setattr(self, name, value)
|