xbintsc 0.5.38 → 0.5.42

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 (132) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/codegen/generator/module.js +23 -7
  3. package/dist/src/codegen/generator/module.js.map +1 -1
  4. package/dist/src/driver/version.d.ts +1 -1
  5. package/dist/src/driver/version.js +1 -1
  6. package/dist/src/extensions/gui/html.js +9 -2
  7. package/dist/src/extensions/gui/html.js.map +1 -1
  8. package/dist/tests/codegen/closures.test.js +7 -0
  9. package/dist/tests/codegen/closures.test.js.map +1 -1
  10. package/dist/tests/e2e/compile-objects-classes.test.js +31 -0
  11. package/dist/tests/e2e/compile-objects-classes.test.js.map +1 -1
  12. package/dist/tests/e2e/gui-canvas-state.test.d.ts +9 -0
  13. package/dist/tests/e2e/gui-canvas-state.test.js +297 -0
  14. package/dist/tests/e2e/gui-canvas-state.test.js.map +1 -0
  15. package/dist/tests/e2e/gui-data-url.test.d.ts +10 -0
  16. package/dist/tests/e2e/gui-data-url.test.js +127 -0
  17. package/dist/tests/e2e/gui-data-url.test.js.map +1 -0
  18. package/dist/tests/e2e/gui-draw-perf.test.d.ts +22 -0
  19. package/dist/tests/e2e/gui-draw-perf.test.js +405 -0
  20. package/dist/tests/e2e/gui-draw-perf.test.js.map +1 -0
  21. package/dist/tests/e2e/gui-effects.test.js +96 -0
  22. package/dist/tests/e2e/gui-effects.test.js.map +1 -1
  23. package/dist/tests/e2e/gui-paint.test.js +77 -0
  24. package/dist/tests/e2e/gui-paint.test.js.map +1 -1
  25. package/dist/tests/e2e/gui-perf.test.d.ts +23 -0
  26. package/dist/tests/e2e/gui-perf.test.js +283 -0
  27. package/dist/tests/e2e/gui-perf.test.js.map +1 -0
  28. package/dist/tests/e2e/gui-script-import-order.test.js +9 -1
  29. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  30. package/dist/tests/e2e/gui-webgl.test.js +4 -4
  31. package/dist/tests/e2e/gui-webgl.test.js.map +1 -1
  32. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.d.ts +8 -0
  33. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js +336 -0
  34. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +1 -0
  35. package/dist/tests/extensions/gui.test.js +23 -0
  36. package/dist/tests/extensions/gui.test.js.map +1 -1
  37. package/doc/gui/api.md +28 -2
  38. package/doc/gui/canvas.md +114 -27
  39. package/doc/gui/known-issues.md +47 -19
  40. package/doc/gui/media.md +11 -2
  41. package/doc/gui/rendering.md +120 -18
  42. package/doc/gui/runtime.md +18 -3
  43. package/doc/gui/webgl.md +38 -19
  44. package/doc/gui.md +38 -5
  45. package/doc/zh-CN/gui/api.md +25 -2
  46. package/doc/zh-CN/gui/canvas.md +97 -26
  47. package/doc/zh-CN/gui/known-issues.md +32 -12
  48. package/doc/zh-CN/gui/media.md +9 -2
  49. package/doc/zh-CN/gui/rendering.md +89 -11
  50. package/doc/zh-CN/gui/runtime.md +13 -3
  51. package/doc/zh-CN/gui/webgl.md +27 -5
  52. package/doc/zh-CN/gui.md +25 -2
  53. package/package.json +7 -7
  54. package/runtime/ext_gui/browser.cpp +3 -1
  55. package/runtime/ext_gui/canvas.cpp +135 -89
  56. package/runtime/ext_gui/canvas.h +116 -0
  57. package/runtime/ext_gui/canvas_clip.cpp +143 -0
  58. package/runtime/ext_gui/canvas_composite.cpp +241 -0
  59. package/runtime/ext_gui/canvas_coverage.cpp +241 -0
  60. package/runtime/ext_gui/canvas_gradient.cpp +41 -4
  61. package/runtime/ext_gui/canvas_image_data.cpp +4 -0
  62. package/runtime/ext_gui/canvas_internal.h +34 -9
  63. package/runtime/ext_gui/canvas_raster.cpp +207 -178
  64. package/runtime/ext_gui/canvas_shadow.cpp +161 -0
  65. package/runtime/ext_gui/canvas_stroke.cpp +37 -9
  66. package/runtime/ext_gui/canvas_style.cpp +59 -0
  67. package/runtime/ext_gui/canvas_text.cpp +140 -0
  68. package/runtime/ext_gui/css.h +7 -0
  69. package/runtime/ext_gui/css_sheet.cpp +22 -0
  70. package/runtime/ext_gui/document.cpp +25 -1
  71. package/runtime/ext_gui/document.h +8 -0
  72. package/runtime/ext_gui/dom_api_canvas.cpp +48 -15
  73. package/runtime/ext_gui/dom_api_canvas_image.cpp +9 -0
  74. package/runtime/ext_gui/dom_api_canvas_state.cpp +217 -0
  75. package/runtime/ext_gui/dom_api_internal.h +36 -37
  76. package/runtime/ext_gui/dom_api_offscreen.cpp +213 -0
  77. package/runtime/ext_gui/dom_api_proto.cpp +18 -0
  78. package/runtime/ext_gui/dom_api_webgl.cpp +19 -9
  79. package/runtime/ext_gui/dom_api_webgl_internal.h +58 -0
  80. package/runtime/ext_gui/dom_api_webgl_source.cpp +18 -0
  81. package/runtime/ext_gui/gradient_values.cpp +56 -49
  82. package/runtime/ext_gui/gui.cpp +5 -0
  83. package/runtime/ext_gui/gui_canvas.cpp +4 -0
  84. package/runtime/ext_gui/gui_engine.h +30 -0
  85. package/runtime/ext_gui/gui_events.cpp +185 -58
  86. package/runtime/ext_gui/gui_media.cpp +3 -0
  87. package/runtime/ext_gui/image.cpp +225 -1
  88. package/runtime/ext_gui/image.h +6 -4
  89. package/runtime/ext_gui/image_bitmap_browser.cpp +11 -0
  90. package/runtime/ext_gui/layout.cpp +4 -0
  91. package/runtime/ext_gui/paint.cpp +72 -3
  92. package/runtime/ext_gui/paint.h +11 -1
  93. package/runtime/ext_gui/paint_background.cpp +110 -12
  94. package/runtime/ext_gui/paint_effects.cpp +2 -1
  95. package/runtime/ext_gui/paint_internal.h +5 -1
  96. package/runtime/ext_gui/perf.cpp +171 -0
  97. package/runtime/ext_gui/perf.h +127 -0
  98. package/runtime/ext_gui/renderer.cpp +190 -54
  99. package/runtime/ext_gui/renderer.h +52 -14
  100. package/runtime/ext_gui/renderer_image.cpp +135 -65
  101. package/runtime/ext_gui/renderer_internal.h +41 -3
  102. package/runtime/ext_gui/renderer_layer.cpp +552 -0
  103. package/runtime/ext_gui/renderer_text.cpp +24 -12
  104. package/runtime/ext_gui/style.cpp +101 -30
  105. package/runtime/ext_gui/style.h +35 -4
  106. package/runtime/ext_gui/style_print.cpp +15 -1
  107. package/runtime/ext_gui/style_values.cpp +10 -0
  108. package/runtime/ext_gui/text.cpp +23 -4
  109. package/runtime/ext_gui/text.h +29 -0
  110. package/runtime/ext_gui/text_cache.cpp +118 -0
  111. package/runtime/ext_gui/text_glyph_cache.cpp +100 -0
  112. package/runtime/ext_gui/webgl.h +48 -146
  113. package/runtime/ext_gui/webgl_blit.cpp +12 -1
  114. package/runtime/ext_gui/webgl_blit_depth.cpp +270 -0
  115. package/runtime/ext_gui/webgl_buffer.cpp +59 -1
  116. package/runtime/ext_gui/webgl_buffer_objects.cpp +106 -9
  117. package/runtime/ext_gui/webgl_clear.cpp +3 -0
  118. package/runtime/ext_gui/webgl_context.cpp +29 -3
  119. package/runtime/ext_gui/webgl_draw.cpp +75 -270
  120. package/runtime/ext_gui/webgl_draw_buffers.cpp +1 -0
  121. package/runtime/ext_gui/webgl_draw_setup.cpp +321 -0
  122. package/runtime/ext_gui/webgl_draw_state.h +130 -0
  123. package/runtime/ext_gui/webgl_glsl_scan.cpp +3 -8
  124. package/runtime/ext_gui/webgl_objects.h +148 -0
  125. package/runtime/ext_gui/webgl_pipeline.cpp +8 -8
  126. package/runtime/ext_gui/webgl_program_internal.h +35 -0
  127. package/runtime/ext_gui/webgl_shader.cpp +22 -30
  128. package/runtime/ext_gui/webgl_webgl2.cpp +36 -0
  129. package/runtime/ext_gui/window.cpp +56 -1
  130. package/src/codegen/generator/module.ts +22 -7
  131. package/src/driver/version.ts +1 -1
  132. package/src/extensions/gui/html.ts +9 -2
@@ -52,6 +52,18 @@ struct CanvasPoint {
52
52
  float y = 0.0f;
53
53
  };
54
54
 
55
+ /** One clip region: per-pixel coverage (0 = fully clipped, 255 = kept). A clip
56
+ * is baked into device space when `clip()` runs, exactly like a browser. */
57
+ struct CanvasClipMask {
58
+ std::vector<uint8_t> data;
59
+ int width = 0;
60
+ int height = 0;
61
+ uint8_t at(int x, int y) const {
62
+ if (x < 0 || y < 0 || x >= width || y >= height) return 0;
63
+ return data[(size_t)y * (size_t)width + (size_t)x];
64
+ }
65
+ };
66
+
55
67
  /** One subpath of the current path (device space). */
56
68
  using CanvasSubpath = std::vector<CanvasPoint>;
57
69
 
@@ -94,6 +106,36 @@ class CanvasGradient {
94
106
  class CanvasGradient;
95
107
  class CanvasPattern;
96
108
 
109
+ /** `globalCompositeOperation` values, in the order the DOM layer maps names. */
110
+ enum CanvasComposite {
111
+ kCanvasCompositeSourceOver = 0,
112
+ kCanvasCompositeSourceIn,
113
+ kCanvasCompositeSourceOut,
114
+ kCanvasCompositeSourceAtop,
115
+ kCanvasCompositeDestinationOver,
116
+ kCanvasCompositeDestinationIn,
117
+ kCanvasCompositeDestinationOut,
118
+ kCanvasCompositeDestinationAtop,
119
+ kCanvasCompositeLighter,
120
+ kCanvasCompositeCopy,
121
+ kCanvasCompositeXor,
122
+ kCanvasCompositeMultiply,
123
+ kCanvasCompositeScreen,
124
+ kCanvasCompositeOverlay,
125
+ kCanvasCompositeDarken,
126
+ kCanvasCompositeLighten,
127
+ kCanvasCompositeColorDodge,
128
+ kCanvasCompositeColorBurn,
129
+ kCanvasCompositeHardLight,
130
+ kCanvasCompositeSoftLight,
131
+ kCanvasCompositeDifference,
132
+ kCanvasCompositeExclusion,
133
+ kCanvasCompositeHue,
134
+ kCanvasCompositeSaturation,
135
+ kCanvasCompositeColor,
136
+ kCanvasCompositeLuminosity,
137
+ };
138
+
97
139
  /** What a fill or stroke paints with: a solid colour, a gradient sampled through
98
140
  * the inverse of the current transform, or a repeating image pattern. `color`
99
141
  * points into the canvas state and stays valid for one draw call. */
@@ -102,6 +144,8 @@ struct CanvasPaint {
102
144
  const CanvasGradient *gradient = nullptr;
103
145
  const CanvasPattern *pattern = nullptr;
104
146
  CanvasTransform inverse;
147
+ /** `globalCompositeOperation` as the `CanvasComposite` enum value. */
148
+ int composite = 0;
105
149
  };
106
150
 
107
151
  /**
@@ -136,6 +180,15 @@ class Canvas2D {
136
180
  uint64_t id() const { return id_; }
137
181
  /** Bumped by every draw; the renderer re-uploads when it changes. */
138
182
  uint64_t generation() const { return generation_; }
183
+ /** Device-space bounds of the pixels changed since the renderer last uploaded
184
+ * them. Empty means nothing visible changed (a no-op or off-canvas draw), so
185
+ * the renderer can skip the upload entirely. */
186
+ struct CanvasDirtyRect {
187
+ int x0 = 0, y0 = 0, x1 = 0, y1 = 0;
188
+ bool empty() const { return x1 <= x0 || y1 <= y0; }
189
+ };
190
+ const CanvasDirtyRect &pending_dirty() const { return dirty_; }
191
+ void clear_dirty() { dirty_ = CanvasDirtyRect(); }
139
192
  const Image &image() const { return image_; }
140
193
  /** Resize the bitmap, discarding its contents (the DOM `canvas.width = …`
141
194
  * behaviour). */
@@ -187,6 +240,24 @@ class Canvas2D {
187
240
  float line_dash_offset() const { return state_.line_dash_offset; }
188
241
  void set_global_alpha(float alpha);
189
242
  float global_alpha() const { return state_.global_alpha; }
243
+ void set_global_composite_operation(const std::string &value);
244
+ const std::string &global_composite_operation() const {
245
+ return state_.global_composite_string;
246
+ }
247
+ void set_shadow_color(const std::string &css);
248
+ const std::string &shadow_color() const { return state_.shadow_color_string; }
249
+ void set_shadow_blur(float blur);
250
+ float shadow_blur() const { return state_.shadow_blur; }
251
+ void set_shadow_offset_x(float offset) { state_.shadow_offset_x = offset; }
252
+ float shadow_offset_x() const { return state_.shadow_offset_x; }
253
+ void set_shadow_offset_y(float offset) { state_.shadow_offset_y = offset; }
254
+ float shadow_offset_y() const { return state_.shadow_offset_y; }
255
+ void set_image_smoothing_enabled(bool enabled) { state_.image_smoothing = enabled; }
256
+ bool image_smoothing_enabled() const { return state_.image_smoothing; }
257
+ void set_image_smoothing_quality(const std::string &value);
258
+ const std::string &image_smoothing_quality() const {
259
+ return state_.image_smoothing_quality;
260
+ }
190
261
  void set_font(const std::string &font);
191
262
  const std::string &font() const { return state_.font_string; }
192
263
  void set_text_align(const std::string &value);
@@ -226,6 +297,15 @@ class Canvas2D {
226
297
  * space and are transformed with the current state, like the current path. */
227
298
  void fill_paths(const std::vector<CanvasSubpath> &paths, bool even_odd);
228
299
  void stroke_paths(const std::vector<CanvasSubpath> &paths);
300
+ /** Intersect the clip region with the current path (or a `Path2D`). */
301
+ void clip(bool even_odd = false);
302
+ void clip_paths(const std::vector<CanvasSubpath> &paths, bool even_odd);
303
+ /** Hit testing in the current user space. */
304
+ bool is_point_in_path(float x, float y, bool even_odd = false) const;
305
+ bool is_point_in_paths(const std::vector<CanvasSubpath> &paths, float x, float y,
306
+ bool even_odd) const;
307
+ bool is_point_in_stroke(float x, float y) const;
308
+ bool is_point_in_stroke_paths(const std::vector<CanvasSubpath> &paths, float x, float y) const;
229
309
 
230
310
  /* -- rectangles -------------------------------------------------------- */
231
311
  void clear_rect(float x, float y, float width, float height);
@@ -286,10 +366,31 @@ class Canvas2D {
286
366
  uint64_t stroke_pattern = 0;
287
367
  std::string line_cap_string = "butt";
288
368
  std::string line_join_string = "miter";
369
+ int global_composite = kCanvasCompositeSourceOver;
370
+ std::string global_composite_string = "source-over";
371
+ float shadow_color[4] = {0.0f, 0.0f, 0.0f, 0.0f};
372
+ std::string shadow_color_string = "rgba(0, 0, 0, 0)";
373
+ float shadow_blur = 0.0f;
374
+ float shadow_offset_x = 0.0f;
375
+ float shadow_offset_y = 0.0f;
376
+ bool image_smoothing = true;
377
+ std::string image_smoothing_quality = "low";
378
+ /** Current clip region; `save`/`restore` share it (a later `clip` installs a
379
+ * new mask instead of mutating the saved one). */
380
+ std::shared_ptr<CanvasClipMask> clip;
289
381
  };
290
382
 
291
383
  /** Transform a user-space point into device space. */
292
384
  void to_device(float x, float y, float *outX, float *outY) const;
385
+ /** Union a device-space rectangle into the pending upload region. */
386
+ void mark_dirty(float x0, float y0, float x1, float y1);
387
+ /** Union the bounds of a device-space path (grown by `expand`) into the
388
+ * pending upload region. */
389
+ void mark_path_dirty(const std::vector<CanvasSubpath> &subpaths, float expand);
390
+ void mark_all_dirty();
391
+ /** How far a stroke can reach outside its centre line (half width, miter
392
+ * limit and a pixel of anti-aliasing slack). */
393
+ float stroke_extent() const;
293
394
  /** The current path transformed into device space. */
294
395
  std::vector<CanvasSubpath> device_subpaths() const;
295
396
  /** Any user-space path transformed into device space. */
@@ -314,11 +415,26 @@ class Canvas2D {
314
415
  std::vector<CanvasSubpath> *out) const;
315
416
  void paint_text(const std::string &text, float x, float y, const CanvasPaint &paint,
316
417
  bool stroke, float max_width);
418
+ /** True when the current state paints a shadow for a drawing operation. */
419
+ bool shadow_active() const;
420
+ /** Redirect the next draw into a scratch layer so its alpha can be turned
421
+ * into a shadow. Returns false while already inside the shadow layer (or when
422
+ * no shadow is active), in which case the caller draws directly. */
423
+ bool begin_shadow_layer();
424
+ void end_shadow_layer();
425
+ /** The current clip coverage, or null when the canvas is unclipped. */
426
+ const CanvasClipMask *clip_mask() const;
427
+ void composite_layer(const Image &layer);
428
+ void composite_shadow_layer(const Image &layer);
317
429
 
318
430
  Image image_;
431
+ /** Scratch bitmap a draw is rendered into while its shadow is built. */
432
+ Image shadow_layer_;
433
+ bool in_shadow_layer_ = false;
319
434
  bool alpha_ = true;
320
435
  uint64_t id_ = 0;
321
436
  uint64_t generation_ = 0;
437
+ CanvasDirtyRect dirty_;
322
438
  State state_;
323
439
  std::vector<State> stack_;
324
440
  std::vector<CanvasSubpath> path_;
@@ -0,0 +1,143 @@
1
+ /*
2
+ * xbintsc GUI engine — Canvas2D clipping and hit testing.
3
+ *
4
+ * `clip()` rasterises the current path into a device-space coverage mask and
5
+ * intersects it with the region already in effect. `save`/`restore` share the
6
+ * mask through a `shared_ptr`; a later `clip()` installs a new mask, so a saved
7
+ * state can never observe a mutated region.
8
+ *
9
+ * `isPointInPath`/`isPointInStroke` run against the same device-space geometry
10
+ * the rasteriser would use, which keeps them consistent with anti-aliasing and
11
+ * the current transform.
12
+ */
13
+
14
+ #include "canvas_internal.h"
15
+
16
+ #include <algorithm>
17
+ #include <cmath>
18
+ #include <memory>
19
+ #include <vector>
20
+
21
+ namespace xtgui {
22
+
23
+ namespace {
24
+
25
+ bool point_in_loops(const std::vector<CanvasSubpath> &loops, float x, float y, bool even_odd) {
26
+ int winding = 0;
27
+ int crossings = 0;
28
+ for (const CanvasSubpath &loop : loops) {
29
+ if (loop.size() < 2) continue;
30
+ for (size_t i = 0; i < loop.size(); i++) {
31
+ const CanvasPoint &p0 = loop[i];
32
+ const CanvasPoint &p1 = loop[(i + 1) % loop.size()];
33
+ if ((p0.y > y) == (p1.y > y)) continue;
34
+ const float intersection = p0.x + (y - p0.y) * (p1.x - p0.x) / (p1.y - p0.y);
35
+ if (intersection <= x) continue;
36
+ if (p1.y > p0.y) winding++;
37
+ else winding--;
38
+ crossings++;
39
+ }
40
+ }
41
+ return even_odd ? (crossings % 2) != 0 : winding != 0;
42
+ }
43
+
44
+ float distance_to_segment(const CanvasPoint &p, const CanvasPoint &a, const CanvasPoint &b) {
45
+ const float dx = b.x - a.x;
46
+ const float dy = b.y - a.y;
47
+ const float lengthSquared = dx * dx + dy * dy;
48
+ if (lengthSquared <= 1e-9f) return std::hypot(p.x - a.x, p.y - a.y);
49
+ float t = ((p.x - a.x) * dx + (p.y - a.y) * dy) / lengthSquared;
50
+ t = std::max(0.0f, std::min(1.0f, t));
51
+ const float cx = a.x + t * dx;
52
+ const float cy = a.y + t * dy;
53
+ return std::hypot(p.x - cx, p.y - cy);
54
+ }
55
+
56
+ bool point_in_stroke_loops(const std::vector<CanvasSubpath> &loops, float x, float y, float half) {
57
+ const CanvasPoint point{x, y};
58
+ for (const CanvasSubpath &loop : loops) {
59
+ if (loop.size() < 2) continue;
60
+ for (size_t i = 0; i + 1 < loop.size(); i++) {
61
+ if (distance_to_segment(point, loop[i], loop[i + 1]) <= half) return true;
62
+ }
63
+ if (loop.front().x != loop.back().x || loop.front().y != loop.back().y) {
64
+ if (distance_to_segment(point, loop.back(), loop.front()) <= half) return true;
65
+ }
66
+ }
67
+ return false;
68
+ }
69
+
70
+ } // namespace
71
+
72
+ void Canvas2D::clip(bool even_odd) { clip_paths(path_, even_odd); }
73
+
74
+ void Canvas2D::clip_paths(const std::vector<CanvasSubpath> &paths, bool even_odd) {
75
+ std::vector<CanvasSubpath> subpaths = device_subpaths_of(paths);
76
+ std::vector<CanvasSubpath> loops;
77
+ for (CanvasSubpath &subpath : subpaths) {
78
+ if (subpath.size() < 3) continue;
79
+ loops.push_back(std::move(subpath));
80
+ }
81
+ auto mask = std::make_shared<CanvasClipMask>();
82
+ mask->width = image_.width;
83
+ mask->height = image_.height;
84
+ mask->data.assign((size_t)image_.width * (size_t)image_.height, 0);
85
+ canvas_detail::CoverageGrid coverage;
86
+ canvas_detail::compute_coverage(image_.width, image_.height, loops, even_odd, &coverage);
87
+ if (!coverage.empty()) {
88
+ const int rows = coverage.y1 - coverage.y0;
89
+ for (int r = 0; r < rows; r++) {
90
+ const uint32_t begin = coverage.row_offset[(size_t)r];
91
+ const uint32_t end = coverage.row_offset[(size_t)r + 1];
92
+ if (begin >= end) continue;
93
+ const int y = coverage.y0 + r;
94
+ const int rx = coverage.row_x[(size_t)r];
95
+ const float *values = coverage.values.data() + begin;
96
+ uint8_t *out = mask->data.data() + (size_t)y * (size_t)image_.width + (size_t)rx;
97
+ for (uint32_t i = begin; i < end; i++) {
98
+ float value = std::max(0.0f, std::min(1.0f, values[i - begin]));
99
+ out[i - begin] = (uint8_t)(value * 255.0f + 0.5f);
100
+ }
101
+ }
102
+ }
103
+ if (state_.clip) {
104
+ const CanvasClipMask &previous = *state_.clip;
105
+ if (previous.width == mask->width && previous.height == mask->height) {
106
+ for (size_t i = 0; i < mask->data.size(); i++) {
107
+ mask->data[i] = (uint8_t)((unsigned)mask->data[i] * (unsigned)previous.data[i] / 255u);
108
+ }
109
+ }
110
+ }
111
+ state_.clip = std::move(mask);
112
+ /* A clip changes future draws only; it touches no pixels, so it must not
113
+ * schedule a texture upload. */
114
+ }
115
+
116
+ bool Canvas2D::is_point_in_path(float x, float y, bool even_odd) const {
117
+ return is_point_in_paths(path_, x, y, even_odd);
118
+ }
119
+
120
+ bool Canvas2D::is_point_in_paths(const std::vector<CanvasSubpath> &paths, float x, float y,
121
+ bool even_odd) const {
122
+ float deviceX = 0.0f;
123
+ float deviceY = 0.0f;
124
+ to_device(x, y, &deviceX, &deviceY);
125
+ return point_in_loops(device_subpaths_of(paths), deviceX, deviceY, even_odd);
126
+ }
127
+
128
+ bool Canvas2D::is_point_in_stroke(float x, float y) const {
129
+ return is_point_in_stroke_paths(path_, x, y);
130
+ }
131
+
132
+ bool Canvas2D::is_point_in_stroke_paths(const std::vector<CanvasSubpath> &paths, float x,
133
+ float y) const {
134
+ float scale = state_.transform.scale();
135
+ if (!(scale > 0.0f)) scale = 1.0f;
136
+ const float half = std::max(0.5f, state_.line_width * scale * 0.5f);
137
+ float deviceX = 0.0f;
138
+ float deviceY = 0.0f;
139
+ to_device(x, y, &deviceX, &deviceY);
140
+ return point_in_stroke_loops(device_subpaths_of(paths), deviceX, deviceY, half);
141
+ }
142
+
143
+ } // namespace xtgui
@@ -0,0 +1,241 @@
1
+ /*
2
+ * xbintsc GUI engine — Canvas2D `globalCompositeOperation`.
3
+ *
4
+ * Every raster path funnels its per-pixel work through `blend_pixel_mode`,
5
+ * which implements the Porter-Duff operators and the separable/non-separable
6
+ * blend modes from the CSS compositing specification. Colours are kept
7
+ * non-premultiplied (the canvas bitmap is RGBA8 straight alpha); the composite
8
+ * is evaluated in premultiplied form and divided back out.
9
+ */
10
+
11
+ #include "canvas_internal.h"
12
+
13
+ #include <algorithm>
14
+ #include <cmath>
15
+
16
+ namespace xtgui {
17
+ namespace canvas_detail {
18
+
19
+ namespace {
20
+
21
+ float clamp_unit(float value) { return value < 0.0f ? 0.0f : (value > 1.0f ? 1.0f : value); }
22
+
23
+ float channel_blend(int mode, float s, float d) {
24
+ switch (mode) {
25
+ case kCanvasCompositeMultiply:
26
+ return s * d;
27
+ case kCanvasCompositeScreen:
28
+ return s + d - s * d;
29
+ case kCanvasCompositeOverlay:
30
+ return d <= 0.5f ? 2.0f * s * d : 1.0f - 2.0f * (1.0f - s) * (1.0f - d);
31
+ case kCanvasCompositeDarken:
32
+ return std::min(s, d);
33
+ case kCanvasCompositeLighten:
34
+ return std::max(s, d);
35
+ case kCanvasCompositeColorDodge:
36
+ if (d <= 0.0f) return 0.0f;
37
+ if (s >= 1.0f) return 1.0f;
38
+ return std::min(1.0f, d / (1.0f - s));
39
+ case kCanvasCompositeColorBurn:
40
+ if (d >= 1.0f) return 1.0f;
41
+ if (s <= 0.0f) return 0.0f;
42
+ return 1.0f - std::min(1.0f, (1.0f - d) / s);
43
+ case kCanvasCompositeHardLight:
44
+ return s <= 0.5f ? 2.0f * s * d : 1.0f - 2.0f * (1.0f - s) * (1.0f - d);
45
+ case kCanvasCompositeSoftLight: {
46
+ if (s <= 0.5f) return d - (1.0f - 2.0f * s) * d * (1.0f - d);
47
+ float d2 = d <= 0.25f ? ((16.0f * d - 12.0f) * d + 4.0f) * d : std::sqrt(d);
48
+ return d + (2.0f * s - 1.0f) * (d2 - d);
49
+ }
50
+ case kCanvasCompositeDifference:
51
+ return std::fabs(s - d);
52
+ case kCanvasCompositeExclusion:
53
+ return s + d - 2.0f * s * d;
54
+ default:
55
+ return s;
56
+ }
57
+ }
58
+
59
+ float luminance(const float c[3]) { return 0.3f * c[0] + 0.59f * c[1] + 0.11f * c[2]; }
60
+
61
+ void clip_color(float c[3]) {
62
+ const float l = luminance(c);
63
+ float n = std::min(c[0], std::min(c[1], c[2]));
64
+ float x = std::max(c[0], std::max(c[1], c[2]));
65
+ if (n < 0.0f) {
66
+ const float scale = l / (l - n);
67
+ for (int i = 0; i < 3; i++) c[i] = l + (c[i] - l) * scale;
68
+ }
69
+ if (x > 1.0f) {
70
+ const float scale = (1.0f - l) / (x - l);
71
+ for (int i = 0; i < 3; i++) c[i] = l + (c[i] - l) * scale;
72
+ }
73
+ }
74
+
75
+ void set_luminance(float c[3], float l) {
76
+ const float delta = l - luminance(c);
77
+ for (int i = 0; i < 3; i++) c[i] += delta;
78
+ clip_color(c);
79
+ }
80
+
81
+ float saturation(const float c[3]) {
82
+ return std::max(c[0], std::max(c[1], c[2])) - std::min(c[0], std::min(c[1], c[2]));
83
+ }
84
+
85
+ void set_saturation(float c[3], float s) {
86
+ int maxIndex = 0;
87
+ int minIndex = 0;
88
+ for (int i = 1; i < 3; i++) {
89
+ if (c[i] > c[maxIndex]) maxIndex = i;
90
+ if (c[i] < c[minIndex]) minIndex = i;
91
+ }
92
+ if (maxIndex == minIndex) return;
93
+ float out[3] = {0.0f, 0.0f, 0.0f};
94
+ out[maxIndex] = s;
95
+ out[minIndex] = 0.0f;
96
+ int middle = 3 - maxIndex - minIndex;
97
+ out[middle] = s * (c[middle] - c[minIndex]) / (c[maxIndex] - c[minIndex]);
98
+ for (int i = 0; i < 3; i++) c[i] = out[i];
99
+ }
100
+
101
+ /** Non-separable modes (`hue`…`luminosity`). */
102
+ void non_separable_blend(int mode, const float s[3], const float d[3], float out[3]) {
103
+ float source[3] = {s[0], s[1], s[2]};
104
+ float dest[3] = {d[0], d[1], d[2]};
105
+ switch (mode) {
106
+ case kCanvasCompositeHue:
107
+ set_saturation(source, saturation(dest));
108
+ set_luminance(source, luminance(dest));
109
+ break;
110
+ case kCanvasCompositeSaturation:
111
+ set_saturation(dest, saturation(source));
112
+ set_luminance(dest, luminance(dest));
113
+ for (int i = 0; i < 3; i++) source[i] = dest[i];
114
+ break;
115
+ case kCanvasCompositeColor:
116
+ set_luminance(source, luminance(dest));
117
+ break;
118
+ case kCanvasCompositeLuminosity:
119
+ set_luminance(dest, luminance(source));
120
+ for (int i = 0; i < 3; i++) source[i] = dest[i];
121
+ break;
122
+ default:
123
+ break;
124
+ }
125
+ for (int i = 0; i < 3; i++) out[i] = clamp_unit(source[i]);
126
+ }
127
+
128
+ bool is_non_separable(int mode) {
129
+ return mode == kCanvasCompositeHue || mode == kCanvasCompositeSaturation ||
130
+ mode == kCanvasCompositeColor || mode == kCanvasCompositeLuminosity;
131
+ }
132
+
133
+ bool is_separable_blend(int mode) {
134
+ return mode >= kCanvasCompositeMultiply && mode <= kCanvasCompositeExclusion && !is_non_separable(mode);
135
+ }
136
+
137
+ } // namespace
138
+
139
+ void blend_pixel_mode(unsigned char *pixel, const float color[4], float coverage, float alpha,
140
+ int composite) {
141
+ if (composite == kCanvasCompositeSourceOver && alpha >= 0.999f && coverage >= 0.999f &&
142
+ color[3] >= 0.999f) {
143
+ pixel[0] = (unsigned char)(clamp_unit(color[0]) * 255.0f + 0.5f);
144
+ pixel[1] = (unsigned char)(clamp_unit(color[1]) * 255.0f + 0.5f);
145
+ pixel[2] = (unsigned char)(clamp_unit(color[2]) * 255.0f + 0.5f);
146
+ pixel[3] = 255;
147
+ return;
148
+ }
149
+
150
+ float src[3] = {clamp_unit(color[0]), clamp_unit(color[1]), clamp_unit(color[2])};
151
+ const float sa = clamp_unit(color[3] * alpha);
152
+ float dst[3] = {pixel[0] / 255.0f, pixel[1] / 255.0f, pixel[2] / 255.0f};
153
+ const float da = pixel[3] / 255.0f;
154
+
155
+ float outA = 0.0f;
156
+ float outC[3] = {0.0f, 0.0f, 0.0f}; // premultiplied
157
+
158
+ if (is_separable_blend(composite) || is_non_separable(composite)) {
159
+ float blended[3];
160
+ if (is_non_separable(composite)) {
161
+ non_separable_blend(composite, src, dst, blended);
162
+ } else {
163
+ for (int i = 0; i < 3; i++) blended[i] = channel_blend(composite, src[i], dst[i]);
164
+ }
165
+ outA = sa + da - sa * da;
166
+ for (int i = 0; i < 3; i++) {
167
+ outC[i] = (1.0f - da) * src[i] * sa + (1.0f - sa) * dst[i] * da + sa * da * blended[i];
168
+ }
169
+ } else {
170
+ switch (composite) {
171
+ case kCanvasCompositeSourceIn:
172
+ outA = sa * da;
173
+ for (int i = 0; i < 3; i++) outC[i] = src[i] * sa * da;
174
+ break;
175
+ case kCanvasCompositeSourceOut:
176
+ outA = sa * (1.0f - da);
177
+ for (int i = 0; i < 3; i++) outC[i] = src[i] * sa * (1.0f - da);
178
+ break;
179
+ case kCanvasCompositeSourceAtop:
180
+ outA = da;
181
+ for (int i = 0; i < 3; i++) outC[i] = src[i] * sa * da + dst[i] * da * (1.0f - sa);
182
+ break;
183
+ case kCanvasCompositeDestinationOver:
184
+ outA = da + sa * (1.0f - da);
185
+ for (int i = 0; i < 3; i++) outC[i] = dst[i] * da + src[i] * sa * (1.0f - da);
186
+ break;
187
+ case kCanvasCompositeDestinationIn:
188
+ outA = da * sa;
189
+ for (int i = 0; i < 3; i++) outC[i] = dst[i] * da * sa;
190
+ break;
191
+ case kCanvasCompositeDestinationOut:
192
+ outA = da * (1.0f - sa);
193
+ for (int i = 0; i < 3; i++) outC[i] = dst[i] * da * (1.0f - sa);
194
+ break;
195
+ case kCanvasCompositeDestinationAtop:
196
+ outA = sa;
197
+ for (int i = 0; i < 3; i++) outC[i] = dst[i] * da * sa + src[i] * sa * (1.0f - da);
198
+ break;
199
+ case kCanvasCompositeLighter:
200
+ outA = std::min(1.0f, sa + da);
201
+ for (int i = 0; i < 3; i++) outC[i] = std::min(1.0f, src[i] * sa + dst[i] * da);
202
+ break;
203
+ case kCanvasCompositeCopy:
204
+ outA = sa;
205
+ for (int i = 0; i < 3; i++) outC[i] = src[i] * sa;
206
+ break;
207
+ case kCanvasCompositeXor:
208
+ outA = sa * (1.0f - da) + da * (1.0f - sa);
209
+ for (int i = 0; i < 3; i++) {
210
+ outC[i] = src[i] * sa * (1.0f - da) + dst[i] * da * (1.0f - sa);
211
+ }
212
+ break;
213
+ default: // source-over
214
+ outA = sa + da * (1.0f - sa);
215
+ for (int i = 0; i < 3; i++) outC[i] = src[i] * sa + dst[i] * da * (1.0f - sa);
216
+ break;
217
+ }
218
+ }
219
+
220
+ /* Partial coverage (anti-aliasing or a clip edge) interpolates between the
221
+ * untouched destination and the full composite, which is correct for every
222
+ * operator, including `destination-in`/`copy`. */
223
+ if (coverage < 0.999f) {
224
+ const float c = clamp_unit(coverage);
225
+ outA = da * (1.0f - c) + outA * c;
226
+ for (int i = 0; i < 3; i++) outC[i] = dst[i] * da * (1.0f - c) + outC[i] * c;
227
+ }
228
+
229
+ if (outA <= 0.00001f) {
230
+ pixel[0] = pixel[1] = pixel[2] = pixel[3] = 0;
231
+ return;
232
+ }
233
+ const float inverse = 1.0f / outA;
234
+ for (int i = 0; i < 3; i++) {
235
+ pixel[i] = (unsigned char)(clamp_unit(outC[i] * inverse) * 255.0f + 0.5f);
236
+ }
237
+ pixel[3] = (unsigned char)(clamp_unit(outA) * 255.0f + 0.5f);
238
+ }
239
+
240
+ } // namespace canvas_detail
241
+ } // namespace xtgui