xbintsc 0.6.10 → 0.6.13

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 (204) hide show
  1. package/dist/src/ast/expressions.d.ts +6 -0
  2. package/dist/src/checker/globals.js +2 -1
  3. package/dist/src/checker/globals.js.map +1 -1
  4. package/dist/src/checker/type-infer.js +1 -1
  5. package/dist/src/checker/type-infer.js.map +1 -1
  6. package/dist/src/cli/help.d.ts +1 -1
  7. package/dist/src/codegen/generator/calls/literals.js +4 -1
  8. package/dist/src/codegen/generator/calls/literals.js.map +1 -1
  9. package/dist/src/codegen/generator/expressions/assignment.js +3 -1
  10. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  11. package/dist/src/codegen/generator/expressions/operators.js +2 -6
  12. package/dist/src/codegen/generator/expressions/operators.js.map +1 -1
  13. package/dist/src/codegen/generator/expressions/primary.d.ts +9 -0
  14. package/dist/src/codegen/generator/expressions/primary.js +44 -7
  15. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  16. package/dist/src/codegen/generator/state.js +3 -0
  17. package/dist/src/codegen/generator/state.js.map +1 -1
  18. package/dist/src/codegen/generator/tables.js +3 -0
  19. package/dist/src/codegen/generator/tables.js.map +1 -1
  20. package/dist/src/driver/bundler/commonjs.js +1 -1
  21. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  22. package/dist/src/driver/bundler/symbols.js +6 -0
  23. package/dist/src/driver/bundler/symbols.js.map +1 -1
  24. package/dist/src/driver/compiler.js +1 -1
  25. package/dist/src/driver/compiler.js.map +1 -1
  26. package/dist/src/driver/runtime-objects.d.ts +1 -1
  27. package/dist/src/driver/runtime-objects.js +2 -0
  28. package/dist/src/driver/runtime-objects.js.map +1 -1
  29. package/dist/src/driver/version.d.ts +1 -1
  30. package/dist/src/driver/version.js +1 -1
  31. package/dist/src/extensions/node/crypto/index.js +12 -1
  32. package/dist/src/extensions/node/crypto/index.js.map +1 -1
  33. package/dist/src/extensions/node/index.js +2 -0
  34. package/dist/src/extensions/node/index.js.map +1 -1
  35. package/dist/src/extensions/node/readline/index.d.ts +12 -0
  36. package/dist/src/extensions/node/readline/index.js +27 -0
  37. package/dist/src/extensions/node/readline/index.js.map +1 -0
  38. package/dist/src/parser/declarations/functions.js +6 -0
  39. package/dist/src/parser/declarations/functions.js.map +1 -1
  40. package/dist/src/parser/literals.js +1 -1
  41. package/dist/src/parser/literals.js.map +1 -1
  42. package/dist/tests/e2e/compile-builtins.test.js +26 -0
  43. package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
  44. package/dist/tests/e2e/compile-modules.test.js +22 -0
  45. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  46. package/dist/tests/e2e/compile-node.test.js +37 -1
  47. package/dist/tests/e2e/compile-node.test.js.map +1 -1
  48. package/dist/tests/e2e/crypto-random.test.d.ts +9 -0
  49. package/dist/tests/e2e/crypto-random.test.js +63 -0
  50. package/dist/tests/e2e/crypto-random.test.js.map +1 -0
  51. package/dist/tests/e2e/date-parse.test.js +44 -1
  52. package/dist/tests/e2e/date-parse.test.js.map +1 -1
  53. package/dist/tests/e2e/gui-browser.test.js +61 -0
  54. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  55. package/dist/tests/e2e/gui-css-coverage.test.js +40 -1
  56. package/dist/tests/e2e/gui-css-coverage.test.js.map +1 -1
  57. package/dist/tests/e2e/gui-css-features.test.js +100 -0
  58. package/dist/tests/e2e/gui-css-features.test.js.map +1 -1
  59. package/dist/tests/e2e/gui-dom.test.js +63 -0
  60. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  61. package/dist/tests/e2e/gui-errors.test.d.ts +11 -0
  62. package/dist/tests/e2e/gui-errors.test.js +68 -0
  63. package/dist/tests/e2e/gui-errors.test.js.map +1 -0
  64. package/dist/tests/e2e/gui-form.test.d.ts +9 -0
  65. package/dist/tests/e2e/gui-form.test.js +359 -0
  66. package/dist/tests/e2e/gui-form.test.js.map +1 -0
  67. package/dist/tests/e2e/gui-script-import-order.test.js +6 -1
  68. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  69. package/dist/tests/e2e/gui-style.test.js +82 -0
  70. package/dist/tests/e2e/gui-style.test.js.map +1 -1
  71. package/dist/tests/e2e/gui-unsupported.test.js +4 -3
  72. package/dist/tests/e2e/gui-unsupported.test.js.map +1 -1
  73. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js +7 -1
  74. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +1 -1
  75. package/dist/tests/e2e/harness.d.ts +4 -0
  76. package/dist/tests/e2e/harness.js +2 -0
  77. package/dist/tests/e2e/harness.js.map +1 -1
  78. package/dist/tests/e2e/json-parse-errors.test.d.ts +9 -0
  79. package/dist/tests/e2e/json-parse-errors.test.js +48 -0
  80. package/dist/tests/e2e/json-parse-errors.test.js.map +1 -0
  81. package/dist/tests/e2e/readline.test.d.ts +7 -0
  82. package/dist/tests/e2e/readline.test.js +73 -0
  83. package/dist/tests/e2e/readline.test.js.map +1 -0
  84. package/doc/ai/language-support.md +2 -1
  85. package/doc/ai/zh-CN/language-support.md +2 -1
  86. package/doc/gui/api.md +24 -3
  87. package/doc/gui/dom.md +28 -14
  88. package/doc/gui/input.md +74 -3
  89. package/doc/gui/known-issues.md +2 -1
  90. package/doc/gui/layout.md +5 -1
  91. package/doc/implemented/node/crypto.md +8 -0
  92. package/doc/implemented/node/fs.md +1 -1
  93. package/doc/implemented/node/readline.md +34 -0
  94. package/doc/implemented/node.md +1 -0
  95. package/doc/implemented/stdlib.md +10 -2
  96. package/doc/unimplemented/node/modules.md +2 -3
  97. package/doc/unimplemented/node.md +2 -2
  98. package/doc/zh-CN/gui/api.md +20 -3
  99. package/doc/zh-CN/gui/dom.md +23 -13
  100. package/doc/zh-CN/gui/input.md +65 -4
  101. package/doc/zh-CN/gui/known-issues.md +2 -1
  102. package/doc/zh-CN/gui/layout.md +4 -1
  103. package/doc/zh-CN/implemented/node/crypto.md +6 -0
  104. package/doc/zh-CN/implemented/node/fs.md +1 -1
  105. package/doc/zh-CN/implemented/node/readline.md +33 -0
  106. package/doc/zh-CN/implemented/node.md +1 -0
  107. package/doc/zh-CN/implemented/stdlib.md +10 -2
  108. package/doc/zh-CN/unimplemented/node/modules.md +1 -2
  109. package/doc/zh-CN/unimplemented/node.md +2 -2
  110. package/package.json +7 -7
  111. package/runtime/ext_gui/browser_abort.cpp +2 -2
  112. package/runtime/ext_gui/browser_loop.cpp +1 -0
  113. package/runtime/ext_gui/browser_media.cpp +1 -1
  114. package/runtime/ext_gui/css_match.cpp +21 -1
  115. package/runtime/ext_gui/css_selector.cpp +2 -2
  116. package/runtime/ext_gui/document.cpp +12 -0
  117. package/runtime/ext_gui/dom.h +41 -2
  118. package/runtime/ext_gui/dom_api_canvas_image.cpp +2 -1
  119. package/runtime/ext_gui/dom_api_document.cpp +28 -0
  120. package/runtime/ext_gui/dom_api_events.cpp +18 -15
  121. package/runtime/ext_gui/dom_api_form.cpp +567 -0
  122. package/runtime/ext_gui/dom_api_internal.h +48 -214
  123. package/runtime/ext_gui/dom_api_mutation.cpp +1 -1
  124. package/runtime/ext_gui/dom_api_node.cpp +28 -15
  125. package/runtime/ext_gui/dom_api_node_read.cpp +28 -1
  126. package/runtime/ext_gui/dom_api_proto.cpp +47 -0
  127. package/runtime/ext_gui/dom_api_webgl_methods.h +217 -0
  128. package/runtime/ext_gui/dom_parse.cpp +114 -1
  129. package/runtime/ext_gui/error_report.cpp +166 -0
  130. package/runtime/ext_gui/form_focus.cpp +293 -0
  131. package/runtime/ext_gui/form_input.cpp +448 -0
  132. package/runtime/ext_gui/form_internal.h +30 -0
  133. package/runtime/ext_gui/form_shared.cpp +126 -0
  134. package/runtime/ext_gui/form_state.cpp +536 -0
  135. package/runtime/ext_gui/form_state.h +97 -0
  136. package/runtime/ext_gui/gui.cpp +16 -0
  137. package/runtime/ext_gui/gui_engine.h +53 -4
  138. package/runtime/ext_gui/gui_events.cpp +67 -49
  139. package/runtime/ext_gui/layout.cpp +6 -0
  140. package/runtime/ext_gui/layout.h +4 -0
  141. package/runtime/ext_gui/layout_flow.cpp +23 -1
  142. package/runtime/ext_gui/layout_inline.cpp +46 -14
  143. package/runtime/ext_gui/paint.cpp +26 -1
  144. package/runtime/ext_gui/paint_form.cpp +389 -0
  145. package/runtime/ext_gui/paint_internal.h +6 -0
  146. package/runtime/ext_gui/script.cpp +1 -1
  147. package/runtime/ext_gui/style.cpp +1 -0
  148. package/runtime/ext_gui/style.h +13 -0
  149. package/runtime/ext_gui/style_print.cpp +129 -0
  150. package/runtime/ext_gui/style_text.cpp +79 -0
  151. package/runtime/ext_gui/style_ua.cpp +42 -0
  152. package/runtime/ext_gui/warnings.cpp +5 -7
  153. package/runtime/ext_gui/webgl_texture.cpp +0 -8
  154. package/runtime/ext_gui/window.cpp +52 -62
  155. package/runtime/ext_gui/window_diagnostics.cpp +58 -0
  156. package/runtime/ext_gui/window_emit.cpp +51 -0
  157. package/runtime/ext_node/crypto/crypto.c +3 -2
  158. package/runtime/ext_node/crypto/crypto_hash.h +13 -0
  159. package/runtime/ext_node/crypto/crypto_random.c +339 -0
  160. package/runtime/ext_node/fs/async_worker.c +1 -1
  161. package/runtime/ext_node/fs/fs_ops.c +1 -1
  162. package/runtime/ext_node/fs/parts/platform.inc +67 -0
  163. package/runtime/ext_node/readline/readline.c +572 -0
  164. package/runtime/rt.h +42 -0
  165. package/runtime/rt_internal.h +3 -2
  166. package/runtime/xt_crypto.c +132 -0
  167. package/runtime/xt_io/exception.inc +29 -6
  168. package/runtime/xt_loop.c +36 -1
  169. package/runtime/xt_promise.c +26 -2
  170. package/runtime/xt_random.c +143 -0
  171. package/runtime/xt_regex/matcher.inc +9 -28
  172. package/runtime/xt_regex/parser.inc +65 -2
  173. package/runtime/xt_regex/types.inc +11 -1
  174. package/runtime/xt_regex.h +6 -2
  175. package/runtime/xt_stdlib/dispatch.inc +2 -0
  176. package/runtime/xt_stdlib/math.inc +1 -1
  177. package/runtime/xt_stdlib2/date.inc +206 -6
  178. package/runtime/xt_stdlib2/json.inc +4 -546
  179. package/runtime/xt_stdlib2/json_parse.inc +347 -0
  180. package/runtime/xt_stdlib2/json_stringify.inc +368 -0
  181. package/runtime/xt_stdlib2/regexp-match.inc +6 -11
  182. package/runtime/xt_stdlib2.c +0 -4
  183. package/runtime/xt_typed_array/elements.inc +2 -6
  184. package/runtime/xt_url.c +0 -6
  185. package/runtime/xt_url_object.c +1 -1
  186. package/src/ast/expressions.ts +6 -0
  187. package/src/checker/globals.ts +2 -0
  188. package/src/checker/type-infer.ts +1 -1
  189. package/src/codegen/generator/calls/literals.ts +4 -1
  190. package/src/codegen/generator/expressions/assignment.ts +3 -1
  191. package/src/codegen/generator/expressions/operators.ts +2 -6
  192. package/src/codegen/generator/expressions/primary.ts +58 -6
  193. package/src/codegen/generator/state.ts +3 -0
  194. package/src/codegen/generator/tables.ts +3 -0
  195. package/src/driver/bundler/commonjs.ts +1 -1
  196. package/src/driver/bundler/symbols.ts +6 -0
  197. package/src/driver/compiler.ts +1 -1
  198. package/src/driver/runtime-objects.ts +2 -0
  199. package/src/driver/version.ts +1 -1
  200. package/src/extensions/node/crypto/index.ts +12 -1
  201. package/src/extensions/node/index.ts +2 -0
  202. package/src/extensions/node/readline/index.ts +31 -0
  203. package/src/parser/declarations/functions.ts +6 -0
  204. package/src/parser/literals.ts +1 -1
@@ -0,0 +1,389 @@
1
+ /*
2
+ * xbintsc GUI engine — form-control painting.
3
+ *
4
+ * The control's value, selection, caret and type-specific decorations
5
+ * (checkbox tick, radio dot, range thumb, select arrow, colour swatch) are
6
+ * painted from `Node::form`. Text is clipped to the content box so a long value
7
+ * cannot bleed over the border.
8
+ */
9
+
10
+ #include "paint_internal.h"
11
+
12
+ #include <algorithm>
13
+ #include <cmath>
14
+ #include <cstdio>
15
+ #include <cstdlib>
16
+ #include <string>
17
+ #include <vector>
18
+
19
+ #include "form_state.h"
20
+ #include "layout.h"
21
+
22
+ namespace xtgui {
23
+
24
+ namespace {
25
+
26
+ FontSpec specOf(const XtStyle &style) {
27
+ FontSpec spec;
28
+ spec.family = style.font_family;
29
+ spec.pixel_size = style.font_size;
30
+ spec.weight = style.font_weight;
31
+ spec.italic = style.font_style == FontStyle::Italic;
32
+ spec.letter_spacing = style.letter_spacing.unit == Unit::Px ? style.letter_spacing.value : 0.0f;
33
+ return spec;
34
+ }
35
+
36
+ float lineHeightOf(const XtStyle &style) { return style.font_size * 1.2f; }
37
+
38
+ void addRun(DisplayList &out, float x, float baseline, const std::string &text,
39
+ const XtStyle &style, const Color &color, const LayoutBox &box, const PaintClip &clip,
40
+ const paint_detail::FilterChain &chain, float viewportWidth, float viewportHeight) {
41
+ if (text.empty() || color.a <= 0.0f) return;
42
+ FontSpec spec = specOf(style);
43
+ float width = xt_text_measure_width(text, spec);
44
+ float height = lineHeightOf(style);
45
+ paint_detail::addTextRun(out, x, baseline, width, height, text, spec, color, box.world, clip,
46
+ style, chain, viewportWidth, viewportHeight);
47
+ }
48
+
49
+ float runWidth(const std::string &text, const XtStyle &style) {
50
+ return xt_text_measure_width(text, specOf(style));
51
+ }
52
+
53
+ float prefixWidth(const std::string &text, size_t units, const XtStyle &style) {
54
+ return runWidth(xt_form_utf16_slice(text, 0, units), style);
55
+ }
56
+
57
+ PaintClip intersectContent(const LayoutBox &box, const PaintClip &clip) {
58
+ PaintClip result = clip;
59
+ float x = box.content_x;
60
+ float y = box.content_y;
61
+ float width = box.content_width;
62
+ float height = box.content_height;
63
+ if (width < 0.0f) width = 0.0f;
64
+ if (height < 0.0f) height = 0.0f;
65
+ if (!clip.active) {
66
+ result.active = true;
67
+ result.x = x;
68
+ result.y = y;
69
+ result.width = width;
70
+ result.height = height;
71
+ return result;
72
+ }
73
+ float x0 = std::max(x, clip.x);
74
+ float y0 = std::max(y, clip.y);
75
+ float x1 = std::min(x + width, clip.x + clip.width);
76
+ float y1 = std::min(y + height, clip.y + clip.height);
77
+ result.x = x0;
78
+ result.y = y0;
79
+ result.width = std::max(0.0f, x1 - x0);
80
+ result.height = std::max(0.0f, y1 - y0);
81
+ return result;
82
+ }
83
+
84
+ void addRect(DisplayList &out, float x, float y, float width, float height, float radius,
85
+ const Color &color, const PaintClip &clip, const LayoutBox &box,
86
+ const paint_detail::FilterChain &chain) {
87
+ if (width <= 0.0f || height <= 0.0f || color.a <= 0.0f) return;
88
+ paint_detail::addRectFiltered(out, x, y, width, height, radius, color, clip, box.world, chain);
89
+ }
90
+
91
+ Color withAlpha(Color color, float alpha) {
92
+ color.a *= alpha;
93
+ return color;
94
+ }
95
+
96
+ Color shade(const XtStyle &style, bool disabled, float alpha) {
97
+ Color color = style.color;
98
+ if (disabled) color = Color{0.55f, 0.55f, 0.55f, color.a};
99
+ color.a *= alpha;
100
+ return color;
101
+ }
102
+
103
+ /** Split `text` into visual lines (`\n`) and report each line's UTF-16 range. */
104
+ struct VisualLine {
105
+ size_t start;
106
+ size_t end;
107
+ std::string text;
108
+ };
109
+
110
+ std::vector<VisualLine> splitLines(const std::string &text) {
111
+ std::vector<VisualLine> lines;
112
+ size_t byte_start = 0;
113
+ size_t units = 0;
114
+ size_t line_start_units = 0;
115
+ size_t offset = 0;
116
+ while (offset <= text.size()) {
117
+ if (offset == text.size() || text[offset] == '\n') {
118
+ VisualLine line;
119
+ line.start = line_start_units;
120
+ line.end = units;
121
+ line.text = text.substr(byte_start, offset - byte_start);
122
+ lines.push_back(line);
123
+ byte_start = offset + 1;
124
+ line_start_units = units;
125
+ if (offset == text.size()) break;
126
+ offset++;
127
+ continue;
128
+ }
129
+ size_t width = 1;
130
+ uint32_t code = (unsigned char)text[offset];
131
+ if (code < 0x80) width = 1;
132
+ else if ((code & 0xE0) == 0xC0) width = 2;
133
+ else if ((code & 0xF0) == 0xE0) width = 3;
134
+ else width = 4;
135
+ units += (code >= 0xF0) ? 2 : 1;
136
+ offset += width;
137
+ }
138
+ return lines;
139
+ }
140
+
141
+ void paintTextControl(const LayoutBox &box, const XtStyle &style, const FormState &state,
142
+ DisplayList &out, const PaintClip &clip,
143
+ const paint_detail::FilterChain &chain, float viewportWidth,
144
+ float viewportHeight) {
145
+ bool disabled = xt_form_is_disabled(box.node);
146
+ bool password = xt_form_type(box.node) == "password";
147
+ std::string display = state.value;
148
+ size_t caret = (size_t)state.selection_end;
149
+ if (state.composing) {
150
+ size_t length = xt_form_utf16_length(state.value);
151
+ size_t start = std::min((size_t)state.selection_start, length);
152
+ size_t end = std::min((size_t)state.selection_end, length);
153
+ display = xt_form_utf16_slice(state.value, 0, start) + state.composition +
154
+ xt_form_utf16_slice(state.value, end, length);
155
+ caret = start + xt_form_utf16_length(state.composition);
156
+ }
157
+ if (password) display = xt_form_mask(display);
158
+ size_t display_units = xt_form_utf16_length(display);
159
+ if (caret > display_units) caret = display_units;
160
+
161
+ float line_height = lineHeightOf(style);
162
+ FontSpec spec = specOf(style);
163
+ float ascent = 0.0f;
164
+ xt_text_metrics(spec, &ascent, nullptr, nullptr);
165
+ if (ascent <= 0.0f) ascent = style.font_size * 0.8f;
166
+
167
+ bool placeholder = display.empty() && !state.composing && !state.focused;
168
+ if (state.value.empty() && !state.composing) {
169
+ const std::string *hint = box.node->attr("placeholder");
170
+ if (hint != nullptr && !hint->empty() && state.value.empty()) {
171
+ std::string text = *hint;
172
+ float baseline = box.content_y + (box.content_height - line_height) / 2.0f + ascent;
173
+ addRun(out, box.content_x, baseline, text, style, shade(style, disabled, 0.45f), box, clip,
174
+ chain, viewportWidth, viewportHeight);
175
+ placeholder = true;
176
+ }
177
+ }
178
+
179
+ bool multiline = xt_form_is_textarea(box.node);
180
+ std::vector<VisualLine> lines = multiline ? splitLines(display) : std::vector<VisualLine>();
181
+ if (!multiline) {
182
+ VisualLine line;
183
+ line.start = 0;
184
+ line.end = display_units;
185
+ line.text = display;
186
+ lines.push_back(line);
187
+ }
188
+
189
+ size_t selection_start = (size_t)state.selection_start;
190
+ size_t selection_end = (size_t)state.selection_end;
191
+ if (selection_end < selection_start) std::swap(selection_start, selection_end);
192
+ bool has_selection = state.focused && selection_end > selection_start && !state.composing;
193
+ Color highlight{0.26f, 0.55f, 1.0f, 0.35f};
194
+
195
+ if (!placeholder) {
196
+ for (size_t index = 0; index < lines.size(); index++) {
197
+ const VisualLine &line = lines[index];
198
+ float y = multiline ? box.content_y + (float)index * line_height
199
+ : box.content_y + (box.content_height - line_height) / 2.0f;
200
+ if (multiline && y >= box.content_y + box.content_height) break;
201
+ float baseline = y + ascent;
202
+ if (has_selection) {
203
+ size_t start = std::max(selection_start, line.start);
204
+ size_t end = std::min(selection_end, line.end);
205
+ if (end > start) {
206
+ float x0 = box.content_x + prefixWidth(line.text, start - line.start, style);
207
+ float x1 = box.content_x + prefixWidth(line.text, end - line.start, style);
208
+ addRect(out, x0, y, x1 - x0, line_height, 0.0f, highlight, clip, box, chain);
209
+ }
210
+ }
211
+ addRun(out, box.content_x, baseline, line.text, style, shade(style, disabled, 1.0f), box,
212
+ clip, chain, viewportWidth, viewportHeight);
213
+ if (state.composing) {
214
+ float x0 = box.content_x;
215
+ float x1 = box.content_x + runWidth(line.text, style);
216
+ addRect(out, x0, y + line_height - 1.0f, x1 - x0, 1.0f, 0.0f,
217
+ shade(style, disabled, 0.9f), clip, box, chain);
218
+ }
219
+ }
220
+ }
221
+
222
+ if (state.focused && !state.composing && !has_selection) {
223
+ size_t line_index = 0;
224
+ size_t column = caret;
225
+ for (size_t index = 0; index < lines.size(); index++) {
226
+ if (caret >= lines[index].start && caret <= lines[index].end) {
227
+ line_index = index;
228
+ column = caret - lines[index].start;
229
+ break;
230
+ }
231
+ }
232
+ float y = multiline ? box.content_y + (float)line_index * line_height
233
+ : box.content_y + (box.content_height - line_height) / 2.0f;
234
+ float x = box.content_x + prefixWidth(lines[line_index].text, column, style);
235
+ addRect(out, x, y + 1.0f, 1.5f, line_height - 2.0f, 0.0f, shade(style, disabled, 1.0f), clip,
236
+ box, chain);
237
+ }
238
+ }
239
+
240
+ void paintCheckbox(const LayoutBox &box, const XtStyle &style, const FormState &state,
241
+ DisplayList &out, const PaintClip &clip, const paint_detail::FilterChain &chain,
242
+ bool radio) {
243
+ bool disabled = xt_form_is_disabled(box.node);
244
+ float size = std::max(13.0f, style.font_size * 0.9f);
245
+ float x = box.content_x + (box.content_width - size) / 2.0f;
246
+ float y = box.content_y + (box.content_height - size) / 2.0f;
247
+ Color border = disabled ? Color{0.6f, 0.6f, 0.6f, 1.0f} : Color{0.34f, 0.34f, 0.34f, 1.0f};
248
+ Color fill = disabled ? Color{0.94f, 0.94f, 0.94f, 1.0f} : Color{1.0f, 1.0f, 1.0f, 1.0f};
249
+ float radius = radio ? size / 2.0f : 2.0f;
250
+ addRect(out, x, y, size, size, radius, border, clip, box, chain);
251
+ addRect(out, x + 1.0f, y + 1.0f, size - 2.0f, size - 2.0f, std::max(0.0f, radius - 1.0f), fill,
252
+ clip, box, chain);
253
+ if (state.checked) {
254
+ Color accent = disabled ? Color{0.6f, 0.6f, 0.6f, 1.0f} : Color{0.17f, 0.42f, 0.69f, 1.0f};
255
+ if (radio) {
256
+ float inner = size - 8.0f;
257
+ addRect(out, x + 4.0f, y + 4.0f, inner, inner, inner / 2.0f, accent, clip, box, chain);
258
+ } else {
259
+ addRect(out, x + 3.0f, y + 3.0f, size - 6.0f, size - 6.0f, 1.0f, accent, clip, box, chain);
260
+ }
261
+ }
262
+ }
263
+
264
+ void paintRange(const LayoutBox &box, const XtStyle &style, const FormState &state,
265
+ DisplayList &out, const PaintClip &clip, const paint_detail::FilterChain &chain) {
266
+ bool disabled = xt_form_is_disabled(box.node);
267
+ double minimum = 0.0;
268
+ double maximum = 100.0;
269
+ const std::string *min_attr = box.node->attr("min");
270
+ const std::string *max_attr = box.node->attr("max");
271
+ if (min_attr != nullptr) minimum = std::strtod(min_attr->c_str(), nullptr);
272
+ if (max_attr != nullptr) maximum = std::strtod(max_attr->c_str(), nullptr);
273
+ if (maximum <= minimum) maximum = minimum + 1.0;
274
+ double value = std::strtod(state.value.c_str(), nullptr);
275
+ float fraction = (float)((value - minimum) / (maximum - minimum));
276
+ if (fraction < 0.0f) fraction = 0.0f;
277
+ if (fraction > 1.0f) fraction = 1.0f;
278
+ float track_height = 4.0f;
279
+ float track_y = box.content_y + (box.content_height - track_height) / 2.0f;
280
+ float thumb = 12.0f;
281
+ float travel = std::max(0.0f, box.content_width - thumb);
282
+ float thumb_x = box.content_x + fraction * travel;
283
+ float thumb_y = box.content_y + (box.content_height - thumb) / 2.0f;
284
+ Color track = disabled ? Color{0.8f, 0.8f, 0.8f, 1.0f} : Color{0.78f, 0.78f, 0.78f, 1.0f};
285
+ Color accent = disabled ? Color{0.6f, 0.6f, 0.6f, 1.0f} : Color{0.17f, 0.42f, 0.69f, 1.0f};
286
+ addRect(out, box.content_x, track_y, box.content_width, track_height, 2.0f, track, clip, box,
287
+ chain);
288
+ addRect(out, box.content_x, track_y, thumb_x - box.content_x + thumb / 2.0f, track_height, 2.0f,
289
+ accent, clip, box, chain);
290
+ addRect(out, thumb_x, thumb_y, thumb, thumb, thumb / 2.0f, accent, clip, box, chain);
291
+ }
292
+
293
+ void paintColorSwatch(const LayoutBox &box, const XtStyle &style, const FormState &state,
294
+ DisplayList &out, const PaintClip &clip,
295
+ const paint_detail::FilterChain &chain) {
296
+ float size = std::min(box.content_width, box.content_height);
297
+ if (size <= 0.0f) return;
298
+ float x = box.content_x + (box.content_width - size) / 2.0f;
299
+ float y = box.content_y + (box.content_height - size) / 2.0f;
300
+ Color color = xt_css_parse_color(state.value);
301
+ if (!color.valid) color = Color{0.0f, 0.0f, 0.0f, 1.0f};
302
+ addRect(out, x, y, size, size, 2.0f, Color{0.34f, 0.34f, 0.34f, 1.0f}, clip, box, chain);
303
+ addRect(out, x + 1.0f, y + 1.0f, size - 2.0f, size - 2.0f, 1.0f, color, clip, box, chain);
304
+ }
305
+
306
+ void paintSelect(const LayoutBox &box, const XtStyle &style, const FormState &state,
307
+ DisplayList &out, const PaintClip &clip, const paint_detail::FilterChain &chain,
308
+ float viewportWidth, float viewportHeight) {
309
+ const Node *option = xt_form_selected_option(box.node);
310
+ std::string label = option != nullptr ? xt_form_option_label(option) : "";
311
+ float line_height = lineHeightOf(style);
312
+ FontSpec spec = specOf(style);
313
+ float ascent = 0.0f;
314
+ xt_text_metrics(spec, &ascent, nullptr, nullptr);
315
+ if (ascent <= 0.0f) ascent = style.font_size * 0.8f;
316
+ float baseline = box.content_y + (box.content_height - line_height) / 2.0f + ascent;
317
+ addRun(out, box.content_x, baseline, label, style, shade(style, xt_form_is_disabled(box.node), 1.0f),
318
+ box, clip, chain, viewportWidth, viewportHeight);
319
+ /* A downward chevron on the right, like a native select. */
320
+ float arrow_size = 5.0f;
321
+ float ax = box.content_x + box.content_width - 10.0f;
322
+ float ay = box.content_y + (box.content_height - arrow_size) / 2.0f + 1.0f;
323
+ addRect(out, ax, ay, arrow_size, 1.5f, 0.0f, Color{0.25f, 0.25f, 0.25f, 1.0f}, clip, box, chain);
324
+ addRect(out, ax + 0.8f, ay + 1.2f, arrow_size - 1.6f, 1.5f, 0.0f,
325
+ Color{0.25f, 0.25f, 0.25f, 1.0f}, clip, box, chain);
326
+ }
327
+
328
+ void paintButtonLabel(const LayoutBox &box, const LayoutBox &shape, const XtStyle &style,
329
+ const std::string &label, DisplayList &out, const PaintClip &clip,
330
+ const paint_detail::FilterChain &chain, float viewportWidth,
331
+ float viewportHeight) {
332
+ float width = runWidth(label, style);
333
+ float line_height = lineHeightOf(style);
334
+ FontSpec spec = specOf(style);
335
+ float ascent = 0.0f;
336
+ xt_text_metrics(spec, &ascent, nullptr, nullptr);
337
+ if (ascent <= 0.0f) ascent = style.font_size * 0.8f;
338
+ float x = box.content_x + (box.content_width - width) / 2.0f;
339
+ float baseline = box.content_y + (box.content_height - line_height) / 2.0f + ascent;
340
+ addRun(out, x, baseline, label, style, shade(style, xt_form_is_disabled(box.node), 1.0f), shape,
341
+ clip, chain, viewportWidth, viewportHeight);
342
+ }
343
+
344
+ } // namespace
345
+
346
+ namespace paint_detail {
347
+
348
+ void xt_paint_form_control(const LayoutBox &box, const XtStyle &style, DisplayList &out,
349
+ const PaintClip &clip, const FilterChain &chain, float viewportWidth,
350
+ float viewportHeight) {
351
+ if (box.node == nullptr || !xt_form_is_control(box.node)) return;
352
+ const FormState *state = xt_form_state(box.node);
353
+ if (state == nullptr) return;
354
+ PaintClip content = intersectContent(box, clip);
355
+ std::string type = xt_form_type(box.node);
356
+ if (xt_form_is_textarea(box.node) || xt_form_is_text_like(box.node)) {
357
+ paintTextControl(box, style, *state, out, content, chain, viewportWidth, viewportHeight);
358
+ return;
359
+ }
360
+ if (type == "checkbox" || type == "radio") {
361
+ paintCheckbox(box, style, *state, out, content, chain, type == "radio");
362
+ return;
363
+ }
364
+ if (type == "range") {
365
+ paintRange(box, style, *state, out, content, chain);
366
+ return;
367
+ }
368
+ if (type == "color") {
369
+ paintColorSwatch(box, style, *state, out, content, chain);
370
+ return;
371
+ }
372
+ if (xt_form_is_select(box.node)) {
373
+ paintSelect(box, style, *state, out, content, chain, viewportWidth, viewportHeight);
374
+ return;
375
+ }
376
+ if (xt_form_is_button_like(box.node)) {
377
+ paintButtonLabel(box, box, style, xt_form_button_label(box.node), out, content, chain,
378
+ viewportWidth, viewportHeight);
379
+ return;
380
+ }
381
+ if (type == "file") {
382
+ paintButtonLabel(box, box, style, "Choose File", out, content, chain, viewportWidth,
383
+ viewportHeight);
384
+ }
385
+ }
386
+
387
+ } // namespace paint_detail
388
+
389
+ } // namespace xtgui
@@ -103,6 +103,12 @@ void addBitmapImage(DisplayList &out, std::shared_ptr<const PaintBitmap> bitmap,
103
103
  bool addBackdropFilter(DisplayList &out, const LayoutBox &box, const XtStyle &style,
104
104
  const PaintClip &clip, float viewportWidth, float viewportHeight);
105
105
 
106
+ /** Paint the value/selection/caret and type-specific decoration of a form
107
+ * control (`<input>`, `<textarea>`, `<select>`). */
108
+ void xt_paint_form_control(const LayoutBox &box, const XtStyle &style, DisplayList &out,
109
+ const PaintClip &clip, const FilterChain &chain,
110
+ float viewportWidth, float viewportHeight);
111
+
106
112
  /** Resolve `clip-path` into a device-space triangulated polygon. */
107
113
  int addClipPathForBox(DisplayList &out, const LayoutBox &box, const XtStyle &style,
108
114
  float viewportWidth, float viewportHeight);
@@ -99,6 +99,6 @@ void xt_gui_run_scripts(XtGuiWindow *win) {
99
99
  auto it = script_registry().find(id);
100
100
  if (it == script_registry().end()) continue;
101
101
  xt_value args[2] = {window_handle, document_handle};
102
- xt_call_with_this(it->second, XT_UNDEFINED, 2, args);
102
+ xt_gui_invoke(it->second, XT_UNDEFINED, 2, args);
103
103
  }
104
104
  }
@@ -187,6 +187,7 @@ void inheritFrom(XtStyle *style, const XtStyle *parent) {
187
187
  style->white_space = parent->white_space;
188
188
  style->text_shadow = parent->text_shadow;
189
189
  style->text_transform = parent->text_transform;
190
+ style->overflow_wrap = parent->overflow_wrap;
190
191
  style->letter_spacing = parent->letter_spacing;
191
192
  style->font_variant_numeric = parent->font_variant_numeric;
192
193
  style->writing_mode = parent->writing_mode;
@@ -10,6 +10,7 @@
10
10
  #ifndef XT_GUI_STYLE_H
11
11
  #define XT_GUI_STYLE_H
12
12
 
13
+ #include <cstdint>
13
14
  #include <map>
14
15
  #include <memory>
15
16
  #include <string>
@@ -37,6 +38,11 @@ enum class TextAlign { Start, Center, End, Justify };
37
38
  enum class TextOverflow { Clip, Ellipsis };
38
39
  enum class WhiteSpace { Normal, NoWrap, Pre, PreWrap, PreLine };
39
40
  enum class Overflow { Visible, Hidden, Scroll, Auto };
41
+ /** `text-decoration-line` is a bit set; `None` is zero. */
42
+ enum class TextDecorationLine : uint8_t { None = 0, Underline = 1, Overline = 2, LineThrough = 4 };
43
+ enum class TextDecorationStyle { Solid, Double, Dotted, Dashed, Wavy };
44
+ /** `overflow-wrap` (and its legacy `word-wrap` spelling). */
45
+ enum class OverflowWrap { Normal, BreakWord, Anywhere };
40
46
  enum class BorderStyle { None, Solid, Dashed, Dotted };
41
47
  enum class FontStyle { Normal, Italic };
42
48
 
@@ -311,6 +317,13 @@ struct XtStyle {
311
317
  TextOverflow text_overflow = TextOverflow::Clip;
312
318
  WhiteSpace white_space = WhiteSpace::Normal; // inherited
313
319
  TextTransform text_transform = TextTransform::None; // inherited
320
+ /** `text-decoration` shorthand (line/style/color); not inherited, but it
321
+ * propagates visually to in-flow descendants. */
322
+ uint8_t text_decoration_line = 0;
323
+ TextDecorationStyle text_decoration_style = TextDecorationStyle::Solid;
324
+ Color text_decoration_color{0.0f, 0.0f, 0.0f, 1.0f};
325
+ bool has_text_decoration_color = false;
326
+ OverflowWrap overflow_wrap = OverflowWrap::Normal; // inherited
314
327
  /** Resolved to pixels in the cascade (em is the element's own font size). */
315
328
  Length letter_spacing{0.0f, Unit::Px}; // inherited
316
329
  bool font_variant_numeric = false; // inherited; accepted, no OpenType features
@@ -234,15 +234,29 @@ std::string xt_style_property_to_string(const XtStyle &style, const std::string
234
234
  static const char *const clearNames[] = {"none", "left", "right", "both"};
235
235
  static const char *const alignTextNames[] = {"start", "center", "end", "justify"};
236
236
  static const char *const textTransformNames[] = {"none", "uppercase", "lowercase", "capitalize"};
237
+ static const char *const decorationStyleNames[] = {"solid", "double", "dotted", "dashed", "wavy"};
238
+ static const char *const overflowWrapNames[] = {"normal", "break-word", "anywhere"};
237
239
  static const char *const writingModeNames[] = {"horizontal-tb", "vertical-rl", "vertical-lr"};
238
240
  static const char *const cursorNames[] = {"auto", "default", "pointer", "text", "move",
239
241
  "wait", "help", "not-allowed", "crosshair"};
240
242
  static const char *const borderStyleNames[] = {"none", "solid", "dashed", "dotted"};
243
+ auto borderColor = [&style]() {
244
+ return style.has_border_color ? formatColor(style.border_color) : formatColor(style.color);
245
+ };
246
+ auto borderWidth = [&style](const Length &length) {
247
+ /* A `none`/`hidden` border has a used width of zero, and `getComputedStyle`
248
+ * reports that resolved width. */
249
+ if (style.border_style == BorderStyle::None) return std::string("0px");
250
+ return formatLength(length);
251
+ };
241
252
 
242
253
  if (property == "display") {
243
254
  if (style.inline_level) return style.display == Display::Grid ? "inline-grid" : "inline-flex";
244
255
  return displayNames[(int)style.display];
245
256
  }
257
+ if (property == "box-sizing") {
258
+ return style.box_sizing == BoxSizing::BorderBox ? "border-box" : "content-box";
259
+ }
246
260
  if (property == "position") return positionNames[(int)style.position];
247
261
  if (property == "color") return formatColor(style.color);
248
262
  if (property == "background-color" || property == "background") {
@@ -269,11 +283,44 @@ std::string xt_style_property_to_string(const XtStyle &style, const std::string
269
283
  if (property == "font-size") return formatNumber(style.font_size) + "px";
270
284
  if (property == "font-weight") return formatNumber((float)style.font_weight);
271
285
  if (property == "font-family") return style.font_family;
286
+ if (property == "font-style") return style.font_style == FontStyle::Italic ? "italic" : "normal";
287
+ if (property == "font-variant") return style.font_variant_numeric ? "tabular-nums" : "normal";
288
+ if (property == "font-stretch") return "100%";
289
+ if (property == "font-kerning") return "auto";
290
+ if (property == "overflow" || property == "overflow-x" || property == "overflow-y") {
291
+ static const char *const overflowNames[] = {"visible", "hidden", "scroll", "auto"};
292
+ return overflowNames[(int)style.overflow];
293
+ }
294
+ if (property == "transition") return style.transitions.empty() ? "none" : "";
295
+ if (property == "animation") return style.animations.empty() ? "none" : "";
272
296
  if (property == "line-height") {
273
297
  return style.line_height.unit == Unit::Auto ? "normal" : formatLength(style.line_height);
274
298
  }
275
299
  if (property == "text-align") return alignTextNames[(int)style.text_align];
276
300
  if (property == "text-transform") return textTransformNames[(int)style.text_transform];
301
+ if (property == "text-decoration" || property == "text-decoration-line") {
302
+ if (style.text_decoration_line == 0) return "none";
303
+ std::string out;
304
+ if (style.text_decoration_line & (uint8_t)TextDecorationLine::Underline) out = "underline";
305
+ if (style.text_decoration_line & (uint8_t)TextDecorationLine::Overline) {
306
+ if (!out.empty()) out += " ";
307
+ out += "overline";
308
+ }
309
+ if (style.text_decoration_line & (uint8_t)TextDecorationLine::LineThrough) {
310
+ if (!out.empty()) out += " ";
311
+ out += "line-through";
312
+ }
313
+ return out;
314
+ }
315
+ if (property == "text-decoration-style") {
316
+ return decorationStyleNames[(int)style.text_decoration_style];
317
+ }
318
+ if (property == "text-decoration-color") {
319
+ return formatColor(style.has_text_decoration_color ? style.text_decoration_color : style.color);
320
+ }
321
+ if (property == "overflow-wrap" || property == "word-wrap") {
322
+ return overflowWrapNames[(int)style.overflow_wrap];
323
+ }
277
324
  if (property == "text-overflow") {
278
325
  return style.text_overflow == TextOverflow::Ellipsis ? "ellipsis" : "clip";
279
326
  }
@@ -317,6 +364,27 @@ std::string xt_style_property_to_string(const XtStyle &style, const std::string
317
364
  if (property == "flex-grow") return formatNumber(style.flex_grow);
318
365
  if (property == "flex-shrink") return formatNumber(style.flex_shrink);
319
366
  if (property == "flex-basis") return formatLength(style.flex_basis);
367
+ if (property == "border-top-width") return borderWidth(style.border_width.top);
368
+ if (property == "border-right-width") return borderWidth(style.border_width.right);
369
+ if (property == "border-bottom-width") return borderWidth(style.border_width.bottom);
370
+ if (property == "border-left-width") return borderWidth(style.border_width.left);
371
+ if (property == "border-width") {
372
+ return borderWidth(style.border_width.top) + " " + borderWidth(style.border_width.right) + " " +
373
+ borderWidth(style.border_width.bottom) + " " + borderWidth(style.border_width.left);
374
+ }
375
+ if (property == "border-top-style" || property == "border-right-style" ||
376
+ property == "border-bottom-style" || property == "border-left-style" ||
377
+ property == "border-style") {
378
+ return borderStyleNames[(int)style.border_style];
379
+ }
380
+ if (property == "border-top-color") return borderColor();
381
+ if (property == "border-right-color") return borderColor();
382
+ if (property == "border-bottom-color") return borderColor();
383
+ if (property == "border-left-color") return borderColor();
384
+ if (property == "border-color") {
385
+ std::string color = borderColor();
386
+ return color + " " + color + " " + color + " " + color;
387
+ }
320
388
  if (property == "border-radius") return formatLength(style.border_radius);
321
389
  if (property == "opacity") return formatNumber(style.opacity);
322
390
  if (property == "mix-blend-mode") {
@@ -353,6 +421,67 @@ std::string xt_style_property_to_string(const XtStyle &style, const std::string
353
421
  if (property == "transform-origin") {
354
422
  return formatLength(style.transform_origin_x) + " " + formatLength(style.transform_origin_y);
355
423
  }
424
+
425
+ /* Standard properties the engine does not model still answer with their
426
+ * CSS initial value. Returning "" made the usual "read the current style"
427
+ * pattern (`getComputedStyle(el).wordBreak`, `.objectFit`, …) see a value no
428
+ * browser produces. Modelled properties are handled above; this table is
429
+ * only the computed default. */
430
+ struct InitialValue {
431
+ const char *name;
432
+ const char *value;
433
+ };
434
+ static const InitialValue kInitialValues[] = {
435
+ {"box-sizing", "content-box"},
436
+ {"text-decoration", "none"},
437
+ {"text-decoration-line", "none"},
438
+ {"text-decoration-style", "solid"},
439
+ {"text-decoration-color", "rgb(0, 0, 0)"},
440
+ {"overflow-wrap", "normal"},
441
+ {"word-wrap", "normal"},
442
+ {"text-indent", "0px"},
443
+ {"word-spacing", "0px"},
444
+ {"word-break", "normal"},
445
+ {"hyphens", "manual"},
446
+ {"tab-size", "8"},
447
+ {"direction", "ltr"},
448
+ {"unicode-bidi", "normal"},
449
+ {"text-rendering", "auto"},
450
+ {"text-justify", "auto"},
451
+ {"background-repeat", "repeat"},
452
+ {"background-position", "0% 0%"},
453
+ {"background-attachment", "scroll"},
454
+ {"background-clip", "border-box"},
455
+ {"background-origin", "padding-box"},
456
+ {"border-collapse", "separate"},
457
+ {"border-spacing", "0px"},
458
+ {"table-layout", "auto"},
459
+ {"caption-side", "top"},
460
+ {"empty-cells", "show"},
461
+ {"list-style-type", "disc"},
462
+ {"list-style-position", "outside"},
463
+ {"list-style-image", "none"},
464
+ {"object-fit", "fill"},
465
+ {"object-position", "50% 50%"},
466
+ {"resize", "none"},
467
+ {"user-select", "auto"},
468
+ {"touch-action", "auto"},
469
+ {"scroll-behavior", "auto"},
470
+ {"overscroll-behavior", "auto"},
471
+ {"will-change", "auto"},
472
+ {"contain", "none"},
473
+ {"isolation", "auto"},
474
+ {"appearance", "none"},
475
+ {"columns", "auto"},
476
+ {"column-count", "auto"},
477
+ {"column-rule", "none"},
478
+ {"caret-color", "auto"},
479
+ {"accent-color", "auto"},
480
+ {"color-scheme", "normal"},
481
+ };
482
+ for (const InitialValue &entry : kInitialValues) {
483
+ if (property == entry.name) return entry.value;
484
+ }
356
485
  return "";
357
486
  }
358
487