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,11 @@
|
|
|
1
|
+
class OpenTypeLibError(Exception):
|
|
2
|
+
def __init__(self, message, location):
|
|
3
|
+
Exception.__init__(self, message)
|
|
4
|
+
self.location = location
|
|
5
|
+
|
|
6
|
+
def __str__(self):
|
|
7
|
+
message = Exception.__str__(self)
|
|
8
|
+
if self.location:
|
|
9
|
+
return f"{self.location}: {message}"
|
|
10
|
+
else:
|
|
11
|
+
return message
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
__all__ = ["maxCtxFont"]
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def maxCtxFont(font):
|
|
5
|
+
"""Calculate the usMaxContext value for an entire font."""
|
|
6
|
+
|
|
7
|
+
maxCtx = 0
|
|
8
|
+
for tag in ("GSUB", "GPOS"):
|
|
9
|
+
if tag not in font:
|
|
10
|
+
continue
|
|
11
|
+
table = font[tag].table
|
|
12
|
+
if not table.LookupList:
|
|
13
|
+
continue
|
|
14
|
+
for lookup in table.LookupList.Lookup:
|
|
15
|
+
for st in lookup.SubTable:
|
|
16
|
+
maxCtx = maxCtxSubtable(maxCtx, tag, lookup.LookupType, st)
|
|
17
|
+
return maxCtx
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def maxCtxSubtable(maxCtx, tag, lookupType, st):
|
|
21
|
+
"""Calculate usMaxContext based on a single lookup table (and an existing
|
|
22
|
+
max value).
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
# single positioning, single / multiple substitution
|
|
26
|
+
if (tag == "GPOS" and lookupType == 1) or (
|
|
27
|
+
tag == "GSUB" and lookupType in (1, 2, 3)
|
|
28
|
+
):
|
|
29
|
+
maxCtx = max(maxCtx, 1)
|
|
30
|
+
|
|
31
|
+
# pair positioning
|
|
32
|
+
elif tag == "GPOS" and lookupType == 2:
|
|
33
|
+
maxCtx = max(maxCtx, 2)
|
|
34
|
+
|
|
35
|
+
# ligatures
|
|
36
|
+
elif tag == "GSUB" and lookupType == 4:
|
|
37
|
+
for ligatures in st.ligatures.values():
|
|
38
|
+
for ligature in ligatures:
|
|
39
|
+
maxCtx = max(maxCtx, ligature.CompCount)
|
|
40
|
+
|
|
41
|
+
# context
|
|
42
|
+
elif (tag == "GPOS" and lookupType == 7) or (tag == "GSUB" and lookupType == 5):
|
|
43
|
+
maxCtx = maxCtxContextualSubtable(maxCtx, st, "Pos" if tag == "GPOS" else "Sub")
|
|
44
|
+
|
|
45
|
+
# chained context
|
|
46
|
+
elif (tag == "GPOS" and lookupType == 8) or (tag == "GSUB" and lookupType == 6):
|
|
47
|
+
maxCtx = maxCtxContextualSubtable(
|
|
48
|
+
maxCtx, st, "Pos" if tag == "GPOS" else "Sub", "Chain"
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# extensions
|
|
52
|
+
elif (tag == "GPOS" and lookupType == 9) or (tag == "GSUB" and lookupType == 7):
|
|
53
|
+
maxCtx = maxCtxSubtable(maxCtx, tag, st.ExtensionLookupType, st.ExtSubTable)
|
|
54
|
+
|
|
55
|
+
# reverse-chained context
|
|
56
|
+
elif tag == "GSUB" and lookupType == 8:
|
|
57
|
+
maxCtx = maxCtxContextualRule(maxCtx, st, "Reverse")
|
|
58
|
+
|
|
59
|
+
return maxCtx
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def maxCtxContextualSubtable(maxCtx, st, ruleType, chain=""):
|
|
63
|
+
"""Calculate usMaxContext based on a contextual feature subtable."""
|
|
64
|
+
|
|
65
|
+
if st.Format == 1:
|
|
66
|
+
for ruleset in getattr(st, "%s%sRuleSet" % (chain, ruleType)):
|
|
67
|
+
if ruleset is None:
|
|
68
|
+
continue
|
|
69
|
+
for rule in getattr(ruleset, "%s%sRule" % (chain, ruleType)):
|
|
70
|
+
if rule is None:
|
|
71
|
+
continue
|
|
72
|
+
maxCtx = maxCtxContextualRule(maxCtx, rule, chain)
|
|
73
|
+
|
|
74
|
+
elif st.Format == 2:
|
|
75
|
+
for ruleset in getattr(st, "%s%sClassSet" % (chain, ruleType)):
|
|
76
|
+
if ruleset is None:
|
|
77
|
+
continue
|
|
78
|
+
for rule in getattr(ruleset, "%s%sClassRule" % (chain, ruleType)):
|
|
79
|
+
if rule is None:
|
|
80
|
+
continue
|
|
81
|
+
maxCtx = maxCtxContextualRule(maxCtx, rule, chain)
|
|
82
|
+
|
|
83
|
+
elif st.Format == 3:
|
|
84
|
+
maxCtx = maxCtxContextualRule(maxCtx, st, chain)
|
|
85
|
+
|
|
86
|
+
return maxCtx
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def maxCtxContextualRule(maxCtx, st, chain):
|
|
90
|
+
"""Calculate usMaxContext based on a contextual feature rule."""
|
|
91
|
+
|
|
92
|
+
if not chain:
|
|
93
|
+
return max(maxCtx, st.GlyphCount)
|
|
94
|
+
elif chain == "Reverse":
|
|
95
|
+
return max(maxCtx, 1 + st.LookAheadGlyphCount)
|
|
96
|
+
return max(maxCtx, st.InputGlyphCount + st.LookAheadGlyphCount)
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from argparse import RawTextHelpFormatter
|
|
2
|
+
from fontTools.otlLib.optimize.gpos import COMPRESSION_LEVEL, compact
|
|
3
|
+
from fontTools.ttLib import TTFont
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def main(args=None):
|
|
7
|
+
"""Optimize the layout tables of an existing font"""
|
|
8
|
+
from argparse import ArgumentParser
|
|
9
|
+
|
|
10
|
+
from fontTools import configLogger
|
|
11
|
+
|
|
12
|
+
parser = ArgumentParser(
|
|
13
|
+
prog="otlLib.optimize",
|
|
14
|
+
description=main.__doc__,
|
|
15
|
+
formatter_class=RawTextHelpFormatter,
|
|
16
|
+
)
|
|
17
|
+
parser.add_argument("font")
|
|
18
|
+
parser.add_argument(
|
|
19
|
+
"-o", metavar="OUTPUTFILE", dest="outfile", default=None, help="output file"
|
|
20
|
+
)
|
|
21
|
+
parser.add_argument(
|
|
22
|
+
"--gpos-compression-level",
|
|
23
|
+
help=COMPRESSION_LEVEL.help,
|
|
24
|
+
default=COMPRESSION_LEVEL.default,
|
|
25
|
+
choices=list(range(10)),
|
|
26
|
+
type=int,
|
|
27
|
+
)
|
|
28
|
+
logging_group = parser.add_mutually_exclusive_group(required=False)
|
|
29
|
+
logging_group.add_argument(
|
|
30
|
+
"-v", "--verbose", action="store_true", help="Run more verbosely."
|
|
31
|
+
)
|
|
32
|
+
logging_group.add_argument(
|
|
33
|
+
"-q", "--quiet", action="store_true", help="Turn verbosity off."
|
|
34
|
+
)
|
|
35
|
+
options = parser.parse_args(args)
|
|
36
|
+
|
|
37
|
+
configLogger(
|
|
38
|
+
level=("DEBUG" if options.verbose else "ERROR" if options.quiet else "INFO")
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
font = TTFont(options.font)
|
|
42
|
+
compact(font, options.gpos_compression_level)
|
|
43
|
+
font.save(options.outfile or options.font)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
if __name__ == "__main__":
|
|
47
|
+
import sys
|
|
48
|
+
|
|
49
|
+
if len(sys.argv) > 1:
|
|
50
|
+
sys.exit(main())
|
|
51
|
+
import doctest
|
|
52
|
+
|
|
53
|
+
sys.exit(doctest.testmod().failed)
|
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
from collections import defaultdict, namedtuple
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from functools import cached_property, reduce
|
|
6
|
+
from itertools import chain
|
|
7
|
+
from math import log2
|
|
8
|
+
from typing import DefaultDict, Dict, Iterable, List, Sequence, Tuple
|
|
9
|
+
|
|
10
|
+
from fontTools.config import OPTIONS
|
|
11
|
+
from fontTools.misc.intTools import bit_count, bit_indices
|
|
12
|
+
from fontTools.ttLib import TTFont
|
|
13
|
+
from fontTools.ttLib.tables import otBase, otTables
|
|
14
|
+
|
|
15
|
+
log = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
COMPRESSION_LEVEL = OPTIONS[f"{__name__}:COMPRESSION_LEVEL"]
|
|
18
|
+
|
|
19
|
+
# Kept because ufo2ft depends on it, to be removed once ufo2ft uses the config instead
|
|
20
|
+
# https://github.com/fonttools/fonttools/issues/2592
|
|
21
|
+
GPOS_COMPACT_MODE_ENV_KEY = "FONTTOOLS_GPOS_COMPACT_MODE"
|
|
22
|
+
GPOS_COMPACT_MODE_DEFAULT = str(COMPRESSION_LEVEL.default)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _compression_level_from_env() -> int:
|
|
26
|
+
env_level = GPOS_COMPACT_MODE_DEFAULT
|
|
27
|
+
if GPOS_COMPACT_MODE_ENV_KEY in os.environ:
|
|
28
|
+
import warnings
|
|
29
|
+
|
|
30
|
+
warnings.warn(
|
|
31
|
+
f"'{GPOS_COMPACT_MODE_ENV_KEY}' environment variable is deprecated. "
|
|
32
|
+
"Please set the 'fontTools.otlLib.optimize.gpos:COMPRESSION_LEVEL' option "
|
|
33
|
+
"in TTFont.cfg.",
|
|
34
|
+
DeprecationWarning,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
env_level = os.environ[GPOS_COMPACT_MODE_ENV_KEY]
|
|
38
|
+
if len(env_level) == 1 and env_level in "0123456789":
|
|
39
|
+
return int(env_level)
|
|
40
|
+
raise ValueError(f"Bad {GPOS_COMPACT_MODE_ENV_KEY}={env_level}")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def compact(font: TTFont, level: int) -> TTFont:
|
|
44
|
+
# Ideal plan:
|
|
45
|
+
# 1. Find lookups of Lookup Type 2: Pair Adjustment Positioning Subtable
|
|
46
|
+
# https://docs.microsoft.com/en-us/typography/opentype/spec/gpos#lookup-type-2-pair-adjustment-positioning-subtable
|
|
47
|
+
# 2. Extract glyph-glyph kerning and class-kerning from all present subtables
|
|
48
|
+
# 3. Regroup into different subtable arrangements
|
|
49
|
+
# 4. Put back into the lookup
|
|
50
|
+
#
|
|
51
|
+
# Actual implementation:
|
|
52
|
+
# 2. Only class kerning is optimized currently
|
|
53
|
+
# 3. If the input kerning is already in several subtables, the subtables
|
|
54
|
+
# are not grouped together first; instead each subtable is treated
|
|
55
|
+
# independently, so currently this step is:
|
|
56
|
+
# Split existing subtables into more smaller subtables
|
|
57
|
+
gpos = font.get("GPOS")
|
|
58
|
+
|
|
59
|
+
# If the font does not contain a GPOS table, there is nothing to do.
|
|
60
|
+
if gpos is None:
|
|
61
|
+
return font
|
|
62
|
+
|
|
63
|
+
for lookup in gpos.table.LookupList.Lookup:
|
|
64
|
+
if lookup.LookupType == 2:
|
|
65
|
+
compact_lookup(font, level, lookup)
|
|
66
|
+
elif lookup.LookupType == 9 and lookup.SubTable[0].ExtensionLookupType == 2:
|
|
67
|
+
compact_ext_lookup(font, level, lookup)
|
|
68
|
+
|
|
69
|
+
return font
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def compact_lookup(font: TTFont, level: int, lookup: otTables.Lookup) -> None:
|
|
73
|
+
new_subtables = compact_pair_pos(font, level, lookup.SubTable)
|
|
74
|
+
lookup.SubTable = new_subtables
|
|
75
|
+
lookup.SubTableCount = len(new_subtables)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def compact_ext_lookup(font: TTFont, level: int, lookup: otTables.Lookup) -> None:
|
|
79
|
+
new_subtables = compact_pair_pos(
|
|
80
|
+
font, level, [ext_subtable.ExtSubTable for ext_subtable in lookup.SubTable]
|
|
81
|
+
)
|
|
82
|
+
new_ext_subtables = []
|
|
83
|
+
for subtable in new_subtables:
|
|
84
|
+
ext_subtable = otTables.ExtensionPos()
|
|
85
|
+
ext_subtable.Format = 1
|
|
86
|
+
ext_subtable.ExtSubTable = subtable
|
|
87
|
+
new_ext_subtables.append(ext_subtable)
|
|
88
|
+
lookup.SubTable = new_ext_subtables
|
|
89
|
+
lookup.SubTableCount = len(new_ext_subtables)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def compact_pair_pos(
|
|
93
|
+
font: TTFont, level: int, subtables: Sequence[otTables.PairPos]
|
|
94
|
+
) -> Sequence[otTables.PairPos]:
|
|
95
|
+
new_subtables = []
|
|
96
|
+
for subtable in subtables:
|
|
97
|
+
if subtable.Format == 1:
|
|
98
|
+
# Not doing anything to Format 1 (yet?)
|
|
99
|
+
new_subtables.append(subtable)
|
|
100
|
+
elif subtable.Format == 2:
|
|
101
|
+
new_subtables.extend(compact_class_pairs(font, level, subtable))
|
|
102
|
+
return new_subtables
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def compact_class_pairs(
|
|
106
|
+
font: TTFont, level: int, subtable: otTables.PairPos
|
|
107
|
+
) -> List[otTables.PairPos]:
|
|
108
|
+
from fontTools.otlLib.builder import buildPairPosClassesSubtable
|
|
109
|
+
|
|
110
|
+
subtables = []
|
|
111
|
+
classes1: DefaultDict[int, List[str]] = defaultdict(list)
|
|
112
|
+
for g in subtable.Coverage.glyphs:
|
|
113
|
+
classes1[subtable.ClassDef1.classDefs.get(g, 0)].append(g)
|
|
114
|
+
classes2: DefaultDict[int, List[str]] = defaultdict(list)
|
|
115
|
+
for g, i in subtable.ClassDef2.classDefs.items():
|
|
116
|
+
classes2[i].append(g)
|
|
117
|
+
all_pairs = {}
|
|
118
|
+
for i, class1 in enumerate(subtable.Class1Record):
|
|
119
|
+
for j, class2 in enumerate(class1.Class2Record):
|
|
120
|
+
if is_really_zero(class2):
|
|
121
|
+
continue
|
|
122
|
+
all_pairs[(tuple(sorted(classes1[i])), tuple(sorted(classes2[j])))] = (
|
|
123
|
+
getattr(class2, "Value1", None),
|
|
124
|
+
getattr(class2, "Value2", None),
|
|
125
|
+
)
|
|
126
|
+
grouped_pairs = cluster_pairs_by_class2_coverage_custom_cost(font, all_pairs, level)
|
|
127
|
+
for pairs in grouped_pairs:
|
|
128
|
+
subtables.append(buildPairPosClassesSubtable(pairs, font.getReverseGlyphMap()))
|
|
129
|
+
return subtables
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def is_really_zero(class2: otTables.Class2Record) -> bool:
|
|
133
|
+
v1 = getattr(class2, "Value1", None)
|
|
134
|
+
v2 = getattr(class2, "Value2", None)
|
|
135
|
+
return (v1 is None or v1.getEffectiveFormat() == 0) and (
|
|
136
|
+
v2 is None or v2.getEffectiveFormat() == 0
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
Pairs = Dict[
|
|
141
|
+
Tuple[Tuple[str, ...], Tuple[str, ...]],
|
|
142
|
+
Tuple[otBase.ValueRecord, otBase.ValueRecord],
|
|
143
|
+
]
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
# Adapted from https://github.com/fonttools/fonttools/blob/f64f0b42f2d1163b2d85194e0979def539f5dca3/Lib/fontTools/ttLib/tables/otTables.py#L935-L958
|
|
147
|
+
def _getClassRanges(glyphIDs: Iterable[int]):
|
|
148
|
+
glyphIDs = sorted(glyphIDs)
|
|
149
|
+
last = glyphIDs[0]
|
|
150
|
+
ranges = [[last]]
|
|
151
|
+
for glyphID in glyphIDs[1:]:
|
|
152
|
+
if glyphID != last + 1:
|
|
153
|
+
ranges[-1].append(last)
|
|
154
|
+
ranges.append([glyphID])
|
|
155
|
+
last = glyphID
|
|
156
|
+
ranges[-1].append(last)
|
|
157
|
+
return ranges, glyphIDs[0], glyphIDs[-1]
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
# Adapted from https://github.com/fonttools/fonttools/blob/f64f0b42f2d1163b2d85194e0979def539f5dca3/Lib/fontTools/ttLib/tables/otTables.py#L960-L989
|
|
161
|
+
def _classDef_bytes(
|
|
162
|
+
class_data: List[Tuple[List[Tuple[int, int]], int, int]],
|
|
163
|
+
class_ids: List[int],
|
|
164
|
+
coverage=False,
|
|
165
|
+
):
|
|
166
|
+
if not class_ids:
|
|
167
|
+
return 0
|
|
168
|
+
first_ranges, min_glyph_id, max_glyph_id = class_data[class_ids[0]]
|
|
169
|
+
range_count = len(first_ranges)
|
|
170
|
+
for i in class_ids[1:]:
|
|
171
|
+
data = class_data[i]
|
|
172
|
+
range_count += len(data[0])
|
|
173
|
+
min_glyph_id = min(min_glyph_id, data[1])
|
|
174
|
+
max_glyph_id = max(max_glyph_id, data[2])
|
|
175
|
+
glyphCount = max_glyph_id - min_glyph_id + 1
|
|
176
|
+
# https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#class-definition-table-format-1
|
|
177
|
+
format1_bytes = 6 + glyphCount * 2
|
|
178
|
+
# https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#class-definition-table-format-2
|
|
179
|
+
format2_bytes = 4 + range_count * 6
|
|
180
|
+
return min(format1_bytes, format2_bytes)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
ClusteringContext = namedtuple(
|
|
184
|
+
"ClusteringContext",
|
|
185
|
+
[
|
|
186
|
+
"lines",
|
|
187
|
+
"all_class1",
|
|
188
|
+
"all_class1_data",
|
|
189
|
+
"all_class2_data",
|
|
190
|
+
"valueFormat1_bytes",
|
|
191
|
+
"valueFormat2_bytes",
|
|
192
|
+
],
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
@dataclass
|
|
197
|
+
class Cluster:
|
|
198
|
+
ctx: ClusteringContext
|
|
199
|
+
indices_bitmask: int
|
|
200
|
+
|
|
201
|
+
@cached_property
|
|
202
|
+
def indices(self):
|
|
203
|
+
return bit_indices(self.indices_bitmask)
|
|
204
|
+
|
|
205
|
+
@cached_property
|
|
206
|
+
def column_indices(self):
|
|
207
|
+
# Indices of columns that have a 1 in at least 1 line
|
|
208
|
+
# => binary OR all the lines
|
|
209
|
+
bitmask = reduce(int.__or__, (self.ctx.lines[i] for i in self.indices))
|
|
210
|
+
return bit_indices(bitmask)
|
|
211
|
+
|
|
212
|
+
@property
|
|
213
|
+
def width(self):
|
|
214
|
+
# Add 1 because Class2=0 cannot be used but needs to be encoded.
|
|
215
|
+
return len(self.column_indices) + 1
|
|
216
|
+
|
|
217
|
+
@cached_property
|
|
218
|
+
def cost(self):
|
|
219
|
+
return (
|
|
220
|
+
# 2 bytes to store the offset to this subtable in the Lookup table above
|
|
221
|
+
2
|
|
222
|
+
# Contents of the subtable
|
|
223
|
+
# From: https://docs.microsoft.com/en-us/typography/opentype/spec/gpos#pair-adjustment-positioning-format-2-class-pair-adjustment
|
|
224
|
+
# uint16 posFormat Format identifier: format = 2
|
|
225
|
+
+ 2
|
|
226
|
+
# Offset16 coverageOffset Offset to Coverage table, from beginning of PairPos subtable.
|
|
227
|
+
+ 2
|
|
228
|
+
+ self.coverage_bytes
|
|
229
|
+
# uint16 valueFormat1 ValueRecord definition — for the first glyph of the pair (may be zero).
|
|
230
|
+
+ 2
|
|
231
|
+
# uint16 valueFormat2 ValueRecord definition — for the second glyph of the pair (may be zero).
|
|
232
|
+
+ 2
|
|
233
|
+
# Offset16 classDef1Offset Offset to ClassDef table, from beginning of PairPos subtable — for the first glyph of the pair.
|
|
234
|
+
+ 2
|
|
235
|
+
+ self.classDef1_bytes
|
|
236
|
+
# Offset16 classDef2Offset Offset to ClassDef table, from beginning of PairPos subtable — for the second glyph of the pair.
|
|
237
|
+
+ 2
|
|
238
|
+
+ self.classDef2_bytes
|
|
239
|
+
# uint16 class1Count Number of classes in classDef1 table — includes Class 0.
|
|
240
|
+
+ 2
|
|
241
|
+
# uint16 class2Count Number of classes in classDef2 table — includes Class 0.
|
|
242
|
+
+ 2
|
|
243
|
+
# Class1Record class1Records[class1Count] Array of Class1 records, ordered by classes in classDef1.
|
|
244
|
+
+ (self.ctx.valueFormat1_bytes + self.ctx.valueFormat2_bytes)
|
|
245
|
+
* len(self.indices)
|
|
246
|
+
* self.width
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
@property
|
|
250
|
+
def coverage_bytes(self):
|
|
251
|
+
format1_bytes = (
|
|
252
|
+
# From https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#coverage-format-1
|
|
253
|
+
# uint16 coverageFormat Format identifier — format = 1
|
|
254
|
+
# uint16 glyphCount Number of glyphs in the glyph array
|
|
255
|
+
4
|
|
256
|
+
# uint16 glyphArray[glyphCount] Array of glyph IDs — in numerical order
|
|
257
|
+
+ sum(len(self.ctx.all_class1[i]) for i in self.indices) * 2
|
|
258
|
+
)
|
|
259
|
+
ranges = sorted(
|
|
260
|
+
chain.from_iterable(self.ctx.all_class1_data[i][0] for i in self.indices)
|
|
261
|
+
)
|
|
262
|
+
merged_range_count = 0
|
|
263
|
+
last = None
|
|
264
|
+
for start, end in ranges:
|
|
265
|
+
if last is not None and start != last + 1:
|
|
266
|
+
merged_range_count += 1
|
|
267
|
+
last = end
|
|
268
|
+
format2_bytes = (
|
|
269
|
+
# From https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#coverage-format-2
|
|
270
|
+
# uint16 coverageFormat Format identifier — format = 2
|
|
271
|
+
# uint16 rangeCount Number of RangeRecords
|
|
272
|
+
4
|
|
273
|
+
# RangeRecord rangeRecords[rangeCount] Array of glyph ranges — ordered by startGlyphID.
|
|
274
|
+
# uint16 startGlyphID First glyph ID in the range
|
|
275
|
+
# uint16 endGlyphID Last glyph ID in the range
|
|
276
|
+
# uint16 startCoverageIndex Coverage Index of first glyph ID in range
|
|
277
|
+
+ merged_range_count * 6
|
|
278
|
+
)
|
|
279
|
+
return min(format1_bytes, format2_bytes)
|
|
280
|
+
|
|
281
|
+
@property
|
|
282
|
+
def classDef1_bytes(self):
|
|
283
|
+
# We can skip encoding one of the Class1 definitions, and use
|
|
284
|
+
# Class1=0 to represent it instead, because Class1 is gated by the
|
|
285
|
+
# Coverage definition. Use Class1=0 for the highest byte savings.
|
|
286
|
+
# Going through all options takes too long, pick the biggest class
|
|
287
|
+
# = what happens in otlLib.builder.ClassDefBuilder.classes()
|
|
288
|
+
biggest_index = max(self.indices, key=lambda i: len(self.ctx.all_class1[i]))
|
|
289
|
+
return _classDef_bytes(
|
|
290
|
+
self.ctx.all_class1_data, [i for i in self.indices if i != biggest_index]
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
@property
|
|
294
|
+
def classDef2_bytes(self):
|
|
295
|
+
# All Class2 need to be encoded because we can't use Class2=0
|
|
296
|
+
return _classDef_bytes(self.ctx.all_class2_data, self.column_indices)
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def cluster_pairs_by_class2_coverage_custom_cost(
|
|
300
|
+
font: TTFont,
|
|
301
|
+
pairs: Pairs,
|
|
302
|
+
compression: int = 5,
|
|
303
|
+
) -> List[Pairs]:
|
|
304
|
+
if not pairs:
|
|
305
|
+
# The subtable was actually empty?
|
|
306
|
+
return [pairs]
|
|
307
|
+
|
|
308
|
+
# Sorted for reproducibility/determinism
|
|
309
|
+
all_class1 = sorted(set(pair[0] for pair in pairs))
|
|
310
|
+
all_class2 = sorted(set(pair[1] for pair in pairs))
|
|
311
|
+
|
|
312
|
+
# Use Python's big ints for binary vectors representing each line
|
|
313
|
+
lines = [
|
|
314
|
+
sum(
|
|
315
|
+
1 << i if (class1, class2) in pairs else 0
|
|
316
|
+
for i, class2 in enumerate(all_class2)
|
|
317
|
+
)
|
|
318
|
+
for class1 in all_class1
|
|
319
|
+
]
|
|
320
|
+
|
|
321
|
+
# Map glyph names to ids and work with ints throughout for ClassDef formats
|
|
322
|
+
name_to_id = font.getReverseGlyphMap()
|
|
323
|
+
# Each entry in the arrays below is (range_count, min_glyph_id, max_glyph_id)
|
|
324
|
+
all_class1_data = [
|
|
325
|
+
_getClassRanges(name_to_id[name] for name in cls) for cls in all_class1
|
|
326
|
+
]
|
|
327
|
+
all_class2_data = [
|
|
328
|
+
_getClassRanges(name_to_id[name] for name in cls) for cls in all_class2
|
|
329
|
+
]
|
|
330
|
+
|
|
331
|
+
format1 = 0
|
|
332
|
+
format2 = 0
|
|
333
|
+
for pair, value in pairs.items():
|
|
334
|
+
format1 |= value[0].getEffectiveFormat() if value[0] else 0
|
|
335
|
+
format2 |= value[1].getEffectiveFormat() if value[1] else 0
|
|
336
|
+
valueFormat1_bytes = bit_count(format1) * 2
|
|
337
|
+
valueFormat2_bytes = bit_count(format2) * 2
|
|
338
|
+
|
|
339
|
+
ctx = ClusteringContext(
|
|
340
|
+
lines,
|
|
341
|
+
all_class1,
|
|
342
|
+
all_class1_data,
|
|
343
|
+
all_class2_data,
|
|
344
|
+
valueFormat1_bytes,
|
|
345
|
+
valueFormat2_bytes,
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
cluster_cache: Dict[int, Cluster] = {}
|
|
349
|
+
|
|
350
|
+
def make_cluster(indices: int) -> Cluster:
|
|
351
|
+
cluster = cluster_cache.get(indices, None)
|
|
352
|
+
if cluster is not None:
|
|
353
|
+
return cluster
|
|
354
|
+
cluster = Cluster(ctx, indices)
|
|
355
|
+
cluster_cache[indices] = cluster
|
|
356
|
+
return cluster
|
|
357
|
+
|
|
358
|
+
def merge(cluster: Cluster, other: Cluster) -> Cluster:
|
|
359
|
+
return make_cluster(cluster.indices_bitmask | other.indices_bitmask)
|
|
360
|
+
|
|
361
|
+
# Agglomerative clustering by hand, checking the cost gain of the new
|
|
362
|
+
# cluster against the previously separate clusters
|
|
363
|
+
# Start with 1 cluster per line
|
|
364
|
+
# cluster = set of lines = new subtable
|
|
365
|
+
clusters = [make_cluster(1 << i) for i in range(len(lines))]
|
|
366
|
+
|
|
367
|
+
# Cost of 1 cluster with everything
|
|
368
|
+
# `(1 << len) - 1` gives a bitmask full of 1's of length `len`
|
|
369
|
+
cost_before_splitting = make_cluster((1 << len(lines)) - 1).cost
|
|
370
|
+
log.debug(f" len(clusters) = {len(clusters)}")
|
|
371
|
+
|
|
372
|
+
while len(clusters) > 1:
|
|
373
|
+
lowest_cost_change = None
|
|
374
|
+
best_cluster_index = None
|
|
375
|
+
best_other_index = None
|
|
376
|
+
best_merged = None
|
|
377
|
+
for i, cluster in enumerate(clusters):
|
|
378
|
+
for j, other in enumerate(clusters[i + 1 :]):
|
|
379
|
+
merged = merge(cluster, other)
|
|
380
|
+
cost_change = merged.cost - cluster.cost - other.cost
|
|
381
|
+
if lowest_cost_change is None or cost_change < lowest_cost_change:
|
|
382
|
+
lowest_cost_change = cost_change
|
|
383
|
+
best_cluster_index = i
|
|
384
|
+
best_other_index = i + 1 + j
|
|
385
|
+
best_merged = merged
|
|
386
|
+
assert lowest_cost_change is not None
|
|
387
|
+
assert best_cluster_index is not None
|
|
388
|
+
assert best_other_index is not None
|
|
389
|
+
assert best_merged is not None
|
|
390
|
+
|
|
391
|
+
# If the best merge we found is still taking down the file size, then
|
|
392
|
+
# there's no question: we must do it, because it's beneficial in both
|
|
393
|
+
# ways (lower file size and lower number of subtables). However, if the
|
|
394
|
+
# best merge we found is not reducing file size anymore, then we need to
|
|
395
|
+
# look at the other stop criteria = the compression factor.
|
|
396
|
+
if lowest_cost_change > 0:
|
|
397
|
+
# Stop critera: check whether we should keep merging.
|
|
398
|
+
# Compute size reduction brought by splitting
|
|
399
|
+
cost_after_splitting = sum(c.cost for c in clusters)
|
|
400
|
+
# size_reduction so that after = before * (1 - size_reduction)
|
|
401
|
+
# E.g. before = 1000, after = 800, 1 - 800/1000 = 0.2
|
|
402
|
+
size_reduction = 1 - cost_after_splitting / cost_before_splitting
|
|
403
|
+
|
|
404
|
+
# Force more merging by taking into account the compression number.
|
|
405
|
+
# Target behaviour: compression number = 1 to 9, default 5 like gzip
|
|
406
|
+
# - 1 = accept to add 1 subtable to reduce size by 50%
|
|
407
|
+
# - 5 = accept to add 5 subtables to reduce size by 50%
|
|
408
|
+
# See https://github.com/harfbuzz/packtab/blob/master/Lib/packTab/__init__.py#L690-L691
|
|
409
|
+
# Given the size reduction we have achieved so far, compute how many
|
|
410
|
+
# new subtables are acceptable.
|
|
411
|
+
max_new_subtables = -log2(1 - size_reduction) * compression
|
|
412
|
+
log.debug(
|
|
413
|
+
f" len(clusters) = {len(clusters):3d} size_reduction={size_reduction:5.2f} max_new_subtables={max_new_subtables}",
|
|
414
|
+
)
|
|
415
|
+
if compression == 9:
|
|
416
|
+
# Override level 9 to mean: create any number of subtables
|
|
417
|
+
max_new_subtables = len(clusters)
|
|
418
|
+
|
|
419
|
+
# If we have managed to take the number of new subtables below the
|
|
420
|
+
# threshold, then we can stop.
|
|
421
|
+
if len(clusters) <= max_new_subtables + 1:
|
|
422
|
+
break
|
|
423
|
+
|
|
424
|
+
# No reason to stop yet, do the merge and move on to the next.
|
|
425
|
+
del clusters[best_other_index]
|
|
426
|
+
clusters[best_cluster_index] = best_merged
|
|
427
|
+
|
|
428
|
+
# All clusters are final; turn bitmasks back into the "Pairs" format
|
|
429
|
+
pairs_by_class1: Dict[Tuple[str, ...], Pairs] = defaultdict(dict)
|
|
430
|
+
for pair, values in pairs.items():
|
|
431
|
+
pairs_by_class1[pair[0]][pair] = values
|
|
432
|
+
pairs_groups: List[Pairs] = []
|
|
433
|
+
for cluster in clusters:
|
|
434
|
+
pairs_group: Pairs = dict()
|
|
435
|
+
for i in cluster.indices:
|
|
436
|
+
class1 = all_class1[i]
|
|
437
|
+
pairs_group.update(pairs_by_class1[class1])
|
|
438
|
+
pairs_groups.append(pairs_group)
|
|
439
|
+
return pairs_groups
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Empty __init__.py file to signal Python this directory is a package."""
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""Calculate the area of a glyph."""
|
|
2
|
+
|
|
3
|
+
from fontTools.pens.basePen import BasePen
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
__all__ = ["AreaPen"]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class AreaPen(BasePen):
|
|
10
|
+
def __init__(self, glyphset=None):
|
|
11
|
+
BasePen.__init__(self, glyphset)
|
|
12
|
+
self.value = 0
|
|
13
|
+
|
|
14
|
+
def _moveTo(self, p0):
|
|
15
|
+
self._p0 = self._startPoint = p0
|
|
16
|
+
|
|
17
|
+
def _lineTo(self, p1):
|
|
18
|
+
x0, y0 = self._p0
|
|
19
|
+
x1, y1 = p1
|
|
20
|
+
self.value -= (x1 - x0) * (y1 + y0) * 0.5
|
|
21
|
+
self._p0 = p1
|
|
22
|
+
|
|
23
|
+
def _qCurveToOne(self, p1, p2):
|
|
24
|
+
# https://github.com/Pomax/bezierinfo/issues/44
|
|
25
|
+
p0 = self._p0
|
|
26
|
+
x0, y0 = p0[0], p0[1]
|
|
27
|
+
x1, y1 = p1[0] - x0, p1[1] - y0
|
|
28
|
+
x2, y2 = p2[0] - x0, p2[1] - y0
|
|
29
|
+
self.value -= (x2 * y1 - x1 * y2) / 3
|
|
30
|
+
self._lineTo(p2)
|
|
31
|
+
self._p0 = p2
|
|
32
|
+
|
|
33
|
+
def _curveToOne(self, p1, p2, p3):
|
|
34
|
+
# https://github.com/Pomax/bezierinfo/issues/44
|
|
35
|
+
p0 = self._p0
|
|
36
|
+
x0, y0 = p0[0], p0[1]
|
|
37
|
+
x1, y1 = p1[0] - x0, p1[1] - y0
|
|
38
|
+
x2, y2 = p2[0] - x0, p2[1] - y0
|
|
39
|
+
x3, y3 = p3[0] - x0, p3[1] - y0
|
|
40
|
+
self.value -= (x1 * (-y2 - y3) + x2 * (y1 - 2 * y3) + x3 * (y1 + 2 * y2)) * 0.15
|
|
41
|
+
self._lineTo(p3)
|
|
42
|
+
self._p0 = p3
|
|
43
|
+
|
|
44
|
+
def _closePath(self):
|
|
45
|
+
self._lineTo(self._startPoint)
|
|
46
|
+
del self._p0, self._startPoint
|
|
47
|
+
|
|
48
|
+
def _endPath(self):
|
|
49
|
+
if self._p0 != self._startPoint:
|
|
50
|
+
# Area is not defined for open contours.
|
|
51
|
+
raise NotImplementedError
|
|
52
|
+
del self._p0, self._startPoint
|