@swf2es/web 0.0.0-stage → 0.1.1
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/README.md +43 -2
- package/dist/air-files.d.ts +40 -0
- package/dist/air-files.d.ts.map +1 -0
- package/dist/air-files.js +304 -0
- package/dist/air-files.js.map +1 -0
- package/dist/air-items.d.ts +28 -0
- package/dist/air-items.d.ts.map +1 -0
- package/dist/air-items.js +186 -0
- package/dist/air-items.js.map +1 -0
- package/dist/air.d.ts +81 -0
- package/dist/air.d.ts.map +1 -0
- package/dist/air.js +409 -0
- package/dist/air.js.map +1 -0
- package/dist/config.d.ts +123 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +127 -0
- package/dist/config.js.map +1 -0
- package/dist/element.d.ts +80 -0
- package/dist/element.d.ts.map +1 -0
- package/dist/element.js +725 -0
- package/dist/element.js.map +1 -0
- package/dist/external.d.ts +49 -0
- package/dist/external.d.ts.map +1 -0
- package/dist/external.js +268 -0
- package/dist/external.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/layout.d.ts +47 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +75 -0
- package/dist/layout.js.map +1 -0
- package/dist/playback.d.ts +107 -0
- package/dist/playback.d.ts.map +1 -0
- package/dist/playback.js +308 -0
- package/dist/playback.js.map +1 -0
- package/dist/replace.d.ts +22 -0
- package/dist/replace.d.ts.map +1 -0
- package/dist/replace.js +141 -0
- package/dist/replace.js.map +1 -0
- package/dist/stage3d.d.ts +25 -0
- package/dist/stage3d.d.ts.map +1 -0
- package/dist/stage3d.js +90 -0
- package/dist/stage3d.js.map +1 -0
- package/package.json +38 -3
- package/src/air-files.ts +371 -0
- package/src/air-items.ts +217 -0
- package/src/air.ts +497 -0
- package/src/config.ts +244 -0
- package/src/element.ts +902 -0
- package/src/external.ts +338 -0
- package/src/index.ts +33 -0
- package/src/layout.ts +127 -0
- package/src/playback.ts +388 -0
- package/src/replace.ts +166 -0
- package/src/stage3d.ts +112 -0
package/src/external.ts
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
// ExternalInterface between a SWF and its page, in Flash's own protocol:
|
|
2
|
+
// what playerglobal sends and expects, and what Flash's plug-in put on the
|
|
3
|
+
// page as __flash__toXML and friends. Values cross as Flash serialised
|
|
4
|
+
// them: strings, numbers, booleans, null, undefined, dates, and arrays and
|
|
5
|
+
// objects, which arrive as plain copies on either side.
|
|
6
|
+
import type { ExternalInterfaceHost } from "@swf2es/player";
|
|
7
|
+
|
|
8
|
+
/** A value as the page sees one. */
|
|
9
|
+
export type PageValue = unknown;
|
|
10
|
+
|
|
11
|
+
const ESCAPES: Record<string, string> = {
|
|
12
|
+
"&": "&",
|
|
13
|
+
"<": "<",
|
|
14
|
+
">": ">",
|
|
15
|
+
'"': """,
|
|
16
|
+
"'": "'",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const escapeXml = (text: string) => text.replace(/[&<>"']/g, (c) => ESCAPES[c]);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A page value as ExternalInterface's XML, as Flash's __flash__toXML wrote
|
|
23
|
+
* it. A value met again inside itself is null, where Flash's recursed
|
|
24
|
+
* until the stack ran out; a function is null, as playerglobal makes one.
|
|
25
|
+
*/
|
|
26
|
+
export function toXml(value: PageValue, seen = new Set<object>()): string {
|
|
27
|
+
switch (typeof value) {
|
|
28
|
+
case "string":
|
|
29
|
+
return `<string>${escapeXml(value)}</string>`;
|
|
30
|
+
case "undefined":
|
|
31
|
+
return "<undefined/>";
|
|
32
|
+
case "number":
|
|
33
|
+
return `<number>${value}</number>`;
|
|
34
|
+
case "boolean":
|
|
35
|
+
return value ? "<true/>" : "<false/>";
|
|
36
|
+
case "object":
|
|
37
|
+
break;
|
|
38
|
+
default:
|
|
39
|
+
return "<null/>";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (value === null || seen.has(value)) {
|
|
43
|
+
return "<null/>";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (value instanceof Date) {
|
|
47
|
+
return `<date>${value.getTime()}</date>`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
seen.add(value);
|
|
51
|
+
const parts: string[] = [];
|
|
52
|
+
const entries = Array.isArray(value)
|
|
53
|
+
? value.map((item, i) => [String(i), item] as const)
|
|
54
|
+
: Object.entries(value);
|
|
55
|
+
for (const [id, item] of entries) {
|
|
56
|
+
parts.push(`<property id="${escapeXml(id)}">${toXml(item, seen)}</property>`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
seen.delete(value);
|
|
60
|
+
const tag = Array.isArray(value) ? "array" : "object";
|
|
61
|
+
return `<${tag}>${parts.join("")}</${tag}>`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The arguments of an invocation, as Flash's __flash__argumentsToXML. */
|
|
65
|
+
export function argumentsToXml(args: readonly PageValue[]): string {
|
|
66
|
+
return `<arguments>${args.map((arg) => toXml(arg)).join("")}</arguments>`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface Element {
|
|
70
|
+
name: string;
|
|
71
|
+
attributes: Map<string, string>;
|
|
72
|
+
/** Text, decoded, and the child elements, in order. */
|
|
73
|
+
text: string;
|
|
74
|
+
children: Element[];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Thrown for XML this does not read: what a SWF answers is always its own _toXML's. */
|
|
78
|
+
class Malformed extends Error {}
|
|
79
|
+
|
|
80
|
+
const ENTITIES: Record<string, string> = {
|
|
81
|
+
amp: "&",
|
|
82
|
+
lt: "<",
|
|
83
|
+
gt: ">",
|
|
84
|
+
quot: '"',
|
|
85
|
+
apos: "'",
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
function decode(text: string): string {
|
|
89
|
+
return text.replace(/&(#x[0-9a-f]+|#\d+|\w+);/gi, (whole, name: string) => {
|
|
90
|
+
if (name[0] === "#") {
|
|
91
|
+
const code =
|
|
92
|
+
name[1] === "x" || name[1] === "X" ? parseInt(name.slice(2), 16) : +name.slice(1);
|
|
93
|
+
return Number.isFinite(code) && code <= 0x10ffff ? String.fromCodePoint(code) : whole;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return ENTITIES[name] ?? whole;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The elements of the XML ExternalInterface sends: tags, their attributes,
|
|
102
|
+
* text and entities, no more. E4X writes what playerglobal's XML() makes,
|
|
103
|
+
* which may close an empty element either way.
|
|
104
|
+
*/
|
|
105
|
+
function parse(xml: string): Element {
|
|
106
|
+
let at = 0;
|
|
107
|
+
const element = (): Element => {
|
|
108
|
+
const open = /^<([\w:.-]+)/.exec(xml.slice(at, at + 256));
|
|
109
|
+
if (!open) {
|
|
110
|
+
throw new Malformed(`no element at ${at}`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
at += open[0].length;
|
|
114
|
+
const node: Element = { name: open[1], attributes: new Map(), text: "", children: [] };
|
|
115
|
+
for (;;) {
|
|
116
|
+
const attribute = /^\s+([\w:.-]+)\s*=\s*("([^"]*)"|'([^']*)')/.exec(xml.slice(at));
|
|
117
|
+
if (!attribute) {
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
node.attributes.set(attribute[1], decode(attribute[3] ?? attribute[4]));
|
|
122
|
+
at += attribute[0].length;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const end = /^\s*(\/?)>/.exec(xml.slice(at, at + 64));
|
|
126
|
+
if (!end) {
|
|
127
|
+
throw new Malformed(`an unclosed tag at ${at}`);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
at += end[0].length;
|
|
131
|
+
if (end[1]) {
|
|
132
|
+
return node;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const text: string[] = [];
|
|
136
|
+
for (;;) {
|
|
137
|
+
const next = xml.indexOf("<", at);
|
|
138
|
+
if (next < 0) {
|
|
139
|
+
throw new Malformed(`no end for <${node.name}>`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
text.push(xml.slice(at, next));
|
|
143
|
+
at = next;
|
|
144
|
+
if (xml.startsWith("</", at)) {
|
|
145
|
+
const close = xml.indexOf(">", at);
|
|
146
|
+
if (close < 0 || xml.slice(at + 2, close).trim() !== node.name) {
|
|
147
|
+
throw new Malformed(`a wrong end for <${node.name}>`);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
at = close + 1;
|
|
151
|
+
node.text = decode(text.join(""));
|
|
152
|
+
return node;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
node.children.push(element());
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
at = xml.search(/\S/);
|
|
160
|
+
if (at < 0) {
|
|
161
|
+
throw new Malformed("no XML");
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return element();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Thrown into the page for an `<exception>`, as Flash's plug-in threw what marshallExceptions sent. */
|
|
168
|
+
export class ExternalInterfaceError extends Error {}
|
|
169
|
+
|
|
170
|
+
/** An ExternalInterface value's XML as a page value. */
|
|
171
|
+
export function fromXml(xml: string): PageValue {
|
|
172
|
+
return value(parse(xml));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function value(node: Element): PageValue {
|
|
176
|
+
switch (node.name) {
|
|
177
|
+
case "string":
|
|
178
|
+
return node.text;
|
|
179
|
+
case "number":
|
|
180
|
+
return Number(node.text);
|
|
181
|
+
case "true":
|
|
182
|
+
return true;
|
|
183
|
+
case "false":
|
|
184
|
+
return false;
|
|
185
|
+
case "null":
|
|
186
|
+
return null;
|
|
187
|
+
case "date":
|
|
188
|
+
return new Date(Number(node.text));
|
|
189
|
+
case "array": {
|
|
190
|
+
const array: PageValue[] = [];
|
|
191
|
+
for (const property of node.children) {
|
|
192
|
+
array[Number(property.attributes.get("id"))] = value(property.children[0]);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return array;
|
|
196
|
+
}
|
|
197
|
+
case "object": {
|
|
198
|
+
const object: Record<string, PageValue> = {};
|
|
199
|
+
for (const property of node.children) {
|
|
200
|
+
const id = property.attributes.get("id") ?? "";
|
|
201
|
+
// A plain own property, though the SWF named one __proto__.
|
|
202
|
+
Object.defineProperty(object, id, {
|
|
203
|
+
value: value(property.children[0]),
|
|
204
|
+
enumerable: true,
|
|
205
|
+
writable: true,
|
|
206
|
+
configurable: true,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return object;
|
|
211
|
+
}
|
|
212
|
+
case "exception":
|
|
213
|
+
throw new ExternalInterfaceError(node.text);
|
|
214
|
+
default:
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** How playerglobal's `call` begins and ends the name in the invocation it sends. */
|
|
220
|
+
const INVOKE = '<invoke name="';
|
|
221
|
+
const NAME_END = '" returntype="xml"><arguments>';
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* An invocation's function name and arguments, as playerglobal's call
|
|
225
|
+
* sends one. It writes the name as it is, unescaped, quotes and all, as an
|
|
226
|
+
* inline function's source has them, so the name is what lies between the
|
|
227
|
+
* start and the first end that follows it: the arguments come after, so
|
|
228
|
+
* nothing in them can move where the name ends.
|
|
229
|
+
*/
|
|
230
|
+
export function readInvocation(xml: string): { name: string; args: PageValue[] } {
|
|
231
|
+
const end = xml.indexOf(NAME_END, INVOKE.length);
|
|
232
|
+
if (!xml.startsWith(INVOKE) || end < 0) {
|
|
233
|
+
throw new Malformed("no invocation");
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const args = parse(
|
|
237
|
+
xml.slice(end + NAME_END.length - "<arguments>".length, xml.lastIndexOf("</invoke>")),
|
|
238
|
+
);
|
|
239
|
+
return { name: xml.slice(INVOKE.length, end), args: args.children.map(value) };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** What a page calls a SWF's callback with: Flash's invocation, answered in XML. */
|
|
243
|
+
export function invocation(name: string, args: readonly PageValue[]): string {
|
|
244
|
+
return `<invoke name="${escapeXml(name)}" returntype="xml">${argumentsToXml(args)}</invoke>`;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** A function name that is a path of identifiers, `a.b.c`, around which spaces do not count. */
|
|
248
|
+
const PATH = /^[\p{L}_$][\p{L}\p{N}_$]*(\.[\p{L}_$][\p{L}\p{N}_$]*)*$/u;
|
|
249
|
+
|
|
250
|
+
/** What the page's `window` runs: the global object of where this runs. */
|
|
251
|
+
const page = globalThis as unknown as Record<string, unknown>;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The function an ExternalInterface.call names, with what to call it on.
|
|
255
|
+
* A path is looked up from the page's global object along its dots, on
|
|
256
|
+
* what holds it. Any other name, an inline function's source as pages
|
|
257
|
+
* wrote them, is evaluated alone, `(name)`, as the code the SWF gave it to
|
|
258
|
+
* be; the call's arguments never are: they are applied to what it gives.
|
|
259
|
+
* Under a policy that forbids eval, such a name comes to nothing.
|
|
260
|
+
*/
|
|
261
|
+
function pageFunction(name: string): { fn: unknown; self: unknown } {
|
|
262
|
+
const path = name.trim();
|
|
263
|
+
if (PATH.test(path)) {
|
|
264
|
+
let self: unknown = page;
|
|
265
|
+
let fn: unknown = page;
|
|
266
|
+
for (const part of path.split(".")) {
|
|
267
|
+
self = fn;
|
|
268
|
+
fn = fn === null || fn === undefined ? undefined : (fn as Record<string, unknown>)[part];
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return { fn, self };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// On a line of its own, so that a name ending in a // comment still closes.
|
|
275
|
+
return { fn: new Function(`return (${name}\n);`)(), self: undefined };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** How the host page answers ExternalInterface, and what it is told of callbacks. */
|
|
279
|
+
export interface PageBridge {
|
|
280
|
+
/** The element's id, or its name; Flash's objectID. */
|
|
281
|
+
objectID: string | null;
|
|
282
|
+
/** The SWF added callback `name`, or removed it with null. */
|
|
283
|
+
callback(name: string, call: ((...args: PageValue[]) => PageValue) | null): void;
|
|
284
|
+
/** Whether the SWF still plays: a callback the page kept does nothing once it does not. */
|
|
285
|
+
alive(): boolean;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* The ExternalInterface host for a page that lets SWFs script it: those
|
|
290
|
+
* `allows` allows, by the calling SWF's URL (the player asks before it
|
|
291
|
+
* calls here). It evaluates no JavaScript, so that a SWF's every call
|
|
292
|
+
* comes as XML: playerglobal's JavaScript form writes an object's keys
|
|
293
|
+
* unquoted, as Flash's did, so a key could carry code into an eval.
|
|
294
|
+
* The XML is read as data, and the name alone made a function
|
|
295
|
+
* (pageFunction). The page's calls into the SWF go as XML both ways.
|
|
296
|
+
*/
|
|
297
|
+
export function externalInterfaceHost(
|
|
298
|
+
bridge: PageBridge,
|
|
299
|
+
report: (error: unknown) => void,
|
|
300
|
+
allows: (url: string) => boolean = () => true,
|
|
301
|
+
): ExternalInterfaceHost {
|
|
302
|
+
return {
|
|
303
|
+
objectID: bridge.objectID,
|
|
304
|
+
allows,
|
|
305
|
+
callOut(request) {
|
|
306
|
+
try {
|
|
307
|
+
const call = readInvocation(request);
|
|
308
|
+
const { fn, self } = pageFunction(call.name);
|
|
309
|
+
return typeof fn === "function" ? toXml(fn.apply(self, call.args)) : "<undefined/>";
|
|
310
|
+
} catch {
|
|
311
|
+
// XML it cannot read, a name that is no expression or that eval is refused for, or a call that threw.
|
|
312
|
+
return "<undefined/>";
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
addCallback(name, callback) {
|
|
316
|
+
if (!callback) {
|
|
317
|
+
bridge.callback(name, null);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
bridge.callback(name, (...args) => {
|
|
322
|
+
if (!bridge.alive()) {
|
|
323
|
+
return undefined;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
let answer: unknown;
|
|
327
|
+
try {
|
|
328
|
+
answer = callback(invocation(name, args), null);
|
|
329
|
+
} catch (error) {
|
|
330
|
+
report(error);
|
|
331
|
+
throw new ExternalInterfaceError(`Error calling method on NPObject: ${name}`);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return typeof answer === "string" ? fromXml(answer) : undefined;
|
|
335
|
+
});
|
|
336
|
+
},
|
|
337
|
+
};
|
|
338
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* swf2es on any web page: the `<swf2es-player>` element, defined as this
|
|
3
|
+
* module loads where there is a DOM, `replaceFlash` for a page's Flash
|
|
4
|
+
* tags, `openAirApp` for an AIR application the element plays, and the
|
|
5
|
+
* page's one configuration.
|
|
6
|
+
*/
|
|
7
|
+
import { defineElement } from "./element.js";
|
|
8
|
+
|
|
9
|
+
// Whether a page's own file inputs should go without a type filter, which iOS's pickers grey
|
|
10
|
+
// SWFs out for.
|
|
11
|
+
export { filtersGreyOutFiles } from "@swf2es/player";
|
|
12
|
+
export { AirApp, type AirAppFile, openAirApp } from "./air.js";
|
|
13
|
+
export {
|
|
14
|
+
type Configuration,
|
|
15
|
+
configuration,
|
|
16
|
+
configure,
|
|
17
|
+
type SocketProxy,
|
|
18
|
+
} from "./config.js";
|
|
19
|
+
export {
|
|
20
|
+
defineElement,
|
|
21
|
+
parseColor,
|
|
22
|
+
parseFlashVars,
|
|
23
|
+
Swf2esPlayerElement,
|
|
24
|
+
type SwfSource,
|
|
25
|
+
scriptAccess,
|
|
26
|
+
TAG,
|
|
27
|
+
} from "./element.js";
|
|
28
|
+
export { ExternalInterfaceError, type PageValue } from "./external.js";
|
|
29
|
+
export { type Placement, place, type ScaleMode, scaleMode } from "./layout.js";
|
|
30
|
+
export { flashAttributes, isFlash, replaceFlash, watchFlash } from "./replace.js";
|
|
31
|
+
export type { Stage3DSurfaceFactory } from "./stage3d.js";
|
|
32
|
+
|
|
33
|
+
defineElement();
|
package/src/layout.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// Where the stage goes in its element, as Flash's plug-in placed it by the
|
|
2
|
+
// embedding's `scale` and `salign`, and the resolution it is drawn at.
|
|
3
|
+
|
|
4
|
+
export type ScaleMode = "showAll" | "noBorder" | "exactFit" | "noScale";
|
|
5
|
+
|
|
6
|
+
/** The stage's box in the element, in CSS pixels, and the renderer's resolution: device pixels to a stage pixel. */
|
|
7
|
+
export interface Placement {
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
resolution: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The largest side of the canvas, in device pixels: past it a stage drawn full screen on a large display costs more memory than it shows. */
|
|
16
|
+
export const MAX_SIDE = 4096;
|
|
17
|
+
|
|
18
|
+
/** A `scale` attribute's mode, any case, Flash's showAll for none or one unknown. */
|
|
19
|
+
export function scaleMode(value: string | null): ScaleMode {
|
|
20
|
+
switch (value?.toLowerCase()) {
|
|
21
|
+
case "noborder":
|
|
22
|
+
return "noBorder";
|
|
23
|
+
case "exactfit":
|
|
24
|
+
return "exactFit";
|
|
25
|
+
case "noscale":
|
|
26
|
+
return "noScale";
|
|
27
|
+
default:
|
|
28
|
+
return "showAll";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The stage of `stageWidth` by `stageHeight` in a box of `boxWidth` by
|
|
34
|
+
* `boxHeight`: showAll fits it whole, noBorder fills the box and crops,
|
|
35
|
+
* exactFit stretches it to the box, and noScale leaves it its size. `salign`
|
|
36
|
+
* holds its edges, as Flash's T, B, L and R in any order and case; the
|
|
37
|
+
* stage is centred along an axis it names neither edge of. `dpr` is the
|
|
38
|
+
* device's pixels to a CSS pixel.
|
|
39
|
+
*/
|
|
40
|
+
export function place(
|
|
41
|
+
stageWidth: number,
|
|
42
|
+
stageHeight: number,
|
|
43
|
+
boxWidth: number,
|
|
44
|
+
boxHeight: number,
|
|
45
|
+
mode: ScaleMode,
|
|
46
|
+
salign: string | null,
|
|
47
|
+
dpr: number,
|
|
48
|
+
leadingEdge = false,
|
|
49
|
+
): Placement {
|
|
50
|
+
const sx = boxWidth / stageWidth;
|
|
51
|
+
const sy = boxHeight / stageHeight;
|
|
52
|
+
let width = boxWidth;
|
|
53
|
+
let height = boxHeight;
|
|
54
|
+
if (mode !== "exactFit") {
|
|
55
|
+
const scale =
|
|
56
|
+
mode === "showAll" ? Math.min(sx, sy) : mode === "noBorder" ? Math.max(sx, sy) : 1;
|
|
57
|
+
width = stageWidth * scale;
|
|
58
|
+
height = stageHeight * scale;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const align = (salign ?? "").toUpperCase();
|
|
62
|
+
const along = (free: number, start: boolean, end: boolean) =>
|
|
63
|
+
start && (leadingEdge || !end) ? 0 : end && !start ? free : free / 2;
|
|
64
|
+
// Stretched one way more than the other, it is drawn as fine as its finer axis needs.
|
|
65
|
+
const finest = Math.max(width / stageWidth, height / stageHeight) * dpr;
|
|
66
|
+
const largest = MAX_SIDE / Math.max(stageWidth, stageHeight);
|
|
67
|
+
return {
|
|
68
|
+
x: along(boxWidth - width, align.includes("L"), align.includes("R")),
|
|
69
|
+
y: along(boxHeight - height, align.includes("T"), align.includes("B")),
|
|
70
|
+
width,
|
|
71
|
+
height,
|
|
72
|
+
// Never nothing: a hidden element's renderer still needs a size.
|
|
73
|
+
resolution: Math.max(Math.min(finest, largest), 1 / 64),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* What the canvas shows, in stage coordinates, and the renderer's resolution
|
|
79
|
+
* for it. Flash draws what lies past the stage's edges where the box shows
|
|
80
|
+
* it, in showAll's and noScale's margins as on its stage, so the canvas
|
|
81
|
+
* covers the whole box and the viewport is the box taken into the stage's
|
|
82
|
+
* coordinates; `css` is where the canvas goes in the box, in CSS pixels.
|
|
83
|
+
* exactFit stretches the stage to the box, one axis more than the other,
|
|
84
|
+
* which leaves no margin: the canvas is the stage, stretched by CSS.
|
|
85
|
+
*/
|
|
86
|
+
export interface Viewport {
|
|
87
|
+
x: number;
|
|
88
|
+
y: number;
|
|
89
|
+
width: number;
|
|
90
|
+
height: number;
|
|
91
|
+
resolution: number;
|
|
92
|
+
css: { x: number; y: number; width: number; height: number };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The viewport for `placed`, the stage of `stageWidth` by `stageHeight` placed in a box of `boxWidth` by `boxHeight`. */
|
|
96
|
+
export function viewport(
|
|
97
|
+
stageWidth: number,
|
|
98
|
+
stageHeight: number,
|
|
99
|
+
boxWidth: number,
|
|
100
|
+
boxHeight: number,
|
|
101
|
+
mode: ScaleMode,
|
|
102
|
+
placed: Placement,
|
|
103
|
+
): Viewport {
|
|
104
|
+
const scale = placed.width / stageWidth;
|
|
105
|
+
if (mode === "exactFit" || !(scale > 0) || !(boxWidth > 0) || !(boxHeight > 0)) {
|
|
106
|
+
return {
|
|
107
|
+
x: 0,
|
|
108
|
+
y: 0,
|
|
109
|
+
width: stageWidth,
|
|
110
|
+
height: stageHeight,
|
|
111
|
+
resolution: placed.resolution,
|
|
112
|
+
css: { x: placed.x, y: placed.y, width: placed.width, height: placed.height },
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const width = boxWidth / scale;
|
|
117
|
+
const height = boxHeight / scale;
|
|
118
|
+
return {
|
|
119
|
+
x: placed.x ? -placed.x / scale : 0,
|
|
120
|
+
y: placed.y ? -placed.y / scale : 0,
|
|
121
|
+
width,
|
|
122
|
+
height,
|
|
123
|
+
// As fine as the stage, whose MAX_SIDE bounds it: the canvas then has at most the box's device pixels.
|
|
124
|
+
resolution: placed.resolution,
|
|
125
|
+
css: { x: 0, y: 0, width: boxWidth, height: boxHeight },
|
|
126
|
+
};
|
|
127
|
+
}
|