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,1148 @@
1
+ from fontTools.config import Config
2
+ from fontTools.misc import xmlWriter
3
+ from fontTools.misc.configTools import AbstractConfig
4
+ from fontTools.misc.textTools import Tag, byteord, tostr
5
+ from fontTools.misc.loggingTools import deprecateArgument
6
+ from fontTools.ttLib import TTLibError
7
+ from fontTools.ttLib.ttGlyphSet import (
8
+ _TTGlyph,
9
+ _TTGlyphSetCFF,
10
+ _TTGlyphSetGlyf,
11
+ _TTGlyphSetVARC,
12
+ )
13
+ from fontTools.ttLib.sfnt import SFNTReader, SFNTWriter
14
+ from io import BytesIO, StringIO, UnsupportedOperation
15
+ import os
16
+ import logging
17
+ import traceback
18
+
19
+ log = logging.getLogger(__name__)
20
+
21
+
22
+ class TTFont(object):
23
+ """Represents a TrueType font.
24
+
25
+ The object manages file input and output, and offers a convenient way of
26
+ accessing tables. Tables will be only decompiled when necessary, ie. when
27
+ they're actually accessed. This means that simple operations can be extremely fast.
28
+
29
+ Example usage:
30
+
31
+ .. code-block:: pycon
32
+
33
+ >>>
34
+ >> from fontTools import ttLib
35
+ >> tt = ttLib.TTFont("afont.ttf") # Load an existing font file
36
+ >> tt['maxp'].numGlyphs
37
+ 242
38
+ >> tt['OS/2'].achVendID
39
+ 'B&H\000'
40
+ >> tt['head'].unitsPerEm
41
+ 2048
42
+
43
+ For details of the objects returned when accessing each table, see the
44
+ :doc:`tables </ttLib/tables>` documentation.
45
+ To add a table to the font, use the :py:func:`newTable` function:
46
+
47
+ .. code-block:: pycon
48
+
49
+ >>>
50
+ >> os2 = newTable("OS/2")
51
+ >> os2.version = 4
52
+ >> # set other attributes
53
+ >> font["OS/2"] = os2
54
+
55
+ TrueType fonts can also be serialized to and from XML format (see also the
56
+ :doc:`ttx </ttx>` binary):
57
+
58
+ .. code-block:: pycon
59
+
60
+ >>
61
+ >> tt.saveXML("afont.ttx")
62
+ Dumping 'LTSH' table...
63
+ Dumping 'OS/2' table...
64
+ [...]
65
+
66
+ >> tt2 = ttLib.TTFont() # Create a new font object
67
+ >> tt2.importXML("afont.ttx")
68
+ >> tt2['maxp'].numGlyphs
69
+ 242
70
+
71
+ The TTFont object may be used as a context manager; this will cause the file
72
+ reader to be closed after the context ``with`` block is exited::
73
+
74
+ with TTFont(filename) as f:
75
+ # Do stuff
76
+
77
+ Args:
78
+ file: When reading a font from disk, either a pathname pointing to a file,
79
+ or a readable file object.
80
+ res_name_or_index: If running on a Macintosh, either a sfnt resource name or
81
+ an sfnt resource index number. If the index number is zero, TTLib will
82
+ autodetect whether the file is a flat file or a suitcase. (If it is a suitcase,
83
+ only the first 'sfnt' resource will be read.)
84
+ sfntVersion (str): When constructing a font object from scratch, sets the four-byte
85
+ sfnt magic number to be used. Defaults to ``\0\1\0\0`` (TrueType). To create
86
+ an OpenType file, use ``OTTO``.
87
+ flavor (str): Set this to ``woff`` when creating a WOFF file or ``woff2`` for a WOFF2
88
+ file.
89
+ checkChecksums (int): How checksum data should be treated. Default is 0
90
+ (no checking). Set to 1 to check and warn on wrong checksums; set to 2 to
91
+ raise an exception if any wrong checksums are found.
92
+ recalcBBoxes (bool): If true (the default), recalculates ``glyf``, ``CFF ``,
93
+ ``head`` bounding box values and ``hhea``/``vhea`` min/max values on save.
94
+ Also compiles the glyphs on importing, which saves memory consumption and
95
+ time.
96
+ ignoreDecompileErrors (bool): If true, exceptions raised during table decompilation
97
+ will be ignored, and the binary data will be returned for those tables instead.
98
+ recalcTimestamp (bool): If true (the default), sets the ``modified`` timestamp in
99
+ the ``head`` table on save.
100
+ fontNumber (int): The index of the font in a TrueType Collection file.
101
+ lazy (bool): If lazy is set to True, many data structures are loaded lazily, upon
102
+ access only. If it is set to False, many data structures are loaded immediately.
103
+ The default is ``lazy=None`` which is somewhere in between.
104
+ """
105
+
106
+ def __init__(
107
+ self,
108
+ file=None,
109
+ res_name_or_index=None,
110
+ sfntVersion="\000\001\000\000",
111
+ flavor=None,
112
+ checkChecksums=0,
113
+ verbose=None,
114
+ recalcBBoxes=True,
115
+ allowVID=NotImplemented,
116
+ ignoreDecompileErrors=False,
117
+ recalcTimestamp=True,
118
+ fontNumber=-1,
119
+ lazy=None,
120
+ quiet=None,
121
+ _tableCache=None,
122
+ cfg={},
123
+ ):
124
+ for name in ("verbose", "quiet"):
125
+ val = locals().get(name)
126
+ if val is not None:
127
+ deprecateArgument(name, "configure logging instead")
128
+ setattr(self, name, val)
129
+
130
+ self.lazy = lazy
131
+ self.recalcBBoxes = recalcBBoxes
132
+ self.recalcTimestamp = recalcTimestamp
133
+ self.tables = {}
134
+ self.reader = None
135
+ self.cfg = cfg.copy() if isinstance(cfg, AbstractConfig) else Config(cfg)
136
+ self.ignoreDecompileErrors = ignoreDecompileErrors
137
+
138
+ if not file:
139
+ self.sfntVersion = sfntVersion
140
+ self.flavor = flavor
141
+ self.flavorData = None
142
+ return
143
+ seekable = True
144
+ if not hasattr(file, "read"):
145
+ closeStream = True
146
+ # assume file is a string
147
+ if res_name_or_index is not None:
148
+ # see if it contains 'sfnt' resources in the resource or data fork
149
+ from . import macUtils
150
+
151
+ if res_name_or_index == 0:
152
+ if macUtils.getSFNTResIndices(file):
153
+ # get the first available sfnt font.
154
+ file = macUtils.SFNTResourceReader(file, 1)
155
+ else:
156
+ file = open(file, "rb")
157
+ else:
158
+ file = macUtils.SFNTResourceReader(file, res_name_or_index)
159
+ else:
160
+ file = open(file, "rb")
161
+ else:
162
+ # assume "file" is a readable file object
163
+ closeStream = False
164
+ # SFNTReader wants the input file to be seekable.
165
+ # SpooledTemporaryFile has no seekable() on < 3.11, but still can seek:
166
+ # https://github.com/fonttools/fonttools/issues/3052
167
+ if hasattr(file, "seekable"):
168
+ seekable = file.seekable()
169
+ elif hasattr(file, "seek"):
170
+ try:
171
+ file.seek(0)
172
+ except UnsupportedOperation:
173
+ seekable = False
174
+
175
+ if not self.lazy:
176
+ # read input file in memory and wrap a stream around it to allow overwriting
177
+ if seekable:
178
+ file.seek(0)
179
+ tmp = BytesIO(file.read())
180
+ if hasattr(file, "name"):
181
+ # save reference to input file name
182
+ tmp.name = file.name
183
+ if closeStream:
184
+ file.close()
185
+ file = tmp
186
+ elif not seekable:
187
+ raise TTLibError("Input file must be seekable when lazy=True")
188
+ self._tableCache = _tableCache
189
+ self.reader = SFNTReader(file, checkChecksums, fontNumber=fontNumber)
190
+ self.sfntVersion = self.reader.sfntVersion
191
+ self.flavor = self.reader.flavor
192
+ self.flavorData = self.reader.flavorData
193
+
194
+ def __enter__(self):
195
+ return self
196
+
197
+ def __exit__(self, type, value, traceback):
198
+ self.close()
199
+
200
+ def close(self):
201
+ """If we still have a reader object, close it."""
202
+ if self.reader is not None:
203
+ self.reader.close()
204
+
205
+ def save(self, file, reorderTables=True):
206
+ """Save the font to disk.
207
+
208
+ Args:
209
+ file: Similarly to the constructor, can be either a pathname or a writable
210
+ file object.
211
+ reorderTables (Option[bool]): If true (the default), reorder the tables,
212
+ sorting them by tag (recommended by the OpenType specification). If
213
+ false, retain the original font order. If None, reorder by table
214
+ dependency (fastest).
215
+ """
216
+ if not hasattr(file, "write"):
217
+ if self.lazy and self.reader.file.name == file:
218
+ raise TTLibError("Can't overwrite TTFont when 'lazy' attribute is True")
219
+ createStream = True
220
+ else:
221
+ # assume "file" is a writable file object
222
+ createStream = False
223
+
224
+ tmp = BytesIO()
225
+
226
+ writer_reordersTables = self._save(tmp)
227
+
228
+ if not (
229
+ reorderTables is None
230
+ or writer_reordersTables
231
+ or (reorderTables is False and self.reader is None)
232
+ ):
233
+ if reorderTables is False:
234
+ # sort tables using the original font's order
235
+ tableOrder = list(self.reader.keys())
236
+ else:
237
+ # use the recommended order from the OpenType specification
238
+ tableOrder = None
239
+ tmp.flush()
240
+ tmp2 = BytesIO()
241
+ reorderFontTables(tmp, tmp2, tableOrder)
242
+ tmp.close()
243
+ tmp = tmp2
244
+
245
+ if createStream:
246
+ # "file" is a path
247
+ with open(file, "wb") as file:
248
+ file.write(tmp.getvalue())
249
+ else:
250
+ file.write(tmp.getvalue())
251
+
252
+ tmp.close()
253
+
254
+ def _save(self, file, tableCache=None):
255
+ """Internal function, to be shared by save() and TTCollection.save()"""
256
+
257
+ if self.recalcTimestamp and "head" in self:
258
+ self[
259
+ "head"
260
+ ] # make sure 'head' is loaded so the recalculation is actually done
261
+
262
+ tags = self.keys()
263
+ tags.pop(0) # skip GlyphOrder tag
264
+ numTables = len(tags)
265
+ # write to a temporary stream to allow saving to unseekable streams
266
+ writer = SFNTWriter(
267
+ file, numTables, self.sfntVersion, self.flavor, self.flavorData
268
+ )
269
+
270
+ done = []
271
+ for tag in tags:
272
+ self._writeTable(tag, writer, done, tableCache)
273
+
274
+ writer.close()
275
+
276
+ return writer.reordersTables()
277
+
278
+ def saveXML(self, fileOrPath, newlinestr="\n", **kwargs):
279
+ """Export the font as TTX (an XML-based text file), or as a series of text
280
+ files when splitTables is true. In the latter case, the 'fileOrPath'
281
+ argument should be a path to a directory.
282
+ The 'tables' argument must either be false (dump all tables) or a
283
+ list of tables to dump. The 'skipTables' argument may be a list of tables
284
+ to skip, but only when the 'tables' argument is false.
285
+ """
286
+
287
+ writer = xmlWriter.XMLWriter(fileOrPath, newlinestr=newlinestr)
288
+ self._saveXML(writer, **kwargs)
289
+ writer.close()
290
+
291
+ def _saveXML(
292
+ self,
293
+ writer,
294
+ writeVersion=True,
295
+ quiet=None,
296
+ tables=None,
297
+ skipTables=None,
298
+ splitTables=False,
299
+ splitGlyphs=False,
300
+ disassembleInstructions=True,
301
+ bitmapGlyphDataFormat="raw",
302
+ ):
303
+ if quiet is not None:
304
+ deprecateArgument("quiet", "configure logging instead")
305
+
306
+ self.disassembleInstructions = disassembleInstructions
307
+ self.bitmapGlyphDataFormat = bitmapGlyphDataFormat
308
+ if not tables:
309
+ tables = self.keys()
310
+ if skipTables:
311
+ tables = [tag for tag in tables if tag not in skipTables]
312
+
313
+ if writeVersion:
314
+ from fontTools import version
315
+
316
+ version = ".".join(version.split(".")[:2])
317
+ writer.begintag(
318
+ "ttFont",
319
+ sfntVersion=repr(tostr(self.sfntVersion))[1:-1],
320
+ ttLibVersion=version,
321
+ )
322
+ else:
323
+ writer.begintag("ttFont", sfntVersion=repr(tostr(self.sfntVersion))[1:-1])
324
+ writer.newline()
325
+
326
+ # always splitTables if splitGlyphs is enabled
327
+ splitTables = splitTables or splitGlyphs
328
+
329
+ if not splitTables:
330
+ writer.newline()
331
+ else:
332
+ path, ext = os.path.splitext(writer.filename)
333
+
334
+ for tag in tables:
335
+ if splitTables:
336
+ tablePath = path + "." + tagToIdentifier(tag) + ext
337
+ tableWriter = xmlWriter.XMLWriter(
338
+ tablePath, newlinestr=writer.newlinestr
339
+ )
340
+ tableWriter.begintag("ttFont", ttLibVersion=version)
341
+ tableWriter.newline()
342
+ tableWriter.newline()
343
+ writer.simpletag(tagToXML(tag), src=os.path.basename(tablePath))
344
+ writer.newline()
345
+ else:
346
+ tableWriter = writer
347
+ self._tableToXML(tableWriter, tag, splitGlyphs=splitGlyphs)
348
+ if splitTables:
349
+ tableWriter.endtag("ttFont")
350
+ tableWriter.newline()
351
+ tableWriter.close()
352
+ writer.endtag("ttFont")
353
+ writer.newline()
354
+
355
+ def _tableToXML(self, writer, tag, quiet=None, splitGlyphs=False):
356
+ if quiet is not None:
357
+ deprecateArgument("quiet", "configure logging instead")
358
+ if tag in self:
359
+ table = self[tag]
360
+ report = "Dumping '%s' table..." % tag
361
+ else:
362
+ report = "No '%s' table found." % tag
363
+ log.info(report)
364
+ if tag not in self:
365
+ return
366
+ xmlTag = tagToXML(tag)
367
+ attrs = dict()
368
+ if hasattr(table, "ERROR"):
369
+ attrs["ERROR"] = "decompilation error"
370
+ from .tables.DefaultTable import DefaultTable
371
+
372
+ if table.__class__ == DefaultTable:
373
+ attrs["raw"] = True
374
+ writer.begintag(xmlTag, **attrs)
375
+ writer.newline()
376
+ if tag == "glyf":
377
+ table.toXML(writer, self, splitGlyphs=splitGlyphs)
378
+ else:
379
+ table.toXML(writer, self)
380
+ writer.endtag(xmlTag)
381
+ writer.newline()
382
+ writer.newline()
383
+
384
+ def importXML(self, fileOrPath, quiet=None):
385
+ """Import a TTX file (an XML-based text format), so as to recreate
386
+ a font object.
387
+ """
388
+ if quiet is not None:
389
+ deprecateArgument("quiet", "configure logging instead")
390
+
391
+ if "maxp" in self and "post" in self:
392
+ # Make sure the glyph order is loaded, as it otherwise gets
393
+ # lost if the XML doesn't contain the glyph order, yet does
394
+ # contain the table which was originally used to extract the
395
+ # glyph names from (ie. 'post', 'cmap' or 'CFF ').
396
+ self.getGlyphOrder()
397
+
398
+ from fontTools.misc import xmlReader
399
+
400
+ reader = xmlReader.XMLReader(fileOrPath, self)
401
+ reader.read()
402
+
403
+ def isLoaded(self, tag):
404
+ """Return true if the table identified by ``tag`` has been
405
+ decompiled and loaded into memory."""
406
+ return tag in self.tables
407
+
408
+ def has_key(self, tag):
409
+ """Test if the table identified by ``tag`` is present in the font.
410
+
411
+ As well as this method, ``tag in font`` can also be used to determine the
412
+ presence of the table."""
413
+ if self.isLoaded(tag):
414
+ return True
415
+ elif self.reader and tag in self.reader:
416
+ return True
417
+ elif tag == "GlyphOrder":
418
+ return True
419
+ else:
420
+ return False
421
+
422
+ __contains__ = has_key
423
+
424
+ def keys(self):
425
+ """Returns the list of tables in the font, along with the ``GlyphOrder`` pseudo-table."""
426
+ keys = list(self.tables.keys())
427
+ if self.reader:
428
+ for key in list(self.reader.keys()):
429
+ if key not in keys:
430
+ keys.append(key)
431
+
432
+ if "GlyphOrder" in keys:
433
+ keys.remove("GlyphOrder")
434
+ keys = sortedTagList(keys)
435
+ return ["GlyphOrder"] + keys
436
+
437
+ def ensureDecompiled(self, recurse=None):
438
+ """Decompile all the tables, even if a TTFont was opened in 'lazy' mode."""
439
+ for tag in self.keys():
440
+ table = self[tag]
441
+ if recurse is None:
442
+ recurse = self.lazy is not False
443
+ if recurse and hasattr(table, "ensureDecompiled"):
444
+ table.ensureDecompiled(recurse=recurse)
445
+ self.lazy = False
446
+
447
+ def __len__(self):
448
+ return len(list(self.keys()))
449
+
450
+ def __getitem__(self, tag):
451
+ tag = Tag(tag)
452
+ table = self.tables.get(tag)
453
+ if table is None:
454
+ if tag == "GlyphOrder":
455
+ table = GlyphOrder(tag)
456
+ self.tables[tag] = table
457
+ elif self.reader is not None:
458
+ table = self._readTable(tag)
459
+ else:
460
+ raise KeyError("'%s' table not found" % tag)
461
+ return table
462
+
463
+ def _readTable(self, tag):
464
+ log.debug("Reading '%s' table from disk", tag)
465
+ data = self.reader[tag]
466
+ if self._tableCache is not None:
467
+ table = self._tableCache.get((tag, data))
468
+ if table is not None:
469
+ return table
470
+ tableClass = getTableClass(tag)
471
+ table = tableClass(tag)
472
+ self.tables[tag] = table
473
+ log.debug("Decompiling '%s' table", tag)
474
+ try:
475
+ table.decompile(data, self)
476
+ except Exception:
477
+ if not self.ignoreDecompileErrors:
478
+ raise
479
+ # fall back to DefaultTable, retaining the binary table data
480
+ log.exception(
481
+ "An exception occurred during the decompilation of the '%s' table", tag
482
+ )
483
+ from .tables.DefaultTable import DefaultTable
484
+
485
+ file = StringIO()
486
+ traceback.print_exc(file=file)
487
+ table = DefaultTable(tag)
488
+ table.ERROR = file.getvalue()
489
+ self.tables[tag] = table
490
+ table.decompile(data, self)
491
+ if self._tableCache is not None:
492
+ self._tableCache[(tag, data)] = table
493
+ return table
494
+
495
+ def __setitem__(self, tag, table):
496
+ self.tables[Tag(tag)] = table
497
+
498
+ def __delitem__(self, tag):
499
+ if tag not in self:
500
+ raise KeyError("'%s' table not found" % tag)
501
+ if tag in self.tables:
502
+ del self.tables[tag]
503
+ if self.reader and tag in self.reader:
504
+ del self.reader[tag]
505
+
506
+ def get(self, tag, default=None):
507
+ """Returns the table if it exists or (optionally) a default if it doesn't."""
508
+ try:
509
+ return self[tag]
510
+ except KeyError:
511
+ return default
512
+
513
+ def setGlyphOrder(self, glyphOrder):
514
+ """Set the glyph order
515
+
516
+ Args:
517
+ glyphOrder ([str]): List of glyph names in order.
518
+ """
519
+ self.glyphOrder = glyphOrder
520
+ if hasattr(self, "_reverseGlyphOrderDict"):
521
+ del self._reverseGlyphOrderDict
522
+ if self.isLoaded("glyf"):
523
+ self["glyf"].setGlyphOrder(glyphOrder)
524
+
525
+ def getGlyphOrder(self):
526
+ """Returns a list of glyph names ordered by their position in the font."""
527
+ try:
528
+ return self.glyphOrder
529
+ except AttributeError:
530
+ pass
531
+ if "CFF " in self:
532
+ cff = self["CFF "]
533
+ self.glyphOrder = cff.getGlyphOrder()
534
+ elif "post" in self:
535
+ # TrueType font
536
+ glyphOrder = self["post"].getGlyphOrder()
537
+ if glyphOrder is None:
538
+ #
539
+ # No names found in the 'post' table.
540
+ # Try to create glyph names from the unicode cmap (if available)
541
+ # in combination with the Adobe Glyph List (AGL).
542
+ #
543
+ self._getGlyphNamesFromCmap()
544
+ elif len(glyphOrder) < self["maxp"].numGlyphs:
545
+ #
546
+ # Not enough names found in the 'post' table.
547
+ # Can happen when 'post' format 1 is improperly used on a font that
548
+ # has more than 258 glyphs (the length of 'standardGlyphOrder').
549
+ #
550
+ log.warning(
551
+ "Not enough names found in the 'post' table, generating them from cmap instead"
552
+ )
553
+ self._getGlyphNamesFromCmap()
554
+ else:
555
+ self.glyphOrder = glyphOrder
556
+ else:
557
+ self._getGlyphNamesFromCmap()
558
+ return self.glyphOrder
559
+
560
+ def _getGlyphNamesFromCmap(self):
561
+ #
562
+ # This is rather convoluted, but then again, it's an interesting problem:
563
+ # - we need to use the unicode values found in the cmap table to
564
+ # build glyph names (eg. because there is only a minimal post table,
565
+ # or none at all).
566
+ # - but the cmap parser also needs glyph names to work with...
567
+ # So here's what we do:
568
+ # - make up glyph names based on glyphID
569
+ # - load a temporary cmap table based on those names
570
+ # - extract the unicode values, build the "real" glyph names
571
+ # - unload the temporary cmap table
572
+ #
573
+ if self.isLoaded("cmap"):
574
+ # Bootstrapping: we're getting called by the cmap parser
575
+ # itself. This means self.tables['cmap'] contains a partially
576
+ # loaded cmap, making it impossible to get at a unicode
577
+ # subtable here. We remove the partially loaded cmap and
578
+ # restore it later.
579
+ # This only happens if the cmap table is loaded before any
580
+ # other table that does f.getGlyphOrder() or f.getGlyphName().
581
+ cmapLoading = self.tables["cmap"]
582
+ del self.tables["cmap"]
583
+ else:
584
+ cmapLoading = None
585
+ # Make up glyph names based on glyphID, which will be used by the
586
+ # temporary cmap and by the real cmap in case we don't find a unicode
587
+ # cmap.
588
+ numGlyphs = int(self["maxp"].numGlyphs)
589
+ glyphOrder = ["glyph%.5d" % i for i in range(numGlyphs)]
590
+ glyphOrder[0] = ".notdef"
591
+ # Set the glyph order, so the cmap parser has something
592
+ # to work with (so we don't get called recursively).
593
+ self.glyphOrder = glyphOrder
594
+
595
+ # Make up glyph names based on the reversed cmap table. Because some
596
+ # glyphs (eg. ligatures or alternates) may not be reachable via cmap,
597
+ # this naming table will usually not cover all glyphs in the font.
598
+ # If the font has no Unicode cmap table, reversecmap will be empty.
599
+ if "cmap" in self:
600
+ reversecmap = self["cmap"].buildReversedMin()
601
+ else:
602
+ reversecmap = {}
603
+ useCount = {}
604
+ for i, tempName in enumerate(glyphOrder):
605
+ if tempName in reversecmap:
606
+ # If a font maps both U+0041 LATIN CAPITAL LETTER A and
607
+ # U+0391 GREEK CAPITAL LETTER ALPHA to the same glyph,
608
+ # we prefer naming the glyph as "A".
609
+ glyphName = self._makeGlyphName(reversecmap[tempName])
610
+ numUses = useCount[glyphName] = useCount.get(glyphName, 0) + 1
611
+ if numUses > 1:
612
+ glyphName = "%s.alt%d" % (glyphName, numUses - 1)
613
+ glyphOrder[i] = glyphName
614
+
615
+ if "cmap" in self:
616
+ # Delete the temporary cmap table from the cache, so it can
617
+ # be parsed again with the right names.
618
+ del self.tables["cmap"]
619
+ self.glyphOrder = glyphOrder
620
+ if cmapLoading:
621
+ # restore partially loaded cmap, so it can continue loading
622
+ # using the proper names.
623
+ self.tables["cmap"] = cmapLoading
624
+
625
+ @staticmethod
626
+ def _makeGlyphName(codepoint):
627
+ from fontTools import agl # Adobe Glyph List
628
+
629
+ if codepoint in agl.UV2AGL:
630
+ return agl.UV2AGL[codepoint]
631
+ elif codepoint <= 0xFFFF:
632
+ return "uni%04X" % codepoint
633
+ else:
634
+ return "u%X" % codepoint
635
+
636
+ def getGlyphNames(self):
637
+ """Get a list of glyph names, sorted alphabetically."""
638
+ glyphNames = sorted(self.getGlyphOrder())
639
+ return glyphNames
640
+
641
+ def getGlyphNames2(self):
642
+ """Get a list of glyph names, sorted alphabetically,
643
+ but not case sensitive.
644
+ """
645
+ from fontTools.misc import textTools
646
+
647
+ return textTools.caselessSort(self.getGlyphOrder())
648
+
649
+ def getGlyphName(self, glyphID):
650
+ """Returns the name for the glyph with the given ID.
651
+
652
+ If no name is available, synthesises one with the form ``glyphXXXXX``` where
653
+ ```XXXXX`` is the zero-padded glyph ID.
654
+ """
655
+ try:
656
+ return self.getGlyphOrder()[glyphID]
657
+ except IndexError:
658
+ return "glyph%.5d" % glyphID
659
+
660
+ def getGlyphNameMany(self, lst):
661
+ """Converts a list of glyph IDs into a list of glyph names."""
662
+ glyphOrder = self.getGlyphOrder()
663
+ cnt = len(glyphOrder)
664
+ return [glyphOrder[gid] if gid < cnt else "glyph%.5d" % gid for gid in lst]
665
+
666
+ def getGlyphID(self, glyphName):
667
+ """Returns the ID of the glyph with the given name."""
668
+ try:
669
+ return self.getReverseGlyphMap()[glyphName]
670
+ except KeyError:
671
+ if glyphName[:5] == "glyph":
672
+ try:
673
+ return int(glyphName[5:])
674
+ except (NameError, ValueError):
675
+ raise KeyError(glyphName)
676
+ raise
677
+
678
+ def getGlyphIDMany(self, lst):
679
+ """Converts a list of glyph names into a list of glyph IDs."""
680
+ d = self.getReverseGlyphMap()
681
+ try:
682
+ return [d[glyphName] for glyphName in lst]
683
+ except KeyError:
684
+ getGlyphID = self.getGlyphID
685
+ return [getGlyphID(glyphName) for glyphName in lst]
686
+
687
+ def getReverseGlyphMap(self, rebuild=False):
688
+ """Returns a mapping of glyph names to glyph IDs."""
689
+ if rebuild or not hasattr(self, "_reverseGlyphOrderDict"):
690
+ self._buildReverseGlyphOrderDict()
691
+ return self._reverseGlyphOrderDict
692
+
693
+ def _buildReverseGlyphOrderDict(self):
694
+ self._reverseGlyphOrderDict = d = {}
695
+ for glyphID, glyphName in enumerate(self.getGlyphOrder()):
696
+ d[glyphName] = glyphID
697
+ return d
698
+
699
+ def _writeTable(self, tag, writer, done, tableCache=None):
700
+ """Internal helper function for self.save(). Keeps track of
701
+ inter-table dependencies.
702
+ """
703
+ if tag in done:
704
+ return
705
+ tableClass = getTableClass(tag)
706
+ for masterTable in tableClass.dependencies:
707
+ if masterTable not in done:
708
+ if masterTable in self:
709
+ self._writeTable(masterTable, writer, done, tableCache)
710
+ else:
711
+ done.append(masterTable)
712
+ done.append(tag)
713
+ tabledata = self.getTableData(tag)
714
+ if tableCache is not None:
715
+ entry = tableCache.get((Tag(tag), tabledata))
716
+ if entry is not None:
717
+ log.debug("reusing '%s' table", tag)
718
+ writer.setEntry(tag, entry)
719
+ return
720
+ log.debug("Writing '%s' table to disk", tag)
721
+ writer[tag] = tabledata
722
+ if tableCache is not None:
723
+ tableCache[(Tag(tag), tabledata)] = writer[tag]
724
+
725
+ def getTableData(self, tag):
726
+ """Returns the binary representation of a table.
727
+
728
+ If the table is currently loaded and in memory, the data is compiled to
729
+ binary and returned; if it is not currently loaded, the binary data is
730
+ read from the font file and returned.
731
+ """
732
+ tag = Tag(tag)
733
+ if self.isLoaded(tag):
734
+ log.debug("Compiling '%s' table", tag)
735
+ return self.tables[tag].compile(self)
736
+ elif self.reader and tag in self.reader:
737
+ log.debug("Reading '%s' table from disk", tag)
738
+ return self.reader[tag]
739
+ else:
740
+ raise KeyError(tag)
741
+
742
+ def getGlyphSet(
743
+ self, preferCFF=True, location=None, normalized=False, recalcBounds=True
744
+ ):
745
+ """Return a generic GlyphSet, which is a dict-like object
746
+ mapping glyph names to glyph objects. The returned glyph objects
747
+ have a ``.draw()`` method that supports the Pen protocol, and will
748
+ have an attribute named 'width'.
749
+
750
+ If the font is CFF-based, the outlines will be taken from the ``CFF ``
751
+ or ``CFF2`` tables. Otherwise the outlines will be taken from the
752
+ ``glyf`` table.
753
+
754
+ If the font contains both a ``CFF ``/``CFF2`` and a ``glyf`` table, you
755
+ can use the ``preferCFF`` argument to specify which one should be taken.
756
+ If the font contains both a ``CFF `` and a ``CFF2`` table, the latter is
757
+ taken.
758
+
759
+ If the ``location`` parameter is set, it should be a dictionary mapping
760
+ four-letter variation tags to their float values, and the returned
761
+ glyph-set will represent an instance of a variable font at that
762
+ location.
763
+
764
+ If the ``normalized`` variable is set to True, that location is
765
+ interpreted as in the normalized (-1..+1) space, otherwise it is in the
766
+ font's defined axes space.
767
+ """
768
+ if location and "fvar" not in self:
769
+ location = None
770
+ if location and not normalized:
771
+ location = self.normalizeLocation(location)
772
+ glyphSet = None
773
+ if ("CFF " in self or "CFF2" in self) and (preferCFF or "glyf" not in self):
774
+ glyphSet = _TTGlyphSetCFF(self, location)
775
+ elif "glyf" in self:
776
+ glyphSet = _TTGlyphSetGlyf(self, location, recalcBounds=recalcBounds)
777
+ else:
778
+ raise TTLibError("Font contains no outlines")
779
+ if "VARC" in self:
780
+ glyphSet = _TTGlyphSetVARC(self, location, glyphSet)
781
+ return glyphSet
782
+
783
+ def normalizeLocation(self, location):
784
+ """Normalize a ``location`` from the font's defined axes space (also
785
+ known as user space) into the normalized (-1..+1) space. It applies
786
+ ``avar`` mapping if the font contains an ``avar`` table.
787
+
788
+ The ``location`` parameter should be a dictionary mapping four-letter
789
+ variation tags to their float values.
790
+
791
+ Raises ``TTLibError`` if the font is not a variable font.
792
+ """
793
+ from fontTools.varLib.models import normalizeLocation
794
+
795
+ if "fvar" not in self:
796
+ raise TTLibError("Not a variable font")
797
+
798
+ axes = self["fvar"].getAxes()
799
+ location = normalizeLocation(location, axes)
800
+ if "avar" in self:
801
+ location = self["avar"].renormalizeLocation(location, self)
802
+ return location
803
+
804
+ def getBestCmap(
805
+ self,
806
+ cmapPreferences=(
807
+ (3, 10),
808
+ (0, 6),
809
+ (0, 4),
810
+ (3, 1),
811
+ (0, 3),
812
+ (0, 2),
813
+ (0, 1),
814
+ (0, 0),
815
+ ),
816
+ ):
817
+ """Returns the 'best' Unicode cmap dictionary available in the font
818
+ or ``None``, if no Unicode cmap subtable is available.
819
+
820
+ By default it will search for the following (platformID, platEncID)
821
+ pairs in order::
822
+
823
+ (3, 10), # Windows Unicode full repertoire
824
+ (0, 6), # Unicode full repertoire (format 13 subtable)
825
+ (0, 4), # Unicode 2.0 full repertoire
826
+ (3, 1), # Windows Unicode BMP
827
+ (0, 3), # Unicode 2.0 BMP
828
+ (0, 2), # Unicode ISO/IEC 10646
829
+ (0, 1), # Unicode 1.1
830
+ (0, 0) # Unicode 1.0
831
+
832
+ This particular order matches what HarfBuzz uses to choose what
833
+ subtable to use by default. This order prefers the largest-repertoire
834
+ subtable, and among those, prefers the Windows-platform over the
835
+ Unicode-platform as the former has wider support.
836
+
837
+ This order can be customized via the ``cmapPreferences`` argument.
838
+ """
839
+ return self["cmap"].getBestCmap(cmapPreferences=cmapPreferences)
840
+
841
+ def reorderGlyphs(self, new_glyph_order):
842
+ from .reorderGlyphs import reorderGlyphs
843
+
844
+ reorderGlyphs(self, new_glyph_order)
845
+
846
+
847
+ class GlyphOrder(object):
848
+ """A pseudo table. The glyph order isn't in the font as a separate
849
+ table, but it's nice to present it as such in the TTX format.
850
+ """
851
+
852
+ def __init__(self, tag=None):
853
+ pass
854
+
855
+ def toXML(self, writer, ttFont):
856
+ glyphOrder = ttFont.getGlyphOrder()
857
+ writer.comment(
858
+ "The 'id' attribute is only for humans; " "it is ignored when parsed."
859
+ )
860
+ writer.newline()
861
+ for i, glyphName in enumerate(glyphOrder):
862
+ writer.simpletag("GlyphID", id=i, name=glyphName)
863
+ writer.newline()
864
+
865
+ def fromXML(self, name, attrs, content, ttFont):
866
+ if not hasattr(self, "glyphOrder"):
867
+ self.glyphOrder = []
868
+ if name == "GlyphID":
869
+ self.glyphOrder.append(attrs["name"])
870
+ ttFont.setGlyphOrder(self.glyphOrder)
871
+
872
+
873
+ def getTableModule(tag):
874
+ """Fetch the packer/unpacker module for a table.
875
+ Return None when no module is found.
876
+ """
877
+ from . import tables
878
+
879
+ pyTag = tagToIdentifier(tag)
880
+ try:
881
+ __import__("fontTools.ttLib.tables." + pyTag)
882
+ except ImportError as err:
883
+ # If pyTag is found in the ImportError message,
884
+ # means table is not implemented. If it's not
885
+ # there, then some other module is missing, don't
886
+ # suppress the error.
887
+ if str(err).find(pyTag) >= 0:
888
+ return None
889
+ else:
890
+ raise err
891
+ else:
892
+ return getattr(tables, pyTag)
893
+
894
+
895
+ # Registry for custom table packer/unpacker classes. Keys are table
896
+ # tags, values are (moduleName, className) tuples.
897
+ # See registerCustomTableClass() and getCustomTableClass()
898
+ _customTableRegistry = {}
899
+
900
+
901
+ def registerCustomTableClass(tag, moduleName, className=None):
902
+ """Register a custom packer/unpacker class for a table.
903
+
904
+ The 'moduleName' must be an importable module. If no 'className'
905
+ is given, it is derived from the tag, for example it will be
906
+ ``table_C_U_S_T_`` for a 'CUST' tag.
907
+
908
+ The registered table class should be a subclass of
909
+ :py:class:`fontTools.ttLib.tables.DefaultTable.DefaultTable`
910
+ """
911
+ if className is None:
912
+ className = "table_" + tagToIdentifier(tag)
913
+ _customTableRegistry[tag] = (moduleName, className)
914
+
915
+
916
+ def unregisterCustomTableClass(tag):
917
+ """Unregister the custom packer/unpacker class for a table."""
918
+ del _customTableRegistry[tag]
919
+
920
+
921
+ def getCustomTableClass(tag):
922
+ """Return the custom table class for tag, if one has been registered
923
+ with 'registerCustomTableClass()'. Else return None.
924
+ """
925
+ if tag not in _customTableRegistry:
926
+ return None
927
+ import importlib
928
+
929
+ moduleName, className = _customTableRegistry[tag]
930
+ module = importlib.import_module(moduleName)
931
+ return getattr(module, className)
932
+
933
+
934
+ def getTableClass(tag):
935
+ """Fetch the packer/unpacker class for a table."""
936
+ tableClass = getCustomTableClass(tag)
937
+ if tableClass is not None:
938
+ return tableClass
939
+ module = getTableModule(tag)
940
+ if module is None:
941
+ from .tables.DefaultTable import DefaultTable
942
+
943
+ return DefaultTable
944
+ pyTag = tagToIdentifier(tag)
945
+ tableClass = getattr(module, "table_" + pyTag)
946
+ return tableClass
947
+
948
+
949
+ def getClassTag(klass):
950
+ """Fetch the table tag for a class object."""
951
+ name = klass.__name__
952
+ assert name[:6] == "table_"
953
+ name = name[6:] # Chop 'table_'
954
+ return identifierToTag(name)
955
+
956
+
957
+ def newTable(tag):
958
+ """Return a new instance of a table."""
959
+ tableClass = getTableClass(tag)
960
+ return tableClass(tag)
961
+
962
+
963
+ def _escapechar(c):
964
+ """Helper function for tagToIdentifier()"""
965
+ import re
966
+
967
+ if re.match("[a-z0-9]", c):
968
+ return "_" + c
969
+ elif re.match("[A-Z]", c):
970
+ return c + "_"
971
+ else:
972
+ return hex(byteord(c))[2:]
973
+
974
+
975
+ def tagToIdentifier(tag):
976
+ """Convert a table tag to a valid (but UGLY) python identifier,
977
+ as well as a filename that's guaranteed to be unique even on a
978
+ caseless file system. Each character is mapped to two characters.
979
+ Lowercase letters get an underscore before the letter, uppercase
980
+ letters get an underscore after the letter. Trailing spaces are
981
+ trimmed. Illegal characters are escaped as two hex bytes. If the
982
+ result starts with a number (as the result of a hex escape), an
983
+ extra underscore is prepended. Examples:
984
+ .. code-block:: pycon
985
+
986
+ >>>
987
+ >> tagToIdentifier('glyf')
988
+ '_g_l_y_f'
989
+ >> tagToIdentifier('cvt ')
990
+ '_c_v_t'
991
+ >> tagToIdentifier('OS/2')
992
+ 'O_S_2f_2'
993
+ """
994
+ import re
995
+
996
+ tag = Tag(tag)
997
+ if tag == "GlyphOrder":
998
+ return tag
999
+ assert len(tag) == 4, "tag should be 4 characters long"
1000
+ while len(tag) > 1 and tag[-1] == " ":
1001
+ tag = tag[:-1]
1002
+ ident = ""
1003
+ for c in tag:
1004
+ ident = ident + _escapechar(c)
1005
+ if re.match("[0-9]", ident):
1006
+ ident = "_" + ident
1007
+ return ident
1008
+
1009
+
1010
+ def identifierToTag(ident):
1011
+ """the opposite of tagToIdentifier()"""
1012
+ if ident == "GlyphOrder":
1013
+ return ident
1014
+ if len(ident) % 2 and ident[0] == "_":
1015
+ ident = ident[1:]
1016
+ assert not (len(ident) % 2)
1017
+ tag = ""
1018
+ for i in range(0, len(ident), 2):
1019
+ if ident[i] == "_":
1020
+ tag = tag + ident[i + 1]
1021
+ elif ident[i + 1] == "_":
1022
+ tag = tag + ident[i]
1023
+ else:
1024
+ # assume hex
1025
+ tag = tag + chr(int(ident[i : i + 2], 16))
1026
+ # append trailing spaces
1027
+ tag = tag + (4 - len(tag)) * " "
1028
+ return Tag(tag)
1029
+
1030
+
1031
+ def tagToXML(tag):
1032
+ """Similarly to tagToIdentifier(), this converts a TT tag
1033
+ to a valid XML element name. Since XML element names are
1034
+ case sensitive, this is a fairly simple/readable translation.
1035
+ """
1036
+ import re
1037
+
1038
+ tag = Tag(tag)
1039
+ if tag == "OS/2":
1040
+ return "OS_2"
1041
+ elif tag == "GlyphOrder":
1042
+ return tag
1043
+ if re.match("[A-Za-z_][A-Za-z_0-9]* *$", tag):
1044
+ return tag.strip()
1045
+ else:
1046
+ return tagToIdentifier(tag)
1047
+
1048
+
1049
+ def xmlToTag(tag):
1050
+ """The opposite of tagToXML()"""
1051
+ if tag == "OS_2":
1052
+ return Tag("OS/2")
1053
+ if len(tag) == 8:
1054
+ return identifierToTag(tag)
1055
+ else:
1056
+ return Tag(tag + " " * (4 - len(tag)))
1057
+
1058
+
1059
+ # Table order as recommended in the OpenType specification 1.4
1060
+ TTFTableOrder = [
1061
+ "head",
1062
+ "hhea",
1063
+ "maxp",
1064
+ "OS/2",
1065
+ "hmtx",
1066
+ "LTSH",
1067
+ "VDMX",
1068
+ "hdmx",
1069
+ "cmap",
1070
+ "fpgm",
1071
+ "prep",
1072
+ "cvt ",
1073
+ "loca",
1074
+ "glyf",
1075
+ "kern",
1076
+ "name",
1077
+ "post",
1078
+ "gasp",
1079
+ "PCLT",
1080
+ ]
1081
+
1082
+ OTFTableOrder = ["head", "hhea", "maxp", "OS/2", "name", "cmap", "post", "CFF "]
1083
+
1084
+
1085
+ def sortedTagList(tagList, tableOrder=None):
1086
+ """Return a sorted copy of tagList, sorted according to the OpenType
1087
+ specification, or according to a custom tableOrder. If given and not
1088
+ None, tableOrder needs to be a list of tag names.
1089
+ """
1090
+ tagList = sorted(tagList)
1091
+ if tableOrder is None:
1092
+ if "DSIG" in tagList:
1093
+ # DSIG should be last (XXX spec reference?)
1094
+ tagList.remove("DSIG")
1095
+ tagList.append("DSIG")
1096
+ if "CFF " in tagList:
1097
+ tableOrder = OTFTableOrder
1098
+ else:
1099
+ tableOrder = TTFTableOrder
1100
+ orderedTables = []
1101
+ for tag in tableOrder:
1102
+ if tag in tagList:
1103
+ orderedTables.append(tag)
1104
+ tagList.remove(tag)
1105
+ orderedTables.extend(tagList)
1106
+ return orderedTables
1107
+
1108
+
1109
+ def reorderFontTables(inFile, outFile, tableOrder=None, checkChecksums=False):
1110
+ """Rewrite a font file, ordering the tables as recommended by the
1111
+ OpenType specification 1.4.
1112
+ """
1113
+ inFile.seek(0)
1114
+ outFile.seek(0)
1115
+ reader = SFNTReader(inFile, checkChecksums=checkChecksums)
1116
+ writer = SFNTWriter(
1117
+ outFile,
1118
+ len(reader.tables),
1119
+ reader.sfntVersion,
1120
+ reader.flavor,
1121
+ reader.flavorData,
1122
+ )
1123
+ tables = list(reader.keys())
1124
+ for tag in sortedTagList(tables, tableOrder):
1125
+ writer[tag] = reader[tag]
1126
+ writer.close()
1127
+
1128
+
1129
+ def maxPowerOfTwo(x):
1130
+ """Return the highest exponent of two, so that
1131
+ (2 ** exponent) <= x. Return 0 if x is 0.
1132
+ """
1133
+ exponent = 0
1134
+ while x:
1135
+ x = x >> 1
1136
+ exponent = exponent + 1
1137
+ return max(exponent - 1, 0)
1138
+
1139
+
1140
+ def getSearchRange(n, itemSize=16):
1141
+ """Calculate searchRange, entrySelector, rangeShift."""
1142
+ # itemSize defaults to 16, for backward compatibility
1143
+ # with upstream fonttools.
1144
+ exponent = maxPowerOfTwo(n)
1145
+ searchRange = (2**exponent) * itemSize
1146
+ entrySelector = exponent
1147
+ rangeShift = max(0, n * itemSize - searchRange)
1148
+ return searchRange, entrySelector, rangeShift