vizmatic 0.8.1 → 0.9.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 +42 -9
- package/README.md +172 -61
- package/ROADMAP.md +9 -6
- package/SCIENCE.md +70 -5
- package/assets/THIRD_PARTY_LICENSES.md +3 -3
- package/assets/fonts/SourceSerif4-24pt-SemiBold.ttf +0 -0
- package/assets/licenses/raster-codecs-MIT.txt +2 -2
- package/dist/chunk-6JI55YX4.js +604 -0
- package/dist/{chunk-LTX44BAE.js → chunk-BYKXNUX7.js} +25 -4
- package/dist/{chunk-ZB3HK7RM.js → chunk-EPZLNABN.js} +345 -25
- package/dist/chunk-EWCO6ZT6.js +696 -0
- package/dist/{chunk-DZHX76RS.js → chunk-G7HRSKHV.js} +33 -4
- package/dist/{chunk-RPAHMKAR.js → chunk-JTWC2OPN.js} +7 -5
- package/dist/chunk-LJZO36IR.js +124 -0
- package/dist/chunk-REL2BZ2B.js +88 -0
- package/dist/chunk-RLPEOWST.js +1 -0
- package/dist/chunk-VMHOWCDL.js +296 -0
- package/dist/chunk-YDD63HHD.js +1 -0
- package/dist/chunk-YNGDEARY.js +7230 -0
- package/dist/cli.js +124 -270
- package/dist/gpu-selection-CDWCZH27.js +1 -0
- package/dist/gpu-selection-MX6NHMNA.js +635 -0
- package/dist/index.cjs +1492 -217
- package/dist/index.d.cts +233 -230
- package/dist/index.d.ts +233 -230
- package/dist/index.js +1103 -479
- package/dist/math-E7W5LFWO.js +1 -0
- package/dist/{math-LXB4FH4E.js → math-NEFQUPRV.js} +3 -1
- package/dist/scene-browser-chunks/chunk-RQNLYYJN.js +70 -0
- package/dist/scene-browser-chunks/{scene-browser-layout-M3XSXMV4.js → scene-browser-layout-RC3ECYRK.js} +1 -1
- package/dist/scene-browser.js +18 -18
- package/dist/scene-cli.js +1 -1
- package/dist/scene-runtime.cjs +28 -5
- package/dist/scene-runtime.js +2 -2
- package/dist/{server-2FYHZ4UO.js → server-Q4COUZHV.js} +3 -3
- package/dist/stage-browser/browser.d.ts +8962 -685
- package/dist/stage-browser/gpu-renderer.js +138 -0
- package/dist/stage-browser/{math-4WFNHVV2.js → math-KYHUOK7A.js} +46 -10
- package/dist/stage-browser/stage.js +110 -5
- package/dist/stage-export.cjs +8616 -3888
- package/dist/stage-export.d.cts +65 -4
- package/dist/stage-export.d.ts +65 -4
- package/dist/stage-export.js +314 -325
- package/dist/stage.cjs +36997 -8756
- package/dist/stage.d.cts +8456 -634
- package/dist/stage.d.ts +8456 -634
- package/dist/stage.js +28135 -3502
- package/dist/{svg-import-C-RG1t7s.d.cts → svg-import-CSCFWYaB.d.cts} +473 -62
- package/dist/{svg-import-C-RG1t7s.d.ts → svg-import-CSCFWYaB.d.ts} +473 -62
- package/dist/teaching-math-DTKc0XsB.d.cts +243 -0
- package/dist/teaching-math-ydDBhJ2M.d.ts +243 -0
- package/dist/{video-BSwNJZkn.d.cts → theme-DhVdDQKO.d.cts} +4 -212
- package/dist/{video-BSwNJZkn.d.ts → theme-DhVdDQKO.d.ts} +4 -212
- package/dist/video-BYCeOz8F.d.ts +259 -0
- package/dist/video-CGLxJXqu.d.cts +259 -0
- package/examples/README.md +1 -1
- package/examples/_teaching/layer-stack-connector.ts +36 -0
- package/examples/_teaching/layer-stack-recurrence.ts +29 -0
- package/examples/_teaching/layer-stack-tiny-gpt.ts +37 -0
- package/examples/_teaching/layer-stack-transformer.ts +44 -0
- package/examples/catalog-convolution-kernel.tsx +6 -7
- package/examples/catalog-science-physics.tsx +6 -6
- package/examples/{aurora-incident.tsx → checkout-incident.tsx} +1 -1
- package/examples/films/_boltzmann-entropy/model.ts +63 -0
- package/examples/films/_catalysis/model.ts +49 -0
- package/examples/films/_gravitational-waves/model.ts +32 -0
- package/examples/films/_heat-equation/model.ts +54 -0
- package/examples/films/_hydrogen-orbitals/model.ts +31 -0
- package/examples/films/_maxwell/model.ts +84 -0
- package/examples/films/_planetary-orbits/appearance.ts +71 -0
- package/examples/films/_planetary-orbits/model.ts +152 -0
- package/examples/films/_planetary-orbits/saturn.ts +142 -0
- package/examples/films/_quantum-tunneling/model.ts +58 -0
- package/examples/films/_reaction-diffusion/model.ts +61 -0
- package/examples/films/_stationary-action/model.ts +38 -0
- package/examples/films/attention.ts +309 -253
- package/examples/films/beam-search.ts +324 -0
- package/examples/films/boltzmann-entropy.ts +104 -0
- package/examples/films/cache-miss.ts +387 -393
- package/examples/films/catalog.json +251 -60
- package/examples/films/catalysis.ts +128 -0
- package/examples/films/central-limit.ts +27 -22
- package/examples/films/complex-maps.ts +70 -56
- package/examples/films/deck-example.ts +261 -200
- package/examples/films/euler-circle.ts +72 -28
- package/examples/films/flash-attention.ts +549 -0
- package/examples/films/forecast.ts +354 -0
- package/examples/films/fourier-epicycles.ts +21 -20
- package/examples/films/gradient-descent.ts +23 -49
- package/examples/films/gravitational-waves.ts +179 -0
- package/examples/films/heat-equation.ts +124 -0
- package/examples/films/hydrogen-orbitals.ts +135 -0
- package/examples/films/io-uring.ts +635 -0
- package/examples/films/linear-maps.ts +42 -31
- package/examples/films/lorenz.ts +42 -79
- package/examples/films/maxwell.ts +197 -0
- package/examples/films/passage/chunking.ts +40 -0
- package/examples/films/passage/evidence.ts +44 -0
- package/examples/films/passage/prompt.ts +34 -0
- package/examples/films/pathlines.ts +85 -0
- package/examples/films/paxos.ts +596 -0
- package/examples/films/planetary-orbits.ts +146 -0
- package/examples/films/pythagoras.ts +18 -10
- package/examples/films/quantum-tunneling.ts +116 -0
- package/examples/films/rag-citations.ts +423 -0
- package/examples/films/reaction-diffusion.ts +70 -0
- package/examples/films/ring-allreduce.ts +531 -0
- package/examples/films/stationary-action.ts +170 -0
- package/examples/films/svd-3d.ts +605 -0
- package/examples/films/taylor-series.ts +10 -10
- package/examples/films/vizmatic-title.ts +1 -0
- package/examples/narrated-cues/README.md +32 -0
- package/examples/narrated-cues/data/numbers.json +1 -0
- package/examples/narrated-cues/film.json +9 -0
- package/examples/narrated-cues/film.ts +78 -0
- package/examples/narrated-cues/package.json +1 -0
- package/examples/narrated-cues/script.json +57 -0
- package/examples/narrated-cues/timing.json +1 -0
- package/examples/narrated-cues/tsconfig.json +1 -0
- package/examples/proofs/decoding-bars.ts +67 -0
- package/examples/proofs/pipeline.ts +27 -0
- package/examples/proofs/table/evaluation.ts +41 -0
- package/examples/proofs/table/ledger.ts +47 -0
- package/examples/proofs/table/routing.ts +46 -0
- package/examples/vizmatic-hero.tsx +102 -32
- package/package.json +11 -4
- package/templates/narrated/README.md +61 -0
- package/templates/narrated/data/numbers.json +5 -0
- package/templates/narrated/film.json +12 -0
- package/templates/narrated/film.ts +14 -0
- package/templates/narrated/gitignore +8 -0
- package/templates/narrated/package.json +1 -0
- package/templates/narrated/scenes/answer.ts +25 -0
- package/templates/narrated/scenes/kit.ts +39 -0
- package/templates/narrated/scenes/question.ts +31 -0
- package/templates/narrated/script.json +26 -0
- package/templates/narrated/sources.md +22 -0
- package/templates/narrated/timing.json +1 -0
- package/templates/narrated/tsconfig.json +1 -0
- package/tools/narration/README.md +123 -0
- package/tools/narration/beats.py +43 -0
- package/tools/narration/checks.py +587 -0
- package/tools/narration/draft.py +73 -0
- package/tools/narration/film.py +341 -0
- package/tools/narration/lag.py +58 -0
- package/tools/narration/mix.py +265 -0
- package/tools/narration/probe.py +99 -0
- package/tools/narration/record.py +33 -0
- package/tools/narration/resound.py +170 -0
- package/tools/narration/soundbed.py +404 -0
- package/tools/narration/sync.py +93 -0
- package/tools/narration/transcribe.py +84 -0
- package/tools/narration/verify.py +59 -0
- package/tools/narration/voice.py +113 -0
- package/dist/chunk-3F2VQ7NW.js +0 -1
- package/dist/chunk-6H2KTNJ7.js +0 -4774
- package/dist/chunk-6N6P2GJG.js +0 -88
- package/dist/math-QVCETQDQ.js +0 -1
- package/dist/scene-browser-chunks/chunk-5MVLYGXM.js +0 -70
|
@@ -35,7 +35,7 @@ both pinned hashes before updating the photo block.
|
|
|
35
35
|
|
|
36
36
|
## RGB PNG codec dependencies
|
|
37
37
|
|
|
38
|
-
The raster helpers use `fast-png` 8.0.0, `iobuffer` 6.0.1, and `fflate` 0.
|
|
38
|
+
The raster helpers use `fast-png` 8.0.0, `iobuffer` 6.0.1, and `fflate` 0.8.3,
|
|
39
39
|
each under the MIT License. Their full notices are in
|
|
40
40
|
[`licenses/raster-codecs-MIT.txt`](licenses/raster-codecs-MIT.txt).
|
|
41
41
|
|
|
@@ -46,7 +46,7 @@ The unmodified source is available at
|
|
|
46
46
|
https://github.com/vercel/satori/tree/a7e9338e15fec771b7e2cfb90df737789104edeb.
|
|
47
47
|
Its bundled dependencies retain their MIT and ISC licenses. Full licenses and
|
|
48
48
|
upstream notices are in [`licenses/satori-NOTICES.txt`](licenses/satori-NOTICES.txt).
|
|
49
|
-
Satori uses the package's
|
|
49
|
+
Satori uses the package's `fflate` 0.8.3 dependency.
|
|
50
50
|
|
|
51
51
|
## Fonts
|
|
52
52
|
|
|
@@ -62,7 +62,7 @@ Satori uses the package's patched `fflate` 0.7.5 dependency.
|
|
|
62
62
|
- Source Serif 4: SIL Open Font License 1.1.
|
|
63
63
|
Copyright 2014 The Source Serif 4 Project Authors.
|
|
64
64
|
Source: https://github.com/adobe-fonts/source-serif
|
|
65
|
-
`SourceSerif4-24pt-Regular.ttf` and `SourceSerif4-24pt-Italic.ttf` are static cuts of the variable fonts at optical size 24. `scripts/build-stage-fonts.py` makes them.
|
|
65
|
+
`SourceSerif4-24pt-Regular.ttf`, `SourceSerif4-24pt-SemiBold.ttf` and `SourceSerif4-24pt-Italic.ttf` are static cuts of the variable fonts at optical size 24. `scripts/build-stage-fonts.py` makes them.
|
|
66
66
|
- Noto Sans: SIL Open Font License 1.1.
|
|
67
67
|
Copyright Google Inc.
|
|
68
68
|
Source: https://github.com/notofonts/noto-fonts
|
|
Binary file
|
|
@@ -53,7 +53,7 @@ fflate
|
|
|
53
53
|
|
|
54
54
|
MIT License
|
|
55
55
|
|
|
56
|
-
Copyright (c)
|
|
56
|
+
Copyright (c) 2026 Arjun Barrett
|
|
57
57
|
|
|
58
58
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
59
59
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -71,4 +71,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
71
71
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
72
72
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
73
73
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
74
|
-
SOFTWARE.
|
|
74
|
+
SOFTWARE.
|
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
import {
|
|
2
|
+
frameToSvg,
|
|
3
|
+
isImageAsset
|
|
4
|
+
} from "./chunk-YNGDEARY.js";
|
|
5
|
+
|
|
6
|
+
// src/stage/raster.ts
|
|
7
|
+
import { createRequire } from "module";
|
|
8
|
+
import { cpus } from "os";
|
|
9
|
+
import { Worker } from "worker_threads";
|
|
10
|
+
import { fileURLToPath } from "url";
|
|
11
|
+
import parseCssColor from "parse-css-color";
|
|
12
|
+
|
|
13
|
+
// src/stage/bloom-kernel.mjs
|
|
14
|
+
function createBloom() {
|
|
15
|
+
const SPAN = 16;
|
|
16
|
+
const LOW_BYTE_FIRST = new Uint8Array(new Uint32Array([1]).buffer)[0] === 1;
|
|
17
|
+
function downsample(src, words, w, layer, background) {
|
|
18
|
+
const { factor, lw, lh, data, sums, finer } = layer;
|
|
19
|
+
sums.fill(0);
|
|
20
|
+
const row = lw * 3;
|
|
21
|
+
if (finer) {
|
|
22
|
+
const step = factor / finer.factor, from = finer.sums, fromRow = finer.lw * 3;
|
|
23
|
+
for (let y = 0; y < lh; y++) {
|
|
24
|
+
for (let yy = 0; yy < step; yy++) {
|
|
25
|
+
let i = y * row, o = (y * step + yy) * fromRow;
|
|
26
|
+
for (let x = 0; x < lw; x++, i += 3) {
|
|
27
|
+
let r = 0, g = 0, b = 0;
|
|
28
|
+
for (let xx = 0; xx < step; xx++, o += 3) {
|
|
29
|
+
r += from[o];
|
|
30
|
+
g += from[o + 1];
|
|
31
|
+
b += from[o + 2];
|
|
32
|
+
}
|
|
33
|
+
sums[i] += r;
|
|
34
|
+
sums[i + 1] += g;
|
|
35
|
+
sums[i + 2] += b;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} else if (words && factor <= 257) {
|
|
40
|
+
for (let y = 0; y < lh; y++) {
|
|
41
|
+
for (let yy = 0; yy < factor; yy++) {
|
|
42
|
+
let i = y * row, o = (y * factor + yy) * w;
|
|
43
|
+
for (let x = 0; x < lw; x++, i += 3) {
|
|
44
|
+
let rb = 0, g = 0;
|
|
45
|
+
for (let xx = 0; xx < factor; xx++, o++) {
|
|
46
|
+
const v = words[o];
|
|
47
|
+
rb += v & 16711935;
|
|
48
|
+
g += v & 65280;
|
|
49
|
+
}
|
|
50
|
+
sums[i] += rb & 65535;
|
|
51
|
+
sums[i + 1] += g >>> 8;
|
|
52
|
+
sums[i + 2] += rb >>> 16;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
for (let y = 0; y < lh; y++) {
|
|
58
|
+
for (let yy = 0; yy < factor; yy++) {
|
|
59
|
+
let i = y * row, o = (y * factor + yy) * w * 4;
|
|
60
|
+
for (let x = 0; x < lw; x++, i += 3) {
|
|
61
|
+
let r = 0, g = 0, b = 0;
|
|
62
|
+
for (let xx = 0; xx < factor; xx++, o += 4) {
|
|
63
|
+
r += src[o];
|
|
64
|
+
g += src[o + 1];
|
|
65
|
+
b += src[o + 2];
|
|
66
|
+
}
|
|
67
|
+
sums[i] += r;
|
|
68
|
+
sums[i + 1] += g;
|
|
69
|
+
sums[i + 2] += b;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const norm = 1 / (factor * factor), br = background[0], bg = background[1], bb = background[2];
|
|
75
|
+
for (let i = 0; i < data.length; i += 3) {
|
|
76
|
+
data[i] = Math.max(0, Math.fround(sums[i] * norm) - br);
|
|
77
|
+
data[i + 1] = Math.max(0, Math.fround(sums[i + 1] * norm) - bg);
|
|
78
|
+
data[i + 2] = Math.max(0, Math.fround(sums[i + 2] * norm) - bb);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function boxBlur(data, tmp, sums, w, h, radius) {
|
|
82
|
+
const norm = 1 / (radius * 2 + 1), row = w * 3, last = w - 1, bottom = h - 1;
|
|
83
|
+
for (let y = 0; y < h; y++) {
|
|
84
|
+
const base = y * row;
|
|
85
|
+
let r = 0, g = 0, b = 0;
|
|
86
|
+
for (let k = -radius; k <= radius; k++) {
|
|
87
|
+
const i = base + (k < 0 ? 0 : k < last ? k : last) * 3;
|
|
88
|
+
r += data[i];
|
|
89
|
+
g += data[i + 1];
|
|
90
|
+
b += data[i + 2];
|
|
91
|
+
}
|
|
92
|
+
for (let x = 0, o = base; x < w; x++, o += 3) {
|
|
93
|
+
tmp[o] = r * norm;
|
|
94
|
+
tmp[o + 1] = g * norm;
|
|
95
|
+
tmp[o + 2] = b * norm;
|
|
96
|
+
const hi = base + (x + radius < last ? x + radius + 1 : last) * 3, lo = base + (x > radius ? x - radius : 0) * 3;
|
|
97
|
+
r += data[hi] - data[lo];
|
|
98
|
+
g += data[hi + 1] - data[lo + 1];
|
|
99
|
+
b += data[hi + 2] - data[lo + 2];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
sums.fill(0, 0, row);
|
|
103
|
+
for (let k = -radius; k <= radius; k++) {
|
|
104
|
+
const base = (k < 0 ? 0 : k < bottom ? k : bottom) * row;
|
|
105
|
+
for (let i = 0; i < row; i++) sums[i] += tmp[base + i];
|
|
106
|
+
}
|
|
107
|
+
for (let y = 0; y < h; y++) {
|
|
108
|
+
const base = y * row;
|
|
109
|
+
const hi = (y + radius < bottom ? y + radius + 1 : bottom) * row, lo = (y > radius ? y - radius : 0) * row;
|
|
110
|
+
for (let i = 0; i < row; i++) {
|
|
111
|
+
data[base + i] = sums[i] * norm;
|
|
112
|
+
sums[i] += tmp[hi + i] - tmp[lo + i];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
let grainBytes = null, grainWidth = 0, grainHeight = 0;
|
|
117
|
+
function grainOf(w, h) {
|
|
118
|
+
if (grainBytes && grainWidth === w && grainHeight === h) return grainBytes;
|
|
119
|
+
const bytes = new Uint8Array(w * h);
|
|
120
|
+
for (let y = 0, i = 0; y < h; y++) {
|
|
121
|
+
const down = 583715e-8 * y;
|
|
122
|
+
for (let x = 0; x < w; x++, i++) {
|
|
123
|
+
const place = 0.06711056 * x + down;
|
|
124
|
+
const noise = 52.9829189 * (place - Math.floor(place));
|
|
125
|
+
bytes[i] = (noise - Math.floor(noise)) * 256;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
grainBytes = bytes;
|
|
129
|
+
grainWidth = w;
|
|
130
|
+
grainHeight = h;
|
|
131
|
+
return bytes;
|
|
132
|
+
}
|
|
133
|
+
let scratch = null;
|
|
134
|
+
function scratchOf(w, h) {
|
|
135
|
+
if (scratch && scratch.w === w && scratch.h === h) return scratch;
|
|
136
|
+
const layers = [];
|
|
137
|
+
const k = w / 1920;
|
|
138
|
+
for (const [base, radius, weight] of [[4, 3, 0.55], [8, 4, 0.3], [16, 4, 0.15]]) {
|
|
139
|
+
const factor = Math.max(1, Math.min(w, h, Math.round(base * k)));
|
|
140
|
+
const lw = Math.max(1, Math.floor(w / factor)), lh = Math.max(1, Math.floor(h / factor));
|
|
141
|
+
let finer = null;
|
|
142
|
+
for (const layer2 of layers) if (factor % layer2.factor === 0) finer = layer2;
|
|
143
|
+
const layer = { factor, lw, lh, finer, radius, weight, data: new Float32Array(lw * lh * 3), sums: new Int32Array(lw * lh * 3) };
|
|
144
|
+
layers.push(layer);
|
|
145
|
+
}
|
|
146
|
+
const tmp = new Float32Array(layers.reduce((most, layer) => Math.max(most, layer.data.length), 0));
|
|
147
|
+
const sums = new Float64Array(layers[0].lw * 3);
|
|
148
|
+
const spans = Math.ceil(w / SPAN);
|
|
149
|
+
for (const layer of layers) {
|
|
150
|
+
const { lw, factor } = layer;
|
|
151
|
+
const x0 = new Int32Array(w), x1 = new Int32Array(w), tx = new Float64Array(w), sx = new Float64Array(w);
|
|
152
|
+
for (let x = 0; x < w; x++) {
|
|
153
|
+
const fx = (x + 0.5) / factor - 0.5;
|
|
154
|
+
const a = Math.min(lw - 1, Math.max(0, Math.floor(fx))), b = Math.min(lw - 1, a + 1);
|
|
155
|
+
const t = Math.min(1, Math.max(0, fx - a));
|
|
156
|
+
x0[x] = a * 3;
|
|
157
|
+
x1[x] = b * 3;
|
|
158
|
+
tx[x] = t;
|
|
159
|
+
sx[x] = 1 - t;
|
|
160
|
+
}
|
|
161
|
+
layer.x0 = x0;
|
|
162
|
+
layer.x1 = x1;
|
|
163
|
+
layer.tx = tx;
|
|
164
|
+
layer.sx = sx;
|
|
165
|
+
layer.rows = [new Float64Array(w * 3), new Float64Array(w * 3)];
|
|
166
|
+
layer.most = [new Float64Array(spans), new Float64Array(spans)];
|
|
167
|
+
layer.held = [-1, -1];
|
|
168
|
+
layer.top = -1;
|
|
169
|
+
layer.low = -1;
|
|
170
|
+
layer.ty = 0;
|
|
171
|
+
layer.sy = 0;
|
|
172
|
+
}
|
|
173
|
+
const runs = new Int32Array(spans * 2), leasts = new Int16Array(spans), picks = new Int32Array(SPAN);
|
|
174
|
+
scratch = { w, h, layers, tmp, sums, spans, runs, leasts, picks };
|
|
175
|
+
return scratch;
|
|
176
|
+
}
|
|
177
|
+
function stretch(layer, index, slot, w, spans) {
|
|
178
|
+
const { data, x0, x1, tx, sx } = layer;
|
|
179
|
+
const out = layer.rows[slot], most = layer.most[slot], base = index * layer.lw * 3;
|
|
180
|
+
layer.held[slot] = index;
|
|
181
|
+
for (let span = 0; span < spans; span++) {
|
|
182
|
+
const from = span * SPAN, to = Math.min(w, from + SPAN);
|
|
183
|
+
let top = 0, least = 0;
|
|
184
|
+
for (let i = base + x0[from], end = base + x1[to - 1] + 3; i < end; i++) {
|
|
185
|
+
const v = data[i];
|
|
186
|
+
top = Math.max(top, v);
|
|
187
|
+
least = Math.min(least, v);
|
|
188
|
+
}
|
|
189
|
+
most[span] = least >= 0 ? top : Infinity;
|
|
190
|
+
if (top === 0 && least === 0) {
|
|
191
|
+
out.fill(0, from * 3, to * 3);
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
for (let x = from, o = from * 3; x < to; x++, o += 3) {
|
|
195
|
+
const a = base + x0[x], b = base + x1[x], t = tx[x], s = sx[x];
|
|
196
|
+
out[o] = data[a] * s + data[b] * t;
|
|
197
|
+
out[o + 1] = data[a + 1] * s + data[b + 1] * t;
|
|
198
|
+
out[o + 2] = data[a + 2] * s + data[b + 2] * t;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function bloom(pixels, w, h, amount, background, dither) {
|
|
203
|
+
const { layers, tmp, sums, spans, runs, picks } = scratchOf(w, h);
|
|
204
|
+
const words = LOW_BYTE_FIRST && pixels.byteOffset % 4 === 0 ? new Uint32Array(pixels.buffer, pixels.byteOffset, w * h) : null;
|
|
205
|
+
for (const layer of layers) downsample(pixels, words, w, layer, background);
|
|
206
|
+
for (const layer of layers) {
|
|
207
|
+
boxBlur(layer.data, tmp, sums, layer.lw, layer.lh, layer.radius);
|
|
208
|
+
boxBlur(layer.data, tmp, sums, layer.lw, layer.lh, layer.radius);
|
|
209
|
+
layer.held[0] = -1;
|
|
210
|
+
layer.held[1] = -1;
|
|
211
|
+
}
|
|
212
|
+
const l0 = layers[0], l1 = layers[1], l2 = layers[2];
|
|
213
|
+
const w0 = l0.weight, w1 = l1.weight, w2 = l2.weight;
|
|
214
|
+
const leasts = dither ? scratch.leasts : null;
|
|
215
|
+
const grain = dither ? grainOf(w, h) : null;
|
|
216
|
+
const STEP = 1 / 256, HALF = 0.5 / 256, INV = 1 / 255, gain = amount * 2.4;
|
|
217
|
+
let count = 0;
|
|
218
|
+
for (let y = 0; y < h; y++) {
|
|
219
|
+
let moved = false;
|
|
220
|
+
for (const layer of layers) {
|
|
221
|
+
const fy = (y + 0.5) / layer.factor - 0.5;
|
|
222
|
+
const y0 = Math.min(layer.lh - 1, Math.max(0, Math.floor(fy))), y1 = Math.min(layer.lh - 1, y0 + 1);
|
|
223
|
+
layer.ty = Math.min(1, Math.max(0, fy - y0));
|
|
224
|
+
layer.sy = 1 - layer.ty;
|
|
225
|
+
let top = layer.held[0] === y0 ? 0 : layer.held[1] === y0 ? 1 : -1;
|
|
226
|
+
if (top < 0) {
|
|
227
|
+
top = layer.held[0] === y1 ? 1 : 0;
|
|
228
|
+
stretch(layer, y0, top, w, spans);
|
|
229
|
+
moved = true;
|
|
230
|
+
}
|
|
231
|
+
let low = layer.held[0] === y1 ? 0 : layer.held[1] === y1 ? 1 : -1;
|
|
232
|
+
if (low < 0) {
|
|
233
|
+
low = 1 - top;
|
|
234
|
+
stretch(layer, y1, low, w, spans);
|
|
235
|
+
moved = true;
|
|
236
|
+
}
|
|
237
|
+
layer.top = top;
|
|
238
|
+
layer.low = low;
|
|
239
|
+
}
|
|
240
|
+
if (moved) {
|
|
241
|
+
const m0 = l0.most[l0.top], n0 = l0.most[l0.low], m1 = l1.most[l1.top], n1 = l1.most[l1.low], m2 = l2.most[l2.top], n2 = l2.most[l2.low];
|
|
242
|
+
count = 0;
|
|
243
|
+
for (let span = 0; span < spans; span++) {
|
|
244
|
+
const c0 = m0[span] > n0[span] ? m0[span] : n0[span], c1 = m1[span] > n1[span] ? m1[span] : n1[span], c2 = m2[span] > n2[span] ? m2[span] : n2[span];
|
|
245
|
+
const limit = (c0 * w0 + c1 * w1 + c2 * w2) * amount * 2.4;
|
|
246
|
+
const dim = limit >= 0 && limit < 0.999;
|
|
247
|
+
const from = span * SPAN, to = Math.min(w, from + SPAN);
|
|
248
|
+
if (!leasts) {
|
|
249
|
+
if (dim) continue;
|
|
250
|
+
if (count > 0 && runs[count - 1] === from) runs[count - 1] = to;
|
|
251
|
+
else {
|
|
252
|
+
runs[count] = from;
|
|
253
|
+
runs[count + 1] = to;
|
|
254
|
+
count += 2;
|
|
255
|
+
}
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
if (limit === 0) continue;
|
|
259
|
+
if (dim) {
|
|
260
|
+
const least = Math.ceil((0.999999 - limit) * 256 - 0.5);
|
|
261
|
+
if (least > 255) continue;
|
|
262
|
+
runs[count] = from;
|
|
263
|
+
runs[count + 1] = to;
|
|
264
|
+
leasts[count >> 1] = least;
|
|
265
|
+
count += 2;
|
|
266
|
+
} else if (count > 0 && runs[count - 1] === from && leasts[(count >> 1) - 1] < 0) runs[count - 1] = to;
|
|
267
|
+
else {
|
|
268
|
+
runs[count] = from;
|
|
269
|
+
runs[count + 1] = to;
|
|
270
|
+
leasts[count >> 1] = -1;
|
|
271
|
+
count += 2;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
const a0 = l0.rows[l0.top], b0 = l0.rows[l0.low], a1 = l1.rows[l1.top], b1 = l1.rows[l1.low], a2 = l2.rows[l2.top], b2 = l2.rows[l2.low];
|
|
276
|
+
const s0 = l0.sy, t0 = l0.ty, s1 = l1.sy, t1 = l1.ty, s2 = l2.sy, t2 = l2.ty;
|
|
277
|
+
const line = y * w * 4;
|
|
278
|
+
if (!leasts) {
|
|
279
|
+
for (let n = 0; n < count; n += 2) {
|
|
280
|
+
for (let o = runs[n] * 3, p = line + runs[n] * 4, end = runs[n + 1] * 3; o < end; o += 3, p += 4) {
|
|
281
|
+
const r = 0 + (a0[o] * s0 + b0[o] * t0) * w0 + (a1[o] * s1 + b1[o] * t1) * w1 + (a2[o] * s2 + b2[o] * t2) * w2;
|
|
282
|
+
const g = 0 + (a0[o + 1] * s0 + b0[o + 1] * t0) * w0 + (a1[o + 1] * s1 + b1[o + 1] * t1) * w1 + (a2[o + 1] * s2 + b2[o + 1] * t2) * w2;
|
|
283
|
+
const b = 0 + (a0[o + 2] * s0 + b0[o + 2] * t0) * w0 + (a1[o + 2] * s1 + b1[o + 2] * t1) * w1 + (a2[o + 2] * s2 + b2[o + 2] * t2) * w2;
|
|
284
|
+
pixels[p] = 255 - (255 - pixels[p]) * (255 - Math.min(255, r * amount * 2.4)) / 255;
|
|
285
|
+
pixels[p + 1] = 255 - (255 - pixels[p + 1]) * (255 - Math.min(255, g * amount * 2.4)) / 255;
|
|
286
|
+
pixels[p + 2] = 255 - (255 - pixels[p + 2]) * (255 - Math.min(255, b * amount * 2.4)) / 255;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
const A0 = s0 * w0 * gain, B0 = t0 * w0 * gain, A1 = s1 * w1 * gain, B1 = t1 * w1 * gain, A2 = s2 * w2 * gain, B2 = t2 * w2 * gain;
|
|
292
|
+
const rowGrain = y * w;
|
|
293
|
+
for (let n = 0; n < count; n += 2) {
|
|
294
|
+
const least = leasts[n >> 1];
|
|
295
|
+
if (least < 0) {
|
|
296
|
+
for (let x = runs[n], o = x * 3, p = line + x * 4, i = rowGrain + x, end = runs[n + 1]; x < end; x++, o += 3, p += 4, i++) {
|
|
297
|
+
const t = pixels[p + 3] === 0 ? 0 : grain[i] * STEP + HALF;
|
|
298
|
+
const r = a0[o] * A0 + b0[o] * B0 + a1[o] * A1 + b1[o] * B1 + a2[o] * A2 + b2[o] * B2;
|
|
299
|
+
const g = a0[o + 1] * A0 + b0[o + 1] * B0 + a1[o + 1] * A1 + b1[o + 1] * B1 + a2[o + 1] * A2 + b2[o + 1] * B2;
|
|
300
|
+
const b = a0[o + 2] * A0 + b0[o + 2] * B0 + a1[o + 2] * A1 + b1[o + 2] * B1 + a2[o + 2] * A2 + b2[o + 2] * B2;
|
|
301
|
+
const pr = pixels[p], pg = pixels[p + 1], pb = pixels[p + 2];
|
|
302
|
+
pixels[p] = pr + Math.min(255, r) * (255 - pr) * INV + t;
|
|
303
|
+
pixels[p + 1] = pg + Math.min(255, g) * (255 - pg) * INV + t;
|
|
304
|
+
pixels[p + 2] = pb + Math.min(255, b) * (255 - pb) * INV + t;
|
|
305
|
+
}
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
let found = 0;
|
|
309
|
+
for (let x = runs[n], i = rowGrain + x, end = runs[n + 1]; x < end; x++, i++) {
|
|
310
|
+
picks[found] = x;
|
|
311
|
+
found += grain[i] - least >>> 31 ^ 1;
|
|
312
|
+
}
|
|
313
|
+
for (let j = 0; j < found; j++) {
|
|
314
|
+
const x = picks[j], o = x * 3, p = line + x * 4;
|
|
315
|
+
const t = grain[rowGrain + x] * STEP + HALF, q = pixels[p + 3] === 0 ? 0 : INV;
|
|
316
|
+
const r = a0[o] * A0 + b0[o] * B0 + a1[o] * A1 + b1[o] * B1 + a2[o] * A2 + b2[o] * B2;
|
|
317
|
+
const g = a0[o + 1] * A0 + b0[o + 1] * B0 + a1[o + 1] * A1 + b1[o + 1] * B1 + a2[o + 1] * A2 + b2[o + 1] * B2;
|
|
318
|
+
const b = a0[o + 2] * A0 + b0[o + 2] * B0 + a1[o + 2] * A1 + b1[o + 2] * B1 + a2[o + 2] * A2 + b2[o + 2] * B2;
|
|
319
|
+
const pr = pixels[p], pg = pixels[p + 1], pb = pixels[p + 2];
|
|
320
|
+
pixels[p] = pr + r * (255 - pr) * q + t;
|
|
321
|
+
pixels[p + 1] = pg + g * (255 - pg) * q + t;
|
|
322
|
+
pixels[p + 2] = pb + b * (255 - pb) * q + t;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return bloom;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// src/stage/raster.ts
|
|
331
|
+
var embeddedJobs = /* @__PURE__ */ new WeakMap();
|
|
332
|
+
function embeddedResources(frame) {
|
|
333
|
+
if (typeof frame.width !== "number" || typeof frame.height !== "number") return false;
|
|
334
|
+
if (frame.background !== null && (typeof frame.background !== "string" || !parseCssColor(frame.background))) return false;
|
|
335
|
+
for (const op of frame.ops) {
|
|
336
|
+
if (op.kind === "image") {
|
|
337
|
+
const { asset, src, width, height } = op.image;
|
|
338
|
+
if (!op.image.pixels && (!isImageAsset(asset) || src !== asset.src) || typeof width !== "number" || typeof height !== "number") return false;
|
|
339
|
+
if (op.blend !== void 0 && !["normal", "screen", "multiply", "overlay"].includes(op.blend)) return false;
|
|
340
|
+
} else {
|
|
341
|
+
if (!["normal", "screen", "multiply", "overlay"].includes(op.blend)) return false;
|
|
342
|
+
if (!["round", "butt", "square"].includes(op.cap) || !["round", "miter", "bevel"].includes(op.join)) return false;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
var BLOOM_SOURCE = `const __name = (fn, name) => Object.defineProperty(fn, 'name', { value: name, configurable: true }); const bloom = (${createBloom.toString()})()`;
|
|
348
|
+
var RASTER_WORKER_SOURCE = String.raw`
|
|
349
|
+
const { parentPort, workerData } = require('node:worker_threads')
|
|
350
|
+
const { Resvg } = require(workerData.resvgPath)
|
|
351
|
+
|
|
352
|
+
${BLOOM_SOURCE}
|
|
353
|
+
function raster(svg, width, height) {
|
|
354
|
+
const image = new Resvg(svg, { fitTo: { mode: 'width', value: width }, font: { loadSystemFonts: false } }).render()
|
|
355
|
+
// Each read of the pixels makes a copy of the whole image, so read them once.
|
|
356
|
+
const pixels = image.pixels
|
|
357
|
+
if (image.height === height) return pixels
|
|
358
|
+
// Rounding can differ by a row at unusual sizes. Keep the requested size exactly.
|
|
359
|
+
const out = new Uint8Array(width * height * 4)
|
|
360
|
+
for (let y = 0; y < height; y++) {
|
|
361
|
+
const row = Math.min(image.height - 1, y) * width * 4
|
|
362
|
+
out.set(pixels.subarray(row, row + width * 4), y * width * 4)
|
|
363
|
+
}
|
|
364
|
+
return out
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// The pixels of a sub-frame as 32-bit numbers. Such a view needs a start on a multiple of 4.
|
|
368
|
+
function pixelWords(px) {
|
|
369
|
+
if (px.byteOffset % 4 !== 0) px = new Uint8Array(px)
|
|
370
|
+
return new Uint32Array(px.buffer, px.byteOffset, px.length >> 2)
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Add up to four sub-frames, each times its count, in one pass over the memory. One number holds the sums
|
|
374
|
+
// of two bytes of a pixel in its two halves, and a second number holds the other two bytes.
|
|
375
|
+
// With a table, write the bytes of the average to the output. Without one, add to the sums.
|
|
376
|
+
function addSubFrames(group, counts, low, high, table, out) {
|
|
377
|
+
const a = group[0], b = group[1] || a, c = group[2] || a, d = group[3] || a
|
|
378
|
+
const ca = counts[0], cb = counts[1] || 0, cc = counts[2] || 0, cd = counts[3] || 0
|
|
379
|
+
if (!table) {
|
|
380
|
+
for (let i = 0; i < a.length; i++) {
|
|
381
|
+
const va = a[i], vb = b[i], vc = c[i], vd = d[i]
|
|
382
|
+
low[i] += (va & 0xff00ff) * ca + (vb & 0xff00ff) * cb + (vc & 0xff00ff) * cc + (vd & 0xff00ff) * cd
|
|
383
|
+
high[i] += ((va >>> 8) & 0xff00ff) * ca + ((vb >>> 8) & 0xff00ff) * cb + ((vc >>> 8) & 0xff00ff) * cc + ((vd >>> 8) & 0xff00ff) * cd
|
|
384
|
+
}
|
|
385
|
+
return
|
|
386
|
+
}
|
|
387
|
+
for (let i = 0; i < a.length; i++) {
|
|
388
|
+
const va = a[i], vb = b[i], vc = c[i], vd = d[i]
|
|
389
|
+
const l = (va & 0xff00ff) * ca + (vb & 0xff00ff) * cb + (vc & 0xff00ff) * cc + (vd & 0xff00ff) * cd
|
|
390
|
+
const h = ((va >>> 8) & 0xff00ff) * ca + ((vb >>> 8) & 0xff00ff) * cb + ((vc >>> 8) & 0xff00ff) * cc + ((vd >>> 8) & 0xff00ff) * cd
|
|
391
|
+
// Each byte goes back to the place in the number that it came from, so the order of the bytes in
|
|
392
|
+
// memory does not matter.
|
|
393
|
+
out[i] = table[l & 0xffff] | table[h & 0xffff] << 8 | table[l >>> 16] << 16 | table[h >>> 16] << 24
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// Motion blur: the average of the sub-frames across the shutter.
|
|
398
|
+
function average(svgs, width, height, clock) {
|
|
399
|
+
const n = svgs.length
|
|
400
|
+
// Up to 257 sub-frames, the sum of a byte fits in 16 bits, so two sums share one 32-bit number.
|
|
401
|
+
const packed = n <= 257
|
|
402
|
+
let group = [], counts = [], low = null, high = null, sum = null
|
|
403
|
+
for (let k = 0; k < n;) {
|
|
404
|
+
// Sub-frames next to each other with the same SVG text give the same pixels. Rasterize the text once,
|
|
405
|
+
// and count it once for each of them.
|
|
406
|
+
let count = 1
|
|
407
|
+
while (k + count < n && svgs[k + count] === svgs[k]) count++
|
|
408
|
+
const before = performance.now()
|
|
409
|
+
const px = raster(svgs[k], width, height)
|
|
410
|
+
clock.rasterMs += performance.now() - before
|
|
411
|
+
// The average of equal sub-frames is the sub-frame.
|
|
412
|
+
if (count === n) return new Uint8Array(px)
|
|
413
|
+
if (!packed) {
|
|
414
|
+
if (!sum) sum = new Uint32Array(px.length)
|
|
415
|
+
for (let i = 0; i < px.length; i++) sum[i] += px[i] * count
|
|
416
|
+
} else {
|
|
417
|
+
if (group.length === 4) {
|
|
418
|
+
// A fifth sub-frame arrives. Add the four before it to the sums, so that no more than four
|
|
419
|
+
// sub-frames stay in memory.
|
|
420
|
+
if (!low) { low = new Uint32Array(width * height); high = new Uint32Array(width * height) }
|
|
421
|
+
addSubFrames(group, counts, low, high, null, null)
|
|
422
|
+
group = []; counts = []
|
|
423
|
+
}
|
|
424
|
+
group.push(pixelWords(px)); counts.push(count)
|
|
425
|
+
}
|
|
426
|
+
k += count
|
|
427
|
+
}
|
|
428
|
+
// A sum has one result. A table gives the division and the rounding once for each sum, not for each byte.
|
|
429
|
+
const table = new Uint8Array(255 * n + 1)
|
|
430
|
+
for (let i = 0; i < table.length; i++) table[i] = i / n + 0.5
|
|
431
|
+
const out = new Uint8Array(width * height * 4), words = new Uint32Array(out.buffer)
|
|
432
|
+
if (!packed) {
|
|
433
|
+
for (let i = 0; i < out.length; i++) out[i] = table[sum[i]]
|
|
434
|
+
} else if (!low) {
|
|
435
|
+
// Four sub-frames or fewer, as at the quality high. One pass gives the result, with no sums in memory.
|
|
436
|
+
addSubFrames(group, counts, null, null, table, words)
|
|
437
|
+
} else {
|
|
438
|
+
addSubFrames(group, counts, low, high, null, null)
|
|
439
|
+
for (let i = 0; i < words.length; i++) {
|
|
440
|
+
const l = low[i], h = high[i]
|
|
441
|
+
words[i] = table[l & 0xffff] | table[h & 0xffff] << 8 | table[l >>> 16] << 16 | table[h >>> 16] << 24
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return out
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
parentPort.on('message', (job) => {
|
|
448
|
+
try {
|
|
449
|
+
// The time of each step goes back with the pixels, for the export benchmark.
|
|
450
|
+
const clock = { rasterMs: 0 }
|
|
451
|
+
const started = performance.now()
|
|
452
|
+
let out
|
|
453
|
+
if (job.svgs.length === 1) {
|
|
454
|
+
out = new Uint8Array(raster(job.svgs[0], job.width, job.height))
|
|
455
|
+
clock.rasterMs = performance.now() - started
|
|
456
|
+
} else {
|
|
457
|
+
out = average(job.svgs, job.width, job.height, clock)
|
|
458
|
+
}
|
|
459
|
+
const averageMs = performance.now() - started - clock.rasterMs
|
|
460
|
+
const beforeBloom = performance.now()
|
|
461
|
+
// With motion blur, the bloom and its dither come once, after the average of the shutter samples. The
|
|
462
|
+
// average cuts each value to a whole level, so grain in a sample would not reach the frame. The bloom is
|
|
463
|
+
// the last write before the encoder, so nothing after it rounds the grain away.
|
|
464
|
+
if (job.bloom > 0) bloom(out, job.width, job.height, job.bloom, job.background, job.dither !== false)
|
|
465
|
+
const bloomMs = performance.now() - beforeBloom
|
|
466
|
+
parentPort.postMessage({ id: job.id, pixels: out, rasterMs: clock.rasterMs, averageMs, bloomMs }, [out.buffer])
|
|
467
|
+
} catch (error) {
|
|
468
|
+
parentPort.postMessage({ id: job.id, error: String(error && error.message || error) })
|
|
469
|
+
}
|
|
470
|
+
})
|
|
471
|
+
`;
|
|
472
|
+
var statsListener;
|
|
473
|
+
function reportExportStats(stats) {
|
|
474
|
+
statsListener?.(stats);
|
|
475
|
+
}
|
|
476
|
+
function resolveResvg() {
|
|
477
|
+
const here = typeof __filename !== "undefined" ? __filename : fileURLToPath(import.meta.url);
|
|
478
|
+
return createRequire(here).resolve("@resvg/resvg-js");
|
|
479
|
+
}
|
|
480
|
+
var RasterPool = class {
|
|
481
|
+
workers = [];
|
|
482
|
+
idle = [];
|
|
483
|
+
queue = [];
|
|
484
|
+
pending = /* @__PURE__ */ new Map();
|
|
485
|
+
nextId = 1;
|
|
486
|
+
failure = null;
|
|
487
|
+
closing = false;
|
|
488
|
+
/** Counts and times of the jobs of this pool. The export adds its own waits. */
|
|
489
|
+
stats = {
|
|
490
|
+
workers: 0,
|
|
491
|
+
computed: 0,
|
|
492
|
+
reused: 0,
|
|
493
|
+
mainMs: 0,
|
|
494
|
+
rasterMs: 0,
|
|
495
|
+
averageMs: 0,
|
|
496
|
+
bloomMs: 0,
|
|
497
|
+
frameWaitMs: 0,
|
|
498
|
+
encoderWaitMs: 0,
|
|
499
|
+
encoderFinishMs: 0
|
|
500
|
+
};
|
|
501
|
+
constructor(size = defaultPoolSize()) {
|
|
502
|
+
const resvgPath = resolveResvg();
|
|
503
|
+
this.stats.workers = size;
|
|
504
|
+
for (let i = 0; i < size; i++) {
|
|
505
|
+
const execArgv = process.execArgv.filter((arg, i2, args) => arg !== "--input-type" && !arg.startsWith("--input-type=") && args[i2 - 1] !== "--input-type");
|
|
506
|
+
const worker = new Worker(RASTER_WORKER_SOURCE, { eval: true, execArgv, workerData: { resvgPath } });
|
|
507
|
+
worker.on("message", (message) => {
|
|
508
|
+
const entry = this.pending.get(message.id);
|
|
509
|
+
this.pending.delete(message.id);
|
|
510
|
+
if (!this.failure && !this.closing) {
|
|
511
|
+
this.idle.push(worker);
|
|
512
|
+
this.pump();
|
|
513
|
+
}
|
|
514
|
+
if (!entry) return;
|
|
515
|
+
if (message.error || !message.pixels) {
|
|
516
|
+
entry.reject(new Error(`Frame rasterization failed: ${message.error}`));
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
this.stats.computed++;
|
|
520
|
+
this.stats.rasterMs += message.rasterMs ?? 0;
|
|
521
|
+
this.stats.averageMs += message.averageMs ?? 0;
|
|
522
|
+
this.stats.bloomMs += message.bloomMs ?? 0;
|
|
523
|
+
entry.resolve(message.pixels);
|
|
524
|
+
});
|
|
525
|
+
worker.on("error", (error) => this.fail(error instanceof Error ? error : new Error(String(error))));
|
|
526
|
+
worker.on("exit", (code) => {
|
|
527
|
+
if (this.closing) return;
|
|
528
|
+
const index = this.idle.indexOf(worker);
|
|
529
|
+
if (index >= 0) this.idle.splice(index, 1);
|
|
530
|
+
if (code !== 0 || this.pending.size > 0 || this.queue.length > 0) {
|
|
531
|
+
this.fail(new Error(`Raster worker exited unexpectedly with code ${code}.`));
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
this.workers.push(worker);
|
|
535
|
+
this.idle.push(worker);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
get size() {
|
|
539
|
+
return this.workers.length;
|
|
540
|
+
}
|
|
541
|
+
fail(error) {
|
|
542
|
+
this.failure ??= error;
|
|
543
|
+
this.queue.length = 0;
|
|
544
|
+
this.idle.length = 0;
|
|
545
|
+
for (const entry of this.pending.values()) entry.reject(this.failure);
|
|
546
|
+
this.pending.clear();
|
|
547
|
+
}
|
|
548
|
+
pump() {
|
|
549
|
+
if (this.failure || this.closing) return;
|
|
550
|
+
while (this.idle.length > 0 && this.queue.length > 0) {
|
|
551
|
+
const worker = this.idle.pop();
|
|
552
|
+
const { job, id } = this.queue.shift();
|
|
553
|
+
const started = performance.now();
|
|
554
|
+
worker.postMessage({ id, ...job });
|
|
555
|
+
this.stats.mainMs += performance.now() - started;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
run(job) {
|
|
559
|
+
if (this.failure) return Promise.reject(this.failure);
|
|
560
|
+
const id = this.nextId++;
|
|
561
|
+
return new Promise((resolve, reject) => {
|
|
562
|
+
this.pending.set(id, { resolve, reject });
|
|
563
|
+
this.queue.push({ job, id });
|
|
564
|
+
this.pump();
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
async close() {
|
|
568
|
+
this.closing = true;
|
|
569
|
+
this.fail(new Error("The raster pool was closed."));
|
|
570
|
+
await Promise.all(this.workers.map((worker) => worker.terminate()));
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
function sameRasterJob(a, b) {
|
|
574
|
+
const admitted = (job) => {
|
|
575
|
+
const original = embeddedJobs.get(job);
|
|
576
|
+
return !!original && original.length === job.svgs.length && original.every((svg, i) => svg === job.svgs[i]);
|
|
577
|
+
};
|
|
578
|
+
return a.width === b.width && a.height === b.height && a.bloom === b.bloom && a.dither !== false === (b.dither !== false) && a.background.length === b.background.length && a.background.every((channel, i) => channel === b.background[i]) && a.svgs.length === b.svgs.length && admitted(a) && admitted(b) && a.svgs.every((svg, i) => svg === b.svgs[i]);
|
|
579
|
+
}
|
|
580
|
+
function defaultPoolSize() {
|
|
581
|
+
return Math.max(1, Math.min(12, cpus().length - 2));
|
|
582
|
+
}
|
|
583
|
+
function framesToJob(frames, width, parseBackground, height, dither = true) {
|
|
584
|
+
const first = frames[0];
|
|
585
|
+
const scale = width / first.width;
|
|
586
|
+
const job = {
|
|
587
|
+
svgs: frames.map((frame) => frameToSvg(frame)),
|
|
588
|
+
width,
|
|
589
|
+
height: height ?? Math.round(first.height * scale),
|
|
590
|
+
bloom: first.dark ? first.bloom : 0,
|
|
591
|
+
background: parseBackground(first.background),
|
|
592
|
+
dither
|
|
593
|
+
};
|
|
594
|
+
if (frames.every(embeddedResources)) embeddedJobs.set(job, job.svgs.slice());
|
|
595
|
+
return job;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
export {
|
|
599
|
+
reportExportStats,
|
|
600
|
+
RasterPool,
|
|
601
|
+
sameRasterJob,
|
|
602
|
+
defaultPoolSize,
|
|
603
|
+
framesToJob
|
|
604
|
+
};
|