Home | History | Annotate | Download | only in tables
      1 from __future__ import print_function, division, absolute_import
      2 from fontTools.misc.py23 import *
      3 from fontTools.misc import sstruct
      4 from fontTools.misc.textTools import safeEval, num2binary, binary2num
      5 from . import DefaultTable
      6 import time
      7 import calendar
      8 
      9 
     10 headFormat = """
     11 		>	# big endian
     12 		tableVersion:       16.16F
     13 		fontRevision:       16.16F
     14 		checkSumAdjustment: I
     15 		magicNumber:        I
     16 		flags:              H
     17 		unitsPerEm:         H
     18 		created:            Q
     19 		modified:           Q
     20 		xMin:               h
     21 		yMin:               h
     22 		xMax:               h
     23 		yMax:               h
     24 		macStyle:           H
     25 		lowestRecPPEM:      H
     26 		fontDirectionHint:  h
     27 		indexToLocFormat:   h
     28 		glyphDataFormat:    h
     29 """
     30 
     31 class table__h_e_a_d(DefaultTable.DefaultTable):
     32 	
     33 	dependencies = ['maxp', 'loca']
     34 	
     35 	def decompile(self, data, ttFont):
     36 		dummy, rest = sstruct.unpack2(headFormat, data, self)
     37 		if rest:
     38 			# this is quite illegal, but there seem to be fonts out there that do this
     39 			assert rest == "\0\0"
     40 	
     41 	def compile(self, ttFont):
     42 		if ttFont.recalcTimestamp:
     43 			self.modified = int(time.time() - mac_epoch_diff)
     44 		data = sstruct.pack(headFormat, self)
     45 		return data
     46 	
     47 	def toXML(self, writer, ttFont):
     48 		writer.comment("Most of this table will be recalculated by the compiler")
     49 		writer.newline()
     50 		formatstring, names, fixes = sstruct.getformat(headFormat)
     51 		for name in names:
     52 			value = getattr(self, name)
     53 			if name in ("created", "modified"):
     54 				try:
     55 					value = time.asctime(time.gmtime(max(0, value + mac_epoch_diff)))
     56 				except ValueError:
     57 					value = time.asctime(time.gmtime(0))
     58 			if name in ("magicNumber", "checkSumAdjustment"):
     59 				if value < 0:
     60 					value = value + 0x100000000
     61 				value = hex(value)
     62 				if value[-1:] == "L":
     63 					value = value[:-1]
     64 			elif name in ("macStyle", "flags"):
     65 				value = num2binary(value, 16)
     66 			writer.simpletag(name, value=value)
     67 			writer.newline()
     68 	
     69 	def fromXML(self, name, attrs, content, ttFont):
     70 		value = attrs["value"]
     71 		if name in ("created", "modified"):
     72 			value = calendar.timegm(time.strptime(value)) - mac_epoch_diff
     73 		elif name in ("macStyle", "flags"):
     74 			value = binary2num(value)
     75 		else:
     76 			value = safeEval(value)
     77 		setattr(self, name, value)
     78 
     79 
     80 # Difference between the original Mac epoch (1904) to the epoch on this machine.
     81 mac_epoch_diff = calendar.timegm((1904, 1, 1, 0, 0, 0, 0, 0, 0))
     82