Debian Codegraph @

tools/generate/tweeks/OpenType/fontdirectory.py

raw
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__ = """ Classes representing the basic OpenType/TrueType Font
23 Directory tables.
24 """
25 from .table import Table, registerStructFields, _setup_structs
26 from .typeutils import int_to_tag
27
28 """ OpenType/TrueType font directory tables.
29 """
30 @_setup_structs
31 @registerStructFields
32 class OffsetTable( Table ):
33 _name = 'Offset'
34 _field_desc = [
35 ( 'sfnt_ver', 'int32', 'Fixed' ),
36 ( 'numTables', 'uint16', ),
37 ( 'searchRange', 'uint16', ),
38 ( 'entrySelector', 'uint16', ),
39 ( 'rangeShift', 'uint16', ),
40 ]
41
42 @_setup_structs
43 @registerStructFields
44 class DirectoryEntry( Table ):
45 _name = 'Entry'
46 _field_desc = [
47 ( 'tag', 'uint32', 'tag' ),
48 ( 'checkSum', 'uint32', 'hex'),
49 ( 'offset', 'uint32', ),
50 ( 'length', 'uint32', ),
51 ]
52
53 def getTagStr( self ):
54 return int_to_tag( self.tag )
55
56 def getIndex( self ):
57 return self._index
58
59 def __init__( self, buf, entryIndex ):
60 self._index = entryIndex
61 offset = OffsetTable._size + self._index * DirectoryEntry._size
62 Table.__init__( self, buf, offset )
63
64 def getDirectoryEntriesByTag( filebuf ):
65 if len( filebuf ) == 0:
66 raise Exception( "Buffer appears to be empty!" )
67
68 ot = OffsetTable( filebuf )
69
70 entries = {}
71 for i in range( 0, ot.numTables ):
72 de = DirectoryEntry( filebuf, i )
73 entries[int_to_tag(de.tag)] = de
74
75 return entries