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
package/dist/text/layout.js
CHANGED
|
@@ -1,24 +1,113 @@
|
|
|
1
|
+
import { hasRtl, mirror, paragraphLevel, resetWhitespace, resolveLevels, visualOrder } from "./bidi.js";
|
|
2
|
+
import { clusters, isWide, noLineEnd, noLineStart } from "./cluster.js";
|
|
1
3
|
//#region src/text/layout.ts
|
|
2
4
|
const NBSP = String.fromCharCode(160);
|
|
3
5
|
const isSpace = (c) => c === " " || c === " " || c === NBSP;
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
+
/** First code point of each run of ten decimal digits: ASCII, Arabic-Indic, Persian, Indic, Thai, Lao, Tibetan, Myanmar, Khmer, Mongolian, full width. */
|
|
7
|
+
const ZEROS = [
|
|
8
|
+
48,
|
|
9
|
+
1632,
|
|
10
|
+
1776,
|
|
11
|
+
1984,
|
|
12
|
+
2406,
|
|
13
|
+
2534,
|
|
14
|
+
2662,
|
|
15
|
+
2790,
|
|
16
|
+
2918,
|
|
17
|
+
3046,
|
|
18
|
+
3174,
|
|
19
|
+
3302,
|
|
20
|
+
3430,
|
|
21
|
+
3558,
|
|
22
|
+
3664,
|
|
23
|
+
3792,
|
|
24
|
+
3872,
|
|
25
|
+
4160,
|
|
26
|
+
4240,
|
|
27
|
+
6112,
|
|
28
|
+
6160,
|
|
29
|
+
65296
|
|
30
|
+
];
|
|
31
|
+
/** The zero of the digit set a character belongs to, or -1 when it is not a digit. */
|
|
32
|
+
const digitZero = (c) => {
|
|
33
|
+
const cp = c.codePointAt(0);
|
|
34
|
+
if (c.length !== 1) return -1;
|
|
35
|
+
for (const z of ZEROS) if (cp >= z && cp < z + 10) return z;
|
|
36
|
+
return -1;
|
|
37
|
+
};
|
|
38
|
+
const isDigit = (c) => digitZero(c) >= 0;
|
|
39
|
+
const ZWJ = "";
|
|
40
|
+
const OBJECT = "";
|
|
41
|
+
const TOKEN = /\{([\w-]+)\}/g;
|
|
42
|
+
/** The `{name}` tokens of a text whose names `known` accepts, in order. Other braces stay text. */
|
|
43
|
+
function inlineTokens(text, known) {
|
|
44
|
+
const out = [];
|
|
45
|
+
for (const m of text.matchAll(TOKEN)) {
|
|
46
|
+
if (!known(m[1])) continue;
|
|
47
|
+
out.push({
|
|
48
|
+
start: m.index,
|
|
49
|
+
end: m.index + m[0].length,
|
|
50
|
+
name: m[1]
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
/** Cursive scripts and scripts baked a word at a time: no letter spacing between their pieces. */
|
|
56
|
+
const SHAPED = /^\u200d?[\p{Script=Arabic}\p{Script=Syriac}\p{Script=Nko}\p{Script=Mandaic}\p{Script=Mongolian}\p{Script=Myanmar}\p{Script=Khmer}\p{Script=Tibetan}\p{Script=Devanagari}\p{Script=Bengali}\p{Script=Gurmukhi}\p{Script=Gujarati}\p{Script=Oriya}\p{Script=Tamil}\p{Script=Telugu}\p{Script=Kannada}\p{Script=Malayalam}\p{Script=Sinhala}]/u;
|
|
57
|
+
const TATWEEL = "ـ";
|
|
58
|
+
const LAM = 1604;
|
|
59
|
+
const ALEFS = /* @__PURE__ */ new Set([
|
|
60
|
+
1570,
|
|
61
|
+
1571,
|
|
62
|
+
1573,
|
|
63
|
+
1575
|
|
64
|
+
]);
|
|
6
65
|
/**
|
|
7
66
|
* Lays out a block of text. Lines break at `\n` and, with `wordWrap`, at spaces. Each character gets
|
|
8
67
|
* its advance, kerning and letter spacing, then the lines are aligned. Widths come from `measure`,
|
|
9
68
|
* so the same layout works for any renderer.
|
|
69
|
+
*
|
|
70
|
+
* Paragraphs that hold right-to-left characters go through the bidi pass, and so does every
|
|
71
|
+
* paragraph when `direction` is `rtl`. Words wrap in reading order, then each line is reordered for
|
|
72
|
+
* display. Arabic and Hebrew read right to left, and numbers and Latin words inside them keep
|
|
73
|
+
* reading left to right.
|
|
74
|
+
*
|
|
75
|
+
* Text is measured and drawn in clusters (see {@link clusters}). Marks stay with their letter.
|
|
76
|
+
* Joined Arabic runs and words of Burmese, Thai and the Indic scripts are one glyph each, so Canvas
|
|
77
|
+
* shapes them. `{name}` tokens listed in `inlines` become boxes for inline objects.
|
|
10
78
|
*/
|
|
11
79
|
function layoutText(text, options, measure) {
|
|
12
80
|
const spacing = options.letterSpacing ?? 0;
|
|
13
81
|
const useKerning = options.kerning ?? true;
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
82
|
+
const direction = options.direction ?? "auto";
|
|
83
|
+
const inlines = options.inlines ?? {};
|
|
84
|
+
const tabs = /* @__PURE__ */ new Map();
|
|
85
|
+
const tabFor = (c) => {
|
|
86
|
+
const z = options.tabularNumbers ? digitZero(c) : -1;
|
|
87
|
+
if (z < 0) return 0;
|
|
88
|
+
let w = tabs.get(z);
|
|
89
|
+
if (w === void 0) {
|
|
90
|
+
w = 0;
|
|
91
|
+
for (let d = 0; d < 10; d++) w = Math.max(w, measure.advance(String.fromCharCode(z + d)));
|
|
92
|
+
tabs.set(z, w);
|
|
93
|
+
}
|
|
94
|
+
return w;
|
|
95
|
+
};
|
|
96
|
+
const tab = options.tabularNumbers ? 1 : 0;
|
|
97
|
+
const advance = (c) => c[0] === OBJECT ? inlines[c.slice(1)]?.width ?? 0 : tabFor(c) || measure.advance(c);
|
|
98
|
+
const kern = (a, b) => useKerning && a[0] !== OBJECT && b[0] !== OBJECT && !(tab && (isDigit(a) || isDigit(b))) ? measure.kerning(a, b) : 0;
|
|
99
|
+
const between = (a, b) => spacing && SHAPED.test(a) && SHAPED.test(b) ? 0 : spacing;
|
|
100
|
+
const tabW = (options.tabSize ?? 8) * measure.advance(" ");
|
|
101
|
+
const step = (c, pos) => {
|
|
102
|
+
if (c !== " " || tabW <= 0) return advance(c);
|
|
103
|
+
const r = pos % tabW;
|
|
104
|
+
return r > tabW - 1e-6 || r < 1e-6 ? tabW : tabW - r;
|
|
105
|
+
};
|
|
17
106
|
const width = (chars, from = 0, to = chars.length) => {
|
|
18
107
|
let w = 0;
|
|
19
108
|
for (let i = from; i < to; i++) {
|
|
20
|
-
if (i > from) w += kern(chars[i - 1], chars[i]) +
|
|
21
|
-
w +=
|
|
109
|
+
if (i > from) w += kern(chars[i - 1], chars[i]) + between(chars[i - 1], chars[i]);
|
|
110
|
+
w += step(chars[i], w);
|
|
22
111
|
}
|
|
23
112
|
return w;
|
|
24
113
|
};
|
|
@@ -29,23 +118,59 @@ function layoutText(text, options, measure) {
|
|
|
29
118
|
let q = text.indexOf("\n", p);
|
|
30
119
|
if (q < 0) q = text.length;
|
|
31
120
|
const stop = q > p && text[q - 1] === "\r" ? q - 1 : q;
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
121
|
+
const para = text.slice(p, stop);
|
|
122
|
+
const bidi = direction === "rtl" || hasRtl(para);
|
|
123
|
+
const units = [];
|
|
124
|
+
const addText = (from, to) => {
|
|
125
|
+
for (const c of clusters(text.slice(from, to), options.wordBreak)) units.push({
|
|
126
|
+
...c,
|
|
127
|
+
start: c.start + from,
|
|
128
|
+
end: c.end + from,
|
|
129
|
+
inner: c.inner.map((o) => o + from)
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
let t = p;
|
|
133
|
+
for (const tok of inlineTokens(para, (n) => Object.hasOwn(inlines, n))) {
|
|
134
|
+
addText(t, p + tok.start);
|
|
135
|
+
t = p + tok.end;
|
|
136
|
+
units.push({
|
|
137
|
+
draw: OBJECT + tok.name,
|
|
138
|
+
start: p + tok.start,
|
|
139
|
+
end: t,
|
|
140
|
+
inner: [],
|
|
141
|
+
type: "ON",
|
|
142
|
+
breakBefore: false,
|
|
143
|
+
inline: tok.name
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
addText(t, stop);
|
|
147
|
+
if (options.wordBreak !== "keep-all") for (let i = 1; i < units.length; i++) {
|
|
148
|
+
const a = units[i - 1];
|
|
149
|
+
const b = units[i];
|
|
150
|
+
if (!a.inline && !b.inline) continue;
|
|
151
|
+
const left = a.inline ? null : Array.from(a.draw).pop() ?? "";
|
|
152
|
+
const right = b.inline ? null : String.fromCodePoint(b.draw.codePointAt(0));
|
|
153
|
+
if (left === null && right === null) b.breakBefore = true;
|
|
154
|
+
else if (left !== null) b.breakBefore = isWide(left) && !noLineEnd(left);
|
|
155
|
+
else if (right !== null) b.breakBefore = isWide(right) && !noLineStart(right);
|
|
39
156
|
}
|
|
157
|
+
const chars = units.map((u) => u.draw);
|
|
158
|
+
const offs = units.map((u) => u.start);
|
|
40
159
|
offs.push(stop);
|
|
160
|
+
const types = units.map((u) => u.type);
|
|
161
|
+
const level = bidi ? paragraphLevel(types, direction) : 0;
|
|
162
|
+
const levels = bidi ? resolveLevels(types, level, chars) : null;
|
|
41
163
|
const ranges = [];
|
|
42
|
-
if (options.wordWrap) wrap(chars, max, options.breakWords ?? false, (a, b) => width(chars, a, b), ranges);
|
|
164
|
+
if (options.wordWrap) wrap(chars, max, options.breakWords ?? false, (a, b) => width(chars, a, b), (i) => units[i].breakBefore, ranges);
|
|
43
165
|
else ranges.push([0, chars.length]);
|
|
44
166
|
ranges.forEach(([from, to], k) => {
|
|
45
167
|
const last = k === ranges.length - 1;
|
|
46
168
|
rows.push({
|
|
47
169
|
chars,
|
|
48
170
|
offs,
|
|
171
|
+
units,
|
|
172
|
+
levels,
|
|
173
|
+
level,
|
|
49
174
|
from,
|
|
50
175
|
to,
|
|
51
176
|
next: last ? chars.length : ranges[k + 1][0],
|
|
@@ -54,53 +179,296 @@ function layoutText(text, options, measure) {
|
|
|
54
179
|
});
|
|
55
180
|
p = q + 1;
|
|
56
181
|
}
|
|
57
|
-
|
|
58
|
-
const
|
|
182
|
+
/** Spreads `extra` over a line: kashida in Arabic words first, then spaces, then between characters. */
|
|
183
|
+
const justify = (row, extra) => {
|
|
184
|
+
if (extra <= 1e-6 || row.to - row.from < 2) return null;
|
|
185
|
+
const out = {
|
|
186
|
+
draw: /* @__PURE__ */ new Map(),
|
|
187
|
+
add: /* @__PURE__ */ new Map()
|
|
188
|
+
};
|
|
189
|
+
const tw = options.kashida === false ? 0 : measure.advance(TATWEEL);
|
|
190
|
+
const points = [];
|
|
191
|
+
for (let u = row.from; u < row.to; u++) {
|
|
192
|
+
const unit = row.units[u];
|
|
193
|
+
if (unit.type !== "AL" || unit.inline) continue;
|
|
194
|
+
for (const o of unit.inner) if (!(text.codePointAt(o - 1) === LAM && ALEFS.has(text.codePointAt(o)))) points.push([u, o]);
|
|
195
|
+
}
|
|
196
|
+
if (tw > 0 && points.length) {
|
|
197
|
+
const n = Math.min(Math.floor(extra / tw), points.length * 2);
|
|
198
|
+
const count = /* @__PURE__ */ new Map();
|
|
199
|
+
for (let i = 0; i < n; i++) {
|
|
200
|
+
const [u, o] = points[(i < points.length ? Math.floor((i + .5) * points.length / Math.min(n, points.length)) : i) % points.length];
|
|
201
|
+
const list = count.get(u) ?? [];
|
|
202
|
+
list.push(o);
|
|
203
|
+
count.set(u, list);
|
|
204
|
+
}
|
|
205
|
+
for (const [u, cuts] of count) {
|
|
206
|
+
const unit = row.units[u];
|
|
207
|
+
const lead = unit.draw.startsWith(ZWJ) ? 1 : 0;
|
|
208
|
+
let d = unit.draw;
|
|
209
|
+
for (const o of [...cuts].sort((a, b) => b - a)) {
|
|
210
|
+
const at = o - unit.start + lead;
|
|
211
|
+
d = d.slice(0, at) + TATWEEL + d.slice(at);
|
|
212
|
+
}
|
|
213
|
+
extra -= measure.advance(d) - measure.advance(unit.draw);
|
|
214
|
+
out.draw.set(u, d);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (extra > 1e-6) {
|
|
218
|
+
const spaces = [];
|
|
219
|
+
for (let u = row.from; u < row.to; u++) if (isSpace(row.chars[u])) spaces.push(u);
|
|
220
|
+
const at = spaces.length ? spaces : Array.from({ length: row.to - row.from - 1 }, (_, i) => row.from + i);
|
|
221
|
+
for (const u of at) out.add.set(u, extra / at.length);
|
|
222
|
+
}
|
|
223
|
+
return out;
|
|
224
|
+
};
|
|
225
|
+
const maxLines = Math.max(0, Math.floor(options.maxLines ?? 0));
|
|
226
|
+
const limit = options.wordWrap ? max : options.maxWidth ?? Number.POSITIVE_INFINITY;
|
|
227
|
+
const ellipsis = options.ellipsis === void 0 ? "…" : options.ellipsis ?? "";
|
|
228
|
+
const cutWide = options.maxWidth !== void 0 || maxLines > 0;
|
|
229
|
+
let truncated = false;
|
|
230
|
+
const cut = (row, force) => {
|
|
231
|
+
const ellW = ellipsis ? advance(ellipsis) : 0;
|
|
232
|
+
const fits = (k) => (k > row.from ? width(row.chars, row.from, k) + (ellipsis ? spacing : 0) : 0) + ellW <= limit + 1e-6;
|
|
233
|
+
let k = row.to;
|
|
234
|
+
if (!force && width(row.chars, row.from, k) <= limit + 1e-6) return row;
|
|
235
|
+
while (k > row.from && !fits(k)) k--;
|
|
236
|
+
while (k > row.from && isSpace(row.chars[k - 1])) k--;
|
|
237
|
+
const units = row.units.slice(0, k);
|
|
238
|
+
const at = row.offs[k];
|
|
239
|
+
if (ellipsis) units.push({
|
|
240
|
+
draw: ellipsis,
|
|
241
|
+
start: at,
|
|
242
|
+
end: at,
|
|
243
|
+
inner: [],
|
|
244
|
+
type: "ON",
|
|
245
|
+
breakBefore: false
|
|
246
|
+
});
|
|
247
|
+
const chars = units.map((u) => u.draw);
|
|
248
|
+
const offs = units.map((u) => u.start);
|
|
249
|
+
offs.push(at);
|
|
250
|
+
truncated = true;
|
|
251
|
+
return {
|
|
252
|
+
chars,
|
|
253
|
+
offs,
|
|
254
|
+
units,
|
|
255
|
+
levels: row.levels ? resolveLevels(units.map((u) => u.type), row.level, chars) : null,
|
|
256
|
+
level: row.level,
|
|
257
|
+
from: row.from,
|
|
258
|
+
to: units.length,
|
|
259
|
+
next: units.length,
|
|
260
|
+
last: true,
|
|
261
|
+
cut: true
|
|
262
|
+
};
|
|
263
|
+
};
|
|
264
|
+
if (maxLines > 0 && rows.length > maxLines) {
|
|
265
|
+
rows.length = maxLines;
|
|
266
|
+
rows[maxLines - 1] = cut(rows[maxLines - 1], true);
|
|
267
|
+
}
|
|
268
|
+
if (cutWide && Number.isFinite(limit)) {
|
|
269
|
+
for (let i = 0; i < rows.length; i++) if (!rows[i].cut) rows[i] = cut(rows[i], false);
|
|
270
|
+
}
|
|
271
|
+
const visual = (row) => {
|
|
272
|
+
const { from, to } = row;
|
|
273
|
+
const lv = row.levels.slice(from, to);
|
|
274
|
+
resetWhitespace(row.units.slice(from, to).map((u) => u.type), lv, row.level);
|
|
275
|
+
return {
|
|
276
|
+
order: visualOrder(lv).map((k) => k + from),
|
|
277
|
+
lv: (u) => lv[u - from]
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
const gap = (row, v, a, b) => {
|
|
281
|
+
const odd = v.lv(a) & 1;
|
|
282
|
+
let k = 0;
|
|
283
|
+
if (odd === (v.lv(b) & 1)) {
|
|
284
|
+
if (!odd && b === a + 1) k = kern(row.chars[a], row.chars[b]);
|
|
285
|
+
else if (odd && a === b + 1) k = kern(row.chars[b], row.chars[a]);
|
|
286
|
+
}
|
|
287
|
+
return k + between(row.chars[a], row.chars[b]);
|
|
288
|
+
};
|
|
289
|
+
const visuals = rows.map((r) => r.levels ? visual(r) : null);
|
|
290
|
+
const widths = rows.map((r, i) => {
|
|
291
|
+
const v = visuals[i];
|
|
292
|
+
if (!v) return width(r.chars, r.from, r.to);
|
|
293
|
+
let w = 0;
|
|
294
|
+
v.order.forEach((u, k) => {
|
|
295
|
+
if (k > 0) w += gap(r, v, v.order[k - 1], u);
|
|
296
|
+
w += step(r.chars[u], w);
|
|
297
|
+
});
|
|
298
|
+
return w;
|
|
299
|
+
});
|
|
300
|
+
const align = options.align ?? "start";
|
|
301
|
+
const target = options.wordWrap ? max : Math.max(0, ...widths);
|
|
302
|
+
const adjust = rows.map((r, i) => align === "justify" && !r.last && !r.cut ? justify(r, target - widths[i]) : null);
|
|
303
|
+
adjust.forEach((a, i) => {
|
|
304
|
+
if (a) widths[i] = target;
|
|
305
|
+
});
|
|
306
|
+
let ascent = options.ascent;
|
|
307
|
+
let descent = options.descent;
|
|
308
|
+
if (options.fitInk !== false) {
|
|
309
|
+
const seen = /* @__PURE__ */ new Set();
|
|
310
|
+
for (const row of rows) {
|
|
311
|
+
if (seen.has(row.units)) continue;
|
|
312
|
+
seen.add(row.units);
|
|
313
|
+
for (const u of row.units) {
|
|
314
|
+
const ink = (u.inline ? inlines[u.inline] : null) ?? (measure.ink && !isSpace(u.draw) ? measure.ink(u.draw) : null);
|
|
315
|
+
if (!ink) continue;
|
|
316
|
+
ascent = Math.max(ascent, ink.ascent);
|
|
317
|
+
descent = Math.max(descent, ink.descent);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
59
321
|
const fontHeight = ascent + descent;
|
|
60
322
|
const lineHeight = options.lineHeight ?? fontHeight;
|
|
61
323
|
const shift = Math.max(0, (lineHeight - fontHeight) / 2);
|
|
62
|
-
const widths = rows.map((r) => width(r.chars, r.from, r.to));
|
|
63
324
|
const blockWidth = Math.max(0, ...widths);
|
|
64
|
-
const align = options.align ?? "left";
|
|
65
325
|
const glyphs = [];
|
|
66
326
|
const out = [];
|
|
327
|
+
/**
|
|
328
|
+
* Where a caret inside a cluster sits, from its start: the middle of the shaped prefix and the
|
|
329
|
+
* whole less the shaped rest. A word that reorders its vowels (Burmese, Indic) shapes its pieces
|
|
330
|
+
* a little differently, and the average lands closer. Never moves back past `done`.
|
|
331
|
+
*/
|
|
332
|
+
const innerX = (u, o, full, done) => {
|
|
333
|
+
const join = u.type === "AL" ? ZWJ : "";
|
|
334
|
+
const head = measure.advance(text.slice(u.start, o) + join);
|
|
335
|
+
const tail = measure.advance(join + text.slice(o, u.end));
|
|
336
|
+
return Math.min(full, Math.max(done, (head + full - tail) / 2));
|
|
337
|
+
};
|
|
338
|
+
const placed = (u, char, x, y, line) => u.inline ? {
|
|
339
|
+
char: OBJECT,
|
|
340
|
+
x,
|
|
341
|
+
y,
|
|
342
|
+
line,
|
|
343
|
+
index: u.start,
|
|
344
|
+
inline: u.inline
|
|
345
|
+
} : {
|
|
346
|
+
char,
|
|
347
|
+
x,
|
|
348
|
+
y,
|
|
349
|
+
line,
|
|
350
|
+
index: u.start
|
|
351
|
+
};
|
|
67
352
|
rows.forEach((row, li) => {
|
|
68
353
|
const { chars, offs, from, to } = row;
|
|
354
|
+
const rtl = row.level === 1;
|
|
69
355
|
const y = li * lineHeight + ascent + shift;
|
|
70
356
|
const lw = widths[li];
|
|
71
|
-
const
|
|
357
|
+
const adj = adjust[li];
|
|
358
|
+
const drawOf = (u) => adj?.draw.get(u) ?? chars[u];
|
|
359
|
+
const extra = (u) => adj?.add.get(u) ?? 0;
|
|
360
|
+
const side = align === "start" || align === "justify" ? rtl ? "right" : "left" : align === "end" ? rtl ? "left" : "right" : align;
|
|
361
|
+
const x0 = side === "center" ? (blockWidth - lw) / 2 : side === "right" ? blockWidth - lw : 0;
|
|
72
362
|
const offsets = [offs[from]];
|
|
73
363
|
const carets = [x0];
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
364
|
+
const v = visuals[li];
|
|
365
|
+
let runs;
|
|
366
|
+
let leads;
|
|
367
|
+
if (!v) {
|
|
368
|
+
let x = x0;
|
|
369
|
+
for (let i = from; i < to; i++) {
|
|
370
|
+
const c = chars[i];
|
|
371
|
+
if (i > from) {
|
|
372
|
+
const g = kern(chars[i - 1], c) + between(chars[i - 1], c);
|
|
373
|
+
offsets.push(offs[i]);
|
|
374
|
+
carets.push(x + g / 2);
|
|
375
|
+
x += g;
|
|
376
|
+
}
|
|
377
|
+
const centre = tab && isDigit(c) ? (tabFor(c) - measure.advance(c)) / 2 : 0;
|
|
378
|
+
const unit = row.units[i];
|
|
379
|
+
const d = drawOf(i);
|
|
380
|
+
const w = step(d, x - x0);
|
|
381
|
+
const stretch = d === c ? 1 : w / advance(c);
|
|
382
|
+
glyphs.push(placed(unit, d, x + centre, y, li));
|
|
383
|
+
let done = 0;
|
|
384
|
+
for (const o of unit.inner) {
|
|
385
|
+
done = innerX(unit, o, advance(c), done);
|
|
386
|
+
offsets.push(o);
|
|
387
|
+
carets.push(x + done * stretch);
|
|
388
|
+
}
|
|
389
|
+
x += w + extra(i);
|
|
82
390
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
391
|
+
if (to > from) {
|
|
392
|
+
offsets.push(offs[to]);
|
|
393
|
+
carets.push(x);
|
|
394
|
+
}
|
|
395
|
+
for (let i = to; i < row.next; i++) {
|
|
396
|
+
x += advance(chars[i]) + spacing;
|
|
397
|
+
offsets.push(offs[i + 1]);
|
|
398
|
+
carets.push(x);
|
|
399
|
+
}
|
|
400
|
+
} else {
|
|
401
|
+
const left = [];
|
|
402
|
+
const right = [];
|
|
403
|
+
let x = x0;
|
|
404
|
+
runs = [];
|
|
405
|
+
v.order.forEach((u, k) => {
|
|
406
|
+
const prev = v.order[k - 1];
|
|
407
|
+
if (k > 0) x += gap(row, v, prev, u);
|
|
408
|
+
const c = chars[u];
|
|
409
|
+
const d = drawOf(u);
|
|
410
|
+
const odd = (v.lv(u) & 1) === 1;
|
|
411
|
+
const centre = tab && isDigit(c) ? (tabFor(c) - measure.advance(c)) / 2 : 0;
|
|
412
|
+
glyphs.push(placed(row.units[u], odd ? mirror(d) : d, x + centre, y, li));
|
|
413
|
+
left[u] = x;
|
|
414
|
+
x += step(d, x - x0);
|
|
415
|
+
right[u] = x;
|
|
416
|
+
x += extra(u);
|
|
417
|
+
const run = runs?.[runs.length - 1];
|
|
418
|
+
const unit = row.units[u];
|
|
419
|
+
if (run && run.rtl === odd && (odd ? u === prev - 1 : u === prev + 1)) {
|
|
420
|
+
run.start = Math.min(run.start, unit.start);
|
|
421
|
+
run.end = Math.max(run.end, unit.end);
|
|
422
|
+
run.right = x;
|
|
423
|
+
} else runs?.push({
|
|
424
|
+
start: unit.start,
|
|
425
|
+
end: unit.end,
|
|
426
|
+
left: left[u],
|
|
427
|
+
right: x,
|
|
428
|
+
rtl: odd
|
|
429
|
+
});
|
|
90
430
|
});
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
offsets.
|
|
95
|
-
carets.
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
431
|
+
const odd = (u) => (v.lv(u) & 1) === 1;
|
|
432
|
+
const lead = (u) => odd(u) ? right[u] : left[u];
|
|
433
|
+
const trail = (u) => odd(u) ? left[u] : right[u];
|
|
434
|
+
offsets.length = 0;
|
|
435
|
+
carets.length = 0;
|
|
436
|
+
leads = [];
|
|
437
|
+
if (to === from) {
|
|
438
|
+
offsets.push(offs[from]);
|
|
439
|
+
carets.push(rtl ? x0 + lw : x0);
|
|
440
|
+
leads.push(carets[0]);
|
|
441
|
+
}
|
|
442
|
+
for (let u = from; u < to; u++) {
|
|
443
|
+
offsets.push(offs[u]);
|
|
444
|
+
carets.push(u === from ? lead(u) : trail(u - 1));
|
|
445
|
+
leads.push(lead(u));
|
|
446
|
+
const unit = row.units[u];
|
|
447
|
+
const full = right[u] - left[u];
|
|
448
|
+
let done = 0;
|
|
449
|
+
const stretch = full / (advance(chars[u]) || full || 1);
|
|
450
|
+
for (const o of unit.inner) {
|
|
451
|
+
done = innerX(unit, o, advance(chars[u]), done);
|
|
452
|
+
offsets.push(o);
|
|
453
|
+
carets.push(odd(u) ? right[u] - done * stretch : left[u] + done * stretch);
|
|
454
|
+
leads.push(carets[carets.length - 1]);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (to > from) {
|
|
458
|
+
offsets.push(offs[to]);
|
|
459
|
+
carets.push(trail(to - 1));
|
|
460
|
+
leads.push(carets[carets.length - 1]);
|
|
461
|
+
}
|
|
462
|
+
let hx = rtl ? x0 : x0 + lw;
|
|
463
|
+
for (let i = to; i < row.next; i++) {
|
|
464
|
+
hx += rtl ? -(advance(chars[i]) + spacing) : advance(chars[i]) + spacing;
|
|
465
|
+
offsets.push(offs[i + 1]);
|
|
466
|
+
carets.push(hx);
|
|
467
|
+
leads.push(hx);
|
|
468
|
+
}
|
|
101
469
|
}
|
|
102
470
|
out.push({
|
|
103
|
-
text:
|
|
471
|
+
text: text.slice(offs[from], offs[to]) + (row.cut && ellipsis ? ellipsis : ""),
|
|
104
472
|
width: lw,
|
|
105
473
|
y,
|
|
106
474
|
x: x0,
|
|
@@ -109,12 +477,19 @@ function layoutText(text, options, measure) {
|
|
|
109
477
|
start: offs[from],
|
|
110
478
|
end: offs[row.next],
|
|
111
479
|
wrapped: !row.last,
|
|
480
|
+
truncated: !!row.cut,
|
|
112
481
|
offsets,
|
|
113
|
-
carets
|
|
482
|
+
carets,
|
|
483
|
+
rtl,
|
|
484
|
+
...runs ? {
|
|
485
|
+
runs,
|
|
486
|
+
leads
|
|
487
|
+
} : {}
|
|
114
488
|
});
|
|
115
489
|
});
|
|
116
490
|
return {
|
|
117
491
|
glyphs,
|
|
492
|
+
truncated,
|
|
118
493
|
lines: out,
|
|
119
494
|
width: blockWidth,
|
|
120
495
|
height: rows.length ? Math.max(lineHeight, fontHeight) + (rows.length - 1) * lineHeight : 0,
|
|
@@ -123,12 +498,16 @@ function layoutText(text, options, measure) {
|
|
|
123
498
|
lineHeight
|
|
124
499
|
};
|
|
125
500
|
}
|
|
126
|
-
/**
|
|
127
|
-
|
|
501
|
+
/**
|
|
502
|
+
* Greedy word wrap of one paragraph into `[from, to)` ranges of its characters. Spaces at a break
|
|
503
|
+
* are dropped. Lines also break where `breakBefore` allows it.
|
|
504
|
+
*/
|
|
505
|
+
function wrap(chars, max, breakWords, width, breakBefore, out) {
|
|
128
506
|
const tokens = [];
|
|
129
507
|
for (let i = 0; i < chars.length; i++) {
|
|
130
508
|
const last = tokens[tokens.length - 1];
|
|
131
|
-
|
|
509
|
+
const space = isSpace(chars[i]);
|
|
510
|
+
if (last && isSpace(chars[last[0]]) === space && (space || !breakBefore(i))) last[1] = i + 1;
|
|
132
511
|
else tokens.push([i, i + 1]);
|
|
133
512
|
}
|
|
134
513
|
let s = 0;
|
|
@@ -193,20 +572,31 @@ function caretAtPoint(layout, x, y, isStop) {
|
|
|
193
572
|
const line = lines[li];
|
|
194
573
|
let best = 0;
|
|
195
574
|
let dist = Infinity;
|
|
575
|
+
let leading = false;
|
|
196
576
|
for (let i = 0; i < line.offsets.length; i++) {
|
|
197
577
|
if (isStop && i > 0 && i < line.offsets.length - 1 && !isStop(line.offsets[i])) continue;
|
|
198
578
|
const d = Math.abs(line.carets[i] - x);
|
|
199
579
|
if (d < dist) {
|
|
200
580
|
dist = d;
|
|
201
581
|
best = i;
|
|
582
|
+
leading = false;
|
|
583
|
+
}
|
|
584
|
+
const l = line.leads?.[i];
|
|
585
|
+
if (l !== void 0 && Math.abs(l - x) < dist - 1e-6) {
|
|
586
|
+
dist = Math.abs(l - x);
|
|
587
|
+
best = i;
|
|
588
|
+
leading = true;
|
|
202
589
|
}
|
|
203
590
|
}
|
|
204
591
|
const last = best === line.offsets.length - 1;
|
|
592
|
+
const run = line.runs?.find((r) => x >= r.left && x < r.right);
|
|
593
|
+
const cx = leading ? line.leads[best] : line.carets[best];
|
|
205
594
|
return {
|
|
206
595
|
index: line.offsets[best],
|
|
207
596
|
line: li,
|
|
208
597
|
upstream: last && line.wrapped && line.offsets.length > 1,
|
|
209
|
-
before: x <
|
|
598
|
+
before: run?.rtl ?? line.rtl ? x > cx : x < cx,
|
|
599
|
+
leading
|
|
210
600
|
};
|
|
211
601
|
}
|
|
212
602
|
/** The line a caret at `index` is on. At a wrap, `upstream` picks the end of the first line over the start of the next. */
|
|
@@ -220,19 +610,61 @@ function lineOfIndex(layout, index, upstream = false) {
|
|
|
220
610
|
}
|
|
221
611
|
return lines.length - 1;
|
|
222
612
|
}
|
|
223
|
-
/**
|
|
224
|
-
|
|
225
|
-
|
|
613
|
+
/**
|
|
614
|
+
* The x of a caret at `index` on line `li`: the nearest caret stop at or before it. `leading` puts
|
|
615
|
+
* it before the character after it where the direction changes (see {@link LayoutLine.leads}).
|
|
616
|
+
*/
|
|
617
|
+
function caretX(layout, li, index, leading = false) {
|
|
618
|
+
const line = layout.lines[li];
|
|
619
|
+
const offsets = line.offsets;
|
|
620
|
+
const carets = leading && line.leads ? line.leads : line.carets;
|
|
226
621
|
if (index <= offsets[0]) return carets[0];
|
|
227
622
|
for (let i = offsets.length - 1; i >= 0; i--) if (offsets[i] <= index) return carets[i];
|
|
228
623
|
return carets[0];
|
|
229
624
|
}
|
|
230
|
-
/**
|
|
231
|
-
|
|
625
|
+
/**
|
|
626
|
+
* The caret stop one step left (`dir` -1) or right (1) on screen from `index`, on the same line.
|
|
627
|
+
* In a line that mixes directions this follows what the eye sees, not the order of the text. The
|
|
628
|
+
* arrow keys then move the way they point, and a caret can reach both places an offset has where
|
|
629
|
+
* the direction changes. Returns null on a line that runs left to right only, or at the line's edge:
|
|
630
|
+
* step through the text instead there. `isStop` filters the stops, for example to grapheme or word
|
|
631
|
+
* boundaries.
|
|
632
|
+
*/
|
|
633
|
+
function caretVisual(layout, index, dir, upstream = false, isStop, leading = false) {
|
|
634
|
+
const li = lineOfIndex(layout, index, upstream);
|
|
635
|
+
const line = layout.lines[li];
|
|
636
|
+
if (!line.runs) return null;
|
|
637
|
+
const x = caretX(layout, li, index, leading);
|
|
638
|
+
let best = null;
|
|
639
|
+
let bestD = Infinity;
|
|
640
|
+
const consider = (o, cx, lead) => {
|
|
641
|
+
if (isStop && !isStop(o)) return;
|
|
642
|
+
const d = (cx - x) * Math.sign(dir);
|
|
643
|
+
if (d <= 1e-6) return;
|
|
644
|
+
if (d < bestD - 1e-6 || Math.abs(d - bestD) <= 1e-6 && best && Math.abs(o - index) < Math.abs(best.index - index)) {
|
|
645
|
+
best = {
|
|
646
|
+
index: o,
|
|
647
|
+
leading: lead
|
|
648
|
+
};
|
|
649
|
+
bestD = d;
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
line.offsets.forEach((o, i) => {
|
|
653
|
+
consider(o, line.carets[i], false);
|
|
654
|
+
const l = line.leads?.[i];
|
|
655
|
+
if (l !== void 0 && Math.abs(l - line.carets[i]) > 1e-6) consider(o, l, true);
|
|
656
|
+
});
|
|
657
|
+
return best;
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Where to draw a caret at `index`, in the layout's units. `leading` puts it before the character
|
|
661
|
+
* after it where the direction changes.
|
|
662
|
+
*/
|
|
663
|
+
function caretRect(layout, index, upstream = false, leading = false) {
|
|
232
664
|
const li = lineOfIndex(layout, index, upstream);
|
|
233
665
|
const line = layout.lines[li];
|
|
234
666
|
return {
|
|
235
|
-
x: caretX(layout, li, index),
|
|
667
|
+
x: caretX(layout, li, index, leading),
|
|
236
668
|
top: line.y - layout.ascent,
|
|
237
669
|
bottom: line.y + layout.descent,
|
|
238
670
|
line: li
|
|
@@ -253,6 +685,16 @@ function rangeRects(layout, from, to, newline) {
|
|
|
253
685
|
const hard = !line.wrapped && li < layout.lines.length - 1;
|
|
254
686
|
const reach = hard ? line.end + 1 : line.end;
|
|
255
687
|
if (b <= line.start || a >= reach || a === line.end && !hard) return;
|
|
688
|
+
if (line.runs) {
|
|
689
|
+
for (const [x0, x1] of bidiSpans(layout, li, a, b, hard ? nl : 0)) out.push({
|
|
690
|
+
x: x0,
|
|
691
|
+
y: line.top,
|
|
692
|
+
width: x1 - x0,
|
|
693
|
+
height: line.bottom - line.top,
|
|
694
|
+
line: li
|
|
695
|
+
});
|
|
696
|
+
return;
|
|
697
|
+
}
|
|
256
698
|
const x0 = caretX(layout, li, Math.max(a, line.start));
|
|
257
699
|
let x1 = caretX(layout, li, Math.min(b, line.end));
|
|
258
700
|
if (hard && b > line.end) x1 += nl;
|
|
@@ -266,5 +708,46 @@ function rangeRects(layout, from, to, newline) {
|
|
|
266
708
|
});
|
|
267
709
|
return out;
|
|
268
710
|
}
|
|
711
|
+
/**
|
|
712
|
+
* The selected spans of a line that went through the bidi pass, left to right. A range that is one
|
|
713
|
+
* piece in reading order can be several pieces on screen when it crosses a change of direction.
|
|
714
|
+
*/
|
|
715
|
+
function bidiSpans(layout, li, a, b, newline) {
|
|
716
|
+
const line = layout.lines[li];
|
|
717
|
+
const runs = line.runs;
|
|
718
|
+
const spans = [];
|
|
719
|
+
const edge = (run, o) => {
|
|
720
|
+
if (o <= run.start) return run.rtl ? run.right : run.left;
|
|
721
|
+
if (o >= run.end) return run.rtl ? run.left : run.right;
|
|
722
|
+
return caretX(layout, li, o);
|
|
723
|
+
};
|
|
724
|
+
let ink = line.start;
|
|
725
|
+
for (const run of runs) {
|
|
726
|
+
ink = Math.max(ink, run.end);
|
|
727
|
+
const s = Math.max(a, run.start);
|
|
728
|
+
const e = Math.min(b, run.end);
|
|
729
|
+
if (e <= s) continue;
|
|
730
|
+
const p = edge(run, s);
|
|
731
|
+
const q = edge(run, e);
|
|
732
|
+
spans.push([Math.min(p, q), Math.max(p, q)]);
|
|
733
|
+
}
|
|
734
|
+
const outer = line.rtl ? line.x : line.x + line.width;
|
|
735
|
+
let reach = outer;
|
|
736
|
+
if (b > ink && line.end > ink) reach = caretX(layout, li, Math.min(b, line.end));
|
|
737
|
+
if (newline > 0 && b > line.end) reach += line.rtl ? -newline : newline;
|
|
738
|
+
if (reach !== outer) {
|
|
739
|
+
const from = a > ink ? caretX(layout, li, a) : outer;
|
|
740
|
+
spans.push([Math.min(from, reach), Math.max(from, reach)]);
|
|
741
|
+
}
|
|
742
|
+
spans.sort((x, y) => x[0] - y[0]);
|
|
743
|
+
const tol = (layout.ascent + layout.descent) / 8;
|
|
744
|
+
const merged = [];
|
|
745
|
+
for (const s of spans) {
|
|
746
|
+
const last = merged[merged.length - 1];
|
|
747
|
+
if (last && s[0] <= last[1] + tol) last[1] = Math.max(last[1], s[1]);
|
|
748
|
+
else if (s[1] > s[0]) merged.push([s[0], s[1]]);
|
|
749
|
+
}
|
|
750
|
+
return merged;
|
|
751
|
+
}
|
|
269
752
|
//#endregion
|
|
270
|
-
export { caretAtPoint, caretRect, caretX, layoutText, lineOfIndex, rangeRects };
|
|
753
|
+
export { caretAtPoint, caretRect, caretVisual, caretX, inlineTokens, layoutText, lineOfIndex, rangeRects };
|