def checksumTables(self): # Check the checksums for all tables for t in self.tables: table = self.get_chunk(t['offset'], t['length']) checksum = calcChecksum(table) if t['tag'] == 'head': adjustment = unpack('>l', table[8:8+4])[0] checksum = add32(checksum, -adjustment) xchecksum = t['checksum'] if xchecksum != checksum: raise TTFError('TTF file "%s": invalid checksum %s table: %s (expected %s)' % (self.filename,hex32(checksum),t['tag'],hex32(xchecksum)))
def makeStream(self): "Finishes the generation and returns the TTF file as a string" stm = getBytesIO() write = stm.write tables = self.tables numTables = len(tables) searchRange = 1 entrySelector = 0 while searchRange * 2 <= numTables: searchRange = searchRange * 2 entrySelector = entrySelector + 1 searchRange = searchRange * 16 rangeShift = numTables * 16 - searchRange # Header write( pack(">lHHHH", 0x00010000, numTables, searchRange, entrySelector, rangeShift)) # Table directory offset = 12 + numTables * 16 wStr = (lambda x: write(bytes(tag, 'latin1'))) if isPy3 else write tables_items = list(sorted(tables.items())) for tag, data in tables_items: if tag == 'head': head_start = offset checksum = calcChecksum(data) wStr(tag) write(pack(">LLL", checksum, offset, len(data))) paddedLength = (len(data) + 3) & ~3 offset = offset + paddedLength # Table data for tag, data in tables_items: data += b"\0\0\0" write(data[:len(data) & ~3]) checksum = calcChecksum(stm.getvalue()) checksum = add32(0xB1B0AFBA, -checksum) stm.seek(head_start + 8) write(pack('>L', checksum)) return stm.getvalue()
def makeStream(self): "Finishes the generation and returns the TTF file as a string" stm = getBytesIO() write = stm.write tables = self.tables numTables = len(tables) searchRange = 1 entrySelector = 0 while searchRange * 2 <= numTables: searchRange = searchRange * 2 entrySelector = entrySelector + 1 searchRange = searchRange * 16 rangeShift = numTables * 16 - searchRange # Header write(pack(">lHHHH", 0x00010000, numTables, searchRange, entrySelector, rangeShift)) # Table directory offset = 12 + numTables * 16 wStr = (lambda x:write(bytes(tag,'latin1'))) if isPy3 else write tables_items = list(sorted(tables.items())) for tag, data in tables_items: if tag == 'head': head_start = offset checksum = calcChecksum(data) wStr(tag) write(pack(">LLL", checksum, offset, len(data))) paddedLength = (len(data)+3)&~3 offset = offset + paddedLength # Table data for tag, data in tables_items: data += b"\0\0\0" write(data[:len(data)&~3]) checksum = calcChecksum(stm.getvalue()) checksum = add32(0xB1B0AFBA, -checksum) stm.seek(head_start + 8) write(pack('>L', checksum)) return stm.getvalue()