@nstudio/nativescript-effect-ui 0.1.1 → 0.1.2

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 (90) hide show
  1. package/CHANGELOG.md +79 -0
  2. package/README.md +28 -0
  3. package/dist/linux/LinuxHost.js +9 -1
  4. package/dist/linux/LinuxHost.js.map +1 -1
  5. package/dist/macos/MacosHost.d.ts +13 -0
  6. package/dist/macos/MacosHost.d.ts.map +1 -1
  7. package/dist/macos/MacosHost.js +373 -12
  8. package/dist/macos/MacosHost.js.map +1 -1
  9. package/dist/macos/styles.d.ts.map +1 -1
  10. package/dist/macos/styles.js +4 -1
  11. package/dist/macos/styles.js.map +1 -1
  12. package/dist/mason/MasonHost.d.ts +35 -1
  13. package/dist/mason/MasonHost.d.ts.map +1 -1
  14. package/dist/mason/MasonHost.js +330 -18
  15. package/dist/mason/MasonHost.js.map +1 -1
  16. package/dist/mason/dialog.d.ts +24 -1
  17. package/dist/mason/dialog.d.ts.map +1 -1
  18. package/dist/mason/dialog.js +162 -2
  19. package/dist/mason/dialog.js.map +1 -1
  20. package/dist/mason/elements.d.ts.map +1 -1
  21. package/dist/mason/elements.js +33 -6
  22. package/dist/mason/elements.js.map +1 -1
  23. package/dist/mason/events.d.ts +4 -0
  24. package/dist/mason/events.d.ts.map +1 -1
  25. package/dist/mason/events.js +110 -2
  26. package/dist/mason/events.js.map +1 -1
  27. package/dist/mason/index.d.ts +2 -1
  28. package/dist/mason/index.d.ts.map +1 -1
  29. package/dist/mason/index.js +2 -1
  30. package/dist/mason/index.js.map +1 -1
  31. package/dist/mason/input-native.d.ts +32 -0
  32. package/dist/mason/input-native.d.ts.map +1 -0
  33. package/dist/mason/input-native.js +280 -0
  34. package/dist/mason/input-native.js.map +1 -0
  35. package/dist/mason/media.d.ts +18 -0
  36. package/dist/mason/media.d.ts.map +1 -0
  37. package/dist/mason/media.js +255 -0
  38. package/dist/mason/media.js.map +1 -0
  39. package/dist/mason/select.d.ts +25 -0
  40. package/dist/mason/select.d.ts.map +1 -0
  41. package/dist/mason/select.js +75 -0
  42. package/dist/mason/select.js.map +1 -0
  43. package/dist/mason/services.d.ts +2 -1
  44. package/dist/mason/services.d.ts.map +1 -1
  45. package/dist/mason/services.js +61 -75
  46. package/dist/mason/services.js.map +1 -1
  47. package/dist/mason/styles.d.ts +16 -0
  48. package/dist/mason/styles.d.ts.map +1 -1
  49. package/dist/mason/styles.js +138 -19
  50. package/dist/mason/styles.js.map +1 -1
  51. package/dist/mason/svg.d.ts +86 -0
  52. package/dist/mason/svg.d.ts.map +1 -0
  53. package/dist/mason/svg.js +932 -0
  54. package/dist/mason/svg.js.map +1 -0
  55. package/dist/mason/video.d.ts +40 -0
  56. package/dist/mason/video.d.ts.map +1 -0
  57. package/dist/mason/video.js +641 -0
  58. package/dist/mason/video.js.map +1 -0
  59. package/dist/motion/index.d.ts +8 -8
  60. package/dist/platform/intl-mapping.d.ts +124 -0
  61. package/dist/platform/intl-mapping.d.ts.map +1 -0
  62. package/dist/platform/intl-mapping.js +540 -0
  63. package/dist/platform/intl-mapping.js.map +1 -0
  64. package/dist/platform/intl-polyfill.d.ts +60 -0
  65. package/dist/platform/intl-polyfill.d.ts.map +1 -0
  66. package/dist/platform/intl-polyfill.js +389 -0
  67. package/dist/platform/intl-polyfill.js.map +1 -0
  68. package/dist/platform/intl.d.ts +9 -0
  69. package/dist/platform/intl.d.ts.map +1 -0
  70. package/dist/platform/intl.js +219 -0
  71. package/dist/platform/intl.js.map +1 -0
  72. package/dist/postcss/color.d.ts +18 -0
  73. package/dist/postcss/color.d.ts.map +1 -0
  74. package/dist/postcss/color.js +620 -0
  75. package/dist/postcss/color.js.map +1 -0
  76. package/dist/postcss/index.d.ts +39 -0
  77. package/dist/postcss/index.d.ts.map +1 -0
  78. package/dist/postcss/index.js +249 -0
  79. package/dist/postcss/index.js.map +1 -0
  80. package/dist/postcss/values.d.ts +49 -0
  81. package/dist/postcss/values.d.ts.map +1 -0
  82. package/dist/postcss/values.js +259 -0
  83. package/dist/postcss/values.js.map +1 -0
  84. package/dist/testing/MockMasonHost.d.ts +2 -2
  85. package/dist/testing/MockMasonHost.d.ts.map +1 -1
  86. package/dist/testing/MockMasonHost.js +1 -1
  87. package/dist/testing/MockMasonHost.js.map +1 -1
  88. package/dist/windows/WindowsHost.js +9 -1
  89. package/dist/windows/WindowsHost.js.map +1 -1
  90. package/package.json +31 -3
@@ -0,0 +1,932 @@
1
+ /**
2
+ * Native `<svg>` rendering for `MasonHost`.
3
+ *
4
+ * The SVG subtree (`<g>`, `<path>`, `<circle>`, ...) stays in MasonHost's
5
+ * logical tree only; the host hands the outer `<svg>` element a plain
6
+ * {@link SvgNode} snapshot of it, {@link buildSvgScene} flattens that into
7
+ * absolute drawing commands in view coordinates, and the element draws them
8
+ * with CAShapeLayers (iOS) or a Canvas overlay drawable (Android).
9
+ */
10
+ import { Color, CSSType, colorProperty, Utils } from '@nativescript/core';
11
+ import { Div } from '@triniwiz/nativescript-masonkit';
12
+ import { PATH_CLOSE, PATH_CUBIC, PATH_LINE, PATH_MOVE, PATH_QUAD, pathCommands, } from '@triniwiz/nativescript-masonkit/svg-path-commands.js';
13
+ /** Tags whose geometry the scene builder draws (plus `a`, drawn as a group). */
14
+ export const SVG_TAGS = new Set([
15
+ 'svg',
16
+ 'g',
17
+ 'path',
18
+ 'circle',
19
+ 'ellipse',
20
+ 'rect',
21
+ 'line',
22
+ 'polyline',
23
+ 'polygon',
24
+ ]);
25
+ export { PATH_CLOSE, PATH_CUBIC, PATH_LINE, PATH_MOVE, PATH_QUAD };
26
+ const IDENTITY = [1, 0, 0, 1, 0, 0];
27
+ const multiply = (m, n) => [
28
+ m[0] * n[0] + m[2] * n[1],
29
+ m[1] * n[0] + m[3] * n[1],
30
+ m[0] * n[2] + m[2] * n[3],
31
+ m[1] * n[2] + m[3] * n[3],
32
+ m[0] * n[4] + m[2] * n[5] + m[4],
33
+ m[1] * n[4] + m[3] * n[5] + m[5],
34
+ ];
35
+ const translate = (x, y) => [1, 0, 0, 1, x, y];
36
+ const NUMBER = /[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/g;
37
+ const numbers = (text) => text === undefined ? [] : (text.match(NUMBER) ?? []).map(Number);
38
+ const LENGTH = /^([+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)\s*(px|%|em|rem|pt|pc|mm|cm|in)?$/i;
39
+ const UNIT_PX = {
40
+ px: 1,
41
+ em: 16,
42
+ rem: 16,
43
+ pt: 4 / 3,
44
+ pc: 16,
45
+ mm: 96 / 25.4,
46
+ cm: 96 / 2.54,
47
+ in: 96,
48
+ };
49
+ /** An SVG length in user units; `%` resolves against `reference`. */
50
+ function length(text, reference) {
51
+ if (text === undefined)
52
+ return undefined;
53
+ const m = LENGTH.exec(text.trim());
54
+ if (!m)
55
+ return undefined;
56
+ const n = Number(m[1]);
57
+ const unit = (m[2] ?? 'px').toLowerCase();
58
+ if (unit === '%')
59
+ return (n / 100) * reference;
60
+ return n * (UNIT_PX[unit] ?? 1);
61
+ }
62
+ const DEG = Math.PI / 180;
63
+ /** Parses an SVG `transform` list into one matrix (left-most applies last). */
64
+ export function parseTransform(text) {
65
+ if (!text)
66
+ return IDENTITY;
67
+ let m = IDENTITY;
68
+ const re = /(matrix|translate|scale|rotate|skewX|skewY)\s*\(([^)]*)\)/gi;
69
+ let match;
70
+ while ((match = re.exec(text))) {
71
+ const args = numbers(match[2]);
72
+ let op;
73
+ switch (match[1].toLowerCase()) {
74
+ case 'matrix':
75
+ if (args.length >= 6)
76
+ op = [args[0], args[1], args[2], args[3], args[4], args[5]];
77
+ break;
78
+ case 'translate':
79
+ if (args.length >= 1)
80
+ op = translate(args[0], args[1] ?? 0);
81
+ break;
82
+ case 'scale':
83
+ if (args.length >= 1)
84
+ op = [args[0], 0, 0, args[1] ?? args[0], 0, 0];
85
+ break;
86
+ case 'rotate': {
87
+ if (args.length < 1)
88
+ break;
89
+ const a = args[0] * DEG;
90
+ const cos = Math.cos(a);
91
+ const sin = Math.sin(a);
92
+ const rotation = [cos, sin, -sin, cos, 0, 0];
93
+ if (args.length >= 3) {
94
+ const cx = args[1];
95
+ const cy = args[2];
96
+ op = multiply(multiply(translate(cx, cy), rotation), translate(-cx, -cy));
97
+ }
98
+ else {
99
+ op = rotation;
100
+ }
101
+ break;
102
+ }
103
+ case 'skewx':
104
+ if (args.length >= 1)
105
+ op = [1, 0, Math.tan(args[0] * DEG), 1, 0, 0];
106
+ break;
107
+ case 'skewy':
108
+ if (args.length >= 1)
109
+ op = [1, Math.tan(args[0] * DEG), 0, 1, 0, 0];
110
+ break;
111
+ }
112
+ if (op)
113
+ m = multiply(m, op);
114
+ }
115
+ return m;
116
+ }
117
+ /** The viewBox -> viewport matrix per `preserveAspectRatio` (default `xMidYMid meet`). */
118
+ function viewBoxMatrix(viewBox, preserveAspectRatio, width, height) {
119
+ const vb = numbers(viewBox);
120
+ if (vb.length < 4)
121
+ return undefined;
122
+ const [minX, minY, vbWidth, vbHeight] = vb;
123
+ if (vbWidth <= 0 || vbHeight <= 0)
124
+ return null;
125
+ let sx = width / vbWidth;
126
+ let sy = height / vbHeight;
127
+ const par = (preserveAspectRatio ?? '').trim().split(/\s+/);
128
+ const align = par[0] && par[0] !== 'defer' ? par[0] : (par[1] ?? 'xMidYMid');
129
+ if (align !== 'none') {
130
+ const slice = par.includes('slice');
131
+ sx = sy = slice ? Math.max(sx, sy) : Math.min(sx, sy);
132
+ }
133
+ let tx = -minX * sx;
134
+ let ty = -minY * sy;
135
+ const lower = align.toLowerCase();
136
+ if (lower !== 'none') {
137
+ const extraX = width - vbWidth * sx;
138
+ const extraY = height - vbHeight * sy;
139
+ if (lower.includes('xmid'))
140
+ tx += extraX / 2;
141
+ else if (lower.includes('xmax'))
142
+ tx += extraX;
143
+ if (lower.includes('ymid'))
144
+ ty += extraY / 2;
145
+ else if (lower.includes('ymax'))
146
+ ty += extraY;
147
+ }
148
+ return [sx, 0, 0, sy, tx, ty];
149
+ }
150
+ const clamp01 = (n) => (Number.isFinite(n) ? Math.min(1, Math.max(0, n)) : 1);
151
+ function opacityValue(text, fallback) {
152
+ if (text === undefined)
153
+ return fallback;
154
+ const t = text.trim();
155
+ if (t.endsWith('%')) {
156
+ const n = Number(t.slice(0, -1));
157
+ return Number.isFinite(n) ? clamp01(n / 100) : fallback;
158
+ }
159
+ const n = Number(t);
160
+ return Number.isFinite(n) ? clamp01(n) : fallback;
161
+ }
162
+ /** Lower-cases attribute names and folds `style="fill: red"` declarations over them. */
163
+ function presentation(attrs) {
164
+ const out = new Map();
165
+ for (const key of Object.keys(attrs)) {
166
+ const value = attrs[key];
167
+ if (typeof value === 'string')
168
+ out.set(key.toLowerCase(), value);
169
+ }
170
+ const style = out.get('style');
171
+ if (style) {
172
+ for (const decl of style.split(';')) {
173
+ const colon = decl.indexOf(':');
174
+ if (colon <= 0)
175
+ continue;
176
+ const prop = decl.slice(0, colon).trim().toLowerCase();
177
+ const value = decl.slice(colon + 1).replace(/!important/i, '').trim();
178
+ if (prop && value)
179
+ out.set(prop, value);
180
+ }
181
+ }
182
+ return out;
183
+ }
184
+ function inherit(parent, p) {
185
+ const pick = (name, current) => {
186
+ const v = p.get(name)?.trim();
187
+ return v === undefined || v === '' || v === 'inherit' ? current : v;
188
+ };
189
+ const enumOf = (name, current, allowed) => {
190
+ const v = p.get(name)?.trim().toLowerCase();
191
+ return v !== undefined && allowed.includes(v) ? v : current;
192
+ };
193
+ const visibility = p.get('visibility')?.trim().toLowerCase();
194
+ const miter = Number(p.get('stroke-miterlimit'));
195
+ return {
196
+ fill: pick('fill', parent.fill),
197
+ fillOpacity: opacityValue(p.get('fill-opacity'), parent.fillOpacity),
198
+ fillRule: enumOf('fill-rule', parent.fillRule, ['nonzero', 'evenodd']),
199
+ stroke: pick('stroke', parent.stroke),
200
+ strokeOpacity: opacityValue(p.get('stroke-opacity'), parent.strokeOpacity),
201
+ strokeWidth: pick('stroke-width', parent.strokeWidth),
202
+ lineCap: enumOf('stroke-linecap', parent.lineCap, ['butt', 'round', 'square']),
203
+ lineJoin: enumOf('stroke-linejoin', parent.lineJoin, ['miter', 'round', 'bevel']),
204
+ miterLimit: Number.isFinite(miter) && miter >= 1 ? miter : parent.miterLimit,
205
+ dashArray: pick('stroke-dasharray', parent.dashArray),
206
+ dashOffset: pick('stroke-dashoffset', parent.dashOffset),
207
+ color: pick('color', parent.color),
208
+ visible: visibility === 'hidden' || visibility === 'collapse'
209
+ ? false
210
+ : visibility === 'visible'
211
+ ? true
212
+ : parent.visible,
213
+ };
214
+ }
215
+ /** Resolves an SVG paint to a CSS colour, or `undefined` for no paint. */
216
+ function resolvePaint(paint, color) {
217
+ let value = paint.trim();
218
+ if (value.toLowerCase().startsWith('url(')) {
219
+ // Paint servers (gradients, patterns) are not drawn; use the fallback colour if any.
220
+ const close = value.indexOf(')');
221
+ value = close < 0 ? '' : value.slice(close + 1).trim();
222
+ if (!value)
223
+ return undefined;
224
+ }
225
+ const lower = value.toLowerCase();
226
+ if (lower === 'none' || lower === 'transparent' || lower === '')
227
+ return undefined;
228
+ if (lower === 'currentcolor')
229
+ return resolvePaint(color, 'black');
230
+ return value;
231
+ }
232
+ const KAPPA = 0.5522847498307936;
233
+ function ellipseCommands(cx, cy, rx, ry) {
234
+ const kx = KAPPA * rx;
235
+ const ky = KAPPA * ry;
236
+ return [
237
+ PATH_MOVE, cx + rx, cy,
238
+ PATH_CUBIC, cx + rx, cy + ky, cx + kx, cy + ry, cx, cy + ry,
239
+ PATH_CUBIC, cx - kx, cy + ry, cx - rx, cy + ky, cx - rx, cy,
240
+ PATH_CUBIC, cx - rx, cy - ky, cx - kx, cy - ry, cx, cy - ry,
241
+ PATH_CUBIC, cx + kx, cy - ry, cx + rx, cy - ky, cx + rx, cy,
242
+ PATH_CLOSE,
243
+ ];
244
+ }
245
+ function rectCommands(x, y, w, h, rx, ry) {
246
+ if (rx <= 0 || ry <= 0) {
247
+ return [PATH_MOVE, x, y, PATH_LINE, x + w, y, PATH_LINE, x + w, y + h, PATH_LINE, x, y + h, PATH_CLOSE];
248
+ }
249
+ const kx = KAPPA * rx;
250
+ const ky = KAPPA * ry;
251
+ const r = x + w;
252
+ const b = y + h;
253
+ return [
254
+ PATH_MOVE, x + rx, y,
255
+ PATH_LINE, r - rx, y,
256
+ PATH_CUBIC, r - rx + kx, y, r, y + ry - ky, r, y + ry,
257
+ PATH_LINE, r, b - ry,
258
+ PATH_CUBIC, r, b - ry + ky, r - rx + kx, b, r - rx, b,
259
+ PATH_LINE, x + rx, b,
260
+ PATH_CUBIC, x + rx - kx, b, x, b - ry + ky, x, b - ry,
261
+ PATH_LINE, x, y + ry,
262
+ PATH_CUBIC, x, y + ry - ky, x + rx - kx, y, x + rx, y,
263
+ PATH_CLOSE,
264
+ ];
265
+ }
266
+ function pointsCommands(text, close) {
267
+ const pts = numbers(text);
268
+ const count = Math.floor(pts.length / 2);
269
+ if (count < 2)
270
+ return [];
271
+ const out = [PATH_MOVE, pts[0], pts[1]];
272
+ for (let i = 1; i < count; i++)
273
+ out.push(PATH_LINE, pts[i * 2], pts[i * 2 + 1]);
274
+ if (close)
275
+ out.push(PATH_CLOSE);
276
+ return out;
277
+ }
278
+ /** The element's geometry in its own user space, or empty when it has none. */
279
+ export function shapeCommands(tag, p, viewport) {
280
+ const w = viewport.width;
281
+ const h = viewport.height;
282
+ const diag = Math.sqrt((w * w + h * h) / 2);
283
+ switch (tag) {
284
+ case 'path': {
285
+ const d = p.get('d');
286
+ return d ? pathCommands(d) : [];
287
+ }
288
+ case 'circle': {
289
+ const r = length(p.get('r'), diag) ?? 0;
290
+ if (r <= 0)
291
+ return [];
292
+ return ellipseCommands(length(p.get('cx'), w) ?? 0, length(p.get('cy'), h) ?? 0, r, r);
293
+ }
294
+ case 'ellipse': {
295
+ let rx = length(p.get('rx'), w);
296
+ let ry = length(p.get('ry'), h);
297
+ rx ??= ry;
298
+ ry ??= rx;
299
+ if (rx === undefined || ry === undefined || rx <= 0 || ry <= 0)
300
+ return [];
301
+ return ellipseCommands(length(p.get('cx'), w) ?? 0, length(p.get('cy'), h) ?? 0, rx, ry);
302
+ }
303
+ case 'rect': {
304
+ const rw = length(p.get('width'), w) ?? 0;
305
+ const rh = length(p.get('height'), h) ?? 0;
306
+ if (rw <= 0 || rh <= 0)
307
+ return [];
308
+ let rx = length(p.get('rx'), w);
309
+ let ry = length(p.get('ry'), h);
310
+ rx ??= ry;
311
+ ry ??= rx;
312
+ return rectCommands(length(p.get('x'), w) ?? 0, length(p.get('y'), h) ?? 0, rw, rh, Math.min(Math.max(rx ?? 0, 0), rw / 2), Math.min(Math.max(ry ?? 0, 0), rh / 2));
313
+ }
314
+ case 'line':
315
+ return [
316
+ PATH_MOVE, length(p.get('x1'), w) ?? 0, length(p.get('y1'), h) ?? 0,
317
+ PATH_LINE, length(p.get('x2'), w) ?? 0, length(p.get('y2'), h) ?? 0,
318
+ ];
319
+ case 'polyline':
320
+ return pointsCommands(p.get('points'), false);
321
+ case 'polygon':
322
+ return pointsCommands(p.get('points'), true);
323
+ default:
324
+ return [];
325
+ }
326
+ }
327
+ function transformCommands(commands, m) {
328
+ const out = [];
329
+ for (let i = 0; i < commands.length;) {
330
+ const op = commands[i++];
331
+ out.push(op);
332
+ const pairs = op === PATH_CUBIC ? 3 : op === PATH_QUAD ? 2 : op === PATH_CLOSE ? 0 : 1;
333
+ for (let k = 0; k < pairs; k++) {
334
+ const x = commands[i++];
335
+ const y = commands[i++];
336
+ out.push(m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]);
337
+ }
338
+ }
339
+ return out;
340
+ }
341
+ const ROOT_INHERITED = {
342
+ fill: 'black',
343
+ fillOpacity: 1,
344
+ fillRule: 'nonzero',
345
+ stroke: 'none',
346
+ strokeOpacity: 1,
347
+ strokeWidth: '1',
348
+ lineCap: 'butt',
349
+ lineJoin: 'miter',
350
+ miterLimit: 4,
351
+ dashArray: 'none',
352
+ dashOffset: '0',
353
+ color: 'currentColor',
354
+ visible: true,
355
+ };
356
+ const CONTAINER_TAGS = new Set(['g', 'a', 'svg']);
357
+ function visit(node, ctx, out, isRoot) {
358
+ const tag = node.tag.toLowerCase();
359
+ const p = presentation(node.attrs);
360
+ if (p.get('display')?.trim().toLowerCase() === 'none')
361
+ return;
362
+ const inherited = inherit(ctx.inherited, p);
363
+ if (tag === 'svg' && !isRoot) {
364
+ const vw = ctx.viewport.width;
365
+ const vh = ctx.viewport.height;
366
+ const width = length(p.get('width'), vw) ?? vw;
367
+ const height = length(p.get('height'), vh) ?? vh;
368
+ if (width <= 0 || height <= 0)
369
+ return;
370
+ const vbm = viewBoxMatrix(p.get('viewbox'), p.get('preserveaspectratio'), width, height);
371
+ if (vbm === null)
372
+ return;
373
+ const vb = numbers(p.get('viewbox'));
374
+ const inner = {
375
+ inherited,
376
+ matrix: multiply(multiply(ctx.matrix, translate(length(p.get('x'), vw) ?? 0, length(p.get('y'), vh) ?? 0)), vbm ?? IDENTITY),
377
+ opacity: ctx.opacity * opacityValue(p.get('opacity'), 1),
378
+ viewport: vbm ? { width: vb[2], height: vb[3] } : { width, height },
379
+ };
380
+ for (const child of node.children)
381
+ visit(child, inner, out, false);
382
+ return;
383
+ }
384
+ // The outer <svg> is a CSS box: its opacity and transform belong to the view.
385
+ const matrix = isRoot ? ctx.matrix : multiply(ctx.matrix, parseTransform(p.get('transform')));
386
+ const opacity = isRoot ? ctx.opacity : ctx.opacity * opacityValue(p.get('opacity'), 1);
387
+ if (CONTAINER_TAGS.has(tag)) {
388
+ const inner = { inherited, matrix, opacity, viewport: ctx.viewport };
389
+ for (const child of node.children)
390
+ visit(child, inner, out, false);
391
+ return;
392
+ }
393
+ if (!SVG_TAGS.has(tag) || !inherited.visible || opacity <= 0)
394
+ return;
395
+ const local = shapeCommands(tag, p, ctx.viewport);
396
+ if (local.length === 0)
397
+ return;
398
+ const vw = ctx.viewport.width;
399
+ const vh = ctx.viewport.height;
400
+ const diag = Math.sqrt((vw * vw + vh * vh) / 2);
401
+ const scale = Math.sqrt(Math.abs(matrix[0] * matrix[3] - matrix[1] * matrix[2]));
402
+ const fill = tag === 'line' ? undefined : resolvePaint(inherited.fill, inherited.color);
403
+ const strokeWidth = (length(inherited.strokeWidth, diag) ?? 1) * scale;
404
+ const stroke = strokeWidth > 0 ? resolvePaint(inherited.stroke, inherited.color) : undefined;
405
+ if (fill === undefined && stroke === undefined)
406
+ return;
407
+ const dashes = inherited.dashArray.trim().toLowerCase() === 'none' ? [] : numbers(inherited.dashArray);
408
+ const validDashes = dashes.length > 0 && dashes.every((d) => d >= 0) && dashes.some((d) => d > 0);
409
+ const dashArray = validDashes
410
+ ? (dashes.length % 2 === 1 ? [...dashes, ...dashes] : dashes).map((d) => d * scale)
411
+ : undefined;
412
+ out.push({
413
+ commands: transformCommands(local, matrix),
414
+ fill,
415
+ fillOpacity: inherited.fillOpacity,
416
+ fillRule: inherited.fillRule,
417
+ stroke,
418
+ strokeOpacity: inherited.strokeOpacity,
419
+ strokeWidth: stroke === undefined ? 0 : strokeWidth,
420
+ lineCap: inherited.lineCap,
421
+ lineJoin: inherited.lineJoin,
422
+ miterLimit: inherited.miterLimit,
423
+ dashArray,
424
+ dashOffset: (length(inherited.dashOffset, diag) ?? 0) * scale,
425
+ opacity,
426
+ });
427
+ }
428
+ /**
429
+ * Flattens an `<svg>` subtree into shapes in view coordinates for a view of
430
+ * `box` size (DIPs). `currentColor` is the CSS `color` the `<svg>` element
431
+ * inherits (defaults to black).
432
+ */
433
+ export function buildSvgScene(root, box, currentColor) {
434
+ const shapes = [];
435
+ const empty = { width: box.width, height: box.height, shapes };
436
+ if (!(box.width > 0) || !(box.height > 0))
437
+ return empty;
438
+ const p = presentation(root.attrs);
439
+ const vbm = viewBoxMatrix(p.get('viewbox'), p.get('preserveaspectratio'), box.width, box.height);
440
+ if (vbm === null)
441
+ return empty;
442
+ const vb = numbers(p.get('viewbox'));
443
+ visit(root, {
444
+ inherited: { ...ROOT_INHERITED, color: currentColor?.trim() || 'black' },
445
+ matrix: vbm ?? IDENTITY,
446
+ opacity: 1,
447
+ viewport: vbm ? { width: vb[2], height: vb[3] } : box,
448
+ }, shapes, true);
449
+ return empty;
450
+ }
451
+ /** Snapshots a host's logical subtree rooted at an `<svg>` element as an {@link SvgNode}. */
452
+ export function collectSvgTree(root, access) {
453
+ const walk = (node, tag) => {
454
+ const children = [];
455
+ for (const child of access.childrenOf(node)) {
456
+ const childTag = access.tagOf(child);
457
+ if (childTag === undefined || childTag.startsWith('#'))
458
+ continue;
459
+ children.push(walk(child, childTag));
460
+ }
461
+ return { tag, attrs: access.attrsOf(node), children };
462
+ };
463
+ return walk(root, access.tagOf(root) ?? 'svg');
464
+ }
465
+ /** Pushes the current logical subtree to an element created by {@link createSvgElementClass}. */
466
+ export function setSvgTree(view, root) {
467
+ view.setSvgTree?.(root);
468
+ }
469
+ const isApple = () => typeof __APPLE__ !== 'undefined' && __APPLE__;
470
+ const isAndroid = () => typeof __ANDROID__ !== 'undefined' && __ANDROID__;
471
+ function cssOfColor(value) {
472
+ if (typeof value === 'string')
473
+ return value;
474
+ const c = value;
475
+ if (!c || typeof c.r !== 'number' || typeof c.g !== 'number' || typeof c.b !== 'number')
476
+ return undefined;
477
+ const a = typeof c.a === 'number' ? c.a / 255 : 1;
478
+ return `rgba(${c.r}, ${c.g}, ${c.b}, ${a})`;
479
+ }
480
+ const colorCache = new Map();
481
+ function rgbaOf(css) {
482
+ let cached = colorCache.get(css);
483
+ if (cached !== undefined)
484
+ return cached;
485
+ try {
486
+ cached = Color.isValid(css) ? new Color(css) : null;
487
+ }
488
+ catch {
489
+ cached = null;
490
+ }
491
+ if (colorCache.size > 256)
492
+ colorCache.clear();
493
+ colorCache.set(css, cached);
494
+ return cached;
495
+ }
496
+ // UIViewAutoresizing.FlexibleWidth | FlexibleHeight
497
+ const IOS_FLEXIBLE_SIZE = 2 | 16;
498
+ let SvgHostViewClass;
499
+ /** A plain UIView that reports size changes, so drawing follows Mason's native layout passes too. */
500
+ function svgHostViewClass() {
501
+ SvgHostViewClass ??= UIView.extend({
502
+ layoutSubviews() {
503
+ UIView.prototype.layoutSubviews.call(this);
504
+ this.owner?.deref()?._svgHostLayout();
505
+ },
506
+ });
507
+ return SvgHostViewClass;
508
+ }
509
+ function iosColor(css, opacity) {
510
+ if (css === undefined)
511
+ return null;
512
+ const c = rgbaOf(css);
513
+ if (!c)
514
+ return null;
515
+ return UIColor.colorWithRedGreenBlueAlpha(c.r / 255, c.g / 255, c.b / 255, (c.a / 255) * opacity).CGColor;
516
+ }
517
+ const IOS_CAPS = {
518
+ butt: () => kCALineCapButt,
519
+ round: () => kCALineCapRound,
520
+ square: () => kCALineCapSquare,
521
+ };
522
+ const IOS_JOINS = {
523
+ miter: () => kCALineJoinMiter,
524
+ round: () => kCALineJoinRound,
525
+ bevel: () => kCALineJoinBevel,
526
+ };
527
+ function iosPath(commands) {
528
+ const path = UIBezierPath.bezierPath();
529
+ let open = false;
530
+ for (let i = 0; i < commands.length;) {
531
+ const op = commands[i++];
532
+ if (op === PATH_MOVE) {
533
+ path.moveToPoint(CGPointMake(commands[i++], commands[i++]));
534
+ open = true;
535
+ }
536
+ else if (!open) {
537
+ break;
538
+ }
539
+ else if (op === PATH_LINE) {
540
+ path.addLineToPoint(CGPointMake(commands[i++], commands[i++]));
541
+ }
542
+ else if (op === PATH_CUBIC) {
543
+ const c1 = CGPointMake(commands[i++], commands[i++]);
544
+ const c2 = CGPointMake(commands[i++], commands[i++]);
545
+ path.addCurveToPointControlPoint1ControlPoint2(CGPointMake(commands[i++], commands[i++]), c1, c2);
546
+ }
547
+ else if (op === PATH_QUAD) {
548
+ const c = CGPointMake(commands[i++], commands[i++]);
549
+ path.addQuadCurveToPointControlPoint(CGPointMake(commands[i++], commands[i++]), c);
550
+ }
551
+ else if (op === PATH_CLOSE) {
552
+ path.closePath();
553
+ }
554
+ else {
555
+ break;
556
+ }
557
+ }
558
+ return path;
559
+ }
560
+ function renderIOS(host, scene) {
561
+ const layer = host.layer;
562
+ CATransaction.begin();
563
+ CATransaction.setDisableActions(true);
564
+ try {
565
+ const old = layer.sublayers;
566
+ if (old) {
567
+ for (let i = old.count - 1; i >= 0; i--)
568
+ old.objectAtIndex(i).removeFromSuperlayer();
569
+ }
570
+ const scale = host.traitCollection?.displayScale || 2;
571
+ for (const shape of scene.shapes) {
572
+ const shapeLayer = CAShapeLayer.layer();
573
+ shapeLayer.frame = host.bounds;
574
+ shapeLayer.contentsScale = scale;
575
+ shapeLayer.path = iosPath(shape.commands).CGPath;
576
+ shapeLayer.fillColor = iosColor(shape.fill, shape.fillOpacity);
577
+ shapeLayer.fillRule = shape.fillRule === 'evenodd' ? kCAFillRuleEvenOdd : kCAFillRuleNonZero;
578
+ shapeLayer.strokeColor = iosColor(shape.stroke, shape.strokeOpacity);
579
+ shapeLayer.lineWidth = shape.strokeWidth;
580
+ shapeLayer.lineCap = IOS_CAPS[shape.lineCap]();
581
+ shapeLayer.lineJoin = IOS_JOINS[shape.lineJoin]();
582
+ shapeLayer.miterLimit = shape.miterLimit;
583
+ if (shape.dashArray) {
584
+ shapeLayer.lineDashPattern = shape.dashArray;
585
+ shapeLayer.lineDashPhase = shape.dashOffset;
586
+ }
587
+ shapeLayer.opacity = shape.opacity;
588
+ layer.addSublayer(shapeLayer);
589
+ }
590
+ }
591
+ finally {
592
+ CATransaction.commit();
593
+ }
594
+ }
595
+ function androidPaint(css, opacity, stroke) {
596
+ const c = rgbaOf(css);
597
+ if (!c)
598
+ return undefined;
599
+ const paint = new android.graphics.Paint();
600
+ paint.setAntiAlias(true);
601
+ paint.setColor(android.graphics.Color.argb(Math.round(c.a * opacity), c.r, c.g, c.b));
602
+ if (!stroke) {
603
+ paint.setStyle(android.graphics.Paint.Style.FILL);
604
+ return paint;
605
+ }
606
+ paint.setStyle(android.graphics.Paint.Style.STROKE);
607
+ paint.setStrokeWidth(stroke.strokeWidth);
608
+ paint.setStrokeMiter(stroke.miterLimit);
609
+ paint.setStrokeCap(stroke.lineCap === 'round'
610
+ ? android.graphics.Paint.Cap.ROUND
611
+ : stroke.lineCap === 'square'
612
+ ? android.graphics.Paint.Cap.SQUARE
613
+ : android.graphics.Paint.Cap.BUTT);
614
+ paint.setStrokeJoin(stroke.lineJoin === 'round'
615
+ ? android.graphics.Paint.Join.ROUND
616
+ : stroke.lineJoin === 'bevel'
617
+ ? android.graphics.Paint.Join.BEVEL
618
+ : android.graphics.Paint.Join.MITER);
619
+ if (stroke.dashArray) {
620
+ const intervals = Array.create('float', stroke.dashArray.length);
621
+ stroke.dashArray.forEach((d, i) => (intervals[i] = d));
622
+ paint.setPathEffect(new android.graphics.DashPathEffect(intervals, stroke.dashOffset));
623
+ }
624
+ return paint;
625
+ }
626
+ function androidOps(scene) {
627
+ const ops = [];
628
+ for (const shape of scene.shapes) {
629
+ const path = new android.graphics.Path();
630
+ const c = shape.commands;
631
+ for (let i = 0; i < c.length;) {
632
+ const op = c[i++];
633
+ if (op === PATH_MOVE)
634
+ path.moveTo(c[i++], c[i++]);
635
+ else if (op === PATH_LINE)
636
+ path.lineTo(c[i++], c[i++]);
637
+ else if (op === PATH_CUBIC)
638
+ path.cubicTo(c[i++], c[i++], c[i++], c[i++], c[i++], c[i++]);
639
+ else if (op === PATH_QUAD)
640
+ path.quadTo(c[i++], c[i++], c[i++], c[i++]);
641
+ else if (op === PATH_CLOSE)
642
+ path.close();
643
+ else
644
+ break;
645
+ }
646
+ path.setFillType(shape.fillRule === 'evenodd' ? android.graphics.Path.FillType.EVEN_ODD : android.graphics.Path.FillType.WINDING);
647
+ ops.push({
648
+ path,
649
+ fill: shape.fill === undefined ? undefined : androidPaint(shape.fill, shape.fillOpacity, undefined),
650
+ stroke: shape.stroke === undefined ? undefined : androidPaint(shape.stroke, shape.strokeOpacity, shape),
651
+ alpha: Math.round(shape.opacity * 255),
652
+ });
653
+ }
654
+ return ops;
655
+ }
656
+ let SvgDrawableClass;
657
+ function svgDrawableClass() {
658
+ SvgDrawableClass ??= android.graphics.drawable.Drawable.extend({
659
+ draw(canvas) {
660
+ const bounds = this.getBounds();
661
+ this.owner?.deref()?._svgAndroidDraw(canvas, bounds.width(), bounds.height());
662
+ },
663
+ setAlpha() { },
664
+ setColorFilter() { },
665
+ getOpacity() {
666
+ return android.graphics.PixelFormat.TRANSLUCENT;
667
+ },
668
+ });
669
+ return SvgDrawableClass;
670
+ }
671
+ const PRESENTATIONAL_LENGTH = /^\s*(\d+(?:\.\d+)?|\.\d+)\s*(px|%)?\s*$/i;
672
+ /** `width`/`height` attribute -> a CSS-tier style value (DIP number or percent string). */
673
+ function presentationalLength(value) {
674
+ if (value == null)
675
+ return undefined;
676
+ const m = PRESENTATIONAL_LENGTH.exec(value);
677
+ if (!m)
678
+ return undefined;
679
+ return m[2] === '%' ? `${m[1]}%` : Number(m[1]);
680
+ }
681
+ let SvgElementClass;
682
+ /**
683
+ * `@CSSType('svg')` MasonKit block that draws its {@link SvgNode} tree
684
+ * natively. `width` / `height` attributes size it at stylesheet precedence,
685
+ * so CSS (e.g. `w-5`) still overrides them as in a browser.
686
+ */
687
+ export function createSvgElementClass() {
688
+ if (SvgElementClass)
689
+ return SvgElementClass;
690
+ const Base = Div;
691
+ class MasonSvgElement extends Base {
692
+ _svgTree;
693
+ _svgDirty = true;
694
+ _svgDrawnWidth = -1;
695
+ _svgDrawnHeight = -1;
696
+ _svgHost;
697
+ _svgDrawable;
698
+ _svgLayoutListener;
699
+ _svgOps;
700
+ _svgPresentational = {};
701
+ _svgStyle() {
702
+ return this.style;
703
+ }
704
+ setSvgTree(root) {
705
+ this._svgTree = root;
706
+ this._svgInvalidate();
707
+ }
708
+ _svgCurrentColor() {
709
+ try {
710
+ return cssOfColor(this._svgStyle()['color']);
711
+ }
712
+ catch {
713
+ return undefined;
714
+ }
715
+ }
716
+ _svgInvalidate() {
717
+ this._svgDirty = true;
718
+ if (isApple())
719
+ this._svgHost?.setNeedsLayout();
720
+ else if (isAndroid())
721
+ this._svgDrawable?.invalidateSelf();
722
+ }
723
+ initNativeView() {
724
+ super.initNativeView();
725
+ try {
726
+ if (isApple())
727
+ this._svgAttachIOS();
728
+ else if (isAndroid())
729
+ this._svgAttachAndroid();
730
+ }
731
+ catch {
732
+ // Drawing is best-effort; the element still lays out as a box.
733
+ }
734
+ }
735
+ disposeNativeView() {
736
+ try {
737
+ if (isApple()) {
738
+ this._svgHost?.removeFromSuperview();
739
+ if (this._svgHost)
740
+ this._svgHost.owner = undefined;
741
+ this._svgHost = undefined;
742
+ }
743
+ else if (isAndroid()) {
744
+ const native = this.nativeViewProtected;
745
+ if (native && this._svgDrawable)
746
+ native.getOverlay().remove(this._svgDrawable);
747
+ if (native && this._svgLayoutListener)
748
+ native.removeOnLayoutChangeListener(this._svgLayoutListener);
749
+ if (this._svgDrawable)
750
+ this._svgDrawable.owner = undefined;
751
+ this._svgDrawable = undefined;
752
+ this._svgLayoutListener = undefined;
753
+ this._svgOps = undefined;
754
+ }
755
+ }
756
+ catch {
757
+ // Native view already gone.
758
+ }
759
+ this._svgDrawnWidth = this._svgDrawnHeight = -1;
760
+ this._svgDirty = true;
761
+ super.disposeNativeView();
762
+ }
763
+ _svgAttachIOS() {
764
+ const native = this.nativeViewProtected;
765
+ if (!native || this._svgHost)
766
+ return;
767
+ const host = new (svgHostViewClass())();
768
+ host.frame = native.bounds;
769
+ host.autoresizingMask = IOS_FLEXIBLE_SIZE;
770
+ host.userInteractionEnabled = false;
771
+ host.clipsToBounds = true;
772
+ host.backgroundColor = UIColor.clearColor;
773
+ host.owner = new WeakRef(this);
774
+ // MasonUIView doesn't autoresize subviews; this element has no Mason
775
+ // children, so the host is the only subview it resizes.
776
+ native.autoresizesSubviews = true;
777
+ native.addSubview(host);
778
+ this._svgHost = host;
779
+ host.setNeedsLayout();
780
+ }
781
+ _svgHostLayout() {
782
+ const host = this._svgHost;
783
+ if (!host || !this._svgTree)
784
+ return;
785
+ const bounds = host.superview?.bounds;
786
+ if (bounds && !CGRectEqualToRect(host.frame, bounds))
787
+ host.frame = bounds;
788
+ const { width, height } = host.bounds.size;
789
+ if (!this._svgDirty && width === this._svgDrawnWidth && height === this._svgDrawnHeight)
790
+ return;
791
+ this._svgDirty = false;
792
+ this._svgDrawnWidth = width;
793
+ this._svgDrawnHeight = height;
794
+ try {
795
+ renderIOS(host, buildSvgScene(this._svgTree, { width, height }, this._svgCurrentColor()));
796
+ }
797
+ catch {
798
+ // Leave the previous drawing in place.
799
+ }
800
+ }
801
+ _svgAttachAndroid() {
802
+ const native = this.nativeViewProtected;
803
+ if (!native || this._svgDrawable)
804
+ return;
805
+ const drawable = new (svgDrawableClass())();
806
+ drawable.owner = new WeakRef(this);
807
+ drawable.setBounds(0, 0, native.getWidth(), native.getHeight());
808
+ native.getOverlay().add(drawable);
809
+ const listener = new android.view.View.OnLayoutChangeListener({
810
+ onLayoutChange(_v, left, top, right, bottom) {
811
+ drawable.setBounds(0, 0, right - left, bottom - top);
812
+ drawable.invalidateSelf();
813
+ },
814
+ });
815
+ native.addOnLayoutChangeListener(listener);
816
+ this._svgDrawable = drawable;
817
+ this._svgLayoutListener = listener;
818
+ }
819
+ _svgAndroidDraw(canvas, width, height) {
820
+ if (!this._svgTree || width <= 0 || height <= 0)
821
+ return;
822
+ const density = Utils.layout.getDisplayDensity() || 1;
823
+ const w = width / density;
824
+ const h = height / density;
825
+ if (this._svgDirty || !this._svgOps || w !== this._svgDrawnWidth || h !== this._svgDrawnHeight) {
826
+ this._svgDirty = false;
827
+ this._svgDrawnWidth = w;
828
+ this._svgDrawnHeight = h;
829
+ try {
830
+ this._svgOps = androidOps(buildSvgScene(this._svgTree, { width: w, height: h }, this._svgCurrentColor()));
831
+ }
832
+ catch {
833
+ this._svgOps = [];
834
+ }
835
+ }
836
+ const saved = canvas.save();
837
+ try {
838
+ canvas.clipRect(0, 0, width, height);
839
+ canvas.scale(density, density);
840
+ for (const op of this._svgOps) {
841
+ const layer = op.alpha < 255 ? canvas.saveLayerAlpha(0, 0, w, h, op.alpha) : -1;
842
+ if (op.fill)
843
+ canvas.drawPath(op.path, op.fill);
844
+ if (op.stroke)
845
+ canvas.drawPath(op.path, op.stroke);
846
+ if (layer >= 0)
847
+ canvas.restoreToCount(layer);
848
+ }
849
+ }
850
+ finally {
851
+ canvas.restoreToCount(saved);
852
+ }
853
+ }
854
+ /** Applies a `width`/`height` attribute unless a stylesheet or inline style already sizes that axis. */
855
+ _svgSetPresentationalSize(prop, value) {
856
+ const style = this._svgStyle();
857
+ const mine = this._svgPresentational[prop];
858
+ const current = style[prop];
859
+ const ownedByAttribute = mine !== undefined && current === mine;
860
+ const unsized = current === undefined || current === 'auto';
861
+ if (!ownedByAttribute && !unsized)
862
+ return;
863
+ const next = presentationalLength(value);
864
+ try {
865
+ if (next === undefined) {
866
+ if (ownedByAttribute)
867
+ style[`css:${prop}`] = 'unset';
868
+ delete this._svgPresentational[prop];
869
+ }
870
+ else {
871
+ style[`css:${prop}`] = next;
872
+ this._svgPresentational[prop] = style[prop];
873
+ }
874
+ // An <svg> sized by attributes keeps that size in a flex row, as a
875
+ // replaced element's automatic minimum size does in a browser.
876
+ const fixed = typeof next === 'number';
877
+ if (prop === 'width' && fixed !== (this._svgPresentational.noShrink === true)) {
878
+ style['css:flex-shrink'] = fixed ? 0 : 'unset';
879
+ this._svgPresentational.noShrink = fixed;
880
+ }
881
+ }
882
+ catch {
883
+ // Unsupported value for this platform's style.
884
+ }
885
+ }
886
+ }
887
+ const proto = MasonSvgElement.prototype;
888
+ // `view.width = "24"` from the host's attribute path is a presentational
889
+ // hint; numeric/Length writes from code keep NativeScript's local semantics.
890
+ for (const prop of ['width', 'height']) {
891
+ let owner = Object.getPrototypeOf(proto);
892
+ let descriptor;
893
+ while (owner && !(descriptor = Object.getOwnPropertyDescriptor(owner, prop))) {
894
+ owner = Object.getPrototypeOf(owner);
895
+ }
896
+ const base = descriptor;
897
+ Object.defineProperty(proto, prop, {
898
+ configurable: true,
899
+ enumerable: base?.enumerable ?? true,
900
+ get() {
901
+ return base?.get ? base.get.call(this) : this._svgStyle()[prop];
902
+ },
903
+ set(value) {
904
+ if (value == null || typeof value === 'string') {
905
+ this._svgSetPresentationalSize(prop, value);
906
+ }
907
+ else if (base?.set) {
908
+ base.set.call(this, value);
909
+ }
910
+ else {
911
+ this._svgStyle()[prop] = value;
912
+ }
913
+ },
914
+ });
915
+ }
916
+ const setColorNative = colorProperty?.setNative;
917
+ if (setColorNative) {
918
+ const inherited = Object.getPrototypeOf(proto)[setColorNative];
919
+ Object.defineProperty(proto, setColorNative, {
920
+ configurable: true,
921
+ writable: true,
922
+ value(value) {
923
+ inherited?.call(this, value);
924
+ this._svgInvalidate();
925
+ },
926
+ });
927
+ }
928
+ CSSType('svg')(MasonSvgElement);
929
+ SvgElementClass = MasonSvgElement;
930
+ return SvgElementClass;
931
+ }
932
+ //# sourceMappingURL=svg.js.map