vizmatic 0.4.0 → 0.6.0
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/PROMPT.md +75 -531
- package/README.md +85 -1052
- package/ROADMAP.md +49 -67
- package/SCIENCE.md +250 -0
- package/assets/THIRD_PARTY_LICENSES.md +44 -1
- package/assets/fonts/InstrumentSerif-Italic.ttf +0 -0
- package/assets/fonts/InstrumentSerif-Regular.ttf +0 -0
- package/assets/fonts/SourceSerif4-24pt-Italic.ttf +0 -0
- package/assets/fonts/SourceSerif4-24pt-Regular.ttf +0 -0
- package/assets/licenses/raster-codecs-MIT.txt +74 -0
- package/dist/chunk-3F2VQ7NW.js +1 -0
- package/dist/{chunk-FEIPTPBZ.js → chunk-4XE64I2U.js} +53 -463
- package/dist/chunk-5ZAFH2JC.js +4231 -0
- package/dist/chunk-JHHPLALU.js +79 -0
- package/dist/chunk-LTX44BAE.js +112 -0
- package/dist/chunk-MLKGABMK.js +9 -0
- package/dist/chunk-PSBF4TEK.js +1 -0
- package/dist/chunk-RPAHMKAR.js +605 -0
- package/dist/chunk-RWF4VBUL.js +3617 -0
- package/dist/cli.js +369 -12
- package/dist/index.cjs +7413 -1810
- package/dist/index.d.cts +900 -599
- package/dist/index.d.ts +900 -599
- package/dist/index.js +5019 -3278
- package/dist/math-LXB4FH4E.js +7 -0
- package/dist/math-QVCETQDQ.js +1 -0
- package/dist/scene-browser-assets.d.ts +38 -0
- package/dist/scene-browser-assets.js +53 -0
- package/dist/scene-browser-chunks/chunk-5MVLYGXM.js +70 -0
- package/dist/scene-browser-chunks/chunk-ASXKN3NU.js +1 -0
- package/dist/scene-browser-chunks/chunk-CM5Y5LZP.js +1 -0
- package/dist/scene-browser-chunks/chunk-DP7NB4EW.js +6 -0
- package/dist/scene-browser-chunks/emscripten-module.browser-M5AEKJGE.js +1 -0
- package/dist/scene-browser-chunks/ffi-FL6ORJU3.js +1 -0
- package/dist/scene-browser-chunks/module-ES6BEMUI-JD5S5VKR.js +1 -0
- package/dist/scene-browser-chunks/module-asyncify-2EFITU5U-HWPFJFJC.js +1 -0
- package/dist/scene-browser-chunks/scene-browser-layout-M3XSXMV4.js +27 -0
- package/dist/scene-browser.d.ts +117 -0
- package/dist/scene-browser.js +220 -0
- package/dist/scene-cli.js +1 -1
- package/dist/scene-runtime.cjs +168 -103
- package/dist/scene-runtime.d.cts +3 -2
- package/dist/scene-runtime.d.ts +3 -2
- package/dist/scene-runtime.js +3 -1
- package/dist/server-2FYHZ4UO.js +331 -0
- package/dist/stage-browser/browser.d.ts +2483 -0
- package/dist/stage-browser/chunk-SQE76S5B.js +1 -0
- package/dist/stage-browser/chunk-VZNL6JY2.js +59 -0
- package/dist/stage-browser/dagre.esm-N2NIVIAZ.js +6 -0
- package/dist/stage-browser/math-4WFNHVV2.js +18 -0
- package/dist/stage-browser/stage.js +19 -0
- package/dist/stage-export.cjs +4980 -0
- package/dist/stage-export.d.cts +112 -0
- package/dist/stage-export.d.ts +112 -0
- package/dist/stage-export.js +544 -0
- package/dist/stage.cjs +9789 -0
- package/dist/stage.d.cts +1679 -0
- package/dist/stage.d.ts +1679 -0
- package/dist/stage.js +5418 -0
- package/dist/svg-import-Djj2q7Pc.d.cts +742 -0
- package/dist/svg-import-Djj2q7Pc.d.ts +742 -0
- package/dist/video-CcHW1qhV.d.cts +532 -0
- package/dist/video-CcHW1qhV.d.ts +532 -0
- package/examples/README.md +18 -0
- package/examples/agent-pipeline.tsx +4 -4
- package/examples/animated-pipeline.tsx +1 -5
- package/examples/attention-head.tsx +12 -12
- package/examples/aurora-incident.tsx +4 -3
- package/examples/building-blocks.tsx +8 -9
- package/examples/catalog-charts.tsx +4 -4
- package/examples/catalog-content.tsx +7 -9
- package/examples/catalog-convolution-kernel.tsx +116 -0
- package/examples/catalog-data.tsx +2 -8
- package/examples/catalog-dataflow.tsx +5 -9
- package/examples/catalog-dense.tsx +80 -0
- package/examples/catalog-deployment.tsx +5 -11
- package/examples/catalog-diagrams.tsx +13 -16
- package/examples/catalog-drawing.tsx +7 -7
- package/examples/catalog-flows.tsx +4 -4
- package/examples/catalog-foundations.tsx +11 -11
- package/examples/catalog-gaussian-blur.tsx +146 -0
- package/examples/catalog-image-tensors.tsx +115 -0
- package/examples/catalog-ml.tsx +36 -31
- package/examples/catalog-numeric-matrices.tsx +93 -0
- package/examples/catalog-photo-blur.tsx +209 -0
- package/examples/catalog-science-ai.tsx +130 -0
- package/examples/catalog-science-chemistry.tsx +103 -0
- package/examples/catalog-science-math.tsx +93 -0
- package/examples/catalog-science-physics.tsx +84 -0
- package/examples/catalog-sequence.tsx +59 -38
- package/examples/data-pipeline-lineage-presentation.tsx +5 -0
- package/examples/data-pipeline-lineage.tsx +85 -0
- package/examples/daybreak-launch.tsx +1 -0
- package/examples/desired-quadrant.tsx +7 -8
- package/examples/engineering-dashboard-presentation.tsx +13 -0
- package/examples/engineering-dashboard.tsx +160 -0
- package/examples/engineering-flow.tsx +2 -4
- package/examples/eval-dashboard.tsx +6 -6
- package/examples/films/attention.ts +278 -0
- package/examples/films/cache-miss.ts +431 -0
- package/examples/films/catalog.json +132 -0
- package/examples/films/central-limit.ts +269 -0
- package/examples/films/complex-maps.ts +273 -0
- package/examples/films/deck-example.ts +224 -0
- package/examples/films/euler-circle.ts +179 -0
- package/examples/films/fourier-epicycles.ts +265 -0
- package/examples/films/gradient-descent.ts +345 -0
- package/examples/films/linear-maps.ts +175 -0
- package/examples/films/lorenz.ts +207 -0
- package/examples/films/pythagoras.ts +185 -0
- package/examples/films/taylor-series.ts +159 -0
- package/examples/films/vizmatic-title.ts +117 -0
- package/examples/llm-serving-stack-presentation.tsx +5 -0
- package/examples/llm-serving-stack.tsx +94 -0
- package/examples/model-garden.tsx +1 -0
- package/examples/model-router.tsx +6 -6
- package/examples/nccl-all-gather.tsx +3 -3
- package/examples/nccl-all-reduce.tsx +3 -3
- package/examples/nccl-all-to-all.tsx +1 -1
- package/examples/nccl-broadcast.tsx +1 -1
- package/examples/nccl-reduce-scatter.tsx +5 -5
- package/examples/orthogonal-routing.tsx +4 -4
- package/examples/pareto-frontier.tsx +9 -10
- package/examples/presentation-frame.tsx +6 -6
- package/examples/production-system-map-presentation.tsx +5 -0
- package/examples/production-system-map.tsx +130 -0
- package/examples/rag-graph.tsx +7 -7
- package/examples/release-card.tsx +5 -5
- package/examples/spec-anatomy.tsx +5 -5
- package/examples/system-architecture.tsx +19 -32
- package/examples/theme-system.tsx +1 -1
- package/examples/token-matrix.tsx +4 -2
- package/examples/transparent-badge.tsx +1 -2
- package/examples/videos/README.md +36 -2
- package/examples/videos/calculus-tangent.tsx +101 -0
- package/examples/videos/catalyst-pathway.tsx +56 -0
- package/examples/videos/causal-attention.tsx +91 -0
- package/examples/videos/convolution-kernel.tsx +34 -0
- package/examples/videos/gaussian-blur.tsx +32 -0
- package/examples/videos/image-to-matrix.tsx +22 -0
- package/examples/videos/matrix-multiplication.tsx +128 -0
- package/examples/videos/photo-gaussian-blur.tsx +39 -0
- package/examples/videos/wave-interference.tsx +80 -0
- package/examples/vizmatic-bare.d.ts +1 -0
- package/examples/vizmatic-hero.tsx +9 -18
- package/examples/website-workflow.tsx +7 -7
- package/examples/weekend-field-notes.tsx +3 -2
- package/package.json +65 -11
- package/dist/chunk-TH3F4SDB.js +0 -79
|
@@ -0,0 +1,3617 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getThemeColors,
|
|
3
|
+
setDensityResolver
|
|
4
|
+
} from "./chunk-RPAHMKAR.js";
|
|
5
|
+
|
|
6
|
+
// src/brand.ts
|
|
7
|
+
import React from "react";
|
|
8
|
+
var DEFAULT_WATERMARK_TEXT = "Vizmatic";
|
|
9
|
+
var WATERMARK_COLOR_DARK = "#a5a7ad";
|
|
10
|
+
var WATERMARK_COLOR_LIGHT = "#5e5a50";
|
|
11
|
+
function VizmaticIcon({ size = 14, color }) {
|
|
12
|
+
return React.createElement(
|
|
13
|
+
"svg",
|
|
14
|
+
{
|
|
15
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
16
|
+
width: size,
|
|
17
|
+
height: size,
|
|
18
|
+
viewBox: "0 0 28 28",
|
|
19
|
+
fill: "none"
|
|
20
|
+
},
|
|
21
|
+
React.createElement("rect", { x: 2.5, y: 2.5, width: 23, height: 23, rx: 6.5, stroke: color, strokeWidth: 2, opacity: 0.54 }),
|
|
22
|
+
React.createElement("rect", { x: 7, y: 7, width: 5.5, height: 5.5, rx: 1.8, fill: color, opacity: 0.35 }),
|
|
23
|
+
React.createElement("rect", { x: 15.5, y: 7, width: 5.5, height: 5.5, rx: 1.8, fill: color }),
|
|
24
|
+
React.createElement("rect", { x: 7, y: 15.5, width: 5.5, height: 5.5, rx: 1.8, fill: color, opacity: 0.82 }),
|
|
25
|
+
React.createElement("rect", { x: 15.5, y: 15.5, width: 5.5, height: 5.5, rx: 1.8, stroke: color, strokeWidth: 1.5, opacity: 0.72 }),
|
|
26
|
+
React.createElement("path", { d: "M9.75 9.75h8.5M9.75 18.25h8.5M9.75 9.75v8.5M18.25 9.75v8.5", stroke: color, strokeWidth: 1.6, strokeLinecap: "round", opacity: 0.88 })
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
function normalizeWatermark(watermark) {
|
|
30
|
+
if (watermark == null || watermark === false) return void 0;
|
|
31
|
+
if (watermark === true) return {};
|
|
32
|
+
if (typeof watermark === "string") return { text: watermark };
|
|
33
|
+
if (React.isValidElement(watermark)) {
|
|
34
|
+
if (watermark.type !== Watermark) return { element: watermark };
|
|
35
|
+
const { children, ...props } = watermark.props;
|
|
36
|
+
return {
|
|
37
|
+
...props,
|
|
38
|
+
element: children ?? props.element
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return watermark;
|
|
42
|
+
}
|
|
43
|
+
function Watermark(_props) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
function positionStyle(position) {
|
|
47
|
+
switch (position) {
|
|
48
|
+
case "top-left":
|
|
49
|
+
return { top: 8, left: 10 };
|
|
50
|
+
case "bottom-left":
|
|
51
|
+
return { bottom: 8, left: 10 };
|
|
52
|
+
case "bottom-right":
|
|
53
|
+
return { bottom: 8, right: 10 };
|
|
54
|
+
case "top-right":
|
|
55
|
+
default:
|
|
56
|
+
return { top: 8, right: 10 };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function imageNode(image) {
|
|
60
|
+
if (!image) return void 0;
|
|
61
|
+
const imageOptions = typeof image === "string" ? { src: image } : image;
|
|
62
|
+
return React.createElement("img", {
|
|
63
|
+
src: imageOptions.src,
|
|
64
|
+
alt: imageOptions.alt ?? "",
|
|
65
|
+
width: imageOptions.width ?? 14,
|
|
66
|
+
height: imageOptions.height ?? 14,
|
|
67
|
+
style: {
|
|
68
|
+
display: "block",
|
|
69
|
+
width: imageOptions.width ?? 14,
|
|
70
|
+
height: imageOptions.height ?? 14,
|
|
71
|
+
objectFit: "contain"
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
function iconNode(icon, color) {
|
|
76
|
+
if (icon === false) return void 0;
|
|
77
|
+
if (icon == null) return React.createElement(VizmaticIcon, { size: 13, color });
|
|
78
|
+
if (typeof icon !== "string") return icon;
|
|
79
|
+
return React.createElement("div", {
|
|
80
|
+
style: {
|
|
81
|
+
minWidth: 13,
|
|
82
|
+
height: 13,
|
|
83
|
+
display: "flex",
|
|
84
|
+
alignItems: "center",
|
|
85
|
+
justifyContent: "center",
|
|
86
|
+
color,
|
|
87
|
+
fontSize: icon.length > 2 ? 8 : 10,
|
|
88
|
+
fontWeight: 800,
|
|
89
|
+
fontFamily: "Inter",
|
|
90
|
+
lineHeight: 1
|
|
91
|
+
}
|
|
92
|
+
}, icon);
|
|
93
|
+
}
|
|
94
|
+
function wrapWithWatermark(element, width, height, theme = "dark", watermark = true) {
|
|
95
|
+
const isDark = theme === "dark";
|
|
96
|
+
const options = normalizeWatermark(watermark);
|
|
97
|
+
const color = options?.color ?? (isDark ? WATERMARK_COLOR_DARK : WATERMARK_COLOR_LIGHT);
|
|
98
|
+
const position = options?.position ?? "top-right";
|
|
99
|
+
const opacity = options?.opacity ?? (isDark ? 0.55 : 0.4);
|
|
100
|
+
const icon = options ? imageNode(options.image) ?? iconNode(options.icon, color) : void 0;
|
|
101
|
+
const text = options?.text ?? DEFAULT_WATERMARK_TEXT;
|
|
102
|
+
const content = options?.element ?? [
|
|
103
|
+
icon,
|
|
104
|
+
text !== false && text && React.createElement("div", {
|
|
105
|
+
key: "text",
|
|
106
|
+
style: {
|
|
107
|
+
fontSize: 11,
|
|
108
|
+
fontWeight: 600,
|
|
109
|
+
color,
|
|
110
|
+
fontFamily: "Inter",
|
|
111
|
+
letterSpacing: 0.3
|
|
112
|
+
}
|
|
113
|
+
}, text)
|
|
114
|
+
];
|
|
115
|
+
return React.createElement(
|
|
116
|
+
"div",
|
|
117
|
+
{
|
|
118
|
+
style: {
|
|
119
|
+
width,
|
|
120
|
+
height,
|
|
121
|
+
display: "flex",
|
|
122
|
+
position: "relative"
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
element,
|
|
126
|
+
options && React.createElement(
|
|
127
|
+
"div",
|
|
128
|
+
{
|
|
129
|
+
style: {
|
|
130
|
+
position: "absolute",
|
|
131
|
+
...positionStyle(position),
|
|
132
|
+
display: "flex",
|
|
133
|
+
alignItems: "center",
|
|
134
|
+
gap: 5,
|
|
135
|
+
opacity
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
content
|
|
139
|
+
)
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
function wrapWithBrand(element, width, height, theme = "dark", label = DEFAULT_WATERMARK_TEXT) {
|
|
143
|
+
return wrapWithWatermark(element, width, height, theme, label);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// src/autocrop.ts
|
|
147
|
+
function detectBackgroundColor(pixels) {
|
|
148
|
+
const r = pixels[0];
|
|
149
|
+
const g = pixels[1];
|
|
150
|
+
const b = pixels[2];
|
|
151
|
+
const a = pixels[3];
|
|
152
|
+
if (a < 10) {
|
|
153
|
+
return "transparent";
|
|
154
|
+
}
|
|
155
|
+
return `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}`;
|
|
156
|
+
}
|
|
157
|
+
function parseHexColor(hex) {
|
|
158
|
+
const h = hex.startsWith("#") ? hex.slice(1) : hex;
|
|
159
|
+
return [
|
|
160
|
+
parseInt(h.slice(0, 2), 16),
|
|
161
|
+
parseInt(h.slice(2, 4), 16),
|
|
162
|
+
parseInt(h.slice(4, 6), 16)
|
|
163
|
+
];
|
|
164
|
+
}
|
|
165
|
+
function createBackgroundMatcher(bgColor, tolerance = 6) {
|
|
166
|
+
if (bgColor === "transparent") {
|
|
167
|
+
return (_r, _g, _b, a) => a < 10;
|
|
168
|
+
}
|
|
169
|
+
const [bgR, bgG, bgB] = parseHexColor(bgColor);
|
|
170
|
+
return (r, g, b, a) => {
|
|
171
|
+
if (a < 10) return true;
|
|
172
|
+
return Math.abs(r - bgR) <= tolerance && Math.abs(g - bgG) <= tolerance && Math.abs(b - bgB) <= tolerance;
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
function detectContentBounds(pixels, width, height, bgColor, padding = 20) {
|
|
176
|
+
const isBackground = createBackgroundMatcher(bgColor);
|
|
177
|
+
let minX = width;
|
|
178
|
+
let minY = height;
|
|
179
|
+
let maxX = 0;
|
|
180
|
+
let maxY = 0;
|
|
181
|
+
for (let y = 0; y < height; y++) {
|
|
182
|
+
for (let x = 0; x < width; x++) {
|
|
183
|
+
const idx = (y * width + x) * 4;
|
|
184
|
+
const r = pixels[idx];
|
|
185
|
+
const g = pixels[idx + 1];
|
|
186
|
+
const b = pixels[idx + 2];
|
|
187
|
+
const a = pixels[idx + 3];
|
|
188
|
+
if (!isBackground(r, g, b, a)) {
|
|
189
|
+
if (x < minX) minX = x;
|
|
190
|
+
if (x > maxX) maxX = x;
|
|
191
|
+
if (y < minY) minY = y;
|
|
192
|
+
if (y > maxY) maxY = y;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (minX > maxX || minY > maxY) {
|
|
197
|
+
return { x: 0, y: 0, width, height };
|
|
198
|
+
}
|
|
199
|
+
const cropX = Math.max(0, minX - padding);
|
|
200
|
+
const cropY = Math.max(0, minY - padding);
|
|
201
|
+
const cropRight = Math.min(width, maxX + 1 + padding);
|
|
202
|
+
const cropBottom = Math.min(height, maxY + 1 + padding);
|
|
203
|
+
return {
|
|
204
|
+
x: cropX,
|
|
205
|
+
y: cropY,
|
|
206
|
+
width: cropRight - cropX,
|
|
207
|
+
height: cropBottom - cropY
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function cropPixels(pixels, srcWidth, region) {
|
|
211
|
+
const out = new Uint8Array(region.width * region.height * 4);
|
|
212
|
+
for (let y = 0; y < region.height; y++) {
|
|
213
|
+
const srcOffset = ((region.y + y) * srcWidth + region.x) * 4;
|
|
214
|
+
const dstOffset = y * region.width * 4;
|
|
215
|
+
out.set(
|
|
216
|
+
pixels.subarray(srcOffset, srcOffset + region.width * 4),
|
|
217
|
+
dstOffset
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
return out;
|
|
221
|
+
}
|
|
222
|
+
function detectOverflow(pixels, width, height, bgColor, edgeTolerance = 3, minDensity = 0.05, alphaThreshold = 10) {
|
|
223
|
+
const isBackground = createBackgroundMatcher(bgColor);
|
|
224
|
+
function isContentPixel(x, y) {
|
|
225
|
+
const idx = (y * width + x) * 4;
|
|
226
|
+
const a = pixels[idx + 3];
|
|
227
|
+
if (a < alphaThreshold) return false;
|
|
228
|
+
return !isBackground(pixels[idx], pixels[idx + 1], pixels[idx + 2], a);
|
|
229
|
+
}
|
|
230
|
+
function checkEdge(scanFn, length) {
|
|
231
|
+
for (let offset = 0; offset < edgeTolerance; offset++) {
|
|
232
|
+
let contentCount = 0;
|
|
233
|
+
for (let i = 0; i < length; i++) {
|
|
234
|
+
const [x, y] = scanFn(i, offset);
|
|
235
|
+
if (x >= 0 && x < width && y >= 0 && y < height && isContentPixel(x, y)) {
|
|
236
|
+
contentCount++;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (contentCount / length >= minDensity) {
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
const bottom = checkEdge((i, off) => [i, height - 1 - off], width);
|
|
246
|
+
const right = checkEdge((i, off) => [width - 1 - off, i], height);
|
|
247
|
+
const top = checkEdge((i, off) => [i, off], width);
|
|
248
|
+
const left = checkEdge((i, off) => [off, i], height);
|
|
249
|
+
const overflowEdges = [];
|
|
250
|
+
if (top) overflowEdges.push("top");
|
|
251
|
+
if (right) overflowEdges.push("right");
|
|
252
|
+
if (bottom) overflowEdges.push("bottom");
|
|
253
|
+
if (left) overflowEdges.push("left");
|
|
254
|
+
const overflows = overflowEdges.length > 0;
|
|
255
|
+
const message = overflows ? `Content overflows canvas at: ${overflowEdges.join(", ")}. Increase the illustration dimensions.` : "";
|
|
256
|
+
return {
|
|
257
|
+
overflows,
|
|
258
|
+
edges: { top, right, bottom, left },
|
|
259
|
+
message
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
function detectLayoutOverflow(nodes, width, height) {
|
|
263
|
+
const edges = { top: false, right: false, bottom: false, left: false };
|
|
264
|
+
for (const node of nodes) {
|
|
265
|
+
if (!String(node.textContent ?? "").trim() || node.width <= 0 || node.height <= 0) continue;
|
|
266
|
+
const style = node.props.style;
|
|
267
|
+
if (style?.transform) continue;
|
|
268
|
+
edges.top ||= node.top + node.height < 0;
|
|
269
|
+
edges.left ||= node.left + node.width < 0;
|
|
270
|
+
edges.bottom ||= node.top + node.height > height + 1;
|
|
271
|
+
edges.right ||= node.left + node.width > width + 1;
|
|
272
|
+
}
|
|
273
|
+
const names = Object.keys(edges).filter((edge) => edges[edge]);
|
|
274
|
+
return {
|
|
275
|
+
overflows: names.length > 0,
|
|
276
|
+
edges,
|
|
277
|
+
message: names.length > 0 ? `Content overflows canvas at: ${names.join(", ")}. Increase the illustration dimensions.` : ""
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// src/render.ts
|
|
282
|
+
import { Resvg } from "@resvg/resvg-js";
|
|
283
|
+
import { homedir } from "os";
|
|
284
|
+
import { readFile, writeFile, mkdir } from "fs/promises";
|
|
285
|
+
import { dirname, join } from "path";
|
|
286
|
+
import { existsSync } from "fs";
|
|
287
|
+
import { fileURLToPath } from "url";
|
|
288
|
+
import { brotliDecompressSync } from "zlib";
|
|
289
|
+
|
|
290
|
+
// src/renderContext.ts
|
|
291
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
292
|
+
var RENDER_CONTEXT_KEY = /* @__PURE__ */ Symbol.for("vizmatic.renderContext");
|
|
293
|
+
var globalRenderContext = globalThis;
|
|
294
|
+
var renderContext = globalRenderContext[RENDER_CONTEXT_KEY] ??= new AsyncLocalStorage();
|
|
295
|
+
setDensityResolver(() => renderContext.getStore()?.density);
|
|
296
|
+
function getRenderBackground() {
|
|
297
|
+
return renderContext.getStore()?.background ?? "transparent";
|
|
298
|
+
}
|
|
299
|
+
async function withRenderContext(context, render) {
|
|
300
|
+
const parent = renderContext.getStore();
|
|
301
|
+
return renderContext.run({ ...parent, ...context }, render);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// src/satori.ts
|
|
305
|
+
import * as satoriModule from "satori";
|
|
306
|
+
|
|
307
|
+
// src/canvas-chart-fit.ts
|
|
308
|
+
import React2, { isValidElement } from "react";
|
|
309
|
+
var CANVAS_CHART_COMPONENT = /* @__PURE__ */ Symbol.for("vizmatic.canvasChartComponent");
|
|
310
|
+
var CHART_FRAME_COMPONENT = /* @__PURE__ */ Symbol.for("vizmatic.chartFrameComponent");
|
|
311
|
+
var FIT_PROP = "__vizmaticCanvasChartFit";
|
|
312
|
+
var SCOPE_PROP = "data-vizmatic-chart-fit-scope";
|
|
313
|
+
var VIEWPORT_PROP = "data-vizmatic-chart-fit-viewport";
|
|
314
|
+
function isCanvasChart(component) {
|
|
315
|
+
return typeof component === "function" && Boolean(component[CANVAS_CHART_COMPONENT]);
|
|
316
|
+
}
|
|
317
|
+
function CanvasChartFit({ children, width, height }) {
|
|
318
|
+
let chart = children;
|
|
319
|
+
for (let depth = 0; isValidElement(chart) && typeof chart.type === "function" && depth < 16; depth += 1) {
|
|
320
|
+
if (chart.type[CHART_FRAME_COMPONENT]) {
|
|
321
|
+
const props = chart.props;
|
|
322
|
+
const padding = typeof props.padding === "number" ? props.padding : 16;
|
|
323
|
+
chart = React2.cloneElement(chart, {
|
|
324
|
+
...props,
|
|
325
|
+
width: typeof props.width === "number" ? props.width + 2 * (padding + 1) : props.width,
|
|
326
|
+
height: void 0
|
|
327
|
+
});
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
chart = chart.type(chart.props);
|
|
331
|
+
}
|
|
332
|
+
return React2.createElement("div", {
|
|
333
|
+
[FIT_PROP]: { width, height },
|
|
334
|
+
style: { position: "relative", display: "flex", width, height, flexShrink: 0 }
|
|
335
|
+
}, React2.createElement("div", {
|
|
336
|
+
style: { position: "absolute", left: 0, top: 0, display: "flex", alignItems: "flex-start" }
|
|
337
|
+
}, chart));
|
|
338
|
+
}
|
|
339
|
+
function prepareChartFits(element) {
|
|
340
|
+
const sizes = /* @__PURE__ */ new Map();
|
|
341
|
+
const hasText = (node) => {
|
|
342
|
+
if (typeof node === "string" || typeof node === "number" || typeof node === "bigint") return true;
|
|
343
|
+
if (Array.isArray(node)) return node.some(hasText);
|
|
344
|
+
return isValidElement(node) && node.type === React2.Fragment && hasText(node.props.children);
|
|
345
|
+
};
|
|
346
|
+
const visit = (node, scope) => {
|
|
347
|
+
if (Array.isArray(node)) return node.map((child) => visit(child, scope));
|
|
348
|
+
if (!isValidElement(node)) return node;
|
|
349
|
+
const forwarded = node.type;
|
|
350
|
+
const render = typeof node.type === "function" ? node.type : forwarded?.$$typeof === /* @__PURE__ */ Symbol.for("react.forward_ref") ? forwarded.render : void 0;
|
|
351
|
+
if (render) {
|
|
352
|
+
const resolved = visit(render(node.props), scope);
|
|
353
|
+
return node.key != null && isValidElement(resolved) ? React2.cloneElement(resolved, { key: node.key }) : resolved;
|
|
354
|
+
}
|
|
355
|
+
const size = node.props[FIT_PROP];
|
|
356
|
+
const props = { ...node.props };
|
|
357
|
+
delete props[FIT_PROP];
|
|
358
|
+
let childScope = scope;
|
|
359
|
+
if (size) {
|
|
360
|
+
const id = String(sizes.size);
|
|
361
|
+
sizes.set(id, size);
|
|
362
|
+
props[VIEWPORT_PROP] = id;
|
|
363
|
+
childScope = id;
|
|
364
|
+
} else if (scope !== void 0 && typeof node.type === "string") {
|
|
365
|
+
props[SCOPE_PROP] = scope;
|
|
366
|
+
}
|
|
367
|
+
const children = node.props.children;
|
|
368
|
+
const preparedChildren = children === void 0 ? void 0 : visit(children, childScope);
|
|
369
|
+
if (typeof node.type === "string") {
|
|
370
|
+
const style = node.props.style;
|
|
371
|
+
props.style = {
|
|
372
|
+
minWidth: 0,
|
|
373
|
+
minHeight: 0,
|
|
374
|
+
...style?.flex === void 0 ? { flexShrink: hasText(preparedChildren) ? 1 : 0 } : {},
|
|
375
|
+
...style
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
return children === void 0 ? React2.cloneElement(node, props) : React2.cloneElement(node, props, preparedChildren);
|
|
379
|
+
};
|
|
380
|
+
return { element: visit(element), sizes };
|
|
381
|
+
}
|
|
382
|
+
function fittedTree(element, geometry) {
|
|
383
|
+
if (Array.isArray(element)) return element.map((child) => fittedTree(child, geometry));
|
|
384
|
+
if (!isValidElement(element)) return element;
|
|
385
|
+
const id = element.props[VIEWPORT_PROP];
|
|
386
|
+
const children = element.props.children;
|
|
387
|
+
if (typeof id === "string") {
|
|
388
|
+
const fit = geometry.get(id);
|
|
389
|
+
const content = React2.Children.only(children);
|
|
390
|
+
return React2.cloneElement(element, {}, React2.cloneElement(content, { style: {
|
|
391
|
+
...content.props.style,
|
|
392
|
+
left: fit.left,
|
|
393
|
+
top: fit.top,
|
|
394
|
+
transform: `scale(${fit.scale})`,
|
|
395
|
+
transformOrigin: "top left"
|
|
396
|
+
} }));
|
|
397
|
+
}
|
|
398
|
+
return children === void 0 ? element : React2.cloneElement(element, {}, fittedTree(children, geometry));
|
|
399
|
+
}
|
|
400
|
+
async function renderWithCanvasChartFit(render, element, options) {
|
|
401
|
+
const prepared = prepareChartFits(element);
|
|
402
|
+
if (!prepared.sizes.size) return render(prepared.element, options);
|
|
403
|
+
const viewports = /* @__PURE__ */ new Map();
|
|
404
|
+
const bounds = /* @__PURE__ */ new Map();
|
|
405
|
+
await render(prepared.element, { ...options, onNodeDetected(node) {
|
|
406
|
+
const viewport = node.props[VIEWPORT_PROP];
|
|
407
|
+
if (typeof viewport === "string") viewports.set(viewport, node);
|
|
408
|
+
const id = node.props[SCOPE_PROP];
|
|
409
|
+
if (typeof id !== "string" || node.width < 0 || node.height < 0) return;
|
|
410
|
+
const prior = bounds.get(id);
|
|
411
|
+
bounds.set(id, {
|
|
412
|
+
left: Math.min(prior?.left ?? Infinity, node.left),
|
|
413
|
+
top: Math.min(prior?.top ?? Infinity, node.top),
|
|
414
|
+
right: Math.max(prior?.right ?? -Infinity, node.left + node.width),
|
|
415
|
+
bottom: Math.max(prior?.bottom ?? -Infinity, node.top + node.height)
|
|
416
|
+
});
|
|
417
|
+
} });
|
|
418
|
+
const geometry = /* @__PURE__ */ new Map();
|
|
419
|
+
for (const [id, size] of prepared.sizes) {
|
|
420
|
+
const viewport = viewports.get(id);
|
|
421
|
+
const measured = bounds.get(id);
|
|
422
|
+
if (!viewport || !measured) throw new Error("Could not measure the chart inside CanvasItem.");
|
|
423
|
+
const width = measured.right - measured.left, height = measured.bottom - measured.top;
|
|
424
|
+
const scale = Math.min(size.width / width, size.height / height);
|
|
425
|
+
if (!Number.isFinite(scale) || scale <= 0) throw new Error("CanvasItem charts require positive, finite dimensions.");
|
|
426
|
+
geometry.set(id, {
|
|
427
|
+
...size,
|
|
428
|
+
scale,
|
|
429
|
+
left: (size.width - width * scale) / 2 - (measured.left - viewport.left) * scale,
|
|
430
|
+
top: (size.height - height * scale) / 2 - (measured.top - viewport.top) * scale
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
const finalViewports = /* @__PURE__ */ new Map();
|
|
434
|
+
return render(fittedTree(prepared.element, geometry), { ...options, onNodeDetected(node) {
|
|
435
|
+
const viewport = node.props[VIEWPORT_PROP];
|
|
436
|
+
if (typeof viewport === "string") finalViewports.set(viewport, node);
|
|
437
|
+
const id = node.props[SCOPE_PROP];
|
|
438
|
+
const fit = typeof id === "string" ? geometry.get(id) : void 0;
|
|
439
|
+
const origin = typeof id === "string" ? finalViewports.get(id) : void 0;
|
|
440
|
+
if (fit && origin) {
|
|
441
|
+
const left = origin.left + fit.left, top = origin.top + fit.top;
|
|
442
|
+
const style = node.props.style ?? {};
|
|
443
|
+
options.onNodeDetected?.({
|
|
444
|
+
...node,
|
|
445
|
+
left: left + (node.left - left) * fit.scale,
|
|
446
|
+
top: top + (node.top - top) * fit.scale,
|
|
447
|
+
width: node.width * fit.scale,
|
|
448
|
+
height: node.height * fit.scale,
|
|
449
|
+
props: { ...node.props, style: { ...style, ...typeof style.fontSize === "number" ? { fontSize: style.fontSize * fit.scale } : {} } }
|
|
450
|
+
});
|
|
451
|
+
} else options.onNodeDetected?.(node);
|
|
452
|
+
} });
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// src/satori.ts
|
|
456
|
+
function resolveSatori(api) {
|
|
457
|
+
if (typeof api === "function") return api;
|
|
458
|
+
if (api && typeof api === "object") {
|
|
459
|
+
const fallback = api.default;
|
|
460
|
+
if (typeof fallback === "function") return fallback;
|
|
461
|
+
if (fallback && typeof fallback === "object") {
|
|
462
|
+
const nestedFallback = fallback.default;
|
|
463
|
+
if (typeof nestedFallback === "function") return nestedFallback;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
throw new Error("satori export could not be resolved");
|
|
467
|
+
}
|
|
468
|
+
var renderSatori = resolveSatori(satoriModule);
|
|
469
|
+
var satori = (element, options) => renderWithCanvasChartFit(renderSatori, element, options);
|
|
470
|
+
|
|
471
|
+
// src/svg-accessibility.ts
|
|
472
|
+
import { cloneElement, isValidElement as isValidElement2 } from "react";
|
|
473
|
+
var SVG_ACCESSIBILITY_COMPONENT = /* @__PURE__ */ Symbol.for("vizmatic.svgAccessibilityComponent");
|
|
474
|
+
function componentHasSvgMetadata(type) {
|
|
475
|
+
return typeof type === "function" && type[SVG_ACCESSIBILITY_COMPONENT] === true;
|
|
476
|
+
}
|
|
477
|
+
function svgToken(value) {
|
|
478
|
+
if (value.length === 0) return "empty";
|
|
479
|
+
return Array.from(value, (character) => {
|
|
480
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
481
|
+
return /^[A-Za-z0-9.-]$/.test(character) ? character : `_u${codePoint.toString(16)}_`;
|
|
482
|
+
}).join("");
|
|
483
|
+
}
|
|
484
|
+
function textProperty(props, name) {
|
|
485
|
+
const value = props[name];
|
|
486
|
+
return typeof value === "string" ? value : "";
|
|
487
|
+
}
|
|
488
|
+
function encodedTextProperty(props, name) {
|
|
489
|
+
const value = textProperty(props, name);
|
|
490
|
+
try {
|
|
491
|
+
return decodeURIComponent(value);
|
|
492
|
+
} catch {
|
|
493
|
+
return value;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
function prepareAccessibleSvg(element) {
|
|
497
|
+
const graphs = [];
|
|
498
|
+
const explicitIds = /* @__PURE__ */ new Set();
|
|
499
|
+
const visit = (node, graph) => {
|
|
500
|
+
if (Array.isArray(node)) return node.map((child) => visit(child, graph));
|
|
501
|
+
if (!isValidElement2(node)) return node;
|
|
502
|
+
const component = node.type;
|
|
503
|
+
if (componentHasSvgMetadata(component)) {
|
|
504
|
+
return visit(component(node.props), graph);
|
|
505
|
+
}
|
|
506
|
+
const props = node.props;
|
|
507
|
+
const title = textProperty(props, "data-vizmatic-svg-title");
|
|
508
|
+
const isGraphRoot = title.length > 0;
|
|
509
|
+
let activeGraph = graph;
|
|
510
|
+
let elementKey = node.key;
|
|
511
|
+
if (isGraphRoot) {
|
|
512
|
+
const explicitId = textProperty(props, "data-vizmatic-svg-id");
|
|
513
|
+
if (explicitId) {
|
|
514
|
+
if (explicitIds.has(explicitId)) {
|
|
515
|
+
throw new Error(`GraphDiagram SVG id "${explicitId}" must be unique in one exported SVG.`);
|
|
516
|
+
}
|
|
517
|
+
explicitIds.add(explicitId);
|
|
518
|
+
}
|
|
519
|
+
const index = graphs.length;
|
|
520
|
+
const token = explicitId ? `id-${svgToken(explicitId)}` : `index-${index}`;
|
|
521
|
+
activeGraph = {
|
|
522
|
+
index,
|
|
523
|
+
token,
|
|
524
|
+
detectionKey: `vizmatic-graph-${index}`,
|
|
525
|
+
title,
|
|
526
|
+
description: textProperty(props, "data-vizmatic-svg-description"),
|
|
527
|
+
nodes: [],
|
|
528
|
+
edges: []
|
|
529
|
+
};
|
|
530
|
+
graphs.push(activeGraph);
|
|
531
|
+
elementKey = activeGraph.detectionKey;
|
|
532
|
+
}
|
|
533
|
+
let nextProps;
|
|
534
|
+
if (activeGraph && Object.hasOwn(props, "data-vizmatic-graph-node-id")) {
|
|
535
|
+
const id = textProperty(props, "data-vizmatic-graph-node-id");
|
|
536
|
+
const label = textProperty(props, "data-vizmatic-graph-node-label") || id;
|
|
537
|
+
const detail = textProperty(props, "data-vizmatic-graph-node-detail");
|
|
538
|
+
activeGraph.nodes.push({
|
|
539
|
+
id,
|
|
540
|
+
label,
|
|
541
|
+
detail,
|
|
542
|
+
title: textProperty(props, "data-vizmatic-graph-node-title") || label,
|
|
543
|
+
href: textProperty(props, "data-vizmatic-graph-node-href") || void 0
|
|
544
|
+
});
|
|
545
|
+
elementKey = `${activeGraph.detectionKey}-node-${svgToken(id)}`;
|
|
546
|
+
} else if (activeGraph && textProperty(props, "data-vizmatic-graph-edge-id")) {
|
|
547
|
+
const id = encodedTextProperty(props, "data-vizmatic-graph-edge-id");
|
|
548
|
+
activeGraph.edges.push({
|
|
549
|
+
id,
|
|
550
|
+
from: encodedTextProperty(props, "data-vizmatic-graph-edge-from"),
|
|
551
|
+
to: encodedTextProperty(props, "data-vizmatic-graph-edge-to"),
|
|
552
|
+
label: encodedTextProperty(props, "data-vizmatic-graph-edge-label"),
|
|
553
|
+
title: encodedTextProperty(props, "data-vizmatic-graph-edge-title"),
|
|
554
|
+
path: textProperty(props, "d"),
|
|
555
|
+
href: encodedTextProperty(props, "data-vizmatic-graph-edge-href") || void 0
|
|
556
|
+
});
|
|
557
|
+
elementKey = `${activeGraph.detectionKey}-edge-${svgToken(id)}`;
|
|
558
|
+
}
|
|
559
|
+
const children = props.children;
|
|
560
|
+
if (children === void 0) {
|
|
561
|
+
if (isGraphRoot) {
|
|
562
|
+
nextProps = { ...props, "data-vizmatic-svg-instance": activeGraph?.token };
|
|
563
|
+
return cloneElement(node, { ...nextProps, key: elementKey });
|
|
564
|
+
}
|
|
565
|
+
return node;
|
|
566
|
+
}
|
|
567
|
+
const preparedChildren = visit(children, activeGraph);
|
|
568
|
+
nextProps = isGraphRoot ? { ...props, "data-vizmatic-svg-instance": activeGraph?.token } : { ...props };
|
|
569
|
+
return cloneElement(
|
|
570
|
+
node,
|
|
571
|
+
{ ...nextProps, key: elementKey },
|
|
572
|
+
preparedChildren
|
|
573
|
+
);
|
|
574
|
+
};
|
|
575
|
+
return { element: visit(element), graphs };
|
|
576
|
+
}
|
|
577
|
+
function escapeXml(value) {
|
|
578
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
579
|
+
}
|
|
580
|
+
function attribute(name, value) {
|
|
581
|
+
return `${name}="${escapeXml(value)}"`;
|
|
582
|
+
}
|
|
583
|
+
function graphBaseId(graph) {
|
|
584
|
+
return `vizmatic-graph-${graph.token}`;
|
|
585
|
+
}
|
|
586
|
+
function nodeId(graph, node) {
|
|
587
|
+
return `${graphBaseId(graph)}-node-${svgToken(node.id)}`;
|
|
588
|
+
}
|
|
589
|
+
function edgeId(graph, edge) {
|
|
590
|
+
return `${graphBaseId(graph)}-edge-${svgToken(edge.id)}`;
|
|
591
|
+
}
|
|
592
|
+
function labelForEdge(graph, edge) {
|
|
593
|
+
const nodes = new Map(graph.nodes.map((node) => [node.id, node.label]));
|
|
594
|
+
const from = nodes.get(edge.from) ?? edge.from;
|
|
595
|
+
const to = nodes.get(edge.to) ?? edge.to;
|
|
596
|
+
return edge.title || (edge.label ? `${from} to ${to}: ${edge.label}` : `${from} to ${to}`);
|
|
597
|
+
}
|
|
598
|
+
function descriptionForEdge(graph, edge) {
|
|
599
|
+
const nodes = new Map(graph.nodes.map((node) => [node.id, node.label]));
|
|
600
|
+
const from = nodes.get(edge.from) ?? edge.from;
|
|
601
|
+
const to = nodes.get(edge.to) ?? edge.to;
|
|
602
|
+
return `Relationship from ${from} to ${to}${edge.label ? `, labeled ${edge.label}` : ""}.`;
|
|
603
|
+
}
|
|
604
|
+
function nodeGroup(graph, node, layout) {
|
|
605
|
+
const id = nodeId(graph, node);
|
|
606
|
+
const label = node.detail ? `${node.label}: ${node.detail}` : node.label;
|
|
607
|
+
const description = node.detail || node.label;
|
|
608
|
+
const titleId = `${id}-title`;
|
|
609
|
+
const descriptionId = `${id}-description`;
|
|
610
|
+
const accessible = `<g ${attribute("id", id)} role="group" aria-labelledby="${titleId}" aria-describedby="${descriptionId}"><title id="${titleId}">${escapeXml(node.title || node.label)}</title><desc id="${descriptionId}">${escapeXml(description)}</desc>`;
|
|
611
|
+
if (!layout) return `${accessible}</g>`;
|
|
612
|
+
const rect = `<rect x="${layout.left}" y="${layout.top}" width="${layout.width}" height="${layout.height}" fill="transparent" pointer-events="all"/>`;
|
|
613
|
+
const hitTarget = node.href ? `<a ${attribute("href", node.href)} role="link" tabindex="0" ${attribute("aria-label", label)}>${rect}</a>` : rect;
|
|
614
|
+
return `${accessible}${hitTarget}</g>`;
|
|
615
|
+
}
|
|
616
|
+
function edgeGroup(graph, edge, root) {
|
|
617
|
+
const id = edgeId(graph, edge);
|
|
618
|
+
const title = labelForEdge(graph, edge);
|
|
619
|
+
const titleId = `${id}-title`;
|
|
620
|
+
const descriptionId = `${id}-description`;
|
|
621
|
+
const accessible = `<g ${attribute("id", id)} role="group" aria-labelledby="${titleId}" aria-describedby="${descriptionId}"><title id="${titleId}">${escapeXml(title)}</title><desc id="${descriptionId}">${escapeXml(descriptionForEdge(graph, edge))}</desc>`;
|
|
622
|
+
if (!root || !edge.path) return `${accessible}</g>`;
|
|
623
|
+
const path = `<path d="${escapeXml(edge.path)}" transform="translate(${root.left} ${root.top})" fill="none" stroke="transparent" stroke-width="14" stroke-linecap="round" pointer-events="stroke"/>`;
|
|
624
|
+
const hitTarget = edge.href ? `<a ${attribute("href", edge.href)} role="link" tabindex="0" ${attribute("aria-label", title)}>${path}</a>` : path;
|
|
625
|
+
return `${accessible}${hitTarget}</g>`;
|
|
626
|
+
}
|
|
627
|
+
function addSvgAccessibility(svg, graphs, layoutNodes) {
|
|
628
|
+
if (graphs.length === 0) return svg;
|
|
629
|
+
const title = graphs.map((graph) => graph.title).join(". ");
|
|
630
|
+
const description = graphs.map((graph) => `${graph.title}. ${graph.description}`).join(" ");
|
|
631
|
+
const hasLinks = graphs.some(
|
|
632
|
+
(graph) => graph.nodes.some((node) => node.href) || graph.edges.some((edge) => edge.href)
|
|
633
|
+
);
|
|
634
|
+
const rootTitleId = "vizmatic-svg-title";
|
|
635
|
+
const rootDescriptionId = "vizmatic-svg-description";
|
|
636
|
+
const groupMarkup = graphs.map((graph) => {
|
|
637
|
+
const baseId = graphBaseId(graph);
|
|
638
|
+
const titleId = `${baseId}-title`;
|
|
639
|
+
const descriptionId = `${baseId}-description`;
|
|
640
|
+
const root = layoutNodes.find((node) => node.key === graph.detectionKey);
|
|
641
|
+
const nodes = graph.nodes.map((item) => {
|
|
642
|
+
const layout = layoutNodes.find((node) => node.key === `${graph.detectionKey}-node-${svgToken(item.id)}`);
|
|
643
|
+
if (item.href && !layout) {
|
|
644
|
+
throw new Error(`Could not resolve GraphDiagram node "${item.id}" bounds for SVG link export.`);
|
|
645
|
+
}
|
|
646
|
+
return nodeGroup(graph, item, layout);
|
|
647
|
+
}).join("");
|
|
648
|
+
const edges = graph.edges.map((item) => {
|
|
649
|
+
if (item.href && !root) {
|
|
650
|
+
throw new Error(`Could not resolve GraphDiagram bounds for SVG edge link export: "${item.id}".`);
|
|
651
|
+
}
|
|
652
|
+
return edgeGroup(graph, item, root);
|
|
653
|
+
}).join("");
|
|
654
|
+
return `<g ${attribute("id", baseId)} role="group" aria-labelledby="${titleId}" aria-describedby="${descriptionId}"><title id="${titleId}">${escapeXml(graph.title)}</title><desc id="${descriptionId}">${escapeXml(graph.description)}</desc>${nodes}${edges}</g>`;
|
|
655
|
+
}).join("");
|
|
656
|
+
const metadata = `<title id="${rootTitleId}">${escapeXml(title)}</title><desc id="${rootDescriptionId}">${escapeXml(description)}</desc>`;
|
|
657
|
+
return svg.replace(
|
|
658
|
+
/^<svg\b([^>]*)>/,
|
|
659
|
+
(_match, attrs) => `<svg${attrs} role="${hasLinks ? "group" : "img"}" aria-labelledby="${rootTitleId}" aria-describedby="${rootDescriptionId}">${metadata}`
|
|
660
|
+
).replace(/<\/svg>\s*$/, `${groupMarkup}</svg>`);
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
// src/render.ts
|
|
664
|
+
var FONT_CACHE_DIR = process.env.VIZMATIC_FONT_DIR ?? join(process.env.XDG_CACHE_HOME ?? join(homedir(), ".cache"), "vizmatic", "fonts");
|
|
665
|
+
var NETWORK_FALLBACK_DISABLED = process.env.VIZMATIC_DISABLE_NETWORK === "1" || process.env.VIZMATIC_OFFLINE === "1";
|
|
666
|
+
function moduleDirectory() {
|
|
667
|
+
if (typeof __dirname !== "undefined") return __dirname;
|
|
668
|
+
return dirname(fileURLToPath(import.meta.url));
|
|
669
|
+
}
|
|
670
|
+
function unique(values) {
|
|
671
|
+
return [...new Set(values.filter((value) => Boolean(value)))];
|
|
672
|
+
}
|
|
673
|
+
function resolveAssetDir() {
|
|
674
|
+
const moduleDir = moduleDirectory();
|
|
675
|
+
const candidates = unique([
|
|
676
|
+
process.env.VIZMATIC_ASSET_DIR,
|
|
677
|
+
join(moduleDir, "..", "assets"),
|
|
678
|
+
join(moduleDir, "assets"),
|
|
679
|
+
join(process.cwd(), "assets")
|
|
680
|
+
]);
|
|
681
|
+
return candidates.find((candidate) => existsSync(candidate));
|
|
682
|
+
}
|
|
683
|
+
var ASSET_DIR = resolveAssetDir();
|
|
684
|
+
var VENDORED_FONT_DIR = ASSET_DIR ? join(ASSET_DIR, "fonts") : void 0;
|
|
685
|
+
var VENDORED_EMOJI_DIR = ASSET_DIR ? join(ASSET_DIR, "emoji") : void 0;
|
|
686
|
+
var VENDORED_EMOJI_BUNDLE = ASSET_DIR ? join(ASSET_DIR, "emoji.json.br") : void 0;
|
|
687
|
+
var FONT_URLS = {
|
|
688
|
+
interRegular: "https://fonts.gstatic.com/s/inter/v20/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuLyfMZg.ttf",
|
|
689
|
+
interSemiBold: "https://fonts.gstatic.com/s/inter/v20/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuGKYMZg.ttf",
|
|
690
|
+
interBold: "https://fonts.gstatic.com/s/inter/v20/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuFuYMZg.ttf",
|
|
691
|
+
jetbrainsMono: "https://fonts.gstatic.com/s/jetbrainsmono/v24/tDbY2o-flEEny0FZhsfKu5WU4zr3E_BX0PnT8RD8yKxjPQ.ttf",
|
|
692
|
+
// Noto Sans (544KB from gstatic): fallback for Phonetic Extensions
|
|
693
|
+
// (ᵀ U+1D40, ᵢ U+1D62) and extended Latin/Greek not covered by Inter.
|
|
694
|
+
notoSans: "https://fonts.gstatic.com/s/notosans/v42/o-0mIpQlx3QUlC5A4PNB6Ryti20_6n1iPHjcz6L1SoM-jCpoiyD9A99d.ttf",
|
|
695
|
+
// Noto Sans Math (766KB from gstatic): math operators (√, ∞, ≠, ×, ≫),
|
|
696
|
+
// Greek letters (τ, Σ), and arrows (→, ←)
|
|
697
|
+
notoSansMath: "https://fonts.gstatic.com/s/notosansmath/v18/7Aump_cpkSecTWaHRlH2hyV5UHkG.ttf",
|
|
698
|
+
instrumentSerifRegular: "https://raw.githubusercontent.com/google/fonts/main/ofl/instrumentserif/InstrumentSerif-Regular.ttf",
|
|
699
|
+
instrumentSerifItalic: "https://raw.githubusercontent.com/google/fonts/main/ofl/instrumentserif/InstrumentSerif-Italic.ttf"
|
|
700
|
+
};
|
|
701
|
+
function fontReadPaths(filename) {
|
|
702
|
+
return unique([
|
|
703
|
+
process.env.VIZMATIC_FONT_DIR ? join(process.env.VIZMATIC_FONT_DIR, filename) : void 0,
|
|
704
|
+
VENDORED_FONT_DIR ? join(VENDORED_FONT_DIR, filename) : void 0,
|
|
705
|
+
join(FONT_CACHE_DIR, filename)
|
|
706
|
+
]);
|
|
707
|
+
}
|
|
708
|
+
async function ensureFont(filename, url) {
|
|
709
|
+
for (const localPath of fontReadPaths(filename)) {
|
|
710
|
+
if (existsSync(localPath)) return readFile(localPath);
|
|
711
|
+
}
|
|
712
|
+
if (NETWORK_FALLBACK_DISABLED) {
|
|
713
|
+
throw new Error(`Font ${filename} was not found in local Vizmatic assets and network fallback is disabled`);
|
|
714
|
+
}
|
|
715
|
+
console.log(` \u2193 Downloading font: ${filename}`);
|
|
716
|
+
const response = await fetch(url);
|
|
717
|
+
if (!response.ok) {
|
|
718
|
+
throw new Error(`Failed to download font ${filename}: ${response.statusText}`);
|
|
719
|
+
}
|
|
720
|
+
const buffer = Buffer.from(await response.arrayBuffer());
|
|
721
|
+
await mkdir(FONT_CACHE_DIR, { recursive: true });
|
|
722
|
+
await writeFile(join(FONT_CACHE_DIR, filename), buffer);
|
|
723
|
+
return buffer;
|
|
724
|
+
}
|
|
725
|
+
function toArrayBuffer(buffer) {
|
|
726
|
+
return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
|
727
|
+
}
|
|
728
|
+
async function loadFonts() {
|
|
729
|
+
await mkdir(FONT_CACHE_DIR, { recursive: true });
|
|
730
|
+
const fonts = [];
|
|
731
|
+
try {
|
|
732
|
+
const [regular, semiBold, bold] = await Promise.all([
|
|
733
|
+
ensureFont("Inter-Regular.ttf", FONT_URLS.interRegular),
|
|
734
|
+
ensureFont("Inter-SemiBold.ttf", FONT_URLS.interSemiBold),
|
|
735
|
+
ensureFont("Inter-Bold.ttf", FONT_URLS.interBold)
|
|
736
|
+
]);
|
|
737
|
+
fonts.push(
|
|
738
|
+
{ name: "Inter", data: toArrayBuffer(regular), weight: 400, style: "normal" },
|
|
739
|
+
{ name: "Inter", data: toArrayBuffer(semiBold), weight: 600, style: "normal" },
|
|
740
|
+
{ name: "Inter", data: toArrayBuffer(bold), weight: 700, style: "normal" }
|
|
741
|
+
);
|
|
742
|
+
} catch (e) {
|
|
743
|
+
console.warn("Warning: Could not load Inter font:", e);
|
|
744
|
+
}
|
|
745
|
+
try {
|
|
746
|
+
const jetbrains = await ensureFont("JetBrainsMono-Regular.ttf", FONT_URLS.jetbrainsMono);
|
|
747
|
+
fonts.push({ name: "JetBrains Mono", data: toArrayBuffer(jetbrains), weight: 400, style: "normal" });
|
|
748
|
+
} catch (e) {
|
|
749
|
+
console.warn("Warning: Could not load JetBrains Mono font:", e);
|
|
750
|
+
}
|
|
751
|
+
try {
|
|
752
|
+
const [serif, serifItalic] = await Promise.all([
|
|
753
|
+
ensureFont("InstrumentSerif-Regular.ttf", FONT_URLS.instrumentSerifRegular),
|
|
754
|
+
ensureFont("InstrumentSerif-Italic.ttf", FONT_URLS.instrumentSerifItalic)
|
|
755
|
+
]);
|
|
756
|
+
fonts.push(
|
|
757
|
+
{ name: "Instrument Serif", data: toArrayBuffer(serif), weight: 400, style: "normal" },
|
|
758
|
+
{ name: "Instrument Serif", data: toArrayBuffer(serifItalic), weight: 400, style: "italic" }
|
|
759
|
+
);
|
|
760
|
+
} catch (e) {
|
|
761
|
+
console.warn("Warning: Could not load Instrument Serif font:", e);
|
|
762
|
+
}
|
|
763
|
+
try {
|
|
764
|
+
const notoSans = await ensureFont("NotoSans-Regular.ttf", FONT_URLS.notoSans);
|
|
765
|
+
fonts.push(
|
|
766
|
+
{ name: "Noto Sans", data: toArrayBuffer(notoSans), weight: 400, style: "normal" }
|
|
767
|
+
);
|
|
768
|
+
} catch (e) {
|
|
769
|
+
console.warn("Warning: Could not load Noto Sans font:", e);
|
|
770
|
+
}
|
|
771
|
+
try {
|
|
772
|
+
const notoMath = await ensureFont("NotoSansMath-Regular.ttf", FONT_URLS.notoSansMath);
|
|
773
|
+
fonts.push(
|
|
774
|
+
{ name: "Noto Sans Math", data: toArrayBuffer(notoMath), weight: 400, style: "normal" }
|
|
775
|
+
);
|
|
776
|
+
} catch (e) {
|
|
777
|
+
console.warn("Warning: Could not load Noto Sans Math font:", e);
|
|
778
|
+
}
|
|
779
|
+
return fonts;
|
|
780
|
+
}
|
|
781
|
+
var EMOJI_CACHE_DIR = join(FONT_CACHE_DIR, "emoji");
|
|
782
|
+
function emojiCodepoints(segment) {
|
|
783
|
+
return [...segment].map((c) => c.codePointAt(0).toString(16));
|
|
784
|
+
}
|
|
785
|
+
function emojiFilenames(segment) {
|
|
786
|
+
const codepoints = emojiCodepoints(segment);
|
|
787
|
+
const full = codepoints.join("-");
|
|
788
|
+
const withoutVariationSelectors = codepoints.filter((cp) => cp !== "fe0f").join("-");
|
|
789
|
+
return unique([full, withoutVariationSelectors]).map((codepoints2) => `${codepoints2}.svg`);
|
|
790
|
+
}
|
|
791
|
+
async function svgFileToDataUri(path) {
|
|
792
|
+
const svgData = await readFile(path, "utf8");
|
|
793
|
+
return `data:image/svg+xml;base64,${Buffer.from(svgData).toString("base64")}`;
|
|
794
|
+
}
|
|
795
|
+
var bundledEmoji;
|
|
796
|
+
async function loadBundledEmoji(filename) {
|
|
797
|
+
if (!VENDORED_EMOJI_BUNDLE || !existsSync(VENDORED_EMOJI_BUNDLE)) return void 0;
|
|
798
|
+
bundledEmoji ??= JSON.parse(
|
|
799
|
+
brotliDecompressSync(await readFile(VENDORED_EMOJI_BUNDLE)).toString("utf8")
|
|
800
|
+
);
|
|
801
|
+
const svg = bundledEmoji[filename];
|
|
802
|
+
return svg ? `data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}` : void 0;
|
|
803
|
+
}
|
|
804
|
+
async function loadEmoji(segment) {
|
|
805
|
+
const filenames = emojiFilenames(segment);
|
|
806
|
+
for (const filename2 of filenames) {
|
|
807
|
+
const paths = unique([
|
|
808
|
+
VENDORED_EMOJI_DIR ? join(VENDORED_EMOJI_DIR, filename2) : void 0,
|
|
809
|
+
join(EMOJI_CACHE_DIR, filename2)
|
|
810
|
+
]);
|
|
811
|
+
for (const localPath of paths) {
|
|
812
|
+
if (existsSync(localPath)) return svgFileToDataUri(localPath);
|
|
813
|
+
}
|
|
814
|
+
const bundled = await loadBundledEmoji(filename2);
|
|
815
|
+
if (bundled) return bundled;
|
|
816
|
+
}
|
|
817
|
+
if (NETWORK_FALLBACK_DISABLED) {
|
|
818
|
+
console.warn(`Warning: Emoji asset not found locally: ${filenames.join(" or ")}`);
|
|
819
|
+
return "";
|
|
820
|
+
}
|
|
821
|
+
const filename = filenames[0];
|
|
822
|
+
if (!filename) return "";
|
|
823
|
+
const url = `https://cdn.jsdelivr.net/npm/@twemoji/svg@15.0.0/${filename}`;
|
|
824
|
+
try {
|
|
825
|
+
console.log(` \u2193 Downloading emoji: ${filename}`);
|
|
826
|
+
const response = await fetch(url);
|
|
827
|
+
if (!response.ok) {
|
|
828
|
+
console.warn(`Warning: Could not download emoji ${filename}: ${response.statusText}`);
|
|
829
|
+
return "";
|
|
830
|
+
}
|
|
831
|
+
const svgData = await response.text();
|
|
832
|
+
await mkdir(EMOJI_CACHE_DIR, { recursive: true });
|
|
833
|
+
await writeFile(join(EMOJI_CACHE_DIR, filename), svgData);
|
|
834
|
+
return `data:image/svg+xml;base64,${Buffer.from(svgData).toString("base64")}`;
|
|
835
|
+
} catch (e) {
|
|
836
|
+
console.warn(`Warning: Could not fetch emoji ${filename}:`, e);
|
|
837
|
+
return "";
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
async function loadAdditionalAsset(code, segment) {
|
|
841
|
+
if (code === "emoji") {
|
|
842
|
+
return loadEmoji(segment);
|
|
843
|
+
}
|
|
844
|
+
return [];
|
|
845
|
+
}
|
|
846
|
+
var cachedFonts = null;
|
|
847
|
+
async function getFonts() {
|
|
848
|
+
if (!cachedFonts) {
|
|
849
|
+
cachedFonts = await loadFonts();
|
|
850
|
+
}
|
|
851
|
+
return cachedFonts;
|
|
852
|
+
}
|
|
853
|
+
function sanitizeSvg(svg) {
|
|
854
|
+
return svg.replace(/width="0"/g, 'width="1"').replace(/height="0"/g, 'height="1"');
|
|
855
|
+
}
|
|
856
|
+
var AUTOCROP_PADDING = 24;
|
|
857
|
+
var CROP_THRESHOLD_PERCENT = 10;
|
|
858
|
+
function replaceSvgAttribute(attrs, name, value) {
|
|
859
|
+
const pattern = new RegExp(`\\s${name}="[^"]*"`);
|
|
860
|
+
if (pattern.test(attrs)) return attrs.replace(pattern, ` ${name}="${value}"`);
|
|
861
|
+
return `${attrs} ${name}="${value}"`;
|
|
862
|
+
}
|
|
863
|
+
function cropSvgViewport(svg, region) {
|
|
864
|
+
const x = Math.max(0, Math.floor(region.x));
|
|
865
|
+
const y = Math.max(0, Math.floor(region.y));
|
|
866
|
+
const width = Math.max(1, Math.ceil(region.width));
|
|
867
|
+
const height = Math.max(1, Math.ceil(region.height));
|
|
868
|
+
return svg.replace(/^<svg\b([^>]*)>/, (_match, attrs) => {
|
|
869
|
+
let nextAttrs = attrs;
|
|
870
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "width", String(width));
|
|
871
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "height", String(height));
|
|
872
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "viewBox", `${x} ${y} ${width} ${height}`);
|
|
873
|
+
return `<svg${nextAttrs}>`;
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
function cropLayoutNodes(nodes, region) {
|
|
877
|
+
return nodes.flatMap((node) => {
|
|
878
|
+
const left = Math.max(node.left, region.x);
|
|
879
|
+
const top = Math.max(node.top, region.y);
|
|
880
|
+
const right = Math.min(node.left + node.width, region.x + region.width);
|
|
881
|
+
const bottom = Math.min(node.top + node.height, region.y + region.height);
|
|
882
|
+
if (right <= left || bottom <= top) return [];
|
|
883
|
+
return [{
|
|
884
|
+
...node,
|
|
885
|
+
left: left - region.x,
|
|
886
|
+
top: top - region.y,
|
|
887
|
+
width: right - left,
|
|
888
|
+
height: bottom - top
|
|
889
|
+
}];
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
var CanvasOverflowError = class extends Error {
|
|
893
|
+
width;
|
|
894
|
+
height;
|
|
895
|
+
overflow;
|
|
896
|
+
animation;
|
|
897
|
+
constructor(width, height, overflow, animation) {
|
|
898
|
+
const position = animation ? [
|
|
899
|
+
animation.sceneIndex != null ? `scene ${animation.sceneIndex + 1}` : void 0,
|
|
900
|
+
animation.frameIndex != null ? `frame ${animation.frameIndex + 1}${animation.frameCount ? `/${animation.frameCount}` : ""}` : void 0,
|
|
901
|
+
animation.time != null ? `${Math.round(animation.time)}ms` : void 0,
|
|
902
|
+
animation.label ? JSON.stringify(animation.label) : void 0
|
|
903
|
+
].filter(Boolean).join(", ") : "";
|
|
904
|
+
super(`Canvas overflow detected (${width}\xD7${height})${position ? ` at ${position}` : ""}: ${overflow.message}`);
|
|
905
|
+
this.name = "CanvasOverflowError";
|
|
906
|
+
this.width = width;
|
|
907
|
+
this.height = height;
|
|
908
|
+
this.overflow = overflow;
|
|
909
|
+
this.animation = animation;
|
|
910
|
+
}
|
|
911
|
+
};
|
|
912
|
+
function resolveWatermark(options) {
|
|
913
|
+
return options.watermark ?? options.brand;
|
|
914
|
+
}
|
|
915
|
+
function shouldCropHeight(crop) {
|
|
916
|
+
return crop !== false;
|
|
917
|
+
}
|
|
918
|
+
function shouldCropViewport(crop) {
|
|
919
|
+
return crop !== false && crop !== "height";
|
|
920
|
+
}
|
|
921
|
+
async function renderToPng(element, options, createFn, theme) {
|
|
922
|
+
await renderToPngWithOutput(element, options, createFn, theme);
|
|
923
|
+
}
|
|
924
|
+
async function renderToPngWithOutput(element, options, createFn, theme) {
|
|
925
|
+
return withRenderContext(
|
|
926
|
+
{ background: options.background ?? "transparent", ...options.density !== void 0 ? { density: options.density } : {} },
|
|
927
|
+
() => renderToPngInContext(element, options, createFn, theme)
|
|
928
|
+
);
|
|
929
|
+
}
|
|
930
|
+
async function renderToPngInContext(element, options, createFn, theme) {
|
|
931
|
+
const fonts = await getFonts();
|
|
932
|
+
const renderTheme = options.theme ?? theme ?? "dark";
|
|
933
|
+
const detectionNodes = [];
|
|
934
|
+
let svg = await satori(element, {
|
|
935
|
+
width: options.width,
|
|
936
|
+
height: options.height,
|
|
937
|
+
fonts,
|
|
938
|
+
loadAdditionalAsset,
|
|
939
|
+
onNodeDetected: (node) => detectionNodes.push(node)
|
|
940
|
+
});
|
|
941
|
+
svg = sanitizeSvg(svg);
|
|
942
|
+
const detectResvg = new Resvg(svg, {
|
|
943
|
+
fitTo: { mode: "width", value: options.width },
|
|
944
|
+
background: "rgba(0, 0, 0, 0)"
|
|
945
|
+
});
|
|
946
|
+
const detectResult = detectResvg.render();
|
|
947
|
+
const detectPixels = detectResult.pixels;
|
|
948
|
+
const pixelData = new Uint8Array(detectPixels.buffer);
|
|
949
|
+
const detectedBg = detectBackgroundColor(pixelData);
|
|
950
|
+
const bounds = detectContentBounds(
|
|
951
|
+
pixelData,
|
|
952
|
+
options.width,
|
|
953
|
+
detectResult.height,
|
|
954
|
+
detectedBg,
|
|
955
|
+
AUTOCROP_PADDING
|
|
956
|
+
);
|
|
957
|
+
const overflow = detectOverflow(
|
|
958
|
+
pixelData,
|
|
959
|
+
options.width,
|
|
960
|
+
detectResult.height,
|
|
961
|
+
detectedBg
|
|
962
|
+
);
|
|
963
|
+
if (overflow.overflows) {
|
|
964
|
+
throw new CanvasOverflowError(options.width, options.height, overflow);
|
|
965
|
+
}
|
|
966
|
+
const layoutOverflow = detectLayoutOverflow(detectionNodes, options.width, options.height);
|
|
967
|
+
if (layoutOverflow.overflows) {
|
|
968
|
+
throw new CanvasOverflowError(options.width, options.height, layoutOverflow);
|
|
969
|
+
}
|
|
970
|
+
let finalWidth = options.width;
|
|
971
|
+
let finalHeight = options.height;
|
|
972
|
+
let finalElement = element;
|
|
973
|
+
const croppedHeight = bounds.y + bounds.height;
|
|
974
|
+
const heightSaved = options.height - croppedHeight;
|
|
975
|
+
const heightSavedPercent = heightSaved / options.height * 100;
|
|
976
|
+
if (shouldCropHeight(options.crop) && heightSavedPercent >= CROP_THRESHOLD_PERCENT) {
|
|
977
|
+
finalHeight = croppedHeight;
|
|
978
|
+
if (createFn && theme) {
|
|
979
|
+
finalElement = createFn(theme);
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
const watermark = resolveWatermark(options);
|
|
983
|
+
const renderFinalSvg = async (targetElement, targetWidth, targetHeight) => {
|
|
984
|
+
const layoutNodes = [];
|
|
985
|
+
const outputElement = watermark ? wrapWithWatermark(
|
|
986
|
+
targetElement,
|
|
987
|
+
targetWidth,
|
|
988
|
+
targetHeight,
|
|
989
|
+
renderTheme,
|
|
990
|
+
watermark
|
|
991
|
+
) : targetElement;
|
|
992
|
+
const renderedSvg = await satori(outputElement, {
|
|
993
|
+
width: targetWidth,
|
|
994
|
+
height: targetHeight,
|
|
995
|
+
fonts,
|
|
996
|
+
loadAdditionalAsset,
|
|
997
|
+
onNodeDetected: options.onNodeDetected ? (node) => layoutNodes.push(node) : void 0
|
|
998
|
+
});
|
|
999
|
+
return { svg: sanitizeSvg(renderedSvg), layoutNodes };
|
|
1000
|
+
};
|
|
1001
|
+
let finalRender = await renderFinalSvg(finalElement, finalWidth, finalHeight);
|
|
1002
|
+
let finalSvg = finalRender.svg;
|
|
1003
|
+
if (finalHeight < options.height) {
|
|
1004
|
+
const cropCheck = new Resvg(finalSvg, {
|
|
1005
|
+
fitTo: { mode: "width", value: finalWidth },
|
|
1006
|
+
background: "rgba(0, 0, 0, 0)"
|
|
1007
|
+
}).render();
|
|
1008
|
+
const cropPixels2 = new Uint8Array(cropCheck.pixels.buffer);
|
|
1009
|
+
const cropBg = detectBackgroundColor(cropPixels2);
|
|
1010
|
+
const cropOverflow = detectOverflow(cropPixels2, finalWidth, cropCheck.height, cropBg);
|
|
1011
|
+
if (cropOverflow.overflows) {
|
|
1012
|
+
finalHeight = options.height;
|
|
1013
|
+
finalElement = element;
|
|
1014
|
+
finalRender = await renderFinalSvg(finalElement, finalWidth, finalHeight);
|
|
1015
|
+
finalSvg = finalRender.svg;
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
if (shouldCropViewport(options.crop)) {
|
|
1019
|
+
const viewportCheck = new Resvg(finalSvg, {
|
|
1020
|
+
fitTo: { mode: "width", value: finalWidth },
|
|
1021
|
+
background: "rgba(0, 0, 0, 0)"
|
|
1022
|
+
}).render();
|
|
1023
|
+
const viewportPixels = new Uint8Array(viewportCheck.pixels.buffer);
|
|
1024
|
+
const viewportBg = detectBackgroundColor(viewportPixels);
|
|
1025
|
+
const viewportBounds = detectContentBounds(
|
|
1026
|
+
viewportPixels,
|
|
1027
|
+
finalWidth,
|
|
1028
|
+
viewportCheck.height,
|
|
1029
|
+
viewportBg,
|
|
1030
|
+
AUTOCROP_PADDING
|
|
1031
|
+
);
|
|
1032
|
+
if (viewportBounds.width < finalWidth || viewportBounds.height < viewportCheck.height) {
|
|
1033
|
+
finalSvg = cropSvgViewport(finalSvg, viewportBounds);
|
|
1034
|
+
finalRender.layoutNodes = cropLayoutNodes(finalRender.layoutNodes, viewportBounds);
|
|
1035
|
+
finalWidth = viewportBounds.width;
|
|
1036
|
+
finalHeight = viewportBounds.height;
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
if (options.onNodeDetected) {
|
|
1040
|
+
finalRender.layoutNodes.forEach(options.onNodeDetected);
|
|
1041
|
+
}
|
|
1042
|
+
const resvg = new Resvg(finalSvg, {
|
|
1043
|
+
fitTo: {
|
|
1044
|
+
mode: "width",
|
|
1045
|
+
value: finalWidth * (options.scale ?? 2)
|
|
1046
|
+
},
|
|
1047
|
+
background: "rgba(0, 0, 0, 0)"
|
|
1048
|
+
});
|
|
1049
|
+
const pngData = resvg.render();
|
|
1050
|
+
const outputPixels = new Uint8Array(pngData.pixels.buffer);
|
|
1051
|
+
const outputBackground = detectBackgroundColor(outputPixels);
|
|
1052
|
+
const physicalContentBounds = detectContentBounds(
|
|
1053
|
+
outputPixels,
|
|
1054
|
+
pngData.width,
|
|
1055
|
+
pngData.height,
|
|
1056
|
+
outputBackground,
|
|
1057
|
+
0
|
|
1058
|
+
);
|
|
1059
|
+
const outputScale = pngData.width / finalWidth;
|
|
1060
|
+
const contentBounds = {
|
|
1061
|
+
x: physicalContentBounds.x / outputScale,
|
|
1062
|
+
y: physicalContentBounds.y / outputScale,
|
|
1063
|
+
width: physicalContentBounds.width / outputScale,
|
|
1064
|
+
height: physicalContentBounds.height / outputScale
|
|
1065
|
+
};
|
|
1066
|
+
const pngBuffer = pngData.asPng();
|
|
1067
|
+
await mkdir(dirname(options.outputPath), { recursive: true });
|
|
1068
|
+
await writeFile(options.outputPath, pngBuffer);
|
|
1069
|
+
return {
|
|
1070
|
+
width: finalWidth,
|
|
1071
|
+
height: finalHeight,
|
|
1072
|
+
pixelWidth: pngData.width,
|
|
1073
|
+
pixelHeight: pngData.height,
|
|
1074
|
+
contentBounds
|
|
1075
|
+
};
|
|
1076
|
+
}
|
|
1077
|
+
async function renderToBuffer(element, width, height, options = {}) {
|
|
1078
|
+
return withRenderContext(
|
|
1079
|
+
{ background: options.background ?? "transparent", ...options.density !== void 0 ? { density: options.density } : {} },
|
|
1080
|
+
() => renderToBufferInContext(element, width, height, options)
|
|
1081
|
+
);
|
|
1082
|
+
}
|
|
1083
|
+
async function renderToBufferInContext(element, width, height, options) {
|
|
1084
|
+
const fonts = await getFonts();
|
|
1085
|
+
const watermark = resolveWatermark(options);
|
|
1086
|
+
const outputElement = watermark ? wrapWithWatermark(
|
|
1087
|
+
element,
|
|
1088
|
+
width,
|
|
1089
|
+
height,
|
|
1090
|
+
options.theme ?? "dark",
|
|
1091
|
+
watermark
|
|
1092
|
+
) : element;
|
|
1093
|
+
let svg = await satori(outputElement, {
|
|
1094
|
+
width,
|
|
1095
|
+
height,
|
|
1096
|
+
fonts,
|
|
1097
|
+
loadAdditionalAsset,
|
|
1098
|
+
onNodeDetected: options.onNodeDetected
|
|
1099
|
+
});
|
|
1100
|
+
svg = sanitizeSvg(svg);
|
|
1101
|
+
const resvg = new Resvg(svg, {
|
|
1102
|
+
fitTo: { mode: "width", value: width * (options.scale ?? 2) },
|
|
1103
|
+
background: "rgba(0, 0, 0, 0)"
|
|
1104
|
+
});
|
|
1105
|
+
const pngData = resvg.render();
|
|
1106
|
+
return Buffer.from(pngData.asPng());
|
|
1107
|
+
}
|
|
1108
|
+
async function renderToSvg(element, width, height, options = {}) {
|
|
1109
|
+
return withRenderContext(
|
|
1110
|
+
{ background: options.background ?? "transparent", ...options.density !== void 0 ? { density: options.density } : {} },
|
|
1111
|
+
() => renderToSvgInContext(element, width, height, options)
|
|
1112
|
+
);
|
|
1113
|
+
}
|
|
1114
|
+
async function renderToSvgInContext(element, width, height, options) {
|
|
1115
|
+
const fonts = await getFonts();
|
|
1116
|
+
const watermark = resolveWatermark(options);
|
|
1117
|
+
const outputElement = watermark ? wrapWithWatermark(
|
|
1118
|
+
element,
|
|
1119
|
+
width,
|
|
1120
|
+
height,
|
|
1121
|
+
options.theme ?? "dark",
|
|
1122
|
+
watermark
|
|
1123
|
+
) : element;
|
|
1124
|
+
const prepared = prepareAccessibleSvg(outputElement);
|
|
1125
|
+
const layoutNodes = [];
|
|
1126
|
+
const svg = await satori(prepared.element, {
|
|
1127
|
+
width,
|
|
1128
|
+
height,
|
|
1129
|
+
fonts,
|
|
1130
|
+
loadAdditionalAsset,
|
|
1131
|
+
onNodeDetected: (node) => {
|
|
1132
|
+
layoutNodes.push(node);
|
|
1133
|
+
options.onNodeDetected?.(node);
|
|
1134
|
+
}
|
|
1135
|
+
});
|
|
1136
|
+
return addSvgAccessibility(sanitizeSvg(svg), prepared.graphs, layoutNodes);
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
// src/timeline.ts
|
|
1140
|
+
var MIN_FPS = 1;
|
|
1141
|
+
var MAX_FPS = 50;
|
|
1142
|
+
var DEFAULT_FPS = 20;
|
|
1143
|
+
function assertDuration(duration, context) {
|
|
1144
|
+
if (!Number.isFinite(duration) || duration <= 0) {
|
|
1145
|
+
throw new Error(`${context} duration must be a positive finite number`);
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
function assertFps(fps) {
|
|
1149
|
+
if (!Number.isFinite(fps) || !Number.isInteger(fps) || fps < MIN_FPS || fps > MAX_FPS) {
|
|
1150
|
+
throw new Error(`Animation fps must be an integer from ${MIN_FPS} to ${MAX_FPS}`);
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
function clampProgress(progress) {
|
|
1154
|
+
return Math.max(0, Math.min(1, progress));
|
|
1155
|
+
}
|
|
1156
|
+
function easingValue(easing = "linear", progress) {
|
|
1157
|
+
const amount = clampProgress(progress);
|
|
1158
|
+
let result;
|
|
1159
|
+
if (typeof easing === "function") {
|
|
1160
|
+
result = easing(amount);
|
|
1161
|
+
} else if (easing === "ease-in") {
|
|
1162
|
+
result = amount * amount * amount;
|
|
1163
|
+
} else if (easing === "ease-out") {
|
|
1164
|
+
result = 1 - (1 - amount) ** 3;
|
|
1165
|
+
} else if (easing === "ease-in-out") {
|
|
1166
|
+
result = amount < 0.5 ? 4 * amount * amount * amount : 1 - (-2 * amount + 2) ** 3 / 2;
|
|
1167
|
+
} else if (easing === "linear") {
|
|
1168
|
+
result = amount;
|
|
1169
|
+
} else {
|
|
1170
|
+
throw new Error(`Unknown animation easing: ${String(easing)}`);
|
|
1171
|
+
}
|
|
1172
|
+
if (!Number.isFinite(result)) throw new Error("Animation easing must return a finite number");
|
|
1173
|
+
return clampProgress(result);
|
|
1174
|
+
}
|
|
1175
|
+
function hold(duration, label) {
|
|
1176
|
+
return { type: "hold", duration, label };
|
|
1177
|
+
}
|
|
1178
|
+
function keyframe(value, label) {
|
|
1179
|
+
return { type: "keyframe", value, label };
|
|
1180
|
+
}
|
|
1181
|
+
function tween(to, options) {
|
|
1182
|
+
return { type: "tween", to, ...options };
|
|
1183
|
+
}
|
|
1184
|
+
function parallel(tracks, label) {
|
|
1185
|
+
return { type: "parallel", tracks, label };
|
|
1186
|
+
}
|
|
1187
|
+
function propertyStepDuration(step) {
|
|
1188
|
+
return step.type === "keyframe" ? 0 : step.duration;
|
|
1189
|
+
}
|
|
1190
|
+
function parallelDuration(step) {
|
|
1191
|
+
return Math.max(0, ...Object.values(step.tracks).map((track) => track?.reduce((total, item) => total + propertyStepDuration(item), 0) ?? 0));
|
|
1192
|
+
}
|
|
1193
|
+
function timelineStepDuration(step) {
|
|
1194
|
+
if (step.type === "keyframe") return 0;
|
|
1195
|
+
if (step.type === "parallel") return parallelDuration(step);
|
|
1196
|
+
return step.duration;
|
|
1197
|
+
}
|
|
1198
|
+
function validatePropertySteps(steps, context) {
|
|
1199
|
+
for (const step of steps) {
|
|
1200
|
+
if (step.type !== "keyframe") assertDuration(step.duration, context);
|
|
1201
|
+
if (step.type === "tween") easingValue(step.easing, 0.5);
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
function validateTimeline(timeline) {
|
|
1205
|
+
if (timeline.length === 0) throw new Error("Animation timeline requires at least one step");
|
|
1206
|
+
let duration = 0;
|
|
1207
|
+
timeline.forEach((step, index) => {
|
|
1208
|
+
if (step.type === "parallel") {
|
|
1209
|
+
const tracks = Object.entries(step.tracks);
|
|
1210
|
+
if (tracks.length === 0) throw new Error(`Animation parallel step ${index} requires at least one track`);
|
|
1211
|
+
tracks.forEach(([name, track]) => {
|
|
1212
|
+
if (!track) return;
|
|
1213
|
+
validatePropertySteps(
|
|
1214
|
+
track,
|
|
1215
|
+
`Animation parallel track ${JSON.stringify(name)}`
|
|
1216
|
+
);
|
|
1217
|
+
});
|
|
1218
|
+
assertDuration(parallelDuration(step), `Animation parallel step ${index}`);
|
|
1219
|
+
} else if (step.type !== "keyframe") {
|
|
1220
|
+
assertDuration(step.duration, `Animation step ${index}`);
|
|
1221
|
+
if (step.type === "tween") easingValue(step.easing, 0.5);
|
|
1222
|
+
}
|
|
1223
|
+
duration += timelineStepDuration(step);
|
|
1224
|
+
});
|
|
1225
|
+
if (duration <= 0) throw new Error("Animation timeline requires at least one timed step");
|
|
1226
|
+
return duration;
|
|
1227
|
+
}
|
|
1228
|
+
function defineAnimation(definition) {
|
|
1229
|
+
const fps = definition.fps ?? DEFAULT_FPS;
|
|
1230
|
+
assertFps(fps);
|
|
1231
|
+
const duration = validateTimeline(definition.timeline);
|
|
1232
|
+
validateAnimationValues(definition.initial, definition.timeline);
|
|
1233
|
+
return {
|
|
1234
|
+
...definition,
|
|
1235
|
+
kind: "vizmatic.animation",
|
|
1236
|
+
duration,
|
|
1237
|
+
fps
|
|
1238
|
+
};
|
|
1239
|
+
}
|
|
1240
|
+
function interpolateValue(from, to, progress, interpolate) {
|
|
1241
|
+
if (interpolate) return interpolate(from, to, progress);
|
|
1242
|
+
if (typeof from === "number" && typeof to === "number" && Number.isFinite(from) && Number.isFinite(to)) {
|
|
1243
|
+
return from + (to - from) * progress;
|
|
1244
|
+
}
|
|
1245
|
+
throw new Error("Animation tweens require finite numeric values or a custom interpolate function");
|
|
1246
|
+
}
|
|
1247
|
+
function validatePropertyInterpolation(initial, steps) {
|
|
1248
|
+
let value = initial;
|
|
1249
|
+
for (const step of steps) {
|
|
1250
|
+
if (step.type === "keyframe") value = step.value;
|
|
1251
|
+
if (step.type === "tween") {
|
|
1252
|
+
interpolateValue(value, step.to, 0.5, step.interpolate);
|
|
1253
|
+
value = step.to;
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
return value;
|
|
1257
|
+
}
|
|
1258
|
+
function validateAnimationValues(initial, timeline) {
|
|
1259
|
+
let state = { ...initial };
|
|
1260
|
+
for (const step of timeline) {
|
|
1261
|
+
if (step.type === "keyframe") {
|
|
1262
|
+
state = { ...state, ...step.value };
|
|
1263
|
+
} else if (step.type === "tween") {
|
|
1264
|
+
interpolateState(state, step, 0.5);
|
|
1265
|
+
state = { ...state, ...step.to };
|
|
1266
|
+
} else if (step.type === "parallel") {
|
|
1267
|
+
const result = { ...state };
|
|
1268
|
+
for (const key of Object.keys(step.tracks)) {
|
|
1269
|
+
const track = step.tracks[key];
|
|
1270
|
+
if (track) result[key] = validatePropertyInterpolation(state[key], track);
|
|
1271
|
+
}
|
|
1272
|
+
state = result;
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
function interpolateState(from, step, progress) {
|
|
1277
|
+
if (step.interpolate) {
|
|
1278
|
+
const fromPatch = Object.fromEntries(Object.keys(step.to).map((key) => [key, from[key]]));
|
|
1279
|
+
return { ...from, ...step.interpolate(fromPatch, step.to, progress) };
|
|
1280
|
+
}
|
|
1281
|
+
const patch = {};
|
|
1282
|
+
for (const key of Object.keys(step.to)) {
|
|
1283
|
+
const target = step.to[key];
|
|
1284
|
+
if (target === void 0) continue;
|
|
1285
|
+
patch[key] = interpolateValue(from[key], target, progress);
|
|
1286
|
+
}
|
|
1287
|
+
return { ...from, ...patch };
|
|
1288
|
+
}
|
|
1289
|
+
function finalState(state, step) {
|
|
1290
|
+
if (step.type === "keyframe") return { ...state, ...step.value };
|
|
1291
|
+
if (step.type === "tween") return { ...state, ...step.to };
|
|
1292
|
+
if (step.type !== "parallel") return state;
|
|
1293
|
+
const result = { ...state };
|
|
1294
|
+
for (const key of Object.keys(step.tracks)) {
|
|
1295
|
+
const track = step.tracks[key];
|
|
1296
|
+
if (!track) continue;
|
|
1297
|
+
let value = state[key];
|
|
1298
|
+
for (const item of track) {
|
|
1299
|
+
if (item.type === "keyframe" || item.type === "tween") value = item.type === "keyframe" ? item.value : item.to;
|
|
1300
|
+
}
|
|
1301
|
+
result[key] = value;
|
|
1302
|
+
}
|
|
1303
|
+
return result;
|
|
1304
|
+
}
|
|
1305
|
+
function evaluatePropertyTrack(initial, steps, time) {
|
|
1306
|
+
let value = initial;
|
|
1307
|
+
let cursor = 0;
|
|
1308
|
+
for (const step of steps) {
|
|
1309
|
+
if (step.type === "keyframe") {
|
|
1310
|
+
value = step.value;
|
|
1311
|
+
continue;
|
|
1312
|
+
}
|
|
1313
|
+
const end = cursor + step.duration;
|
|
1314
|
+
if (time < end) {
|
|
1315
|
+
if (step.type === "hold") return value;
|
|
1316
|
+
const progress = easingValue(step.easing, (time - cursor) / step.duration);
|
|
1317
|
+
return interpolateValue(value, step.to, progress, step.interpolate);
|
|
1318
|
+
}
|
|
1319
|
+
if (step.type === "tween") value = step.to;
|
|
1320
|
+
cursor = end;
|
|
1321
|
+
}
|
|
1322
|
+
return value;
|
|
1323
|
+
}
|
|
1324
|
+
function evaluateAnimation(animation, requestedTime) {
|
|
1325
|
+
const time = Math.max(0, Math.min(animation.duration, requestedTime));
|
|
1326
|
+
let state = { ...animation.initial };
|
|
1327
|
+
let cursor = 0;
|
|
1328
|
+
for (let index = 0; index < animation.timeline.length; index += 1) {
|
|
1329
|
+
const step = animation.timeline[index];
|
|
1330
|
+
if (step.type === "keyframe") {
|
|
1331
|
+
state = { ...state, ...step.value };
|
|
1332
|
+
continue;
|
|
1333
|
+
}
|
|
1334
|
+
const duration = timelineStepDuration(step);
|
|
1335
|
+
const end = cursor + duration;
|
|
1336
|
+
if (time < end) {
|
|
1337
|
+
const rawProgress = (time - cursor) / duration;
|
|
1338
|
+
if (step.type === "hold") {
|
|
1339
|
+
return { state, step: index, stepProgress: rawProgress, label: step.label };
|
|
1340
|
+
}
|
|
1341
|
+
if (step.type === "tween") {
|
|
1342
|
+
const progress = easingValue(step.easing, rawProgress);
|
|
1343
|
+
return {
|
|
1344
|
+
state: interpolateState(state, step, progress),
|
|
1345
|
+
step: index,
|
|
1346
|
+
stepProgress: rawProgress,
|
|
1347
|
+
label: step.label
|
|
1348
|
+
};
|
|
1349
|
+
}
|
|
1350
|
+
const parallelState = { ...state };
|
|
1351
|
+
for (const key of Object.keys(step.tracks)) {
|
|
1352
|
+
const track = step.tracks[key];
|
|
1353
|
+
if (track) parallelState[key] = evaluatePropertyTrack(state[key], track, time - cursor);
|
|
1354
|
+
}
|
|
1355
|
+
return {
|
|
1356
|
+
state: parallelState,
|
|
1357
|
+
step: index,
|
|
1358
|
+
stepProgress: rawProgress,
|
|
1359
|
+
label: step.label
|
|
1360
|
+
};
|
|
1361
|
+
}
|
|
1362
|
+
state = finalState(state, step);
|
|
1363
|
+
cursor = end;
|
|
1364
|
+
}
|
|
1365
|
+
const lastIndex = animation.timeline.length - 1;
|
|
1366
|
+
return {
|
|
1367
|
+
state,
|
|
1368
|
+
step: lastIndex,
|
|
1369
|
+
stepProgress: 1,
|
|
1370
|
+
label: animation.timeline[lastIndex]?.label
|
|
1371
|
+
};
|
|
1372
|
+
}
|
|
1373
|
+
function sampleAnimation(animation, time) {
|
|
1374
|
+
if (!Number.isFinite(time)) throw new Error("Animation sample time must be finite");
|
|
1375
|
+
return evaluateAnimation(animation, time).state;
|
|
1376
|
+
}
|
|
1377
|
+
function sampleAnimationFrame(animation, requestedTime, timing) {
|
|
1378
|
+
if (!Number.isFinite(requestedTime)) throw new Error("Animation sample time must be finite");
|
|
1379
|
+
const time = Math.max(0, Math.min(animation.duration, requestedTime));
|
|
1380
|
+
const evaluated = evaluateAnimation(animation, time);
|
|
1381
|
+
return {
|
|
1382
|
+
state: evaluated.state,
|
|
1383
|
+
frame: {
|
|
1384
|
+
...timing,
|
|
1385
|
+
time,
|
|
1386
|
+
duration: animation.duration,
|
|
1387
|
+
progress: time / animation.duration,
|
|
1388
|
+
step: evaluated.step,
|
|
1389
|
+
stepProgress: evaluated.stepProgress,
|
|
1390
|
+
label: evaluated.label
|
|
1391
|
+
}
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1394
|
+
function animationCadence(duration, fps) {
|
|
1395
|
+
const interval = 1e3 / fps;
|
|
1396
|
+
const times = [0];
|
|
1397
|
+
for (let time = interval; time < duration; time += interval) times.push(time);
|
|
1398
|
+
if (times.at(-1) !== duration) times.push(duration);
|
|
1399
|
+
const boundaries = times.map((time) => Math.round(time / 10) * 10);
|
|
1400
|
+
const delays = times.map((_, index) => index === times.length - 1 ? 20 : Math.max(20, boundaries[index + 1] - boundaries[index]));
|
|
1401
|
+
const targetDuration = Math.max(
|
|
1402
|
+
Math.round(duration / 10) * 10,
|
|
1403
|
+
delays.length * 20
|
|
1404
|
+
);
|
|
1405
|
+
let excess = delays.reduce((total, delay) => total + delay, 0) - targetDuration;
|
|
1406
|
+
for (let index = delays.length - 2; index >= 0 && excess > 0; index -= 1) {
|
|
1407
|
+
const reduction = Math.min(excess, delays[index] - 20);
|
|
1408
|
+
delays[index] -= reduction;
|
|
1409
|
+
excess -= reduction;
|
|
1410
|
+
}
|
|
1411
|
+
return { times, delays };
|
|
1412
|
+
}
|
|
1413
|
+
function analyzeAnimationCadence(animation, options = {}) {
|
|
1414
|
+
const fps = options.fps ?? animation.fps;
|
|
1415
|
+
assertFps(fps);
|
|
1416
|
+
const cadence = animationCadence(animation.duration, fps);
|
|
1417
|
+
return {
|
|
1418
|
+
fps,
|
|
1419
|
+
duration: animation.duration,
|
|
1420
|
+
frameCount: cadence.times.length,
|
|
1421
|
+
encodedDuration: cadence.delays.reduce((total, delay) => total + delay, 0),
|
|
1422
|
+
targetInterval: 1e3 / fps,
|
|
1423
|
+
minDelay: Math.min(...cadence.delays),
|
|
1424
|
+
maxDelay: Math.max(...cadence.delays)
|
|
1425
|
+
};
|
|
1426
|
+
}
|
|
1427
|
+
function sampleAnimationFrames(animation, options = {}) {
|
|
1428
|
+
const fps = options.fps ?? animation.fps;
|
|
1429
|
+
assertFps(fps);
|
|
1430
|
+
const cadence = animationCadence(animation.duration, fps);
|
|
1431
|
+
const count = cadence.times.length;
|
|
1432
|
+
return cadence.times.map((time, index) => sampleAnimationFrame(animation, time, {
|
|
1433
|
+
index,
|
|
1434
|
+
count,
|
|
1435
|
+
delay: cadence.delays[index],
|
|
1436
|
+
fps
|
|
1437
|
+
}));
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
// src/video.ts
|
|
1441
|
+
import { spawn } from "child_process";
|
|
1442
|
+
import { randomUUID } from "crypto";
|
|
1443
|
+
import { chmod, mkdir as mkdir2, open, rename, rm, stat } from "fs/promises";
|
|
1444
|
+
import { basename, dirname as dirname2, join as join2, resolve } from "path";
|
|
1445
|
+
import { Resvg as Resvg2 } from "@resvg/resvg-js";
|
|
1446
|
+
import parseCssColor from "parse-css-color";
|
|
1447
|
+
var MAX_STDERR_CHARS = 8192;
|
|
1448
|
+
var COLOR_FILTER = "scale=in_range=pc:out_range=tv:out_color_matrix=bt709:flags=lanczos+accurate_rnd+full_chroma_int,format=yuv420p,setparams=range=tv:color_primaries=bt709:color_trc=bt709:colorspace=bt709";
|
|
1449
|
+
var COLOR_TAGS = ["-colorspace", "bt709", "-color_primaries", "bt709", "-color_trc", "bt709", "-color_range", "tv"];
|
|
1450
|
+
function validateTiming(duration, fps) {
|
|
1451
|
+
if (!Number.isFinite(duration) || duration <= 0) {
|
|
1452
|
+
throw new Error("duration must be a positive finite number");
|
|
1453
|
+
}
|
|
1454
|
+
if (!Number.isInteger(fps) || fps < 1 || fps > 120) {
|
|
1455
|
+
throw new Error("fps must be an integer between 1 and 120");
|
|
1456
|
+
}
|
|
1457
|
+
const count = Math.round(duration * fps / 1e3);
|
|
1458
|
+
if (!Number.isSafeInteger(count) || count < 1) {
|
|
1459
|
+
throw new Error("duration must round to at least one frame and no more than Number.MAX_SAFE_INTEGER frames");
|
|
1460
|
+
}
|
|
1461
|
+
return { count, duration: count * 1e3 / fps };
|
|
1462
|
+
}
|
|
1463
|
+
function frameInfo(index, schedule, fps) {
|
|
1464
|
+
return {
|
|
1465
|
+
index,
|
|
1466
|
+
count: schedule.count,
|
|
1467
|
+
time: index * 1e3 / fps,
|
|
1468
|
+
duration: schedule.duration,
|
|
1469
|
+
progress: index / schedule.count,
|
|
1470
|
+
fps
|
|
1471
|
+
};
|
|
1472
|
+
}
|
|
1473
|
+
function* videoFrameSchedule(duration, fps = 30) {
|
|
1474
|
+
const schedule = validateTiming(duration, fps);
|
|
1475
|
+
for (let index = 0; index < schedule.count; index++) {
|
|
1476
|
+
yield frameInfo(index, schedule, fps);
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
async function renderAnimationVideo(animation, options) {
|
|
1480
|
+
const scale = options.scale ?? 1;
|
|
1481
|
+
if (!Number.isFinite(options.width) || options.width <= 0 || !Number.isFinite(options.height) || options.height <= 0) {
|
|
1482
|
+
throw new Error("Animation width and height must be positive finite numbers");
|
|
1483
|
+
}
|
|
1484
|
+
if (!Number.isFinite(scale) || scale <= 0) throw new Error("scale must be a positive finite number");
|
|
1485
|
+
const theme = options.theme ?? "dark";
|
|
1486
|
+
const background = options.background ?? "theme";
|
|
1487
|
+
const backgroundColor = background === "theme" ? getThemeColors(theme).bg : background;
|
|
1488
|
+
const parsedBackground = parseCssColor(backgroundColor);
|
|
1489
|
+
if (!parsedBackground || parsedBackground.alpha !== 1) {
|
|
1490
|
+
throw new Error('MP4 requires an opaque background; use "theme" or an opaque CSS color');
|
|
1491
|
+
}
|
|
1492
|
+
return renderRgbaVideo(async (info) => {
|
|
1493
|
+
const sampled = sampleAnimationFrame(animation, info.time, {
|
|
1494
|
+
index: info.index,
|
|
1495
|
+
count: info.count,
|
|
1496
|
+
delay: 1e3 / info.fps,
|
|
1497
|
+
fps: info.fps
|
|
1498
|
+
});
|
|
1499
|
+
const edges = { top: false, right: false, bottom: false, left: false };
|
|
1500
|
+
const svg = await renderToSvg(animation.render(sampled.state, sampled.frame), options.width, options.height, {
|
|
1501
|
+
theme,
|
|
1502
|
+
background,
|
|
1503
|
+
watermark: options.watermark ?? options.brand,
|
|
1504
|
+
onNodeDetected: (node) => {
|
|
1505
|
+
edges.top ||= node.top < -0.5;
|
|
1506
|
+
edges.right ||= node.left + node.width > options.width + 0.5;
|
|
1507
|
+
edges.bottom ||= node.top + node.height > options.height + 0.5;
|
|
1508
|
+
edges.left ||= node.left < -0.5;
|
|
1509
|
+
}
|
|
1510
|
+
});
|
|
1511
|
+
const names = Object.keys(edges).filter((edge) => edges[edge]);
|
|
1512
|
+
if (names.length) {
|
|
1513
|
+
const overflow = {
|
|
1514
|
+
overflows: true,
|
|
1515
|
+
edges,
|
|
1516
|
+
message: `Content overflows canvas at: ${names.join(", ")}. Increase the illustration dimensions.`
|
|
1517
|
+
};
|
|
1518
|
+
throw new CanvasOverflowError(options.width, options.height, overflow, {
|
|
1519
|
+
frameIndex: info.index,
|
|
1520
|
+
frameCount: info.count,
|
|
1521
|
+
time: info.time,
|
|
1522
|
+
label: sampled.frame.label
|
|
1523
|
+
});
|
|
1524
|
+
}
|
|
1525
|
+
const rendered = new Resvg2(svg, {
|
|
1526
|
+
fitTo: { mode: "width", value: options.width * scale },
|
|
1527
|
+
background: backgroundColor
|
|
1528
|
+
}).render();
|
|
1529
|
+
if (rendered.width !== options.width * scale || rendered.height !== options.height * scale) {
|
|
1530
|
+
throw new Error("All video frames must match the scaled canvas dimensions");
|
|
1531
|
+
}
|
|
1532
|
+
return rendered.pixels;
|
|
1533
|
+
}, {
|
|
1534
|
+
...options,
|
|
1535
|
+
width: options.width * scale,
|
|
1536
|
+
height: options.height * scale,
|
|
1537
|
+
duration: animation.duration
|
|
1538
|
+
});
|
|
1539
|
+
}
|
|
1540
|
+
function abortError() {
|
|
1541
|
+
const error = new Error("Video export was aborted.");
|
|
1542
|
+
error.name = "AbortError";
|
|
1543
|
+
return error;
|
|
1544
|
+
}
|
|
1545
|
+
function withAbort(promise, signal) {
|
|
1546
|
+
if (!signal) return promise;
|
|
1547
|
+
if (signal.aborted) return Promise.reject(abortError());
|
|
1548
|
+
return new Promise((resolvePromise, rejectPromise) => {
|
|
1549
|
+
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
1550
|
+
const onAbort = () => {
|
|
1551
|
+
cleanup();
|
|
1552
|
+
rejectPromise(abortError());
|
|
1553
|
+
};
|
|
1554
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1555
|
+
promise.then(
|
|
1556
|
+
(value) => {
|
|
1557
|
+
cleanup();
|
|
1558
|
+
resolvePromise(value);
|
|
1559
|
+
},
|
|
1560
|
+
(error) => {
|
|
1561
|
+
cleanup();
|
|
1562
|
+
rejectPromise(error);
|
|
1563
|
+
}
|
|
1564
|
+
);
|
|
1565
|
+
});
|
|
1566
|
+
}
|
|
1567
|
+
function formatSeconds(durationMs) {
|
|
1568
|
+
return (durationMs / 1e3).toFixed(12).replace(/0+$/, "").replace(/\.$/, "");
|
|
1569
|
+
}
|
|
1570
|
+
function describeExit(code, signal, stderr) {
|
|
1571
|
+
const status = signal ? `signal ${signal}` : `exit code ${code}`;
|
|
1572
|
+
const detail = stderr.trim();
|
|
1573
|
+
return new Error(`FFmpeg failed (${status})${detail ? `: ${detail}` : ""}`);
|
|
1574
|
+
}
|
|
1575
|
+
async function stopEncoder(child, exited) {
|
|
1576
|
+
if (child.exitCode !== null || child.signalCode !== null) return;
|
|
1577
|
+
child.kill("SIGTERM");
|
|
1578
|
+
let timer;
|
|
1579
|
+
const terminated = await Promise.race([
|
|
1580
|
+
exited.then(() => true),
|
|
1581
|
+
new Promise((resolvePromise) => {
|
|
1582
|
+
timer = setTimeout(() => resolvePromise(false), 1e3);
|
|
1583
|
+
})
|
|
1584
|
+
]);
|
|
1585
|
+
if (timer) clearTimeout(timer);
|
|
1586
|
+
if (!terminated) {
|
|
1587
|
+
child.kill("SIGKILL");
|
|
1588
|
+
await exited;
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
async function renderRgbaVideo(renderFrame, options) {
|
|
1592
|
+
const fps = options.fps ?? 30;
|
|
1593
|
+
const schedule = validateTiming(options.duration, fps);
|
|
1594
|
+
const { width, height } = options;
|
|
1595
|
+
if (!Number.isSafeInteger(width) || width <= 0 || width % 2 !== 0) {
|
|
1596
|
+
throw new Error("width must be a positive even integer");
|
|
1597
|
+
}
|
|
1598
|
+
if (!Number.isSafeInteger(height) || height <= 0 || height % 2 !== 0) {
|
|
1599
|
+
throw new Error("height must be a positive even integer");
|
|
1600
|
+
}
|
|
1601
|
+
const expectedBytes = width * height * 4;
|
|
1602
|
+
if (!Number.isSafeInteger(expectedBytes)) {
|
|
1603
|
+
throw new Error("frame dimensions are too large");
|
|
1604
|
+
}
|
|
1605
|
+
if (typeof renderFrame !== "function") throw new Error("renderFrame must be a function");
|
|
1606
|
+
if (typeof options.outputPath !== "string" || options.outputPath.length === 0) {
|
|
1607
|
+
throw new Error("outputPath must be a non-empty path");
|
|
1608
|
+
}
|
|
1609
|
+
const ffmpegPath = options.ffmpegPath ?? "ffmpeg";
|
|
1610
|
+
const codec = options.codec ?? "h264";
|
|
1611
|
+
if (codec !== "h264" && codec !== "vp9") throw new Error("codec must be h264 or vp9");
|
|
1612
|
+
const preset = options.preset ?? "fast";
|
|
1613
|
+
const crf = options.crf ?? (codec === "vp9" ? 30 : 18);
|
|
1614
|
+
const threads = options.threads ?? 4;
|
|
1615
|
+
if (typeof ffmpegPath !== "string" || ffmpegPath.length === 0) throw new Error("ffmpegPath must be a non-empty path");
|
|
1616
|
+
if (typeof preset !== "string" || preset.length === 0) throw new Error("preset must be a non-empty string");
|
|
1617
|
+
const maxCrf = codec === "vp9" ? 63 : 51;
|
|
1618
|
+
if (!Number.isInteger(crf) || crf < 0 || crf > maxCrf) throw new Error(`crf must be an integer between 0 and ${maxCrf}`);
|
|
1619
|
+
if (options.tune !== void 0 && (typeof options.tune !== "string" || options.tune.length === 0)) {
|
|
1620
|
+
throw new Error("tune must be a non-empty string when provided");
|
|
1621
|
+
}
|
|
1622
|
+
if (options.audioOffset !== void 0 && (!Number.isFinite(options.audioOffset) || options.audioOffset < 0)) {
|
|
1623
|
+
throw new Error("audioOffset must be a non-negative finite number of seconds");
|
|
1624
|
+
}
|
|
1625
|
+
if (!Number.isSafeInteger(threads) || threads < 1) throw new Error("threads must be a positive safe integer");
|
|
1626
|
+
if (options.audioPath !== void 0 && (typeof options.audioPath !== "string" || options.audioPath.length === 0)) {
|
|
1627
|
+
throw new Error("audioPath must be a non-empty path when provided");
|
|
1628
|
+
}
|
|
1629
|
+
if (options.signal?.aborted) throw abortError();
|
|
1630
|
+
const outputPath = resolve(options.outputPath);
|
|
1631
|
+
const outputDirectory = dirname2(outputPath);
|
|
1632
|
+
await mkdir2(outputDirectory, { recursive: true });
|
|
1633
|
+
const extension = codec === "vp9" ? "webm" : "mp4";
|
|
1634
|
+
const tempPath = join2(outputDirectory, `.${basename(outputPath)}.${randomUUID()}.tmp.${extension}`);
|
|
1635
|
+
const tempFile = await open(tempPath, "wx", 384);
|
|
1636
|
+
try {
|
|
1637
|
+
await tempFile.close();
|
|
1638
|
+
} catch (error) {
|
|
1639
|
+
await rm(tempPath, { force: true });
|
|
1640
|
+
throw error;
|
|
1641
|
+
}
|
|
1642
|
+
const seconds = formatSeconds(schedule.duration);
|
|
1643
|
+
const args = [
|
|
1644
|
+
"-hide_banner",
|
|
1645
|
+
"-loglevel",
|
|
1646
|
+
"error",
|
|
1647
|
+
"-nostdin",
|
|
1648
|
+
"-y",
|
|
1649
|
+
"-f",
|
|
1650
|
+
"rawvideo",
|
|
1651
|
+
"-pixel_format",
|
|
1652
|
+
"rgba",
|
|
1653
|
+
"-video_size",
|
|
1654
|
+
`${width}x${height}`,
|
|
1655
|
+
"-framerate",
|
|
1656
|
+
String(fps),
|
|
1657
|
+
"-i",
|
|
1658
|
+
"pipe:0"
|
|
1659
|
+
];
|
|
1660
|
+
if (options.audioPath) {
|
|
1661
|
+
if (options.audioOffset) args.push("-ss", String(options.audioOffset));
|
|
1662
|
+
args.push("-i", resolve(options.audioPath));
|
|
1663
|
+
}
|
|
1664
|
+
args.push("-map", "0:v:0");
|
|
1665
|
+
if (options.audioPath) args.push("-map", "1:a:0", "-af", `asetpts=PTS-STARTPTS,apad,atrim=duration=${seconds}`);
|
|
1666
|
+
args.push("-frames:v", String(schedule.count), "-t", seconds, "-vf", COLOR_FILTER);
|
|
1667
|
+
if (codec === "vp9") {
|
|
1668
|
+
args.push("-c:v", "libvpx-vp9", "-crf", String(crf), "-b:v", "0", "-deadline", "good", "-cpu-used", "2", "-row-mt", "1", "-threads:v", String(threads));
|
|
1669
|
+
} else {
|
|
1670
|
+
args.push("-c:v", "libx264", "-preset", preset);
|
|
1671
|
+
if (options.tune) args.push("-tune", options.tune);
|
|
1672
|
+
args.push("-crf", String(crf), "-threads:v", String(threads));
|
|
1673
|
+
}
|
|
1674
|
+
args.push("-pix_fmt", "yuv420p", ...COLOR_TAGS);
|
|
1675
|
+
if (options.audioPath) {
|
|
1676
|
+
if (codec === "vp9") args.push("-c:a", "libopus", "-b:a", "160k", "-ar", "48000", "-ac", "2");
|
|
1677
|
+
else args.push("-c:a", "aac", "-b:a", "192k", "-ar", "48000", "-ac", "2");
|
|
1678
|
+
}
|
|
1679
|
+
if (codec === "h264") args.push("-movflags", "+faststart");
|
|
1680
|
+
args.push(tempPath);
|
|
1681
|
+
let stderr = "";
|
|
1682
|
+
let inputError;
|
|
1683
|
+
let child;
|
|
1684
|
+
try {
|
|
1685
|
+
child = spawn(ffmpegPath, args, { stdio: ["pipe", "ignore", "pipe"] });
|
|
1686
|
+
} catch (error) {
|
|
1687
|
+
await rm(tempPath, { force: true });
|
|
1688
|
+
throw error;
|
|
1689
|
+
}
|
|
1690
|
+
child.stderr?.on("data", (chunk) => {
|
|
1691
|
+
stderr += chunk.toString();
|
|
1692
|
+
if (stderr.length > MAX_STDERR_CHARS) stderr = stderr.slice(-MAX_STDERR_CHARS);
|
|
1693
|
+
});
|
|
1694
|
+
child.stdin?.on("error", (error) => {
|
|
1695
|
+
inputError = error;
|
|
1696
|
+
});
|
|
1697
|
+
let exitResult;
|
|
1698
|
+
const exited = new Promise((resolvePromise) => {
|
|
1699
|
+
child.once("error", (error) => {
|
|
1700
|
+
exitResult = { code: null, signal: null, error };
|
|
1701
|
+
resolvePromise(exitResult);
|
|
1702
|
+
});
|
|
1703
|
+
child.once("close", (code, signal) => {
|
|
1704
|
+
exitResult = { code, signal };
|
|
1705
|
+
resolvePromise(exitResult);
|
|
1706
|
+
});
|
|
1707
|
+
});
|
|
1708
|
+
function observeEncoder(operation, index, finishAfterSuccess) {
|
|
1709
|
+
const lastFrame = index === schedule.count - 1;
|
|
1710
|
+
const completed = (exit) => {
|
|
1711
|
+
if (!exit.error && exit.code === 0 && lastFrame) {
|
|
1712
|
+
return finishAfterSuccess ? Promise.resolve(void 0) : operation;
|
|
1713
|
+
}
|
|
1714
|
+
if (exit.error) return Promise.reject(new Error(`Unable to start FFmpeg: ${exit.error.message}`, { cause: exit.error }));
|
|
1715
|
+
return Promise.reject(describeExit(exit.code, exit.signal, stderr));
|
|
1716
|
+
};
|
|
1717
|
+
if (exitResult) return completed(exitResult);
|
|
1718
|
+
return new Promise((resolvePromise, rejectPromise) => {
|
|
1719
|
+
const cleanup = () => {
|
|
1720
|
+
child.removeListener("error", onError);
|
|
1721
|
+
child.removeListener("close", onClose);
|
|
1722
|
+
};
|
|
1723
|
+
const onError = (error) => {
|
|
1724
|
+
cleanup();
|
|
1725
|
+
rejectPromise(new Error(`Unable to start FFmpeg: ${error.message}`, { cause: error }));
|
|
1726
|
+
};
|
|
1727
|
+
const onClose = (code, signal) => {
|
|
1728
|
+
cleanup();
|
|
1729
|
+
void completed({ code, signal }).then(resolvePromise, rejectPromise);
|
|
1730
|
+
};
|
|
1731
|
+
child.once("error", onError);
|
|
1732
|
+
child.once("close", onClose);
|
|
1733
|
+
operation.then(
|
|
1734
|
+
(value) => {
|
|
1735
|
+
cleanup();
|
|
1736
|
+
resolvePromise(value);
|
|
1737
|
+
},
|
|
1738
|
+
(error) => {
|
|
1739
|
+
cleanup();
|
|
1740
|
+
rejectPromise(error);
|
|
1741
|
+
}
|
|
1742
|
+
);
|
|
1743
|
+
});
|
|
1744
|
+
}
|
|
1745
|
+
try {
|
|
1746
|
+
const input = child.stdin;
|
|
1747
|
+
if (!input) throw new Error("FFmpeg did not provide a frame input stream");
|
|
1748
|
+
for (let index = 0; index < schedule.count; index++) {
|
|
1749
|
+
const info = frameInfo(index, schedule, fps);
|
|
1750
|
+
const rendered = await withAbort(observeEncoder(Promise.resolve(renderFrame(info)), index, false), options.signal);
|
|
1751
|
+
if (!(rendered instanceof Uint8Array) || rendered.byteLength !== expectedBytes) {
|
|
1752
|
+
throw new Error(`Frame ${index} must contain exactly ${expectedBytes} RGBA bytes`);
|
|
1753
|
+
}
|
|
1754
|
+
if (inputError) throw describeExit(null, null, inputError.message || stderr);
|
|
1755
|
+
const pixels = Buffer.from(rendered.buffer, rendered.byteOffset, rendered.byteLength);
|
|
1756
|
+
const write = new Promise((resolvePromise, rejectPromise) => {
|
|
1757
|
+
input.write(pixels, (error) => error ? rejectPromise(error) : resolvePromise());
|
|
1758
|
+
});
|
|
1759
|
+
await withAbort(observeEncoder(write, index, true), options.signal);
|
|
1760
|
+
if (options.onProgress) {
|
|
1761
|
+
await withAbort(observeEncoder(Promise.resolve(options.onProgress(info)), index, false), options.signal);
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
input.end();
|
|
1765
|
+
const exit = await withAbort(exited, options.signal);
|
|
1766
|
+
if (exit.error) throw new Error(`Unable to start FFmpeg: ${exit.error.message}`, { cause: exit.error });
|
|
1767
|
+
if (exit.code !== 0) throw describeExit(exit.code, exit.signal, stderr);
|
|
1768
|
+
const outputStat = await stat(tempPath);
|
|
1769
|
+
await chmod(tempPath, 420);
|
|
1770
|
+
await rename(tempPath, outputPath);
|
|
1771
|
+
return {
|
|
1772
|
+
width,
|
|
1773
|
+
height,
|
|
1774
|
+
frameCount: schedule.count,
|
|
1775
|
+
duration: schedule.duration,
|
|
1776
|
+
fps,
|
|
1777
|
+
bytes: outputStat.size,
|
|
1778
|
+
outputPath,
|
|
1779
|
+
audioIncluded: options.audioPath !== void 0
|
|
1780
|
+
};
|
|
1781
|
+
} catch (error) {
|
|
1782
|
+
await stopEncoder(child, exited);
|
|
1783
|
+
if (inputError && error instanceof Error && (error.message === "write EPIPE" || error.code === "EPIPE")) {
|
|
1784
|
+
throw describeExit(null, null, stderr || inputError.message);
|
|
1785
|
+
}
|
|
1786
|
+
throw error;
|
|
1787
|
+
} finally {
|
|
1788
|
+
await rm(tempPath, { force: true });
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
|
|
1792
|
+
// node_modules/.pnpm/fast-png@8.0.0/node_modules/fast-png/lib/convert_indexed_to_rgb.js
|
|
1793
|
+
function convertIndexedToRgb(decodedImage) {
|
|
1794
|
+
const palette = decodedImage.palette;
|
|
1795
|
+
const depth = decodedImage.depth;
|
|
1796
|
+
if (!palette) {
|
|
1797
|
+
throw new Error("Color palette is undefined.");
|
|
1798
|
+
}
|
|
1799
|
+
checkDataSize(decodedImage);
|
|
1800
|
+
const indexSize = decodedImage.width * decodedImage.height;
|
|
1801
|
+
const resSize = indexSize * palette[0].length;
|
|
1802
|
+
const res = new Uint8Array(resSize);
|
|
1803
|
+
let indexPos = 0;
|
|
1804
|
+
let offset = 0;
|
|
1805
|
+
const indexes = new Uint8Array(indexSize);
|
|
1806
|
+
let bit = 255;
|
|
1807
|
+
switch (depth) {
|
|
1808
|
+
case 1:
|
|
1809
|
+
bit = 128;
|
|
1810
|
+
break;
|
|
1811
|
+
case 2:
|
|
1812
|
+
bit = 192;
|
|
1813
|
+
break;
|
|
1814
|
+
case 4:
|
|
1815
|
+
bit = 240;
|
|
1816
|
+
break;
|
|
1817
|
+
case 8:
|
|
1818
|
+
bit = 255;
|
|
1819
|
+
break;
|
|
1820
|
+
default:
|
|
1821
|
+
throw new Error("Incorrect depth value");
|
|
1822
|
+
}
|
|
1823
|
+
for (const byte of decodedImage.data) {
|
|
1824
|
+
let bit2 = bit;
|
|
1825
|
+
let shift = 8;
|
|
1826
|
+
while (bit2) {
|
|
1827
|
+
shift -= depth;
|
|
1828
|
+
indexes[indexPos++] = (byte & bit2) >> shift;
|
|
1829
|
+
bit2 = bit2 >> depth;
|
|
1830
|
+
if (indexPos % decodedImage.width === 0) {
|
|
1831
|
+
break;
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
if (decodedImage.palette) {
|
|
1836
|
+
for (const index of indexes) {
|
|
1837
|
+
const color = decodedImage.palette.at(index);
|
|
1838
|
+
if (!color) {
|
|
1839
|
+
throw new Error("Incorrect index of palette color");
|
|
1840
|
+
}
|
|
1841
|
+
res.set(color, offset);
|
|
1842
|
+
offset += color.length;
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
return res;
|
|
1846
|
+
}
|
|
1847
|
+
function checkDataSize(image) {
|
|
1848
|
+
const expectedSize = image.depth < 8 ? Math.ceil(image.width * image.depth / 8) * image.height * image.channels : image.width * image.height * image.channels;
|
|
1849
|
+
if (image.data.length !== expectedSize) {
|
|
1850
|
+
throw new RangeError(`wrong data size. Found ${image.data.length}, expected ${expectedSize}`);
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
// node_modules/.pnpm/fast-png@8.0.0/node_modules/fast-png/lib/png_decoder.js
|
|
1855
|
+
import { Unzlib as Inflator, unzlibSync } from "fflate";
|
|
1856
|
+
|
|
1857
|
+
// node_modules/.pnpm/iobuffer@6.0.1/node_modules/iobuffer/lib/text.js
|
|
1858
|
+
function decode(bytes, encoding = "utf8") {
|
|
1859
|
+
const decoder = new TextDecoder(encoding);
|
|
1860
|
+
return decoder.decode(bytes);
|
|
1861
|
+
}
|
|
1862
|
+
var encoder = new TextEncoder();
|
|
1863
|
+
function encode(str) {
|
|
1864
|
+
return encoder.encode(str);
|
|
1865
|
+
}
|
|
1866
|
+
|
|
1867
|
+
// node_modules/.pnpm/iobuffer@6.0.1/node_modules/iobuffer/lib/iobuffer.js
|
|
1868
|
+
var defaultByteLength = 1024 * 8;
|
|
1869
|
+
var hostBigEndian = (() => {
|
|
1870
|
+
const array = new Uint8Array(4);
|
|
1871
|
+
const view = new Uint32Array(array.buffer);
|
|
1872
|
+
return !((view[0] = 1) & array[0]);
|
|
1873
|
+
})();
|
|
1874
|
+
var typedArrays = {
|
|
1875
|
+
int8: globalThis.Int8Array,
|
|
1876
|
+
uint8: globalThis.Uint8Array,
|
|
1877
|
+
int16: globalThis.Int16Array,
|
|
1878
|
+
uint16: globalThis.Uint16Array,
|
|
1879
|
+
int32: globalThis.Int32Array,
|
|
1880
|
+
uint32: globalThis.Uint32Array,
|
|
1881
|
+
uint64: globalThis.BigUint64Array,
|
|
1882
|
+
int64: globalThis.BigInt64Array,
|
|
1883
|
+
float32: globalThis.Float32Array,
|
|
1884
|
+
float64: globalThis.Float64Array
|
|
1885
|
+
};
|
|
1886
|
+
var IOBuffer = class _IOBuffer {
|
|
1887
|
+
/**
|
|
1888
|
+
* Reference to the internal ArrayBuffer object.
|
|
1889
|
+
*/
|
|
1890
|
+
buffer;
|
|
1891
|
+
/**
|
|
1892
|
+
* Byte length of the internal ArrayBuffer.
|
|
1893
|
+
*/
|
|
1894
|
+
byteLength;
|
|
1895
|
+
/**
|
|
1896
|
+
* Byte offset of the internal ArrayBuffer.
|
|
1897
|
+
*/
|
|
1898
|
+
byteOffset;
|
|
1899
|
+
/**
|
|
1900
|
+
* Byte length of the internal ArrayBuffer.
|
|
1901
|
+
*/
|
|
1902
|
+
length;
|
|
1903
|
+
/**
|
|
1904
|
+
* The current offset of the buffer's pointer.
|
|
1905
|
+
*/
|
|
1906
|
+
offset;
|
|
1907
|
+
lastWrittenByte;
|
|
1908
|
+
littleEndian;
|
|
1909
|
+
_data;
|
|
1910
|
+
_mark;
|
|
1911
|
+
_marks;
|
|
1912
|
+
/**
|
|
1913
|
+
* Create a new IOBuffer.
|
|
1914
|
+
* @param data - The data to construct the IOBuffer with.
|
|
1915
|
+
* If data is a number, it will be the new buffer's length<br>
|
|
1916
|
+
* If data is `undefined`, the buffer will be initialized with a default length of 8Kb<br>
|
|
1917
|
+
* If data is an ArrayBuffer, SharedArrayBuffer, an ArrayBufferView (Typed Array), an IOBuffer instance,
|
|
1918
|
+
* or a Node.js Buffer, a view will be created over the underlying ArrayBuffer.
|
|
1919
|
+
* @param options - An object for the options.
|
|
1920
|
+
* @returns A new IOBuffer instance.
|
|
1921
|
+
*/
|
|
1922
|
+
constructor(data = defaultByteLength, options = {}) {
|
|
1923
|
+
let dataIsGiven = false;
|
|
1924
|
+
if (typeof data === "number") {
|
|
1925
|
+
data = new ArrayBuffer(data);
|
|
1926
|
+
} else {
|
|
1927
|
+
dataIsGiven = true;
|
|
1928
|
+
this.lastWrittenByte = data.byteLength;
|
|
1929
|
+
}
|
|
1930
|
+
const offset = options.offset ? options.offset >>> 0 : 0;
|
|
1931
|
+
const byteLength = data.byteLength - offset;
|
|
1932
|
+
let dvOffset = offset;
|
|
1933
|
+
if (ArrayBuffer.isView(data) || data instanceof _IOBuffer) {
|
|
1934
|
+
if (data.byteLength !== data.buffer.byteLength) {
|
|
1935
|
+
dvOffset = data.byteOffset + offset;
|
|
1936
|
+
}
|
|
1937
|
+
data = data.buffer;
|
|
1938
|
+
}
|
|
1939
|
+
if (dataIsGiven) {
|
|
1940
|
+
this.lastWrittenByte = byteLength;
|
|
1941
|
+
} else {
|
|
1942
|
+
this.lastWrittenByte = 0;
|
|
1943
|
+
}
|
|
1944
|
+
this.buffer = data;
|
|
1945
|
+
this.length = byteLength;
|
|
1946
|
+
this.byteLength = byteLength;
|
|
1947
|
+
this.byteOffset = dvOffset;
|
|
1948
|
+
this.offset = 0;
|
|
1949
|
+
this.littleEndian = true;
|
|
1950
|
+
this._data = new DataView(this.buffer, dvOffset, byteLength);
|
|
1951
|
+
this._mark = 0;
|
|
1952
|
+
this._marks = [];
|
|
1953
|
+
}
|
|
1954
|
+
/**
|
|
1955
|
+
* Checks if the memory allocated to the buffer is sufficient to store more
|
|
1956
|
+
* bytes after the offset.
|
|
1957
|
+
* @param byteLength - The needed memory in bytes.
|
|
1958
|
+
* @returns `true` if there is sufficient space and `false` otherwise.
|
|
1959
|
+
*/
|
|
1960
|
+
available(byteLength = 1) {
|
|
1961
|
+
return this.offset + byteLength <= this.length;
|
|
1962
|
+
}
|
|
1963
|
+
/**
|
|
1964
|
+
* Check if little-endian mode is used for reading and writing multi-byte
|
|
1965
|
+
* values.
|
|
1966
|
+
* @returns `true` if little-endian mode is used, `false` otherwise.
|
|
1967
|
+
*/
|
|
1968
|
+
isLittleEndian() {
|
|
1969
|
+
return this.littleEndian;
|
|
1970
|
+
}
|
|
1971
|
+
/**
|
|
1972
|
+
* Set little-endian mode for reading and writing multi-byte values.
|
|
1973
|
+
* @returns This.
|
|
1974
|
+
*/
|
|
1975
|
+
setLittleEndian() {
|
|
1976
|
+
this.littleEndian = true;
|
|
1977
|
+
return this;
|
|
1978
|
+
}
|
|
1979
|
+
/**
|
|
1980
|
+
* Check if big-endian mode is used for reading and writing multi-byte values.
|
|
1981
|
+
* @returns `true` if big-endian mode is used, `false` otherwise.
|
|
1982
|
+
*/
|
|
1983
|
+
isBigEndian() {
|
|
1984
|
+
return !this.littleEndian;
|
|
1985
|
+
}
|
|
1986
|
+
/**
|
|
1987
|
+
* Switches to big-endian mode for reading and writing multi-byte values.
|
|
1988
|
+
* @returns This.
|
|
1989
|
+
*/
|
|
1990
|
+
setBigEndian() {
|
|
1991
|
+
this.littleEndian = false;
|
|
1992
|
+
return this;
|
|
1993
|
+
}
|
|
1994
|
+
/**
|
|
1995
|
+
* Move the pointer n bytes forward.
|
|
1996
|
+
* @param n - Number of bytes to skip.
|
|
1997
|
+
* @returns This.
|
|
1998
|
+
*/
|
|
1999
|
+
skip(n = 1) {
|
|
2000
|
+
this.offset += n;
|
|
2001
|
+
return this;
|
|
2002
|
+
}
|
|
2003
|
+
/**
|
|
2004
|
+
* Move the pointer n bytes backward.
|
|
2005
|
+
* @param n - Number of bytes to move back.
|
|
2006
|
+
* @returns This.
|
|
2007
|
+
*/
|
|
2008
|
+
back(n = 1) {
|
|
2009
|
+
this.offset -= n;
|
|
2010
|
+
return this;
|
|
2011
|
+
}
|
|
2012
|
+
/**
|
|
2013
|
+
* Move the pointer to the given offset.
|
|
2014
|
+
* @param offset - The offset to move to.
|
|
2015
|
+
* @returns This.
|
|
2016
|
+
*/
|
|
2017
|
+
seek(offset) {
|
|
2018
|
+
this.offset = offset;
|
|
2019
|
+
return this;
|
|
2020
|
+
}
|
|
2021
|
+
/**
|
|
2022
|
+
* Store the current pointer offset.
|
|
2023
|
+
* @see {@link IOBuffer#reset}
|
|
2024
|
+
* @returns This.
|
|
2025
|
+
*/
|
|
2026
|
+
mark() {
|
|
2027
|
+
this._mark = this.offset;
|
|
2028
|
+
return this;
|
|
2029
|
+
}
|
|
2030
|
+
/**
|
|
2031
|
+
* Move the pointer back to the last pointer offset set by mark.
|
|
2032
|
+
* @see {@link IOBuffer#mark}
|
|
2033
|
+
* @returns This.
|
|
2034
|
+
*/
|
|
2035
|
+
reset() {
|
|
2036
|
+
this.offset = this._mark;
|
|
2037
|
+
return this;
|
|
2038
|
+
}
|
|
2039
|
+
/**
|
|
2040
|
+
* Push the current pointer offset to the mark stack.
|
|
2041
|
+
* @see {@link IOBuffer#popMark}
|
|
2042
|
+
* @returns This.
|
|
2043
|
+
*/
|
|
2044
|
+
pushMark() {
|
|
2045
|
+
this._marks.push(this.offset);
|
|
2046
|
+
return this;
|
|
2047
|
+
}
|
|
2048
|
+
/**
|
|
2049
|
+
* Pop the last pointer offset from the mark stack, and set the current
|
|
2050
|
+
* pointer offset to the popped value.
|
|
2051
|
+
* @see {@link IOBuffer#pushMark}
|
|
2052
|
+
* @returns This.
|
|
2053
|
+
*/
|
|
2054
|
+
popMark() {
|
|
2055
|
+
const offset = this._marks.pop();
|
|
2056
|
+
if (offset === void 0) {
|
|
2057
|
+
throw new Error("Mark stack empty");
|
|
2058
|
+
}
|
|
2059
|
+
this.seek(offset);
|
|
2060
|
+
return this;
|
|
2061
|
+
}
|
|
2062
|
+
/**
|
|
2063
|
+
* Move the pointer offset back to 0.
|
|
2064
|
+
* @returns This.
|
|
2065
|
+
*/
|
|
2066
|
+
rewind() {
|
|
2067
|
+
this.offset = 0;
|
|
2068
|
+
return this;
|
|
2069
|
+
}
|
|
2070
|
+
/**
|
|
2071
|
+
* Make sure the buffer has sufficient memory to write a given byteLength at
|
|
2072
|
+
* the current pointer offset.
|
|
2073
|
+
* If the buffer's memory is insufficient, this method will create a new
|
|
2074
|
+
* buffer (a copy) with a length that is twice (byteLength + current offset).
|
|
2075
|
+
* @param byteLength - The needed memory in bytes.
|
|
2076
|
+
* @returns This.
|
|
2077
|
+
*/
|
|
2078
|
+
ensureAvailable(byteLength = 1) {
|
|
2079
|
+
if (!this.available(byteLength)) {
|
|
2080
|
+
const lengthNeeded = this.offset + byteLength;
|
|
2081
|
+
const newLength = lengthNeeded * 2;
|
|
2082
|
+
const newArray = new Uint8Array(newLength);
|
|
2083
|
+
newArray.set(new Uint8Array(this.buffer));
|
|
2084
|
+
this.buffer = newArray.buffer;
|
|
2085
|
+
this.length = newLength;
|
|
2086
|
+
this.byteLength = newLength;
|
|
2087
|
+
this._data = new DataView(this.buffer);
|
|
2088
|
+
}
|
|
2089
|
+
return this;
|
|
2090
|
+
}
|
|
2091
|
+
/**
|
|
2092
|
+
* Read a byte and return false if the byte's value is 0, or true otherwise.
|
|
2093
|
+
* Moves pointer forward by one byte.
|
|
2094
|
+
* @returns The read boolean.
|
|
2095
|
+
*/
|
|
2096
|
+
readBoolean() {
|
|
2097
|
+
return this.readUint8() !== 0;
|
|
2098
|
+
}
|
|
2099
|
+
/**
|
|
2100
|
+
* Read a signed 8-bit integer and move pointer forward by 1 byte.
|
|
2101
|
+
* @returns The read byte.
|
|
2102
|
+
*/
|
|
2103
|
+
readInt8() {
|
|
2104
|
+
return this._data.getInt8(this.offset++);
|
|
2105
|
+
}
|
|
2106
|
+
/**
|
|
2107
|
+
* Read an unsigned 8-bit integer and move pointer forward by 1 byte.
|
|
2108
|
+
* @returns The read byte.
|
|
2109
|
+
*/
|
|
2110
|
+
readUint8() {
|
|
2111
|
+
return this._data.getUint8(this.offset++);
|
|
2112
|
+
}
|
|
2113
|
+
/**
|
|
2114
|
+
* Alias for {@link IOBuffer#readUint8}.
|
|
2115
|
+
* @returns The read byte.
|
|
2116
|
+
*/
|
|
2117
|
+
readByte() {
|
|
2118
|
+
return this.readUint8();
|
|
2119
|
+
}
|
|
2120
|
+
/**
|
|
2121
|
+
* Read `n` bytes and move pointer forward by `n` bytes.
|
|
2122
|
+
* @param n - Number of bytes to read.
|
|
2123
|
+
* @returns The read bytes.
|
|
2124
|
+
*/
|
|
2125
|
+
readBytes(n = 1) {
|
|
2126
|
+
return this.readArray(n, "uint8");
|
|
2127
|
+
}
|
|
2128
|
+
/**
|
|
2129
|
+
* Creates an array of corresponding to the type `type` and size `size`.
|
|
2130
|
+
* For example, type `uint8` will create a `Uint8Array`.
|
|
2131
|
+
* @param size - size of the resulting array
|
|
2132
|
+
* @param type - number type of elements to read
|
|
2133
|
+
* @returns The read array.
|
|
2134
|
+
*/
|
|
2135
|
+
readArray(size, type) {
|
|
2136
|
+
const bytes = typedArrays[type].BYTES_PER_ELEMENT * size;
|
|
2137
|
+
const offset = this.byteOffset + this.offset;
|
|
2138
|
+
const slice = this.buffer.slice(offset, offset + bytes);
|
|
2139
|
+
if (this.littleEndian === hostBigEndian && type !== "uint8" && type !== "int8") {
|
|
2140
|
+
const slice2 = new Uint8Array(this.buffer.slice(offset, offset + bytes));
|
|
2141
|
+
slice2.reverse();
|
|
2142
|
+
const returnArray2 = new typedArrays[type](slice2.buffer);
|
|
2143
|
+
this.offset += bytes;
|
|
2144
|
+
returnArray2.reverse();
|
|
2145
|
+
return returnArray2;
|
|
2146
|
+
}
|
|
2147
|
+
const returnArray = new typedArrays[type](slice);
|
|
2148
|
+
this.offset += bytes;
|
|
2149
|
+
return returnArray;
|
|
2150
|
+
}
|
|
2151
|
+
/**
|
|
2152
|
+
* Read a 16-bit signed integer and move pointer forward by 2 bytes.
|
|
2153
|
+
* @returns The read value.
|
|
2154
|
+
*/
|
|
2155
|
+
readInt16() {
|
|
2156
|
+
const value = this._data.getInt16(this.offset, this.littleEndian);
|
|
2157
|
+
this.offset += 2;
|
|
2158
|
+
return value;
|
|
2159
|
+
}
|
|
2160
|
+
/**
|
|
2161
|
+
* Read a 16-bit unsigned integer and move pointer forward by 2 bytes.
|
|
2162
|
+
* @returns The read value.
|
|
2163
|
+
*/
|
|
2164
|
+
readUint16() {
|
|
2165
|
+
const value = this._data.getUint16(this.offset, this.littleEndian);
|
|
2166
|
+
this.offset += 2;
|
|
2167
|
+
return value;
|
|
2168
|
+
}
|
|
2169
|
+
/**
|
|
2170
|
+
* Read a 32-bit signed integer and move pointer forward by 4 bytes.
|
|
2171
|
+
* @returns The read value.
|
|
2172
|
+
*/
|
|
2173
|
+
readInt32() {
|
|
2174
|
+
const value = this._data.getInt32(this.offset, this.littleEndian);
|
|
2175
|
+
this.offset += 4;
|
|
2176
|
+
return value;
|
|
2177
|
+
}
|
|
2178
|
+
/**
|
|
2179
|
+
* Read a 32-bit unsigned integer and move pointer forward by 4 bytes.
|
|
2180
|
+
* @returns The read value.
|
|
2181
|
+
*/
|
|
2182
|
+
readUint32() {
|
|
2183
|
+
const value = this._data.getUint32(this.offset, this.littleEndian);
|
|
2184
|
+
this.offset += 4;
|
|
2185
|
+
return value;
|
|
2186
|
+
}
|
|
2187
|
+
/**
|
|
2188
|
+
* Read a 32-bit floating number and move pointer forward by 4 bytes.
|
|
2189
|
+
* @returns The read value.
|
|
2190
|
+
*/
|
|
2191
|
+
readFloat32() {
|
|
2192
|
+
const value = this._data.getFloat32(this.offset, this.littleEndian);
|
|
2193
|
+
this.offset += 4;
|
|
2194
|
+
return value;
|
|
2195
|
+
}
|
|
2196
|
+
/**
|
|
2197
|
+
* Read a 64-bit floating number and move pointer forward by 8 bytes.
|
|
2198
|
+
* @returns The read value.
|
|
2199
|
+
*/
|
|
2200
|
+
readFloat64() {
|
|
2201
|
+
const value = this._data.getFloat64(this.offset, this.littleEndian);
|
|
2202
|
+
this.offset += 8;
|
|
2203
|
+
return value;
|
|
2204
|
+
}
|
|
2205
|
+
/**
|
|
2206
|
+
* Read a 64-bit signed integer number and move pointer forward by 8 bytes.
|
|
2207
|
+
* @returns The read value.
|
|
2208
|
+
*/
|
|
2209
|
+
readBigInt64() {
|
|
2210
|
+
const value = this._data.getBigInt64(this.offset, this.littleEndian);
|
|
2211
|
+
this.offset += 8;
|
|
2212
|
+
return value;
|
|
2213
|
+
}
|
|
2214
|
+
/**
|
|
2215
|
+
* Read a 64-bit unsigned integer number and move pointer forward by 8 bytes.
|
|
2216
|
+
* @returns The read value.
|
|
2217
|
+
*/
|
|
2218
|
+
readBigUint64() {
|
|
2219
|
+
const value = this._data.getBigUint64(this.offset, this.littleEndian);
|
|
2220
|
+
this.offset += 8;
|
|
2221
|
+
return value;
|
|
2222
|
+
}
|
|
2223
|
+
/**
|
|
2224
|
+
* Read a 1-byte ASCII character and move pointer forward by 1 byte.
|
|
2225
|
+
* @returns The read character.
|
|
2226
|
+
*/
|
|
2227
|
+
readChar() {
|
|
2228
|
+
return String.fromCharCode(this.readInt8());
|
|
2229
|
+
}
|
|
2230
|
+
/**
|
|
2231
|
+
* Read `n` 1-byte ASCII characters and move pointer forward by `n` bytes.
|
|
2232
|
+
* @param n - Number of characters to read.
|
|
2233
|
+
* @returns The read characters.
|
|
2234
|
+
*/
|
|
2235
|
+
readChars(n = 1) {
|
|
2236
|
+
let result = "";
|
|
2237
|
+
for (let i = 0; i < n; i++) {
|
|
2238
|
+
result += this.readChar();
|
|
2239
|
+
}
|
|
2240
|
+
return result;
|
|
2241
|
+
}
|
|
2242
|
+
/**
|
|
2243
|
+
* Read the next `n` bytes, return a UTF-8 decoded string and move pointer
|
|
2244
|
+
* forward by `n` bytes.
|
|
2245
|
+
* @param n - Number of bytes to read.
|
|
2246
|
+
* @returns The decoded string.
|
|
2247
|
+
*/
|
|
2248
|
+
readUtf8(n = 1) {
|
|
2249
|
+
return decode(this.readBytes(n));
|
|
2250
|
+
}
|
|
2251
|
+
/**
|
|
2252
|
+
* Read the next `n` bytes, return a string decoded with `encoding` and move pointer
|
|
2253
|
+
* forward by `n` bytes.
|
|
2254
|
+
* If no encoding is passed, the function is equivalent to @see {@link IOBuffer#readUtf8}
|
|
2255
|
+
* @param n - Number of bytes to read.
|
|
2256
|
+
* @param encoding - The encoding to use. Default is 'utf8'.
|
|
2257
|
+
* @returns The decoded string.
|
|
2258
|
+
*/
|
|
2259
|
+
decodeText(n = 1, encoding = "utf8") {
|
|
2260
|
+
return decode(this.readBytes(n), encoding);
|
|
2261
|
+
}
|
|
2262
|
+
/**
|
|
2263
|
+
* Write 0xff if the passed value is truthy, 0x00 otherwise and move pointer
|
|
2264
|
+
* forward by 1 byte.
|
|
2265
|
+
* @param value - The value to write.
|
|
2266
|
+
* @returns This.
|
|
2267
|
+
*/
|
|
2268
|
+
writeBoolean(value) {
|
|
2269
|
+
this.writeUint8(value ? 255 : 0);
|
|
2270
|
+
return this;
|
|
2271
|
+
}
|
|
2272
|
+
/**
|
|
2273
|
+
* Write `value` as an 8-bit signed integer and move pointer forward by 1 byte.
|
|
2274
|
+
* @param value - The value to write.
|
|
2275
|
+
* @returns This.
|
|
2276
|
+
*/
|
|
2277
|
+
writeInt8(value) {
|
|
2278
|
+
this.ensureAvailable(1);
|
|
2279
|
+
this._data.setInt8(this.offset++, value);
|
|
2280
|
+
this._updateLastWrittenByte();
|
|
2281
|
+
return this;
|
|
2282
|
+
}
|
|
2283
|
+
/**
|
|
2284
|
+
* Write `value` as an 8-bit unsigned integer and move pointer forward by 1
|
|
2285
|
+
* byte.
|
|
2286
|
+
* @param value - The value to write.
|
|
2287
|
+
* @returns This.
|
|
2288
|
+
*/
|
|
2289
|
+
writeUint8(value) {
|
|
2290
|
+
this.ensureAvailable(1);
|
|
2291
|
+
this._data.setUint8(this.offset++, value);
|
|
2292
|
+
this._updateLastWrittenByte();
|
|
2293
|
+
return this;
|
|
2294
|
+
}
|
|
2295
|
+
/**
|
|
2296
|
+
* An alias for {@link IOBuffer#writeUint8}.
|
|
2297
|
+
* @param value - The value to write.
|
|
2298
|
+
* @returns This.
|
|
2299
|
+
*/
|
|
2300
|
+
writeByte(value) {
|
|
2301
|
+
return this.writeUint8(value);
|
|
2302
|
+
}
|
|
2303
|
+
/**
|
|
2304
|
+
* Write all elements of `bytes` as uint8 values and move pointer forward by
|
|
2305
|
+
* `bytes.length` bytes.
|
|
2306
|
+
* @param bytes - The array of bytes to write.
|
|
2307
|
+
* @returns This.
|
|
2308
|
+
*/
|
|
2309
|
+
writeBytes(bytes) {
|
|
2310
|
+
this.ensureAvailable(bytes.length);
|
|
2311
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
2312
|
+
this._data.setUint8(this.offset++, bytes[i]);
|
|
2313
|
+
}
|
|
2314
|
+
this._updateLastWrittenByte();
|
|
2315
|
+
return this;
|
|
2316
|
+
}
|
|
2317
|
+
/**
|
|
2318
|
+
* Write `value` as a 16-bit signed integer and move pointer forward by 2
|
|
2319
|
+
* bytes.
|
|
2320
|
+
* @param value - The value to write.
|
|
2321
|
+
* @returns This.
|
|
2322
|
+
*/
|
|
2323
|
+
writeInt16(value) {
|
|
2324
|
+
this.ensureAvailable(2);
|
|
2325
|
+
this._data.setInt16(this.offset, value, this.littleEndian);
|
|
2326
|
+
this.offset += 2;
|
|
2327
|
+
this._updateLastWrittenByte();
|
|
2328
|
+
return this;
|
|
2329
|
+
}
|
|
2330
|
+
/**
|
|
2331
|
+
* Write `value` as a 16-bit unsigned integer and move pointer forward by 2
|
|
2332
|
+
* bytes.
|
|
2333
|
+
* @param value - The value to write.
|
|
2334
|
+
* @returns This.
|
|
2335
|
+
*/
|
|
2336
|
+
writeUint16(value) {
|
|
2337
|
+
this.ensureAvailable(2);
|
|
2338
|
+
this._data.setUint16(this.offset, value, this.littleEndian);
|
|
2339
|
+
this.offset += 2;
|
|
2340
|
+
this._updateLastWrittenByte();
|
|
2341
|
+
return this;
|
|
2342
|
+
}
|
|
2343
|
+
/**
|
|
2344
|
+
* Write `value` as a 32-bit signed integer and move pointer forward by 4
|
|
2345
|
+
* bytes.
|
|
2346
|
+
* @param value - The value to write.
|
|
2347
|
+
* @returns This.
|
|
2348
|
+
*/
|
|
2349
|
+
writeInt32(value) {
|
|
2350
|
+
this.ensureAvailable(4);
|
|
2351
|
+
this._data.setInt32(this.offset, value, this.littleEndian);
|
|
2352
|
+
this.offset += 4;
|
|
2353
|
+
this._updateLastWrittenByte();
|
|
2354
|
+
return this;
|
|
2355
|
+
}
|
|
2356
|
+
/**
|
|
2357
|
+
* Write `value` as a 32-bit unsigned integer and move pointer forward by 4
|
|
2358
|
+
* bytes.
|
|
2359
|
+
* @param value - The value to write.
|
|
2360
|
+
* @returns This.
|
|
2361
|
+
*/
|
|
2362
|
+
writeUint32(value) {
|
|
2363
|
+
this.ensureAvailable(4);
|
|
2364
|
+
this._data.setUint32(this.offset, value, this.littleEndian);
|
|
2365
|
+
this.offset += 4;
|
|
2366
|
+
this._updateLastWrittenByte();
|
|
2367
|
+
return this;
|
|
2368
|
+
}
|
|
2369
|
+
/**
|
|
2370
|
+
* Write `value` as a 32-bit floating number and move pointer forward by 4
|
|
2371
|
+
* bytes.
|
|
2372
|
+
* @param value - The value to write.
|
|
2373
|
+
* @returns This.
|
|
2374
|
+
*/
|
|
2375
|
+
writeFloat32(value) {
|
|
2376
|
+
this.ensureAvailable(4);
|
|
2377
|
+
this._data.setFloat32(this.offset, value, this.littleEndian);
|
|
2378
|
+
this.offset += 4;
|
|
2379
|
+
this._updateLastWrittenByte();
|
|
2380
|
+
return this;
|
|
2381
|
+
}
|
|
2382
|
+
/**
|
|
2383
|
+
* Write `value` as a 64-bit floating number and move pointer forward by 8
|
|
2384
|
+
* bytes.
|
|
2385
|
+
* @param value - The value to write.
|
|
2386
|
+
* @returns This.
|
|
2387
|
+
*/
|
|
2388
|
+
writeFloat64(value) {
|
|
2389
|
+
this.ensureAvailable(8);
|
|
2390
|
+
this._data.setFloat64(this.offset, value, this.littleEndian);
|
|
2391
|
+
this.offset += 8;
|
|
2392
|
+
this._updateLastWrittenByte();
|
|
2393
|
+
return this;
|
|
2394
|
+
}
|
|
2395
|
+
/**
|
|
2396
|
+
* Write `value` as a 64-bit signed bigint and move pointer forward by 8
|
|
2397
|
+
* bytes.
|
|
2398
|
+
* @param value - The value to write.
|
|
2399
|
+
* @returns This.
|
|
2400
|
+
*/
|
|
2401
|
+
writeBigInt64(value) {
|
|
2402
|
+
this.ensureAvailable(8);
|
|
2403
|
+
this._data.setBigInt64(this.offset, value, this.littleEndian);
|
|
2404
|
+
this.offset += 8;
|
|
2405
|
+
this._updateLastWrittenByte();
|
|
2406
|
+
return this;
|
|
2407
|
+
}
|
|
2408
|
+
/**
|
|
2409
|
+
* Write `value` as a 64-bit unsigned bigint and move pointer forward by 8
|
|
2410
|
+
* bytes.
|
|
2411
|
+
* @param value - The value to write.
|
|
2412
|
+
* @returns This.
|
|
2413
|
+
*/
|
|
2414
|
+
writeBigUint64(value) {
|
|
2415
|
+
this.ensureAvailable(8);
|
|
2416
|
+
this._data.setBigUint64(this.offset, value, this.littleEndian);
|
|
2417
|
+
this.offset += 8;
|
|
2418
|
+
this._updateLastWrittenByte();
|
|
2419
|
+
return this;
|
|
2420
|
+
}
|
|
2421
|
+
/**
|
|
2422
|
+
* Write the charCode of `str`'s first character as an 8-bit unsigned integer
|
|
2423
|
+
* and move pointer forward by 1 byte.
|
|
2424
|
+
* @param str - The character to write.
|
|
2425
|
+
* @returns This.
|
|
2426
|
+
*/
|
|
2427
|
+
writeChar(str) {
|
|
2428
|
+
return this.writeUint8(str.charCodeAt(0));
|
|
2429
|
+
}
|
|
2430
|
+
/**
|
|
2431
|
+
* Write the charCodes of all `str`'s characters as 8-bit unsigned integers
|
|
2432
|
+
* and move pointer forward by `str.length` bytes.
|
|
2433
|
+
* @param str - The characters to write.
|
|
2434
|
+
* @returns This.
|
|
2435
|
+
*/
|
|
2436
|
+
writeChars(str) {
|
|
2437
|
+
for (let i = 0; i < str.length; i++) {
|
|
2438
|
+
this.writeUint8(str.charCodeAt(i));
|
|
2439
|
+
}
|
|
2440
|
+
return this;
|
|
2441
|
+
}
|
|
2442
|
+
/**
|
|
2443
|
+
* UTF-8 encode and write `str` to the current pointer offset and move pointer
|
|
2444
|
+
* forward according to the encoded length.
|
|
2445
|
+
* @param str - The string to write.
|
|
2446
|
+
* @returns This.
|
|
2447
|
+
*/
|
|
2448
|
+
writeUtf8(str) {
|
|
2449
|
+
return this.writeBytes(encode(str));
|
|
2450
|
+
}
|
|
2451
|
+
/**
|
|
2452
|
+
* Export a Uint8Array view of the internal buffer.
|
|
2453
|
+
* The view starts at the byte offset and its length
|
|
2454
|
+
* is calculated to stop at the last written byte or the original length.
|
|
2455
|
+
* @returns A new Uint8Array view.
|
|
2456
|
+
*/
|
|
2457
|
+
toArray() {
|
|
2458
|
+
return new Uint8Array(this.buffer, this.byteOffset, this.lastWrittenByte);
|
|
2459
|
+
}
|
|
2460
|
+
/**
|
|
2461
|
+
* Get the total number of bytes written so far, regardless of the current offset.
|
|
2462
|
+
* @returns - Total number of bytes.
|
|
2463
|
+
*/
|
|
2464
|
+
getWrittenByteLength() {
|
|
2465
|
+
return this.lastWrittenByte - this.byteOffset;
|
|
2466
|
+
}
|
|
2467
|
+
/**
|
|
2468
|
+
* Update the last written byte offset
|
|
2469
|
+
* @private
|
|
2470
|
+
*/
|
|
2471
|
+
_updateLastWrittenByte() {
|
|
2472
|
+
if (this.offset > this.lastWrittenByte) {
|
|
2473
|
+
this.lastWrittenByte = this.offset;
|
|
2474
|
+
}
|
|
2475
|
+
}
|
|
2476
|
+
};
|
|
2477
|
+
|
|
2478
|
+
// node_modules/.pnpm/fast-png@8.0.0/node_modules/fast-png/lib/helpers/crc.js
|
|
2479
|
+
var crcTable = [];
|
|
2480
|
+
for (let n = 0; n < 256; n++) {
|
|
2481
|
+
let c = n;
|
|
2482
|
+
for (let k = 0; k < 8; k++) {
|
|
2483
|
+
if (c & 1) {
|
|
2484
|
+
c = 3988292384 ^ c >>> 1;
|
|
2485
|
+
} else {
|
|
2486
|
+
c = c >>> 1;
|
|
2487
|
+
}
|
|
2488
|
+
}
|
|
2489
|
+
crcTable[n] = c;
|
|
2490
|
+
}
|
|
2491
|
+
var initialCrc = 4294967295;
|
|
2492
|
+
function updateCrc(currentCrc, data, length) {
|
|
2493
|
+
let c = currentCrc;
|
|
2494
|
+
for (let n = 0; n < length; n++) {
|
|
2495
|
+
c = crcTable[(c ^ data[n]) & 255] ^ c >>> 8;
|
|
2496
|
+
}
|
|
2497
|
+
return c;
|
|
2498
|
+
}
|
|
2499
|
+
function crc(data, length) {
|
|
2500
|
+
return (updateCrc(initialCrc, data, length) ^ initialCrc) >>> 0;
|
|
2501
|
+
}
|
|
2502
|
+
function checkCrc(buffer, crcLength, chunkName) {
|
|
2503
|
+
const expectedCrc = buffer.readUint32();
|
|
2504
|
+
const actualCrc = crc(new Uint8Array(buffer.buffer, buffer.byteOffset + buffer.offset - crcLength - 4, crcLength), crcLength);
|
|
2505
|
+
if (actualCrc !== expectedCrc) {
|
|
2506
|
+
throw new Error(`CRC mismatch for chunk ${chunkName}. Expected ${expectedCrc}, found ${actualCrc}`);
|
|
2507
|
+
}
|
|
2508
|
+
}
|
|
2509
|
+
function writeCrc(buffer, length) {
|
|
2510
|
+
buffer.writeUint32(crc(new Uint8Array(buffer.buffer, buffer.byteOffset + buffer.offset - length, length), length));
|
|
2511
|
+
}
|
|
2512
|
+
|
|
2513
|
+
// node_modules/.pnpm/fast-png@8.0.0/node_modules/fast-png/lib/helpers/unfilter.js
|
|
2514
|
+
function unfilterNone(currentLine, newLine, bytesPerLine) {
|
|
2515
|
+
for (let i = 0; i < bytesPerLine; i++) {
|
|
2516
|
+
newLine[i] = currentLine[i];
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
function unfilterSub(currentLine, newLine, bytesPerLine, bytesPerPixel) {
|
|
2520
|
+
let i = 0;
|
|
2521
|
+
for (; i < bytesPerPixel; i++) {
|
|
2522
|
+
newLine[i] = currentLine[i];
|
|
2523
|
+
}
|
|
2524
|
+
for (; i < bytesPerLine; i++) {
|
|
2525
|
+
newLine[i] = currentLine[i] + newLine[i - bytesPerPixel] & 255;
|
|
2526
|
+
}
|
|
2527
|
+
}
|
|
2528
|
+
function unfilterUp(currentLine, newLine, prevLine, bytesPerLine) {
|
|
2529
|
+
let i = 0;
|
|
2530
|
+
if (prevLine.length === 0) {
|
|
2531
|
+
for (; i < bytesPerLine; i++) {
|
|
2532
|
+
newLine[i] = currentLine[i];
|
|
2533
|
+
}
|
|
2534
|
+
} else {
|
|
2535
|
+
for (; i < bytesPerLine; i++) {
|
|
2536
|
+
newLine[i] = currentLine[i] + prevLine[i] & 255;
|
|
2537
|
+
}
|
|
2538
|
+
}
|
|
2539
|
+
}
|
|
2540
|
+
function unfilterAverage(currentLine, newLine, prevLine, bytesPerLine, bytesPerPixel) {
|
|
2541
|
+
let i = 0;
|
|
2542
|
+
if (prevLine.length === 0) {
|
|
2543
|
+
for (; i < bytesPerPixel; i++) {
|
|
2544
|
+
newLine[i] = currentLine[i];
|
|
2545
|
+
}
|
|
2546
|
+
for (; i < bytesPerLine; i++) {
|
|
2547
|
+
newLine[i] = currentLine[i] + (newLine[i - bytesPerPixel] >> 1) & 255;
|
|
2548
|
+
}
|
|
2549
|
+
} else {
|
|
2550
|
+
for (; i < bytesPerPixel; i++) {
|
|
2551
|
+
newLine[i] = currentLine[i] + (prevLine[i] >> 1) & 255;
|
|
2552
|
+
}
|
|
2553
|
+
for (; i < bytesPerLine; i++) {
|
|
2554
|
+
newLine[i] = currentLine[i] + (newLine[i - bytesPerPixel] + prevLine[i] >> 1) & 255;
|
|
2555
|
+
}
|
|
2556
|
+
}
|
|
2557
|
+
}
|
|
2558
|
+
function unfilterPaeth(currentLine, newLine, prevLine, bytesPerLine, bytesPerPixel) {
|
|
2559
|
+
let i = 0;
|
|
2560
|
+
if (prevLine.length === 0) {
|
|
2561
|
+
for (; i < bytesPerPixel; i++) {
|
|
2562
|
+
newLine[i] = currentLine[i];
|
|
2563
|
+
}
|
|
2564
|
+
for (; i < bytesPerLine; i++) {
|
|
2565
|
+
newLine[i] = currentLine[i] + newLine[i - bytesPerPixel] & 255;
|
|
2566
|
+
}
|
|
2567
|
+
} else {
|
|
2568
|
+
for (; i < bytesPerPixel; i++) {
|
|
2569
|
+
newLine[i] = currentLine[i] + prevLine[i] & 255;
|
|
2570
|
+
}
|
|
2571
|
+
for (; i < bytesPerLine; i++) {
|
|
2572
|
+
newLine[i] = currentLine[i] + paethPredictor(newLine[i - bytesPerPixel], prevLine[i], prevLine[i - bytesPerPixel]) & 255;
|
|
2573
|
+
}
|
|
2574
|
+
}
|
|
2575
|
+
}
|
|
2576
|
+
function paethPredictor(a, b, c) {
|
|
2577
|
+
const p = a + b - c;
|
|
2578
|
+
const pa = Math.abs(p - a);
|
|
2579
|
+
const pb = Math.abs(p - b);
|
|
2580
|
+
const pc = Math.abs(p - c);
|
|
2581
|
+
if (pa <= pb && pa <= pc)
|
|
2582
|
+
return a;
|
|
2583
|
+
else if (pb <= pc)
|
|
2584
|
+
return b;
|
|
2585
|
+
else
|
|
2586
|
+
return c;
|
|
2587
|
+
}
|
|
2588
|
+
|
|
2589
|
+
// node_modules/.pnpm/fast-png@8.0.0/node_modules/fast-png/lib/helpers/apply_unfilter.js
|
|
2590
|
+
function applyUnfilter(filterType, currentLine, newLine, prevLine, passLineBytes, bytesPerPixel) {
|
|
2591
|
+
switch (filterType) {
|
|
2592
|
+
case 0:
|
|
2593
|
+
unfilterNone(currentLine, newLine, passLineBytes);
|
|
2594
|
+
break;
|
|
2595
|
+
case 1:
|
|
2596
|
+
unfilterSub(currentLine, newLine, passLineBytes, bytesPerPixel);
|
|
2597
|
+
break;
|
|
2598
|
+
case 2:
|
|
2599
|
+
unfilterUp(currentLine, newLine, prevLine, passLineBytes);
|
|
2600
|
+
break;
|
|
2601
|
+
case 3:
|
|
2602
|
+
unfilterAverage(currentLine, newLine, prevLine, passLineBytes, bytesPerPixel);
|
|
2603
|
+
break;
|
|
2604
|
+
case 4:
|
|
2605
|
+
unfilterPaeth(currentLine, newLine, prevLine, passLineBytes, bytesPerPixel);
|
|
2606
|
+
break;
|
|
2607
|
+
default:
|
|
2608
|
+
throw new Error(`Unsupported filter: ${filterType}`);
|
|
2609
|
+
}
|
|
2610
|
+
}
|
|
2611
|
+
|
|
2612
|
+
// node_modules/.pnpm/fast-png@8.0.0/node_modules/fast-png/lib/helpers/decode_interlace_adam7.js
|
|
2613
|
+
var uint16 = new Uint16Array([255]);
|
|
2614
|
+
var uint8 = new Uint8Array(uint16.buffer);
|
|
2615
|
+
var osIsLittleEndian = uint8[0] === 255;
|
|
2616
|
+
function decodeInterlaceAdam7(params) {
|
|
2617
|
+
const { data, width, height, channels, depth } = params;
|
|
2618
|
+
const passes = [
|
|
2619
|
+
{ x: 0, y: 0, xStep: 8, yStep: 8 },
|
|
2620
|
+
// Pass 1
|
|
2621
|
+
{ x: 4, y: 0, xStep: 8, yStep: 8 },
|
|
2622
|
+
// Pass 2
|
|
2623
|
+
{ x: 0, y: 4, xStep: 4, yStep: 8 },
|
|
2624
|
+
// Pass 3
|
|
2625
|
+
{ x: 2, y: 0, xStep: 4, yStep: 4 },
|
|
2626
|
+
// Pass 4
|
|
2627
|
+
{ x: 0, y: 2, xStep: 2, yStep: 4 },
|
|
2628
|
+
// Pass 5
|
|
2629
|
+
{ x: 1, y: 0, xStep: 2, yStep: 2 },
|
|
2630
|
+
// Pass 6
|
|
2631
|
+
{ x: 0, y: 1, xStep: 1, yStep: 2 }
|
|
2632
|
+
// Pass 7
|
|
2633
|
+
];
|
|
2634
|
+
const bytesPerPixel = Math.ceil(depth / 8) * channels;
|
|
2635
|
+
const resultData = new Uint8Array(height * width * bytesPerPixel);
|
|
2636
|
+
let offset = 0;
|
|
2637
|
+
for (let passIndex = 0; passIndex < 7; passIndex++) {
|
|
2638
|
+
const pass = passes[passIndex];
|
|
2639
|
+
const passWidth = Math.ceil((width - pass.x) / pass.xStep);
|
|
2640
|
+
const passHeight = Math.ceil((height - pass.y) / pass.yStep);
|
|
2641
|
+
if (passWidth <= 0 || passHeight <= 0)
|
|
2642
|
+
continue;
|
|
2643
|
+
const passLineBytes = passWidth * bytesPerPixel;
|
|
2644
|
+
const prevLine = new Uint8Array(passLineBytes);
|
|
2645
|
+
for (let y = 0; y < passHeight; y++) {
|
|
2646
|
+
const filterType = data[offset++];
|
|
2647
|
+
const currentLine = data.subarray(offset, offset + passLineBytes);
|
|
2648
|
+
offset += passLineBytes;
|
|
2649
|
+
const newLine = new Uint8Array(passLineBytes);
|
|
2650
|
+
applyUnfilter(filterType, currentLine, newLine, prevLine, passLineBytes, bytesPerPixel);
|
|
2651
|
+
prevLine.set(newLine);
|
|
2652
|
+
for (let x = 0; x < passWidth; x++) {
|
|
2653
|
+
const outputX = pass.x + x * pass.xStep;
|
|
2654
|
+
const outputY = pass.y + y * pass.yStep;
|
|
2655
|
+
if (outputX >= width || outputY >= height)
|
|
2656
|
+
continue;
|
|
2657
|
+
for (let i = 0; i < bytesPerPixel; i++) {
|
|
2658
|
+
resultData[(outputY * width + outputX) * bytesPerPixel + i] = newLine[x * bytesPerPixel + i];
|
|
2659
|
+
}
|
|
2660
|
+
}
|
|
2661
|
+
}
|
|
2662
|
+
}
|
|
2663
|
+
if (depth === 16) {
|
|
2664
|
+
const uint16Data = new Uint16Array(resultData.buffer);
|
|
2665
|
+
if (osIsLittleEndian) {
|
|
2666
|
+
for (let k = 0; k < uint16Data.length; k++) {
|
|
2667
|
+
uint16Data[k] = swap16(uint16Data[k]);
|
|
2668
|
+
}
|
|
2669
|
+
}
|
|
2670
|
+
return uint16Data;
|
|
2671
|
+
} else {
|
|
2672
|
+
return resultData;
|
|
2673
|
+
}
|
|
2674
|
+
}
|
|
2675
|
+
function swap16(val) {
|
|
2676
|
+
return (val & 255) << 8 | val >> 8 & 255;
|
|
2677
|
+
}
|
|
2678
|
+
|
|
2679
|
+
// node_modules/.pnpm/fast-png@8.0.0/node_modules/fast-png/lib/helpers/decode_interlace_null.js
|
|
2680
|
+
var uint162 = new Uint16Array([255]);
|
|
2681
|
+
var uint82 = new Uint8Array(uint162.buffer);
|
|
2682
|
+
var osIsLittleEndian2 = uint82[0] === 255;
|
|
2683
|
+
var empty = new Uint8Array(0);
|
|
2684
|
+
function decodeInterlaceNull(params) {
|
|
2685
|
+
const { data, width, height, channels, depth } = params;
|
|
2686
|
+
const bytesPerPixel = Math.ceil(depth / 8) * channels;
|
|
2687
|
+
const bytesPerLine = Math.ceil(depth / 8 * channels * width);
|
|
2688
|
+
const newData = new Uint8Array(height * bytesPerLine);
|
|
2689
|
+
let prevLine = empty;
|
|
2690
|
+
let offset = 0;
|
|
2691
|
+
let currentLine;
|
|
2692
|
+
let newLine;
|
|
2693
|
+
for (let i = 0; i < height; i++) {
|
|
2694
|
+
currentLine = data.subarray(offset + 1, offset + 1 + bytesPerLine);
|
|
2695
|
+
newLine = newData.subarray(i * bytesPerLine, (i + 1) * bytesPerLine);
|
|
2696
|
+
switch (data[offset]) {
|
|
2697
|
+
case 0:
|
|
2698
|
+
unfilterNone(currentLine, newLine, bytesPerLine);
|
|
2699
|
+
break;
|
|
2700
|
+
case 1:
|
|
2701
|
+
unfilterSub(currentLine, newLine, bytesPerLine, bytesPerPixel);
|
|
2702
|
+
break;
|
|
2703
|
+
case 2:
|
|
2704
|
+
unfilterUp(currentLine, newLine, prevLine, bytesPerLine);
|
|
2705
|
+
break;
|
|
2706
|
+
case 3:
|
|
2707
|
+
unfilterAverage(currentLine, newLine, prevLine, bytesPerLine, bytesPerPixel);
|
|
2708
|
+
break;
|
|
2709
|
+
case 4:
|
|
2710
|
+
unfilterPaeth(currentLine, newLine, prevLine, bytesPerLine, bytesPerPixel);
|
|
2711
|
+
break;
|
|
2712
|
+
default:
|
|
2713
|
+
throw new Error(`Unsupported filter: ${data[offset]}`);
|
|
2714
|
+
}
|
|
2715
|
+
prevLine = newLine;
|
|
2716
|
+
offset += bytesPerLine + 1;
|
|
2717
|
+
}
|
|
2718
|
+
if (depth === 16) {
|
|
2719
|
+
const uint16Data = new Uint16Array(newData.buffer);
|
|
2720
|
+
if (osIsLittleEndian2) {
|
|
2721
|
+
for (let k = 0; k < uint16Data.length; k++) {
|
|
2722
|
+
uint16Data[k] = swap162(uint16Data[k]);
|
|
2723
|
+
}
|
|
2724
|
+
}
|
|
2725
|
+
return uint16Data;
|
|
2726
|
+
} else {
|
|
2727
|
+
return newData;
|
|
2728
|
+
}
|
|
2729
|
+
}
|
|
2730
|
+
function swap162(val) {
|
|
2731
|
+
return (val & 255) << 8 | val >> 8 & 255;
|
|
2732
|
+
}
|
|
2733
|
+
|
|
2734
|
+
// node_modules/.pnpm/fast-png@8.0.0/node_modules/fast-png/lib/helpers/signature.js
|
|
2735
|
+
var pngSignature = Uint8Array.of(137, 80, 78, 71, 13, 10, 26, 10);
|
|
2736
|
+
function writeSignature(buffer) {
|
|
2737
|
+
buffer.writeBytes(pngSignature);
|
|
2738
|
+
}
|
|
2739
|
+
function checkSignature(buffer) {
|
|
2740
|
+
if (!hasPngSignature(buffer.readBytes(pngSignature.length))) {
|
|
2741
|
+
throw new Error("wrong PNG signature");
|
|
2742
|
+
}
|
|
2743
|
+
}
|
|
2744
|
+
function hasPngSignature(array) {
|
|
2745
|
+
if (array.length < pngSignature.length) {
|
|
2746
|
+
return false;
|
|
2747
|
+
}
|
|
2748
|
+
for (let i = 0; i < pngSignature.length; i++) {
|
|
2749
|
+
if (array[i] !== pngSignature[i]) {
|
|
2750
|
+
return false;
|
|
2751
|
+
}
|
|
2752
|
+
}
|
|
2753
|
+
return true;
|
|
2754
|
+
}
|
|
2755
|
+
|
|
2756
|
+
// node_modules/.pnpm/fast-png@8.0.0/node_modules/fast-png/lib/helpers/text.js
|
|
2757
|
+
var textChunkName = "tEXt";
|
|
2758
|
+
var NULL = 0;
|
|
2759
|
+
var latin1Decoder = new TextDecoder("latin1");
|
|
2760
|
+
function validateKeyword(keyword) {
|
|
2761
|
+
validateLatin1(keyword);
|
|
2762
|
+
if (keyword.length === 0 || keyword.length > 79) {
|
|
2763
|
+
throw new Error("keyword length must be between 1 and 79");
|
|
2764
|
+
}
|
|
2765
|
+
}
|
|
2766
|
+
var latin1Regex = /^[\u0000-\u00FF]*$/;
|
|
2767
|
+
function validateLatin1(text) {
|
|
2768
|
+
if (!latin1Regex.test(text)) {
|
|
2769
|
+
throw new Error("invalid latin1 text");
|
|
2770
|
+
}
|
|
2771
|
+
}
|
|
2772
|
+
function decodetEXt(text, buffer, length) {
|
|
2773
|
+
const keyword = readKeyword(buffer);
|
|
2774
|
+
text[keyword] = readLatin1(buffer, length - keyword.length - 1);
|
|
2775
|
+
}
|
|
2776
|
+
function encodetEXt(buffer, keyword, text) {
|
|
2777
|
+
validateKeyword(keyword);
|
|
2778
|
+
validateLatin1(text);
|
|
2779
|
+
const length = keyword.length + 1 + text.length;
|
|
2780
|
+
buffer.writeUint32(length);
|
|
2781
|
+
buffer.writeChars(textChunkName);
|
|
2782
|
+
buffer.writeChars(keyword);
|
|
2783
|
+
buffer.writeByte(NULL);
|
|
2784
|
+
buffer.writeChars(text);
|
|
2785
|
+
writeCrc(buffer, length + 4);
|
|
2786
|
+
}
|
|
2787
|
+
function readKeyword(buffer) {
|
|
2788
|
+
buffer.mark();
|
|
2789
|
+
while (buffer.readByte() !== NULL) {
|
|
2790
|
+
}
|
|
2791
|
+
const end = buffer.offset;
|
|
2792
|
+
buffer.reset();
|
|
2793
|
+
const keyword = latin1Decoder.decode(buffer.readBytes(end - buffer.offset - 1));
|
|
2794
|
+
buffer.skip(1);
|
|
2795
|
+
validateKeyword(keyword);
|
|
2796
|
+
return keyword;
|
|
2797
|
+
}
|
|
2798
|
+
function readLatin1(buffer, length) {
|
|
2799
|
+
return latin1Decoder.decode(buffer.readBytes(length));
|
|
2800
|
+
}
|
|
2801
|
+
|
|
2802
|
+
// node_modules/.pnpm/fast-png@8.0.0/node_modules/fast-png/lib/internal_types.js
|
|
2803
|
+
var ColorType = {
|
|
2804
|
+
UNKNOWN: -1,
|
|
2805
|
+
GREYSCALE: 0,
|
|
2806
|
+
TRUECOLOUR: 2,
|
|
2807
|
+
INDEXED_COLOUR: 3,
|
|
2808
|
+
GREYSCALE_ALPHA: 4,
|
|
2809
|
+
TRUECOLOUR_ALPHA: 6
|
|
2810
|
+
};
|
|
2811
|
+
var CompressionMethod = {
|
|
2812
|
+
UNKNOWN: -1,
|
|
2813
|
+
DEFLATE: 0
|
|
2814
|
+
};
|
|
2815
|
+
var FilterMethod = {
|
|
2816
|
+
UNKNOWN: -1,
|
|
2817
|
+
ADAPTIVE: 0
|
|
2818
|
+
};
|
|
2819
|
+
var InterlaceMethod = {
|
|
2820
|
+
UNKNOWN: -1,
|
|
2821
|
+
NO_INTERLACE: 0,
|
|
2822
|
+
ADAM7: 1
|
|
2823
|
+
};
|
|
2824
|
+
var DisposeOpType = {
|
|
2825
|
+
NONE: 0,
|
|
2826
|
+
BACKGROUND: 1,
|
|
2827
|
+
PREVIOUS: 2
|
|
2828
|
+
};
|
|
2829
|
+
var BlendOpType = {
|
|
2830
|
+
SOURCE: 0,
|
|
2831
|
+
OVER: 1
|
|
2832
|
+
};
|
|
2833
|
+
|
|
2834
|
+
// node_modules/.pnpm/fast-png@8.0.0/node_modules/fast-png/lib/png_decoder.js
|
|
2835
|
+
var PngDecoder = class extends IOBuffer {
|
|
2836
|
+
_checkCrc;
|
|
2837
|
+
_inflator;
|
|
2838
|
+
_png;
|
|
2839
|
+
_apng;
|
|
2840
|
+
_end;
|
|
2841
|
+
_hasPalette;
|
|
2842
|
+
_palette;
|
|
2843
|
+
_hasTransparency;
|
|
2844
|
+
_transparency;
|
|
2845
|
+
_compressionMethod;
|
|
2846
|
+
_filterMethod;
|
|
2847
|
+
_interlaceMethod;
|
|
2848
|
+
_colorType;
|
|
2849
|
+
_isAnimated;
|
|
2850
|
+
_numberOfFrames;
|
|
2851
|
+
_numberOfPlays;
|
|
2852
|
+
_frames;
|
|
2853
|
+
_writingDataChunks;
|
|
2854
|
+
_chunks;
|
|
2855
|
+
_inflatorResult;
|
|
2856
|
+
constructor(data, options = {}) {
|
|
2857
|
+
super(data);
|
|
2858
|
+
const { checkCrc: checkCrc2 = false } = options;
|
|
2859
|
+
this._checkCrc = checkCrc2;
|
|
2860
|
+
this._inflator = new Inflator((chunk, final) => {
|
|
2861
|
+
this._chunks.push(chunk);
|
|
2862
|
+
if (final) {
|
|
2863
|
+
const totalLength = this._chunks.reduce((sum, c) => sum + c.length, 0);
|
|
2864
|
+
this._inflatorResult = new Uint8Array(totalLength);
|
|
2865
|
+
let offset = 0;
|
|
2866
|
+
for (const chunk2 of this._chunks) {
|
|
2867
|
+
this._inflatorResult.set(chunk2, offset);
|
|
2868
|
+
offset += chunk2.length;
|
|
2869
|
+
}
|
|
2870
|
+
this._chunks = [];
|
|
2871
|
+
}
|
|
2872
|
+
});
|
|
2873
|
+
this._chunks = [];
|
|
2874
|
+
this._png = {
|
|
2875
|
+
width: -1,
|
|
2876
|
+
height: -1,
|
|
2877
|
+
channels: -1,
|
|
2878
|
+
data: new Uint8Array(0),
|
|
2879
|
+
depth: 1,
|
|
2880
|
+
text: {}
|
|
2881
|
+
};
|
|
2882
|
+
this._apng = {
|
|
2883
|
+
width: -1,
|
|
2884
|
+
height: -1,
|
|
2885
|
+
channels: -1,
|
|
2886
|
+
depth: 1,
|
|
2887
|
+
numberOfFrames: 1,
|
|
2888
|
+
numberOfPlays: 0,
|
|
2889
|
+
text: {},
|
|
2890
|
+
frames: []
|
|
2891
|
+
};
|
|
2892
|
+
this._end = false;
|
|
2893
|
+
this._hasPalette = false;
|
|
2894
|
+
this._palette = [];
|
|
2895
|
+
this._hasTransparency = false;
|
|
2896
|
+
this._transparency = new Uint16Array(0);
|
|
2897
|
+
this._compressionMethod = CompressionMethod.UNKNOWN;
|
|
2898
|
+
this._filterMethod = FilterMethod.UNKNOWN;
|
|
2899
|
+
this._interlaceMethod = InterlaceMethod.UNKNOWN;
|
|
2900
|
+
this._colorType = ColorType.UNKNOWN;
|
|
2901
|
+
this._isAnimated = false;
|
|
2902
|
+
this._numberOfFrames = 1;
|
|
2903
|
+
this._numberOfPlays = 0;
|
|
2904
|
+
this._frames = [];
|
|
2905
|
+
this._writingDataChunks = false;
|
|
2906
|
+
this._inflatorResult = new Uint8Array(0);
|
|
2907
|
+
this.setBigEndian();
|
|
2908
|
+
}
|
|
2909
|
+
decode() {
|
|
2910
|
+
checkSignature(this);
|
|
2911
|
+
while (!this._end) {
|
|
2912
|
+
const length = this.readUint32();
|
|
2913
|
+
const type = this.readChars(4);
|
|
2914
|
+
this.decodeChunk(length, type);
|
|
2915
|
+
}
|
|
2916
|
+
this._inflator.push(new Uint8Array(0), true);
|
|
2917
|
+
this.decodeImage();
|
|
2918
|
+
return this._png;
|
|
2919
|
+
}
|
|
2920
|
+
decodeApng() {
|
|
2921
|
+
checkSignature(this);
|
|
2922
|
+
while (!this._end) {
|
|
2923
|
+
const length = this.readUint32();
|
|
2924
|
+
const type = this.readChars(4);
|
|
2925
|
+
this.decodeApngChunk(length, type);
|
|
2926
|
+
}
|
|
2927
|
+
this.decodeApngImage();
|
|
2928
|
+
return this._apng;
|
|
2929
|
+
}
|
|
2930
|
+
// https://www.w3.org/TR/PNG/#5Chunk-layout
|
|
2931
|
+
decodeChunk(length, type) {
|
|
2932
|
+
const offset = this.offset;
|
|
2933
|
+
switch (type) {
|
|
2934
|
+
// 11.2 Critical chunks
|
|
2935
|
+
case "IHDR":
|
|
2936
|
+
this.decodeIHDR();
|
|
2937
|
+
break;
|
|
2938
|
+
case "PLTE":
|
|
2939
|
+
this.decodePLTE(length);
|
|
2940
|
+
break;
|
|
2941
|
+
case "IDAT":
|
|
2942
|
+
this.decodeIDAT(length);
|
|
2943
|
+
break;
|
|
2944
|
+
case "IEND":
|
|
2945
|
+
this._end = true;
|
|
2946
|
+
break;
|
|
2947
|
+
// 11.3 Ancillary chunks
|
|
2948
|
+
case "tRNS":
|
|
2949
|
+
this.decodetRNS(length);
|
|
2950
|
+
break;
|
|
2951
|
+
case "iCCP":
|
|
2952
|
+
this.decodeiCCP(length);
|
|
2953
|
+
break;
|
|
2954
|
+
case textChunkName:
|
|
2955
|
+
decodetEXt(this._png.text, this, length);
|
|
2956
|
+
break;
|
|
2957
|
+
case "pHYs":
|
|
2958
|
+
this.decodepHYs();
|
|
2959
|
+
break;
|
|
2960
|
+
default:
|
|
2961
|
+
this.skip(length);
|
|
2962
|
+
break;
|
|
2963
|
+
}
|
|
2964
|
+
if (this.offset - offset !== length) {
|
|
2965
|
+
throw new Error(`Length mismatch while decoding chunk ${type}`);
|
|
2966
|
+
}
|
|
2967
|
+
if (this._checkCrc) {
|
|
2968
|
+
checkCrc(this, length + 4, type);
|
|
2969
|
+
} else {
|
|
2970
|
+
this.skip(4);
|
|
2971
|
+
}
|
|
2972
|
+
}
|
|
2973
|
+
decodeApngChunk(length, type) {
|
|
2974
|
+
const offset = this.offset;
|
|
2975
|
+
if (type !== "fdAT" && type !== "IDAT" && this._writingDataChunks) {
|
|
2976
|
+
this.pushDataToFrame();
|
|
2977
|
+
}
|
|
2978
|
+
switch (type) {
|
|
2979
|
+
case "acTL":
|
|
2980
|
+
this.decodeACTL();
|
|
2981
|
+
break;
|
|
2982
|
+
case "fcTL":
|
|
2983
|
+
this.decodeFCTL();
|
|
2984
|
+
break;
|
|
2985
|
+
case "fdAT":
|
|
2986
|
+
this.decodeFDAT(length);
|
|
2987
|
+
break;
|
|
2988
|
+
default:
|
|
2989
|
+
this.decodeChunk(length, type);
|
|
2990
|
+
this.offset = offset + length;
|
|
2991
|
+
break;
|
|
2992
|
+
}
|
|
2993
|
+
if (this.offset - offset !== length) {
|
|
2994
|
+
throw new Error(`Length mismatch while decoding chunk ${type}`);
|
|
2995
|
+
}
|
|
2996
|
+
if (this._checkCrc) {
|
|
2997
|
+
checkCrc(this, length + 4, type);
|
|
2998
|
+
} else {
|
|
2999
|
+
this.skip(4);
|
|
3000
|
+
}
|
|
3001
|
+
}
|
|
3002
|
+
// https://www.w3.org/TR/PNG/#11IHDR
|
|
3003
|
+
decodeIHDR() {
|
|
3004
|
+
const image = this._png;
|
|
3005
|
+
image.width = this.readUint32();
|
|
3006
|
+
image.height = this.readUint32();
|
|
3007
|
+
image.depth = checkBitDepth(this.readUint8());
|
|
3008
|
+
const colorType = this.readUint8();
|
|
3009
|
+
this._colorType = colorType;
|
|
3010
|
+
let channels;
|
|
3011
|
+
switch (colorType) {
|
|
3012
|
+
case ColorType.GREYSCALE:
|
|
3013
|
+
channels = 1;
|
|
3014
|
+
break;
|
|
3015
|
+
case ColorType.TRUECOLOUR:
|
|
3016
|
+
channels = 3;
|
|
3017
|
+
break;
|
|
3018
|
+
case ColorType.INDEXED_COLOUR:
|
|
3019
|
+
channels = 1;
|
|
3020
|
+
break;
|
|
3021
|
+
case ColorType.GREYSCALE_ALPHA:
|
|
3022
|
+
channels = 2;
|
|
3023
|
+
break;
|
|
3024
|
+
case ColorType.TRUECOLOUR_ALPHA:
|
|
3025
|
+
channels = 4;
|
|
3026
|
+
break;
|
|
3027
|
+
// Kept for exhaustiveness.
|
|
3028
|
+
// eslint-disable-next-line unicorn/no-useless-switch-case
|
|
3029
|
+
case ColorType.UNKNOWN:
|
|
3030
|
+
default:
|
|
3031
|
+
throw new Error(`Unknown color type: ${colorType}`);
|
|
3032
|
+
}
|
|
3033
|
+
this._png.channels = channels;
|
|
3034
|
+
this._compressionMethod = this.readUint8();
|
|
3035
|
+
if (this._compressionMethod !== CompressionMethod.DEFLATE) {
|
|
3036
|
+
throw new Error(`Unsupported compression method: ${this._compressionMethod}`);
|
|
3037
|
+
}
|
|
3038
|
+
this._filterMethod = this.readUint8();
|
|
3039
|
+
this._interlaceMethod = this.readUint8();
|
|
3040
|
+
}
|
|
3041
|
+
decodeACTL() {
|
|
3042
|
+
this._numberOfFrames = this.readUint32();
|
|
3043
|
+
this._numberOfPlays = this.readUint32();
|
|
3044
|
+
this._isAnimated = true;
|
|
3045
|
+
}
|
|
3046
|
+
decodeFCTL() {
|
|
3047
|
+
const image = {
|
|
3048
|
+
sequenceNumber: this.readUint32(),
|
|
3049
|
+
width: this.readUint32(),
|
|
3050
|
+
height: this.readUint32(),
|
|
3051
|
+
xOffset: this.readUint32(),
|
|
3052
|
+
yOffset: this.readUint32(),
|
|
3053
|
+
delayNumber: this.readUint16(),
|
|
3054
|
+
delayDenominator: this.readUint16(),
|
|
3055
|
+
disposeOp: this.readUint8(),
|
|
3056
|
+
blendOp: this.readUint8(),
|
|
3057
|
+
data: new Uint8Array(0)
|
|
3058
|
+
};
|
|
3059
|
+
this._frames.push(image);
|
|
3060
|
+
}
|
|
3061
|
+
// https://www.w3.org/TR/PNG/#11PLTE
|
|
3062
|
+
decodePLTE(length) {
|
|
3063
|
+
if (length % 3 !== 0) {
|
|
3064
|
+
throw new RangeError(`PLTE field length must be a multiple of 3. Got ${length}`);
|
|
3065
|
+
}
|
|
3066
|
+
const l = length / 3;
|
|
3067
|
+
this._hasPalette = true;
|
|
3068
|
+
const palette = [];
|
|
3069
|
+
this._palette = palette;
|
|
3070
|
+
for (let i = 0; i < l; i++) {
|
|
3071
|
+
palette.push([this.readUint8(), this.readUint8(), this.readUint8()]);
|
|
3072
|
+
}
|
|
3073
|
+
}
|
|
3074
|
+
// https://www.w3.org/TR/PNG/#11IDAT
|
|
3075
|
+
decodeIDAT(length) {
|
|
3076
|
+
this._writingDataChunks = true;
|
|
3077
|
+
const dataLength = length;
|
|
3078
|
+
const dataOffset = this.offset + this.byteOffset;
|
|
3079
|
+
try {
|
|
3080
|
+
this._inflator.push(new Uint8Array(this.buffer, dataOffset, dataLength), false);
|
|
3081
|
+
} catch (error) {
|
|
3082
|
+
throw new Error("Error while decompressing the data:", { cause: error });
|
|
3083
|
+
}
|
|
3084
|
+
this.skip(length);
|
|
3085
|
+
}
|
|
3086
|
+
decodeFDAT(length) {
|
|
3087
|
+
this._writingDataChunks = true;
|
|
3088
|
+
let dataLength = length;
|
|
3089
|
+
let dataOffset = this.offset + this.byteOffset;
|
|
3090
|
+
dataOffset += 4;
|
|
3091
|
+
dataLength -= 4;
|
|
3092
|
+
try {
|
|
3093
|
+
this._inflator.push(new Uint8Array(this.buffer, dataOffset, dataLength), false);
|
|
3094
|
+
} catch (error) {
|
|
3095
|
+
throw new Error("Error while decompressing the data:", { cause: error });
|
|
3096
|
+
}
|
|
3097
|
+
this.skip(length);
|
|
3098
|
+
}
|
|
3099
|
+
// https://www.w3.org/TR/PNG/#11tRNS
|
|
3100
|
+
decodetRNS(length) {
|
|
3101
|
+
switch (this._colorType) {
|
|
3102
|
+
case ColorType.GREYSCALE:
|
|
3103
|
+
case ColorType.TRUECOLOUR: {
|
|
3104
|
+
if (length % 2 !== 0) {
|
|
3105
|
+
throw new RangeError(`tRNS chunk length must be a multiple of 2. Got ${length}`);
|
|
3106
|
+
}
|
|
3107
|
+
if (length / 2 > this._png.width * this._png.height) {
|
|
3108
|
+
throw new Error(`tRNS chunk contains more alpha values than there are pixels (${length / 2} vs ${this._png.width * this._png.height})`);
|
|
3109
|
+
}
|
|
3110
|
+
this._hasTransparency = true;
|
|
3111
|
+
this._transparency = new Uint16Array(length / 2);
|
|
3112
|
+
for (let i = 0; i < length / 2; i++) {
|
|
3113
|
+
this._transparency[i] = this.readUint16();
|
|
3114
|
+
}
|
|
3115
|
+
break;
|
|
3116
|
+
}
|
|
3117
|
+
case ColorType.INDEXED_COLOUR: {
|
|
3118
|
+
if (length > this._palette.length) {
|
|
3119
|
+
throw new Error(`tRNS chunk contains more alpha values than there are palette colors (${length} vs ${this._palette.length})`);
|
|
3120
|
+
}
|
|
3121
|
+
let i = 0;
|
|
3122
|
+
for (; i < length; i++) {
|
|
3123
|
+
const alpha = this.readByte();
|
|
3124
|
+
this._palette[i].push(alpha);
|
|
3125
|
+
}
|
|
3126
|
+
for (; i < this._palette.length; i++) {
|
|
3127
|
+
this._palette[i].push(255);
|
|
3128
|
+
}
|
|
3129
|
+
break;
|
|
3130
|
+
}
|
|
3131
|
+
// Kept for exhaustiveness.
|
|
3132
|
+
/* eslint-disable unicorn/no-useless-switch-case */
|
|
3133
|
+
case ColorType.UNKNOWN:
|
|
3134
|
+
case ColorType.GREYSCALE_ALPHA:
|
|
3135
|
+
case ColorType.TRUECOLOUR_ALPHA:
|
|
3136
|
+
default: {
|
|
3137
|
+
throw new Error(`tRNS chunk is not supported for color type ${this._colorType}`);
|
|
3138
|
+
}
|
|
3139
|
+
}
|
|
3140
|
+
}
|
|
3141
|
+
// https://www.w3.org/TR/PNG/#11iCCP
|
|
3142
|
+
decodeiCCP(length) {
|
|
3143
|
+
const name = readKeyword(this);
|
|
3144
|
+
const compressionMethod = this.readUint8();
|
|
3145
|
+
if (compressionMethod !== CompressionMethod.DEFLATE) {
|
|
3146
|
+
throw new Error(`Unsupported iCCP compression method: ${compressionMethod}`);
|
|
3147
|
+
}
|
|
3148
|
+
const compressedProfile = this.readBytes(length - name.length - 2);
|
|
3149
|
+
this._png.iccEmbeddedProfile = {
|
|
3150
|
+
name,
|
|
3151
|
+
profile: unzlibSync(compressedProfile)
|
|
3152
|
+
};
|
|
3153
|
+
}
|
|
3154
|
+
// https://www.w3.org/TR/PNG/#11pHYs
|
|
3155
|
+
decodepHYs() {
|
|
3156
|
+
const ppuX = this.readUint32();
|
|
3157
|
+
const ppuY = this.readUint32();
|
|
3158
|
+
const unitSpecifier = this.readByte();
|
|
3159
|
+
this._png.resolution = {
|
|
3160
|
+
x: ppuX,
|
|
3161
|
+
y: ppuY,
|
|
3162
|
+
unit: unitSpecifier
|
|
3163
|
+
};
|
|
3164
|
+
}
|
|
3165
|
+
decodeApngImage() {
|
|
3166
|
+
this._apng.width = this._png.width;
|
|
3167
|
+
this._apng.height = this._png.height;
|
|
3168
|
+
this._apng.channels = this._png.channels;
|
|
3169
|
+
this._apng.depth = this._png.depth;
|
|
3170
|
+
this._apng.numberOfFrames = this._numberOfFrames;
|
|
3171
|
+
this._apng.numberOfPlays = this._numberOfPlays;
|
|
3172
|
+
this._apng.text = this._png.text;
|
|
3173
|
+
this._apng.resolution = this._png.resolution;
|
|
3174
|
+
for (let i = 0; i < this._numberOfFrames; i++) {
|
|
3175
|
+
const newFrame = {
|
|
3176
|
+
sequenceNumber: this._frames[i].sequenceNumber,
|
|
3177
|
+
delayNumber: this._frames[i].delayNumber,
|
|
3178
|
+
delayDenominator: this._frames[i].delayDenominator,
|
|
3179
|
+
data: this._apng.depth === 8 ? new Uint8Array(this._apng.width * this._apng.height * this._apng.channels) : new Uint16Array(this._apng.width * this._apng.height * this._apng.channels)
|
|
3180
|
+
};
|
|
3181
|
+
const frame = this._frames.at(i);
|
|
3182
|
+
if (frame) {
|
|
3183
|
+
frame.data = decodeInterlaceNull({
|
|
3184
|
+
data: frame.data,
|
|
3185
|
+
width: frame.width,
|
|
3186
|
+
height: frame.height,
|
|
3187
|
+
channels: this._apng.channels,
|
|
3188
|
+
depth: this._apng.depth
|
|
3189
|
+
});
|
|
3190
|
+
if (this._hasPalette) {
|
|
3191
|
+
this._apng.palette = this._palette;
|
|
3192
|
+
}
|
|
3193
|
+
if (this._hasTransparency) {
|
|
3194
|
+
this._apng.transparency = this._transparency;
|
|
3195
|
+
}
|
|
3196
|
+
if (i === 0 || frame.xOffset === 0 && frame.yOffset === 0 && frame.width === this._png.width && frame.height === this._png.height) {
|
|
3197
|
+
newFrame.data = frame.data;
|
|
3198
|
+
} else {
|
|
3199
|
+
const prevFrame = this._apng.frames.at(i - 1);
|
|
3200
|
+
this.disposeFrame(frame, prevFrame, newFrame);
|
|
3201
|
+
this.addFrameDataToCanvas(newFrame, frame);
|
|
3202
|
+
}
|
|
3203
|
+
this._apng.frames.push(newFrame);
|
|
3204
|
+
}
|
|
3205
|
+
}
|
|
3206
|
+
return this._apng;
|
|
3207
|
+
}
|
|
3208
|
+
disposeFrame(frame, prevFrame, imageFrame) {
|
|
3209
|
+
switch (frame.disposeOp) {
|
|
3210
|
+
case DisposeOpType.NONE:
|
|
3211
|
+
break;
|
|
3212
|
+
case DisposeOpType.BACKGROUND:
|
|
3213
|
+
for (let row = 0; row < this._png.height; row++) {
|
|
3214
|
+
for (let col = 0; col < this._png.width; col++) {
|
|
3215
|
+
const index = (row * frame.width + col) * this._png.channels;
|
|
3216
|
+
for (let channel = 0; channel < this._png.channels; channel++) {
|
|
3217
|
+
imageFrame.data[index + channel] = 0;
|
|
3218
|
+
}
|
|
3219
|
+
}
|
|
3220
|
+
}
|
|
3221
|
+
break;
|
|
3222
|
+
case DisposeOpType.PREVIOUS:
|
|
3223
|
+
imageFrame.data.set(prevFrame.data);
|
|
3224
|
+
break;
|
|
3225
|
+
default:
|
|
3226
|
+
throw new Error("Unknown disposeOp");
|
|
3227
|
+
}
|
|
3228
|
+
}
|
|
3229
|
+
addFrameDataToCanvas(imageFrame, frame) {
|
|
3230
|
+
const maxValue = 1 << this._png.depth;
|
|
3231
|
+
const calculatePixelIndices = (row, col) => {
|
|
3232
|
+
const index = ((row + frame.yOffset) * this._png.width + frame.xOffset + col) * this._png.channels;
|
|
3233
|
+
const frameIndex = (row * frame.width + col) * this._png.channels;
|
|
3234
|
+
return { index, frameIndex };
|
|
3235
|
+
};
|
|
3236
|
+
switch (frame.blendOp) {
|
|
3237
|
+
case BlendOpType.SOURCE:
|
|
3238
|
+
for (let row = 0; row < frame.height; row++) {
|
|
3239
|
+
for (let col = 0; col < frame.width; col++) {
|
|
3240
|
+
const { index, frameIndex } = calculatePixelIndices(row, col);
|
|
3241
|
+
for (let channel = 0; channel < this._png.channels; channel++) {
|
|
3242
|
+
imageFrame.data[index + channel] = frame.data[frameIndex + channel];
|
|
3243
|
+
}
|
|
3244
|
+
}
|
|
3245
|
+
}
|
|
3246
|
+
break;
|
|
3247
|
+
// https://www.w3.org/TR/png-3/#13Alpha-channel-processing
|
|
3248
|
+
case BlendOpType.OVER:
|
|
3249
|
+
for (let row = 0; row < frame.height; row++) {
|
|
3250
|
+
for (let col = 0; col < frame.width; col++) {
|
|
3251
|
+
const { index, frameIndex } = calculatePixelIndices(row, col);
|
|
3252
|
+
for (let channel = 0; channel < this._png.channels; channel++) {
|
|
3253
|
+
const sourceAlpha = frame.data[frameIndex + this._png.channels - 1] / maxValue;
|
|
3254
|
+
const foregroundValue = channel % (this._png.channels - 1) === 0 ? 1 : frame.data[frameIndex + channel];
|
|
3255
|
+
const value = Math.floor(sourceAlpha * foregroundValue + (1 - sourceAlpha) * imageFrame.data[index + channel]);
|
|
3256
|
+
imageFrame.data[index + channel] += value;
|
|
3257
|
+
}
|
|
3258
|
+
}
|
|
3259
|
+
}
|
|
3260
|
+
break;
|
|
3261
|
+
default:
|
|
3262
|
+
throw new Error("Unknown blendOp");
|
|
3263
|
+
}
|
|
3264
|
+
}
|
|
3265
|
+
decodeImage() {
|
|
3266
|
+
const data = this._inflatorResult;
|
|
3267
|
+
if (this._filterMethod !== FilterMethod.ADAPTIVE) {
|
|
3268
|
+
throw new Error(`Filter method ${this._filterMethod} not supported`);
|
|
3269
|
+
}
|
|
3270
|
+
if (this._interlaceMethod === InterlaceMethod.NO_INTERLACE) {
|
|
3271
|
+
this._png.data = decodeInterlaceNull({
|
|
3272
|
+
data,
|
|
3273
|
+
width: this._png.width,
|
|
3274
|
+
height: this._png.height,
|
|
3275
|
+
channels: this._png.channels,
|
|
3276
|
+
depth: this._png.depth
|
|
3277
|
+
});
|
|
3278
|
+
} else if (this._interlaceMethod === InterlaceMethod.ADAM7) {
|
|
3279
|
+
this._png.data = decodeInterlaceAdam7({
|
|
3280
|
+
data,
|
|
3281
|
+
width: this._png.width,
|
|
3282
|
+
height: this._png.height,
|
|
3283
|
+
channels: this._png.channels,
|
|
3284
|
+
depth: this._png.depth
|
|
3285
|
+
});
|
|
3286
|
+
} else {
|
|
3287
|
+
throw new Error(`Interlace method ${this._interlaceMethod} not supported`);
|
|
3288
|
+
}
|
|
3289
|
+
if (this._hasPalette) {
|
|
3290
|
+
this._png.palette = this._palette;
|
|
3291
|
+
}
|
|
3292
|
+
if (this._hasTransparency) {
|
|
3293
|
+
this._png.transparency = this._transparency;
|
|
3294
|
+
}
|
|
3295
|
+
}
|
|
3296
|
+
pushDataToFrame() {
|
|
3297
|
+
this._inflator.push(new Uint8Array(0), true);
|
|
3298
|
+
const result = this._inflatorResult;
|
|
3299
|
+
const lastFrame = this._frames.at(-1);
|
|
3300
|
+
if (lastFrame) {
|
|
3301
|
+
lastFrame.data = result;
|
|
3302
|
+
} else {
|
|
3303
|
+
this._frames.push({
|
|
3304
|
+
sequenceNumber: 0,
|
|
3305
|
+
width: this._png.width,
|
|
3306
|
+
height: this._png.height,
|
|
3307
|
+
xOffset: 0,
|
|
3308
|
+
yOffset: 0,
|
|
3309
|
+
delayNumber: 0,
|
|
3310
|
+
delayDenominator: 0,
|
|
3311
|
+
disposeOp: DisposeOpType.NONE,
|
|
3312
|
+
blendOp: BlendOpType.SOURCE,
|
|
3313
|
+
data: result
|
|
3314
|
+
});
|
|
3315
|
+
}
|
|
3316
|
+
this._inflator = new Inflator((chunk, final) => {
|
|
3317
|
+
this._chunks.push(chunk);
|
|
3318
|
+
if (final) {
|
|
3319
|
+
const totalLength = this._chunks.reduce((sum, c) => sum + c.length, 0);
|
|
3320
|
+
this._inflatorResult = new Uint8Array(totalLength);
|
|
3321
|
+
let offset = 0;
|
|
3322
|
+
for (const chunk2 of this._chunks) {
|
|
3323
|
+
this._inflatorResult.set(chunk2, offset);
|
|
3324
|
+
offset += chunk2.length;
|
|
3325
|
+
}
|
|
3326
|
+
this._chunks = [];
|
|
3327
|
+
}
|
|
3328
|
+
});
|
|
3329
|
+
this._chunks = [];
|
|
3330
|
+
this._writingDataChunks = false;
|
|
3331
|
+
}
|
|
3332
|
+
};
|
|
3333
|
+
function checkBitDepth(value) {
|
|
3334
|
+
if (value !== 1 && value !== 2 && value !== 4 && value !== 8 && value !== 16) {
|
|
3335
|
+
throw new Error(`invalid bit depth: ${value}`);
|
|
3336
|
+
}
|
|
3337
|
+
return value;
|
|
3338
|
+
}
|
|
3339
|
+
|
|
3340
|
+
// node_modules/.pnpm/fast-png@8.0.0/node_modules/fast-png/lib/png_encoder.js
|
|
3341
|
+
import { zlibSync } from "fflate";
|
|
3342
|
+
var defaultZlibOptions = {
|
|
3343
|
+
level: 3
|
|
3344
|
+
};
|
|
3345
|
+
var PngEncoder = class extends IOBuffer {
|
|
3346
|
+
_png;
|
|
3347
|
+
_zlibOptions;
|
|
3348
|
+
_colorType;
|
|
3349
|
+
_interlaceMethod;
|
|
3350
|
+
constructor(data, options = {}) {
|
|
3351
|
+
super();
|
|
3352
|
+
this._colorType = ColorType.UNKNOWN;
|
|
3353
|
+
this._zlibOptions = { ...defaultZlibOptions, ...options.zlib };
|
|
3354
|
+
this._png = this._checkData(data);
|
|
3355
|
+
this._interlaceMethod = (options.interlace === "Adam7" ? InterlaceMethod.ADAM7 : InterlaceMethod.NO_INTERLACE) ?? InterlaceMethod.NO_INTERLACE;
|
|
3356
|
+
this.setBigEndian();
|
|
3357
|
+
}
|
|
3358
|
+
encode() {
|
|
3359
|
+
writeSignature(this);
|
|
3360
|
+
this.encodeIHDR();
|
|
3361
|
+
if (this._png.palette) {
|
|
3362
|
+
this.encodePLTE();
|
|
3363
|
+
if (this._png.palette[0].length === 4) {
|
|
3364
|
+
this.encodeTRNS();
|
|
3365
|
+
}
|
|
3366
|
+
}
|
|
3367
|
+
this.encodeData();
|
|
3368
|
+
if (this._png.text) {
|
|
3369
|
+
for (const [keyword, text] of Object.entries(this._png.text)) {
|
|
3370
|
+
encodetEXt(this, keyword, text);
|
|
3371
|
+
}
|
|
3372
|
+
}
|
|
3373
|
+
this.encodeIEND();
|
|
3374
|
+
return this.toArray();
|
|
3375
|
+
}
|
|
3376
|
+
// https://www.w3.org/TR/PNG/#11IHDR
|
|
3377
|
+
encodeIHDR() {
|
|
3378
|
+
this.writeUint32(13);
|
|
3379
|
+
this.writeChars("IHDR");
|
|
3380
|
+
this.writeUint32(this._png.width);
|
|
3381
|
+
this.writeUint32(this._png.height);
|
|
3382
|
+
this.writeByte(this._png.depth);
|
|
3383
|
+
this.writeByte(this._colorType);
|
|
3384
|
+
this.writeByte(CompressionMethod.DEFLATE);
|
|
3385
|
+
this.writeByte(FilterMethod.ADAPTIVE);
|
|
3386
|
+
this.writeByte(this._interlaceMethod);
|
|
3387
|
+
writeCrc(this, 17);
|
|
3388
|
+
}
|
|
3389
|
+
// https://www.w3.org/TR/PNG/#11IEND
|
|
3390
|
+
encodeIEND() {
|
|
3391
|
+
this.writeUint32(0);
|
|
3392
|
+
this.writeChars("IEND");
|
|
3393
|
+
writeCrc(this, 4);
|
|
3394
|
+
}
|
|
3395
|
+
encodePLTE() {
|
|
3396
|
+
const paletteLength = this._png.palette?.length * 3;
|
|
3397
|
+
this.writeUint32(paletteLength);
|
|
3398
|
+
this.writeChars("PLTE");
|
|
3399
|
+
for (const color of this._png.palette) {
|
|
3400
|
+
this.writeByte(color[0]);
|
|
3401
|
+
this.writeByte(color[1]);
|
|
3402
|
+
this.writeByte(color[2]);
|
|
3403
|
+
}
|
|
3404
|
+
writeCrc(this, 4 + paletteLength);
|
|
3405
|
+
}
|
|
3406
|
+
encodeTRNS() {
|
|
3407
|
+
const alpha = this._png.palette.filter((color) => {
|
|
3408
|
+
return color.at(-1) !== 255;
|
|
3409
|
+
});
|
|
3410
|
+
this.writeUint32(alpha.length);
|
|
3411
|
+
this.writeChars("tRNS");
|
|
3412
|
+
for (const el of alpha) {
|
|
3413
|
+
this.writeByte(el.at(-1));
|
|
3414
|
+
}
|
|
3415
|
+
writeCrc(this, 4 + alpha.length);
|
|
3416
|
+
}
|
|
3417
|
+
// https://www.w3.org/TR/PNG/#11IDAT
|
|
3418
|
+
encodeIDAT(data) {
|
|
3419
|
+
this.writeUint32(data.length);
|
|
3420
|
+
this.writeChars("IDAT");
|
|
3421
|
+
this.writeBytes(data);
|
|
3422
|
+
writeCrc(this, data.length + 4);
|
|
3423
|
+
}
|
|
3424
|
+
encodeData() {
|
|
3425
|
+
const { width, height, channels, depth, data } = this._png;
|
|
3426
|
+
const slotsPerLine = depth <= 8 ? Math.ceil(width * depth / 8) * channels : Math.ceil(width * depth / 8 * channels / 2);
|
|
3427
|
+
const newData = new IOBuffer().setBigEndian();
|
|
3428
|
+
let offset = 0;
|
|
3429
|
+
if (this._interlaceMethod === InterlaceMethod.NO_INTERLACE) {
|
|
3430
|
+
for (let i = 0; i < height; i++) {
|
|
3431
|
+
newData.writeByte(0);
|
|
3432
|
+
if (depth === 16) {
|
|
3433
|
+
offset = writeDataUint16(data, newData, slotsPerLine, offset);
|
|
3434
|
+
} else {
|
|
3435
|
+
offset = writeDataBytes(data, newData, slotsPerLine, offset);
|
|
3436
|
+
}
|
|
3437
|
+
}
|
|
3438
|
+
} else if (this._interlaceMethod === InterlaceMethod.ADAM7) {
|
|
3439
|
+
offset = writeDataInterlaced(this._png, data, newData, offset);
|
|
3440
|
+
}
|
|
3441
|
+
const buffer = newData.toArray();
|
|
3442
|
+
const compressed = zlibSync(buffer, this._zlibOptions);
|
|
3443
|
+
this.encodeIDAT(compressed);
|
|
3444
|
+
}
|
|
3445
|
+
_checkData(data) {
|
|
3446
|
+
const { colorType, channels, depth } = getColorType(data, data.palette);
|
|
3447
|
+
const png = {
|
|
3448
|
+
width: checkInteger(data.width, "width"),
|
|
3449
|
+
height: checkInteger(data.height, "height"),
|
|
3450
|
+
channels,
|
|
3451
|
+
data: data.data,
|
|
3452
|
+
depth,
|
|
3453
|
+
text: data.text,
|
|
3454
|
+
palette: data.palette
|
|
3455
|
+
};
|
|
3456
|
+
this._colorType = colorType;
|
|
3457
|
+
const expectedSize = depth < 8 ? Math.ceil(png.width * depth / 8) * png.height * channels : png.width * png.height * channels;
|
|
3458
|
+
if (png.data.length !== expectedSize) {
|
|
3459
|
+
throw new RangeError(`wrong data size. Found ${png.data.length}, expected ${expectedSize}`);
|
|
3460
|
+
}
|
|
3461
|
+
return png;
|
|
3462
|
+
}
|
|
3463
|
+
};
|
|
3464
|
+
function checkInteger(value, name) {
|
|
3465
|
+
if (Number.isInteger(value) && value > 0) {
|
|
3466
|
+
return value;
|
|
3467
|
+
}
|
|
3468
|
+
throw new TypeError(`${name} must be a positive integer`);
|
|
3469
|
+
}
|
|
3470
|
+
function getColorType(data, palette) {
|
|
3471
|
+
const { channels = 4, depth = 8 } = data;
|
|
3472
|
+
if (channels !== 4 && channels !== 3 && channels !== 2 && channels !== 1) {
|
|
3473
|
+
throw new RangeError(`unsupported number of channels: ${channels}`);
|
|
3474
|
+
}
|
|
3475
|
+
const returnValue = {
|
|
3476
|
+
channels,
|
|
3477
|
+
depth,
|
|
3478
|
+
colorType: ColorType.UNKNOWN
|
|
3479
|
+
};
|
|
3480
|
+
switch (channels) {
|
|
3481
|
+
case 4:
|
|
3482
|
+
returnValue.colorType = ColorType.TRUECOLOUR_ALPHA;
|
|
3483
|
+
break;
|
|
3484
|
+
case 3:
|
|
3485
|
+
returnValue.colorType = ColorType.TRUECOLOUR;
|
|
3486
|
+
break;
|
|
3487
|
+
case 1:
|
|
3488
|
+
if (palette) {
|
|
3489
|
+
returnValue.colorType = ColorType.INDEXED_COLOUR;
|
|
3490
|
+
} else {
|
|
3491
|
+
returnValue.colorType = ColorType.GREYSCALE;
|
|
3492
|
+
}
|
|
3493
|
+
break;
|
|
3494
|
+
case 2:
|
|
3495
|
+
returnValue.colorType = ColorType.GREYSCALE_ALPHA;
|
|
3496
|
+
break;
|
|
3497
|
+
default:
|
|
3498
|
+
throw new Error("unsupported number of channels");
|
|
3499
|
+
}
|
|
3500
|
+
return returnValue;
|
|
3501
|
+
}
|
|
3502
|
+
function writeDataBytes(data, newData, slotsPerLine, offset) {
|
|
3503
|
+
for (let j = 0; j < slotsPerLine; j++) {
|
|
3504
|
+
newData.writeByte(data[offset++]);
|
|
3505
|
+
}
|
|
3506
|
+
return offset;
|
|
3507
|
+
}
|
|
3508
|
+
function writeDataInterlaced(imageData, data, newData, offset) {
|
|
3509
|
+
const passes = [
|
|
3510
|
+
{ x: 0, y: 0, xStep: 8, yStep: 8 },
|
|
3511
|
+
{ x: 4, y: 0, xStep: 8, yStep: 8 },
|
|
3512
|
+
{ x: 0, y: 4, xStep: 4, yStep: 8 },
|
|
3513
|
+
{ x: 2, y: 0, xStep: 4, yStep: 4 },
|
|
3514
|
+
{ x: 0, y: 2, xStep: 2, yStep: 4 },
|
|
3515
|
+
{ x: 1, y: 0, xStep: 2, yStep: 2 },
|
|
3516
|
+
{ x: 0, y: 1, xStep: 1, yStep: 2 }
|
|
3517
|
+
];
|
|
3518
|
+
const { width, height, channels, depth } = imageData;
|
|
3519
|
+
let pixelSize;
|
|
3520
|
+
if (depth === 16) {
|
|
3521
|
+
pixelSize = channels * depth / 8 / 2;
|
|
3522
|
+
} else {
|
|
3523
|
+
pixelSize = channels * depth / 8;
|
|
3524
|
+
}
|
|
3525
|
+
for (let passIndex = 0; passIndex < 7; passIndex++) {
|
|
3526
|
+
const pass = passes[passIndex];
|
|
3527
|
+
const passWidth = Math.floor((width - pass.x + pass.xStep - 1) / pass.xStep);
|
|
3528
|
+
const passHeight = Math.floor((height - pass.y + pass.yStep - 1) / pass.yStep);
|
|
3529
|
+
if (passWidth <= 0 || passHeight <= 0)
|
|
3530
|
+
continue;
|
|
3531
|
+
const passLineBytes = passWidth * pixelSize;
|
|
3532
|
+
for (let y = 0; y < passHeight; y++) {
|
|
3533
|
+
const imageY = pass.y + y * pass.yStep;
|
|
3534
|
+
const rawScanline = depth <= 8 ? new Uint8Array(passLineBytes) : new Uint16Array(passLineBytes);
|
|
3535
|
+
let rawOffset = 0;
|
|
3536
|
+
for (let x = 0; x < passWidth; x++) {
|
|
3537
|
+
const imageX = pass.x + x * pass.xStep;
|
|
3538
|
+
if (imageX < width && imageY < height) {
|
|
3539
|
+
const srcPos = (imageY * width + imageX) * pixelSize;
|
|
3540
|
+
for (let i = 0; i < pixelSize; i++) {
|
|
3541
|
+
rawScanline[rawOffset++] = data[srcPos + i];
|
|
3542
|
+
}
|
|
3543
|
+
}
|
|
3544
|
+
}
|
|
3545
|
+
newData.writeByte(0);
|
|
3546
|
+
if (depth === 8) {
|
|
3547
|
+
newData.writeBytes(rawScanline);
|
|
3548
|
+
} else if (depth === 16) {
|
|
3549
|
+
for (const value of rawScanline) {
|
|
3550
|
+
newData.writeByte(value >> 8 & 255);
|
|
3551
|
+
newData.writeByte(value & 255);
|
|
3552
|
+
}
|
|
3553
|
+
}
|
|
3554
|
+
}
|
|
3555
|
+
}
|
|
3556
|
+
return offset;
|
|
3557
|
+
}
|
|
3558
|
+
function writeDataUint16(data, newData, slotsPerLine, offset) {
|
|
3559
|
+
for (let j = 0; j < slotsPerLine; j++) {
|
|
3560
|
+
newData.writeUint16(data[offset++]);
|
|
3561
|
+
}
|
|
3562
|
+
return offset;
|
|
3563
|
+
}
|
|
3564
|
+
|
|
3565
|
+
// node_modules/.pnpm/fast-png@8.0.0/node_modules/fast-png/lib/index.js
|
|
3566
|
+
function decodePng(data, options) {
|
|
3567
|
+
const decoder = new PngDecoder(data, options);
|
|
3568
|
+
return decoder.decode();
|
|
3569
|
+
}
|
|
3570
|
+
function encodePng(png, options) {
|
|
3571
|
+
const encoder2 = new PngEncoder(png, options);
|
|
3572
|
+
return encoder2.encode();
|
|
3573
|
+
}
|
|
3574
|
+
|
|
3575
|
+
export {
|
|
3576
|
+
getRenderBackground,
|
|
3577
|
+
withRenderContext,
|
|
3578
|
+
SVG_ACCESSIBILITY_COMPONENT,
|
|
3579
|
+
prepareAccessibleSvg,
|
|
3580
|
+
addSvgAccessibility,
|
|
3581
|
+
CANVAS_CHART_COMPONENT,
|
|
3582
|
+
CHART_FRAME_COMPONENT,
|
|
3583
|
+
isCanvasChart,
|
|
3584
|
+
CanvasChartFit,
|
|
3585
|
+
convertIndexedToRgb,
|
|
3586
|
+
decodePng,
|
|
3587
|
+
encodePng,
|
|
3588
|
+
normalizeWatermark,
|
|
3589
|
+
Watermark,
|
|
3590
|
+
wrapWithWatermark,
|
|
3591
|
+
wrapWithBrand,
|
|
3592
|
+
detectBackgroundColor,
|
|
3593
|
+
detectContentBounds,
|
|
3594
|
+
cropPixels,
|
|
3595
|
+
detectOverflow,
|
|
3596
|
+
detectLayoutOverflow,
|
|
3597
|
+
satori,
|
|
3598
|
+
loadAdditionalAsset,
|
|
3599
|
+
getFonts,
|
|
3600
|
+
CanvasOverflowError,
|
|
3601
|
+
renderToPng,
|
|
3602
|
+
renderToPngWithOutput,
|
|
3603
|
+
renderToBuffer,
|
|
3604
|
+
renderToSvg,
|
|
3605
|
+
hold,
|
|
3606
|
+
keyframe,
|
|
3607
|
+
tween,
|
|
3608
|
+
parallel,
|
|
3609
|
+
defineAnimation,
|
|
3610
|
+
sampleAnimation,
|
|
3611
|
+
sampleAnimationFrame,
|
|
3612
|
+
analyzeAnimationCadence,
|
|
3613
|
+
sampleAnimationFrames,
|
|
3614
|
+
videoFrameSchedule,
|
|
3615
|
+
renderAnimationVideo,
|
|
3616
|
+
renderRgbaVideo
|
|
3617
|
+
};
|