@codehz/draw-call 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.
package/render.cjs ADDED
@@ -0,0 +1,1312 @@
1
+
2
+ //#region src/types/base.ts
3
+ function linearGradient(angle, ...stops) {
4
+ return {
5
+ type: "linear-gradient",
6
+ angle,
7
+ stops: stops.map((stop, index) => {
8
+ if (typeof stop === "string") return {
9
+ offset: stops.length > 1 ? index / (stops.length - 1) : 0,
10
+ color: stop
11
+ };
12
+ return {
13
+ offset: stop[0],
14
+ color: stop[1]
15
+ };
16
+ })
17
+ };
18
+ }
19
+ function radialGradient(options, ...stops) {
20
+ const colorStops = stops.map((stop, index) => {
21
+ if (typeof stop === "string") return {
22
+ offset: stops.length > 1 ? index / (stops.length - 1) : 0,
23
+ color: stop
24
+ };
25
+ return {
26
+ offset: stop[0],
27
+ color: stop[1]
28
+ };
29
+ });
30
+ return {
31
+ type: "radial-gradient",
32
+ ...options,
33
+ stops: colorStops
34
+ };
35
+ }
36
+ function normalizeSpacing(value) {
37
+ if (value === void 0) return {
38
+ top: 0,
39
+ right: 0,
40
+ bottom: 0,
41
+ left: 0
42
+ };
43
+ if (typeof value === "number") return {
44
+ top: value,
45
+ right: value,
46
+ bottom: value,
47
+ left: value
48
+ };
49
+ return {
50
+ top: value.top ?? 0,
51
+ right: value.right ?? 0,
52
+ bottom: value.bottom ?? 0,
53
+ left: value.left ?? 0
54
+ };
55
+ }
56
+ function normalizeBorderRadius(value) {
57
+ if (value === void 0) return [
58
+ 0,
59
+ 0,
60
+ 0,
61
+ 0
62
+ ];
63
+ if (typeof value === "number") return [
64
+ value,
65
+ value,
66
+ value,
67
+ value
68
+ ];
69
+ return value;
70
+ }
71
+
72
+ //#endregion
73
+ //#region src/layout/components/box.ts
74
+ function calcEffectiveSize(element, padding, availableWidth) {
75
+ return {
76
+ width: typeof element.width === "number" ? element.width - padding.left - padding.right : availableWidth > 0 ? availableWidth : 0,
77
+ height: typeof element.height === "number" ? element.height - padding.top - padding.bottom : 0
78
+ };
79
+ }
80
+ function collectChildSizes(children, ctx, availableWidth, padding, measureChild) {
81
+ const childSizes = [];
82
+ for (const child of children) {
83
+ const childMargin = normalizeSpacing(child.margin);
84
+ const childSize = measureChild(child, ctx, availableWidth - padding.left - padding.right - childMargin.left - childMargin.right);
85
+ childSizes.push({
86
+ width: childSize.width,
87
+ height: childSize.height,
88
+ margin: childMargin
89
+ });
90
+ }
91
+ return childSizes;
92
+ }
93
+ function measureWrappedContent(childSizes, gap, availableMain, isRow) {
94
+ let currentMain = 0;
95
+ let currentCross = 0;
96
+ let totalCross = 0;
97
+ let maxMain = 0;
98
+ let lineCount = 0;
99
+ for (let i = 0; i < childSizes.length; i++) {
100
+ const { width, height, margin } = childSizes[i];
101
+ const itemMain = isRow ? width + margin.left + margin.right : height + margin.top + margin.bottom;
102
+ const itemCross = isRow ? height + margin.top + margin.bottom : width + margin.left + margin.right;
103
+ if (lineCount > 0 && currentMain + gap + itemMain > availableMain) {
104
+ totalCross += currentCross;
105
+ maxMain = Math.max(maxMain, currentMain);
106
+ lineCount++;
107
+ currentMain = itemMain;
108
+ currentCross = itemCross;
109
+ } else {
110
+ if (lineCount > 0 || i > 0) currentMain += gap;
111
+ currentMain += itemMain;
112
+ currentCross = Math.max(currentCross, itemCross);
113
+ if (i === 0) lineCount = 1;
114
+ }
115
+ }
116
+ if (childSizes.length > 0) {
117
+ totalCross += currentCross;
118
+ maxMain = Math.max(maxMain, currentMain);
119
+ }
120
+ if (lineCount > 1) totalCross += gap * (lineCount - 1);
121
+ return isRow ? {
122
+ width: maxMain,
123
+ height: totalCross
124
+ } : {
125
+ width: totalCross,
126
+ height: maxMain
127
+ };
128
+ }
129
+ /**
130
+ * 测量 Box 元素的固有尺寸
131
+ */
132
+ function measureBoxSize(element, ctx, availableWidth, measureChild) {
133
+ const padding = normalizeSpacing(element.padding);
134
+ const gap = element.gap ?? 0;
135
+ const direction = element.direction ?? "row";
136
+ const wrap = element.wrap ?? false;
137
+ const isRow = direction === "row" || direction === "row-reverse";
138
+ let contentWidth = 0;
139
+ let contentHeight = 0;
140
+ const children = element.children ?? [];
141
+ const { width: effectiveWidth, height: effectiveHeight } = calcEffectiveSize(element, padding, availableWidth);
142
+ if (wrap && isRow && effectiveWidth > 0) {
143
+ const wrapped = measureWrappedContent(collectChildSizes(children, ctx, availableWidth, padding, measureChild), gap, effectiveWidth, true);
144
+ contentWidth = wrapped.width;
145
+ contentHeight = wrapped.height;
146
+ } else if (wrap && !isRow && effectiveHeight > 0) {
147
+ const wrapped = measureWrappedContent(collectChildSizes(children, ctx, availableWidth, padding, measureChild), gap, effectiveHeight, false);
148
+ contentWidth = wrapped.width;
149
+ contentHeight = wrapped.height;
150
+ } else for (let i = 0; i < children.length; i++) {
151
+ const child = children[i];
152
+ const childMargin = normalizeSpacing(child.margin);
153
+ const childSize = measureChild(child, ctx, availableWidth - padding.left - padding.right - childMargin.left - childMargin.right);
154
+ if (isRow) {
155
+ contentWidth += childSize.width + childMargin.left + childMargin.right;
156
+ contentHeight = Math.max(contentHeight, childSize.height + childMargin.top + childMargin.bottom);
157
+ if (i > 0) contentWidth += gap;
158
+ } else {
159
+ contentHeight += childSize.height + childMargin.top + childMargin.bottom;
160
+ contentWidth = Math.max(contentWidth, childSize.width + childMargin.left + childMargin.right);
161
+ if (i > 0) contentHeight += gap;
162
+ }
163
+ }
164
+ const intrinsicWidth = contentWidth + padding.left + padding.right;
165
+ const intrinsicHeight = contentHeight + padding.top + padding.bottom;
166
+ return {
167
+ width: typeof element.width === "number" ? element.width : intrinsicWidth,
168
+ height: typeof element.height === "number" ? element.height : intrinsicHeight
169
+ };
170
+ }
171
+
172
+ //#endregion
173
+ //#region src/layout/components/image.ts
174
+ /**
175
+ * 测量 Image 元素的固有尺寸
176
+ */
177
+ function measureImageSize(element, _ctx, _availableWidth) {
178
+ const imageElement = element;
179
+ if (imageElement.width !== void 0 && imageElement.height !== void 0) return {
180
+ width: typeof imageElement.width === "number" ? imageElement.width : 0,
181
+ height: typeof imageElement.height === "number" ? imageElement.height : 0
182
+ };
183
+ const src = imageElement.src;
184
+ if (src) {
185
+ const imgWidth = "naturalWidth" in src ? src.naturalWidth : "width" in src ? +src.width : 0;
186
+ const imgHeight = "naturalHeight" in src ? src.naturalHeight : "height" in src ? +src.height : 0;
187
+ if (imgWidth > 0 && imgHeight > 0) return {
188
+ width: imgWidth,
189
+ height: imgHeight
190
+ };
191
+ }
192
+ return {
193
+ width: 0,
194
+ height: 0
195
+ };
196
+ }
197
+
198
+ //#endregion
199
+ //#region src/layout/components/stack.ts
200
+ /**
201
+ * 测量 Stack 元素的固有尺寸
202
+ */
203
+ function measureStackSize(element, ctx, availableWidth, measureChild) {
204
+ const padding = normalizeSpacing(element.padding);
205
+ let contentWidth = 0;
206
+ let contentHeight = 0;
207
+ const children = element.children ?? [];
208
+ for (const child of children) {
209
+ const childMargin = normalizeSpacing(child.margin);
210
+ const childSize = measureChild(child, ctx, availableWidth - padding.left - padding.right - childMargin.left - childMargin.right);
211
+ contentWidth = Math.max(contentWidth, childSize.width + childMargin.left + childMargin.right);
212
+ contentHeight = Math.max(contentHeight, childSize.height + childMargin.top + childMargin.bottom);
213
+ }
214
+ const intrinsicWidth = contentWidth + padding.left + padding.right;
215
+ const intrinsicHeight = contentHeight + padding.top + padding.bottom;
216
+ return {
217
+ width: typeof element.width === "number" ? element.width : intrinsicWidth,
218
+ height: typeof element.height === "number" ? element.height : intrinsicHeight
219
+ };
220
+ }
221
+
222
+ //#endregion
223
+ //#region src/layout/components/svg.ts
224
+ /**
225
+ * 测量 SVG 元素的固有尺寸
226
+ */
227
+ function measureSvgSize(element, _ctx, _availableWidth) {
228
+ const svgElement = element;
229
+ if (svgElement.width !== void 0 && svgElement.height !== void 0) return {
230
+ width: typeof svgElement.width === "number" ? svgElement.width : 0,
231
+ height: typeof svgElement.height === "number" ? svgElement.height : 0
232
+ };
233
+ if (svgElement.viewBox) {
234
+ const viewBoxWidth = svgElement.viewBox.width;
235
+ const viewBoxHeight = svgElement.viewBox.height;
236
+ const aspectRatio = viewBoxWidth / viewBoxHeight;
237
+ if (svgElement.width !== void 0 && typeof svgElement.width === "number") return {
238
+ width: svgElement.width,
239
+ height: svgElement.width / aspectRatio
240
+ };
241
+ if (svgElement.height !== void 0 && typeof svgElement.height === "number") return {
242
+ width: svgElement.height * aspectRatio,
243
+ height: svgElement.height
244
+ };
245
+ return {
246
+ width: viewBoxWidth,
247
+ height: viewBoxHeight
248
+ };
249
+ }
250
+ return {
251
+ width: 0,
252
+ height: 0
253
+ };
254
+ }
255
+
256
+ //#endregion
257
+ //#region src/render/utils/font.ts
258
+ /**
259
+ * 构建 Canvas 字体字符串
260
+ * @param font 字体属性
261
+ * @returns CSS 字体字符串
262
+ */
263
+ function buildFontString(font) {
264
+ return `${font.style ?? "normal"} ${font.weight ?? "normal"} ${font.size ?? 16}px ${font.family ?? "sans-serif"}`;
265
+ }
266
+
267
+ //#endregion
268
+ //#region src/layout/utils/measure.ts
269
+ function createCanvasMeasureContext(ctx) {
270
+ return { measureText(text, font) {
271
+ ctx.font = buildFontString(font);
272
+ ctx.textBaseline = "middle";
273
+ const metrics = ctx.measureText(text);
274
+ const height = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
275
+ return {
276
+ width: metrics.width,
277
+ height: height || font.size || 16,
278
+ offset: (metrics.actualBoundingBoxAscent - metrics.actualBoundingBoxDescent) / 2
279
+ };
280
+ } };
281
+ }
282
+ function wrapText(ctx, text, maxWidth, font) {
283
+ if (maxWidth <= 0) {
284
+ const { offset } = ctx.measureText(text, font);
285
+ return {
286
+ lines: [text],
287
+ offsets: [offset]
288
+ };
289
+ }
290
+ const lines = [];
291
+ const offsets = [];
292
+ const paragraphs = text.split("\n");
293
+ for (const paragraph of paragraphs) {
294
+ if (paragraph === "") {
295
+ lines.push("");
296
+ offsets.push(0);
297
+ continue;
298
+ }
299
+ const words = paragraph.split(/(\s+)/);
300
+ let currentLine = "";
301
+ for (const word of words) {
302
+ const testLine = currentLine + word;
303
+ const { width } = ctx.measureText(testLine, font);
304
+ if (width > maxWidth && currentLine !== "") {
305
+ const trimmed = currentLine.trim();
306
+ lines.push(trimmed);
307
+ offsets.push(ctx.measureText(trimmed, font).offset);
308
+ currentLine = word.trimStart();
309
+ } else currentLine = testLine;
310
+ }
311
+ if (currentLine) {
312
+ const trimmed = currentLine.trim();
313
+ lines.push(trimmed);
314
+ offsets.push(ctx.measureText(trimmed, font).offset);
315
+ }
316
+ }
317
+ return lines.length > 0 ? {
318
+ lines,
319
+ offsets
320
+ } : {
321
+ lines: [""],
322
+ offsets: [0]
323
+ };
324
+ }
325
+ function truncateText(ctx, text, maxWidth, font, ellipsis = "...") {
326
+ const measured = ctx.measureText(text, font);
327
+ if (measured.width <= maxWidth) return {
328
+ text,
329
+ offset: measured.offset
330
+ };
331
+ const availableWidth = maxWidth - ctx.measureText(ellipsis, font).width;
332
+ if (availableWidth <= 0) {
333
+ const { offset } = ctx.measureText(ellipsis, font);
334
+ return {
335
+ text: ellipsis,
336
+ offset
337
+ };
338
+ }
339
+ let left = 0;
340
+ let right = text.length;
341
+ while (left < right) {
342
+ const mid = Math.floor((left + right + 1) / 2);
343
+ const truncated = text.slice(0, mid);
344
+ const { width: truncatedWidth } = ctx.measureText(truncated, font);
345
+ if (truncatedWidth <= availableWidth) left = mid;
346
+ else right = mid - 1;
347
+ }
348
+ const result = text.slice(0, left) + ellipsis;
349
+ const { offset } = ctx.measureText(result, font);
350
+ return {
351
+ text: result,
352
+ offset
353
+ };
354
+ }
355
+
356
+ //#endregion
357
+ //#region src/layout/components/text.ts
358
+ /**
359
+ * 测量文本元素的固有尺寸
360
+ */
361
+ function measureTextSize(element, ctx, availableWidth) {
362
+ const font = element.font ?? {};
363
+ const lineHeightPx = (font.size ?? 16) * (element.lineHeight ?? 1.2);
364
+ if (element.wrap && availableWidth > 0 && availableWidth < Infinity) {
365
+ const { lines } = wrapText(ctx, element.content, availableWidth, font);
366
+ const { width: maxLineWidth } = lines.reduce((max, line) => {
367
+ const { width } = ctx.measureText(line, font);
368
+ return width > max.width ? { width } : max;
369
+ }, { width: 0 });
370
+ return {
371
+ width: maxLineWidth,
372
+ height: lines.length * lineHeightPx
373
+ };
374
+ }
375
+ const { width, height } = ctx.measureText(element.content, font);
376
+ return {
377
+ width,
378
+ height: Math.max(height, lineHeightPx)
379
+ };
380
+ }
381
+
382
+ //#endregion
383
+ //#region src/layout/components/index.ts
384
+ /**
385
+ * 计算元素的固有尺寸(不依赖父容器的尺寸)
386
+ */
387
+ function measureIntrinsicSize(element, ctx, availableWidth) {
388
+ switch (element.type) {
389
+ case "text": return measureTextSize(element, ctx, availableWidth);
390
+ case "box": return measureBoxSize(element, ctx, availableWidth, measureIntrinsicSize);
391
+ case "stack": return measureStackSize(element, ctx, availableWidth, measureIntrinsicSize);
392
+ case "image": return measureImageSize(element, ctx, availableWidth);
393
+ case "svg": return measureSvgSize(element, ctx, availableWidth);
394
+ default: return {
395
+ width: 0,
396
+ height: 0
397
+ };
398
+ }
399
+ }
400
+
401
+ //#endregion
402
+ //#region src/layout/utils/offset.ts
403
+ /**
404
+ * 递归地为布局节点及其子节点应用位置偏移
405
+ */
406
+ function applyOffset(node, dx, dy) {
407
+ node.layout.x += dx;
408
+ node.layout.y += dy;
409
+ node.layout.contentX += dx;
410
+ node.layout.contentY += dy;
411
+ for (const child of node.children) applyOffset(child, dx, dy);
412
+ }
413
+
414
+ //#endregion
415
+ //#region src/types/layout.ts
416
+ function resolveSize(size, available, auto) {
417
+ if (size === void 0 || size === "auto") return auto;
418
+ if (size === "fill") return available;
419
+ if (typeof size === "number") return size;
420
+ return available * parseFloat(size) / 100;
421
+ }
422
+ function sizeNeedsParent(size) {
423
+ if (size === void 0 || size === "auto") return false;
424
+ if (size === "fill") return true;
425
+ if (typeof size === "string" && size.endsWith("%")) return true;
426
+ return false;
427
+ }
428
+
429
+ //#endregion
430
+ //#region src/layout/engine.ts
431
+ function computeLayout(element, ctx, constraints, x = 0, y = 0) {
432
+ const margin = normalizeSpacing(element.margin);
433
+ const padding = normalizeSpacing("padding" in element ? element.padding : void 0);
434
+ const availableWidth = constraints.maxWidth - margin.left - margin.right;
435
+ const availableHeight = constraints.maxHeight - margin.top - margin.bottom;
436
+ const intrinsic = measureIntrinsicSize(element, ctx, availableWidth);
437
+ let width = constraints.minWidth === constraints.maxWidth && constraints.minWidth > 0 ? constraints.maxWidth - margin.left - margin.right : resolveSize(element.width, availableWidth, intrinsic.width);
438
+ let height = constraints.minHeight === constraints.maxHeight && constraints.minHeight > 0 ? constraints.maxHeight - margin.top - margin.bottom : resolveSize(element.height, availableHeight, intrinsic.height);
439
+ if (element.minWidth !== void 0) width = Math.max(width, element.minWidth);
440
+ if (element.maxWidth !== void 0) width = Math.min(width, element.maxWidth);
441
+ if (element.minHeight !== void 0) height = Math.max(height, element.minHeight);
442
+ if (element.maxHeight !== void 0) height = Math.min(height, element.maxHeight);
443
+ const actualX = x + margin.left;
444
+ const actualY = y + margin.top;
445
+ const contentX = actualX + padding.left;
446
+ const contentY = actualY + padding.top;
447
+ const contentWidth = width - padding.left - padding.right;
448
+ const contentHeight = height - padding.top - padding.bottom;
449
+ const node = {
450
+ element,
451
+ layout: {
452
+ x: actualX,
453
+ y: actualY,
454
+ width,
455
+ height,
456
+ contentX,
457
+ contentY,
458
+ contentWidth,
459
+ contentHeight
460
+ },
461
+ children: []
462
+ };
463
+ if (element.type === "text") {
464
+ const font = element.font ?? {};
465
+ if (element.wrap && contentWidth > 0) {
466
+ let { lines, offsets } = wrapText(ctx, element.content, contentWidth, font);
467
+ if (element.maxLines && lines.length > element.maxLines) {
468
+ lines = lines.slice(0, element.maxLines);
469
+ offsets = offsets.slice(0, element.maxLines);
470
+ if (element.ellipsis && lines.length > 0) {
471
+ const lastIdx = lines.length - 1;
472
+ const truncated = truncateText(ctx, lines[lastIdx], contentWidth, font);
473
+ lines[lastIdx] = truncated.text;
474
+ offsets[lastIdx] = truncated.offset;
475
+ }
476
+ }
477
+ node.lines = lines;
478
+ node.lineOffsets = offsets;
479
+ } else {
480
+ const { text, offset } = truncateText(ctx, element.content, contentWidth > 0 && element.ellipsis ? contentWidth : Infinity, font);
481
+ node.lines = [text];
482
+ node.lineOffsets = [offset];
483
+ }
484
+ }
485
+ if (element.type === "box" || element.type === "stack") {
486
+ const children = element.children ?? [];
487
+ if (element.type === "stack") {
488
+ const stackAlign = element.align ?? "start";
489
+ const stackJustify = element.justify ?? "start";
490
+ for (const child of children) {
491
+ const childNode = computeLayout(child, ctx, {
492
+ minWidth: 0,
493
+ maxWidth: contentWidth,
494
+ minHeight: 0,
495
+ maxHeight: contentHeight
496
+ }, contentX, contentY);
497
+ const childMargin = normalizeSpacing(child.margin);
498
+ const childOuterWidth = childNode.layout.width + childMargin.left + childMargin.right;
499
+ const childOuterHeight = childNode.layout.height + childMargin.top + childMargin.bottom;
500
+ let offsetX = 0;
501
+ if (stackAlign === "center") offsetX = (contentWidth - childOuterWidth) / 2;
502
+ else if (stackAlign === "end") offsetX = contentWidth - childOuterWidth;
503
+ let offsetY = 0;
504
+ if (stackJustify === "center") offsetY = (contentHeight - childOuterHeight) / 2;
505
+ else if (stackJustify === "end") offsetY = contentHeight - childOuterHeight;
506
+ if (offsetX !== 0 || offsetY !== 0) applyOffset(childNode, offsetX, offsetY);
507
+ node.children.push(childNode);
508
+ }
509
+ } else {
510
+ const direction = element.direction ?? "row";
511
+ const justify = element.justify ?? "start";
512
+ const align = element.align ?? "stretch";
513
+ const gap = element.gap ?? 0;
514
+ const wrap = element.wrap ?? false;
515
+ const isRow = direction === "row" || direction === "row-reverse";
516
+ const isReverse = direction === "row-reverse" || direction === "column-reverse";
517
+ const getContentMainSize = () => isRow ? contentWidth : contentHeight;
518
+ const getContentCrossSize = () => isRow ? contentHeight : contentWidth;
519
+ const childInfos = [];
520
+ for (const child of children) {
521
+ const childMargin = normalizeSpacing(child.margin);
522
+ const childFlex = child.flex ?? 0;
523
+ if (childFlex > 0) childInfos.push({
524
+ element: child,
525
+ width: 0,
526
+ height: 0,
527
+ flex: childFlex,
528
+ margin: childMargin
529
+ });
530
+ else {
531
+ const size = measureIntrinsicSize(child, ctx, contentWidth - childMargin.left - childMargin.right);
532
+ const shouldStretchWidth = !isRow && child.width === void 0 && align === "stretch";
533
+ const shouldStretchHeight = isRow && child.height === void 0 && align === "stretch";
534
+ let w = sizeNeedsParent(child.width) ? resolveSize(child.width, contentWidth - childMargin.left - childMargin.right, size.width) : resolveSize(child.width, 0, size.width);
535
+ let h = sizeNeedsParent(child.height) ? resolveSize(child.height, contentHeight - childMargin.top - childMargin.bottom, size.height) : resolveSize(child.height, 0, size.height);
536
+ if (shouldStretchWidth && !wrap) w = contentWidth - childMargin.left - childMargin.right;
537
+ if (shouldStretchHeight && !wrap) h = contentHeight - childMargin.top - childMargin.bottom;
538
+ childInfos.push({
539
+ element: child,
540
+ width: w,
541
+ height: h,
542
+ flex: 0,
543
+ margin: childMargin
544
+ });
545
+ }
546
+ }
547
+ const lines = [];
548
+ if (wrap) {
549
+ let currentLine = [];
550
+ let currentLineSize = 0;
551
+ const mainAxisSize = getContentMainSize();
552
+ for (const info of childInfos) {
553
+ const itemSize = isRow ? info.width + info.margin.left + info.margin.right : info.height + info.margin.top + info.margin.bottom;
554
+ if (currentLine.length > 0 && currentLineSize + gap + itemSize > mainAxisSize) {
555
+ lines.push(currentLine);
556
+ currentLine = [info];
557
+ currentLineSize = itemSize;
558
+ } else {
559
+ currentLine.push(info);
560
+ currentLineSize += (currentLine.length > 1 ? gap : 0) + itemSize;
561
+ }
562
+ }
563
+ if (currentLine.length > 0) lines.push(currentLine);
564
+ } else lines.push(childInfos);
565
+ for (const lineInfos of lines) {
566
+ let totalFixed = 0;
567
+ let totalFlex = 0;
568
+ const totalGap = lineInfos.length > 1 ? gap * (lineInfos.length - 1) : 0;
569
+ for (const info of lineInfos) if (info.flex > 0) totalFlex += info.flex;
570
+ else if (isRow) totalFixed += info.width + info.margin.left + info.margin.right;
571
+ else totalFixed += info.height + info.margin.top + info.margin.bottom;
572
+ const mainAxisSize = getContentMainSize();
573
+ const availableForFlex = Math.max(0, mainAxisSize - totalFixed - totalGap);
574
+ for (const info of lineInfos) if (info.flex > 0) {
575
+ const flexSize = totalFlex > 0 ? availableForFlex * info.flex / totalFlex : 0;
576
+ if (isRow) {
577
+ info.width = flexSize;
578
+ const size = measureIntrinsicSize(info.element, ctx, flexSize);
579
+ info.height = sizeNeedsParent(info.element.height) ? resolveSize(info.element.height, contentHeight - info.margin.top - info.margin.bottom, size.height) : resolveSize(info.element.height, 0, size.height);
580
+ } else {
581
+ info.height = flexSize;
582
+ const size = measureIntrinsicSize(info.element, ctx, contentWidth - info.margin.left - info.margin.right);
583
+ info.width = sizeNeedsParent(info.element.width) ? resolveSize(info.element.width, contentWidth - info.margin.left - info.margin.right, size.width) : resolveSize(info.element.width, 0, size.width);
584
+ }
585
+ }
586
+ }
587
+ let crossOffset = 0;
588
+ for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
589
+ const lineInfos = lines[lineIndex];
590
+ const totalGap = lineInfos.length > 1 ? gap * (lineInfos.length - 1) : 0;
591
+ const totalSize = lineInfos.reduce((sum, info) => {
592
+ return sum + (isRow ? info.width + info.margin.left + info.margin.right : info.height + info.margin.top + info.margin.bottom);
593
+ }, 0) + totalGap;
594
+ const freeSpace = getContentMainSize() - totalSize;
595
+ let mainStart = 0;
596
+ let mainGap = gap;
597
+ switch (justify) {
598
+ case "start":
599
+ mainStart = 0;
600
+ break;
601
+ case "end":
602
+ mainStart = freeSpace;
603
+ break;
604
+ case "center":
605
+ mainStart = freeSpace / 2;
606
+ break;
607
+ case "space-between":
608
+ mainStart = 0;
609
+ if (lineInfos.length > 1) mainGap = gap + freeSpace / (lineInfos.length - 1);
610
+ break;
611
+ case "space-around":
612
+ if (lineInfos.length > 0) {
613
+ const spacing = freeSpace / lineInfos.length;
614
+ mainStart = spacing / 2;
615
+ mainGap = gap + spacing;
616
+ }
617
+ break;
618
+ case "space-evenly":
619
+ if (lineInfos.length > 0) {
620
+ const spacing = freeSpace / (lineInfos.length + 1);
621
+ mainStart = spacing;
622
+ mainGap = gap + spacing;
623
+ }
624
+ break;
625
+ }
626
+ const lineCrossSize = lineInfos.reduce((max, info) => {
627
+ const itemCrossSize = isRow ? info.height + info.margin.top + info.margin.bottom : info.width + info.margin.left + info.margin.right;
628
+ return Math.max(max, itemCrossSize);
629
+ }, 0);
630
+ let mainOffset = mainStart;
631
+ const orderedInfos = isReverse ? [...lineInfos].reverse() : lineInfos;
632
+ for (let i = 0; i < orderedInfos.length; i++) {
633
+ const info = orderedInfos[i];
634
+ const crossAxisSize = wrap ? lineCrossSize : getContentCrossSize();
635
+ const childCrossSize = isRow ? info.height + info.margin.top + info.margin.bottom : info.width + info.margin.left + info.margin.right;
636
+ let itemCrossOffset = 0;
637
+ const effectiveAlign = info.element.alignSelf ?? align;
638
+ if (effectiveAlign === "start") itemCrossOffset = 0;
639
+ else if (effectiveAlign === "end") itemCrossOffset = crossAxisSize - childCrossSize;
640
+ else if (effectiveAlign === "center") itemCrossOffset = (crossAxisSize - childCrossSize) / 2;
641
+ else if (effectiveAlign === "stretch") {
642
+ itemCrossOffset = 0;
643
+ if (isRow && info.element.height === void 0) info.height = crossAxisSize - info.margin.top - info.margin.bottom;
644
+ else if (!isRow && info.element.width === void 0) info.width = crossAxisSize - info.margin.left - info.margin.right;
645
+ }
646
+ const childX = isRow ? contentX + mainOffset + info.margin.left : contentX + crossOffset + itemCrossOffset + info.margin.left;
647
+ const childY = isRow ? contentY + crossOffset + itemCrossOffset + info.margin.top : contentY + mainOffset + info.margin.top;
648
+ let minWidth = 0;
649
+ let maxWidth = info.width;
650
+ let minHeight = 0;
651
+ let maxHeight = info.height;
652
+ let shouldStretchCross = false;
653
+ if (info.flex > 0) if (isRow) {
654
+ minWidth = maxWidth = info.width;
655
+ if (info.element.height === void 0 && align === "stretch") {
656
+ minHeight = info.height;
657
+ maxHeight = element.height !== void 0 ? info.height : Infinity;
658
+ shouldStretchCross = true;
659
+ }
660
+ } else {
661
+ minHeight = maxHeight = info.height;
662
+ if (info.element.width === void 0 && align === "stretch") {
663
+ minWidth = info.width;
664
+ maxWidth = element.width !== void 0 ? info.width : Infinity;
665
+ shouldStretchCross = true;
666
+ }
667
+ }
668
+ else {
669
+ if (!isRow && info.element.width === void 0 && align === "stretch") minWidth = maxWidth = crossAxisSize - info.margin.left - info.margin.right;
670
+ if (isRow && info.element.height === void 0 && align === "stretch") minHeight = maxHeight = crossAxisSize - info.margin.top - info.margin.bottom;
671
+ }
672
+ const childNode = computeLayout(info.element, ctx, {
673
+ minWidth,
674
+ maxWidth,
675
+ minHeight,
676
+ maxHeight
677
+ }, childX - info.margin.left, childY - info.margin.top);
678
+ if (shouldStretchCross && info.flex > 0) {
679
+ const childPadding = normalizeSpacing("padding" in info.element ? info.element.padding : void 0);
680
+ if (isRow && childNode.layout.height < info.height) {
681
+ childNode.layout.height = info.height;
682
+ childNode.layout.contentHeight = info.height - childPadding.top - childPadding.bottom;
683
+ } else if (!isRow && childNode.layout.width < info.width) {
684
+ childNode.layout.width = info.width;
685
+ childNode.layout.contentWidth = info.width - childPadding.left - childPadding.right;
686
+ }
687
+ }
688
+ node.children.push(childNode);
689
+ mainOffset += isRow ? info.width + info.margin.left + info.margin.right : info.height + info.margin.top + info.margin.bottom;
690
+ if (i < orderedInfos.length - 1) mainOffset += mainGap;
691
+ }
692
+ crossOffset += lineCrossSize;
693
+ if (lineIndex < lines.length - 1) crossOffset += gap;
694
+ }
695
+ if (wrap && element.height === void 0 && isRow) {
696
+ const actualContentHeight = crossOffset;
697
+ const actualHeight = actualContentHeight + padding.top + padding.bottom;
698
+ node.layout.height = actualHeight;
699
+ node.layout.contentHeight = actualContentHeight;
700
+ } else if (wrap && element.width === void 0 && !isRow) {
701
+ const actualContentWidth = crossOffset;
702
+ const actualWidth = actualContentWidth + padding.left + padding.right;
703
+ node.layout.width = actualWidth;
704
+ node.layout.contentWidth = actualContentWidth;
705
+ }
706
+ if (!wrap) {
707
+ let maxChildCrossSize = 0;
708
+ for (const childNode of node.children) {
709
+ const childMargin = normalizeSpacing(childNode.element.margin);
710
+ if (isRow) {
711
+ const childOuterHeight = childNode.layout.height + childMargin.top + childMargin.bottom;
712
+ maxChildCrossSize = Math.max(maxChildCrossSize, childOuterHeight);
713
+ } else {
714
+ const childOuterWidth = childNode.layout.width + childMargin.left + childMargin.right;
715
+ maxChildCrossSize = Math.max(maxChildCrossSize, childOuterWidth);
716
+ }
717
+ }
718
+ if (isRow && element.height === void 0) {
719
+ const actualHeight = maxChildCrossSize + padding.top + padding.bottom;
720
+ if (actualHeight > node.layout.height) {
721
+ node.layout.height = actualHeight;
722
+ node.layout.contentHeight = maxChildCrossSize;
723
+ }
724
+ } else if (!isRow && element.width === void 0) {
725
+ const actualWidth = maxChildCrossSize + padding.left + padding.right;
726
+ if (actualWidth > node.layout.width) {
727
+ node.layout.width = actualWidth;
728
+ node.layout.contentWidth = maxChildCrossSize;
729
+ }
730
+ }
731
+ }
732
+ if (isReverse) node.children.reverse();
733
+ }
734
+ }
735
+ return node;
736
+ }
737
+
738
+ //#endregion
739
+ //#region src/render/utils/colors.ts
740
+ function isGradientDescriptor$1(color) {
741
+ return typeof color === "object" && color !== null && "type" in color && (color.type === "linear-gradient" || color.type === "radial-gradient");
742
+ }
743
+ function resolveGradient$1(ctx, descriptor, x, y, width, height) {
744
+ if (descriptor.type === "linear-gradient") {
745
+ const angleRad = (descriptor.angle - 90) * Math.PI / 180;
746
+ const centerX = x + width / 2;
747
+ const centerY = y + height / 2;
748
+ const diagLength = Math.sqrt(width * width + height * height) / 2;
749
+ const x0 = centerX - Math.cos(angleRad) * diagLength;
750
+ const y0 = centerY - Math.sin(angleRad) * diagLength;
751
+ const x1 = centerX + Math.cos(angleRad) * diagLength;
752
+ const y1 = centerY + Math.sin(angleRad) * diagLength;
753
+ const gradient = ctx.createLinearGradient(x0, y0, x1, y1);
754
+ for (const stop of descriptor.stops) gradient.addColorStop(stop.offset, stop.color);
755
+ return gradient;
756
+ } else {
757
+ const diagLength = Math.sqrt(width * width + height * height);
758
+ const startX = x + (descriptor.startX ?? .5) * width;
759
+ const startY = y + (descriptor.startY ?? .5) * height;
760
+ const startRadius = (descriptor.startRadius ?? 0) * diagLength;
761
+ const endX = x + (descriptor.endX ?? .5) * width;
762
+ const endY = y + (descriptor.endY ?? .5) * height;
763
+ const endRadius = (descriptor.endRadius ?? .5) * diagLength;
764
+ const gradient = ctx.createRadialGradient(startX, startY, startRadius, endX, endY, endRadius);
765
+ for (const stop of descriptor.stops) gradient.addColorStop(stop.offset, stop.color);
766
+ return gradient;
767
+ }
768
+ }
769
+ function resolveColor$1(ctx, color, x, y, width, height) {
770
+ if (isGradientDescriptor$1(color)) return resolveGradient$1(ctx, color, x, y, width, height);
771
+ return color;
772
+ }
773
+
774
+ //#endregion
775
+ //#region src/render/utils/shadows.ts
776
+ function applyShadow$1(ctx, shadow) {
777
+ if (shadow) {
778
+ ctx.shadowOffsetX = shadow.offsetX ?? 0;
779
+ ctx.shadowOffsetY = shadow.offsetY ?? 0;
780
+ ctx.shadowBlur = shadow.blur ?? 0;
781
+ ctx.shadowColor = shadow.color ?? "rgba(0,0,0,0.5)";
782
+ } else {
783
+ ctx.shadowOffsetX = 0;
784
+ ctx.shadowOffsetY = 0;
785
+ ctx.shadowBlur = 0;
786
+ ctx.shadowColor = "transparent";
787
+ }
788
+ }
789
+ function clearShadow$1(ctx) {
790
+ ctx.shadowOffsetX = 0;
791
+ ctx.shadowOffsetY = 0;
792
+ ctx.shadowBlur = 0;
793
+ ctx.shadowColor = "transparent";
794
+ }
795
+
796
+ //#endregion
797
+ //#region src/render/utils/shapes.ts
798
+ function roundRectPath(ctx, x, y, width, height, radius) {
799
+ const [tl, tr, br, bl] = radius;
800
+ ctx.beginPath();
801
+ ctx.moveTo(x + tl, y);
802
+ ctx.lineTo(x + width - tr, y);
803
+ ctx.quadraticCurveTo(x + width, y, x + width, y + tr);
804
+ ctx.lineTo(x + width, y + height - br);
805
+ ctx.quadraticCurveTo(x + width, y + height, x + width - br, y + height);
806
+ ctx.lineTo(x + bl, y + height);
807
+ ctx.quadraticCurveTo(x, y + height, x, y + height - bl);
808
+ ctx.lineTo(x, y + tl);
809
+ ctx.quadraticCurveTo(x, y, x + tl, y);
810
+ ctx.closePath();
811
+ }
812
+
813
+ //#endregion
814
+ //#region src/render/components/box.ts
815
+ function renderBox(ctx, node) {
816
+ const element = node.element;
817
+ const { x, y, width, height } = node.layout;
818
+ const border = element.border;
819
+ const radius = normalizeBorderRadius(border?.radius);
820
+ const hasRadius = radius.some((r) => r > 0);
821
+ if (element.opacity !== void 0 && element.opacity < 1) ctx.globalAlpha = element.opacity;
822
+ if (element.shadow && element.background) applyShadow$1(ctx, element.shadow);
823
+ if (element.background) {
824
+ ctx.fillStyle = resolveColor$1(ctx, element.background, x, y, width, height);
825
+ if (hasRadius) {
826
+ roundRectPath(ctx, x, y, width, height, radius);
827
+ ctx.fill();
828
+ } else ctx.fillRect(x, y, width, height);
829
+ clearShadow$1(ctx);
830
+ }
831
+ if (border && border.width && border.width > 0) {
832
+ ctx.strokeStyle = border.color ? resolveColor$1(ctx, border.color, x, y, width, height) : "#000";
833
+ ctx.lineWidth = border.width;
834
+ if (hasRadius) {
835
+ roundRectPath(ctx, x, y, width, height, radius);
836
+ ctx.stroke();
837
+ } else ctx.strokeRect(x, y, width, height);
838
+ }
839
+ if (element.opacity !== void 0 && element.opacity < 1) ctx.globalAlpha = 1;
840
+ }
841
+
842
+ //#endregion
843
+ //#region src/render/components/image.ts
844
+ function renderImage(ctx, node) {
845
+ const element = node.element;
846
+ const { x, y, width, height } = node.layout;
847
+ const src = element.src;
848
+ if (!src) return;
849
+ if (element.opacity !== void 0 && element.opacity < 1) ctx.globalAlpha = element.opacity;
850
+ if (element.shadow) applyShadow$1(ctx, element.shadow);
851
+ const border = element.border;
852
+ const radius = normalizeBorderRadius(border?.radius);
853
+ const hasRadius = radius.some((r) => r > 0);
854
+ if (hasRadius) {
855
+ ctx.save();
856
+ roundRectPath(ctx, x, y, width, height, radius);
857
+ ctx.clip();
858
+ }
859
+ const imgWidth = "naturalWidth" in src ? src.naturalWidth : "width" in src ? +src.width : 0;
860
+ const imgHeight = "naturalHeight" in src ? src.naturalHeight : "height" in src ? +src.height : 0;
861
+ const fit = element.fit ?? "fill";
862
+ let drawX = x;
863
+ let drawY = y;
864
+ let drawWidth = width;
865
+ let drawHeight = height;
866
+ if (fit !== "fill" && imgWidth > 0 && imgHeight > 0) {
867
+ const imgAspect = imgWidth / imgHeight;
868
+ const boxAspect = width / height;
869
+ let scale = 1;
870
+ switch (fit) {
871
+ case "contain":
872
+ scale = imgAspect > boxAspect ? width / imgWidth : height / imgHeight;
873
+ break;
874
+ case "cover":
875
+ scale = imgAspect > boxAspect ? height / imgHeight : width / imgWidth;
876
+ break;
877
+ case "scale-down":
878
+ scale = Math.min(1, imgAspect > boxAspect ? width / imgWidth : height / imgHeight);
879
+ break;
880
+ case "none":
881
+ scale = 1;
882
+ break;
883
+ }
884
+ drawWidth = imgWidth * scale;
885
+ drawHeight = imgHeight * scale;
886
+ const position = element.position ?? {};
887
+ const posX = position.x ?? "center";
888
+ const posY = position.y ?? "center";
889
+ if (typeof posX === "number") drawX = x + posX;
890
+ else switch (posX) {
891
+ case "left":
892
+ drawX = x;
893
+ break;
894
+ case "center":
895
+ drawX = x + (width - drawWidth) / 2;
896
+ break;
897
+ case "right":
898
+ drawX = x + width - drawWidth;
899
+ break;
900
+ }
901
+ if (typeof posY === "number") drawY = y + posY;
902
+ else switch (posY) {
903
+ case "top":
904
+ drawY = y;
905
+ break;
906
+ case "center":
907
+ drawY = y + (height - drawHeight) / 2;
908
+ break;
909
+ case "bottom":
910
+ drawY = y + height - drawHeight;
911
+ break;
912
+ }
913
+ }
914
+ ctx.drawImage(src, drawX, drawY, drawWidth, drawHeight);
915
+ if (element.shadow) clearShadow$1(ctx);
916
+ if (hasRadius) ctx.restore();
917
+ if (border && border.width && border.width > 0) {
918
+ ctx.strokeStyle = border.color ? resolveColor$1(ctx, border.color, x, y, width, height) : "#000";
919
+ ctx.lineWidth = border.width;
920
+ if (hasRadius) {
921
+ roundRectPath(ctx, x, y, width, height, radius);
922
+ ctx.stroke();
923
+ } else ctx.strokeRect(x, y, width, height);
924
+ }
925
+ if (element.opacity !== void 0 && element.opacity < 1) ctx.globalAlpha = 1;
926
+ }
927
+
928
+ //#endregion
929
+ //#region src/compat/DOMMatrix.ts
930
+ const DOMMatrixCompat = (() => {
931
+ if (typeof DOMMatrix !== "undefined") return DOMMatrix;
932
+ try {
933
+ return require("@napi-rs/canvas").DOMMatrix;
934
+ } catch {
935
+ throw new Error("DOMMatrix is not available. In Node.js, install @napi-rs/canvas.");
936
+ }
937
+ })();
938
+
939
+ //#endregion
940
+ //#region src/compat/Path2D.ts
941
+ const Path2DCompat = (() => {
942
+ if (typeof Path2D !== "undefined") return Path2D;
943
+ try {
944
+ return require("@napi-rs/canvas").Path2D;
945
+ } catch {
946
+ throw new Error("Path2D is not available. In Node.js, install @napi-rs/canvas.");
947
+ }
948
+ })();
949
+
950
+ //#endregion
951
+ //#region src/render/components/svg.ts
952
+ function isGradientDescriptor(color) {
953
+ return typeof color === "object" && color !== null && "type" in color && (color.type === "linear-gradient" || color.type === "radial-gradient");
954
+ }
955
+ function resolveGradient(ctx, descriptor, x, y, width, height) {
956
+ if (descriptor.type === "linear-gradient") {
957
+ const angleRad = (descriptor.angle - 90) * Math.PI / 180;
958
+ const centerX = x + width / 2;
959
+ const centerY = y + height / 2;
960
+ const diagLength = Math.sqrt(width * width + height * height) / 2;
961
+ const x0 = centerX - Math.cos(angleRad) * diagLength;
962
+ const y0 = centerY - Math.sin(angleRad) * diagLength;
963
+ const x1 = centerX + Math.cos(angleRad) * diagLength;
964
+ const y1 = centerY + Math.sin(angleRad) * diagLength;
965
+ const gradient = ctx.createLinearGradient(x0, y0, x1, y1);
966
+ for (const stop of descriptor.stops) gradient.addColorStop(stop.offset, stop.color);
967
+ return gradient;
968
+ } else {
969
+ const diagLength = Math.sqrt(width * width + height * height);
970
+ const startX = x + (descriptor.startX ?? .5) * width;
971
+ const startY = y + (descriptor.startY ?? .5) * height;
972
+ const startRadius = (descriptor.startRadius ?? 0) * diagLength;
973
+ const endX = x + (descriptor.endX ?? .5) * width;
974
+ const endY = y + (descriptor.endY ?? .5) * height;
975
+ const endRadius = (descriptor.endRadius ?? .5) * diagLength;
976
+ const gradient = ctx.createRadialGradient(startX, startY, startRadius, endX, endY, endRadius);
977
+ for (const stop of descriptor.stops) gradient.addColorStop(stop.offset, stop.color);
978
+ return gradient;
979
+ }
980
+ }
981
+ function resolveColor(ctx, color, x, y, width, height) {
982
+ if (isGradientDescriptor(color)) return resolveGradient(ctx, color, x, y, width, height);
983
+ return color;
984
+ }
985
+ function applyTransform(base, transform) {
986
+ if (!transform) return base;
987
+ let result = new DOMMatrixCompat([
988
+ base.a,
989
+ base.b,
990
+ base.c,
991
+ base.d,
992
+ base.e,
993
+ base.f
994
+ ]);
995
+ if (transform.matrix) {
996
+ const [a, b, c, d, e, f] = transform.matrix;
997
+ result = result.multiply(new DOMMatrixCompat([
998
+ a,
999
+ b,
1000
+ c,
1001
+ d,
1002
+ e,
1003
+ f
1004
+ ]));
1005
+ }
1006
+ if (transform.translate) result = result.translate(transform.translate[0], transform.translate[1]);
1007
+ if (transform.rotate !== void 0) if (typeof transform.rotate === "number") result = result.rotate(transform.rotate);
1008
+ else {
1009
+ const [angle, cx, cy] = transform.rotate;
1010
+ result = result.translate(cx, cy).rotate(angle).translate(-cx, -cy);
1011
+ }
1012
+ if (transform.scale !== void 0) if (typeof transform.scale === "number") result = result.scale(transform.scale);
1013
+ else result = result.scale(transform.scale[0], transform.scale[1]);
1014
+ if (transform.skewX !== void 0) result = result.skewX(transform.skewX);
1015
+ if (transform.skewY !== void 0) result = result.skewY(transform.skewY);
1016
+ return result;
1017
+ }
1018
+ function applyStroke(ctx, stroke, bounds) {
1019
+ ctx.strokeStyle = resolveColor(ctx, stroke.color, bounds.x, bounds.y, bounds.width, bounds.height);
1020
+ ctx.lineWidth = stroke.width;
1021
+ ctx.setLineDash(stroke.dash ?? []);
1022
+ if (stroke.cap) ctx.lineCap = stroke.cap;
1023
+ if (stroke.join) ctx.lineJoin = stroke.join;
1024
+ }
1025
+ function applyFill(ctx, fill, bounds) {
1026
+ ctx.fillStyle = resolveColor(ctx, fill, bounds.x, bounds.y, bounds.width, bounds.height);
1027
+ ctx.fill();
1028
+ }
1029
+ function applyFillAndStroke(ctx, shape, bounds) {
1030
+ if (shape.fill && shape.fill !== "none") applyFill(ctx, shape.fill, bounds);
1031
+ if (shape.stroke) {
1032
+ applyStroke(ctx, shape.stroke, bounds);
1033
+ ctx.stroke();
1034
+ }
1035
+ }
1036
+ function renderSvgRect(ctx, rect, bounds) {
1037
+ const { x = 0, y = 0, width, height, rx = 0, ry = 0 } = rect;
1038
+ ctx.beginPath();
1039
+ if (rx || ry) ctx.roundRect(x, y, width, height, Math.max(rx, ry));
1040
+ else ctx.rect(x, y, width, height);
1041
+ applyFillAndStroke(ctx, rect, bounds);
1042
+ }
1043
+ function renderSvgCircle(ctx, circle, bounds) {
1044
+ ctx.beginPath();
1045
+ ctx.arc(circle.cx, circle.cy, circle.r, 0, Math.PI * 2);
1046
+ applyFillAndStroke(ctx, circle, bounds);
1047
+ }
1048
+ function renderSvgEllipse(ctx, ellipse, bounds) {
1049
+ ctx.beginPath();
1050
+ ctx.ellipse(ellipse.cx, ellipse.cy, ellipse.rx, ellipse.ry, 0, 0, Math.PI * 2);
1051
+ applyFillAndStroke(ctx, ellipse, bounds);
1052
+ }
1053
+ function renderSvgLine(ctx, line, bounds) {
1054
+ ctx.beginPath();
1055
+ ctx.moveTo(line.x1, line.y1);
1056
+ ctx.lineTo(line.x2, line.y2);
1057
+ if (line.stroke) {
1058
+ applyStroke(ctx, line.stroke, bounds);
1059
+ ctx.stroke();
1060
+ }
1061
+ }
1062
+ function renderSvgPolyline(ctx, polyline, bounds) {
1063
+ const { points } = polyline;
1064
+ if (points.length === 0) return;
1065
+ ctx.beginPath();
1066
+ ctx.moveTo(points[0][0], points[0][1]);
1067
+ for (let i = 1; i < points.length; i++) ctx.lineTo(points[i][0], points[i][1]);
1068
+ applyFillAndStroke(ctx, polyline, bounds);
1069
+ }
1070
+ function renderSvgPolygon(ctx, polygon, bounds) {
1071
+ const { points } = polygon;
1072
+ if (points.length === 0) return;
1073
+ ctx.beginPath();
1074
+ ctx.moveTo(points[0][0], points[0][1]);
1075
+ for (let i = 1; i < points.length; i++) ctx.lineTo(points[i][0], points[i][1]);
1076
+ ctx.closePath();
1077
+ applyFillAndStroke(ctx, polygon, bounds);
1078
+ }
1079
+ function renderSvgPath(ctx, path, bounds) {
1080
+ const path2d = new Path2DCompat(path.d);
1081
+ if (path.fill && path.fill !== "none") {
1082
+ ctx.fillStyle = resolveColor(ctx, path.fill, bounds.x, bounds.y, bounds.width, bounds.height);
1083
+ ctx.fill(path2d);
1084
+ }
1085
+ if (path.stroke) {
1086
+ applyStroke(ctx, path.stroke, bounds);
1087
+ ctx.stroke(path2d);
1088
+ }
1089
+ }
1090
+ const TEXT_ANCHOR_MAP = {
1091
+ start: "left",
1092
+ middle: "center",
1093
+ end: "right"
1094
+ };
1095
+ const BASELINE_MAP = {
1096
+ auto: "alphabetic",
1097
+ middle: "middle",
1098
+ hanging: "hanging"
1099
+ };
1100
+ function renderSvgText(ctx, text, bounds) {
1101
+ const { x = 0, y = 0, content, font, textAnchor = "start", dominantBaseline = "auto" } = text;
1102
+ ctx.font = buildFontString(font ?? {});
1103
+ ctx.textAlign = TEXT_ANCHOR_MAP[textAnchor] ?? "left";
1104
+ ctx.textBaseline = BASELINE_MAP[dominantBaseline] ?? "alphabetic";
1105
+ if (text.fill && text.fill !== "none") {
1106
+ ctx.fillStyle = resolveColor(ctx, text.fill, bounds.x, bounds.y, bounds.width, bounds.height);
1107
+ ctx.fillText(content, x, y);
1108
+ }
1109
+ if (text.stroke) {
1110
+ applyStroke(ctx, text.stroke, bounds);
1111
+ ctx.strokeText(content, x, y);
1112
+ }
1113
+ }
1114
+ function renderSvgChild(ctx, child, parentTransform, bounds, baseTransform) {
1115
+ const localTransform = applyTransform(parentTransform, child.transform);
1116
+ ctx.save();
1117
+ ctx.setTransform(baseTransform.multiply(localTransform));
1118
+ if (child.opacity !== void 0) ctx.globalAlpha *= child.opacity;
1119
+ switch (child.type) {
1120
+ case "rect":
1121
+ renderSvgRect(ctx, child, bounds);
1122
+ break;
1123
+ case "circle":
1124
+ renderSvgCircle(ctx, child, bounds);
1125
+ break;
1126
+ case "ellipse":
1127
+ renderSvgEllipse(ctx, child, bounds);
1128
+ break;
1129
+ case "line":
1130
+ renderSvgLine(ctx, child, bounds);
1131
+ break;
1132
+ case "polyline":
1133
+ renderSvgPolyline(ctx, child, bounds);
1134
+ break;
1135
+ case "polygon":
1136
+ renderSvgPolygon(ctx, child, bounds);
1137
+ break;
1138
+ case "path":
1139
+ renderSvgPath(ctx, child, bounds);
1140
+ break;
1141
+ case "text":
1142
+ renderSvgText(ctx, child, bounds);
1143
+ break;
1144
+ case "g":
1145
+ renderSvgGroup(ctx, child, localTransform, bounds, baseTransform);
1146
+ break;
1147
+ }
1148
+ ctx.restore();
1149
+ }
1150
+ function renderSvgGroup(ctx, group, parentTransform, bounds, baseTransform) {
1151
+ for (const child of group.children) renderSvgChild(ctx, child, parentTransform, bounds, baseTransform);
1152
+ }
1153
+ function calculateViewBoxTransform(x, y, width, height, viewBox, preserveAspectRatio) {
1154
+ const vbX = viewBox.x ?? 0;
1155
+ const vbY = viewBox.y ?? 0;
1156
+ const vbWidth = viewBox.width;
1157
+ const vbHeight = viewBox.height;
1158
+ const scaleX = width / vbWidth;
1159
+ const scaleY = height / vbHeight;
1160
+ const align = preserveAspectRatio?.align ?? "xMidYMid";
1161
+ const meetOrSlice = preserveAspectRatio?.meetOrSlice ?? "meet";
1162
+ if (align === "none") return new DOMMatrixCompat().translate(x, y).scale(scaleX, scaleY).translate(-vbX, -vbY);
1163
+ const scale = meetOrSlice === "meet" ? Math.min(scaleX, scaleY) : Math.max(scaleX, scaleY);
1164
+ const scaledWidth = vbWidth * scale;
1165
+ const scaledHeight = vbHeight * scale;
1166
+ let translateX = x;
1167
+ let translateY = y;
1168
+ if (align.includes("xMid")) translateX += (width - scaledWidth) / 2;
1169
+ else if (align.includes("xMax")) translateX += width - scaledWidth;
1170
+ if (align.includes("YMid")) translateY += (height - scaledHeight) / 2;
1171
+ else if (align.includes("YMax")) translateY += height - scaledHeight;
1172
+ return new DOMMatrixCompat().translate(translateX, translateY).scale(scale, scale).translate(-vbX, -vbY);
1173
+ }
1174
+ function applyShadow(ctx, shadow) {
1175
+ ctx.shadowOffsetX = shadow.offsetX ?? 0;
1176
+ ctx.shadowOffsetY = shadow.offsetY ?? 0;
1177
+ ctx.shadowBlur = shadow.blur ?? 0;
1178
+ ctx.shadowColor = shadow.color ?? "rgba(0,0,0,0.5)";
1179
+ }
1180
+ function clearShadow(ctx) {
1181
+ ctx.shadowOffsetX = 0;
1182
+ ctx.shadowOffsetY = 0;
1183
+ ctx.shadowBlur = 0;
1184
+ ctx.shadowColor = "transparent";
1185
+ }
1186
+ function renderSvg(ctx, node) {
1187
+ const element = node.element;
1188
+ const { x, y, width, height } = node.layout;
1189
+ const bounds = {
1190
+ x,
1191
+ y,
1192
+ width,
1193
+ height
1194
+ };
1195
+ if (element.background) {
1196
+ if (element.shadow) applyShadow(ctx, element.shadow);
1197
+ ctx.fillStyle = resolveColor(ctx, element.background, x, y, width, height);
1198
+ ctx.fillRect(x, y, width, height);
1199
+ if (element.shadow) clearShadow(ctx);
1200
+ }
1201
+ ctx.save();
1202
+ ctx.beginPath();
1203
+ ctx.rect(x, y, width, height);
1204
+ ctx.clip();
1205
+ const baseTransform = ctx.getTransform();
1206
+ const transform = calculateViewBoxTransform(x, y, width, height, element.viewBox ?? {
1207
+ x: 0,
1208
+ y: 0,
1209
+ width,
1210
+ height
1211
+ }, element.preserveAspectRatio);
1212
+ for (const child of element.children) renderSvgChild(ctx, child, transform, bounds, baseTransform);
1213
+ ctx.restore();
1214
+ }
1215
+
1216
+ //#endregion
1217
+ //#region src/render/components/text.ts
1218
+ function renderText(ctx, node) {
1219
+ const element = node.element;
1220
+ const { contentX, contentY, contentWidth, contentHeight } = node.layout;
1221
+ const lines = node.lines ?? [element.content];
1222
+ const font = element.font ?? {};
1223
+ const lineHeightPx = (font.size ?? 16) * (element.lineHeight ?? 1.2);
1224
+ ctx.font = buildFontString(font);
1225
+ ctx.fillStyle = element.color ? resolveColor$1(ctx, element.color, contentX, contentY, contentWidth, contentHeight) : "#000";
1226
+ let textAlign = "left";
1227
+ if (element.align === "center") textAlign = "center";
1228
+ else if (element.align === "right") textAlign = "right";
1229
+ ctx.textAlign = textAlign;
1230
+ ctx.textBaseline = "middle";
1231
+ const totalTextHeight = lines.length * lineHeightPx;
1232
+ let verticalOffset = 0;
1233
+ if (element.verticalAlign === "middle") verticalOffset = (contentHeight - totalTextHeight) / 2;
1234
+ else if (element.verticalAlign === "bottom") verticalOffset = contentHeight - totalTextHeight;
1235
+ let textX = contentX;
1236
+ if (element.align === "center") textX = contentX + contentWidth / 2;
1237
+ else if (element.align === "right") textX = contentX + contentWidth;
1238
+ if (element.shadow) applyShadow$1(ctx, element.shadow);
1239
+ for (let i = 0; i < lines.length; i++) {
1240
+ const correctedLineY = contentY + verticalOffset + i * lineHeightPx + lineHeightPx / 2 + (node.lineOffsets?.[i] ?? 0);
1241
+ if (element.stroke) {
1242
+ ctx.strokeStyle = resolveColor$1(ctx, element.stroke.color, contentX, contentY, contentWidth, contentHeight);
1243
+ ctx.lineWidth = element.stroke.width;
1244
+ ctx.strokeText(lines[i], textX, correctedLineY);
1245
+ }
1246
+ ctx.fillText(lines[i], textX, correctedLineY);
1247
+ }
1248
+ if (element.shadow) clearShadow$1(ctx);
1249
+ }
1250
+
1251
+ //#endregion
1252
+ //#region src/render/index.ts
1253
+ function renderNode(ctx, node) {
1254
+ const element = node.element;
1255
+ switch (element.type) {
1256
+ case "box":
1257
+ case "stack": {
1258
+ renderBox(ctx, node);
1259
+ const shouldClip = element.clip === true;
1260
+ if (shouldClip) {
1261
+ ctx.save();
1262
+ const { x, y, width, height } = node.layout;
1263
+ roundRectPath(ctx, x, y, width, height, normalizeBorderRadius(element.border?.radius));
1264
+ ctx.clip();
1265
+ }
1266
+ for (const child of node.children) renderNode(ctx, child);
1267
+ if (shouldClip) ctx.restore();
1268
+ break;
1269
+ }
1270
+ case "text":
1271
+ renderText(ctx, node);
1272
+ break;
1273
+ case "image":
1274
+ renderImage(ctx, node);
1275
+ break;
1276
+ case "svg":
1277
+ renderSvg(ctx, node);
1278
+ break;
1279
+ }
1280
+ }
1281
+
1282
+ //#endregion
1283
+ Object.defineProperty(exports, 'computeLayout', {
1284
+ enumerable: true,
1285
+ get: function () {
1286
+ return computeLayout;
1287
+ }
1288
+ });
1289
+ Object.defineProperty(exports, 'createCanvasMeasureContext', {
1290
+ enumerable: true,
1291
+ get: function () {
1292
+ return createCanvasMeasureContext;
1293
+ }
1294
+ });
1295
+ Object.defineProperty(exports, 'linearGradient', {
1296
+ enumerable: true,
1297
+ get: function () {
1298
+ return linearGradient;
1299
+ }
1300
+ });
1301
+ Object.defineProperty(exports, 'radialGradient', {
1302
+ enumerable: true,
1303
+ get: function () {
1304
+ return radialGradient;
1305
+ }
1306
+ });
1307
+ Object.defineProperty(exports, 'renderNode', {
1308
+ enumerable: true,
1309
+ get: function () {
1310
+ return renderNode;
1311
+ }
1312
+ });