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