| 1 |
from __future__ import print_function |
| 2 |
__license__ = """ |
| 3 |
This file is part of GNU FreeFont. |
| 4 |
|
| 5 |
GNU FreeFont is free software: you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License as published by the Free Software |
| 7 |
Foundation, either version 3 of the License, or (at your option) any later |
| 8 |
version. |
| 9 |
|
| 10 |
GNU FreeFont is distributed in the hope that it will be useful, but WITHOUT |
| 11 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License along with |
| 15 |
GNU FreeFont. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
""" |
| 17 |
__author__ = "Stevan White" |
| 18 |
__email__ = "[email protected]" |
| 19 |
__copyright__ = "Copyright 2017, 2018 Stevan White" |
| 20 |
__date__ = "$Date: 2018-06-03 22:20:36 +0200 (So, 03. Jun 2018) $" |
| 21 |
__version__ = "$Revision: 3921 $" |
| 22 |
|
| 23 |
__doc__ = """ Utilities for calculating 32-bit checksums of table fields. |
| 24 |
This assumes the tables are padded with zeros to 32 bits. |
| 25 |
""" |
| 26 |
from ctypes import c_uint64, c_uint32, c_uint16, c_uint8, c_ubyte |
| 27 |
|
| 28 |
PANOSE = c_ubyte * 10 |
| 29 |
|
| 30 |
class BigEndian32BitList: |
| 31 |
""" Breaks up fields into bytes in big-endian fashion, and adds them |
| 32 |
to a list of 32-byte integers also in big-endian fashion. |
| 33 |
""" |
| 34 |
def __init__( self ): |
| 35 |
self._byte_count = 0 |
| 36 |
self._wordList = [] |
| 37 |
|
| 38 |
def accumVal( self, val, size ): |
| 39 |
""" Pushes a value onto the current 32 bit integer of the |
| 40 |
list, making a new integer entry when needed. |
| 41 |
For 10-byte PANOSE entries, order of entries is not changed. |
| 42 |
(Only the order of bits within the bytes is affected by |
| 43 |
endianness.) |
| 44 |
""" |
| 45 |
if size == 1: |
| 46 |
v = 0xFF & c_ubyte( val ).value |
| 47 |
self.addByteVal( v ) |
| 48 |
elif size == 2: |
| 49 |
v = c_uint16( val ).value |
| 50 |
for b in range( 0, size ): |
| 51 |
shift = 8 * ( size - b - 1 ) |
| 52 |
self.accumByte( 0xFF & ( v >> shift ) ) |
| 53 |
elif size == 4: |
| 54 |
v = c_uint32( val ).value |
| 55 |
for b in range( 0, size ): |
| 56 |
shift = 8 * ( size - b - 1 ) |
| 57 |
self.accumByte( 0xFF & ( v >> shift ) ) |
| 58 |
elif size == 8: |
| 59 |
v = c_uint64( val ).value |
| 60 |
for b in range( 0, size ): |
| 61 |
shift = 8 * ( size - b - 1 ) |
| 62 |
self.accumByte( 0xFF & ( v >> shift ) ) |
| 63 |
elif size == 10: |
| 64 |
for i in range( 0, size ): |
| 65 |
v = ord( val[i] ) |
| 66 |
#print( "FLAG", "{0:d}".format( v ) ) |
| 67 |
self.accumByte( 0xFF & v ) |
| 68 |
else: |
| 69 |
raise Exception( "Unexpected size {}".format( size ) ) |
| 70 |
|
| 71 |
def accumByte( self, val ): |
| 72 |
""" Pushes a byte value onto the current 32 bit integer of the |
| 73 |
list, making a new integer entry when needed. |
| 74 |
Records the count of bytes thus pushed. |
| 75 |
(Unless the count is a mult of 4, this is different from 1/4 |
| 76 |
the number of entries in the word list.) |
| 77 |
""" |
| 78 |
if self._byte_count % 4 == 0: |
| 79 |
self._wordList.append( 0 ) |
| 80 |
byte_off = ( 3 - self._byte_count ) % 4 |
| 81 |
|
| 82 |
new_byte = int( val ) << ( byte_off * 8 ) |
| 83 |
#print( "new_byte", new_byte ) |
| 84 |
self._wordList[-1] |= new_byte |
| 85 |
self._byte_count += 1 |
| 86 |
|
| 87 |
def get32BitSum( self ): |
| 88 |
""" Returns the 32-bit truncated unsigned sum of the unsigned |
| 89 |
integers in the list. |
| 90 |
""" |
| 91 |
checksum = 0 |
| 92 |
for w in self._wordList: |
| 93 |
checksum = 0xFFFFFFFF & ( checksum + w ) |
| 94 |
return checksum |
| 95 |
|
| 96 |
from numpy import fromfile, uint32, uint64 |
| 97 |
|
| 98 |
def get_file32Bit_checkSumAdjustment( f ): |
| 99 |
""" See the OpenType spec |
| 100 |
"The OpenType Font File" |
| 101 |
section |
| 102 |
"Calculating Checksums" |
| 103 |
""" |
| 104 |
a = fromfile( f, dtype='>u4', count=-1 ) |
| 105 |
#FIXME I can't tell from the docs if this is platform-independent or not |
| 106 |
s = uint64( 0 ) |
| 107 |
for v in a: |
| 108 |
s += v |
| 109 |
cs = 0xFFFFFFFF & int( s ) |
| 110 |
checkSumAdjustment = 0xB1B0AFBA - cs |
| 111 |
return 0xFFFFFFFF & checkSumAdjustment |
| 112 |
|