swf2js 0.1.0 → 0.1.2
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/README.md +9 -9
- package/package.json +3 -2
- package/src/cli.js +15 -6
- package/src/emitter/bundle-emitter.js +56 -16
- package/src/runtime/avm1-runtime.js +329 -84
- package/src/runtime/avm2-runtime.js +56 -6
- package/src/runtime/embedded-movie-runtime.js +6 -1
- package/src/runtime/index.js +2 -6
- package/src/runtime/movie-runtime.js +2 -1
- package/src/runtime/swflib/assemble.js +34 -0
- package/src/runtime/swflib/bitmap-image.js +4 -0
- package/src/runtime/swflib/bitmap.js +1004 -0
- package/src/runtime/swflib/builtins.js +156 -0
- package/src/runtime/swflib/bytearray.js +949 -0
- package/src/runtime/swflib/compiled-swf.js +3 -0
- package/src/runtime/swflib/context-menu.js +245 -0
- package/src/runtime/swflib/cursor-flag.js +12 -0
- package/src/runtime/swflib/dictionary.js +121 -0
- package/src/runtime/swflib/display.js +823 -0
- package/src/runtime/swflib/events.js +754 -0
- package/src/runtime/swflib/filters.js +298 -0
- package/src/runtime/swflib/flash.js +130 -0
- package/src/runtime/swflib/geom.js +759 -0
- package/src/runtime/swflib/glyph-metrics.js +14 -0
- package/src/runtime/swflib/graphics.js +735 -0
- package/src/runtime/swflib/host.js +2 -0
- package/src/runtime/swflib/inherit.js +22 -0
- package/src/runtime/swflib/input.js +1085 -0
- package/src/runtime/swflib/loader.js +687 -0
- package/src/runtime/swflib/marks.js +16 -0
- package/src/runtime/swflib/matrix3d.js +144 -0
- package/src/runtime/swflib/pointer-bridge.js +8 -0
- package/src/runtime/swflib/projection.js +158 -0
- package/src/runtime/swflib/render.js +1240 -0
- package/src/runtime/swflib/sound.js +1166 -0
- package/src/runtime/swflib/sprite-drag.js +116 -0
- package/src/runtime/swflib/stage-ref.js +9 -0
- package/src/runtime/swflib/stage.js +484 -0
- package/src/runtime/swflib/system.js +987 -0
- package/src/runtime/swflib/text-field.js +519 -0
- package/src/runtime/swflib/text-paint.js +797 -0
- package/src/runtime/swflib/text.js +1387 -0
- package/src/runtime/swflib/transform.js +133 -0
- package/src/runtime/swflib/xml.js +268 -0
- package/src/transpiler/avm1/avm1-ir.js +2 -1
- package/src/transpiler/avm1/avm1-rewrite.js +23 -2
- package/src/transpiler/avm1/avm1-transpiler.js +4 -1
- package/src/transpiler/avm2/avm2-transpiler.js +38 -7
- package/src/transpiler/timeline/timeline-codegen.js +366 -123
- package/src/runtime/swflib.js +0 -14050
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
import { InteractiveObject, authoredTextLocalBounds } from './display.js';
|
|
2
|
+
import { AntiAliasType, GridFitType, TextFieldAutoSize, TextFieldType, csmGridFitTypes } from './events.js';
|
|
3
|
+
import { Rectangle } from './geom.js';
|
|
4
|
+
import { getTextMeasurementContext, layoutTextField, measureTextRun, parseHtmlTextLines, textFirstVisibleLine, textHorizontalScroll } from './text-paint.js';
|
|
5
|
+
import { StyleSheet, TextFormat } from './text.js';
|
|
6
|
+
|
|
7
|
+
export var fixedTextFieldSizes = new WeakMap();
|
|
8
|
+
export function textFieldUsesCenteredAutoSize(field) {
|
|
9
|
+
return field._swoofAuthoredText && field.type === TextFieldType.DYNAMIC &&
|
|
10
|
+
!field.wordWrap && field.autoSize === TextFieldAutoSize.CENTER;
|
|
11
|
+
}
|
|
12
|
+
export function textFieldFitsContent(field) {
|
|
13
|
+
return textFieldUsesCenteredAutoSize(field) ||
|
|
14
|
+
(field.type === TextFieldType.DYNAMIC && (!field.wordWrap || field._swoofAuthoredText) &&
|
|
15
|
+
(field.autoSize === 'left' || field.autoSize === true));
|
|
16
|
+
}
|
|
17
|
+
export var textFieldSizeProperties = {
|
|
18
|
+
width: {
|
|
19
|
+
enumerable: true, configurable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return !this.wordWrap && textFieldFitsContent(this) ? layoutTextField(this).width : fixedTextFieldSizes.get(this).width;
|
|
22
|
+
},
|
|
23
|
+
set: function (value) { fixedTextFieldSizes.get(this).width = value; },
|
|
24
|
+
},
|
|
25
|
+
height: {
|
|
26
|
+
enumerable: true, configurable: true,
|
|
27
|
+
get: function () { return textFieldFitsContent(this) ? layoutTextField(this).height : fixedTextFieldSizes.get(this).height; },
|
|
28
|
+
set: function (value) { fixedTextFieldSizes.get(this).height = value; },
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
export function bindTextFieldSize(field) {
|
|
32
|
+
fixedTextFieldSizes.set(field, { width: field.width, height: field.height });
|
|
33
|
+
Object.defineProperties(field, textFieldSizeProperties);
|
|
34
|
+
}
|
|
35
|
+
export var avm1TextFieldEvents = new WeakMap();
|
|
36
|
+
export function addAvm1TextFieldListener(listener) {
|
|
37
|
+
var state = avm1TextFieldEvents.get(this);
|
|
38
|
+
if (!state || !listener || listener === this || typeof listener !== 'object' && typeof listener !== 'function' ||
|
|
39
|
+
state.listeners.has(listener)) return false;
|
|
40
|
+
state.listeners.add(listener);
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
export function removeAvm1TextFieldListener(listener) {
|
|
44
|
+
return avm1TextFieldEvents.get(this)?.listeners.delete(listener) ?? false;
|
|
45
|
+
}
|
|
46
|
+
export function broadcastAvm1TextField(field, event) {
|
|
47
|
+
var state = avm1TextFieldEvents.get(field);
|
|
48
|
+
if (!state || field._swoofDetached) return;
|
|
49
|
+
var listeners = Array.from(state.listeners);
|
|
50
|
+
state.invoke(field, field, event);
|
|
51
|
+
for (var listener of listeners) {
|
|
52
|
+
if (!field._swoofDetached && !listener._swoofDetached && state.listeners.has(listener)) {
|
|
53
|
+
state.invoke(field, listener, event);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export function commitAvm1TextFieldScroll(field, vertical, horizontal) {
|
|
58
|
+
var state = avm1TextFieldEvents.get(field);
|
|
59
|
+
if (!state || field._swoofDetached) return;
|
|
60
|
+
vertical = vertical ?? state.scrollV;
|
|
61
|
+
horizontal = horizontal ?? state.scrollH;
|
|
62
|
+
if (vertical === state.scrollV && horizontal === state.scrollH) return;
|
|
63
|
+
state.scrollV = vertical;
|
|
64
|
+
state.scrollH = horizontal;
|
|
65
|
+
broadcastAvm1TextField(field, 'onScroller');
|
|
66
|
+
}
|
|
67
|
+
export function writeAvm1TextFieldScroll(field, horizontal, value) {
|
|
68
|
+
var property = horizontal ? 'scrollH' : 'scrollV';
|
|
69
|
+
if ((horizontal ? field._scrollH : field[property]) === value) return;
|
|
70
|
+
var alias = horizontal ? 'hscroll' : 'scroll';
|
|
71
|
+
var previous = field[alias];
|
|
72
|
+
field[property] = value;
|
|
73
|
+
var next = field[alias];
|
|
74
|
+
if (next !== previous) commitAvm1TextFieldScroll(field, horizontal ? undefined : next, horizontal ? next : undefined);
|
|
75
|
+
}
|
|
76
|
+
export function nativeTextHorizontalViewport(field) {
|
|
77
|
+
var viewport = field._swoofOverlayElement?._swoofHorizontalScroll;
|
|
78
|
+
return viewport?.field === field && viewport.maxScrollH != null ? viewport : null;
|
|
79
|
+
}
|
|
80
|
+
export function nativeTextVerticalViewport(field) {
|
|
81
|
+
var viewport = field._swoofOverlayElement?._swoofVerticalScroll;
|
|
82
|
+
return viewport?.field === field ? viewport : null;
|
|
83
|
+
}
|
|
84
|
+
export var avm1TextFieldScrollProperties = {
|
|
85
|
+
scroll: {
|
|
86
|
+
enumerable: true,
|
|
87
|
+
get: function () { return textFirstVisibleLine(this, nativeTextVerticalViewport(this) || layoutTextField(this)) + 1; },
|
|
88
|
+
set: function (value) { writeAvm1TextFieldScroll(this, false, value); },
|
|
89
|
+
},
|
|
90
|
+
hscroll: {
|
|
91
|
+
enumerable: true,
|
|
92
|
+
get: function () { return textHorizontalScroll(this, nativeTextHorizontalViewport(this) || layoutTextField(this)); },
|
|
93
|
+
set: function (value) { writeAvm1TextFieldScroll(this, true, value); },
|
|
94
|
+
},
|
|
95
|
+
maxscroll: { enumerable: true, get: function () { return this.maxScrollV; }, set: function () {} },
|
|
96
|
+
maxhscroll: { enumerable: true, get: function () { return this.maxScrollH; }, set: function () {} },
|
|
97
|
+
bottomScroll: { enumerable: true, get: function () { return this.bottomScrollV; }, set: function () {} },
|
|
98
|
+
addListener: { configurable: true, writable: true, value: addAvm1TextFieldListener },
|
|
99
|
+
removeListener: { configurable: true, writable: true, value: removeAvm1TextFieldListener },
|
|
100
|
+
};
|
|
101
|
+
export function bindAvm1TextFieldScroll(field, invoke) {
|
|
102
|
+
avm1TextFieldEvents.set(field, { listeners: new Set(), invoke: invoke, scrollV: 1, scrollH: 0 });
|
|
103
|
+
Object.defineProperties(field, avm1TextFieldScrollProperties);
|
|
104
|
+
}
|
|
105
|
+
export function applyCsmTextSettings(field, settings) {
|
|
106
|
+
if (!settings) return;
|
|
107
|
+
field.antiAliasType = settings.useFlashType === 1 ? AntiAliasType.ADVANCED : AntiAliasType.NORMAL;
|
|
108
|
+
field.gridFitType = csmGridFitTypes[settings.gridFit] ?? GridFitType.PIXEL;
|
|
109
|
+
field.thickness = settings.thickness;
|
|
110
|
+
field.sharpness = settings.sharpness;
|
|
111
|
+
}
|
|
112
|
+
export var textFieldColorRanges = new WeakMap();
|
|
113
|
+
export var avm1NewTextFormats = new WeakMap();
|
|
114
|
+
export function copyAvm1NewTextFormat(format) {
|
|
115
|
+
var copy = Object.assign(new TextFormat(), format);
|
|
116
|
+
if (Array.isArray(copy.tabStops)) copy.tabStops = copy.tabStops.slice();
|
|
117
|
+
return copy;
|
|
118
|
+
}
|
|
119
|
+
export function getAvm1NewTextFormat() {
|
|
120
|
+
return copyAvm1NewTextFormat(avm1NewTextFormats.get(this) || this.defaultTextFormat);
|
|
121
|
+
}
|
|
122
|
+
export function setAvm1NewTextFormat(format) {
|
|
123
|
+
if (!(format instanceof TextFormat)) return;
|
|
124
|
+
var next = getAvm1NewTextFormat.call(this);
|
|
125
|
+
for (var key of Object.keys(next)) {
|
|
126
|
+
if (format[key] != null) next[key] = format[key];
|
|
127
|
+
}
|
|
128
|
+
avm1NewTextFormats.set(this, copyAvm1NewTextFormat(next));
|
|
129
|
+
}
|
|
130
|
+
export function bindAvm1TextFieldFormat(field) {
|
|
131
|
+
field.getNewTextFormat = getAvm1NewTextFormat;
|
|
132
|
+
field.setNewTextFormat = setAvm1NewTextFormat;
|
|
133
|
+
}
|
|
134
|
+
export class TextField extends InteractiveObject {
|
|
135
|
+
constructor() {
|
|
136
|
+
super();
|
|
137
|
+
this.width = 100;
|
|
138
|
+
this.height = 100;
|
|
139
|
+
this.type = TextFieldType.DYNAMIC;
|
|
140
|
+
this._text = '';
|
|
141
|
+
this._htmlText = '';
|
|
142
|
+
this._htmlTextExplicit = false;
|
|
143
|
+
this.defaultTextFormat = new TextFormat();
|
|
144
|
+
this.textColor = 0;
|
|
145
|
+
this.background = false;
|
|
146
|
+
this.backgroundColor = 0xffffff;
|
|
147
|
+
this.border = false;
|
|
148
|
+
this.borderColor = 0;
|
|
149
|
+
this.selectable = true;
|
|
150
|
+
this.wordWrap = false;
|
|
151
|
+
this.multiline = false;
|
|
152
|
+
this.embedFonts = false;
|
|
153
|
+
this.antiAliasType = AntiAliasType.NORMAL;
|
|
154
|
+
this.gridFitType = GridFitType.PIXEL;
|
|
155
|
+
this.thickness = 0;
|
|
156
|
+
this.sharpness = 0;
|
|
157
|
+
this.displayAsPassword = false;
|
|
158
|
+
this.autoSize = 'none';
|
|
159
|
+
this._maxChars = 0;
|
|
160
|
+
this._restrict = null;
|
|
161
|
+
this.scrollH = 0;
|
|
162
|
+
this.scrollV = 1;
|
|
163
|
+
this.length = 0;
|
|
164
|
+
this.mouseWheelEnabled = true;
|
|
165
|
+
this.selectionBeginIndex = 0;
|
|
166
|
+
this.selectionEndIndex = 0;
|
|
167
|
+
this.caretIndex = 0;
|
|
168
|
+
this._swoofSelectionPending = false;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
appendText(s) {
|
|
172
|
+
this.text = (this.text || '') + (s == null ? '' : String(s));
|
|
173
|
+
this.length = this.text.length;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
replaceText(begin, end, text) {
|
|
177
|
+
var t = this.text || '';
|
|
178
|
+
this.text = t.slice(0, begin | 0) + (text == null ? '' : String(text)) + t.slice(end | 0);
|
|
179
|
+
this.length = this.text.length;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
setTextFormat(format, beginIndex, endIndex) {
|
|
183
|
+
if (!format) return;
|
|
184
|
+
var fromStart = beginIndex == null || beginIndex === -1 || beginIndex === 0;
|
|
185
|
+
var throughEnd = endIndex == null || endIndex === -1 || endIndex === this.length;
|
|
186
|
+
var wholeField = fromStart && throughEnd;
|
|
187
|
+
if (!wholeField && !this._htmlTextExplicit && format instanceof TextFormat && format.color != null &&
|
|
188
|
+
Object.keys(format).every(function (key) { return key === 'color' || format[key] == null; })) {
|
|
189
|
+
var start = fromStart ? 0 : Math.max(0, beginIndex | 0);
|
|
190
|
+
var end = throughEnd ? this.length : Math.min(this.length, endIndex | 0);
|
|
191
|
+
setTextColorRange(this, start, end, format.color);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
var authored = this._swoofAuthoredText || (this._swoofAvm1TextField && this._swoofCharacterId != null);
|
|
195
|
+
if (wholeField && authored && format instanceof TextFormat) {
|
|
196
|
+
var previous = this.defaultTextFormat;
|
|
197
|
+
var merged = new TextFormat();
|
|
198
|
+
for (var property of Object.keys(merged)) {
|
|
199
|
+
merged[property] = format[property] ?? previous?.[property] ?? null;
|
|
200
|
+
}
|
|
201
|
+
this.defaultTextFormat = merged;
|
|
202
|
+
} else this.defaultTextFormat = format;
|
|
203
|
+
if (wholeField && format.color != null) {
|
|
204
|
+
this._swoofAppliedTextColor = format.color;
|
|
205
|
+
textFieldColorRanges.delete(this);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
getTextFormat() {
|
|
210
|
+
return this.defaultTextFormat ? this.defaultTextFormat.clone() : new TextFormat();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
setSelection(beginIndex, endIndex) {
|
|
214
|
+
var len = String(this.text || '').length;
|
|
215
|
+
var begin = Math.max(0, Math.min(len, beginIndex | 0));
|
|
216
|
+
var end = Math.max(0, Math.min(len, endIndex | 0));
|
|
217
|
+
this.selectionBeginIndex = Math.min(begin, end);
|
|
218
|
+
this.selectionEndIndex = Math.max(begin, end);
|
|
219
|
+
this.caretIndex = end;
|
|
220
|
+
this._swoofSelectionPending = true;
|
|
221
|
+
try {
|
|
222
|
+
if (typeof globalThis !== 'undefined' && typeof globalThis.__swoof_applyTextSelection === 'function') {
|
|
223
|
+
globalThis.__swoof_applyTextSelection(this);
|
|
224
|
+
}
|
|
225
|
+
} catch (_) {}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
getLineText(index) { return textLineAt(layoutTextField(this), index).text; }
|
|
229
|
+
getLineLength(index) { return textLineAt(layoutTextField(this), index).text.length; }
|
|
230
|
+
getLineOffset(index) { return textLineAt(layoutTextField(this), index).start; }
|
|
231
|
+
getLineMetrics(index) {
|
|
232
|
+
var layout = layoutTextField(this), line = textLineAt(layout, index);
|
|
233
|
+
var bounds = authoredTextLocalBounds(this);
|
|
234
|
+
return new TextLineMetrics((bounds?.x || 0) + line.x - textHorizontalScroll(this, layout), line.width, line.height,
|
|
235
|
+
line.ascent, line.descent, line.leading);
|
|
236
|
+
}
|
|
237
|
+
getLineIndexOfChar(charIndex) {
|
|
238
|
+
var index = charIndex | 0, lines = layoutTextField(this).lines;
|
|
239
|
+
if (index < 0 || index >= this.length) return -1;
|
|
240
|
+
return lines.findIndex(function (line) { return index < line.start + line.text.length; });
|
|
241
|
+
}
|
|
242
|
+
getLineIndexAtPoint(x, y) {
|
|
243
|
+
var layout = layoutTextField(this);
|
|
244
|
+
var bounds = authoredTextLocalBounds(this);
|
|
245
|
+
x = Number(x) - (bounds?.x || 0);
|
|
246
|
+
y = Number(y) - (bounds?.y || 0);
|
|
247
|
+
if (!Number.isFinite(x) || !Number.isFinite(y) || x < 0 || y < 0 ||
|
|
248
|
+
x >= layout.width || y >= layout.height) return -1;
|
|
249
|
+
var first = textFirstVisibleLine(this, layout);
|
|
250
|
+
var absoluteY = y + layout.lines[first].y;
|
|
251
|
+
for (var i = first + 1; i < layout.lines.length; i++) {
|
|
252
|
+
if (absoluteY < layout.lines[i].y) return i - 1;
|
|
253
|
+
}
|
|
254
|
+
return layout.lines.length - 1;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
getFirstCharInParagraph(charIndex) {
|
|
258
|
+
return textParagraphAt(this, charIndex).paragraphStart;
|
|
259
|
+
}
|
|
260
|
+
getParagraphLength(charIndex) {
|
|
261
|
+
var line = textParagraphAt(this, charIndex);
|
|
262
|
+
return line.paragraphEnd - line.paragraphStart + ((charIndex | 0) === this.length ? 1 : 0);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
getCharBoundaries(charIndex) {
|
|
266
|
+
var index = charIndex | 0, lineIndex = this.getLineIndexOfChar(index);
|
|
267
|
+
var layout = layoutTextField(this), first = textFirstVisibleLine(this, layout);
|
|
268
|
+
if (lineIndex < first) return null;
|
|
269
|
+
var line = layout.lines[lineIndex], y = line.y - layout.lines[first].y;
|
|
270
|
+
if (layout.height > 0 && y >= layout.height) return null;
|
|
271
|
+
var x = line.x - textHorizontalScroll(this, layout);
|
|
272
|
+
for (var run of line.runs) {
|
|
273
|
+
var start = index - run.start;
|
|
274
|
+
if (start >= 0 && start < run.text.length) {
|
|
275
|
+
if (start > 0 && run.text.codePointAt(start - 1) > 0xffff) start--;
|
|
276
|
+
var end = start + (run.text.codePointAt(start) > 0xffff ? 2 : 1);
|
|
277
|
+
var advance = textRunAdvance(run, start);
|
|
278
|
+
var bounds = authoredTextLocalBounds(this);
|
|
279
|
+
return new Rectangle((bounds?.x || 0) + x + advance, (bounds?.y || 0) + y + line.ascent - run.ascent,
|
|
280
|
+
textRunAdvance(run, end) - advance, run.ascent + run.descent);
|
|
281
|
+
}
|
|
282
|
+
x += run.width;
|
|
283
|
+
}
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
getCharIndexAtPoint(x, y) {
|
|
288
|
+
x = Number(x); y = Number(y);
|
|
289
|
+
var lineIndex = this.getLineIndexAtPoint(x, y);
|
|
290
|
+
if (lineIndex < 0) return -1;
|
|
291
|
+
var layout = layoutTextField(this), first = textFirstVisibleLine(this, layout);
|
|
292
|
+
var line = layout.lines[lineIndex], lineY = line.y - layout.lines[first].y;
|
|
293
|
+
var bounds = authoredTextLocalBounds(this);
|
|
294
|
+
x -= (bounds?.x || 0) + line.x - textHorizontalScroll(this, layout);
|
|
295
|
+
y -= bounds?.y || 0;
|
|
296
|
+
if (x < 0) return -1;
|
|
297
|
+
for (var run of line.runs) {
|
|
298
|
+
if (x < run.width) {
|
|
299
|
+
var runY = lineY + line.ascent - run.ascent;
|
|
300
|
+
if (y < runY || y >= runY + run.ascent + run.descent) return -1;
|
|
301
|
+
for (var index = 0; index < run.text.length;) {
|
|
302
|
+
var end = index + (run.text.codePointAt(index) > 0xffff ? 2 : 1);
|
|
303
|
+
if (x < textRunAdvance(run, end)) return run.start + index;
|
|
304
|
+
index = end;
|
|
305
|
+
}
|
|
306
|
+
return -1;
|
|
307
|
+
}
|
|
308
|
+
x -= run.width;
|
|
309
|
+
}
|
|
310
|
+
return -1;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
export function createMovieTextField(fontDefinitions, base = TextField) {
|
|
314
|
+
if (base !== TextField) return base;
|
|
315
|
+
class MovieTextField extends TextField {
|
|
316
|
+
constructor() {
|
|
317
|
+
super();
|
|
318
|
+
this._swoofEmbeddedFontDefinitions = fontDefinitions;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
Object.defineProperty(MovieTextField, Symbol.hasInstance, { value: function (value) {
|
|
322
|
+
return Function.prototype[Symbol.hasInstance].call(this === MovieTextField ? TextField : this, value);
|
|
323
|
+
} });
|
|
324
|
+
MovieTextField.__swoofQualifiedName = 'flash.text.TextField';
|
|
325
|
+
return MovieTextField;
|
|
326
|
+
}
|
|
327
|
+
Object.defineProperty(TextField.prototype, 'type', {
|
|
328
|
+
get: function () { return this._type || TextFieldType.DYNAMIC; },
|
|
329
|
+
set: function (value) {
|
|
330
|
+
this._type = value === TextFieldType.INPUT ? TextFieldType.INPUT : TextFieldType.DYNAMIC;
|
|
331
|
+
this.tabEnabled = this._type === TextFieldType.INPUT;
|
|
332
|
+
},
|
|
333
|
+
});
|
|
334
|
+
Object.defineProperties(TextField.prototype, {
|
|
335
|
+
autoSize: {
|
|
336
|
+
configurable: true, enumerable: true,
|
|
337
|
+
get: function () { return this._autoSize; },
|
|
338
|
+
set: function (value) {
|
|
339
|
+
this._autoSize = value;
|
|
340
|
+
if (value === TextFieldAutoSize.LEFT && !fixedTextFieldSizes.has(this)) bindTextFieldSize(this);
|
|
341
|
+
},
|
|
342
|
+
},
|
|
343
|
+
condenseWhite: {
|
|
344
|
+
get: function () { return !!this._condenseWhite; },
|
|
345
|
+
set: function (value) { this._condenseWhite = !!value; },
|
|
346
|
+
},
|
|
347
|
+
styleSheet: {
|
|
348
|
+
get: function () { return this._styleSheet || null; },
|
|
349
|
+
set: function (value) {
|
|
350
|
+
if (value != null && !(value instanceof StyleSheet)) throw new TypeError('Expected a StyleSheet');
|
|
351
|
+
this._styleSheet = value;
|
|
352
|
+
if (value && !fixedTextFieldSizes.has(this)) bindTextFieldSize(this);
|
|
353
|
+
},
|
|
354
|
+
},
|
|
355
|
+
restrict: {
|
|
356
|
+
get: function () { return this._restrict; },
|
|
357
|
+
set: function (value) { this._restrict = value == null ? null : String(value); },
|
|
358
|
+
},
|
|
359
|
+
maxChars: {
|
|
360
|
+
get: function () { return this._maxChars; },
|
|
361
|
+
set: function (value) {
|
|
362
|
+
this._maxChars = Math.max(0, value | 0);
|
|
363
|
+
this._swoofMaxCharsExplicit = true;
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
});
|
|
367
|
+
export function decodeTextEntities(text) {
|
|
368
|
+
return String(text).replace(/&(#x[0-9a-f]+|#\d+|nbsp|amp|lt|gt|quot|apos|#39);/gi, function (match, entity) {
|
|
369
|
+
var key = entity.toLowerCase();
|
|
370
|
+
if (key === 'nbsp') return '\u00a0';
|
|
371
|
+
if (key === 'amp') return '&';
|
|
372
|
+
if (key === 'lt') return '<';
|
|
373
|
+
if (key === 'gt') return '>';
|
|
374
|
+
if (key === 'quot') return '"';
|
|
375
|
+
if (key === 'apos' || key === '#39') return "'";
|
|
376
|
+
var code = key.indexOf('#x') === 0 ? parseInt(key.slice(2), 16) : parseInt(key.slice(1), 10);
|
|
377
|
+
try { return Number.isFinite(code) && code >= 0 && code <= 0x10ffff ? String.fromCodePoint(code) : match; }
|
|
378
|
+
catch (_) { return match; }
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
export function htmlTextToPlainText(markup, condenseWhite) {
|
|
382
|
+
return plainTextFromHtmlLines(parseHtmlTextLines(markup, {}, null, condenseWhite));
|
|
383
|
+
}
|
|
384
|
+
export function plainTextFromHtmlLines(lines) {
|
|
385
|
+
return lines.map(function (line) {
|
|
386
|
+
return line.runs.map(function (run) { return run.text; }).join('');
|
|
387
|
+
}).join('\n');
|
|
388
|
+
}
|
|
389
|
+
export function uniformHtmlRunProperty(lines, name) {
|
|
390
|
+
var value = lines.find(function (line) { return line.runs.length; })?.runs[0].style[name];
|
|
391
|
+
if (value == null) return null;
|
|
392
|
+
return lines.every(function (line) {
|
|
393
|
+
return line.runs.every(function (run) { return run.style[name] === value; });
|
|
394
|
+
}) ? value : null;
|
|
395
|
+
}
|
|
396
|
+
Object.defineProperty(TextField.prototype, 'text', {
|
|
397
|
+
enumerable: true, configurable: true,
|
|
398
|
+
get: function () { return this._text || ''; },
|
|
399
|
+
set: function (value) {
|
|
400
|
+
if (this.styleSheet) { this.htmlText = value; return; }
|
|
401
|
+
this._text = value == null ? '' : String(value);
|
|
402
|
+
this._htmlText = this._text;
|
|
403
|
+
this._htmlTextExplicit = false;
|
|
404
|
+
this._htmlCondenseWhite = false;
|
|
405
|
+
this._swoofHtmlLetterSpacing = null;
|
|
406
|
+
this._swoofHtmlKerning = null;
|
|
407
|
+
this._swoofAppliedTextColor = null;
|
|
408
|
+
var next = avm1NewTextFormats.get(this);
|
|
409
|
+
if (next) {
|
|
410
|
+
this.defaultTextFormat = copyAvm1NewTextFormat(next);
|
|
411
|
+
if (next.color != null) this.textColor = next.color;
|
|
412
|
+
}
|
|
413
|
+
textFieldColorRanges.delete(this);
|
|
414
|
+
this.length = this._text.length;
|
|
415
|
+
},
|
|
416
|
+
});
|
|
417
|
+
Object.defineProperty(TextField.prototype, 'htmlText', {
|
|
418
|
+
enumerable: true, configurable: true,
|
|
419
|
+
get: function () { return this._htmlText || ''; },
|
|
420
|
+
set: function (value) {
|
|
421
|
+
this._htmlText = value == null ? '' : String(value);
|
|
422
|
+
this._htmlTextExplicit = true;
|
|
423
|
+
this._htmlCondenseWhite = this.condenseWhite;
|
|
424
|
+
this._swoofAppliedTextColor = null;
|
|
425
|
+
textFieldColorRanges.delete(this);
|
|
426
|
+
var lines = parseHtmlTextLines(this._htmlText, {}, null, this._htmlCondenseWhite);
|
|
427
|
+
this._text = plainTextFromHtmlLines(lines);
|
|
428
|
+
this._swoofHtmlLetterSpacing = uniformHtmlRunProperty(lines, 'letterSpacing');
|
|
429
|
+
this._swoofHtmlKerning = uniformHtmlRunProperty(lines, 'kerning');
|
|
430
|
+
this.length = this._text.length;
|
|
431
|
+
},
|
|
432
|
+
});
|
|
433
|
+
export function setTextColorRange(field, start, end, color) {
|
|
434
|
+
if (start >= end) return;
|
|
435
|
+
var ranges = [];
|
|
436
|
+
for (var range of textFieldColorRanges.get(field) || []) {
|
|
437
|
+
if (range.end <= start || range.start >= end) ranges.push(range);
|
|
438
|
+
else {
|
|
439
|
+
if (range.start < start) ranges.push({ start: range.start, end: start, color: range.color });
|
|
440
|
+
if (range.end > end) ranges.push({ start: end, end: range.end, color: range.color });
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
ranges.push({ start: start, end: end, color: color });
|
|
444
|
+
ranges.sort(function (a, b) { return a.start - b.start; });
|
|
445
|
+
textFieldColorRanges.set(field, ranges);
|
|
446
|
+
}
|
|
447
|
+
export class TextLineMetrics {
|
|
448
|
+
constructor(x, width, height, ascent, descent, leading) {
|
|
449
|
+
this.x = +x || 0;
|
|
450
|
+
this.width = +width || 0;
|
|
451
|
+
this.height = +height || 0;
|
|
452
|
+
this.ascent = +ascent || 0;
|
|
453
|
+
this.descent = +descent || 0;
|
|
454
|
+
this.leading = +leading || 0;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
export function textLineAt(layout, index) {
|
|
459
|
+
var line = layout.lines[index | 0];
|
|
460
|
+
if (!line) throw new RangeError('Line index is out of range.');
|
|
461
|
+
return line;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
export function textParagraphAt(field, charIndex) {
|
|
465
|
+
var index = charIndex | 0, lines = layoutTextField(field).lines;
|
|
466
|
+
if (index < 0 || index > field.length) throw new RangeError('Character index is out of range.');
|
|
467
|
+
if (index === field.length) return lines[lines.length - 1];
|
|
468
|
+
return lines[field.getLineIndexOfChar(index)];
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
export function textRunAdvance(run, index, ctx) {
|
|
472
|
+
if (index <= 0) return 0;
|
|
473
|
+
if (index >= run.text.length) return run.width;
|
|
474
|
+
var addedSpacing = (run.justifiedSpacing || 0) * index;
|
|
475
|
+
var glyphs = run.embeddedGlyphs || run.spacedGlyphs;
|
|
476
|
+
if (glyphs) {
|
|
477
|
+
var advance = glyphs.find(function (glyph) { return glyph.index === index; })?.x;
|
|
478
|
+
return advance == null ? run.width : advance + addedSpacing;
|
|
479
|
+
}
|
|
480
|
+
ctx = ctx || getTextMeasurementContext();
|
|
481
|
+
if (ctx) return measureTextRun(ctx, run.text.slice(0, index), run.style).width + addedSpacing;
|
|
482
|
+
return run.width * Array.from(run.text.slice(0, index)).length / Array.from(run.text).length;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
Object.defineProperties(TextField.prototype, {
|
|
486
|
+
numLines: { get: function () { return layoutTextField(this).lines.length; } },
|
|
487
|
+
textWidth: { get: function () { return layoutTextField(this).textWidth; } },
|
|
488
|
+
textHeight: { get: function () { return layoutTextField(this).textHeight; } },
|
|
489
|
+
scrollH: {
|
|
490
|
+
enumerable: true,
|
|
491
|
+
get: function () {
|
|
492
|
+
var viewport = nativeTextHorizontalViewport(this);
|
|
493
|
+
return viewport ? textHorizontalScroll(this, viewport) : this._scrollH;
|
|
494
|
+
},
|
|
495
|
+
set: function (value) { this._scrollH = value; },
|
|
496
|
+
},
|
|
497
|
+
maxScrollH: { get: function () { return nativeTextHorizontalViewport(this)?.maxScrollH ?? layoutTextField(this).maxScrollH; } },
|
|
498
|
+
maxScrollV: { get: function () { return nativeTextVerticalViewport(this)?.maxScrollV ?? layoutTextField(this).maxScrollV; } },
|
|
499
|
+
bottomScrollV: { get: function () {
|
|
500
|
+
var viewport = nativeTextVerticalViewport(this);
|
|
501
|
+
if (viewport?.lineCount != null) {
|
|
502
|
+
var first = textFirstVisibleLine(this, viewport);
|
|
503
|
+
var top = this._swoofOverlayElement.scrollTop;
|
|
504
|
+
// A newer script row is effective before its native pixel offset is painted.
|
|
505
|
+
if (first + 1 !== viewport.requested) top = first ? viewport.paddingTop + first * viewport.lineHeight : 0;
|
|
506
|
+
if (Number.isFinite(top)) {
|
|
507
|
+
top = Math.max(0, Math.min(viewport.maxTop, top));
|
|
508
|
+
var visibleRows = (top + viewport.clientHeight - viewport.paddingTop) / viewport.lineHeight;
|
|
509
|
+
return Math.max(1, Math.min(viewport.lineCount, Math.ceil(visibleRows - 1e-9)));
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
var layout = layoutTextField(this), bottom = textFirstVisibleLine(this, layout);
|
|
513
|
+
var end = layout.lines[bottom].y + layout.height;
|
|
514
|
+
while (bottom + 1 < layout.lines.length &&
|
|
515
|
+
(!layout.height || layout.lines[bottom + 1].y < end)) bottom++;
|
|
516
|
+
return bottom + 1;
|
|
517
|
+
} },
|
|
518
|
+
});
|
|
519
|
+
|