Use struct format 'Q' to read/write 64bit integers

This commit is contained in:
Behdad Esfahbod 2013-12-06 17:26:09 -05:00
parent 5e36f854aa
commit b8e1afa809
2 changed files with 3 additions and 33 deletions

View File

@ -113,7 +113,7 @@ _elementRE = re.compile(
"\s*" # whitespace "\s*" # whitespace
"([A-Za-z_][A-Za-z_0-9]*)" # name (python identifier) "([A-Za-z_][A-Za-z_0-9]*)" # name (python identifier)
"\s*:\s*" # whitespace : whitespace "\s*:\s*" # whitespace : whitespace
"([cbBhHiIlLfd]|[0-9]+[ps]|" # formatchar... "([cbBhHiIlLqQfd]|[0-9]+[ps]|" # formatchar...
"([0-9]+)\.([0-9]+)(F))" # ...formatchar "([0-9]+)\.([0-9]+)(F))" # ...formatchar
"\s*" # whitespace "\s*" # whitespace
"(#.*)?$" # [comment] + end of string "(#.*)?$" # [comment] + end of string

View File

@ -14,8 +14,8 @@ headFormat = """
magicNumber: I magicNumber: I
flags: H flags: H
unitsPerEm: H unitsPerEm: H
created: 8s created: Q
modified: 8s modified: Q
xMin: h xMin: h
yMin: h yMin: h
xMax: h xMax: h
@ -36,23 +36,12 @@ class table__h_e_a_d(DefaultTable.DefaultTable):
if rest: if rest:
# this is quite illegal, but there seem to be fonts out there that do this # this is quite illegal, but there seem to be fonts out there that do this
assert rest == "\0\0" assert rest == "\0\0"
self.strings2dates()
def compile(self, ttFont): def compile(self, ttFont):
self.modified = int(time.time() - mac_epoch_diff) self.modified = int(time.time() - mac_epoch_diff)
self.dates2strings()
data = sstruct.pack(headFormat, self) data = sstruct.pack(headFormat, self)
self.strings2dates()
return data return data
def strings2dates(self):
self.created = bin2long(self.created)
self.modified = bin2long(self.modified)
def dates2strings(self):
self.created = long2bin(self.created)
self.modified = long2bin(self.modified)
def toXML(self, writer, ttFont): def toXML(self, writer, ttFont):
writer.comment("Most of this table will be recalculated by the compiler") writer.comment("Most of this table will be recalculated by the compiler")
writer.newline() writer.newline()
@ -114,22 +103,3 @@ def parse_date(datestring):
hour, minute, second = [int(item) for item in tim.split(":")] hour, minute, second = [int(item) for item in tim.split(":")]
t = (year, month, day, hour, minute, second, weekday, 0, 0) t = (year, month, day, hour, minute, second, weekday, 0, 0)
return int(time.mktime(t) - time.timezone) return int(time.mktime(t) - time.timezone)
def bin2long(data):
# thanks </F>!
v = 0
for i in map(byteord, data):
v = v<<8 | i
return v
def long2bin(v, bytes=8):
mask = int("FF" * bytes, 16)
data = b""
while v:
data = bytechr(v & 0xff) + data
v = (v >> 8) & mask
data = (bytes - len(data)) * b"\0" + data
assert len(data) == 8, "long too long"
return data