fonttools 4.58.3__cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.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.

Potentially problematic release.


This version of fonttools might be problematic. Click here for more details.

Files changed (334) 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/cffLib/CFF2ToCFF.py +203 -0
  6. fontTools/cffLib/CFFToCFF2.py +305 -0
  7. fontTools/cffLib/__init__.py +3694 -0
  8. fontTools/cffLib/specializer.py +927 -0
  9. fontTools/cffLib/transforms.py +490 -0
  10. fontTools/cffLib/width.py +210 -0
  11. fontTools/colorLib/__init__.py +0 -0
  12. fontTools/colorLib/builder.py +664 -0
  13. fontTools/colorLib/errors.py +2 -0
  14. fontTools/colorLib/geometry.py +143 -0
  15. fontTools/colorLib/table_builder.py +223 -0
  16. fontTools/colorLib/unbuilder.py +81 -0
  17. fontTools/config/__init__.py +90 -0
  18. fontTools/cu2qu/__init__.py +15 -0
  19. fontTools/cu2qu/__main__.py +6 -0
  20. fontTools/cu2qu/benchmark.py +54 -0
  21. fontTools/cu2qu/cli.py +198 -0
  22. fontTools/cu2qu/cu2qu.c +15545 -0
  23. fontTools/cu2qu/cu2qu.cpython-313-aarch64-linux-gnu.so +0 -0
  24. fontTools/cu2qu/cu2qu.py +531 -0
  25. fontTools/cu2qu/errors.py +77 -0
  26. fontTools/cu2qu/ufo.py +349 -0
  27. fontTools/designspaceLib/__init__.py +3338 -0
  28. fontTools/designspaceLib/__main__.py +6 -0
  29. fontTools/designspaceLib/split.py +475 -0
  30. fontTools/designspaceLib/statNames.py +260 -0
  31. fontTools/designspaceLib/types.py +147 -0
  32. fontTools/encodings/MacRoman.py +258 -0
  33. fontTools/encodings/StandardEncoding.py +258 -0
  34. fontTools/encodings/__init__.py +1 -0
  35. fontTools/encodings/codecs.py +135 -0
  36. fontTools/feaLib/__init__.py +4 -0
  37. fontTools/feaLib/__main__.py +78 -0
  38. fontTools/feaLib/ast.py +2142 -0
  39. fontTools/feaLib/builder.py +1796 -0
  40. fontTools/feaLib/error.py +22 -0
  41. fontTools/feaLib/lexer.c +17336 -0
  42. fontTools/feaLib/lexer.cpython-313-aarch64-linux-gnu.so +0 -0
  43. fontTools/feaLib/lexer.py +287 -0
  44. fontTools/feaLib/location.py +12 -0
  45. fontTools/feaLib/lookupDebugInfo.py +12 -0
  46. fontTools/feaLib/parser.py +2379 -0
  47. fontTools/feaLib/variableScalar.py +113 -0
  48. fontTools/fontBuilder.py +1014 -0
  49. fontTools/help.py +36 -0
  50. fontTools/merge/__init__.py +248 -0
  51. fontTools/merge/__main__.py +6 -0
  52. fontTools/merge/base.py +81 -0
  53. fontTools/merge/cmap.py +173 -0
  54. fontTools/merge/layout.py +526 -0
  55. fontTools/merge/options.py +85 -0
  56. fontTools/merge/tables.py +352 -0
  57. fontTools/merge/unicode.py +78 -0
  58. fontTools/merge/util.py +143 -0
  59. fontTools/misc/__init__.py +1 -0
  60. fontTools/misc/arrayTools.py +424 -0
  61. fontTools/misc/bezierTools.c +40136 -0
  62. fontTools/misc/bezierTools.cpython-313-aarch64-linux-gnu.so +0 -0
  63. fontTools/misc/bezierTools.py +1497 -0
  64. fontTools/misc/classifyTools.py +170 -0
  65. fontTools/misc/cliTools.py +53 -0
  66. fontTools/misc/configTools.py +349 -0
  67. fontTools/misc/cython.py +27 -0
  68. fontTools/misc/dictTools.py +83 -0
  69. fontTools/misc/eexec.py +119 -0
  70. fontTools/misc/encodingTools.py +72 -0
  71. fontTools/misc/etree.py +456 -0
  72. fontTools/misc/filenames.py +245 -0
  73. fontTools/misc/fixedTools.py +253 -0
  74. fontTools/misc/intTools.py +25 -0
  75. fontTools/misc/iterTools.py +12 -0
  76. fontTools/misc/lazyTools.py +42 -0
  77. fontTools/misc/loggingTools.py +543 -0
  78. fontTools/misc/macCreatorType.py +56 -0
  79. fontTools/misc/macRes.py +261 -0
  80. fontTools/misc/plistlib/__init__.py +681 -0
  81. fontTools/misc/plistlib/py.typed +0 -0
  82. fontTools/misc/psCharStrings.py +1496 -0
  83. fontTools/misc/psLib.py +398 -0
  84. fontTools/misc/psOperators.py +572 -0
  85. fontTools/misc/py23.py +96 -0
  86. fontTools/misc/roundTools.py +110 -0
  87. fontTools/misc/sstruct.py +231 -0
  88. fontTools/misc/symfont.py +242 -0
  89. fontTools/misc/testTools.py +233 -0
  90. fontTools/misc/textTools.py +154 -0
  91. fontTools/misc/timeTools.py +88 -0
  92. fontTools/misc/transform.py +516 -0
  93. fontTools/misc/treeTools.py +45 -0
  94. fontTools/misc/vector.py +147 -0
  95. fontTools/misc/visitor.py +142 -0
  96. fontTools/misc/xmlReader.py +188 -0
  97. fontTools/misc/xmlWriter.py +204 -0
  98. fontTools/mtiLib/__init__.py +1400 -0
  99. fontTools/mtiLib/__main__.py +5 -0
  100. fontTools/otlLib/__init__.py +1 -0
  101. fontTools/otlLib/builder.py +3435 -0
  102. fontTools/otlLib/error.py +11 -0
  103. fontTools/otlLib/maxContextCalc.py +96 -0
  104. fontTools/otlLib/optimize/__init__.py +53 -0
  105. fontTools/otlLib/optimize/__main__.py +6 -0
  106. fontTools/otlLib/optimize/gpos.py +439 -0
  107. fontTools/pens/__init__.py +1 -0
  108. fontTools/pens/areaPen.py +52 -0
  109. fontTools/pens/basePen.py +475 -0
  110. fontTools/pens/boundsPen.py +98 -0
  111. fontTools/pens/cairoPen.py +26 -0
  112. fontTools/pens/cocoaPen.py +26 -0
  113. fontTools/pens/cu2quPen.py +325 -0
  114. fontTools/pens/explicitClosingLinePen.py +101 -0
  115. fontTools/pens/filterPen.py +241 -0
  116. fontTools/pens/freetypePen.py +462 -0
  117. fontTools/pens/hashPointPen.py +89 -0
  118. fontTools/pens/momentsPen.c +13459 -0
  119. fontTools/pens/momentsPen.cpython-313-aarch64-linux-gnu.so +0 -0
  120. fontTools/pens/momentsPen.py +879 -0
  121. fontTools/pens/perimeterPen.py +69 -0
  122. fontTools/pens/pointInsidePen.py +192 -0
  123. fontTools/pens/pointPen.py +609 -0
  124. fontTools/pens/qtPen.py +29 -0
  125. fontTools/pens/qu2cuPen.py +105 -0
  126. fontTools/pens/quartzPen.py +43 -0
  127. fontTools/pens/recordingPen.py +335 -0
  128. fontTools/pens/reportLabPen.py +79 -0
  129. fontTools/pens/reverseContourPen.py +96 -0
  130. fontTools/pens/roundingPen.py +130 -0
  131. fontTools/pens/statisticsPen.py +312 -0
  132. fontTools/pens/svgPathPen.py +310 -0
  133. fontTools/pens/t2CharStringPen.py +88 -0
  134. fontTools/pens/teePen.py +55 -0
  135. fontTools/pens/transformPen.py +115 -0
  136. fontTools/pens/ttGlyphPen.py +335 -0
  137. fontTools/pens/wxPen.py +29 -0
  138. fontTools/qu2cu/__init__.py +15 -0
  139. fontTools/qu2cu/__main__.py +7 -0
  140. fontTools/qu2cu/benchmark.py +56 -0
  141. fontTools/qu2cu/cli.py +125 -0
  142. fontTools/qu2cu/qu2cu.c +16738 -0
  143. fontTools/qu2cu/qu2cu.cpython-313-aarch64-linux-gnu.so +0 -0
  144. fontTools/qu2cu/qu2cu.py +405 -0
  145. fontTools/subset/__init__.py +3929 -0
  146. fontTools/subset/__main__.py +6 -0
  147. fontTools/subset/cff.py +184 -0
  148. fontTools/subset/svg.py +253 -0
  149. fontTools/subset/util.py +25 -0
  150. fontTools/svgLib/__init__.py +3 -0
  151. fontTools/svgLib/path/__init__.py +65 -0
  152. fontTools/svgLib/path/arc.py +154 -0
  153. fontTools/svgLib/path/parser.py +322 -0
  154. fontTools/svgLib/path/shapes.py +183 -0
  155. fontTools/t1Lib/__init__.py +648 -0
  156. fontTools/tfmLib.py +460 -0
  157. fontTools/ttLib/__init__.py +30 -0
  158. fontTools/ttLib/__main__.py +148 -0
  159. fontTools/ttLib/macUtils.py +54 -0
  160. fontTools/ttLib/removeOverlaps.py +393 -0
  161. fontTools/ttLib/reorderGlyphs.py +285 -0
  162. fontTools/ttLib/scaleUpem.py +436 -0
  163. fontTools/ttLib/sfnt.py +662 -0
  164. fontTools/ttLib/standardGlyphOrder.py +271 -0
  165. fontTools/ttLib/tables/B_A_S_E_.py +14 -0
  166. fontTools/ttLib/tables/BitmapGlyphMetrics.py +64 -0
  167. fontTools/ttLib/tables/C_B_D_T_.py +113 -0
  168. fontTools/ttLib/tables/C_B_L_C_.py +19 -0
  169. fontTools/ttLib/tables/C_F_F_.py +61 -0
  170. fontTools/ttLib/tables/C_F_F__2.py +26 -0
  171. fontTools/ttLib/tables/C_O_L_R_.py +165 -0
  172. fontTools/ttLib/tables/C_P_A_L_.py +305 -0
  173. fontTools/ttLib/tables/D_S_I_G_.py +158 -0
  174. fontTools/ttLib/tables/D__e_b_g.py +35 -0
  175. fontTools/ttLib/tables/DefaultTable.py +49 -0
  176. fontTools/ttLib/tables/E_B_D_T_.py +835 -0
  177. fontTools/ttLib/tables/E_B_L_C_.py +718 -0
  178. fontTools/ttLib/tables/F_F_T_M_.py +52 -0
  179. fontTools/ttLib/tables/F__e_a_t.py +149 -0
  180. fontTools/ttLib/tables/G_D_E_F_.py +13 -0
  181. fontTools/ttLib/tables/G_M_A_P_.py +148 -0
  182. fontTools/ttLib/tables/G_P_K_G_.py +133 -0
  183. fontTools/ttLib/tables/G_P_O_S_.py +14 -0
  184. fontTools/ttLib/tables/G_S_U_B_.py +13 -0
  185. fontTools/ttLib/tables/G_V_A_R_.py +5 -0
  186. fontTools/ttLib/tables/G__l_a_t.py +235 -0
  187. fontTools/ttLib/tables/G__l_o_c.py +85 -0
  188. fontTools/ttLib/tables/H_V_A_R_.py +13 -0
  189. fontTools/ttLib/tables/J_S_T_F_.py +13 -0
  190. fontTools/ttLib/tables/L_T_S_H_.py +58 -0
  191. fontTools/ttLib/tables/M_A_T_H_.py +13 -0
  192. fontTools/ttLib/tables/M_E_T_A_.py +352 -0
  193. fontTools/ttLib/tables/M_V_A_R_.py +13 -0
  194. fontTools/ttLib/tables/O_S_2f_2.py +752 -0
  195. fontTools/ttLib/tables/S_I_N_G_.py +99 -0
  196. fontTools/ttLib/tables/S_T_A_T_.py +15 -0
  197. fontTools/ttLib/tables/S_V_G_.py +223 -0
  198. fontTools/ttLib/tables/S__i_l_f.py +1040 -0
  199. fontTools/ttLib/tables/S__i_l_l.py +92 -0
  200. fontTools/ttLib/tables/T_S_I_B_.py +13 -0
  201. fontTools/ttLib/tables/T_S_I_C_.py +14 -0
  202. fontTools/ttLib/tables/T_S_I_D_.py +13 -0
  203. fontTools/ttLib/tables/T_S_I_J_.py +13 -0
  204. fontTools/ttLib/tables/T_S_I_P_.py +13 -0
  205. fontTools/ttLib/tables/T_S_I_S_.py +13 -0
  206. fontTools/ttLib/tables/T_S_I_V_.py +26 -0
  207. fontTools/ttLib/tables/T_S_I__0.py +70 -0
  208. fontTools/ttLib/tables/T_S_I__1.py +166 -0
  209. fontTools/ttLib/tables/T_S_I__2.py +17 -0
  210. fontTools/ttLib/tables/T_S_I__3.py +22 -0
  211. fontTools/ttLib/tables/T_S_I__5.py +60 -0
  212. fontTools/ttLib/tables/T_T_F_A_.py +14 -0
  213. fontTools/ttLib/tables/TupleVariation.py +884 -0
  214. fontTools/ttLib/tables/V_A_R_C_.py +12 -0
  215. fontTools/ttLib/tables/V_D_M_X_.py +249 -0
  216. fontTools/ttLib/tables/V_O_R_G_.py +165 -0
  217. fontTools/ttLib/tables/V_V_A_R_.py +13 -0
  218. fontTools/ttLib/tables/__init__.py +98 -0
  219. fontTools/ttLib/tables/_a_n_k_r.py +15 -0
  220. fontTools/ttLib/tables/_a_v_a_r.py +191 -0
  221. fontTools/ttLib/tables/_b_s_l_n.py +15 -0
  222. fontTools/ttLib/tables/_c_i_d_g.py +24 -0
  223. fontTools/ttLib/tables/_c_m_a_p.py +1591 -0
  224. fontTools/ttLib/tables/_c_v_a_r.py +94 -0
  225. fontTools/ttLib/tables/_c_v_t.py +57 -0
  226. fontTools/ttLib/tables/_f_e_a_t.py +15 -0
  227. fontTools/ttLib/tables/_f_p_g_m.py +62 -0
  228. fontTools/ttLib/tables/_f_v_a_r.py +261 -0
  229. fontTools/ttLib/tables/_g_a_s_p.py +63 -0
  230. fontTools/ttLib/tables/_g_c_i_d.py +13 -0
  231. fontTools/ttLib/tables/_g_l_y_f.py +2312 -0
  232. fontTools/ttLib/tables/_g_v_a_r.py +337 -0
  233. fontTools/ttLib/tables/_h_d_m_x.py +127 -0
  234. fontTools/ttLib/tables/_h_e_a_d.py +130 -0
  235. fontTools/ttLib/tables/_h_h_e_a.py +147 -0
  236. fontTools/ttLib/tables/_h_m_t_x.py +160 -0
  237. fontTools/ttLib/tables/_k_e_r_n.py +289 -0
  238. fontTools/ttLib/tables/_l_c_a_r.py +13 -0
  239. fontTools/ttLib/tables/_l_o_c_a.py +70 -0
  240. fontTools/ttLib/tables/_l_t_a_g.py +72 -0
  241. fontTools/ttLib/tables/_m_a_x_p.py +147 -0
  242. fontTools/ttLib/tables/_m_e_t_a.py +112 -0
  243. fontTools/ttLib/tables/_m_o_r_t.py +14 -0
  244. fontTools/ttLib/tables/_m_o_r_x.py +15 -0
  245. fontTools/ttLib/tables/_n_a_m_e.py +1237 -0
  246. fontTools/ttLib/tables/_o_p_b_d.py +14 -0
  247. fontTools/ttLib/tables/_p_o_s_t.py +320 -0
  248. fontTools/ttLib/tables/_p_r_e_p.py +16 -0
  249. fontTools/ttLib/tables/_p_r_o_p.py +12 -0
  250. fontTools/ttLib/tables/_s_b_i_x.py +129 -0
  251. fontTools/ttLib/tables/_t_r_a_k.py +332 -0
  252. fontTools/ttLib/tables/_v_h_e_a.py +139 -0
  253. fontTools/ttLib/tables/_v_m_t_x.py +19 -0
  254. fontTools/ttLib/tables/asciiTable.py +20 -0
  255. fontTools/ttLib/tables/grUtils.py +92 -0
  256. fontTools/ttLib/tables/otBase.py +1466 -0
  257. fontTools/ttLib/tables/otConverters.py +2068 -0
  258. fontTools/ttLib/tables/otData.py +6400 -0
  259. fontTools/ttLib/tables/otTables.py +2708 -0
  260. fontTools/ttLib/tables/otTraverse.py +163 -0
  261. fontTools/ttLib/tables/sbixGlyph.py +149 -0
  262. fontTools/ttLib/tables/sbixStrike.py +177 -0
  263. fontTools/ttLib/tables/table_API_readme.txt +91 -0
  264. fontTools/ttLib/tables/ttProgram.py +594 -0
  265. fontTools/ttLib/ttCollection.py +125 -0
  266. fontTools/ttLib/ttFont.py +1157 -0
  267. fontTools/ttLib/ttGlyphSet.py +490 -0
  268. fontTools/ttLib/ttVisitor.py +32 -0
  269. fontTools/ttLib/woff2.py +1683 -0
  270. fontTools/ttx.py +479 -0
  271. fontTools/ufoLib/__init__.py +2477 -0
  272. fontTools/ufoLib/converters.py +398 -0
  273. fontTools/ufoLib/errors.py +30 -0
  274. fontTools/ufoLib/etree.py +6 -0
  275. fontTools/ufoLib/filenames.py +346 -0
  276. fontTools/ufoLib/glifLib.py +2029 -0
  277. fontTools/ufoLib/kerning.py +121 -0
  278. fontTools/ufoLib/plistlib.py +47 -0
  279. fontTools/ufoLib/pointPen.py +6 -0
  280. fontTools/ufoLib/utils.py +79 -0
  281. fontTools/ufoLib/validators.py +1186 -0
  282. fontTools/unicode.py +50 -0
  283. fontTools/unicodedata/Blocks.py +801 -0
  284. fontTools/unicodedata/Mirrored.py +446 -0
  285. fontTools/unicodedata/OTTags.py +50 -0
  286. fontTools/unicodedata/ScriptExtensions.py +826 -0
  287. fontTools/unicodedata/Scripts.py +3617 -0
  288. fontTools/unicodedata/__init__.py +302 -0
  289. fontTools/varLib/__init__.py +1517 -0
  290. fontTools/varLib/__main__.py +6 -0
  291. fontTools/varLib/avar.py +260 -0
  292. fontTools/varLib/avarPlanner.py +1004 -0
  293. fontTools/varLib/builder.py +215 -0
  294. fontTools/varLib/cff.py +631 -0
  295. fontTools/varLib/errors.py +219 -0
  296. fontTools/varLib/featureVars.py +695 -0
  297. fontTools/varLib/hvar.py +113 -0
  298. fontTools/varLib/instancer/__init__.py +1946 -0
  299. fontTools/varLib/instancer/__main__.py +5 -0
  300. fontTools/varLib/instancer/featureVars.py +190 -0
  301. fontTools/varLib/instancer/names.py +388 -0
  302. fontTools/varLib/instancer/solver.py +309 -0
  303. fontTools/varLib/interpolatable.py +1209 -0
  304. fontTools/varLib/interpolatableHelpers.py +396 -0
  305. fontTools/varLib/interpolatablePlot.py +1269 -0
  306. fontTools/varLib/interpolatableTestContourOrder.py +82 -0
  307. fontTools/varLib/interpolatableTestStartingPoint.py +107 -0
  308. fontTools/varLib/interpolate_layout.py +124 -0
  309. fontTools/varLib/iup.c +19830 -0
  310. fontTools/varLib/iup.cpython-313-aarch64-linux-gnu.so +0 -0
  311. fontTools/varLib/iup.py +490 -0
  312. fontTools/varLib/merger.py +1717 -0
  313. fontTools/varLib/models.py +642 -0
  314. fontTools/varLib/multiVarStore.py +253 -0
  315. fontTools/varLib/mutator.py +518 -0
  316. fontTools/varLib/mvar.py +40 -0
  317. fontTools/varLib/plot.py +238 -0
  318. fontTools/varLib/stat.py +149 -0
  319. fontTools/varLib/varStore.py +739 -0
  320. fontTools/voltLib/__init__.py +5 -0
  321. fontTools/voltLib/__main__.py +206 -0
  322. fontTools/voltLib/ast.py +452 -0
  323. fontTools/voltLib/error.py +12 -0
  324. fontTools/voltLib/lexer.py +99 -0
  325. fontTools/voltLib/parser.py +664 -0
  326. fontTools/voltLib/voltToFea.py +911 -0
  327. fonttools-4.58.3.data/data/share/man/man1/ttx.1 +225 -0
  328. fonttools-4.58.3.dist-info/METADATA +2133 -0
  329. fonttools-4.58.3.dist-info/RECORD +334 -0
  330. fonttools-4.58.3.dist-info/WHEEL +7 -0
  331. fonttools-4.58.3.dist-info/entry_points.txt +5 -0
  332. fonttools-4.58.3.dist-info/licenses/LICENSE +21 -0
  333. fonttools-4.58.3.dist-info/licenses/LICENSE.external +359 -0
  334. fonttools-4.58.3.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1157 @@
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 = list(self.keys())
263
+ if "GlyphOrder" in tags:
264
+ tags.remove("GlyphOrder")
265
+ numTables = len(tags)
266
+ # write to a temporary stream to allow saving to unseekable streams
267
+ writer = SFNTWriter(
268
+ file, numTables, self.sfntVersion, self.flavor, self.flavorData
269
+ )
270
+
271
+ done = []
272
+ for tag in tags:
273
+ self._writeTable(tag, writer, done, tableCache)
274
+
275
+ writer.close()
276
+
277
+ return writer.reordersTables()
278
+
279
+ def saveXML(self, fileOrPath, newlinestr="\n", **kwargs):
280
+ """Export the font as TTX (an XML-based text file), or as a series of text
281
+ files when splitTables is true. In the latter case, the 'fileOrPath'
282
+ argument should be a path to a directory.
283
+ The 'tables' argument must either be false (dump all tables) or a
284
+ list of tables to dump. The 'skipTables' argument may be a list of tables
285
+ to skip, but only when the 'tables' argument is false.
286
+ """
287
+
288
+ writer = xmlWriter.XMLWriter(fileOrPath, newlinestr=newlinestr)
289
+ self._saveXML(writer, **kwargs)
290
+ writer.close()
291
+
292
+ def _saveXML(
293
+ self,
294
+ writer,
295
+ writeVersion=True,
296
+ quiet=None,
297
+ tables=None,
298
+ skipTables=None,
299
+ splitTables=False,
300
+ splitGlyphs=False,
301
+ disassembleInstructions=True,
302
+ bitmapGlyphDataFormat="raw",
303
+ ):
304
+ if quiet is not None:
305
+ deprecateArgument("quiet", "configure logging instead")
306
+
307
+ self.disassembleInstructions = disassembleInstructions
308
+ self.bitmapGlyphDataFormat = bitmapGlyphDataFormat
309
+ if not tables:
310
+ tables = list(self.keys())
311
+ if "GlyphOrder" not in tables:
312
+ tables = ["GlyphOrder"] + tables
313
+ if skipTables:
314
+ for tag in skipTables:
315
+ if tag in tables:
316
+ tables.remove(tag)
317
+ numTables = len(tables)
318
+
319
+ if writeVersion:
320
+ from fontTools import version
321
+
322
+ version = ".".join(version.split(".")[:2])
323
+ writer.begintag(
324
+ "ttFont",
325
+ sfntVersion=repr(tostr(self.sfntVersion))[1:-1],
326
+ ttLibVersion=version,
327
+ )
328
+ else:
329
+ writer.begintag("ttFont", sfntVersion=repr(tostr(self.sfntVersion))[1:-1])
330
+ writer.newline()
331
+
332
+ # always splitTables if splitGlyphs is enabled
333
+ splitTables = splitTables or splitGlyphs
334
+
335
+ if not splitTables:
336
+ writer.newline()
337
+ else:
338
+ path, ext = os.path.splitext(writer.filename)
339
+
340
+ for i in range(numTables):
341
+ tag = tables[i]
342
+ if splitTables:
343
+ tablePath = path + "." + tagToIdentifier(tag) + ext
344
+ tableWriter = xmlWriter.XMLWriter(
345
+ tablePath, newlinestr=writer.newlinestr
346
+ )
347
+ tableWriter.begintag("ttFont", ttLibVersion=version)
348
+ tableWriter.newline()
349
+ tableWriter.newline()
350
+ writer.simpletag(tagToXML(tag), src=os.path.basename(tablePath))
351
+ writer.newline()
352
+ else:
353
+ tableWriter = writer
354
+ self._tableToXML(tableWriter, tag, splitGlyphs=splitGlyphs)
355
+ if splitTables:
356
+ tableWriter.endtag("ttFont")
357
+ tableWriter.newline()
358
+ tableWriter.close()
359
+ writer.endtag("ttFont")
360
+ writer.newline()
361
+
362
+ def _tableToXML(self, writer, tag, quiet=None, splitGlyphs=False):
363
+ if quiet is not None:
364
+ deprecateArgument("quiet", "configure logging instead")
365
+ if tag in self:
366
+ table = self[tag]
367
+ report = "Dumping '%s' table..." % tag
368
+ else:
369
+ report = "No '%s' table found." % tag
370
+ log.info(report)
371
+ if tag not in self:
372
+ return
373
+ xmlTag = tagToXML(tag)
374
+ attrs = dict()
375
+ if hasattr(table, "ERROR"):
376
+ attrs["ERROR"] = "decompilation error"
377
+ from .tables.DefaultTable import DefaultTable
378
+
379
+ if table.__class__ == DefaultTable:
380
+ attrs["raw"] = True
381
+ writer.begintag(xmlTag, **attrs)
382
+ writer.newline()
383
+ if tag == "glyf":
384
+ table.toXML(writer, self, splitGlyphs=splitGlyphs)
385
+ else:
386
+ table.toXML(writer, self)
387
+ writer.endtag(xmlTag)
388
+ writer.newline()
389
+ writer.newline()
390
+
391
+ def importXML(self, fileOrPath, quiet=None):
392
+ """Import a TTX file (an XML-based text format), so as to recreate
393
+ a font object.
394
+ """
395
+ if quiet is not None:
396
+ deprecateArgument("quiet", "configure logging instead")
397
+
398
+ if "maxp" in self and "post" in self:
399
+ # Make sure the glyph order is loaded, as it otherwise gets
400
+ # lost if the XML doesn't contain the glyph order, yet does
401
+ # contain the table which was originally used to extract the
402
+ # glyph names from (ie. 'post', 'cmap' or 'CFF ').
403
+ self.getGlyphOrder()
404
+
405
+ from fontTools.misc import xmlReader
406
+
407
+ reader = xmlReader.XMLReader(fileOrPath, self)
408
+ reader.read()
409
+
410
+ def isLoaded(self, tag):
411
+ """Return true if the table identified by ``tag`` has been
412
+ decompiled and loaded into memory."""
413
+ return tag in self.tables
414
+
415
+ def has_key(self, tag):
416
+ """Test if the table identified by ``tag`` is present in the font.
417
+
418
+ As well as this method, ``tag in font`` can also be used to determine the
419
+ presence of the table."""
420
+ if self.isLoaded(tag):
421
+ return True
422
+ elif self.reader and tag in self.reader:
423
+ return True
424
+ elif tag == "GlyphOrder":
425
+ return True
426
+ else:
427
+ return False
428
+
429
+ __contains__ = has_key
430
+
431
+ def keys(self):
432
+ """Returns the list of tables in the font, along with the ``GlyphOrder`` pseudo-table."""
433
+ keys = list(self.tables.keys())
434
+ if self.reader:
435
+ for key in list(self.reader.keys()):
436
+ if key not in keys:
437
+ keys.append(key)
438
+
439
+ if "GlyphOrder" in keys:
440
+ keys.remove("GlyphOrder")
441
+ keys = sortedTagList(keys)
442
+ return ["GlyphOrder"] + keys
443
+
444
+ def ensureDecompiled(self, recurse=None):
445
+ """Decompile all the tables, even if a TTFont was opened in 'lazy' mode."""
446
+ for tag in self.keys():
447
+ table = self[tag]
448
+ if recurse is None:
449
+ recurse = self.lazy is not False
450
+ if recurse and hasattr(table, "ensureDecompiled"):
451
+ table.ensureDecompiled(recurse=recurse)
452
+ self.lazy = False
453
+
454
+ def __len__(self):
455
+ return len(list(self.keys()))
456
+
457
+ def __getitem__(self, tag):
458
+ tag = Tag(tag)
459
+ table = self.tables.get(tag)
460
+ if table is None:
461
+ if tag == "GlyphOrder":
462
+ table = GlyphOrder(tag)
463
+ self.tables[tag] = table
464
+ elif self.reader is not None:
465
+ table = self._readTable(tag)
466
+ else:
467
+ raise KeyError("'%s' table not found" % tag)
468
+ return table
469
+
470
+ def _readTable(self, tag):
471
+ log.debug("Reading '%s' table from disk", tag)
472
+ data = self.reader[tag]
473
+ if self._tableCache is not None:
474
+ table = self._tableCache.get((tag, data))
475
+ if table is not None:
476
+ return table
477
+ tableClass = getTableClass(tag)
478
+ table = tableClass(tag)
479
+ self.tables[tag] = table
480
+ log.debug("Decompiling '%s' table", tag)
481
+ try:
482
+ table.decompile(data, self)
483
+ except Exception:
484
+ if not self.ignoreDecompileErrors:
485
+ raise
486
+ # fall back to DefaultTable, retaining the binary table data
487
+ log.exception(
488
+ "An exception occurred during the decompilation of the '%s' table", tag
489
+ )
490
+ from .tables.DefaultTable import DefaultTable
491
+
492
+ file = StringIO()
493
+ traceback.print_exc(file=file)
494
+ table = DefaultTable(tag)
495
+ table.ERROR = file.getvalue()
496
+ self.tables[tag] = table
497
+ table.decompile(data, self)
498
+ if self._tableCache is not None:
499
+ self._tableCache[(tag, data)] = table
500
+ return table
501
+
502
+ def __setitem__(self, tag, table):
503
+ self.tables[Tag(tag)] = table
504
+
505
+ def __delitem__(self, tag):
506
+ if tag not in self:
507
+ raise KeyError("'%s' table not found" % tag)
508
+ if tag in self.tables:
509
+ del self.tables[tag]
510
+ if self.reader and tag in self.reader:
511
+ del self.reader[tag]
512
+
513
+ def get(self, tag, default=None):
514
+ """Returns the table if it exists or (optionally) a default if it doesn't."""
515
+ try:
516
+ return self[tag]
517
+ except KeyError:
518
+ return default
519
+
520
+ def setGlyphOrder(self, glyphOrder):
521
+ """Set the glyph order
522
+
523
+ Args:
524
+ glyphOrder ([str]): List of glyph names in order.
525
+ """
526
+ self.glyphOrder = glyphOrder
527
+ if hasattr(self, "_reverseGlyphOrderDict"):
528
+ del self._reverseGlyphOrderDict
529
+ if self.isLoaded("glyf"):
530
+ self["glyf"].setGlyphOrder(glyphOrder)
531
+
532
+ def getGlyphOrder(self):
533
+ """Returns a list of glyph names ordered by their position in the font."""
534
+ try:
535
+ return self.glyphOrder
536
+ except AttributeError:
537
+ pass
538
+ if "CFF " in self:
539
+ cff = self["CFF "]
540
+ self.glyphOrder = cff.getGlyphOrder()
541
+ elif "post" in self:
542
+ # TrueType font
543
+ glyphOrder = self["post"].getGlyphOrder()
544
+ if glyphOrder is None:
545
+ #
546
+ # No names found in the 'post' table.
547
+ # Try to create glyph names from the unicode cmap (if available)
548
+ # in combination with the Adobe Glyph List (AGL).
549
+ #
550
+ self._getGlyphNamesFromCmap()
551
+ elif len(glyphOrder) < self["maxp"].numGlyphs:
552
+ #
553
+ # Not enough names found in the 'post' table.
554
+ # Can happen when 'post' format 1 is improperly used on a font that
555
+ # has more than 258 glyphs (the length of 'standardGlyphOrder').
556
+ #
557
+ log.warning(
558
+ "Not enough names found in the 'post' table, generating them from cmap instead"
559
+ )
560
+ self._getGlyphNamesFromCmap()
561
+ else:
562
+ self.glyphOrder = glyphOrder
563
+ else:
564
+ self._getGlyphNamesFromCmap()
565
+ return self.glyphOrder
566
+
567
+ def _getGlyphNamesFromCmap(self):
568
+ #
569
+ # This is rather convoluted, but then again, it's an interesting problem:
570
+ # - we need to use the unicode values found in the cmap table to
571
+ # build glyph names (eg. because there is only a minimal post table,
572
+ # or none at all).
573
+ # - but the cmap parser also needs glyph names to work with...
574
+ # So here's what we do:
575
+ # - make up glyph names based on glyphID
576
+ # - load a temporary cmap table based on those names
577
+ # - extract the unicode values, build the "real" glyph names
578
+ # - unload the temporary cmap table
579
+ #
580
+ if self.isLoaded("cmap"):
581
+ # Bootstrapping: we're getting called by the cmap parser
582
+ # itself. This means self.tables['cmap'] contains a partially
583
+ # loaded cmap, making it impossible to get at a unicode
584
+ # subtable here. We remove the partially loaded cmap and
585
+ # restore it later.
586
+ # This only happens if the cmap table is loaded before any
587
+ # other table that does f.getGlyphOrder() or f.getGlyphName().
588
+ cmapLoading = self.tables["cmap"]
589
+ del self.tables["cmap"]
590
+ else:
591
+ cmapLoading = None
592
+ # Make up glyph names based on glyphID, which will be used by the
593
+ # temporary cmap and by the real cmap in case we don't find a unicode
594
+ # cmap.
595
+ numGlyphs = int(self["maxp"].numGlyphs)
596
+ glyphOrder = ["glyph%.5d" % i for i in range(numGlyphs)]
597
+ glyphOrder[0] = ".notdef"
598
+ # Set the glyph order, so the cmap parser has something
599
+ # to work with (so we don't get called recursively).
600
+ self.glyphOrder = glyphOrder
601
+
602
+ # Make up glyph names based on the reversed cmap table. Because some
603
+ # glyphs (eg. ligatures or alternates) may not be reachable via cmap,
604
+ # this naming table will usually not cover all glyphs in the font.
605
+ # If the font has no Unicode cmap table, reversecmap will be empty.
606
+ if "cmap" in self:
607
+ reversecmap = self["cmap"].buildReversedMin()
608
+ else:
609
+ reversecmap = {}
610
+ useCount = {}
611
+ for i in range(numGlyphs):
612
+ tempName = glyphOrder[i]
613
+ if tempName in reversecmap:
614
+ # If a font maps both U+0041 LATIN CAPITAL LETTER A and
615
+ # U+0391 GREEK CAPITAL LETTER ALPHA to the same glyph,
616
+ # we prefer naming the glyph as "A".
617
+ glyphName = self._makeGlyphName(reversecmap[tempName])
618
+ numUses = useCount[glyphName] = useCount.get(glyphName, 0) + 1
619
+ if numUses > 1:
620
+ glyphName = "%s.alt%d" % (glyphName, numUses - 1)
621
+ glyphOrder[i] = glyphName
622
+
623
+ if "cmap" in self:
624
+ # Delete the temporary cmap table from the cache, so it can
625
+ # be parsed again with the right names.
626
+ del self.tables["cmap"]
627
+ self.glyphOrder = glyphOrder
628
+ if cmapLoading:
629
+ # restore partially loaded cmap, so it can continue loading
630
+ # using the proper names.
631
+ self.tables["cmap"] = cmapLoading
632
+
633
+ @staticmethod
634
+ def _makeGlyphName(codepoint):
635
+ from fontTools import agl # Adobe Glyph List
636
+
637
+ if codepoint in agl.UV2AGL:
638
+ return agl.UV2AGL[codepoint]
639
+ elif codepoint <= 0xFFFF:
640
+ return "uni%04X" % codepoint
641
+ else:
642
+ return "u%X" % codepoint
643
+
644
+ def getGlyphNames(self):
645
+ """Get a list of glyph names, sorted alphabetically."""
646
+ glyphNames = sorted(self.getGlyphOrder())
647
+ return glyphNames
648
+
649
+ def getGlyphNames2(self):
650
+ """Get a list of glyph names, sorted alphabetically,
651
+ but not case sensitive.
652
+ """
653
+ from fontTools.misc import textTools
654
+
655
+ return textTools.caselessSort(self.getGlyphOrder())
656
+
657
+ def getGlyphName(self, glyphID):
658
+ """Returns the name for the glyph with the given ID.
659
+
660
+ If no name is available, synthesises one with the form ``glyphXXXXX``` where
661
+ ```XXXXX`` is the zero-padded glyph ID.
662
+ """
663
+ try:
664
+ return self.getGlyphOrder()[glyphID]
665
+ except IndexError:
666
+ return "glyph%.5d" % glyphID
667
+
668
+ def getGlyphNameMany(self, lst):
669
+ """Converts a list of glyph IDs into a list of glyph names."""
670
+ glyphOrder = self.getGlyphOrder()
671
+ cnt = len(glyphOrder)
672
+ return [glyphOrder[gid] if gid < cnt else "glyph%.5d" % gid for gid in lst]
673
+
674
+ def getGlyphID(self, glyphName):
675
+ """Returns the ID of the glyph with the given name."""
676
+ try:
677
+ return self.getReverseGlyphMap()[glyphName]
678
+ except KeyError:
679
+ if glyphName[:5] == "glyph":
680
+ try:
681
+ return int(glyphName[5:])
682
+ except (NameError, ValueError):
683
+ raise KeyError(glyphName)
684
+ raise
685
+
686
+ def getGlyphIDMany(self, lst):
687
+ """Converts a list of glyph names into a list of glyph IDs."""
688
+ d = self.getReverseGlyphMap()
689
+ try:
690
+ return [d[glyphName] for glyphName in lst]
691
+ except KeyError:
692
+ getGlyphID = self.getGlyphID
693
+ return [getGlyphID(glyphName) for glyphName in lst]
694
+
695
+ def getReverseGlyphMap(self, rebuild=False):
696
+ """Returns a mapping of glyph names to glyph IDs."""
697
+ if rebuild or not hasattr(self, "_reverseGlyphOrderDict"):
698
+ self._buildReverseGlyphOrderDict()
699
+ return self._reverseGlyphOrderDict
700
+
701
+ def _buildReverseGlyphOrderDict(self):
702
+ self._reverseGlyphOrderDict = d = {}
703
+ for glyphID, glyphName in enumerate(self.getGlyphOrder()):
704
+ d[glyphName] = glyphID
705
+ return d
706
+
707
+ def _writeTable(self, tag, writer, done, tableCache=None):
708
+ """Internal helper function for self.save(). Keeps track of
709
+ inter-table dependencies.
710
+ """
711
+ if tag in done:
712
+ return
713
+ tableClass = getTableClass(tag)
714
+ for masterTable in tableClass.dependencies:
715
+ if masterTable not in done:
716
+ if masterTable in self:
717
+ self._writeTable(masterTable, writer, done, tableCache)
718
+ else:
719
+ done.append(masterTable)
720
+ done.append(tag)
721
+ tabledata = self.getTableData(tag)
722
+ if tableCache is not None:
723
+ entry = tableCache.get((Tag(tag), tabledata))
724
+ if entry is not None:
725
+ log.debug("reusing '%s' table", tag)
726
+ writer.setEntry(tag, entry)
727
+ return
728
+ log.debug("Writing '%s' table to disk", tag)
729
+ writer[tag] = tabledata
730
+ if tableCache is not None:
731
+ tableCache[(Tag(tag), tabledata)] = writer[tag]
732
+
733
+ def getTableData(self, tag):
734
+ """Returns the binary representation of a table.
735
+
736
+ If the table is currently loaded and in memory, the data is compiled to
737
+ binary and returned; if it is not currently loaded, the binary data is
738
+ read from the font file and returned.
739
+ """
740
+ tag = Tag(tag)
741
+ if self.isLoaded(tag):
742
+ log.debug("Compiling '%s' table", tag)
743
+ return self.tables[tag].compile(self)
744
+ elif self.reader and tag in self.reader:
745
+ log.debug("Reading '%s' table from disk", tag)
746
+ return self.reader[tag]
747
+ else:
748
+ raise KeyError(tag)
749
+
750
+ def getGlyphSet(
751
+ self, preferCFF=True, location=None, normalized=False, recalcBounds=True
752
+ ):
753
+ """Return a generic GlyphSet, which is a dict-like object
754
+ mapping glyph names to glyph objects. The returned glyph objects
755
+ have a ``.draw()`` method that supports the Pen protocol, and will
756
+ have an attribute named 'width'.
757
+
758
+ If the font is CFF-based, the outlines will be taken from the ``CFF ``
759
+ or ``CFF2`` tables. Otherwise the outlines will be taken from the
760
+ ``glyf`` table.
761
+
762
+ If the font contains both a ``CFF ``/``CFF2`` and a ``glyf`` table, you
763
+ can use the ``preferCFF`` argument to specify which one should be taken.
764
+ If the font contains both a ``CFF `` and a ``CFF2`` table, the latter is
765
+ taken.
766
+
767
+ If the ``location`` parameter is set, it should be a dictionary mapping
768
+ four-letter variation tags to their float values, and the returned
769
+ glyph-set will represent an instance of a variable font at that
770
+ location.
771
+
772
+ If the ``normalized`` variable is set to True, that location is
773
+ interpreted as in the normalized (-1..+1) space, otherwise it is in the
774
+ font's defined axes space.
775
+ """
776
+ if location and "fvar" not in self:
777
+ location = None
778
+ if location and not normalized:
779
+ location = self.normalizeLocation(location)
780
+ glyphSet = None
781
+ if ("CFF " in self or "CFF2" in self) and (preferCFF or "glyf" not in self):
782
+ glyphSet = _TTGlyphSetCFF(self, location)
783
+ elif "glyf" in self:
784
+ glyphSet = _TTGlyphSetGlyf(self, location, recalcBounds=recalcBounds)
785
+ else:
786
+ raise TTLibError("Font contains no outlines")
787
+ if "VARC" in self:
788
+ glyphSet = _TTGlyphSetVARC(self, location, glyphSet)
789
+ return glyphSet
790
+
791
+ def normalizeLocation(self, location):
792
+ """Normalize a ``location`` from the font's defined axes space (also
793
+ known as user space) into the normalized (-1..+1) space. It applies
794
+ ``avar`` mapping if the font contains an ``avar`` table.
795
+
796
+ The ``location`` parameter should be a dictionary mapping four-letter
797
+ variation tags to their float values.
798
+
799
+ Raises ``TTLibError`` if the font is not a variable font.
800
+ """
801
+ from fontTools.varLib.models import normalizeLocation
802
+
803
+ if "fvar" not in self:
804
+ raise TTLibError("Not a variable font")
805
+
806
+ axes = self["fvar"].getAxes()
807
+ location = normalizeLocation(location, axes)
808
+ if "avar" in self:
809
+ location = self["avar"].renormalizeLocation(location, self)
810
+ return location
811
+
812
+ def getBestCmap(
813
+ self,
814
+ cmapPreferences=(
815
+ (3, 10),
816
+ (0, 6),
817
+ (0, 4),
818
+ (3, 1),
819
+ (0, 3),
820
+ (0, 2),
821
+ (0, 1),
822
+ (0, 0),
823
+ ),
824
+ ):
825
+ """Returns the 'best' Unicode cmap dictionary available in the font
826
+ or ``None``, if no Unicode cmap subtable is available.
827
+
828
+ By default it will search for the following (platformID, platEncID)
829
+ pairs in order::
830
+
831
+ (3, 10), # Windows Unicode full repertoire
832
+ (0, 6), # Unicode full repertoire (format 13 subtable)
833
+ (0, 4), # Unicode 2.0 full repertoire
834
+ (3, 1), # Windows Unicode BMP
835
+ (0, 3), # Unicode 2.0 BMP
836
+ (0, 2), # Unicode ISO/IEC 10646
837
+ (0, 1), # Unicode 1.1
838
+ (0, 0) # Unicode 1.0
839
+
840
+ This particular order matches what HarfBuzz uses to choose what
841
+ subtable to use by default. This order prefers the largest-repertoire
842
+ subtable, and among those, prefers the Windows-platform over the
843
+ Unicode-platform as the former has wider support.
844
+
845
+ This order can be customized via the ``cmapPreferences`` argument.
846
+ """
847
+ return self["cmap"].getBestCmap(cmapPreferences=cmapPreferences)
848
+
849
+ def reorderGlyphs(self, new_glyph_order):
850
+ from .reorderGlyphs import reorderGlyphs
851
+
852
+ reorderGlyphs(self, new_glyph_order)
853
+
854
+
855
+ class GlyphOrder(object):
856
+ """A pseudo table. The glyph order isn't in the font as a separate
857
+ table, but it's nice to present it as such in the TTX format.
858
+ """
859
+
860
+ def __init__(self, tag=None):
861
+ pass
862
+
863
+ def toXML(self, writer, ttFont):
864
+ glyphOrder = ttFont.getGlyphOrder()
865
+ writer.comment(
866
+ "The 'id' attribute is only for humans; " "it is ignored when parsed."
867
+ )
868
+ writer.newline()
869
+ for i in range(len(glyphOrder)):
870
+ glyphName = glyphOrder[i]
871
+ writer.simpletag("GlyphID", id=i, name=glyphName)
872
+ writer.newline()
873
+
874
+ def fromXML(self, name, attrs, content, ttFont):
875
+ if not hasattr(self, "glyphOrder"):
876
+ self.glyphOrder = []
877
+ if name == "GlyphID":
878
+ self.glyphOrder.append(attrs["name"])
879
+ ttFont.setGlyphOrder(self.glyphOrder)
880
+
881
+
882
+ def getTableModule(tag):
883
+ """Fetch the packer/unpacker module for a table.
884
+ Return None when no module is found.
885
+ """
886
+ from . import tables
887
+
888
+ pyTag = tagToIdentifier(tag)
889
+ try:
890
+ __import__("fontTools.ttLib.tables." + pyTag)
891
+ except ImportError as err:
892
+ # If pyTag is found in the ImportError message,
893
+ # means table is not implemented. If it's not
894
+ # there, then some other module is missing, don't
895
+ # suppress the error.
896
+ if str(err).find(pyTag) >= 0:
897
+ return None
898
+ else:
899
+ raise err
900
+ else:
901
+ return getattr(tables, pyTag)
902
+
903
+
904
+ # Registry for custom table packer/unpacker classes. Keys are table
905
+ # tags, values are (moduleName, className) tuples.
906
+ # See registerCustomTableClass() and getCustomTableClass()
907
+ _customTableRegistry = {}
908
+
909
+
910
+ def registerCustomTableClass(tag, moduleName, className=None):
911
+ """Register a custom packer/unpacker class for a table.
912
+
913
+ The 'moduleName' must be an importable module. If no 'className'
914
+ is given, it is derived from the tag, for example it will be
915
+ ``table_C_U_S_T_`` for a 'CUST' tag.
916
+
917
+ The registered table class should be a subclass of
918
+ :py:class:`fontTools.ttLib.tables.DefaultTable.DefaultTable`
919
+ """
920
+ if className is None:
921
+ className = "table_" + tagToIdentifier(tag)
922
+ _customTableRegistry[tag] = (moduleName, className)
923
+
924
+
925
+ def unregisterCustomTableClass(tag):
926
+ """Unregister the custom packer/unpacker class for a table."""
927
+ del _customTableRegistry[tag]
928
+
929
+
930
+ def getCustomTableClass(tag):
931
+ """Return the custom table class for tag, if one has been registered
932
+ with 'registerCustomTableClass()'. Else return None.
933
+ """
934
+ if tag not in _customTableRegistry:
935
+ return None
936
+ import importlib
937
+
938
+ moduleName, className = _customTableRegistry[tag]
939
+ module = importlib.import_module(moduleName)
940
+ return getattr(module, className)
941
+
942
+
943
+ def getTableClass(tag):
944
+ """Fetch the packer/unpacker class for a table."""
945
+ tableClass = getCustomTableClass(tag)
946
+ if tableClass is not None:
947
+ return tableClass
948
+ module = getTableModule(tag)
949
+ if module is None:
950
+ from .tables.DefaultTable import DefaultTable
951
+
952
+ return DefaultTable
953
+ pyTag = tagToIdentifier(tag)
954
+ tableClass = getattr(module, "table_" + pyTag)
955
+ return tableClass
956
+
957
+
958
+ def getClassTag(klass):
959
+ """Fetch the table tag for a class object."""
960
+ name = klass.__name__
961
+ assert name[:6] == "table_"
962
+ name = name[6:] # Chop 'table_'
963
+ return identifierToTag(name)
964
+
965
+
966
+ def newTable(tag):
967
+ """Return a new instance of a table."""
968
+ tableClass = getTableClass(tag)
969
+ return tableClass(tag)
970
+
971
+
972
+ def _escapechar(c):
973
+ """Helper function for tagToIdentifier()"""
974
+ import re
975
+
976
+ if re.match("[a-z0-9]", c):
977
+ return "_" + c
978
+ elif re.match("[A-Z]", c):
979
+ return c + "_"
980
+ else:
981
+ return hex(byteord(c))[2:]
982
+
983
+
984
+ def tagToIdentifier(tag):
985
+ """Convert a table tag to a valid (but UGLY) python identifier,
986
+ as well as a filename that's guaranteed to be unique even on a
987
+ caseless file system. Each character is mapped to two characters.
988
+ Lowercase letters get an underscore before the letter, uppercase
989
+ letters get an underscore after the letter. Trailing spaces are
990
+ trimmed. Illegal characters are escaped as two hex bytes. If the
991
+ result starts with a number (as the result of a hex escape), an
992
+ extra underscore is prepended. Examples:
993
+ .. code-block:: pycon
994
+
995
+ >>>
996
+ >> tagToIdentifier('glyf')
997
+ '_g_l_y_f'
998
+ >> tagToIdentifier('cvt ')
999
+ '_c_v_t'
1000
+ >> tagToIdentifier('OS/2')
1001
+ 'O_S_2f_2'
1002
+ """
1003
+ import re
1004
+
1005
+ tag = Tag(tag)
1006
+ if tag == "GlyphOrder":
1007
+ return tag
1008
+ assert len(tag) == 4, "tag should be 4 characters long"
1009
+ while len(tag) > 1 and tag[-1] == " ":
1010
+ tag = tag[:-1]
1011
+ ident = ""
1012
+ for c in tag:
1013
+ ident = ident + _escapechar(c)
1014
+ if re.match("[0-9]", ident):
1015
+ ident = "_" + ident
1016
+ return ident
1017
+
1018
+
1019
+ def identifierToTag(ident):
1020
+ """the opposite of tagToIdentifier()"""
1021
+ if ident == "GlyphOrder":
1022
+ return ident
1023
+ if len(ident) % 2 and ident[0] == "_":
1024
+ ident = ident[1:]
1025
+ assert not (len(ident) % 2)
1026
+ tag = ""
1027
+ for i in range(0, len(ident), 2):
1028
+ if ident[i] == "_":
1029
+ tag = tag + ident[i + 1]
1030
+ elif ident[i + 1] == "_":
1031
+ tag = tag + ident[i]
1032
+ else:
1033
+ # assume hex
1034
+ tag = tag + chr(int(ident[i : i + 2], 16))
1035
+ # append trailing spaces
1036
+ tag = tag + (4 - len(tag)) * " "
1037
+ return Tag(tag)
1038
+
1039
+
1040
+ def tagToXML(tag):
1041
+ """Similarly to tagToIdentifier(), this converts a TT tag
1042
+ to a valid XML element name. Since XML element names are
1043
+ case sensitive, this is a fairly simple/readable translation.
1044
+ """
1045
+ import re
1046
+
1047
+ tag = Tag(tag)
1048
+ if tag == "OS/2":
1049
+ return "OS_2"
1050
+ elif tag == "GlyphOrder":
1051
+ return tag
1052
+ if re.match("[A-Za-z_][A-Za-z_0-9]* *$", tag):
1053
+ return tag.strip()
1054
+ else:
1055
+ return tagToIdentifier(tag)
1056
+
1057
+
1058
+ def xmlToTag(tag):
1059
+ """The opposite of tagToXML()"""
1060
+ if tag == "OS_2":
1061
+ return Tag("OS/2")
1062
+ if len(tag) == 8:
1063
+ return identifierToTag(tag)
1064
+ else:
1065
+ return Tag(tag + " " * (4 - len(tag)))
1066
+
1067
+
1068
+ # Table order as recommended in the OpenType specification 1.4
1069
+ TTFTableOrder = [
1070
+ "head",
1071
+ "hhea",
1072
+ "maxp",
1073
+ "OS/2",
1074
+ "hmtx",
1075
+ "LTSH",
1076
+ "VDMX",
1077
+ "hdmx",
1078
+ "cmap",
1079
+ "fpgm",
1080
+ "prep",
1081
+ "cvt ",
1082
+ "loca",
1083
+ "glyf",
1084
+ "kern",
1085
+ "name",
1086
+ "post",
1087
+ "gasp",
1088
+ "PCLT",
1089
+ ]
1090
+
1091
+ OTFTableOrder = ["head", "hhea", "maxp", "OS/2", "name", "cmap", "post", "CFF "]
1092
+
1093
+
1094
+ def sortedTagList(tagList, tableOrder=None):
1095
+ """Return a sorted copy of tagList, sorted according to the OpenType
1096
+ specification, or according to a custom tableOrder. If given and not
1097
+ None, tableOrder needs to be a list of tag names.
1098
+ """
1099
+ tagList = sorted(tagList)
1100
+ if tableOrder is None:
1101
+ if "DSIG" in tagList:
1102
+ # DSIG should be last (XXX spec reference?)
1103
+ tagList.remove("DSIG")
1104
+ tagList.append("DSIG")
1105
+ if "CFF " in tagList:
1106
+ tableOrder = OTFTableOrder
1107
+ else:
1108
+ tableOrder = TTFTableOrder
1109
+ orderedTables = []
1110
+ for tag in tableOrder:
1111
+ if tag in tagList:
1112
+ orderedTables.append(tag)
1113
+ tagList.remove(tag)
1114
+ orderedTables.extend(tagList)
1115
+ return orderedTables
1116
+
1117
+
1118
+ def reorderFontTables(inFile, outFile, tableOrder=None, checkChecksums=False):
1119
+ """Rewrite a font file, ordering the tables as recommended by the
1120
+ OpenType specification 1.4.
1121
+ """
1122
+ inFile.seek(0)
1123
+ outFile.seek(0)
1124
+ reader = SFNTReader(inFile, checkChecksums=checkChecksums)
1125
+ writer = SFNTWriter(
1126
+ outFile,
1127
+ len(reader.tables),
1128
+ reader.sfntVersion,
1129
+ reader.flavor,
1130
+ reader.flavorData,
1131
+ )
1132
+ tables = list(reader.keys())
1133
+ for tag in sortedTagList(tables, tableOrder):
1134
+ writer[tag] = reader[tag]
1135
+ writer.close()
1136
+
1137
+
1138
+ def maxPowerOfTwo(x):
1139
+ """Return the highest exponent of two, so that
1140
+ (2 ** exponent) <= x. Return 0 if x is 0.
1141
+ """
1142
+ exponent = 0
1143
+ while x:
1144
+ x = x >> 1
1145
+ exponent = exponent + 1
1146
+ return max(exponent - 1, 0)
1147
+
1148
+
1149
+ def getSearchRange(n, itemSize=16):
1150
+ """Calculate searchRange, entrySelector, rangeShift."""
1151
+ # itemSize defaults to 16, for backward compatibility
1152
+ # with upstream fonttools.
1153
+ exponent = maxPowerOfTwo(n)
1154
+ searchRange = (2**exponent) * itemSize
1155
+ entrySelector = exponent
1156
+ rangeShift = max(0, n * itemSize - searchRange)
1157
+ return searchRange, entrySelector, rangeShift