| 1 |
__license__ = """ |
| 2 |
This file is part of GNU FreeFont. |
| 3 |
|
| 4 |
GNU FreeFont is free software: you can redistribute it and/or modify it under |
| 5 |
the terms of the GNU General Public License as published by the Free Software |
| 6 |
Foundation, either version 3 of the License, or (at your option) any later |
| 7 |
version. |
| 8 |
|
| 9 |
GNU FreeFont is distributed in the hope that it will be useful, but WITHOUT |
| 10 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along with |
| 14 |
GNU FreeFont. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
""" |
| 16 |
__author__ = "Stevan White" |
| 17 |
__email__ = "[email protected]" |
| 18 |
__copyright__ = "Copyright 2012, 2015, 2018 Stevan White" |
| 19 |
__date__ = "$Date: 2018-06-03 22:20:36 +0200 (So, 03. Jun 2018) $" |
| 20 |
__version__ = "$Revision: 3921 $" |
| 21 |
|
| 22 |
__doc__ = """ Utility class to translate between TrueType/OpenType table |
| 23 |
field descriptors and 'struct' module field descriptors. |
| 24 |
that implement automatic generation of internal named structures, |
| 25 |
and automatic generation of code format strings for output. |
| 26 |
""" |
| 27 |
|
| 28 |
import re |
| 29 |
|
| 30 |
""" |
| 31 |
uint16 offset, glyphID, |
| 32 |
int16 short, bitfield, f2dot14 |
| 33 |
uint32 uint, Fixed, bitfield |
| 34 |
int32 long, bitfield, 4-char tag, |
| 35 |
uint64 long |
| 36 |
int64 longlong, longdatetime |
| 37 |
byte byte |
| 38 |
char[] (signed byte... used for strings) |
| 39 |
""" |
| 40 |
""" |
| 41 |
Regarding PANOSE |
| 42 |
|
| 43 |
The OpenType Standard explains: |
| 44 |
"The PANOSE definition contains ten digits each of which currently describes up to sixteen variations." |
| 45 |
|
| 46 |
"digits:? But it's a 10-*byte* field? Want to call a byte a digit? |
| 47 |
OK, but then it would take up to 2^8 = 256 values. |
| 48 |
Well, if only half of the bits of each byte are used, that would amount |
| 49 |
to 2^4 = 16 different values. |
| 50 |
OK, so each byte of this 10-byte field takes hex values 0 through 0xF, and no others. |
| 51 |
Still, a funny sort of "digit". |
| 52 |
|
| 53 |
If that is the case, there's no need to worry about the sign bit, |
| 54 |
although the OpenType standard says Panose is a uint8[10]. |
| 55 |
The unsigned-byte and signed-byte interpretation would always be the same. |
| 56 |
""" |
| 57 |
|
| 58 |
_pytype_structsym = { |
| 59 |
'byte': 'b', |
| 60 |
'char': 'c', |
| 61 |
'string': 's', |
| 62 |
'uint16': 'H', |
| 63 |
'int16': 'h', |
| 64 |
'uint32': 'L', |
| 65 |
'int32': 'l', |
| 66 |
'uint64': 'Q', |
| 67 |
'int64': 'q', |
| 68 |
# used in MS OpenType docs |
| 69 |
'GlyphID': 'H', |
| 70 |
'Offset': 'H', |
| 71 |
'SHORT': 'h', |
| 72 |
'USHORT': 'H', |
| 73 |
'LONG': 'i', |
| 74 |
'ULONG': 'I', |
| 75 |
'Fixed': 'I', |
| 76 |
'Tag': 'I4', |
| 77 |
'PANOSE': '10s', |
| 78 |
} |
| 79 |
|
| 80 |
_pytype_fmtsym = { |
| 81 |
'uint16': 'u', |
| 82 |
'int16': 'i', |
| 83 |
'uint32': 'u', |
| 84 |
'int32': 'i', |
| 85 |
'uint64': 'u', |
| 86 |
'int64': 'i', |
| 87 |
'string': 's', |
| 88 |
# used in MS OpenType docs |
| 89 |
'GlyphID': 'u', |
| 90 |
'Offset': 'u', |
| 91 |
'SHORT': 'i', |
| 92 |
'USHORT': 'u', |
| 93 |
'LONG': 'i', |
| 94 |
'ULONG': 'u', |
| 95 |
'Fixed': 'u', |
| 96 |
'PANOSE': '10u', |
| 97 |
} |
| 98 |
_interp_conv = { |
| 99 |
'longdatetime': 'longdatetime2date', |
| 100 |
'bitfield': 'bitfield2str', |
| 101 |
'Fixed': 'fixed_str', |
| 102 |
'hex': 'hex', |
| 103 |
'tag': 'int_to_tag', |
| 104 |
'bool': 'bool', |
| 105 |
'PANOSE': 'panose_str', |
| 106 |
} |
| 107 |
|
| 108 |
_OT_typesize = { |
| 109 |
'byte': 1, |
| 110 |
'char': 1, |
| 111 |
'uint16': 2, |
| 112 |
'int16': 2, |
| 113 |
'uint32': 4, |
| 114 |
'int32': 4, |
| 115 |
'uint64': 8, |
| 116 |
'int64': 8, |
| 117 |
'PANOSE': 10, |
| 118 |
} |
| 119 |
|
| 120 |
_type_plurality_re = re.compile( '(\w+)(?:\[(\d\+)\])?' ) |
| 121 |
|
| 122 |
class FieldDesc(): |
| 123 |
def __init__( self, name, pytype, interp = None, desc = '' ): |
| 124 |
self.name = name |
| 125 |
m = _type_plurality_re.match( pytype ) |
| 126 |
self.num = 0 |
| 127 |
if m: |
| 128 |
self.pytype = m.group(1) |
| 129 |
if len( m.groups() ) > 2: |
| 130 |
self.num = int( m.group(2) ) |
| 131 |
else: |
| 132 |
self.num = 1 |
| 133 |
else: |
| 134 |
raise Exception( "can't parse field type:" + pytype ) |
| 135 |
self.interp = interp |
| 136 |
self.desc = desc |
| 137 |
|
| 138 |
@classmethod |
| 139 |
def buildList( cls, fdtuple ): |
| 140 |
return [ FieldDesc( *fd ) for fd in fdtuple ] |
| 141 |
|
| 142 |
@classmethod |
| 143 |
def structdefs( cls, fielddefs ): |
| 144 |
""" Takes a list of FieldDesc, returns a string |
| 145 |
for 'struct' module Struct constructor and calcsize(). |
| 146 |
""" |
| 147 |
deflist = [ '>' ] |
| 148 |
for fd in fielddefs: |
| 149 |
structsym = _pytype_structsym[fd.pytype] |
| 150 |
if fd.num > 1: |
| 151 |
structsym = str( fd.num ) + structsym |
| 152 |
deflist.append( structsym ) |
| 153 |
return ' '.join( deflist ) |
| 154 |
|
| 155 |
@classmethod |
| 156 |
def namelist( cls, fielddefs ): |
| 157 |
return ' '.join( [ fd.name for fd in fielddefs ] ) |
| 158 |
|
| 159 |
@classmethod |
| 160 |
def formatstring( cls, tablename, fielddefs ): |
| 161 |
""" From a table name and field definitions, returns a python |
| 162 |
format string, which, when fed corresponding field values |
| 163 |
produces a human-readable string representing the table. |
| 164 |
""" |
| 165 |
frmt = [] |
| 166 |
conv = [] |
| 167 |
for fd in fielddefs: |
| 168 |
n = fd.name + ": " |
| 169 |
v = 'self.' + fd.name |
| 170 |
if fd.interp: |
| 171 |
if fd.interp == "Fixed": |
| 172 |
frmt.append( n + '%s' ) |
| 173 |
else: |
| 174 |
frmt.append( n + "'%s'" ) |
| 175 |
conv.append( _interp_conv[fd.interp] |
| 176 |
+ '( ' + v + ' )' ) |
| 177 |
else: |
| 178 |
frmt.append( n + '%' + _pytype_fmtsym[fd.pytype] ) |
| 179 |
conv.append( v ) |
| 180 |
frmtstr = tablename + ' = { ' + ', '.join( frmt ) + ' }' |
| 181 |
convstr = ', '.join( conv ) |
| 182 |
s = '"%s" %s ( %s )' % ( frmtstr, '%', convstr ) |
| 183 |
#print( s ) |
| 184 |
return s |
| 185 |
|
| 186 |
@classmethod |
| 187 |
def has_type_symbol( cls, symb ): |
| 188 |
return symb in _pytype_structsym |
| 189 |
|
| 190 |
@classmethod |
| 191 |
def type_to_format( cls, symb ): |
| 192 |
return _pytype_structsym[symb] |
| 193 |
|
| 194 |
@classmethod |
| 195 |
def size_OT_type( cls, symb ): |
| 196 |
return _OT_typesize[symb] |