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,1387 @@
|
|
|
1
|
+
import { DisplayObject, DisplayObjectContainer, displayBoundsInTarget, isStageDescendant } from './display.js';
|
|
2
|
+
import { EventDispatcher } from './events.js';
|
|
3
|
+
import { Point, Rectangle, Vector } from './geom.js';
|
|
4
|
+
import { global } from './host.js';
|
|
5
|
+
import { applySuper } from './inherit.js';
|
|
6
|
+
import { textLineMark } from './marks.js';
|
|
7
|
+
import { asUint } from './system.js';
|
|
8
|
+
|
|
9
|
+
/** flash.text.TextFormatAlign. Values are the strings TextFormat.align stores. */
|
|
10
|
+
export class TextFormatAlign {
|
|
11
|
+
static LEFT = 'left';
|
|
12
|
+
static CENTER = 'center';
|
|
13
|
+
static RIGHT = 'right';
|
|
14
|
+
static JUSTIFY = 'justify';
|
|
15
|
+
static START = 'start';
|
|
16
|
+
static END = 'end';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const registeredFonts = [];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* flash.text.Font. Embedded font classes, such as QWOP's GS1, extend this.
|
|
23
|
+
* Glyph outlines and DefineFont4 faces stay in the movie font lookup; this is
|
|
24
|
+
* the ActionScript class those subclasses and Font.registerFont use.
|
|
25
|
+
*/
|
|
26
|
+
export class Font {
|
|
27
|
+
static registerFont(fontClass) {
|
|
28
|
+
if (typeof fontClass === 'function' && !registeredFonts.includes(fontClass)) registeredFonts.push(fontClass);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static enumerateFonts() {
|
|
32
|
+
return registeredFonts.map(fontClass => new fontClass());
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get fontName() { return this._swoofFontName || ''; }
|
|
36
|
+
get fontStyle() { return this._swoofFontStyle || 'regular'; }
|
|
37
|
+
get fontType() { return this._swoofFontType || 'embedded'; }
|
|
38
|
+
|
|
39
|
+
hasGlyphs(text) {
|
|
40
|
+
text = String(text ?? '');
|
|
41
|
+
const glyphs = this._swoofGlyphs;
|
|
42
|
+
if (!glyphs) return true;
|
|
43
|
+
for (let i = 0; i < text.length; i++) {
|
|
44
|
+
const code = text.codePointAt(i);
|
|
45
|
+
if (!glyphs.has(code)) return false;
|
|
46
|
+
if (code > 0xffff) i++;
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export class TextFormat {
|
|
53
|
+
constructor(font, size, color, bold, italic, underline, url, target, align, leftMargin, rightMargin, indent, leading) {
|
|
54
|
+
this.font = font || null;
|
|
55
|
+
this.size = size == null ? null : size;
|
|
56
|
+
this.color = color == null ? null : color;
|
|
57
|
+
this.bold = bold == null ? null : !!bold;
|
|
58
|
+
this.italic = italic == null ? null : !!italic;
|
|
59
|
+
this.underline = underline == null ? null : !!underline;
|
|
60
|
+
this.url = url || null;
|
|
61
|
+
this.target = target || null;
|
|
62
|
+
this.align = align || null;
|
|
63
|
+
this.leftMargin = leftMargin == null ? null : leftMargin;
|
|
64
|
+
this.rightMargin = rightMargin == null ? null : rightMargin;
|
|
65
|
+
this.indent = indent == null ? null : indent;
|
|
66
|
+
this.leading = leading == null ? null : leading;
|
|
67
|
+
this.letterSpacing = null;
|
|
68
|
+
this.kerning = null;
|
|
69
|
+
this.tabStops = null;
|
|
70
|
+
this.bullet = null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
clone() {
|
|
74
|
+
var t = new TextFormat(this.font, this.size, this.color, this.bold, this.italic, this.underline,
|
|
75
|
+
this.url, this.target, this.align, this.leftMargin, this.rightMargin, this.indent, this.leading);
|
|
76
|
+
t.letterSpacing = this.letterSpacing;
|
|
77
|
+
t.kerning = this.kerning;
|
|
78
|
+
return t;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export class StyleSheet extends EventDispatcher {
|
|
83
|
+
constructor() {
|
|
84
|
+
super();
|
|
85
|
+
this._styles = new Map();
|
|
86
|
+
this._revision = 0;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
get styleNames() { return Array.from(this._styles.keys()); }
|
|
90
|
+
|
|
91
|
+
clear() {
|
|
92
|
+
this._styles.clear();
|
|
93
|
+
this._revision++;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
getStyle(name) {
|
|
97
|
+
var style = this._styles.get(String(name));
|
|
98
|
+
return style ? { ...style } : null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
setStyle(name, style) {
|
|
102
|
+
name = String(name);
|
|
103
|
+
if (style == null) this._styles.delete(name);
|
|
104
|
+
else this._styles.set(name, { ...style });
|
|
105
|
+
this._revision++;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
parseCSS(source) {
|
|
109
|
+
var css = String(source ?? '').replace(/\/\*[\s\S]*?\*\//g, '');
|
|
110
|
+
var rule = /([^{}]+)\{([^{}]*)\}/g, match;
|
|
111
|
+
while ((match = rule.exec(css))) {
|
|
112
|
+
var declarations = Object.create(null);
|
|
113
|
+
for (var part of match[2].split(';')) {
|
|
114
|
+
var property = /^\s*([a-z-]+)\s*:\s*(.*?)\s*$/i.exec(part);
|
|
115
|
+
if (property && property[2]) {
|
|
116
|
+
var name = property[1].toLowerCase().replace(/-([a-z])/g, function (_, c) { return c.toUpperCase(); });
|
|
117
|
+
declarations[name] = property[2];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
for (var selector of match[1].split(',')) {
|
|
121
|
+
selector = selector.trim();
|
|
122
|
+
if (/^\.?[a-z_][\w-]*$/i.test(selector)) {
|
|
123
|
+
this._styles.set(selector, { ...this._styles.get(selector), ...declarations });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
this._revision++;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
transform(style) {
|
|
131
|
+
var format = new TextFormat();
|
|
132
|
+
if (!style) return format;
|
|
133
|
+
if (typeof style.fontFamily === 'string') {
|
|
134
|
+
var font = style.fontFamily.split(',')[0].trim().replace(/^(['"])(.*)\1$/, '$2');
|
|
135
|
+
var deviceFonts = { mono: '_typewriter', 'sans-serif': '_sans', serif: '_serif' };
|
|
136
|
+
format.font = Object.hasOwn(deviceFonts, font) ? deviceFonts[font] : (font || null);
|
|
137
|
+
}
|
|
138
|
+
if (typeof style.color === 'string' && /^#[0-9a-f]{6}$/i.test(style.color)) {
|
|
139
|
+
format.color = parseInt(style.color.slice(1), 16);
|
|
140
|
+
}
|
|
141
|
+
var size = parseFloat(style.fontSize);
|
|
142
|
+
if (Number.isFinite(size) && size > 0) format.size = size;
|
|
143
|
+
if (style.fontWeight === 'bold' || style.fontWeight === 'normal') format.bold = style.fontWeight === 'bold';
|
|
144
|
+
if (style.fontStyle === 'italic' || style.fontStyle === 'normal') format.italic = style.fontStyle === 'italic';
|
|
145
|
+
if (style.textDecoration === 'underline' || style.textDecoration === 'none') format.underline = style.textDecoration === 'underline';
|
|
146
|
+
if (style.kerning === 'true' || style.kerning === 'false') format.kerning = style.kerning === 'true';
|
|
147
|
+
if (['left', 'center', 'right', 'justify'].includes(style.textAlign)) format.align = style.textAlign;
|
|
148
|
+
for (var [property, key] of [['leftMargin', 'marginLeft'], ['rightMargin', 'marginRight'],
|
|
149
|
+
['indent', 'textIndent'], ['leading', 'leading'], ['letterSpacing', 'letterSpacing']]) {
|
|
150
|
+
var value = parseFloat(style[key]);
|
|
151
|
+
if (Number.isFinite(value)) format[property] = value;
|
|
152
|
+
}
|
|
153
|
+
return format;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
Object.defineProperty(StyleSheet, '__swoofQualifiedName', { value: 'flash.text.StyleSheet' });
|
|
157
|
+
|
|
158
|
+
export function textEngineEnum(name, constants) {
|
|
159
|
+
class Enum {}
|
|
160
|
+
Object.defineProperty(Enum, 'name', { value: name, configurable: true });
|
|
161
|
+
for (const [key, value] of Object.entries(constants)) {
|
|
162
|
+
Object.defineProperty(Enum, key, { value, enumerable: true, writable: false, configurable: false });
|
|
163
|
+
}
|
|
164
|
+
return Enum;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export var textEngine = {
|
|
168
|
+
LineJustification: textEngineEnum('LineJustification', { UNJUSTIFIED: 'unjustified', ALL_BUT_LAST: 'allButLast',
|
|
169
|
+
ALL_BUT_MANDATORY_BREAK: 'allButMandatoryBreak', ALL_INCLUDING_LAST: 'allIncludingLast' }),
|
|
170
|
+
JustificationStyle: textEngineEnum('JustificationStyle', { PUSH_IN_KINSOKU: 'pushInKinsoku',
|
|
171
|
+
PUSH_OUT_ONLY: 'pushOutOnly', PRIORITIZE_LEAST_ADJUSTMENT: 'prioritizeLeastAdjustment' }),
|
|
172
|
+
TabAlignment: textEngineEnum('TabAlignment', { START: 'start', CENTER: 'center', END: 'end', DECIMAL: 'decimal' }),
|
|
173
|
+
TextLineValidity: textEngineEnum('TextLineValidity', { VALID: 'valid', INVALID: 'invalid', POSSIBLY_INVALID: 'possiblyInvalid', STATIC: 'static' }),
|
|
174
|
+
TextLineCreationResult: textEngineEnum('TextLineCreationResult', { SUCCESS: 'success', COMPLETE: 'complete',
|
|
175
|
+
EMERGENCY: 'emergency', INSUFFICIENT_WIDTH: 'insufficientWidth' }),
|
|
176
|
+
FontWeight: textEngineEnum('FontWeight', { NORMAL: 'normal', BOLD: 'bold' }),
|
|
177
|
+
FontPosture: textEngineEnum('FontPosture', { NORMAL: 'normal', ITALIC: 'italic' }),
|
|
178
|
+
FontLookup: textEngineEnum('FontLookup', { DEVICE: 'device', EMBEDDED_CFF: 'embeddedCFF' }),
|
|
179
|
+
RenderingMode: textEngineEnum('RenderingMode', { NORMAL: 'normal', CFF: 'cff' }),
|
|
180
|
+
CFFHinting: textEngineEnum('CFFHinting', { NONE: 'none', HORIZONTAL_STEM: 'horizontalStem' }),
|
|
181
|
+
TextRotation: textEngineEnum('TextRotation', { AUTO: 'auto', ROTATE_0: 'rotate0', ROTATE_90: 'rotate90',
|
|
182
|
+
ROTATE_180: 'rotate180', ROTATE_270: 'rotate270' }),
|
|
183
|
+
TextBaseline: textEngineEnum('TextBaseline', { ASCENT: 'ascent', DESCENT: 'descent', ROMAN: 'roman',
|
|
184
|
+
IDEOGRAPHIC_TOP: 'ideographicTop', IDEOGRAPHIC_CENTER: 'ideographicCenter',
|
|
185
|
+
IDEOGRAPHIC_BOTTOM: 'ideographicBottom', USE_DOMINANT_BASELINE: 'useDominantBaseline' }),
|
|
186
|
+
Kerning: textEngineEnum('Kerning', { AUTO: 'auto', ON: 'on', OFF: 'off' }),
|
|
187
|
+
BreakOpportunity: textEngineEnum('BreakOpportunity', { AUTO: 'auto', ALL: 'all', ANY: 'any', NONE: 'none' }),
|
|
188
|
+
DigitCase: textEngineEnum('DigitCase', { DEFAULT: 'default', LINING: 'lining', OLD_STYLE: 'oldStyle' }),
|
|
189
|
+
DigitWidth: textEngineEnum('DigitWidth', { DEFAULT: 'default', PROPORTIONAL: 'proportional', TABULAR: 'tabular' }),
|
|
190
|
+
LigatureLevel: textEngineEnum('LigatureLevel', { NONE: 'none', MINIMUM: 'minimum', COMMON: 'common',
|
|
191
|
+
UNCOMMON: 'uncommon', EXOTIC: 'exotic' }),
|
|
192
|
+
TypographicCase: textEngineEnum('TypographicCase', { DEFAULT: 'default', CAPS: 'caps', CAPS_AND_SMALL_CAPS: 'capsAndSmallCaps',
|
|
193
|
+
SMALL_CAPS: 'smallCaps', LOWERCASE: 'lowercase', UPPERCASE: 'uppercase', TITLE: 'title' }),
|
|
194
|
+
};
|
|
195
|
+
for (var [engineName, engineType] of Object.entries(textEngine)) {
|
|
196
|
+
Object.defineProperty(engineType, '__swoofQualifiedName', { value: 'flash.text.engine.' + engineName });
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function textEngineError(name, message) {
|
|
200
|
+
var error = new Error(message);
|
|
201
|
+
error.name = name;
|
|
202
|
+
return error;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function enumValue(type) {
|
|
206
|
+
var values = Object.values(type);
|
|
207
|
+
return function (value) {
|
|
208
|
+
var name = textEngineString(value);
|
|
209
|
+
if (!values.includes(name)) throw textEngineError('ArgumentError', 'Invalid ' + type.name + ' value');
|
|
210
|
+
return name;
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function textEngineString(value) { return value == null ? null : String(value); }
|
|
215
|
+
export function textEngineFontSize(value) {
|
|
216
|
+
var size = Number(value);
|
|
217
|
+
if (size < 0) throw textEngineError('ArgumentError', 'ElementFormat.fontSize must be non-negative');
|
|
218
|
+
return size;
|
|
219
|
+
}
|
|
220
|
+
export function formatFontDescription(value) {
|
|
221
|
+
if (value == null) return new FontDescription();
|
|
222
|
+
if (!(value instanceof FontDescription)) throw new TypeError('ElementFormat.fontDescription must be a FontDescription');
|
|
223
|
+
return value;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export var fontDescriptionFields = [
|
|
227
|
+
['fontName', '_serif', textEngineString],
|
|
228
|
+
['fontWeight', 'normal', enumValue(textEngine.FontWeight)],
|
|
229
|
+
['fontPosture', 'normal', enumValue(textEngine.FontPosture)],
|
|
230
|
+
['fontLookup', 'device', enumValue(textEngine.FontLookup)],
|
|
231
|
+
['renderingMode', 'cff', enumValue(textEngine.RenderingMode)],
|
|
232
|
+
['cffHinting', 'horizontalStem', enumValue(textEngine.CFFHinting)],
|
|
233
|
+
];
|
|
234
|
+
export var elementFormatFields = [
|
|
235
|
+
['fontDescription', null, formatFontDescription],
|
|
236
|
+
['fontSize', 12, textEngineFontSize],
|
|
237
|
+
['color', 0, asUint],
|
|
238
|
+
['alpha', 1, Number],
|
|
239
|
+
['textRotation', 'auto', enumValue(textEngine.TextRotation)],
|
|
240
|
+
['dominantBaseline', 'roman', enumValue(textEngine.TextBaseline)],
|
|
241
|
+
['alignmentBaseline', 'useDominantBaseline', enumValue(textEngine.TextBaseline)],
|
|
242
|
+
['baselineShift', 0, Number],
|
|
243
|
+
['kerning', 'on', enumValue(textEngine.Kerning)],
|
|
244
|
+
['trackingRight', 0, Number],
|
|
245
|
+
['trackingLeft', 0, Number],
|
|
246
|
+
['locale', 'en', textEngineString],
|
|
247
|
+
['breakOpportunity', 'auto', enumValue(textEngine.BreakOpportunity)],
|
|
248
|
+
['digitCase', 'default', enumValue(textEngine.DigitCase)],
|
|
249
|
+
['digitWidth', 'default', enumValue(textEngine.DigitWidth)],
|
|
250
|
+
['ligatureLevel', 'common', enumValue(textEngine.LigatureLevel)],
|
|
251
|
+
['typographicCase', 'default', enumValue(textEngine.TypographicCase)],
|
|
252
|
+
];
|
|
253
|
+
export var fontDescriptionState = new WeakMap();
|
|
254
|
+
export var elementFormatState = new WeakMap();
|
|
255
|
+
|
|
256
|
+
export function initializeTextEngineValue(receiver, state, fields, args) {
|
|
257
|
+
var values = { locked: false };
|
|
258
|
+
for (var i = 0; i < fields.length; i++) {
|
|
259
|
+
var [name, initial, normalize] = fields[i];
|
|
260
|
+
values[name] = normalize(args[i] === undefined ? initial : args[i]);
|
|
261
|
+
}
|
|
262
|
+
state.set(receiver, values);
|
|
263
|
+
return values;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function defineTextEngineValue(Type, state, fields) {
|
|
267
|
+
for (const [name, , normalize] of [...fields, ['locked', false, Boolean]]) {
|
|
268
|
+
Object.defineProperty(Type.prototype, name, {
|
|
269
|
+
get: function () { return state.get(this)[name]; },
|
|
270
|
+
set: function (value) {
|
|
271
|
+
var values = state.get(this);
|
|
272
|
+
if (values.locked) throw textEngineError('IllegalOperationError', Type.name + ' is locked');
|
|
273
|
+
var next = normalize(value);
|
|
274
|
+
if (name === 'fontDescription') fontDescriptionState.get(next).locked = true;
|
|
275
|
+
values[name] = next;
|
|
276
|
+
},
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export class FontDescription {
|
|
282
|
+
constructor(...args) {
|
|
283
|
+
initializeTextEngineValue(this, fontDescriptionState, fontDescriptionFields, args);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
clone() {
|
|
287
|
+
var values = fontDescriptionState.get(this);
|
|
288
|
+
return new FontDescription(...fontDescriptionFields.map(function ([name]) { return values[name]; }));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
defineTextEngineValue(FontDescription, fontDescriptionState, fontDescriptionFields);
|
|
292
|
+
|
|
293
|
+
export class ElementFormat {
|
|
294
|
+
constructor(...args) {
|
|
295
|
+
var values = initializeTextEngineValue(this, elementFormatState, elementFormatFields, args);
|
|
296
|
+
fontDescriptionState.get(values.fontDescription).locked = true;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
clone() {
|
|
300
|
+
var values = elementFormatState.get(this);
|
|
301
|
+
return new ElementFormat(...elementFormatFields.map(function ([name]) { return values[name]; }));
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
defineTextEngineValue(ElementFormat, elementFormatState, elementFormatFields);
|
|
305
|
+
|
|
306
|
+
export function createMovieFontLookup(assets, loaded) {
|
|
307
|
+
var names = new Map();
|
|
308
|
+
var cffFaces = [];
|
|
309
|
+
for (var asset of assets) {
|
|
310
|
+
if (asset.type !== 'font' || asset.fontVersion !== 4 || !asset.fontName || !asset.family) continue;
|
|
311
|
+
var faces = names.get(asset.fontName);
|
|
312
|
+
if (!faces) names.set(asset.fontName, faces = []);
|
|
313
|
+
faces.push(asset);
|
|
314
|
+
cffFaces.push(asset);
|
|
315
|
+
}
|
|
316
|
+
function ready(face) { return loaded?.[face.id]?.family === face.family; }
|
|
317
|
+
function findCffFont(name, weight, posture, requireReady) {
|
|
318
|
+
return names.get(name)?.find(function (face) {
|
|
319
|
+
return (face.bold ? 'bold' : 'normal') === weight && (face.italic ? 'italic' : 'normal') === posture &&
|
|
320
|
+
(!requireReady || ready(face));
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
var fontWeight = enumValue(textEngine.FontWeight), fontPosture = enumValue(textEngine.FontPosture);
|
|
324
|
+
return {
|
|
325
|
+
findCffFont: findCffFont,
|
|
326
|
+
readyCount: function () {
|
|
327
|
+
return cffFaces.reduce(function (count, face) { return count + Number(ready(face)); }, 0);
|
|
328
|
+
},
|
|
329
|
+
findReadyCffFont: function (name, weight, posture) {
|
|
330
|
+
return findCffFont(name, weight, posture, true)?.family ?? null;
|
|
331
|
+
},
|
|
332
|
+
isFontCompatible: function (name, weight, posture) {
|
|
333
|
+
return Boolean(findCffFont(textEngineString(name), fontWeight(weight), fontPosture(posture)));
|
|
334
|
+
},
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export var noMovieFonts = createMovieFontLookup([]);
|
|
339
|
+
|
|
340
|
+
export var contentElementState = new WeakMap();
|
|
341
|
+
export var normalizeContentRotation = enumValue(textEngine.TextRotation);
|
|
342
|
+
|
|
343
|
+
export class ContentElement {
|
|
344
|
+
constructor(elementFormat, eventMirror, textRotation = 'rotate0') {
|
|
345
|
+
if (new.target === ContentElement) throw textEngineError('ArgumentError', 'ContentElement is abstract');
|
|
346
|
+
contentElementState.set(this, {
|
|
347
|
+
group: null, textBlock: null, format: null, mirror: null, rotation: 'rotate0', text: null, children: null,
|
|
348
|
+
});
|
|
349
|
+
this.userData = null;
|
|
350
|
+
this.textRotation = textRotation;
|
|
351
|
+
this.eventMirror = eventMirror;
|
|
352
|
+
this.elementFormat = elementFormat;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
get elementFormat() { return contentElementState.get(this).format; }
|
|
356
|
+
set elementFormat(format) {
|
|
357
|
+
validateContentFormat(format);
|
|
358
|
+
if (format != null) elementFormatState.get(format).locked = true;
|
|
359
|
+
contentElementState.get(this).format = format ?? null;
|
|
360
|
+
invalidateContentLines(this);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
get eventMirror() { return contentElementState.get(this).mirror; }
|
|
364
|
+
set eventMirror(mirror) {
|
|
365
|
+
if (mirror != null && !(mirror instanceof EventDispatcher)) throw new TypeError('ContentElement.eventMirror must be an EventDispatcher');
|
|
366
|
+
contentElementState.get(this).mirror = mirror ?? null;
|
|
367
|
+
invalidateContentLines(this);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
get groupElement() { return contentElementState.get(this).group; }
|
|
371
|
+
get rawText() { return contentText(this, true); }
|
|
372
|
+
get text() { return contentText(this, false); }
|
|
373
|
+
|
|
374
|
+
get textBlock() {
|
|
375
|
+
var state = contentElementState.get(this);
|
|
376
|
+
while (state.group) state = contentElementState.get(state.group);
|
|
377
|
+
return state.textBlock;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
get textBlockBeginIndex() {
|
|
381
|
+
if (!this.textBlock) return -1;
|
|
382
|
+
var offset = 0;
|
|
383
|
+
for (var child = this, group = child.groupElement; group; child = group, group = group.groupElement) {
|
|
384
|
+
for (var sibling of contentElementState.get(group).children) {
|
|
385
|
+
if (sibling === child) break;
|
|
386
|
+
offset += contentLength(sibling);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return offset;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
get textRotation() { return contentElementState.get(this).rotation; }
|
|
393
|
+
set textRotation(value) {
|
|
394
|
+
var rotation = normalizeContentRotation(value);
|
|
395
|
+
if (rotation === 'auto') throw textEngineError('ArgumentError', 'ContentElement does not support automatic rotation');
|
|
396
|
+
var state = contentElementState.get(this);
|
|
397
|
+
if (rotation !== 'rotate0' && (rotatedContentAncestor(state.group) || state.children?.some(rotatedContent))) {
|
|
398
|
+
throw textEngineError('ArgumentError', 'ContentElement rotations cannot be nested');
|
|
399
|
+
}
|
|
400
|
+
state.rotation = rotation;
|
|
401
|
+
invalidateContentLines(this);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
Object.defineProperty(ContentElement, 'GRAPHIC_ELEMENT', { value: 0xfdef });
|
|
405
|
+
Object.defineProperty(FontDescription, 'isFontCompatible', { value: function (name, weight, posture) {
|
|
406
|
+
var lookup = global.__swoof?.fontLookup || noMovieFonts;
|
|
407
|
+
return lookup.isFontCompatible(name, weight, posture);
|
|
408
|
+
}, configurable: true, writable: true });
|
|
409
|
+
|
|
410
|
+
export function contentText(element, includeGraphics) {
|
|
411
|
+
var state = contentElementState.get(element);
|
|
412
|
+
return state.children ? state.children.map(function (child) { return contentText(child, includeGraphics); }).join('')
|
|
413
|
+
: state.graphicElement && !includeGraphics ? '' : state.text;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
export function contentLength(element) {
|
|
417
|
+
var state = contentElementState.get(element);
|
|
418
|
+
return state.children ? state.children.reduce(function (length, child) { return length + contentLength(child); }, 0)
|
|
419
|
+
: state.text?.length || 0;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
export function rotatedContent(element) {
|
|
423
|
+
var state = contentElementState.get(element);
|
|
424
|
+
return state.rotation !== 'rotate0' || (state.children && state.children.some(rotatedContent));
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
export function rotatedContentAncestor(group) {
|
|
428
|
+
for (var element = group; element; element = contentElementState.get(element).group) {
|
|
429
|
+
if (contentElementState.get(element).rotation !== 'rotate0') return true;
|
|
430
|
+
}
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
export function validateContentFormat(format) {
|
|
435
|
+
if (format != null && !elementFormatState.has(format)) {
|
|
436
|
+
throw new TypeError('ContentElement.elementFormat must be an ElementFormat');
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export function contentRange(begin, end, length) {
|
|
441
|
+
if (begin < 0 || end < begin || end > length) throw new RangeError('ContentElement range is out of bounds');
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
export class TextElement extends ContentElement {
|
|
445
|
+
constructor(text, elementFormat, eventMirror, textRotation) {
|
|
446
|
+
super(elementFormat, eventMirror, textRotation);
|
|
447
|
+
this.text = text;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
get text() { return contentElementState.get(this).text; }
|
|
451
|
+
set text(value) {
|
|
452
|
+
contentElementState.get(this).text = textEngineString(value);
|
|
453
|
+
invalidateContentLines(this);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
replaceText(beginIndex, endIndex, text) {
|
|
457
|
+
var begin = beginIndex | 0, end = endIndex | 0, original = this.text ?? '';
|
|
458
|
+
contentRange(begin, end, original.length);
|
|
459
|
+
this.text = original.slice(0, begin) + (text == null ? '' : String(text)) + original.slice(end);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
export class GraphicElement extends ContentElement {
|
|
464
|
+
constructor(graphic, elementWidth = 15, elementHeight = 15, elementFormat, eventMirror, textRotation) {
|
|
465
|
+
super(elementFormat, eventMirror, textRotation);
|
|
466
|
+
var state = contentElementState.get(this);
|
|
467
|
+
state.graphicElement = true;
|
|
468
|
+
state.text = String.fromCharCode(ContentElement.GRAPHIC_ELEMENT);
|
|
469
|
+
this.elementWidth = elementWidth;
|
|
470
|
+
this.elementHeight = elementHeight;
|
|
471
|
+
this.graphic = graphic;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
get elementWidth() { return contentElementState.get(this).elementWidth; }
|
|
475
|
+
set elementWidth(value) {
|
|
476
|
+
value = Number(value);
|
|
477
|
+
if (!Number.isFinite(value) || value < 0) throw new RangeError('GraphicElement dimensions must be finite and non-negative');
|
|
478
|
+
contentElementState.get(this).elementWidth = value;
|
|
479
|
+
invalidateContentLines(this);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
get elementHeight() { return contentElementState.get(this).elementHeight; }
|
|
483
|
+
set elementHeight(value) {
|
|
484
|
+
value = Number(value);
|
|
485
|
+
if (!Number.isFinite(value) || value < 0) throw new RangeError('GraphicElement dimensions must be finite and non-negative');
|
|
486
|
+
contentElementState.get(this).elementHeight = value;
|
|
487
|
+
invalidateContentLines(this);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
get graphic() { return contentElementState.get(this).graphic; }
|
|
491
|
+
set graphic(value) {
|
|
492
|
+
if (value != null && !(value instanceof DisplayObject)) throw new TypeError('GraphicElement.graphic must be a DisplayObject');
|
|
493
|
+
var state = contentElementState.get(this), placement = state.placement;
|
|
494
|
+
if (placement && (value ?? null) !== state.graphic) placeFteGraphic(placement.line, placement.atom, value ?? null);
|
|
495
|
+
state.graphic = value ?? null;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
export class GroupElement extends ContentElement {
|
|
500
|
+
constructor(elements, elementFormat, eventMirror, textRotation) {
|
|
501
|
+
super(null, eventMirror, textRotation);
|
|
502
|
+
validateContentFormat(elementFormat);
|
|
503
|
+
contentElementState.get(this).children = [];
|
|
504
|
+
this.setElements(elements);
|
|
505
|
+
this.elementFormat = elementFormat;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
get elementCount() { return contentElementState.get(this).children.length; }
|
|
509
|
+
|
|
510
|
+
getElementAt(index) {
|
|
511
|
+
index |= 0;
|
|
512
|
+
var children = contentElementState.get(this).children;
|
|
513
|
+
contentRange(index, index + 1, children.length);
|
|
514
|
+
return children[index];
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
getElementIndex(element) { return contentElementState.get(this).children.indexOf(element); }
|
|
518
|
+
|
|
519
|
+
getElementAtCharIndex(charIndex) {
|
|
520
|
+
var remaining = charIndex | 0;
|
|
521
|
+
if (remaining >= 0) {
|
|
522
|
+
for (var child of contentElementState.get(this).children) {
|
|
523
|
+
var length = contentLength(child);
|
|
524
|
+
if (remaining < length) return child;
|
|
525
|
+
remaining -= length;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
throw new RangeError('GroupElement character index is out of bounds');
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
setElements(elements) {
|
|
532
|
+
replaceGroupChildren(this, 0, this.elementCount, validateGroupChildren(this, elements));
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
replaceElements(beginIndex, endIndex, elements) {
|
|
536
|
+
var begin = beginIndex | 0, end = endIndex | 0;
|
|
537
|
+
contentRange(begin, end, this.elementCount);
|
|
538
|
+
var removed = replaceGroupChildren(this, begin, end, validateGroupChildren(this, elements));
|
|
539
|
+
var result = new Vector(removed.length, false);
|
|
540
|
+
for (var i = 0; i < removed.length; i++) result[i] = removed[i];
|
|
541
|
+
Object.defineProperties(result, {
|
|
542
|
+
__swoofVectorType: { value: 'Object' },
|
|
543
|
+
__swoofVectorTypeName: { value: ContentElement.__swoofQualifiedName },
|
|
544
|
+
__swoofVectorElementClass: { value: ContentElement },
|
|
545
|
+
});
|
|
546
|
+
return result;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
groupElements(beginIndex, endIndex) {
|
|
550
|
+
var begin = beginIndex | 0, end = endIndex | 0;
|
|
551
|
+
contentRange(begin, end, this.elementCount);
|
|
552
|
+
var group = new GroupElement();
|
|
553
|
+
var grouped = replaceGroupChildren(this, begin, end, [group]);
|
|
554
|
+
replaceGroupChildren(group, 0, 0, grouped);
|
|
555
|
+
return group;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
ungroupElements(groupIndex) {
|
|
559
|
+
groupIndex |= 0;
|
|
560
|
+
var group = this.getElementAt(groupIndex);
|
|
561
|
+
if (!(group instanceof GroupElement)) throw textEngineError('ArgumentError', 'Expected a GroupElement');
|
|
562
|
+
var elements = replaceGroupChildren(group, 0, group.elementCount, []);
|
|
563
|
+
replaceGroupChildren(this, groupIndex, groupIndex + 1, elements);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
splitTextElement(elementIndex, splitIndex) {
|
|
567
|
+
var index = elementIndex | 0, split = splitIndex | 0;
|
|
568
|
+
var element = this.getElementAt(index);
|
|
569
|
+
if (!(element instanceof TextElement)) throw textEngineError('ArgumentError', 'Expected a TextElement');
|
|
570
|
+
var text = element.text ?? '';
|
|
571
|
+
contentRange(split, split, text.length);
|
|
572
|
+
var latter = new TextElement(text.slice(split), element.elementFormat, element.eventMirror, element.textRotation);
|
|
573
|
+
element.text = text.slice(0, split);
|
|
574
|
+
replaceGroupChildren(this, index + 1, index + 1, [latter]);
|
|
575
|
+
return latter;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
mergeTextElements(beginIndex, endIndex) {
|
|
579
|
+
var begin = beginIndex | 0, end = endIndex | 0;
|
|
580
|
+
contentRange(begin, end, this.elementCount);
|
|
581
|
+
if (begin === end) throw new RangeError('No TextElements to merge');
|
|
582
|
+
var elements = contentElementState.get(this).children.slice(begin, end);
|
|
583
|
+
if (elements.some(function (element) { return !(element instanceof TextElement); })) {
|
|
584
|
+
throw textEngineError('ArgumentError', 'The range must contain only TextElements');
|
|
585
|
+
}
|
|
586
|
+
if (elements.length > 1) {
|
|
587
|
+
elements[0].text = elements.map(function (element) { return element.text; }).join('');
|
|
588
|
+
replaceGroupChildren(this, begin + 1, end, []);
|
|
589
|
+
}
|
|
590
|
+
return elements[0];
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
export function validateGroupChildren(group, elements) {
|
|
595
|
+
if (elements == null) return [];
|
|
596
|
+
if (!Array.isArray(elements)) throw new TypeError('GroupElement elements must be a Vector');
|
|
597
|
+
var children = Array.from(elements), seen = new Set();
|
|
598
|
+
var rotatedAncestor = rotatedContentAncestor(group);
|
|
599
|
+
for (var child of children) {
|
|
600
|
+
var state = contentElementState.get(child);
|
|
601
|
+
if (!state || !(child instanceof TextElement || child instanceof GroupElement || child instanceof GraphicElement) ||
|
|
602
|
+
state.group || state.textBlock || seen.has(child)) {
|
|
603
|
+
throw textEngineError('ArgumentError', 'GroupElement requires distinct, unowned content elements');
|
|
604
|
+
}
|
|
605
|
+
for (var parent = group; parent; parent = contentElementState.get(parent).group) {
|
|
606
|
+
if (parent === child) throw textEngineError('ArgumentError', 'GroupElement cannot contain itself or an ancestor');
|
|
607
|
+
}
|
|
608
|
+
if (rotatedAncestor && rotatedContent(child)) throw textEngineError('ArgumentError', 'ContentElement rotations cannot be nested');
|
|
609
|
+
seen.add(child);
|
|
610
|
+
}
|
|
611
|
+
return children;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
export function replaceGroupChildren(group, begin, end, children) {
|
|
615
|
+
var state = contentElementState.get(group), previous = state.children;
|
|
616
|
+
var removed = previous.slice(begin, end);
|
|
617
|
+
state.children = previous.slice(0, begin).concat(children, previous.slice(end));
|
|
618
|
+
for (var element of removed) contentElementState.get(element).group = null;
|
|
619
|
+
for (var element of children) contentElementState.get(element).group = group;
|
|
620
|
+
invalidateContentLines(group);
|
|
621
|
+
return removed;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
export var textBlockState = new WeakMap();
|
|
625
|
+
export var textLineState = new WeakMap();
|
|
626
|
+
export var tabStopState = new WeakMap();
|
|
627
|
+
export var textJustifierState = new WeakMap();
|
|
628
|
+
export var normalizeTabAlignment = enumValue(textEngine.TabAlignment);
|
|
629
|
+
export var normalizeLineJustification = enumValue(textEngine.LineJustification);
|
|
630
|
+
export var normalizeJustificationStyle = enumValue(textEngine.JustificationStyle);
|
|
631
|
+
export var DEFAULT_FTE_TAB_INTERVAL = 36; // Half an inch at 20 twips per pixel.
|
|
632
|
+
export var fteMeasureContext;
|
|
633
|
+
export var fteGraphemeSegmenter;
|
|
634
|
+
|
|
635
|
+
export function justifierLocale(value) {
|
|
636
|
+
var locale = textEngineString(value);
|
|
637
|
+
if (!locale || locale.length < 2) throw textEngineError('ArgumentError', 'Invalid TextJustifier locale');
|
|
638
|
+
return locale;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
export class TextJustifier {
|
|
642
|
+
constructor(locale, lineJustification) {
|
|
643
|
+
if (new.target === TextJustifier) throw textEngineError('ArgumentError', 'TextJustifier is abstract');
|
|
644
|
+
textJustifierState.set(this, { locale: justifierLocale(locale), lineJustification: normalizeLineJustification(lineJustification) });
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
get locale() { return textJustifierState.get(this).locale; }
|
|
648
|
+
get lineJustification() { return textJustifierState.get(this).lineJustification; }
|
|
649
|
+
set lineJustification(value) { textJustifierState.get(this).lineJustification = normalizeLineJustification(value); }
|
|
650
|
+
|
|
651
|
+
static getJustifierForLocale(value) {
|
|
652
|
+
var locale = justifierLocale(value);
|
|
653
|
+
return /^(ja|ko|zh)(?:$|[-_])/i.test(locale) ? new EastAsianJustifier(locale) : new SpaceJustifier(locale);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
export class SpaceJustifier extends TextJustifier {
|
|
658
|
+
constructor(locale = 'en', lineJustification = 'unjustified', letterSpacing = false) {
|
|
659
|
+
super(locale, lineJustification);
|
|
660
|
+
Object.assign(textJustifierState.get(this), { letterSpacing: Boolean(letterSpacing),
|
|
661
|
+
minimumSpacing: 0.5, optimumSpacing: 1, maximumSpacing: 1.5 });
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
get letterSpacing() { return textJustifierState.get(this).letterSpacing; }
|
|
665
|
+
set letterSpacing(value) { textJustifierState.get(this).letterSpacing = Boolean(value); }
|
|
666
|
+
|
|
667
|
+
get minimumSpacing() { return textJustifierState.get(this).minimumSpacing; }
|
|
668
|
+
set minimumSpacing(value) {
|
|
669
|
+
var state = textJustifierState.get(this);
|
|
670
|
+
value = Number(value);
|
|
671
|
+
if (!Number.isFinite(value) || value < 0 || value > state.optimumSpacing) {
|
|
672
|
+
throw textEngineError('ArgumentError', 'Invalid SpaceJustifier minimumSpacing');
|
|
673
|
+
}
|
|
674
|
+
state.minimumSpacing = value;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
get optimumSpacing() { return textJustifierState.get(this).optimumSpacing; }
|
|
678
|
+
set optimumSpacing(value) {
|
|
679
|
+
var state = textJustifierState.get(this);
|
|
680
|
+
value = Number(value);
|
|
681
|
+
if (!Number.isFinite(value) || value < state.minimumSpacing || value > state.maximumSpacing) {
|
|
682
|
+
throw textEngineError('ArgumentError', 'Invalid SpaceJustifier optimumSpacing');
|
|
683
|
+
}
|
|
684
|
+
state.optimumSpacing = value;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
get maximumSpacing() { return textJustifierState.get(this).maximumSpacing; }
|
|
688
|
+
set maximumSpacing(value) {
|
|
689
|
+
var state = textJustifierState.get(this);
|
|
690
|
+
value = Number(value);
|
|
691
|
+
if (!Number.isFinite(value) || value < state.optimumSpacing || value > Infinity) {
|
|
692
|
+
throw textEngineError('ArgumentError', 'Invalid SpaceJustifier maximumSpacing');
|
|
693
|
+
}
|
|
694
|
+
state.maximumSpacing = value;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
clone() {
|
|
698
|
+
var copy = new SpaceJustifier(this.locale, this.lineJustification, this.letterSpacing);
|
|
699
|
+
Object.assign(textJustifierState.get(copy), textJustifierState.get(this));
|
|
700
|
+
return copy;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
export class EastAsianJustifier extends TextJustifier {
|
|
705
|
+
constructor(locale = 'ja', lineJustification = 'allButLast', justificationStyle = 'pushInKinsoku') {
|
|
706
|
+
super(locale, lineJustification);
|
|
707
|
+
this.justificationStyle = justificationStyle;
|
|
708
|
+
this.composeTrailingIdeographicSpaces = false;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
get justificationStyle() { return textJustifierState.get(this).justificationStyle; }
|
|
712
|
+
set justificationStyle(value) { textJustifierState.get(this).justificationStyle = normalizeJustificationStyle(value); }
|
|
713
|
+
|
|
714
|
+
get composeTrailingIdeographicSpaces() { return textJustifierState.get(this).composeTrailingIdeographicSpaces; }
|
|
715
|
+
set composeTrailingIdeographicSpaces(value) { textJustifierState.get(this).composeTrailingIdeographicSpaces = Boolean(value); }
|
|
716
|
+
|
|
717
|
+
clone() {
|
|
718
|
+
var copy = new EastAsianJustifier(this.locale, this.lineJustification, this.justificationStyle);
|
|
719
|
+
copy.composeTrailingIdeographicSpaces = this.composeTrailingIdeographicSpaces;
|
|
720
|
+
return copy;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
export class TabStop {
|
|
725
|
+
constructor(alignment = 'start', position = 0, decimalAlignmentToken = '') {
|
|
726
|
+
tabStopState.set(this, {});
|
|
727
|
+
this.alignment = alignment;
|
|
728
|
+
this.position = position;
|
|
729
|
+
this.decimalAlignmentToken = decimalAlignmentToken;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
get alignment() { return tabStopState.get(this).alignment; }
|
|
733
|
+
set alignment(value) { tabStopState.get(this).alignment = normalizeTabAlignment(value); }
|
|
734
|
+
|
|
735
|
+
get position() { return tabStopState.get(this).position; }
|
|
736
|
+
set position(value) {
|
|
737
|
+
value = Number(value);
|
|
738
|
+
if (!Number.isFinite(value) || value < 0) throw textEngineError('ArgumentError', 'TabStop position must be finite and non-negative');
|
|
739
|
+
tabStopState.get(this).position = value;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
get decimalAlignmentToken() { return tabStopState.get(this).decimalAlignmentToken; }
|
|
743
|
+
set decimalAlignmentToken(value) { tabStopState.get(this).decimalAlignmentToken = textEngineString(value); }
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
export function copyTabStops(stops) {
|
|
747
|
+
if (stops == null) return null;
|
|
748
|
+
if (!Array.isArray(stops)) throw new TypeError('TextBlock.tabStops must be a Vector');
|
|
749
|
+
var copy = new Vector(stops.length, false);
|
|
750
|
+
for (var i = 0; i < stops.length; i++) {
|
|
751
|
+
var stop = tabStopState.get(stops[i]);
|
|
752
|
+
if (!stop || (i && stop.position <= copy[i - 1].position)) {
|
|
753
|
+
throw textEngineError('ArgumentError', 'TextBlock.tabStops must be sorted by increasing position');
|
|
754
|
+
}
|
|
755
|
+
copy[i] = new TabStop(stop.alignment, stop.position, stop.decimalAlignmentToken);
|
|
756
|
+
}
|
|
757
|
+
Object.defineProperties(copy, {
|
|
758
|
+
__swoofVectorType: { value: 'Object' },
|
|
759
|
+
__swoofVectorTypeName: { value: TabStop.__swoofQualifiedName },
|
|
760
|
+
__swoofVectorElementClass: { value: TabStop },
|
|
761
|
+
});
|
|
762
|
+
return copy;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
export function invalidateContentLines(element) {
|
|
766
|
+
var block = element.textBlock;
|
|
767
|
+
if (!block) return;
|
|
768
|
+
var state = textBlockState.get(block);
|
|
769
|
+
state.atoms = null;
|
|
770
|
+
invalidateTextLines(state.lines, 0);
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
export function invalidateTextLines(lines, start) {
|
|
774
|
+
for (var i = start; i < lines.length; i++) textLineState.get(lines[i]).validity = 'invalid';
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
export class TextBlock {
|
|
778
|
+
constructor(content, tabStops, textJustifier, lineRotation = 'rotate0', baselineZero = 'roman', bidiLevel = 0,
|
|
779
|
+
applyNonLinearFontScaling = true, baselineFontDescription = null, baselineFontSize = 12) {
|
|
780
|
+
textBlockState.set(this, { content: null, lines: [], atoms: null, result: null, fontLookup: global.__swoof?.fontLookup });
|
|
781
|
+
this.tabStops = tabStops ?? null;
|
|
782
|
+
this.textJustifier = textJustifier ?? new SpaceJustifier();
|
|
783
|
+
this.lineRotation = normalizeContentRotation(lineRotation);
|
|
784
|
+
this.baselineZero = baselineZero;
|
|
785
|
+
this.bidiLevel = bidiLevel;
|
|
786
|
+
this.applyNonLinearFontScaling = Boolean(applyNonLinearFontScaling);
|
|
787
|
+
this.baselineFontDescription = baselineFontDescription;
|
|
788
|
+
this.baselineFontSize = Number(baselineFontSize);
|
|
789
|
+
this.userData = null;
|
|
790
|
+
this.content = content;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
get bidiLevel() { return textBlockState.get(this).bidiLevel; }
|
|
794
|
+
set bidiLevel(value) {
|
|
795
|
+
value |= 0;
|
|
796
|
+
if (value < 0) throw textEngineError('ArgumentError', 'TextBlock bidiLevel must be non-negative');
|
|
797
|
+
var state = textBlockState.get(this);
|
|
798
|
+
if (state.bidiLevel === value) return;
|
|
799
|
+
state.bidiLevel = value;
|
|
800
|
+
state.atoms = null;
|
|
801
|
+
invalidateTextLines(state.lines, 0);
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
get content() { return textBlockState.get(this).content; }
|
|
805
|
+
set content(content) {
|
|
806
|
+
var state = textBlockState.get(this), previous = state.content;
|
|
807
|
+
if ((content ?? null) === previous) return;
|
|
808
|
+
if (content != null) {
|
|
809
|
+
var element = contentElementState.get(content);
|
|
810
|
+
if (!element || element.group || element.textBlock) throw textEngineError('ArgumentError', 'TextBlock requires unowned content');
|
|
811
|
+
}
|
|
812
|
+
if (previous) contentElementState.get(previous).textBlock = null;
|
|
813
|
+
if (content != null) contentElementState.get(content).textBlock = this;
|
|
814
|
+
state.content = content ?? null;
|
|
815
|
+
state.atoms = null;
|
|
816
|
+
invalidateTextLines(state.lines, 0);
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
get tabStops() { return copyTabStops(textBlockState.get(this).tabStops); }
|
|
820
|
+
set tabStops(stops) {
|
|
821
|
+
var state = textBlockState.get(this);
|
|
822
|
+
state.tabStops = copyTabStops(stops);
|
|
823
|
+
invalidateTextLines(state.lines, 0);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
get textJustifier() { return textBlockState.get(this).textJustifier.clone(); }
|
|
827
|
+
set textJustifier(value) {
|
|
828
|
+
if (!(value instanceof SpaceJustifier || value instanceof EastAsianJustifier) || !textJustifierState.has(value)) {
|
|
829
|
+
throw textEngineError('ArgumentError', 'Expected a supported TextJustifier');
|
|
830
|
+
}
|
|
831
|
+
var state = textBlockState.get(this);
|
|
832
|
+
state.textJustifier = value.clone();
|
|
833
|
+
invalidateTextLines(state.lines, 0);
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
get firstLine() { return textBlockState.get(this).lines[0] ?? null; }
|
|
837
|
+
get lastLine() { return textBlockState.get(this).lines.at(-1) ?? null; }
|
|
838
|
+
get firstInvalidLine() {
|
|
839
|
+
return textBlockState.get(this).lines.find(function (line) { return line.validity !== 'valid'; }) ?? null;
|
|
840
|
+
}
|
|
841
|
+
get textLineCreationResult() { return textBlockState.get(this).result; }
|
|
842
|
+
|
|
843
|
+
createTextLine(previousLine = null, width = TextLine.MAX_LINE_WIDTH, lineOffset = 0, fitSomething = false) {
|
|
844
|
+
return composeTextLine(this, null, previousLine, width, lineOffset, fitSomething);
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
recreateTextLine(line, previousLine = null, width = TextLine.MAX_LINE_WIDTH, lineOffset = 0, fitSomething = false) {
|
|
848
|
+
if (!textLineState.has(line) || line.validity === 'static') throw textEngineError('ArgumentError', 'Expected a reusable TextLine');
|
|
849
|
+
return composeTextLine(this, line, previousLine, width, lineOffset, fitSomething);
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
releaseLines(first, last) {
|
|
853
|
+
var lines = textBlockState.get(this).lines;
|
|
854
|
+
var start = lines.indexOf(first), end = lines.indexOf(last);
|
|
855
|
+
if (start < 0 || end < start) throw textEngineError('ArgumentError', 'Expected an owned TextLine range');
|
|
856
|
+
detachTextLines(this, start, end - start + 1);
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
releaseLineCreationData() { textBlockState.get(this).atoms = null; }
|
|
860
|
+
|
|
861
|
+
getTextLineAtCharIndex(index) {
|
|
862
|
+
index |= 0;
|
|
863
|
+
var state = textBlockState.get(this);
|
|
864
|
+
contentRange(index, index + 1, state.content ? contentLength(state.content) : 0);
|
|
865
|
+
return state.lines.find(function (line) {
|
|
866
|
+
return index >= line.textBlockBeginIndex && index < line.textBlockBeginIndex + line.rawTextLength;
|
|
867
|
+
}) ?? null;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
export function createMovieTextBlock(fontLookup) {
|
|
872
|
+
class MovieTextBlock extends TextBlock {
|
|
873
|
+
constructor(...args) {
|
|
874
|
+
super(...args);
|
|
875
|
+
textBlockState.get(this).fontLookup = fontLookup;
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
Object.defineProperty(MovieTextBlock, Symbol.hasInstance, { value: function (value) {
|
|
879
|
+
return Function.prototype[Symbol.hasInstance].call(this === MovieTextBlock ? TextBlock : this, value);
|
|
880
|
+
} });
|
|
881
|
+
MovieTextBlock.__swoofQualifiedName = TextBlock.__swoofQualifiedName;
|
|
882
|
+
return MovieTextBlock;
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
export class TextLine extends DisplayObjectContainer {
|
|
886
|
+
constructor() { throw textEngineError('ArgumentError', 'TextLine must be created by TextBlock'); }
|
|
887
|
+
|
|
888
|
+
get textBlock() { return textLineState.get(this).textBlock; }
|
|
889
|
+
get previousLine() { return textLineState.get(this).previousLine; }
|
|
890
|
+
get nextLine() { return textLineState.get(this).nextLine; }
|
|
891
|
+
get textBlockBeginIndex() { return textLineState.get(this).textBlockBeginIndex; }
|
|
892
|
+
get rawTextLength() { return textLineState.get(this).rawTextLength; }
|
|
893
|
+
get textWidth() { return textLineState.get(this).textWidth; }
|
|
894
|
+
get textHeight() { return textLineState.get(this).textHeight; }
|
|
895
|
+
get ascent() { return textLineState.get(this).ascent; }
|
|
896
|
+
get descent() { return textLineState.get(this).descent; }
|
|
897
|
+
get specifiedWidth() { return textLineState.get(this).specifiedWidth; }
|
|
898
|
+
get hasGraphicElement() { return textLineState.get(this).hasGraphicElement; }
|
|
899
|
+
get hasTabs() { return textLineState.get(this).hasTabs; }
|
|
900
|
+
get unjustifiedTextWidth() { return textLineState.get(this).unjustifiedTextWidth; }
|
|
901
|
+
get totalAscent() { return textLineState.get(this).totalAscent; }
|
|
902
|
+
get totalDescent() { return textLineState.get(this).totalDescent; }
|
|
903
|
+
get totalHeight() { return textLineState.get(this).totalHeight; }
|
|
904
|
+
|
|
905
|
+
get atomCount() {
|
|
906
|
+
var state = textLineState.get(this);
|
|
907
|
+
if (state.validity === 'static') throw new Error('TextLine is static');
|
|
908
|
+
return state.atomCount;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
get validity() { return textLineState.get(this).validity; }
|
|
912
|
+
set validity(value) {
|
|
913
|
+
value = textEngineString(value);
|
|
914
|
+
if (value === 'possiblyInvalid') throw textEngineError('ArgumentError', 'Cannot set possiblyInvalid');
|
|
915
|
+
var state = textLineState.get(this);
|
|
916
|
+
if (state.validity === 'static') throw textEngineError('ArgumentError', 'TextLine is static');
|
|
917
|
+
if (value === 'static' && state.textBlock) state.textBlock.releaseLines(this, this);
|
|
918
|
+
state.validity = value;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
getAtomBounds(index) { return fteAtomBounds(fteAtom(this, index)); }
|
|
922
|
+
getAtomCenter(index) {
|
|
923
|
+
var atom = fteAtom(this, index);
|
|
924
|
+
return atom.x + atom.width / 2;
|
|
925
|
+
}
|
|
926
|
+
getAtomTextBlockBeginIndex(index) { return fteAtom(this, index).begin; }
|
|
927
|
+
getAtomTextBlockEndIndex(index) { return fteAtom(this, index).end; }
|
|
928
|
+
getAtomBidiLevel(index) { return fteAtom(this, index).level; }
|
|
929
|
+
getAtomGraphic(index) {
|
|
930
|
+
var graphic = fteAtom(this, index).graphic;
|
|
931
|
+
return graphic?.parent === this ? graphic : null;
|
|
932
|
+
}
|
|
933
|
+
getAtomIndexAtCharIndex(index) {
|
|
934
|
+
index |= 0;
|
|
935
|
+
var state = textLineState.get(this);
|
|
936
|
+
if (state.validity === 'static') throw new Error('TextLine is static');
|
|
937
|
+
return state.atoms.findIndex(function (atom) { return index >= atom.begin && index < atom.end; });
|
|
938
|
+
}
|
|
939
|
+
getAtomIndexAtPoint(stageX, stageY) {
|
|
940
|
+
var state = textLineState.get(this);
|
|
941
|
+
if (state.validity === 'static') throw new Error('TextLine is static');
|
|
942
|
+
var point = this.globalToLocal(new Point(stageX, stageY));
|
|
943
|
+
return state.atoms.findIndex(function (atom) {
|
|
944
|
+
return point.x >= atom.x && point.x < atom.x + atom.width && fteAtomBounds(atom).containsPoint(point);
|
|
945
|
+
});
|
|
946
|
+
}
|
|
947
|
+
getBounds(target) {
|
|
948
|
+
if (this._scrollRect) return displayBoundsInTarget(this, target, true);
|
|
949
|
+
return new Rectangle(target === this ? 0 : this.x, (target === this ? 0 : this.y) - this.ascent, this.width, this.height);
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
TextLine.prototype[textLineMark] = true;
|
|
953
|
+
Object.defineProperty(TextLine, 'MAX_LINE_WIDTH', { value: 1000000 });
|
|
954
|
+
|
|
955
|
+
var textEngineTypes = {
|
|
956
|
+
FontDescription: FontDescription, ElementFormat: ElementFormat,
|
|
957
|
+
ContentElement: ContentElement, TextElement: TextElement, GroupElement: GroupElement, GraphicElement: GraphicElement,
|
|
958
|
+
TextBlock: TextBlock, TextLine: TextLine, TabStop: TabStop,
|
|
959
|
+
TextJustifier: TextJustifier, SpaceJustifier: SpaceJustifier, EastAsianJustifier: EastAsianJustifier,
|
|
960
|
+
};
|
|
961
|
+
for (var [engineName, engineType] of Object.entries(textEngineTypes)) {
|
|
962
|
+
textEngine[engineName] = engineType;
|
|
963
|
+
Object.defineProperty(engineType, '__swoofQualifiedName', { value: 'flash.text.engine.' + engineName });
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
export function fteAtom(line, index) {
|
|
967
|
+
index |= 0;
|
|
968
|
+
var state = textLineState.get(line);
|
|
969
|
+
if (state.validity === 'static') throw new Error('TextLine is static');
|
|
970
|
+
contentRange(index, index + 1, state.atomCount);
|
|
971
|
+
return state.atoms[index];
|
|
972
|
+
}
|
|
973
|
+
export function fteAtomBounds(atom) {
|
|
974
|
+
if (atom.graphicTop != null) return new Rectangle(atom.x, atom.graphicTop, atom.width, atom.graphicHeight);
|
|
975
|
+
return new Rectangle(atom.x, -atom.ascent, atom.width, atom.ascent + atom.descent);
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
export function flashDeviceFontAlias(name) {
|
|
979
|
+
if (name === '_serif') return 'serif';
|
|
980
|
+
if (name === '_sans') return 'sans-serif';
|
|
981
|
+
if (name === '_typewriter') return 'monospace';
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
export function fteFont(format, fontLookup) {
|
|
985
|
+
var description = format?.fontDescription, size = format?.fontSize ?? 12;
|
|
986
|
+
var fontNames = (description?.fontName || '_serif').split(',').map(function (name) { return name.trim(); });
|
|
987
|
+
var names = fontNames.map(function (name) {
|
|
988
|
+
return flashDeviceFontAlias(name) || JSON.stringify(name);
|
|
989
|
+
});
|
|
990
|
+
if (description?.fontLookup === 'embeddedCFF' && fontLookup) {
|
|
991
|
+
for (var name of fontNames) {
|
|
992
|
+
var family = fontLookup.findReadyCffFont(name, description.fontWeight, description.fontPosture);
|
|
993
|
+
if (!family) continue;
|
|
994
|
+
names.unshift(JSON.stringify(family));
|
|
995
|
+
break;
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
return (description?.fontPosture === 'italic' ? 'italic ' : '') +
|
|
999
|
+
(description?.fontWeight === 'bold' ? 'bold ' : '') + size + 'px ' + names.join(',');
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
export function collectFteAtoms(element, atoms, begin, fontLookup) {
|
|
1003
|
+
var state = contentElementState.get(element);
|
|
1004
|
+
if (state.children) {
|
|
1005
|
+
for (var child of state.children) begin = collectFteAtoms(child, atoms, begin, fontLookup);
|
|
1006
|
+
return begin;
|
|
1007
|
+
}
|
|
1008
|
+
if (state.graphicElement) {
|
|
1009
|
+
var height = state.elementHeight, baselineShift = state.format?.baselineShift || 0;
|
|
1010
|
+
atoms.push({ begin: begin, end: begin + 1, text: '', graphicElement: element, width: state.elementWidth,
|
|
1011
|
+
graphicAlpha: Math.max(0, Math.min(1, state.format?.alpha ?? 1)),
|
|
1012
|
+
graphicTop: baselineShift - height, graphicHeight: height,
|
|
1013
|
+
ascent: Math.max(0, height - baselineShift), descent: Math.max(0, baselineShift),
|
|
1014
|
+
breakAfter: state.format?.breakOpportunity !== 'none' });
|
|
1015
|
+
return begin + 1;
|
|
1016
|
+
}
|
|
1017
|
+
var format = state.format, size = format?.fontSize ?? 12, font = fteFont(format, fontLookup);
|
|
1018
|
+
if (fteMeasureContext === undefined) fteMeasureContext = global.document?.createElement('canvas')?.getContext('2d') || null;
|
|
1019
|
+
if (fteMeasureContext) fteMeasureContext.font = font;
|
|
1020
|
+
var textValue = state.text || '';
|
|
1021
|
+
var parts;
|
|
1022
|
+
if (/[^\x00-\x7f]/u.test(textValue) && typeof global.Intl?.Segmenter === 'function') {
|
|
1023
|
+
if (!fteGraphemeSegmenter) fteGraphemeSegmenter = new global.Intl.Segmenter(undefined, { granularity: 'grapheme' });
|
|
1024
|
+
parts = Array.from(fteGraphemeSegmenter.segment(textValue), function (entry) { return entry.segment; });
|
|
1025
|
+
} else parts = textValue.match(/\r\n|[\s\S]/gu) || [];
|
|
1026
|
+
for (var text of parts) {
|
|
1027
|
+
var newline = text === '\r\n' || text === '\n' || text === '\r';
|
|
1028
|
+
var tab = text === '\t';
|
|
1029
|
+
var invisible = newline || tab || text === '\u200b';
|
|
1030
|
+
var measured = invisible ? null : fteMeasureContext?.measureText(text);
|
|
1031
|
+
var width = invisible ? 0 : Math.max(0, (measured?.width ?? size * 0.6) + (format?.trackingLeft || 0) + (format?.trackingRight || 0));
|
|
1032
|
+
var shift = format?.baselineShift || 0;
|
|
1033
|
+
atoms.push({ begin: begin, end: begin + text.length, text: invisible ? '' : text, newline: newline, tab: tab,
|
|
1034
|
+
tracking: Boolean(format?.trackingLeft || format?.trackingRight),
|
|
1035
|
+
breakAfter: format?.breakOpportunity === 'all' || format?.breakOpportunity === 'any' ||
|
|
1036
|
+
format?.breakOpportunity !== 'none' && /[\s\u200b-]/u.test(text),
|
|
1037
|
+
width: width, font: font, alpha: format?.alpha ?? 1, color: format?.color ?? 0, shift: shift,
|
|
1038
|
+
ascent: Math.max(0, (measured?.fontBoundingBoxAscent ?? size * 0.8) - shift),
|
|
1039
|
+
descent: Math.max(0, (measured?.fontBoundingBoxDescent ?? size * 0.2) + shift),
|
|
1040
|
+
});
|
|
1041
|
+
begin += text.length;
|
|
1042
|
+
}
|
|
1043
|
+
return begin;
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
export function fteBidiType(atom) {
|
|
1047
|
+
if (/\p{Number}/u.test(atom.text)) return 'number';
|
|
1048
|
+
if (!/\p{Letter}/u.test(atom.text)) return 'neutral';
|
|
1049
|
+
return /[\p{Script=Arabic}\p{Script=Hebrew}]/u.test(atom.text) ? 'rtl' : 'ltr';
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
export function fteShapingMatch(left, right) {
|
|
1053
|
+
return right && left.level === right.level && left.font === right.font && left.color === right.color &&
|
|
1054
|
+
left.alpha === right.alpha && left.shift === right.shift && !left.tracking && !right.tracking && left.text && right.text;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
export function resolveFteBidi(atoms, base) {
|
|
1058
|
+
var baseDirection = base & 1 ? 'rtl' : 'ltr';
|
|
1059
|
+
var previous = baseDirection;
|
|
1060
|
+
for (var atom of atoms) {
|
|
1061
|
+
var type = fteBidiType(atom);
|
|
1062
|
+
if (atom.newline) previous = baseDirection;
|
|
1063
|
+
if (type === 'ltr' || type === 'rtl') previous = type;
|
|
1064
|
+
atom.direction = type === 'number' ? (previous === 'rtl' ? 'number' : 'ltr') : type;
|
|
1065
|
+
}
|
|
1066
|
+
for (var i = 1; i + 1 < atoms.length; i++) {
|
|
1067
|
+
if (atoms[i - 1].direction === 'number' && atoms[i + 1].direction === 'number' && /[.,\u066b\u066c]/u.test(atoms[i].text)) {
|
|
1068
|
+
atoms[i].direction = 'number';
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
var next = baseDirection;
|
|
1072
|
+
for (var i = atoms.length - 1; i >= 0; i--) {
|
|
1073
|
+
var item = atoms[i];
|
|
1074
|
+
if (item.newline) next = baseDirection;
|
|
1075
|
+
item.nextDirection = next;
|
|
1076
|
+
if (item.direction !== 'neutral') next = item.direction === 'number' ? 'rtl' : item.direction;
|
|
1077
|
+
}
|
|
1078
|
+
previous = baseDirection;
|
|
1079
|
+
for (var item of atoms) {
|
|
1080
|
+
if (item.newline) previous = baseDirection;
|
|
1081
|
+
var direction = item.direction === 'neutral' ? (previous === item.nextDirection ? previous : baseDirection) : item.direction;
|
|
1082
|
+
if (item.direction !== 'neutral') previous = item.direction === 'number' ? 'rtl' : item.direction;
|
|
1083
|
+
item.level = direction === 'rtl' ? (base | 1) : direction === 'number' ? (base | 1) + 1 : base + (base & 1);
|
|
1084
|
+
}
|
|
1085
|
+
if (!fteMeasureContext) return;
|
|
1086
|
+
for (var start = 0; start < atoms.length;) {
|
|
1087
|
+
var first = atoms[start], end = start + 1;
|
|
1088
|
+
if ((first.level & 1) && first.text) {
|
|
1089
|
+
while (fteShapingMatch(first, atoms[end])) end++;
|
|
1090
|
+
if (end > start + 1) {
|
|
1091
|
+
var run = atoms.slice(start, end);
|
|
1092
|
+
var text = run.map(function (atom) { return atom.text; }).join('');
|
|
1093
|
+
var width = run.reduce(function (sum, atom) { return sum + atom.width; }, 0);
|
|
1094
|
+
fteMeasureContext.font = first.font;
|
|
1095
|
+
fteMeasureContext.direction = 'rtl';
|
|
1096
|
+
var shaped = fteMeasureContext.measureText(text).width;
|
|
1097
|
+
if (width > 0 && Number.isFinite(shaped)) for (var atom of run) atom.width *= shaped / width;
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
start = end;
|
|
1101
|
+
}
|
|
1102
|
+
fteMeasureContext.direction = 'ltr';
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
export function layoutFteBidi(atoms, base) {
|
|
1106
|
+
if (atoms.some(function (atom) { return atom.tab; })) return null;
|
|
1107
|
+
var odd = atoms.filter(function (atom) { return atom.level & 1; });
|
|
1108
|
+
if (!odd.length) return null;
|
|
1109
|
+
var visual = atoms.slice(), minOdd = odd.reduce(function (min, atom) { return Math.min(min, atom.level); }, Infinity);
|
|
1110
|
+
var max = atoms.reduce(function (level, atom) { return Math.max(level, atom.level); }, base);
|
|
1111
|
+
for (var level = max; level >= minOdd; level--) {
|
|
1112
|
+
for (var start = 0; start < visual.length;) {
|
|
1113
|
+
if (visual[start].level < level) { start++; continue; }
|
|
1114
|
+
var end = start + 1;
|
|
1115
|
+
while (end < visual.length && visual[end].level >= level) end++;
|
|
1116
|
+
for (var left = start, right = end - 1; left < right; left++, right--) {
|
|
1117
|
+
var item = visual[left]; visual[left] = visual[right]; visual[right] = item;
|
|
1118
|
+
}
|
|
1119
|
+
start = end;
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
var x = 0, minInk = Infinity, maxInk = 0;
|
|
1123
|
+
for (var atom of visual) {
|
|
1124
|
+
atom.x = x;
|
|
1125
|
+
x += atom.width;
|
|
1126
|
+
if (atom.graphicElement || /\S/u.test(atom.text)) { minInk = Math.min(minInk, atom.x); maxInk = x; }
|
|
1127
|
+
}
|
|
1128
|
+
if (minInk === Infinity) minInk = 0;
|
|
1129
|
+
for (var atom of visual) atom.x -= minInk;
|
|
1130
|
+
return { visual: visual, width: maxInk - minInk };
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
export function fteTabWidth(stops, atoms, index, advance, lineOffset) {
|
|
1134
|
+
var origin = advance + lineOffset, field = [], fieldWidth = 0, fieldText = '';
|
|
1135
|
+
if (stops?.some(function (stop) { return stop.position > origin && stop.alignment !== 'start'; })) {
|
|
1136
|
+
for (var i = index + 1; i < atoms.length && !atoms[i].tab && !atoms[i].newline; i++) {
|
|
1137
|
+
var atom = atoms[i];
|
|
1138
|
+
field.push({ offset: fieldText.length, x: fieldWidth });
|
|
1139
|
+
fieldText += atom.text;
|
|
1140
|
+
fieldWidth += atom.width;
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
for (var stop of stops || []) {
|
|
1144
|
+
if (stop.position <= origin) continue;
|
|
1145
|
+
var inset = 0;
|
|
1146
|
+
if (stop.alignment === 'center') inset = fieldWidth / 2;
|
|
1147
|
+
if (stop.alignment === 'end') inset = fieldWidth;
|
|
1148
|
+
if (stop.alignment === 'decimal') {
|
|
1149
|
+
var token = stop.decimalAlignmentToken;
|
|
1150
|
+
var offset = token ? fieldText.indexOf(token) : -1;
|
|
1151
|
+
inset = fieldWidth;
|
|
1152
|
+
if (offset >= 0) {
|
|
1153
|
+
for (var entry of field) {
|
|
1154
|
+
if (entry.offset > offset) break;
|
|
1155
|
+
inset = entry.x;
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
var gap = stop.position - origin - inset;
|
|
1160
|
+
if (gap >= 0) return gap;
|
|
1161
|
+
}
|
|
1162
|
+
var last = stops?.at(-1)?.position || 0;
|
|
1163
|
+
var steps = Math.max(1, Math.floor((origin - last) / DEFAULT_FTE_TAB_INTERVAL) + 1);
|
|
1164
|
+
return last + steps * DEFAULT_FTE_TAB_INTERVAL - origin;
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
export function justifyFteLine(justifier, atoms, width, naturalWidth) {
|
|
1168
|
+
if (atoms.some(function (atom) { return atom.tab || (atom.level & 1); })) return naturalWidth;
|
|
1169
|
+
var eastAsian = justifier instanceof EastAsianJustifier;
|
|
1170
|
+
var first = atoms.findIndex(function (atom) { return atom.graphicElement || /\S/u.test(atom.text); });
|
|
1171
|
+
if (first < 0) return naturalWidth;
|
|
1172
|
+
var last = atoms.findLastIndex(function (atom) {
|
|
1173
|
+
return atom.graphicElement || /\S/u.test(atom.text) || eastAsian && justifier.composeTrailingIdeographicSpaces && atom.text === '\u3000';
|
|
1174
|
+
});
|
|
1175
|
+
var composedWidth = atoms[last].x + atoms[last].width;
|
|
1176
|
+
var spaces = [], letters = [];
|
|
1177
|
+
var cjk = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
|
|
1178
|
+
for (var i = first; i < last; i++) {
|
|
1179
|
+
var atom = atoms[i], next = atoms[i + 1];
|
|
1180
|
+
if (atom.text === ' ') spaces.push(i);
|
|
1181
|
+
if (eastAsian ? (/\S/u.test(atom.text) && /\S/u.test(next.text) && (cjk.test(atom.text) || cjk.test(next.text))) :
|
|
1182
|
+
(justifier.letterSpacing && /[\p{Letter}\p{Number}]/u.test(atom.text) && /[\p{Letter}\p{Number}]/u.test(next.text))) {
|
|
1183
|
+
letters.push(i);
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
var remaining = width - composedWidth;
|
|
1188
|
+
if (!remaining || (!spaces.length && !letters.length)) return composedWidth;
|
|
1189
|
+
if (eastAsian && remaining < 0 && justifier.justificationStyle === 'pushOutOnly') return composedWidth;
|
|
1190
|
+
|
|
1191
|
+
var adjustments = new Map();
|
|
1192
|
+
if (spaces.length) {
|
|
1193
|
+
var spaceWidth = spaces.reduce(function (sum, index) { return sum + atoms[index].width; }, 0);
|
|
1194
|
+
if (spaceWidth > 0) {
|
|
1195
|
+
var min = eastAsian ? 0 : justifier.minimumSpacing;
|
|
1196
|
+
var max = eastAsian || !letters.length ? Infinity : justifier.maximumSpacing;
|
|
1197
|
+
var ratio = Math.max(min, Math.min(max, 1 + remaining / spaceWidth));
|
|
1198
|
+
for (var index of spaces) {
|
|
1199
|
+
var adjustment = atoms[index].width * (ratio - 1);
|
|
1200
|
+
adjustments.set(index, adjustment);
|
|
1201
|
+
remaining -= adjustment;
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
if (letters.length && remaining > 0) {
|
|
1206
|
+
var letterExtra = remaining / letters.length;
|
|
1207
|
+
for (var index of letters) adjustments.set(index, letterExtra);
|
|
1208
|
+
}
|
|
1209
|
+
var offset = 0;
|
|
1210
|
+
for (var i = 0; i < atoms.length; i++) {
|
|
1211
|
+
atoms[i].x += offset;
|
|
1212
|
+
var adjustment = adjustments.get(i) || 0;
|
|
1213
|
+
atoms[i].width += adjustment;
|
|
1214
|
+
offset += adjustment;
|
|
1215
|
+
}
|
|
1216
|
+
return composedWidth + offset;
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
export function breakFteLine(block, start, width, lineOffset, fitSomething) {
|
|
1220
|
+
var readyCount = block.fontLookup?.readyCount() || 0;
|
|
1221
|
+
if (!block.atoms || block.readyFonts !== readyCount) {
|
|
1222
|
+
block.atoms = [];
|
|
1223
|
+
block.readyFonts = readyCount;
|
|
1224
|
+
if (block.content) collectFteAtoms(block.content, block.atoms, 0, block.fontLookup);
|
|
1225
|
+
resolveFteBidi(block.atoms, block.bidiLevel);
|
|
1226
|
+
}
|
|
1227
|
+
var atoms = block.atoms, first = atoms.findIndex(function (atom) { return atom.begin === start; });
|
|
1228
|
+
if (first < 0) { block.result = 'complete'; return null; }
|
|
1229
|
+
var end = first, lastBreak = first, advance = 0;
|
|
1230
|
+
var tabWidths = new Map();
|
|
1231
|
+
var justifier = block.textJustifier;
|
|
1232
|
+
var mode = justifier.lineJustification;
|
|
1233
|
+
var optimum = mode !== 'unjustified' && justifier instanceof SpaceJustifier ? justifier.optimumSpacing : 1;
|
|
1234
|
+
if (optimum !== 1 && atoms.some(function (atom) { return atom.tab; })) optimum = 1;
|
|
1235
|
+
block.result = 'success';
|
|
1236
|
+
for (; end < atoms.length; end++) {
|
|
1237
|
+
var atom = atoms[end];
|
|
1238
|
+
if (atom.newline) { end++; break; }
|
|
1239
|
+
var atomWidth = atom.tab ? fteTabWidth(block.tabStops, atoms, end, advance, lineOffset) :
|
|
1240
|
+
atom.width * (atom.text === ' ' ? optimum : 1);
|
|
1241
|
+
if (atom.tab) tabWidths.set(end, atomWidth);
|
|
1242
|
+
if (advance + atomWidth > width) {
|
|
1243
|
+
if (end > first && /^\s+$/u.test(atom.text) && atom.breakAfter) {
|
|
1244
|
+
while (end < atoms.length && /^\s+$/u.test(atoms[end].text) && atoms[end].breakAfter) end++;
|
|
1245
|
+
} else if (atom.tab && end > first) break;
|
|
1246
|
+
else if (lastBreak > first) end = lastBreak;
|
|
1247
|
+
else if (end > first || fitSomething) {
|
|
1248
|
+
if (end === first) end++;
|
|
1249
|
+
block.result = 'emergency';
|
|
1250
|
+
} else { block.result = 'insufficientWidth'; return null; }
|
|
1251
|
+
break;
|
|
1252
|
+
}
|
|
1253
|
+
advance += atomWidth;
|
|
1254
|
+
if (atom.breakAfter) lastBreak = end + 1;
|
|
1255
|
+
}
|
|
1256
|
+
var lineAtoms = [], x = 0, ascent = 0, descent = 0, inkWidth = 0, hasTabs = false, hasGraphicElement = false;
|
|
1257
|
+
for (var i = first; i < end; i++) {
|
|
1258
|
+
var item = atoms[i];
|
|
1259
|
+
var itemWidth = tabWidths.get(i) ?? item.width;
|
|
1260
|
+
if (!item.newline) lineAtoms.push({ ...item, x: x, width: itemWidth });
|
|
1261
|
+
hasTabs = hasTabs || Boolean(item.tab);
|
|
1262
|
+
hasGraphicElement = hasGraphicElement || Boolean(item.graphicElement);
|
|
1263
|
+
x += itemWidth;
|
|
1264
|
+
if (item.graphicElement || /\S/u.test(item.text)) inkWidth = x;
|
|
1265
|
+
ascent = Math.max(ascent, item.ascent);
|
|
1266
|
+
descent = Math.max(descent, item.descent);
|
|
1267
|
+
}
|
|
1268
|
+
var bidi = layoutFteBidi(lineAtoms, block.bidiLevel);
|
|
1269
|
+
if (bidi) inkWidth = bidi.width;
|
|
1270
|
+
var justify = mode === 'allIncludingLast' || end < atoms.length &&
|
|
1271
|
+
(mode === 'allButLast' || mode === 'allButMandatoryBreak' && !atoms[end - 1].newline);
|
|
1272
|
+
var textWidth = justify ? justifyFteLine(justifier, lineAtoms, width, inkWidth) : inkWidth;
|
|
1273
|
+
return { atoms: lineAtoms, visualAtoms: bidi?.visual, textBlockBeginIndex: start, rawTextLength: atoms[end - 1].end - start,
|
|
1274
|
+
specifiedWidth: width, textWidth: textWidth, unjustifiedTextWidth: inkWidth, atomCount: lineAtoms.length,
|
|
1275
|
+
ascent: ascent, descent: descent, totalAscent: ascent, totalDescent: descent,
|
|
1276
|
+
hasTabs: hasTabs, hasGraphicElement: hasGraphicElement,
|
|
1277
|
+
textHeight: ascent + descent, totalHeight: ascent + descent };
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
export function relinkTextLines(block, lines) {
|
|
1281
|
+
for (var i = 0; i < lines.length; i++) {
|
|
1282
|
+
var state = textLineState.get(lines[i]);
|
|
1283
|
+
state.textBlock = block;
|
|
1284
|
+
state.previousLine = lines[i - 1] ?? null;
|
|
1285
|
+
state.nextLine = lines[i + 1] ?? null;
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
export function detachTextLines(block, start, count) {
|
|
1290
|
+
var lines = textBlockState.get(block).lines;
|
|
1291
|
+
for (var line of lines.splice(start, count)) {
|
|
1292
|
+
var state = textLineState.get(line);
|
|
1293
|
+
for (var atom of state.atoms) {
|
|
1294
|
+
if (!atom.graphicElement) continue;
|
|
1295
|
+
var graphic = contentElementState.get(atom.graphicElement);
|
|
1296
|
+
if (graphic.placement?.line === line) graphic.placement = null;
|
|
1297
|
+
atom.graphicElement = null;
|
|
1298
|
+
}
|
|
1299
|
+
state.textBlock = state.previousLine = state.nextLine = null;
|
|
1300
|
+
state.validity = 'invalid';
|
|
1301
|
+
}
|
|
1302
|
+
invalidateTextLines(lines, start);
|
|
1303
|
+
relinkTextLines(block, lines);
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
export function placeFteGraphic(line, atom, graphic) {
|
|
1307
|
+
var previous = atom.graphic;
|
|
1308
|
+
if (graphic && isStageDescendant(graphic, line)) throw new Error('An inline graphic cannot contain its TextLine');
|
|
1309
|
+
var index = previous?.parent === line ? line.getChildIndex(previous) : line.numChildren;
|
|
1310
|
+
var opacity = textLineState.get(line).graphicOpacity;
|
|
1311
|
+
opacity.delete(previous);
|
|
1312
|
+
if (previous && previous.parent === line) line.removeChild(previous);
|
|
1313
|
+
atom.graphic = graphic;
|
|
1314
|
+
if (graphic) {
|
|
1315
|
+
if (atom.graphicAlpha !== 1) opacity.set(graphic, atom.graphicAlpha);
|
|
1316
|
+
graphic.x = atom.x;
|
|
1317
|
+
graphic.y = atom.graphicTop;
|
|
1318
|
+
line.addChildAt(graphic, Math.min(index, line.numChildren));
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
export function composeTextLine(block, recycled, previous, width, lineOffset, fitSomething) {
|
|
1323
|
+
var state = textBlockState.get(block), lines = state.lines;
|
|
1324
|
+
var previousIndex = previous == null ? -1 : lines.indexOf(previous);
|
|
1325
|
+
if (previous != null && (previousIndex < 0 || previous.validity !== 'valid' || previous === recycled)) {
|
|
1326
|
+
throw textEngineError('ArgumentError', 'Previous TextLine must be valid and owned by this TextBlock');
|
|
1327
|
+
}
|
|
1328
|
+
width = Number(width);
|
|
1329
|
+
if (!Number.isFinite(width) || width > TextLine.MAX_LINE_WIDTH || (width < 0 && !fitSomething)) {
|
|
1330
|
+
throw textEngineError('ArgumentError', 'TextLine width is out of range');
|
|
1331
|
+
}
|
|
1332
|
+
var start = previousIndex < 0 ? 0 : previous.textBlockBeginIndex + previous.rawTextLength;
|
|
1333
|
+
lineOffset = Number(lineOffset);
|
|
1334
|
+
var layout = breakFteLine(state, start, width, Number.isFinite(lineOffset) ? lineOffset : 0, fitSomething);
|
|
1335
|
+
if (!layout) return null;
|
|
1336
|
+
if (recycled) {
|
|
1337
|
+
for (var atom of layout.atoms) {
|
|
1338
|
+
var candidate = atom.graphicElement && contentElementState.get(atom.graphicElement).graphic;
|
|
1339
|
+
if (candidate && isStageDescendant(candidate, recycled)) throw new Error('An inline graphic cannot contain its TextLine');
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
if (recycled && recycled.textBlock) recycled.textBlock.releaseLines(recycled, recycled);
|
|
1343
|
+
var insertion = previous ? lines.indexOf(previous) + 1 : 0;
|
|
1344
|
+
var stop = insertion;
|
|
1345
|
+
while (stop < lines.length && lines[stop].textBlockBeginIndex < start + layout.rawTextLength) stop++;
|
|
1346
|
+
if (stop > insertion) detachTextLines(block, insertion, stop - insertion);
|
|
1347
|
+
var line = recycled || Object.create(TextLine.prototype);
|
|
1348
|
+
var parent = line.parent ?? null;
|
|
1349
|
+
if (recycled) while (line.numChildren) line.removeChildAt(line.numChildren - 1);
|
|
1350
|
+
applySuper(DisplayObjectContainer, line);
|
|
1351
|
+
line.parent = parent;
|
|
1352
|
+
line.userData = null;
|
|
1353
|
+
line.width = layout.textWidth;
|
|
1354
|
+
line.height = layout.textHeight;
|
|
1355
|
+
textLineState.set(line, { ...layout, validity: 'valid', textBlock: block, graphicOpacity: new Map() });
|
|
1356
|
+
lines.splice(insertion, 0, line);
|
|
1357
|
+
relinkTextLines(block, lines);
|
|
1358
|
+
for (var atom of layout.atoms) {
|
|
1359
|
+
if (!atom.graphicElement) continue;
|
|
1360
|
+
var graphic = contentElementState.get(atom.graphicElement);
|
|
1361
|
+
placeFteGraphic(line, atom, graphic.graphic);
|
|
1362
|
+
graphic.placement = { line: line, atom: atom };
|
|
1363
|
+
}
|
|
1364
|
+
return line;
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
export function paintFteLine(ctx, line) {
|
|
1368
|
+
var alpha = ctx.globalAlpha;
|
|
1369
|
+
ctx.textBaseline = 'alphabetic';
|
|
1370
|
+
ctx.textAlign = 'left';
|
|
1371
|
+
var state = textLineState.get(line);
|
|
1372
|
+
var atoms = state.visualAtoms || state.atoms;
|
|
1373
|
+
for (var i = 0; i < atoms.length; i++) {
|
|
1374
|
+
var atom = atoms[i];
|
|
1375
|
+
if (!/\S/u.test(atom.text)) continue;
|
|
1376
|
+
ctx.font = atom.font;
|
|
1377
|
+
ctx.fillStyle = '#' + (atom.color & 0xffffff).toString(16).padStart(6, '0');
|
|
1378
|
+
ctx.globalAlpha = alpha * atom.alpha;
|
|
1379
|
+
var text = atom.text;
|
|
1380
|
+
var rtl = state.visualAtoms && (atom.level & 1);
|
|
1381
|
+
ctx.direction = rtl ? 'rtl' : 'ltr';
|
|
1382
|
+
if (rtl) while (fteShapingMatch(atom, atoms[i + 1])) text = atoms[++i].text + text;
|
|
1383
|
+
ctx.fillText(text, atom.x, atom.shift);
|
|
1384
|
+
}
|
|
1385
|
+
ctx.globalAlpha = alpha;
|
|
1386
|
+
}
|
|
1387
|
+
|