@beff/aula 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/dist/generated/aula.core5.wasm +0 -0
- package/dist/index.d.ts +7304 -6
- package/dist/index.js +14864 -11
- package/dist/index.js.map +1 -0
- package/package.json +8 -25
- package/dist/animations.d.ts +0 -635
- package/dist/animations.js +0 -1778
- package/dist/braces.d.ts +0 -84
- package/dist/braces.js +0 -253
- package/dist/card.d.ts +0 -17
- package/dist/card.js +0 -90
- package/dist/colors-palette.d.ts +0 -2242
- package/dist/colors-palette.js +0 -2241
- package/dist/colors.d.ts +0 -120
- package/dist/colors.js +0 -403
- package/dist/curves.d.ts +0 -10
- package/dist/curves.js +0 -128
- package/dist/engine.d.ts +0 -1705
- package/dist/engine.js +0 -3561
- package/dist/errors.d.ts +0 -19
- package/dist/errors.js +0 -25
- package/dist/geometry.d.ts +0 -551
- package/dist/geometry.js +0 -1555
- package/dist/graph.d.ts +0 -117
- package/dist/graph.js +0 -526
- package/dist/graphing.d.ts +0 -597
- package/dist/graphing.js +0 -1504
- package/dist/host.d.ts +0 -19
- package/dist/host.js +0 -85
- package/dist/lexicon.d.ts +0 -10
- package/dist/lexicon.js +0 -23
- package/dist/load.d.ts +0 -21
- package/dist/load.js +0 -109
- package/dist/logo.d.ts +0 -30
- package/dist/logo.js +0 -186
- package/dist/manim.d.ts +0 -22
- package/dist/manim.js +0 -19
- package/dist/media.d.ts +0 -34
- package/dist/media.js +0 -391
- package/dist/mobjects.d.ts +0 -127
- package/dist/mobjects.js +0 -316
- package/dist/picture.d.ts +0 -23
- package/dist/picture.js +0 -291
- package/dist/pointcloud.d.ts +0 -144
- package/dist/pointcloud.js +0 -384
- package/dist/rates.d.ts +0 -19
- package/dist/rates.js +0 -54
- package/dist/run.d.ts +0 -101
- package/dist/run.js +0 -323
- package/dist/tables.d.ts +0 -179
- package/dist/tables.js +0 -403
- package/dist/tex.d.ts +0 -8
- package/dist/tex.js +0 -257
- package/dist/text-layouts.d.ts +0 -64
- package/dist/text-layouts.js +0 -225
- package/dist/text.d.ts +0 -61
- package/dist/text.js +0 -78
- package/dist/theme.d.ts +0 -25
- package/dist/theme.js +0 -12
- package/dist/user-code.d.ts +0 -5
- package/dist/user-code.js +0 -20
- package/dist/utils.d.ts +0 -152
- package/dist/utils.js +0 -517
- package/dist/vector-field.d.ts +0 -132
- package/dist/vector-field.js +0 -361
package/dist/mobjects.js
DELETED
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
// Manim CE's groups, points, frames, trackers and debug helpers that the engine draws with its
|
|
2
|
-
// own shapes: Group, VDict, VectorizedPoint, CurvesAsSubmobjects, ScreenRectangle,
|
|
3
|
-
// FullScreenRectangle, ComplexValueTracker, Variable, printFamily and indexLabels.
|
|
4
|
-
import { Animation, BLACK, DecimalNumber, FRAME_HEIGHT, Integer, MathTex, Mobject, ORIGIN, RIGHT, LEFT, Polyline, Tex, VGroup, ValueTracker, raw, wrong, } from "./engine.js";
|
|
5
|
-
import { AnimationGroup } from "./animations.js";
|
|
6
|
-
import { Rectangle } from "./geometry.js";
|
|
7
|
-
import { engine } from "./load.js";
|
|
8
|
-
/** Manim's `Group`: any mobjects as one. aula draws every mobject as vectors, so it is a VGroup. */
|
|
9
|
-
export function Group(...mobjects) {
|
|
10
|
-
return VGroup(...mobjects);
|
|
11
|
-
}
|
|
12
|
-
function entriesOf(m) {
|
|
13
|
-
return Symbol.iterator in m ? [...m] : Object.entries(m);
|
|
14
|
-
}
|
|
15
|
-
/** True for a Mobject or an array of them (not VDict entries). */
|
|
16
|
-
function isMobjects(v) {
|
|
17
|
-
return v instanceof Mobject || (Array.isArray(v) && v.every((x) => x instanceof Mobject));
|
|
18
|
-
}
|
|
19
|
-
/** A VGroup whose submobjects have keys: Manim's `VDict`. `get(key)` is Manim's `vdict[key]`. */
|
|
20
|
-
export class VDictMobject extends Mobject {
|
|
21
|
-
/** The submobject of each key (Manim `submob_dict`). */
|
|
22
|
-
submobDict = new Map();
|
|
23
|
-
showKeys = false;
|
|
24
|
-
/** Adds each entry (Manim `add`); a Mobject is added as a plain submobject. */
|
|
25
|
-
add(...mappingOrIterable) {
|
|
26
|
-
for (const item of mappingOrIterable) {
|
|
27
|
-
if (isMobjects(item))
|
|
28
|
-
super.add(item);
|
|
29
|
-
else
|
|
30
|
-
for (const [key, value] of entriesOf(item))
|
|
31
|
-
this.addKeyValuePair(key, value);
|
|
32
|
-
}
|
|
33
|
-
return this;
|
|
34
|
-
}
|
|
35
|
-
/** Removes the submobject of `key` (Manim `remove(key)`). A key that is not there is an error. */
|
|
36
|
-
remove(...key) {
|
|
37
|
-
for (const k of key) {
|
|
38
|
-
if (typeof k !== "string" && typeof k !== "number") {
|
|
39
|
-
super.remove(k);
|
|
40
|
-
continue;
|
|
41
|
-
}
|
|
42
|
-
const value = this.submobDict.get(k);
|
|
43
|
-
if (value === undefined)
|
|
44
|
-
wrong("VDict.remove", `the key ${JSON.stringify(k)} is not in the VDict`);
|
|
45
|
-
super.remove(value);
|
|
46
|
-
this.submobDict.delete(k);
|
|
47
|
-
}
|
|
48
|
-
return this;
|
|
49
|
-
}
|
|
50
|
-
/** The submobject of `key`, or undefined (Manim `vdict[key]`). */
|
|
51
|
-
get(key) {
|
|
52
|
-
return this.submobDict.get(key);
|
|
53
|
-
}
|
|
54
|
-
/** `set(key, value)` replaces the submobject of `key` (Manim `vdict[key] = value`).
|
|
55
|
-
* `set(attributes)` is `Mobject.set`. */
|
|
56
|
-
set(keyOrAttributes, value) {
|
|
57
|
-
if (typeof keyOrAttributes === "object")
|
|
58
|
-
return super.set(keyOrAttributes);
|
|
59
|
-
if (value === undefined)
|
|
60
|
-
return wrong("VDict.set", "give the value of the key");
|
|
61
|
-
if (this.submobDict.has(keyOrAttributes))
|
|
62
|
-
this.remove(keyOrAttributes);
|
|
63
|
-
return this.addKeyValuePair(keyOrAttributes, value);
|
|
64
|
-
}
|
|
65
|
-
has(key) {
|
|
66
|
-
return this.submobDict.has(key);
|
|
67
|
-
}
|
|
68
|
-
/** The values (Manim `get_all_submobjects`). */
|
|
69
|
-
getAllSubmobjects() {
|
|
70
|
-
return [...this.submobDict.values()];
|
|
71
|
-
}
|
|
72
|
-
/** Adds `value` with `key`. With `showKeys`, a `Tex` of the key goes to its left. */
|
|
73
|
-
addKeyValuePair(key, value) {
|
|
74
|
-
if (this.showKeys)
|
|
75
|
-
value.add(Tex(String(key)).nextTo(value, LEFT));
|
|
76
|
-
this.submobDict.set(key, value);
|
|
77
|
-
super.add(value);
|
|
78
|
-
return this;
|
|
79
|
-
}
|
|
80
|
-
copy() {
|
|
81
|
-
const c = new VDictMobject(raw(this).copy());
|
|
82
|
-
c.showKeys = this.showKeys;
|
|
83
|
-
const children = c.submobjects;
|
|
84
|
-
[...this.submobDict.keys()].forEach((k, i) => {
|
|
85
|
-
const child = children[i];
|
|
86
|
-
if (child !== undefined)
|
|
87
|
-
c.submobDict.set(k, child);
|
|
88
|
-
});
|
|
89
|
-
return c;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
/** Manim's `VDict(mappingOrIterable, { showKeys })`: `VDict({ a: square, b: circle })`. */
|
|
93
|
-
export function VDict(mappingOrIterable = {}, opts = {}) {
|
|
94
|
-
const d = new VDictMobject(engine.vGroup([]));
|
|
95
|
-
d.showKeys = opts.showKeys ?? false;
|
|
96
|
-
return d.add(mappingOrIterable);
|
|
97
|
-
}
|
|
98
|
-
/** One point with a small artificial size: Manim's `VectorizedPoint`. */
|
|
99
|
-
export class VectorizedPointMobject extends Mobject {
|
|
100
|
-
artificialWidth = 0.01;
|
|
101
|
-
artificialHeight = 0.01;
|
|
102
|
-
get width() {
|
|
103
|
-
return this.artificialWidth;
|
|
104
|
-
}
|
|
105
|
-
set width(value) {
|
|
106
|
-
this.scaleToFitWidth(value);
|
|
107
|
-
}
|
|
108
|
-
get height() {
|
|
109
|
-
return this.artificialHeight;
|
|
110
|
-
}
|
|
111
|
-
set height(value) {
|
|
112
|
-
this.scaleToFitHeight(value);
|
|
113
|
-
}
|
|
114
|
-
/** The point (Manim `get_location`). */
|
|
115
|
-
getLocation() {
|
|
116
|
-
return this.getPoints()[0] ?? this.getCenter();
|
|
117
|
-
}
|
|
118
|
-
/** Moves the point to `newLoc` (Manim `set_location`). */
|
|
119
|
-
setLocation(newLoc) {
|
|
120
|
-
return this.setPoints([newLoc]);
|
|
121
|
-
}
|
|
122
|
-
copy() {
|
|
123
|
-
const c = new VectorizedPointMobject(raw(this).copy());
|
|
124
|
-
c.artificialWidth = this.artificialWidth;
|
|
125
|
-
c.artificialHeight = this.artificialHeight;
|
|
126
|
-
return c;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
/** An invisible point (Manim `VectorizedPoint`): black, no fill, no stroke. */
|
|
130
|
-
export function VectorizedPoint(location = ORIGIN, opts = {}) {
|
|
131
|
-
const { artificialWidth = 0.01, artificialHeight = 0.01, color = BLACK, fillOpacity = 0, strokeWidth = 0, ...style } = opts;
|
|
132
|
-
const p = new VectorizedPointMobject(raw(Polyline(location, location)));
|
|
133
|
-
p.artificialWidth = artificialWidth;
|
|
134
|
-
p.artificialHeight = artificialHeight;
|
|
135
|
-
p.setLocation(location).setColor(color);
|
|
136
|
-
p.setFill(style.fillColor ?? color, { opacity: fillOpacity });
|
|
137
|
-
p.setStroke(style.strokeColor ?? color, { width: strokeWidth, opacity: style.strokeOpacity ?? 1 });
|
|
138
|
-
return p;
|
|
139
|
-
}
|
|
140
|
-
/** Each curve of a path as a submobject: Manim's `CurvesAsSubmobjects`. */
|
|
141
|
-
export class CurvesAsSubmobjectsMobject extends Mobject {
|
|
142
|
-
/** The point at `alpha` of the total arc length of the curves (Manim `point_from_proportion`). */
|
|
143
|
-
pointFromProportion(alpha) {
|
|
144
|
-
if (alpha < 0 || alpha > 1)
|
|
145
|
-
wrong("pointFromProportion", `alpha ${String(alpha)} is not between 0 and 1`);
|
|
146
|
-
const parts = this.submobjects.filter((p) => p.hasPoints());
|
|
147
|
-
const last = parts.at(-1);
|
|
148
|
-
if (last === undefined)
|
|
149
|
-
return wrong("pointFromProportion", "the CurvesAsSubmobjects has no curves");
|
|
150
|
-
if (alpha === 1)
|
|
151
|
-
return last.getEnd();
|
|
152
|
-
const lengths = parts.map(arcLength);
|
|
153
|
-
const target = alpha * lengths.reduce((a, b) => a + b, 0);
|
|
154
|
-
let run = 0;
|
|
155
|
-
for (const [i, part] of parts.entries()) {
|
|
156
|
-
const l = lengths[i] ?? 0;
|
|
157
|
-
if (run + l >= target)
|
|
158
|
-
return part.pointFromProportion(l === 0 ? 0 : (target - run) / l);
|
|
159
|
-
run += l;
|
|
160
|
-
}
|
|
161
|
-
return last.getEnd();
|
|
162
|
-
}
|
|
163
|
-
copy() {
|
|
164
|
-
return new CurvesAsSubmobjectsMobject(raw(this).copy());
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
/** The arc length of the own path, from 32 samples of each curve. */
|
|
168
|
-
function arcLength(m) {
|
|
169
|
-
let total = 0;
|
|
170
|
-
for (const [a, h1, h2, b] of m.getCubicBezierTuples()) {
|
|
171
|
-
let prev = a;
|
|
172
|
-
for (let i = 1; i <= 32; i++) {
|
|
173
|
-
const t = i / 32;
|
|
174
|
-
const u = 1 - t;
|
|
175
|
-
const p = [
|
|
176
|
-
u * u * u * a[0] + 3 * u * u * t * h1[0] + 3 * u * t * t * h2[0] + t * t * t * b[0],
|
|
177
|
-
u * u * u * a[1] + 3 * u * u * t * h1[1] + 3 * u * t * t * h2[1] + t * t * t * b[1],
|
|
178
|
-
];
|
|
179
|
-
total += Math.hypot(p[0] - prev[0], p[1] - prev[1]);
|
|
180
|
-
prev = p;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return total;
|
|
184
|
-
}
|
|
185
|
-
/** One submobject for each cubic curve of `vmobject`, in its style (Manim `CurvesAsSubmobjects`). */
|
|
186
|
-
export function CurvesAsSubmobjects(vmobject) {
|
|
187
|
-
const group = new CurvesAsSubmobjectsMobject(engine.vGroup([]));
|
|
188
|
-
for (const tuple of vmobject.getCubicBezierTuples()) {
|
|
189
|
-
const part = Polyline(tuple[0], tuple[3]).setPoints(tuple);
|
|
190
|
-
part.setFill(vmobject.getFillColor(), { opacity: vmobject.getFillOpacity() });
|
|
191
|
-
part.setStroke(vmobject.getStrokeColor(), {
|
|
192
|
-
width: vmobject.getStrokeWidth(),
|
|
193
|
-
opacity: vmobject.getStrokeOpacity(),
|
|
194
|
-
});
|
|
195
|
-
group.add(part);
|
|
196
|
-
}
|
|
197
|
-
return group;
|
|
198
|
-
}
|
|
199
|
-
/** A rectangle with the shape of the screen: Manim's `ScreenRectangle`. */
|
|
200
|
-
export class ScreenRectangleMobject extends Mobject {
|
|
201
|
-
/** The width over the height; setting it stretches the width. */
|
|
202
|
-
get aspectRatio() {
|
|
203
|
-
return this.width / this.height;
|
|
204
|
-
}
|
|
205
|
-
set aspectRatio(value) {
|
|
206
|
-
this.stretchToFitWidth(value * this.height);
|
|
207
|
-
}
|
|
208
|
-
copy() {
|
|
209
|
-
return new ScreenRectangleMobject(raw(this).copy());
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
/** Manim's `ScreenRectangle({ aspectRatio, height })`: 16:9 and 4 high by default. */
|
|
213
|
-
export function ScreenRectangle(opts = {}) {
|
|
214
|
-
const { aspectRatio = 16 / 9, height = 4, ...style } = opts;
|
|
215
|
-
return new ScreenRectangleMobject(raw(Rectangle({ width: aspectRatio * height, height, ...style })));
|
|
216
|
-
}
|
|
217
|
-
/** A ScreenRectangle as high as the frame: Manim's `FullScreenRectangle`. */
|
|
218
|
-
export function FullScreenRectangle(opts = {}) {
|
|
219
|
-
const r = ScreenRectangle(opts);
|
|
220
|
-
r.height = FRAME_HEIGHT;
|
|
221
|
-
return r;
|
|
222
|
-
}
|
|
223
|
-
/** A complex number that animations change (Manim `ComplexValueTracker`), as `[re, im]`. */
|
|
224
|
-
export class ComplexValueTrackerObject {
|
|
225
|
-
#re;
|
|
226
|
-
#im;
|
|
227
|
-
constructor(value) {
|
|
228
|
-
const [re, im] = typeof value === "number" ? [value, 0] : value;
|
|
229
|
-
this.#re = ValueTracker(re);
|
|
230
|
-
this.#im = ValueTracker(im);
|
|
231
|
-
}
|
|
232
|
-
getValue() {
|
|
233
|
-
return [this.#re.getValue(), this.#im.getValue()];
|
|
234
|
-
}
|
|
235
|
-
setValue(value) {
|
|
236
|
-
const [re, im] = typeof value === "number" ? [value, 0] : value;
|
|
237
|
-
this.#re.setValue(re);
|
|
238
|
-
this.#im.setValue(im);
|
|
239
|
-
return this;
|
|
240
|
-
}
|
|
241
|
-
incrementValue(dValue) {
|
|
242
|
-
const [re, im] = typeof dValue === "number" ? [dValue, 0] : dValue;
|
|
243
|
-
const [a, b] = this.getValue();
|
|
244
|
-
return this.setValue([a + re, b + im]);
|
|
245
|
-
}
|
|
246
|
-
/** `tracker.animate.setValue(z)` and `incrementValue(dz)`, or with the animation options first. */
|
|
247
|
-
get animate() {
|
|
248
|
-
let options = {};
|
|
249
|
-
const both = (re, im) => AnimationGroup(re, im, options);
|
|
250
|
-
const methods = {
|
|
251
|
-
setValue: (value) => {
|
|
252
|
-
const [re, im] = typeof value === "number" ? [value, 0] : value;
|
|
253
|
-
return both(this.#re.animate.setValue(re), this.#im.animate.setValue(im));
|
|
254
|
-
},
|
|
255
|
-
incrementValue: (d) => {
|
|
256
|
-
const [re, im] = typeof d === "number" ? [d, 0] : d;
|
|
257
|
-
return both(this.#re.animate.incrementValue(re), this.#im.animate.incrementValue(im));
|
|
258
|
-
},
|
|
259
|
-
};
|
|
260
|
-
const call = (opts) => {
|
|
261
|
-
options = opts;
|
|
262
|
-
return methods;
|
|
263
|
-
};
|
|
264
|
-
return Object.assign(call, methods);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
/** Manim's `ComplexValueTracker(value)`, with `[re, im]` for a complex number. */
|
|
268
|
-
export function ComplexValueTracker(value = 0) {
|
|
269
|
-
return new ComplexValueTrackerObject(value);
|
|
270
|
-
}
|
|
271
|
-
/** A label, `=` and a number that follows a tracker: Manim's `Variable`. */
|
|
272
|
-
export class VariableMobject extends Mobject {
|
|
273
|
-
/** The label with its `=` (Manim `label`). */
|
|
274
|
-
label;
|
|
275
|
-
/** The number (Manim `value`). */
|
|
276
|
-
value;
|
|
277
|
-
/** The tracker that the number follows (Manim `tracker`). */
|
|
278
|
-
tracker;
|
|
279
|
-
constructor(handle, label, value, tracker) {
|
|
280
|
-
super(handle);
|
|
281
|
-
this.label = label;
|
|
282
|
-
this.value = value;
|
|
283
|
-
this.tracker = tracker;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
/** Manim's `Variable(var, label, { varType, numDecimalPlaces })`: a string label is a `MathTex`.
|
|
287
|
-
* Change `variable.tracker` to change the number. */
|
|
288
|
-
export function Variable(variable, label, opts = {}) {
|
|
289
|
-
const { varType = DecimalNumber, numDecimalPlaces = 2 } = opts;
|
|
290
|
-
const labelMob = typeof label === "string" ? MathTex(label) : label;
|
|
291
|
-
labelMob.add(MathTex("=").nextTo(labelMob, RIGHT));
|
|
292
|
-
const tracker = ValueTracker(variable);
|
|
293
|
-
const value = varType === Integer ? Integer(tracker.getValue()) : DecimalNumber(tracker.getValue(), { numDecimalPlaces });
|
|
294
|
-
value.addUpdater((v) => v.setValue(tracker.getValue())).nextTo(labelMob, RIGHT);
|
|
295
|
-
return new VariableMobject(engine.vGroup([raw(labelMob), raw(value)]), labelMob, value, tracker);
|
|
296
|
-
}
|
|
297
|
-
/** The tree of `mobject`, one line for each node, indented by depth (Manim `print_family`).
|
|
298
|
-
* aula returns the text: print it with `console.log`. */
|
|
299
|
-
export function printFamily(mobject, nTabs = 0) {
|
|
300
|
-
const name = mobject.constructor.name;
|
|
301
|
-
const own = `${"\t".repeat(nTabs)} ${name} ${String(mobject.id())}`;
|
|
302
|
-
return [own, ...mobject.submobjects.map((s) => printFamily(s, nTabs + 1))].join("\n");
|
|
303
|
-
}
|
|
304
|
-
/** An `Integer` on each submobject, with its index (Manim `index_labels`). Each label is a
|
|
305
|
-
* VGroup of an outline in `backgroundStrokeColor` and the Integer. */
|
|
306
|
-
export function indexLabels(mobject, opts = {}) {
|
|
307
|
-
const { labelHeight = 0.15, backgroundStrokeWidth = 5, backgroundStrokeColor = BLACK, ...rest } = opts;
|
|
308
|
-
const labels = mobject.submobjects.map((submob, n) => {
|
|
309
|
-
const label = Integer(n, rest);
|
|
310
|
-
label.height = labelHeight;
|
|
311
|
-
label.moveTo(submob);
|
|
312
|
-
const outline = label.copy().setStroke(backgroundStrokeColor, { width: backgroundStrokeWidth });
|
|
313
|
-
return VGroup(outline, label);
|
|
314
|
-
});
|
|
315
|
-
return VGroup(labels);
|
|
316
|
-
}
|
package/dist/picture.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { MediaSource } from "./media.ts";
|
|
2
|
-
/** The most bytes of one picture file. */
|
|
3
|
-
export declare const MAX_PICTURE_BYTES: number;
|
|
4
|
-
/** The most pixels of one picture (its width times its height). */
|
|
5
|
-
export declare const MAX_PICTURE_PIXELS = 36000000;
|
|
6
|
-
/** A picture that the engine draws: the media type, the size in pixels (for an SVG, the size
|
|
7
|
-
* that it says), and the bytes of the still. */
|
|
8
|
-
export interface Picture {
|
|
9
|
-
readonly mime: string;
|
|
10
|
-
readonly width: number;
|
|
11
|
-
readonly height: number;
|
|
12
|
-
readonly bytes: Uint8Array;
|
|
13
|
-
}
|
|
14
|
-
/** A picture of the project: its path from the project folder, as the engine tells two files
|
|
15
|
-
* apart, and the picture. */
|
|
16
|
-
export interface PlacedPicture extends Picture {
|
|
17
|
-
readonly path: string;
|
|
18
|
-
}
|
|
19
|
-
type Found = Picture | string;
|
|
20
|
-
/** The picture that the file `source` is, or why aula cannot draw it (a problem that fits after
|
|
21
|
-
* the path of the file). The size is checked before the read. */
|
|
22
|
-
export declare function probePicture(source: MediaSource): Found;
|
|
23
|
-
export {};
|
package/dist/picture.js
DELETED
|
@@ -1,291 +0,0 @@
|
|
|
1
|
-
// The pictures of a scene (`m.Image`): what a file is and how big, read from its header. Pure:
|
|
2
|
-
// the host gives the bytes (`Host.media`), the same for the CLI and the web app. A picture is
|
|
3
|
-
// one still, so the file that goes to the engine has what every viewer shows the same way: a GIF
|
|
4
|
-
// is cut after its first frame and a PNG loses its animation chunks; an animated WebP, a JPEG
|
|
5
|
-
// with an EXIF rotation, and an SVG whose picture depends on the viewer (scripts, animation,
|
|
6
|
-
// text, other files) are refused, with the fix.
|
|
7
|
-
/** The most bytes of one picture file. */
|
|
8
|
-
export const MAX_PICTURE_BYTES = 10 * 1024 * 1024;
|
|
9
|
-
/** The most pixels of one picture (its width times its height). */
|
|
10
|
-
export const MAX_PICTURE_PIXELS = 36_000_000;
|
|
11
|
-
/** The formats of a picture, for the messages. */
|
|
12
|
-
const PICTURE_FORMATS = "PNG, JPEG, WebP, GIF (its first frame) or SVG";
|
|
13
|
-
const megabytes = (bytes) => (bytes / (1024 * 1024)).toFixed(1);
|
|
14
|
-
const byteAt = (bytes, at) => bytes[at] ?? 0;
|
|
15
|
-
const ascii = (bytes, at, length) => String.fromCharCode(...bytes.subarray(at, at + length));
|
|
16
|
-
const be16 = (bytes, at) => (byteAt(bytes, at) << 8) | byteAt(bytes, at + 1);
|
|
17
|
-
const be32 = (bytes, at) => be16(bytes, at) * 65536 + be16(bytes, at + 2);
|
|
18
|
-
const le16 = (bytes, at) => byteAt(bytes, at) | (byteAt(bytes, at + 1) << 8);
|
|
19
|
-
const le24 = (bytes, at) => le16(bytes, at) | (byteAt(bytes, at + 2) << 16);
|
|
20
|
-
const startsWith = (bytes, head) => bytes.length >= head.length && head.every((b, i) => bytes[i] === b);
|
|
21
|
-
const join = (...parts) => {
|
|
22
|
-
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
23
|
-
let at = 0;
|
|
24
|
-
for (const part of parts) {
|
|
25
|
-
out.set(part, at);
|
|
26
|
-
at += part.length;
|
|
27
|
-
}
|
|
28
|
-
return out;
|
|
29
|
-
};
|
|
30
|
-
/** The picture that the file `source` is, or why aula cannot draw it (a problem that fits after
|
|
31
|
-
* the path of the file). The size is checked before the read. */
|
|
32
|
-
export function probePicture(source) {
|
|
33
|
-
if (source.size > MAX_PICTURE_BYTES) {
|
|
34
|
-
return `is ${megabytes(source.size)} MB, and the limit is ${megabytes(MAX_PICTURE_BYTES)} MB`;
|
|
35
|
-
}
|
|
36
|
-
const bytes = source.read();
|
|
37
|
-
const found = probeFormat(bytes);
|
|
38
|
-
if (typeof found === "string")
|
|
39
|
-
return found;
|
|
40
|
-
if (found.width < 1 || found.height < 1)
|
|
41
|
-
return "has no size";
|
|
42
|
-
if (found.width * found.height > MAX_PICTURE_PIXELS) {
|
|
43
|
-
return `is ${String(found.width)}×${String(found.height)} pixels, and the limit is ${String(MAX_PICTURE_PIXELS / 1_000_000)} million`;
|
|
44
|
-
}
|
|
45
|
-
return found;
|
|
46
|
-
}
|
|
47
|
-
function probeFormat(bytes) {
|
|
48
|
-
if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))
|
|
49
|
-
return probePng(bytes);
|
|
50
|
-
if (startsWith(bytes, [0xff, 0xd8, 0xff]))
|
|
51
|
-
return probeJpeg(bytes);
|
|
52
|
-
if (bytes.length >= 12 && ascii(bytes, 0, 4) === "RIFF" && ascii(bytes, 8, 4) === "WEBP")
|
|
53
|
-
return probeWebp(bytes);
|
|
54
|
-
if (bytes.length >= 10 && (ascii(bytes, 0, 6) === "GIF87a" || ascii(bytes, 0, 6) === "GIF89a")) {
|
|
55
|
-
return probeGif(bytes);
|
|
56
|
-
}
|
|
57
|
-
const svg = probeSvg(bytes);
|
|
58
|
-
if (svg !== undefined)
|
|
59
|
-
return svg;
|
|
60
|
-
return `has an unknown format: aula draws ${PICTURE_FORMATS}`;
|
|
61
|
-
}
|
|
62
|
-
// --- PNG ------------------------------------------------------------------------------------
|
|
63
|
-
const APNG_CHUNKS = new Set(["acTL", "fcTL", "fdAT"]);
|
|
64
|
-
function probePng(bytes) {
|
|
65
|
-
if (bytes.length < 33 || ascii(bytes, 12, 4) !== "IHDR")
|
|
66
|
-
return "is not a complete PNG file";
|
|
67
|
-
const kept = [];
|
|
68
|
-
let at = 8;
|
|
69
|
-
let ended = false;
|
|
70
|
-
let animated = false;
|
|
71
|
-
while (at + 12 <= bytes.length) {
|
|
72
|
-
const end = at + 12 + be32(bytes, at);
|
|
73
|
-
if (end > bytes.length)
|
|
74
|
-
break;
|
|
75
|
-
const type = ascii(bytes, at + 4, 4);
|
|
76
|
-
if (APNG_CHUNKS.has(type)) {
|
|
77
|
-
animated = true;
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
kept.push(bytes.subarray(at, end));
|
|
81
|
-
}
|
|
82
|
-
at = end;
|
|
83
|
-
if (type === "IEND") {
|
|
84
|
-
ended = true;
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
if (!ended)
|
|
89
|
-
return "is cut short: it is not a complete PNG file";
|
|
90
|
-
return {
|
|
91
|
-
mime: "image/png",
|
|
92
|
-
width: be32(bytes, 16),
|
|
93
|
-
height: be32(bytes, 20),
|
|
94
|
-
bytes: animated ? join(bytes.subarray(0, 8), ...kept) : bytes,
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
// --- JPEG -----------------------------------------------------------------------------------
|
|
98
|
-
const JPEG_SOF = new Set([0xc0, 0xc1, 0xc2]);
|
|
99
|
-
const JPEG_OTHER_SOF = new Set([0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf]);
|
|
100
|
-
/** The EXIF orientation of the APP1 segment at `data` (1 when it says none). */
|
|
101
|
-
function exifOrientation(bytes, data, end) {
|
|
102
|
-
if (ascii(bytes, data, 6) !== "Exif\0\0")
|
|
103
|
-
return 1;
|
|
104
|
-
const tiff = data + 6;
|
|
105
|
-
const little = ascii(bytes, tiff, 2) === "II";
|
|
106
|
-
const u16 = (at) => (little ? le16(bytes, at) : be16(bytes, at));
|
|
107
|
-
const u32 = (at) => (little ? u16(at) + u16(at + 2) * 65536 : u16(at) * 65536 + u16(at + 2));
|
|
108
|
-
const ifd = tiff + u32(tiff + 4);
|
|
109
|
-
if (ifd + 2 > end)
|
|
110
|
-
return 1;
|
|
111
|
-
const count = u16(ifd);
|
|
112
|
-
for (let i = 0; i < count; i += 1) {
|
|
113
|
-
const entry = ifd + 2 + i * 12;
|
|
114
|
-
if (entry + 12 > end)
|
|
115
|
-
return 1;
|
|
116
|
-
if (u16(entry) === 0x0112)
|
|
117
|
-
return u16(entry + 8);
|
|
118
|
-
}
|
|
119
|
-
return 1;
|
|
120
|
-
}
|
|
121
|
-
function probeJpeg(bytes) {
|
|
122
|
-
let at = 2;
|
|
123
|
-
let orientation = 1;
|
|
124
|
-
while (at + 4 <= bytes.length) {
|
|
125
|
-
if (byteAt(bytes, at) !== 0xff)
|
|
126
|
-
return "is not a complete JPEG file";
|
|
127
|
-
const marker = byteAt(bytes, at + 1);
|
|
128
|
-
if (marker === 0xff) {
|
|
129
|
-
at += 1;
|
|
130
|
-
continue;
|
|
131
|
-
}
|
|
132
|
-
if (marker === 0xd8 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {
|
|
133
|
-
at += 2;
|
|
134
|
-
continue;
|
|
135
|
-
}
|
|
136
|
-
const length = be16(bytes, at + 2);
|
|
137
|
-
const end = at + 2 + length;
|
|
138
|
-
if (end > bytes.length)
|
|
139
|
-
return "is cut short: it is not a complete JPEG file";
|
|
140
|
-
if (marker === 0xe1 && orientation === 1)
|
|
141
|
-
orientation = exifOrientation(bytes, at + 4, end);
|
|
142
|
-
if (JPEG_OTHER_SOF.has(marker))
|
|
143
|
-
return "is a kind of JPEG that aula does not draw (lossless or arithmetic): save it as a normal JPEG";
|
|
144
|
-
if (JPEG_SOF.has(marker)) {
|
|
145
|
-
if (byteAt(bytes, at + 4) !== 8)
|
|
146
|
-
return "has more than 8 bits for each color: save it as a normal JPEG";
|
|
147
|
-
const components = byteAt(bytes, at + 9);
|
|
148
|
-
if (components !== 1 && components !== 3) {
|
|
149
|
-
return "is not RGB or gray (a CMYK file): save it as RGB, so every viewer shows the same colors";
|
|
150
|
-
}
|
|
151
|
-
if (orientation !== 1) {
|
|
152
|
-
return `has an EXIF orientation (${String(orientation)}): turn the picture upright and save it again, so every viewer shows it the same way`;
|
|
153
|
-
}
|
|
154
|
-
return { mime: "image/jpeg", width: be16(bytes, at + 7), height: be16(bytes, at + 5), bytes };
|
|
155
|
-
}
|
|
156
|
-
if (marker === 0xda)
|
|
157
|
-
break;
|
|
158
|
-
at = end;
|
|
159
|
-
}
|
|
160
|
-
return "is cut short: it is not a complete JPEG file";
|
|
161
|
-
}
|
|
162
|
-
// --- WebP -----------------------------------------------------------------------------------
|
|
163
|
-
function probeWebp(bytes) {
|
|
164
|
-
const kind = ascii(bytes, 12, 4);
|
|
165
|
-
const mime = "image/webp";
|
|
166
|
-
if (kind === "VP8X" && bytes.length >= 30) {
|
|
167
|
-
if ((byteAt(bytes, 20) & 0x02) !== 0) {
|
|
168
|
-
return "is an animated WebP: aula draws one still, so save a still WebP (or a PNG)";
|
|
169
|
-
}
|
|
170
|
-
return { mime, width: le24(bytes, 24) + 1, height: le24(bytes, 27) + 1, bytes };
|
|
171
|
-
}
|
|
172
|
-
if (kind === "VP8L" && bytes.length >= 25 && byteAt(bytes, 20) === 0x2f) {
|
|
173
|
-
const bits = le16(bytes, 21) | (le16(bytes, 23) << 16);
|
|
174
|
-
return { mime, width: (bits & 0x3fff) + 1, height: ((bits >>> 14) & 0x3fff) + 1, bytes };
|
|
175
|
-
}
|
|
176
|
-
if (kind === "VP8 " && bytes.length >= 30 && byteAt(bytes, 23) === 0x9d && byteAt(bytes, 24) === 0x01) {
|
|
177
|
-
return { mime, width: le16(bytes, 26) & 0x3fff, height: le16(bytes, 28) & 0x3fff, bytes };
|
|
178
|
-
}
|
|
179
|
-
return "is not a complete WebP file";
|
|
180
|
-
}
|
|
181
|
-
// --- GIF ------------------------------------------------------------------------------------
|
|
182
|
-
/** The offset after the sub-blocks that start at `at`. */
|
|
183
|
-
function afterBlocks(bytes, start) {
|
|
184
|
-
let at = start;
|
|
185
|
-
while (at < bytes.length) {
|
|
186
|
-
const size = byteAt(bytes, at);
|
|
187
|
-
at += 1 + size;
|
|
188
|
-
if (size === 0)
|
|
189
|
-
return at;
|
|
190
|
-
}
|
|
191
|
-
return bytes.length + 1;
|
|
192
|
-
}
|
|
193
|
-
function probeGif(bytes) {
|
|
194
|
-
const flags = byteAt(bytes, 10);
|
|
195
|
-
let at = 13 + (flags & 0x80 ? 3 * 2 ** ((flags & 7) + 1) : 0);
|
|
196
|
-
while (at < bytes.length) {
|
|
197
|
-
const block = byteAt(bytes, at);
|
|
198
|
-
if (block === 0x21) {
|
|
199
|
-
at = afterBlocks(bytes, at + 2);
|
|
200
|
-
}
|
|
201
|
-
else if (block === 0x2c) {
|
|
202
|
-
const local = byteAt(bytes, at + 9);
|
|
203
|
-
at += 10 + (local & 0x80 ? 3 * 2 ** ((local & 7) + 1) : 0) + 1;
|
|
204
|
-
at = afterBlocks(bytes, at);
|
|
205
|
-
if (at > bytes.length)
|
|
206
|
-
break;
|
|
207
|
-
return {
|
|
208
|
-
mime: "image/gif",
|
|
209
|
-
width: le16(bytes, 6),
|
|
210
|
-
height: le16(bytes, 8),
|
|
211
|
-
bytes: join(bytes.subarray(0, at), Uint8Array.of(0x3b)),
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
else {
|
|
215
|
-
break;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
return "is cut short: it has no complete first frame";
|
|
219
|
-
}
|
|
220
|
-
// --- SVG ------------------------------------------------------------------------------------
|
|
221
|
-
/** What makes the picture of an SVG file depend on the viewer: a pattern, and the fix. */
|
|
222
|
-
const SVG_REFUSED = [
|
|
223
|
-
[/<script\b/i, "has a <script>: aula draws a picture, not a program"],
|
|
224
|
-
[/<foreignObject\b/i, "has a <foreignObject>: its content is not drawn the same way everywhere"],
|
|
225
|
-
[/<(?:animate\w*|set)\b/i, "has an animation (<animate>, <set>): aula draws one still"],
|
|
226
|
-
[/@keyframes\b/i, "has a CSS animation (@keyframes): aula draws one still"],
|
|
227
|
-
[/<image\b/i, "has an <image>: put the picture in the scene as its own m.Image"],
|
|
228
|
-
[
|
|
229
|
-
/<(?:text|tspan|textPath)\b/i,
|
|
230
|
-
"has text: turn the text into paths in the editor, so every viewer draws the same letters",
|
|
231
|
-
],
|
|
232
|
-
[
|
|
233
|
-
/(?:xlink:)?href\s*=\s*["'](?!#)/i,
|
|
234
|
-
"refers to another file or URL (href): the SVG file must hold all of its picture",
|
|
235
|
-
],
|
|
236
|
-
[/@import\b|url\(\s*(?!["']?#)/i, "refers to another file or URL: the SVG file must hold all of its picture"],
|
|
237
|
-
];
|
|
238
|
-
const LENGTH = /^\s*([0-9]*\.?[0-9]+(?:e[+-]?[0-9]+)?)\s*(?:px)?\s*$/i;
|
|
239
|
-
/** The value of the attribute `name` of the tag `tag`, if it has it. */
|
|
240
|
-
function attribute(tag, name) {
|
|
241
|
-
return new RegExp(`(?:^|\\s)${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)')`)
|
|
242
|
-
.exec(tag)
|
|
243
|
-
?.slice(1)
|
|
244
|
-
.find((v) => v !== undefined);
|
|
245
|
-
}
|
|
246
|
-
function svgSize(root) {
|
|
247
|
-
const box = attribute(root, "viewBox");
|
|
248
|
-
const given = [attribute(root, "width"), attribute(root, "height")];
|
|
249
|
-
if (given[0] === undefined && given[1] === undefined) {
|
|
250
|
-
const numbers = (box ?? "")
|
|
251
|
-
.trim()
|
|
252
|
-
.split(/[\s,]+/)
|
|
253
|
-
.map(Number);
|
|
254
|
-
const [, , width, height] = numbers;
|
|
255
|
-
if (numbers.length !== 4 || !(width !== undefined && width > 0) || !(height !== undefined && height > 0)) {
|
|
256
|
-
return "has no size: give the <svg> a width and a height, or a viewBox";
|
|
257
|
-
}
|
|
258
|
-
return { width, height };
|
|
259
|
-
}
|
|
260
|
-
const [width, height] = given.map((value) => {
|
|
261
|
-
const found = value === undefined ? null : LENGTH.exec(value);
|
|
262
|
-
return found?.[1] === undefined ? Number.NaN : Number(found[1]);
|
|
263
|
-
});
|
|
264
|
-
if (width === undefined || height === undefined || !(width > 0) || !(height > 0)) {
|
|
265
|
-
return 'has a width and a height that are not both a number of pixels (50% and 2cm change with the viewer): write width="200" height="100"';
|
|
266
|
-
}
|
|
267
|
-
return { width, height };
|
|
268
|
-
}
|
|
269
|
-
/** The picture of an SVG file, or undefined when the bytes are not one. */
|
|
270
|
-
function probeSvg(bytes) {
|
|
271
|
-
const text = new TextDecoder().decode(bytes);
|
|
272
|
-
if (text.includes("\uFFFD"))
|
|
273
|
-
return undefined;
|
|
274
|
-
const plain = text.replace(/^\uFEFF/, "").replace(/<!--[\s\S]*?-->/g, "");
|
|
275
|
-
const root = /<svg(\s[^>]*)?>/i.exec(plain);
|
|
276
|
-
if (root === null)
|
|
277
|
-
return undefined;
|
|
278
|
-
for (const [pattern, problem] of SVG_REFUSED) {
|
|
279
|
-
if (pattern.test(plain))
|
|
280
|
-
return problem;
|
|
281
|
-
}
|
|
282
|
-
const size = svgSize(root[1] ?? "");
|
|
283
|
-
if (typeof size === "string")
|
|
284
|
-
return size;
|
|
285
|
-
return {
|
|
286
|
-
mime: "image/svg+xml",
|
|
287
|
-
width: Math.max(1, Math.round(size.width)),
|
|
288
|
-
height: Math.max(1, Math.round(size.height)),
|
|
289
|
-
bytes,
|
|
290
|
-
};
|
|
291
|
-
}
|