@open-artifact/motion-stage 0.0.0-stage → 0.12.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 (173) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +69 -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 +155 -0
  21. package/src/protocol/guards.d.ts +6 -0
  22. package/src/protocol/guards.js +208 -0
  23. package/src/protocol/layer-guards.d.ts +7 -0
  24. package/src/protocol/layer-guards.js +97 -0
  25. package/src/protocol/layers.d.ts +123 -0
  26. package/src/protocol/layers.js +1 -0
  27. package/src/protocol/types.d.ts +261 -0
  28. package/src/protocol/types.js +7 -0
  29. package/src/protocol.d.ts +2 -0
  30. package/src/protocol.js +2 -0
  31. package/src/raster.d.ts +4 -0
  32. package/src/raster.js +2 -0
  33. package/src/runtime/assets.d.ts +19 -0
  34. package/src/runtime/assets.js +63 -0
  35. package/src/runtime/captions.d.ts +26 -0
  36. package/src/runtime/captions.js +103 -0
  37. package/src/runtime/console.d.ts +13 -0
  38. package/src/runtime/console.js +99 -0
  39. package/src/runtime/cut.d.ts +7 -0
  40. package/src/runtime/cut.js +63 -0
  41. package/src/runtime/determinism.d.ts +12 -0
  42. package/src/runtime/determinism.js +47 -0
  43. package/src/runtime/film.d.ts +40 -0
  44. package/src/runtime/film.js +29 -0
  45. package/src/runtime/focus.d.ts +8 -0
  46. package/src/runtime/focus.js +71 -0
  47. package/src/runtime/fonts.d.ts +22 -0
  48. package/src/runtime/fonts.js +109 -0
  49. package/src/runtime/frame.d.ts +9 -0
  50. package/src/runtime/frame.js +123 -0
  51. package/src/runtime/kit/camera.d.ts +40 -0
  52. package/src/runtime/kit/camera.js +56 -0
  53. package/src/runtime/kit/chip.d.ts +25 -0
  54. package/src/runtime/kit/chip.js +75 -0
  55. package/src/runtime/kit/cursor.d.ts +46 -0
  56. package/src/runtime/kit/cursor.js +108 -0
  57. package/src/runtime/kit/dom.d.ts +14 -0
  58. package/src/runtime/kit/dom.js +46 -0
  59. package/src/runtime/kit/geometry.d.ts +22 -0
  60. package/src/runtime/kit/geometry.js +46 -0
  61. package/src/runtime/kit/index.d.ts +95 -0
  62. package/src/runtime/kit/index.js +149 -0
  63. package/src/runtime/kit/mark-strokes.d.ts +65 -0
  64. package/src/runtime/kit/mark-strokes.js +226 -0
  65. package/src/runtime/kit/marks.d.ts +21 -0
  66. package/src/runtime/kit/marks.js +165 -0
  67. package/src/runtime/kit/math.d.ts +23 -0
  68. package/src/runtime/kit/math.js +35 -0
  69. package/src/runtime/kit/paper.d.ts +7 -0
  70. package/src/runtime/kit/paper.js +27 -0
  71. package/src/runtime/kit/random.d.ts +8 -0
  72. package/src/runtime/kit/random.js +32 -0
  73. package/src/runtime/kit/text-fx.d.ts +18 -0
  74. package/src/runtime/kit/text-fx.js +55 -0
  75. package/src/runtime/kit/typing.d.ts +21 -0
  76. package/src/runtime/kit/typing.js +42 -0
  77. package/src/runtime/layers/describe.d.ts +41 -0
  78. package/src/runtime/layers/describe.js +327 -0
  79. package/src/runtime/layers/item.d.ts +121 -0
  80. package/src/runtime/layers/item.js +149 -0
  81. package/src/runtime/layers/parts.d.ts +50 -0
  82. package/src/runtime/layers/parts.js +155 -0
  83. package/src/runtime/layers/rows.d.ts +4 -0
  84. package/src/runtime/layers/rows.js +233 -0
  85. package/src/runtime/layers/scan.d.ts +24 -0
  86. package/src/runtime/layers/scan.js +202 -0
  87. package/src/runtime/layers/walk.d.ts +16 -0
  88. package/src/runtime/layers/walk.js +333 -0
  89. package/src/runtime/load.d.ts +10 -0
  90. package/src/runtime/load.js +228 -0
  91. package/src/runtime/main.d.ts +1 -0
  92. package/src/runtime/main.js +69 -0
  93. package/src/runtime/modules.d.ts +23 -0
  94. package/src/runtime/modules.js +53 -0
  95. package/src/runtime/port.d.ts +8 -0
  96. package/src/runtime/port.js +46 -0
  97. package/src/runtime/questions.d.ts +24 -0
  98. package/src/runtime/questions.js +162 -0
  99. package/src/runtime/recut.d.ts +35 -0
  100. package/src/runtime/recut.js +158 -0
  101. package/src/runtime/render.d.ts +80 -0
  102. package/src/runtime/render.js +342 -0
  103. package/src/runtime/retouch/ask.d.ts +17 -0
  104. package/src/runtime/retouch/ask.js +156 -0
  105. package/src/runtime/retouch/entry.d.ts +77 -0
  106. package/src/runtime/retouch/entry.js +1 -0
  107. package/src/runtime/retouch/judge.d.ts +25 -0
  108. package/src/runtime/retouch/judge.js +209 -0
  109. package/src/runtime/retouch/look.d.ts +39 -0
  110. package/src/runtime/retouch/look.js +135 -0
  111. package/src/runtime/retouch/parts.d.ts +22 -0
  112. package/src/runtime/retouch/parts.js +166 -0
  113. package/src/runtime/retouch/retouch.d.ts +37 -0
  114. package/src/runtime/retouch/retouch.js +238 -0
  115. package/src/runtime/retouch/seat.d.ts +40 -0
  116. package/src/runtime/retouch/seat.js +228 -0
  117. package/src/runtime/retouch/state.d.ts +37 -0
  118. package/src/runtime/retouch/state.js +153 -0
  119. package/src/runtime/retouch/type-style.d.ts +19 -0
  120. package/src/runtime/retouch/type-style.js +111 -0
  121. package/src/runtime/scan.d.ts +2 -0
  122. package/src/runtime/scan.js +49 -0
  123. package/src/runtime/scenes/card.d.ts +12 -0
  124. package/src/runtime/scenes/card.js +56 -0
  125. package/src/runtime/scenes/install.d.ts +47 -0
  126. package/src/runtime/scenes/install.js +387 -0
  127. package/src/runtime/scenes/read.d.ts +15 -0
  128. package/src/runtime/scenes/read.js +57 -0
  129. package/src/runtime/scenes/scoped.d.ts +13 -0
  130. package/src/runtime/scenes/scoped.js +40 -0
  131. package/src/runtime/score.d.ts +26 -0
  132. package/src/runtime/score.js +221 -0
  133. package/src/runtime/sketch/pen.d.ts +3 -0
  134. package/src/runtime/sketch/pen.js +371 -0
  135. package/src/runtime/sketch/strokes.d.ts +41 -0
  136. package/src/runtime/sketch/strokes.js +187 -0
  137. package/src/runtime/snapshot/draw.d.ts +14 -0
  138. package/src/runtime/snapshot/draw.js +125 -0
  139. package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
  140. package/src/runtime/snapshot/hold-first-draw.js +128 -0
  141. package/src/runtime/snapshot/plate.d.ts +20 -0
  142. package/src/runtime/snapshot/plate.js +79 -0
  143. package/src/runtime/snapshot/serialize.d.ts +49 -0
  144. package/src/runtime/snapshot/serialize.js +271 -0
  145. package/src/runtime/snapshot/styles.d.ts +8 -0
  146. package/src/runtime/snapshot/styles.js +109 -0
  147. package/src/runtime/snapshot/xml.d.ts +6 -0
  148. package/src/runtime/snapshot/xml.js +110 -0
  149. package/src/runtime/stage-size.d.ts +7 -0
  150. package/src/runtime/stage-size.js +3 -0
  151. package/src/runtime/stage.d.ts +27 -0
  152. package/src/runtime/stage.js +154 -0
  153. package/src/runtime/stretch.d.ts +13 -0
  154. package/src/runtime/stretch.js +16 -0
  155. package/src/runtime/texts.d.ts +24 -0
  156. package/src/runtime/texts.js +210 -0
  157. package/src/runtime/video-sync.d.ts +26 -0
  158. package/src/runtime/video-sync.js +126 -0
  159. package/src/stills.d.ts +41 -0
  160. package/src/stills.js +253 -0
  161. package/src/tools/film.d.ts +13 -0
  162. package/src/tools/film.js +116 -0
  163. package/src/tools/main.d.ts +1 -0
  164. package/src/tools/main.js +216 -0
  165. package/src/tools/score.d.ts +4 -0
  166. package/src/tools/score.js +29 -0
  167. package/src/tools/types.d.ts +69 -0
  168. package/src/tools/types.js +2 -0
  169. package/src/tools.d.ts +5 -0
  170. package/src/tools.js +3 -0
  171. package/src/virtual.d.ts +11 -0
  172. package/src/vite.d.ts +8 -0
  173. package/src/vite.js +43 -0
@@ -0,0 +1,155 @@
1
+ /*
2
+ * The parts of a thing on the picture that both the reading of the layers and a person's changes by hand go by.
3
+ * A thing is one element of the film's; inside an inline svg, a part of the drawing.
4
+ */
5
+ export const SKIP = /^(style|script|template|link|meta|title)$/i;
6
+ export const PHRASE = /^(span|em|b|i|strong|u|a|br|small|sub|sup|mark|s|code|wbr|tspan)$/i;
7
+ export const UNIT = /^(img|svg|canvas|video|picture)$/i;
8
+ /** A g holds parts, each other tag is one part. Whatever else stands in an svg (defs, a mask, a nested svg) is passed over. */
9
+ export const ART = /^(g|path|rect|circle|ellipse|line|polyline|polygon|text|use|image|foreignObject)$/i;
10
+ /** The parts that paint themselves. */
11
+ export const SHAPE = /^(path|rect|circle|ellipse|line|polyline|polygon)$/i;
12
+ /** The two colours of a shape a person can change, each with the property it is laid as. */
13
+ export const PAINT = { svgfill: 'fill', svgstroke: 'stroke' };
14
+ export const PAINT_KEYS = ['svgfill', 'svgstroke'];
15
+ const SVG_NS = 'http://www.w3.org/2000/svg';
16
+ const SIDES = ['top', 'right', 'bottom', 'left'];
17
+ /** The element's own inline style, where it has one. */
18
+ export function styleOf(el) {
19
+ return el instanceof HTMLElement || el instanceof SVGElement ? el.style : null;
20
+ }
21
+ /** A rough scene's pen strokes, which are no thing of the film's. */
22
+ export const isPen = (node) => node instanceof Element && node.hasAttribute('data-sketch-pen');
23
+ /**
24
+ * The film's own elements under `el`: a thing is known by its place among them. A caption is passed over: the stage's
25
+ * own goes to the end only after a frame is drawn, so it can stand ahead of what the film added meanwhile.
26
+ */
27
+ export function kidsOf(el) {
28
+ const out = [];
29
+ if (!(el instanceof Element))
30
+ return out;
31
+ for (let child = el.firstElementChild; child; child = child.nextElementSibling)
32
+ if (!isPen(child) && !SKIP.test(child.tagName) && !child.classList.contains('film-caption'))
33
+ out.push(child);
34
+ return out;
35
+ }
36
+ export function alphaOf(colour) {
37
+ if (!colour || colour === 'transparent')
38
+ return 0;
39
+ const rgba = /^rgba\((.+)\)$/.exec(colour);
40
+ const slash = colour.indexOf('/');
41
+ return rgba ? parseFloat(rgba[1].split(',')[3] ?? '1') : slash > 0 ? parseFloat(colour.slice(slash + 1)) : 1;
42
+ }
43
+ /** It paints a box: a background, a shadow or a border that shows. Read inside `inOwnColors`. */
44
+ export function paints(cs, el) {
45
+ if (/\b(edge|fill)\b/.test(el.getAttribute('data-sketch') ?? ''))
46
+ return true;
47
+ const set = (value) => !!value && value !== 'none';
48
+ const inWords = cs.backgroundClip === 'text' || cs.getPropertyValue('-webkit-background-clip') === 'text';
49
+ if (set(cs.boxShadow) || (!inWords && (set(cs.backgroundImage) || alphaOf(cs.backgroundColor) > 0.03)))
50
+ return true;
51
+ return SIDES.some((side) => parseFloat(cs.getPropertyValue(`border-${side}-width`)) > 0 &&
52
+ cs.getPropertyValue(`border-${side}-style`) !== 'none' &&
53
+ alphaOf(cs.getPropertyValue(`border-${side}-color`)) > 0.03);
54
+ }
55
+ /** A part of an svg drawing. The kit's hand-drawn marks are none: their order and number differ from runner to runner. */
56
+ export const isPart = (el) => el instanceof Element && el.namespaceURI === SVG_NS && ART.test(el.tagName) && !el.hasAttribute('data-film-mark');
57
+ export const isArtWords = (el) => isPart(el) && /^text$/i.test(el.tagName);
58
+ /**
59
+ * What an element's own attributes say of how it is drawn: tag, id, classes, link and paint, not its shape or place,
60
+ * which most films change from frame to frame. `blur`: numbers in its id and classes count for nothing.
61
+ */
62
+ export function saidOf(el, blur = false) {
63
+ const own = el.id + ' ' + (el.getAttribute('class') ?? '');
64
+ const style = styleOf(el);
65
+ return [el.tagName, blur ? own.replace(/\d+/g, '#') : own]
66
+ .concat(['href', 'fill', 'stroke', 'stroke-width', 'color'].map((name) => el.getAttribute(name) ?? ''))
67
+ .concat(style ? [style.fill, style.stroke, style.color] : [])
68
+ .join(' ');
69
+ }
70
+ /** The same of a part and all the parts under it. `memo` lasts while the stage stands as drawn. */
71
+ export function drawnAs(el, blur, memo) {
72
+ const known = memo.get(el);
73
+ if (known !== undefined)
74
+ return known;
75
+ const out = saidOf(el, blur) +
76
+ kidsOf(el)
77
+ .filter(isPart)
78
+ .map((child) => '(' + drawnAs(child, blur, memo) + ')')
79
+ .join('');
80
+ memo.set(el, out);
81
+ return out;
82
+ }
83
+ /** A paint shows at all: a colour or a pattern, not see-through. */
84
+ export function showsPaint(paint, opacity) {
85
+ const o = parseFloat(opacity);
86
+ return !!paint && paint !== 'none' && (/^url\(/.test(paint) ? 1 : alphaOf(paint)) * (isNaN(o) ? 1 : o) > 0.03;
87
+ }
88
+ /** A filled shape, or a use of one: only such a part can be the Background. */
89
+ export function fills(cs, el) {
90
+ if (/^use$/i.test(el.tagName)) {
91
+ const id = (el.getAttribute('href') ?? el.getAttribute('xlink:href') ?? '').slice(1);
92
+ const to = id ? el.ownerDocument.getElementById(id) : null;
93
+ if (to)
94
+ return !/^use$/i.test(to.tagName) && fills(getComputedStyle(to), to);
95
+ }
96
+ return !/^(g|use|image|foreignObject|line)$/i.test(el.tagName) && showsPaint(cs.fill, cs.fillOpacity);
97
+ }
98
+ /** A use, an image, a foreignObject and a g read as one thing always count: what they show is not theirs to paint. */
99
+ export function inks(cs, el) {
100
+ return /^(g|use|image|foreignObject)$/i.test(el.tagName) || fills(cs, el) || (parseFloat(cs.strokeWidth) > 0 && showsPaint(cs.stroke, cs.strokeOpacity));
101
+ }
102
+ export function ownText(el) {
103
+ for (let node = el.firstChild; node; node = node.nextSibling)
104
+ if (node instanceof Text && /\S/.test(node.data))
105
+ return true;
106
+ return false;
107
+ }
108
+ /** A line of words: text, and nothing under it but spans and the like. Spans that paint with no text beside them are things of their own. */
109
+ export function isWords(el) {
110
+ // kit.h's holder has no box, so what is in it is read for itself.
111
+ if (styleOf(el)?.display === 'contents')
112
+ return false;
113
+ const all = el.getElementsByTagName('*');
114
+ // The tag first: it settles nearly every case, and a walk up to look for a pen is dear.
115
+ for (const each of Array.from(all))
116
+ if (!PHRASE.test(each.tagName) && !each.closest('[data-sketch-pen]'))
117
+ return false;
118
+ if (!/\S/.test(el.textContent ?? ''))
119
+ return false;
120
+ if (ownText(el))
121
+ return true;
122
+ for (const each of Array.from(all))
123
+ if (PHRASE.test(each.tagName) && paints(getComputedStyle(each), each))
124
+ return false;
125
+ return true;
126
+ }
127
+ /** A declared value that uses a var(), with each fallback taken out that uses none itself; null: it uses none, or a var() is not closed. */
128
+ export function drawnValue(text) {
129
+ if (!/var\(/i.test(text))
130
+ return null;
131
+ let out = '';
132
+ let any = false;
133
+ for (let i = 0; i < text.length;) {
134
+ const found = /[\w-]/.test(text[i - 1] ?? '') ? null : /^var\(\s*(--[^\s,)]+)\s*/i.exec(text.slice(i));
135
+ if (!found) {
136
+ out += text[i++];
137
+ continue;
138
+ }
139
+ let deep = 1;
140
+ let j = i + 4;
141
+ for (; j < text.length && deep; j++)
142
+ deep += text[j] === '(' ? 1 : text[j] === ')' ? -1 : 0;
143
+ if (deep)
144
+ return null;
145
+ const rest = text.slice(i + found[0].length, j - 1);
146
+ const back = rest[0] === ',' ? drawnValue(rest.slice(1).trim()) : null;
147
+ out += 'var(' + found[1] + (back ? ', ' + back : '') + ')';
148
+ i = j;
149
+ any = true;
150
+ }
151
+ return any ? out : null;
152
+ }
153
+ /** What of a person's change is laid on the element (the retouch marks). */
154
+ export const laidOn = (el, what) => (el.getAttribute('data-film-laid') ?? '').split(' ').includes(what);
155
+ export const hiddenByHand = (el) => laidOn(el, 'hidden');
@@ -0,0 +1,4 @@
1
+ import type { Stretch } from './describe.ts';
2
+ import type { Item, Layer } from './item.ts';
3
+ /** The rows of a reading, in the order they come on, the ground last, before the cut of what each shows. */
4
+ export declare function buildRows(s: Stretch, els: Map<string, Item>): Layer[];
@@ -0,0 +1,233 @@
1
+ import { beneath, inOrder, lead, measures, stays } from "./describe.js";
2
+ import { MOST, pinned } from "./item.js";
3
+ import { SHAPE, drawnAs } from "./parts.js";
4
+ import { markOdd } from "./walk.js";
5
+ const ALIKE = 4;
6
+ const BRIEF_S = 0.15;
7
+ const MOST_KIDS = 12;
8
+ /** The rows of a reading, in the order they come on, the ground last, before the cut of what each shows. */
9
+ export function buildRows(s, els) {
10
+ const { W, H, from, to } = s;
11
+ // Inside an svg the rows follow the drawing's own tree.
12
+ const below = new Map();
13
+ const all = new Map();
14
+ for (const it of els.values())
15
+ if (it.art)
16
+ below.set(it.up, (below.get(it.up) ?? []).concat(it));
17
+ /** All that is drawn of it: a g that was shut in the course of the reading counts with the parts read before that. */
18
+ const inked = (it) => {
19
+ let got = all.get(it);
20
+ if (!got) {
21
+ got = (below.get(it) ?? []).reduce((some, c) => some.concat(inked(c)), it.cand && it.shown ? [it] : []).sort(inOrder);
22
+ all.set(it, got);
23
+ }
24
+ return got;
25
+ };
26
+ const { isFull, swelled, grown, describe } = measures(s, inked);
27
+ const heads = new Map();
28
+ const order = [];
29
+ for (const it of els.values()) {
30
+ if (!it.cand || it.shown === 0 || it.art)
31
+ continue;
32
+ /*
33
+ * It belongs to the nearest thing above it that moves, that the film named, that paints a box round it, or that a
34
+ * person changed or hid; unless it is such a thing itself. A thing as large as the stage looks for no holder, and is
35
+ * no holder, unless a hand-set Scale made it that large.
36
+ */
37
+ let head = it;
38
+ if ((!isFull(it) || grown(it)) && !it.moved && !it.named && !(it.easel && it.big && !beneath(it)))
39
+ for (let up = it.up; up; up = up.up)
40
+ if ((!isFull(up) || swelled(up) || grown(up)) && (up.moved || up.named || up.paint || stays(up))) {
41
+ head = up;
42
+ break;
43
+ }
44
+ let group = heads.get(head);
45
+ if (!group) {
46
+ group = [];
47
+ heads.set(head, group);
48
+ order.push(head);
49
+ }
50
+ group.push(it);
51
+ it.head = head;
52
+ }
53
+ order.sort(inOrder);
54
+ const lasting = (l) => l.long >= Math.min(BRIEF_S, (to - from) / 4);
55
+ let list = order
56
+ .map((head) => {
57
+ const parts = (heads.get(head) ?? []).sort(inOrder);
58
+ const l = describe(head, parts);
59
+ /*
60
+ * What it holds, without the box it paints round them. A full row lists up to MOST of those, and each of them lists
61
+ * what it holds in turn; a kid that holds things also has a row of its own under the same id.
62
+ */
63
+ const under = (p) => {
64
+ const k = describe(p, [p]);
65
+ const own = (heads.get(p) ?? []).filter((q) => q !== p);
66
+ if (own.length && !(own.length === 1 && own[0]?.easel))
67
+ k.kids = own
68
+ .sort(inOrder)
69
+ .map(under)
70
+ .filter(lasting)
71
+ .filter((c, i) => i < MOST || pinned(c));
72
+ return k;
73
+ };
74
+ if (parts.length > 1)
75
+ l.kids = parts
76
+ .filter((p) => p !== head)
77
+ .map(l.full ? under : (p) => describe(p, [p]))
78
+ .filter(lasting)
79
+ .filter((k, i) => i < (l.full ? MOST : MOST_KIDS) || (pinned(k) && (l.full || !(heads.get(k.it) ?? []).some((q) => q !== k.it))));
80
+ return l;
81
+ })
82
+ .filter(lasting);
83
+ // Four or more alike side by side (a grid's cells, a list's rows) are one layer, which holds them.
84
+ const drawnNow = new Map();
85
+ const alike = (l) => l.kind === 'ground'
86
+ ? null
87
+ : l.it.path.slice(0, -1).join('.') +
88
+ '|' +
89
+ l.kind +
90
+ '|' +
91
+ (l.it.el.getAttribute('class') ?? '').replace(/\d+/g, '#') +
92
+ '|' +
93
+ l.it.el.tagName +
94
+ (l.it.art ? '|' + (l.name.replace(/\d+/g, '#') || drawnAs(l.it.el, true, drawnNow)) : '');
95
+ const fold = (rows) => {
96
+ const folded = [];
97
+ const packs = new Map();
98
+ for (const l of rows) {
99
+ const k = alike(l);
100
+ if (k)
101
+ packs.set(k, (packs.get(k) ?? []).concat(l));
102
+ }
103
+ for (const l of rows) {
104
+ const k = alike(l);
105
+ const pack = k ? (packs.get(k) ?? [l]) : [l];
106
+ if (pack.length < ALIKE) {
107
+ folded.push(l);
108
+ continue;
109
+ }
110
+ if (pack[0] !== l)
111
+ continue;
112
+ let every = [];
113
+ for (const p of pack) {
114
+ every = every.concat(p.parts);
115
+ if (!p.it.art && !p.full)
116
+ p.kids = p.kids.filter(pinned);
117
+ }
118
+ const g = describe(l.it, every);
119
+ // "*": the pack, as against its first member, whose place and tag it is called by.
120
+ g.id += '*';
121
+ g.pack = true;
122
+ g.kind = 'group';
123
+ g.round = false;
124
+ g.drawn = '';
125
+ g.full = false;
126
+ g.name = pack.every((p) => p.name === l.name) ? l.name : '';
127
+ g.many = pack.length;
128
+ g.keep = pack.some(pinned);
129
+ g.at = pack[pack.length - 1]?.at ?? g.at;
130
+ g.kids = l.it.art ? pack : pack.filter((p, i) => i < MOST_KIDS || pinned(p));
131
+ folded.push(g);
132
+ }
133
+ return folded;
134
+ };
135
+ // A speck that is on at no look has no row; one that only begins so (a line that grows) has.
136
+ for (const it of els.values())
137
+ if (it.speck && !it.shown)
138
+ markOdd(it);
139
+ const heldBy = (it) => (below.get(it) ?? []).filter((c) => inked(c).length).sort(inOrder);
140
+ const groups = (c) => heldBy(c).some((x) => !x.cand);
141
+ /*
142
+ * A g that is no thing on the picture, like any wrapper: at some look as large as the stage; or over half of it and
143
+ * holding other groups (a camera, a world); or alone in its holder, if it holds groups itself or its holder has a row.
144
+ */
145
+ const wraps = (c, alone, bare) => !c.cand && !stays(c) && (c.big || (groups(c) && c.most >= (W * H) / 2) || (alone && (!bare || groups(c))));
146
+ /*
147
+ * A row takes what it holds, if that is more than one thing or must keep its row. A g that holds only things alike
148
+ * stands for them, as a pack does; an svg does not. sole: the one shape that is all a row holds, unless a person hid it.
149
+ */
150
+ const hold = (l, held) => {
151
+ let kids = held;
152
+ const only = kids[0];
153
+ if (l.it.art && kids.length === 1 && only?.pack) {
154
+ l.many = only.many;
155
+ kids = only.kids;
156
+ }
157
+ const one = kids[0];
158
+ if (kids.length === 1 && one && !one.pack && SHAPE.test(one.it.el.tagName) && !one.it.own)
159
+ l.sole = one;
160
+ else if (kids.length > 1 || kids.some((k) => k.pack || pinned(k)))
161
+ l.kids = kids;
162
+ return l;
163
+ };
164
+ const rowsIn = (up, bare) => {
165
+ const held = heldBy(up);
166
+ return held
167
+ .reduce((rows, c) => {
168
+ if (wraps(c, held.length === 1, bare))
169
+ return rows.concat(rowsIn(c, bare));
170
+ const l = describe(c, inked(c));
171
+ const only = heldBy(c);
172
+ const sole = only.length === 1 ? only[0] : undefined;
173
+ if (sole)
174
+ l.name = l.name || describe(sole, l.parts).name;
175
+ return rows.concat(hold(l, c.easel ? fold(rowsIn(c, false)) : []));
176
+ }, [])
177
+ .filter(lasting);
178
+ };
179
+ /*
180
+ * An svg as large as the stage is such a wrapper too, if what it holds fits the list beside the other rows: else its
181
+ * row stays and holds them ('drawing'). A smaller one keeps its row, which holds its parts.
182
+ */
183
+ const wide = (l) => !!l.it.easel && l.it.big && !stays(l.it);
184
+ let others = Math.min(MOST, fold(list.filter((l) => !wide(l))).length);
185
+ const down = (k) => k.kids.reduce((a, c) => a.concat(down(c)), [k]);
186
+ list = list.reduce((every, l) => {
187
+ let rows = wide(l) ? fold(rowsIn(l.it, true)) : [];
188
+ if (wide(l) && rows.length + others > 2 * MOST) {
189
+ hold(l, rows);
190
+ l.drawn = 'drawing';
191
+ rows = [];
192
+ others++;
193
+ }
194
+ others += rows.length;
195
+ // A box that paints round one svg has two rows under one id; each holds the svg's parts, which count once.
196
+ for (const k of down(l)) {
197
+ if (k.it.art)
198
+ continue;
199
+ const own = heads.get(k.it) ?? [];
200
+ const e = k.it.easel ? k.it : own.length === 1 && own[0]?.easel ? own[0] : null;
201
+ if (e && !k.kids.length && !rows.length) {
202
+ hold(k, fold(rowsIn(e, false)));
203
+ k.svg = e;
204
+ k.inked = inked(e).filter((p) => p !== e);
205
+ if (!k.parts.includes(e))
206
+ k.texts = k.texts
207
+ .concat(lead(k.inked.filter((p) => p.text).map((p) => p.text)))
208
+ .slice(0, MOST);
209
+ }
210
+ }
211
+ return every.concat(rows.length ? rows : l);
212
+ }, []);
213
+ const folded = fold(list);
214
+ const lvl = new Map();
215
+ // below: the level at which the box's row will stand once a change pins it under what holds it.
216
+ for (const p of folded) {
217
+ lvl.set(p.it, 1);
218
+ if (p.pack)
219
+ for (const m of p.kids)
220
+ lvl.set(m.it, 2);
221
+ }
222
+ for (const l of folded)
223
+ if (l.svg && l.it.head && l.it.head !== l.it)
224
+ l.below = (lvl.get(l.it.head) ?? 2) + 1;
225
+ // As they come on; the ground last.
226
+ folded.forEach((l, i) => {
227
+ l.n = i;
228
+ });
229
+ list = folded.sort((a, b) => Number(a.kind === 'ground') - Number(b.kind === 'ground') || (a.spans[0]?.[0] ?? 0) - (b.spans[0]?.[0] ?? 0) || a.n - b.n);
230
+ // MOST of them, what is pinned first, and beside those the rows of drawings, which were let in only as far as they fit.
231
+ let room = MOST - list.filter((l) => !l.it.art && pinned(l)).length;
232
+ return list.filter((l) => l.it.art || pinned(l) || room-- > 0);
233
+ }
@@ -0,0 +1,24 @@
1
+ import type { LayersReport } from '../../protocol.ts';
2
+ import type { LookSys } from './describe.ts';
3
+ export interface ScanSys extends LookSys {
4
+ stage: HTMLElement;
5
+ /** Draws a played moment at once; throws where it cannot. */
6
+ render(t: number): void;
7
+ /** The record's entries for the stretch on show that find nothing. */
8
+ lost(): string[];
9
+ /** Of those, the hides whose thing is hidden by another entry all the same. */
10
+ underHide(): string[];
11
+ }
12
+ export interface LayersAsk {
13
+ seq: number;
14
+ from: number;
15
+ to: number;
16
+ at: number;
17
+ scene: string | null;
18
+ }
19
+ export declare const forgetReading: () => void;
20
+ /**
21
+ * Reads the layers of a stretch: what is on the picture, when, how it comes and goes, and how the film draws it at the
22
+ * moment on show. `gone`: a newer question has come, or none is wanted any more; the reading then ends with null.
23
+ */
24
+ export declare function scanLayers(d: LayersAsk, sys: ScanSys, gone: () => boolean): Promise<LayersReport | null>;
@@ -0,0 +1,202 @@
1
+ import { srect } from "../kit/geometry.js";
2
+ import { inOwnColors } from "../snapshot/serialize.js";
3
+ import { stageSize } from "../stage-size.js";
4
+ import { lookOf } from "./describe.js";
5
+ import { MOST, breath, inTurn, pinned, union } from "./item.js";
6
+ import { SHAPE, fills, showsPaint } from "./parts.js";
7
+ import { buildRows } from "./rows.js";
8
+ import { lookAt } from "./walk.js";
9
+ const MOST_LOOKS = 96;
10
+ const FEWEST_LOOKS = 5;
11
+ const STEP_S = 1 / 15;
12
+ const SLICE_MS = 12;
13
+ const WHOLE_MS = 1500;
14
+ const LAST_MS = 4000;
15
+ const MOST_TOLD = 400;
16
+ const DEEP = 4;
17
+ /** The last reading, kept while the stretch asked of is the same: a new moment on show needs only a new look. */
18
+ let kept = null;
19
+ export const forgetReading = () => {
20
+ kept = null;
21
+ };
22
+ const boxOf = (r) => [r.x, r.y, r.w, r.h];
23
+ /** The box round an element's text alone: a Range over the element would also take in each child element's whole box. */
24
+ function wordsBox(el) {
25
+ const doc = el.ownerDocument;
26
+ const walker = doc.createTreeWalker(el, NodeFilter.SHOW_TEXT);
27
+ const span = doc.createRange();
28
+ let out = null;
29
+ for (let n = walker.nextNode(); n; n = walker.nextNode()) {
30
+ if (!(n instanceof Text) || !/\S/.test(n.data))
31
+ continue;
32
+ span.selectNodeContents(n);
33
+ const r = srect(span);
34
+ if (r.w > 0 && r.h > 0)
35
+ out = union(out, r);
36
+ }
37
+ return out;
38
+ }
39
+ /**
40
+ * Reads the layers of a stretch: what is on the picture, when, how it comes and goes, and how the film draws it at the
41
+ * moment on show. `gone`: a newer question has come, or none is wanted any more; the reading then ends with null.
42
+ */
43
+ export async function scanLayers(d, sys, gone) {
44
+ const stage = sys.stage;
45
+ const W = stageSize.width;
46
+ const H = stageSize.height;
47
+ const from = Math.max(0, d.from || 0);
48
+ const to = Math.max(from + 0.01, d.to || 0);
49
+ const stretch = d.scene || 'film';
50
+ if (srect(stage).w < 1)
51
+ throw new Error('the page has no layout');
52
+ const key = from + '|' + to;
53
+ let read = kept && kept.key === key ? kept : null;
54
+ if (!read) {
55
+ const reading = { stage, W, H, prefix: d.scene ? d.scene + '-' : null, els: new Map(), nodes: new WeakMap(), used: new Set() };
56
+ const all = Math.max(8, Math.min(MOST_LOOKS, Math.ceil((to - from) / STEP_S)));
57
+ const turn = inTurn(all);
58
+ const taken = [];
59
+ let spent = 0;
60
+ const moment = (i) => from + ((to - from - 1 / 240) * i) / (all - 1);
61
+ let s0 = performance.now();
62
+ for (let q = 0; q < turn.length; q++) {
63
+ // A film that is slow to draw is looked at less often.
64
+ if ((spent > WHOLE_MS && q >= FEWEST_LOOKS) || spent > LAST_MS)
65
+ break;
66
+ if (performance.now() - s0 > SLICE_MS) {
67
+ await breath();
68
+ if (gone())
69
+ return null;
70
+ s0 = performance.now();
71
+ }
72
+ const i = turn[q] ?? 0;
73
+ const l0 = performance.now();
74
+ lookAt(reading, sys.render, moment(i), i, false);
75
+ spent += performance.now() - l0;
76
+ taken.push(i);
77
+ }
78
+ taken.sort((a, b) => a - b);
79
+ const n = taken.length;
80
+ const times = taken.map(moment);
81
+ for (const it of reading.els.values()) {
82
+ const on = it.on;
83
+ const op = it.op;
84
+ const box = it.box;
85
+ it.on = taken.map((i) => on[i] ?? false);
86
+ it.op = taken.map((i) => op[i] ?? 0);
87
+ it.box = taken.map((i) => box[i] ?? null);
88
+ }
89
+ const s = { W, H, from, to, n, times, stretch, prefix: reading.prefix };
90
+ read = { key, list: buildRows(s, reading.els), reading, n };
91
+ // A reading that found nothing is not kept: the page may have been parked.
92
+ kept = read.list.length ? read : null;
93
+ }
94
+ const reading = read.reading;
95
+ const at = Math.max(from, Math.min(to - 1 / 240, d.at || 0));
96
+ const NOW = read.n;
97
+ for (const it of reading.els.values()) {
98
+ it.on[NOW] = false;
99
+ it.box[NOW] = null;
100
+ }
101
+ lookAt(reading, sys.render, at, NOW, true);
102
+ /*
103
+ * What each row shows of what it holds: DEEP levels, MOST to a row and MOST_TOLD rows in all, shared out level by
104
+ * level. A cut takes all that is pinned, then what is on the picture now, which a press can pick, and keeps the order.
105
+ */
106
+ const memo = {};
107
+ const shows = new Map();
108
+ const met = new Set();
109
+ let total = read.list.length;
110
+ const rank = (k) => (pinned(k) ? 0 : k.parts.some((p) => p.on[NOW]) ? 1 : 2);
111
+ for (let level = read.list, deep = 1; level.length && deep < DEEP; deep++) {
112
+ level = level.reduce((next, l) => {
113
+ const most = Math.min(MOST, Math.max(0, MOST_TOLD - total));
114
+ const pick = [0, 1, 2].reduce((some, r) => some.concat(l.kids.filter((k) => rank(k) === r)), []).filter((k, i) => i < most || pinned(k) || met.has(k.id));
115
+ const kids = l.kids.filter((k) => pick.includes(k));
116
+ shows.set(l, kids);
117
+ for (const k of kids)
118
+ if (!met.has(k.id)) {
119
+ met.add(k.id);
120
+ total++;
121
+ }
122
+ return next.concat(kids);
123
+ }, []);
124
+ }
125
+ const used = reading.used;
126
+ const rows = inOwnColors(() => {
127
+ const depth = (x) => x.kids.reduce((most, k) => Math.max(most, 1 + depth(k)), 0);
128
+ /*
129
+ * entire: all that is drawn in the svg is listed under the row, each a shape of which every paint that shows is
130
+ * offered, so a colour changed on the row leaves none of it behind. ever: a part off the picture now counts as on.
131
+ */
132
+ const entire = (l, ever) => {
133
+ const tips = [];
134
+ const tip = (x) => {
135
+ if (x.sole)
136
+ tips.push(x.sole);
137
+ else if (x.kids.length)
138
+ (shows.get(x) ?? []).forEach(tip);
139
+ else if (x !== l)
140
+ tips.push(x);
141
+ };
142
+ tip(l);
143
+ if (!tips.length || !l.svg || l.svg.odd || l.below + depth(l) > DEEP)
144
+ return false;
145
+ if (!l.inked.every((p) => tips.some((x) => x.it === p)))
146
+ return false;
147
+ return tips.every((x) => {
148
+ const el = x.it.el;
149
+ const cs = getComputedStyle(el);
150
+ const k = x.now ?? (ever ? lookOf(x, sys, memo, used) : null);
151
+ if (ever && !el.isConnected)
152
+ return true;
153
+ const stroked = parseFloat(cs.strokeWidth) > 0 && showsPaint(cs.stroke, cs.strokeOpacity);
154
+ return !!k && SHAPE.test(el.tagName) && (!fills(cs, el) || k.svgfill !== null) && (!stroked || k.svgstroke !== null);
155
+ });
156
+ };
157
+ const told = (l) => {
158
+ let b = null;
159
+ for (const p of l.parts)
160
+ if (p.on[NOW])
161
+ b = union(b, p.box[NOW] ?? null);
162
+ l.now = b ? lookOf(l, sys, memo, used) : null;
163
+ const only = l.parts[0];
164
+ const words = b && l.kind === 'text' && l.parts.length === 1 && only ? wordsBox(only.el) : null;
165
+ let kids = (shows.get(l) ?? []).map(told);
166
+ let sole = l.sole ? told(l.sole) : null;
167
+ const whole = !!l.svg && entire(l, false);
168
+ // A lone shape the row can never stand for has a row while a person's change is laid on it, the way to take that back.
169
+ if (sole && l.svg && !whole) {
170
+ if (l.sole && pinned(l.sole) && !entire(l, true))
171
+ kids = [sole];
172
+ sole = null;
173
+ }
174
+ const held = l.it.held;
175
+ return {
176
+ id: l.id,
177
+ kind: l.kind,
178
+ round: l.round,
179
+ name: l.name,
180
+ hidden: held ? 'held' : l.it.own && !l.pack ? 'own' : false,
181
+ under: held ? stretch + ':' + held.key : null,
182
+ own: l.it.own && !l.pack,
183
+ many: l.many,
184
+ texts: l.texts,
185
+ spans: l.spans.slice(0, 6).map(([a, z]) => [a, z]),
186
+ at: l.at,
187
+ enter: l.enter,
188
+ leave: l.leave,
189
+ box: b ? boxOf(b) : null,
190
+ wordsBox: words ? boxOf(words) : null,
191
+ look: l.now,
192
+ kids,
193
+ sole,
194
+ whole,
195
+ drawn: l.drawn === 'group' && (shows.get(l) ?? []).length < l.kids.length ? '' : l.drawn,
196
+ full: l.full,
197
+ };
198
+ };
199
+ return read.list.map(told);
200
+ });
201
+ return { seq: d.seq, from: from, to: to, at: at, lost: sys.lost(), underHide: sys.underHide(), rows };
202
+ }
@@ -0,0 +1,16 @@
1
+ import type { Item } from './item.ts';
2
+ export interface Reading {
3
+ stage: HTMLElement;
4
+ W: number;
5
+ H: number;
6
+ /** The class prefix by which the scene names its things. */
7
+ prefix: string | null;
8
+ els: Map<string, Item>;
9
+ nodes: WeakMap<Element, Item>;
10
+ /** The ids that a use on the stage points to at any look. */
11
+ used: Set<string>;
12
+ }
13
+ /** It holds something that may draw but has no row, so no row can stand for all of it. */
14
+ export declare function markOdd(it: Item | null): void;
15
+ /** Draws moment `t` (played) and reads look `i` of the stage. */
16
+ export declare function lookAt(r: Reading, render: (t: number) => void, t: number, i: number, closed: boolean): void;