| 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, 2017, 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 the core required OpenType/TrueType font tables. |
| 23 |
""" |
| 24 |
from .table import Table, _setup_structs, ReferredTable, registerStructFields, TableRecord |
| 25 |
|
| 26 |
@_setup_structs |
| 27 |
@registerStructFields |
| 28 |
class postTable( ReferredTable ): |
| 29 |
_name = 'post' |
| 30 |
_field_desc = [ |
| 31 |
( 'format', 'uint32', 'Fixed' ), |
| 32 |
( 'italicAngle', 'uint32', 'Fixed' ), |
| 33 |
( 'underlinePosition', 'int16', ), |
| 34 |
( 'underlineThickness', 'uint16', ), |
| 35 |
( 'isFixedPitch', 'uint32', 'bool' ), |
| 36 |
( 'minMemType42', 'uint32', ), |
| 37 |
( 'maxMemType42', 'uint32', ), |
| 38 |
( 'minMemType1', 'uint32', ), |
| 39 |
( 'maxMemType1', 'uint32', ), |
| 40 |
] |
| 41 |
|
| 42 |
@_setup_structs |
| 43 |
@registerStructFields |
| 44 |
class headTable( ReferredTable ): |
| 45 |
_name = 'head' |
| 46 |
_field_desc = [ |
| 47 |
( 'version', 'uint32', 'Fixed' ), |
| 48 |
( 'fontRevision', 'uint32', 'Fixed' ), |
| 49 |
( 'checkSumAdjustment', 'uint32', ), |
| 50 |
( 'magicNumber', 'uint32', 'hex' ), |
| 51 |
( 'flags', 'uint16', 'bitfield' ), |
| 52 |
( 'unitsPerEm', 'uint16', ), |
| 53 |
( 'created', 'int64', 'longdatetime' ), |
| 54 |
( 'modified', 'int64', 'longdatetime' ), |
| 55 |
( 'xMin', 'int16', ), |
| 56 |
( 'yMin', 'int16', ), |
| 57 |
( 'xMax', 'int16', ), |
| 58 |
( 'yMax', 'int16', ), |
| 59 |
( 'macStyle', 'uint16', 'bitfield' ), |
| 60 |
( 'lowestRecPPEM', 'uint16', ) , |
| 61 |
( 'fontDirectionHint', 'int16', ), |
| 62 |
( 'indexToLocFormat', 'int16', ), |
| 63 |
( 'glyphDataFormat', 'int16', ), |
| 64 |
] |
| 65 |
|
| 66 |
@_setup_structs |
| 67 |
@registerStructFields |
| 68 |
class hheaTable( ReferredTable ): |
| 69 |
_name = 'hhea' |
| 70 |
_field_desc = [ |
| 71 |
( 'version', 'uint32', 'Fixed' ), |
| 72 |
( 'ascent', 'int16' ), |
| 73 |
( 'descent', 'int16' ), |
| 74 |
( 'linegap', 'int16' ), |
| 75 |
( 'advanceWidthMax', 'uint16' ), |
| 76 |
( 'minLeftSideBearing', 'int16' ), |
| 77 |
( 'minRightSideBearing', 'int16' ), |
| 78 |
( 'xMaxExtent', 'int16' ), |
| 79 |
( 'caretSlopeRise', 'int16' ), |
| 80 |
( 'caretSlopeRun', 'int16' ), |
| 81 |
( 'caretOffset', 'uint16' ), |
| 82 |
( 'reserved1', 'int16' ), |
| 83 |
( 'reserved2', 'int16' ), |
| 84 |
( 'reserved3', 'int16' ), |
| 85 |
( 'reserved4', 'int16' ), |
| 86 |
( 'metricDataFormat', 'int16' ), |
| 87 |
( 'numOfLongHorMetrics', 'uint16' ), |
| 88 |
] |
| 89 |
|
| 90 |
@_setup_structs |
| 91 |
@registerStructFields |
| 92 |
class maxpTable( ReferredTable ): |
| 93 |
_name = 'maxp' |
| 94 |
_field_desc = [ |
| 95 |
( 'version', 'uint32', 'Fixed', |
| 96 |
'0x00010000 (1.0)' ), |
| 97 |
( 'numGlyphs', 'uint16', None, |
| 98 |
'the number of glyphs in the font' ), |
| 99 |
( 'maxPoints', 'uint16', None, |
| 100 |
'points in non-compound glyph' ), |
| 101 |
( 'maxContours', 'uint16', None, |
| 102 |
'contours in non-compound glyph' ), |
| 103 |
( 'maxComponentPoints', 'uint16', None, |
| 104 |
'points in compound glyph' ), |
| 105 |
( 'maxComponentContours', 'uint16', None, |
| 106 |
'contours in compound glyph' ), |
| 107 |
( 'maxZones', 'uint16', None, |
| 108 |
'set to 2' ), |
| 109 |
( 'maxTwilightPoints', 'uint16', None, |
| 110 |
'points used in Twilight Zone (Z0)' ), |
| 111 |
( 'maxStorage', 'uint16', None, |
| 112 |
'number of Storage Area locations' ), |
| 113 |
( 'maxFunctionDefs', 'uint16', None, |
| 114 |
'number of FDEFs' ), |
| 115 |
( 'maxInstructionDefs', 'uint16', None, |
| 116 |
'number of IDEFs' ), |
| 117 |
( 'maxStackElements', 'uint16', None, |
| 118 |
'maximum stack depth' ), |
| 119 |
( 'maxSizeOfInstructions', 'uint16', None, |
| 120 |
'byte count for glyph instructions' ), |
| 121 |
( 'maxComponentElements', 'uint16', None, |
| 122 |
'number of glyphs referenced at top level' ), |
| 123 |
( 'maxComponentDepth', 'uint16', None, |
| 124 |
'levels of recursion, set to 0 if font has only simple glyphs' ), |
| 125 |
|
| 126 |
] |
| 127 |
|
| 128 |
@_setup_structs |
| 129 |
@registerStructFields |
| 130 |
class OS_2Table( ReferredTable ): |
| 131 |
""" See https://www.microsoft.com/typography/otspec/os2.htm |
| 132 |
The table changes size depending on its "version" entry. |
| 133 |
This very much complicates reading it. |
| 134 |
Even messier: the names of the fields changes between versions. |
| 135 |
That means, code that refers to the table has to check the version, |
| 136 |
and/or check that it has a given field, in order to use it. |
| 137 |
""" |
| 138 |
_name = 'OS/2' |
| 139 |
_field_desc_0 = [ |
| 140 |
( 'version', 'uint16', 'Fixed', |
| 141 |
'(always 0)' ), |
| 142 |
( 'xAvgCharWidth', 'uint16', None, |
| 143 |
'Average weighted advance width of lower case letters' ), |
| 144 |
( 'usWeightClass', 'uint16', None, |
| 145 |
'Glyph visual weight' ), |
| 146 |
( 'usWidthClass', 'uint16', None, |
| 147 |
'Multiplier of normal glyph aspect ratio (width to height)' ), |
| 148 |
( 'fsType', 'int16', None, |
| 149 |
'font characteristics and properties' ), |
| 150 |
( 'ySubscriptXSize', 'int16', None, |
| 151 |
'Horizontal size in pixels of subscripts' ), |
| 152 |
( 'ySubscriptYSize', 'int16', None, |
| 153 |
'Vertical size in pixels of subscripts' ), |
| 154 |
( 'ySubscriptXOffset', 'int16', None, |
| 155 |
'Horizontal offset of subscripts' ), |
| 156 |
( 'ySubscriptYOffset', 'int16', None, |
| 157 |
'Vertical offset from baseline of subscripts' ), |
| 158 |
( 'ySuperscriptXSize', 'int16', None, |
| 159 |
'Horizontal size in pixels of superscripts' ), |
| 160 |
( 'ySuperscriptYSize', 'int16', None, |
| 161 |
'Vertical size in pixels of superscripts' ), |
| 162 |
( 'ySuperscriptXOffset', 'int16', None, |
| 163 |
'Horizontal offset of superscripts from baseline' ), |
| 164 |
( 'ySuperscriptYOffset', 'int16', None, |
| 165 |
'Vertical offset of superscripts from baseline' ), |
| 166 |
( 'yStrikeoutSize', 'int16', None, |
| 167 |
'Width of strikeout stroke' ), |
| 168 |
( 'yStrikeoutPosition', 'int16', None, |
| 169 |
'Offset of strikeout stroke from baseline' ), |
| 170 |
( 'sFamilyClass', 'int16', None, |
| 171 |
'font-family design classification' ), |
| 172 |
( 'panose', 'PANOSE', 'PANOSE', |
| 173 |
'Visual characteristics of typeface' ), |
| 174 |
( 'ulUnicodeRange1', 'uint32', 'bitfield', |
| 175 |
'Low 96 bits specify Unicode blocks supported by font.' ), |
| 176 |
( 'ulUnicodeRange2', 'uint32', 'bitfield', |
| 177 |
'Low 96 bits specify Unicode blocks supported by font.' ), |
| 178 |
( 'ulUnicodeRange3', 'uint32', 'bitfield', |
| 179 |
'Low 96 bits specify Unicode blocks supported by font.' ), |
| 180 |
( 'ulUnicodeRange4', 'uint32', 'bitfield', |
| 181 |
'High 32 bits specify script sets supported by font' ), |
| 182 |
( 'achVendID', 'int32', 'tag', |
| 183 |
'Font vendor ID' ), |
| 184 |
( 'fsSelection', 'uint16', 'bitfield', |
| 185 |
'Font patterns info' ), |
| 186 |
( 'usFirstCharIndex', 'uint16', None, |
| 187 |
'Minimum Unicode value in font' ), |
| 188 |
( 'usLastCharIndex', 'uint16', None, |
| 189 |
'Maximum Unicode value in font' ), |
| 190 |
( 'sTypoAscender', 'int16', None, |
| 191 |
'' ), |
| 192 |
( 'sTypoDescender', 'int16', None, |
| 193 |
'' ), |
| 194 |
( 'sTypoLineGap', 'int16', None, |
| 195 |
'' ), |
| 196 |
( 'usWinAscent', 'uint16', None, |
| 197 |
'' ), |
| 198 |
( 'usWinDescent', 'uint16', None, |
| 199 |
'' ), |
| 200 |
] |
| 201 |
_field_desc_0_1 = [ |
| 202 |
( 'ulCodePageRange1', 'uint32', 'bitfield', |
| 203 |
'' ), |
| 204 |
( 'ulCodePageRange2', 'uint32', 'bitfield', |
| 205 |
'' ), |
| 206 |
] |
| 207 |
_field_desc_1_2 = [ |
| 208 |
( 'sxHeight', 'int16', None, |
| 209 |
'' ), |
| 210 |
( 'sxCapHeight', 'int16', None, |
| 211 |
'' ), |
| 212 |
( 'usDefaultChar', 'uint16', None, |
| 213 |
'' ), |
| 214 |
( 'usBreakChar', 'uint16', None, |
| 215 |
'' ), |
| 216 |
( 'usMaxContext', 'uint16', None, |
| 217 |
'' ), |
| 218 |
] |
| 219 |
_field_desc_2_5 = [ |
| 220 |
( 'usLowerOpticalPointSize', 'uint16', None, |
| 221 |
'' ), |
| 222 |
( 'usUpperOpticalPointSize', 'uint16', None, |
| 223 |
'' ), |
| 224 |
] |
| 225 |
#FIXME this is valid only for versions 2, 3 and 4 of the OS/2 table |
| 226 |
_field_desc = _field_desc_0 + _field_desc_0_1 + _field_desc_1_2 |
| 227 |
|
| 228 |
@_setup_structs |
| 229 |
@registerStructFields |
| 230 |
class cmapTable( ReferredTable ): |
| 231 |
_name = 'cmap' |
| 232 |
_field_desc = [ |
| 233 |
( 'version', 'uint16', None, 'Set to 0' ), |
| 234 |
( 'numberSubtables', 'uint16', None, 'Number of encoding subtables' ), |
| 235 |
] |
| 236 |
|
| 237 |
def __init__( self, buf, offset = 0 ): |
| 238 |
self._records = [] |
| 239 |
ReferredTable.__init__( self, buf, offset ) |
| 240 |
#print "cmapTable " + str( self ) |
| 241 |
for i in range( self.numberSubtables ): |
| 242 |
rec = cmapRecord( buf, self, i ) |
| 243 |
self._records.append( rec ) |
| 244 |
|
| 245 |
def getRecords( self ): |
| 246 |
return self._records |
| 247 |
|
| 248 |
cmapPlatformIDs = { |
| 249 |
0: "Other", |
| 250 |
1: "Macintosh", |
| 251 |
3: "Windows", |
| 252 |
} |
| 253 |
""" MS requires at least one format 4 cmap table, with 'platform ID' = 3 |
| 254 |
which uses the 'Encoding ID' field, where is interpreted as follows. |
| 255 |
""" |
| 256 |
cmapEncodingIDs = { |
| 257 |
0: 'Symbol', # on Mac, this is default. |
| 258 |
1: 'Unicode BMP (UCS-2)', # preferred for Windows fonts |
| 259 |
2: 'ShiftJIS', |
| 260 |
3: 'PRC', |
| 261 |
4: 'Big5', |
| 262 |
5: 'Wansung', |
| 263 |
6: 'Johab', |
| 264 |
7: 'Reserved', |
| 265 |
8: 'Reserved', |
| 266 |
9: 'Reserved', |
| 267 |
10: 'Unicode UCS-4', |
| 268 |
} |
| 269 |
|
| 270 |
class CMAPTableException( Exception ): |
| 271 |
pass |
| 272 |
|
| 273 |
@_setup_structs |
| 274 |
@registerStructFields |
| 275 |
class cmapRecord( TableRecord ): |
| 276 |
_name = 'cmapRecord' |
| 277 |
_field_desc = [ |
| 278 |
( 'platformID', 'uint16', None, 'Platform ID' ), |
| 279 |
( 'encodingID', 'uint16', None, 'Platform-specific encoding ID' ), |
| 280 |
( 'offset', 'uint32', None, 'Offset from table start to record' ), |
| 281 |
] |
| 282 |
|
| 283 |
def getRecordContents(): |
| 284 |
return _contents |
| 285 |
|
| 286 |
def __init__( self, buf, parent, index ): |
| 287 |
self._contents = None |
| 288 |
TableRecord.__init__( self, buf, parent, index ) |
| 289 |
if not self.platformID in cmapPlatformIDs: |
| 290 |
raise CMAPTableException( |
| 291 |
"Unknown cmap platform ID in cmap encoding record: " |
| 292 |
+ str( self ) ) |
| 293 |
if not self.encodingID in cmapEncodingIDs: |
| 294 |
raise CMAPTableException( |
| 295 |
"Unknown cmap encoding ID in cmap encoding record: " |
| 296 |
+ str( self ) ) |
| 297 |
#print str( self ) |
| 298 |
self._contents = cmapSubtable( buf, self.offset ) |
| 299 |
|