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.
Files changed (353) hide show
  1. fontTools/__init__.py +8 -0
  2. fontTools/__main__.py +35 -0
  3. fontTools/afmLib.py +439 -0
  4. fontTools/agl.py +5233 -0
  5. fontTools/annotations.py +30 -0
  6. fontTools/cffLib/CFF2ToCFF.py +258 -0
  7. fontTools/cffLib/CFFToCFF2.py +305 -0
  8. fontTools/cffLib/__init__.py +3694 -0
  9. fontTools/cffLib/specializer.py +927 -0
  10. fontTools/cffLib/transforms.py +495 -0
  11. fontTools/cffLib/width.py +210 -0
  12. fontTools/colorLib/__init__.py +0 -0
  13. fontTools/colorLib/builder.py +664 -0
  14. fontTools/colorLib/errors.py +2 -0
  15. fontTools/colorLib/geometry.py +143 -0
  16. fontTools/colorLib/table_builder.py +223 -0
  17. fontTools/colorLib/unbuilder.py +81 -0
  18. fontTools/config/__init__.py +90 -0
  19. fontTools/cu2qu/__init__.py +15 -0
  20. fontTools/cu2qu/__main__.py +6 -0
  21. fontTools/cu2qu/benchmark.py +54 -0
  22. fontTools/cu2qu/cli.py +198 -0
  23. fontTools/cu2qu/cu2qu.c +15817 -0
  24. fontTools/cu2qu/cu2qu.cp311-win32.pyd +0 -0
  25. fontTools/cu2qu/cu2qu.py +563 -0
  26. fontTools/cu2qu/errors.py +77 -0
  27. fontTools/cu2qu/ufo.py +363 -0
  28. fontTools/designspaceLib/__init__.py +3343 -0
  29. fontTools/designspaceLib/__main__.py +6 -0
  30. fontTools/designspaceLib/split.py +475 -0
  31. fontTools/designspaceLib/statNames.py +260 -0
  32. fontTools/designspaceLib/types.py +147 -0
  33. fontTools/encodings/MacRoman.py +258 -0
  34. fontTools/encodings/StandardEncoding.py +258 -0
  35. fontTools/encodings/__init__.py +1 -0
  36. fontTools/encodings/codecs.py +135 -0
  37. fontTools/feaLib/__init__.py +4 -0
  38. fontTools/feaLib/__main__.py +78 -0
  39. fontTools/feaLib/ast.py +2143 -0
  40. fontTools/feaLib/builder.py +1814 -0
  41. fontTools/feaLib/error.py +22 -0
  42. fontTools/feaLib/lexer.c +17029 -0
  43. fontTools/feaLib/lexer.cp311-win32.pyd +0 -0
  44. fontTools/feaLib/lexer.py +287 -0
  45. fontTools/feaLib/location.py +12 -0
  46. fontTools/feaLib/lookupDebugInfo.py +12 -0
  47. fontTools/feaLib/parser.py +2394 -0
  48. fontTools/feaLib/variableScalar.py +118 -0
  49. fontTools/fontBuilder.py +1014 -0
  50. fontTools/help.py +36 -0
  51. fontTools/merge/__init__.py +248 -0
  52. fontTools/merge/__main__.py +6 -0
  53. fontTools/merge/base.py +81 -0
  54. fontTools/merge/cmap.py +173 -0
  55. fontTools/merge/layout.py +526 -0
  56. fontTools/merge/options.py +85 -0
  57. fontTools/merge/tables.py +352 -0
  58. fontTools/merge/unicode.py +78 -0
  59. fontTools/merge/util.py +143 -0
  60. fontTools/misc/__init__.py +1 -0
  61. fontTools/misc/arrayTools.py +424 -0
  62. fontTools/misc/bezierTools.c +39731 -0
  63. fontTools/misc/bezierTools.cp311-win32.pyd +0 -0
  64. fontTools/misc/bezierTools.py +1500 -0
  65. fontTools/misc/classifyTools.py +170 -0
  66. fontTools/misc/cliTools.py +53 -0
  67. fontTools/misc/configTools.py +349 -0
  68. fontTools/misc/cython.py +27 -0
  69. fontTools/misc/dictTools.py +83 -0
  70. fontTools/misc/eexec.py +119 -0
  71. fontTools/misc/encodingTools.py +72 -0
  72. fontTools/misc/enumTools.py +23 -0
  73. fontTools/misc/etree.py +456 -0
  74. fontTools/misc/filenames.py +245 -0
  75. fontTools/misc/filesystem/__init__.py +68 -0
  76. fontTools/misc/filesystem/_base.py +134 -0
  77. fontTools/misc/filesystem/_copy.py +45 -0
  78. fontTools/misc/filesystem/_errors.py +54 -0
  79. fontTools/misc/filesystem/_info.py +75 -0
  80. fontTools/misc/filesystem/_osfs.py +164 -0
  81. fontTools/misc/filesystem/_path.py +67 -0
  82. fontTools/misc/filesystem/_subfs.py +92 -0
  83. fontTools/misc/filesystem/_tempfs.py +34 -0
  84. fontTools/misc/filesystem/_tools.py +34 -0
  85. fontTools/misc/filesystem/_walk.py +55 -0
  86. fontTools/misc/filesystem/_zipfs.py +204 -0
  87. fontTools/misc/fixedTools.py +253 -0
  88. fontTools/misc/intTools.py +25 -0
  89. fontTools/misc/iterTools.py +12 -0
  90. fontTools/misc/lazyTools.py +42 -0
  91. fontTools/misc/loggingTools.py +543 -0
  92. fontTools/misc/macCreatorType.py +56 -0
  93. fontTools/misc/macRes.py +261 -0
  94. fontTools/misc/plistlib/__init__.py +681 -0
  95. fontTools/misc/plistlib/py.typed +0 -0
  96. fontTools/misc/psCharStrings.py +1511 -0
  97. fontTools/misc/psLib.py +398 -0
  98. fontTools/misc/psOperators.py +572 -0
  99. fontTools/misc/py23.py +96 -0
  100. fontTools/misc/roundTools.py +110 -0
  101. fontTools/misc/sstruct.py +227 -0
  102. fontTools/misc/symfont.py +242 -0
  103. fontTools/misc/testTools.py +233 -0
  104. fontTools/misc/textTools.py +156 -0
  105. fontTools/misc/timeTools.py +88 -0
  106. fontTools/misc/transform.py +516 -0
  107. fontTools/misc/treeTools.py +45 -0
  108. fontTools/misc/vector.py +147 -0
  109. fontTools/misc/visitor.py +158 -0
  110. fontTools/misc/xmlReader.py +188 -0
  111. fontTools/misc/xmlWriter.py +231 -0
  112. fontTools/mtiLib/__init__.py +1400 -0
  113. fontTools/mtiLib/__main__.py +5 -0
  114. fontTools/otlLib/__init__.py +1 -0
  115. fontTools/otlLib/builder.py +3465 -0
  116. fontTools/otlLib/error.py +11 -0
  117. fontTools/otlLib/maxContextCalc.py +96 -0
  118. fontTools/otlLib/optimize/__init__.py +53 -0
  119. fontTools/otlLib/optimize/__main__.py +6 -0
  120. fontTools/otlLib/optimize/gpos.py +439 -0
  121. fontTools/pens/__init__.py +1 -0
  122. fontTools/pens/areaPen.py +52 -0
  123. fontTools/pens/basePen.py +475 -0
  124. fontTools/pens/boundsPen.py +98 -0
  125. fontTools/pens/cairoPen.py +26 -0
  126. fontTools/pens/cocoaPen.py +26 -0
  127. fontTools/pens/cu2quPen.py +325 -0
  128. fontTools/pens/explicitClosingLinePen.py +101 -0
  129. fontTools/pens/filterPen.py +433 -0
  130. fontTools/pens/freetypePen.py +462 -0
  131. fontTools/pens/hashPointPen.py +89 -0
  132. fontTools/pens/momentsPen.c +13378 -0
  133. fontTools/pens/momentsPen.cp311-win32.pyd +0 -0
  134. fontTools/pens/momentsPen.py +879 -0
  135. fontTools/pens/perimeterPen.py +69 -0
  136. fontTools/pens/pointInsidePen.py +192 -0
  137. fontTools/pens/pointPen.py +643 -0
  138. fontTools/pens/qtPen.py +29 -0
  139. fontTools/pens/qu2cuPen.py +105 -0
  140. fontTools/pens/quartzPen.py +43 -0
  141. fontTools/pens/recordingPen.py +335 -0
  142. fontTools/pens/reportLabPen.py +79 -0
  143. fontTools/pens/reverseContourPen.py +96 -0
  144. fontTools/pens/roundingPen.py +130 -0
  145. fontTools/pens/statisticsPen.py +312 -0
  146. fontTools/pens/svgPathPen.py +310 -0
  147. fontTools/pens/t2CharStringPen.py +88 -0
  148. fontTools/pens/teePen.py +55 -0
  149. fontTools/pens/transformPen.py +115 -0
  150. fontTools/pens/ttGlyphPen.py +335 -0
  151. fontTools/pens/wxPen.py +29 -0
  152. fontTools/qu2cu/__init__.py +15 -0
  153. fontTools/qu2cu/__main__.py +7 -0
  154. fontTools/qu2cu/benchmark.py +56 -0
  155. fontTools/qu2cu/cli.py +125 -0
  156. fontTools/qu2cu/qu2cu.c +16682 -0
  157. fontTools/qu2cu/qu2cu.cp311-win32.pyd +0 -0
  158. fontTools/qu2cu/qu2cu.py +405 -0
  159. fontTools/subset/__init__.py +4096 -0
  160. fontTools/subset/__main__.py +6 -0
  161. fontTools/subset/cff.py +184 -0
  162. fontTools/subset/svg.py +253 -0
  163. fontTools/subset/util.py +25 -0
  164. fontTools/svgLib/__init__.py +3 -0
  165. fontTools/svgLib/path/__init__.py +65 -0
  166. fontTools/svgLib/path/arc.py +154 -0
  167. fontTools/svgLib/path/parser.py +322 -0
  168. fontTools/svgLib/path/shapes.py +183 -0
  169. fontTools/t1Lib/__init__.py +648 -0
  170. fontTools/tfmLib.py +460 -0
  171. fontTools/ttLib/__init__.py +30 -0
  172. fontTools/ttLib/__main__.py +148 -0
  173. fontTools/ttLib/macUtils.py +54 -0
  174. fontTools/ttLib/removeOverlaps.py +395 -0
  175. fontTools/ttLib/reorderGlyphs.py +285 -0
  176. fontTools/ttLib/scaleUpem.py +436 -0
  177. fontTools/ttLib/sfnt.py +661 -0
  178. fontTools/ttLib/standardGlyphOrder.py +271 -0
  179. fontTools/ttLib/tables/B_A_S_E_.py +14 -0
  180. fontTools/ttLib/tables/BitmapGlyphMetrics.py +64 -0
  181. fontTools/ttLib/tables/C_B_D_T_.py +113 -0
  182. fontTools/ttLib/tables/C_B_L_C_.py +19 -0
  183. fontTools/ttLib/tables/C_F_F_.py +61 -0
  184. fontTools/ttLib/tables/C_F_F__2.py +26 -0
  185. fontTools/ttLib/tables/C_O_L_R_.py +165 -0
  186. fontTools/ttLib/tables/C_P_A_L_.py +305 -0
  187. fontTools/ttLib/tables/D_S_I_G_.py +158 -0
  188. fontTools/ttLib/tables/D__e_b_g.py +35 -0
  189. fontTools/ttLib/tables/DefaultTable.py +49 -0
  190. fontTools/ttLib/tables/E_B_D_T_.py +835 -0
  191. fontTools/ttLib/tables/E_B_L_C_.py +718 -0
  192. fontTools/ttLib/tables/F_F_T_M_.py +52 -0
  193. fontTools/ttLib/tables/F__e_a_t.py +149 -0
  194. fontTools/ttLib/tables/G_D_E_F_.py +13 -0
  195. fontTools/ttLib/tables/G_M_A_P_.py +148 -0
  196. fontTools/ttLib/tables/G_P_K_G_.py +133 -0
  197. fontTools/ttLib/tables/G_P_O_S_.py +14 -0
  198. fontTools/ttLib/tables/G_S_U_B_.py +13 -0
  199. fontTools/ttLib/tables/G_V_A_R_.py +5 -0
  200. fontTools/ttLib/tables/G__l_a_t.py +235 -0
  201. fontTools/ttLib/tables/G__l_o_c.py +85 -0
  202. fontTools/ttLib/tables/H_V_A_R_.py +13 -0
  203. fontTools/ttLib/tables/J_S_T_F_.py +13 -0
  204. fontTools/ttLib/tables/L_T_S_H_.py +58 -0
  205. fontTools/ttLib/tables/M_A_T_H_.py +13 -0
  206. fontTools/ttLib/tables/M_E_T_A_.py +352 -0
  207. fontTools/ttLib/tables/M_V_A_R_.py +13 -0
  208. fontTools/ttLib/tables/O_S_2f_2.py +752 -0
  209. fontTools/ttLib/tables/S_I_N_G_.py +99 -0
  210. fontTools/ttLib/tables/S_T_A_T_.py +15 -0
  211. fontTools/ttLib/tables/S_V_G_.py +223 -0
  212. fontTools/ttLib/tables/S__i_l_f.py +1040 -0
  213. fontTools/ttLib/tables/S__i_l_l.py +92 -0
  214. fontTools/ttLib/tables/T_S_I_B_.py +13 -0
  215. fontTools/ttLib/tables/T_S_I_C_.py +14 -0
  216. fontTools/ttLib/tables/T_S_I_D_.py +13 -0
  217. fontTools/ttLib/tables/T_S_I_J_.py +13 -0
  218. fontTools/ttLib/tables/T_S_I_P_.py +13 -0
  219. fontTools/ttLib/tables/T_S_I_S_.py +13 -0
  220. fontTools/ttLib/tables/T_S_I_V_.py +26 -0
  221. fontTools/ttLib/tables/T_S_I__0.py +70 -0
  222. fontTools/ttLib/tables/T_S_I__1.py +163 -0
  223. fontTools/ttLib/tables/T_S_I__2.py +17 -0
  224. fontTools/ttLib/tables/T_S_I__3.py +22 -0
  225. fontTools/ttLib/tables/T_S_I__5.py +60 -0
  226. fontTools/ttLib/tables/T_T_F_A_.py +14 -0
  227. fontTools/ttLib/tables/TupleVariation.py +884 -0
  228. fontTools/ttLib/tables/V_A_R_C_.py +12 -0
  229. fontTools/ttLib/tables/V_D_M_X_.py +249 -0
  230. fontTools/ttLib/tables/V_O_R_G_.py +165 -0
  231. fontTools/ttLib/tables/V_V_A_R_.py +13 -0
  232. fontTools/ttLib/tables/__init__.py +98 -0
  233. fontTools/ttLib/tables/_a_n_k_r.py +15 -0
  234. fontTools/ttLib/tables/_a_v_a_r.py +193 -0
  235. fontTools/ttLib/tables/_b_s_l_n.py +15 -0
  236. fontTools/ttLib/tables/_c_i_d_g.py +24 -0
  237. fontTools/ttLib/tables/_c_m_a_p.py +1591 -0
  238. fontTools/ttLib/tables/_c_v_a_r.py +94 -0
  239. fontTools/ttLib/tables/_c_v_t.py +56 -0
  240. fontTools/ttLib/tables/_f_e_a_t.py +15 -0
  241. fontTools/ttLib/tables/_f_p_g_m.py +62 -0
  242. fontTools/ttLib/tables/_f_v_a_r.py +261 -0
  243. fontTools/ttLib/tables/_g_a_s_p.py +63 -0
  244. fontTools/ttLib/tables/_g_c_i_d.py +13 -0
  245. fontTools/ttLib/tables/_g_l_y_f.py +2311 -0
  246. fontTools/ttLib/tables/_g_v_a_r.py +340 -0
  247. fontTools/ttLib/tables/_h_d_m_x.py +127 -0
  248. fontTools/ttLib/tables/_h_e_a_d.py +130 -0
  249. fontTools/ttLib/tables/_h_h_e_a.py +147 -0
  250. fontTools/ttLib/tables/_h_m_t_x.py +164 -0
  251. fontTools/ttLib/tables/_k_e_r_n.py +289 -0
  252. fontTools/ttLib/tables/_l_c_a_r.py +13 -0
  253. fontTools/ttLib/tables/_l_o_c_a.py +70 -0
  254. fontTools/ttLib/tables/_l_t_a_g.py +72 -0
  255. fontTools/ttLib/tables/_m_a_x_p.py +147 -0
  256. fontTools/ttLib/tables/_m_e_t_a.py +112 -0
  257. fontTools/ttLib/tables/_m_o_r_t.py +14 -0
  258. fontTools/ttLib/tables/_m_o_r_x.py +15 -0
  259. fontTools/ttLib/tables/_n_a_m_e.py +1242 -0
  260. fontTools/ttLib/tables/_o_p_b_d.py +14 -0
  261. fontTools/ttLib/tables/_p_o_s_t.py +319 -0
  262. fontTools/ttLib/tables/_p_r_e_p.py +16 -0
  263. fontTools/ttLib/tables/_p_r_o_p.py +12 -0
  264. fontTools/ttLib/tables/_s_b_i_x.py +129 -0
  265. fontTools/ttLib/tables/_t_r_a_k.py +332 -0
  266. fontTools/ttLib/tables/_v_h_e_a.py +139 -0
  267. fontTools/ttLib/tables/_v_m_t_x.py +19 -0
  268. fontTools/ttLib/tables/asciiTable.py +20 -0
  269. fontTools/ttLib/tables/grUtils.py +92 -0
  270. fontTools/ttLib/tables/otBase.py +1458 -0
  271. fontTools/ttLib/tables/otConverters.py +2068 -0
  272. fontTools/ttLib/tables/otData.py +6400 -0
  273. fontTools/ttLib/tables/otTables.py +2703 -0
  274. fontTools/ttLib/tables/otTraverse.py +163 -0
  275. fontTools/ttLib/tables/sbixGlyph.py +149 -0
  276. fontTools/ttLib/tables/sbixStrike.py +177 -0
  277. fontTools/ttLib/tables/table_API_readme.txt +91 -0
  278. fontTools/ttLib/tables/ttProgram.py +594 -0
  279. fontTools/ttLib/ttCollection.py +125 -0
  280. fontTools/ttLib/ttFont.py +1148 -0
  281. fontTools/ttLib/ttGlyphSet.py +490 -0
  282. fontTools/ttLib/ttVisitor.py +32 -0
  283. fontTools/ttLib/woff2.py +1680 -0
  284. fontTools/ttx.py +479 -0
  285. fontTools/ufoLib/__init__.py +2575 -0
  286. fontTools/ufoLib/converters.py +407 -0
  287. fontTools/ufoLib/errors.py +30 -0
  288. fontTools/ufoLib/etree.py +6 -0
  289. fontTools/ufoLib/filenames.py +356 -0
  290. fontTools/ufoLib/glifLib.py +2120 -0
  291. fontTools/ufoLib/kerning.py +141 -0
  292. fontTools/ufoLib/plistlib.py +47 -0
  293. fontTools/ufoLib/pointPen.py +6 -0
  294. fontTools/ufoLib/utils.py +107 -0
  295. fontTools/ufoLib/validators.py +1208 -0
  296. fontTools/unicode.py +50 -0
  297. fontTools/unicodedata/Blocks.py +817 -0
  298. fontTools/unicodedata/Mirrored.py +446 -0
  299. fontTools/unicodedata/OTTags.py +50 -0
  300. fontTools/unicodedata/ScriptExtensions.py +832 -0
  301. fontTools/unicodedata/Scripts.py +3639 -0
  302. fontTools/unicodedata/__init__.py +306 -0
  303. fontTools/varLib/__init__.py +1600 -0
  304. fontTools/varLib/__main__.py +6 -0
  305. fontTools/varLib/avar/__init__.py +0 -0
  306. fontTools/varLib/avar/__main__.py +72 -0
  307. fontTools/varLib/avar/build.py +79 -0
  308. fontTools/varLib/avar/map.py +108 -0
  309. fontTools/varLib/avar/plan.py +1004 -0
  310. fontTools/varLib/avar/unbuild.py +271 -0
  311. fontTools/varLib/avarPlanner.py +8 -0
  312. fontTools/varLib/builder.py +215 -0
  313. fontTools/varLib/cff.py +631 -0
  314. fontTools/varLib/errors.py +219 -0
  315. fontTools/varLib/featureVars.py +703 -0
  316. fontTools/varLib/hvar.py +113 -0
  317. fontTools/varLib/instancer/__init__.py +2052 -0
  318. fontTools/varLib/instancer/__main__.py +5 -0
  319. fontTools/varLib/instancer/featureVars.py +190 -0
  320. fontTools/varLib/instancer/names.py +388 -0
  321. fontTools/varLib/instancer/solver.py +309 -0
  322. fontTools/varLib/interpolatable.py +1209 -0
  323. fontTools/varLib/interpolatableHelpers.py +399 -0
  324. fontTools/varLib/interpolatablePlot.py +1269 -0
  325. fontTools/varLib/interpolatableTestContourOrder.py +82 -0
  326. fontTools/varLib/interpolatableTestStartingPoint.py +107 -0
  327. fontTools/varLib/interpolate_layout.py +124 -0
  328. fontTools/varLib/iup.c +19815 -0
  329. fontTools/varLib/iup.cp311-win32.pyd +0 -0
  330. fontTools/varLib/iup.py +490 -0
  331. fontTools/varLib/merger.py +1717 -0
  332. fontTools/varLib/models.py +642 -0
  333. fontTools/varLib/multiVarStore.py +253 -0
  334. fontTools/varLib/mutator.py +529 -0
  335. fontTools/varLib/mvar.py +40 -0
  336. fontTools/varLib/plot.py +238 -0
  337. fontTools/varLib/stat.py +149 -0
  338. fontTools/varLib/varStore.py +739 -0
  339. fontTools/voltLib/__init__.py +5 -0
  340. fontTools/voltLib/__main__.py +206 -0
  341. fontTools/voltLib/ast.py +452 -0
  342. fontTools/voltLib/error.py +12 -0
  343. fontTools/voltLib/lexer.py +99 -0
  344. fontTools/voltLib/parser.py +664 -0
  345. fontTools/voltLib/voltToFea.py +911 -0
  346. fonttools-4.60.2.data/data/share/man/man1/ttx.1 +225 -0
  347. fonttools-4.60.2.dist-info/METADATA +2250 -0
  348. fonttools-4.60.2.dist-info/RECORD +353 -0
  349. fonttools-4.60.2.dist-info/WHEEL +5 -0
  350. fonttools-4.60.2.dist-info/entry_points.txt +5 -0
  351. fonttools-4.60.2.dist-info/licenses/LICENSE +21 -0
  352. fonttools-4.60.2.dist-info/licenses/LICENSE.external +388 -0
  353. fonttools-4.60.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,911 @@
1
+ """\
2
+ MS VOLT ``.vtp`` to AFDKO ``.fea`` OpenType Layout converter.
3
+
4
+ Usage
5
+ -----
6
+
7
+ To convert a VTP project file:
8
+
9
+
10
+ .. code-block:: sh
11
+
12
+ $ fonttools voltLib.voltToFea input.vtp output.fea
13
+
14
+ It is also possible convert font files with `TSIV` table (as saved from Volt),
15
+ in this case the glyph names used in the Volt project will be mapped to the
16
+ actual glyph names in the font files when written to the feature file:
17
+
18
+ .. code-block:: sh
19
+
20
+ $ fonttools voltLib.voltToFea input.ttf output.fea
21
+
22
+ The ``--quiet`` option can be used to suppress warnings.
23
+
24
+ The ``--traceback`` can be used to get Python traceback in case of exceptions,
25
+ instead of suppressing the traceback.
26
+
27
+
28
+ Limitations
29
+ -----------
30
+
31
+ * Not all VOLT features are supported, the script will error if it it
32
+ encounters something it does not understand. Please report an issue if this
33
+ happens.
34
+ * AFDKO feature file syntax for mark positioning is awkward and does not allow
35
+ setting the mark coverage. It also defines mark anchors globally, as a result
36
+ some mark positioning lookups might cover many marks than what was in the VOLT
37
+ file. This should not be an issue in practice, but if it is then the only way
38
+ is to modify the VOLT file or the generated feature file manually to use unique
39
+ mark anchors for each lookup.
40
+ * VOLT allows subtable breaks in any lookup type, but AFDKO feature file
41
+ implementations vary in their support; currently AFDKO’s makeOTF supports
42
+ subtable breaks in pair positioning lookups only, while FontTools’ feaLib
43
+ support it for most substitution lookups and only some positioning lookups.
44
+ """
45
+
46
+ import logging
47
+ import re
48
+ from io import StringIO
49
+ from graphlib import TopologicalSorter
50
+
51
+ from fontTools.feaLib import ast
52
+ from fontTools.ttLib import TTFont, TTLibError
53
+ from fontTools.voltLib import ast as VAst
54
+ from fontTools.voltLib.parser import Parser as VoltParser
55
+
56
+ log = logging.getLogger("fontTools.voltLib.voltToFea")
57
+
58
+ TABLES = ["GDEF", "GSUB", "GPOS"]
59
+
60
+
61
+ def _flatten_group(group):
62
+ ret = []
63
+ if isinstance(group, (tuple, list)):
64
+ for item in group:
65
+ ret.extend(_flatten_group(item))
66
+ elif hasattr(group, "enum"):
67
+ ret.extend(_flatten_group(group.enum))
68
+ else:
69
+ ret.append(group)
70
+ return ret
71
+
72
+
73
+ # Topologically sort of group definitions to ensure that all groups are defined
74
+ # before they are referenced. This is necessary because FEA requires it but
75
+ # VOLT does not, see below.
76
+ def sort_groups(groups):
77
+ group_map = {group.name.lower(): group for group in groups}
78
+ graph = {
79
+ group.name.lower(): [
80
+ x.group.lower()
81
+ for x in _flatten_group(group)
82
+ if isinstance(x, VAst.GroupName)
83
+ ]
84
+ for group in groups
85
+ }
86
+ sorter = TopologicalSorter(graph)
87
+ return [group_map[name] for name in sorter.static_order()]
88
+
89
+
90
+ class Lookup(ast.LookupBlock):
91
+ def __init__(self, name, use_extension=False, location=None):
92
+ super().__init__(name, use_extension, location)
93
+ self.chained = []
94
+
95
+
96
+ class VoltToFea:
97
+ _NOT_LOOKUP_NAME_RE = re.compile(r"[^A-Za-z_0-9.]")
98
+ _NOT_CLASS_NAME_RE = re.compile(r"[^A-Za-z_0-9.\-]")
99
+
100
+ def __init__(self, file_or_path, font=None):
101
+ if isinstance(file_or_path, VAst.VoltFile):
102
+ self._doc, self._file_or_path = file_or_path, None
103
+ else:
104
+ self._doc, self._file_or_path = None, file_or_path
105
+ self._font = font
106
+
107
+ self._glyph_map = {}
108
+ self._glyph_order = None
109
+
110
+ self._gdef = {}
111
+ self._glyphclasses = {}
112
+ self._features = {}
113
+ self._lookups = {}
114
+
115
+ self._marks = set()
116
+ self._ligatures = {}
117
+
118
+ self._markclasses = {}
119
+ self._anchors = {}
120
+
121
+ self._settings = {}
122
+
123
+ self._lookup_names = {}
124
+ self._class_names = {}
125
+
126
+ def _lookupName(self, name):
127
+ if name not in self._lookup_names:
128
+ res = self._NOT_LOOKUP_NAME_RE.sub("_", name)
129
+ while res in self._lookup_names.values():
130
+ res += "_"
131
+ self._lookup_names[name] = res
132
+ return self._lookup_names[name]
133
+
134
+ def _className(self, name):
135
+ if name not in self._class_names:
136
+ res = self._NOT_CLASS_NAME_RE.sub("_", name)
137
+ while res in self._class_names.values():
138
+ res += "_"
139
+ self._class_names[name] = res
140
+ return self._class_names[name]
141
+
142
+ def _collectStatements(self, doc, tables, ignore_unsupported_settings=False):
143
+ # Collect glyph difinitions first, as we need them to map VOLT glyph names to font glyph name.
144
+ for statement in doc.statements:
145
+ if isinstance(statement, VAst.GlyphDefinition):
146
+ self._glyphDefinition(statement)
147
+
148
+ # Collect and sort group definitions first, to make sure a group
149
+ # definition that references other groups comes after them since VOLT
150
+ # does not enforce such ordering, and feature file require it.
151
+ groups = [s for s in doc.statements if isinstance(s, VAst.GroupDefinition)]
152
+ for group in sort_groups(groups):
153
+ self._groupDefinition(group)
154
+
155
+ for statement in doc.statements:
156
+ if isinstance(statement, VAst.AnchorDefinition):
157
+ if "GPOS" in tables:
158
+ self._anchorDefinition(statement)
159
+ elif isinstance(statement, VAst.SettingDefinition):
160
+ self._settingDefinition(statement, ignore_unsupported_settings)
161
+ elif isinstance(statement, (VAst.GlyphDefinition, VAst.GroupDefinition)):
162
+ pass # Handled above
163
+ elif isinstance(statement, VAst.ScriptDefinition):
164
+ self._scriptDefinition(statement)
165
+ elif not isinstance(statement, VAst.LookupDefinition):
166
+ raise NotImplementedError(statement)
167
+
168
+ # Lookup definitions need to be handled last as they reference glyph
169
+ # and mark classes that might be defined after them.
170
+ for statement in doc.statements:
171
+ if isinstance(statement, VAst.LookupDefinition):
172
+ if statement.pos and "GPOS" not in tables:
173
+ continue
174
+ if statement.sub and "GSUB" not in tables:
175
+ continue
176
+ self._lookupDefinition(statement)
177
+
178
+ def _buildFeatureFile(self, tables):
179
+ doc = ast.FeatureFile()
180
+ statements = doc.statements
181
+
182
+ if self._glyphclasses:
183
+ statements.append(ast.Comment("# Glyph classes"))
184
+ statements.extend(self._glyphclasses.values())
185
+
186
+ if self._markclasses:
187
+ statements.append(ast.Comment("\n# Mark classes"))
188
+ statements.extend(c[1] for c in sorted(self._markclasses.items()))
189
+
190
+ if self._lookups:
191
+ statements.append(ast.Comment("\n# Lookups"))
192
+ for lookup in self._lookups.values():
193
+ statements.extend(lookup.chained)
194
+ statements.append(lookup)
195
+
196
+ # Prune features
197
+ features = self._features.copy()
198
+ for feature_tag in features:
199
+ scripts = features[feature_tag]
200
+ for script_tag in scripts:
201
+ langs = scripts[script_tag]
202
+ for language_tag in langs:
203
+ langs[language_tag] = [
204
+ l for l in langs[language_tag] if l.lower() in self._lookups
205
+ ]
206
+ scripts[script_tag] = {t: l for t, l in langs.items() if l}
207
+ features[feature_tag] = {t: s for t, s in scripts.items() if s}
208
+ features = {t: f for t, f in features.items() if f}
209
+
210
+ if features:
211
+ statements.append(ast.Comment("# Features"))
212
+ for feature_tag, scripts in features.items():
213
+ feature = ast.FeatureBlock(feature_tag)
214
+ script_tags = sorted(scripts, key=lambda k: 0 if k == "DFLT" else 1)
215
+ if feature_tag == "aalt" and len(script_tags) > 1:
216
+ log.warning(
217
+ "FEA syntax does not allow script statements in 'aalt' feature, "
218
+ "so only lookups from the first script will be included."
219
+ )
220
+ script_tags = script_tags[:1]
221
+ for script_tag in script_tags:
222
+ if feature_tag != "aalt":
223
+ feature.statements.append(ast.ScriptStatement(script_tag))
224
+ language_tags = sorted(
225
+ scripts[script_tag],
226
+ key=lambda k: 0 if k == "dflt" else 1,
227
+ )
228
+ if feature_tag == "aalt" and len(language_tags) > 1:
229
+ log.warning(
230
+ "FEA syntax does not allow language statements in 'aalt' feature, "
231
+ "so only lookups from the first language will be included."
232
+ )
233
+ language_tags = language_tags[:1]
234
+ for language_tag in language_tags:
235
+ if feature_tag != "aalt":
236
+ include_default = True if language_tag == "dflt" else False
237
+ feature.statements.append(
238
+ ast.LanguageStatement(
239
+ language_tag.ljust(4),
240
+ include_default=include_default,
241
+ )
242
+ )
243
+ for name in scripts[script_tag][language_tag]:
244
+ lookup = self._lookups[name.lower()]
245
+ lookupref = ast.LookupReferenceStatement(lookup)
246
+ feature.statements.append(lookupref)
247
+ statements.append(feature)
248
+
249
+ if self._gdef and "GDEF" in tables:
250
+ classes = []
251
+ for name in ("BASE", "MARK", "LIGATURE", "COMPONENT"):
252
+ if name in self._gdef:
253
+ classname = "GDEF_" + name.lower()
254
+ glyphclass = ast.GlyphClassDefinition(classname, self._gdef[name])
255
+ statements.append(glyphclass)
256
+ classes.append(ast.GlyphClassName(glyphclass))
257
+ else:
258
+ classes.append(None)
259
+
260
+ gdef = ast.TableBlock("GDEF")
261
+ gdef.statements.append(ast.GlyphClassDefStatement(*classes))
262
+ statements.append(gdef)
263
+
264
+ return doc
265
+
266
+ def convert(self, tables=None, ignore_unsupported_settings=False):
267
+ if self._doc is None:
268
+ self._doc = VoltParser(self._file_or_path).parse()
269
+ doc = self._doc
270
+
271
+ if tables is None:
272
+ tables = TABLES
273
+ if self._font is not None:
274
+ self._glyph_order = self._font.getGlyphOrder()
275
+
276
+ self._collectStatements(doc, tables, ignore_unsupported_settings)
277
+ fea = self._buildFeatureFile(tables)
278
+ return fea.asFea()
279
+
280
+ def _glyphName(self, glyph):
281
+ try:
282
+ name = glyph.glyph
283
+ except AttributeError:
284
+ name = glyph
285
+ return ast.GlyphName(self._glyph_map.get(name, name))
286
+
287
+ def _groupName(self, group):
288
+ try:
289
+ name = group.group
290
+ except AttributeError:
291
+ name = group
292
+ return ast.GlyphClassName(self._glyphclasses[name.lower()])
293
+
294
+ def _glyphSet(self, item):
295
+ return [
296
+ (self._glyphName(x) if isinstance(x, (str, VAst.GlyphName)) else x)
297
+ for x in item.glyphSet()
298
+ ]
299
+
300
+ def _coverage(self, coverage, flatten=False):
301
+ items = []
302
+ for item in coverage:
303
+ if isinstance(item, VAst.GlyphName):
304
+ items.append(self._glyphName(item))
305
+ elif isinstance(item, VAst.GroupName):
306
+ items.append(self._groupName(item))
307
+ elif isinstance(item, VAst.Enum):
308
+ item = self._coverage(item.enum, flatten=True)
309
+ if flatten:
310
+ items.extend(item)
311
+ else:
312
+ items.append(ast.GlyphClass(item))
313
+ elif isinstance(item, VAst.Range):
314
+ item = self._glyphSet(item)
315
+ if flatten:
316
+ items.extend(item)
317
+ else:
318
+ items.append(ast.GlyphClass(item))
319
+ else:
320
+ raise NotImplementedError(item)
321
+ return items
322
+
323
+ def _context(self, context):
324
+ out = []
325
+ for item in context:
326
+ coverage = self._coverage(item, flatten=True)
327
+ if len(coverage) > 1:
328
+ coverage = ast.GlyphClass(coverage)
329
+ else:
330
+ coverage = coverage[0]
331
+ out.append(coverage)
332
+ return out
333
+
334
+ def _groupDefinition(self, group):
335
+ name = self._className(group.name)
336
+ glyphs = self._coverage(group.enum.enum, flatten=True)
337
+ glyphclass = ast.GlyphClass(glyphs)
338
+ classdef = ast.GlyphClassDefinition(name, glyphclass)
339
+ self._glyphclasses[group.name.lower()] = classdef
340
+
341
+ def _glyphDefinition(self, glyph):
342
+ try:
343
+ self._glyph_map[glyph.name] = self._glyph_order[glyph.id]
344
+ except TypeError:
345
+ pass
346
+
347
+ if glyph.type in ("BASE", "MARK", "LIGATURE", "COMPONENT"):
348
+ if glyph.type not in self._gdef:
349
+ self._gdef[glyph.type] = ast.GlyphClass()
350
+ self._gdef[glyph.type].glyphs.append(self._glyphName(glyph.name))
351
+
352
+ if glyph.type == "MARK":
353
+ self._marks.add(glyph.name)
354
+ elif glyph.type == "LIGATURE":
355
+ self._ligatures[glyph.name] = glyph.components
356
+
357
+ def _scriptDefinition(self, script):
358
+ stag = script.tag
359
+ for lang in script.langs:
360
+ ltag = lang.tag
361
+ for feature in lang.features:
362
+ lookups = {l.split("\\")[0]: True for l in feature.lookups}
363
+ ftag = feature.tag
364
+ if ftag not in self._features:
365
+ self._features[ftag] = {}
366
+ if stag not in self._features[ftag]:
367
+ self._features[ftag][stag] = {}
368
+ assert ltag not in self._features[ftag][stag]
369
+ self._features[ftag][stag][ltag] = lookups.keys()
370
+
371
+ def _settingDefinition(self, setting, ignore_unsupported=False):
372
+ if setting.name.startswith("COMPILER_"):
373
+ self._settings[setting.name] = setting.value
374
+ elif not ignore_unsupported:
375
+ log.warning(f"Unsupported setting ignored: {setting.name}")
376
+
377
+ def _adjustment(self, adjustment):
378
+ adv, dx, dy, adv_adjust_by, dx_adjust_by, dy_adjust_by = adjustment
379
+
380
+ adv_device = adv_adjust_by and adv_adjust_by.items() or None
381
+ dx_device = dx_adjust_by and dx_adjust_by.items() or None
382
+ dy_device = dy_adjust_by and dy_adjust_by.items() or None
383
+
384
+ return ast.ValueRecord(
385
+ xPlacement=dx,
386
+ yPlacement=dy,
387
+ xAdvance=adv,
388
+ xPlaDevice=dx_device,
389
+ yPlaDevice=dy_device,
390
+ xAdvDevice=adv_device,
391
+ )
392
+
393
+ def _anchor(self, adjustment):
394
+ adv, dx, dy, adv_adjust_by, dx_adjust_by, dy_adjust_by = adjustment
395
+
396
+ assert not adv_adjust_by
397
+ dx_device = dx_adjust_by and dx_adjust_by.items() or None
398
+ dy_device = dy_adjust_by and dy_adjust_by.items() or None
399
+
400
+ return ast.Anchor(
401
+ dx or 0,
402
+ dy or 0,
403
+ xDeviceTable=dx_device or None,
404
+ yDeviceTable=dy_device or None,
405
+ )
406
+
407
+ def _anchorDefinition(self, anchordef):
408
+ anchorname = anchordef.name
409
+ glyphname = anchordef.glyph_name
410
+ anchor = self._anchor(anchordef.pos)
411
+
412
+ if glyphname not in self._anchors:
413
+ self._anchors[glyphname] = {}
414
+ if anchorname.startswith("MARK_"):
415
+ anchorname = anchorname[:5] + anchorname[5:].lower()
416
+ else:
417
+ anchorname = anchorname.lower()
418
+ if anchorname not in self._anchors[glyphname]:
419
+ self._anchors[glyphname][anchorname] = {}
420
+ self._anchors[glyphname][anchorname][anchordef.component] = anchor
421
+
422
+ def _gposLookup(self, lookup, fealookup):
423
+ statements = fealookup.statements
424
+
425
+ pos = lookup.pos
426
+ if isinstance(pos, VAst.PositionAdjustPairDefinition):
427
+ for (idx1, idx2), (pos1, pos2) in pos.adjust_pair.items():
428
+ coverage_1 = pos.coverages_1[idx1 - 1]
429
+ coverage_2 = pos.coverages_2[idx2 - 1]
430
+
431
+ # If not both are groups, use “enum pos” otherwise makeotf will
432
+ # fail.
433
+ enumerated = False
434
+ for item in coverage_1 + coverage_2:
435
+ if not isinstance(item, VAst.GroupName):
436
+ enumerated = True
437
+
438
+ glyphs1 = self._coverage(coverage_1)
439
+ glyphs2 = self._coverage(coverage_2)
440
+ record1 = self._adjustment(pos1)
441
+ record2 = self._adjustment(pos2)
442
+ assert len(glyphs1) == 1
443
+ assert len(glyphs2) == 1
444
+ statements.append(
445
+ ast.PairPosStatement(
446
+ glyphs1[0], record1, glyphs2[0], record2, enumerated=enumerated
447
+ )
448
+ )
449
+ elif isinstance(pos, VAst.PositionAdjustSingleDefinition):
450
+ for a, b in pos.adjust_single:
451
+ glyphs = self._coverage(a)
452
+ record = self._adjustment(b)
453
+ assert len(glyphs) == 1
454
+ statements.append(
455
+ ast.SinglePosStatement([(glyphs[0], record)], [], [], False)
456
+ )
457
+ elif isinstance(pos, VAst.PositionAttachDefinition):
458
+ anchors = {}
459
+ allmarks = set()
460
+ for coverage, anchorname in pos.coverage_to:
461
+ # In feature files mark classes are global, but in VOLT they
462
+ # are defined per-lookup. If we output mark class definitions
463
+ # for all marks that use a given anchor, we might end up with a
464
+ # mark used in two different classes in the same lookup, which
465
+ # is causes feature file compilation error.
466
+ # At the expense of uglier feature code, we make the mark class
467
+ # name by appending the current lookup name not the anchor
468
+ # name, and output mark class definitions only for marks used
469
+ # in this lookup.
470
+ classname = self._className(f"{anchorname}.{lookup.name}")
471
+ markclass = ast.MarkClass(classname)
472
+
473
+ # Anchor names are case-insensitive in VOLT
474
+ anchorname = anchorname.lower()
475
+
476
+ # We might still end in marks used in two different anchor
477
+ # classes, so we filter out already used marks.
478
+ marks = set()
479
+ for mark in coverage:
480
+ marks.update(mark.glyphSet())
481
+ if not marks.isdisjoint(allmarks):
482
+ marks.difference_update(allmarks)
483
+ if not marks:
484
+ continue
485
+ allmarks.update(marks)
486
+
487
+ for glyphname in marks:
488
+ glyph = self._glyphName(glyphname)
489
+ anchor = self._anchors[glyphname][f"MARK_{anchorname}"][1]
490
+ markdef = ast.MarkClassDefinition(markclass, anchor, glyph)
491
+ self._markclasses[(glyphname, classname)] = markdef
492
+
493
+ for base in pos.coverage:
494
+ for name in base.glyphSet():
495
+ if name not in anchors:
496
+ anchors[name] = []
497
+ if (anchorname, classname) not in anchors[name]:
498
+ anchors[name].append((anchorname, classname))
499
+
500
+ is_ligature = all(n in self._ligatures for n in anchors)
501
+ is_mark = all(n in self._marks for n in anchors)
502
+ for name in anchors:
503
+ components = 1
504
+ if is_ligature:
505
+ components = self._ligatures[name]
506
+
507
+ marks = [[] for _ in range(components)]
508
+ for mark, classname in anchors[name]:
509
+ markclass = ast.MarkClass(classname)
510
+ for component in range(1, components + 1):
511
+ if component in self._anchors[name][mark]:
512
+ anchor = self._anchors[name][mark][component]
513
+ marks[component - 1].append((anchor, markclass))
514
+
515
+ base = self._glyphName(name)
516
+ if is_mark:
517
+ mark = ast.MarkMarkPosStatement(base, marks[0])
518
+ elif is_ligature:
519
+ mark = ast.MarkLigPosStatement(base, marks)
520
+ else:
521
+ mark = ast.MarkBasePosStatement(base, marks[0])
522
+ statements.append(mark)
523
+ elif isinstance(pos, VAst.PositionAttachCursiveDefinition):
524
+ # Collect enter and exit glyphs
525
+ enter_coverage = []
526
+ for coverage in pos.coverages_enter:
527
+ for base in coverage:
528
+ for name in base.glyphSet():
529
+ enter_coverage.append(name)
530
+ exit_coverage = []
531
+ for coverage in pos.coverages_exit:
532
+ for base in coverage:
533
+ for name in base.glyphSet():
534
+ exit_coverage.append(name)
535
+
536
+ # Write enter anchors, also check if the glyph has exit anchor and
537
+ # write it, too.
538
+ for name in enter_coverage:
539
+ glyph = self._glyphName(name)
540
+ entry = self._anchors[name]["entry"][1]
541
+ exit = None
542
+ if name in exit_coverage:
543
+ exit = self._anchors[name]["exit"][1]
544
+ exit_coverage.pop(exit_coverage.index(name))
545
+ statements.append(ast.CursivePosStatement(glyph, entry, exit))
546
+
547
+ # Write any remaining exit anchors.
548
+ for name in exit_coverage:
549
+ glyph = self._glyphName(name)
550
+ exit = self._anchors[name]["exit"][1]
551
+ statements.append(ast.CursivePosStatement(glyph, None, exit))
552
+ else:
553
+ raise NotImplementedError(pos)
554
+
555
+ def _gposContextLookup(self, lookup, prefix, suffix, ignore, fealookup, chained):
556
+ statements = fealookup.statements
557
+
558
+ pos = lookup.pos
559
+ if isinstance(pos, VAst.PositionAdjustPairDefinition):
560
+ for (idx1, idx2), (pos1, pos2) in pos.adjust_pair.items():
561
+ glyphs1 = self._coverage(pos.coverages_1[idx1 - 1])
562
+ glyphs2 = self._coverage(pos.coverages_2[idx2 - 1])
563
+ assert len(glyphs1) == 1
564
+ assert len(glyphs2) == 1
565
+ glyphs = (glyphs1[0], glyphs2[0])
566
+
567
+ if ignore:
568
+ statement = ast.IgnorePosStatement([(prefix, glyphs, suffix)])
569
+ else:
570
+ statement = ast.ChainContextPosStatement(
571
+ prefix, glyphs, suffix, [chained, chained]
572
+ )
573
+ statements.append(statement)
574
+ elif isinstance(pos, VAst.PositionAdjustSingleDefinition):
575
+ glyphs = [ast.GlyphClass()]
576
+ for a, _ in pos.adjust_single:
577
+ glyphs[0].extend(self._coverage(a, flatten=True))
578
+
579
+ if ignore:
580
+ statement = ast.IgnorePosStatement([(prefix, glyphs, suffix)])
581
+ else:
582
+ statement = ast.ChainContextPosStatement(
583
+ prefix, glyphs, suffix, [chained]
584
+ )
585
+ statements.append(statement)
586
+ elif isinstance(pos, VAst.PositionAttachDefinition):
587
+ glyphs = [ast.GlyphClass()]
588
+ for coverage, _ in pos.coverage_to:
589
+ glyphs[0].extend(self._coverage(coverage, flatten=True))
590
+
591
+ if ignore:
592
+ statement = ast.IgnorePosStatement([(prefix, glyphs, suffix)])
593
+ else:
594
+ statement = ast.ChainContextPosStatement(
595
+ prefix, glyphs, suffix, [chained]
596
+ )
597
+ statements.append(statement)
598
+ else:
599
+ raise NotImplementedError(pos)
600
+
601
+ def _gsubLookup(self, lookup, fealookup):
602
+ statements = fealookup.statements
603
+
604
+ sub = lookup.sub
605
+
606
+ # Alternate substitutions are represented by adding multiple
607
+ # substitutions for the same glyph, so we need to collect them into one
608
+ # to many mapping.
609
+ if isinstance(sub, VAst.SubstitutionAlternateDefinition):
610
+ alternates = {}
611
+ for key, val in sub.mapping.items():
612
+ if not key or not val:
613
+ path, line, column = sub.location
614
+ log.warning(f"{path}:{line}:{column}: Ignoring empty substitution")
615
+ continue
616
+ glyphs = self._coverage(key)
617
+ replacements = self._coverage(val)
618
+ assert len(glyphs) == 1
619
+ for src_glyph, repl_glyph in zip(
620
+ glyphs[0].glyphSet(), replacements[0].glyphSet()
621
+ ):
622
+ alternates.setdefault(str(self._glyphName(src_glyph)), []).append(
623
+ str(self._glyphName(repl_glyph))
624
+ )
625
+
626
+ for glyph, replacements in alternates.items():
627
+ statement = ast.AlternateSubstStatement(
628
+ [], glyph, [], ast.GlyphClass(replacements)
629
+ )
630
+ statements.append(statement)
631
+ return
632
+
633
+ for key, val in sub.mapping.items():
634
+ if not key or not val:
635
+ path, line, column = sub.location
636
+ log.warning(f"{path}:{line}:{column}: Ignoring empty substitution")
637
+ continue
638
+ glyphs = self._coverage(key)
639
+ replacements = self._coverage(val)
640
+ if isinstance(sub, VAst.SubstitutionSingleDefinition):
641
+ assert len(glyphs) == 1
642
+ assert len(replacements) == 1
643
+ statements.append(
644
+ ast.SingleSubstStatement(glyphs, replacements, [], [], False)
645
+ )
646
+ elif isinstance(sub, VAst.SubstitutionReverseChainingSingleDefinition):
647
+ # This is handled in gsubContextLookup()
648
+ pass
649
+ elif isinstance(sub, VAst.SubstitutionMultipleDefinition):
650
+ assert len(glyphs) == 1
651
+ statements.append(
652
+ ast.MultipleSubstStatement([], glyphs[0], [], replacements)
653
+ )
654
+ elif isinstance(sub, VAst.SubstitutionLigatureDefinition):
655
+ assert len(replacements) == 1
656
+ statement = ast.LigatureSubstStatement(
657
+ [], glyphs, [], replacements[0], False
658
+ )
659
+
660
+ # If any of the input glyphs is a group, we need to
661
+ # explode the substitution into multiple ligature substitutions
662
+ # since feature file syntax does not support classes in
663
+ # ligature substitutions.
664
+ n = max(len(x.glyphSet()) for x in glyphs)
665
+ if n > 1:
666
+ # All input should either be groups of the same length or single glyphs
667
+ assert all(len(x.glyphSet()) in (n, 1) for x in glyphs)
668
+ glyphs = [x.glyphSet() for x in glyphs]
669
+ glyphs = [([x[0]] * n if len(x) == 1 else x) for x in glyphs]
670
+
671
+ # In this case ligature replacements must be a group of the same length
672
+ # as the input groups, or a single glyph. VOLT
673
+ # allows the replacement glyphs to be longer and truncates them.
674
+ # So well allow that and zip() below will do the truncation
675
+ # for us.
676
+ replacement = replacements[0].glyphSet()
677
+ if len(replacement) == 1:
678
+ replacement = [replacement[0]] * n
679
+ assert len(replacement) >= n
680
+
681
+ # Add the unexploded statement commented out for reference.
682
+ statements.append(ast.Comment(f"# {statement}"))
683
+
684
+ for zipped in zip(*glyphs, replacement):
685
+ zipped = [self._glyphName(x) for x in zipped]
686
+ statements.append(
687
+ ast.LigatureSubstStatement(
688
+ [], zipped[:-1], [], zipped[-1], False
689
+ )
690
+ )
691
+ else:
692
+ statements.append(statement)
693
+ else:
694
+ raise NotImplementedError(sub)
695
+
696
+ def _gsubContextLookup(self, lookup, prefix, suffix, ignore, fealookup, chained):
697
+ statements = fealookup.statements
698
+
699
+ sub = lookup.sub
700
+
701
+ if isinstance(sub, VAst.SubstitutionReverseChainingSingleDefinition):
702
+ # Reverse substitutions is a special case, it can’t use chained lookups.
703
+ for key, val in sub.mapping.items():
704
+ if not key or not val:
705
+ path, line, column = sub.location
706
+ log.warning(f"{path}:{line}:{column}: Ignoring empty substitution")
707
+ continue
708
+ glyphs = self._coverage(key)
709
+ replacements = self._coverage(val)
710
+ statements.append(
711
+ ast.ReverseChainSingleSubstStatement(
712
+ prefix, suffix, glyphs, replacements
713
+ )
714
+ )
715
+ fealookup.chained = []
716
+ return
717
+
718
+ if not isinstance(
719
+ sub,
720
+ (
721
+ VAst.SubstitutionSingleDefinition,
722
+ VAst.SubstitutionMultipleDefinition,
723
+ VAst.SubstitutionLigatureDefinition,
724
+ VAst.SubstitutionAlternateDefinition,
725
+ ),
726
+ ):
727
+ raise NotImplementedError(type(sub))
728
+
729
+ glyphs = []
730
+ for key, val in sub.mapping.items():
731
+ if not key or not val:
732
+ path, line, column = sub.location
733
+ log.warning(f"{path}:{line}:{column}: Ignoring empty substitution")
734
+ continue
735
+ glyphs.extend(self._coverage(key, flatten=True))
736
+
737
+ if len(glyphs) > 1:
738
+ glyphs = [ast.GlyphClass(glyphs)]
739
+ if ignore:
740
+ statements.append(ast.IgnoreSubstStatement([(prefix, glyphs, suffix)]))
741
+ else:
742
+ statements.append(
743
+ ast.ChainContextSubstStatement(prefix, glyphs, suffix, [chained])
744
+ )
745
+
746
+ def _lookupDefinition(self, lookup):
747
+ mark_attachement = None
748
+ mark_filtering = None
749
+
750
+ flags = 0
751
+ if lookup.direction == "RTL":
752
+ flags |= 1
753
+ if not lookup.process_base:
754
+ flags |= 2
755
+ # FIXME: Does VOLT support this?
756
+ # if not lookup.process_ligatures:
757
+ # flags |= 4
758
+ if not lookup.process_marks:
759
+ flags |= 8
760
+ elif isinstance(lookup.process_marks, str):
761
+ mark_attachement = self._groupName(lookup.process_marks)
762
+ elif lookup.mark_glyph_set is not None:
763
+ mark_filtering = self._groupName(lookup.mark_glyph_set)
764
+
765
+ lookupflags = None
766
+ if flags or mark_attachement is not None or mark_filtering is not None:
767
+ lookupflags = ast.LookupFlagStatement(
768
+ flags, mark_attachement, mark_filtering
769
+ )
770
+
771
+ use_extension = False
772
+ if self._settings.get("COMPILER_USEEXTENSIONLOOKUPS"):
773
+ use_extension = True
774
+
775
+ if "\\" in lookup.name:
776
+ # Merge sub lookups as subtables (lookups named “base\sub”),
777
+ # makeotf/feaLib will issue a warning and ignore the subtable
778
+ # statement if it is not a pairpos lookup, though.
779
+ name = lookup.name.split("\\")[0]
780
+ if name.lower() not in self._lookups:
781
+ fealookup = Lookup(
782
+ self._lookupName(name),
783
+ use_extension=use_extension,
784
+ )
785
+ if lookupflags is not None:
786
+ fealookup.statements.append(lookupflags)
787
+ fealookup.statements.append(ast.Comment("# " + lookup.name))
788
+ else:
789
+ fealookup = self._lookups[name.lower()]
790
+ fealookup.statements.append(ast.SubtableStatement())
791
+ fealookup.statements.append(ast.Comment("# " + lookup.name))
792
+ self._lookups[name.lower()] = fealookup
793
+ else:
794
+ fealookup = Lookup(
795
+ self._lookupName(lookup.name),
796
+ use_extension=use_extension,
797
+ )
798
+ if lookupflags is not None:
799
+ fealookup.statements.append(lookupflags)
800
+ self._lookups[lookup.name.lower()] = fealookup
801
+
802
+ if lookup.comments is not None:
803
+ fealookup.statements.append(ast.Comment("# " + lookup.comments))
804
+
805
+ contexts = []
806
+ for context in lookup.context:
807
+ prefix = self._context(context.left)
808
+ suffix = self._context(context.right)
809
+ ignore = context.ex_or_in == "EXCEPT_CONTEXT"
810
+ contexts.append([prefix, suffix, ignore])
811
+ # It seems that VOLT will create contextual substitution using
812
+ # only the input if there is no other contexts in this lookup.
813
+ if ignore and len(lookup.context) == 1:
814
+ contexts.append([[], [], False])
815
+
816
+ if contexts:
817
+ chained = ast.LookupBlock(
818
+ self._lookupName(lookup.name + " chained"),
819
+ use_extension=use_extension,
820
+ )
821
+ fealookup.chained.append(chained)
822
+ if lookup.sub is not None:
823
+ self._gsubLookup(lookup, chained)
824
+ elif lookup.pos is not None:
825
+ self._gposLookup(lookup, chained)
826
+ for prefix, suffix, ignore in contexts:
827
+ if lookup.sub is not None:
828
+ self._gsubContextLookup(
829
+ lookup, prefix, suffix, ignore, fealookup, chained
830
+ )
831
+ elif lookup.pos is not None:
832
+ self._gposContextLookup(
833
+ lookup, prefix, suffix, ignore, fealookup, chained
834
+ )
835
+ else:
836
+ if lookup.sub is not None:
837
+ self._gsubLookup(lookup, fealookup)
838
+ elif lookup.pos is not None:
839
+ self._gposLookup(lookup, fealookup)
840
+
841
+
842
+ def main(args=None):
843
+ """Convert MS VOLT to AFDKO feature files."""
844
+
845
+ import argparse
846
+ from pathlib import Path
847
+
848
+ from fontTools import configLogger
849
+
850
+ parser = argparse.ArgumentParser(
851
+ "fonttools voltLib.voltToFea", description=main.__doc__
852
+ )
853
+ parser.add_argument(
854
+ "input", metavar="INPUT", type=Path, help="input font/VTP file to process"
855
+ )
856
+ parser.add_argument(
857
+ "featurefile", metavar="OUTPUT", type=Path, help="output feature file"
858
+ )
859
+ parser.add_argument(
860
+ "-t",
861
+ "--table",
862
+ action="append",
863
+ choices=TABLES,
864
+ dest="tables",
865
+ help="List of tables to write, by default all tables are written",
866
+ )
867
+ parser.add_argument(
868
+ "-q", "--quiet", action="store_true", help="Suppress non-error messages"
869
+ )
870
+ parser.add_argument(
871
+ "--traceback", action="store_true", help="Don’t catch exceptions"
872
+ )
873
+
874
+ options = parser.parse_args(args)
875
+
876
+ configLogger(level=("ERROR" if options.quiet else "INFO"))
877
+
878
+ file_or_path = options.input
879
+ font = None
880
+ try:
881
+ font = TTFont(file_or_path)
882
+ if "TSIV" in font:
883
+ file_or_path = StringIO(font["TSIV"].data.decode("utf-8"))
884
+ else:
885
+ log.error('"TSIV" table is missing, font was not saved from VOLT?')
886
+ return 1
887
+ except TTLibError:
888
+ pass
889
+
890
+ converter = VoltToFea(file_or_path, font)
891
+ try:
892
+ fea = converter.convert(options.tables)
893
+ except NotImplementedError as e:
894
+ if options.traceback:
895
+ raise
896
+ location = getattr(e.args[0], "location", None)
897
+ message = f'"{e}" is not supported'
898
+ if location:
899
+ path, line, column = location
900
+ log.error(f"{path}:{line}:{column}: {message}")
901
+ else:
902
+ log.error(message)
903
+ return 1
904
+ with open(options.featurefile, "w") as feafile:
905
+ feafile.write(fea)
906
+
907
+
908
+ if __name__ == "__main__":
909
+ import sys
910
+
911
+ sys.exit(main())