@fairyhunter13/opentui-core 0.1.90 → 0.1.92

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 (571) hide show
  1. package/3d/SpriteResourceManager.d.ts +74 -0
  2. package/3d/SpriteUtils.d.ts +13 -0
  3. package/3d/TextureUtils.d.ts +24 -0
  4. package/3d/ThreeRenderable.d.ts +40 -0
  5. package/3d/WGPURenderer.d.ts +61 -0
  6. package/3d/animation/ExplodingSpriteEffect.d.ts +71 -0
  7. package/3d/animation/PhysicsExplodingSpriteEffect.d.ts +76 -0
  8. package/3d/animation/SpriteAnimator.d.ts +124 -0
  9. package/3d/animation/SpriteParticleGenerator.d.ts +62 -0
  10. package/3d/canvas.d.ts +44 -0
  11. package/3d/index.d.ts +12 -0
  12. package/3d/physics/PlanckPhysicsAdapter.d.ts +19 -0
  13. package/3d/physics/RapierPhysicsAdapter.d.ts +19 -0
  14. package/3d/physics/physics-interface.d.ts +27 -0
  15. package/3d.d.ts +2 -0
  16. package/3d.js +34042 -0
  17. package/3d.js.map +155 -0
  18. package/LICENSE +21 -0
  19. package/NativeSpanFeed.d.ts +41 -0
  20. package/README.md +2 -2
  21. package/Renderable.d.ts +334 -0
  22. package/animation/Timeline.d.ts +126 -0
  23. package/ansi.d.ts +13 -0
  24. package/buffer.d.ts +107 -0
  25. package/console.d.ts +143 -0
  26. package/edit-buffer.d.ts +98 -0
  27. package/editor-view.d.ts +73 -0
  28. package/index-e6ec7apq.js +18415 -0
  29. package/index-e6ec7apq.js.map +64 -0
  30. package/index-h066zmrb.js +12619 -0
  31. package/index-h066zmrb.js.map +43 -0
  32. package/index-ynzawt3n.js +113 -0
  33. package/index-ynzawt3n.js.map +10 -0
  34. package/index.d.ts +21 -0
  35. package/index.js +430 -0
  36. package/index.js.map +9 -0
  37. package/lib/KeyHandler.d.ts +61 -0
  38. package/lib/RGBA.d.ts +25 -0
  39. package/lib/ascii.font.d.ts +508 -0
  40. package/lib/border.d.ts +49 -0
  41. package/lib/bunfs.d.ts +7 -0
  42. package/lib/clipboard.d.ts +17 -0
  43. package/lib/clock.d.ts +15 -0
  44. package/lib/data-paths.d.ts +26 -0
  45. package/lib/debounce.d.ts +42 -0
  46. package/lib/detect-links.d.ts +6 -0
  47. package/lib/env.d.ts +42 -0
  48. package/lib/extmarks-history.d.ts +17 -0
  49. package/lib/extmarks.d.ts +89 -0
  50. package/lib/hast-styled-text.d.ts +17 -0
  51. package/lib/index.d.ts +21 -0
  52. package/lib/keymapping.d.ts +25 -0
  53. package/lib/objects-in-viewport.d.ts +24 -0
  54. package/lib/output.capture.d.ts +24 -0
  55. package/lib/parse.keypress-kitty.d.ts +2 -0
  56. package/lib/parse.keypress.d.ts +26 -0
  57. package/lib/parse.mouse.d.ts +30 -0
  58. package/lib/paste.d.ts +7 -0
  59. package/lib/queue.d.ts +15 -0
  60. package/lib/renderable.validations.d.ts +12 -0
  61. package/lib/scroll-acceleration.d.ts +43 -0
  62. package/lib/selection.d.ts +63 -0
  63. package/lib/singleton.d.ts +7 -0
  64. package/lib/stdin-parser.d.ts +76 -0
  65. package/lib/styled-text.d.ts +63 -0
  66. package/lib/terminal-capability-detection.d.ts +30 -0
  67. package/lib/terminal-palette.d.ts +50 -0
  68. package/lib/tree-sitter/assets/update.d.ts +11 -0
  69. package/lib/tree-sitter/client.d.ts +47 -0
  70. package/lib/tree-sitter/default-parsers.d.ts +2 -0
  71. package/lib/tree-sitter/download-utils.d.ts +21 -0
  72. package/lib/tree-sitter/index.d.ts +8 -0
  73. package/lib/tree-sitter/parser.worker.d.ts +1 -0
  74. package/lib/tree-sitter/parsers-config.d.ts +38 -0
  75. package/lib/tree-sitter/resolve-ft.d.ts +2 -0
  76. package/lib/tree-sitter/types.d.ts +81 -0
  77. package/lib/tree-sitter-styled-text.d.ts +14 -0
  78. package/lib/validate-dir-name.d.ts +1 -0
  79. package/lib/yoga.options.d.ts +32 -0
  80. package/package.json +51 -63
  81. package/parser.worker.js +869 -0
  82. package/parser.worker.js.map +12 -0
  83. package/plugins/core-slot.d.ts +72 -0
  84. package/plugins/registry.d.ts +38 -0
  85. package/plugins/types.d.ts +34 -0
  86. package/post/filters.d.ts +105 -0
  87. package/renderables/ASCIIFont.d.ts +52 -0
  88. package/renderables/Box.d.ts +72 -0
  89. package/renderables/Code.d.ts +78 -0
  90. package/renderables/Diff.d.ts +142 -0
  91. package/renderables/EditBufferRenderable.d.ts +162 -0
  92. package/renderables/FrameBuffer.d.ts +16 -0
  93. package/renderables/Input.d.ts +67 -0
  94. package/renderables/LineNumberRenderable.d.ts +74 -0
  95. package/renderables/Markdown.d.ts +173 -0
  96. package/renderables/ScrollBar.d.ts +77 -0
  97. package/renderables/ScrollBox.d.ts +124 -0
  98. package/renderables/Select.d.ts +115 -0
  99. package/renderables/Slider.d.ts +44 -0
  100. package/renderables/TabSelect.d.ts +96 -0
  101. package/renderables/Text.d.ts +36 -0
  102. package/renderables/TextBufferRenderable.d.ts +105 -0
  103. package/renderables/TextNode.d.ts +91 -0
  104. package/renderables/TextTable.d.ts +140 -0
  105. package/renderables/Textarea.d.ts +114 -0
  106. package/renderables/TimeToFirstDraw.d.ts +24 -0
  107. package/renderables/__tests__/renderable-test-utils.d.ts +12 -0
  108. package/renderables/composition/VRenderable.d.ts +16 -0
  109. package/renderables/composition/constructs.d.ts +35 -0
  110. package/renderables/composition/vnode.d.ts +46 -0
  111. package/renderables/index.d.ts +22 -0
  112. package/renderables/markdown-parser.d.ts +10 -0
  113. package/renderer.d.ts +388 -0
  114. package/runtime-plugin-support.d.ts +3 -0
  115. package/runtime-plugin-support.js +29 -0
  116. package/runtime-plugin-support.js.map +10 -0
  117. package/runtime-plugin.d.ts +11 -0
  118. package/runtime-plugin.js +16 -0
  119. package/runtime-plugin.js.map +9 -0
  120. package/syntax-style.d.ts +54 -0
  121. package/testing/manual-clock.d.ts +16 -0
  122. package/testing/mock-keys.d.ts +81 -0
  123. package/testing/mock-mouse.d.ts +38 -0
  124. package/testing/mock-tree-sitter-client.d.ts +23 -0
  125. package/testing/spy.d.ts +7 -0
  126. package/testing/test-recorder.d.ts +61 -0
  127. package/testing/test-renderer.d.ts +23 -0
  128. package/testing.d.ts +6 -0
  129. package/testing.js +675 -0
  130. package/testing.js.map +15 -0
  131. package/text-buffer-view.d.ts +42 -0
  132. package/text-buffer.d.ts +67 -0
  133. package/types.d.ts +131 -0
  134. package/utils.d.ts +14 -0
  135. package/zig-structs.d.ts +155 -0
  136. package/zig.d.ts +351 -0
  137. package/dev/keypress-debug-renderer.ts +0 -148
  138. package/dev/keypress-debug.ts +0 -43
  139. package/dev/print-env-vars.ts +0 -32
  140. package/dev/test-tmux-graphics-334.sh +0 -68
  141. package/dev/thai-debug-test.ts +0 -68
  142. package/docs/development.md +0 -141
  143. package/docs/env-vars.md +0 -140
  144. package/docs/getting-started.md +0 -353
  145. package/docs/renderables-vs-constructs.md +0 -159
  146. package/docs/tree-sitter.md +0 -311
  147. package/scripts/build.ts +0 -400
  148. package/scripts/publish.ts +0 -60
  149. package/src/3d/SpriteResourceManager.ts +0 -286
  150. package/src/3d/SpriteUtils.ts +0 -71
  151. package/src/3d/TextureUtils.ts +0 -196
  152. package/src/3d/ThreeRenderable.ts +0 -197
  153. package/src/3d/WGPURenderer.ts +0 -294
  154. package/src/3d/animation/ExplodingSpriteEffect.ts +0 -513
  155. package/src/3d/animation/PhysicsExplodingSpriteEffect.ts +0 -429
  156. package/src/3d/animation/SpriteAnimator.ts +0 -633
  157. package/src/3d/animation/SpriteParticleGenerator.ts +0 -435
  158. package/src/3d/canvas.ts +0 -464
  159. package/src/3d/index.ts +0 -12
  160. package/src/3d/physics/PlanckPhysicsAdapter.ts +0 -72
  161. package/src/3d/physics/RapierPhysicsAdapter.ts +0 -66
  162. package/src/3d/physics/physics-interface.ts +0 -31
  163. package/src/3d/shaders/supersampling.wgsl +0 -201
  164. package/src/3d.ts +0 -3
  165. package/src/NativeSpanFeed.ts +0 -300
  166. package/src/Renderable.ts +0 -1698
  167. package/src/__snapshots__/buffer.test.ts.snap +0 -28
  168. package/src/animation/Timeline.test.ts +0 -2709
  169. package/src/animation/Timeline.ts +0 -598
  170. package/src/ansi.ts +0 -18
  171. package/src/benchmark/latest-all-bench-run.json +0 -707
  172. package/src/benchmark/latest-async-bench-run.json +0 -336
  173. package/src/benchmark/latest-default-bench-run.json +0 -657
  174. package/src/benchmark/latest-large-bench-run.json +0 -707
  175. package/src/benchmark/latest-quick-bench-run.json +0 -207
  176. package/src/benchmark/markdown-benchmark.ts +0 -1804
  177. package/src/benchmark/native-span-feed-async-benchmark.ts +0 -355
  178. package/src/benchmark/native-span-feed-benchmark.md +0 -56
  179. package/src/benchmark/native-span-feed-benchmark.ts +0 -596
  180. package/src/benchmark/native-span-feed-compare.ts +0 -280
  181. package/src/benchmark/renderer-benchmark.ts +0 -754
  182. package/src/benchmark/text-table-benchmark.ts +0 -947
  183. package/src/buffer.test.ts +0 -291
  184. package/src/buffer.ts +0 -519
  185. package/src/console.test.ts +0 -612
  186. package/src/console.ts +0 -1255
  187. package/src/edit-buffer.test.ts +0 -1769
  188. package/src/edit-buffer.ts +0 -411
  189. package/src/editor-view.test.ts +0 -1032
  190. package/src/editor-view.ts +0 -284
  191. package/src/examples/ascii-font-selection-demo.ts +0 -245
  192. package/src/examples/assets/Water_2_M_Normal.jpg +0 -0
  193. package/src/examples/assets/concrete.png +0 -0
  194. package/src/examples/assets/crate.png +0 -0
  195. package/src/examples/assets/crate_emissive.png +0 -0
  196. package/src/examples/assets/forrest_background.png +0 -0
  197. package/src/examples/assets/hast-example.json +0 -1018
  198. package/src/examples/assets/heart.png +0 -0
  199. package/src/examples/assets/main_char_heavy_attack.png +0 -0
  200. package/src/examples/assets/main_char_idle.png +0 -0
  201. package/src/examples/assets/main_char_jump_end.png +0 -0
  202. package/src/examples/assets/main_char_jump_landing.png +0 -0
  203. package/src/examples/assets/main_char_jump_start.png +0 -0
  204. package/src/examples/assets/main_char_run_loop.png +0 -0
  205. package/src/examples/assets/roughness_map.jpg +0 -0
  206. package/src/examples/build.ts +0 -115
  207. package/src/examples/code-demo.ts +0 -584
  208. package/src/examples/console-demo.ts +0 -358
  209. package/src/examples/core-plugin-slots-demo.ts +0 -759
  210. package/src/examples/diff-demo.ts +0 -699
  211. package/src/examples/draggable-three-demo.ts +0 -259
  212. package/src/examples/editor-demo.ts +0 -322
  213. package/src/examples/extmarks-demo.ts +0 -204
  214. package/src/examples/focus-restore-demo.ts +0 -310
  215. package/src/examples/fonts.ts +0 -245
  216. package/src/examples/fractal-shader-demo.ts +0 -268
  217. package/src/examples/framebuffer-demo.ts +0 -674
  218. package/src/examples/full-unicode-demo.ts +0 -181
  219. package/src/examples/golden-star-demo.ts +0 -933
  220. package/src/examples/grayscale-buffer-demo.ts +0 -249
  221. package/src/examples/hast-syntax-highlighting-demo.ts +0 -129
  222. package/src/examples/index.ts +0 -925
  223. package/src/examples/input-demo.ts +0 -377
  224. package/src/examples/input-select-layout-demo.ts +0 -425
  225. package/src/examples/install.sh +0 -143
  226. package/src/examples/keypress-debug-demo.ts +0 -452
  227. package/src/examples/lib/HexList.ts +0 -122
  228. package/src/examples/lib/PaletteGrid.ts +0 -125
  229. package/src/examples/lib/standalone-keys.ts +0 -25
  230. package/src/examples/lib/tab-controller.ts +0 -243
  231. package/src/examples/lights-phong-demo.ts +0 -290
  232. package/src/examples/link-demo.ts +0 -220
  233. package/src/examples/live-state-demo.ts +0 -480
  234. package/src/examples/markdown-demo.ts +0 -620
  235. package/src/examples/mouse-interaction-demo.ts +0 -428
  236. package/src/examples/nested-zindex-demo.ts +0 -357
  237. package/src/examples/opacity-example.ts +0 -235
  238. package/src/examples/opentui-demo.ts +0 -1057
  239. package/src/examples/physx-planck-2d-demo.ts +0 -507
  240. package/src/examples/physx-rapier-2d-demo.ts +0 -526
  241. package/src/examples/relative-positioning-demo.ts +0 -323
  242. package/src/examples/scroll-example.ts +0 -214
  243. package/src/examples/scrollbox-mouse-test.ts +0 -112
  244. package/src/examples/scrollbox-overlay-hit-test.ts +0 -206
  245. package/src/examples/select-demo.ts +0 -237
  246. package/src/examples/shader-cube-demo.ts +0 -772
  247. package/src/examples/simple-layout-example.ts +0 -591
  248. package/src/examples/slider-demo.ts +0 -617
  249. package/src/examples/split-mode-demo.ts +0 -445
  250. package/src/examples/sprite-animation-demo.ts +0 -443
  251. package/src/examples/sprite-particle-generator-demo.ts +0 -486
  252. package/src/examples/static-sprite-demo.ts +0 -193
  253. package/src/examples/sticky-scroll-example.ts +0 -308
  254. package/src/examples/styled-text-demo.ts +0 -282
  255. package/src/examples/tab-select-demo.ts +0 -219
  256. package/src/examples/terminal-title.ts +0 -29
  257. package/src/examples/terminal.ts +0 -305
  258. package/src/examples/text-node-demo.ts +0 -416
  259. package/src/examples/text-selection-demo.ts +0 -377
  260. package/src/examples/text-table-demo.ts +0 -503
  261. package/src/examples/text-truncation-demo.ts +0 -481
  262. package/src/examples/text-wrap.ts +0 -757
  263. package/src/examples/texture-loading-demo.ts +0 -259
  264. package/src/examples/timeline-example.ts +0 -670
  265. package/src/examples/transparency-demo.ts +0 -241
  266. package/src/examples/vnode-composition-demo.ts +0 -404
  267. package/src/index.ts +0 -22
  268. package/src/lib/KeyHandler.integration.test.ts +0 -292
  269. package/src/lib/KeyHandler.stopPropagation.test.ts +0 -289
  270. package/src/lib/KeyHandler.test.ts +0 -662
  271. package/src/lib/KeyHandler.ts +0 -222
  272. package/src/lib/RGBA.test.ts +0 -984
  273. package/src/lib/RGBA.ts +0 -204
  274. package/src/lib/ascii.font.ts +0 -330
  275. package/src/lib/border.test.ts +0 -83
  276. package/src/lib/border.ts +0 -168
  277. package/src/lib/bunfs.test.ts +0 -27
  278. package/src/lib/bunfs.ts +0 -18
  279. package/src/lib/clipboard.test.ts +0 -41
  280. package/src/lib/clipboard.ts +0 -47
  281. package/src/lib/clock.ts +0 -31
  282. package/src/lib/data-paths.test.ts +0 -133
  283. package/src/lib/data-paths.ts +0 -109
  284. package/src/lib/debounce.ts +0 -106
  285. package/src/lib/detect-links.test.ts +0 -98
  286. package/src/lib/detect-links.ts +0 -56
  287. package/src/lib/env.test.ts +0 -228
  288. package/src/lib/env.ts +0 -209
  289. package/src/lib/extmarks-history.ts +0 -51
  290. package/src/lib/extmarks-multiwidth.test.ts +0 -322
  291. package/src/lib/extmarks.test.ts +0 -3457
  292. package/src/lib/extmarks.ts +0 -843
  293. package/src/lib/fonts/block.json +0 -405
  294. package/src/lib/fonts/grid.json +0 -265
  295. package/src/lib/fonts/huge.json +0 -741
  296. package/src/lib/fonts/pallet.json +0 -314
  297. package/src/lib/fonts/shade.json +0 -591
  298. package/src/lib/fonts/slick.json +0 -321
  299. package/src/lib/fonts/tiny.json +0 -69
  300. package/src/lib/hast-styled-text.ts +0 -59
  301. package/src/lib/index.ts +0 -21
  302. package/src/lib/keymapping.test.ts +0 -280
  303. package/src/lib/keymapping.ts +0 -87
  304. package/src/lib/objects-in-viewport.test.ts +0 -787
  305. package/src/lib/objects-in-viewport.ts +0 -153
  306. package/src/lib/output.capture.ts +0 -58
  307. package/src/lib/parse.keypress-kitty.protocol.test.ts +0 -340
  308. package/src/lib/parse.keypress-kitty.test.ts +0 -663
  309. package/src/lib/parse.keypress-kitty.ts +0 -439
  310. package/src/lib/parse.keypress.test.ts +0 -1849
  311. package/src/lib/parse.keypress.ts +0 -397
  312. package/src/lib/parse.mouse.test.ts +0 -552
  313. package/src/lib/parse.mouse.ts +0 -232
  314. package/src/lib/paste.ts +0 -16
  315. package/src/lib/queue.ts +0 -65
  316. package/src/lib/renderable.validations.test.ts +0 -87
  317. package/src/lib/renderable.validations.ts +0 -83
  318. package/src/lib/scroll-acceleration.ts +0 -98
  319. package/src/lib/selection.ts +0 -240
  320. package/src/lib/singleton.ts +0 -28
  321. package/src/lib/stdin-parser.test.ts +0 -1676
  322. package/src/lib/stdin-parser.ts +0 -1248
  323. package/src/lib/styled-text.ts +0 -178
  324. package/src/lib/terminal-capability-detection.test.ts +0 -202
  325. package/src/lib/terminal-capability-detection.ts +0 -79
  326. package/src/lib/terminal-palette.test.ts +0 -878
  327. package/src/lib/terminal-palette.ts +0 -383
  328. package/src/lib/tree-sitter/assets/README.md +0 -118
  329. package/src/lib/tree-sitter/assets/update.ts +0 -331
  330. package/src/lib/tree-sitter/assets.d.ts +0 -9
  331. package/src/lib/tree-sitter/cache.test.ts +0 -270
  332. package/src/lib/tree-sitter/client.test.ts +0 -1061
  333. package/src/lib/tree-sitter/client.ts +0 -615
  334. package/src/lib/tree-sitter/default-parsers.ts +0 -80
  335. package/src/lib/tree-sitter/download-utils.ts +0 -148
  336. package/src/lib/tree-sitter/index.ts +0 -28
  337. package/src/lib/tree-sitter/parser.worker.ts +0 -1001
  338. package/src/lib/tree-sitter/parsers-config.ts +0 -75
  339. package/src/lib/tree-sitter/resolve-ft.ts +0 -62
  340. package/src/lib/tree-sitter/types.ts +0 -81
  341. package/src/lib/tree-sitter-styled-text.test.ts +0 -1253
  342. package/src/lib/tree-sitter-styled-text.ts +0 -306
  343. package/src/lib/validate-dir-name.ts +0 -55
  344. package/src/lib/yoga.options.test.ts +0 -628
  345. package/src/lib/yoga.options.ts +0 -346
  346. package/src/plugins/core-slot.ts +0 -579
  347. package/src/plugins/registry.ts +0 -377
  348. package/src/plugins/types.ts +0 -46
  349. package/src/post/filters.ts +0 -888
  350. package/src/renderables/ASCIIFont.ts +0 -219
  351. package/src/renderables/Box.test.ts +0 -160
  352. package/src/renderables/Box.ts +0 -295
  353. package/src/renderables/Code.test.ts +0 -2062
  354. package/src/renderables/Code.ts +0 -357
  355. package/src/renderables/Diff.regression.test.ts +0 -226
  356. package/src/renderables/Diff.test.ts +0 -3027
  357. package/src/renderables/Diff.ts +0 -1209
  358. package/src/renderables/EditBufferRenderable.ts +0 -764
  359. package/src/renderables/FrameBuffer.ts +0 -47
  360. package/src/renderables/Input.test.ts +0 -1228
  361. package/src/renderables/Input.ts +0 -245
  362. package/src/renderables/LineNumberRenderable.ts +0 -675
  363. package/src/renderables/Markdown.ts +0 -1106
  364. package/src/renderables/ScrollBar.ts +0 -422
  365. package/src/renderables/ScrollBox.ts +0 -883
  366. package/src/renderables/Select.test.ts +0 -1010
  367. package/src/renderables/Select.ts +0 -523
  368. package/src/renderables/Slider.test.ts +0 -456
  369. package/src/renderables/Slider.ts +0 -347
  370. package/src/renderables/TabSelect.test.ts +0 -197
  371. package/src/renderables/TabSelect.ts +0 -455
  372. package/src/renderables/Text.selection-buffer.test.ts +0 -123
  373. package/src/renderables/Text.test.ts +0 -2660
  374. package/src/renderables/Text.ts +0 -147
  375. package/src/renderables/TextBufferRenderable.ts +0 -518
  376. package/src/renderables/TextNode.test.ts +0 -1058
  377. package/src/renderables/TextNode.ts +0 -325
  378. package/src/renderables/TextTable.test.ts +0 -1421
  379. package/src/renderables/TextTable.ts +0 -1344
  380. package/src/renderables/Textarea.ts +0 -732
  381. package/src/renderables/TimeToFirstDraw.ts +0 -89
  382. package/src/renderables/__snapshots__/Code.test.ts.snap +0 -13
  383. package/src/renderables/__snapshots__/Diff.test.ts.snap +0 -785
  384. package/src/renderables/__snapshots__/Text.test.ts.snap +0 -421
  385. package/src/renderables/__snapshots__/TextTable.test.ts.snap +0 -215
  386. package/src/renderables/__tests__/LineNumberRenderable.scrollbox-simple.test.ts +0 -144
  387. package/src/renderables/__tests__/LineNumberRenderable.scrollbox.test.ts +0 -816
  388. package/src/renderables/__tests__/LineNumberRenderable.test.ts +0 -1787
  389. package/src/renderables/__tests__/LineNumberRenderable.wrapping.test.ts +0 -85
  390. package/src/renderables/__tests__/Markdown.test.ts +0 -2287
  391. package/src/renderables/__tests__/MultiRenderable.selection.test.ts +0 -87
  392. package/src/renderables/__tests__/Textarea.buffer.test.ts +0 -682
  393. package/src/renderables/__tests__/Textarea.destroyed-events.test.ts +0 -675
  394. package/src/renderables/__tests__/Textarea.editing.test.ts +0 -2041
  395. package/src/renderables/__tests__/Textarea.error-handling.test.ts +0 -35
  396. package/src/renderables/__tests__/Textarea.events.test.ts +0 -738
  397. package/src/renderables/__tests__/Textarea.highlights.test.ts +0 -590
  398. package/src/renderables/__tests__/Textarea.keybinding.test.ts +0 -3149
  399. package/src/renderables/__tests__/Textarea.paste.test.ts +0 -357
  400. package/src/renderables/__tests__/Textarea.rendering.test.ts +0 -1864
  401. package/src/renderables/__tests__/Textarea.scroll.test.ts +0 -733
  402. package/src/renderables/__tests__/Textarea.selection.test.ts +0 -1590
  403. package/src/renderables/__tests__/Textarea.stress.test.ts +0 -670
  404. package/src/renderables/__tests__/Textarea.undo-redo.test.ts +0 -383
  405. package/src/renderables/__tests__/Textarea.visual-lines.test.ts +0 -310
  406. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.code.test.ts.snap +0 -221
  407. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox-simple.test.ts.snap +0 -89
  408. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox.test.ts.snap +0 -457
  409. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.test.ts.snap +0 -158
  410. package/src/renderables/__tests__/__snapshots__/Textarea.rendering.test.ts.snap +0 -387
  411. package/src/renderables/__tests__/markdown-parser.test.ts +0 -217
  412. package/src/renderables/__tests__/renderable-test-utils.ts +0 -60
  413. package/src/renderables/composition/README.md +0 -8
  414. package/src/renderables/composition/VRenderable.ts +0 -32
  415. package/src/renderables/composition/constructs.ts +0 -127
  416. package/src/renderables/composition/vnode.ts +0 -289
  417. package/src/renderables/index.ts +0 -22
  418. package/src/renderables/markdown-parser.ts +0 -66
  419. package/src/renderer.ts +0 -2363
  420. package/src/runtime-plugin-support.ts +0 -39
  421. package/src/runtime-plugin.ts +0 -144
  422. package/src/syntax-style.test.ts +0 -841
  423. package/src/syntax-style.ts +0 -264
  424. package/src/testing/README.md +0 -210
  425. package/src/testing/capture-spans.test.ts +0 -194
  426. package/src/testing/integration.test.ts +0 -276
  427. package/src/testing/manual-clock.ts +0 -106
  428. package/src/testing/mock-keys.test.ts +0 -1356
  429. package/src/testing/mock-keys.ts +0 -449
  430. package/src/testing/mock-mouse.test.ts +0 -218
  431. package/src/testing/mock-mouse.ts +0 -247
  432. package/src/testing/mock-tree-sitter-client.ts +0 -73
  433. package/src/testing/spy.ts +0 -13
  434. package/src/testing/test-recorder.test.ts +0 -415
  435. package/src/testing/test-recorder.ts +0 -145
  436. package/src/testing/test-renderer.ts +0 -116
  437. package/src/testing.ts +0 -7
  438. package/src/tests/__snapshots__/absolute-positioning.snapshot.test.ts.snap +0 -481
  439. package/src/tests/__snapshots__/renderable.snapshot.test.ts.snap +0 -19
  440. package/src/tests/__snapshots__/scrollbox.test.ts.snap +0 -29
  441. package/src/tests/absolute-positioning.snapshot.test.ts +0 -638
  442. package/src/tests/allocator-stats.test.ts +0 -38
  443. package/src/tests/destroy-during-render.test.ts +0 -200
  444. package/src/tests/hover-cursor.test.ts +0 -98
  445. package/src/tests/native-span-feed-async.test.ts +0 -173
  446. package/src/tests/native-span-feed-close.test.ts +0 -120
  447. package/src/tests/native-span-feed-coverage.test.ts +0 -227
  448. package/src/tests/native-span-feed-edge-cases.test.ts +0 -352
  449. package/src/tests/native-span-feed-use-after-free.test.ts +0 -45
  450. package/src/tests/opacity.test.ts +0 -123
  451. package/src/tests/renderable.snapshot.test.ts +0 -524
  452. package/src/tests/renderable.test.ts +0 -1281
  453. package/src/tests/renderer.console-startup.test.ts +0 -65
  454. package/src/tests/renderer.control.test.ts +0 -364
  455. package/src/tests/renderer.core-slot-binding.test.ts +0 -952
  456. package/src/tests/renderer.cursor.test.ts +0 -26
  457. package/src/tests/renderer.destroy-during-render.test.ts +0 -110
  458. package/src/tests/renderer.focus-restore.test.ts +0 -228
  459. package/src/tests/renderer.focus.test.ts +0 -251
  460. package/src/tests/renderer.idle.test.ts +0 -219
  461. package/src/tests/renderer.input.test.ts +0 -2145
  462. package/src/tests/renderer.kitty-flags.test.ts +0 -195
  463. package/src/tests/renderer.mouse.test.ts +0 -1269
  464. package/src/tests/renderer.palette.test.ts +0 -629
  465. package/src/tests/renderer.selection.test.ts +0 -49
  466. package/src/tests/renderer.slot-registry.test.ts +0 -649
  467. package/src/tests/renderer.useMouse.test.ts +0 -50
  468. package/src/tests/runtime-plugin-support.fixture.ts +0 -11
  469. package/src/tests/runtime-plugin-support.test.ts +0 -28
  470. package/src/tests/runtime-plugin.fixture.ts +0 -40
  471. package/src/tests/runtime-plugin.test.ts +0 -190
  472. package/src/tests/scrollbox-culling-bug.test.ts +0 -114
  473. package/src/tests/scrollbox-hitgrid-resize.test.ts +0 -136
  474. package/src/tests/scrollbox-hitgrid.test.ts +0 -909
  475. package/src/tests/scrollbox.test.ts +0 -1530
  476. package/src/tests/wrap-resize-perf.test.ts +0 -229
  477. package/src/tests/yoga-setters.test.ts +0 -921
  478. package/src/text-buffer-view.test.ts +0 -705
  479. package/src/text-buffer-view.ts +0 -189
  480. package/src/text-buffer.test.ts +0 -347
  481. package/src/text-buffer.ts +0 -250
  482. package/src/types.ts +0 -152
  483. package/src/utils.ts +0 -88
  484. package/src/zig/ansi.zig +0 -268
  485. package/src/zig/bench/README.md +0 -50
  486. package/src/zig/bench/buffer-draw-text-buffer_bench.zig +0 -887
  487. package/src/zig/bench/edit-buffer_bench.zig +0 -476
  488. package/src/zig/bench/native-span-feed_bench.zig +0 -100
  489. package/src/zig/bench/rope-markers_bench.zig +0 -713
  490. package/src/zig/bench/rope_bench.zig +0 -514
  491. package/src/zig/bench/styled-text_bench.zig +0 -470
  492. package/src/zig/bench/text-buffer-coords_bench.zig +0 -362
  493. package/src/zig/bench/text-buffer-view_bench.zig +0 -459
  494. package/src/zig/bench/text-chunk-graphemes_bench.zig +0 -273
  495. package/src/zig/bench/utf8_bench.zig +0 -799
  496. package/src/zig/bench-utils.zig +0 -431
  497. package/src/zig/bench.zig +0 -217
  498. package/src/zig/buffer.zig +0 -2223
  499. package/src/zig/build.zig +0 -289
  500. package/src/zig/build.zig.zon +0 -16
  501. package/src/zig/edit-buffer.zig +0 -825
  502. package/src/zig/editor-view.zig +0 -802
  503. package/src/zig/event-bus.zig +0 -13
  504. package/src/zig/event-emitter.zig +0 -65
  505. package/src/zig/file-logger.zig +0 -92
  506. package/src/zig/grapheme.zig +0 -599
  507. package/src/zig/lib.zig +0 -1834
  508. package/src/zig/link.zig +0 -333
  509. package/src/zig/logger.zig +0 -43
  510. package/src/zig/mem-registry.zig +0 -125
  511. package/src/zig/native-span-feed-bench-lib.zig +0 -7
  512. package/src/zig/native-span-feed.zig +0 -708
  513. package/src/zig/renderer.zig +0 -1386
  514. package/src/zig/rope.zig +0 -1220
  515. package/src/zig/syntax-style.zig +0 -161
  516. package/src/zig/terminal.zig +0 -975
  517. package/src/zig/test.zig +0 -70
  518. package/src/zig/tests/README.md +0 -18
  519. package/src/zig/tests/buffer_test.zig +0 -2526
  520. package/src/zig/tests/edit-buffer-history_test.zig +0 -271
  521. package/src/zig/tests/edit-buffer_test.zig +0 -1689
  522. package/src/zig/tests/editor-view_test.zig +0 -3299
  523. package/src/zig/tests/event-emitter_test.zig +0 -249
  524. package/src/zig/tests/grapheme_test.zig +0 -1304
  525. package/src/zig/tests/link_test.zig +0 -190
  526. package/src/zig/tests/mem-registry_test.zig +0 -473
  527. package/src/zig/tests/memory_leak_regression_test.zig +0 -159
  528. package/src/zig/tests/native-span-feed_test.zig +0 -1264
  529. package/src/zig/tests/renderer_test.zig +0 -1010
  530. package/src/zig/tests/rope-nested_test.zig +0 -712
  531. package/src/zig/tests/rope_fuzz_test.zig +0 -238
  532. package/src/zig/tests/rope_test.zig +0 -2362
  533. package/src/zig/tests/segment-merge.test.zig +0 -148
  534. package/src/zig/tests/syntax-style_test.zig +0 -557
  535. package/src/zig/tests/terminal_test.zig +0 -719
  536. package/src/zig/tests/text-buffer-drawing_test.zig +0 -3237
  537. package/src/zig/tests/text-buffer-highlights_test.zig +0 -666
  538. package/src/zig/tests/text-buffer-iterators_test.zig +0 -776
  539. package/src/zig/tests/text-buffer-segment_test.zig +0 -320
  540. package/src/zig/tests/text-buffer-selection_test.zig +0 -1035
  541. package/src/zig/tests/text-buffer-selection_viewport_test.zig +0 -358
  542. package/src/zig/tests/text-buffer-view_test.zig +0 -3649
  543. package/src/zig/tests/text-buffer_test.zig +0 -2191
  544. package/src/zig/tests/unicode-width-map.zon +0 -3909
  545. package/src/zig/tests/utf8_no_zwj_test.zig +0 -260
  546. package/src/zig/tests/utf8_test.zig +0 -4057
  547. package/src/zig/tests/utf8_wcwidth_cursor_test.zig +0 -267
  548. package/src/zig/tests/utf8_wcwidth_test.zig +0 -357
  549. package/src/zig/tests/word-wrap-editing_test.zig +0 -498
  550. package/src/zig/tests/wrap-cache-perf_test.zig +0 -113
  551. package/src/zig/text-buffer-iterators.zig +0 -499
  552. package/src/zig/text-buffer-segment.zig +0 -404
  553. package/src/zig/text-buffer-view.zig +0 -1371
  554. package/src/zig/text-buffer.zig +0 -1180
  555. package/src/zig/utf8.zig +0 -1948
  556. package/src/zig/utils.zig +0 -9
  557. package/src/zig-structs.ts +0 -261
  558. package/src/zig.ts +0 -3843
  559. package/tsconfig.build.json +0 -22
  560. package/tsconfig.json +0 -28
  561. /package/{src/lib/tree-sitter/assets → assets}/javascript/highlights.scm +0 -0
  562. /package/{src/lib/tree-sitter/assets → assets}/javascript/tree-sitter-javascript.wasm +0 -0
  563. /package/{src/lib/tree-sitter/assets → assets}/markdown/highlights.scm +0 -0
  564. /package/{src/lib/tree-sitter/assets → assets}/markdown/injections.scm +0 -0
  565. /package/{src/lib/tree-sitter/assets → assets}/markdown/tree-sitter-markdown.wasm +0 -0
  566. /package/{src/lib/tree-sitter/assets → assets}/markdown_inline/highlights.scm +0 -0
  567. /package/{src/lib/tree-sitter/assets → assets}/markdown_inline/tree-sitter-markdown_inline.wasm +0 -0
  568. /package/{src/lib/tree-sitter/assets → assets}/typescript/highlights.scm +0 -0
  569. /package/{src/lib/tree-sitter/assets → assets}/typescript/tree-sitter-typescript.wasm +0 -0
  570. /package/{src/lib/tree-sitter/assets → assets}/zig/highlights.scm +0 -0
  571. /package/{src/lib/tree-sitter/assets → assets}/zig/tree-sitter-zig.wasm +0 -0
@@ -1,2223 +0,0 @@
1
- const std = @import("std");
2
- const Allocator = std.mem.Allocator;
3
- const ansi = @import("ansi.zig");
4
- const tb = @import("text-buffer.zig");
5
- const tbv = @import("text-buffer-view.zig");
6
- const edv = @import("editor-view.zig");
7
- const ss = @import("syntax-style.zig");
8
- const math = std.math;
9
- const assert = std.debug.assert;
10
-
11
- const gp = @import("grapheme.zig");
12
- const link = @import("link.zig");
13
-
14
- const logger = @import("logger.zig");
15
- const utf8 = @import("utf8.zig");
16
- const uucode = @import("uucode");
17
-
18
- pub const RGBA = ansi.RGBA;
19
- pub const Vec3f = @Vector(3, f32);
20
- pub const Vec4f = @Vector(4, f32);
21
-
22
- const TextBuffer = tb.TextBuffer;
23
- const TextBufferView = tbv.TextBufferView;
24
- const EditorView = edv.EditorView;
25
-
26
- const INV_255: f32 = 1.0 / 255.0;
27
- pub const DEFAULT_SPACE_CHAR: u32 = 32;
28
- const MAX_UNICODE_CODEPOINT: u32 = 0x10FFFF;
29
- const BLOCK_CHAR: u32 = 0x2588; // Full block █
30
- const QUADRANT_CHARS_COUNT = 16;
31
-
32
- const GRAYSCALE_CHARS = " .'^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$";
33
-
34
- pub const BorderSides = packed struct {
35
- top: bool = false,
36
- right: bool = false,
37
- bottom: bool = false,
38
- left: bool = false,
39
- };
40
-
41
- pub const BorderCharIndex = enum(u8) {
42
- topLeft = 0,
43
- topRight = 1,
44
- bottomLeft = 2,
45
- bottomRight = 3,
46
- horizontal = 4,
47
- vertical = 5,
48
- topT = 6,
49
- bottomT = 7,
50
- leftT = 8,
51
- rightT = 9,
52
- cross = 10,
53
- };
54
-
55
- pub const TextSelection = struct {
56
- start: u32,
57
- end: u32,
58
- bgColor: ?RGBA,
59
- fgColor: ?RGBA,
60
- };
61
-
62
- pub const ClipRect = struct {
63
- x: i32,
64
- y: i32,
65
- width: u32,
66
- height: u32,
67
- };
68
-
69
- pub const BufferError = error{
70
- OutOfMemory,
71
- InvalidDimensions,
72
- InvalidUnicode,
73
- BufferTooSmall,
74
- };
75
-
76
- pub fn rgbaToVec4f(color: RGBA) Vec4f {
77
- return Vec4f{ color[0], color[1], color[2], color[3] };
78
- }
79
-
80
- pub fn rgbaEqual(a: RGBA, b: RGBA, epsilon: f32) bool {
81
- const va = rgbaToVec4f(a);
82
- const vb = rgbaToVec4f(b);
83
- const diff = @abs(va - vb);
84
- const eps = @as(Vec4f, @splat(epsilon));
85
- return @reduce(.And, diff < eps);
86
- }
87
-
88
- pub const Cell = struct {
89
- char: u32,
90
- fg: RGBA,
91
- bg: RGBA,
92
- attributes: u32,
93
- };
94
-
95
- fn isRGBAWithAlpha(color: RGBA) bool {
96
- return color[3] < 1.0;
97
- }
98
-
99
- inline fn isFullyOpaque(opacity: f32, fg: RGBA, bg: RGBA) bool {
100
- return opacity == 1.0 and !isRGBAWithAlpha(fg) and !isRGBAWithAlpha(bg);
101
- }
102
-
103
- fn blendColors(overlay: RGBA, text: RGBA) RGBA {
104
- if (overlay[3] == 1.0) {
105
- return overlay;
106
- }
107
-
108
- if (text[3] == 0.0) {
109
- const alpha = overlay[3];
110
- const r = overlay[0] * alpha;
111
- const g = overlay[1] * alpha;
112
- const b = overlay[2] * alpha;
113
- if (r < 0.01 and g < 0.01 and b < 0.01) {
114
- return .{ 0.0, 0.0, 0.0, 0.0 };
115
- }
116
- return .{ r, g, b, alpha };
117
- }
118
-
119
- const alpha = overlay[3];
120
- var perceptualAlpha: f32 = undefined;
121
-
122
- // For high alpha values (>0.8), use a more aggressive curve
123
- if (alpha > 0.8) {
124
- const normalizedHighAlpha = (alpha - 0.8) * 5.0;
125
- const curvedHighAlpha = std.math.pow(f32, normalizedHighAlpha, 0.2);
126
- perceptualAlpha = 0.8 + (curvedHighAlpha * 0.2);
127
- } else {
128
- perceptualAlpha = std.math.pow(f32, alpha, 0.9);
129
- }
130
-
131
- const overlayVec = Vec3f{ overlay[0], overlay[1], overlay[2] };
132
- const textVec = Vec3f{ text[0], text[1], text[2] };
133
- const alphaSplat = @as(Vec3f, @splat(perceptualAlpha));
134
- const oneMinusAlpha = @as(Vec3f, @splat(1.0 - perceptualAlpha));
135
- const blended = overlayVec * alphaSplat + textVec * oneMinusAlpha;
136
-
137
- const resultAlpha = alpha + text[3] * (1.0 - alpha);
138
-
139
- return .{ blended[0], blended[1], blended[2], resultAlpha };
140
- }
141
-
142
- /// Optimized buffer for terminal rendering
143
- pub const OptimizedBuffer = struct {
144
- buffer: struct {
145
- char: []u32,
146
- fg: []RGBA,
147
- bg: []RGBA,
148
- attributes: []u32,
149
- },
150
- width: u32,
151
- height: u32,
152
- respectAlpha: bool,
153
- allocator: Allocator,
154
- pool: *gp.GraphemePool,
155
- link_pool: *link.LinkPool,
156
-
157
- grapheme_tracker: gp.GraphemeTracker,
158
- link_tracker: link.LinkTracker,
159
- width_method: utf8.WidthMethod,
160
- id: []const u8,
161
- scissor_stack: std.ArrayListUnmanaged(ClipRect),
162
- opacity_stack: std.ArrayListUnmanaged(f32),
163
-
164
- const InitOptions = struct {
165
- respectAlpha: bool = false,
166
- pool: *gp.GraphemePool,
167
- width_method: utf8.WidthMethod = .unicode,
168
- id: []const u8 = "unnamed buffer",
169
- link_pool: ?*link.LinkPool = null,
170
- };
171
-
172
- pub fn init(allocator: Allocator, width: u32, height: u32, options: InitOptions) BufferError!*OptimizedBuffer {
173
- if (width == 0 or height == 0) {
174
- logger.warn("OptimizedBuffer.init: Invalid dimensions {}x{}", .{ width, height });
175
- return BufferError.InvalidDimensions;
176
- }
177
-
178
- const self = allocator.create(OptimizedBuffer) catch return BufferError.OutOfMemory;
179
- errdefer allocator.destroy(self);
180
-
181
- const size = width * height;
182
-
183
- const owned_id = allocator.dupe(u8, options.id) catch return BufferError.OutOfMemory;
184
- errdefer allocator.free(owned_id);
185
-
186
- var scissor_stack: std.ArrayListUnmanaged(ClipRect) = .{};
187
- errdefer scissor_stack.deinit(allocator);
188
-
189
- var opacity_stack: std.ArrayListUnmanaged(f32) = .{};
190
- errdefer opacity_stack.deinit(allocator);
191
-
192
- const lp = options.link_pool orelse link.initGlobalLinkPool(allocator);
193
- const char_buffer = allocator.alloc(u32, size) catch return BufferError.OutOfMemory;
194
- errdefer allocator.free(char_buffer);
195
-
196
- const fg_buffer = allocator.alloc(RGBA, size) catch return BufferError.OutOfMemory;
197
- errdefer allocator.free(fg_buffer);
198
-
199
- const bg_buffer = allocator.alloc(RGBA, size) catch return BufferError.OutOfMemory;
200
- errdefer allocator.free(bg_buffer);
201
-
202
- const attributes_buffer = allocator.alloc(u32, size) catch return BufferError.OutOfMemory;
203
- errdefer allocator.free(attributes_buffer);
204
-
205
- self.* = .{
206
- .buffer = .{
207
- .char = char_buffer,
208
- .fg = fg_buffer,
209
- .bg = bg_buffer,
210
- .attributes = attributes_buffer,
211
- },
212
- .width = width,
213
- .height = height,
214
- .respectAlpha = options.respectAlpha,
215
- .allocator = allocator,
216
- .pool = options.pool,
217
- .link_pool = lp,
218
- .grapheme_tracker = gp.GraphemeTracker.init(allocator, options.pool),
219
- .link_tracker = link.LinkTracker.init(allocator, lp),
220
- .width_method = options.width_method,
221
- .id = owned_id,
222
- .scissor_stack = scissor_stack,
223
- .opacity_stack = opacity_stack,
224
- };
225
-
226
- @memset(self.buffer.char, 0);
227
- @memset(self.buffer.fg, .{ 0.0, 0.0, 0.0, 0.0 });
228
- @memset(self.buffer.bg, .{ 0.0, 0.0, 0.0, 0.0 });
229
- @memset(self.buffer.attributes, 0);
230
-
231
- return self;
232
- }
233
-
234
- pub fn getCharPtr(self: *OptimizedBuffer) [*]u32 {
235
- return self.buffer.char.ptr;
236
- }
237
-
238
- pub fn getFgPtr(self: *OptimizedBuffer) [*]RGBA {
239
- return self.buffer.fg.ptr;
240
- }
241
-
242
- pub fn getBgPtr(self: *OptimizedBuffer) [*]RGBA {
243
- return self.buffer.bg.ptr;
244
- }
245
-
246
- pub fn getAttributesPtr(self: *OptimizedBuffer) [*]u32 {
247
- return self.buffer.attributes.ptr;
248
- }
249
-
250
- pub fn deinit(self: *OptimizedBuffer) void {
251
- self.opacity_stack.deinit(self.allocator);
252
- self.scissor_stack.deinit(self.allocator);
253
- self.link_tracker.deinit();
254
- self.grapheme_tracker.deinit();
255
- self.allocator.free(self.buffer.char);
256
- self.allocator.free(self.buffer.fg);
257
- self.allocator.free(self.buffer.bg);
258
- self.allocator.free(self.buffer.attributes);
259
- self.allocator.free(self.id);
260
- self.allocator.destroy(self);
261
- }
262
-
263
- pub fn getCurrentScissorRect(self: *const OptimizedBuffer) ?ClipRect {
264
- if (self.scissor_stack.items.len == 0) return null;
265
- return self.scissor_stack.items[self.scissor_stack.items.len - 1];
266
- }
267
-
268
- pub fn isPointInScissor(self: *const OptimizedBuffer, x: i32, y: i32) bool {
269
- const scissor = self.getCurrentScissorRect() orelse return true;
270
- return x >= scissor.x and x < scissor.x + @as(i32, @intCast(scissor.width)) and
271
- y >= scissor.y and y < scissor.y + @as(i32, @intCast(scissor.height));
272
- }
273
-
274
- pub fn isRectInScissor(self: *const OptimizedBuffer, x: i32, y: i32, width: u32, height: u32) bool {
275
- const scissor = self.getCurrentScissorRect() orelse return true;
276
-
277
- const rect_end_x = x + @as(i32, @intCast(width));
278
- const rect_end_y = y + @as(i32, @intCast(height));
279
- const scissor_end_x = scissor.x + @as(i32, @intCast(scissor.width));
280
- const scissor_end_y = scissor.y + @as(i32, @intCast(scissor.height));
281
-
282
- return !(x >= scissor_end_x or rect_end_x <= scissor.x or
283
- y >= scissor_end_y or rect_end_y <= scissor.y);
284
- }
285
-
286
- pub fn clipRectToScissor(self: *const OptimizedBuffer, x: i32, y: i32, width: u32, height: u32) ?ClipRect {
287
- const scissor = self.getCurrentScissorRect() orelse return ClipRect{
288
- .x = x,
289
- .y = y,
290
- .width = width,
291
- .height = height,
292
- };
293
-
294
- const rect_end_x = x + @as(i32, @intCast(width));
295
- const rect_end_y = y + @as(i32, @intCast(height));
296
- const scissor_end_x = scissor.x + @as(i32, @intCast(scissor.width));
297
- const scissor_end_y = scissor.y + @as(i32, @intCast(scissor.height));
298
-
299
- const intersect_x = @max(x, scissor.x);
300
- const intersect_y = @max(y, scissor.y);
301
- const intersect_end_x = @min(rect_end_x, scissor_end_x);
302
- const intersect_end_y = @min(rect_end_y, scissor_end_y);
303
-
304
- if (intersect_x >= intersect_end_x or intersect_y >= intersect_end_y) {
305
- return null; // No intersection
306
- }
307
-
308
- return ClipRect{
309
- .x = intersect_x,
310
- .y = intersect_y,
311
- .width = @intCast(intersect_end_x - intersect_x),
312
- .height = @intCast(intersect_end_y - intersect_y),
313
- };
314
- }
315
-
316
- pub fn pushScissorRect(self: *OptimizedBuffer, x: i32, y: i32, width: u32, height: u32) !void {
317
- var rect = ClipRect{
318
- .x = x,
319
- .y = y,
320
- .width = width,
321
- .height = height,
322
- };
323
-
324
- // Intersect with current scissor (if any) so nested scissor rects always clip to parents.
325
- if (self.getCurrentScissorRect() != null) {
326
- const intersect = self.clipRectToScissor(rect.x, rect.y, rect.width, rect.height);
327
- if (intersect) |clipped| {
328
- rect = clipped;
329
- } else {
330
- // Completely outside current scissor; push a degenerate rect so nothing renders.
331
- rect = ClipRect{ .x = 0, .y = 0, .width = 0, .height = 0 };
332
- }
333
- }
334
-
335
- try self.scissor_stack.append(self.allocator, rect);
336
- }
337
-
338
- pub fn popScissorRect(self: *OptimizedBuffer) void {
339
- if (self.scissor_stack.items.len > 0) {
340
- _ = self.scissor_stack.pop();
341
- }
342
- }
343
-
344
- pub fn clearScissorRects(self: *OptimizedBuffer) void {
345
- self.scissor_stack.clearRetainingCapacity();
346
- }
347
-
348
- /// Get the current effective opacity (product of all stacked opacities)
349
- pub fn getCurrentOpacity(self: *const OptimizedBuffer) f32 {
350
- if (self.opacity_stack.items.len == 0) return 1.0;
351
- return self.opacity_stack.items[self.opacity_stack.items.len - 1];
352
- }
353
-
354
- /// Push an opacity value onto the stack. The effective opacity is multiplied with the current.
355
- pub fn pushOpacity(self: *OptimizedBuffer, opacity: f32) !void {
356
- const current = self.getCurrentOpacity();
357
- const effective = current * std.math.clamp(opacity, 0.0, 1.0);
358
- try self.opacity_stack.append(self.allocator, effective);
359
- }
360
-
361
- /// Pop an opacity value from the stack
362
- pub fn popOpacity(self: *OptimizedBuffer) void {
363
- if (self.opacity_stack.items.len > 0) {
364
- _ = self.opacity_stack.pop();
365
- }
366
- }
367
-
368
- /// Clear all opacity values from the stack
369
- pub fn clearOpacity(self: *OptimizedBuffer) void {
370
- self.opacity_stack.clearRetainingCapacity();
371
- }
372
-
373
- pub fn resize(self: *OptimizedBuffer, width: u32, height: u32) BufferError!void {
374
- if (self.width == width and self.height == height) return;
375
- if (width == 0 or height == 0) return BufferError.InvalidDimensions;
376
-
377
- const size = width * height;
378
-
379
- self.buffer.char = self.allocator.realloc(self.buffer.char, size) catch return BufferError.OutOfMemory;
380
- self.buffer.fg = self.allocator.realloc(self.buffer.fg, size) catch return BufferError.OutOfMemory;
381
- self.buffer.bg = self.allocator.realloc(self.buffer.bg, size) catch return BufferError.OutOfMemory;
382
- self.buffer.attributes = self.allocator.realloc(self.buffer.attributes, size) catch return BufferError.OutOfMemory;
383
-
384
- self.width = width;
385
- self.height = height;
386
-
387
- // Always clear after resize to initialize cells (realloc doesn't zero memory)
388
- // This handles both growing (new cells are garbage) and shrinking (grapheme cleanup)
389
- try self.clear(.{ 0.0, 0.0, 0.0, 1.0 }, null);
390
- }
391
-
392
- fn coordsToIndex(self: *const OptimizedBuffer, x: u32, y: u32) u32 {
393
- return y * self.width + x;
394
- }
395
-
396
- fn indexToCoords(self: *const OptimizedBuffer, index: u32) struct { x: u32, y: u32 } {
397
- return .{
398
- .x = index % self.width,
399
- .y = index / self.width,
400
- };
401
- }
402
-
403
- pub fn clear(self: *OptimizedBuffer, bg: RGBA, char: ?u32) !void {
404
- const cellChar = char orelse DEFAULT_SPACE_CHAR;
405
- self.link_tracker.clear();
406
- self.grapheme_tracker.clear();
407
- @memset(self.buffer.char, @intCast(cellChar));
408
- @memset(self.buffer.attributes, 0);
409
- @memset(self.buffer.fg, .{ 1.0, 1.0, 1.0, 1.0 });
410
- @memset(self.buffer.bg, bg);
411
- }
412
-
413
- /// Write a single cell and update link tracker. No grapheme tracking,
414
- /// span cleanup, or continuation propagation.
415
- pub fn setRaw(self: *OptimizedBuffer, x: u32, y: u32, cell: Cell) void {
416
- const index = self.validateAndIndex(x, y) orelse return;
417
- self.writeCellAndLinks(index, cell);
418
- }
419
-
420
- /// Like set(), but without span cleanup. Writes the cell, its continuation
421
- /// cells (for width-2+ graphemes), and updates grapheme/link trackers.
422
- ///
423
- /// Intended for the renderer's diff loop where cells are synced from an
424
- /// authoritative source buffer. Span cleanup is skipped because it can
425
- /// destroy continuation cells that were correctly written by an earlier
426
- /// iteration of the same left-to-right pass (issue #723).
427
- pub fn syncCell(self: *OptimizedBuffer, x: u32, y: u32, cell: Cell) void {
428
- self.setInternal(x, y, cell, false);
429
- }
430
-
431
- pub fn set(self: *OptimizedBuffer, x: u32, y: u32, cell: Cell) void {
432
- self.setInternal(x, y, cell, true);
433
- }
434
-
435
- fn setInternal(self: *OptimizedBuffer, x: u32, y: u32, cell: Cell, comptime span_cleanup: bool) void {
436
- const index = self.validateAndIndex(x, y) orelse return;
437
- const prev_char = self.buffer.char[index];
438
- const prev_link_id = ansi.TextAttributes.getLinkId(self.buffer.attributes[index]);
439
- var tracker_replaced = false;
440
-
441
- if (!span_cleanup) {
442
- const old_start_id: ?u32 = if (gp.isGraphemeChar(prev_char)) gp.graphemeIdFromChar(prev_char) else null;
443
- const new_start_id: ?u32 = blk: {
444
- if (!gp.isGraphemeChar(cell.char)) break :blk null;
445
- const new_width = gp.charRightExtent(cell.char) + 1;
446
- if (x + new_width > self.width) break :blk null;
447
- break :blk gp.graphemeIdFromChar(cell.char);
448
- };
449
-
450
- if (old_start_id != null or new_start_id != null) {
451
- self.grapheme_tracker.replace(old_start_id, new_start_id);
452
- tracker_replaced = true;
453
- }
454
- }
455
-
456
- // If overwriting a grapheme span (start or continuation) with a different char, clear that span first
457
- if (span_cleanup) {
458
- if ((gp.isGraphemeChar(prev_char) or gp.isContinuationChar(prev_char)) and prev_char != cell.char) {
459
- const row_start: u32 = y * self.width;
460
- const row_end: u32 = row_start + self.width - 1;
461
- const left = gp.charLeftExtent(prev_char);
462
- const right = gp.charRightExtent(prev_char);
463
- const id = gp.graphemeIdFromChar(prev_char);
464
-
465
- const new_grapheme_id: ?u32 = blk: {
466
- if (!gp.isGraphemeChar(cell.char)) break :blk null;
467
- const new_width = gp.charRightExtent(cell.char) + 1;
468
- if (x + new_width > self.width) break :blk null;
469
- break :blk gp.graphemeIdFromChar(cell.char);
470
- };
471
- self.grapheme_tracker.replace(id, new_grapheme_id);
472
- tracker_replaced = true;
473
-
474
- const span_start = index - @min(left, index - row_start);
475
- const span_end = index + @min(right, row_end - index);
476
-
477
- var span_i: u32 = span_start;
478
- while (span_i <= span_end) : (span_i += 1) {
479
- const span_char = self.buffer.char[span_i];
480
- if (!(gp.isGraphemeChar(span_char) or gp.isContinuationChar(span_char))) continue;
481
- if (gp.graphemeIdFromChar(span_char) != id) continue;
482
-
483
- const span_link_id = ansi.TextAttributes.getLinkId(self.buffer.attributes[span_i]);
484
- if (span_link_id != 0) {
485
- self.link_tracker.removeCellRef(span_link_id);
486
- }
487
-
488
- self.buffer.char[span_i] = @intCast(DEFAULT_SPACE_CHAR);
489
- self.buffer.attributes[span_i] = 0;
490
- }
491
- }
492
- }
493
-
494
- if (gp.isGraphemeChar(cell.char)) {
495
- const right = gp.charRightExtent(cell.char);
496
- const width: u32 = 1 + right;
497
-
498
- if (x + width > self.width) {
499
- const end_of_line = (y + 1) * self.width;
500
- var eol_i = index;
501
- while (eol_i < end_of_line) : (eol_i += 1) {
502
- const eol_link_id = ansi.TextAttributes.getLinkId(self.buffer.attributes[eol_i]);
503
- if (eol_link_id != 0) {
504
- self.link_tracker.removeCellRef(eol_link_id);
505
- }
506
- }
507
- @memset(self.buffer.char[index..end_of_line], @intCast(DEFAULT_SPACE_CHAR));
508
- @memset(self.buffer.attributes[index..end_of_line], cell.attributes);
509
- @memset(self.buffer.fg[index..end_of_line], cell.fg);
510
- @memset(self.buffer.bg[index..end_of_line], cell.bg);
511
- const new_link_id = ansi.TextAttributes.getLinkId(cell.attributes);
512
- if (new_link_id != 0) {
513
- const cells_written = end_of_line - index;
514
- var link_i: u32 = 0;
515
- while (link_i < cells_written) : (link_i += 1) {
516
- self.link_tracker.addCellRef(new_link_id);
517
- }
518
- }
519
- return;
520
- }
521
-
522
- self.buffer.char[index] = cell.char;
523
- self.buffer.fg[index] = cell.fg;
524
- self.buffer.bg[index] = cell.bg;
525
- self.buffer.attributes[index] = cell.attributes;
526
-
527
- const id: u32 = gp.graphemeIdFromChar(cell.char);
528
- const is_same_grapheme_start = gp.isGraphemeChar(prev_char) and prev_char == cell.char;
529
- if (!tracker_replaced and !is_same_grapheme_start) {
530
- self.grapheme_tracker.add(id);
531
- }
532
-
533
- const new_link_id = ansi.TextAttributes.getLinkId(cell.attributes);
534
- if (prev_link_id != 0 and prev_link_id != new_link_id) {
535
- self.link_tracker.removeCellRef(prev_link_id);
536
- }
537
- if (new_link_id != 0 and new_link_id != prev_link_id) {
538
- self.link_tracker.addCellRef(new_link_id);
539
- }
540
-
541
- if (width > 1) {
542
- const row_end_index: u32 = (y * self.width) + self.width - 1;
543
- const max_right = @min(right, row_end_index - index);
544
- if (max_right > 0) {
545
- var cont_i: u32 = 1;
546
- while (cont_i <= max_right) : (cont_i += 1) {
547
- const cont_link_id = ansi.TextAttributes.getLinkId(self.buffer.attributes[index + cont_i]);
548
- if (cont_link_id != 0) {
549
- self.link_tracker.removeCellRef(cont_link_id);
550
- }
551
- }
552
-
553
- @memset(self.buffer.fg[index + 1 .. index + 1 + max_right], cell.fg);
554
- @memset(self.buffer.bg[index + 1 .. index + 1 + max_right], cell.bg);
555
- @memset(self.buffer.attributes[index + 1 .. index + 1 + max_right], cell.attributes);
556
- var k: u32 = 1;
557
- while (k <= max_right) : (k += 1) {
558
- const cont = gp.packContinuation(k, max_right - k, id);
559
- self.buffer.char[index + k] = cont;
560
- if (new_link_id != 0) {
561
- self.link_tracker.addCellRef(new_link_id);
562
- }
563
- }
564
- }
565
- }
566
- } else {
567
- self.writeCellAndLinks(index, cell);
568
- }
569
- }
570
-
571
- /// Validate coordinates and return buffer index, or null if out of bounds / scissor.
572
- fn validateAndIndex(self: *OptimizedBuffer, x: u32, y: u32) ?u32 {
573
- if (x >= self.width or y >= self.height) return null;
574
- if (!self.isPointInScissor(@intCast(x), @intCast(y))) return null;
575
- return self.coordsToIndex(x, y);
576
- }
577
-
578
- /// Write cell data at index and update link tracker.
579
- fn writeCellAndLinks(self: *OptimizedBuffer, index: u32, cell: Cell) void {
580
- const prev_link_id = ansi.TextAttributes.getLinkId(self.buffer.attributes[index]);
581
- const new_link_id = ansi.TextAttributes.getLinkId(cell.attributes);
582
-
583
- self.buffer.char[index] = cell.char;
584
- self.buffer.fg[index] = cell.fg;
585
- self.buffer.bg[index] = cell.bg;
586
- self.buffer.attributes[index] = cell.attributes;
587
-
588
- if (prev_link_id != 0 and prev_link_id != new_link_id) {
589
- self.link_tracker.removeCellRef(prev_link_id);
590
- }
591
- if (new_link_id != 0 and new_link_id != prev_link_id) {
592
- self.link_tracker.addCellRef(new_link_id);
593
- }
594
- }
595
-
596
- pub fn get(self: *const OptimizedBuffer, x: u32, y: u32) ?Cell {
597
- if (x >= self.width or y >= self.height) return null;
598
-
599
- const index = self.coordsToIndex(x, y);
600
- return Cell{
601
- .char = self.buffer.char[index],
602
- .fg = self.buffer.fg[index],
603
- .bg = self.buffer.bg[index],
604
- .attributes = self.buffer.attributes[index],
605
- };
606
- }
607
-
608
- pub fn getWidth(self: *const OptimizedBuffer) u32 {
609
- return self.width;
610
- }
611
-
612
- pub fn getHeight(self: *const OptimizedBuffer) u32 {
613
- return self.height;
614
- }
615
-
616
- pub fn setRespectAlpha(self: *OptimizedBuffer, respectAlpha: bool) void {
617
- self.respectAlpha = respectAlpha;
618
- }
619
-
620
- pub fn getRespectAlpha(self: *const OptimizedBuffer) bool {
621
- return self.respectAlpha;
622
- }
623
-
624
- pub fn getId(self: *const OptimizedBuffer) []const u8 {
625
- return self.id;
626
- }
627
-
628
- /// Calculate the real byte size of the character buffer including grapheme pool data
629
- pub fn getRealCharSize(self: *const OptimizedBuffer) u32 {
630
- const total_chars = self.width * self.height;
631
- const grapheme_count = self.grapheme_tracker.getGraphemeCellCount();
632
- const total_grapheme_bytes = self.grapheme_tracker.getTotalGraphemeBytes();
633
-
634
- const regular_char_bytes = (total_chars - grapheme_count) * @sizeOf(u32);
635
- return regular_char_bytes + total_grapheme_bytes;
636
- }
637
-
638
- /// Write all resolved character bytes to the given output buffer
639
- /// Returns the number of bytes written, or 0 if the output buffer is too small
640
- pub fn writeResolvedChars(self: *const OptimizedBuffer, output_buffer: []u8, addLineBreaks: bool) BufferError!u32 {
641
- var bytes_written: u32 = 0;
642
- const total_cells = self.width * self.height;
643
-
644
- var i: u32 = 0;
645
- while (i < total_cells) : (i += 1) {
646
- const char_code = self.buffer.char[i];
647
-
648
- if (gp.isGraphemeChar(char_code)) {
649
- const gid = gp.graphemeIdFromChar(char_code);
650
- if (self.pool.get(gid)) |grapheme_bytes| {
651
- if (bytes_written + grapheme_bytes.len > output_buffer.len) {
652
- return BufferError.BufferTooSmall;
653
- }
654
- @memcpy(output_buffer[bytes_written .. bytes_written + grapheme_bytes.len], grapheme_bytes);
655
- bytes_written += @intCast(grapheme_bytes.len);
656
- } else |_| {
657
- if (bytes_written + 1 > output_buffer.len) {
658
- return BufferError.BufferTooSmall;
659
- }
660
- output_buffer[bytes_written] = ' ';
661
- bytes_written += 1;
662
- }
663
- } else if (gp.isContinuationChar(char_code)) {
664
- continue;
665
- } else {
666
- const codepoint = char_code;
667
-
668
- if (codepoint > 0x10FFFF) {
669
- if (bytes_written + 1 > output_buffer.len) {
670
- return BufferError.BufferTooSmall;
671
- }
672
- output_buffer[bytes_written] = ' ';
673
- bytes_written += 1;
674
- continue;
675
- }
676
-
677
- var utf8_bytes: [4]u8 = undefined;
678
- const utf8_len = std.unicode.utf8Encode(@intCast(codepoint), &utf8_bytes) catch {
679
- if (bytes_written + 1 > output_buffer.len) {
680
- return BufferError.BufferTooSmall;
681
- }
682
- output_buffer[bytes_written] = ' ';
683
- bytes_written += 1;
684
- continue;
685
- };
686
-
687
- if (bytes_written + utf8_len > output_buffer.len) {
688
- return BufferError.BufferTooSmall;
689
- }
690
- @memcpy(output_buffer[bytes_written .. bytes_written + utf8_len], utf8_bytes[0..utf8_len]);
691
- bytes_written += @intCast(utf8_len);
692
- }
693
-
694
- if (addLineBreaks and (i + 1) % self.width == 0) {
695
- if (bytes_written + 1 > output_buffer.len) {
696
- return BufferError.BufferTooSmall;
697
- }
698
- output_buffer[bytes_written] = '\n';
699
- bytes_written += 1;
700
- }
701
- }
702
-
703
- return bytes_written;
704
- }
705
-
706
- pub fn blendCells(overlayCell: Cell, destCell: Cell) Cell {
707
- const hasBgAlpha = isRGBAWithAlpha(overlayCell.bg);
708
- const hasFgAlpha = isRGBAWithAlpha(overlayCell.fg);
709
-
710
- if (hasBgAlpha or hasFgAlpha) {
711
- const blendedBgRgb = if (hasBgAlpha) blendColors(overlayCell.bg, destCell.bg) else overlayCell.bg;
712
- const charIsDefaultSpace = overlayCell.char == DEFAULT_SPACE_CHAR;
713
- const destNotZero = destCell.char != 0;
714
- const destNotDefaultSpace = destCell.char != DEFAULT_SPACE_CHAR;
715
- const destWidthIsOne = gp.encodedCharWidth(destCell.char) == 1;
716
-
717
- const preserveChar = (charIsDefaultSpace and
718
- destNotZero and
719
- destNotDefaultSpace and
720
- destWidthIsOne);
721
- const finalChar = if (preserveChar) destCell.char else overlayCell.char;
722
-
723
- var finalFg: RGBA = undefined;
724
- if (preserveChar) {
725
- finalFg = blendColors(overlayCell.bg, destCell.fg);
726
- } else {
727
- finalFg = if (hasFgAlpha) blendColors(overlayCell.fg, destCell.bg) else overlayCell.fg;
728
- }
729
-
730
- // When preserving char, preserve its base attributes but NOT its link
731
- // Links ALWAYS come from overlay, never from destination
732
- // Even if overlay has no link (link_id=0), it clears the destination's link
733
- const baseAttrs = if (preserveChar)
734
- ansi.TextAttributes.getBaseAttributes(destCell.attributes)
735
- else
736
- ansi.TextAttributes.getBaseAttributes(overlayCell.attributes);
737
- // Overlay link always wins - whether it's a real link or 0 (no link)
738
- const overlayLinkId = ansi.TextAttributes.getLinkId(overlayCell.attributes);
739
- const finalAttributes = ansi.TextAttributes.setLinkId(@as(u32, baseAttrs), overlayLinkId);
740
-
741
- // When overlay background is fully transparent, preserve destination background alpha
742
- const finalBgAlpha = if (overlayCell.bg[3] == 0.0) destCell.bg[3] else overlayCell.bg[3];
743
-
744
- return Cell{
745
- .char = finalChar,
746
- .fg = finalFg,
747
- .bg = .{ blendedBgRgb[0], blendedBgRgb[1], blendedBgRgb[2], finalBgAlpha },
748
- .attributes = finalAttributes,
749
- };
750
- }
751
-
752
- return overlayCell;
753
- }
754
-
755
- pub fn setCellWithAlphaBlending(
756
- self: *OptimizedBuffer,
757
- x: u32,
758
- y: u32,
759
- char: u32,
760
- fg: RGBA,
761
- bg: RGBA,
762
- attributes: u32,
763
- ) !void {
764
- if (!self.isPointInScissor(@intCast(x), @intCast(y))) return;
765
-
766
- // Apply current opacity from the stack
767
- const opacity = self.getCurrentOpacity();
768
- if (isFullyOpaque(opacity, fg, bg)) {
769
- self.set(x, y, Cell{ .char = char, .fg = fg, .bg = bg, .attributes = attributes });
770
- return;
771
- }
772
-
773
- const effectiveFg = RGBA{ fg[0], fg[1], fg[2], fg[3] * opacity };
774
- const effectiveBg = RGBA{ bg[0], bg[1], bg[2], bg[3] * opacity };
775
-
776
- const overlayCell = Cell{ .char = char, .fg = effectiveFg, .bg = effectiveBg, .attributes = attributes };
777
-
778
- if (self.get(x, y)) |destCell| {
779
- const blendedCell = blendCells(overlayCell, destCell);
780
- self.set(x, y, blendedCell);
781
- } else {
782
- self.set(x, y, overlayCell);
783
- }
784
- }
785
-
786
- pub fn setCellWithAlphaBlendingRaw(
787
- self: *OptimizedBuffer,
788
- x: u32,
789
- y: u32,
790
- char: u32,
791
- fg: RGBA,
792
- bg: RGBA,
793
- attributes: u32,
794
- ) !void {
795
- if (!self.isPointInScissor(@intCast(x), @intCast(y))) return;
796
-
797
- // Apply current opacity from the stack
798
- const opacity = self.getCurrentOpacity();
799
- if (isFullyOpaque(opacity, fg, bg)) {
800
- const overlayCell = Cell{ .char = char, .fg = fg, .bg = bg, .attributes = attributes };
801
- assert(!gp.isGraphemeChar(char));
802
- assert(!gp.isContinuationChar(char));
803
- self.setRaw(x, y, overlayCell);
804
- return;
805
- }
806
-
807
- const effectiveFg = RGBA{ fg[0], fg[1], fg[2], fg[3] * opacity };
808
- const effectiveBg = RGBA{ bg[0], bg[1], bg[2], bg[3] * opacity };
809
-
810
- const overlayCell = Cell{ .char = char, .fg = effectiveFg, .bg = effectiveBg, .attributes = attributes };
811
-
812
- if (self.get(x, y)) |destCell| {
813
- const blendedCell = blendCells(overlayCell, destCell);
814
- assert(!gp.isGraphemeChar(blendedCell.char));
815
- assert(!gp.isContinuationChar(blendedCell.char));
816
- self.setRaw(x, y, blendedCell);
817
- } else {
818
- assert(!gp.isGraphemeChar(overlayCell.char));
819
- assert(!gp.isContinuationChar(overlayCell.char));
820
- self.setRaw(x, y, overlayCell);
821
- }
822
- }
823
-
824
- pub fn drawChar(
825
- self: *OptimizedBuffer,
826
- char: u32,
827
- x: u32,
828
- y: u32,
829
- fg: RGBA,
830
- bg: RGBA,
831
- attributes: u32,
832
- ) !void {
833
- if (!self.isPointInScissor(@intCast(x), @intCast(y))) return;
834
-
835
- if (isRGBAWithAlpha(bg) or isRGBAWithAlpha(fg)) {
836
- try self.setCellWithAlphaBlending(x, y, char, fg, bg, attributes);
837
- } else {
838
- self.set(x, y, Cell{
839
- .char = char,
840
- .fg = fg,
841
- .bg = bg,
842
- .attributes = attributes,
843
- });
844
- }
845
- }
846
-
847
- pub fn fillRect(
848
- self: *OptimizedBuffer,
849
- x: u32,
850
- y: u32,
851
- width: u32,
852
- height: u32,
853
- bg: RGBA,
854
- ) !void {
855
- if (self.width == 0 or self.height == 0 or width == 0 or height == 0) return;
856
- if (x >= self.width or y >= self.height) return;
857
-
858
- if (!self.isRectInScissor(@intCast(x), @intCast(y), width, height)) return;
859
-
860
- const startX = x;
861
- const startY = y;
862
- const maxEndX = if (x < self.width) self.width - 1 else 0;
863
- const maxEndY = if (y < self.height) self.height - 1 else 0;
864
- const requestedEndX = x + width - 1;
865
- const requestedEndY = y + height - 1;
866
- const endX = @min(maxEndX, requestedEndX);
867
- const endY = @min(maxEndY, requestedEndY);
868
-
869
- if (startX > endX or startY > endY) return;
870
-
871
- const clippedRect = self.clipRectToScissor(@intCast(startX), @intCast(startY), endX - startX + 1, endY - startY + 1) orelse return;
872
- const clippedStartX = @max(startX, @as(u32, @intCast(clippedRect.x)));
873
- const clippedStartY = @max(startY, @as(u32, @intCast(clippedRect.y)));
874
- const clippedEndX = @min(endX, @as(u32, @intCast(clippedRect.x + @as(i32, @intCast(clippedRect.width)) - 1)));
875
- const clippedEndY = @min(endY, @as(u32, @intCast(clippedRect.y + @as(i32, @intCast(clippedRect.height)) - 1)));
876
-
877
- const opacity = self.getCurrentOpacity();
878
- const hasAlpha = isRGBAWithAlpha(bg) or opacity < 1.0;
879
- const linkAware = self.link_tracker.hasAny();
880
-
881
- if (hasAlpha or self.grapheme_tracker.hasAny() or linkAware) {
882
- var fillY = clippedStartY;
883
- while (fillY <= clippedEndY) : (fillY += 1) {
884
- var fillX = clippedStartX;
885
- while (fillX <= clippedEndX) : (fillX += 1) {
886
- try self.setCellWithAlphaBlending(fillX, fillY, DEFAULT_SPACE_CHAR, .{ 1.0, 1.0, 1.0, 1.0 }, bg, 0);
887
- }
888
- }
889
- } else {
890
- // For non-alpha (fully opaque) backgrounds with no graphemes or links, we can do direct filling
891
- var fillY = clippedStartY;
892
- while (fillY <= clippedEndY) : (fillY += 1) {
893
- const rowStartIndex = self.coordsToIndex(@intCast(clippedStartX), @intCast(fillY));
894
- const rowWidth = clippedEndX - clippedStartX + 1;
895
-
896
- const rowSliceChar = self.buffer.char[rowStartIndex .. rowStartIndex + rowWidth];
897
- const rowSliceFg = self.buffer.fg[rowStartIndex .. rowStartIndex + rowWidth];
898
- const rowSliceBg = self.buffer.bg[rowStartIndex .. rowStartIndex + rowWidth];
899
- const rowSliceAttrs = self.buffer.attributes[rowStartIndex .. rowStartIndex + rowWidth];
900
-
901
- @memset(rowSliceChar, @intCast(DEFAULT_SPACE_CHAR));
902
- @memset(rowSliceFg, .{ 1.0, 1.0, 1.0, 1.0 });
903
- @memset(rowSliceBg, bg);
904
- @memset(rowSliceAttrs, 0);
905
- }
906
- }
907
- }
908
-
909
- pub fn drawText(
910
- self: *OptimizedBuffer,
911
- text: []const u8,
912
- x: u32,
913
- y: u32,
914
- fg: RGBA,
915
- bg: ?RGBA,
916
- attributes: u32,
917
- ) BufferError!void {
918
- if (x >= self.width or y >= self.height) return;
919
- if (text.len == 0) return;
920
-
921
- const is_ascii_only = utf8.isAsciiOnly(text);
922
-
923
- var grapheme_list: std.ArrayListUnmanaged(utf8.GraphemeInfo) = .{};
924
- defer grapheme_list.deinit(self.allocator);
925
-
926
- const tab_width: u8 = 2;
927
- try utf8.findGraphemeInfo(text, tab_width, is_ascii_only, self.width_method, self.allocator, &grapheme_list);
928
- const specials = grapheme_list.items;
929
-
930
- var advance_cells: u32 = 0;
931
- var byte_offset: u32 = 0;
932
- var col: u32 = 0;
933
- var special_idx: usize = 0;
934
-
935
- while (byte_offset < text.len) {
936
- const charX = x + advance_cells;
937
- if (charX >= self.width) break;
938
-
939
- const at_special = special_idx < specials.len and specials[special_idx].col_offset == col;
940
-
941
- var grapheme_bytes: []const u8 = undefined;
942
- var g_width: u8 = undefined;
943
-
944
- if (at_special) {
945
- const g = specials[special_idx];
946
- grapheme_bytes = text[g.byte_offset .. g.byte_offset + g.byte_len];
947
- g_width = g.width;
948
- byte_offset = g.byte_offset + g.byte_len;
949
- special_idx += 1;
950
- } else {
951
- if (byte_offset >= text.len) break;
952
- grapheme_bytes = text[byte_offset .. byte_offset + 1];
953
- g_width = 1;
954
- byte_offset += 1;
955
- }
956
-
957
- if (!self.isPointInScissor(@intCast(charX), @intCast(y))) {
958
- advance_cells += g_width;
959
- col += g_width;
960
- continue;
961
- }
962
-
963
- var bgColor: RGBA = undefined;
964
- if (bg) |b| {
965
- bgColor = b;
966
- } else if (self.get(charX, y)) |existingCell| {
967
- bgColor = existingCell.bg;
968
- } else {
969
- bgColor = .{ 0.0, 0.0, 0.0, 1.0 };
970
- }
971
-
972
- const cell_width = utf8.getWidthAt(text, if (at_special) specials[special_idx - 1].byte_offset else byte_offset - 1, tab_width, self.width_method);
973
- if (cell_width == 0) {
974
- col += g_width;
975
- continue;
976
- }
977
-
978
- if (grapheme_bytes.len == 1 and grapheme_bytes[0] == '\t') {
979
- var tab_col: u32 = 0;
980
- while (tab_col < g_width) : (tab_col += 1) {
981
- const tab_x = charX + tab_col;
982
- if (tab_x >= self.width) break;
983
-
984
- if (isRGBAWithAlpha(bgColor)) {
985
- try self.setCellWithAlphaBlending(
986
- tab_x,
987
- y,
988
- DEFAULT_SPACE_CHAR,
989
- fg,
990
- bgColor,
991
- attributes,
992
- );
993
- } else {
994
- self.set(tab_x, y, Cell{
995
- .char = DEFAULT_SPACE_CHAR,
996
- .fg = fg,
997
- .bg = bgColor,
998
- .attributes = attributes,
999
- });
1000
- }
1001
- }
1002
- advance_cells += g_width;
1003
- col += g_width;
1004
- continue;
1005
- }
1006
-
1007
- var encoded_char: u32 = 0;
1008
- if (grapheme_bytes.len == 1 and cell_width == 1 and grapheme_bytes[0] >= 32) {
1009
- encoded_char = @as(u32, grapheme_bytes[0]);
1010
- } else {
1011
- const gid = self.pool.alloc(grapheme_bytes) catch return BufferError.OutOfMemory;
1012
- encoded_char = gp.packGraphemeStart(gid & gp.GRAPHEME_ID_MASK, cell_width);
1013
- }
1014
-
1015
- if (isRGBAWithAlpha(bgColor)) {
1016
- try self.setCellWithAlphaBlending(charX, y, encoded_char, fg, bgColor, attributes);
1017
- } else {
1018
- self.set(charX, y, Cell{
1019
- .char = encoded_char,
1020
- .fg = fg,
1021
- .bg = bgColor,
1022
- .attributes = attributes,
1023
- });
1024
- }
1025
-
1026
- advance_cells += cell_width;
1027
- col += g_width;
1028
- }
1029
- }
1030
-
1031
- pub fn drawFrameBuffer(self: *OptimizedBuffer, destX: i32, destY: i32, frameBuffer: *OptimizedBuffer, sourceX: ?u32, sourceY: ?u32, sourceWidth: ?u32, sourceHeight: ?u32) void {
1032
- if (self.width == 0 or self.height == 0 or frameBuffer.width == 0 or frameBuffer.height == 0) return;
1033
-
1034
- const srcX = sourceX orelse 0;
1035
- const srcY = sourceY orelse 0;
1036
- const srcWidth = sourceWidth orelse frameBuffer.width;
1037
- const srcHeight = sourceHeight orelse frameBuffer.height;
1038
-
1039
- if (srcX >= frameBuffer.width or srcY >= frameBuffer.height) return;
1040
- if (srcWidth == 0 or srcHeight == 0) return;
1041
-
1042
- const clampedSrcWidth = @min(srcWidth, frameBuffer.width - srcX);
1043
- const clampedSrcHeight = @min(srcHeight, frameBuffer.height - srcY);
1044
-
1045
- const startDestX = @max(0, destX);
1046
- const startDestY = @max(0, destY);
1047
- const endDestX = @min(@as(i32, @intCast(self.width)) - 1, destX + @as(i32, @intCast(clampedSrcWidth)) - 1);
1048
- const endDestY = @min(@as(i32, @intCast(self.height)) - 1, destY + @as(i32, @intCast(clampedSrcHeight)) - 1);
1049
-
1050
- if (startDestX > endDestX or startDestY > endDestY) return;
1051
-
1052
- // Check if the destination rectangle intersects with the scissor rect
1053
- const destWidth = @as(u32, @intCast(endDestX - startDestX + 1));
1054
- const destHeight = @as(u32, @intCast(endDestY - startDestY + 1));
1055
- if (!self.isRectInScissor(startDestX, startDestY, destWidth, destHeight)) return;
1056
-
1057
- const graphemeAware = self.grapheme_tracker.hasAny() or frameBuffer.grapheme_tracker.hasAny();
1058
- const linkAware = self.link_tracker.hasAny() or frameBuffer.link_tracker.hasAny();
1059
-
1060
- // Calculate clipping once for both paths
1061
- const clippedRect = self.clipRectToScissor(startDestX, startDestY, destWidth, destHeight) orelse return;
1062
- const clippedStartX = @max(startDestX, clippedRect.x);
1063
- const clippedStartY = @max(startDestY, clippedRect.y);
1064
- const clippedEndX = @min(endDestX, @as(i32, @intCast(clippedRect.x + @as(i32, @intCast(clippedRect.width)) - 1)));
1065
- const clippedEndY = @min(endDestY, @as(i32, @intCast(clippedRect.y + @as(i32, @intCast(clippedRect.height)) - 1)));
1066
-
1067
- if (!graphemeAware and !frameBuffer.respectAlpha and !linkAware) {
1068
- // Fast path: direct memory copy
1069
- var dY = clippedStartY;
1070
-
1071
- while (dY <= clippedEndY) : (dY += 1) {
1072
- const relativeDestY = dY - destY;
1073
- const sY = srcY + @as(u32, @intCast(relativeDestY));
1074
-
1075
- if (sY >= frameBuffer.height) continue;
1076
-
1077
- const relativeDestX = clippedStartX - destX;
1078
- const sX = srcX + @as(u32, @intCast(relativeDestX));
1079
-
1080
- if (sX >= frameBuffer.width) continue;
1081
-
1082
- const destRowStart = self.coordsToIndex(@intCast(clippedStartX), @intCast(dY));
1083
- const srcRowStart = frameBuffer.coordsToIndex(sX, sY);
1084
- const actualCopyWidth = @min(@as(u32, @intCast(clippedEndX - clippedStartX + 1)), frameBuffer.width - sX);
1085
-
1086
- @memcpy(self.buffer.char[destRowStart .. destRowStart + actualCopyWidth], frameBuffer.buffer.char[srcRowStart .. srcRowStart + actualCopyWidth]);
1087
- @memcpy(self.buffer.fg[destRowStart .. destRowStart + actualCopyWidth], frameBuffer.buffer.fg[srcRowStart .. srcRowStart + actualCopyWidth]);
1088
- @memcpy(self.buffer.bg[destRowStart .. destRowStart + actualCopyWidth], frameBuffer.buffer.bg[srcRowStart .. srcRowStart + actualCopyWidth]);
1089
- @memcpy(self.buffer.attributes[destRowStart .. destRowStart + actualCopyWidth], frameBuffer.buffer.attributes[srcRowStart .. srcRowStart + actualCopyWidth]);
1090
- }
1091
- return;
1092
- }
1093
-
1094
- var dY = clippedStartY;
1095
- while (dY <= clippedEndY) : (dY += 1) {
1096
- var lastDrawnGraphemeId: u32 = 0;
1097
-
1098
- var dX = clippedStartX;
1099
- while (dX <= clippedEndX) : (dX += 1) {
1100
- const relativeDestX = dX - destX;
1101
- const relativeDestY = dY - destY;
1102
- const sX = srcX + @as(u32, @intCast(relativeDestX));
1103
- const sY = srcY + @as(u32, @intCast(relativeDestY));
1104
-
1105
- if (sX >= frameBuffer.width or sY >= frameBuffer.height) continue;
1106
-
1107
- const srcIndex = frameBuffer.coordsToIndex(sX, sY);
1108
- if (srcIndex >= frameBuffer.buffer.char.len) continue;
1109
-
1110
- const srcChar = frameBuffer.buffer.char[srcIndex];
1111
- const srcFg = frameBuffer.buffer.fg[srcIndex];
1112
- const srcBg = frameBuffer.buffer.bg[srcIndex];
1113
- const srcAttr = frameBuffer.buffer.attributes[srcIndex];
1114
-
1115
- if (srcBg[3] == 0.0 and srcFg[3] == 0.0) continue;
1116
-
1117
- if (graphemeAware) {
1118
- if (gp.isContinuationChar(srcChar)) {
1119
- const graphemeId = srcChar & gp.GRAPHEME_ID_MASK;
1120
- if (graphemeId != lastDrawnGraphemeId) {
1121
- // We haven't drawn the start character for this grapheme (likely out of bounds to the left)
1122
- // Draw a space with the same attributes to fill the cell
1123
- self.setCellWithAlphaBlending(@intCast(dX), @intCast(dY), DEFAULT_SPACE_CHAR, srcFg, srcBg, srcAttr) catch {};
1124
- }
1125
- continue;
1126
- }
1127
-
1128
- if (gp.isGraphemeChar(srcChar)) {
1129
- lastDrawnGraphemeId = srcChar & gp.GRAPHEME_ID_MASK;
1130
- }
1131
-
1132
- self.setCellWithAlphaBlending(@intCast(dX), @intCast(dY), srcChar, srcFg, srcBg, srcAttr) catch {};
1133
- continue;
1134
- }
1135
-
1136
- self.setCellWithAlphaBlendingRaw(@intCast(dX), @intCast(dY), srcChar, srcFg, srcBg, srcAttr) catch {};
1137
- }
1138
- }
1139
- }
1140
-
1141
- /// Draw a TextBufferView to this OptimizedBuffer with selection support and optional syntax highlighting
1142
- pub fn drawTextBuffer(
1143
- self: *OptimizedBuffer,
1144
- text_buffer_view: *TextBufferView,
1145
- x: i32,
1146
- y: i32,
1147
- ) !void {
1148
- try self.drawTextBufferInternal(TextBufferView, text_buffer_view, x, y);
1149
- }
1150
-
1151
- /// Internal implementation that accepts either TextBufferView or EditorView
1152
- /// Both types must expose: getVirtualLines(), getViewport(), getCachedLineInfo(), getVirtualLineSpans(), getTextBuffer(), getSelection()
1153
- fn drawTextBufferInternal(
1154
- self: *OptimizedBuffer,
1155
- comptime ViewType: type,
1156
- view: *ViewType,
1157
- x: i32,
1158
- y: i32,
1159
- ) !void {
1160
- const virtual_lines = view.getVirtualLines();
1161
- if (virtual_lines.len == 0) return;
1162
-
1163
- const firstVisibleLine: u32 = if (y < 0) @intCast(-y) else 0;
1164
- const bufferBottomY = self.height;
1165
- const lastPossibleLine = if (y >= @as(i32, @intCast(bufferBottomY)))
1166
- 0
1167
- else if (y < 0)
1168
- @min(virtual_lines.len, firstVisibleLine + bufferBottomY)
1169
- else
1170
- @min(virtual_lines.len, bufferBottomY - @as(u32, @intCast(y)));
1171
-
1172
- if (firstVisibleLine >= virtual_lines.len or lastPossibleLine == 0) return;
1173
- if (firstVisibleLine >= lastPossibleLine) return;
1174
-
1175
- const viewport = view.getViewport();
1176
- const horizontal_offset: u32 = if (viewport) |vp| vp.x else 0;
1177
- const viewport_width: u32 = if (viewport) |vp| vp.width else std.math.maxInt(u32);
1178
-
1179
- var currentX = x;
1180
- var currentY = y + @as(i32, @intCast(firstVisibleLine));
1181
- const text_buffer = view.getTextBuffer();
1182
- const total_line_count = text_buffer.lineCount();
1183
-
1184
- const line_info = view.getCachedLineInfo();
1185
- var globalCharPos: u32 = if (firstVisibleLine < line_info.line_start_cols.len)
1186
- line_info.line_start_cols[firstVisibleLine]
1187
- else
1188
- 0;
1189
-
1190
- for (virtual_lines[firstVisibleLine..lastPossibleLine], 0..) |vline, slice_idx| {
1191
- if (currentY >= bufferBottomY) break;
1192
-
1193
- currentX = x;
1194
- var column_in_line: u32 = 0;
1195
- globalCharPos = vline.col_offset;
1196
-
1197
- // When viewport is set, virtual_lines is a slice starting from viewport.y
1198
- // But getVirtualLineSpans expects absolute indices, so we need to use the absolute index
1199
- // slice_idx is relative to the slice (0, 1, 2...), we need to add viewport offset + firstVisibleLine
1200
- const viewport_offset: u32 = if (viewport) |vp| vp.y else 0;
1201
- const vline_idx = viewport_offset + firstVisibleLine + slice_idx;
1202
- const vline_span_info = view.getVirtualLineSpans(vline_idx);
1203
- const spans = vline_span_info.spans;
1204
- const col_offset = vline_span_info.col_offset;
1205
- var span_idx: usize = 0;
1206
- const defaults = text_buffer.defaults();
1207
- var lineFg = defaults.fg orelse RGBA{ 1.0, 1.0, 1.0, 1.0 };
1208
- var lineBg = defaults.bg orelse RGBA{ 0.0, 0.0, 0.0, 0.0 };
1209
- var lineAttributes = defaults.attributes orelse 0;
1210
- const defaultFg = lineFg;
1211
- const defaultBg = lineBg;
1212
- const defaultAttributes = lineAttributes;
1213
-
1214
- // Find the span that contains the starting render position (col_offset + horizontal_offset)
1215
- const start_col = col_offset + horizontal_offset;
1216
- while (span_idx < spans.len and spans[span_idx].next_col <= start_col) {
1217
- span_idx += 1;
1218
- }
1219
-
1220
- var next_change_col: u32 = if (span_idx < spans.len)
1221
- spans[span_idx].next_col
1222
- else
1223
- std.math.maxInt(u32);
1224
-
1225
- // Apply the style at the starting position
1226
- if (span_idx < spans.len and spans[span_idx].col <= start_col and spans[span_idx].style_id != 0) {
1227
- if (text_buffer.getSyntaxStyle()) |style| {
1228
- if (style.resolveById(spans[span_idx].style_id)) |resolved_style| {
1229
- if (resolved_style.fg) |fg| lineFg = fg;
1230
- if (resolved_style.bg) |bg| lineBg = bg;
1231
- lineAttributes |= resolved_style.attributes;
1232
- }
1233
- }
1234
- }
1235
-
1236
- for (vline.chunks.items) |vchunk| {
1237
- const chunk = vchunk.chunk;
1238
- const chunk_bytes = chunk.getBytes(text_buffer.memRegistry());
1239
- const specials = chunk.getGraphemes(text_buffer.memRegistry(), text_buffer.getAllocator(), text_buffer.tabWidth(), text_buffer.widthMethod()) catch continue;
1240
- const line_col_offset = vline.col_offset;
1241
-
1242
- if (currentX >= @as(i32, @intCast(self.width))) {
1243
- globalCharPos += vchunk.width;
1244
- currentX += @intCast(vchunk.width);
1245
- continue;
1246
- }
1247
- const col_end = vchunk.grapheme_start + vchunk.width;
1248
- var col = vchunk.grapheme_start;
1249
- var special_idx: usize = 0;
1250
- var byte_offset: u32 = 0;
1251
-
1252
- if (vchunk.grapheme_start > 0) {
1253
- // Use UTF-8 aware position finding to skip to the grapheme_start
1254
- const is_ascii_only = (vchunk.chunk.flags & tb.TextChunk.Flags.ASCII_ONLY) != 0;
1255
- const pos_result = utf8.findPosByWidth(chunk_bytes, vchunk.grapheme_start, text_buffer.tabWidth(), is_ascii_only, false, text_buffer.widthMethod());
1256
- byte_offset = pos_result.byte_offset;
1257
-
1258
- // Advance special_idx to match the skipped columns
1259
- var init_col: u32 = 0;
1260
- while (init_col < vchunk.grapheme_start and special_idx < specials.len) {
1261
- const g = specials[special_idx];
1262
- if (g.col_offset < vchunk.grapheme_start) {
1263
- special_idx += 1;
1264
- init_col = g.col_offset + g.width;
1265
- } else {
1266
- break;
1267
- }
1268
- }
1269
- }
1270
-
1271
- while (col < col_end) {
1272
- const at_special = special_idx < specials.len and specials[special_idx].col_offset == col;
1273
-
1274
- var grapheme_bytes: []const u8 = undefined;
1275
- var g_width: u8 = undefined;
1276
-
1277
- if (at_special) {
1278
- const g = specials[special_idx];
1279
- grapheme_bytes = chunk_bytes[g.byte_offset .. g.byte_offset + g.byte_len];
1280
- g_width = g.width;
1281
- byte_offset = g.byte_offset + g.byte_len;
1282
- special_idx += 1;
1283
- } else {
1284
- if (byte_offset >= chunk_bytes.len) break;
1285
- const cp_len = std.unicode.utf8ByteSequenceLength(chunk_bytes[byte_offset]) catch 1;
1286
- const next_byte_offset = @min(byte_offset + cp_len, chunk_bytes.len);
1287
- grapheme_bytes = chunk_bytes[byte_offset..next_byte_offset];
1288
- g_width = 1;
1289
- byte_offset = next_byte_offset;
1290
- }
1291
-
1292
- if (column_in_line < horizontal_offset) {
1293
- globalCharPos += g_width;
1294
- column_in_line += g_width;
1295
- col += g_width;
1296
- continue;
1297
- }
1298
-
1299
- if (column_in_line >= horizontal_offset + viewport_width) {
1300
- globalCharPos += (col_end - col);
1301
- break;
1302
- }
1303
-
1304
- if (currentX < -@as(i32, @intCast(g_width))) {
1305
- globalCharPos += g_width;
1306
- currentX += @as(i32, @intCast(g_width));
1307
- column_in_line += g_width;
1308
- col += g_width;
1309
- continue;
1310
- }
1311
-
1312
- if (currentX >= @as(i32, @intCast(self.width))) {
1313
- globalCharPos += (col_end - col);
1314
- break;
1315
- }
1316
-
1317
- if (!self.isPointInScissor(currentX, currentY)) {
1318
- globalCharPos += g_width;
1319
- currentX += @as(i32, @intCast(g_width));
1320
- column_in_line += g_width;
1321
- col += g_width;
1322
- continue;
1323
- }
1324
-
1325
- var selection_offset = globalCharPos;
1326
- if (vline.is_truncated and globalCharPos >= line_col_offset) {
1327
- const ellipsis_width: u32 = 3;
1328
- const column_offset_in_line = globalCharPos - line_col_offset;
1329
- if (column_offset_in_line >= vline.ellipsis_pos and column_offset_in_line < vline.ellipsis_pos + ellipsis_width) {
1330
- selection_offset = line_col_offset + vline.ellipsis_pos;
1331
- } else if (column_offset_in_line >= vline.ellipsis_pos + ellipsis_width) {
1332
- selection_offset = line_col_offset + vline.truncation_suffix_start +
1333
- (column_offset_in_line - vline.ellipsis_pos - ellipsis_width);
1334
- } else {
1335
- selection_offset = line_col_offset + column_offset_in_line;
1336
- }
1337
- }
1338
-
1339
- // Track the actual column position in the source line (including horizontal offset)
1340
- var source_col_pos = col_offset + column_in_line;
1341
- if (vline.is_truncated) {
1342
- const ellipsis_width: u32 = 3;
1343
- const column_offset_in_line = globalCharPos - line_col_offset;
1344
- if (column_offset_in_line >= vline.ellipsis_pos and column_offset_in_line < vline.ellipsis_pos + ellipsis_width) {
1345
- source_col_pos = std.math.maxInt(u32);
1346
- } else if (column_offset_in_line >= vline.ellipsis_pos + ellipsis_width) {
1347
- source_col_pos = vline.truncation_suffix_start + (column_offset_in_line - vline.ellipsis_pos - ellipsis_width);
1348
- }
1349
- }
1350
-
1351
- if (source_col_pos >= next_change_col and span_idx + 1 < spans.len) {
1352
- span_idx += 1;
1353
- const new_span = spans[span_idx];
1354
-
1355
- lineFg = defaultFg;
1356
- lineBg = defaultBg;
1357
- lineAttributes = defaultAttributes;
1358
-
1359
- if (text_buffer.getSyntaxStyle()) |style| {
1360
- if (new_span.style_id != 0) {
1361
- if (style.resolveById(new_span.style_id)) |resolved_style| {
1362
- if (resolved_style.fg) |fg| lineFg = fg;
1363
- if (resolved_style.bg) |bg| lineBg = bg;
1364
- lineAttributes |= resolved_style.attributes;
1365
- }
1366
- }
1367
- }
1368
-
1369
- next_change_col = new_span.next_col;
1370
- }
1371
-
1372
- if (vline.is_truncated) {
1373
- const column_offset_in_line = globalCharPos - line_col_offset;
1374
- const ellipsis_width: u32 = 3;
1375
- if (column_offset_in_line >= vline.ellipsis_pos and column_offset_in_line < vline.ellipsis_pos + ellipsis_width) {
1376
- lineFg = defaultFg;
1377
- lineBg = defaultBg;
1378
- lineAttributes = defaultAttributes;
1379
- } else if (column_offset_in_line >= vline.ellipsis_pos + ellipsis_width) {
1380
- const suffix_col_pos = vline.truncation_suffix_start + (column_offset_in_line - vline.ellipsis_pos - ellipsis_width);
1381
- if (spans.len == 0) {
1382
- lineFg = defaultFg;
1383
- lineBg = defaultBg;
1384
- lineAttributes = defaultAttributes;
1385
- next_change_col = std.math.maxInt(u32);
1386
- } else {
1387
- var suffix_span_idx: usize = 0;
1388
- while (suffix_span_idx < spans.len and spans[suffix_span_idx].next_col <= suffix_col_pos) {
1389
- suffix_span_idx += 1;
1390
- }
1391
- if (suffix_span_idx < spans.len) {
1392
- span_idx = suffix_span_idx;
1393
- }
1394
- const active_span = spans[span_idx];
1395
- lineFg = defaultFg;
1396
- lineBg = defaultBg;
1397
- lineAttributes = defaultAttributes;
1398
- if (text_buffer.getSyntaxStyle()) |style| {
1399
- if (active_span.style_id != 0) {
1400
- if (style.resolveById(active_span.style_id)) |resolved_style| {
1401
- if (resolved_style.fg) |fg| lineFg = fg;
1402
- if (resolved_style.bg) |bg| lineBg = bg;
1403
- lineAttributes |= resolved_style.attributes;
1404
- }
1405
- }
1406
- }
1407
- next_change_col = active_span.next_col;
1408
- }
1409
- }
1410
- }
1411
-
1412
- var finalFg = lineFg;
1413
- var finalBg = lineBg;
1414
- const finalAttributes = lineAttributes;
1415
-
1416
- var cell_idx: u32 = 0;
1417
- while (cell_idx < g_width) : (cell_idx += 1) {
1418
- if (view.getSelection()) |sel| {
1419
- const isSelected = selection_offset + cell_idx >= sel.start and selection_offset + cell_idx < sel.end;
1420
- if (isSelected) {
1421
- if (sel.bgColor) |selBg| {
1422
- finalBg = selBg;
1423
- if (sel.fgColor) |selFg| {
1424
- finalFg = selFg;
1425
- }
1426
- } else {
1427
- const temp = lineFg;
1428
- finalFg = if (lineBg[3] > 0) lineBg else RGBA{ 0.0, 0.0, 0.0, 1.0 };
1429
- finalBg = temp;
1430
- }
1431
- break;
1432
- }
1433
- }
1434
- }
1435
-
1436
- // Skip zero-width characters (ZWJ, VS16, etc.) - don't render them
1437
- // Don't increment col since they take no space
1438
- if (g_width == 0) {
1439
- continue;
1440
- }
1441
-
1442
- var drawFg = finalFg;
1443
- var drawBg = finalBg;
1444
- const drawAttributes = finalAttributes;
1445
-
1446
- if (drawAttributes & (1 << 5) != 0) {
1447
- const temp = drawFg;
1448
- drawFg = drawBg;
1449
- drawBg = temp;
1450
- }
1451
-
1452
- if (grapheme_bytes.len == 1 and grapheme_bytes[0] == '\t') {
1453
- const tab_indicator = view.getTabIndicator();
1454
- const tab_indicator_color = view.getTabIndicatorColor();
1455
-
1456
- var tab_col: u32 = 0;
1457
- while (tab_col < g_width) : (tab_col += 1) {
1458
- if (currentX + @as(i32, @intCast(tab_col)) >= @as(i32, @intCast(self.width))) break;
1459
-
1460
- const char = if (tab_col == 0 and tab_indicator != null) tab_indicator.? else DEFAULT_SPACE_CHAR;
1461
- const fg = if (tab_col == 0 and tab_indicator_color != null) tab_indicator_color.? else drawFg;
1462
-
1463
- try self.setCellWithAlphaBlending(
1464
- @intCast(currentX + @as(i32, @intCast(tab_col))),
1465
- @intCast(currentY),
1466
- char,
1467
- fg,
1468
- drawBg,
1469
- drawAttributes,
1470
- );
1471
- }
1472
- } else {
1473
- var encoded_char: u32 = 0;
1474
- if (grapheme_bytes.len == 1 and g_width == 1 and grapheme_bytes[0] >= 32) {
1475
- encoded_char = @as(u32, grapheme_bytes[0]);
1476
- } else {
1477
- const gid = self.pool.alloc(grapheme_bytes) catch |err| {
1478
- logger.warn("GraphemePool.alloc FAILED for grapheme (len={d}, bytes={any}): {}", .{ grapheme_bytes.len, grapheme_bytes, err });
1479
- globalCharPos += g_width;
1480
- currentX += @as(i32, @intCast(g_width));
1481
- col += g_width;
1482
- continue;
1483
- };
1484
- encoded_char = gp.packGraphemeStart(gid & gp.GRAPHEME_ID_MASK, g_width);
1485
- }
1486
-
1487
- try self.setCellWithAlphaBlending(
1488
- @intCast(currentX),
1489
- @intCast(currentY),
1490
- encoded_char,
1491
- drawFg,
1492
- drawBg,
1493
- drawAttributes,
1494
- );
1495
- }
1496
-
1497
- globalCharPos += g_width;
1498
- currentX += @as(i32, @intCast(g_width));
1499
- column_in_line += g_width;
1500
- col += g_width;
1501
- }
1502
- }
1503
-
1504
- const is_last_vline_of_logical_line = (slice_idx + 1 >= virtual_lines[firstVisibleLine..lastPossibleLine].len) or
1505
- (virtual_lines[firstVisibleLine..lastPossibleLine][slice_idx + 1].source_line != vline.source_line);
1506
-
1507
- if (is_last_vline_of_logical_line) {
1508
- const is_last_logical_line = vline.source_line + 1 >= total_line_count;
1509
- if (!is_last_logical_line) {
1510
- globalCharPos += 1;
1511
- }
1512
- }
1513
-
1514
- currentY += 1;
1515
- }
1516
- }
1517
-
1518
- /// Draw an EditorView to this OptimizedBuffer
1519
- /// EditorView wraps TextBufferView, so we just delegate to drawTextBufferInternal
1520
- /// EditorView handles viewport management and returns only the visible lines
1521
- pub fn drawEditorView(
1522
- self: *OptimizedBuffer,
1523
- editor_view: *EditorView,
1524
- x: i32,
1525
- y: i32,
1526
- ) !void {
1527
- try self.drawTextBufferInternal(EditorView, editor_view, x, y);
1528
- }
1529
-
1530
- /// Draw a complete border grid in a single call.
1531
- /// columnOffsets and rowOffsets include an extra trailing entry so that
1532
- /// the range for column `i` is `[columnOffsets[i]+1 .. columnOffsets[i+1]-1]`.
1533
- pub fn drawGrid(
1534
- self: *OptimizedBuffer,
1535
- borderChars: [*]const u32,
1536
- borderFg: RGBA,
1537
- borderBg: RGBA,
1538
- columnOffsets: [*]const i32,
1539
- columnCount: u32,
1540
- rowOffsets: [*]const i32,
1541
- rowCount: u32,
1542
- drawInner: bool,
1543
- drawOuter: bool,
1544
- ) void {
1545
- if (rowCount == 0 or columnCount == 0) return;
1546
- if (!drawInner and !drawOuter) return;
1547
-
1548
- const hChar = borderChars[@intFromEnum(BorderCharIndex.horizontal)];
1549
- const vChar = borderChars[@intFromEnum(BorderCharIndex.vertical)];
1550
- const bufWidth = self.width;
1551
- const bufHeight = self.height;
1552
- const bufWidthI32 = @as(i32, @intCast(bufWidth));
1553
- const bufHeightI32 = @as(i32, @intCast(bufHeight));
1554
-
1555
- // Draw row-by-row: horizontal border line, then vertical borders for the row's content area
1556
- var rowIdx: u32 = 0;
1557
- while (rowIdx <= rowCount) : (rowIdx += 1) {
1558
- const is_outer_row = rowIdx == 0 or rowIdx == rowCount;
1559
- const should_draw_horizontal = if (is_outer_row) drawOuter else drawInner;
1560
- const borderY = rowOffsets[rowIdx];
1561
- if (borderY >= bufHeightI32) break;
1562
-
1563
- // --- horizontal border line: intersections + fills ---
1564
- if (should_draw_horizontal and borderY >= 0) {
1565
- var colBorderIdx: u32 = 0;
1566
- while (colBorderIdx <= columnCount) : (colBorderIdx += 1) {
1567
- const is_outer_col = colBorderIdx == 0 or colBorderIdx == columnCount;
1568
- const should_draw_vertical = if (is_outer_col) drawOuter else drawInner;
1569
- if (!should_draw_vertical) continue;
1570
-
1571
- const bx = columnOffsets[colBorderIdx];
1572
- if (bx >= bufWidthI32) break;
1573
- if (bx < 0) continue;
1574
-
1575
- const has_up = rowIdx > 0 and should_draw_vertical;
1576
- const has_down = rowIdx < rowCount and should_draw_vertical;
1577
- const has_left = colBorderIdx > 0;
1578
- const has_right = colBorderIdx < columnCount;
1579
- const intersection = tableBorderIntersectionByConnections(borderChars, has_up, has_down, has_left, has_right);
1580
-
1581
- self.setRaw(@as(u32, @intCast(bx)), @as(u32, @intCast(borderY)), Cell{ .char = intersection, .fg = borderFg, .bg = borderBg, .attributes = 0 });
1582
- }
1583
-
1584
- var colIdx: u32 = 0;
1585
- while (colIdx < columnCount) : (colIdx += 1) {
1586
- const has_boundary_after = if (colIdx < columnCount - 1) drawInner else drawOuter;
1587
- const boundary_padding: i32 = if (has_boundary_after) 0 else 1;
1588
- const startX = columnOffsets[colIdx] + 1;
1589
- const endX = columnOffsets[colIdx + 1] + boundary_padding;
1590
-
1591
- if (startX >= bufWidthI32) break;
1592
- if (endX <= 0) continue;
1593
-
1594
- const clampedStart = @as(u32, @intCast(@max(@as(i32, 0), startX)));
1595
- const clampedEnd = @as(u32, @intCast(@min(bufWidthI32, endX)));
1596
-
1597
- if (clampedStart < clampedEnd) {
1598
- const borderYU32 = @as(u32, @intCast(borderY));
1599
- @memset(self.buffer.char[borderYU32 * bufWidth + clampedStart .. borderYU32 * bufWidth + clampedEnd], hChar);
1600
- @memset(self.buffer.fg[borderYU32 * bufWidth + clampedStart .. borderYU32 * bufWidth + clampedEnd], borderFg);
1601
- @memset(self.buffer.bg[borderYU32 * bufWidth + clampedStart .. borderYU32 * bufWidth + clampedEnd], borderBg);
1602
- @memset(self.buffer.attributes[borderYU32 * bufWidth + clampedStart .. borderYU32 * bufWidth + clampedEnd], 0);
1603
- }
1604
- }
1605
- }
1606
-
1607
- if (rowIdx >= rowCount) break;
1608
-
1609
- // --- vertical borders for each content line in this row ---
1610
- const has_row_boundary_after = if (rowIdx < rowCount - 1) drawInner else drawOuter;
1611
- const row_boundary_padding: i32 = if (has_row_boundary_after) 0 else 1;
1612
- const contentStartY = borderY + 1;
1613
- const contentEndY = rowOffsets[rowIdx + 1] + row_boundary_padding;
1614
- var cy = contentStartY;
1615
- while (cy < contentEndY and cy < bufHeightI32) : (cy += 1) {
1616
- if (cy < 0) continue;
1617
-
1618
- const rowBase = @as(u32, @intCast(cy)) * bufWidth;
1619
- var colBorderIdx: u32 = 0;
1620
- while (colBorderIdx <= columnCount) : (colBorderIdx += 1) {
1621
- const is_outer_col = colBorderIdx == 0 or colBorderIdx == columnCount;
1622
- const should_draw_vertical = if (is_outer_col) drawOuter else drawInner;
1623
- if (!should_draw_vertical) continue;
1624
-
1625
- const bx = columnOffsets[colBorderIdx];
1626
- if (bx >= bufWidthI32) break;
1627
- if (bx < 0) continue;
1628
-
1629
- const idx = rowBase + @as(u32, @intCast(bx));
1630
- self.buffer.char[idx] = vChar;
1631
- self.buffer.fg[idx] = borderFg;
1632
- self.buffer.bg[idx] = borderBg;
1633
- self.buffer.attributes[idx] = 0;
1634
- }
1635
- }
1636
- }
1637
- }
1638
-
1639
- fn tableBorderIntersectionByConnections(borderChars: [*]const u32, hasUp: bool, hasDown: bool, hasLeft: bool, hasRight: bool) u32 {
1640
- if (hasUp and hasDown and hasLeft and hasRight) return borderChars[@intFromEnum(BorderCharIndex.cross)];
1641
-
1642
- if (!hasUp and hasDown and !hasLeft and hasRight) return borderChars[@intFromEnum(BorderCharIndex.topLeft)];
1643
- if (!hasUp and hasDown and hasLeft and !hasRight) return borderChars[@intFromEnum(BorderCharIndex.topRight)];
1644
- if (hasUp and !hasDown and !hasLeft and hasRight) return borderChars[@intFromEnum(BorderCharIndex.bottomLeft)];
1645
- if (hasUp and !hasDown and hasLeft and !hasRight) return borderChars[@intFromEnum(BorderCharIndex.bottomRight)];
1646
-
1647
- if (hasUp and hasDown and !hasLeft and hasRight) return borderChars[@intFromEnum(BorderCharIndex.leftT)];
1648
- if (hasUp and hasDown and hasLeft and !hasRight) return borderChars[@intFromEnum(BorderCharIndex.rightT)];
1649
- if (!hasUp and hasDown and hasLeft and hasRight) return borderChars[@intFromEnum(BorderCharIndex.topT)];
1650
- if (hasUp and !hasDown and hasLeft and hasRight) return borderChars[@intFromEnum(BorderCharIndex.bottomT)];
1651
-
1652
- if ((hasLeft or hasRight) and !hasUp and !hasDown) return borderChars[@intFromEnum(BorderCharIndex.horizontal)];
1653
- if ((hasUp or hasDown) and !hasLeft and !hasRight) return borderChars[@intFromEnum(BorderCharIndex.vertical)];
1654
-
1655
- return borderChars[@intFromEnum(BorderCharIndex.cross)];
1656
- }
1657
-
1658
- /// Draw a box with borders and optional fill
1659
- pub fn drawBox(
1660
- self: *OptimizedBuffer,
1661
- x: i32,
1662
- y: i32,
1663
- width: u32,
1664
- height: u32,
1665
- borderChars: [*]const u32, // Array of 11 border characters
1666
- borderSides: BorderSides,
1667
- borderColor: RGBA,
1668
- backgroundColor: RGBA,
1669
- shouldFill: bool,
1670
- title: ?[]const u8,
1671
- titleAlignment: u8, // 0=left, 1=center, 2=right
1672
- ) !void {
1673
- const startX = @max(0, x);
1674
- const startY = @max(0, y);
1675
- const endX = @min(@as(i32, @intCast(self.width)) - 1, x + @as(i32, @intCast(width)) - 1);
1676
- const endY = @min(@as(i32, @intCast(self.height)) - 1, y + @as(i32, @intCast(height)) - 1);
1677
-
1678
- if (startX > endX or startY > endY) return;
1679
-
1680
- const boxWidth = @as(u32, @intCast(endX - startX + 1));
1681
- const boxHeight = @as(u32, @intCast(endY - startY + 1));
1682
- if (!self.isRectInScissor(startX, startY, boxWidth, boxHeight)) return;
1683
-
1684
- const isAtActualLeft = startX == x;
1685
- const isAtActualRight = endX == x + @as(i32, @intCast(width)) - 1;
1686
- const isAtActualTop = startY == y;
1687
- const isAtActualBottom = endY == y + @as(i32, @intCast(height)) - 1;
1688
-
1689
- var shouldDrawTitle = false;
1690
- var titleX: i32 = startX;
1691
- var titleStartX: i32 = 0;
1692
- var titleEndX: i32 = 0;
1693
-
1694
- if (title) |titleText| {
1695
- if (titleText.len > 0 and borderSides.top and isAtActualTop) {
1696
- const is_ascii = utf8.isAsciiOnly(titleText);
1697
- const titleLength = @as(i32, @intCast(utf8.calculateTextWidth(titleText, 2, is_ascii, self.width_method)));
1698
- const minTitleSpace = 4;
1699
-
1700
- shouldDrawTitle = @as(i32, @intCast(width)) >= titleLength + minTitleSpace;
1701
-
1702
- if (shouldDrawTitle) {
1703
- const padding = 2;
1704
-
1705
- if (titleAlignment == 1) { // center
1706
- titleX = startX + @max(padding, @divFloor(@as(i32, @intCast(width)) - titleLength, 2));
1707
- } else if (titleAlignment == 2) { // right
1708
- titleX = startX + @as(i32, @intCast(width)) - padding - titleLength;
1709
- } else { // left
1710
- titleX = startX + padding;
1711
- }
1712
-
1713
- titleX = @max(startX + padding, @min(titleX, endX - titleLength));
1714
- titleStartX = titleX;
1715
- titleEndX = titleX + titleLength - 1;
1716
- }
1717
- }
1718
- }
1719
-
1720
- if (shouldFill) {
1721
- if (!borderSides.top and !borderSides.right and !borderSides.bottom and !borderSides.left) {
1722
- const fillWidth = @as(u32, @intCast(endX - startX + 1));
1723
- const fillHeight = @as(u32, @intCast(endY - startY + 1));
1724
- try self.fillRect(@intCast(startX), @intCast(startY), fillWidth, fillHeight, backgroundColor);
1725
- } else {
1726
- const innerStartX = startX + if (borderSides.left and isAtActualLeft) @as(i32, 1) else @as(i32, 0);
1727
- const innerStartY = startY + if (borderSides.top and isAtActualTop) @as(i32, 1) else @as(i32, 0);
1728
- const innerEndX = endX - if (borderSides.right and isAtActualRight) @as(i32, 1) else @as(i32, 0);
1729
- const innerEndY = endY - if (borderSides.bottom and isAtActualBottom) @as(i32, 1) else @as(i32, 0);
1730
-
1731
- if (innerEndX >= innerStartX and innerEndY >= innerStartY) {
1732
- const fillWidth = @as(u32, @intCast(innerEndX - innerStartX + 1));
1733
- const fillHeight = @as(u32, @intCast(innerEndY - innerStartY + 1));
1734
- try self.fillRect(@intCast(innerStartX), @intCast(innerStartY), fillWidth, fillHeight, backgroundColor);
1735
- }
1736
- }
1737
- }
1738
-
1739
- // Special cases for extending vertical borders
1740
- const leftBorderOnly = borderSides.left and isAtActualLeft and !borderSides.top and !borderSides.bottom;
1741
- const rightBorderOnly = borderSides.right and isAtActualRight and !borderSides.top and !borderSides.bottom;
1742
- const bottomOnlyWithVerticals = borderSides.bottom and isAtActualBottom and !borderSides.top and (borderSides.left or borderSides.right);
1743
- const topOnlyWithVerticals = borderSides.top and isAtActualTop and !borderSides.bottom and (borderSides.left or borderSides.right);
1744
-
1745
- const extendVerticalsToTop = leftBorderOnly or rightBorderOnly or bottomOnlyWithVerticals;
1746
- const extendVerticalsToBottom = leftBorderOnly or rightBorderOnly or topOnlyWithVerticals;
1747
-
1748
- // Draw horizontal borders
1749
- if (borderSides.top or borderSides.bottom) {
1750
- // Draw top border
1751
- if (borderSides.top and isAtActualTop) {
1752
- var drawX = startX;
1753
- while (drawX <= endX) : (drawX += 1) {
1754
- if (startY >= 0 and startY < @as(i32, @intCast(self.height))) {
1755
- if (shouldDrawTitle and drawX >= titleStartX and drawX <= titleEndX) {
1756
- continue;
1757
- }
1758
-
1759
- var char = borderChars[@intFromEnum(BorderCharIndex.horizontal)];
1760
-
1761
- // Handle corners
1762
- if (drawX == startX and isAtActualLeft) {
1763
- char = if (borderSides.left) borderChars[@intFromEnum(BorderCharIndex.topLeft)] else borderChars[@intFromEnum(BorderCharIndex.horizontal)];
1764
- } else if (drawX == endX and isAtActualRight) {
1765
- char = if (borderSides.right) borderChars[@intFromEnum(BorderCharIndex.topRight)] else borderChars[@intFromEnum(BorderCharIndex.horizontal)];
1766
- }
1767
-
1768
- try self.setCellWithAlphaBlending(@intCast(drawX), @intCast(startY), char, borderColor, backgroundColor, 0);
1769
- }
1770
- }
1771
- }
1772
-
1773
- // Draw bottom border
1774
- if (borderSides.bottom and isAtActualBottom) {
1775
- var drawX = startX;
1776
- while (drawX <= endX) : (drawX += 1) {
1777
- if (endY >= 0 and endY < @as(i32, @intCast(self.height))) {
1778
- var char = borderChars[@intFromEnum(BorderCharIndex.horizontal)];
1779
-
1780
- // Handle corners
1781
- if (drawX == startX and isAtActualLeft) {
1782
- char = if (borderSides.left) borderChars[@intFromEnum(BorderCharIndex.bottomLeft)] else borderChars[@intFromEnum(BorderCharIndex.horizontal)];
1783
- } else if (drawX == endX and isAtActualRight) {
1784
- char = if (borderSides.right) borderChars[@intFromEnum(BorderCharIndex.bottomRight)] else borderChars[@intFromEnum(BorderCharIndex.horizontal)];
1785
- }
1786
-
1787
- try self.setCellWithAlphaBlending(@intCast(drawX), @intCast(endY), char, borderColor, backgroundColor, 0);
1788
- }
1789
- }
1790
- }
1791
- }
1792
-
1793
- // Draw vertical borders
1794
- const verticalStartY = if (extendVerticalsToTop) startY else startY + if (borderSides.top and isAtActualTop) @as(i32, 1) else @as(i32, 0);
1795
- const verticalEndY = if (extendVerticalsToBottom) endY else endY - if (borderSides.bottom and isAtActualBottom) @as(i32, 1) else @as(i32, 0);
1796
-
1797
- if (borderSides.left or borderSides.right) {
1798
- var drawY = verticalStartY;
1799
- while (drawY <= verticalEndY) : (drawY += 1) {
1800
- // Left border
1801
- if (borderSides.left and isAtActualLeft and startX >= 0 and startX < @as(i32, @intCast(self.width))) {
1802
- try self.setCellWithAlphaBlending(@intCast(startX), @intCast(drawY), borderChars[@intFromEnum(BorderCharIndex.vertical)], borderColor, backgroundColor, 0);
1803
- }
1804
-
1805
- // Right border
1806
- if (borderSides.right and isAtActualRight and endX >= 0 and endX < @as(i32, @intCast(self.width))) {
1807
- try self.setCellWithAlphaBlending(@intCast(endX), @intCast(drawY), borderChars[@intFromEnum(BorderCharIndex.vertical)], borderColor, backgroundColor, 0);
1808
- }
1809
- }
1810
- }
1811
-
1812
- if (shouldDrawTitle) {
1813
- if (title) |titleText| {
1814
- try self.drawText(titleText, @intCast(titleX), @intCast(startY), borderColor, backgroundColor, 0);
1815
- }
1816
- }
1817
- }
1818
-
1819
- /// Draw a buffer of pixel data using super sampling (2x2 pixels per character cell)
1820
- /// alignedBytesPerRow: The number of bytes per row in the pixelData buffer, considering alignment/padding.
1821
- pub fn drawSuperSampleBuffer(
1822
- self: *OptimizedBuffer,
1823
- posX: u32,
1824
- posY: u32,
1825
- pixelData: [*]const u8,
1826
- len: usize,
1827
- format: u8, // 0: bgra8unorm, 1: rgba8unorm
1828
- alignedBytesPerRow: u32,
1829
- ) !void {
1830
- const bytesPerPixel = 4;
1831
- const isBGRA = (format == 0);
1832
-
1833
- // TODO: A more robust implementation might take source width/height explicitly.
1834
-
1835
- var y_cell = posY;
1836
- while (y_cell < self.height) : (y_cell += 1) {
1837
- var x_cell = posX;
1838
- while (x_cell < self.width) : (x_cell += 1) {
1839
- if (!self.isPointInScissor(@intCast(x_cell), @intCast(y_cell))) {
1840
- continue;
1841
- }
1842
-
1843
- const renderX: u32 = (x_cell - posX) * 2;
1844
- const renderY: u32 = (y_cell - posY) * 2;
1845
-
1846
- const tlIndex: usize = @intCast(renderY * alignedBytesPerRow + renderX * bytesPerPixel);
1847
- const trIndex: usize = tlIndex + bytesPerPixel;
1848
- const blIndex: usize = @intCast((renderY + 1) * alignedBytesPerRow + renderX * bytesPerPixel);
1849
- const brIndex: usize = blIndex + bytesPerPixel;
1850
-
1851
- const indices = [_]usize{ tlIndex, trIndex, blIndex, brIndex };
1852
-
1853
- // Get RGBA colors for TL, TR, BL, BR
1854
- var pixelsRgba: [4]RGBA = undefined;
1855
- pixelsRgba[0] = getPixelColor(indices[0], pixelData, len, isBGRA); // TL
1856
- pixelsRgba[1] = getPixelColor(indices[1], pixelData, len, isBGRA); // TR
1857
- pixelsRgba[2] = getPixelColor(indices[2], pixelData, len, isBGRA); // BL
1858
- pixelsRgba[3] = getPixelColor(indices[3], pixelData, len, isBGRA); // BR
1859
-
1860
- const cellResult = renderQuadrantBlock(pixelsRgba);
1861
-
1862
- try self.setCellWithAlphaBlending(x_cell, y_cell, cellResult.char, cellResult.fg, cellResult.bg, 0);
1863
- }
1864
- }
1865
- }
1866
-
1867
- /// Draw a buffer of pixel data using pre-computed super sample results from compute shader
1868
- /// data contains an array of CellResult structs (48 bytes each)
1869
- /// Each CellResult: bg(16) + fg(16) + char(4) + padding1(4) + padding2(4) + padding3(4) = 48 bytes
1870
- pub fn drawPackedBuffer(
1871
- self: *OptimizedBuffer,
1872
- data: [*]const u8,
1873
- dataLen: usize,
1874
- posX: u32,
1875
- posY: u32,
1876
- terminalWidthCells: u32,
1877
- terminalHeightCells: u32,
1878
- ) void {
1879
- const cellResultSize = 48;
1880
- const numCells = dataLen / cellResultSize;
1881
- const bufferWidthCells = terminalWidthCells;
1882
-
1883
- var i: usize = 0;
1884
- while (i < numCells) : (i += 1) {
1885
- const cellDataOffset = i * cellResultSize;
1886
-
1887
- const cellX = posX + @as(u32, @intCast(i % bufferWidthCells));
1888
- const cellY = posY + @as(u32, @intCast(i / bufferWidthCells));
1889
-
1890
- if (cellX >= terminalWidthCells or cellY >= terminalHeightCells) continue;
1891
- if (cellX >= self.width or cellY >= self.height) continue;
1892
-
1893
- if (!self.isPointInScissor(@intCast(cellX), @intCast(cellY))) continue;
1894
-
1895
- const bgPtr = @as([*]const f32, @ptrCast(@alignCast(data + cellDataOffset)));
1896
- const bg: RGBA = .{ bgPtr[0], bgPtr[1], bgPtr[2], bgPtr[3] };
1897
-
1898
- const fgPtr = @as([*]const f32, @ptrCast(@alignCast(data + cellDataOffset + 16)));
1899
- const fg: RGBA = .{ fgPtr[0], fgPtr[1], fgPtr[2], fgPtr[3] };
1900
-
1901
- const charPtr = @as([*]const u32, @ptrCast(@alignCast(data + cellDataOffset + 32)));
1902
- var char = charPtr[0];
1903
-
1904
- if (char == 0 or char > MAX_UNICODE_CODEPOINT) {
1905
- char = DEFAULT_SPACE_CHAR;
1906
- }
1907
-
1908
- if (char < 32 or (char > 126 and char < 0x2580)) {
1909
- char = BLOCK_CHAR;
1910
- }
1911
-
1912
- self.setCellWithAlphaBlending(cellX, cellY, char, fg, bg, 0) catch {};
1913
- }
1914
- }
1915
-
1916
- fn getGrayscaleChar(intensity: f32) u32 {
1917
- if (intensity < 0.01) return ' ';
1918
- const clamped = @min(@max(intensity, 0.0), 1.0);
1919
- const index: usize = @intFromFloat(clamped * @as(f32, @floatFromInt(GRAYSCALE_CHARS.len - 1)));
1920
- return @as(u32, GRAYSCALE_CHARS[index]);
1921
- }
1922
-
1923
- pub fn drawGrayscaleBuffer(
1924
- self: *OptimizedBuffer,
1925
- posX: i32,
1926
- posY: i32,
1927
- intensities: [*]const f32,
1928
- srcWidth: u32,
1929
- srcHeight: u32,
1930
- fgColor: ?RGBA,
1931
- bgColor: ?RGBA,
1932
- ) void {
1933
- const bg = bgColor orelse RGBA{ 0.0, 0.0, 0.0, 0.0 };
1934
- if (srcWidth == 0 or srcHeight == 0) return;
1935
- if (posX >= @as(i32, @intCast(self.width)) or posY >= @as(i32, @intCast(self.height))) return;
1936
-
1937
- const startX: u32 = if (posX < 0) @intCast(-posX) else 0;
1938
- const startY: u32 = if (posY < 0) @intCast(-posY) else 0;
1939
-
1940
- const destStartX: u32 = if (posX < 0) 0 else @intCast(posX);
1941
- const destStartY: u32 = if (posY < 0) 0 else @intCast(posY);
1942
-
1943
- if (startX >= srcWidth or startY >= srcHeight) return;
1944
-
1945
- const visibleWidth = @min(srcWidth - startX, self.width - destStartX);
1946
- const visibleHeight = @min(srcHeight - startY, self.height - destStartY);
1947
-
1948
- if (visibleWidth == 0 or visibleHeight == 0) return;
1949
-
1950
- const baseFg = fgColor orelse RGBA{ 1.0, 1.0, 1.0, 1.0 };
1951
-
1952
- const opacity = self.getCurrentOpacity();
1953
- const graphemeAware = self.grapheme_tracker.hasAny();
1954
- const linkAware = self.link_tracker.hasAny();
1955
-
1956
- var srcY: u32 = startY;
1957
- var destY: u32 = destStartY;
1958
- while (srcY < startY + visibleHeight) : ({
1959
- srcY += 1;
1960
- destY += 1;
1961
- }) {
1962
- var srcX: u32 = startX;
1963
- var destX: u32 = destStartX;
1964
- while (srcX < startX + visibleWidth) : ({
1965
- srcX += 1;
1966
- destX += 1;
1967
- }) {
1968
- if (!self.isPointInScissor(@intCast(destX), @intCast(destY))) continue;
1969
-
1970
- const srcIndex = srcY * srcWidth + srcX;
1971
- const intensity = intensities[srcIndex];
1972
-
1973
- if (intensity < 0.01) continue;
1974
-
1975
- const char = getGrayscaleChar(intensity);
1976
-
1977
- const gray = @min(@max(intensity, 0.0), 1.0);
1978
- const fg: RGBA = .{ baseFg[0], baseFg[1], baseFg[2], gray * baseFg[3] * opacity };
1979
-
1980
- if (graphemeAware or linkAware) {
1981
- self.setCellWithAlphaBlending(destX, destY, char, fg, bg, 0) catch {};
1982
- } else {
1983
- self.setCellWithAlphaBlendingRaw(destX, destY, char, fg, bg, 0) catch {};
1984
- }
1985
- }
1986
- }
1987
- }
1988
-
1989
- pub fn drawGrayscaleBufferSupersampled(
1990
- self: *OptimizedBuffer,
1991
- posX: i32,
1992
- posY: i32,
1993
- intensities: [*]const f32,
1994
- srcWidth: u32,
1995
- srcHeight: u32,
1996
- fgColor: ?RGBA,
1997
- bgColor: ?RGBA,
1998
- ) void {
1999
- const bg = bgColor orelse RGBA{ 0.0, 0.0, 0.0, 0.0 };
2000
- const termWidth = srcWidth / 2;
2001
- const termHeight = srcHeight / 2;
2002
-
2003
- if (termWidth == 0 or termHeight == 0) return;
2004
- if (posX >= @as(i32, @intCast(self.width)) or posY >= @as(i32, @intCast(self.height))) return;
2005
-
2006
- const startX: u32 = if (posX < 0) @intCast(-posX) else 0;
2007
- const startY: u32 = if (posY < 0) @intCast(-posY) else 0;
2008
-
2009
- const destStartX: u32 = if (posX < 0) 0 else @intCast(posX);
2010
- const destStartY: u32 = if (posY < 0) 0 else @intCast(posY);
2011
-
2012
- if (startX >= termWidth or startY >= termHeight) return;
2013
-
2014
- const visibleWidth = @min(termWidth - startX, self.width - destStartX);
2015
- const visibleHeight = @min(termHeight - startY, self.height - destStartY);
2016
-
2017
- if (visibleWidth == 0 or visibleHeight == 0) return;
2018
-
2019
- const baseFg = fgColor orelse RGBA{ 1.0, 1.0, 1.0, 1.0 };
2020
-
2021
- const opacity = self.getCurrentOpacity();
2022
- const graphemeAware = self.grapheme_tracker.hasAny();
2023
- const linkAware = self.link_tracker.hasAny();
2024
-
2025
- const maxIdx = srcHeight * srcWidth;
2026
- var cellY: u32 = startY;
2027
- var destY: u32 = destStartY;
2028
- while (cellY < startY + visibleHeight) : ({
2029
- cellY += 1;
2030
- destY += 1;
2031
- }) {
2032
- var cellX: u32 = startX;
2033
- var destX: u32 = destStartX;
2034
- while (cellX < startX + visibleWidth) : ({
2035
- cellX += 1;
2036
- destX += 1;
2037
- }) {
2038
- if (!self.isPointInScissor(@intCast(destX), @intCast(destY))) continue;
2039
-
2040
- const qx = cellX * 2;
2041
- const qy = cellY * 2;
2042
-
2043
- const tlIdx = qy * srcWidth + qx;
2044
- const trIdx = qy * srcWidth + qx + 1;
2045
- const blIdx = (qy + 1) * srcWidth + qx;
2046
- const brIdx = (qy + 1) * srcWidth + qx + 1;
2047
-
2048
- const tl: f32 = if (tlIdx < maxIdx) intensities[tlIdx] else 0.0;
2049
- const tr: f32 = if (trIdx < maxIdx and qx + 1 < srcWidth) intensities[trIdx] else 0.0;
2050
- const bl: f32 = if (blIdx < maxIdx and qy + 1 < srcHeight) intensities[blIdx] else 0.0;
2051
- const br: f32 = if (brIdx < maxIdx and qx + 1 < srcWidth and qy + 1 < srcHeight) intensities[brIdx] else 0.0;
2052
-
2053
- const avgIntensity = (tl + tr + bl + br) / 4.0;
2054
-
2055
- if (avgIntensity < 0.01) continue;
2056
-
2057
- const char = getGrayscaleChar(avgIntensity);
2058
-
2059
- const gray = @min(@max(avgIntensity, 0.0), 1.0);
2060
- const fg: RGBA = .{ baseFg[0], baseFg[1], baseFg[2], gray * baseFg[3] * opacity };
2061
-
2062
- if (graphemeAware or linkAware) {
2063
- self.setCellWithAlphaBlending(destX, destY, char, fg, bg, 0) catch {};
2064
- } else {
2065
- self.setCellWithAlphaBlendingRaw(destX, destY, char, fg, bg, 0) catch {};
2066
- }
2067
- }
2068
- }
2069
- }
2070
- };
2071
-
2072
- fn getPixelColor(idx: usize, data: [*]const u8, dataLen: usize, bgra: bool) RGBA {
2073
- if (idx + 3 >= dataLen) {
2074
- return .{ 1.0, 0.0, 1.0, 0.0 }; // Return Transparent Magenta for out-of-bounds
2075
- }
2076
- var rByte: u8 = undefined;
2077
- var gByte: u8 = undefined;
2078
- var bByte: u8 = undefined;
2079
- var aByte: u8 = undefined;
2080
-
2081
- if (bgra) {
2082
- bByte = data[idx];
2083
- gByte = data[idx + 1];
2084
- rByte = data[idx + 2];
2085
- aByte = data[idx + 3];
2086
- } else { // Assume RGBA
2087
- rByte = data[idx];
2088
- gByte = data[idx + 1];
2089
- bByte = data[idx + 2];
2090
- aByte = data[idx + 3];
2091
- }
2092
-
2093
- return .{
2094
- @as(f32, @floatFromInt(rByte)) * INV_255,
2095
- @as(f32, @floatFromInt(gByte)) * INV_255,
2096
- @as(f32, @floatFromInt(bByte)) * INV_255,
2097
- @as(f32, @floatFromInt(aByte)) * INV_255,
2098
- };
2099
- }
2100
-
2101
- const quadrantChars = [_]u32{
2102
- 32, // 0000
2103
- 0x2597, // 0001 BR ░
2104
- 0x2596, // 0010 BL ░
2105
- 0x2584, // 0011 Lower Half Block ▄
2106
- 0x259D, // 0100 TR ░
2107
- 0x2590, // 0101 Right Half Block ▐
2108
- 0x259E, // 0110 TR+BL ░
2109
- 0x259F, // 0111 TR+BL+BR ░
2110
- 0x2598, // 1000 TL ░
2111
- 0x259A, // 1001 TL+BR ░
2112
- 0x258C, // 1010 Left Half Block ▌
2113
- 0x2599, // 1011 TL+BL+BR ░
2114
- 0x2580, // 1100 Upper Half Block ▀
2115
- 0x259C, // 1101 TL+TR+BR ░
2116
- 0x259B, // 1110 TL+TR+BL ░
2117
- 0x2588, // 1111 Full Block █
2118
- };
2119
-
2120
- fn colorDistance(a: RGBA, b: RGBA) f32 {
2121
- const dr = a[0] - b[0];
2122
- const dg = a[1] - b[1];
2123
- const db = a[2] - b[2];
2124
- return dr * dr + dg * dg + db * db;
2125
- }
2126
-
2127
- fn closestColorIndex(pixel: RGBA, candidates: [2]RGBA) u1 {
2128
- return if (colorDistance(pixel, candidates[0]) <= colorDistance(pixel, candidates[1])) 0 else 1;
2129
- }
2130
-
2131
- fn averageColorRgba(pixels: []const RGBA) RGBA {
2132
- if (pixels.len == 0) return .{ 0.0, 0.0, 0.0, 0.0 };
2133
-
2134
- var sumR: f32 = 0.0;
2135
- var sumG: f32 = 0.0;
2136
- var sumB: f32 = 0.0;
2137
- var sumA: f32 = 0.0;
2138
-
2139
- for (pixels) |p| {
2140
- sumR += p[0];
2141
- sumG += p[1];
2142
- sumB += p[2];
2143
- sumA += p[3];
2144
- }
2145
-
2146
- const len = @as(f32, @floatFromInt(pixels.len));
2147
- return .{ sumR / len, sumG / len, sumB / len, sumA / len };
2148
- }
2149
-
2150
- fn luminance(color: RGBA) f32 {
2151
- return 0.2126 * color[0] + 0.7152 * color[1] + 0.0722 * color[2];
2152
- }
2153
-
2154
- pub const QuadrantResult = struct {
2155
- char: u32,
2156
- fg: RGBA,
2157
- bg: RGBA,
2158
- };
2159
-
2160
- // Calculate the quadrant block character and colors from RGBA pixels
2161
- fn renderQuadrantBlock(pixels: [4]RGBA) QuadrantResult {
2162
- // 1. Find the most different pair of pixels
2163
- var p_idxA: u3 = 0;
2164
- var p_idxB: u3 = 1;
2165
- var maxDist = colorDistance(pixels[0], pixels[1]);
2166
-
2167
- inline for (0..4) |i| {
2168
- inline for ((i + 1)..4) |j| {
2169
- const dist = colorDistance(pixels[i], pixels[j]);
2170
- if (dist > maxDist) {
2171
- p_idxA = @intCast(i);
2172
- p_idxB = @intCast(j);
2173
- maxDist = dist;
2174
- }
2175
- }
2176
- }
2177
- const p_candA = pixels[p_idxA];
2178
- const p_candB = pixels[p_idxB];
2179
-
2180
- // 2. Determine chosen_dark_color and chosen_light_color based on luminance
2181
- var chosen_dark_color: RGBA = undefined;
2182
- var chosen_light_color: RGBA = undefined;
2183
-
2184
- if (luminance(p_candA) <= luminance(p_candB)) {
2185
- chosen_dark_color = p_candA;
2186
- chosen_light_color = p_candB;
2187
- } else {
2188
- chosen_dark_color = p_candB;
2189
- chosen_light_color = p_candA;
2190
- }
2191
-
2192
- // 3. Classify quadrants and build quadrantBits
2193
- var quadrantBits: u4 = 0;
2194
- const bitValues = [_]u4{ 8, 4, 2, 1 };
2195
-
2196
- inline for (0..4) |i| {
2197
- const pixelRgba = pixels[i];
2198
- if (closestColorIndex(pixelRgba, .{ chosen_dark_color, chosen_light_color }) == 0) {
2199
- quadrantBits |= bitValues[i];
2200
- }
2201
- }
2202
-
2203
- // 4. Construct Result
2204
- if (quadrantBits == 0) { // All light
2205
- return QuadrantResult{
2206
- .char = 32,
2207
- .fg = chosen_dark_color,
2208
- .bg = averageColorRgba(pixels[0..4]),
2209
- };
2210
- } else if (quadrantBits == 15) { // All dark
2211
- return QuadrantResult{
2212
- .char = quadrantChars[15],
2213
- .fg = averageColorRgba(pixels[0..4]),
2214
- .bg = chosen_light_color,
2215
- };
2216
- } else { // Mixed pattern
2217
- return QuadrantResult{
2218
- .char = quadrantChars[quadrantBits],
2219
- .fg = chosen_dark_color,
2220
- .bg = chosen_light_color,
2221
- };
2222
- }
2223
- }