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
|
2013-11-27 17:27:45 -05:00
|
|
|
from . import DefaultTable
|
|
|
|
import time
|
2013-12-06 17:47:14 -05:00
|
|
|
import calendar
|
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):
|
|
|
|
|
|
|
|
dependencies = ['maxp', 'loca']
|
|
|
|
|
|
|
|
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
|
|
|
|
assert rest == "\0\0"
|
1999-12-16 21:34:53 +00:00
|
|
|
|
|
|
|
def compile(self, ttFont):
|
2014-05-01 15:13:22 -07:00
|
|
|
if ttFont.recalcTimestamp:
|
|
|
|
self.modified = int(time.time() - mac_epoch_diff)
|
1999-12-16 21:34:53 +00:00
|
|
|
data = sstruct.pack(headFormat, self)
|
|
|
|
return data
|
|
|
|
|
|
|
|
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"):
|
2006-01-25 15:12:14 +00:00
|
|
|
try:
|
|
|
|
value = time.asctime(time.gmtime(max(0, value + mac_epoch_diff)))
|
|
|
|
except ValueError:
|
|
|
|
value = time.asctime(time.gmtime(0))
|
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()
|
|
|
|
|
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"):
|
2013-12-06 17:47:14 -05:00
|
|
|
value = calendar.timegm(time.strptime(value)) - mac_epoch_diff
|
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)
|
|
|
|
|
|
|
|
|
2013-12-06 17:56:09 -05:00
|
|
|
# Difference between the original Mac epoch (1904) to the epoch on this machine.
|
|
|
|
mac_epoch_diff = calendar.timegm((1904, 1, 1, 0, 0, 0, 0, 0, 0))
|