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