1from fontTools.misc import sstruct 2from fontTools.misc.fixedTools import floatToFixedToStr, strToFixedToFloat 3from fontTools.misc.textTools import safeEval, num2binary, binary2num 4from fontTools.misc.timeTools import timestampFromString, timestampToString, timestampNow 5from fontTools.misc.timeTools import epoch_diff as mac_epoch_diff # For backward compat 6from fontTools.misc.arrayTools import intRect, unionRect 7from . import DefaultTable 8import logging 9 10 11log = logging.getLogger(__name__) 12 13headFormat = """ 14 > # big endian 15 tableVersion: 16.16F 16 fontRevision: 16.16F 17 checkSumAdjustment: I 18 magicNumber: I 19 flags: H 20 unitsPerEm: H 21 created: Q 22 modified: Q 23 xMin: h 24 yMin: h 25 xMax: h 26 yMax: h 27 macStyle: H 28 lowestRecPPEM: H 29 fontDirectionHint: h 30 indexToLocFormat: h 31 glyphDataFormat: h 32""" 33 34class table__h_e_a_d(DefaultTable.DefaultTable): 35 36 dependencies = ['maxp', 'loca', 'CFF ', 'CFF2'] 37 38 def decompile(self, data, ttFont): 39 dummy, rest = sstruct.unpack2(headFormat, data, self) 40 if rest: 41 # this is quite illegal, but there seem to be fonts out there that do this 42 log.warning("extra bytes at the end of 'head' table") 43 assert rest == b"\0\0" 44 45 # For timestamp fields, ignore the top four bytes. Some fonts have 46 # bogus values there. Since till 2038 those bytes only can be zero, 47 # ignore them. 48 # 49 # https://github.com/fonttools/fonttools/issues/99#issuecomment-66776810 50 for stamp in 'created', 'modified': 51 value = getattr(self, stamp) 52 if value > 0xFFFFFFFF: 53 log.warning("'%s' timestamp out of range; ignoring top bytes", stamp) 54 value &= 0xFFFFFFFF 55 setattr(self, stamp, value) 56 if value < 0x7C259DC0: # January 1, 1970 00:00:00 57 log.warning("'%s' timestamp seems very low; regarding as unix timestamp", stamp) 58 value += 0x7C259DC0 59 setattr(self, stamp, value) 60 61 def compile(self, ttFont): 62 if ttFont.recalcBBoxes: 63 # For TT-flavored fonts, xMin, yMin, xMax and yMax are set in table__m_a_x_p.recalc(). 64 if 'CFF ' in ttFont: 65 topDict = ttFont['CFF '].cff.topDictIndex[0] 66 self.xMin, self.yMin, self.xMax, self.yMax = intRect(topDict.FontBBox) 67 elif 'CFF2' in ttFont: 68 topDict = ttFont['CFF2'].cff.topDictIndex[0] 69 charStrings = topDict.CharStrings 70 fontBBox = None 71 for charString in charStrings.values(): 72 bounds = charString.calcBounds(charStrings) 73 if bounds is not None: 74 if fontBBox is not None: 75 fontBBox = unionRect(fontBBox, bounds) 76 else: 77 fontBBox = bounds 78 if fontBBox is not None: 79 self.xMin, self.yMin, self.xMax, self.yMax = intRect(fontBBox) 80 if ttFont.recalcTimestamp: 81 self.modified = timestampNow() 82 data = sstruct.pack(headFormat, self) 83 return data 84 85 def toXML(self, writer, ttFont): 86 writer.comment("Most of this table will be recalculated by the compiler") 87 writer.newline() 88 _, names, fixes = sstruct.getformat(headFormat) 89 for name in names: 90 value = getattr(self, name) 91 if name in fixes: 92 value = floatToFixedToStr(value, precisionBits=fixes[name]) 93 elif name in ("created", "modified"): 94 value = timestampToString(value) 95 elif name in ("magicNumber", "checkSumAdjustment"): 96 if value < 0: 97 value = value + 0x100000000 98 value = hex(value) 99 if value[-1:] == "L": 100 value = value[:-1] 101 elif name in ("macStyle", "flags"): 102 value = num2binary(value, 16) 103 writer.simpletag(name, value=value) 104 writer.newline() 105 106 def fromXML(self, name, attrs, content, ttFont): 107 value = attrs["value"] 108 fixes = sstruct.getformat(headFormat)[2] 109 if name in fixes: 110 value = strToFixedToFloat(value, precisionBits=fixes[name]) 111 elif name in ("created", "modified"): 112 value = timestampFromString(value) 113 elif name in ("macStyle", "flags"): 114 value = binary2num(value) 115 else: 116 value = safeEval(value) 117 setattr(self, name, value) 118