@open-artifact/motion-stage 0.0.0-stage → 0.12.0-rc.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +71 -4
- package/src/build.d.ts +18 -0
- package/src/build.js +73 -0
- package/src/host.d.ts +37 -0
- package/src/host.js +76 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +6 -0
- package/src/layers.d.ts +30 -0
- package/src/layers.js +56 -0
- package/src/offline/audio.d.ts +19 -0
- package/src/offline/audio.js +20 -0
- package/src/offline/data.d.ts +26 -0
- package/src/offline/data.js +71 -0
- package/src/offline/main.d.ts +9 -0
- package/src/offline/main.js +52 -0
- package/src/offline/player.d.ts +23 -0
- package/src/offline/player.js +155 -0
- package/src/protocol/guards.d.ts +6 -0
- package/src/protocol/guards.js +208 -0
- package/src/protocol/layer-guards.d.ts +7 -0
- package/src/protocol/layer-guards.js +97 -0
- package/src/protocol/layers.d.ts +123 -0
- package/src/protocol/layers.js +1 -0
- package/src/protocol/types.d.ts +261 -0
- package/src/protocol/types.js +7 -0
- package/src/protocol.d.ts +2 -0
- package/src/protocol.js +2 -0
- package/src/raster.d.ts +4 -0
- package/src/raster.js +2 -0
- package/src/runtime/assets.d.ts +19 -0
- package/src/runtime/assets.js +63 -0
- package/src/runtime/captions.d.ts +26 -0
- package/src/runtime/captions.js +103 -0
- package/src/runtime/console.d.ts +13 -0
- package/src/runtime/console.js +99 -0
- package/src/runtime/cut.d.ts +7 -0
- package/src/runtime/cut.js +63 -0
- package/src/runtime/determinism.d.ts +12 -0
- package/src/runtime/determinism.js +47 -0
- package/src/runtime/film.d.ts +40 -0
- package/src/runtime/film.js +29 -0
- package/src/runtime/focus.d.ts +8 -0
- package/src/runtime/focus.js +71 -0
- package/src/runtime/fonts.d.ts +22 -0
- package/src/runtime/fonts.js +109 -0
- package/src/runtime/frame.d.ts +9 -0
- package/src/runtime/frame.js +123 -0
- package/src/runtime/kit/camera.d.ts +40 -0
- package/src/runtime/kit/camera.js +56 -0
- package/src/runtime/kit/chip.d.ts +25 -0
- package/src/runtime/kit/chip.js +75 -0
- package/src/runtime/kit/cursor.d.ts +46 -0
- package/src/runtime/kit/cursor.js +108 -0
- package/src/runtime/kit/dom.d.ts +14 -0
- package/src/runtime/kit/dom.js +46 -0
- package/src/runtime/kit/geometry.d.ts +22 -0
- package/src/runtime/kit/geometry.js +46 -0
- package/src/runtime/kit/index.d.ts +95 -0
- package/src/runtime/kit/index.js +149 -0
- package/src/runtime/kit/mark-strokes.d.ts +65 -0
- package/src/runtime/kit/mark-strokes.js +226 -0
- package/src/runtime/kit/marks.d.ts +21 -0
- package/src/runtime/kit/marks.js +165 -0
- package/src/runtime/kit/math.d.ts +23 -0
- package/src/runtime/kit/math.js +35 -0
- package/src/runtime/kit/paper.d.ts +7 -0
- package/src/runtime/kit/paper.js +27 -0
- package/src/runtime/kit/random.d.ts +8 -0
- package/src/runtime/kit/random.js +32 -0
- package/src/runtime/kit/text-fx.d.ts +18 -0
- package/src/runtime/kit/text-fx.js +55 -0
- package/src/runtime/kit/typing.d.ts +21 -0
- package/src/runtime/kit/typing.js +42 -0
- package/src/runtime/layers/describe.d.ts +41 -0
- package/src/runtime/layers/describe.js +327 -0
- package/src/runtime/layers/item.d.ts +121 -0
- package/src/runtime/layers/item.js +149 -0
- package/src/runtime/layers/parts.d.ts +50 -0
- package/src/runtime/layers/parts.js +155 -0
- package/src/runtime/layers/rows.d.ts +4 -0
- package/src/runtime/layers/rows.js +233 -0
- package/src/runtime/layers/scan.d.ts +24 -0
- package/src/runtime/layers/scan.js +202 -0
- package/src/runtime/layers/walk.d.ts +16 -0
- package/src/runtime/layers/walk.js +333 -0
- package/src/runtime/load.d.ts +10 -0
- package/src/runtime/load.js +228 -0
- package/src/runtime/main.d.ts +1 -0
- package/src/runtime/main.js +69 -0
- package/src/runtime/modules.d.ts +23 -0
- package/src/runtime/modules.js +53 -0
- package/src/runtime/port.d.ts +8 -0
- package/src/runtime/port.js +46 -0
- package/src/runtime/questions.d.ts +24 -0
- package/src/runtime/questions.js +162 -0
- package/src/runtime/recut.d.ts +35 -0
- package/src/runtime/recut.js +158 -0
- package/src/runtime/render.d.ts +80 -0
- package/src/runtime/render.js +342 -0
- package/src/runtime/retouch/ask.d.ts +17 -0
- package/src/runtime/retouch/ask.js +156 -0
- package/src/runtime/retouch/entry.d.ts +77 -0
- package/src/runtime/retouch/entry.js +1 -0
- package/src/runtime/retouch/judge.d.ts +25 -0
- package/src/runtime/retouch/judge.js +209 -0
- package/src/runtime/retouch/look.d.ts +39 -0
- package/src/runtime/retouch/look.js +135 -0
- package/src/runtime/retouch/parts.d.ts +22 -0
- package/src/runtime/retouch/parts.js +166 -0
- package/src/runtime/retouch/retouch.d.ts +37 -0
- package/src/runtime/retouch/retouch.js +238 -0
- package/src/runtime/retouch/seat.d.ts +40 -0
- package/src/runtime/retouch/seat.js +228 -0
- package/src/runtime/retouch/state.d.ts +37 -0
- package/src/runtime/retouch/state.js +153 -0
- package/src/runtime/retouch/type-style.d.ts +19 -0
- package/src/runtime/retouch/type-style.js +111 -0
- package/src/runtime/scan.d.ts +2 -0
- package/src/runtime/scan.js +49 -0
- package/src/runtime/scenes/card.d.ts +12 -0
- package/src/runtime/scenes/card.js +56 -0
- package/src/runtime/scenes/install.d.ts +47 -0
- package/src/runtime/scenes/install.js +387 -0
- package/src/runtime/scenes/read.d.ts +15 -0
- package/src/runtime/scenes/read.js +57 -0
- package/src/runtime/scenes/scoped.d.ts +13 -0
- package/src/runtime/scenes/scoped.js +40 -0
- package/src/runtime/score.d.ts +26 -0
- package/src/runtime/score.js +221 -0
- package/src/runtime/sketch/pen.d.ts +3 -0
- package/src/runtime/sketch/pen.js +371 -0
- package/src/runtime/sketch/strokes.d.ts +41 -0
- package/src/runtime/sketch/strokes.js +187 -0
- package/src/runtime/snapshot/draw.d.ts +14 -0
- package/src/runtime/snapshot/draw.js +125 -0
- package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
- package/src/runtime/snapshot/hold-first-draw.js +128 -0
- package/src/runtime/snapshot/plate.d.ts +20 -0
- package/src/runtime/snapshot/plate.js +79 -0
- package/src/runtime/snapshot/serialize.d.ts +49 -0
- package/src/runtime/snapshot/serialize.js +271 -0
- package/src/runtime/snapshot/styles.d.ts +8 -0
- package/src/runtime/snapshot/styles.js +109 -0
- package/src/runtime/snapshot/xml.d.ts +6 -0
- package/src/runtime/snapshot/xml.js +110 -0
- package/src/runtime/stage-size.d.ts +7 -0
- package/src/runtime/stage-size.js +3 -0
- package/src/runtime/stage.d.ts +27 -0
- package/src/runtime/stage.js +154 -0
- package/src/runtime/stretch.d.ts +13 -0
- package/src/runtime/stretch.js +16 -0
- package/src/runtime/texts.d.ts +24 -0
- package/src/runtime/texts.js +210 -0
- package/src/runtime/video-sync.d.ts +26 -0
- package/src/runtime/video-sync.js +126 -0
- package/src/stills.d.ts +41 -0
- package/src/stills.js +253 -0
- package/src/tools/film.d.ts +13 -0
- package/src/tools/film.js +116 -0
- package/src/tools/main.d.ts +1 -0
- package/src/tools/main.js +216 -0
- package/src/tools/score.d.ts +4 -0
- package/src/tools/score.js +29 -0
- package/src/tools/types.d.ts +69 -0
- package/src/tools/types.js +2 -0
- package/src/tools.d.ts +5 -0
- package/src/tools.js +3 -0
- package/src/virtual.d.ts +11 -0
- package/src/vite.d.ts +8 -0
- package/src/vite.js +43 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { dropSlightDisplacement, sayWholeStyle, sayWholeStyles } from "./styles.js";
|
|
2
|
+
import { xmlOf } from "./xml.js";
|
|
3
|
+
/** A stand-in with no picture: an <img> without a source draws with a broken-image border. */
|
|
4
|
+
export const CLEAR_PIXEL = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNgYGBgAAAABQABeqhXUAAAAABJRU5ErkJggg==';
|
|
5
|
+
const BIG_PICTURE_CHARS = 65536;
|
|
6
|
+
/** WebKit can paint a picture inside an SVG image blank unless it is told to decode before painting. */
|
|
7
|
+
export function decodeFirst(root) {
|
|
8
|
+
for (const image of root.querySelectorAll('img'))
|
|
9
|
+
image.setAttribute('decoding', 'sync');
|
|
10
|
+
}
|
|
11
|
+
function copyAt(source, w, h, scale) {
|
|
12
|
+
const canvas = document.createElement('canvas');
|
|
13
|
+
canvas.width = Math.max(1, Math.round(w * scale));
|
|
14
|
+
canvas.height = Math.max(1, Math.round(h * scale));
|
|
15
|
+
canvas.getContext('2d')?.drawImage(source, 0, 0, canvas.width, canvas.height);
|
|
16
|
+
return canvas;
|
|
17
|
+
}
|
|
18
|
+
/** What a person did to a layer by hand goes by marks on its element: the picture standing in for it takes them over. */
|
|
19
|
+
function keepMarks(from, to) {
|
|
20
|
+
for (const name of ['data-film-over', 'data-film-laid']) {
|
|
21
|
+
const value = from.getAttribute(name);
|
|
22
|
+
if (value !== null)
|
|
23
|
+
to.setAttribute(name, value);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The node as XHTML text for an SVG foreignObject. Canvas, clip and big picture pixels are spliced into the text
|
|
28
|
+
* after serializing: an <img src> set in this long-lived document would start a load it keeps.
|
|
29
|
+
*/
|
|
30
|
+
export function serializeForRaster(node, options) {
|
|
31
|
+
const clone = node.cloneNode(true);
|
|
32
|
+
if (!(clone instanceof HTMLElement))
|
|
33
|
+
return '';
|
|
34
|
+
sayWholeStyles(node, clone);
|
|
35
|
+
if (options.smoothAt > 0)
|
|
36
|
+
dropSlightDisplacement(clone, options.smoothAt);
|
|
37
|
+
const urls = [];
|
|
38
|
+
const mark = 'data-raster-canvas-' + Math.random().toString(36).slice(2);
|
|
39
|
+
const standIn = (image, url) => {
|
|
40
|
+
image.setAttribute(mark, String(urls.length));
|
|
41
|
+
urls.push(url);
|
|
42
|
+
};
|
|
43
|
+
if (options.mediaScale < 1)
|
|
44
|
+
shrinkPictures(node, clone, options.mediaScale, standIn);
|
|
45
|
+
// Read #fx afresh while a clip is on the stage: in Chromium a clip copied just after 'seeked' can still show the frame before.
|
|
46
|
+
const fx = options.fx && node.querySelector('video') ? null : options.fx;
|
|
47
|
+
replaceCanvases(node, clone, options.mediaScale, fx, standIn);
|
|
48
|
+
replaceVideos(node, clone, options, standIn);
|
|
49
|
+
keepInputs(node, clone);
|
|
50
|
+
decodeFirst(clone);
|
|
51
|
+
if (clone.id === 'stage') {
|
|
52
|
+
clone.style.transform = 'none';
|
|
53
|
+
clone.style.left = '0';
|
|
54
|
+
clone.style.top = '0';
|
|
55
|
+
}
|
|
56
|
+
const wrap = document.createElement('div');
|
|
57
|
+
wrap.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
|
|
58
|
+
wrap.style.cssText = `width:${options.width}px;height:${options.height}px;overflow:hidden;position:relative;margin:0;padding:0`;
|
|
59
|
+
wrap.appendChild(clone);
|
|
60
|
+
let out = xmlOf(wrap);
|
|
61
|
+
urls.forEach((url, k) => {
|
|
62
|
+
out = out.replace(' ' + mark + '="' + k + '"', ' src="' + (/^data:image\//.test(url) ? url : CLEAR_PIXEL) + '"');
|
|
63
|
+
});
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
function shrinkPictures(node, clone, scale, standIn) {
|
|
67
|
+
const fonts = clone.querySelector('#film-fonts');
|
|
68
|
+
if (fonts)
|
|
69
|
+
fonts.textContent = '';
|
|
70
|
+
const sources = node.querySelectorAll('img');
|
|
71
|
+
const copies = clone.querySelectorAll('img');
|
|
72
|
+
for (let i = 0; i < copies.length && i < sources.length; i++) {
|
|
73
|
+
const source = sources[i];
|
|
74
|
+
const copy = copies[i];
|
|
75
|
+
if (!source || !copy)
|
|
76
|
+
continue;
|
|
77
|
+
const from = source.currentSrc || source.src;
|
|
78
|
+
if (!source.complete || !source.naturalWidth || from.length < BIG_PICTURE_CHARS)
|
|
79
|
+
continue;
|
|
80
|
+
const box = getComputedStyle(source);
|
|
81
|
+
// Left whole: an SVG, whose natural size may be a default, and the two fits that draw a picture at its natural size.
|
|
82
|
+
if (/^data:image\/svg/i.test(from) || box.objectFit === 'none' || box.objectFit === 'scale-down')
|
|
83
|
+
continue;
|
|
84
|
+
let url;
|
|
85
|
+
try {
|
|
86
|
+
url = copyAt(source, source.naturalWidth, source.naturalHeight, scale).toDataURL('image/png');
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
copy.style.setProperty('width', box.width, 'important');
|
|
92
|
+
copy.style.setProperty('height', box.height, 'important');
|
|
93
|
+
copy.removeAttribute('src');
|
|
94
|
+
copy.removeAttribute('srcset');
|
|
95
|
+
standIn(copy, url);
|
|
96
|
+
// The writes above re-serialize the copy's style, which can empty more values.
|
|
97
|
+
sayWholeStyle(source, copy);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// Told by identity, not by place among the canvases: a scene may have taken #fx into its box.
|
|
101
|
+
function replaceCanvases(node, clone, scale, fx, standIn) {
|
|
102
|
+
const sources = node.querySelectorAll('canvas');
|
|
103
|
+
const copies = clone.querySelectorAll('canvas');
|
|
104
|
+
for (let i = 0; i < copies.length && i < sources.length; i++) {
|
|
105
|
+
const source = sources[i];
|
|
106
|
+
const copy = copies[i];
|
|
107
|
+
if (!source || !copy)
|
|
108
|
+
continue;
|
|
109
|
+
const image = document.createElement('img');
|
|
110
|
+
let url = '';
|
|
111
|
+
try {
|
|
112
|
+
url = scale < 1 ? copyAt(source, source.width, source.height, scale).toDataURL('image/png') : fx && fx.canvas === source ? fx.png() : source.toDataURL('image/png');
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
image.alt = '';
|
|
116
|
+
}
|
|
117
|
+
standIn(image, url);
|
|
118
|
+
image.setAttribute('style', copy.getAttribute('style') ?? '');
|
|
119
|
+
image.className = copy.className;
|
|
120
|
+
image.width = source.width;
|
|
121
|
+
image.height = source.height;
|
|
122
|
+
keepMarks(copy, image);
|
|
123
|
+
if (!copy.style.width)
|
|
124
|
+
image.style.width = (source.clientWidth || source.width) + 'px';
|
|
125
|
+
if (!copy.style.height)
|
|
126
|
+
image.style.height = (source.clientHeight || source.height) + 'px';
|
|
127
|
+
sayWholeStyle(source, image);
|
|
128
|
+
copy.parentNode?.replaceChild(image, copy);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// A clip draws as its current frame; the clone's own load is cancelled.
|
|
132
|
+
function replaceVideos(node, clone, options, standIn) {
|
|
133
|
+
const sources = node.querySelectorAll('video');
|
|
134
|
+
const copies = clone.querySelectorAll('video');
|
|
135
|
+
for (let i = 0; i < copies.length && i < sources.length; i++) {
|
|
136
|
+
const source = sources[i];
|
|
137
|
+
const copy = copies[i];
|
|
138
|
+
if (!source || !copy)
|
|
139
|
+
continue;
|
|
140
|
+
const image = document.createElement('img');
|
|
141
|
+
const shown = getComputedStyle(source);
|
|
142
|
+
let url = '';
|
|
143
|
+
copy.removeAttribute('src');
|
|
144
|
+
copy.textContent = '';
|
|
145
|
+
try {
|
|
146
|
+
copy.load();
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
// Nothing to cancel.
|
|
150
|
+
}
|
|
151
|
+
if (source.readyState >= 2 && source.videoWidth > 0) {
|
|
152
|
+
try {
|
|
153
|
+
// WebP keeps alpha at a fraction of PNG's bytes: the raster document keeps every data: URL until recycled.
|
|
154
|
+
url = copyAt(source, source.videoWidth, source.videoHeight, Math.min(1, options.mediaScale)).toDataURL('image/webp', 0.92);
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
options.warn(source, 'taint', 'cannot be exported (' + (error instanceof Error ? error.name : String(error)) + '); it shows empty in frames');
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else if ((source.currentSrc || source.src) && !source.error)
|
|
161
|
+
options.warn(source, 'empty', 'had no decoded frame when a frame was exported; it shows empty there');
|
|
162
|
+
if (!url)
|
|
163
|
+
image.alt = '';
|
|
164
|
+
standIn(image, url);
|
|
165
|
+
image.setAttribute('style', copy.getAttribute('style') ?? '');
|
|
166
|
+
image.className = copy.className;
|
|
167
|
+
if (copy.id)
|
|
168
|
+
image.id = copy.id;
|
|
169
|
+
keepMarks(copy, image);
|
|
170
|
+
image.style.objectFit = shown.objectFit;
|
|
171
|
+
image.style.objectPosition = shown.objectPosition;
|
|
172
|
+
if (!copy.style.width)
|
|
173
|
+
image.style.width = shown.width;
|
|
174
|
+
if (!copy.style.height)
|
|
175
|
+
image.style.height = shown.height;
|
|
176
|
+
sayWholeStyle(source, image);
|
|
177
|
+
copy.parentNode?.replaceChild(image, copy);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function keepInputs(node, clone) {
|
|
181
|
+
const query = 'input, textarea, select';
|
|
182
|
+
const sources = node.querySelectorAll(query);
|
|
183
|
+
const copies = clone.querySelectorAll(query);
|
|
184
|
+
for (let i = 0; i < copies.length && i < sources.length; i++) {
|
|
185
|
+
const source = sources[i];
|
|
186
|
+
const copy = copies[i];
|
|
187
|
+
if (!source || !copy)
|
|
188
|
+
continue;
|
|
189
|
+
if (source instanceof HTMLInputElement && (source.type === 'checkbox' || source.type === 'radio')) {
|
|
190
|
+
if (source.checked)
|
|
191
|
+
copy.setAttribute('checked', '');
|
|
192
|
+
else
|
|
193
|
+
copy.removeAttribute('checked');
|
|
194
|
+
}
|
|
195
|
+
else if (source instanceof HTMLTextAreaElement)
|
|
196
|
+
copy.textContent = source.value;
|
|
197
|
+
else if (source instanceof HTMLSelectElement) {
|
|
198
|
+
copy.querySelectorAll('option').forEach((option, k) => {
|
|
199
|
+
if (k === source.selectedIndex)
|
|
200
|
+
option.setAttribute('selected', '');
|
|
201
|
+
else
|
|
202
|
+
option.removeAttribute('selected');
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
else if (source instanceof HTMLInputElement)
|
|
206
|
+
copy.setAttribute('value', source.value);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/** A snapshot's text color under forced colors when nothing gives it one: a page's own, with no theme. */
|
|
210
|
+
export const PLAIN_INK = '#000';
|
|
211
|
+
/**
|
|
212
|
+
* The SVG keeps its content's own size with no viewBox, and the draw scales it to pw × ph: WebKit ignores an
|
|
213
|
+
* SVG-level scale for positioned HTML in a foreignObject. data-raster-size draws nothing: it makes the text, and so
|
|
214
|
+
* the URL the browser caches by, differ per output size.
|
|
215
|
+
*
|
|
216
|
+
* A snapshot is a document of its own, which the stage's opt-out of forced colors does not reach: the foreignObject
|
|
217
|
+
* opts out, frame and plate alike, or Chromium repaints the picture in the theme's colors. Its default text color is
|
|
218
|
+
* the theme's too, so under forced colors the caller gives `ink` for the root, as an attribute any rule in the snapshot
|
|
219
|
+
* overrides. Not light-dark(), as the stage has: Firefox takes a picture's scheme from the theme.
|
|
220
|
+
*/
|
|
221
|
+
export function svgForRaster(xml, width, height, scale, ink) {
|
|
222
|
+
const pw = Math.max(1, Math.round(width * scale));
|
|
223
|
+
const ph = Math.max(1, Math.round(height * scale));
|
|
224
|
+
return {
|
|
225
|
+
pw,
|
|
226
|
+
ph,
|
|
227
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" data-raster-size="${pw}x${ph}"${ink === undefined ? '' : ` color="${ink}"`}>` +
|
|
228
|
+
`<foreignObject x="0" y="0" width="${width}" height="${height}" style="forced-color-adjust:none">` +
|
|
229
|
+
xml +
|
|
230
|
+
'</foreignObject></svg>',
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
let forcedColors = null;
|
|
234
|
+
/** Taken before the film runs: a film's own matchMedia can replace the global. */
|
|
235
|
+
export function watchForcedColors() {
|
|
236
|
+
try {
|
|
237
|
+
forcedColors = matchMedia('(forced-colors: active)');
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
forcedColors = null;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Under forced colors (a Windows contrast theme) a frame keeps the film's own colors: the document opts out while
|
|
245
|
+
* `read` runs, or getComputedStyle would give the theme's colors. `read` must stay synchronous.
|
|
246
|
+
*/
|
|
247
|
+
export function inOwnColors(read) {
|
|
248
|
+
if (!forcedColors || !forcedColors.matches)
|
|
249
|
+
return read();
|
|
250
|
+
const root = document.documentElement;
|
|
251
|
+
const bare = !root.hasAttribute('style');
|
|
252
|
+
const style = root.style;
|
|
253
|
+
const had = style.getPropertyValue('forced-color-adjust');
|
|
254
|
+
const priority = style.getPropertyPriority('forced-color-adjust');
|
|
255
|
+
style.setProperty('forced-color-adjust', 'none', 'important');
|
|
256
|
+
try {
|
|
257
|
+
return read();
|
|
258
|
+
}
|
|
259
|
+
finally {
|
|
260
|
+
if (had)
|
|
261
|
+
style.setProperty('forced-color-adjust', had, priority);
|
|
262
|
+
else
|
|
263
|
+
style.removeProperty('forced-color-adjust');
|
|
264
|
+
if (bare) {
|
|
265
|
+
// A browser may write a style change into the attribute only when it is read; removed before that, it comes back as style="".
|
|
266
|
+
root.getAttribute('style');
|
|
267
|
+
root.removeAttribute('style');
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
export const forcedColorsActive = () => !!forcedColors && forcedColors.matches;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** copy: el's clone, or the picture that stands in for it. */
|
|
2
|
+
export declare function sayWholeStyle(el: Element, copy: Element): void;
|
|
3
|
+
/**
|
|
4
|
+
* The clone is searched first, the stage only if the clone has such a style. They are paired by position, so where
|
|
5
|
+
* their counts differ (a custom element may change itself as it is cloned) nothing is filled.
|
|
6
|
+
*/
|
|
7
|
+
export declare function sayWholeStyles(node: Element, clone: Element): void;
|
|
8
|
+
export declare function dropSlightDisplacement(clone: Element, scale: number): void;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Once a shorthand holding a var() (border: 2px solid var(--line)) has had a longhand set or removed, any write to
|
|
3
|
+
* el.style makes the browser write the other longhands with no value ("border-top-width: ;"), and a copy drawn from
|
|
4
|
+
* that text lacks them. The copy gets the values shown instead. A value over LONGEST_FILLED characters (in practice a
|
|
5
|
+
* picture) fills none: the rest alone would show the background colour without its picture.
|
|
6
|
+
*/
|
|
7
|
+
const LONGEST_FILLED = 2048;
|
|
8
|
+
/**
|
|
9
|
+
* getComputedStyle gives currentcolor as the element's own colour, which children would then inherit as fixed; and a
|
|
10
|
+
* unitless line-height in px. It rounds a border, outline or column-rule width down to device pixels, which a
|
|
11
|
+
* snapshot drawn at a ratio of 1 would draw thinner: the whole px just above is written.
|
|
12
|
+
*/
|
|
13
|
+
function shownValue(el, shown, property) {
|
|
14
|
+
const value = shown.getPropertyValue(property);
|
|
15
|
+
const up = Math.ceil(parseFloat(value));
|
|
16
|
+
if (/^(border-(top|right|bottom|left|(block|inline)-(start|end))|outline|column-rule)-width$/.test(property) &&
|
|
17
|
+
devicePixelRatio > 1 &&
|
|
18
|
+
up - parseFloat(value) < 1 / devicePixelRatio)
|
|
19
|
+
return up + 'px';
|
|
20
|
+
if (/^(text-emphasis|-webkit-text-(fill|stroke))-color$/.test(property))
|
|
21
|
+
return value === shown.color ? 'currentcolor' : value;
|
|
22
|
+
if (property !== 'line-height')
|
|
23
|
+
return value;
|
|
24
|
+
if (typeof el.computedStyleMap === 'function')
|
|
25
|
+
return String(el.computedStyleMap().get(property));
|
|
26
|
+
const ratio = parseFloat(value) / parseFloat(shown.fontSize);
|
|
27
|
+
return isFinite(ratio) ? String(ratio) : value;
|
|
28
|
+
}
|
|
29
|
+
/** copy: el's clone, or the picture that stands in for it. */
|
|
30
|
+
export function sayWholeStyle(el, copy) {
|
|
31
|
+
try {
|
|
32
|
+
const text = copy.getAttribute('style') ?? '';
|
|
33
|
+
if (!/: ( !important)?;/.test(text) || !(el instanceof HTMLElement || el instanceof SVGElement))
|
|
34
|
+
return;
|
|
35
|
+
const own = el.style;
|
|
36
|
+
const shown = getComputedStyle(el);
|
|
37
|
+
const unsaid = new Set([...own].filter((property) => own.getPropertyValue(property) === ''));
|
|
38
|
+
let fits = true;
|
|
39
|
+
// An escaped character and a quoted string are stepped over whole: a string may hold what looks like an empty value.
|
|
40
|
+
const filled = text.replace(/\\[\s\S]|"(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|(^|; )([-a-z]+): ( !important)?(?=;)/g, (found, lead, property, important) => {
|
|
41
|
+
if (!property)
|
|
42
|
+
return found;
|
|
43
|
+
const value = unsaid.has(property) ? shownValue(el, shown, property) : '';
|
|
44
|
+
if (value.length > LONGEST_FILLED)
|
|
45
|
+
fits = false;
|
|
46
|
+
return (lead ?? '') + property + ': ' + value + (important ?? '');
|
|
47
|
+
});
|
|
48
|
+
if (fits)
|
|
49
|
+
copy.setAttribute('style', filled);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// The picture lacks what it lacked before.
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The clone is searched first, the stage only if the clone has such a style. They are paired by position, so where
|
|
57
|
+
* their counts differ (a custom element may change itself as it is cloned) nothing is filled.
|
|
58
|
+
*/
|
|
59
|
+
export function sayWholeStyles(node, clone) {
|
|
60
|
+
const lacking = (root) => {
|
|
61
|
+
const query = '[style*=": ;"], [style*=": !important;"]';
|
|
62
|
+
return [...root.querySelectorAll(query), ...(root.matches(query) ? [root] : [])];
|
|
63
|
+
};
|
|
64
|
+
const copies = lacking(clone);
|
|
65
|
+
const originals = copies.length ? lacking(node) : [];
|
|
66
|
+
if (originals.length !== copies.length)
|
|
67
|
+
return;
|
|
68
|
+
copies.forEach((copy, i) => {
|
|
69
|
+
const original = originals[i];
|
|
70
|
+
if (original)
|
|
71
|
+
sayWholeStyle(original, copy);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/*
|
|
75
|
+
* For small previews: a shift of SLIGHT_PX px is hardly seen there, yet a browser filters a filter's whole region
|
|
76
|
+
* for every snapshot. A feDisplacementMap moves a pixel by at most half its scale, in the element's own units.
|
|
77
|
+
*/
|
|
78
|
+
const SLIGHT_PX = 1.25;
|
|
79
|
+
const FILTER_ID = /^\s*url\(\s*["']?#([^"')\s]+)["']?\s*\)\s*$/;
|
|
80
|
+
export function dropSlightDisplacement(clone, scale) {
|
|
81
|
+
const slight = new Map();
|
|
82
|
+
for (const filter of clone.querySelectorAll('filter')) {
|
|
83
|
+
// Where two filters share an id, a browser uses the first.
|
|
84
|
+
if (slight.has(filter.id))
|
|
85
|
+
continue;
|
|
86
|
+
const last = filter.lastElementChild;
|
|
87
|
+
slight.set(filter.id, !!last &&
|
|
88
|
+
last.localName === 'feDisplacementMap' &&
|
|
89
|
+
last.getAttribute('in') === 'SourceGraphic' &&
|
|
90
|
+
(Math.abs(parseFloat(last.getAttribute('scale') ?? '') || 0) / 2) * scale <= SLIGHT_PX &&
|
|
91
|
+
filter.getAttribute('primitiveUnits') !== 'objectBoundingBox' &&
|
|
92
|
+
!filter.hasAttribute('href') &&
|
|
93
|
+
!filter.hasAttribute('xlink:href'));
|
|
94
|
+
}
|
|
95
|
+
const isSlight = (value) => {
|
|
96
|
+
const found = FILTER_ID.exec(value);
|
|
97
|
+
return !!found && slight.get(found[1] ?? '') === true;
|
|
98
|
+
};
|
|
99
|
+
for (const named of clone.querySelectorAll('[filter]'))
|
|
100
|
+
if (isSlight(named.getAttribute('filter') ?? ''))
|
|
101
|
+
named.removeAttribute('filter');
|
|
102
|
+
// Edited as text: a write to el.style re-serializes the attribute. Set to none, not removed, so no other filter applies.
|
|
103
|
+
for (const styled of clone.querySelectorAll('[style*="url("]')) {
|
|
104
|
+
const had = styled.getAttribute('style') ?? '';
|
|
105
|
+
const now = had.replace(/(^|;)\s*filter\s*:([^;]*)/gi, (all, lead, value) => (isSlight(value) ? lead + 'filter:none' : all));
|
|
106
|
+
if (now !== had)
|
|
107
|
+
styled.setAttribute('style', now);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The node as XML text. A form feed separates class names and CSS tokens, so it becomes a space; half a surrogate
|
|
3
|
+
* pair becomes U+FFFD, as browsers draw it; other refused characters become U+2060, which draws nothing and keeps
|
|
4
|
+
* apart what they kept apart.
|
|
5
|
+
*/
|
|
6
|
+
export declare function xmlOf(root: Node): string;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A snapshot is XML, and one thing in it that XML cannot hold fails the whole picture, though HTML and the DOM accept
|
|
3
|
+
* it: names left by a stray quote inside style="…", @click, <a:b>, a prefix nothing declares, "--" in a comment.
|
|
4
|
+
* They draw nothing, so the copy drops them and renames such elements; what XML can hold stays as it is.
|
|
5
|
+
*/
|
|
6
|
+
const PLAIN_NAME = /^[A-Za-z_][-.\w]*$/;
|
|
7
|
+
const namesAsked = new Map();
|
|
8
|
+
/**
|
|
9
|
+
* XML holds a plain ASCII name, and none with any other ASCII character. The rest are put to this browser's own XML
|
|
10
|
+
* parser and the answer kept: Firefox refuses many that XML allows.
|
|
11
|
+
*/
|
|
12
|
+
function xmlHoldsName(name) {
|
|
13
|
+
if (PLAIN_NAME.test(name))
|
|
14
|
+
return true;
|
|
15
|
+
if (!/^[-.\w\u0080-\uffff]+$/.test(name))
|
|
16
|
+
return false;
|
|
17
|
+
const known = namesAsked.get(name);
|
|
18
|
+
if (known !== undefined)
|
|
19
|
+
return known;
|
|
20
|
+
if (namesAsked.size > 999)
|
|
21
|
+
namesAsked.clear();
|
|
22
|
+
const got = new DOMParser().parseFromString('<a ' + name + '=""/>', 'application/xml').documentElement.attributes;
|
|
23
|
+
const holds = got.length === 1 && got[0]?.name === name;
|
|
24
|
+
namesAsked.set(name, holds);
|
|
25
|
+
return holds;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* An attribute a:b with no namespace does nothing on the stage, but XML reads a: as a prefix that must be declared,
|
|
29
|
+
* and xmlns:… as a declaration, which may then be written twice. xml: needs no declaration. XML forbids xmlns:a="".
|
|
30
|
+
*/
|
|
31
|
+
function xmlHoldsAttribute(attribute) {
|
|
32
|
+
if (attribute.namespaceURI === null)
|
|
33
|
+
return xmlHoldsName(attribute.name.replace(/^xml:/, ''));
|
|
34
|
+
return (xmlHoldsName(attribute.localName) &&
|
|
35
|
+
(!attribute.prefix || xmlHoldsName(attribute.prefix)) &&
|
|
36
|
+
!(attribute.prefix === 'xmlns' && attribute.value === ''));
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* XML cannot hold a control character other than tab, line feed and carriage return, U+FFFE, U+FFFF, or half a
|
|
40
|
+
* surrogate pair.
|
|
41
|
+
*/
|
|
42
|
+
const NOT_IN_XML = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\ufffe\uffff]/;
|
|
43
|
+
const SHOW_ELEMENTS_PIS_COMMENTS = 0x1 | 0x40 | 0x80;
|
|
44
|
+
// Everything is found before anything changes, so a throw while looking (a film may replace DOMParser) leaves the copy whole.
|
|
45
|
+
function leaveOutOfXml(root) {
|
|
46
|
+
const attributes = [];
|
|
47
|
+
const nodes = [];
|
|
48
|
+
const misnamed = [];
|
|
49
|
+
const find = (under) => {
|
|
50
|
+
const walker = document.createTreeWalker(under, SHOW_ELEMENTS_PIS_COMMENTS);
|
|
51
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
52
|
+
if (node instanceof Comment) {
|
|
53
|
+
if (NOT_IN_XML.test(node.data) || /--|-$/.test(node.data))
|
|
54
|
+
nodes.push(node);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (node instanceof ProcessingInstruction) {
|
|
58
|
+
if (NOT_IN_XML.test(node.data) || /^xml$/i.test(node.target) || !xmlHoldsName(node.target))
|
|
59
|
+
nodes.push(node);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (!(node instanceof Element))
|
|
63
|
+
continue;
|
|
64
|
+
if (node instanceof HTMLTemplateElement)
|
|
65
|
+
find(node.content);
|
|
66
|
+
if (!xmlHoldsName(node.localName) || (node.prefix && !xmlHoldsName(node.prefix)))
|
|
67
|
+
misnamed.push(node);
|
|
68
|
+
if (!node.getAttributeNames().every((name) => PLAIN_NAME.test(name)))
|
|
69
|
+
for (const attribute of node.attributes)
|
|
70
|
+
if (!xmlHoldsAttribute(attribute))
|
|
71
|
+
attributes.push(attribute);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
find(root);
|
|
75
|
+
for (const attribute of attributes)
|
|
76
|
+
attribute.ownerElement?.removeAttributeNode(attribute);
|
|
77
|
+
for (const node of nodes)
|
|
78
|
+
node.parentNode?.removeChild(node);
|
|
79
|
+
// An element the browser does not know draws the same under any unknown name; one with no hyphen, so no film can have defined it.
|
|
80
|
+
for (const element of misnamed) {
|
|
81
|
+
const renamed = document.createElementNS(element.namespaceURI, 'x_unnamed');
|
|
82
|
+
while (element.attributes.length) {
|
|
83
|
+
const first = element.attributes[0];
|
|
84
|
+
if (first)
|
|
85
|
+
renamed.setAttributeNode(element.removeAttributeNode(first));
|
|
86
|
+
}
|
|
87
|
+
while (element.firstChild)
|
|
88
|
+
renamed.appendChild(element.firstChild);
|
|
89
|
+
element.parentNode?.replaceChild(renamed, element);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The node as XML text. A form feed separates class names and CSS tokens, so it becomes a space; half a surrogate
|
|
94
|
+
* pair becomes U+FFFD, as browsers draw it; other refused characters become U+2060, which draws nothing and keeps
|
|
95
|
+
* apart what they kept apart.
|
|
96
|
+
*/
|
|
97
|
+
export function xmlOf(root) {
|
|
98
|
+
try {
|
|
99
|
+
leaveOutOfXml(root);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
// The copy goes out whole, and only its characters are mended.
|
|
103
|
+
}
|
|
104
|
+
let text = new XMLSerializer().serializeToString(root);
|
|
105
|
+
if (NOT_IN_XML.test(text))
|
|
106
|
+
text = text.replace(/\u000c/g, ' ').replace(/[\u0000-\u0008\u000b\u000e-\u001f\ufffe\uffff]/g, '\u2060');
|
|
107
|
+
return /[\ud800-\udfff]/.test(text)
|
|
108
|
+
? text.replace(/[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]/g, (found) => (found.length === 2 ? found : '\ufffd'))
|
|
109
|
+
: text;
|
|
110
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { FxPicture } from './snapshot/serialize.ts';
|
|
2
|
+
export interface FxWatch extends FxPicture {
|
|
3
|
+
clear(width: number, height: number): void;
|
|
4
|
+
reset(): void;
|
|
5
|
+
end(): void;
|
|
6
|
+
}
|
|
7
|
+
/** Inside .words a heading or paragraph has no margin unless the film gives it one; alone in the first layer, any rule of the film's wins. */
|
|
8
|
+
export declare const noMargin: (within: string) => string;
|
|
9
|
+
export interface StageParts {
|
|
10
|
+
root: HTMLElement;
|
|
11
|
+
stage: HTMLElement;
|
|
12
|
+
fontStyle: HTMLStyleElement;
|
|
13
|
+
fxCanvas: HTMLCanvasElement;
|
|
14
|
+
fx: CanvasRenderingContext2D;
|
|
15
|
+
fxWatch: FxWatch;
|
|
16
|
+
}
|
|
17
|
+
export declare const stageParts: () => StageParts | null;
|
|
18
|
+
export declare function builtStage(): StageParts;
|
|
19
|
+
export declare function buildStage(width: number, height: number, background: string): StageParts;
|
|
20
|
+
/** Scales the stage to the frame, centred. */
|
|
21
|
+
export declare function fit(): void;
|
|
22
|
+
/** A new frame has no layout until it has its size: there is nothing to measure on the stage before then. */
|
|
23
|
+
export declare function laidOut(): boolean;
|
|
24
|
+
/** Settles once the stage has a layout, or after `ms` without one. Timers, not animation frames: a hidden frame gets none. */
|
|
25
|
+
export declare function whenLaidOut(ms: number): Promise<void>;
|
|
26
|
+
/** A film may have placed the stage itself: at an unchanged frame size it is left alone. */
|
|
27
|
+
export declare function fitIfResized(): void;
|