pixi-silk 1.0.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +3 -2
- package/dist/SilkGraphics.d.ts +2 -1
- package/dist/SilkGraphics.js +6 -1
- package/dist/app.d.ts +8 -1
- package/dist/app.js +5 -1
- package/dist/batch.d.ts +2 -1
- package/dist/batch.js +4 -3
- package/dist/glass/GlassFilter.js +2 -1
- package/dist/shader-slot.js +3 -1
- package/dist/shader.js +4 -2
- package/dist/text/SilkText.d.ts +143 -6
- package/dist/text/SilkText.js +264 -35
- package/dist/text/atlas.d.ts +52 -2
- package/dist/text/atlas.js +231 -27
- package/dist/text/bidi.d.ts +36 -0
- package/dist/text/bidi.js +632 -0
- package/dist/text/cluster.d.ts +37 -0
- package/dist/text/cluster.js +277 -0
- package/dist/text/label.d.ts +20 -0
- package/dist/text/label.js +27 -1
- package/dist/text/layout.d.ts +162 -11
- package/dist/text/layout.js +541 -58
- package/dist/text/rich.d.ts +84 -0
- package/dist/text/rich.js +218 -0
- package/dist/text/shader.js +35 -5
- package/dist/text.d.ts +7 -4
- package/dist/text.js +6 -3
- package/dist/ui/textedit.d.ts +17 -2
- package/dist/ui/textedit.js +55 -18
- package/dist/webgl1/glsl100.js +123 -0
- package/dist/webgl1/hooks.js +137 -0
- package/dist/webgl1/programs.js +244 -0
- package/dist/webgl1.d.ts +41 -0
- package/dist/webgl1.js +104 -0
- package/package.json +14 -12
- package/skills/pixi-silk/SKILL.md +1 -0
- package/skills/pixi-silk-text/SKILL.md +57 -2
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
//#region src/text/bidi.ts
|
|
2
|
+
const inRange = (c, ranges) => {
|
|
3
|
+
for (let i = 0; i < ranges.length; i += 2) if (c >= ranges[i] && c <= ranges[i + 1]) return true;
|
|
4
|
+
return false;
|
|
5
|
+
};
|
|
6
|
+
const EN = [
|
|
7
|
+
48,
|
|
8
|
+
57,
|
|
9
|
+
178,
|
|
10
|
+
179,
|
|
11
|
+
185,
|
|
12
|
+
185,
|
|
13
|
+
1776,
|
|
14
|
+
1785,
|
|
15
|
+
8304,
|
|
16
|
+
8304,
|
|
17
|
+
8308,
|
|
18
|
+
8313,
|
|
19
|
+
8320,
|
|
20
|
+
8329,
|
|
21
|
+
65296,
|
|
22
|
+
65305
|
|
23
|
+
];
|
|
24
|
+
const AN = [
|
|
25
|
+
1536,
|
|
26
|
+
1541,
|
|
27
|
+
1632,
|
|
28
|
+
1641,
|
|
29
|
+
1643,
|
|
30
|
+
1644,
|
|
31
|
+
1757,
|
|
32
|
+
1757,
|
|
33
|
+
2274,
|
|
34
|
+
2274,
|
|
35
|
+
69216,
|
|
36
|
+
69246
|
|
37
|
+
];
|
|
38
|
+
const ES = [
|
|
39
|
+
43,
|
|
40
|
+
43,
|
|
41
|
+
45,
|
|
42
|
+
45,
|
|
43
|
+
8314,
|
|
44
|
+
8315,
|
|
45
|
+
8330,
|
|
46
|
+
8331,
|
|
47
|
+
8722,
|
|
48
|
+
8722,
|
|
49
|
+
65122,
|
|
50
|
+
65123,
|
|
51
|
+
65291,
|
|
52
|
+
65291,
|
|
53
|
+
65293,
|
|
54
|
+
65293
|
|
55
|
+
];
|
|
56
|
+
const ET = [
|
|
57
|
+
35,
|
|
58
|
+
37,
|
|
59
|
+
162,
|
|
60
|
+
165,
|
|
61
|
+
176,
|
|
62
|
+
177,
|
|
63
|
+
1423,
|
|
64
|
+
1423,
|
|
65
|
+
1545,
|
|
66
|
+
1546,
|
|
67
|
+
1642,
|
|
68
|
+
1642,
|
|
69
|
+
2546,
|
|
70
|
+
2547,
|
|
71
|
+
3647,
|
|
72
|
+
3647,
|
|
73
|
+
8240,
|
|
74
|
+
8244,
|
|
75
|
+
8352,
|
|
76
|
+
8399,
|
|
77
|
+
8494,
|
|
78
|
+
8494,
|
|
79
|
+
8723,
|
|
80
|
+
8723,
|
|
81
|
+
65119,
|
|
82
|
+
65119,
|
|
83
|
+
65129,
|
|
84
|
+
65130,
|
|
85
|
+
65283,
|
|
86
|
+
65285,
|
|
87
|
+
65504,
|
|
88
|
+
65505,
|
|
89
|
+
65509,
|
|
90
|
+
65510
|
|
91
|
+
];
|
|
92
|
+
const CS = [
|
|
93
|
+
44,
|
|
94
|
+
44,
|
|
95
|
+
46,
|
|
96
|
+
47,
|
|
97
|
+
58,
|
|
98
|
+
58,
|
|
99
|
+
160,
|
|
100
|
+
160,
|
|
101
|
+
1548,
|
|
102
|
+
1548,
|
|
103
|
+
8239,
|
|
104
|
+
8239,
|
|
105
|
+
8260,
|
|
106
|
+
8260,
|
|
107
|
+
65104,
|
|
108
|
+
65104,
|
|
109
|
+
65106,
|
|
110
|
+
65106,
|
|
111
|
+
65109,
|
|
112
|
+
65109,
|
|
113
|
+
65292,
|
|
114
|
+
65292,
|
|
115
|
+
65294,
|
|
116
|
+
65295,
|
|
117
|
+
65306,
|
|
118
|
+
65306
|
|
119
|
+
];
|
|
120
|
+
const WS = [
|
|
121
|
+
32,
|
|
122
|
+
32,
|
|
123
|
+
12,
|
|
124
|
+
12,
|
|
125
|
+
5760,
|
|
126
|
+
5760,
|
|
127
|
+
8192,
|
|
128
|
+
8202,
|
|
129
|
+
8232,
|
|
130
|
+
8232,
|
|
131
|
+
8287,
|
|
132
|
+
8287,
|
|
133
|
+
12288,
|
|
134
|
+
12288
|
|
135
|
+
];
|
|
136
|
+
const S = [
|
|
137
|
+
9,
|
|
138
|
+
9,
|
|
139
|
+
11,
|
|
140
|
+
11,
|
|
141
|
+
31,
|
|
142
|
+
31
|
|
143
|
+
];
|
|
144
|
+
const B = [
|
|
145
|
+
10,
|
|
146
|
+
10,
|
|
147
|
+
13,
|
|
148
|
+
13,
|
|
149
|
+
28,
|
|
150
|
+
30,
|
|
151
|
+
133,
|
|
152
|
+
133,
|
|
153
|
+
8233,
|
|
154
|
+
8233
|
|
155
|
+
];
|
|
156
|
+
const BN = [
|
|
157
|
+
0,
|
|
158
|
+
8,
|
|
159
|
+
14,
|
|
160
|
+
27,
|
|
161
|
+
127,
|
|
162
|
+
132,
|
|
163
|
+
134,
|
|
164
|
+
159,
|
|
165
|
+
173,
|
|
166
|
+
173,
|
|
167
|
+
6158,
|
|
168
|
+
6158,
|
|
169
|
+
8203,
|
|
170
|
+
8205,
|
|
171
|
+
8234,
|
|
172
|
+
8238,
|
|
173
|
+
8288,
|
|
174
|
+
8297,
|
|
175
|
+
65279,
|
|
176
|
+
65279
|
|
177
|
+
];
|
|
178
|
+
const AL = [
|
|
179
|
+
1544,
|
|
180
|
+
1544,
|
|
181
|
+
1547,
|
|
182
|
+
1547,
|
|
183
|
+
1549,
|
|
184
|
+
1549,
|
|
185
|
+
1563,
|
|
186
|
+
1610,
|
|
187
|
+
1645,
|
|
188
|
+
1647,
|
|
189
|
+
1649,
|
|
190
|
+
1749,
|
|
191
|
+
1765,
|
|
192
|
+
1766,
|
|
193
|
+
1774,
|
|
194
|
+
1775,
|
|
195
|
+
1786,
|
|
196
|
+
1805,
|
|
197
|
+
1807,
|
|
198
|
+
1808,
|
|
199
|
+
1810,
|
|
200
|
+
1839,
|
|
201
|
+
1869,
|
|
202
|
+
1957,
|
|
203
|
+
1969,
|
|
204
|
+
1969,
|
|
205
|
+
2144,
|
|
206
|
+
2154,
|
|
207
|
+
2160,
|
|
208
|
+
2258,
|
|
209
|
+
64336,
|
|
210
|
+
64829,
|
|
211
|
+
64832,
|
|
212
|
+
65023,
|
|
213
|
+
65136,
|
|
214
|
+
65278,
|
|
215
|
+
68864,
|
|
216
|
+
68899,
|
|
217
|
+
69424,
|
|
218
|
+
69445,
|
|
219
|
+
126464,
|
|
220
|
+
126719
|
|
221
|
+
];
|
|
222
|
+
const R = [
|
|
223
|
+
1424,
|
|
224
|
+
1535,
|
|
225
|
+
1984,
|
|
226
|
+
2143,
|
|
227
|
+
8207,
|
|
228
|
+
8207,
|
|
229
|
+
64285,
|
|
230
|
+
64335,
|
|
231
|
+
67584,
|
|
232
|
+
69631,
|
|
233
|
+
124928,
|
|
234
|
+
126463,
|
|
235
|
+
126720,
|
|
236
|
+
126975
|
|
237
|
+
];
|
|
238
|
+
const MARK = /^[\p{Mn}\p{Me}]$/u;
|
|
239
|
+
const CONTROLS = [
|
|
240
|
+
"LRE",
|
|
241
|
+
"RLE",
|
|
242
|
+
"PDF",
|
|
243
|
+
"LRO",
|
|
244
|
+
"RLO"
|
|
245
|
+
];
|
|
246
|
+
const CONTROLS2 = [
|
|
247
|
+
"LRI",
|
|
248
|
+
"RLI",
|
|
249
|
+
"FSI",
|
|
250
|
+
"PDI"
|
|
251
|
+
];
|
|
252
|
+
const PUNCT = /^[\p{P}\p{S}]$/u;
|
|
253
|
+
/** The bidi type of one code point. Covers the common scripts. Anything unlisted is `L`, or `ON` for punctuation and symbols. */
|
|
254
|
+
function bidiClass(cp) {
|
|
255
|
+
if (cp < 128) {
|
|
256
|
+
if (cp >= 65 && cp <= 90) return "L";
|
|
257
|
+
if (cp >= 97 && cp <= 122) return "L";
|
|
258
|
+
}
|
|
259
|
+
const control = CONTROLS[cp - 8234] ?? CONTROLS2[cp - 8294];
|
|
260
|
+
if (cp >= 8234 && cp <= 8238 || cp >= 8294 && cp <= 8297) return control;
|
|
261
|
+
if (inRange(cp, EN)) return "EN";
|
|
262
|
+
if (inRange(cp, AN)) return "AN";
|
|
263
|
+
if (inRange(cp, WS)) return "WS";
|
|
264
|
+
if (inRange(cp, ES)) return "ES";
|
|
265
|
+
if (inRange(cp, CS)) return "CS";
|
|
266
|
+
if (inRange(cp, ET)) return "ET";
|
|
267
|
+
if (inRange(cp, S)) return "S";
|
|
268
|
+
if (inRange(cp, B)) return "B";
|
|
269
|
+
if (inRange(cp, BN)) return "BN";
|
|
270
|
+
if (cp === 8206) return "L";
|
|
271
|
+
if (cp === 1564) return "AL";
|
|
272
|
+
const ch = String.fromCodePoint(cp);
|
|
273
|
+
if (MARK.test(ch)) return "NSM";
|
|
274
|
+
if (cp === 64830 || cp === 64831) return "ON";
|
|
275
|
+
if (inRange(cp, AL)) return "AL";
|
|
276
|
+
if (inRange(cp, R)) return "R";
|
|
277
|
+
if (PUNCT.test(ch)) return "ON";
|
|
278
|
+
return "L";
|
|
279
|
+
}
|
|
280
|
+
const RTL_TEST = /[\u0590-\u08ff\u200f\u061c\u202a-\u202e\u2066-\u2069\ufb1d-\ufdff\ufe70-\ufefe\u{10800}-\u{10fff}\u{1e800}-\u{1efff}]/u;
|
|
281
|
+
/** True when the text holds a right-to-left character or a bidi control, so it needs the bidi pass. */
|
|
282
|
+
function hasRtl(text) {
|
|
283
|
+
return RTL_TEST.test(text);
|
|
284
|
+
}
|
|
285
|
+
/** Paragraph level from the first strong type (P2, P3): 1 for `R` or `AL`, 0 for `L` or none. Text inside isolates is skipped. */
|
|
286
|
+
function paragraphLevel(types, direction = "auto") {
|
|
287
|
+
if (direction === "ltr") return 0;
|
|
288
|
+
if (direction === "rtl") return 1;
|
|
289
|
+
return firstStrong(types, 0, types.length) ?? 0;
|
|
290
|
+
}
|
|
291
|
+
const ISOLATE = /* @__PURE__ */ new Set([
|
|
292
|
+
"LRI",
|
|
293
|
+
"RLI",
|
|
294
|
+
"FSI"
|
|
295
|
+
]);
|
|
296
|
+
const EMBED = /* @__PURE__ */ new Set([
|
|
297
|
+
"LRE",
|
|
298
|
+
"RLE",
|
|
299
|
+
"LRO",
|
|
300
|
+
"RLO"
|
|
301
|
+
]);
|
|
302
|
+
/** Removed by X9: embedding controls and boundary neutrals. They take the level of what comes before. */
|
|
303
|
+
const REMOVED = /* @__PURE__ */ new Set([
|
|
304
|
+
"LRE",
|
|
305
|
+
"RLE",
|
|
306
|
+
"LRO",
|
|
307
|
+
"RLO",
|
|
308
|
+
"PDF",
|
|
309
|
+
"BN"
|
|
310
|
+
]);
|
|
311
|
+
const isNI = (t) => t === "B" || t === "S" || t === "WS" || t === "ON" || t === "LRI" || t === "RLI" || t === "FSI" || t === "PDI";
|
|
312
|
+
/** P2 and P3 between `from` and `to`: the direction of the first strong type outside isolates, or null. */
|
|
313
|
+
function firstStrong(types, from, to) {
|
|
314
|
+
let depth = 0;
|
|
315
|
+
for (let i = from; i < to; i++) {
|
|
316
|
+
const t = types[i];
|
|
317
|
+
if (ISOLATE.has(t)) depth++;
|
|
318
|
+
else if (t === "PDI") {
|
|
319
|
+
if (depth > 0) depth--;
|
|
320
|
+
else if (from > 0) return null;
|
|
321
|
+
} else if (depth === 0 && t === "L") return 0;
|
|
322
|
+
else if (depth === 0 && (t === "R" || t === "AL")) return 1;
|
|
323
|
+
}
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
/** BD9: the index of the PDI that closes each isolate initiator, or -1. */
|
|
327
|
+
function matchingPdi(types) {
|
|
328
|
+
const out = new Array(types.length).fill(-1);
|
|
329
|
+
const open = [];
|
|
330
|
+
for (let i = 0; i < types.length; i++) if (ISOLATE.has(types[i])) open.push(i);
|
|
331
|
+
else if (types[i] === "PDI" && open.length) out[open.pop()] = i;
|
|
332
|
+
else if (types[i] === "B") open.length = 0;
|
|
333
|
+
return out;
|
|
334
|
+
}
|
|
335
|
+
const BRACKETS = "()[]{}༺༻༼༽᚛᚜⁅⁆⁽⁾₍₎⌈⌉⌊⌋〈〉❨❩❪❫❬❭❮❯❰❱❲❳❴❵⟅⟆⟦⟧⟨⟩⟪⟫⦃⦄⦅⦆〈〉《》「」『』【】〔〕〖〗〘〙〚〛﹙﹚﹛﹜﹝﹞()[]{}⦅⦆「」";
|
|
336
|
+
const OPENING = /* @__PURE__ */ new Map();
|
|
337
|
+
const CLOSING = /* @__PURE__ */ new Map();
|
|
338
|
+
for (let i = 0; i < 84; i += 2) {
|
|
339
|
+
OPENING.set(BRACKETS[i], BRACKETS[i + 1]);
|
|
340
|
+
CLOSING.set(BRACKETS[i + 1], BRACKETS[i]);
|
|
341
|
+
}
|
|
342
|
+
const canon = (c) => c === "〈" ? "〈" : c === "〉" ? "〉" : c;
|
|
343
|
+
/**
|
|
344
|
+
* Embedding levels of one paragraph, one per unit, in logical order (UAX #9 rules X1 to X10, W1 to
|
|
345
|
+
* W7, N0 to N2, I1 and I2). `types` are the original types. `chars` are the units' text, used to
|
|
346
|
+
* pair brackets (N0). Even levels run left to right, odd ones right to left. Embedding controls and
|
|
347
|
+
* boundary neutrals take the level of the unit before them.
|
|
348
|
+
*/
|
|
349
|
+
function resolveLevels(types, level, chars) {
|
|
350
|
+
const n = types.length;
|
|
351
|
+
const t = types.slice();
|
|
352
|
+
const levels = new Array(n).fill(level);
|
|
353
|
+
const pdi = matchingPdi(types);
|
|
354
|
+
const stack = [{
|
|
355
|
+
level,
|
|
356
|
+
override: "",
|
|
357
|
+
isolate: false
|
|
358
|
+
}];
|
|
359
|
+
let overflowIsolate = 0;
|
|
360
|
+
let overflowEmbed = 0;
|
|
361
|
+
let validIsolate = 0;
|
|
362
|
+
const MAX = 125;
|
|
363
|
+
for (let i = 0; i < n; i++) {
|
|
364
|
+
const top = stack[stack.length - 1];
|
|
365
|
+
let c = t[i];
|
|
366
|
+
if (c === "FSI") {
|
|
367
|
+
const end = pdi[i] < 0 ? n : pdi[i];
|
|
368
|
+
c = firstStrong(types, i + 1, end) === 1 ? "RLI" : "LRI";
|
|
369
|
+
}
|
|
370
|
+
if (EMBED.has(c)) {
|
|
371
|
+
const next = c === "RLE" || c === "RLO" ? top.level + 1 | 1 : top.level + 2 & -2;
|
|
372
|
+
levels[i] = top.level;
|
|
373
|
+
if (next <= MAX && overflowIsolate === 0 && overflowEmbed === 0) stack.push({
|
|
374
|
+
level: next,
|
|
375
|
+
override: c === "LRO" ? "L" : c === "RLO" ? "R" : "",
|
|
376
|
+
isolate: false
|
|
377
|
+
});
|
|
378
|
+
else if (overflowIsolate === 0) overflowEmbed++;
|
|
379
|
+
} else if (c === "RLI" || c === "LRI") {
|
|
380
|
+
levels[i] = top.level;
|
|
381
|
+
if (top.override) t[i] = top.override;
|
|
382
|
+
const next = c === "RLI" ? top.level + 1 | 1 : top.level + 2 & -2;
|
|
383
|
+
if (next <= MAX && overflowIsolate === 0 && overflowEmbed === 0) {
|
|
384
|
+
validIsolate++;
|
|
385
|
+
stack.push({
|
|
386
|
+
level: next,
|
|
387
|
+
override: "",
|
|
388
|
+
isolate: true
|
|
389
|
+
});
|
|
390
|
+
} else overflowIsolate++;
|
|
391
|
+
} else if (c === "PDI") {
|
|
392
|
+
if (overflowIsolate > 0) overflowIsolate--;
|
|
393
|
+
else if (validIsolate > 0) {
|
|
394
|
+
overflowEmbed = 0;
|
|
395
|
+
while (!stack[stack.length - 1].isolate) stack.pop();
|
|
396
|
+
stack.pop();
|
|
397
|
+
validIsolate--;
|
|
398
|
+
}
|
|
399
|
+
const now = stack[stack.length - 1];
|
|
400
|
+
levels[i] = now.level;
|
|
401
|
+
if (now.override) t[i] = now.override;
|
|
402
|
+
} else if (c === "PDF") {
|
|
403
|
+
levels[i] = top.level;
|
|
404
|
+
if (overflowIsolate > 0) {} else if (overflowEmbed > 0) overflowEmbed--;
|
|
405
|
+
else if (!top.isolate && stack.length >= 2) stack.pop();
|
|
406
|
+
} else if (c === "B") levels[i] = level;
|
|
407
|
+
else {
|
|
408
|
+
levels[i] = top.level;
|
|
409
|
+
if (c !== "BN" && top.override) t[i] = top.override;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
const kept = [];
|
|
413
|
+
for (let i = 0; i < n; i++) if (!REMOVED.has(types[i])) kept.push(i);
|
|
414
|
+
const runs = [];
|
|
415
|
+
for (const i of kept) {
|
|
416
|
+
const last = runs[runs.length - 1];
|
|
417
|
+
if (last && levels[last[last.length - 1]] === levels[i]) last.push(i);
|
|
418
|
+
else runs.push([i]);
|
|
419
|
+
}
|
|
420
|
+
const runOf = /* @__PURE__ */ new Map();
|
|
421
|
+
for (const r of runs) runOf.set(r[0], r);
|
|
422
|
+
const sequences = [];
|
|
423
|
+
const chained = /* @__PURE__ */ new Set();
|
|
424
|
+
for (const r of runs) {
|
|
425
|
+
if (chained.has(r)) continue;
|
|
426
|
+
if (types[r[0]] === "PDI" && pdi.includes(r[0])) continue;
|
|
427
|
+
const seq = [...r];
|
|
428
|
+
let end = r[r.length - 1];
|
|
429
|
+
while (ISOLATE.has(types[end]) && pdi[end] >= 0) {
|
|
430
|
+
const next = runOf.get(pdi[end]);
|
|
431
|
+
if (!next) break;
|
|
432
|
+
chained.add(next);
|
|
433
|
+
seq.push(...next);
|
|
434
|
+
end = next[next.length - 1];
|
|
435
|
+
}
|
|
436
|
+
sequences.push(seq);
|
|
437
|
+
}
|
|
438
|
+
const prevKept = (i) => {
|
|
439
|
+
for (let k = i - 1; k >= 0; k--) if (!REMOVED.has(types[k])) return k;
|
|
440
|
+
return -1;
|
|
441
|
+
};
|
|
442
|
+
const nextKept = (i) => {
|
|
443
|
+
for (let k = i + 1; k < n; k++) if (!REMOVED.has(types[k])) return k;
|
|
444
|
+
return -1;
|
|
445
|
+
};
|
|
446
|
+
for (const seq of sequences) {
|
|
447
|
+
const lv = levels[seq[0]];
|
|
448
|
+
const first = seq[0];
|
|
449
|
+
const last = seq[seq.length - 1];
|
|
450
|
+
const p = prevKept(first);
|
|
451
|
+
const q = ISOLATE.has(types[last]) ? -1 : nextKept(last);
|
|
452
|
+
resolveSequence(t, seq, Math.max(lv, p < 0 ? level : levels[p]) & 1 ? "R" : "L", Math.max(lv, q < 0 ? level : levels[q]) & 1 ? "R" : "L", lv, types, chars);
|
|
453
|
+
}
|
|
454
|
+
for (const i of kept) {
|
|
455
|
+
const c = t[i];
|
|
456
|
+
const l = levels[i];
|
|
457
|
+
if ((l & 1) === 0) levels[i] = c === "R" ? l + 1 : c === "AN" || c === "EN" ? l + 2 : l;
|
|
458
|
+
else levels[i] = c === "L" || c === "EN" || c === "AN" ? l + 1 : l;
|
|
459
|
+
}
|
|
460
|
+
for (let i = 0; i < n; i++) if (REMOVED.has(types[i])) levels[i] = i > 0 ? levels[i - 1] : level;
|
|
461
|
+
return levels;
|
|
462
|
+
}
|
|
463
|
+
/** W1 to W7, N0, N1 and N2 on one isolating run sequence. Changes `t`. */
|
|
464
|
+
function resolveSequence(t, seq, sos, eos, lv, original, chars) {
|
|
465
|
+
const m = seq.length;
|
|
466
|
+
const at = (k) => t[seq[k]];
|
|
467
|
+
const set = (k, c) => {
|
|
468
|
+
t[seq[k]] = c;
|
|
469
|
+
};
|
|
470
|
+
for (let k = 0; k < m; k++) {
|
|
471
|
+
if (at(k) !== "NSM") continue;
|
|
472
|
+
const prev = k > 0 ? at(k - 1) : sos;
|
|
473
|
+
set(k, ISOLATE.has(prev) || prev === "PDI" || prev === "LRI" || prev === "RLI" ? "ON" : prev);
|
|
474
|
+
}
|
|
475
|
+
let strong = sos;
|
|
476
|
+
for (let k = 0; k < m; k++) {
|
|
477
|
+
const c = at(k);
|
|
478
|
+
if (c === "L" || c === "R" || c === "AL") strong = c;
|
|
479
|
+
else if (c === "EN" && strong === "AL") set(k, "AN");
|
|
480
|
+
}
|
|
481
|
+
for (let k = 0; k < m; k++) if (at(k) === "AL") set(k, "R");
|
|
482
|
+
for (let k = 1; k < m - 1; k++) {
|
|
483
|
+
const a = at(k - 1);
|
|
484
|
+
const b = at(k + 1);
|
|
485
|
+
if (at(k) === "ES" && a === "EN" && b === "EN") set(k, "EN");
|
|
486
|
+
else if (at(k) === "CS" && a === b && (a === "EN" || a === "AN")) set(k, a);
|
|
487
|
+
}
|
|
488
|
+
for (let k = 0; k < m; k++) {
|
|
489
|
+
if (at(k) !== "ET") continue;
|
|
490
|
+
let j = k;
|
|
491
|
+
while (j < m && at(j) === "ET") j++;
|
|
492
|
+
if (k > 0 && at(k - 1) === "EN" || j < m && at(j) === "EN") for (let x = k; x < j; x++) set(x, "EN");
|
|
493
|
+
k = j - 1;
|
|
494
|
+
}
|
|
495
|
+
for (let k = 0; k < m; k++) if (at(k) === "ES" || at(k) === "ET" || at(k) === "CS") set(k, "ON");
|
|
496
|
+
strong = sos;
|
|
497
|
+
for (let k = 0; k < m; k++) {
|
|
498
|
+
const c = at(k);
|
|
499
|
+
if (c === "L" || c === "R") strong = c;
|
|
500
|
+
else if (c === "EN" && strong === "L") set(k, "L");
|
|
501
|
+
}
|
|
502
|
+
const e = lv & 1 ? "R" : "L";
|
|
503
|
+
const dirOf = (c) => c === "L" ? "L" : c === "R" || c === "AN" || c === "EN" ? "R" : null;
|
|
504
|
+
if (chars) {
|
|
505
|
+
const pairs = [];
|
|
506
|
+
const open = [];
|
|
507
|
+
for (let k = 0; k < m; k++) {
|
|
508
|
+
if (at(k) !== "ON") continue;
|
|
509
|
+
const ch = canon(chars[seq[k]] ?? "");
|
|
510
|
+
if (OPENING.has(ch)) {
|
|
511
|
+
if (open.length === 63) break;
|
|
512
|
+
open.push([canon(OPENING.get(ch)), k]);
|
|
513
|
+
} else if (CLOSING.has(ch)) for (let s = open.length - 1; s >= 0; s--) {
|
|
514
|
+
if (open[s][0] !== ch) continue;
|
|
515
|
+
pairs.push([open[s][1], k]);
|
|
516
|
+
open.length = s;
|
|
517
|
+
break;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
pairs.sort((a, b) => a[0] - b[0]);
|
|
521
|
+
for (const [a, b] of pairs) {
|
|
522
|
+
let found = null;
|
|
523
|
+
for (let k = a + 1; k < b; k++) {
|
|
524
|
+
const d = dirOf(at(k));
|
|
525
|
+
if (d === e) {
|
|
526
|
+
found = e;
|
|
527
|
+
break;
|
|
528
|
+
}
|
|
529
|
+
if (d) found = d;
|
|
530
|
+
}
|
|
531
|
+
if (!found) continue;
|
|
532
|
+
let dir = e;
|
|
533
|
+
if (found !== e) {
|
|
534
|
+
let before = sos === "R" ? "R" : "L";
|
|
535
|
+
for (let k = a - 1; k >= 0; k--) {
|
|
536
|
+
const d = dirOf(at(k));
|
|
537
|
+
if (d) {
|
|
538
|
+
before = d;
|
|
539
|
+
break;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
dir = before === found ? found : e;
|
|
543
|
+
}
|
|
544
|
+
set(a, dir);
|
|
545
|
+
set(b, dir);
|
|
546
|
+
for (const k of [a, b]) for (let x = k + 1; x < m && original[seq[x]] === "NSM"; x++) set(x, dir);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
for (let k = 0; k < m; k++) {
|
|
550
|
+
if (!isNI(at(k))) continue;
|
|
551
|
+
let j = k;
|
|
552
|
+
while (j < m && isNI(at(j))) j++;
|
|
553
|
+
const before = k > 0 ? dirOf(at(k - 1)) ?? e : sos === "R" ? "R" : "L";
|
|
554
|
+
const dir = before === (j < m ? dirOf(at(j)) ?? e : eos === "R" ? "R" : "L") ? before : e;
|
|
555
|
+
for (let x = k; x < j; x++) set(x, dir);
|
|
556
|
+
k = j - 1;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* L1 for one line: whitespace and isolate controls at the end of the line, and before tabs, fall
|
|
561
|
+
* back to the paragraph level. `types` are the original types of the line's units. Changes
|
|
562
|
+
* `levels` in place.
|
|
563
|
+
*/
|
|
564
|
+
function resetWhitespace(types, levels, level) {
|
|
565
|
+
let trailing = true;
|
|
566
|
+
for (let i = types.length - 1; i >= 0; i--) {
|
|
567
|
+
const c = types[i];
|
|
568
|
+
if (c === "S" || c === "B") {
|
|
569
|
+
levels[i] = level;
|
|
570
|
+
trailing = true;
|
|
571
|
+
} else if (trailing && (c === "WS" || isNI(c) || REMOVED.has(c)) && c !== "ON") levels[i] = level;
|
|
572
|
+
else trailing = false;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
/** L2: the visual order of a line as indices into `levels`, left to right. */
|
|
576
|
+
function visualOrder(levels) {
|
|
577
|
+
const order = levels.map((_, i) => i);
|
|
578
|
+
let high = 0;
|
|
579
|
+
let lowOdd = Infinity;
|
|
580
|
+
for (const l of levels) {
|
|
581
|
+
if (l > high) high = l;
|
|
582
|
+
if (l & 1 && l < lowOdd) lowOdd = l;
|
|
583
|
+
}
|
|
584
|
+
for (let lv = high; lv >= lowOdd && lv > 0; lv--) for (let i = 0; i < order.length; i++) {
|
|
585
|
+
if (levels[order[i]] < lv) continue;
|
|
586
|
+
let j = i;
|
|
587
|
+
while (j < order.length && levels[order[j]] >= lv) j++;
|
|
588
|
+
order.splice(i, j - i, ...order.slice(i, j).reverse());
|
|
589
|
+
i = j;
|
|
590
|
+
}
|
|
591
|
+
return order;
|
|
592
|
+
}
|
|
593
|
+
const MIRROR = {};
|
|
594
|
+
for (const pair of [
|
|
595
|
+
"()",
|
|
596
|
+
"<>",
|
|
597
|
+
"[]",
|
|
598
|
+
"{}",
|
|
599
|
+
"«»",
|
|
600
|
+
"‹›",
|
|
601
|
+
"⁅⁆",
|
|
602
|
+
"⁽⁾",
|
|
603
|
+
"₍₎",
|
|
604
|
+
"≤≥",
|
|
605
|
+
"≦≧",
|
|
606
|
+
"⌈⌉",
|
|
607
|
+
"⌊⌋",
|
|
608
|
+
"〈〉",
|
|
609
|
+
"⟨⟩",
|
|
610
|
+
"⟦⟧",
|
|
611
|
+
"「」",
|
|
612
|
+
"『』",
|
|
613
|
+
"【】",
|
|
614
|
+
"〔〕",
|
|
615
|
+
"〈〉",
|
|
616
|
+
"《》",
|
|
617
|
+
"﴾﴿",
|
|
618
|
+
"()",
|
|
619
|
+
"[]",
|
|
620
|
+
"{}",
|
|
621
|
+
"<>"
|
|
622
|
+
]) {
|
|
623
|
+
const [a, b] = Array.from(pair);
|
|
624
|
+
MIRROR[a] = b;
|
|
625
|
+
MIRROR[b] = a;
|
|
626
|
+
}
|
|
627
|
+
/** L4: the mirrored form of a character drawn right to left, such as `)` for `(`, or the character itself. */
|
|
628
|
+
function mirror(char) {
|
|
629
|
+
return MIRROR[char] ?? char;
|
|
630
|
+
}
|
|
631
|
+
//#endregion
|
|
632
|
+
export { bidiClass, hasRtl, mirror, paragraphLevel, resetWhitespace, resolveLevels, visualOrder };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { BidiClass } from "./bidi.js";
|
|
2
|
+
//#region src/text/cluster.d.ts
|
|
3
|
+
/** How lines may break inside text without spaces, as in CSS `word-break`. */
|
|
4
|
+
export type WordBreak = "normal" | "keep-all";
|
|
5
|
+
/** A run of text drawn as one glyph, as {@link clusters} returns them. */
|
|
6
|
+
export interface Cluster {
|
|
7
|
+
/** What to measure, bake and draw. Joined runs that were cut carry a zero width joiner at the cut. */
|
|
8
|
+
draw: string;
|
|
9
|
+
/** Offset of the first code point in the text. */
|
|
10
|
+
start: number;
|
|
11
|
+
/** Offset after the last code point. */
|
|
12
|
+
end: number;
|
|
13
|
+
/** Offsets inside the cluster where a caret can sit: its grapheme boundaries after the first. */
|
|
14
|
+
inner: number[];
|
|
15
|
+
/** Bidi type of the cluster's first character. */
|
|
16
|
+
type: BidiClass;
|
|
17
|
+
/** True when a line may break before the cluster although no space comes first. */
|
|
18
|
+
breakBefore: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Splits a paragraph into the clusters {@link layoutText} measures and draws, in reading order.
|
|
22
|
+
*
|
|
23
|
+
* - Plain text (Latin, Greek, Cyrillic, Chinese, Japanese, Korean) is one cluster per code point.
|
|
24
|
+
* - A character with its marks, an emoji with its modifiers and joiners, and a flag are one cluster.
|
|
25
|
+
* - Letters of a cursive script (Arabic, Syriac) stay together while they join, so Canvas shapes the
|
|
26
|
+
* run. A run breaks after a letter that does not join forward.
|
|
27
|
+
* - A word of a script that needs shaping (Burmese, Thai, Khmer, Lao, Tibetan, the Indic scripts and
|
|
28
|
+
* Sinhala) is one cluster. Vowels written before their consonant and stacked consonants then
|
|
29
|
+
* come out right.
|
|
30
|
+
*
|
|
31
|
+
* `breakBefore` marks where a line may break without a space. That is between Chinese and Japanese
|
|
32
|
+
* characters and Korean syllables unless `wordBreak` is `keep-all`, never before closing
|
|
33
|
+
* punctuation or small kana, never after opening brackets. It is also between words of Thai, Lao,
|
|
34
|
+
* Khmer and Burmese.
|
|
35
|
+
*/
|
|
36
|
+
export declare function clusters(text: string, wordBreak?: WordBreak): Cluster[];
|
|
37
|
+
//#endregion
|