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,594 @@
1
+ """ttLib.tables.ttProgram.py -- Assembler/disassembler for TrueType bytecode programs."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from fontTools.misc.textTools import num2binary, binary2num, readHex, strjoin
6
+ import array
7
+ from io import StringIO
8
+ from typing import List
9
+ import re
10
+ import logging
11
+
12
+
13
+ log = logging.getLogger(__name__)
14
+
15
+ # fmt: off
16
+
17
+ # first, the list of instructions that eat bytes or words from the instruction stream
18
+
19
+ streamInstructions = [
20
+ #
21
+ # opcode mnemonic argBits descriptive name pops pushes eats from instruction stream pushes
22
+ #
23
+ (0x40, 'NPUSHB', 0, 'PushNBytes', 0, -1), # n, b1, b2,...bn b1,b2...bn
24
+ (0x41, 'NPUSHW', 0, 'PushNWords', 0, -1), # n, w1, w2,...w w1,w2...wn
25
+ (0xb0, 'PUSHB', 3, 'PushBytes', 0, -1), # b0, b1,..bn b0, b1, ...,bn
26
+ (0xb8, 'PUSHW', 3, 'PushWords', 0, -1), # w0,w1,..wn w0 ,w1, ...wn
27
+ ]
28
+
29
+
30
+ # next, the list of "normal" instructions
31
+
32
+ instructions = [
33
+ #
34
+ # opcode mnemonic argBits descriptive name pops pushes eats from instruction stream pushes
35
+ #
36
+ (0x7f, 'AA', 0, 'AdjustAngle', 1, 0), # p -
37
+ (0x64, 'ABS', 0, 'Absolute', 1, 1), # n |n|
38
+ (0x60, 'ADD', 0, 'Add', 2, 1), # n2, n1 (n1 + n2)
39
+ (0x27, 'ALIGNPTS', 0, 'AlignPts', 2, 0), # p2, p1 -
40
+ (0x3c, 'ALIGNRP', 0, 'AlignRelativePt', -1, 0), # p1, p2, ... , ploopvalue -
41
+ (0x5a, 'AND', 0, 'LogicalAnd', 2, 1), # e2, e1 b
42
+ (0x2b, 'CALL', 0, 'CallFunction', 1, 0), # f -
43
+ (0x67, 'CEILING', 0, 'Ceiling', 1, 1), # n ceil(n)
44
+ (0x25, 'CINDEX', 0, 'CopyXToTopStack', 1, 1), # k ek
45
+ (0x22, 'CLEAR', 0, 'ClearStack', -1, 0), # all items on the stack -
46
+ (0x4f, 'DEBUG', 0, 'DebugCall', 1, 0), # n -
47
+ (0x73, 'DELTAC1', 0, 'DeltaExceptionC1', -1, 0), # argn, cn, argn-1,cn-1, , arg1, c1 -
48
+ (0x74, 'DELTAC2', 0, 'DeltaExceptionC2', -1, 0), # argn, cn, argn-1,cn-1, , arg1, c1 -
49
+ (0x75, 'DELTAC3', 0, 'DeltaExceptionC3', -1, 0), # argn, cn, argn-1,cn-1, , arg1, c1 -
50
+ (0x5d, 'DELTAP1', 0, 'DeltaExceptionP1', -1, 0), # argn, pn, argn-1, pn-1, , arg1, p1 -
51
+ (0x71, 'DELTAP2', 0, 'DeltaExceptionP2', -1, 0), # argn, pn, argn-1, pn-1, , arg1, p1 -
52
+ (0x72, 'DELTAP3', 0, 'DeltaExceptionP3', -1, 0), # argn, pn, argn-1, pn-1, , arg1, p1 -
53
+ (0x24, 'DEPTH', 0, 'GetDepthStack', 0, 1), # - n
54
+ (0x62, 'DIV', 0, 'Divide', 2, 1), # n2, n1 (n1 * 64)/ n2
55
+ (0x20, 'DUP', 0, 'DuplicateTopStack', 1, 2), # e e, e
56
+ (0x59, 'EIF', 0, 'EndIf', 0, 0), # - -
57
+ (0x1b, 'ELSE', 0, 'Else', 0, 0), # - -
58
+ (0x2d, 'ENDF', 0, 'EndFunctionDefinition', 0, 0), # - -
59
+ (0x54, 'EQ', 0, 'Equal', 2, 1), # e2, e1 b
60
+ (0x57, 'EVEN', 0, 'Even', 1, 1), # e b
61
+ (0x2c, 'FDEF', 0, 'FunctionDefinition', 1, 0), # f -
62
+ (0x4e, 'FLIPOFF', 0, 'SetAutoFlipOff', 0, 0), # - -
63
+ (0x4d, 'FLIPON', 0, 'SetAutoFlipOn', 0, 0), # - -
64
+ (0x80, 'FLIPPT', 0, 'FlipPoint', -1, 0), # p1, p2, ..., ploopvalue -
65
+ (0x82, 'FLIPRGOFF', 0, 'FlipRangeOff', 2, 0), # h, l -
66
+ (0x81, 'FLIPRGON', 0, 'FlipRangeOn', 2, 0), # h, l -
67
+ (0x66, 'FLOOR', 0, 'Floor', 1, 1), # n floor(n)
68
+ (0x46, 'GC', 1, 'GetCoordOnPVector', 1, 1), # p c
69
+ (0x88, 'GETINFO', 0, 'GetInfo', 1, 1), # selector result
70
+ (0x91, 'GETVARIATION', 0, 'GetVariation', 0, -1), # - a1,..,an
71
+ (0x0d, 'GFV', 0, 'GetFVector', 0, 2), # - px, py
72
+ (0x0c, 'GPV', 0, 'GetPVector', 0, 2), # - px, py
73
+ (0x52, 'GT', 0, 'GreaterThan', 2, 1), # e2, e1 b
74
+ (0x53, 'GTEQ', 0, 'GreaterThanOrEqual', 2, 1), # e2, e1 b
75
+ (0x89, 'IDEF', 0, 'InstructionDefinition', 1, 0), # f -
76
+ (0x58, 'IF', 0, 'If', 1, 0), # e -
77
+ (0x8e, 'INSTCTRL', 0, 'SetInstrExecControl', 2, 0), # s, v -
78
+ (0x39, 'IP', 0, 'InterpolatePts', -1, 0), # p1, p2, ... , ploopvalue -
79
+ (0x0f, 'ISECT', 0, 'MovePtToIntersect', 5, 0), # a1, a0, b1, b0, p -
80
+ (0x30, 'IUP', 1, 'InterpolateUntPts', 0, 0), # - -
81
+ (0x1c, 'JMPR', 0, 'Jump', 1, 0), # offset -
82
+ (0x79, 'JROF', 0, 'JumpRelativeOnFalse', 2, 0), # e, offset -
83
+ (0x78, 'JROT', 0, 'JumpRelativeOnTrue', 2, 0), # e, offset -
84
+ (0x2a, 'LOOPCALL', 0, 'LoopAndCallFunction', 2, 0), # f, count -
85
+ (0x50, 'LT', 0, 'LessThan', 2, 1), # e2, e1 b
86
+ (0x51, 'LTEQ', 0, 'LessThenOrEqual', 2, 1), # e2, e1 b
87
+ (0x8b, 'MAX', 0, 'Maximum', 2, 1), # e2, e1 max(e1, e2)
88
+ (0x49, 'MD', 1, 'MeasureDistance', 2, 1), # p2,p1 d
89
+ (0x2e, 'MDAP', 1, 'MoveDirectAbsPt', 1, 0), # p -
90
+ (0xc0, 'MDRP', 5, 'MoveDirectRelPt', 1, 0), # p -
91
+ (0x3e, 'MIAP', 1, 'MoveIndirectAbsPt', 2, 0), # n, p -
92
+ (0x8c, 'MIN', 0, 'Minimum', 2, 1), # e2, e1 min(e1, e2)
93
+ (0x26, 'MINDEX', 0, 'MoveXToTopStack', 1, 1), # k ek
94
+ (0xe0, 'MIRP', 5, 'MoveIndirectRelPt', 2, 0), # n, p -
95
+ (0x4b, 'MPPEM', 0, 'MeasurePixelPerEm', 0, 1), # - ppem
96
+ (0x4c, 'MPS', 0, 'MeasurePointSize', 0, 1), # - pointSize
97
+ (0x3a, 'MSIRP', 1, 'MoveStackIndirRelPt', 2, 0), # d, p -
98
+ (0x63, 'MUL', 0, 'Multiply', 2, 1), # n2, n1 (n1 * n2)/64
99
+ (0x65, 'NEG', 0, 'Negate', 1, 1), # n -n
100
+ (0x55, 'NEQ', 0, 'NotEqual', 2, 1), # e2, e1 b
101
+ (0x5c, 'NOT', 0, 'LogicalNot', 1, 1), # e ( not e )
102
+ (0x6c, 'NROUND', 2, 'NoRound', 1, 1), # n1 n2
103
+ (0x56, 'ODD', 0, 'Odd', 1, 1), # e b
104
+ (0x5b, 'OR', 0, 'LogicalOr', 2, 1), # e2, e1 b
105
+ (0x21, 'POP', 0, 'PopTopStack', 1, 0), # e -
106
+ (0x45, 'RCVT', 0, 'ReadCVT', 1, 1), # location value
107
+ (0x7d, 'RDTG', 0, 'RoundDownToGrid', 0, 0), # - -
108
+ (0x7a, 'ROFF', 0, 'RoundOff', 0, 0), # - -
109
+ (0x8a, 'ROLL', 0, 'RollTopThreeStack', 3, 3), # a,b,c b,a,c
110
+ (0x68, 'ROUND', 2, 'Round', 1, 1), # n1 n2
111
+ (0x43, 'RS', 0, 'ReadStore', 1, 1), # n v
112
+ (0x3d, 'RTDG', 0, 'RoundToDoubleGrid', 0, 0), # - -
113
+ (0x18, 'RTG', 0, 'RoundToGrid', 0, 0), # - -
114
+ (0x19, 'RTHG', 0, 'RoundToHalfGrid', 0, 0), # - -
115
+ (0x7c, 'RUTG', 0, 'RoundUpToGrid', 0, 0), # - -
116
+ (0x77, 'S45ROUND', 0, 'SuperRound45Degrees', 1, 0), # n -
117
+ (0x7e, 'SANGW', 0, 'SetAngleWeight', 1, 0), # weight -
118
+ (0x85, 'SCANCTRL', 0, 'ScanConversionControl', 1, 0), # n -
119
+ (0x8d, 'SCANTYPE', 0, 'ScanType', 1, 0), # n -
120
+ (0x48, 'SCFS', 0, 'SetCoordFromStackFP', 2, 0), # c, p -
121
+ (0x1d, 'SCVTCI', 0, 'SetCVTCutIn', 1, 0), # n -
122
+ (0x5e, 'SDB', 0, 'SetDeltaBaseInGState', 1, 0), # n -
123
+ (0x86, 'SDPVTL', 1, 'SetDualPVectorToLine', 2, 0), # p2, p1 -
124
+ (0x5f, 'SDS', 0, 'SetDeltaShiftInGState', 1, 0), # n -
125
+ (0x0b, 'SFVFS', 0, 'SetFVectorFromStack', 2, 0), # y, x -
126
+ (0x04, 'SFVTCA', 1, 'SetFVectorToAxis', 0, 0), # - -
127
+ (0x08, 'SFVTL', 1, 'SetFVectorToLine', 2, 0), # p2, p1 -
128
+ (0x0e, 'SFVTPV', 0, 'SetFVectorToPVector', 0, 0), # - -
129
+ (0x34, 'SHC', 1, 'ShiftContourByLastPt', 1, 0), # c -
130
+ (0x32, 'SHP', 1, 'ShiftPointByLastPoint', -1, 0), # p1, p2, ..., ploopvalue -
131
+ (0x38, 'SHPIX', 0, 'ShiftZoneByPixel', -1, 0), # d, p1, p2, ..., ploopvalue -
132
+ (0x36, 'SHZ', 1, 'ShiftZoneByLastPoint', 1, 0), # e -
133
+ (0x17, 'SLOOP', 0, 'SetLoopVariable', 1, 0), # n -
134
+ (0x1a, 'SMD', 0, 'SetMinimumDistance', 1, 0), # distance -
135
+ (0x0a, 'SPVFS', 0, 'SetPVectorFromStack', 2, 0), # y, x -
136
+ (0x02, 'SPVTCA', 1, 'SetPVectorToAxis', 0, 0), # - -
137
+ (0x06, 'SPVTL', 1, 'SetPVectorToLine', 2, 0), # p2, p1 -
138
+ (0x76, 'SROUND', 0, 'SuperRound', 1, 0), # n -
139
+ (0x10, 'SRP0', 0, 'SetRefPoint0', 1, 0), # p -
140
+ (0x11, 'SRP1', 0, 'SetRefPoint1', 1, 0), # p -
141
+ (0x12, 'SRP2', 0, 'SetRefPoint2', 1, 0), # p -
142
+ (0x1f, 'SSW', 0, 'SetSingleWidth', 1, 0), # n -
143
+ (0x1e, 'SSWCI', 0, 'SetSingleWidthCutIn', 1, 0), # n -
144
+ (0x61, 'SUB', 0, 'Subtract', 2, 1), # n2, n1 (n1 - n2)
145
+ (0x00, 'SVTCA', 1, 'SetFPVectorToAxis', 0, 0), # - -
146
+ (0x23, 'SWAP', 0, 'SwapTopStack', 2, 2), # e2, e1 e1, e2
147
+ (0x13, 'SZP0', 0, 'SetZonePointer0', 1, 0), # n -
148
+ (0x14, 'SZP1', 0, 'SetZonePointer1', 1, 0), # n -
149
+ (0x15, 'SZP2', 0, 'SetZonePointer2', 1, 0), # n -
150
+ (0x16, 'SZPS', 0, 'SetZonePointerS', 1, 0), # n -
151
+ (0x29, 'UTP', 0, 'UnTouchPt', 1, 0), # p -
152
+ (0x70, 'WCVTF', 0, 'WriteCVTInFUnits', 2, 0), # n, l -
153
+ (0x44, 'WCVTP', 0, 'WriteCVTInPixels', 2, 0), # v, l -
154
+ (0x42, 'WS', 0, 'WriteStore', 2, 0), # v, l -
155
+ ]
156
+
157
+ # fmt: on
158
+
159
+
160
+ def bitRepr(value, bits):
161
+ s = ""
162
+ for i in range(bits):
163
+ s = "01"[value & 0x1] + s
164
+ value = value >> 1
165
+ return s
166
+
167
+
168
+ _mnemonicPat = re.compile(r"[A-Z][A-Z0-9]*$")
169
+
170
+
171
+ def _makeDict(instructionList):
172
+ opcodeDict = {}
173
+ mnemonicDict = {}
174
+ for op, mnemonic, argBits, name, pops, pushes in instructionList:
175
+ assert _mnemonicPat.match(mnemonic)
176
+ mnemonicDict[mnemonic] = op, argBits, name
177
+ if argBits:
178
+ argoffset = op
179
+ for i in range(1 << argBits):
180
+ opcodeDict[op + i] = mnemonic, argBits, argoffset, name
181
+ else:
182
+ opcodeDict[op] = mnemonic, 0, 0, name
183
+ return opcodeDict, mnemonicDict
184
+
185
+
186
+ streamOpcodeDict, streamMnemonicDict = _makeDict(streamInstructions)
187
+ opcodeDict, mnemonicDict = _makeDict(instructions)
188
+
189
+
190
+ class tt_instructions_error(Exception):
191
+ def __init__(self, error):
192
+ self.error = error
193
+
194
+ def __str__(self):
195
+ return "TT instructions error: %s" % repr(self.error)
196
+
197
+
198
+ _comment = r"/\*.*?\*/"
199
+ _instruction = r"([A-Z][A-Z0-9]*)\s*\[(.*?)\]"
200
+ _number = r"-?[0-9]+"
201
+ _token = "(%s)|(%s)|(%s)" % (_instruction, _number, _comment)
202
+
203
+ _tokenRE = re.compile(_token)
204
+ _whiteRE = re.compile(r"\s*")
205
+
206
+ _pushCountPat = re.compile(r"[A-Z][A-Z0-9]*\s*\[.*?\]\s*/\* ([0-9]+).*?\*/")
207
+
208
+ _indentRE = re.compile(r"^FDEF|IF|ELSE\[ \]\t.+")
209
+ _unindentRE = re.compile(r"^ELSE|ENDF|EIF\[ \]\t.+")
210
+
211
+
212
+ def _skipWhite(data, pos):
213
+ m = _whiteRE.match(data, pos)
214
+ newPos = m.regs[0][1]
215
+ assert newPos >= pos
216
+ return newPos
217
+
218
+
219
+ class Program(object):
220
+ def __init__(self) -> None:
221
+ pass
222
+
223
+ def fromBytecode(self, bytecode: bytes) -> None:
224
+ self.bytecode = array.array("B", bytecode)
225
+ if hasattr(self, "assembly"):
226
+ del self.assembly
227
+
228
+ def fromAssembly(self, assembly: List[str] | str) -> None:
229
+ if isinstance(assembly, list):
230
+ self.assembly = assembly
231
+ elif isinstance(assembly, str):
232
+ self.assembly = assembly.splitlines()
233
+ else:
234
+ raise TypeError(f"expected str or List[str], got {type(assembly).__name__}")
235
+ if hasattr(self, "bytecode"):
236
+ del self.bytecode
237
+
238
+ def getBytecode(self) -> bytes:
239
+ if not hasattr(self, "bytecode"):
240
+ self._assemble()
241
+ return self.bytecode.tobytes()
242
+
243
+ def getAssembly(self, preserve=True) -> List[str]:
244
+ if not hasattr(self, "assembly"):
245
+ self._disassemble(preserve=preserve)
246
+ return self.assembly
247
+
248
+ def toXML(self, writer, ttFont) -> None:
249
+ if (
250
+ not hasattr(ttFont, "disassembleInstructions")
251
+ or ttFont.disassembleInstructions
252
+ ):
253
+ try:
254
+ assembly = self.getAssembly()
255
+ except:
256
+ import traceback
257
+
258
+ tmp = StringIO()
259
+ traceback.print_exc(file=tmp)
260
+ msg = "An exception occurred during the decompilation of glyph program:\n\n"
261
+ msg += tmp.getvalue()
262
+ log.error(msg)
263
+ writer.begintag("bytecode")
264
+ writer.newline()
265
+ writer.comment(msg.strip())
266
+ writer.newline()
267
+ writer.dumphex(self.getBytecode())
268
+ writer.endtag("bytecode")
269
+ writer.newline()
270
+ else:
271
+ if not assembly:
272
+ return
273
+ writer.begintag("assembly")
274
+ writer.newline()
275
+ i = 0
276
+ indent = 0
277
+ nInstr = len(assembly)
278
+ while i < nInstr:
279
+ instr = assembly[i]
280
+ if _unindentRE.match(instr):
281
+ indent -= 1
282
+ writer.write(writer.indentwhite * indent)
283
+ writer.write(instr)
284
+ writer.newline()
285
+ m = _pushCountPat.match(instr)
286
+ i = i + 1
287
+ if m:
288
+ nValues = int(m.group(1))
289
+ line: List[str] = []
290
+ j = 0
291
+ for j in range(nValues):
292
+ if j and not (j % 25):
293
+ writer.write(writer.indentwhite * indent)
294
+ writer.write(" ".join(line))
295
+ writer.newline()
296
+ line = []
297
+ line.append(assembly[i + j])
298
+ writer.write(writer.indentwhite * indent)
299
+ writer.write(" ".join(line))
300
+ writer.newline()
301
+ i = i + j + 1
302
+ if _indentRE.match(instr):
303
+ indent += 1
304
+ writer.endtag("assembly")
305
+ writer.newline()
306
+ else:
307
+ bytecode = self.getBytecode()
308
+ if not bytecode:
309
+ return
310
+ writer.begintag("bytecode")
311
+ writer.newline()
312
+ writer.dumphex(bytecode)
313
+ writer.endtag("bytecode")
314
+ writer.newline()
315
+
316
+ def fromXML(self, name, attrs, content, ttFont) -> None:
317
+ if name == "assembly":
318
+ self.fromAssembly(strjoin(content))
319
+ self._assemble()
320
+ del self.assembly
321
+ else:
322
+ assert name == "bytecode"
323
+ self.fromBytecode(readHex(content))
324
+
325
+ def _assemble(self) -> None:
326
+ assembly = " ".join(getattr(self, "assembly", []))
327
+ bytecode: List[int] = []
328
+ push = bytecode.append
329
+ lenAssembly = len(assembly)
330
+ pos = _skipWhite(assembly, 0)
331
+ while pos < lenAssembly:
332
+ m = _tokenRE.match(assembly, pos)
333
+ if m is None:
334
+ raise tt_instructions_error(
335
+ "Syntax error in TT program (%s)" % assembly[pos - 5 : pos + 15]
336
+ )
337
+ dummy, mnemonic, arg, number, comment = m.groups()
338
+ pos = m.regs[0][1]
339
+ if comment:
340
+ pos = _skipWhite(assembly, pos)
341
+ continue
342
+
343
+ arg = arg.strip()
344
+ if mnemonic.startswith("INSTR"):
345
+ # Unknown instruction
346
+ op = int(mnemonic[5:])
347
+ push(op)
348
+ elif mnemonic not in ("PUSH", "NPUSHB", "NPUSHW", "PUSHB", "PUSHW"):
349
+ op, argBits, name = mnemonicDict[mnemonic]
350
+ if len(arg) != argBits:
351
+ raise tt_instructions_error(
352
+ "Incorrect number of argument bits (%s[%s])" % (mnemonic, arg)
353
+ )
354
+ if arg:
355
+ arg = binary2num(arg)
356
+ push(op + arg)
357
+ else:
358
+ push(op)
359
+ else:
360
+ args = []
361
+ pos = _skipWhite(assembly, pos)
362
+ while pos < lenAssembly:
363
+ m = _tokenRE.match(assembly, pos)
364
+ if m is None:
365
+ raise tt_instructions_error(
366
+ "Syntax error in TT program (%s)" % assembly[pos : pos + 15]
367
+ )
368
+ dummy, _mnemonic, arg, number, comment = m.groups()
369
+ if number is None and comment is None:
370
+ break
371
+ pos = m.regs[0][1]
372
+ pos = _skipWhite(assembly, pos)
373
+ if comment is not None:
374
+ continue
375
+ args.append(int(number))
376
+ nArgs = len(args)
377
+ if mnemonic == "PUSH":
378
+ # Automatically choose the most compact representation
379
+ nWords = 0
380
+ while nArgs:
381
+ while (
382
+ nWords < nArgs
383
+ and nWords < 255
384
+ and not (0 <= args[nWords] <= 255)
385
+ ):
386
+ nWords += 1
387
+ nBytes = 0
388
+ while (
389
+ nWords + nBytes < nArgs
390
+ and nBytes < 255
391
+ and 0 <= args[nWords + nBytes] <= 255
392
+ ):
393
+ nBytes += 1
394
+ if (
395
+ nBytes < 2
396
+ and nWords + nBytes < 255
397
+ and nWords + nBytes != nArgs
398
+ ):
399
+ # Will write bytes as words
400
+ nWords += nBytes
401
+ continue
402
+
403
+ # Write words
404
+ if nWords:
405
+ if nWords <= 8:
406
+ op, argBits, name = streamMnemonicDict["PUSHW"]
407
+ op = op + nWords - 1
408
+ push(op)
409
+ else:
410
+ op, argBits, name = streamMnemonicDict["NPUSHW"]
411
+ push(op)
412
+ push(nWords)
413
+ for value in args[:nWords]:
414
+ assert -32768 <= value < 32768, (
415
+ "PUSH value out of range %d" % value
416
+ )
417
+ push((value >> 8) & 0xFF)
418
+ push(value & 0xFF)
419
+
420
+ # Write bytes
421
+ if nBytes:
422
+ pass
423
+ if nBytes <= 8:
424
+ op, argBits, name = streamMnemonicDict["PUSHB"]
425
+ op = op + nBytes - 1
426
+ push(op)
427
+ else:
428
+ op, argBits, name = streamMnemonicDict["NPUSHB"]
429
+ push(op)
430
+ push(nBytes)
431
+ for value in args[nWords : nWords + nBytes]:
432
+ push(value)
433
+
434
+ nTotal = nWords + nBytes
435
+ args = args[nTotal:]
436
+ nArgs -= nTotal
437
+ nWords = 0
438
+ else:
439
+ # Write exactly what we've been asked to
440
+ words = mnemonic[-1] == "W"
441
+ op, argBits, name = streamMnemonicDict[mnemonic]
442
+ if mnemonic[0] != "N":
443
+ assert nArgs <= 8, nArgs
444
+ op = op + nArgs - 1
445
+ push(op)
446
+ else:
447
+ assert nArgs < 256
448
+ push(op)
449
+ push(nArgs)
450
+ if words:
451
+ for value in args:
452
+ assert -32768 <= value < 32768, (
453
+ "PUSHW value out of range %d" % value
454
+ )
455
+ push((value >> 8) & 0xFF)
456
+ push(value & 0xFF)
457
+ else:
458
+ for value in args:
459
+ assert 0 <= value < 256, (
460
+ "PUSHB value out of range %d" % value
461
+ )
462
+ push(value)
463
+
464
+ pos = _skipWhite(assembly, pos)
465
+
466
+ if bytecode:
467
+ assert max(bytecode) < 256 and min(bytecode) >= 0
468
+ self.bytecode = array.array("B", bytecode)
469
+
470
+ def _disassemble(self, preserve=False) -> None:
471
+ assembly = []
472
+ i = 0
473
+ bytecode = getattr(self, "bytecode", [])
474
+ numBytecode = len(bytecode)
475
+ while i < numBytecode:
476
+ op = bytecode[i]
477
+ try:
478
+ mnemonic, argBits, argoffset, name = opcodeDict[op]
479
+ except KeyError:
480
+ if op in streamOpcodeDict:
481
+ values = []
482
+
483
+ # Merge consecutive PUSH operations
484
+ while bytecode[i] in streamOpcodeDict:
485
+ op = bytecode[i]
486
+ mnemonic, argBits, argoffset, name = streamOpcodeDict[op]
487
+ words = mnemonic[-1] == "W"
488
+ if argBits:
489
+ nValues = op - argoffset + 1
490
+ else:
491
+ i = i + 1
492
+ nValues = bytecode[i]
493
+ i = i + 1
494
+ assert nValues > 0
495
+ if not words:
496
+ for j in range(nValues):
497
+ value = bytecode[i]
498
+ values.append(repr(value))
499
+ i = i + 1
500
+ else:
501
+ for j in range(nValues):
502
+ # cast to signed int16
503
+ value = (bytecode[i] << 8) | bytecode[i + 1]
504
+ if value >= 0x8000:
505
+ value = value - 0x10000
506
+ values.append(repr(value))
507
+ i = i + 2
508
+ if preserve:
509
+ break
510
+
511
+ if not preserve:
512
+ mnemonic = "PUSH"
513
+ nValues = len(values)
514
+ if nValues == 1:
515
+ assembly.append("%s[ ] /* 1 value pushed */" % mnemonic)
516
+ else:
517
+ assembly.append(
518
+ "%s[ ] /* %s values pushed */" % (mnemonic, nValues)
519
+ )
520
+ assembly.extend(values)
521
+ else:
522
+ assembly.append("INSTR%d[ ]" % op)
523
+ i = i + 1
524
+ else:
525
+ if argBits:
526
+ assembly.append(
527
+ mnemonic
528
+ + "[%s] /* %s */" % (num2binary(op - argoffset, argBits), name)
529
+ )
530
+ else:
531
+ assembly.append(mnemonic + "[ ] /* %s */" % name)
532
+ i = i + 1
533
+ self.assembly = assembly
534
+
535
+ def __bool__(self) -> bool:
536
+ """
537
+ >>> p = Program()
538
+ >>> bool(p)
539
+ False
540
+ >>> bc = array.array("B", [0])
541
+ >>> p.fromBytecode(bc)
542
+ >>> bool(p)
543
+ True
544
+ >>> p.bytecode.pop()
545
+ 0
546
+ >>> bool(p)
547
+ False
548
+
549
+ >>> p = Program()
550
+ >>> asm = ['SVTCA[0]']
551
+ >>> p.fromAssembly(asm)
552
+ >>> bool(p)
553
+ True
554
+ >>> p.assembly.pop()
555
+ 'SVTCA[0]'
556
+ >>> bool(p)
557
+ False
558
+ """
559
+ return (hasattr(self, "assembly") and len(self.assembly) > 0) or (
560
+ hasattr(self, "bytecode") and len(self.bytecode) > 0
561
+ )
562
+
563
+ __nonzero__ = __bool__
564
+
565
+ def __eq__(self, other) -> bool:
566
+ if type(self) != type(other):
567
+ return NotImplemented
568
+ return self.__dict__ == other.__dict__
569
+
570
+ def __ne__(self, other) -> bool:
571
+ result = self.__eq__(other)
572
+ return result if result is NotImplemented else not result
573
+
574
+
575
+ def _test():
576
+ """
577
+ >>> _test()
578
+ True
579
+ """
580
+
581
+ bc = b"""@;:9876543210/.-,+*)(\'&%$#"! \037\036\035\034\033\032\031\030\027\026\025\024\023\022\021\020\017\016\015\014\013\012\011\010\007\006\005\004\003\002\001\000,\001\260\030CXEj\260\031C`\260F#D#\020 \260FN\360M/\260\000\022\033!#\0213Y-,\001\260\030CX\260\005+\260\000\023K\260\024PX\261\000@8Y\260\006+\033!#\0213Y-,\001\260\030CXN\260\003%\020\362!\260\000\022M\033 E\260\004%\260\004%#Jad\260(RX!#\020\326\033\260\003%\020\362!\260\000\022YY-,\260\032CX!!\033\260\002%\260\002%I\260\003%\260\003%Ja d\260\020PX!!!\033\260\003%\260\003%I\260\000PX\260\000PX\270\377\3428!\033\260\0208!Y\033\260\000RX\260\0368!\033\270\377\3608!YYYY-,\001\260\030CX\260\005+\260\000\023K\260\024PX\271\000\000\377\3008Y\260\006+\033!#\0213Y-,N\001\212\020\261F\031CD\260\000\024\261\000F\342\260\000\025\271\000\000\377\3608\000\260\000<\260(+\260\002%\020\260\000<-,\001\030\260\000/\260\001\024\362\260\001\023\260\001\025M\260\000\022-,\001\260\030CX\260\005+\260\000\023\271\000\000\377\3408\260\006+\033!#\0213Y-,\001\260\030CXEdj#Edi\260\031Cd``\260F#D#\020 \260F\360/\260\000\022\033!! \212 \212RX\0213\033!!YY-,\001\261\013\012C#Ce\012-,\000\261\012\013C#C\013-,\000\260F#p\261\001F>\001\260F#p\261\002FE:\261\002\000\010\015-,\260\022+\260\002%E\260\002%Ej\260@\213`\260\002%#D!!!-,\260\023+\260\002%E\260\002%Ej\270\377\300\214`\260\002%#D!!!-,\260\000\260\022+!!!-,\260\000\260\023+!!!-,\001\260\006C\260\007Ce\012-, i\260@a\260\000\213 \261,\300\212\214\270\020\000b`+\014d#da\\X\260\003aY-,\261\000\003%EhT\260\034KPZX\260\003%E\260\003%E`h \260\004%#D\260\004%#D\033\260\003% Eh \212#D\260\003%Eh`\260\003%#DY-,\260\003% Eh \212#D\260\003%Edhe`\260\004%\260\001`#D-,\260\011CX\207!\300\033\260\022CX\207E\260\021+\260G#D\260Gz\344\033\003\212E\030i \260G#D\212\212\207 \260\240QX\260\021+\260G#D\260Gz\344\033!\260Gz\344YYY\030-, \212E#Eh`D-,EjB-,\001\030/-,\001\260\030CX\260\004%\260\004%Id#Edi\260@\213a \260\200bj\260\002%\260\002%a\214\260\031C`\260F#D!\212\020\260F\366!\033!!!!Y-,\001\260\030CX\260\002%E\260\002%Ed`j\260\003%Eja \260\004%Ej \212\213e\260\004%#D\214\260\003%#D!!\033 EjD EjDY-,\001 E\260\000U\260\030CZXEh#Ei\260@\213a \260\200bj \212#a \260\003%\213e\260\004%#D\214\260\003%#D!!\033!!\260\031+Y-,\001\212\212Ed#EdadB-,\260\004%\260\004%\260\031+\260\030CX\260\004%\260\004%\260\003%\260\033+\001\260\002%C\260@T\260\002%C\260\000TZX\260\003% E\260@aDY\260\002%C\260\000T\260\002%C\260@TZX\260\004% E\260@`DYY!!!!-,\001KRXC\260\002%E#aD\033!!Y-,\001KRXC\260\002%E#`D\033!!Y-,KRXED\033!!Y-,\001 \260\003%#I\260@`\260 c \260\000RX#\260\002%8#\260\002%e8\000\212c8\033!!!!!Y\001-,KPXED\033!!Y-,\001\260\005%\020# \212\365\000\260\001`#\355\354-,\001\260\005%\020# \212\365\000\260\001a#\355\354-,\001\260\006%\020\365\000\355\354-,F#F`\212\212F# F\212`\212a\270\377\200b# \020#\212\261KK\212pE` \260\000PX\260\001a\270\377\272\213\033\260F\214Y\260\020`h\001:-, E\260\003%FRX\260\002%F ha\260\003%\260\003%?#!8\033!\021Y-, E\260\003%FPX\260\002%F ha\260\003%\260\003%?#!8\033!\021Y-,\000\260\007C\260\006C\013-,\212\020\354-,\260\014CX!\033 F\260\000RX\270\377\3608\033\260\0208YY-, \260\000UX\270\020\000c\260\003%Ed\260\003%Eda\260\000SX\260\002\033\260@a\260\003Y%EiSXED\033!!Y\033!\260\002%E\260\002%Ead\260(QXED\033!!YY-,!!\014d#d\213\270@\000b-,!\260\200QX\014d#d\213\270 \000b\033\262\000@/+Y\260\002`-,!\260\300QX\014d#d\213\270\025Ub\033\262\000\200/+Y\260\002`-,\014d#d\213\270@\000b`#!-,KSX\260\004%\260\004%Id#Edi\260@\213a \260\200bj\260\002%\260\002%a\214\260F#D!\212\020\260F\366!\033!\212\021#\022 9/Y-,\260\002%\260\002%Id\260\300TX\270\377\3708\260\0108\033!!Y-,\260\023CX\003\033\002Y-,\260\023CX\002\033\003Y-,\260\012+#\020 <\260\027+-,\260\002%\270\377\3608\260(+\212\020# \320#\260\020+\260\005CX\300\033<Y \020\021\260\000\022\001-,KS#KQZX8\033!!Y-,\001\260\002%\020\320#\311\001\260\001\023\260\000\024\020\260\001<\260\001\026-,\001\260\000\023\260\001\260\003%I\260\003\0278\260\001\023-,KS#KQZX E\212`D\033!!Y-, 9/-"""
582
+
583
+ p = Program()
584
+ p.fromBytecode(bc)
585
+ asm = p.getAssembly(preserve=True)
586
+ p.fromAssembly(asm)
587
+ print(bc == p.getBytecode())
588
+
589
+
590
+ if __name__ == "__main__":
591
+ import sys
592
+ import doctest
593
+
594
+ sys.exit(doctest.testmod().failed)