meerk40t 0.9.3001__py2.py3-none-any.whl → 0.9.7010__py2.py3-none-any.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 (445) hide show
  1. meerk40t/__init__.py +1 -1
  2. meerk40t/balormk/balor_params.py +167 -167
  3. meerk40t/balormk/clone_loader.py +457 -457
  4. meerk40t/balormk/controller.py +1566 -1512
  5. meerk40t/balormk/cylindermod.py +64 -0
  6. meerk40t/balormk/device.py +966 -1959
  7. meerk40t/balormk/driver.py +778 -591
  8. meerk40t/balormk/galvo_commands.py +1195 -0
  9. meerk40t/balormk/gui/balorconfig.py +237 -111
  10. meerk40t/balormk/gui/balorcontroller.py +191 -184
  11. meerk40t/balormk/gui/baloroperationproperties.py +116 -115
  12. meerk40t/balormk/gui/corscene.py +845 -0
  13. meerk40t/balormk/gui/gui.py +179 -147
  14. meerk40t/balormk/livelightjob.py +466 -382
  15. meerk40t/balormk/mock_connection.py +131 -109
  16. meerk40t/balormk/plugin.py +133 -135
  17. meerk40t/balormk/usb_connection.py +306 -301
  18. meerk40t/camera/__init__.py +1 -1
  19. meerk40t/camera/camera.py +514 -397
  20. meerk40t/camera/gui/camerapanel.py +1241 -1095
  21. meerk40t/camera/gui/gui.py +58 -58
  22. meerk40t/camera/plugin.py +441 -399
  23. meerk40t/ch341/__init__.py +27 -27
  24. meerk40t/ch341/ch341device.py +628 -628
  25. meerk40t/ch341/libusb.py +595 -589
  26. meerk40t/ch341/mock.py +171 -171
  27. meerk40t/ch341/windriver.py +157 -157
  28. meerk40t/constants.py +13 -0
  29. meerk40t/core/__init__.py +1 -1
  30. meerk40t/core/bindalias.py +550 -539
  31. meerk40t/core/core.py +47 -47
  32. meerk40t/core/cutcode/cubiccut.py +73 -73
  33. meerk40t/core/cutcode/cutcode.py +315 -312
  34. meerk40t/core/cutcode/cutgroup.py +141 -137
  35. meerk40t/core/cutcode/cutobject.py +192 -185
  36. meerk40t/core/cutcode/dwellcut.py +37 -37
  37. meerk40t/core/cutcode/gotocut.py +29 -29
  38. meerk40t/core/cutcode/homecut.py +29 -29
  39. meerk40t/core/cutcode/inputcut.py +34 -34
  40. meerk40t/core/cutcode/linecut.py +33 -33
  41. meerk40t/core/cutcode/outputcut.py +34 -34
  42. meerk40t/core/cutcode/plotcut.py +335 -335
  43. meerk40t/core/cutcode/quadcut.py +61 -61
  44. meerk40t/core/cutcode/rastercut.py +168 -148
  45. meerk40t/core/cutcode/waitcut.py +34 -34
  46. meerk40t/core/cutplan.py +1843 -1316
  47. meerk40t/core/drivers.py +330 -329
  48. meerk40t/core/elements/align.py +801 -669
  49. meerk40t/core/elements/branches.py +1844 -1507
  50. meerk40t/core/elements/clipboard.py +229 -219
  51. meerk40t/core/elements/element_treeops.py +4561 -2837
  52. meerk40t/core/elements/element_types.py +125 -105
  53. meerk40t/core/elements/elements.py +4329 -3617
  54. meerk40t/core/elements/files.py +117 -64
  55. meerk40t/core/elements/geometry.py +473 -224
  56. meerk40t/core/elements/grid.py +467 -316
  57. meerk40t/core/elements/materials.py +158 -94
  58. meerk40t/core/elements/notes.py +50 -38
  59. meerk40t/core/elements/offset_clpr.py +933 -912
  60. meerk40t/core/elements/offset_mk.py +963 -955
  61. meerk40t/core/elements/penbox.py +339 -267
  62. meerk40t/core/elements/placements.py +300 -83
  63. meerk40t/core/elements/render.py +785 -687
  64. meerk40t/core/elements/shapes.py +2618 -2092
  65. meerk40t/core/elements/trace.py +651 -563
  66. meerk40t/core/elements/tree_commands.py +415 -409
  67. meerk40t/core/elements/undo_redo.py +116 -58
  68. meerk40t/core/elements/wordlist.py +319 -200
  69. meerk40t/core/exceptions.py +9 -9
  70. meerk40t/core/laserjob.py +220 -220
  71. meerk40t/core/logging.py +63 -63
  72. meerk40t/core/node/blobnode.py +83 -86
  73. meerk40t/core/node/bootstrap.py +105 -103
  74. meerk40t/core/node/branch_elems.py +40 -31
  75. meerk40t/core/node/branch_ops.py +45 -38
  76. meerk40t/core/node/branch_regmark.py +48 -41
  77. meerk40t/core/node/cutnode.py +29 -32
  78. meerk40t/core/node/effect_hatch.py +375 -257
  79. meerk40t/core/node/effect_warp.py +398 -0
  80. meerk40t/core/node/effect_wobble.py +441 -309
  81. meerk40t/core/node/elem_ellipse.py +404 -309
  82. meerk40t/core/node/elem_image.py +1082 -801
  83. meerk40t/core/node/elem_line.py +358 -292
  84. meerk40t/core/node/elem_path.py +259 -201
  85. meerk40t/core/node/elem_point.py +129 -102
  86. meerk40t/core/node/elem_polyline.py +310 -246
  87. meerk40t/core/node/elem_rect.py +376 -286
  88. meerk40t/core/node/elem_text.py +445 -418
  89. meerk40t/core/node/filenode.py +59 -40
  90. meerk40t/core/node/groupnode.py +138 -74
  91. meerk40t/core/node/image_processed.py +777 -766
  92. meerk40t/core/node/image_raster.py +156 -113
  93. meerk40t/core/node/layernode.py +31 -31
  94. meerk40t/core/node/mixins.py +135 -107
  95. meerk40t/core/node/node.py +1427 -1304
  96. meerk40t/core/node/nutils.py +117 -114
  97. meerk40t/core/node/op_cut.py +462 -335
  98. meerk40t/core/node/op_dots.py +296 -251
  99. meerk40t/core/node/op_engrave.py +414 -311
  100. meerk40t/core/node/op_image.py +755 -369
  101. meerk40t/core/node/op_raster.py +787 -522
  102. meerk40t/core/node/place_current.py +37 -40
  103. meerk40t/core/node/place_point.py +329 -126
  104. meerk40t/core/node/refnode.py +58 -47
  105. meerk40t/core/node/rootnode.py +225 -219
  106. meerk40t/core/node/util_console.py +48 -48
  107. meerk40t/core/node/util_goto.py +84 -65
  108. meerk40t/core/node/util_home.py +61 -61
  109. meerk40t/core/node/util_input.py +102 -102
  110. meerk40t/core/node/util_output.py +102 -102
  111. meerk40t/core/node/util_wait.py +65 -65
  112. meerk40t/core/parameters.py +709 -707
  113. meerk40t/core/planner.py +875 -785
  114. meerk40t/core/plotplanner.py +656 -652
  115. meerk40t/core/space.py +120 -113
  116. meerk40t/core/spoolers.py +706 -705
  117. meerk40t/core/svg_io.py +1836 -1549
  118. meerk40t/core/treeop.py +534 -445
  119. meerk40t/core/undos.py +278 -124
  120. meerk40t/core/units.py +784 -680
  121. meerk40t/core/view.py +393 -322
  122. meerk40t/core/webhelp.py +62 -62
  123. meerk40t/core/wordlist.py +513 -504
  124. meerk40t/cylinder/cylinder.py +247 -0
  125. meerk40t/cylinder/gui/cylindersettings.py +41 -0
  126. meerk40t/cylinder/gui/gui.py +24 -0
  127. meerk40t/device/__init__.py +1 -1
  128. meerk40t/device/basedevice.py +322 -123
  129. meerk40t/device/devicechoices.py +50 -0
  130. meerk40t/device/dummydevice.py +163 -128
  131. meerk40t/device/gui/defaultactions.py +618 -602
  132. meerk40t/device/gui/effectspanel.py +114 -0
  133. meerk40t/device/gui/formatterpanel.py +253 -290
  134. meerk40t/device/gui/warningpanel.py +337 -260
  135. meerk40t/device/mixins.py +13 -13
  136. meerk40t/dxf/__init__.py +1 -1
  137. meerk40t/dxf/dxf_io.py +766 -554
  138. meerk40t/dxf/plugin.py +47 -35
  139. meerk40t/external_plugins.py +79 -79
  140. meerk40t/external_plugins_build.py +28 -28
  141. meerk40t/extra/cag.py +112 -116
  142. meerk40t/extra/coolant.py +403 -0
  143. meerk40t/extra/encode_detect.py +198 -0
  144. meerk40t/extra/ezd.py +1165 -1165
  145. meerk40t/extra/hershey.py +835 -340
  146. meerk40t/extra/imageactions.py +322 -316
  147. meerk40t/extra/inkscape.py +630 -622
  148. meerk40t/extra/lbrn.py +424 -424
  149. meerk40t/extra/outerworld.py +284 -0
  150. meerk40t/extra/param_functions.py +1542 -1556
  151. meerk40t/extra/potrace.py +257 -253
  152. meerk40t/extra/serial_exchange.py +118 -0
  153. meerk40t/extra/updater.py +602 -453
  154. meerk40t/extra/vectrace.py +147 -146
  155. meerk40t/extra/winsleep.py +83 -83
  156. meerk40t/extra/xcs_reader.py +597 -0
  157. meerk40t/fill/fills.py +781 -335
  158. meerk40t/fill/patternfill.py +1061 -1061
  159. meerk40t/fill/patterns.py +614 -567
  160. meerk40t/grbl/control.py +87 -87
  161. meerk40t/grbl/controller.py +990 -903
  162. meerk40t/grbl/device.py +1081 -768
  163. meerk40t/grbl/driver.py +989 -771
  164. meerk40t/grbl/emulator.py +532 -497
  165. meerk40t/grbl/gcodejob.py +783 -767
  166. meerk40t/grbl/gui/grblconfiguration.py +373 -298
  167. meerk40t/grbl/gui/grblcontroller.py +485 -271
  168. meerk40t/grbl/gui/grblhardwareconfig.py +269 -153
  169. meerk40t/grbl/gui/grbloperationconfig.py +105 -0
  170. meerk40t/grbl/gui/gui.py +147 -116
  171. meerk40t/grbl/interpreter.py +44 -44
  172. meerk40t/grbl/loader.py +22 -22
  173. meerk40t/grbl/mock_connection.py +56 -56
  174. meerk40t/grbl/plugin.py +294 -264
  175. meerk40t/grbl/serial_connection.py +93 -88
  176. meerk40t/grbl/tcp_connection.py +81 -79
  177. meerk40t/grbl/ws_connection.py +112 -0
  178. meerk40t/gui/__init__.py +1 -1
  179. meerk40t/gui/about.py +2042 -296
  180. meerk40t/gui/alignment.py +1644 -1608
  181. meerk40t/gui/autoexec.py +199 -0
  182. meerk40t/gui/basicops.py +791 -670
  183. meerk40t/gui/bufferview.py +77 -71
  184. meerk40t/gui/busy.py +170 -133
  185. meerk40t/gui/choicepropertypanel.py +1673 -1469
  186. meerk40t/gui/consolepanel.py +706 -542
  187. meerk40t/gui/devicepanel.py +687 -581
  188. meerk40t/gui/dialogoptions.py +110 -107
  189. meerk40t/gui/executejob.py +316 -306
  190. meerk40t/gui/fonts.py +90 -90
  191. meerk40t/gui/functionwrapper.py +252 -0
  192. meerk40t/gui/gui_mixins.py +729 -0
  193. meerk40t/gui/guicolors.py +205 -182
  194. meerk40t/gui/help_assets/help_assets.py +218 -201
  195. meerk40t/gui/helper.py +154 -0
  196. meerk40t/gui/hersheymanager.py +1430 -846
  197. meerk40t/gui/icons.py +3422 -2747
  198. meerk40t/gui/imagesplitter.py +555 -508
  199. meerk40t/gui/keymap.py +354 -344
  200. meerk40t/gui/laserpanel.py +892 -806
  201. meerk40t/gui/laserrender.py +1470 -1232
  202. meerk40t/gui/lasertoolpanel.py +805 -793
  203. meerk40t/gui/magnetoptions.py +436 -0
  204. meerk40t/gui/materialmanager.py +2917 -0
  205. meerk40t/gui/materialtest.py +1722 -1694
  206. meerk40t/gui/mkdebug.py +646 -359
  207. meerk40t/gui/mwindow.py +163 -140
  208. meerk40t/gui/navigationpanels.py +2605 -2467
  209. meerk40t/gui/notes.py +143 -142
  210. meerk40t/gui/opassignment.py +414 -410
  211. meerk40t/gui/operation_info.py +310 -299
  212. meerk40t/gui/plugin.py +494 -328
  213. meerk40t/gui/position.py +714 -669
  214. meerk40t/gui/preferences.py +901 -650
  215. meerk40t/gui/propertypanels/attributes.py +1461 -1131
  216. meerk40t/gui/propertypanels/blobproperty.py +117 -114
  217. meerk40t/gui/propertypanels/consoleproperty.py +83 -80
  218. meerk40t/gui/propertypanels/gotoproperty.py +77 -0
  219. meerk40t/gui/propertypanels/groupproperties.py +223 -217
  220. meerk40t/gui/propertypanels/hatchproperty.py +489 -469
  221. meerk40t/gui/propertypanels/imageproperty.py +2244 -1384
  222. meerk40t/gui/propertypanels/inputproperty.py +59 -58
  223. meerk40t/gui/propertypanels/opbranchproperties.py +82 -80
  224. meerk40t/gui/propertypanels/operationpropertymain.py +1890 -1638
  225. meerk40t/gui/propertypanels/outputproperty.py +59 -58
  226. meerk40t/gui/propertypanels/pathproperty.py +389 -380
  227. meerk40t/gui/propertypanels/placementproperty.py +1214 -383
  228. meerk40t/gui/propertypanels/pointproperty.py +140 -136
  229. meerk40t/gui/propertypanels/propertywindow.py +313 -181
  230. meerk40t/gui/propertypanels/rasterwizardpanels.py +996 -912
  231. meerk40t/gui/propertypanels/regbranchproperties.py +76 -0
  232. meerk40t/gui/propertypanels/textproperty.py +770 -755
  233. meerk40t/gui/propertypanels/waitproperty.py +56 -55
  234. meerk40t/gui/propertypanels/warpproperty.py +121 -0
  235. meerk40t/gui/propertypanels/wobbleproperty.py +255 -204
  236. meerk40t/gui/ribbon.py +2468 -2210
  237. meerk40t/gui/scene/scene.py +1100 -1051
  238. meerk40t/gui/scene/sceneconst.py +22 -22
  239. meerk40t/gui/scene/scenepanel.py +439 -349
  240. meerk40t/gui/scene/scenespacewidget.py +365 -365
  241. meerk40t/gui/scene/widget.py +518 -505
  242. meerk40t/gui/scenewidgets/affinemover.py +215 -215
  243. meerk40t/gui/scenewidgets/attractionwidget.py +315 -309
  244. meerk40t/gui/scenewidgets/bedwidget.py +120 -97
  245. meerk40t/gui/scenewidgets/elementswidget.py +137 -107
  246. meerk40t/gui/scenewidgets/gridwidget.py +785 -745
  247. meerk40t/gui/scenewidgets/guidewidget.py +765 -765
  248. meerk40t/gui/scenewidgets/laserpathwidget.py +66 -66
  249. meerk40t/gui/scenewidgets/machineoriginwidget.py +86 -86
  250. meerk40t/gui/scenewidgets/nodeselector.py +28 -28
  251. meerk40t/gui/scenewidgets/rectselectwidget.py +589 -346
  252. meerk40t/gui/scenewidgets/relocatewidget.py +33 -33
  253. meerk40t/gui/scenewidgets/reticlewidget.py +83 -83
  254. meerk40t/gui/scenewidgets/selectionwidget.py +2952 -2756
  255. meerk40t/gui/simpleui.py +357 -333
  256. meerk40t/gui/simulation.py +2431 -2094
  257. meerk40t/gui/snapoptions.py +208 -203
  258. meerk40t/gui/spoolerpanel.py +1227 -1180
  259. meerk40t/gui/statusbarwidgets/defaultoperations.py +480 -353
  260. meerk40t/gui/statusbarwidgets/infowidget.py +520 -483
  261. meerk40t/gui/statusbarwidgets/opassignwidget.py +356 -355
  262. meerk40t/gui/statusbarwidgets/selectionwidget.py +172 -171
  263. meerk40t/gui/statusbarwidgets/shapepropwidget.py +754 -236
  264. meerk40t/gui/statusbarwidgets/statusbar.py +272 -260
  265. meerk40t/gui/statusbarwidgets/statusbarwidget.py +268 -270
  266. meerk40t/gui/statusbarwidgets/strokewidget.py +267 -251
  267. meerk40t/gui/themes.py +200 -78
  268. meerk40t/gui/tips.py +591 -0
  269. meerk40t/gui/toolwidgets/circlebrush.py +35 -35
  270. meerk40t/gui/toolwidgets/toolcircle.py +248 -242
  271. meerk40t/gui/toolwidgets/toolcontainer.py +82 -77
  272. meerk40t/gui/toolwidgets/tooldraw.py +97 -90
  273. meerk40t/gui/toolwidgets/toolellipse.py +219 -212
  274. meerk40t/gui/toolwidgets/toolimagecut.py +25 -132
  275. meerk40t/gui/toolwidgets/toolline.py +39 -144
  276. meerk40t/gui/toolwidgets/toollinetext.py +79 -236
  277. meerk40t/gui/toolwidgets/toollinetext_inline.py +296 -0
  278. meerk40t/gui/toolwidgets/toolmeasure.py +160 -216
  279. meerk40t/gui/toolwidgets/toolnodeedit.py +2088 -2074
  280. meerk40t/gui/toolwidgets/toolnodemove.py +92 -94
  281. meerk40t/gui/toolwidgets/toolparameter.py +754 -668
  282. meerk40t/gui/toolwidgets/toolplacement.py +108 -108
  283. meerk40t/gui/toolwidgets/toolpoint.py +68 -59
  284. meerk40t/gui/toolwidgets/toolpointlistbuilder.py +294 -0
  285. meerk40t/gui/toolwidgets/toolpointmove.py +183 -0
  286. meerk40t/gui/toolwidgets/toolpolygon.py +288 -403
  287. meerk40t/gui/toolwidgets/toolpolyline.py +38 -196
  288. meerk40t/gui/toolwidgets/toolrect.py +211 -207
  289. meerk40t/gui/toolwidgets/toolrelocate.py +72 -72
  290. meerk40t/gui/toolwidgets/toolribbon.py +598 -113
  291. meerk40t/gui/toolwidgets/tooltabedit.py +546 -0
  292. meerk40t/gui/toolwidgets/tooltext.py +98 -89
  293. meerk40t/gui/toolwidgets/toolvector.py +213 -204
  294. meerk40t/gui/toolwidgets/toolwidget.py +39 -39
  295. meerk40t/gui/usbconnect.py +98 -91
  296. meerk40t/gui/utilitywidgets/buttonwidget.py +18 -18
  297. meerk40t/gui/utilitywidgets/checkboxwidget.py +90 -90
  298. meerk40t/gui/utilitywidgets/controlwidget.py +14 -14
  299. meerk40t/gui/utilitywidgets/cyclocycloidwidget.py +343 -340
  300. meerk40t/gui/utilitywidgets/debugwidgets.py +148 -0
  301. meerk40t/gui/utilitywidgets/handlewidget.py +27 -27
  302. meerk40t/gui/utilitywidgets/harmonograph.py +450 -447
  303. meerk40t/gui/utilitywidgets/openclosewidget.py +40 -40
  304. meerk40t/gui/utilitywidgets/rotationwidget.py +54 -54
  305. meerk40t/gui/utilitywidgets/scalewidget.py +75 -75
  306. meerk40t/gui/utilitywidgets/seekbarwidget.py +183 -183
  307. meerk40t/gui/utilitywidgets/togglewidget.py +142 -142
  308. meerk40t/gui/utilitywidgets/toolbarwidget.py +8 -8
  309. meerk40t/gui/wordlisteditor.py +985 -931
  310. meerk40t/gui/wxmeerk40t.py +1444 -1169
  311. meerk40t/gui/wxmmain.py +5578 -4112
  312. meerk40t/gui/wxmribbon.py +1591 -1076
  313. meerk40t/gui/wxmscene.py +1635 -1453
  314. meerk40t/gui/wxmtree.py +2410 -2089
  315. meerk40t/gui/wxutils.py +1769 -1099
  316. meerk40t/gui/zmatrix.py +102 -102
  317. meerk40t/image/__init__.py +1 -1
  318. meerk40t/image/dither.py +429 -0
  319. meerk40t/image/imagetools.py +2778 -2269
  320. meerk40t/internal_plugins.py +150 -130
  321. meerk40t/kernel/__init__.py +63 -12
  322. meerk40t/kernel/channel.py +259 -212
  323. meerk40t/kernel/context.py +538 -538
  324. meerk40t/kernel/exceptions.py +41 -41
  325. meerk40t/kernel/functions.py +463 -414
  326. meerk40t/kernel/jobs.py +100 -100
  327. meerk40t/kernel/kernel.py +3809 -3571
  328. meerk40t/kernel/lifecycles.py +71 -71
  329. meerk40t/kernel/module.py +49 -49
  330. meerk40t/kernel/service.py +147 -147
  331. meerk40t/kernel/settings.py +383 -343
  332. meerk40t/lihuiyu/controller.py +883 -876
  333. meerk40t/lihuiyu/device.py +1181 -1069
  334. meerk40t/lihuiyu/driver.py +1466 -1372
  335. meerk40t/lihuiyu/gui/gui.py +127 -106
  336. meerk40t/lihuiyu/gui/lhyaccelgui.py +377 -363
  337. meerk40t/lihuiyu/gui/lhycontrollergui.py +741 -651
  338. meerk40t/lihuiyu/gui/lhydrivergui.py +470 -446
  339. meerk40t/lihuiyu/gui/lhyoperationproperties.py +238 -237
  340. meerk40t/lihuiyu/gui/tcpcontroller.py +226 -190
  341. meerk40t/lihuiyu/interpreter.py +53 -53
  342. meerk40t/lihuiyu/laserspeed.py +450 -450
  343. meerk40t/lihuiyu/loader.py +90 -90
  344. meerk40t/lihuiyu/parser.py +404 -404
  345. meerk40t/lihuiyu/plugin.py +101 -102
  346. meerk40t/lihuiyu/tcp_connection.py +111 -109
  347. meerk40t/main.py +231 -165
  348. meerk40t/moshi/builder.py +788 -781
  349. meerk40t/moshi/controller.py +505 -499
  350. meerk40t/moshi/device.py +495 -442
  351. meerk40t/moshi/driver.py +862 -696
  352. meerk40t/moshi/gui/gui.py +78 -76
  353. meerk40t/moshi/gui/moshicontrollergui.py +538 -522
  354. meerk40t/moshi/gui/moshidrivergui.py +87 -75
  355. meerk40t/moshi/plugin.py +43 -43
  356. meerk40t/network/console_server.py +102 -57
  357. meerk40t/network/kernelserver.py +10 -9
  358. meerk40t/network/tcp_server.py +142 -140
  359. meerk40t/network/udp_server.py +103 -77
  360. meerk40t/network/web_server.py +390 -0
  361. meerk40t/newly/controller.py +1158 -1144
  362. meerk40t/newly/device.py +874 -732
  363. meerk40t/newly/driver.py +540 -412
  364. meerk40t/newly/gui/gui.py +219 -188
  365. meerk40t/newly/gui/newlyconfig.py +116 -101
  366. meerk40t/newly/gui/newlycontroller.py +193 -186
  367. meerk40t/newly/gui/operationproperties.py +51 -51
  368. meerk40t/newly/mock_connection.py +82 -82
  369. meerk40t/newly/newly_params.py +56 -56
  370. meerk40t/newly/plugin.py +1214 -1246
  371. meerk40t/newly/usb_connection.py +322 -322
  372. meerk40t/rotary/gui/gui.py +52 -46
  373. meerk40t/rotary/gui/rotarysettings.py +240 -232
  374. meerk40t/rotary/rotary.py +202 -98
  375. meerk40t/ruida/control.py +291 -91
  376. meerk40t/ruida/controller.py +138 -1088
  377. meerk40t/ruida/device.py +672 -231
  378. meerk40t/ruida/driver.py +534 -472
  379. meerk40t/ruida/emulator.py +1494 -1491
  380. meerk40t/ruida/exceptions.py +4 -4
  381. meerk40t/ruida/gui/gui.py +71 -76
  382. meerk40t/ruida/gui/ruidaconfig.py +239 -72
  383. meerk40t/ruida/gui/ruidacontroller.py +187 -184
  384. meerk40t/ruida/gui/ruidaoperationproperties.py +48 -47
  385. meerk40t/ruida/loader.py +54 -52
  386. meerk40t/ruida/mock_connection.py +57 -109
  387. meerk40t/ruida/plugin.py +124 -87
  388. meerk40t/ruida/rdjob.py +2084 -945
  389. meerk40t/ruida/serial_connection.py +116 -0
  390. meerk40t/ruida/tcp_connection.py +146 -0
  391. meerk40t/ruida/udp_connection.py +73 -0
  392. meerk40t/svgelements.py +9671 -9669
  393. meerk40t/tools/driver_to_path.py +584 -579
  394. meerk40t/tools/geomstr.py +5583 -4680
  395. meerk40t/tools/jhfparser.py +357 -292
  396. meerk40t/tools/kerftest.py +904 -890
  397. meerk40t/tools/livinghinges.py +1168 -1033
  398. meerk40t/tools/pathtools.py +987 -949
  399. meerk40t/tools/pmatrix.py +234 -0
  400. meerk40t/tools/pointfinder.py +942 -942
  401. meerk40t/tools/polybool.py +940 -940
  402. meerk40t/tools/rasterplotter.py +1660 -547
  403. meerk40t/tools/shxparser.py +989 -901
  404. meerk40t/tools/ttfparser.py +726 -446
  405. meerk40t/tools/zinglplotter.py +595 -593
  406. {meerk40t-0.9.3001.dist-info → meerk40t-0.9.7010.dist-info}/LICENSE +21 -21
  407. {meerk40t-0.9.3001.dist-info → meerk40t-0.9.7010.dist-info}/METADATA +150 -139
  408. meerk40t-0.9.7010.dist-info/RECORD +445 -0
  409. {meerk40t-0.9.3001.dist-info → meerk40t-0.9.7010.dist-info}/WHEEL +1 -1
  410. {meerk40t-0.9.3001.dist-info → meerk40t-0.9.7010.dist-info}/top_level.txt +0 -1
  411. {meerk40t-0.9.3001.dist-info → meerk40t-0.9.7010.dist-info}/zip-safe +1 -1
  412. meerk40t/balormk/elementlightjob.py +0 -159
  413. meerk40t-0.9.3001.dist-info/RECORD +0 -437
  414. test/bootstrap.py +0 -63
  415. test/test_cli.py +0 -12
  416. test/test_core_cutcode.py +0 -418
  417. test/test_core_elements.py +0 -144
  418. test/test_core_plotplanner.py +0 -397
  419. test/test_core_viewports.py +0 -312
  420. test/test_drivers_grbl.py +0 -108
  421. test/test_drivers_lihuiyu.py +0 -443
  422. test/test_drivers_newly.py +0 -113
  423. test/test_element_degenerate_points.py +0 -43
  424. test/test_elements_classify.py +0 -97
  425. test/test_elements_penbox.py +0 -22
  426. test/test_file_svg.py +0 -176
  427. test/test_fill.py +0 -155
  428. test/test_geomstr.py +0 -1523
  429. test/test_geomstr_nodes.py +0 -18
  430. test/test_imagetools_actualize.py +0 -306
  431. test/test_imagetools_wizard.py +0 -258
  432. test/test_kernel.py +0 -200
  433. test/test_laser_speeds.py +0 -3303
  434. test/test_length.py +0 -57
  435. test/test_lifecycle.py +0 -66
  436. test/test_operations.py +0 -251
  437. test/test_operations_hatch.py +0 -57
  438. test/test_ruida.py +0 -19
  439. test/test_spooler.py +0 -22
  440. test/test_tools_rasterplotter.py +0 -29
  441. test/test_wobble.py +0 -133
  442. test/test_zingl.py +0 -124
  443. {test → meerk40t/cylinder}/__init__.py +0 -0
  444. /meerk40t/{core/element_commands.py → cylinder/gui/__init__.py} +0 -0
  445. {meerk40t-0.9.3001.dist-info → meerk40t-0.9.7010.dist-info}/entry_points.txt +0 -0
@@ -1,901 +1,989 @@
1
- from math import atan2, cos, isinf, sin, tau
2
-
3
- SHXPARSER_VERSION = "0.0.2"
4
-
5
-
6
- END_OF_SHAPE = 0
7
- PEN_DOWN = 1
8
- PEN_UP = 2
9
- DIVIDE_VECTOR = 3
10
- MULTIPLY_VECTOR = 4
11
- PUSH_STACK = 5
12
- POP_STACK = 6
13
- DRAW_SUBSHAPE = 7
14
- XY_DISPLACEMENT = 8
15
- POLY_XY_DISPLACEMENT = 9 # 0,0 terminated
16
- OCTANT_ARC = 0xA
17
- FRACTIONAL_ARC = 0xB # 5 bytes, start, end, high radius, radius, ±0SC
18
- BULGE_ARC = 0xC # dx, dy, bulge
19
- POLY_BULGE_ARC = 0xD # 0,0 terminated BULGE_ARC
20
- COND_MODE_2 = 0x0E # PROCESS this command *only if mode=2*
21
-
22
-
23
- def signed8(b):
24
- if b > 127:
25
- return -256 + b
26
- else:
27
- return b
28
-
29
-
30
- def int_16le(byte):
31
- return (byte[0] & 0xFF) + ((byte[1] & 0xFF) << 8)
32
-
33
-
34
- def int_32le(b):
35
- return (
36
- (b[0] & 0xFF)
37
- + ((b[1] & 0xFF) << 8)
38
- + ((b[2] & 0xFF) << 16)
39
- + ((b[3] & 0xFF) << 24)
40
- )
41
-
42
-
43
- def read_int_8(stream):
44
- byte = bytearray(stream.read(1))
45
- if len(byte) == 1:
46
- return byte[0]
47
- return None
48
-
49
-
50
- def read_int_16le(stream):
51
- byte = bytearray(stream.read(2))
52
- if len(byte) == 2:
53
- return int_16le(byte)
54
- return None
55
-
56
-
57
- def read_int_32le(stream):
58
- b = bytearray(stream.read(4))
59
- if len(b) == 4:
60
- return int_32le(b)
61
- return None
62
-
63
-
64
- def read_string(stream):
65
- try:
66
- bb = bytearray()
67
- while True:
68
- b = stream.read(1)
69
- if b == b"":
70
- return bb.decode("utf-8")
71
- if b == b"\r" or b == b"\n" or b == b"\x00":
72
- return bb.decode("utf-8")
73
- bb += b
74
- except UnicodeDecodeError as e:
75
- raise ShxFontParseError(f"Read string did not capture valid text. {bb}") from e
76
-
77
-
78
- class ShxPath:
79
- """
80
- Example path code. Any class with these functions would work as well. When render is called on the ShxFont class
81
- the path is given particular useful segments.
82
- """
83
-
84
- def __init__(self):
85
- self.path = list()
86
-
87
- def bounds(self):
88
- """
89
- Get bounds of paths.
90
- :return:
91
- """
92
- min_x = float("inf")
93
- min_y = float("inf")
94
- max_x = -float("inf")
95
- max_y = -float("inf")
96
- for p in self.path:
97
- if p is None:
98
- continue
99
- min_x = min(p[0], min_x)
100
- min_y = min(p[1], min_y)
101
- max_x = max(p[0], max_x)
102
- max_y = max(p[1], max_y)
103
-
104
- min_x = min(p[-2], min_x)
105
- min_y = min(p[-1], min_y)
106
- max_x = max(p[-2], max_x)
107
- max_y = max(p[-1], max_y)
108
- if isinf(min_x):
109
- return None
110
- return min_x, min_y, max_x, max_y
111
-
112
- def scale(self, scale_x, scale_y):
113
- for p in self.path:
114
- if p is None:
115
- continue
116
- if len(p) >= 2:
117
- p[0] *= scale_x
118
- p[1] *= scale_y
119
- if len(p) >= 4:
120
- p[2] *= scale_x
121
- p[3] *= scale_y
122
- if len(p) >= 6:
123
- p[4] *= scale_x
124
- p[5] *= scale_y
125
-
126
- def translate(self, translate_x, translate_y):
127
- for p in self.path:
128
- if p is None:
129
- continue
130
- if len(p) >= 2:
131
- p[0] += translate_x
132
- p[1] += translate_y
133
- if len(p) >= 4:
134
- p[2] += translate_x
135
- p[3] += translate_y
136
- if len(p) >= 6:
137
- p[4] += translate_x
138
- p[5] += translate_y
139
-
140
- def new_path(self):
141
- """
142
- Start of a new path.
143
- """
144
- self.path.append(None)
145
-
146
- def move(self, x, y):
147
- """
148
- Move current point to the point specified.
149
- """
150
- self.path.append([x, y])
151
-
152
- def line(self, x0, y0, x1, y1):
153
- """
154
- Draw a line from the current point to the specified point.
155
- """
156
- self.path.append([x0, y0, x1, y1])
157
-
158
- def arc(self, x0, y0, cx, cy, x1, y1):
159
- """
160
- Draw an arc from the current point to specified point going through the control point.
161
-
162
- 3 Points define a circular arc, there is only one arc which travels from start to end going through a given
163
- control point. The exceptions are when the arc points are collinear or two arc points are coincident. In some
164
- cases the start and end points will be equal and the control point will be located the circle diameter away.
165
- """
166
- self.path.append([x0, y0, cx, cy, x1, y1])
167
-
168
-
169
- class ShxFontParseError(Exception):
170
- """
171
- Exception thrown if unable to pop a value from the given codes or other suspected parsing errors.
172
- """
173
-
174
-
175
- class ShxFont:
176
- """
177
- This class performs the parsing of the three major types of .SHX fonts. Composing them into specific glyphs which
178
- consist of commands in a vector-shape language. When .render() is called on some text, vector actions are performed
179
- on the font which create the vector path.
180
- """
181
-
182
- def __init__(self, filename, debug=False):
183
- self.format = None # format (usually AutoCAD-86)
184
- self.type = None # Font type: shapes, bigfont, unifont
185
- self.version = None # Font file version (usually 1.0).
186
- self.glyphs = dict() # Glyph dictionary
187
- self.font_name = None # Parsed font name.
188
- self.above = None # Distance above baseline for capital letters.
189
- self.below = None # Distance below baseline for lowercase letters
190
- self.modes = None # 0 Horizontal Only, 2 Dual mode (Horizontal or Vertical)
191
- self.encoding = False # 0 unicode, 1 packed multibyte, 2 shape file
192
- self.embedded = False # 0 font can be embedded, 1 font cannot be embedded, 2 embedding is read-only
193
-
194
- self._debug = debug
195
- self._code = None
196
- self._path = None
197
- self._skip = False
198
- self._pen = False
199
- self._horizontal = True
200
- self._letter = None
201
- self._x = 0
202
- self._y = 0
203
- self._last_x = 0
204
- self._last_y = 0
205
- self._scale = 1
206
- self._stack = []
207
-
208
- self._parse(filename)
209
-
210
- def __str__(self):
211
- return f'{self.type}("{self.font_name}", {self.version}, glyphs: {len(self.glyphs)})'
212
-
213
- def _parse(self, filename):
214
- with open(filename, "br") as f:
215
- self._parse_header(f)
216
- if self._debug:
217
- print(f"Font header indicates font type is {self.type}")
218
- if self.type == "shapes":
219
- self._parse_shapes(f)
220
- elif self.type == "bigfont":
221
- self._parse_bigfont(f)
222
- elif self.type == "unifont":
223
- self._parse_unifont(f)
224
- else:
225
- raise ShxFontParseError(f"{self.type} is not a valid shx file type.")
226
-
227
- def _parse_header(self, f):
228
- header = read_string(f)
229
- parts = header.split(" ")
230
- if len(parts) != 3:
231
- raise ShxFontParseError(f"Header information invalid: {header}")
232
- self.format = parts[0]
233
- self.type = parts[1]
234
- self.version = parts[2]
235
- f.read(2)
236
-
237
- def _parse_shapes(self, f):
238
- start = read_int_16le(f)
239
- end = read_int_16le(f)
240
- count = read_int_16le(f)
241
- if self._debug:
242
- print(f"Parsing shape: start={start}, end={end}, count={count}")
243
- glyph_ref = list()
244
- for i in range(count):
245
- index = read_int_16le(f)
246
- length = read_int_16le(f)
247
- glyph_ref.append((index, length))
248
-
249
- for index, length in glyph_ref:
250
- if index == 0:
251
- if self.font_name is not None:
252
- raise ShxFontParseError("Double-initializing glyph data detected")
253
- self.font_name = read_string(f)
254
- self.above = read_int_8(f) # vector lengths above baseline
255
- self.below = read_int_8(f) # vector lengths below baseline
256
- # 0 - Horizontal, 2 - dual. 0x0E command only when mode=2
257
- self.modes = read_int_8(f)
258
- # end = read_int_16le(f)
259
- else:
260
- read = f.read(length)
261
- data = read
262
- if len(data) != length:
263
- raise ShxFontParseError("Glyph length did not exist in file.")
264
- if data[0] == 0 and data[1] == 0:
265
- data = data[2:]
266
- elif data[0] == 0:
267
- data = data[1:]
268
- find = data.find(b"\x00")
269
- if find != -1:
270
- name = data[:find]
271
-
272
- for c in name:
273
- if (
274
- ord("A") <= c <= ord("Z")
275
- or ord("0") <= c <= ord("9")
276
- or c == ord(" ")
277
- or c == ord("&")
278
- ):
279
- continue
280
- name = None
281
- break
282
- if name is not None:
283
- data = data[find + 1 :]
284
- name = name.decode()
285
- self.glyphs[name] = data
286
- else:
287
- if self._debug:
288
- print(f"{data} did not contain a name.")
289
- self.glyphs[index] = data
290
-
291
- def _parse_bigfont(self, f):
292
- count = read_int_16le(f)
293
- length = read_int_16le(f)
294
- changes = list()
295
- change_count = read_int_16le(f)
296
- if self._debug:
297
- print(
298
- f"Parsing bigfont: count={count}, length={length}, change_count={change_count}"
299
- )
300
- for i in range(change_count):
301
- start = read_int_16le(f)
302
- end = read_int_16le(f)
303
- changes.append((start, end))
304
-
305
- glyph_ref = list()
306
- for i in range(count):
307
- index = read_int_16le(f)
308
- length = read_int_16le(f)
309
- offset = read_int_32le(f)
310
- glyph_ref.append((index, length, offset))
311
-
312
- for index, length, offset in glyph_ref:
313
- f.seek(offset, 0)
314
- if index == 0:
315
- # self.font_name = read_string(f)
316
- self.above = read_int_8(f) # vector lengths above baseline
317
- self.below = read_int_8(f) # vector lengths below baseline
318
- # 0 - Horizontal, 2 - dual. 0x0E command only when mode=2
319
- self.modes = read_int_8(f)
320
-
321
- else:
322
- self.glyphs[index] = f.read(length)
323
-
324
- def _parse_unifont(self, f):
325
- count = read_int_32le(f)
326
- length = read_int_16le(f)
327
- f.seek(5)
328
- self.font_name = read_string(f)
329
- self.above = read_int_8(f)
330
- self.below = read_int_8(f)
331
- self.mode = read_int_8(f)
332
- self.encoding = read_int_8(f)
333
- self.embedded = read_int_8(f)
334
- ignore = read_int_8(f)
335
- if self._debug:
336
- print(
337
- f"Parsing unifont: name={self.font_name}, count={count}, length={length}"
338
- )
339
- for i in range(count - 1):
340
- index = read_int_16le(f)
341
- length = read_int_16le(f)
342
- self.glyphs[index] = f.read(length)
343
-
344
- def pop(self):
345
- try:
346
- return self._code.pop()
347
- except IndexError as e:
348
- raise ShxFontParseError("No codes to pop()") from e
349
-
350
- def render(self, path, text, horizontal=True, font_size=12.0):
351
- if self.above is None:
352
- self.above = 1
353
- self._scale = font_size / self.above
354
- self._horizontal = horizontal
355
- self._path = path
356
- replacer = []
357
- for tchar in text:
358
- to_replace = None
359
- # Yes, I am German :-)
360
- if ord(tchar) not in self.glyphs:
361
- if tchar == "ä":
362
- to_replace = (tchar, "ae")
363
- elif tchar == "ö":
364
- to_replace = (tchar, "ue")
365
- elif tchar == "ü":
366
- to_replace = (tchar, "ue")
367
- elif tchar == "Ä":
368
- to_replace = (tchar, "Ae")
369
- elif tchar == "Ö":
370
- to_replace = (tchar, "Oe")
371
- elif tchar == "Ü":
372
- to_replace = (tchar, "Ue")
373
- elif tchar == "ß":
374
- to_replace = (tchar, "ss")
375
- if to_replace is not None and to_replace not in replacer:
376
- replacer.append(to_replace)
377
- for to_replace in replacer:
378
- # print (f"Replace all '{to_replace[0]}' with '{to_replace[1]}'")
379
- text = text.replace(to_replace[0], to_replace[1])
380
- for letter in text:
381
- self._letter = letter
382
- try:
383
- self._code = bytearray(reversed(self.glyphs[ord(letter)]))
384
- except KeyError:
385
- # Letter is not found.
386
- continue
387
- self._pen = True
388
- while self._code:
389
- try:
390
- self._parse_code()
391
- except IndexError as e:
392
- raise ShxFontParseError("Stack Error during render.") from e
393
- self._skip = False
394
- if self._debug:
395
- print(f"Render Complete.\n\n\n")
396
-
397
- def _parse_code(self):
398
- b = self.pop()
399
- direction = b & 0x0F
400
- length = (b & 0xF0) >> 4
401
- if length == 0:
402
- self._parse_code_special(direction)
403
- else:
404
- self._parse_code_length(direction, length)
405
-
406
- def _parse_code_length(self, direction, length):
407
- """
408
- Length direction codes. If length is 0 then direction is special otherwise the
409
- command is a move in one of 16 different directions moving in 22. increments for
410
- a distance of 1 to 15 units lengths.
411
-
412
- :param direction:
413
- :param length:
414
- :return:
415
- """
416
- if self._debug:
417
- print(
418
- f"MOVE DIRECTION {direction} for {length} {'(Skipped)' if self._skip else ''}"
419
- )
420
- if self._skip:
421
- self._skip = False
422
- return
423
- if direction in (2, 1, 0, 0xF, 0xE):
424
- dx = 1.0
425
- elif direction in (3, 0xD):
426
- dx = 0.5
427
- elif direction in (4, 0xC):
428
- dx = 0.0
429
- elif direction in (5, 0xB):
430
- dx = -0.5
431
- else: # (6, 7, 8, 9, 0xa):
432
- dx = -1.0
433
- if direction in (6, 5, 4, 3, 2):
434
- dy = 1.0
435
- elif direction in (7, 1):
436
- dy = 0.5
437
- elif direction in (8, 0):
438
- dy = 0.0
439
- elif direction in (9, 0xF):
440
- dy = -0.5
441
- else: # (0xa, 0xb, 0xc, 0xd, 0xe, 0xf):
442
- dy = -1.0
443
- self._x += dx * length * self._scale
444
- self._y += dy * length * self._scale
445
- if self._pen:
446
- self._path.line(self._last_x, self._last_y, self._x, self._y)
447
- else:
448
- self._path.move(self._x, self._y)
449
- self._last_x, self._last_y = self._x, self._y
450
-
451
- def _parse_code_special(self, special):
452
- if special == END_OF_SHAPE:
453
- self._end_of_shape()
454
- elif special == PEN_DOWN:
455
- self._pen_down()
456
- elif special == PEN_UP:
457
- self._pen_up()
458
- elif special == DIVIDE_VECTOR:
459
- self._divide_vector()
460
- elif special == MULTIPLY_VECTOR:
461
- self._multiply_vector()
462
- elif special == PUSH_STACK:
463
- self._push_stack()
464
- elif special == POP_STACK:
465
- self._pop_stack()
466
- elif special == DRAW_SUBSHAPE:
467
- self._draw_subshape()
468
- elif special == XY_DISPLACEMENT:
469
- self._xy_displacement()
470
- elif special == POLY_XY_DISPLACEMENT:
471
- self._poly_xy_displacement()
472
- elif special == OCTANT_ARC:
473
- self._octant_arc()
474
- elif special == FRACTIONAL_ARC:
475
- self._fractional_arc()
476
- elif special == BULGE_ARC:
477
- self._bulge_arc()
478
- elif special == POLY_BULGE_ARC:
479
- self._poly_bulge_arc()
480
- elif special == COND_MODE_2:
481
- self._cond_mode_2()
482
-
483
- def _end_of_shape(self):
484
- """
485
- End of shape definition.
486
- :return:
487
- """
488
- try:
489
- while self.pop() != 0:
490
- pass
491
- except ShxFontParseError:
492
- pass
493
- if self._debug:
494
- print("END_OF_SHAPE")
495
- if self._skip:
496
- self._skip = False
497
- return
498
- self._path.new_path()
499
-
500
- def _pen_down(self):
501
- """
502
- Activates draw mode. Pen is down. Draw is activated for each shape.
503
- :return:
504
- """
505
- if self._debug:
506
- print(f"PEN_DOWN: {self._x}, {self._y} {'(Skipped)' if self._skip else ''}")
507
- if self._skip:
508
- self._skip = False
509
- return
510
- self._pen = True
511
- self._path.move(self._x, self._y)
512
-
513
- def _pen_up(self):
514
- """
515
- Deactivates draw mode. Subsequent draws are moves to new locations.
516
- :return:
517
- """
518
- if self._debug:
519
- print(f"PEN_UP {'(Skipped)' if self._skip else ''}")
520
- if self._skip:
521
- self._skip = False
522
- return
523
- self._pen = False
524
-
525
- def _divide_vector(self):
526
- """
527
- Height is specified with shape command. Initially considered the length of a single vector.
528
- Divides the scale factor by the next byte.
529
-
530
- :return:
531
- """
532
- factor = self.pop()
533
- if self._debug:
534
- print(
535
- f"DIVIDE_VECTOR {self._scale}/{factor} {'(Skipped)' if self._skip else ''}"
536
- )
537
- if factor == 0:
538
- raise ShxFontParseError("Divide Vector is not permitted to be 0.")
539
- if self._skip:
540
- self._skip = False
541
- return
542
- self._scale /= factor
543
-
544
- def _multiply_vector(self):
545
- """
546
- Multiplies the scale factor by the next byte.
547
-
548
- :return:
549
- """
550
- factor = self.pop()
551
- if self._debug:
552
- print(
553
- f"MULTIPLY_VECTOR {self._scale}*{factor} {'(Skipped)' if self._skip else ''}"
554
- )
555
- if factor == 0:
556
- raise ShxFontParseError("Multiply Vector is not permitted to be 0.")
557
- if self._skip:
558
- self._skip = False
559
- return
560
- self._scale *= factor
561
-
562
- def _push_stack(self):
563
- """
564
- Stack is considered four units deep. Everything pushed on the stack must be
565
- popped from the stack. Overflows respond with an error message.
566
-
567
- :return:
568
- """
569
- if self._debug:
570
- print(
571
- f"PUSH_STACK {self._x}, {self._y} {'(Skipped)' if self._skip else ''}"
572
- )
573
- if self._skip:
574
- self._skip = False
575
- return
576
- self._stack.append((self._x, self._y))
577
- if len(self._stack) == 4:
578
- raise IndexError(f"Position stack overflow in shape {self._letter}")
579
-
580
- def _pop_stack(self):
581
- """
582
- Stack is considered four units deep. You may not pop more locations than have
583
- been pushed onto the stack. Attempts to do so will respond with an error message.
584
- :return:
585
- """
586
- if self._debug:
587
- print(
588
- f"POP_STACK {self._x}, {self._y} {'(Skipped)' if self._skip else ''}"
589
- )
590
-
591
- if self._skip:
592
- self._skip = False
593
- return
594
- try:
595
- self._x, self._y = self._stack.pop()
596
- except IndexError:
597
- raise IndexError(f"Position stack underflow in shape {self._letter}")
598
- self._path.move(self._x, self._y)
599
- self._last_x, self._last_y = self._x, self._y
600
-
601
- def _draw_subshape_shapes(self):
602
- subshape = self.pop()
603
- if self._debug:
604
- print(
605
- f"Appending glyph {subshape} (Type={self.type}). {'(Skipped)' if self._skip else ''}"
606
- )
607
- if self._skip:
608
- self._skip = False
609
- return
610
- try:
611
- shape = self.glyphs[subshape]
612
- except KeyError as e:
613
- raise ShxFontParseError("Referenced subshape does not exist.") from e
614
- self._code += bytearray(reversed(shape))
615
-
616
- def _draw_subshape_bigfont(self):
617
- subshape = self.pop()
618
- if self._debug:
619
- print(
620
- f"Appending glyph {subshape} (Type={self.type}). {'(Skipped)' if self._skip else ''}"
621
- )
622
- if subshape == 0:
623
- subshape = int_16le([self.pop(), self.pop()])
624
- origin_x = self.pop() * self._scale
625
- origin_y = self.pop() * self._scale
626
- width = self.pop() * self._scale
627
- height = self.pop() * self._scale
628
- if self._debug:
629
- print(
630
- f"Extended Bigfont Glyph: {subshape}, origin_x = {origin_x}, origin_y = {origin_y}. {width}x{height}"
631
- )
632
- if self._skip:
633
- self._skip = False
634
- return
635
- try:
636
- shape = self.glyphs[subshape]
637
- except KeyError as e:
638
- raise ShxFontParseError("Referenced subshape does not exist.") from e
639
- self._code += bytearray(reversed(shape))
640
-
641
- def _draw_subshape_unifont(self):
642
- subshape = int_16le([self.pop(), self.pop()])
643
- if self._debug:
644
- print(
645
- f"Appending glyph {subshape} (Type={self.type}). {'(Skipped)' if self._skip else ''}"
646
- )
647
- if self._skip:
648
- self._skip = False
649
- return
650
- try:
651
- shape = self.glyphs[subshape]
652
- except KeyError as e:
653
- raise ShxFontParseError("Referenced subshape does not exist.") from e
654
- self._code += bytearray(reversed(shape))
655
-
656
- def _draw_subshape(self):
657
- """
658
- Subshape is given in next byte, for non-unicode fonts one byte is used for
659
- unicode fonts two bytes are used and shapes are numbered from 1 to 65535.
660
-
661
- Drawmode is not reset for the subshape. When complete the current shape
662
- continues.
663
- :return:
664
- """
665
- if self.type == "shapes":
666
- self._draw_subshape_shapes()
667
- elif self.type == "bigfont":
668
- self._draw_subshape_bigfont()
669
- elif self.type == "unifont":
670
- self._draw_subshape_unifont()
671
-
672
- def _xy_displacement(self):
673
- """
674
- X,Y displacement given in next two bytes 1 byte-x, 1 byte-y. The displacement
675
- ranges from -128 to +127.
676
- :return:
677
- """
678
- dx = signed8(self.pop()) * self._scale
679
- dy = signed8(self.pop()) * self._scale
680
- if self._debug:
681
- print(f"XY_DISPLACEMENT {dx} {dy} {'(Skipped)' if self._skip else ''}")
682
- if self._skip:
683
- self._skip = False
684
- return
685
- self._x += dx
686
- self._y += dy
687
- if self._pen:
688
- self._path.line(self._last_x, self._last_y, self._x, self._y)
689
- else:
690
- self._path.move(self._x, self._y)
691
- self._last_x, self._last_y = self._x, self._y
692
-
693
- def _poly_xy_displacement(self):
694
- """
695
- XY displacement in a series terminated with (0,0)
696
- :return:
697
- """
698
- while True:
699
- dx = signed8(self.pop()) * self._scale
700
- dy = signed8(self.pop()) * self._scale
701
- if self._debug:
702
- print(
703
- f"POLY_XY_DISPLACEMENT {dx} {dy} {'(Skipped)' if self._skip else ''}"
704
- )
705
- if dx == 0 and dy == 0:
706
- if self._debug:
707
- print("POLY_XY_DISPLACEMENT (Terminated)")
708
- break
709
- if self._skip:
710
- continue
711
- self._x += dx
712
- self._y += dy
713
- if self._pen:
714
- self._path.line(self._last_x, self._last_y, self._x, self._y)
715
- else:
716
- self._path.move(self._x, self._y)
717
- self._last_x, self._last_y = self._x, self._y
718
- if self._skip:
719
- self._skip = False
720
-
721
- def _octant_arc(self):
722
- """
723
- Octant arc spans one or more 45° octants starting and ending at a boundary.
724
- Octants are numbered ccw starting from 0° at the 3 o'clock position.
725
-
726
- 3 2 1
727
- /
728
- 4-O-0
729
- / \
730
- 5 6 7
731
-
732
- First byte specifies the radius as a value from 1 to 255. The second is the
733
- direction of the arc. Each nibble of the second byte defines s and c the start
734
- and the span.
735
- :return:
736
- """
737
- radius = self.pop() * self._scale
738
- sc = signed8(self.pop())
739
- s = (sc >> 4) & 0x7
740
- c = sc & 0x7
741
- if self._debug:
742
- print(f"OCTANT_ARC, {radius}, {s}, {c} {'(Skipped)' if self._skip else ''}")
743
- if self._skip:
744
- self._skip = False
745
- return
746
- octant = tau / 8.0
747
- ccw = (sc >> 7) & 1
748
- if c == 0:
749
- c = 8
750
- if ccw:
751
- s = -s
752
- start_angle = s * octant
753
- end_angle = (c + s) * octant
754
- mid_angle = (start_angle + end_angle) / 2
755
- # negative radius in the direction of start_octent finds center.
756
- cx = self._x - radius * cos(start_angle)
757
- cy = self._y - radius * sin(start_angle)
758
- mx = cx + radius * cos(mid_angle)
759
- my = cy + radius * sin(mid_angle)
760
- self._x = cx + radius * cos(end_angle)
761
- self._y = cy + radius * sin(end_angle)
762
- if self._pen:
763
- self._path.arc(self._last_x, self._last_y, mx, my, self._x, self._y)
764
- else:
765
- self._path.move(self._x, self._y)
766
- self._last_x, self._last_y = self._x, self._y
767
-
768
- def _fractional_arc(self):
769
- """
770
- Fractional Arc.
771
- Octant Arc plus fractional bits 0-255 parts of 45°
772
- 55° -> (55 - 45) * (256 / 45) = 56 (octent 1)
773
- 45° + (56/256 * 45°) = 55°
774
- 95° -> (95 - 90) * (256 / 45) = 28 (octent 2)
775
- 90° + (28/256 * 45°) = 95°
776
- """
777
- octant = tau / 8.0
778
- start_offset = octant * self.pop() / 256.0
779
- end_offset = octant * self.pop() / 256.0
780
- radius = (256 * self.pop() + self.pop()) * self._scale
781
- sc = signed8(self.pop())
782
- s = (sc >> 4) & 0x7
783
- c = sc & 0x7
784
-
785
- if self._debug:
786
- print(
787
- f"FRACTION_ARC {start_offset}, {end_offset}, {radius}, {s}, {c} {'(Skipped)' if self._skip else ''}"
788
- )
789
- if self._skip:
790
- self._skip = False
791
- return
792
- ccw = (sc >> 7) & 1
793
-
794
- if c == 0:
795
- c = 8
796
- if ccw:
797
- s = -s
798
- start_angle = start_offset + (s * octant)
799
- end_angle = (c + s) * octant + end_offset
800
- mid_angle = (start_angle + end_angle) / 2
801
- cx = self._x - radius * cos(start_angle)
802
- cy = self._y - radius * sin(start_angle)
803
- mx = cx + radius * cos(mid_angle)
804
- my = cy + radius * sin(mid_angle)
805
- self._x = cx + radius * cos(end_angle)
806
- self._y = cy + radius * sin(end_angle)
807
- if self._pen:
808
- self._path.arc(self._last_x, self._last_y, mx, my, self._x, self._y)
809
- else:
810
- self._path.move(self._x, self._y)
811
- self._last_x, self._last_y = self._x, self._y
812
-
813
- def _bulge_arc(self):
814
- """
815
- Arc defined by xy and displacement bulge. 1-byte-X, 1-byte-Y, 1-byte-bulge.
816
-
817
- This gives us X from -127 to +127 and Y from -127 to +127. The bulge height
818
- is given as 127 * 2 * H / D If the sign is negative the location is clockwise.
819
-
820
- :return:
821
- """
822
-
823
- dx = signed8(self.pop()) * self._scale
824
- dy = signed8(self.pop()) * self._scale
825
- h = signed8(self.pop())
826
-
827
- if self._debug:
828
- print(f"BULGE_ARC {dx}, {dy}, {h} {'(Skipped)' if self._skip else ''}")
829
- if self._skip:
830
- self._skip = False
831
- return
832
- r = abs(complex(dx, dy)) / 2
833
- bulge = h / 127.0
834
- bx = self._x + (dx / 2)
835
- by = self._y + (dy / 2)
836
- bulge_angle = atan2(dy, dx) - tau / 4
837
- mx = bx + r * bulge * cos(bulge_angle)
838
- my = by + r * bulge * sin(bulge_angle)
839
- self._x += dx
840
- self._y += dy
841
- if self._pen:
842
- if bulge == 0:
843
- self._path.line(self._last_x, self._last_y, self._x, self._y)
844
- else:
845
- self._path.arc(self._last_x, self._last_y, mx, my, self._x, self._y)
846
- else:
847
- self._path.move(self._x, self._y)
848
- self._last_x, self._last_y = self._x, self._y
849
-
850
- def _poly_bulge_arc(self):
851
- """
852
- Similar to bulge but repeated, until X and Y are (0,0).
853
- :return:
854
- """
855
- h = 0
856
- while True:
857
- dx = signed8(self.pop()) * self._scale
858
- dy = signed8(self.pop()) * self._scale
859
- if self._debug:
860
- print(
861
- f"POLY_BULGE_ARC {dx}, {dy}, {h} {'(Skipped)' if self._skip else ''}"
862
- )
863
- if dx == 0 and dy == 0:
864
- if self._debug:
865
- print(f"POLY_BULGE_ARC (TERMINATED)")
866
- break
867
- h = signed8(self.pop())
868
- if self._skip:
869
- continue
870
- r = abs(complex(dx, dy)) / 2
871
- bulge = h / 127.0
872
- bx = self._x + (dx / 2)
873
- by = self._y + (dy / 2)
874
- bulge_angle = atan2(dy, dx) - tau / 4
875
- mx = bx + r * bulge * cos(bulge_angle)
876
- my = by + r * bulge * sin(bulge_angle)
877
- self._x += dx
878
- self._y += dy
879
- if self._pen:
880
- if bulge == 0:
881
- self._path.line(self._last_x, self._last_y, self._x, self._y)
882
- else:
883
- self._path.arc(self._last_x, self._last_y, mx, my, self._x, self._y)
884
- else:
885
- self._path.move(self._x, self._y)
886
- self._last_x, self._last_y = self._x, self._y
887
- if self._skip:
888
- self._skip = False
889
-
890
- def _cond_mode_2(self):
891
- """
892
- Process the next command only in vertical text.
893
-
894
- :return:
895
- """
896
- if self._debug:
897
- print("COND_MODE_2")
898
- if self.modes == 2 and self._horizontal:
899
- if self._debug:
900
- print("SKIP NEXT")
901
- self._skip = True
1
+ from math import atan2, cos, isinf, sin, tau
2
+
3
+ SHXPARSER_VERSION = "0.0.2"
4
+
5
+
6
+ END_OF_SHAPE = 0
7
+ PEN_DOWN = 1
8
+ PEN_UP = 2
9
+ DIVIDE_VECTOR = 3
10
+ MULTIPLY_VECTOR = 4
11
+ PUSH_STACK = 5
12
+ POP_STACK = 6
13
+ DRAW_SUBSHAPE = 7
14
+ XY_DISPLACEMENT = 8
15
+ POLY_XY_DISPLACEMENT = 9 # 0,0 terminated
16
+ OCTANT_ARC = 0xA
17
+ FRACTIONAL_ARC = 0xB # 5 bytes, start, end, high radius, radius, ±0SC
18
+ BULGE_ARC = 0xC # dx, dy, bulge
19
+ POLY_BULGE_ARC = 0xD # 0,0 terminated BULGE_ARC
20
+ COND_MODE_2 = 0x0E # PROCESS this command *only if mode=2*
21
+
22
+
23
+ def signed8(b):
24
+ if b > 127:
25
+ return -256 + b
26
+ else:
27
+ return b
28
+
29
+
30
+ def int_16le(byte):
31
+ return (byte[0] & 0xFF) + ((byte[1] & 0xFF) << 8)
32
+
33
+
34
+ def int_32le(b):
35
+ return (
36
+ (b[0] & 0xFF)
37
+ + ((b[1] & 0xFF) << 8)
38
+ + ((b[2] & 0xFF) << 16)
39
+ + ((b[3] & 0xFF) << 24)
40
+ )
41
+
42
+
43
+ def read_int_8(stream):
44
+ byte = bytearray(stream.read(1))
45
+ if len(byte) == 1:
46
+ return byte[0]
47
+ return None
48
+
49
+
50
+ def read_int_16le(stream):
51
+ byte = bytearray(stream.read(2))
52
+ if len(byte) == 2:
53
+ return int_16le(byte)
54
+ return None
55
+
56
+
57
+ def read_int_32le(stream):
58
+ b = bytearray(stream.read(4))
59
+ if len(b) == 4:
60
+ return int_32le(b)
61
+ return None
62
+
63
+
64
+ def read_string(stream):
65
+ bb = bytearray()
66
+ try:
67
+ while True:
68
+ b = stream.read(1)
69
+ if b == b"":
70
+ return bb.decode("utf-8")
71
+ if b == b"\r" or b == b"\n" or b == b"\x00":
72
+ return bb.decode("utf-8")
73
+ bb += b
74
+ except UnicodeDecodeError as e:
75
+ raise ShxFontParseError(f"Read string did not capture valid text. {bb}") from e
76
+
77
+
78
+ class ShxPath:
79
+ """
80
+ Example path code. Any class with these functions would work as well. When render is called on the ShxFont class
81
+ the path is given particular useful segments.
82
+ """
83
+
84
+ def __init__(self):
85
+ self.path = list()
86
+
87
+ def bounds(self):
88
+ """
89
+ Get bounds of paths.
90
+ :return:
91
+ """
92
+ min_x = float("inf")
93
+ min_y = float("inf")
94
+ max_x = -float("inf")
95
+ max_y = -float("inf")
96
+ for p in self.path:
97
+ if p is None:
98
+ continue
99
+ min_x = min(p[0], min_x)
100
+ min_y = min(p[1], min_y)
101
+ max_x = max(p[0], max_x)
102
+ max_y = max(p[1], max_y)
103
+
104
+ min_x = min(p[-2], min_x)
105
+ min_y = min(p[-1], min_y)
106
+ max_x = max(p[-2], max_x)
107
+ max_y = max(p[-1], max_y)
108
+ if isinf(min_x):
109
+ return None
110
+ return min_x, min_y, max_x, max_y
111
+
112
+ def scale(self, scale_x, scale_y):
113
+ for p in self.path:
114
+ if p is None:
115
+ continue
116
+ if len(p) >= 2:
117
+ p[0] *= scale_x
118
+ p[1] *= scale_y
119
+ if len(p) >= 4:
120
+ p[2] *= scale_x
121
+ p[3] *= scale_y
122
+ if len(p) >= 6:
123
+ p[4] *= scale_x
124
+ p[5] *= scale_y
125
+
126
+ def translate(self, translate_x, translate_y):
127
+ for p in self.path:
128
+ if p is None:
129
+ continue
130
+ if len(p) >= 2:
131
+ p[0] += translate_x
132
+ p[1] += translate_y
133
+ if len(p) >= 4:
134
+ p[2] += translate_x
135
+ p[3] += translate_y
136
+ if len(p) >= 6:
137
+ p[4] += translate_x
138
+ p[5] += translate_y
139
+
140
+ def new_path(self):
141
+ """
142
+ Start of a new path.
143
+ """
144
+ self.path.append(None)
145
+
146
+ def move(self, x, y):
147
+ """
148
+ Move current point to the point specified.
149
+ """
150
+ self.path.append([x, y])
151
+
152
+ def line(self, x0, y0, x1, y1):
153
+ """
154
+ Draw a line from the current point to the specified point.
155
+ """
156
+ self.path.append([x0, y0, x1, y1])
157
+
158
+ def arc(self, x0, y0, cx, cy, x1, y1):
159
+ """
160
+ Draw an arc from the current point to specified point going through the control point.
161
+
162
+ 3 Points define a circular arc, there is only one arc which travels from start to end going through a given
163
+ control point. The exceptions are when the arc points are collinear or two arc points are coincident. In some
164
+ cases the start and end points will be equal and the control point will be located the circle diameter away.
165
+ """
166
+ self.path.append([x0, y0, cx, cy, x1, y1])
167
+
168
+
169
+ class ShxFontParseError(Exception):
170
+ """
171
+ Exception thrown if unable to pop a value from the given codes or other suspected parsing errors.
172
+ """
173
+
174
+
175
+ class ShxFont:
176
+ """
177
+ This class performs the parsing of the three major types of .SHX fonts. Composing them into specific glyphs which
178
+ consist of commands in a vector-shape language. When .render() is called on some text, vector actions are performed
179
+ on the font which create the vector path.
180
+ """
181
+
182
+ def __init__(self, filename, debug=False):
183
+ self.STROKE_BASED = True
184
+ self.format = None # format (usually AutoCAD-86)
185
+ self.type = None # Font type: shapes, bigfont, unifont
186
+ self.version = None # Font file version (usually 1.0).
187
+ self.glyphs = dict() # Glyph dictionary
188
+ self.font_name = None # Parsed font name.
189
+ self.above = None # Distance above baseline for capital letters.
190
+ self.below = None # Distance below baseline for lowercase letters
191
+ self.modes = None # 0 Horizontal Only, 2 Dual mode (Horizontal or Vertical)
192
+ self.encoding = False # 0 unicode, 1 packed multibyte, 2 shape file
193
+ self.embedded = False # 0 font can be embedded, 1 font cannot be embedded, 2 embedding is read-only
194
+
195
+ self._debug = debug
196
+ self._code = None
197
+ self._path = None
198
+ self._skip = False
199
+ self._pen = False
200
+ self._horizontal = True
201
+ self._letter = None
202
+ self._x = 0
203
+ self._y = 0
204
+ self._last_x = 0
205
+ self._last_y = 0
206
+ self._scale = 1
207
+ self._stack = []
208
+ self.active = True
209
+ self._line_information = []
210
+
211
+ self._parse(filename)
212
+
213
+ def __str__(self):
214
+ return f'{self.type}("{self.font_name}", {self.version}, glyphs: {len(self.glyphs)})'
215
+
216
+ def line_information(self):
217
+ return self._line_information
218
+
219
+ def _parse(self, filename):
220
+ with open(filename, "br") as f:
221
+ self._parse_header(f)
222
+ if self._debug:
223
+ print(f"Font header indicates font type is {self.type}")
224
+ if self.type == "shapes":
225
+ self._parse_shapes(f)
226
+ elif self.type == "bigfont":
227
+ self._parse_bigfont(f)
228
+ elif self.type == "unifont":
229
+ self._parse_unifont(f)
230
+ else:
231
+ raise ShxFontParseError(f"{self.type} is not a valid shx file type.")
232
+
233
+ def _parse_header(self, f):
234
+ header = read_string(f)
235
+ parts = header.split(" ")
236
+ if len(parts) != 3:
237
+ raise ShxFontParseError(f"Header information invalid: {header}")
238
+ self.format = parts[0]
239
+ self.type = parts[1]
240
+ self.version = parts[2]
241
+ f.read(2)
242
+
243
+ def _parse_shapes(self, f):
244
+ start = read_int_16le(f)
245
+ end = read_int_16le(f)
246
+ count = read_int_16le(f)
247
+ if self._debug:
248
+ print(f"Parsing shape: start={start}, end={end}, count={count}")
249
+ glyph_ref = list()
250
+ for i in range(count):
251
+ index = read_int_16le(f)
252
+ length = read_int_16le(f)
253
+ glyph_ref.append((index, length))
254
+
255
+ for index, length in glyph_ref:
256
+ if index == 0:
257
+ if self.font_name is not None:
258
+ raise ShxFontParseError("Double-initializing glyph data detected")
259
+ self.font_name = read_string(f)
260
+ self.above = read_int_8(f) # vector lengths above baseline
261
+ self.below = read_int_8(f) # vector lengths below baseline
262
+ # 0 - Horizontal, 2 - dual. 0x0E command only when mode=2
263
+ self.modes = read_int_8(f)
264
+ # end = read_int_16le(f)
265
+ else:
266
+ read = f.read(length)
267
+ data = read
268
+ if len(data) != length:
269
+ raise ShxFontParseError("Glyph length did not exist in file.")
270
+ if data[0] == 0 and data[1] == 0:
271
+ data = data[2:]
272
+ elif data[0] == 0:
273
+ data = data[1:]
274
+ find = data.find(b"\x00")
275
+ if find != -1:
276
+ name = data[:find]
277
+
278
+ for c in name:
279
+ if (
280
+ ord("A") <= c <= ord("Z")
281
+ or ord("0") <= c <= ord("9")
282
+ or c == ord(" ")
283
+ or c == ord("&")
284
+ ):
285
+ continue
286
+ name = None
287
+ break
288
+ if name is not None:
289
+ data = data[find + 1 :]
290
+ name = name.decode()
291
+ self.glyphs[name] = data
292
+ else:
293
+ if self._debug:
294
+ print(f"{data} did not contain a name.")
295
+ self.glyphs[index] = data
296
+
297
+ def _parse_bigfont(self, f):
298
+ count = read_int_16le(f)
299
+ length = read_int_16le(f)
300
+ changes = list()
301
+ change_count = read_int_16le(f)
302
+ if self._debug:
303
+ print(
304
+ f"Parsing bigfont: count={count}, length={length}, change_count={change_count}"
305
+ )
306
+ for i in range(change_count):
307
+ start = read_int_16le(f)
308
+ end = read_int_16le(f)
309
+ changes.append((start, end))
310
+
311
+ glyph_ref = list()
312
+ for i in range(count):
313
+ index = read_int_16le(f)
314
+ length = read_int_16le(f)
315
+ offset = read_int_32le(f)
316
+ glyph_ref.append((index, length, offset))
317
+
318
+ for index, length, offset in glyph_ref:
319
+ f.seek(offset, 0)
320
+ if index == 0:
321
+ # self.font_name = read_string(f)
322
+ self.above = read_int_8(f) # vector lengths above baseline
323
+ self.below = read_int_8(f) # vector lengths below baseline
324
+ # 0 - Horizontal, 2 - dual. 0x0E command only when mode=2
325
+ self.modes = read_int_8(f)
326
+
327
+ else:
328
+ self.glyphs[index] = f.read(length)
329
+
330
+ def _parse_unifont(self, f):
331
+ count = read_int_32le(f)
332
+ length = read_int_16le(f)
333
+ f.seek(5)
334
+ self.font_name = read_string(f)
335
+ self.above = read_int_8(f)
336
+ self.below = read_int_8(f)
337
+ self.mode = read_int_8(f)
338
+ self.encoding = read_int_8(f)
339
+ self.embedded = read_int_8(f)
340
+ ignore = read_int_8(f)
341
+ if self._debug:
342
+ print(
343
+ f"Parsing unifont: name={self.font_name}, count={count}, length={length}"
344
+ )
345
+ for i in range(count - 1):
346
+ index = read_int_16le(f)
347
+ length = read_int_16le(f)
348
+ self.glyphs[index] = f.read(length)
349
+
350
+ def pop(self):
351
+ try:
352
+ return self._code.pop()
353
+ except IndexError as e:
354
+ raise ShxFontParseError("No codes to pop()") from e
355
+
356
+ def render(
357
+ self,
358
+ path,
359
+ vtext,
360
+ horizontal=True,
361
+ font_size=12.0,
362
+ h_spacing=1.0,
363
+ v_spacing=1.1,
364
+ align="start",
365
+ ):
366
+ def _do_render(to_render, offsets):
367
+ if self.above is None:
368
+ self.above = 1
369
+ if self.below is None:
370
+ self.below = 0
371
+ self._scale = font_size / self.above
372
+ self._horizontal = horizontal
373
+ self._path = path
374
+ replacer = []
375
+ for tchar in to_render:
376
+ to_replace = None
377
+ if tchar == "\n":
378
+ continue
379
+ # Yes, I am German :-)
380
+ if ord(tchar) not in self.glyphs:
381
+ if tchar == "ä":
382
+ to_replace = (tchar, "ae")
383
+ elif tchar == "ö":
384
+ to_replace = (tchar, "ue")
385
+ elif tchar == "ü":
386
+ to_replace = (tchar, "ue")
387
+ elif tchar == "Ä":
388
+ to_replace = (tchar, "Ae")
389
+ elif tchar == "Ö":
390
+ to_replace = (tchar, "Oe")
391
+ elif tchar == "Ü":
392
+ to_replace = (tchar, "Ue")
393
+ elif tchar == "ß":
394
+ to_replace = (tchar, "ss")
395
+ if to_replace is not None and to_replace not in replacer:
396
+ replacer.append(to_replace)
397
+ for to_replace in replacer:
398
+ # print (f"Replace all '{to_replace[0]}' with '{to_replace[1]}'")
399
+ to_render = to_render.replace(to_replace[0], to_replace[1])
400
+ lines = to_render.split("\n")
401
+ offset_y = 0
402
+ if offsets is None:
403
+ offsets = [0] * len(lines)
404
+ self._line_information.clear()
405
+ for text, offs in zip(lines, offsets):
406
+ self._y = offset_y * self._scale
407
+ self._last_y = self._y
408
+ self._x = offs
409
+ self._last_x = offs
410
+ maxx = offs
411
+ line_start_x = self._x
412
+ line_start_y = self._y
413
+ for letter in text:
414
+ last_letter_x = self._last_x
415
+ last_letter_y = self._last_y
416
+ self._letter = letter
417
+ try:
418
+ self._code = bytearray(reversed(self.glyphs[ord(letter)]))
419
+ except KeyError:
420
+ # Letter is not found.
421
+ continue
422
+ self._pen = True
423
+ while self._code:
424
+ try:
425
+ self._parse_code()
426
+ except IndexError as e:
427
+ raise ShxFontParseError("Stack Error during render.") from e
428
+ self._skip = False
429
+ if h_spacing != 1.0:
430
+ dx = (h_spacing - 1) * (self._last_x - last_letter_x)
431
+ self._last_x += dx
432
+ self._x += dx
433
+ maxx = max(maxx, self._x)
434
+ if self.active:
435
+ path.character_end()
436
+ # Store start point, nonscaled width plus scaled width and height of line
437
+ self._line_information.append(
438
+ (
439
+ line_start_x,
440
+ line_start_y,
441
+ maxx,
442
+ maxx - line_start_x,
443
+ self._scale * (self.above + self.below),
444
+ )
445
+ )
446
+ offset_y -= v_spacing * (self.above + self.below)
447
+
448
+ if self._debug:
449
+ print(f"Render Complete.\n\n\n")
450
+ line_lens = [e[2] for e in self._line_information]
451
+ return line_lens
452
+
453
+ if vtext is None or vtext == "":
454
+ return
455
+ self.active = False
456
+ line_lengths = _do_render(vtext, None)
457
+ max_len = max(line_lengths)
458
+ offsets = []
459
+ for ll in line_lengths:
460
+ # NB anchor not only defines the alignment of the individual
461
+ # lines to another but as well of the whole block relative
462
+ # to the origin
463
+ if align == "middle":
464
+ offs = -max_len / 2 + (max_len - ll) / 2
465
+ elif align == "end":
466
+ offs = -ll
467
+ else:
468
+ offs = 0
469
+ offsets.append(offs)
470
+ self.active = True
471
+ line_lengths = _do_render(vtext, offsets)
472
+
473
+ def _parse_code(self):
474
+ b = self.pop()
475
+ direction = b & 0x0F
476
+ length = (b & 0xF0) >> 4
477
+ if length == 0:
478
+ self._parse_code_special(direction)
479
+ else:
480
+ self._parse_code_length(direction, length)
481
+
482
+ def _parse_code_length(self, direction, length):
483
+ """
484
+ Length direction codes. If length is 0 then direction is special otherwise the
485
+ command is a move in one of 16 different directions moving in 22.5° increments for
486
+ a distance of 1 to 15 units lengths.
487
+
488
+ :param direction:
489
+ :param length:
490
+ :return:
491
+ """
492
+ if self._debug:
493
+ print(
494
+ f"MOVE DIRECTION {direction} for {length} {'(Skipped)' if self._skip else ''}"
495
+ )
496
+ if self._skip:
497
+ self._skip = False
498
+ return
499
+ if direction in (2, 1, 0, 0xF, 0xE):
500
+ dx = 1.0
501
+ elif direction in (3, 0xD):
502
+ dx = 0.5
503
+ elif direction in (4, 0xC):
504
+ dx = 0.0
505
+ elif direction in (5, 0xB):
506
+ dx = -0.5
507
+ else: # (6, 7, 8, 9, 0xa):
508
+ dx = -1.0
509
+ if direction in (6, 5, 4, 3, 2):
510
+ dy = 1.0
511
+ elif direction in (7, 1):
512
+ dy = 0.5
513
+ elif direction in (8, 0):
514
+ dy = 0.0
515
+ elif direction in (9, 0xF):
516
+ dy = -0.5
517
+ else: # (0xa, 0xb, 0xc, 0xd, 0xe, 0xf):
518
+ dy = -1.0
519
+ self._x += dx * length * self._scale
520
+ self._y += dy * length * self._scale
521
+ if self.active:
522
+ if self._pen:
523
+ self._path.line(self._last_x, self._last_y, self._x, self._y)
524
+ else:
525
+ self._path.move(self._x, self._y)
526
+ self._last_x, self._last_y = self._x, self._y
527
+
528
+ def _parse_code_special(self, special):
529
+ if special == END_OF_SHAPE:
530
+ self._end_of_shape()
531
+ elif special == PEN_DOWN:
532
+ self._pen_down()
533
+ elif special == PEN_UP:
534
+ self._pen_up()
535
+ elif special == DIVIDE_VECTOR:
536
+ self._divide_vector()
537
+ elif special == MULTIPLY_VECTOR:
538
+ self._multiply_vector()
539
+ elif special == PUSH_STACK:
540
+ self._push_stack()
541
+ elif special == POP_STACK:
542
+ self._pop_stack()
543
+ elif special == DRAW_SUBSHAPE:
544
+ self._draw_subshape()
545
+ elif special == XY_DISPLACEMENT:
546
+ self._xy_displacement()
547
+ elif special == POLY_XY_DISPLACEMENT:
548
+ self._poly_xy_displacement()
549
+ elif special == OCTANT_ARC:
550
+ self._octant_arc()
551
+ elif special == FRACTIONAL_ARC:
552
+ self._fractional_arc()
553
+ elif special == BULGE_ARC:
554
+ self._bulge_arc()
555
+ elif special == POLY_BULGE_ARC:
556
+ self._poly_bulge_arc()
557
+ elif special == COND_MODE_2:
558
+ self._cond_mode_2()
559
+
560
+ def _end_of_shape(self):
561
+ """
562
+ End of shape definition.
563
+ :return:
564
+ """
565
+ try:
566
+ while self.pop() != 0:
567
+ pass
568
+ except ShxFontParseError:
569
+ pass
570
+ if self._debug:
571
+ print("END_OF_SHAPE")
572
+ if self._skip:
573
+ self._skip = False
574
+ return
575
+ if self.active:
576
+ self._path.new_path()
577
+
578
+ def _pen_down(self):
579
+ """
580
+ Activates draw mode. Pen is down. Draw is activated for each shape.
581
+ :return:
582
+ """
583
+ if self._debug:
584
+ print(f"PEN_DOWN: {self._x}, {self._y} {'(Skipped)' if self._skip else ''}")
585
+ if self._skip:
586
+ self._skip = False
587
+ return
588
+ self._pen = True
589
+ if self.active:
590
+ self._path.move(self._x, self._y)
591
+
592
+ def _pen_up(self):
593
+ """
594
+ Deactivates draw mode. Subsequent draws are moves to new locations.
595
+ :return:
596
+ """
597
+ if self._debug:
598
+ print(f"PEN_UP {'(Skipped)' if self._skip else ''}")
599
+ if self._skip:
600
+ self._skip = False
601
+ return
602
+ self._pen = False
603
+
604
+ def _divide_vector(self):
605
+ """
606
+ Height is specified with shape command. Initially considered the length of a single vector.
607
+ Divides the scale factor by the next byte.
608
+
609
+ :return:
610
+ """
611
+ factor = self.pop()
612
+ if self._debug:
613
+ print(
614
+ f"DIVIDE_VECTOR {self._scale}/{factor} {'(Skipped)' if self._skip else ''}"
615
+ )
616
+ if factor == 0:
617
+ raise ShxFontParseError("Divide Vector is not permitted to be 0.")
618
+ if self._skip:
619
+ self._skip = False
620
+ return
621
+ self._scale /= factor
622
+
623
+ def _multiply_vector(self):
624
+ """
625
+ Multiplies the scale factor by the next byte.
626
+
627
+ :return:
628
+ """
629
+ factor = self.pop()
630
+ if self._debug:
631
+ print(
632
+ f"MULTIPLY_VECTOR {self._scale}*{factor} {'(Skipped)' if self._skip else ''}"
633
+ )
634
+ if factor == 0:
635
+ raise ShxFontParseError("Multiply Vector is not permitted to be 0.")
636
+ if self._skip:
637
+ self._skip = False
638
+ return
639
+ self._scale *= factor
640
+
641
+ def _push_stack(self):
642
+ """
643
+ Stack is considered four units deep. Everything pushed on the stack must be
644
+ popped from the stack. Overflows respond with an error message.
645
+
646
+ :return:
647
+ """
648
+ if self._debug:
649
+ print(
650
+ f"PUSH_STACK {self._x}, {self._y} {'(Skipped)' if self._skip else ''}"
651
+ )
652
+ if self._skip:
653
+ self._skip = False
654
+ return
655
+ self._stack.append((self._x, self._y))
656
+ if len(self._stack) == 4:
657
+ raise IndexError(f"Position stack overflow in shape {self._letter}")
658
+
659
+ def _pop_stack(self):
660
+ """
661
+ Stack is considered four units deep. You may not pop more locations than have
662
+ been pushed onto the stack. Attempts to do so will respond with an error message.
663
+ :return:
664
+ """
665
+ if self._debug:
666
+ print(
667
+ f"POP_STACK {self._x}, {self._y} {'(Skipped)' if self._skip else ''}"
668
+ )
669
+
670
+ if self._skip:
671
+ self._skip = False
672
+ return
673
+ try:
674
+ self._x, self._y = self._stack.pop()
675
+ except IndexError:
676
+ raise IndexError(f"Position stack underflow in shape {self._letter}")
677
+ if self.active:
678
+ self._path.move(self._x, self._y)
679
+ self._last_x, self._last_y = self._x, self._y
680
+
681
+ def _draw_subshape_shapes(self):
682
+ subshape = self.pop()
683
+ if self._debug:
684
+ print(
685
+ f"Appending glyph {subshape} (Type={self.type}). {'(Skipped)' if self._skip else ''}"
686
+ )
687
+ if self._skip:
688
+ self._skip = False
689
+ return
690
+ try:
691
+ shape = self.glyphs[subshape]
692
+ except KeyError as e:
693
+ raise ShxFontParseError("Referenced subshape does not exist.") from e
694
+ self._code += bytearray(reversed(shape))
695
+
696
+ def _draw_subshape_bigfont(self):
697
+ subshape = self.pop()
698
+ if self._debug:
699
+ print(
700
+ f"Appending glyph {subshape} (Type={self.type}). {'(Skipped)' if self._skip else ''}"
701
+ )
702
+ if subshape == 0:
703
+ subshape = int_16le([self.pop(), self.pop()])
704
+ origin_x = self.pop() * self._scale
705
+ origin_y = self.pop() * self._scale
706
+ width = self.pop() * self._scale
707
+ height = self.pop() * self._scale
708
+ if self._debug:
709
+ print(
710
+ f"Extended Bigfont Glyph: {subshape}, origin_x = {origin_x}, origin_y = {origin_y}. {width}x{height}"
711
+ )
712
+ if self._skip:
713
+ self._skip = False
714
+ return
715
+ try:
716
+ shape = self.glyphs[subshape]
717
+ except KeyError as e:
718
+ raise ShxFontParseError("Referenced subshape does not exist.") from e
719
+ self._code += bytearray(reversed(shape))
720
+
721
+ def _draw_subshape_unifont(self):
722
+ subshape = int_16le([self.pop(), self.pop()])
723
+ if self._debug:
724
+ print(
725
+ f"Appending glyph {subshape} (Type={self.type}). {'(Skipped)' if self._skip else ''}"
726
+ )
727
+ if self._skip:
728
+ self._skip = False
729
+ return
730
+ try:
731
+ shape = self.glyphs[subshape]
732
+ except KeyError as e:
733
+ raise ShxFontParseError("Referenced subshape does not exist.") from e
734
+ self._code += bytearray(reversed(shape))
735
+
736
+ def _draw_subshape(self):
737
+ """
738
+ Subshape is given in next byte, for non-unicode fonts one byte is used for
739
+ unicode fonts two bytes are used and shapes are numbered from 1 to 65535.
740
+
741
+ Drawmode is not reset for the subshape. When complete the current shape
742
+ continues.
743
+ :return:
744
+ """
745
+ if self.type == "shapes":
746
+ self._draw_subshape_shapes()
747
+ elif self.type == "bigfont":
748
+ self._draw_subshape_bigfont()
749
+ elif self.type == "unifont":
750
+ self._draw_subshape_unifont()
751
+
752
+ def _xy_displacement(self):
753
+ """
754
+ X,Y displacement given in next two bytes 1 byte-x, 1 byte-y. The displacement
755
+ ranges from -128 to +127.
756
+ :return:
757
+ """
758
+ dx = signed8(self.pop()) * self._scale
759
+ dy = signed8(self.pop()) * self._scale
760
+ if self._debug:
761
+ print(f"XY_DISPLACEMENT {dx} {dy} {'(Skipped)' if self._skip else ''}")
762
+ if self._skip:
763
+ self._skip = False
764
+ return
765
+ self._x += dx
766
+ self._y += dy
767
+ if self.active:
768
+ if self._pen:
769
+ self._path.line(self._last_x, self._last_y, self._x, self._y)
770
+ else:
771
+ self._path.move(self._x, self._y)
772
+ self._last_x, self._last_y = self._x, self._y
773
+
774
+ def _poly_xy_displacement(self):
775
+ """
776
+ XY displacement in a series terminated with (0,0)
777
+ :return:
778
+ """
779
+ while True:
780
+ dx = signed8(self.pop()) * self._scale
781
+ dy = signed8(self.pop()) * self._scale
782
+ if self._debug:
783
+ print(
784
+ f"POLY_XY_DISPLACEMENT {dx} {dy} {'(Skipped)' if self._skip else ''}"
785
+ )
786
+ if dx == 0 and dy == 0:
787
+ if self._debug:
788
+ print("POLY_XY_DISPLACEMENT (Terminated)")
789
+ break
790
+ if self._skip:
791
+ continue
792
+ self._x += dx
793
+ self._y += dy
794
+ if self.active:
795
+ if self._pen:
796
+ self._path.line(self._last_x, self._last_y, self._x, self._y)
797
+ else:
798
+ self._path.move(self._x, self._y)
799
+ self._last_x, self._last_y = self._x, self._y
800
+ if self._skip:
801
+ self._skip = False
802
+
803
+ def _octant_arc(self):
804
+ """
805
+ Octant arc spans one or more 45° octants starting and ending at a boundary.
806
+ Octants are numbered ccw starting from 0° at the 3 o'clock position.
807
+
808
+ 3 2 1
809
+ ⍀ /
810
+ 4-O-0
811
+ / \
812
+ 5 6 7
813
+
814
+ First byte specifies the radius as a value from 1 to 255. The second is the
815
+ direction of the arc. Each nibble of the second byte defines s and c the start
816
+ and the span.
817
+ :return:
818
+ """
819
+ radius = self.pop() * self._scale
820
+ sc = signed8(self.pop())
821
+ s = (sc >> 4) & 0x7
822
+ c = sc & 0x7
823
+ if self._debug:
824
+ print(f"OCTANT_ARC, {radius}, {s}, {c} {'(Skipped)' if self._skip else ''}")
825
+ if self._skip:
826
+ self._skip = False
827
+ return
828
+ octant = tau / 8.0
829
+ ccw = (sc >> 7) & 1
830
+ if c == 0:
831
+ c = 8
832
+ if ccw:
833
+ s = -s
834
+ start_angle = s * octant
835
+ end_angle = (c + s) * octant
836
+ mid_angle = (start_angle + end_angle) / 2
837
+ # negative radius in the direction of start_octent finds center.
838
+ cx = self._x - radius * cos(start_angle)
839
+ cy = self._y - radius * sin(start_angle)
840
+ mx = cx + radius * cos(mid_angle)
841
+ my = cy + radius * sin(mid_angle)
842
+ self._x = cx + radius * cos(end_angle)
843
+ self._y = cy + radius * sin(end_angle)
844
+ if self.active:
845
+ if self._pen:
846
+ self._path.arc(self._last_x, self._last_y, mx, my, self._x, self._y)
847
+ else:
848
+ self._path.move(self._x, self._y)
849
+ self._last_x, self._last_y = self._x, self._y
850
+
851
+ def _fractional_arc(self):
852
+ """
853
+ Fractional Arc.
854
+ Octant Arc plus fractional bits 0-255 parts of 45°
855
+ 55° -> (55 - 45) * (256 / 45) = 56 (octent 1)
856
+ 45° + (56/256 * 45°) = 55°
857
+ 95° -> (95 - 90) * (256 / 45) = 28 (octent 2)
858
+ 90° + (28/256 * 45°) = 95°
859
+ """
860
+ octant = tau / 8.0
861
+ start_offset = octant * self.pop() / 256.0
862
+ end_offset = octant * self.pop() / 256.0
863
+ radius = (256 * self.pop() + self.pop()) * self._scale
864
+ sc = signed8(self.pop())
865
+ s = (sc >> 4) & 0x7
866
+ c = sc & 0x7
867
+
868
+ if self._debug:
869
+ print(
870
+ f"FRACTION_ARC {start_offset}, {end_offset}, {radius}, {s}, {c} {'(Skipped)' if self._skip else ''}"
871
+ )
872
+ if self._skip:
873
+ self._skip = False
874
+ return
875
+ ccw = (sc >> 7) & 1
876
+
877
+ if c == 0:
878
+ c = 8
879
+ if ccw:
880
+ s = -s
881
+ start_angle = start_offset + (s * octant)
882
+ end_angle = (c + s) * octant + end_offset
883
+ mid_angle = (start_angle + end_angle) / 2
884
+ cx = self._x - radius * cos(start_angle)
885
+ cy = self._y - radius * sin(start_angle)
886
+ mx = cx + radius * cos(mid_angle)
887
+ my = cy + radius * sin(mid_angle)
888
+ self._x = cx + radius * cos(end_angle)
889
+ self._y = cy + radius * sin(end_angle)
890
+ if self.active:
891
+ if self._pen:
892
+ self._path.arc(self._last_x, self._last_y, mx, my, self._x, self._y)
893
+ else:
894
+ self._path.move(self._x, self._y)
895
+ self._last_x, self._last_y = self._x, self._y
896
+
897
+ def _bulge_arc(self):
898
+ """
899
+ Arc defined by xy and displacement bulge. 1-byte-X, 1-byte-Y, 1-byte-bulge.
900
+
901
+ This gives us X from -127 to +127 and Y from -127 to +127. The bulge height
902
+ is given as 127 * 2 * H / D If the sign is negative the location is clockwise.
903
+
904
+ :return:
905
+ """
906
+
907
+ dx = signed8(self.pop()) * self._scale
908
+ dy = signed8(self.pop()) * self._scale
909
+ h = signed8(self.pop())
910
+
911
+ if self._debug:
912
+ print(f"BULGE_ARC {dx}, {dy}, {h} {'(Skipped)' if self._skip else ''}")
913
+ if self._skip:
914
+ self._skip = False
915
+ return
916
+ r = abs(complex(dx, dy)) / 2
917
+ bulge = h / 127.0
918
+ bx = self._x + (dx / 2)
919
+ by = self._y + (dy / 2)
920
+ bulge_angle = atan2(dy, dx) - tau / 4
921
+ mx = bx + r * bulge * cos(bulge_angle)
922
+ my = by + r * bulge * sin(bulge_angle)
923
+ self._x += dx
924
+ self._y += dy
925
+ if self.active:
926
+ if self._pen:
927
+ if bulge == 0:
928
+ self._path.line(self._last_x, self._last_y, self._x, self._y)
929
+ else:
930
+ self._path.arc(self._last_x, self._last_y, mx, my, self._x, self._y)
931
+ else:
932
+ self._path.move(self._x, self._y)
933
+ self._last_x, self._last_y = self._x, self._y
934
+
935
+ def _poly_bulge_arc(self):
936
+ """
937
+ Similar to bulge but repeated, until X and Y are (0,0).
938
+ :return:
939
+ """
940
+ h = 0
941
+ while True:
942
+ dx = signed8(self.pop()) * self._scale
943
+ dy = signed8(self.pop()) * self._scale
944
+ if self._debug:
945
+ print(
946
+ f"POLY_BULGE_ARC {dx}, {dy}, {h} {'(Skipped)' if self._skip else ''}"
947
+ )
948
+ if dx == 0 and dy == 0:
949
+ if self._debug:
950
+ print(f"POLY_BULGE_ARC (TERMINATED)")
951
+ break
952
+ h = signed8(self.pop())
953
+ if self._skip:
954
+ continue
955
+ r = abs(complex(dx, dy)) / 2
956
+ bulge = h / 127.0
957
+ bx = self._x + (dx / 2)
958
+ by = self._y + (dy / 2)
959
+ bulge_angle = atan2(dy, dx) - tau / 4
960
+ mx = bx + r * bulge * cos(bulge_angle)
961
+ my = by + r * bulge * sin(bulge_angle)
962
+ self._x += dx
963
+ self._y += dy
964
+ if self.active:
965
+ if self._pen:
966
+ if bulge == 0:
967
+ self._path.line(self._last_x, self._last_y, self._x, self._y)
968
+ else:
969
+ self._path.arc(
970
+ self._last_x, self._last_y, mx, my, self._x, self._y
971
+ )
972
+ else:
973
+ self._path.move(self._x, self._y)
974
+ self._last_x, self._last_y = self._x, self._y
975
+ if self._skip:
976
+ self._skip = False
977
+
978
+ def _cond_mode_2(self):
979
+ """
980
+ Process the next command only in vertical text.
981
+
982
+ :return:
983
+ """
984
+ if self._debug:
985
+ print("COND_MODE_2")
986
+ if self.modes == 2 and self._horizontal:
987
+ if self._debug:
988
+ print("SKIP NEXT")
989
+ self._skip = True