| 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 for TrueType font cmap tables. |
| 23 |
""" |
| 24 |
from .table import Table, _setup_structs, registerStructFields, TableRecord |
| 25 |
|
| 26 |
_cmap_subtable_common = [ |
| 27 |
( 'format', 'uint16', None, 'Format number' ), |
| 28 |
] |
| 29 |
_cmap_0_fields = [ |
| 30 |
( 'length', 'uint16', None, 'length of subtable in bytes' ), |
| 31 |
( 'language', 'uint16', None, '' ), |
| 32 |
( 'glyphIdArray', 'string[256]', None, 'maps char code to glyph IDs' ) |
| 33 |
] |
| 34 |
_cmap_2_fields = [ |
| 35 |
( 'length', 'uint16', None, 'length of subtable in bytes' ), |
| 36 |
( 'language', 'uint16', None, '' ), |
| 37 |
( 'subHeaderKeys', 'uint16[256]', None, 'maps hi bytes to subheaders' ), |
| 38 |
( 'subHeaders', 'uint16[5]', None, 'maps hi bytes to subheaders', 4 ), |
| 39 |
( 'glyphIndexArray', 'uint16', None, 'maps hi bytes to subheaders' ) |
| 40 |
] |
| 41 |
_cmap_4_fields = [ |
| 42 |
( 'length', 'uint16', None, 'length of subtable in bytes' ), |
| 43 |
( 'language', 'uint16', None, '' ), |
| 44 |
( 'secCountX2', 'uint16', None, 'segCount x 2' ), |
| 45 |
( 'searchRange', 'uint16', None, '2x(2^floor( log_2( segCount ) ) )' ), |
| 46 |
( 'entrySelector', 'uint16', None, 'log_2( searchRange / 2 )' ), |
| 47 |
( 'rangeShift', 'uint16', None, '2 x segCount - searchRange' ), |
| 48 |
] |
| 49 |
_cmap_6_fields = [ |
| 50 |
( 'length', 'uint16', None, 'length of subtable in bytes' ), |
| 51 |
( 'language', 'uint16', None, '' ), |
| 52 |
( 'firstCode', 'uint16', None, 'first character code' ), |
| 53 |
( 'entryCount', 'uint16', None, 'number of character codes' ), |
| 54 |
( 'glyphIdArray', 'uint16[]', None, 'char code glyph index array' ), |
| 55 |
] |
| 56 |
_cmap_8_fields = [ |
| 57 |
( 'reserved', 'uint16', None, 'reserved; set to 0' ), |
| 58 |
( 'length', 'uint32', None, 'byte length subtable and header' ), |
| 59 |
( 'language', 'uint32', None, '' ), |
| 60 |
( 'is32', 'byte[8192]', None, 'each shows if index is 32 bit char' ), |
| 61 |
( 'nGroups', 'uint32', None, 'number of groups to follow' ), |
| 62 |
] |
| 63 |
_cmap_10_fields = [ |
| 64 |
( 'reserved', 'uint16', None, 'set to 0' ), |
| 65 |
( 'length', 'uint32', None, 'byte length of subtable and header' ), |
| 66 |
( 'language', 'uint32', None, '' ), |
| 67 |
( 'startCharCode', 'uint32', None, 'first character code' ), |
| 68 |
( 'numChars', 'uint32', None, 'number of character codes' ), |
| 69 |
( 'glyphs', 'uint16', None, 'char code glyph index array' ), |
| 70 |
] |
| 71 |
_cmap_12_fields = [ |
| 72 |
( 'reserved', 'uint16', None, 'set to 0' ), |
| 73 |
( 'length', 'uint32', None, 'byte length of subtable and header' ), |
| 74 |
( 'language', 'uint32', None, '' ), |
| 75 |
( 'nGroups', 'uint32', None, 'number of groups to follow' ), |
| 76 |
] |
| 77 |
_cmap_13_fields = [ |
| 78 |
( 'reserved', 'uint16', None, 'set to 0' ), |
| 79 |
( 'length', 'uint32', None, 'byte length of subtable and header' ), |
| 80 |
( 'language', 'uint32', None, '' ), |
| 81 |
( 'nGroups', 'uint32', None, 'number of groups to follow' ), |
| 82 |
] |
| 83 |
_cmap_14_fields = [ |
| 84 |
( 'length', 'uint32', None, 'byte length of subtable and header' ), |
| 85 |
( 'numVarSelectorRecords', 'uint32', None, |
| 86 |
'number of variation selector records' ), |
| 87 |
] |
| 88 |
|
| 89 |
@_setup_structs |
| 90 |
@registerStructFields |
| 91 |
class cmapSubtable( Table ): |
| 92 |
_name = 'cmap_generic_subtable' |
| 93 |
_field_desc = _cmap_subtable_common |
| 94 |
_further_field_desc = [] |
| 95 |
|
| 96 |
def _copy( self ): |
| 97 |
self.format = other.format |
| 98 |
|
| 99 |
""" Format 0: byte encoding table """ |
| 100 |
@_setup_structs |
| 101 |
@registerStructFields |
| 102 |
class cmap0Subtable( cmapSubtable ): |
| 103 |
_name = 'cmap_0' |
| 104 |
_further_field_desc = [ |
| 105 |
( 'glyphIdArray', 'string[256]', None, 'maps char code to glyph IDs' ) |
| 106 |
] |
| 107 |
|
| 108 |
""" Format 2: high-byte mapping through table. |
| 109 |
For CJK encodings.""" |
| 110 |
@_setup_structs |
| 111 |
@registerStructFields |
| 112 |
class cmap2Subtable( cmapSubtable ): |
| 113 |
_name = 'cmap_2' |
| 114 |
_further_field_desc = [ |
| 115 |
( 'subHeaderKeys', 'uint16[256]', None, 'maps hi bytes to subheaders' ), |
| 116 |
""" |
| 117 |
( 'subHeaders', 'uint16[4]', None, 'maps hi bytes to subheaders' ), |
| 118 |
( 'glyphIndexArray', 'uint16', None, 'maps hi bytes to subheaders' ) |
| 119 |
""" |
| 120 |
] |
| 121 |
#FIXME lots of problems with this. Immediately followed by two arrays. |
| 122 |
def __init__( self, buf, offset = 0 ): |
| 123 |
raise CMAPTableException( "Unsupported 'cmap' subtable type 2" ) |
| 124 |
|
| 125 |
class cmap2Subheader( TableRecord ): |
| 126 |
_name = 'cmap_2_subheader' |
| 127 |
_field_desc = [ |
| 128 |
( 'firstCode', 'uint16', None, 'first "valid" low byte for subheader' ), |
| 129 |
( 'entryCount', 'uint16', None, 'num "valid" low bytes for subheader' ), |
| 130 |
( 'idDelta', 'int16', None, 'complicated' ), |
| 131 |
( 'idRangeOffset', 'uint16', None, 'complicated' ), |
| 132 |
] |
| 133 |
|
| 134 |
def _cmap4variable_fields( sc ): |
| 135 |
return [ |
| 136 |
( 'endCount', 'uint16', None, 'End character code for segments', sc ), |
| 137 |
( 'reservedPad', 'uint16', None, 'padding set to 0' ), |
| 138 |
( 'startCount', 'uint16', None, 'Start char code for segments', sc ), |
| 139 |
( 'idDelta', 'int16', None, 'Delta for char codes in segment', sc ), |
| 140 |
( 'idRangeOffset', 'uint16', None, 'Offsets of glyphIdArray or 0', sc ), |
| 141 |
( 'glyphIdArray', 'uint16', None, 'Glyph index array', '[]'), |
| 142 |
] |
| 143 |
|
| 144 |
""" Format 4: segment mapping to delta values """ |
| 145 |
@_setup_structs |
| 146 |
@registerStructFields |
| 147 |
class cmap4Subtable( cmapSubtable ): |
| 148 |
_name = 'cmap_4' |
| 149 |
_further_field_desc = [ |
| 150 |
( 'secCountX2', 'uint16', None, 'segCount x 2' ), |
| 151 |
( 'searchRange', 'uint16', None, '2x(2^floor( log_2( segCount ) ) )' ), |
| 152 |
( 'entrySelector', 'uint16', None, 'log_2( searchRange / 2 )' ), |
| 153 |
( 'rangeShift', 'uint16', None, '2 x segCount - searchRange' ), |
| 154 |
] |
| 155 |
def __init__( self, buf, offset = 0 ): |
| 156 |
TableRecord.__init__( self, buf, offset ) |
| 157 |
sc = self.segCountX2 / 2 |
| 158 |
|
| 159 |
""" Format 6: trimmed table mapping """ |
| 160 |
@_setup_structs |
| 161 |
@registerStructFields |
| 162 |
class cmap6Subtable( cmapSubtable ): |
| 163 |
_name = 'cmap_6' |
| 164 |
_further_field_desc = [ |
| 165 |
( 'secCountX2', 'uint16', None, 'segCount x 2' ), |
| 166 |
( 'searchRange', 'uint16', None, '2x(2^floor( log_2( segCount ) ) )' ), |
| 167 |
( 'entrySelector', 'uint16', None, 'log_2( searchRange / 2 )' ), |
| 168 |
( 'rangeShift', 'uint16', None, '2 x segCount - searchRange' ), |
| 169 |
] |
| 170 |
def __init__( self, buf, offset = 0 ): |
| 171 |
TableRecord.__init__( self, buf, offset ) |
| 172 |
sc = self.segCountX2 / 2 |
| 173 |
|
| 174 |
""" Format 8: mixed 16-bit and 32-bit coverage """ |
| 175 |
@_setup_structs |
| 176 |
@registerStructFields |
| 177 |
class cmap8Subtable( cmapSubtable ): |
| 178 |
_name = 'cmap_8' |
| 179 |
_further_field_desc = [ |
| 180 |
( 'startCharCode', 'uint32', None, '1st char code of group.' ), |
| 181 |
( 'endCharCode', 'uint32', None, 'last char code in group' ), |
| 182 |
( 'startGlyphID', 'uint32', None, 'glyph index of start char code' ), |
| 183 |
] |
| 184 |
def __init__( self, buf, offset = 0 ): |
| 185 |
TableRecord.__init__( self, buf, offset ) |
| 186 |
|
| 187 |
""" Format 10: trimmed array """ |
| 188 |
@_setup_structs |
| 189 |
@registerStructFields |
| 190 |
class cmap10Subtable( cmapSubtable ): |
| 191 |
_name = 'cmap_10' |
| 192 |
_further_field_desc = [] |
| 193 |
def __init__( self, buf, offset = 0 ): |
| 194 |
TableRecord.__init__( self, buf, offset ) |
| 195 |
|
| 196 |
""" Format 12: segmented coverage """ |
| 197 |
@_setup_structs |
| 198 |
@registerStructFields |
| 199 |
class cmap12Subtable( cmapSubtable ): |
| 200 |
_name = 'cmap_12' |
| 201 |
_further_field_desc = [ |
| 202 |
( 'startCharCode', 'uint32', None, '1st char code of group.' ), |
| 203 |
( 'endCharCode', 'uint32', None, 'last char code in group' ), |
| 204 |
( 'startGlyphID', 'uint32', None, 'glyph index of start char code' ), |
| 205 |
] |
| 206 |
def __init__( self, buf, offset = 0 ): |
| 207 |
TableRecord.__init__( self, buf, offset ) |
| 208 |
|
| 209 |
""" Format 13: last resort font """ |
| 210 |
@_setup_structs |
| 211 |
@registerStructFields |
| 212 |
class cmap13Subtable( cmapSubtable ): |
| 213 |
_name = 'cmap_13' |
| 214 |
_further_field_desc = [ |
| 215 |
( 'startCharCode', 'uint32', None, '1st char code of group' ), |
| 216 |
( 'endCharCode', 'uint32', None, 'last char code in group' ), |
| 217 |
( 'glyphID', 'uint32', None, 'glyph index of chars in range' ), |
| 218 |
] |
| 219 |
def __init__( self, buf, offset = 0 ): |
| 220 |
TableRecord.__init__( self, buf, offset ) |
| 221 |
|
| 222 |
""" Format 14: Unicode variation sequences """ |
| 223 |
""" Mysterious phrase: |
| 224 |
"Unicode Variation Sequences supported by the font may be specified in |
| 225 |
the cmap table only under platform ID 0 and encoding ID 5, using a |
| 226 |
format 14 cmap subtable." |
| 227 |
""" |
| 228 |
@_setup_structs |
| 229 |
@registerStructFields |
| 230 |
class cmap14Subtable( cmapSubtable ): |
| 231 |
_name = 'cmap_14' |
| 232 |
_further_field_desc = [ |
| 233 |
( 'varSelector', 'uint24', None, 'variation selector' ), |
| 234 |
( 'defaultUVSOffset', 'uint32', None, |
| 235 |
'offset to Default UVS Table, or 0' ), |
| 236 |
( 'nonDefaultUVSOffset', 'uint32', None, |
| 237 |
'offset to Non-Default UVS Table, or 0' ), |
| 238 |
] |
| 239 |
def __init__( self, buf, offset = 0 ): |
| 240 |
TableRecord.__init__( self, buf, offset ) |
| 241 |
|
| 242 |
class cmapSubtableFactory( object ): |
| 243 |
_subtable_versions = { |
| 244 |
0: cmap0Subtable, |
| 245 |
2: cmap2Subtable, |
| 246 |
4: cmap4Subtable, |
| 247 |
6: cmap6Subtable, |
| 248 |
8: cmap8Subtable, |
| 249 |
10: cmap10Subtable, |
| 250 |
12: cmap12Subtable, |
| 251 |
13: cmap13Subtable, |
| 252 |
14: cmap14Subtable, |
| 253 |
} |
| 254 |
def make( self, buf, offset ): |
| 255 |
base = cmapSubtable( buf, offset ) |
| 256 |
if not base.version in _subtable_versions: |
| 257 |
raise CMAPTableException( |
| 258 |
"Unknown 'cmap' table version: " + str( base.version ) ) |
| 259 |
constr = cmapSubtableFactory._subtable_versions[base.version] |
| 260 |
return constr( base, buf, offset ) |
| 261 |
|