| 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 2012, 2015, 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__ = """ Supporting superclasses of font tables, with base classes |
| 24 |
implementing automatic generation of internal named structures and code |
| 25 |
format strings for output. |
| 26 |
""" |
| 27 |
|
| 28 |
from .fielddesc import FieldDesc |
| 29 |
from struct import Struct, calcsize |
| 30 |
from collections import namedtuple |
| 31 |
from .checksum import BigEndian32BitList |
| 32 |
from .typeutils import fixed_str, panose_str, bitfield2str, int_to_tag |
| 33 |
|
| 34 |
def registerStructFields( cls ): |
| 35 |
""" Decorator for subclasses of Table; associates members of the |
| 36 |
structure defined in _field_desc with class members via the _fdlist |
| 37 |
member. |
| 38 |
""" |
| 39 |
cls._fdlist = FieldDesc.buildList( cls._field_desc ) |
| 40 |
return cls |
| 41 |
|
| 42 |
def _setup_structs( cls ): |
| 43 |
#FIXME REALLY need to warn programmer if decorator is not present |
| 44 |
# otherwise they get nasty recursions |
| 45 |
""" Decorator for all basic subclasses of Table. |
| 46 |
Initializes class internal variables based on the class variables |
| 47 |
_name (must be unique to class) and |
| 48 |
_fdlist, a list of FieldDesc constructor argument lists. |
| 49 |
Seems to run *after* the rest of the class members are initialized. |
| 50 |
1) makes class members _layout, _size, _structtype if not present. |
| 51 |
2) from class' _fdlist member, entry in a _layout entry for this |
| 52 |
class' name, and calculates _size |
| 53 |
3) generates a program for string formatting of current object |
| 54 |
4) compiles program and puts in _strcode |
| 55 |
""" |
| 56 |
if not '_layout' in cls.__dict__: |
| 57 |
cls._layout = {} |
| 58 |
if not '_size' in cls.__dict__: |
| 59 |
cls._size = 0 |
| 60 |
if not '_structtype' in cls.__dict__: |
| 61 |
cls._structtype = [] |
| 62 |
#print cls._classname() |
| 63 |
structname = cls._classname() + 'struct' |
| 64 |
structdef = FieldDesc.structdefs( cls._fdlist ) |
| 65 |
cls._layout[cls._classname()] = Struct( structdef ) |
| 66 |
cls._size += calcsize( structdef ) |
| 67 |
#cls._size += cls._layout[cls._classname()].size() |
| 68 |
namelist = FieldDesc.namelist( cls._fdlist ) |
| 69 |
cls._structtype.append( namedtuple( structname, namelist ) ) |
| 70 |
strfmt = FieldDesc.formatstring( cls._name, cls._fdlist ) |
| 71 |
cls._strcode = compile( 'self._astr = ' + strfmt, '<string>', 'exec' ) |
| 72 |
return cls |
| 73 |
|
| 74 |
class BaseTable( object ): |
| 75 |
"""Base class of font tables. |
| 76 |
Much magic. Intent is to render tables to such a degree |
| 77 |
as possible to be objects whose members are set by |
| 78 |
field descriptions. |
| 79 |
""" |
| 80 |
|
| 81 |
@classmethod |
| 82 |
def _classname( cls ): |
| 83 |
return cls.__name__.rpartition( '.' )[2] |
| 84 |
|
| 85 |
def __init__( self, buf, offset = 0 ): |
| 86 |
""" Idea is to override the member access functions, so that |
| 87 |
font structure fields come from an implementation struct, |
| 88 |
which can be replaced on the fly. |
| 89 |
""" |
| 90 |
self._offset = offset |
| 91 |
#print self.__class__ |
| 92 |
#FIXME need to calculate offset |
| 93 |
#print "unpacking: " + str( self._structtype ) |
| 94 |
vals = self._layout[self.__class__.__name__].unpack_from( buf, offset ) |
| 95 |
structconstructor = self._structtype[0] |
| 96 |
self.__dict__['_struct'] = structconstructor( *vals ) |
| 97 |
# Note __dict__ is an auto member--other proper members |
| 98 |
# are items in __dict__. Here, avoid the use of __setitem__ |
| 99 |
# by explicitly placing _struct in __dict__. |
| 100 |
|
| 101 |
def __getattr__( self, name ): |
| 102 |
if name in self.__dict__: #don't override proper members |
| 103 |
return self.__dict__[name] |
| 104 |
if not '_struct' in self.__dict__: |
| 105 |
raise Exception( "No _struct member: " + str( self ) ) |
| 106 |
if name in self._struct.__dict__: |
| 107 |
return self._struct.__dict__[name] |
| 108 |
else: |
| 109 |
raise KeyError( "item " + name + " not found" ) |
| 110 |
|
| 111 |
def __setattr__( self, name, value ): |
| 112 |
if( '_struct' in self.__dict__ |
| 113 |
and name in self.__dict__['_struct'] ): |
| 114 |
self._struct.name = value |
| 115 |
else: |
| 116 |
self.__dict__[name] = value |
| 117 |
|
| 118 |
def __getitem__( self, key ): |
| 119 |
return self._struct[key] |
| 120 |
|
| 121 |
def __setitem__( self, key, value ): |
| 122 |
self._struct[key] = value |
| 123 |
|
| 124 |
def __str__( self ): |
| 125 |
self._astr = '' |
| 126 |
if hasattr( type(self), '_strcode' ): # class member |
| 127 |
#print( "in str - class:", str( type(self)._strcode ) ) |
| 128 |
exec( type(self)._strcode ) |
| 129 |
elif hasattr( self, '_strcode' ): # object member |
| 130 |
#print( "in str - obj:", str( self._strcode ) ) |
| 131 |
exec( self._strcode ) |
| 132 |
else: |
| 133 |
self._astr = repr( self ) |
| 134 |
return self._astr |
| 135 |
|
| 136 |
def getOffset( self ): |
| 137 |
return self._offset |
| 138 |
|
| 139 |
def getSize( self ): |
| 140 |
#FIXME some tables change size, so this won't work for them |
| 141 |
thestruct = self._layout[self.__class__.__name__] |
| 142 |
return thestruct.size |
| 143 |
|
| 144 |
def writeInto( self, buf, offset=0 ): |
| 145 |
#FIXME some tables change size, so this won't work for them |
| 146 |
thestruct = self._layout[self.__class__.__name__] |
| 147 |
vals = [ self.__getattr__( i.name ) for i in self._fdlist ] |
| 148 |
res = thestruct.pack_into( buf, offset, *vals ) |
| 149 |
return res |
| 150 |
|
| 151 |
def getChecksum( self ): |
| 152 |
wL = BigEndian32BitList() |
| 153 |
for desc in self._field_desc: |
| 154 |
key = desc[0] |
| 155 |
val = self.__getattr__( key ) |
| 156 |
size = FieldDesc.size_OT_type( desc[1] ) |
| 157 |
#print( "size val key", size, val, key ) |
| 158 |
wL.accumVal( val, size ) |
| 159 |
return wL.get32BitSum() |
| 160 |
|
| 161 |
class Table( BaseTable ): |
| 162 |
|
| 163 |
def __init__( self, buf, offset = 0 ): |
| 164 |
BaseTable.__init__( self, buf, offset ) |
| 165 |
|
| 166 |
@classmethod |
| 167 |
def getTableSize( cls ): |
| 168 |
return cls._size |
| 169 |
|
| 170 |
@classmethod |
| 171 |
def getFieldDesc( cls ): |
| 172 |
return cls._fdlist |
| 173 |
|
| 174 |
class VariableSizedTable( BaseTable ): |
| 175 |
def __init__( self, buf, offset = 0 ): |
| 176 |
_setup_structs( self ) |
| 177 |
BaseTable.__init__( self, buf, offset ) |
| 178 |
|
| 179 |
def getTableSize( self ): |
| 180 |
return self._size |
| 181 |
|
| 182 |
def getFieldDesc( self ): |
| 183 |
return self._fdlist |
| 184 |
|
| 185 |
class ReferredTable( Table ): |
| 186 |
@classmethod |
| 187 |
def getTableName( cls ): |
| 188 |
return _tag; |
| 189 |
|
| 190 |
class TableRecord( Table ): |
| 191 |
def __init__( self, buf, parent, index ): |
| 192 |
self._itemno = index + 1 |
| 193 |
self._parent = parent |
| 194 |
offset = parent.getOffset() + parent.getTableSize() + index * type(self)._size |
| 195 |
Table.__init__( self, buf, offset ) |
| 196 |
|
| 197 |
def getItemNo( self ): |
| 198 |
#FIXME what if order of items is changed in parent? |
| 199 |
return self._itemno |
| 200 |
|
| 201 |
def getParentTable( self ): |
| 202 |
return self._parent |
| 203 |
|
| 204 |
class StructArrayTable( VariableSizedTable ): |
| 205 |
"""Table that contains a variable-sized array of other tables. |
| 206 |
The "size" of such a table is thus more complex: |
| 207 |
Has "getHeaderSize", "getTotalSize" |
| 208 |
""" |
| 209 |
def __init__( self, filebuf, offset ): |
| 210 |
VariableSizedTable.__init__( self, filebuf, offset ) |
| 211 |
self._items = [] |
| 212 |
if not getattr( self, '_item_type' ): |
| 213 |
raise Exception( |
| 214 |
"Subclasses must set _item_type class member" ) |
| 215 |
|
| 216 |
def getItemSize( self ): |
| 217 |
return self._item_type.getTableSize() |
| 218 |
|
| 219 |
def getNumItems( self ): |
| 220 |
raise Exception( "Subclasses must override" ) |
| 221 |
|
| 222 |
def getItems( self, filebuf ): |
| 223 |
if not self._items: |
| 224 |
size = self.getTableSize() |
| 225 |
self._readItems( filebuf, size + self._offset ) |
| 226 |
return self._items |
| 227 |
|
| 228 |
def getItem( self, filebuf, idx ): |
| 229 |
self.getItems( filebuf ) |
| 230 |
if self._items: |
| 231 |
return self._items[idx] |
| 232 |
else: |
| 233 |
raise Exception( 'Got no items.' ) |
| 234 |
|
| 235 |
def _readItems( self, filebuf, start ): |
| 236 |
item_size = self.getItemSize() |
| 237 |
for i in range( self.getNumItems() ): |
| 238 |
sr = self._item_type( filebuf, start + item_size * i ) |
| 239 |
self._items.append( sr ) |
| 240 |
|
| 241 |
#@classmethod |
| 242 |
def getTableHeaderSize( cls ): |
| 243 |
return cls.getTableSize() |
| 244 |
|
| 245 |
class NestedStructTable( BaseTable ): |
| 246 |
"""Table that contains a fixed mixture of primitive and named structs. |
| 247 |
I think in OpenType, the structs always appear last in such a mixed |
| 248 |
situation, so that assumption is reflected here. |
| 249 |
|
| 250 |
WORK IN PROGRESS |
| 251 |
""" |
| 252 |
def __init__( self, buf, offset = 0 ): |
| 253 |
""" Idea is to override the member access functions, so that |
| 254 |
font structure fields come from an implementation struct, |
| 255 |
which can be replaced on the fly. |
| 256 |
""" |
| 257 |
raise Exception( "Not implemented" ) |
| 258 |
self._offset = offset |
| 259 |
BaseTable.__init__( self, filebuf, offset ) |
| 260 |
#print self.__class__ |
| 261 |
#FIXME need to calculate offset |
| 262 |
#print "unpacking: " + str( self._structtype ) |
| 263 |
for desc in self._field_desc: |
| 264 |
vals = self._layout[self.__class__.__name__].unpack_from( buf, offset ) |
| 265 |
structconstructor = self._structtype[0] |
| 266 |
self.__dict__['_struct'] = structconstructor( *vals ) |
| 267 |
# Note __dict__ is an auto member--other proper members |
| 268 |
# are items in __dict__. Here, avoid the use of __setitem__ |
| 269 |
# by explicitly placing _struct in __dict__. |
| 270 |
|
| 271 |
def getItemSize( self ): |
| 272 |
return self._item_type.getTableSize() |
| 273 |
|
| 274 |
#@classmethod |
| 275 |
def getTableHeaderSize( cls ): |
| 276 |
return cls.getTableSize() |
| 277 |
|
| 278 |
class SimpleArrayTable( VariableSizedTable ): |
| 279 |
""" An array of primitive types (ints, etc). |
| 280 |
Subclasses indicate a primitive type with the _item_type class member. |
| 281 |
""" |
| 282 |
def __init__( self, filebuf, offset ): |
| 283 |
VariableSizedTable.__init__( self, filebuf, offset ) |
| 284 |
self._items = () |
| 285 |
if not hasattr( self, '_item_type' ): |
| 286 |
raise Exception( |
| 287 |
'Subclasses must set _item_type class member' ) |
| 288 |
if not FieldDesc.has_type_symbol( self._item_type ): |
| 289 |
raise Exception( '_item_type not a known simple type' ) |
| 290 |
|
| 291 |
def getItems( self, filebuf ): |
| 292 |
if not self._items: |
| 293 |
size = self.getTableSize() |
| 294 |
self._readItems( filebuf, size + self._offset ) |
| 295 |
return self._items |
| 296 |
|
| 297 |
def _readItems( self, filebuf, start ): |
| 298 |
item_size = FieldDesc.size_OT_type( self._item_type ) |
| 299 |
item_format = FieldDesc.type_to_format( self._item_type ) |
| 300 |
fmt_str = '>' + str( self.getNumItems() ) + item_format |
| 301 |
self._items = struct.unpack_from( fmt_str, filebuf, start ) |
| 302 |
|
| 303 |
def getItem( self, filebuf, idx ): |
| 304 |
self.getItems( filebuf ) |
| 305 |
return self._items[ idx ] |
| 306 |
|
| 307 |
def getTableHeaderSize( cls ): |
| 308 |
return cls.getTableSize() |
| 309 |
|
| 310 |
class TableOffsetArrayTable( SimpleArrayTable ): |
| 311 |
_item_type = 'uint16' |
| 312 |
def __init__( self, filebuf, offset ): |
| 313 |
SimpleArrayTable.__init__( self, filebuf, offset ) |
| 314 |
if not getattr( self, '_reference_type' ): |
| 315 |
raise Exception( |
| 316 |
"Subclasses must set _reference_type class member" ) |
| 317 |
|
| 318 |
def getReferencedItems( self, filebuf ): |
| 319 |
return [ self.getReferencedItem( filebuf, i ) |
| 320 |
for i in range( self.getNumItems() ) ] |
| 321 |
|
| 322 |
def getReferencedItem( self, filebuf, idx ): |
| 323 |
off = self.getItem( filebuf, idx ) |
| 324 |
return self._reference_type( filebuf, self._offset + off ) |
| 325 |
|