@openartifactsai/motion-stage 0.0.0-stage → 0.23.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.
Files changed (225) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +70 -4
  5. package/src/build.d.ts +18 -0
  6. package/src/build.js +73 -0
  7. package/src/host.d.ts +37 -0
  8. package/src/host.js +76 -0
  9. package/src/index.d.ts +11 -0
  10. package/src/index.js +6 -0
  11. package/src/layers.d.ts +30 -0
  12. package/src/layers.js +56 -0
  13. package/src/offline/audio.d.ts +19 -0
  14. package/src/offline/audio.js +20 -0
  15. package/src/offline/data.d.ts +26 -0
  16. package/src/offline/data.js +71 -0
  17. package/src/offline/main.d.ts +9 -0
  18. package/src/offline/main.js +52 -0
  19. package/src/offline/player.d.ts +23 -0
  20. package/src/offline/player.js +157 -0
  21. package/src/protocol/clip-guards.d.ts +5 -0
  22. package/src/protocol/clip-guards.js +22 -0
  23. package/src/protocol/guards.d.ts +6 -0
  24. package/src/protocol/guards.js +257 -0
  25. package/src/protocol/layer-guards.d.ts +7 -0
  26. package/src/protocol/layer-guards.js +100 -0
  27. package/src/protocol/layers.d.ts +135 -0
  28. package/src/protocol/layers.js +1 -0
  29. package/src/protocol/types.d.ts +340 -0
  30. package/src/protocol/types.js +7 -0
  31. package/src/protocol.d.ts +2 -0
  32. package/src/protocol.js +2 -0
  33. package/src/raster.d.ts +4 -0
  34. package/src/raster.js +2 -0
  35. package/src/runtime/assets.d.ts +19 -0
  36. package/src/runtime/assets.js +63 -0
  37. package/src/runtime/caption-box.d.ts +3 -0
  38. package/src/runtime/caption-box.js +16 -0
  39. package/src/runtime/captions.d.ts +31 -0
  40. package/src/runtime/captions.js +115 -0
  41. package/src/runtime/clip-captions.d.ts +9 -0
  42. package/src/runtime/clip-captions.js +27 -0
  43. package/src/runtime/clips.d.ts +83 -0
  44. package/src/runtime/clips.js +259 -0
  45. package/src/runtime/console.d.ts +13 -0
  46. package/src/runtime/console.js +99 -0
  47. package/src/runtime/cut.d.ts +7 -0
  48. package/src/runtime/cut.js +63 -0
  49. package/src/runtime/determinism.d.ts +12 -0
  50. package/src/runtime/determinism.js +47 -0
  51. package/src/runtime/film.d.ts +43 -0
  52. package/src/runtime/film.js +31 -0
  53. package/src/runtime/focus.d.ts +8 -0
  54. package/src/runtime/focus.js +71 -0
  55. package/src/runtime/fonts.d.ts +22 -0
  56. package/src/runtime/fonts.js +109 -0
  57. package/src/runtime/frame.d.ts +9 -0
  58. package/src/runtime/frame.js +140 -0
  59. package/src/runtime/fx/backdrop.d.ts +60 -0
  60. package/src/runtime/fx/backdrop.js +264 -0
  61. package/src/runtime/fx/color.d.ts +19 -0
  62. package/src/runtime/fx/color.js +84 -0
  63. package/src/runtime/fx/css-color.d.ts +6 -0
  64. package/src/runtime/fx/css-color.js +44 -0
  65. package/src/runtime/fx/curve.d.ts +12 -0
  66. package/src/runtime/fx/curve.js +34 -0
  67. package/src/runtime/fx/gl.d.ts +29 -0
  68. package/src/runtime/fx/gl.js +207 -0
  69. package/src/runtime/fx/glsl.d.ts +35 -0
  70. package/src/runtime/fx/glsl.js +113 -0
  71. package/src/runtime/fx/index.d.ts +23 -0
  72. package/src/runtime/fx/index.js +151 -0
  73. package/src/runtime/fx/key.d.ts +11 -0
  74. package/src/runtime/fx/key.js +21 -0
  75. package/src/runtime/fx/numbers.d.ts +14 -0
  76. package/src/runtime/fx/numbers.js +57 -0
  77. package/src/runtime/fx/pace.d.ts +17 -0
  78. package/src/runtime/fx/pace.js +26 -0
  79. package/src/runtime/fx/paint.d.ts +28 -0
  80. package/src/runtime/fx/paint.js +150 -0
  81. package/src/runtime/guard.d.ts +4 -0
  82. package/src/runtime/guard.js +141 -0
  83. package/src/runtime/hand-titles/index.d.ts +9 -0
  84. package/src/runtime/hand-titles/index.js +73 -0
  85. package/src/runtime/hand-titles/layer.d.ts +20 -0
  86. package/src/runtime/hand-titles/layer.js +118 -0
  87. package/src/runtime/hand-titles/place.d.ts +27 -0
  88. package/src/runtime/hand-titles/place.js +45 -0
  89. package/src/runtime/held-store.d.ts +35 -0
  90. package/src/runtime/held-store.js +63 -0
  91. package/src/runtime/held.d.ts +19 -0
  92. package/src/runtime/held.js +225 -0
  93. package/src/runtime/kit/camera.d.ts +59 -0
  94. package/src/runtime/kit/camera.js +81 -0
  95. package/src/runtime/kit/chip.d.ts +25 -0
  96. package/src/runtime/kit/chip.js +75 -0
  97. package/src/runtime/kit/count.d.ts +37 -0
  98. package/src/runtime/kit/count.js +100 -0
  99. package/src/runtime/kit/cursor.d.ts +46 -0
  100. package/src/runtime/kit/cursor.js +108 -0
  101. package/src/runtime/kit/dom.d.ts +14 -0
  102. package/src/runtime/kit/dom.js +46 -0
  103. package/src/runtime/kit/geometry.d.ts +24 -0
  104. package/src/runtime/kit/geometry.js +56 -0
  105. package/src/runtime/kit/index.d.ts +113 -0
  106. package/src/runtime/kit/index.js +157 -0
  107. package/src/runtime/kit/mark-strokes.d.ts +65 -0
  108. package/src/runtime/kit/mark-strokes.js +226 -0
  109. package/src/runtime/kit/marks.d.ts +21 -0
  110. package/src/runtime/kit/marks.js +165 -0
  111. package/src/runtime/kit/math.d.ts +23 -0
  112. package/src/runtime/kit/math.js +35 -0
  113. package/src/runtime/kit/paper.d.ts +7 -0
  114. package/src/runtime/kit/paper.js +27 -0
  115. package/src/runtime/kit/random.d.ts +8 -0
  116. package/src/runtime/kit/random.js +32 -0
  117. package/src/runtime/kit/text-fx.d.ts +18 -0
  118. package/src/runtime/kit/text-fx.js +55 -0
  119. package/src/runtime/kit/typing.d.ts +21 -0
  120. package/src/runtime/kit/typing.js +42 -0
  121. package/src/runtime/laid/layer.d.ts +44 -0
  122. package/src/runtime/laid/layer.js +370 -0
  123. package/src/runtime/layers/describe.d.ts +41 -0
  124. package/src/runtime/layers/describe.js +327 -0
  125. package/src/runtime/layers/item.d.ts +123 -0
  126. package/src/runtime/layers/item.js +150 -0
  127. package/src/runtime/layers/parts.d.ts +53 -0
  128. package/src/runtime/layers/parts.js +158 -0
  129. package/src/runtime/layers/rows.d.ts +4 -0
  130. package/src/runtime/layers/rows.js +233 -0
  131. package/src/runtime/layers/scan.d.ts +24 -0
  132. package/src/runtime/layers/scan.js +204 -0
  133. package/src/runtime/layers/walk.d.ts +16 -0
  134. package/src/runtime/layers/walk.js +337 -0
  135. package/src/runtime/load.d.ts +10 -0
  136. package/src/runtime/load.js +267 -0
  137. package/src/runtime/main.d.ts +1 -0
  138. package/src/runtime/main.js +90 -0
  139. package/src/runtime/modules.d.ts +23 -0
  140. package/src/runtime/modules.js +53 -0
  141. package/src/runtime/port.d.ts +8 -0
  142. package/src/runtime/port.js +46 -0
  143. package/src/runtime/questions.d.ts +24 -0
  144. package/src/runtime/questions.js +162 -0
  145. package/src/runtime/recut.d.ts +81 -0
  146. package/src/runtime/recut.js +325 -0
  147. package/src/runtime/render.d.ts +80 -0
  148. package/src/runtime/render.js +358 -0
  149. package/src/runtime/retouch/ask.d.ts +17 -0
  150. package/src/runtime/retouch/ask.js +171 -0
  151. package/src/runtime/retouch/entry.d.ts +77 -0
  152. package/src/runtime/retouch/entry.js +1 -0
  153. package/src/runtime/retouch/judge.d.ts +25 -0
  154. package/src/runtime/retouch/judge.js +209 -0
  155. package/src/runtime/retouch/look.d.ts +39 -0
  156. package/src/runtime/retouch/look.js +135 -0
  157. package/src/runtime/retouch/own-styles.d.ts +19 -0
  158. package/src/runtime/retouch/own-styles.js +141 -0
  159. package/src/runtime/retouch/parts.d.ts +22 -0
  160. package/src/runtime/retouch/parts.js +166 -0
  161. package/src/runtime/retouch/retouch.d.ts +37 -0
  162. package/src/runtime/retouch/retouch.js +238 -0
  163. package/src/runtime/retouch/seat.d.ts +40 -0
  164. package/src/runtime/retouch/seat.js +228 -0
  165. package/src/runtime/retouch/state.d.ts +37 -0
  166. package/src/runtime/retouch/state.js +153 -0
  167. package/src/runtime/retouch/type-style.d.ts +19 -0
  168. package/src/runtime/retouch/type-style.js +111 -0
  169. package/src/runtime/scan.d.ts +2 -0
  170. package/src/runtime/scan.js +49 -0
  171. package/src/runtime/scenes/card.d.ts +12 -0
  172. package/src/runtime/scenes/card.js +56 -0
  173. package/src/runtime/scenes/install.d.ts +69 -0
  174. package/src/runtime/scenes/install.js +528 -0
  175. package/src/runtime/scenes/read.d.ts +27 -0
  176. package/src/runtime/scenes/read.js +73 -0
  177. package/src/runtime/scenes/scoped.d.ts +13 -0
  178. package/src/runtime/scenes/scoped.js +46 -0
  179. package/src/runtime/score.d.ts +32 -0
  180. package/src/runtime/score.js +262 -0
  181. package/src/runtime/sketch/pen.d.ts +3 -0
  182. package/src/runtime/sketch/pen.js +371 -0
  183. package/src/runtime/sketch/strokes.d.ts +41 -0
  184. package/src/runtime/sketch/strokes.js +187 -0
  185. package/src/runtime/snapshot/draw.d.ts +14 -0
  186. package/src/runtime/snapshot/draw.js +125 -0
  187. package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
  188. package/src/runtime/snapshot/hold-first-draw.js +128 -0
  189. package/src/runtime/snapshot/lift.d.ts +41 -0
  190. package/src/runtime/snapshot/lift.js +161 -0
  191. package/src/runtime/snapshot/plate.d.ts +20 -0
  192. package/src/runtime/snapshot/plate.js +79 -0
  193. package/src/runtime/snapshot/serialize.d.ts +51 -0
  194. package/src/runtime/snapshot/serialize.js +286 -0
  195. package/src/runtime/snapshot/styles.d.ts +8 -0
  196. package/src/runtime/snapshot/styles.js +109 -0
  197. package/src/runtime/snapshot/xml.d.ts +6 -0
  198. package/src/runtime/snapshot/xml.js +110 -0
  199. package/src/runtime/stage-size.d.ts +7 -0
  200. package/src/runtime/stage-size.js +3 -0
  201. package/src/runtime/stage.d.ts +27 -0
  202. package/src/runtime/stage.js +154 -0
  203. package/src/runtime/stretch.d.ts +13 -0
  204. package/src/runtime/stretch.js +16 -0
  205. package/src/runtime/texts.d.ts +24 -0
  206. package/src/runtime/texts.js +211 -0
  207. package/src/runtime/video-sync.d.ts +51 -0
  208. package/src/runtime/video-sync.js +218 -0
  209. package/src/stills.d.ts +90 -0
  210. package/src/stills.js +329 -0
  211. package/src/tools/film.d.ts +13 -0
  212. package/src/tools/film.js +116 -0
  213. package/src/tools/levels.d.ts +8 -0
  214. package/src/tools/levels.js +28 -0
  215. package/src/tools/main.d.ts +1 -0
  216. package/src/tools/main.js +223 -0
  217. package/src/tools/score.d.ts +4 -0
  218. package/src/tools/score.js +35 -0
  219. package/src/tools/types.d.ts +84 -0
  220. package/src/tools/types.js +2 -0
  221. package/src/tools.d.ts +6 -0
  222. package/src/tools.js +4 -0
  223. package/src/virtual.d.ts +11 -0
  224. package/src/vite.d.ts +8 -0
  225. package/src/vite.js +43 -0
@@ -0,0 +1,228 @@
1
+ import { inOwnColors } from "../snapshot/serialize.js";
2
+ import { ART, SKIP, UNIT, drawnAs, isArtWords, isPart, isWords, kidsOf, saidOf } from "../layers/parts.js";
3
+ import { holds } from "./parts.js";
4
+ /*
5
+ * How an entry of the record finds its thing again on a stage that may have been drawn anew: by its tag and classes,
6
+ * its turn among like things, and its print, a short hash of its words.
7
+ */
8
+ const SVG_NS = 'http://www.w3.org/2000/svg';
9
+ /**
10
+ * The line of words a text node is part of, as "place/tag": which child of which, from the stage down, then the tag.
11
+ * null: there is none, or the way leads into a picture, the cursor or the fx canvas, each of which is one thing. An
12
+ * inline svg is entered only through g parts, down to a text.
13
+ */
14
+ export function lineOf(stage, node, memo) {
15
+ const way = [];
16
+ let el = node.parentElement;
17
+ for (; el && el !== stage; el = el.parentElement)
18
+ way.unshift(el);
19
+ if (!el)
20
+ return null;
21
+ const of = (x) => {
22
+ let known = memo.get(x);
23
+ if (!known)
24
+ memo.set(x, (known = { k: -1, words: null, places: null }));
25
+ return known;
26
+ };
27
+ const path = [];
28
+ let up = stage;
29
+ for (const step of way) {
30
+ const above = of(up);
31
+ const known = of(step);
32
+ if (!above.places) {
33
+ const places = new Map();
34
+ kidsOf(up).forEach((child, k) => places.set(child, k));
35
+ above.places = places;
36
+ }
37
+ if (known.words === null) {
38
+ const k = above.places.get(step);
39
+ const art = step.namespaceURI === SVG_NS;
40
+ const shut = art && up.namespaceURI === step.namespaceURI ? !(isPart(step) && /^(g|text)$/i.test(step.tagName)) : !art && UNIT.test(step.tagName);
41
+ known.k = k === undefined || (step.id === 'fx' && up === stage) || shut || step.classList.contains('film-cursor') ? -1 : k;
42
+ known.words = known.k >= 0 && (!art || isArtWords(step)) && isWords(step);
43
+ }
44
+ if (known.k < 0)
45
+ return null;
46
+ path.push(known.k);
47
+ if (known.words)
48
+ return path.join('.') + '/' + step.tagName.toLowerCase();
49
+ up = step;
50
+ }
51
+ return null;
52
+ }
53
+ export function fits(el, tag, cls) {
54
+ return el.tagName.toLowerCase() === tag && cls.every((c) => el.classList.contains(c));
55
+ }
56
+ export function standsAt(stage, path) {
57
+ let el = stage;
58
+ for (let k = 0; el && k < path.length; k++)
59
+ el = kidsOf(el)[path[k]];
60
+ return el ?? null;
61
+ }
62
+ /** The things on the stage as drawn, shown or not, in document order: not the fx canvas, nor what is inside a picture, the cursor or a line of words. */
63
+ function things(stage) {
64
+ const out = [];
65
+ const walk = (up) => {
66
+ for (const el of kidsOf(up)) {
67
+ if (el.id === 'fx' && up === stage)
68
+ continue;
69
+ out.push(el);
70
+ if (!UNIT.test(el.tagName) && !el.classList.contains('film-cursor') && !isWords(el))
71
+ walk(el);
72
+ }
73
+ };
74
+ inOwnColors(() => walk(stage));
75
+ return out;
76
+ }
77
+ /** The parts inside the svgs among `all`, in document order, listed apart so every other thing counts as if the svgs were closed. */
78
+ function partsIn(all) {
79
+ const out = [];
80
+ const walk = (up) => {
81
+ for (const el of kidsOf(up).filter(isPart)) {
82
+ out.push(el);
83
+ if (/^g$/i.test(el.tagName))
84
+ walk(el);
85
+ }
86
+ };
87
+ all.filter((el) => /^svg$/i.test(el.tagName)).forEach(walk);
88
+ return out;
89
+ }
90
+ /** Where a part stands in its drawing: down from the svg, its turn among the parts of its tag beside it, their number, and what its own attributes say. */
91
+ function standing(el, memo) {
92
+ const turns = (memo.turns ??= new Map());
93
+ const parent = el.parentElement;
94
+ if (!parent)
95
+ return '';
96
+ if (!turns.has(el)) {
97
+ const all = kidsOf(parent).filter(isPart);
98
+ const n = new Map();
99
+ const k = new Map();
100
+ for (const c of all)
101
+ n.set(c.tagName, (n.get(c.tagName) ?? 0) + 1);
102
+ for (const c of all) {
103
+ k.set(c.tagName, (k.get(c.tagName) ?? 0) + 1);
104
+ turns.set(c, k.get(c.tagName) + '/' + n.get(c.tagName) + ' ' + saidOf(c));
105
+ }
106
+ }
107
+ return (isPart(parent) ? standing(parent, memo) : saidOf(parent)) + ' > ' + (turns.get(el) ?? '');
108
+ }
109
+ /** An element's words, a sheet's or a script's text left out. */
110
+ export function wordsOf(el, memo) {
111
+ const words = (memo.words ??= new Map());
112
+ const known = words.get(el);
113
+ if (known !== undefined)
114
+ return known;
115
+ let s = '';
116
+ for (let n = el.firstChild; n; n = n.nextSibling)
117
+ s += n instanceof Text ? n.data : n instanceof Element && !SKIP.test(n.tagName) ? ' ' + wordsOf(n, memo) + ' ' : '';
118
+ const out = s.replace(/\s+/g, ' ').trim();
119
+ words.set(el, out);
120
+ return out;
121
+ }
122
+ /**
123
+ * A thing's print: a short hash (FNV-1a) of its words; with none, of "|" and the words of the nearest element round
124
+ * it that has some; for a part of an svg with none, of "#", where it stands in the drawing and how it is drawn.
125
+ */
126
+ export function printOf(stage, el, memo) {
127
+ let s = wordsOf(el, memo);
128
+ if (!s && isPart(el))
129
+ s = '#' + standing(el, memo) + ' ' + drawnAs(el, false, (memo.drawn ??= new Map()));
130
+ else if (!s) {
131
+ for (let up = el.parentElement; up && up !== stage && !s; up = up.parentElement)
132
+ s = wordsOf(up, memo);
133
+ s = '|' + s;
134
+ }
135
+ let h = 0x811c9dc5;
136
+ for (let i = 0; i < s.length; i++)
137
+ h = Math.imul(h ^ s.charCodeAt(i), 16777619);
138
+ return (h >>> 0).toString(36);
139
+ }
140
+ /** The things of a tag as they are now, their classes as values: the stage may be drawn anew before they are read. */
141
+ export function peers(stage, memo, tag) {
142
+ const all = (memo.all ??= things(stage));
143
+ const tags = (memo.tags ??= new Map());
144
+ const known = tags.get(tag);
145
+ if (known)
146
+ return known;
147
+ const pool = ART.test(tag) ? (memo.art ??= all.concat(partsIn(all))) : all;
148
+ const out = pool.filter((el) => el.tagName.toLowerCase() === tag).map((el) => ({ el, cls: Array.from(el.classList), print: null }));
149
+ tags.set(tag, out);
150
+ return out;
151
+ }
152
+ /** Those of them that have every class of `cls`, with their prints, made now unless they were before. */
153
+ export function like(stage, all, cls, memo) {
154
+ return all
155
+ .filter((x) => cls.every((c) => x.cls.includes(c)))
156
+ .map((x) => {
157
+ if (x.print === null)
158
+ x.print = printOf(stage, x.el, memo);
159
+ return x;
160
+ });
161
+ }
162
+ /** An entry's kin: which of how many like things the element is, then its print if no other of them has it. null: it is not one of them. */
163
+ export function kinOf(stage, all, el, cls, memo) {
164
+ const l = like(stage, all, cls, memo);
165
+ const k = l.findIndex((x) => x.el === el);
166
+ if (k < 0)
167
+ return null;
168
+ return k + '/' + l.length + (l.some((x, i) => i !== k && x.print === l[k].print) ? '' : '/' + l[k].print);
169
+ }
170
+ /** A part of an svg is found by its print alone, but for an svg text that the entry does not hide. */
171
+ const byPrintAlone = (e) => (x) => isPart(x.el) && (!!e.pairs.hidden || !isArtWords(x.el));
172
+ /**
173
+ * The hidden g of a scene that its entry's print no longer finds, after a change inside it: only where one g had the
174
+ * entry's classes when it was saved and one has them now, with no other class and no words, and it still holds the
175
+ * entry's parts.
176
+ */
177
+ function byParts(e, l, memo) {
178
+ const only = l.length === 1 ? l[0] : null;
179
+ if (!only || !e.pairs.hidden || !e.print || !e.parts || e.scene === 'film' || e.n !== 1)
180
+ return null;
181
+ if (!/^g$/i.test(only.el.tagName) || !only.cls.every((c) => e.cls.includes(c)) || wordsOf(only.el, memo))
182
+ return null;
183
+ return holds(only.el, e.parts, memo) ? only : null;
184
+ }
185
+ /**
186
+ * An entry's thing on the stage as drawn: the one like thing with its print; else, among as many as there were, the
187
+ * one in its turn, unless its print is on others only; and a hidden g whose print is on nothing, by its parts. null:
188
+ * it cannot be told. `own`: at the entry's own moment; in another stretch one that has a print is found by that alone.
189
+ */
190
+ export function seat(stage, e, memo, own) {
191
+ const l = like(stage, peers(stage, memo, e.tag), e.cls, memo);
192
+ const said = e.print ? l.filter((x) => x.print === e.print) : [];
193
+ const inTurn = l[e.k];
194
+ const found = said.length === 1 ? said[0] : !l.some(byPrintAlone(e)) && (own || !e.print) && l.length === e.n && inTurn && (!said.length || said.includes(inTurn)) ? inTurn : null;
195
+ const held = found ? null : byParts(e, l, memo);
196
+ const it = found ?? held;
197
+ if (!it)
198
+ return null;
199
+ const shared = like(stage, peers(stage, memo, e.tag), [], memo).some((x) => x !== it && x.print === it.print);
200
+ return {
201
+ el: it.el,
202
+ sure: said.length === 1,
203
+ ...(held ? { held: true, had: it.print } : {}),
204
+ worded: !!wordsOf(it.el, memo),
205
+ k: l.indexOf(it),
206
+ n: l.length,
207
+ print: shared ? null : it.print,
208
+ };
209
+ }
210
+ /**
211
+ * The element an entry is laid on now: the node it was found on, while that is on the stage. Once the node has left,
212
+ * the like thing in the same turn while there are as many; else the one thing of its tag with the print it had then.
213
+ * What is found by its print alone: the one like part with the entry's print, or the print it had when its parts found it.
214
+ */
215
+ export function find(stage, e, s, memo) {
216
+ if (stage.contains(s.el))
217
+ return s.el;
218
+ const all = peers(stage, memo, e.tag);
219
+ const l = like(stage, all, e.cls, memo);
220
+ const print = s.held ? s.had : e.print;
221
+ const mine = (e.pairs.hidden || e.pairs.svgfill || e.pairs.svgstroke) && e.print ? l.filter((x) => x.print === print) : [];
222
+ if (mine.length || l.some(byPrintAlone(e)))
223
+ return mine.length === 1 ? mine[0].el : null;
224
+ if (l.length === s.n)
225
+ return l[s.k]?.el ?? null;
226
+ const said = s.print === null ? [] : like(stage, all, [], memo).filter((x) => x.print === s.print);
227
+ return said.length === 1 ? said[0].el : null;
228
+ }
@@ -0,0 +1,37 @@
1
+ import type { RetouchedRecord } from '@openartifactsai/motion-core';
2
+ import type { Entry, Told } from './entry.js';
3
+ /** Which entry marks the element. */
4
+ export declare const MARK = "data-film-over";
5
+ /** What of it is laid on the element: its keys; for the radius, the corners that take it; for an svg text's ink, "paint". */
6
+ export declare const LAID = "data-film-laid";
7
+ /** What of it no longer counts. */
8
+ export declare const LAPSED = "data-film-lapsed";
9
+ export declare const ID: RegExp;
10
+ export declare const state: {
11
+ list: Entry[];
12
+ /** The elements this stage marked at its last pass: a mark a film's copy of a node brings along does not count. */
13
+ marked: Element[];
14
+ sheet: HTMLStyleElement | null;
15
+ };
16
+ /** The record's entries as this stage lays them; the record is parsed again, since this is the trust boundary for the sheet. */
17
+ export declare function entriesOf(record: RetouchedRecord): Entry[];
18
+ /** The sheet's text. Of a pair only `v` reaches it, a colour, a number or a word out of a closed set; no selector is made of anything in the record. */
19
+ export declare function rulesText(list: readonly Entry[]): string;
20
+ export declare function unmark(): void;
21
+ /** The entry that marks an element. */
22
+ export declare function marks(el: Element): Entry | undefined;
23
+ export declare const lapsedOn: (el: Element) => string[];
24
+ export declare function toldAt(e: Entry, lo: number): Told | null;
25
+ /** Of an entry of the scene nothing could be told in this load: which element is its own is not known, so no look is told of any. */
26
+ export declare const blind: (scene: string, lo: number) => boolean;
27
+ /** An entry's own moment lies in the stretch: a scene's always does, since it counts from the scene's start. */
28
+ export declare const home: (e: Entry, lo: number, hi: number) => boolean;
29
+ /**
30
+ * One found otherwise keeps an element from one found by its parts, and one found by its print from one found by its
31
+ * turn; else the later in the record, changed last, takes it.
32
+ */
33
+ export declare function takes(e: Entry, w: Entry, lo: number): boolean;
34
+ /** Of entries seated on one node one is laid; the others, which apply passes over and lost names. */
35
+ export declare function beaten(scene: string, lo: number): Entry[];
36
+ /** All of an entry that a verdict on it rests on: not `v`. */
37
+ export declare function basis(e: Entry): string;
@@ -0,0 +1,153 @@
1
+ import { parseRetouchedRecord } from '@openartifactsai/motion-core';
2
+ import { PAINT, PAINT_KEYS, SHAPE } from "../layers/parts.js";
3
+ import { CORNERS, KEYS } from "./look.js";
4
+ import { TYPE_KEYS, typeRule } from "./type-style.js";
5
+ /*
6
+ * What a person did by hand to the look of things, as this stage lays it. After each frame is drawn the thing's
7
+ * element is marked with attributes, and one sheet in the stage styles what is marked. So the live view, the
8
+ * thumbnails, an export and a still made offline all show it; the film's own code and inline styles are not touched,
9
+ * and come back when the mark goes.
10
+ */
11
+ /** Which entry marks the element. */
12
+ export const MARK = 'data-film-over';
13
+ /** What of it is laid on the element: its keys; for the radius, the corners that take it; for an svg text's ink, "paint". */
14
+ export const LAID = 'data-film-laid';
15
+ /** What of it no longer counts. */
16
+ export const LAPSED = 'data-film-lapsed';
17
+ export const ID = /^([a-z][a-z0-9-]{0,31}):(\d{1,3}(?:\.\d{1,3}){0,23})\/([a-z][a-z0-9-]{0,31})$/;
18
+ const KIN = /^(\d{1,4})\/(\d{1,4})(?:\/([0-9a-z]{1,7}))?$/;
19
+ export const state = {
20
+ list: [],
21
+ /** The elements this stage marked at its last pass: a mark a film's copy of a node brings along does not count. */
22
+ marked: [],
23
+ sheet: null,
24
+ };
25
+ /** The record's entries as this stage lays them; the record is parsed again, since this is the trust boundary for the sheet. */
26
+ export function entriesOf(record) {
27
+ const out = [];
28
+ for (const layer of parseRetouchedRecord({ v: 1, layers: record.layers }).layers) {
29
+ const id = ID.exec(layer.id);
30
+ const kin = KIN.exec(layer.kin);
31
+ if (!id || !kin)
32
+ continue;
33
+ const tag = id[3];
34
+ const entry = {
35
+ id: layer.id,
36
+ at: layer.at,
37
+ told: new Map(),
38
+ scene: id[1],
39
+ tag,
40
+ cls: layer.cls.split(/\s+/).filter(Boolean),
41
+ kin: layer.kin,
42
+ k: Number(kin[1]),
43
+ n: Number(kin[2]),
44
+ print: kin[3] ?? null,
45
+ parts: layer.parts ? layer.parts.split('.') : null,
46
+ pairs: {},
47
+ };
48
+ for (const key of KEYS) {
49
+ const pair = layer[key];
50
+ if (!pair)
51
+ continue;
52
+ if ((key === 'svgfill' || key === 'svgstroke') && !SHAPE.test(tag))
53
+ continue;
54
+ entry.pairs[key] = { was: pair.was, v: pair.v };
55
+ }
56
+ // With no pair it lays it changes nothing.
57
+ if (KEYS.some((key) => entry.pairs[key]))
58
+ out.push(entry);
59
+ }
60
+ return out;
61
+ }
62
+ /** The sheet's text. Of a pair only `v` reaches it, a colour, a number or a word out of a closed set; no selector is made of anything in the record. */
63
+ export function rulesText(list) {
64
+ return list
65
+ .map((e, n) => {
66
+ const at = (what) => '[' + MARK + '="' + n + '"][' + LAID + '~="' + what + '"]';
67
+ // Twice: in a layer it also wins over every rule of the film's that says !important outside one; a browser that knows no layers takes the first.
68
+ const twice = (rule) => rule + '@layer film-laid{' + rule + '}';
69
+ const colour = (key) => String(e.pairs[key]?.v ?? '').toLowerCase();
70
+ let out = '';
71
+ if (e.pairs.fill)
72
+ out += at('fill') + '{background-image:none!important;background-color:' + colour('fill') + '!important}';
73
+ if (e.pairs.ink)
74
+ out += twice(at('ink') + '{color:' + colour('ink') + '!important}') + twice(at('paint') + '{fill:' + colour('ink') + '!important}');
75
+ // The property, not a transform: the film's own transform, and the point it turns on, go on counting.
76
+ if (e.pairs.size)
77
+ out += at('size') + '{scale:' + Number(e.pairs.size.v) + '!important}';
78
+ if (e.pairs.radius)
79
+ for (const corner of CORNERS)
80
+ out += at(corner) + '{border-' + corner + '-radius:' + Number(e.pairs.radius.v) + 'px!important}';
81
+ // Visibility keeps its room, so nothing round it moves; on all under it too, since a child that says visible would otherwise show.
82
+ if (e.pairs.hidden)
83
+ out += at('hidden') + ',' + at('hidden') + ' *{visibility:hidden!important}';
84
+ for (const key of TYPE_KEYS) {
85
+ const pair = e.pairs[key];
86
+ if (pair)
87
+ out += twice(at(key) + '{' + typeRule(key, pair.v) + '}');
88
+ }
89
+ for (const key of PAINT_KEYS)
90
+ if (e.pairs[key])
91
+ out += twice(at(key) + '{' + PAINT[key] + ':' + colour(key) + '!important}');
92
+ return out;
93
+ })
94
+ .join('\n');
95
+ }
96
+ export function unmark() {
97
+ for (const el of state.marked) {
98
+ el.removeAttribute(MARK);
99
+ el.removeAttribute(LAID);
100
+ el.removeAttribute(LAPSED);
101
+ }
102
+ state.marked = [];
103
+ }
104
+ /** The entry that marks an element. */
105
+ export function marks(el) {
106
+ if (!state.marked.includes(el))
107
+ return undefined;
108
+ const i = el.getAttribute(MARK);
109
+ return i !== null && /^\d+$/.test(i) ? state.list[Number(i)] : undefined;
110
+ }
111
+ export const lapsedOn = (el) => (el.getAttribute(LAPSED) ?? '').split(' ').filter(Boolean);
112
+ export function toldAt(e, lo) {
113
+ const told = e.told.get(lo);
114
+ return told ? told : null;
115
+ }
116
+ /** Of an entry of the scene nothing could be told in this load: which element is its own is not known, so no look is told of any. */
117
+ export const blind = (scene, lo) => state.list.some((e) => e.scene === scene && e.told.get(lo) === null);
118
+ /** An entry's own moment lies in the stretch: a scene's always does, since it counts from the scene's start. */
119
+ export const home = (e, lo, hi) => e.scene !== 'film' || (e.at >= lo && e.at < hi);
120
+ /**
121
+ * One found otherwise keeps an element from one found by its parts, and one found by its print from one found by its
122
+ * turn; else the later in the record, changed last, takes it.
123
+ */
124
+ export function takes(e, w, lo) {
125
+ const a = toldAt(e, lo)?.seat;
126
+ const b = toldAt(w, lo)?.seat;
127
+ if (!a?.held !== !b?.held)
128
+ return !a?.held;
129
+ return !!a?.sure || !b?.sure;
130
+ }
131
+ /** Of entries seated on one node one is laid; the others, which apply passes over and lost names. */
132
+ export function beaten(scene, lo) {
133
+ const by = new Map();
134
+ const out = [];
135
+ for (const e of state.list) {
136
+ const told = e.scene === scene ? toldAt(e, lo) : null;
137
+ if (!told)
138
+ continue;
139
+ const w = by.get(told.seat.el);
140
+ if (w && !takes(e, w, lo)) {
141
+ out.push(e);
142
+ continue;
143
+ }
144
+ if (w)
145
+ out.push(w);
146
+ by.set(told.seat.el, e);
147
+ }
148
+ return out;
149
+ }
150
+ /** All of an entry that a verdict on it rests on: not `v`. */
151
+ export function basis(e) {
152
+ return JSON.stringify([e.id, e.cls, e.kin, e.parts, e.at, ...KEYS.map((key) => e.pairs[key]?.was ?? null)]);
153
+ }
@@ -0,0 +1,19 @@
1
+ import type { TypeLook } from '../../protocol.js';
2
+ export declare const TYPE_KEYS: readonly ["weight", "italic", "px", "leading", "tracking", "align"];
3
+ export type TypeKey = (typeof TYPE_KEYS)[number];
4
+ /** The CSS property each is read off. */
5
+ export declare const TYPE_PROPS: Record<TypeKey, readonly string[]>;
6
+ /** The head of its font stack; '': none that is a plain name. */
7
+ export declare function familyHead(cs: CSSStyleDeclaration): string;
8
+ /** How the film sets the words now; null for what the record cannot hold. */
9
+ export declare function typeSeen(cs: CSSStyleDeclaration): TypeLook;
10
+ /**
11
+ * What a change would not show on is not offered. For offering alone: a pair that was saved goes on counting by what
12
+ * the film gives, though the line may come to fill its box at some moment.
13
+ */
14
+ export declare function offerType(cs: CSSStyleDeclaration, el: Element, now: TypeLook): void;
15
+ /** What the film itself changes in the course of a stretch is not offered: compared look to look. */
16
+ export declare function typeHues(cs: CSSStyleDeclaration): Record<TypeKey, string>;
17
+ export declare function sameType(key: TypeKey, now: string | number | null, was: string | number): boolean;
18
+ /** The declaration that lays value `v`. The record was parsed by the core, so `v` is within its bounds. */
19
+ export declare function typeRule(key: TypeKey, v: string | number): string;
@@ -0,0 +1,111 @@
1
+ import { TEXT_ALIGNS, TEXT_STYLE_BOUNDS } from '@openartifactsai/motion-core';
2
+ /*
3
+ * The type of a line of words as a person sets it by hand: read off an element, held to a saved pair and written into
4
+ * the retouch sheet. A typeface picked for one line is not laid on this stage.
5
+ */
6
+ export const TYPE_KEYS = ['weight', 'italic', 'px', 'leading', 'tracking', 'align'];
7
+ /** The CSS property each is read off. */
8
+ export const TYPE_PROPS = {
9
+ weight: ['font-weight'],
10
+ italic: ['font-style'],
11
+ px: ['font-size'],
12
+ leading: ['line-height'],
13
+ tracking: ['letter-spacing'],
14
+ align: ['text-align'],
15
+ };
16
+ /** How close two readings must be to count as the same: engines round differently. */
17
+ const NEAR = { weight: 0.5, px: 0.05, leading: 0.005, tracking: 0.002 };
18
+ const within = (bounds) => (value) => typeof value === 'number' && Number.isFinite(value) && value >= bounds[0] && value <= bounds[1];
19
+ const isAlign = (value) => TEXT_ALIGNS.some((align) => align === value);
20
+ const round = (n, to) => Math.round(n * to) / to;
21
+ const spacing = (cs) => (cs.letterSpacing === 'normal' ? 0 : round(parseFloat(cs.letterSpacing) / parseFloat(cs.fontSize), 1000));
22
+ const leadingOf = (cs) => (cs.lineHeight === 'normal' ? 'normal' : round(parseFloat(cs.lineHeight) / parseFloat(cs.fontSize), 1000));
23
+ /** The head of its font stack; '': none that is a plain name. */
24
+ export function familyHead(cs) {
25
+ const head = (cs.fontFamily.split(',')[0] ?? '').trim().replace(/^["']|["']$/g, '');
26
+ return /^[A-Za-z0-9 _-]{1,64}$/.test(head) ? head : '';
27
+ }
28
+ /** How the film sets the words now; null for what the record cannot hold. */
29
+ export function typeSeen(cs) {
30
+ const weight = cs.fontWeight === 'bold' ? 700 : cs.fontWeight === 'normal' ? 400 : parseFloat(cs.fontWeight);
31
+ const said = cs.textAlign.replace(/^-webkit-/, '');
32
+ const rtl = cs.direction === 'rtl';
33
+ const align = said === 'start' ? (rtl ? 'right' : 'left') : said === 'end' ? (rtl ? 'left' : 'right') : said;
34
+ const leading = leadingOf(cs);
35
+ const px = round(parseFloat(cs.fontSize), 100);
36
+ const tracking = spacing(cs);
37
+ return {
38
+ weight: within(TEXT_STYLE_BOUNDS.weight.was)(weight) ? weight : null,
39
+ italic: /^(italic|oblique)/.test(cs.fontStyle) ? 1 : 0,
40
+ px: within(TEXT_STYLE_BOUNDS.px.was)(px) ? px : null,
41
+ leading: leading === 'normal' || within(TEXT_STYLE_BOUNDS.leading.was)(leading) ? leading : null,
42
+ tracking: within(TEXT_STYLE_BOUNDS.tracking.was)(tracking) ? tracking : null,
43
+ align: isAlign(align) ? align : null,
44
+ };
45
+ }
46
+ /** Whether another text-align would move anything: the box lays out lines of its own, and has more than one, or one shorter than it is wide. */
47
+ function loose(cs, el) {
48
+ if (!/^(block|inline-block|list-item|table-cell|flow-root)$/.test(cs.display) || !(el instanceof HTMLElement))
49
+ return false;
50
+ const range = document.createRange();
51
+ const tops = [];
52
+ range.selectNodeContents(el);
53
+ for (const rect of Array.from(range.getClientRects()))
54
+ if (rect.width > 0 && tops.every((top) => Math.abs(top - rect.top) > rect.height / 2))
55
+ tops.push(rect.top);
56
+ const inner = el.offsetWidth > 0 ? (el.getBoundingClientRect().width * (el.clientWidth - parseFloat(cs.paddingLeft) - parseFloat(cs.paddingRight))) / el.offsetWidth : 0;
57
+ return tops.length > 1 || range.getBoundingClientRect().width < inner * 0.98;
58
+ }
59
+ /**
60
+ * What a change would not show on is not offered. For offering alone: a pair that was saved goes on counting by what
61
+ * the film gives, though the line may come to fill its box at some moment.
62
+ */
63
+ export function offerType(cs, el, now) {
64
+ // Words in an svg have no line box: neither text-align nor line-height moves them.
65
+ const art = el.namespaceURI === 'http://www.w3.org/2000/svg';
66
+ if (art || !loose(cs, el))
67
+ now.align = null;
68
+ if (art)
69
+ now.leading = null;
70
+ // An svg's units may put the size outside what a person may give, and the Size field could not return to it.
71
+ if (art && now.px !== null && !within(TEXT_STYLE_BOUNDS.px.v)(now.px))
72
+ now.px = null;
73
+ }
74
+ /** What the film itself changes in the course of a stretch is not offered: compared look to look. */
75
+ export function typeHues(cs) {
76
+ return {
77
+ weight: cs.fontWeight,
78
+ italic: cs.fontStyle,
79
+ px: cs.fontSize,
80
+ leading: String(leadingOf(cs)),
81
+ tracking: String(spacing(cs)),
82
+ align: cs.textAlign,
83
+ };
84
+ }
85
+ export function sameType(key, now, was) {
86
+ const near = NEAR[key];
87
+ if (now === null)
88
+ return false;
89
+ return near === undefined || typeof now !== 'number' || typeof was !== 'number' ? now === was : Math.abs(now - was) <= near;
90
+ }
91
+ /** The declaration that lays value `v`. The record was parsed by the core, so `v` is within its bounds. */
92
+ export function typeRule(key, v) {
93
+ switch (key) {
94
+ case 'weight':
95
+ return 'font-weight:' + Number(v) + '!important';
96
+ case 'italic':
97
+ return 'font-style:' + (v ? 'italic' : 'normal') + '!important';
98
+ case 'px':
99
+ return 'font-size:' + Number(v) + 'px!important';
100
+ case 'leading':
101
+ return 'line-height:' + Number(v) + '!important';
102
+ case 'tracking':
103
+ return 'letter-spacing:' + Number(v) + 'em!important';
104
+ case 'align':
105
+ return 'text-align:' + (typeof v === 'string' && isAlign(v) ? v : 'left') + '!important';
106
+ default: {
107
+ const never = key;
108
+ return never;
109
+ }
110
+ }
111
+ }
@@ -0,0 +1,2 @@
1
+ /** What on the stage an exported frame cannot show, said once each; and focus a film asked for, taken back. */
2
+ export declare function scanStage(): void;
@@ -0,0 +1,49 @@
1
+ import { stageParts } from "./stage.js";
2
+ const told = new Set();
3
+ const CSS_URL = /url\(\s*(?:"([^"\\]*(?:\\[\s\S][^"\\]*)*)|'([^'\\]*(?:\\[\s\S][^'\\]*)*)|([^'")\s]*))/g;
4
+ function once(key, message) {
5
+ if (told.has(key))
6
+ return;
7
+ told.add(key);
8
+ console.error('raster-safety: ' + message);
9
+ }
10
+ const isOutside = (url) => url !== '' && !url.startsWith('data:') && !url.startsWith('#');
11
+ /** What on the stage an exported frame cannot show, said once each; and focus a film asked for, taken back. */
12
+ export function scanStage() {
13
+ const parts = stageParts();
14
+ if (!parts)
15
+ return;
16
+ const { stage, fontStyle } = parts;
17
+ for (const image of stage.querySelectorAll('img')) {
18
+ const source = image.getAttribute('src') ?? '';
19
+ if (source && !source.startsWith('data:'))
20
+ once('img:' + source.slice(0, 40), '<img src> must be a data: URL (kit.assets.url): "' + source.slice(0, 60) + '" will not appear in exported frames');
21
+ }
22
+ if (stage.querySelector('iframe'))
23
+ once('iframe', '<iframe> inside the stage cannot be exported');
24
+ if (stage.querySelector('link[rel=stylesheet]'))
25
+ once('link', 'external stylesheets are blocked; put CSS in a module and kit.css() it');
26
+ for (const focused of stage.querySelectorAll('[autofocus]'))
27
+ focused.removeAttribute('autofocus');
28
+ for (const node of stage.querySelectorAll('*')) {
29
+ if (node.scrollTop || node.scrollLeft) {
30
+ once('scroll', 'an element is scrolled (scrollTop/scrollLeft); exported frames ignore scroll offsets, so animate a translateY instead');
31
+ break;
32
+ }
33
+ }
34
+ const texts = [];
35
+ for (const styled of stage.querySelectorAll('[style*="url("]'))
36
+ texts.push(styled.getAttribute('style') ?? '');
37
+ for (const sheet of stage.querySelectorAll('style'))
38
+ if (sheet !== fontStyle)
39
+ texts.push(sheet.textContent ?? '');
40
+ for (const text of texts) {
41
+ for (const match of text.matchAll(CSS_URL)) {
42
+ const url = (match[1] ?? match[2] ?? match[3] ?? '').trim();
43
+ if (isOutside(url)) {
44
+ once('cssurl', 'CSS uses url(' + url.slice(0, 40) + ') that is not a data: URL; it will not load here (inline it as an asset data: URL or drop the rule)');
45
+ return;
46
+ }
47
+ }
48
+ }
49
+ }
@@ -0,0 +1,12 @@
1
+ export declare const LIGHT_INK = "rgba(255,255,255,.72)";
2
+ export declare const DARK_INK = "rgba(0,0,0,.64)";
3
+ export type CardInk = typeof LIGHT_INK | typeof DARK_INK;
4
+ type Rgb = [number, number, number];
5
+ /** The ground's red, green and blue, if it is an opaque colour the canvas reads (not a gradient, not var()). */
6
+ export declare function groundRgb(context: CanvasRenderingContext2D, ground: string): Rgb | null;
7
+ export declare const greyOf: (rgb: Rgb) => number;
8
+ /** The ink for a card on `rgb`, which is the card's ground as shown (greyed already where it is). */
9
+ export declare function cardInk(rgb: Rgb): CardInk;
10
+ /** The card ink on whatever the canvas makes of the ground: an unreadable ground counts as its fallback. */
11
+ export declare function inkOn(context: CanvasRenderingContext2D, ground: string, greyed: boolean): CardInk;
12
+ export {};