xbintsc 0.5.31 → 0.5.36

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 (286) hide show
  1. package/README.md +25 -16
  2. package/README.zh-CN.md +10 -8
  3. package/dist/scripts/check-version.d.ts +27 -0
  4. package/dist/scripts/check-version.js +71 -0
  5. package/dist/scripts/check-version.js.map +1 -0
  6. package/dist/scripts/package-platform.d.ts +58 -0
  7. package/dist/scripts/package-platform.js +142 -0
  8. package/dist/scripts/package-platform.js.map +1 -0
  9. package/dist/scripts/verify-platform-package.d.ts +34 -0
  10. package/dist/scripts/verify-platform-package.js +161 -0
  11. package/dist/scripts/verify-platform-package.js.map +1 -0
  12. package/dist/src/binder/references.js +14 -1
  13. package/dist/src/binder/references.js.map +1 -1
  14. package/dist/src/cli/help.d.ts +1 -1
  15. package/dist/src/cli/main.js +3 -1
  16. package/dist/src/cli/main.js.map +1 -1
  17. package/dist/src/codegen/generator/module.d.ts +1 -0
  18. package/dist/src/codegen/generator/module.js +54 -0
  19. package/dist/src/codegen/generator/module.js.map +1 -1
  20. package/dist/src/codegen/generator/statements/variables.js +6 -4
  21. package/dist/src/codegen/generator/statements/variables.js.map +1 -1
  22. package/dist/src/driver/runtime-lib.d.ts +29 -4
  23. package/dist/src/driver/runtime-lib.js +68 -10
  24. package/dist/src/driver/runtime-lib.js.map +1 -1
  25. package/dist/src/driver/version.d.ts +1 -1
  26. package/dist/src/driver/version.js +1 -1
  27. package/dist/src/extensions/gui/css.d.ts +26 -0
  28. package/dist/src/extensions/gui/css.js +100 -0
  29. package/dist/src/extensions/gui/css.js.map +1 -0
  30. package/dist/src/extensions/gui/html.js +9 -2
  31. package/dist/src/extensions/gui/html.js.map +1 -1
  32. package/dist/src/extensions/gui/index.js +22 -7
  33. package/dist/src/extensions/gui/index.js.map +1 -1
  34. package/dist/src/extensions/gui/three.d.ts +16 -0
  35. package/dist/src/extensions/gui/three.js +22 -0
  36. package/dist/src/extensions/gui/three.js.map +1 -0
  37. package/dist/src/extensions/media/index.d.ts +6 -5
  38. package/dist/src/extensions/media/index.js +15 -16
  39. package/dist/src/extensions/media/index.js.map +1 -1
  40. package/dist/src/parser/context.d.ts +41 -0
  41. package/dist/src/parser/context.js +89 -4
  42. package/dist/src/parser/context.js.map +1 -1
  43. package/dist/src/parser/literals.js +6 -12
  44. package/dist/src/parser/literals.js.map +1 -1
  45. package/dist/tests/driver/platform-package.test.d.ts +1 -0
  46. package/dist/tests/driver/platform-package.test.js +49 -0
  47. package/dist/tests/driver/platform-package.test.js.map +1 -0
  48. package/dist/tests/driver/runtime-lib.test.js +51 -4
  49. package/dist/tests/driver/runtime-lib.test.js.map +1 -1
  50. package/dist/tests/e2e/compile-builtins.test.js +24 -0
  51. package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
  52. package/dist/tests/e2e/compile-objects-classes.test.js +23 -0
  53. package/dist/tests/e2e/compile-objects-classes.test.js.map +1 -1
  54. package/dist/tests/e2e/gui-audio.test.d.ts +14 -0
  55. package/dist/tests/e2e/gui-audio.test.js +287 -0
  56. package/dist/tests/e2e/gui-audio.test.js.map +1 -0
  57. package/dist/tests/e2e/gui-browser.test.js +1 -1
  58. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  59. package/dist/tests/e2e/gui-effects.test.d.ts +6 -0
  60. package/dist/tests/e2e/gui-effects.test.js +262 -0
  61. package/dist/tests/e2e/gui-effects.test.js.map +1 -0
  62. package/dist/tests/e2e/gui-example-webgl.test.d.ts +10 -0
  63. package/dist/tests/e2e/gui-example-webgl.test.js +75 -0
  64. package/dist/tests/e2e/gui-example-webgl.test.js.map +1 -0
  65. package/dist/tests/e2e/gui-example-webgl2.test.d.ts +8 -0
  66. package/dist/tests/e2e/gui-example-webgl2.test.js +72 -0
  67. package/dist/tests/e2e/gui-example-webgl2.test.js.map +1 -0
  68. package/dist/tests/e2e/gui-helpers.d.ts +7 -0
  69. package/dist/tests/e2e/gui-helpers.js +1 -0
  70. package/dist/tests/e2e/gui-helpers.js.map +1 -1
  71. package/dist/tests/e2e/gui-layout.test.js +48 -0
  72. package/dist/tests/e2e/gui-layout.test.js.map +1 -1
  73. package/dist/tests/e2e/gui-media.test.js +83 -4
  74. package/dist/tests/e2e/gui-media.test.js.map +1 -1
  75. package/dist/tests/e2e/gui-style.test.js +55 -0
  76. package/dist/tests/e2e/gui-style.test.js.map +1 -1
  77. package/dist/tests/e2e/gui-webgl.test.d.ts +11 -0
  78. package/dist/tests/e2e/gui-webgl.test.js +437 -0
  79. package/dist/tests/e2e/gui-webgl.test.js.map +1 -0
  80. package/dist/tests/e2e/gui-webgl2-3d.test.d.ts +4 -0
  81. package/dist/tests/e2e/gui-webgl2-3d.test.js +264 -0
  82. package/dist/tests/e2e/gui-webgl2-3d.test.js.map +1 -0
  83. package/dist/tests/e2e/gui-webgl2-blit.test.d.ts +10 -0
  84. package/dist/tests/e2e/gui-webgl2-blit.test.js +262 -0
  85. package/dist/tests/e2e/gui-webgl2-blit.test.js.map +1 -0
  86. package/dist/tests/e2e/gui-webgl2-formats.test.d.ts +6 -0
  87. package/dist/tests/e2e/gui-webgl2-formats.test.js +395 -0
  88. package/dist/tests/e2e/gui-webgl2-formats.test.js.map +1 -0
  89. package/dist/tests/e2e/gui-webgl2-mrt.test.d.ts +4 -0
  90. package/dist/tests/e2e/gui-webgl2-mrt.test.js +105 -0
  91. package/dist/tests/e2e/gui-webgl2-mrt.test.js.map +1 -0
  92. package/dist/tests/e2e/gui-webgl2-ubo.test.d.ts +8 -0
  93. package/dist/tests/e2e/gui-webgl2-ubo.test.js +112 -0
  94. package/dist/tests/e2e/gui-webgl2-ubo.test.js.map +1 -0
  95. package/dist/tests/e2e/gui-webgl2.test.d.ts +10 -0
  96. package/dist/tests/e2e/gui-webgl2.test.js +282 -0
  97. package/dist/tests/e2e/gui-webgl2.test.js.map +1 -0
  98. package/dist/tests/extensions/gui-index.test.js +1 -0
  99. package/dist/tests/extensions/gui-index.test.js.map +1 -1
  100. package/dist/tests/extensions/gui.test.js +41 -0
  101. package/dist/tests/extensions/gui.test.js.map +1 -1
  102. package/dist/tests/extensions/media.test.js +8 -3
  103. package/dist/tests/extensions/media.test.js.map +1 -1
  104. package/dist/tests/parser/expressions.test.js +30 -0
  105. package/dist/tests/parser/expressions.test.js.map +1 -1
  106. package/dist/tests/platform-tarball.test.d.ts +1 -0
  107. package/dist/tests/platform-tarball.test.js +111 -0
  108. package/dist/tests/platform-tarball.test.js.map +1 -0
  109. package/dist/tests/version.test.js +6 -0
  110. package/dist/tests/version.test.js.map +1 -1
  111. package/doc/ai/contributing.md +11 -0
  112. package/doc/ai/extensions.md +35 -12
  113. package/doc/ai/troubleshooting.md +17 -11
  114. package/doc/ai/zh-CN/contributing.md +9 -0
  115. package/doc/ai/zh-CN/extensions.md +30 -10
  116. package/doc/ai/zh-CN/troubleshooting.md +14 -8
  117. package/doc/gui/api.md +63 -14
  118. package/doc/gui/build.md +79 -16
  119. package/doc/gui/canvas.md +11 -3
  120. package/doc/gui/dom.md +35 -17
  121. package/doc/gui/known-issues.md +57 -24
  122. package/doc/gui/layout.md +18 -0
  123. package/doc/gui/media.md +21 -13
  124. package/doc/gui/rendering.md +50 -3
  125. package/doc/gui/webgl.md +315 -0
  126. package/doc/gui.md +113 -18
  127. package/doc/npm-package.md +59 -29
  128. package/doc/requirements.md +24 -11
  129. package/doc/zh-CN/gui/api.md +53 -11
  130. package/doc/zh-CN/gui/build.md +70 -16
  131. package/doc/zh-CN/gui/canvas.md +9 -2
  132. package/doc/zh-CN/gui/dom.md +22 -7
  133. package/doc/zh-CN/gui/known-issues.md +45 -19
  134. package/doc/zh-CN/gui/layout.md +14 -0
  135. package/doc/zh-CN/gui/media.md +18 -11
  136. package/doc/zh-CN/gui/rendering.md +41 -4
  137. package/doc/zh-CN/gui/webgl.md +121 -0
  138. package/doc/zh-CN/gui.md +91 -15
  139. package/doc/zh-CN/npm-package.md +48 -24
  140. package/doc/zh-CN/requirements.md +16 -7
  141. package/llms.txt +2 -0
  142. package/package.json +17 -1
  143. package/runtime/ext_gui/audio.cpp +365 -153
  144. package/runtime/ext_gui/audio_buffer.cpp +166 -0
  145. package/runtime/ext_gui/audio_buffer.h +37 -0
  146. package/runtime/ext_gui/audio_decode.cpp +219 -0
  147. package/runtime/ext_gui/audio_decode.h +39 -0
  148. package/runtime/ext_gui/audio_device.cpp +87 -0
  149. package/runtime/ext_gui/audio_effects.cpp +163 -0
  150. package/runtime/ext_gui/audio_effects.h +25 -0
  151. package/runtime/ext_gui/audio_internal.h +30 -0
  152. package/runtime/ext_gui/audio_mixer.cpp +494 -0
  153. package/runtime/ext_gui/audio_mixer.h +238 -0
  154. package/runtime/ext_gui/audio_param.cpp +104 -0
  155. package/runtime/ext_gui/audio_param_object.cpp +155 -0
  156. package/runtime/ext_gui/browser.cpp +50 -14
  157. package/runtime/ext_gui/canvas.cpp +3 -18
  158. package/runtime/ext_gui/canvas_internal.h +10 -0
  159. package/runtime/ext_gui/canvas_raster.cpp +60 -0
  160. package/runtime/ext_gui/clip_values.cpp +246 -0
  161. package/runtime/ext_gui/css.cpp +94 -0
  162. package/runtime/ext_gui/css.h +22 -1
  163. package/runtime/ext_gui/css_match.cpp +3 -0
  164. package/runtime/ext_gui/css_selector.cpp +30 -3
  165. package/runtime/ext_gui/css_sheet.cpp +89 -45
  166. package/runtime/ext_gui/document.cpp +4 -1
  167. package/runtime/ext_gui/document.h +2 -0
  168. package/runtime/ext_gui/dom.h +16 -0
  169. package/runtime/ext_gui/dom_api.cpp +1 -0
  170. package/runtime/ext_gui/dom_api_canvas.cpp +17 -80
  171. package/runtime/ext_gui/dom_api_canvas_extra.cpp +130 -1
  172. package/runtime/ext_gui/dom_api_canvas_pending.cpp +41 -0
  173. package/runtime/ext_gui/dom_api_internal.h +275 -0
  174. package/runtime/ext_gui/dom_api_webgl.cpp +383 -0
  175. package/runtime/ext_gui/dom_api_webgl2.cpp +549 -0
  176. package/runtime/ext_gui/dom_api_webgl2_draw.cpp +269 -0
  177. package/runtime/ext_gui/dom_api_webgl_buffer.cpp +216 -0
  178. package/runtime/ext_gui/dom_api_webgl_compressed.cpp +167 -0
  179. package/runtime/ext_gui/dom_api_webgl_framebuffer.cpp +160 -0
  180. package/runtime/ext_gui/dom_api_webgl_rt.h +30 -0
  181. package/runtime/ext_gui/dom_api_webgl_shader.cpp +443 -0
  182. package/runtime/ext_gui/dom_api_webgl_source.cpp +161 -0
  183. package/runtime/ext_gui/dom_api_webgl_state.cpp +590 -0
  184. package/runtime/ext_gui/dom_api_webgl_texture.cpp +485 -0
  185. package/runtime/ext_gui/dom_api_webgl_ubo.cpp +81 -0
  186. package/runtime/ext_gui/effect_values.cpp +312 -0
  187. package/runtime/ext_gui/gradient_values.cpp +476 -0
  188. package/runtime/ext_gui/grid_values.cpp +230 -0
  189. package/runtime/ext_gui/gui.cpp +44 -9
  190. package/runtime/ext_gui/gui_canvas.cpp +50 -2
  191. package/runtime/ext_gui/gui_engine.h +36 -4
  192. package/runtime/ext_gui/gui_events.cpp +4 -1
  193. package/runtime/ext_gui/gui_media.cpp +1 -1
  194. package/runtime/ext_gui/gui_webgl.cpp +83 -0
  195. package/runtime/ext_gui/image_bitmap.cpp +28 -0
  196. package/runtime/ext_gui/image_bitmap.h +26 -0
  197. package/runtime/ext_gui/image_bitmap_browser.cpp +136 -0
  198. package/runtime/ext_gui/layout.cpp +21 -0
  199. package/runtime/ext_gui/layout_flow.cpp +81 -4
  200. package/runtime/ext_gui/layout_flow_internal.h +31 -0
  201. package/runtime/ext_gui/layout_grid.cpp +374 -0
  202. package/runtime/ext_gui/media.cpp +32 -0
  203. package/runtime/ext_gui/media.h +14 -1
  204. package/runtime/ext_gui/media_api.h +25 -2
  205. package/runtime/ext_gui/media_query.cpp +211 -0
  206. package/runtime/ext_gui/paint.cpp +111 -77
  207. package/runtime/ext_gui/paint.h +33 -3
  208. package/runtime/ext_gui/paint_background.cpp +229 -0
  209. package/runtime/ext_gui/paint_clip.cpp +268 -0
  210. package/runtime/ext_gui/paint_effects.cpp +324 -0
  211. package/runtime/ext_gui/paint_internal.h +85 -0
  212. package/runtime/ext_gui/renderer.cpp +8 -31
  213. package/runtime/ext_gui/renderer_clip.cpp +196 -0
  214. package/runtime/ext_gui/renderer_image.cpp +13 -8
  215. package/runtime/ext_gui/renderer_internal.h +15 -8
  216. package/runtime/ext_gui/renderer_text.cpp +6 -31
  217. package/runtime/ext_gui/style.cpp +343 -20
  218. package/runtime/ext_gui/style.h +144 -3
  219. package/runtime/ext_gui/style_print.cpp +134 -1
  220. package/runtime/ext_gui/style_values.cpp +152 -126
  221. package/runtime/ext_gui/style_values_internal.h +35 -0
  222. package/runtime/ext_gui/webgl.h +600 -0
  223. package/runtime/ext_gui/webgl2_constants.h +274 -0
  224. package/runtime/ext_gui/webgl_blit.cpp +95 -0
  225. package/runtime/ext_gui/webgl_buffer.cpp +480 -0
  226. package/runtime/ext_gui/webgl_buffer_objects.cpp +270 -0
  227. package/runtime/ext_gui/webgl_clear.cpp +142 -0
  228. package/runtime/ext_gui/webgl_compressed.cpp +180 -0
  229. package/runtime/ext_gui/webgl_constants.h +317 -0
  230. package/runtime/ext_gui/webgl_context.cpp +551 -0
  231. package/runtime/ext_gui/webgl_draw.cpp +560 -0
  232. package/runtime/ext_gui/webgl_draw_buffers.cpp +75 -0
  233. package/runtime/ext_gui/webgl_glsl.h +165 -0
  234. package/runtime/ext_gui/webgl_glsl_compile.cpp +437 -0
  235. package/runtime/ext_gui/webgl_glsl_rewrite.cpp +367 -0
  236. package/runtime/ext_gui/webgl_glsl_scan.cpp +590 -0
  237. package/runtime/ext_gui/webgl_glsl_scan_block.cpp +126 -0
  238. package/runtime/ext_gui/webgl_glsl_tokens.h +28 -0
  239. package/runtime/ext_gui/webgl_msl.cpp +62 -0
  240. package/runtime/ext_gui/webgl_msl.h +33 -0
  241. package/runtime/ext_gui/webgl_pipeline.cpp +326 -0
  242. package/runtime/ext_gui/webgl_program_blocks.cpp +58 -0
  243. package/runtime/ext_gui/webgl_program_internal.h +24 -0
  244. package/runtime/ext_gui/webgl_shader.cpp +594 -0
  245. package/runtime/ext_gui/webgl_state.cpp +228 -0
  246. package/runtime/ext_gui/webgl_texture.cpp +420 -0
  247. package/runtime/ext_gui/webgl_texture_3d.cpp +215 -0
  248. package/runtime/ext_gui/webgl_uniform.cpp +167 -0
  249. package/runtime/ext_gui/webgl_uniform_block.h +31 -0
  250. package/runtime/ext_gui/webgl_webgl2.cpp +277 -0
  251. package/runtime/ext_media/decode_audio.cpp +308 -0
  252. package/runtime/ext_media/media.cpp +1 -1
  253. package/runtime/ext_media/media_bridge.c +11 -3
  254. package/runtime/rt.h +17 -0
  255. package/runtime/rt_internal.h +1 -0
  256. package/runtime/xt_array_buffer.c +30 -0
  257. package/runtime/xt_containers/access.inc +8 -0
  258. package/runtime/xt_regex/api.inc +2 -0
  259. package/runtime/xt_regex/matcher.inc +16 -3
  260. package/runtime/xt_regex/parser.inc +8 -2
  261. package/runtime/xt_regex/types.inc +3 -0
  262. package/runtime/xt_stdlib2/regexp-match.inc +60 -21
  263. package/runtime/xt_stdlib2/regexp.inc +1 -0
  264. package/runtime/xt_typed_array/elements.inc +13 -0
  265. package/runtime/xt_typed_array.c +1 -1
  266. package/scripts/build-gui.ts +90 -3
  267. package/scripts/build-media.ts +335 -62
  268. package/scripts/build-runtime.ts +21 -16
  269. package/scripts/check-version.ts +39 -9
  270. package/scripts/package-platform.ts +183 -0
  271. package/scripts/set-platform-version.ts +62 -0
  272. package/scripts/setup-vs-toolchain.ps1 +152 -0
  273. package/scripts/verify-platform-package.ts +182 -0
  274. package/src/binder/references.ts +13 -1
  275. package/src/cli/main.ts +5 -1
  276. package/src/codegen/generator/module.ts +50 -0
  277. package/src/codegen/generator/statements/variables.ts +6 -3
  278. package/src/driver/runtime-lib.ts +73 -9
  279. package/src/driver/version.ts +1 -1
  280. package/src/extensions/gui/css.ts +113 -0
  281. package/src/extensions/gui/html.ts +9 -2
  282. package/src/extensions/gui/index.ts +22 -7
  283. package/src/extensions/gui/three.ts +25 -0
  284. package/src/extensions/media/index.ts +16 -18
  285. package/src/parser/context.ts +99 -4
  286. package/src/parser/literals.ts +6 -12
@@ -269,6 +269,66 @@ void draw_image(std::vector<unsigned char> &pixels, int width, int height, const
269
269
  }
270
270
  }
271
271
 
272
+
273
+ void draw_image_affine(std::vector<unsigned char> &pixels, int width, int height,
274
+ const Image &source, float dx, float dy, float dw, float dh, float sx,
275
+ float sy, float sw, float sh, const CanvasTransform &transform,
276
+ float alpha) {
277
+ if (source.width <= 0 || source.height <= 0 || dw <= 0.0f || dh <= 0.0f || sw <= 0.0f ||
278
+ sh <= 0.0f)
279
+ return;
280
+ CanvasTransform inverse = transform.inverted();
281
+ float cornersX[4];
282
+ float cornersY[4];
283
+ const float corners[4][2] = {{dx, dy}, {dx + dw, dy}, {dx + dw, dy + dh}, {dx, dy + dh}};
284
+ float minX = 0.0f, minY = 0.0f, maxX = 0.0f, maxY = 0.0f;
285
+ for (int i = 0; i < 4; i++) {
286
+ transform.apply(corners[i][0], corners[i][1], &cornersX[i], &cornersY[i]);
287
+ if (i == 0) {
288
+ minX = maxX = cornersX[i];
289
+ minY = maxY = cornersY[i];
290
+ } else {
291
+ minX = std::min(minX, cornersX[i]);
292
+ maxX = std::max(maxX, cornersX[i]);
293
+ minY = std::min(minY, cornersY[i]);
294
+ maxY = std::max(maxY, cornersY[i]);
295
+ }
296
+ }
297
+ int ix0 = std::max(0, (int)std::floor(minX));
298
+ int ix1 = std::min(width, (int)std::ceil(maxX));
299
+ int iy0 = std::max(0, (int)std::floor(minY));
300
+ int iy1 = std::min(height, (int)std::ceil(maxY));
301
+ for (int py = iy0; py < iy1; py++) {
302
+ for (int px = ix0; px < ix1; px++) {
303
+ float ux = 0.0f;
304
+ float uy = 0.0f;
305
+ inverse.apply((float)px + 0.5f, (float)py + 0.5f, &ux, &uy);
306
+ float u = (ux - dx) / dw;
307
+ float v = (uy - dy) / dh;
308
+ if (u < 0.0f || u >= 1.0f || v < 0.0f || v >= 1.0f) continue;
309
+ int sxi = (int)(sx + u * sw);
310
+ int syi = (int)(sy + v * sh);
311
+ if (sxi < 0) sxi = 0;
312
+ if (syi < 0) syi = 0;
313
+ if (sxi >= source.width) sxi = source.width - 1;
314
+ if (syi >= source.height) syi = source.height - 1;
315
+ const unsigned char *texel =
316
+ source.pixels.data() + ((size_t)syi * (size_t)source.width + (size_t)sxi) * 4;
317
+ unsigned char *dst = pixels.data() + ((size_t)py * (size_t)width + (size_t)px) * 4;
318
+ if (alpha >= 0.999f) {
319
+ const unsigned char ta = texel[3];
320
+ if (ta == 255) {
321
+ dst[0] = texel[0]; dst[1] = texel[1]; dst[2] = texel[2]; dst[3] = 255;
322
+ continue;
323
+ }
324
+ if (ta == 0) continue;
325
+ }
326
+ float color[4] = {texel[0] / 255.0f, texel[1] / 255.0f, texel[2] / 255.0f, texel[3] / 255.0f};
327
+ blend_pixel(dst, color, 1.0f, alpha);
328
+ }
329
+ }
330
+ }
331
+
272
332
  void blit_glyph(std::vector<unsigned char> &pixels, int width, int height,
273
333
  const std::vector<unsigned char> &glyph, int glyphWidth, int glyphHeight, int left,
274
334
  int top, float x, float baselineY, const CanvasPaint &paint, float alpha) {
@@ -0,0 +1,246 @@
1
+ /*
2
+ * xbintsc GUI engine — `clip-path` value parsing.
3
+ *
4
+ * The geometry is resolved in `paint_clip.cpp`, where the element's border box
5
+ * and transform are known. Values here keep CSS lengths unresolved so `%`, em
6
+ * and viewport units survive the cascade.
7
+ */
8
+ #include <cctype>
9
+ #include <string>
10
+ #include <utility>
11
+ #include <vector>
12
+
13
+ #include "css_internal.h"
14
+ #include "style_internal.h"
15
+ #include "style_values_internal.h"
16
+
17
+ namespace xtgui {
18
+
19
+ namespace {
20
+
21
+ std::vector<std::string> splitTokens(const std::string &text) {
22
+ std::vector<std::string> parts;
23
+ std::string current;
24
+ int depth = 0;
25
+ for (char c : text) {
26
+ if (c == '(') depth++;
27
+ if (c == ')') depth--;
28
+ if ((c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == ',') && depth == 0) {
29
+ if (!current.empty()) {
30
+ parts.push_back(current);
31
+ current.clear();
32
+ }
33
+ } else {
34
+ current.push_back(c);
35
+ }
36
+ }
37
+ if (!current.empty()) parts.push_back(current);
38
+ return parts;
39
+ }
40
+
41
+ /** Split `text` on a top-level keyword (`at` / `round`), keeping both sides. */
42
+ bool splitKeyword(const std::string &text, const std::string &keyword, std::string *left,
43
+ std::string *right) {
44
+ std::string lower = xt_css_lower(text);
45
+ int depth = 0;
46
+ for (size_t i = 0; i + keyword.size() <= text.size(); i++) {
47
+ char c = text[i];
48
+ if (c == '(') depth++;
49
+ else if (c == ')') depth--;
50
+ if (depth != 0) continue;
51
+ if (lower.compare(i, keyword.size(), keyword) != 0) continue;
52
+ bool before = i == 0 || xt_is_css_space(text[i - 1]) || text[i - 1] == ',';
53
+ bool after = i + keyword.size() >= text.size() ||
54
+ xt_is_css_space(text[i + keyword.size()]) || text[i + keyword.size()] == ',';
55
+ if (!before || !after) continue;
56
+ *left = xt_css_trim(text.substr(0, i));
57
+ *right = xt_css_trim(text.substr(i + keyword.size()));
58
+ return true;
59
+ }
60
+ return false;
61
+ }
62
+
63
+ bool parsePositionToken(const std::string &raw, Length *out, bool *vertical) {
64
+ std::string text = xt_css_lower(xt_css_trim(raw));
65
+ if (text == "left") { *out = {0.0f, Unit::Percent}; *vertical = false; return true; }
66
+ if (text == "right") { *out = {100.0f, Unit::Percent}; *vertical = false; return true; }
67
+ if (text == "top") { *out = {0.0f, Unit::Percent}; *vertical = true; return true; }
68
+ if (text == "bottom") { *out = {100.0f, Unit::Percent}; *vertical = true; return true; }
69
+ if (text == "center") { *out = {50.0f, Unit::Percent}; *vertical = false; return true; }
70
+ Length length = xt_css_parse_length(text);
71
+ if (length.unit == Unit::Invalid || length.unit == Unit::Auto || length.unit == Unit::None)
72
+ return false;
73
+ *out = length;
74
+ *vertical = false;
75
+ return true;
76
+ }
77
+
78
+ bool parseCenter(const std::string &text, Length *x, Length *y) {
79
+ std::vector<std::string> tokens = splitTokens(text);
80
+ if (tokens.empty() || tokens.size() > 2) return false;
81
+ Length values[2];
82
+ bool vertical[2] = {false, false};
83
+ for (size_t i = 0; i < tokens.size(); i++) {
84
+ if (!parsePositionToken(tokens[i], &values[i], &vertical[i])) return false;
85
+ }
86
+ if (tokens.size() == 1) {
87
+ if (vertical[0]) {
88
+ *x = {50.0f, Unit::Percent};
89
+ *y = values[0];
90
+ } else {
91
+ *x = values[0];
92
+ *y = {50.0f, Unit::Percent};
93
+ }
94
+ return true;
95
+ }
96
+ if (vertical[0] && !vertical[1]) {
97
+ *x = values[1];
98
+ *y = values[0];
99
+ } else {
100
+ *x = values[0];
101
+ *y = values[1];
102
+ }
103
+ return true;
104
+ }
105
+
106
+ /** Parse the 1–4 side lengths of `inset(...)` and an optional round radius. */
107
+ bool parseInset(const std::string &body, ClipPathSpec *spec) {
108
+ std::string sides = body;
109
+ std::string round;
110
+ if (splitKeyword(body, "round", &sides, &round)) {
111
+ std::vector<std::string> radiusTokens = splitTokens(round);
112
+ if (!radiusTokens.empty()) {
113
+ Length radius = xt_css_parse_length(radiusTokens[0]);
114
+ if (radius.unit != Unit::Invalid && radius.unit != Unit::Auto &&
115
+ radius.unit != Unit::None)
116
+ spec->inset_radius = radius;
117
+ }
118
+ }
119
+ std::vector<std::string> tokens = splitTokens(sides);
120
+ if (tokens.empty() || tokens.size() > 4) return false;
121
+ Length values[4];
122
+ for (size_t i = 0; i < tokens.size(); i++) {
123
+ values[i] = xt_css_parse_length(tokens[i]);
124
+ if (values[i].unit == Unit::Invalid) return false;
125
+ }
126
+ switch (tokens.size()) {
127
+ case 1:
128
+ spec->inset_top = spec->inset_right = spec->inset_bottom = spec->inset_left = values[0];
129
+ break;
130
+ case 2:
131
+ spec->inset_top = spec->inset_bottom = values[0];
132
+ spec->inset_right = spec->inset_left = values[1];
133
+ break;
134
+ case 3:
135
+ spec->inset_top = values[0];
136
+ spec->inset_right = spec->inset_left = values[1];
137
+ spec->inset_bottom = values[2];
138
+ break;
139
+ default:
140
+ spec->inset_top = values[0];
141
+ spec->inset_right = values[1];
142
+ spec->inset_bottom = values[2];
143
+ spec->inset_left = values[3];
144
+ break;
145
+ }
146
+ spec->type = ClipPathSpec::Type::Inset;
147
+ return true;
148
+ }
149
+
150
+ bool parseCircle(const std::string &body, ClipPathSpec *spec) {
151
+ std::string radius;
152
+ std::string center;
153
+ if (splitKeyword(body, "at", &radius, &center)) {
154
+ if (!parseCenter(center, &spec->center_x, &spec->center_y)) return false;
155
+ } else {
156
+ radius = body;
157
+ }
158
+ std::vector<std::string> tokens = splitTokens(radius);
159
+ if (!tokens.empty()) {
160
+ Length length = xt_css_parse_length(tokens[0]);
161
+ if (length.unit == Unit::Invalid) return false;
162
+ spec->radius_x = length;
163
+ spec->radius_y = length;
164
+ spec->radius_explicit = true;
165
+ }
166
+ spec->type = ClipPathSpec::Type::Circle;
167
+ return true;
168
+ }
169
+
170
+ bool parseEllipse(const std::string &body, ClipPathSpec *spec) {
171
+ std::string radii = body;
172
+ std::string center;
173
+ splitKeyword(body, "at", &radii, &center);
174
+ if (!center.empty() && !parseCenter(center, &spec->center_x, &spec->center_y)) return false;
175
+ std::vector<std::string> tokens = splitTokens(radii);
176
+ if (!tokens.empty()) {
177
+ Length rx = xt_css_parse_length(tokens[0]);
178
+ Length ry = tokens.size() > 1 ? xt_css_parse_length(tokens[1]) : rx;
179
+ if (rx.unit == Unit::Invalid || ry.unit == Unit::Invalid) return false;
180
+ spec->radius_x = rx;
181
+ spec->radius_y = ry;
182
+ spec->radius_explicit = true;
183
+ }
184
+ spec->type = ClipPathSpec::Type::Ellipse;
185
+ return true;
186
+ }
187
+
188
+ bool parsePolygon(const std::string &body, ClipPathSpec *spec) {
189
+ std::string text = xt_css_trim(body);
190
+ std::string lower = xt_css_lower(text);
191
+ if (lower.rfind("nonzero", 0) == 0) text = xt_css_trim(text.substr(7));
192
+ else if (lower.rfind("evenodd", 0) == 0) text = xt_css_trim(text.substr(7));
193
+ /* Pairs are separated by commas in CSS, but whitespace is accepted too. */
194
+ std::vector<std::string> tokens = splitTokens(text);
195
+ if (tokens.size() < 3) return false;
196
+ std::vector<Length> points;
197
+ for (const std::string &token : tokens) {
198
+ Length length = xt_css_parse_length(token);
199
+ if (length.unit == Unit::Invalid) return false;
200
+ points.push_back(length);
201
+ }
202
+ if (points.size() < 3) return false;
203
+ if (points.size() % 2 != 0) points.pop_back();
204
+ spec->polygon = std::move(points);
205
+ spec->type = ClipPathSpec::Type::Polygon;
206
+ return true;
207
+ }
208
+
209
+ } // namespace
210
+
211
+ bool xt_style_apply_clip_path(XtStyle *style, const std::string &property,
212
+ const std::string &value) {
213
+ if (property != "clip-path") return false;
214
+ std::string text = xt_css_trim(value);
215
+ std::string lower = xt_css_lower(text);
216
+ ClipPathSpec spec;
217
+ if (lower == "none" || text.empty()) {
218
+ style->clip_path = spec;
219
+ return true;
220
+ }
221
+ if (text.size() < 8 || text.back() != ')') {
222
+ style->clip_path = spec;
223
+ return true;
224
+ }
225
+ if (lower.rfind("inset(", 0) == 0) {
226
+ if (parseInset(text.substr(6, text.size() - 7), &spec)) style->clip_path = spec;
227
+ return true;
228
+ }
229
+ if (lower.rfind("circle(", 0) == 0) {
230
+ if (parseCircle(text.substr(7, text.size() - 8), &spec)) style->clip_path = spec;
231
+ return true;
232
+ }
233
+ if (lower.rfind("ellipse(", 0) == 0) {
234
+ if (parseEllipse(text.substr(8, text.size() - 9), &spec)) style->clip_path = spec;
235
+ return true;
236
+ }
237
+ if (lower.rfind("polygon(", 0) == 0) {
238
+ if (parsePolygon(text.substr(8, text.size() - 9), &spec)) style->clip_path = spec;
239
+ return true;
240
+ }
241
+ /* `url(#…)`, `path()` and other shapes are not part of the subset. */
242
+ style->clip_path = spec;
243
+ return true;
244
+ }
245
+
246
+ } // namespace xtgui
@@ -45,6 +45,39 @@ std::vector<std::string> xt_css_split_whitespace(const std::string &text) {
45
45
  return parts;
46
46
  }
47
47
 
48
+ std::vector<std::string> xt_css_split_commas(const std::string &text) {
49
+ std::vector<std::string> parts;
50
+ std::string current;
51
+ int depth = 0;
52
+ char quote = 0;
53
+ for (char c : text) {
54
+ if (quote != 0) {
55
+ current.push_back(c);
56
+ if (c == quote) quote = 0;
57
+ continue;
58
+ }
59
+ if (c == '"' || c == '\'') {
60
+ quote = c;
61
+ current.push_back(c);
62
+ } else if (c == '(' || c == '[') {
63
+ depth++;
64
+ current.push_back(c);
65
+ } else if (c == ')' || c == ']') {
66
+ if (depth > 0) depth--;
67
+ current.push_back(c);
68
+ } else if (c == ',' && depth == 0) {
69
+ std::string part = xt_css_trim(current);
70
+ if (!part.empty()) parts.push_back(part);
71
+ current.clear();
72
+ } else {
73
+ current.push_back(c);
74
+ }
75
+ }
76
+ std::string part = xt_css_trim(current);
77
+ if (!part.empty()) parts.push_back(part);
78
+ return parts;
79
+ }
80
+
48
81
  /* -- numbers / lengths / colors ------------------------------------------- */
49
82
 
50
83
  bool xt_css_parse_number(const std::string &value, float *out) {
@@ -59,6 +92,60 @@ bool xt_css_parse_number(const std::string &value, float *out) {
59
92
  return true;
60
93
  }
61
94
 
95
+
96
+ /** Split a CSS function's argument list at top-level commas. */
97
+ static std::vector<std::string> splitFunctionArgs(const std::string &text) {
98
+ std::vector<std::string> args;
99
+ std::string current;
100
+ int depth = 0;
101
+ for (char c : text) {
102
+ if (c == '(') depth++;
103
+ if (c == ')') depth--;
104
+ if (c == ',' && depth == 0) {
105
+ args.push_back(xt_css_trim(current));
106
+ current.clear();
107
+ } else {
108
+ current.push_back(c);
109
+ }
110
+ }
111
+ if (!current.empty() || !args.empty()) args.push_back(xt_css_trim(current));
112
+ return args;
113
+ }
114
+
115
+ /** `clamp(min, value, max)`, `min(...)` and `max(...)` over lengths. The
116
+ * arguments stay unresolved; layout resolves them against the element's em and
117
+ * the viewport. `env(safe-area-inset-*)` has no notch information in a desktop
118
+ * window, so it resolves to zero (its fallback is ignored, matching the
119
+ * browser's value without insets). */
120
+ static Length parseFunctionLength(const std::string &name, const std::string &inner) {
121
+ Length result;
122
+ if (name == "env") {
123
+ result.unit = Unit::Px;
124
+ return result;
125
+ }
126
+ if (name != "clamp" && name != "min" && name != "max") {
127
+ result.unit = Unit::Invalid;
128
+ return result;
129
+ }
130
+ std::vector<std::string> args = splitFunctionArgs(inner);
131
+ size_t expected = name == "clamp" ? 3 : 1;
132
+ if (args.size() < expected) {
133
+ result.unit = Unit::Invalid;
134
+ return result;
135
+ }
136
+ result.unit = name == "clamp" ? Unit::Clamp : (name == "min" ? Unit::Min : Unit::Max);
137
+ for (const std::string &arg : args) {
138
+ Length value = xt_css_parse_length(arg);
139
+ if (value.unit == Unit::Invalid) {
140
+ result.unit = Unit::Invalid;
141
+ result.args.clear();
142
+ return result;
143
+ }
144
+ result.args.push_back(value);
145
+ }
146
+ return result;
147
+ }
148
+
62
149
  Length xt_css_parse_length(const std::string &value) {
63
150
  Length length;
64
151
  std::string text = xt_css_lower(xt_css_trim(value));
@@ -74,6 +161,13 @@ Length xt_css_parse_length(const std::string &value) {
74
161
  length.unit = Unit::None;
75
162
  return length;
76
163
  }
164
+ size_t open = text.find('(');
165
+ if (open != std::string::npos && !text.empty() && text.back() == ')') {
166
+ std::string name = text.substr(0, open);
167
+ if (name == "clamp" || name == "min" || name == "max" || name == "env") {
168
+ return parseFunctionLength(name, text.substr(open + 1, text.size() - open - 2));
169
+ }
170
+ }
77
171
  char *end = nullptr;
78
172
  float number = strtof(text.c_str(), &end);
79
173
  if (end == text.c_str()) {
@@ -17,11 +17,15 @@
17
17
 
18
18
  namespace xtgui {
19
19
 
20
- enum class Unit { Auto, None, Px, Percent, Em, Rem, Vw, Vh, Number, Invalid };
20
+ enum class Unit { Auto, None, Px, Percent, Em, Rem, Vw, Vh, Number, Clamp, Min, Max, Invalid };
21
21
 
22
22
  struct Length {
23
23
  float value = 0.0f;
24
24
  Unit unit = Unit::Auto;
25
+ /* Arguments of `clamp(...)`, `min(...)` and `max(...)`; empty for a scalar
26
+ * length. Kept unresolved so vw/vh/em are resolved against the element that
27
+ * uses the length, not the viewport at parse time. */
28
+ std::vector<Length> args = {};
25
29
  };
26
30
 
27
31
  struct Color {
@@ -52,6 +56,9 @@ struct CompoundSelector {
52
56
  std::vector<std::string> classes;
53
57
  std::vector<AttrSelector> attrs;
54
58
  std::vector<std::string> pseudoClasses;
59
+ /** `before`/`after` for a `::before`/`::after` (or legacy `:before`)
60
+ * pseudo-element; empty for an ordinary selector. */
61
+ std::string pseudo_element;
55
62
  };
56
63
 
57
64
  enum class Combinator { Descendant, Child, Adjacent, Sibling };
@@ -66,6 +73,10 @@ struct Rule {
66
73
  std::vector<ComplexSelector> selectors;
67
74
  std::vector<Declaration> declarations;
68
75
  int order = 0;
76
+ /** Raw `@media` prelude this rule came from (empty = unconditional). The
77
+ * query is evaluated against the current viewport during the cascade, so a
78
+ * resize restyles with the right rules. */
79
+ std::string media;
69
80
  };
70
81
 
71
82
  struct StyleSheet {
@@ -85,6 +96,16 @@ std::vector<Declaration> xt_css_parse_declarations(const std::string &text);
85
96
  bool xt_css_match(const Node *node, const ComplexSelector &selector);
86
97
  bool xt_css_match(const Node *node, const ComplexSelector &selector, const MatchState &state);
87
98
 
99
+ /** True when a raw `@media` prelude matches a viewport. Supports media types
100
+ * (`all`/`screen`), comma-separated query lists, `not`/`only`, `and`, and the
101
+ * width/height/orientation/hover/pointer/colour-scheme features (with
102
+ * `min-`/`max-` ranges). Unknown features never match, as in a browser. */
103
+ bool xt_css_media_matches(const std::string &query, float viewport_width, float viewport_height);
104
+
105
+ /** Split `text` at top-level commas (outside `(...)`, `[...]` and quotes),
106
+ * trimming each part and dropping empty ones. */
107
+ std::vector<std::string> xt_css_split_commas(const std::string &text);
108
+
88
109
  Length xt_css_parse_length(const std::string &value);
89
110
  Color xt_css_parse_color(const std::string &value);
90
111
  bool xt_css_parse_number(const std::string &value, float *out);
@@ -132,6 +132,9 @@ bool matchPseudo(const Node *node, const std::string &pseudo, const MatchState &
132
132
 
133
133
  bool matchCompound(const Node *node, const CompoundSelector &compound, const MatchState &state) {
134
134
  if (!node->isElement()) return false;
135
+ /* A pseudo-element selector matches a generated node, never the origin
136
+ * element; the cascade matches it separately in `style.cpp`. */
137
+ if (!compound.pseudo_element.empty()) return false;
135
138
  if (!compound.tag.empty() && node->tag != compound.tag) return false;
136
139
  if (!compound.id.empty()) {
137
140
  const std::string *id = node->attr("id");
@@ -16,11 +16,29 @@ class SelectorParser {
16
16
  std::vector<ComplexSelector> selectors;
17
17
  size_t start = 0;
18
18
  int depth = 0;
19
+ char quote = 0;
20
+ /* Split at top-level commas. A selector list may be formatted across any
21
+ * number of lines: newlines are ordinary CSS whitespace, and `parseComplex`
22
+ * turns a whitespace run into a descendant combinator. Quoted attribute
23
+ * values are skipped so a `,`/`(`/`[` inside them cannot unbalance the
24
+ * split (an attribute value may itself span lines). */
19
25
  for (size_t i = 0; i <= text_.size(); i++) {
20
26
  char c = (i < text_.size()) ? text_[i] : ',';
21
- if (c == '(' || c == '[') depth++;
22
- else if (c == ')' || c == ']') depth--;
23
- else if (c == ',' && depth == 0) {
27
+ if (quote != 0) {
28
+ if (c == '\\' && i + 1 < text_.size()) {
29
+ i++;
30
+ continue;
31
+ }
32
+ if (c == quote) quote = 0;
33
+ continue;
34
+ }
35
+ if (c == '"' || c == '\'') {
36
+ quote = c;
37
+ } else if (c == '(' || c == '[') {
38
+ depth++;
39
+ } else if (c == ')' || c == ']') {
40
+ if (depth > 0) depth--;
41
+ } else if (c == ',' && depth == 0) {
24
42
  std::string part = text_.substr(start, i - start);
25
43
  ComplexSelector selector;
26
44
  if (parseComplex(part, &selector) && !selector.compounds.empty()) {
@@ -72,6 +90,13 @@ class SelectorParser {
72
90
  }
73
91
  std::string name = xt_css_lower(readIdent(text, index));
74
92
  if (name.empty()) return false;
93
+ /* `::before`/`::after` are generated-content elements, not states. The
94
+ * single-colon spellings (`:before`) are the legacy equivalent. */
95
+ if (pseudoElement || name == "before" || name == "after") {
96
+ compound->pseudo_element = name;
97
+ any = true;
98
+ continue;
99
+ }
75
100
  if (!pseudoElement && *index < text.size() && text[*index] == '(') {
76
101
  int depth = 0;
77
102
  size_t start = *index;
@@ -192,6 +217,8 @@ class SelectorParser {
192
217
  if (!compound.id.empty()) ids++;
193
218
  classes += computeCompoundSpecificity(compound);
194
219
  if (!compound.tag.empty()) types++;
220
+ /* A pseudo-element counts like a type selector (CSS specificity). */
221
+ if (!compound.pseudo_element.empty()) types++;
195
222
  }
196
223
  return (ids << 20) | (classes << 10) | types;
197
224
  }