fonttools 4.60.2__cp311-cp311-win32.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fontTools/__init__.py +8 -0
- fontTools/__main__.py +35 -0
- fontTools/afmLib.py +439 -0
- fontTools/agl.py +5233 -0
- fontTools/annotations.py +30 -0
- fontTools/cffLib/CFF2ToCFF.py +258 -0
- fontTools/cffLib/CFFToCFF2.py +305 -0
- fontTools/cffLib/__init__.py +3694 -0
- fontTools/cffLib/specializer.py +927 -0
- fontTools/cffLib/transforms.py +495 -0
- fontTools/cffLib/width.py +210 -0
- fontTools/colorLib/__init__.py +0 -0
- fontTools/colorLib/builder.py +664 -0
- fontTools/colorLib/errors.py +2 -0
- fontTools/colorLib/geometry.py +143 -0
- fontTools/colorLib/table_builder.py +223 -0
- fontTools/colorLib/unbuilder.py +81 -0
- fontTools/config/__init__.py +90 -0
- fontTools/cu2qu/__init__.py +15 -0
- fontTools/cu2qu/__main__.py +6 -0
- fontTools/cu2qu/benchmark.py +54 -0
- fontTools/cu2qu/cli.py +198 -0
- fontTools/cu2qu/cu2qu.c +15817 -0
- fontTools/cu2qu/cu2qu.cp311-win32.pyd +0 -0
- fontTools/cu2qu/cu2qu.py +563 -0
- fontTools/cu2qu/errors.py +77 -0
- fontTools/cu2qu/ufo.py +363 -0
- fontTools/designspaceLib/__init__.py +3343 -0
- fontTools/designspaceLib/__main__.py +6 -0
- fontTools/designspaceLib/split.py +475 -0
- fontTools/designspaceLib/statNames.py +260 -0
- fontTools/designspaceLib/types.py +147 -0
- fontTools/encodings/MacRoman.py +258 -0
- fontTools/encodings/StandardEncoding.py +258 -0
- fontTools/encodings/__init__.py +1 -0
- fontTools/encodings/codecs.py +135 -0
- fontTools/feaLib/__init__.py +4 -0
- fontTools/feaLib/__main__.py +78 -0
- fontTools/feaLib/ast.py +2143 -0
- fontTools/feaLib/builder.py +1814 -0
- fontTools/feaLib/error.py +22 -0
- fontTools/feaLib/lexer.c +17029 -0
- fontTools/feaLib/lexer.cp311-win32.pyd +0 -0
- fontTools/feaLib/lexer.py +287 -0
- fontTools/feaLib/location.py +12 -0
- fontTools/feaLib/lookupDebugInfo.py +12 -0
- fontTools/feaLib/parser.py +2394 -0
- fontTools/feaLib/variableScalar.py +118 -0
- fontTools/fontBuilder.py +1014 -0
- fontTools/help.py +36 -0
- fontTools/merge/__init__.py +248 -0
- fontTools/merge/__main__.py +6 -0
- fontTools/merge/base.py +81 -0
- fontTools/merge/cmap.py +173 -0
- fontTools/merge/layout.py +526 -0
- fontTools/merge/options.py +85 -0
- fontTools/merge/tables.py +352 -0
- fontTools/merge/unicode.py +78 -0
- fontTools/merge/util.py +143 -0
- fontTools/misc/__init__.py +1 -0
- fontTools/misc/arrayTools.py +424 -0
- fontTools/misc/bezierTools.c +39731 -0
- fontTools/misc/bezierTools.cp311-win32.pyd +0 -0
- fontTools/misc/bezierTools.py +1500 -0
- fontTools/misc/classifyTools.py +170 -0
- fontTools/misc/cliTools.py +53 -0
- fontTools/misc/configTools.py +349 -0
- fontTools/misc/cython.py +27 -0
- fontTools/misc/dictTools.py +83 -0
- fontTools/misc/eexec.py +119 -0
- fontTools/misc/encodingTools.py +72 -0
- fontTools/misc/enumTools.py +23 -0
- fontTools/misc/etree.py +456 -0
- fontTools/misc/filenames.py +245 -0
- fontTools/misc/filesystem/__init__.py +68 -0
- fontTools/misc/filesystem/_base.py +134 -0
- fontTools/misc/filesystem/_copy.py +45 -0
- fontTools/misc/filesystem/_errors.py +54 -0
- fontTools/misc/filesystem/_info.py +75 -0
- fontTools/misc/filesystem/_osfs.py +164 -0
- fontTools/misc/filesystem/_path.py +67 -0
- fontTools/misc/filesystem/_subfs.py +92 -0
- fontTools/misc/filesystem/_tempfs.py +34 -0
- fontTools/misc/filesystem/_tools.py +34 -0
- fontTools/misc/filesystem/_walk.py +55 -0
- fontTools/misc/filesystem/_zipfs.py +204 -0
- fontTools/misc/fixedTools.py +253 -0
- fontTools/misc/intTools.py +25 -0
- fontTools/misc/iterTools.py +12 -0
- fontTools/misc/lazyTools.py +42 -0
- fontTools/misc/loggingTools.py +543 -0
- fontTools/misc/macCreatorType.py +56 -0
- fontTools/misc/macRes.py +261 -0
- fontTools/misc/plistlib/__init__.py +681 -0
- fontTools/misc/plistlib/py.typed +0 -0
- fontTools/misc/psCharStrings.py +1511 -0
- fontTools/misc/psLib.py +398 -0
- fontTools/misc/psOperators.py +572 -0
- fontTools/misc/py23.py +96 -0
- fontTools/misc/roundTools.py +110 -0
- fontTools/misc/sstruct.py +227 -0
- fontTools/misc/symfont.py +242 -0
- fontTools/misc/testTools.py +233 -0
- fontTools/misc/textTools.py +156 -0
- fontTools/misc/timeTools.py +88 -0
- fontTools/misc/transform.py +516 -0
- fontTools/misc/treeTools.py +45 -0
- fontTools/misc/vector.py +147 -0
- fontTools/misc/visitor.py +158 -0
- fontTools/misc/xmlReader.py +188 -0
- fontTools/misc/xmlWriter.py +231 -0
- fontTools/mtiLib/__init__.py +1400 -0
- fontTools/mtiLib/__main__.py +5 -0
- fontTools/otlLib/__init__.py +1 -0
- fontTools/otlLib/builder.py +3465 -0
- fontTools/otlLib/error.py +11 -0
- fontTools/otlLib/maxContextCalc.py +96 -0
- fontTools/otlLib/optimize/__init__.py +53 -0
- fontTools/otlLib/optimize/__main__.py +6 -0
- fontTools/otlLib/optimize/gpos.py +439 -0
- fontTools/pens/__init__.py +1 -0
- fontTools/pens/areaPen.py +52 -0
- fontTools/pens/basePen.py +475 -0
- fontTools/pens/boundsPen.py +98 -0
- fontTools/pens/cairoPen.py +26 -0
- fontTools/pens/cocoaPen.py +26 -0
- fontTools/pens/cu2quPen.py +325 -0
- fontTools/pens/explicitClosingLinePen.py +101 -0
- fontTools/pens/filterPen.py +433 -0
- fontTools/pens/freetypePen.py +462 -0
- fontTools/pens/hashPointPen.py +89 -0
- fontTools/pens/momentsPen.c +13378 -0
- fontTools/pens/momentsPen.cp311-win32.pyd +0 -0
- fontTools/pens/momentsPen.py +879 -0
- fontTools/pens/perimeterPen.py +69 -0
- fontTools/pens/pointInsidePen.py +192 -0
- fontTools/pens/pointPen.py +643 -0
- fontTools/pens/qtPen.py +29 -0
- fontTools/pens/qu2cuPen.py +105 -0
- fontTools/pens/quartzPen.py +43 -0
- fontTools/pens/recordingPen.py +335 -0
- fontTools/pens/reportLabPen.py +79 -0
- fontTools/pens/reverseContourPen.py +96 -0
- fontTools/pens/roundingPen.py +130 -0
- fontTools/pens/statisticsPen.py +312 -0
- fontTools/pens/svgPathPen.py +310 -0
- fontTools/pens/t2CharStringPen.py +88 -0
- fontTools/pens/teePen.py +55 -0
- fontTools/pens/transformPen.py +115 -0
- fontTools/pens/ttGlyphPen.py +335 -0
- fontTools/pens/wxPen.py +29 -0
- fontTools/qu2cu/__init__.py +15 -0
- fontTools/qu2cu/__main__.py +7 -0
- fontTools/qu2cu/benchmark.py +56 -0
- fontTools/qu2cu/cli.py +125 -0
- fontTools/qu2cu/qu2cu.c +16682 -0
- fontTools/qu2cu/qu2cu.cp311-win32.pyd +0 -0
- fontTools/qu2cu/qu2cu.py +405 -0
- fontTools/subset/__init__.py +4096 -0
- fontTools/subset/__main__.py +6 -0
- fontTools/subset/cff.py +184 -0
- fontTools/subset/svg.py +253 -0
- fontTools/subset/util.py +25 -0
- fontTools/svgLib/__init__.py +3 -0
- fontTools/svgLib/path/__init__.py +65 -0
- fontTools/svgLib/path/arc.py +154 -0
- fontTools/svgLib/path/parser.py +322 -0
- fontTools/svgLib/path/shapes.py +183 -0
- fontTools/t1Lib/__init__.py +648 -0
- fontTools/tfmLib.py +460 -0
- fontTools/ttLib/__init__.py +30 -0
- fontTools/ttLib/__main__.py +148 -0
- fontTools/ttLib/macUtils.py +54 -0
- fontTools/ttLib/removeOverlaps.py +395 -0
- fontTools/ttLib/reorderGlyphs.py +285 -0
- fontTools/ttLib/scaleUpem.py +436 -0
- fontTools/ttLib/sfnt.py +661 -0
- fontTools/ttLib/standardGlyphOrder.py +271 -0
- fontTools/ttLib/tables/B_A_S_E_.py +14 -0
- fontTools/ttLib/tables/BitmapGlyphMetrics.py +64 -0
- fontTools/ttLib/tables/C_B_D_T_.py +113 -0
- fontTools/ttLib/tables/C_B_L_C_.py +19 -0
- fontTools/ttLib/tables/C_F_F_.py +61 -0
- fontTools/ttLib/tables/C_F_F__2.py +26 -0
- fontTools/ttLib/tables/C_O_L_R_.py +165 -0
- fontTools/ttLib/tables/C_P_A_L_.py +305 -0
- fontTools/ttLib/tables/D_S_I_G_.py +158 -0
- fontTools/ttLib/tables/D__e_b_g.py +35 -0
- fontTools/ttLib/tables/DefaultTable.py +49 -0
- fontTools/ttLib/tables/E_B_D_T_.py +835 -0
- fontTools/ttLib/tables/E_B_L_C_.py +718 -0
- fontTools/ttLib/tables/F_F_T_M_.py +52 -0
- fontTools/ttLib/tables/F__e_a_t.py +149 -0
- fontTools/ttLib/tables/G_D_E_F_.py +13 -0
- fontTools/ttLib/tables/G_M_A_P_.py +148 -0
- fontTools/ttLib/tables/G_P_K_G_.py +133 -0
- fontTools/ttLib/tables/G_P_O_S_.py +14 -0
- fontTools/ttLib/tables/G_S_U_B_.py +13 -0
- fontTools/ttLib/tables/G_V_A_R_.py +5 -0
- fontTools/ttLib/tables/G__l_a_t.py +235 -0
- fontTools/ttLib/tables/G__l_o_c.py +85 -0
- fontTools/ttLib/tables/H_V_A_R_.py +13 -0
- fontTools/ttLib/tables/J_S_T_F_.py +13 -0
- fontTools/ttLib/tables/L_T_S_H_.py +58 -0
- fontTools/ttLib/tables/M_A_T_H_.py +13 -0
- fontTools/ttLib/tables/M_E_T_A_.py +352 -0
- fontTools/ttLib/tables/M_V_A_R_.py +13 -0
- fontTools/ttLib/tables/O_S_2f_2.py +752 -0
- fontTools/ttLib/tables/S_I_N_G_.py +99 -0
- fontTools/ttLib/tables/S_T_A_T_.py +15 -0
- fontTools/ttLib/tables/S_V_G_.py +223 -0
- fontTools/ttLib/tables/S__i_l_f.py +1040 -0
- fontTools/ttLib/tables/S__i_l_l.py +92 -0
- fontTools/ttLib/tables/T_S_I_B_.py +13 -0
- fontTools/ttLib/tables/T_S_I_C_.py +14 -0
- fontTools/ttLib/tables/T_S_I_D_.py +13 -0
- fontTools/ttLib/tables/T_S_I_J_.py +13 -0
- fontTools/ttLib/tables/T_S_I_P_.py +13 -0
- fontTools/ttLib/tables/T_S_I_S_.py +13 -0
- fontTools/ttLib/tables/T_S_I_V_.py +26 -0
- fontTools/ttLib/tables/T_S_I__0.py +70 -0
- fontTools/ttLib/tables/T_S_I__1.py +163 -0
- fontTools/ttLib/tables/T_S_I__2.py +17 -0
- fontTools/ttLib/tables/T_S_I__3.py +22 -0
- fontTools/ttLib/tables/T_S_I__5.py +60 -0
- fontTools/ttLib/tables/T_T_F_A_.py +14 -0
- fontTools/ttLib/tables/TupleVariation.py +884 -0
- fontTools/ttLib/tables/V_A_R_C_.py +12 -0
- fontTools/ttLib/tables/V_D_M_X_.py +249 -0
- fontTools/ttLib/tables/V_O_R_G_.py +165 -0
- fontTools/ttLib/tables/V_V_A_R_.py +13 -0
- fontTools/ttLib/tables/__init__.py +98 -0
- fontTools/ttLib/tables/_a_n_k_r.py +15 -0
- fontTools/ttLib/tables/_a_v_a_r.py +193 -0
- fontTools/ttLib/tables/_b_s_l_n.py +15 -0
- fontTools/ttLib/tables/_c_i_d_g.py +24 -0
- fontTools/ttLib/tables/_c_m_a_p.py +1591 -0
- fontTools/ttLib/tables/_c_v_a_r.py +94 -0
- fontTools/ttLib/tables/_c_v_t.py +56 -0
- fontTools/ttLib/tables/_f_e_a_t.py +15 -0
- fontTools/ttLib/tables/_f_p_g_m.py +62 -0
- fontTools/ttLib/tables/_f_v_a_r.py +261 -0
- fontTools/ttLib/tables/_g_a_s_p.py +63 -0
- fontTools/ttLib/tables/_g_c_i_d.py +13 -0
- fontTools/ttLib/tables/_g_l_y_f.py +2311 -0
- fontTools/ttLib/tables/_g_v_a_r.py +340 -0
- fontTools/ttLib/tables/_h_d_m_x.py +127 -0
- fontTools/ttLib/tables/_h_e_a_d.py +130 -0
- fontTools/ttLib/tables/_h_h_e_a.py +147 -0
- fontTools/ttLib/tables/_h_m_t_x.py +164 -0
- fontTools/ttLib/tables/_k_e_r_n.py +289 -0
- fontTools/ttLib/tables/_l_c_a_r.py +13 -0
- fontTools/ttLib/tables/_l_o_c_a.py +70 -0
- fontTools/ttLib/tables/_l_t_a_g.py +72 -0
- fontTools/ttLib/tables/_m_a_x_p.py +147 -0
- fontTools/ttLib/tables/_m_e_t_a.py +112 -0
- fontTools/ttLib/tables/_m_o_r_t.py +14 -0
- fontTools/ttLib/tables/_m_o_r_x.py +15 -0
- fontTools/ttLib/tables/_n_a_m_e.py +1242 -0
- fontTools/ttLib/tables/_o_p_b_d.py +14 -0
- fontTools/ttLib/tables/_p_o_s_t.py +319 -0
- fontTools/ttLib/tables/_p_r_e_p.py +16 -0
- fontTools/ttLib/tables/_p_r_o_p.py +12 -0
- fontTools/ttLib/tables/_s_b_i_x.py +129 -0
- fontTools/ttLib/tables/_t_r_a_k.py +332 -0
- fontTools/ttLib/tables/_v_h_e_a.py +139 -0
- fontTools/ttLib/tables/_v_m_t_x.py +19 -0
- fontTools/ttLib/tables/asciiTable.py +20 -0
- fontTools/ttLib/tables/grUtils.py +92 -0
- fontTools/ttLib/tables/otBase.py +1458 -0
- fontTools/ttLib/tables/otConverters.py +2068 -0
- fontTools/ttLib/tables/otData.py +6400 -0
- fontTools/ttLib/tables/otTables.py +2703 -0
- fontTools/ttLib/tables/otTraverse.py +163 -0
- fontTools/ttLib/tables/sbixGlyph.py +149 -0
- fontTools/ttLib/tables/sbixStrike.py +177 -0
- fontTools/ttLib/tables/table_API_readme.txt +91 -0
- fontTools/ttLib/tables/ttProgram.py +594 -0
- fontTools/ttLib/ttCollection.py +125 -0
- fontTools/ttLib/ttFont.py +1148 -0
- fontTools/ttLib/ttGlyphSet.py +490 -0
- fontTools/ttLib/ttVisitor.py +32 -0
- fontTools/ttLib/woff2.py +1680 -0
- fontTools/ttx.py +479 -0
- fontTools/ufoLib/__init__.py +2575 -0
- fontTools/ufoLib/converters.py +407 -0
- fontTools/ufoLib/errors.py +30 -0
- fontTools/ufoLib/etree.py +6 -0
- fontTools/ufoLib/filenames.py +356 -0
- fontTools/ufoLib/glifLib.py +2120 -0
- fontTools/ufoLib/kerning.py +141 -0
- fontTools/ufoLib/plistlib.py +47 -0
- fontTools/ufoLib/pointPen.py +6 -0
- fontTools/ufoLib/utils.py +107 -0
- fontTools/ufoLib/validators.py +1208 -0
- fontTools/unicode.py +50 -0
- fontTools/unicodedata/Blocks.py +817 -0
- fontTools/unicodedata/Mirrored.py +446 -0
- fontTools/unicodedata/OTTags.py +50 -0
- fontTools/unicodedata/ScriptExtensions.py +832 -0
- fontTools/unicodedata/Scripts.py +3639 -0
- fontTools/unicodedata/__init__.py +306 -0
- fontTools/varLib/__init__.py +1600 -0
- fontTools/varLib/__main__.py +6 -0
- fontTools/varLib/avar/__init__.py +0 -0
- fontTools/varLib/avar/__main__.py +72 -0
- fontTools/varLib/avar/build.py +79 -0
- fontTools/varLib/avar/map.py +108 -0
- fontTools/varLib/avar/plan.py +1004 -0
- fontTools/varLib/avar/unbuild.py +271 -0
- fontTools/varLib/avarPlanner.py +8 -0
- fontTools/varLib/builder.py +215 -0
- fontTools/varLib/cff.py +631 -0
- fontTools/varLib/errors.py +219 -0
- fontTools/varLib/featureVars.py +703 -0
- fontTools/varLib/hvar.py +113 -0
- fontTools/varLib/instancer/__init__.py +2052 -0
- fontTools/varLib/instancer/__main__.py +5 -0
- fontTools/varLib/instancer/featureVars.py +190 -0
- fontTools/varLib/instancer/names.py +388 -0
- fontTools/varLib/instancer/solver.py +309 -0
- fontTools/varLib/interpolatable.py +1209 -0
- fontTools/varLib/interpolatableHelpers.py +399 -0
- fontTools/varLib/interpolatablePlot.py +1269 -0
- fontTools/varLib/interpolatableTestContourOrder.py +82 -0
- fontTools/varLib/interpolatableTestStartingPoint.py +107 -0
- fontTools/varLib/interpolate_layout.py +124 -0
- fontTools/varLib/iup.c +19815 -0
- fontTools/varLib/iup.cp311-win32.pyd +0 -0
- fontTools/varLib/iup.py +490 -0
- fontTools/varLib/merger.py +1717 -0
- fontTools/varLib/models.py +642 -0
- fontTools/varLib/multiVarStore.py +253 -0
- fontTools/varLib/mutator.py +529 -0
- fontTools/varLib/mvar.py +40 -0
- fontTools/varLib/plot.py +238 -0
- fontTools/varLib/stat.py +149 -0
- fontTools/varLib/varStore.py +739 -0
- fontTools/voltLib/__init__.py +5 -0
- fontTools/voltLib/__main__.py +206 -0
- fontTools/voltLib/ast.py +452 -0
- fontTools/voltLib/error.py +12 -0
- fontTools/voltLib/lexer.py +99 -0
- fontTools/voltLib/parser.py +664 -0
- fontTools/voltLib/voltToFea.py +911 -0
- fonttools-4.60.2.data/data/share/man/man1/ttx.1 +225 -0
- fonttools-4.60.2.dist-info/METADATA +2250 -0
- fonttools-4.60.2.dist-info/RECORD +353 -0
- fonttools-4.60.2.dist-info/WHEEL +5 -0
- fonttools-4.60.2.dist-info/entry_points.txt +5 -0
- fonttools-4.60.2.dist-info/licenses/LICENSE +21 -0
- fonttools-4.60.2.dist-info/licenses/LICENSE.external +388 -0
- fonttools-4.60.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,1458 @@
|
|
|
1
|
+
from fontTools.config import OPTIONS
|
|
2
|
+
from fontTools.misc.textTools import Tag, bytesjoin
|
|
3
|
+
from .DefaultTable import DefaultTable
|
|
4
|
+
from enum import IntEnum
|
|
5
|
+
import sys
|
|
6
|
+
import array
|
|
7
|
+
import struct
|
|
8
|
+
import logging
|
|
9
|
+
from functools import lru_cache
|
|
10
|
+
from typing import Iterator, NamedTuple, Optional, Tuple
|
|
11
|
+
|
|
12
|
+
log = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
have_uharfbuzz = False
|
|
15
|
+
try:
|
|
16
|
+
import uharfbuzz as hb
|
|
17
|
+
|
|
18
|
+
have_uharfbuzz = True
|
|
19
|
+
except ImportError:
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
USE_HARFBUZZ_REPACKER = OPTIONS[f"{__name__}:USE_HARFBUZZ_REPACKER"]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class OverflowErrorRecord(object):
|
|
26
|
+
def __init__(self, overflowTuple):
|
|
27
|
+
self.tableType = overflowTuple[0]
|
|
28
|
+
self.LookupListIndex = overflowTuple[1]
|
|
29
|
+
self.SubTableIndex = overflowTuple[2]
|
|
30
|
+
self.itemName = overflowTuple[3]
|
|
31
|
+
self.itemIndex = overflowTuple[4]
|
|
32
|
+
|
|
33
|
+
def __repr__(self):
|
|
34
|
+
return str(
|
|
35
|
+
(
|
|
36
|
+
self.tableType,
|
|
37
|
+
"LookupIndex:",
|
|
38
|
+
self.LookupListIndex,
|
|
39
|
+
"SubTableIndex:",
|
|
40
|
+
self.SubTableIndex,
|
|
41
|
+
"ItemName:",
|
|
42
|
+
self.itemName,
|
|
43
|
+
"ItemIndex:",
|
|
44
|
+
self.itemIndex,
|
|
45
|
+
)
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class OTLOffsetOverflowError(Exception):
|
|
50
|
+
def __init__(self, overflowErrorRecord):
|
|
51
|
+
self.value = overflowErrorRecord
|
|
52
|
+
|
|
53
|
+
def __str__(self):
|
|
54
|
+
return repr(self.value)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class RepackerState(IntEnum):
|
|
58
|
+
# Repacking control flow is implemnted using a state machine. The state machine table:
|
|
59
|
+
#
|
|
60
|
+
# State | Packing Success | Packing Failed | Exception Raised |
|
|
61
|
+
# ------------+-----------------+----------------+------------------+
|
|
62
|
+
# PURE_FT | Return result | PURE_FT | Return failure |
|
|
63
|
+
# HB_FT | Return result | HB_FT | FT_FALLBACK |
|
|
64
|
+
# FT_FALLBACK | HB_FT | FT_FALLBACK | Return failure |
|
|
65
|
+
|
|
66
|
+
# Pack only with fontTools, don't allow sharing between extensions.
|
|
67
|
+
PURE_FT = 1
|
|
68
|
+
|
|
69
|
+
# Attempt to pack with harfbuzz (allowing sharing between extensions)
|
|
70
|
+
# use fontTools to attempt overflow resolution.
|
|
71
|
+
HB_FT = 2
|
|
72
|
+
|
|
73
|
+
# Fallback if HB/FT packing gets stuck. Pack only with fontTools, don't allow sharing between
|
|
74
|
+
# extensions.
|
|
75
|
+
FT_FALLBACK = 3
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class BaseTTXConverter(DefaultTable):
|
|
79
|
+
"""Generic base class for TTX table converters. It functions as an
|
|
80
|
+
adapter between the TTX (ttLib actually) table model and the model
|
|
81
|
+
we use for OpenType tables, which is necessarily subtly different.
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
def decompile(self, data, font):
|
|
85
|
+
"""Create an object from the binary data. Called automatically on access."""
|
|
86
|
+
from . import otTables
|
|
87
|
+
|
|
88
|
+
reader = OTTableReader(data, tableTag=self.tableTag)
|
|
89
|
+
tableClass = getattr(otTables, self.tableTag)
|
|
90
|
+
self.table = tableClass()
|
|
91
|
+
self.table.decompile(reader, font)
|
|
92
|
+
|
|
93
|
+
def compile(self, font):
|
|
94
|
+
"""Compiles the table into binary. Called automatically on save."""
|
|
95
|
+
|
|
96
|
+
# General outline:
|
|
97
|
+
# Create a top-level OTTableWriter for the GPOS/GSUB table.
|
|
98
|
+
# Call the compile method for the the table
|
|
99
|
+
# for each 'converter' record in the table converter list
|
|
100
|
+
# call converter's write method for each item in the value.
|
|
101
|
+
# - For simple items, the write method adds a string to the
|
|
102
|
+
# writer's self.items list.
|
|
103
|
+
# - For Struct/Table/Subtable items, it add first adds new writer to the
|
|
104
|
+
# to the writer's self.items, then calls the item's compile method.
|
|
105
|
+
# This creates a tree of writers, rooted at the GUSB/GPOS writer, with
|
|
106
|
+
# each writer representing a table, and the writer.items list containing
|
|
107
|
+
# the child data strings and writers.
|
|
108
|
+
# call the getAllData method
|
|
109
|
+
# call _doneWriting, which removes duplicates
|
|
110
|
+
# call _gatherTables. This traverses the tables, adding unique occurences to a flat list of tables
|
|
111
|
+
# Traverse the flat list of tables, calling getDataLength on each to update their position
|
|
112
|
+
# Traverse the flat list of tables again, calling getData each get the data in the table, now that
|
|
113
|
+
# pos's and offset are known.
|
|
114
|
+
|
|
115
|
+
# If a lookup subtable overflows an offset, we have to start all over.
|
|
116
|
+
overflowRecord = None
|
|
117
|
+
# this is 3-state option: default (None) means automatically use hb.repack or
|
|
118
|
+
# silently fall back if it fails; True, use it and raise error if not possible
|
|
119
|
+
# or it errors out; False, don't use it, even if you can.
|
|
120
|
+
use_hb_repack = font.cfg[USE_HARFBUZZ_REPACKER]
|
|
121
|
+
if self.tableTag in ("GSUB", "GPOS"):
|
|
122
|
+
if use_hb_repack is False:
|
|
123
|
+
log.debug(
|
|
124
|
+
"hb.repack disabled, compiling '%s' with pure-python serializer",
|
|
125
|
+
self.tableTag,
|
|
126
|
+
)
|
|
127
|
+
elif not have_uharfbuzz:
|
|
128
|
+
if use_hb_repack is True:
|
|
129
|
+
raise ImportError("No module named 'uharfbuzz'")
|
|
130
|
+
else:
|
|
131
|
+
assert use_hb_repack is None
|
|
132
|
+
log.debug(
|
|
133
|
+
"uharfbuzz not found, compiling '%s' with pure-python serializer",
|
|
134
|
+
self.tableTag,
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
if (
|
|
138
|
+
use_hb_repack in (None, True)
|
|
139
|
+
and have_uharfbuzz
|
|
140
|
+
and self.tableTag in ("GSUB", "GPOS")
|
|
141
|
+
):
|
|
142
|
+
state = RepackerState.HB_FT
|
|
143
|
+
else:
|
|
144
|
+
state = RepackerState.PURE_FT
|
|
145
|
+
|
|
146
|
+
hb_first_error_logged = False
|
|
147
|
+
lastOverflowRecord = None
|
|
148
|
+
while True:
|
|
149
|
+
try:
|
|
150
|
+
writer = OTTableWriter(tableTag=self.tableTag)
|
|
151
|
+
self.table.compile(writer, font)
|
|
152
|
+
if state == RepackerState.HB_FT:
|
|
153
|
+
return self.tryPackingHarfbuzz(writer, hb_first_error_logged)
|
|
154
|
+
elif state == RepackerState.PURE_FT:
|
|
155
|
+
return self.tryPackingFontTools(writer)
|
|
156
|
+
elif state == RepackerState.FT_FALLBACK:
|
|
157
|
+
# Run packing with FontTools only, but don't return the result as it will
|
|
158
|
+
# not be optimally packed. Once a successful packing has been found, state is
|
|
159
|
+
# changed back to harfbuzz packing to produce the final, optimal, packing.
|
|
160
|
+
self.tryPackingFontTools(writer)
|
|
161
|
+
log.debug(
|
|
162
|
+
"Re-enabling sharing between extensions and switching back to "
|
|
163
|
+
"harfbuzz+fontTools packing."
|
|
164
|
+
)
|
|
165
|
+
state = RepackerState.HB_FT
|
|
166
|
+
|
|
167
|
+
except OTLOffsetOverflowError as e:
|
|
168
|
+
hb_first_error_logged = True
|
|
169
|
+
ok = self.tryResolveOverflow(font, e, lastOverflowRecord)
|
|
170
|
+
lastOverflowRecord = e.value
|
|
171
|
+
|
|
172
|
+
if ok:
|
|
173
|
+
continue
|
|
174
|
+
|
|
175
|
+
if state is RepackerState.HB_FT:
|
|
176
|
+
log.debug(
|
|
177
|
+
"Harfbuzz packing out of resolutions, disabling sharing between extensions and "
|
|
178
|
+
"switching to fontTools only packing."
|
|
179
|
+
)
|
|
180
|
+
state = RepackerState.FT_FALLBACK
|
|
181
|
+
else:
|
|
182
|
+
raise
|
|
183
|
+
|
|
184
|
+
def tryPackingHarfbuzz(self, writer, hb_first_error_logged):
|
|
185
|
+
try:
|
|
186
|
+
log.debug("serializing '%s' with hb.repack", self.tableTag)
|
|
187
|
+
return writer.getAllDataUsingHarfbuzz(self.tableTag)
|
|
188
|
+
except (ValueError, MemoryError, hb.RepackerError) as e:
|
|
189
|
+
# Only log hb repacker errors the first time they occur in
|
|
190
|
+
# the offset-overflow resolution loop, they are just noisy.
|
|
191
|
+
# Maybe we can revisit this if/when uharfbuzz actually gives
|
|
192
|
+
# us more info as to why hb.repack failed...
|
|
193
|
+
if not hb_first_error_logged:
|
|
194
|
+
error_msg = f"{type(e).__name__}"
|
|
195
|
+
if str(e) != "":
|
|
196
|
+
error_msg += f": {e}"
|
|
197
|
+
log.warning(
|
|
198
|
+
"hb.repack failed to serialize '%s', attempting fonttools resolutions "
|
|
199
|
+
"; the error message was: %s",
|
|
200
|
+
self.tableTag,
|
|
201
|
+
error_msg,
|
|
202
|
+
)
|
|
203
|
+
hb_first_error_logged = True
|
|
204
|
+
return writer.getAllData(remove_duplicate=False)
|
|
205
|
+
|
|
206
|
+
def tryPackingFontTools(self, writer):
|
|
207
|
+
return writer.getAllData()
|
|
208
|
+
|
|
209
|
+
def tryResolveOverflow(self, font, e, lastOverflowRecord):
|
|
210
|
+
ok = 0
|
|
211
|
+
if lastOverflowRecord == e.value:
|
|
212
|
+
# Oh well...
|
|
213
|
+
return ok
|
|
214
|
+
|
|
215
|
+
overflowRecord = e.value
|
|
216
|
+
log.info("Attempting to fix OTLOffsetOverflowError %s", e)
|
|
217
|
+
|
|
218
|
+
if overflowRecord.itemName is None:
|
|
219
|
+
from .otTables import fixLookupOverFlows
|
|
220
|
+
|
|
221
|
+
ok = fixLookupOverFlows(font, overflowRecord)
|
|
222
|
+
else:
|
|
223
|
+
from .otTables import fixSubTableOverFlows
|
|
224
|
+
|
|
225
|
+
ok = fixSubTableOverFlows(font, overflowRecord)
|
|
226
|
+
|
|
227
|
+
if ok:
|
|
228
|
+
return ok
|
|
229
|
+
|
|
230
|
+
# Try upgrading lookup to Extension and hope
|
|
231
|
+
# that cross-lookup sharing not happening would
|
|
232
|
+
# fix overflow...
|
|
233
|
+
from .otTables import fixLookupOverFlows
|
|
234
|
+
|
|
235
|
+
return fixLookupOverFlows(font, overflowRecord)
|
|
236
|
+
|
|
237
|
+
def toXML(self, writer, font):
|
|
238
|
+
self.table.toXML2(writer, font)
|
|
239
|
+
|
|
240
|
+
def fromXML(self, name, attrs, content, font):
|
|
241
|
+
from . import otTables
|
|
242
|
+
|
|
243
|
+
if not hasattr(self, "table"):
|
|
244
|
+
tableClass = getattr(otTables, self.tableTag)
|
|
245
|
+
self.table = tableClass()
|
|
246
|
+
self.table.fromXML(name, attrs, content, font)
|
|
247
|
+
self.table.populateDefaults()
|
|
248
|
+
|
|
249
|
+
def ensureDecompiled(self, recurse=True):
|
|
250
|
+
self.table.ensureDecompiled(recurse=recurse)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
# https://github.com/fonttools/fonttools/pull/2285#issuecomment-834652928
|
|
254
|
+
assert len(struct.pack("i", 0)) == 4
|
|
255
|
+
assert array.array("i").itemsize == 4, "Oops, file a bug against fonttools."
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
class OTTableReader(object):
|
|
259
|
+
"""Helper class to retrieve data from an OpenType table."""
|
|
260
|
+
|
|
261
|
+
__slots__ = ("data", "offset", "pos", "localState", "tableTag")
|
|
262
|
+
|
|
263
|
+
def __init__(self, data, localState=None, offset=0, tableTag=None):
|
|
264
|
+
self.data = data
|
|
265
|
+
self.offset = offset
|
|
266
|
+
self.pos = offset
|
|
267
|
+
self.localState = localState
|
|
268
|
+
self.tableTag = tableTag
|
|
269
|
+
|
|
270
|
+
def advance(self, count):
|
|
271
|
+
self.pos += count
|
|
272
|
+
|
|
273
|
+
def seek(self, pos):
|
|
274
|
+
self.pos = pos
|
|
275
|
+
|
|
276
|
+
def copy(self):
|
|
277
|
+
other = self.__class__(self.data, self.localState, self.offset, self.tableTag)
|
|
278
|
+
other.pos = self.pos
|
|
279
|
+
return other
|
|
280
|
+
|
|
281
|
+
def getSubReader(self, offset):
|
|
282
|
+
offset = self.offset + offset
|
|
283
|
+
return self.__class__(self.data, self.localState, offset, self.tableTag)
|
|
284
|
+
|
|
285
|
+
def readValue(self, typecode, staticSize):
|
|
286
|
+
pos = self.pos
|
|
287
|
+
newpos = pos + staticSize
|
|
288
|
+
(value,) = struct.unpack(f">{typecode}", self.data[pos:newpos])
|
|
289
|
+
self.pos = newpos
|
|
290
|
+
return value
|
|
291
|
+
|
|
292
|
+
def readArray(self, typecode, staticSize, count):
|
|
293
|
+
pos = self.pos
|
|
294
|
+
newpos = pos + count * staticSize
|
|
295
|
+
value = array.array(typecode, self.data[pos:newpos])
|
|
296
|
+
if sys.byteorder != "big":
|
|
297
|
+
value.byteswap()
|
|
298
|
+
self.pos = newpos
|
|
299
|
+
return value.tolist()
|
|
300
|
+
|
|
301
|
+
def readInt8(self):
|
|
302
|
+
return self.readValue("b", staticSize=1)
|
|
303
|
+
|
|
304
|
+
def readInt8Array(self, count):
|
|
305
|
+
return self.readArray("b", staticSize=1, count=count)
|
|
306
|
+
|
|
307
|
+
def readShort(self):
|
|
308
|
+
return self.readValue("h", staticSize=2)
|
|
309
|
+
|
|
310
|
+
def readShortArray(self, count):
|
|
311
|
+
return self.readArray("h", staticSize=2, count=count)
|
|
312
|
+
|
|
313
|
+
def readLong(self):
|
|
314
|
+
return self.readValue("i", staticSize=4)
|
|
315
|
+
|
|
316
|
+
def readLongArray(self, count):
|
|
317
|
+
return self.readArray("i", staticSize=4, count=count)
|
|
318
|
+
|
|
319
|
+
def readUInt8(self):
|
|
320
|
+
return self.readValue("B", staticSize=1)
|
|
321
|
+
|
|
322
|
+
def readUInt8Array(self, count):
|
|
323
|
+
return self.readArray("B", staticSize=1, count=count)
|
|
324
|
+
|
|
325
|
+
def readUShort(self):
|
|
326
|
+
return self.readValue("H", staticSize=2)
|
|
327
|
+
|
|
328
|
+
def readUShortArray(self, count):
|
|
329
|
+
return self.readArray("H", staticSize=2, count=count)
|
|
330
|
+
|
|
331
|
+
def readULong(self):
|
|
332
|
+
return self.readValue("I", staticSize=4)
|
|
333
|
+
|
|
334
|
+
def readULongArray(self, count):
|
|
335
|
+
return self.readArray("I", staticSize=4, count=count)
|
|
336
|
+
|
|
337
|
+
def readUInt24(self):
|
|
338
|
+
pos = self.pos
|
|
339
|
+
newpos = pos + 3
|
|
340
|
+
(value,) = struct.unpack(">l", b"\0" + self.data[pos:newpos])
|
|
341
|
+
self.pos = newpos
|
|
342
|
+
return value
|
|
343
|
+
|
|
344
|
+
def readUInt24Array(self, count):
|
|
345
|
+
return [self.readUInt24() for _ in range(count)]
|
|
346
|
+
|
|
347
|
+
def readTag(self):
|
|
348
|
+
pos = self.pos
|
|
349
|
+
newpos = pos + 4
|
|
350
|
+
value = Tag(self.data[pos:newpos])
|
|
351
|
+
assert len(value) == 4, value
|
|
352
|
+
self.pos = newpos
|
|
353
|
+
return value
|
|
354
|
+
|
|
355
|
+
def readData(self, count):
|
|
356
|
+
pos = self.pos
|
|
357
|
+
newpos = pos + count
|
|
358
|
+
value = self.data[pos:newpos]
|
|
359
|
+
self.pos = newpos
|
|
360
|
+
return value
|
|
361
|
+
|
|
362
|
+
def __setitem__(self, name, value):
|
|
363
|
+
state = self.localState.copy() if self.localState else dict()
|
|
364
|
+
state[name] = value
|
|
365
|
+
self.localState = state
|
|
366
|
+
|
|
367
|
+
def __getitem__(self, name):
|
|
368
|
+
return self.localState and self.localState[name]
|
|
369
|
+
|
|
370
|
+
def __contains__(self, name):
|
|
371
|
+
return self.localState and name in self.localState
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
class OffsetToWriter(object):
|
|
375
|
+
def __init__(self, subWriter, offsetSize):
|
|
376
|
+
self.subWriter = subWriter
|
|
377
|
+
self.offsetSize = offsetSize
|
|
378
|
+
|
|
379
|
+
def __eq__(self, other):
|
|
380
|
+
if type(self) != type(other):
|
|
381
|
+
return NotImplemented
|
|
382
|
+
return self.subWriter == other.subWriter and self.offsetSize == other.offsetSize
|
|
383
|
+
|
|
384
|
+
def __hash__(self):
|
|
385
|
+
# only works after self._doneWriting() has been called
|
|
386
|
+
return hash((self.subWriter, self.offsetSize))
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
class OTTableWriter(object):
|
|
390
|
+
"""Helper class to gather and assemble data for OpenType tables."""
|
|
391
|
+
|
|
392
|
+
def __init__(self, localState=None, tableTag=None):
|
|
393
|
+
self.items = []
|
|
394
|
+
self.pos = None
|
|
395
|
+
self.localState = localState
|
|
396
|
+
self.tableTag = tableTag
|
|
397
|
+
self.parent = None
|
|
398
|
+
self.name = "<none>"
|
|
399
|
+
|
|
400
|
+
def __setitem__(self, name, value):
|
|
401
|
+
state = self.localState.copy() if self.localState else dict()
|
|
402
|
+
state[name] = value
|
|
403
|
+
self.localState = state
|
|
404
|
+
|
|
405
|
+
def __getitem__(self, name):
|
|
406
|
+
return self.localState[name]
|
|
407
|
+
|
|
408
|
+
def __delitem__(self, name):
|
|
409
|
+
del self.localState[name]
|
|
410
|
+
|
|
411
|
+
# assembler interface
|
|
412
|
+
|
|
413
|
+
def getDataLength(self):
|
|
414
|
+
"""Return the length of this table in bytes, without subtables."""
|
|
415
|
+
l = 0
|
|
416
|
+
for item in self.items:
|
|
417
|
+
if hasattr(item, "getCountData"):
|
|
418
|
+
l += item.size
|
|
419
|
+
elif hasattr(item, "subWriter"):
|
|
420
|
+
l += item.offsetSize
|
|
421
|
+
else:
|
|
422
|
+
l = l + len(item)
|
|
423
|
+
return l
|
|
424
|
+
|
|
425
|
+
def getData(self):
|
|
426
|
+
"""Assemble the data for this writer/table, without subtables."""
|
|
427
|
+
items = list(self.items) # make a shallow copy
|
|
428
|
+
pos = self.pos
|
|
429
|
+
numItems = len(items)
|
|
430
|
+
for i in range(numItems):
|
|
431
|
+
item = items[i]
|
|
432
|
+
|
|
433
|
+
if hasattr(item, "subWriter"):
|
|
434
|
+
if item.offsetSize == 4:
|
|
435
|
+
items[i] = packULong(item.subWriter.pos - pos)
|
|
436
|
+
elif item.offsetSize == 2:
|
|
437
|
+
try:
|
|
438
|
+
items[i] = packUShort(item.subWriter.pos - pos)
|
|
439
|
+
except struct.error:
|
|
440
|
+
# provide data to fix overflow problem.
|
|
441
|
+
overflowErrorRecord = self.getOverflowErrorRecord(
|
|
442
|
+
item.subWriter
|
|
443
|
+
)
|
|
444
|
+
|
|
445
|
+
raise OTLOffsetOverflowError(overflowErrorRecord)
|
|
446
|
+
elif item.offsetSize == 3:
|
|
447
|
+
items[i] = packUInt24(item.subWriter.pos - pos)
|
|
448
|
+
else:
|
|
449
|
+
raise ValueError(item.offsetSize)
|
|
450
|
+
|
|
451
|
+
return bytesjoin(items)
|
|
452
|
+
|
|
453
|
+
def getDataForHarfbuzz(self):
|
|
454
|
+
"""Assemble the data for this writer/table with all offset field set to 0"""
|
|
455
|
+
items = list(self.items)
|
|
456
|
+
packFuncs = {2: packUShort, 3: packUInt24, 4: packULong}
|
|
457
|
+
for i, item in enumerate(items):
|
|
458
|
+
if hasattr(item, "subWriter"):
|
|
459
|
+
# Offset value is not needed in harfbuzz repacker, so setting offset to 0 to avoid overflow here
|
|
460
|
+
if item.offsetSize in packFuncs:
|
|
461
|
+
items[i] = packFuncs[item.offsetSize](0)
|
|
462
|
+
else:
|
|
463
|
+
raise ValueError(item.offsetSize)
|
|
464
|
+
|
|
465
|
+
return bytesjoin(items)
|
|
466
|
+
|
|
467
|
+
def __hash__(self):
|
|
468
|
+
# only works after self._doneWriting() has been called
|
|
469
|
+
return hash(self.items)
|
|
470
|
+
|
|
471
|
+
def __ne__(self, other):
|
|
472
|
+
result = self.__eq__(other)
|
|
473
|
+
return result if result is NotImplemented else not result
|
|
474
|
+
|
|
475
|
+
def __eq__(self, other):
|
|
476
|
+
if type(self) != type(other):
|
|
477
|
+
return NotImplemented
|
|
478
|
+
return self.items == other.items
|
|
479
|
+
|
|
480
|
+
def _doneWriting(self, internedTables, shareExtension=False):
|
|
481
|
+
# Convert CountData references to data string items
|
|
482
|
+
# collapse duplicate table references to a unique entry
|
|
483
|
+
# "tables" are OTTableWriter objects.
|
|
484
|
+
|
|
485
|
+
# For Extension Lookup types, we can
|
|
486
|
+
# eliminate duplicates only within the tree under the Extension Lookup,
|
|
487
|
+
# as offsets may exceed 64K even between Extension LookupTable subtables.
|
|
488
|
+
isExtension = hasattr(self, "Extension")
|
|
489
|
+
|
|
490
|
+
# Certain versions of Uniscribe reject the font if the GSUB/GPOS top-level
|
|
491
|
+
# arrays (ScriptList, FeatureList, LookupList) point to the same, possibly
|
|
492
|
+
# empty, array. So, we don't share those.
|
|
493
|
+
# See: https://github.com/fonttools/fonttools/issues/518
|
|
494
|
+
dontShare = hasattr(self, "DontShare")
|
|
495
|
+
|
|
496
|
+
if isExtension and not shareExtension:
|
|
497
|
+
internedTables = {}
|
|
498
|
+
|
|
499
|
+
items = self.items
|
|
500
|
+
for i, item in enumerate(items):
|
|
501
|
+
if hasattr(item, "getCountData"):
|
|
502
|
+
items[i] = item.getCountData()
|
|
503
|
+
elif hasattr(item, "subWriter"):
|
|
504
|
+
item.subWriter._doneWriting(
|
|
505
|
+
internedTables, shareExtension=shareExtension
|
|
506
|
+
)
|
|
507
|
+
# At this point, all subwriters are hashable based on their items.
|
|
508
|
+
# (See hash and comparison magic methods above.) So the ``setdefault``
|
|
509
|
+
# call here will return the first writer object we've seen with
|
|
510
|
+
# equal content, or store it in the dictionary if it's not been
|
|
511
|
+
# seen yet. We therefore replace the subwriter object with an equivalent
|
|
512
|
+
# object, which deduplicates the tree.
|
|
513
|
+
if not dontShare:
|
|
514
|
+
items[i].subWriter = internedTables.setdefault(
|
|
515
|
+
item.subWriter, item.subWriter
|
|
516
|
+
)
|
|
517
|
+
self.items = tuple(items)
|
|
518
|
+
|
|
519
|
+
def _gatherTables(self, tables, extTables, done):
|
|
520
|
+
# Convert table references in self.items tree to a flat
|
|
521
|
+
# list of tables in depth-first traversal order.
|
|
522
|
+
# "tables" are OTTableWriter objects.
|
|
523
|
+
# We do the traversal in reverse order at each level, in order to
|
|
524
|
+
# resolve duplicate references to be the last reference in the list of tables.
|
|
525
|
+
# For extension lookups, duplicate references can be merged only within the
|
|
526
|
+
# writer tree under the extension lookup.
|
|
527
|
+
|
|
528
|
+
done[id(self)] = True
|
|
529
|
+
|
|
530
|
+
numItems = len(self.items)
|
|
531
|
+
iRange = list(range(numItems))
|
|
532
|
+
iRange.reverse()
|
|
533
|
+
|
|
534
|
+
isExtension = hasattr(self, "Extension")
|
|
535
|
+
|
|
536
|
+
selfTables = tables
|
|
537
|
+
|
|
538
|
+
if isExtension:
|
|
539
|
+
assert (
|
|
540
|
+
extTables is not None
|
|
541
|
+
), "Program or XML editing error. Extension subtables cannot contain extensions subtables"
|
|
542
|
+
tables, extTables, done = extTables, None, {}
|
|
543
|
+
|
|
544
|
+
# add Coverage table if it is sorted last.
|
|
545
|
+
sortCoverageLast = False
|
|
546
|
+
if hasattr(self, "sortCoverageLast"):
|
|
547
|
+
# Find coverage table
|
|
548
|
+
for i in range(numItems):
|
|
549
|
+
item = self.items[i]
|
|
550
|
+
if (
|
|
551
|
+
hasattr(item, "subWriter")
|
|
552
|
+
and getattr(item.subWriter, "name", None) == "Coverage"
|
|
553
|
+
):
|
|
554
|
+
sortCoverageLast = True
|
|
555
|
+
break
|
|
556
|
+
if id(item.subWriter) not in done:
|
|
557
|
+
item.subWriter._gatherTables(tables, extTables, done)
|
|
558
|
+
else:
|
|
559
|
+
# We're a new parent of item
|
|
560
|
+
pass
|
|
561
|
+
|
|
562
|
+
for i in iRange:
|
|
563
|
+
item = self.items[i]
|
|
564
|
+
if not hasattr(item, "subWriter"):
|
|
565
|
+
continue
|
|
566
|
+
|
|
567
|
+
if (
|
|
568
|
+
sortCoverageLast
|
|
569
|
+
and (i == 1)
|
|
570
|
+
and getattr(item.subWriter, "name", None) == "Coverage"
|
|
571
|
+
):
|
|
572
|
+
# we've already 'gathered' it above
|
|
573
|
+
continue
|
|
574
|
+
|
|
575
|
+
if id(item.subWriter) not in done:
|
|
576
|
+
item.subWriter._gatherTables(tables, extTables, done)
|
|
577
|
+
else:
|
|
578
|
+
# Item is already written out by other parent
|
|
579
|
+
pass
|
|
580
|
+
|
|
581
|
+
selfTables.append(self)
|
|
582
|
+
|
|
583
|
+
def _gatherGraphForHarfbuzz(self, tables, obj_list, done, objidx, virtual_edges):
|
|
584
|
+
real_links = []
|
|
585
|
+
virtual_links = []
|
|
586
|
+
item_idx = objidx
|
|
587
|
+
|
|
588
|
+
# Merge virtual_links from parent
|
|
589
|
+
for idx in virtual_edges:
|
|
590
|
+
virtual_links.append((0, 0, idx))
|
|
591
|
+
|
|
592
|
+
sortCoverageLast = False
|
|
593
|
+
coverage_idx = 0
|
|
594
|
+
if hasattr(self, "sortCoverageLast"):
|
|
595
|
+
# Find coverage table
|
|
596
|
+
for i, item in enumerate(self.items):
|
|
597
|
+
if getattr(item, "name", None) == "Coverage":
|
|
598
|
+
sortCoverageLast = True
|
|
599
|
+
if id(item) not in done:
|
|
600
|
+
coverage_idx = item_idx = item._gatherGraphForHarfbuzz(
|
|
601
|
+
tables, obj_list, done, item_idx, virtual_edges
|
|
602
|
+
)
|
|
603
|
+
else:
|
|
604
|
+
coverage_idx = done[id(item)]
|
|
605
|
+
virtual_edges.append(coverage_idx)
|
|
606
|
+
break
|
|
607
|
+
|
|
608
|
+
child_idx = 0
|
|
609
|
+
offset_pos = 0
|
|
610
|
+
for i, item in enumerate(self.items):
|
|
611
|
+
if hasattr(item, "subWriter"):
|
|
612
|
+
pos = offset_pos
|
|
613
|
+
elif hasattr(item, "getCountData"):
|
|
614
|
+
offset_pos += item.size
|
|
615
|
+
continue
|
|
616
|
+
else:
|
|
617
|
+
offset_pos = offset_pos + len(item)
|
|
618
|
+
continue
|
|
619
|
+
|
|
620
|
+
if id(item.subWriter) not in done:
|
|
621
|
+
child_idx = item_idx = item.subWriter._gatherGraphForHarfbuzz(
|
|
622
|
+
tables, obj_list, done, item_idx, virtual_edges
|
|
623
|
+
)
|
|
624
|
+
else:
|
|
625
|
+
child_idx = done[id(item.subWriter)]
|
|
626
|
+
|
|
627
|
+
real_edge = (pos, item.offsetSize, child_idx)
|
|
628
|
+
real_links.append(real_edge)
|
|
629
|
+
offset_pos += item.offsetSize
|
|
630
|
+
|
|
631
|
+
tables.append(self)
|
|
632
|
+
obj_list.append((real_links, virtual_links))
|
|
633
|
+
item_idx += 1
|
|
634
|
+
done[id(self)] = item_idx
|
|
635
|
+
if sortCoverageLast:
|
|
636
|
+
virtual_edges.pop()
|
|
637
|
+
|
|
638
|
+
return item_idx
|
|
639
|
+
|
|
640
|
+
def getAllDataUsingHarfbuzz(self, tableTag):
|
|
641
|
+
"""The Whole table is represented as a Graph.
|
|
642
|
+
Assemble graph data and call Harfbuzz repacker to pack the table.
|
|
643
|
+
Harfbuzz repacker is faster and retain as much sub-table sharing as possible, see also:
|
|
644
|
+
https://github.com/harfbuzz/harfbuzz/blob/main/docs/repacker.md
|
|
645
|
+
The input format for hb.repack() method is explained here:
|
|
646
|
+
https://github.com/harfbuzz/uharfbuzz/blob/main/src/uharfbuzz/_harfbuzz.pyx#L1149
|
|
647
|
+
"""
|
|
648
|
+
internedTables = {}
|
|
649
|
+
self._doneWriting(internedTables, shareExtension=True)
|
|
650
|
+
tables = []
|
|
651
|
+
obj_list = []
|
|
652
|
+
done = {}
|
|
653
|
+
objidx = 0
|
|
654
|
+
virtual_edges = []
|
|
655
|
+
self._gatherGraphForHarfbuzz(tables, obj_list, done, objidx, virtual_edges)
|
|
656
|
+
# Gather all data in two passes: the absolute positions of all
|
|
657
|
+
# subtable are needed before the actual data can be assembled.
|
|
658
|
+
pos = 0
|
|
659
|
+
for table in tables:
|
|
660
|
+
table.pos = pos
|
|
661
|
+
pos = pos + table.getDataLength()
|
|
662
|
+
|
|
663
|
+
data = []
|
|
664
|
+
for table in tables:
|
|
665
|
+
tableData = table.getDataForHarfbuzz()
|
|
666
|
+
data.append(tableData)
|
|
667
|
+
|
|
668
|
+
return hb.serialize_with_tag(str(tableTag), data, obj_list)
|
|
669
|
+
|
|
670
|
+
def getAllData(self, remove_duplicate=True):
|
|
671
|
+
"""Assemble all data, including all subtables."""
|
|
672
|
+
if remove_duplicate:
|
|
673
|
+
internedTables = {}
|
|
674
|
+
self._doneWriting(internedTables)
|
|
675
|
+
tables = []
|
|
676
|
+
extTables = []
|
|
677
|
+
done = {}
|
|
678
|
+
self._gatherTables(tables, extTables, done)
|
|
679
|
+
tables.reverse()
|
|
680
|
+
extTables.reverse()
|
|
681
|
+
# Gather all data in two passes: the absolute positions of all
|
|
682
|
+
# subtable are needed before the actual data can be assembled.
|
|
683
|
+
pos = 0
|
|
684
|
+
for table in tables:
|
|
685
|
+
table.pos = pos
|
|
686
|
+
pos = pos + table.getDataLength()
|
|
687
|
+
|
|
688
|
+
for table in extTables:
|
|
689
|
+
table.pos = pos
|
|
690
|
+
pos = pos + table.getDataLength()
|
|
691
|
+
|
|
692
|
+
data = []
|
|
693
|
+
for table in tables:
|
|
694
|
+
tableData = table.getData()
|
|
695
|
+
data.append(tableData)
|
|
696
|
+
|
|
697
|
+
for table in extTables:
|
|
698
|
+
tableData = table.getData()
|
|
699
|
+
data.append(tableData)
|
|
700
|
+
|
|
701
|
+
return bytesjoin(data)
|
|
702
|
+
|
|
703
|
+
# interface for gathering data, as used by table.compile()
|
|
704
|
+
|
|
705
|
+
def getSubWriter(self):
|
|
706
|
+
subwriter = self.__class__(self.localState, self.tableTag)
|
|
707
|
+
subwriter.parent = (
|
|
708
|
+
self # because some subtables have idential values, we discard
|
|
709
|
+
)
|
|
710
|
+
# the duplicates under the getAllData method. Hence some
|
|
711
|
+
# subtable writers can have more than one parent writer.
|
|
712
|
+
# But we just care about first one right now.
|
|
713
|
+
return subwriter
|
|
714
|
+
|
|
715
|
+
def writeValue(self, typecode, value):
|
|
716
|
+
self.items.append(struct.pack(f">{typecode}", value))
|
|
717
|
+
|
|
718
|
+
def writeArray(self, typecode, values):
|
|
719
|
+
a = array.array(typecode, values)
|
|
720
|
+
if sys.byteorder != "big":
|
|
721
|
+
a.byteswap()
|
|
722
|
+
self.items.append(a.tobytes())
|
|
723
|
+
|
|
724
|
+
def writeInt8(self, value):
|
|
725
|
+
assert -128 <= value < 128, value
|
|
726
|
+
self.items.append(struct.pack(">b", value))
|
|
727
|
+
|
|
728
|
+
def writeInt8Array(self, values):
|
|
729
|
+
self.writeArray("b", values)
|
|
730
|
+
|
|
731
|
+
def writeShort(self, value):
|
|
732
|
+
assert -32768 <= value < 32768, value
|
|
733
|
+
self.items.append(struct.pack(">h", value))
|
|
734
|
+
|
|
735
|
+
def writeShortArray(self, values):
|
|
736
|
+
self.writeArray("h", values)
|
|
737
|
+
|
|
738
|
+
def writeLong(self, value):
|
|
739
|
+
self.items.append(struct.pack(">i", value))
|
|
740
|
+
|
|
741
|
+
def writeLongArray(self, values):
|
|
742
|
+
self.writeArray("i", values)
|
|
743
|
+
|
|
744
|
+
def writeUInt8(self, value):
|
|
745
|
+
assert 0 <= value < 256, value
|
|
746
|
+
self.items.append(struct.pack(">B", value))
|
|
747
|
+
|
|
748
|
+
def writeUInt8Array(self, values):
|
|
749
|
+
self.writeArray("B", values)
|
|
750
|
+
|
|
751
|
+
def writeUShort(self, value):
|
|
752
|
+
assert 0 <= value < 0x10000, value
|
|
753
|
+
self.items.append(struct.pack(">H", value))
|
|
754
|
+
|
|
755
|
+
def writeUShortArray(self, values):
|
|
756
|
+
self.writeArray("H", values)
|
|
757
|
+
|
|
758
|
+
def writeULong(self, value):
|
|
759
|
+
self.items.append(struct.pack(">I", value))
|
|
760
|
+
|
|
761
|
+
def writeULongArray(self, values):
|
|
762
|
+
self.writeArray("I", values)
|
|
763
|
+
|
|
764
|
+
def writeUInt24(self, value):
|
|
765
|
+
assert 0 <= value < 0x1000000, value
|
|
766
|
+
b = struct.pack(">L", value)
|
|
767
|
+
self.items.append(b[1:])
|
|
768
|
+
|
|
769
|
+
def writeUInt24Array(self, values):
|
|
770
|
+
for value in values:
|
|
771
|
+
self.writeUInt24(value)
|
|
772
|
+
|
|
773
|
+
def writeTag(self, tag):
|
|
774
|
+
tag = Tag(tag).tobytes()
|
|
775
|
+
assert len(tag) == 4, tag
|
|
776
|
+
self.items.append(tag)
|
|
777
|
+
|
|
778
|
+
def writeSubTable(self, subWriter, offsetSize):
|
|
779
|
+
self.items.append(OffsetToWriter(subWriter, offsetSize))
|
|
780
|
+
|
|
781
|
+
def writeCountReference(self, table, name, size=2, value=None):
|
|
782
|
+
ref = CountReference(table, name, size=size, value=value)
|
|
783
|
+
self.items.append(ref)
|
|
784
|
+
return ref
|
|
785
|
+
|
|
786
|
+
def writeStruct(self, format, values):
|
|
787
|
+
data = struct.pack(*(format,) + values)
|
|
788
|
+
self.items.append(data)
|
|
789
|
+
|
|
790
|
+
def writeData(self, data):
|
|
791
|
+
self.items.append(data)
|
|
792
|
+
|
|
793
|
+
def getOverflowErrorRecord(self, item):
|
|
794
|
+
LookupListIndex = SubTableIndex = itemName = itemIndex = None
|
|
795
|
+
if self.name == "LookupList":
|
|
796
|
+
LookupListIndex = item.repeatIndex
|
|
797
|
+
elif self.name == "Lookup":
|
|
798
|
+
LookupListIndex = self.repeatIndex
|
|
799
|
+
SubTableIndex = item.repeatIndex
|
|
800
|
+
else:
|
|
801
|
+
itemName = getattr(item, "name", "<none>")
|
|
802
|
+
if hasattr(item, "repeatIndex"):
|
|
803
|
+
itemIndex = item.repeatIndex
|
|
804
|
+
if self.name == "SubTable":
|
|
805
|
+
LookupListIndex = self.parent.repeatIndex
|
|
806
|
+
SubTableIndex = self.repeatIndex
|
|
807
|
+
elif self.name == "ExtSubTable":
|
|
808
|
+
LookupListIndex = self.parent.parent.repeatIndex
|
|
809
|
+
SubTableIndex = self.parent.repeatIndex
|
|
810
|
+
else: # who knows how far below the SubTable level we are! Climb back up to the nearest subtable.
|
|
811
|
+
itemName = ".".join([self.name, itemName])
|
|
812
|
+
p1 = self.parent
|
|
813
|
+
while p1 and p1.name not in ["ExtSubTable", "SubTable"]:
|
|
814
|
+
itemName = ".".join([p1.name, itemName])
|
|
815
|
+
p1 = p1.parent
|
|
816
|
+
if p1:
|
|
817
|
+
if p1.name == "ExtSubTable":
|
|
818
|
+
LookupListIndex = p1.parent.parent.repeatIndex
|
|
819
|
+
SubTableIndex = p1.parent.repeatIndex
|
|
820
|
+
else:
|
|
821
|
+
LookupListIndex = p1.parent.repeatIndex
|
|
822
|
+
SubTableIndex = p1.repeatIndex
|
|
823
|
+
|
|
824
|
+
return OverflowErrorRecord(
|
|
825
|
+
(self.tableTag, LookupListIndex, SubTableIndex, itemName, itemIndex)
|
|
826
|
+
)
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
class CountReference(object):
|
|
830
|
+
"""A reference to a Count value, not a count of references."""
|
|
831
|
+
|
|
832
|
+
def __init__(self, table, name, size=None, value=None):
|
|
833
|
+
self.table = table
|
|
834
|
+
self.name = name
|
|
835
|
+
self.size = size
|
|
836
|
+
if value is not None:
|
|
837
|
+
self.setValue(value)
|
|
838
|
+
|
|
839
|
+
def setValue(self, value):
|
|
840
|
+
table = self.table
|
|
841
|
+
name = self.name
|
|
842
|
+
if table[name] is None:
|
|
843
|
+
table[name] = value
|
|
844
|
+
else:
|
|
845
|
+
assert table[name] == value, (name, table[name], value)
|
|
846
|
+
|
|
847
|
+
def getValue(self):
|
|
848
|
+
return self.table[self.name]
|
|
849
|
+
|
|
850
|
+
def getCountData(self):
|
|
851
|
+
v = self.table[self.name]
|
|
852
|
+
if v is None:
|
|
853
|
+
v = 0
|
|
854
|
+
return {1: packUInt8, 2: packUShort, 4: packULong}[self.size](v)
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
def packUInt8(value):
|
|
858
|
+
return struct.pack(">B", value)
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
def packUShort(value):
|
|
862
|
+
return struct.pack(">H", value)
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
def packULong(value):
|
|
866
|
+
assert 0 <= value < 0x100000000, value
|
|
867
|
+
return struct.pack(">I", value)
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
def packUInt24(value):
|
|
871
|
+
assert 0 <= value < 0x1000000, value
|
|
872
|
+
return struct.pack(">I", value)[1:]
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
class BaseTable(object):
|
|
876
|
+
"""Generic base class for all OpenType (sub)tables."""
|
|
877
|
+
|
|
878
|
+
def __getattr__(self, attr):
|
|
879
|
+
reader = self.__dict__.get("reader")
|
|
880
|
+
if reader:
|
|
881
|
+
del self.reader
|
|
882
|
+
font = self.font
|
|
883
|
+
del self.font
|
|
884
|
+
self.decompile(reader, font)
|
|
885
|
+
return getattr(self, attr)
|
|
886
|
+
|
|
887
|
+
raise AttributeError(attr)
|
|
888
|
+
|
|
889
|
+
def ensureDecompiled(self, recurse=False):
|
|
890
|
+
reader = self.__dict__.get("reader")
|
|
891
|
+
if reader:
|
|
892
|
+
del self.reader
|
|
893
|
+
font = self.font
|
|
894
|
+
del self.font
|
|
895
|
+
self.decompile(reader, font)
|
|
896
|
+
if recurse:
|
|
897
|
+
for subtable in self.iterSubTables():
|
|
898
|
+
subtable.value.ensureDecompiled(recurse)
|
|
899
|
+
|
|
900
|
+
def __getstate__(self):
|
|
901
|
+
# before copying/pickling 'lazy' objects, make a shallow copy of OTTableReader
|
|
902
|
+
# https://github.com/fonttools/fonttools/issues/2965
|
|
903
|
+
if "reader" in self.__dict__:
|
|
904
|
+
state = self.__dict__.copy()
|
|
905
|
+
state["reader"] = self.__dict__["reader"].copy()
|
|
906
|
+
return state
|
|
907
|
+
return self.__dict__
|
|
908
|
+
|
|
909
|
+
@classmethod
|
|
910
|
+
def getRecordSize(cls, reader):
|
|
911
|
+
totalSize = 0
|
|
912
|
+
for conv in cls.converters:
|
|
913
|
+
size = conv.getRecordSize(reader)
|
|
914
|
+
if size is NotImplemented:
|
|
915
|
+
return NotImplemented
|
|
916
|
+
countValue = 1
|
|
917
|
+
if conv.repeat:
|
|
918
|
+
if conv.repeat in reader:
|
|
919
|
+
countValue = reader[conv.repeat] + conv.aux
|
|
920
|
+
else:
|
|
921
|
+
return NotImplemented
|
|
922
|
+
totalSize += size * countValue
|
|
923
|
+
return totalSize
|
|
924
|
+
|
|
925
|
+
def getConverters(self):
|
|
926
|
+
return self.converters
|
|
927
|
+
|
|
928
|
+
def getConverterByName(self, name):
|
|
929
|
+
return self.convertersByName[name]
|
|
930
|
+
|
|
931
|
+
def populateDefaults(self, propagator=None):
|
|
932
|
+
for conv in self.getConverters():
|
|
933
|
+
if conv.repeat:
|
|
934
|
+
if not hasattr(self, conv.name):
|
|
935
|
+
setattr(self, conv.name, [])
|
|
936
|
+
countValue = len(getattr(self, conv.name)) - conv.aux
|
|
937
|
+
try:
|
|
938
|
+
count_conv = self.getConverterByName(conv.repeat)
|
|
939
|
+
setattr(self, conv.repeat, countValue)
|
|
940
|
+
except KeyError:
|
|
941
|
+
# conv.repeat is a propagated count
|
|
942
|
+
if propagator and conv.repeat in propagator:
|
|
943
|
+
propagator[conv.repeat].setValue(countValue)
|
|
944
|
+
else:
|
|
945
|
+
if conv.aux and not eval(conv.aux, None, self.__dict__):
|
|
946
|
+
continue
|
|
947
|
+
if hasattr(self, conv.name):
|
|
948
|
+
continue # Warn if it should NOT be present?!
|
|
949
|
+
if hasattr(conv, "writeNullOffset"):
|
|
950
|
+
setattr(self, conv.name, None) # Warn?
|
|
951
|
+
# elif not conv.isCount:
|
|
952
|
+
# # Warn?
|
|
953
|
+
# pass
|
|
954
|
+
if hasattr(conv, "DEFAULT"):
|
|
955
|
+
# OptionalValue converters (e.g. VarIndex)
|
|
956
|
+
setattr(self, conv.name, conv.DEFAULT)
|
|
957
|
+
|
|
958
|
+
def decompile(self, reader, font):
|
|
959
|
+
self.readFormat(reader)
|
|
960
|
+
table = {}
|
|
961
|
+
self.__rawTable = table # for debugging
|
|
962
|
+
for conv in self.getConverters():
|
|
963
|
+
if conv.name == "SubTable":
|
|
964
|
+
conv = conv.getConverter(reader.tableTag, table["LookupType"])
|
|
965
|
+
if conv.name == "ExtSubTable":
|
|
966
|
+
conv = conv.getConverter(reader.tableTag, table["ExtensionLookupType"])
|
|
967
|
+
if conv.name == "FeatureParams":
|
|
968
|
+
conv = conv.getConverter(reader["FeatureTag"])
|
|
969
|
+
if conv.name == "SubStruct":
|
|
970
|
+
conv = conv.getConverter(reader.tableTag, table["MorphType"])
|
|
971
|
+
try:
|
|
972
|
+
if conv.repeat:
|
|
973
|
+
if isinstance(conv.repeat, int):
|
|
974
|
+
countValue = conv.repeat
|
|
975
|
+
elif conv.repeat in table:
|
|
976
|
+
countValue = table[conv.repeat]
|
|
977
|
+
else:
|
|
978
|
+
# conv.repeat is a propagated count
|
|
979
|
+
countValue = reader[conv.repeat]
|
|
980
|
+
countValue += conv.aux
|
|
981
|
+
table[conv.name] = conv.readArray(reader, font, table, countValue)
|
|
982
|
+
else:
|
|
983
|
+
if conv.aux and not eval(conv.aux, None, table):
|
|
984
|
+
continue
|
|
985
|
+
table[conv.name] = conv.read(reader, font, table)
|
|
986
|
+
if conv.isPropagated:
|
|
987
|
+
reader[conv.name] = table[conv.name]
|
|
988
|
+
except Exception as e:
|
|
989
|
+
name = conv.name
|
|
990
|
+
e.args = e.args + (name,)
|
|
991
|
+
raise
|
|
992
|
+
|
|
993
|
+
if hasattr(self, "postRead"):
|
|
994
|
+
self.postRead(table, font)
|
|
995
|
+
else:
|
|
996
|
+
self.__dict__.update(table)
|
|
997
|
+
|
|
998
|
+
del self.__rawTable # succeeded, get rid of debugging info
|
|
999
|
+
|
|
1000
|
+
def compile(self, writer, font):
|
|
1001
|
+
self.ensureDecompiled()
|
|
1002
|
+
# TODO Following hack to be removed by rewriting how FormatSwitching tables
|
|
1003
|
+
# are handled.
|
|
1004
|
+
# https://github.com/fonttools/fonttools/pull/2238#issuecomment-805192631
|
|
1005
|
+
if hasattr(self, "preWrite"):
|
|
1006
|
+
deleteFormat = not hasattr(self, "Format")
|
|
1007
|
+
table = self.preWrite(font)
|
|
1008
|
+
deleteFormat = deleteFormat and hasattr(self, "Format")
|
|
1009
|
+
else:
|
|
1010
|
+
deleteFormat = False
|
|
1011
|
+
table = self.__dict__.copy()
|
|
1012
|
+
|
|
1013
|
+
# some count references may have been initialized in a custom preWrite; we set
|
|
1014
|
+
# these in the writer's state beforehand (instead of sequentially) so they will
|
|
1015
|
+
# be propagated to all nested subtables even if the count appears in the current
|
|
1016
|
+
# table only *after* the offset to the subtable that it is counting.
|
|
1017
|
+
for conv in self.getConverters():
|
|
1018
|
+
if conv.isCount and conv.isPropagated:
|
|
1019
|
+
value = table.get(conv.name)
|
|
1020
|
+
if isinstance(value, CountReference):
|
|
1021
|
+
writer[conv.name] = value
|
|
1022
|
+
|
|
1023
|
+
if hasattr(self, "sortCoverageLast"):
|
|
1024
|
+
writer.sortCoverageLast = 1
|
|
1025
|
+
|
|
1026
|
+
if hasattr(self, "DontShare"):
|
|
1027
|
+
writer.DontShare = True
|
|
1028
|
+
|
|
1029
|
+
if hasattr(self.__class__, "LookupType"):
|
|
1030
|
+
writer["LookupType"].setValue(self.__class__.LookupType)
|
|
1031
|
+
|
|
1032
|
+
self.writeFormat(writer)
|
|
1033
|
+
for conv in self.getConverters():
|
|
1034
|
+
value = table.get(
|
|
1035
|
+
conv.name
|
|
1036
|
+
) # TODO Handle defaults instead of defaulting to None!
|
|
1037
|
+
if conv.repeat:
|
|
1038
|
+
if value is None:
|
|
1039
|
+
value = []
|
|
1040
|
+
countValue = len(value) - conv.aux
|
|
1041
|
+
if isinstance(conv.repeat, int):
|
|
1042
|
+
assert len(value) == conv.repeat, "expected %d values, got %d" % (
|
|
1043
|
+
conv.repeat,
|
|
1044
|
+
len(value),
|
|
1045
|
+
)
|
|
1046
|
+
elif conv.repeat in table:
|
|
1047
|
+
CountReference(table, conv.repeat, value=countValue)
|
|
1048
|
+
else:
|
|
1049
|
+
# conv.repeat is a propagated count
|
|
1050
|
+
writer[conv.repeat].setValue(countValue)
|
|
1051
|
+
try:
|
|
1052
|
+
conv.writeArray(writer, font, table, value)
|
|
1053
|
+
except Exception as e:
|
|
1054
|
+
e.args = e.args + (conv.name + "[]",)
|
|
1055
|
+
raise
|
|
1056
|
+
elif conv.isCount:
|
|
1057
|
+
# Special-case Count values.
|
|
1058
|
+
# Assumption: a Count field will *always* precede
|
|
1059
|
+
# the actual array(s).
|
|
1060
|
+
# We need a default value, as it may be set later by a nested
|
|
1061
|
+
# table. We will later store it here.
|
|
1062
|
+
# We add a reference: by the time the data is assembled
|
|
1063
|
+
# the Count value will be filled in.
|
|
1064
|
+
# We ignore the current count value since it will be recomputed,
|
|
1065
|
+
# unless it's a CountReference that was already initialized in a custom preWrite.
|
|
1066
|
+
if isinstance(value, CountReference):
|
|
1067
|
+
ref = value
|
|
1068
|
+
ref.size = conv.staticSize
|
|
1069
|
+
writer.writeData(ref)
|
|
1070
|
+
table[conv.name] = ref.getValue()
|
|
1071
|
+
else:
|
|
1072
|
+
ref = writer.writeCountReference(table, conv.name, conv.staticSize)
|
|
1073
|
+
table[conv.name] = None
|
|
1074
|
+
if conv.isPropagated:
|
|
1075
|
+
writer[conv.name] = ref
|
|
1076
|
+
elif conv.isLookupType:
|
|
1077
|
+
# We make sure that subtables have the same lookup type,
|
|
1078
|
+
# and that the type is the same as the one set on the
|
|
1079
|
+
# Lookup object, if any is set.
|
|
1080
|
+
if conv.name not in table:
|
|
1081
|
+
table[conv.name] = None
|
|
1082
|
+
ref = writer.writeCountReference(
|
|
1083
|
+
table, conv.name, conv.staticSize, table[conv.name]
|
|
1084
|
+
)
|
|
1085
|
+
writer["LookupType"] = ref
|
|
1086
|
+
else:
|
|
1087
|
+
if conv.aux and not eval(conv.aux, None, table):
|
|
1088
|
+
continue
|
|
1089
|
+
try:
|
|
1090
|
+
conv.write(writer, font, table, value)
|
|
1091
|
+
except Exception as e:
|
|
1092
|
+
name = value.__class__.__name__ if value is not None else conv.name
|
|
1093
|
+
e.args = e.args + (name,)
|
|
1094
|
+
raise
|
|
1095
|
+
if conv.isPropagated:
|
|
1096
|
+
writer[conv.name] = value
|
|
1097
|
+
|
|
1098
|
+
if deleteFormat:
|
|
1099
|
+
del self.Format
|
|
1100
|
+
|
|
1101
|
+
def readFormat(self, reader):
|
|
1102
|
+
pass
|
|
1103
|
+
|
|
1104
|
+
def writeFormat(self, writer):
|
|
1105
|
+
pass
|
|
1106
|
+
|
|
1107
|
+
def toXML(self, xmlWriter, font, attrs=None, name=None):
|
|
1108
|
+
tableName = name if name else self.__class__.__name__
|
|
1109
|
+
if attrs is None:
|
|
1110
|
+
attrs = []
|
|
1111
|
+
if hasattr(self, "Format"):
|
|
1112
|
+
attrs = attrs + [("Format", self.Format)]
|
|
1113
|
+
xmlWriter.begintag(tableName, attrs)
|
|
1114
|
+
xmlWriter.newline()
|
|
1115
|
+
self.toXML2(xmlWriter, font)
|
|
1116
|
+
xmlWriter.endtag(tableName)
|
|
1117
|
+
xmlWriter.newline()
|
|
1118
|
+
|
|
1119
|
+
def toXML2(self, xmlWriter, font):
|
|
1120
|
+
# Simpler variant of toXML, *only* for the top level tables (like GPOS, GSUB).
|
|
1121
|
+
# This is because in TTX our parent writes our main tag, and in otBase.py we
|
|
1122
|
+
# do it ourselves. I think I'm getting schizophrenic...
|
|
1123
|
+
for conv in self.getConverters():
|
|
1124
|
+
if conv.repeat:
|
|
1125
|
+
value = getattr(self, conv.name, [])
|
|
1126
|
+
for i, item in enumerate(value):
|
|
1127
|
+
conv.xmlWrite(xmlWriter, font, item, conv.name, [("index", i)])
|
|
1128
|
+
else:
|
|
1129
|
+
if conv.aux and not eval(conv.aux, None, vars(self)):
|
|
1130
|
+
continue
|
|
1131
|
+
value = getattr(
|
|
1132
|
+
self, conv.name, None
|
|
1133
|
+
) # TODO Handle defaults instead of defaulting to None!
|
|
1134
|
+
conv.xmlWrite(xmlWriter, font, value, conv.name, [])
|
|
1135
|
+
|
|
1136
|
+
def fromXML(self, name, attrs, content, font):
|
|
1137
|
+
try:
|
|
1138
|
+
conv = self.getConverterByName(name)
|
|
1139
|
+
except KeyError:
|
|
1140
|
+
raise # XXX on KeyError, raise nice error
|
|
1141
|
+
value = conv.xmlRead(attrs, content, font)
|
|
1142
|
+
# Some manually-written tables have a conv.repeat of ""
|
|
1143
|
+
# to represent lists. Hence comparing to None here to
|
|
1144
|
+
# allow those lists to be read correctly from XML.
|
|
1145
|
+
if conv.repeat is not None:
|
|
1146
|
+
seq = getattr(self, conv.name, None)
|
|
1147
|
+
if seq is None:
|
|
1148
|
+
seq = []
|
|
1149
|
+
setattr(self, conv.name, seq)
|
|
1150
|
+
seq.append(value)
|
|
1151
|
+
else:
|
|
1152
|
+
setattr(self, conv.name, value)
|
|
1153
|
+
|
|
1154
|
+
def __ne__(self, other):
|
|
1155
|
+
result = self.__eq__(other)
|
|
1156
|
+
return result if result is NotImplemented else not result
|
|
1157
|
+
|
|
1158
|
+
def __eq__(self, other):
|
|
1159
|
+
if type(self) != type(other):
|
|
1160
|
+
return NotImplemented
|
|
1161
|
+
|
|
1162
|
+
self.ensureDecompiled()
|
|
1163
|
+
other.ensureDecompiled()
|
|
1164
|
+
|
|
1165
|
+
return self.__dict__ == other.__dict__
|
|
1166
|
+
|
|
1167
|
+
class SubTableEntry(NamedTuple):
|
|
1168
|
+
"""See BaseTable.iterSubTables()"""
|
|
1169
|
+
|
|
1170
|
+
name: str
|
|
1171
|
+
value: "BaseTable"
|
|
1172
|
+
index: Optional[int] = None # index into given array, None for single values
|
|
1173
|
+
|
|
1174
|
+
def iterSubTables(self) -> Iterator[SubTableEntry]:
|
|
1175
|
+
"""Yield (name, value, index) namedtuples for all subtables of current table.
|
|
1176
|
+
|
|
1177
|
+
A sub-table is an instance of BaseTable (or subclass thereof) that is a child
|
|
1178
|
+
of self, the current parent table.
|
|
1179
|
+
The tuples also contain the attribute name (str) of the of parent table to get
|
|
1180
|
+
a subtable, and optionally, for lists of subtables (i.e. attributes associated
|
|
1181
|
+
with a converter that has a 'repeat'), an index into the list containing the
|
|
1182
|
+
given subtable value.
|
|
1183
|
+
This method can be useful to traverse trees of otTables.
|
|
1184
|
+
"""
|
|
1185
|
+
for conv in self.getConverters():
|
|
1186
|
+
name = conv.name
|
|
1187
|
+
value = getattr(self, name, None)
|
|
1188
|
+
if value is None:
|
|
1189
|
+
continue
|
|
1190
|
+
if isinstance(value, BaseTable):
|
|
1191
|
+
yield self.SubTableEntry(name, value)
|
|
1192
|
+
elif isinstance(value, list):
|
|
1193
|
+
yield from (
|
|
1194
|
+
self.SubTableEntry(name, v, index=i)
|
|
1195
|
+
for i, v in enumerate(value)
|
|
1196
|
+
if isinstance(v, BaseTable)
|
|
1197
|
+
)
|
|
1198
|
+
|
|
1199
|
+
# instance (not @class)method for consistency with FormatSwitchingBaseTable
|
|
1200
|
+
def getVariableAttrs(self):
|
|
1201
|
+
return getVariableAttrs(self.__class__)
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
class FormatSwitchingBaseTable(BaseTable):
|
|
1205
|
+
"""Minor specialization of BaseTable, for tables that have multiple
|
|
1206
|
+
formats, eg. CoverageFormat1 vs. CoverageFormat2."""
|
|
1207
|
+
|
|
1208
|
+
@classmethod
|
|
1209
|
+
def getRecordSize(cls, reader):
|
|
1210
|
+
return NotImplemented
|
|
1211
|
+
|
|
1212
|
+
def getConverters(self):
|
|
1213
|
+
try:
|
|
1214
|
+
fmt = self.Format
|
|
1215
|
+
except AttributeError:
|
|
1216
|
+
# some FormatSwitchingBaseTables (e.g. Coverage) no longer have 'Format'
|
|
1217
|
+
# attribute after fully decompiled, only gain one in preWrite before being
|
|
1218
|
+
# recompiled. In the decompiled state, these hand-coded classes defined in
|
|
1219
|
+
# otTables.py lose their format-specific nature and gain more high-level
|
|
1220
|
+
# attributes that are not tied to converters.
|
|
1221
|
+
return []
|
|
1222
|
+
return self.converters.get(self.Format, [])
|
|
1223
|
+
|
|
1224
|
+
def getConverterByName(self, name):
|
|
1225
|
+
return self.convertersByName[self.Format][name]
|
|
1226
|
+
|
|
1227
|
+
def readFormat(self, reader):
|
|
1228
|
+
self.Format = reader.readUShort()
|
|
1229
|
+
|
|
1230
|
+
def writeFormat(self, writer):
|
|
1231
|
+
writer.writeUShort(self.Format)
|
|
1232
|
+
|
|
1233
|
+
def toXML(self, xmlWriter, font, attrs=None, name=None):
|
|
1234
|
+
BaseTable.toXML(self, xmlWriter, font, attrs, name)
|
|
1235
|
+
|
|
1236
|
+
def getVariableAttrs(self):
|
|
1237
|
+
return getVariableAttrs(self.__class__, self.Format)
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
class UInt8FormatSwitchingBaseTable(FormatSwitchingBaseTable):
|
|
1241
|
+
def readFormat(self, reader):
|
|
1242
|
+
self.Format = reader.readUInt8()
|
|
1243
|
+
|
|
1244
|
+
def writeFormat(self, writer):
|
|
1245
|
+
writer.writeUInt8(self.Format)
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
formatSwitchingBaseTables = {
|
|
1249
|
+
"uint16": FormatSwitchingBaseTable,
|
|
1250
|
+
"uint8": UInt8FormatSwitchingBaseTable,
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
def getFormatSwitchingBaseTableClass(formatType):
|
|
1255
|
+
try:
|
|
1256
|
+
return formatSwitchingBaseTables[formatType]
|
|
1257
|
+
except KeyError:
|
|
1258
|
+
raise TypeError(f"Unsupported format type: {formatType!r}")
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
# memoize since these are parsed from otData.py, thus stay constant
|
|
1262
|
+
@lru_cache()
|
|
1263
|
+
def getVariableAttrs(cls: BaseTable, fmt: Optional[int] = None) -> Tuple[str]:
|
|
1264
|
+
"""Return sequence of variable table field names (can be empty).
|
|
1265
|
+
|
|
1266
|
+
Attributes are deemed "variable" when their otData.py's description contain
|
|
1267
|
+
'VarIndexBase + {offset}', e.g. COLRv1 PaintVar* tables.
|
|
1268
|
+
"""
|
|
1269
|
+
if not issubclass(cls, BaseTable):
|
|
1270
|
+
raise TypeError(cls)
|
|
1271
|
+
if issubclass(cls, FormatSwitchingBaseTable):
|
|
1272
|
+
if fmt is None:
|
|
1273
|
+
raise TypeError(f"'fmt' is required for format-switching {cls.__name__}")
|
|
1274
|
+
converters = cls.convertersByName[fmt]
|
|
1275
|
+
else:
|
|
1276
|
+
converters = cls.convertersByName
|
|
1277
|
+
# assume if no 'VarIndexBase' field is present, table has no variable fields
|
|
1278
|
+
if "VarIndexBase" not in converters:
|
|
1279
|
+
return ()
|
|
1280
|
+
varAttrs = {}
|
|
1281
|
+
for name, conv in converters.items():
|
|
1282
|
+
offset = conv.getVarIndexOffset()
|
|
1283
|
+
if offset is not None:
|
|
1284
|
+
varAttrs[name] = offset
|
|
1285
|
+
return tuple(sorted(varAttrs, key=varAttrs.__getitem__))
|
|
1286
|
+
|
|
1287
|
+
|
|
1288
|
+
#
|
|
1289
|
+
# Support for ValueRecords
|
|
1290
|
+
#
|
|
1291
|
+
# This data type is so different from all other OpenType data types that
|
|
1292
|
+
# it requires quite a bit of code for itself. It even has special support
|
|
1293
|
+
# in OTTableReader and OTTableWriter...
|
|
1294
|
+
#
|
|
1295
|
+
|
|
1296
|
+
valueRecordFormat = [
|
|
1297
|
+
# Mask Name isDevice signed
|
|
1298
|
+
(0x0001, "XPlacement", 0, 1),
|
|
1299
|
+
(0x0002, "YPlacement", 0, 1),
|
|
1300
|
+
(0x0004, "XAdvance", 0, 1),
|
|
1301
|
+
(0x0008, "YAdvance", 0, 1),
|
|
1302
|
+
(0x0010, "XPlaDevice", 1, 0),
|
|
1303
|
+
(0x0020, "YPlaDevice", 1, 0),
|
|
1304
|
+
(0x0040, "XAdvDevice", 1, 0),
|
|
1305
|
+
(0x0080, "YAdvDevice", 1, 0),
|
|
1306
|
+
# reserved:
|
|
1307
|
+
(0x0100, "Reserved1", 0, 0),
|
|
1308
|
+
(0x0200, "Reserved2", 0, 0),
|
|
1309
|
+
(0x0400, "Reserved3", 0, 0),
|
|
1310
|
+
(0x0800, "Reserved4", 0, 0),
|
|
1311
|
+
(0x1000, "Reserved5", 0, 0),
|
|
1312
|
+
(0x2000, "Reserved6", 0, 0),
|
|
1313
|
+
(0x4000, "Reserved7", 0, 0),
|
|
1314
|
+
(0x8000, "Reserved8", 0, 0),
|
|
1315
|
+
]
|
|
1316
|
+
|
|
1317
|
+
|
|
1318
|
+
def _buildDict():
|
|
1319
|
+
d = {}
|
|
1320
|
+
for mask, name, isDevice, signed in valueRecordFormat:
|
|
1321
|
+
d[name] = mask, isDevice, signed
|
|
1322
|
+
return d
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
valueRecordFormatDict = _buildDict()
|
|
1326
|
+
|
|
1327
|
+
|
|
1328
|
+
class ValueRecordFactory(object):
|
|
1329
|
+
"""Given a format code, this object convert ValueRecords."""
|
|
1330
|
+
|
|
1331
|
+
def __init__(self, valueFormat):
|
|
1332
|
+
format = []
|
|
1333
|
+
for mask, name, isDevice, signed in valueRecordFormat:
|
|
1334
|
+
if valueFormat & mask:
|
|
1335
|
+
format.append((name, isDevice, signed))
|
|
1336
|
+
self.format = format
|
|
1337
|
+
|
|
1338
|
+
def __len__(self):
|
|
1339
|
+
return len(self.format)
|
|
1340
|
+
|
|
1341
|
+
def readValueRecord(self, reader, font):
|
|
1342
|
+
format = self.format
|
|
1343
|
+
if not format:
|
|
1344
|
+
return None
|
|
1345
|
+
valueRecord = ValueRecord()
|
|
1346
|
+
for name, isDevice, signed in format:
|
|
1347
|
+
if signed:
|
|
1348
|
+
value = reader.readShort()
|
|
1349
|
+
else:
|
|
1350
|
+
value = reader.readUShort()
|
|
1351
|
+
if isDevice:
|
|
1352
|
+
if value:
|
|
1353
|
+
from . import otTables
|
|
1354
|
+
|
|
1355
|
+
subReader = reader.getSubReader(value)
|
|
1356
|
+
value = getattr(otTables, name)()
|
|
1357
|
+
value.decompile(subReader, font)
|
|
1358
|
+
else:
|
|
1359
|
+
value = None
|
|
1360
|
+
setattr(valueRecord, name, value)
|
|
1361
|
+
return valueRecord
|
|
1362
|
+
|
|
1363
|
+
def writeValueRecord(self, writer, font, valueRecord):
|
|
1364
|
+
for name, isDevice, signed in self.format:
|
|
1365
|
+
value = getattr(valueRecord, name, 0)
|
|
1366
|
+
if isDevice:
|
|
1367
|
+
if value:
|
|
1368
|
+
subWriter = writer.getSubWriter()
|
|
1369
|
+
writer.writeSubTable(subWriter, offsetSize=2)
|
|
1370
|
+
value.compile(subWriter, font)
|
|
1371
|
+
else:
|
|
1372
|
+
writer.writeUShort(0)
|
|
1373
|
+
elif signed:
|
|
1374
|
+
writer.writeShort(value)
|
|
1375
|
+
else:
|
|
1376
|
+
writer.writeUShort(value)
|
|
1377
|
+
|
|
1378
|
+
|
|
1379
|
+
class ValueRecord(object):
|
|
1380
|
+
# see ValueRecordFactory
|
|
1381
|
+
|
|
1382
|
+
def __init__(self, valueFormat=None, src=None):
|
|
1383
|
+
if valueFormat is not None:
|
|
1384
|
+
for mask, name, isDevice, signed in valueRecordFormat:
|
|
1385
|
+
if valueFormat & mask:
|
|
1386
|
+
setattr(self, name, None if isDevice else 0)
|
|
1387
|
+
if src is not None:
|
|
1388
|
+
for key, val in src.__dict__.items():
|
|
1389
|
+
if not hasattr(self, key):
|
|
1390
|
+
continue
|
|
1391
|
+
setattr(self, key, val)
|
|
1392
|
+
elif src is not None:
|
|
1393
|
+
self.__dict__ = src.__dict__.copy()
|
|
1394
|
+
|
|
1395
|
+
def getFormat(self):
|
|
1396
|
+
format = 0
|
|
1397
|
+
for name in self.__dict__.keys():
|
|
1398
|
+
format = format | valueRecordFormatDict[name][0]
|
|
1399
|
+
return format
|
|
1400
|
+
|
|
1401
|
+
def getEffectiveFormat(self):
|
|
1402
|
+
format = 0
|
|
1403
|
+
for name, value in self.__dict__.items():
|
|
1404
|
+
if value:
|
|
1405
|
+
format = format | valueRecordFormatDict[name][0]
|
|
1406
|
+
return format
|
|
1407
|
+
|
|
1408
|
+
def toXML(self, xmlWriter, font, valueName, attrs=None):
|
|
1409
|
+
if attrs is None:
|
|
1410
|
+
simpleItems = []
|
|
1411
|
+
else:
|
|
1412
|
+
simpleItems = list(attrs)
|
|
1413
|
+
for mask, name, isDevice, format in valueRecordFormat[:4]: # "simple" values
|
|
1414
|
+
if hasattr(self, name):
|
|
1415
|
+
simpleItems.append((name, getattr(self, name)))
|
|
1416
|
+
deviceItems = []
|
|
1417
|
+
for mask, name, isDevice, format in valueRecordFormat[4:8]: # device records
|
|
1418
|
+
if hasattr(self, name):
|
|
1419
|
+
device = getattr(self, name)
|
|
1420
|
+
if device is not None:
|
|
1421
|
+
deviceItems.append((name, device))
|
|
1422
|
+
if deviceItems:
|
|
1423
|
+
xmlWriter.begintag(valueName, simpleItems)
|
|
1424
|
+
xmlWriter.newline()
|
|
1425
|
+
for name, deviceRecord in deviceItems:
|
|
1426
|
+
if deviceRecord is not None:
|
|
1427
|
+
deviceRecord.toXML(xmlWriter, font, name=name)
|
|
1428
|
+
xmlWriter.endtag(valueName)
|
|
1429
|
+
xmlWriter.newline()
|
|
1430
|
+
else:
|
|
1431
|
+
xmlWriter.simpletag(valueName, simpleItems)
|
|
1432
|
+
xmlWriter.newline()
|
|
1433
|
+
|
|
1434
|
+
def fromXML(self, name, attrs, content, font):
|
|
1435
|
+
from . import otTables
|
|
1436
|
+
|
|
1437
|
+
for k, v in attrs.items():
|
|
1438
|
+
setattr(self, k, int(v))
|
|
1439
|
+
for element in content:
|
|
1440
|
+
if not isinstance(element, tuple):
|
|
1441
|
+
continue
|
|
1442
|
+
name, attrs, content = element
|
|
1443
|
+
value = getattr(otTables, name)()
|
|
1444
|
+
for elem2 in content:
|
|
1445
|
+
if not isinstance(elem2, tuple):
|
|
1446
|
+
continue
|
|
1447
|
+
name2, attrs2, content2 = elem2
|
|
1448
|
+
value.fromXML(name2, attrs2, content2, font)
|
|
1449
|
+
setattr(self, name, value)
|
|
1450
|
+
|
|
1451
|
+
def __ne__(self, other):
|
|
1452
|
+
result = self.__eq__(other)
|
|
1453
|
+
return result if result is NotImplemented else not result
|
|
1454
|
+
|
|
1455
|
+
def __eq__(self, other):
|
|
1456
|
+
if type(self) != type(other):
|
|
1457
|
+
return NotImplemented
|
|
1458
|
+
return self.__dict__ == other.__dict__
|