Example #1
0
    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()
Example #2
0
    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()
Example #3
0
 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)))
Example #4
0
 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)))
Example #5
0
 def checksumFile(self):
     # Check the checksums for the whole file
     checksum = calcChecksum(self._ttf_data)
     if 0xB1B0AFBA!=checksum:
         raise TTFError('TTF file "%s": invalid checksum %s (expected 0xB1B0AFBA) len: %d &3: %d' % (self.filename,hex32(checksum),len(self._ttf_data),(len(self._ttf_data)&3)))
Example #6
0
 def checksumFile(self):
     # Check the checksums for the whole file
     checksum = calcChecksum(self._ttf_data)
     if 0xB1B0AFBA!=checksum:
         raise TTFError('TTF file "%s": invalid checksum %s (expected 0xB1B0AFBA) len: %d &3: %d' % (self.filename,hex32(checksum),len(self._ttf_data),(len(self._ttf_data)&3)))