ltms-reader 0.0.0-stage → 0.1.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 +136 -0
- package/LICENSE +21 -0
- package/LICENSE-UNICODE +49 -0
- package/README.md +284 -2
- package/bin/ltms-reader.mjs +16 -0
- package/package.json +32 -4
- package/src/app.mjs +415 -0
- package/src/bidi-table.mjs +354 -0
- package/src/bidi.mjs +251 -0
- package/src/engine.mjs +505 -0
- package/src/guard.mjs +116 -0
- package/src/helper.mjs +127 -0
- package/src/layout.mjs +181 -0
- package/src/lines.mjs +18 -0
- package/src/main.mjs +133 -0
- package/src/reading.mjs +165 -0
- package/src/screen-process.mjs +6 -0
- package/src/screen.mjs +127 -0
- package/src/sources.mjs +134 -0
- package/src/start.mjs +123 -0
- package/src/text.mjs +712 -0
- package/src/view.mjs +337 -0
- package/src/views.mjs +518 -0
- package/src/width-table.mjs +101 -0
package/src/text.mjs
ADDED
|
@@ -0,0 +1,712 @@
|
|
|
1
|
+
// src/text.mjs — what of a transcript's text reaches the terminal, and how many columns it takes there.
|
|
2
|
+
//
|
|
3
|
+
// A transcript can hold escape sequences (tool output often does: colours, cursor moves, a title, OSC 52, which writes
|
|
4
|
+
// the clipboard). None reaches the terminal as such: every C0 and C1 control character and DEL, but the new line and the
|
|
5
|
+
// tab, is shown as a visible escape (ESC as ␛, the rest as \xNN), so a transcript cannot move the cursor, set the title,
|
|
6
|
+
// write the clipboard or leave the terminal in another mode. The new line ends a line and the tab becomes spaces (by
|
|
7
|
+
// the callers below); neither is written to the terminal as itself. What reorders or hides text is shown the same way
|
|
8
|
+
// (R.F5, decided S376), as \uXXXX or \u{XXXXX}: the bidirectional embeddings, overrides and isolates (U+202A to U+202E,
|
|
9
|
+
// U+2066 to U+2069), which on a terminal that lays bidirectional text out itself reorder the rest of their row, the
|
|
10
|
+
// reader's own words after them too; the byte order mark (U+FEFF); the line and paragraph separators (U+2028, U+2029);
|
|
11
|
+
// and the tag characters (U+E0000 to U+E007F). The light marks Hebrew and Arabic text use, LRM (U+200E), RLM (U+200F)
|
|
12
|
+
// and ALM (U+061C), are written as they are.
|
|
13
|
+
//
|
|
14
|
+
// Width, by grapheme cluster (Intl.Segmenter): a cluster takes two columns when it holds a wide or fullwidth character
|
|
15
|
+
// (East_Asian_Width W or F: src/width-table.mjs, generated from Unicode's EastAsianWidth.txt), when it is an emoji
|
|
16
|
+
// presentation sequence (an emoji and U+FE0F) or a flag (regional indicators); none when every character in it takes
|
|
17
|
+
// none (combining marks, niqqud and cantillation among them, and format characters); one otherwise. A line is cut by
|
|
18
|
+
// columns, between clusters, never inside one; a space where it is cut stays at the end of the line before (lineSpans:
|
|
19
|
+
// V7, round 5, as UAX #14 has it).
|
|
20
|
+
//
|
|
21
|
+
// Right to left (R1, decided by the measurement of S377 on three terminals; Y1, round 6, by that of S378 on four): there
|
|
22
|
+
// are two modes (displayOrder, below): "reader", the default in every terminal, in which the reader lays the text out
|
|
23
|
+
// itself, by the Unicode Bidirectional Algorithm (src/bidi.mjs), and writes it in visual order (src/screen.mjs writes a
|
|
24
|
+
// row of it that holds right-to-left text inside a left-to-right override, so that a terminal that lays text out itself,
|
|
25
|
+
// as Konsole does, keeps that order); and "terminal", LTMS_READER_BIDI's choice, in which the text is written as stored,
|
|
26
|
+
// with a right-to-left mark (RLM) at the end of each right-to-left paragraph, which on Konsole puts the paragraph's end
|
|
27
|
+
// punctuation where it belongs. rtlModeOf reads the choice; no terminal is detected. A paragraph here is a
|
|
28
|
+
// segment of a row (a list's cell, the reader's own words before it, a line cut to the row's width), or a text a view
|
|
29
|
+
// wraps into lines (a record's text line, a message, an error, the reader's own words), each line of which names it: its
|
|
30
|
+
// levels are resolved once, and each line is reordered from them, whole (W1, round 4; V7, round 5; UAX #9 section 3.4:
|
|
31
|
+
// paragraphLine). A path or a URL in a paragraph is laid out as one left-to-right unit (UAX #9 HL4).
|
|
32
|
+
import { AL, AN, BN, ISO, L as LTR_CLASS, NSM, R, classOf, classesOf, lineLevels, mirrorOf, paragraphLevel, resolveLevels, visualOrder } from './bidi.mjs';
|
|
33
|
+
import { WIDE } from './width-table.mjs';
|
|
34
|
+
|
|
35
|
+
/** C0 but the tab and the new line, DEL, C1; and what reorders or hides text: the bidirectional embeddings, overrides
|
|
36
|
+
* and isolates, the line and paragraph separators, the byte order mark, the tag characters. */
|
|
37
|
+
const CONTROL = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u2028-\u202e\u2066-\u2069\ufeff\u{e0000}-\u{e007f}]/gu;
|
|
38
|
+
|
|
39
|
+
/** One such character as it is shown: ESC as ␛ (U+241B); the others of C0, DEL and C1 as \xNN; the rest as \uXXXX, or
|
|
40
|
+
* \u{XXXXX} past U+FFFF. */
|
|
41
|
+
function shown(c) {
|
|
42
|
+
if (c === '\x1b') return '\u241b';
|
|
43
|
+
const cp = c.codePointAt(0);
|
|
44
|
+
if (cp <= 0xff) return `\\x${cp.toString(16).padStart(2, '0')}`;
|
|
45
|
+
return cp <= 0xffff ? `\\u${cp.toString(16).padStart(4, '0')}` : `\\u{${cp.toString(16)}}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** s with every control character but \n and \t, and every character that reorders or hides text, as a visible
|
|
49
|
+
* escape: ESC as ␛ (U+241B), the others of C0, DEL and C1 as \xNN, the rest as \uXXXX (\u{XXXXX}). */
|
|
50
|
+
export function visible(s) {
|
|
51
|
+
const t = typeof s === 'string' ? s : String(s);
|
|
52
|
+
return t.replace(CONTROL, shown);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Whether a code point is East_Asian_Width W or F (a binary search of the generated ranges). */
|
|
56
|
+
export function isWide(cp) {
|
|
57
|
+
if (cp < WIDE[0]) return false;
|
|
58
|
+
let lo = 0, hi = WIDE.length / 2 - 1;
|
|
59
|
+
while (lo <= hi) {
|
|
60
|
+
const mid = (lo + hi) >> 1;
|
|
61
|
+
if (cp < WIDE[2 * mid]) hi = mid - 1;
|
|
62
|
+
else if (cp > WIDE[2 * mid + 1]) lo = mid + 1;
|
|
63
|
+
else return true;
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Characters that take no column of their own (Markus Kuhn's wcwidth rule): nonspacing and enclosing marks, format
|
|
69
|
+
// characters but the soft hyphen, and the Hangul medial vowels and final consonants, which join the syllable before them.
|
|
70
|
+
const ZERO = /^[\p{Mn}\p{Me}\p{Cf}\u1160-\u11ff\ud7b0-\ud7ff]$/u;
|
|
71
|
+
const EMOJI = /^\p{Emoji}$/u;
|
|
72
|
+
const isRegional = (cp) => cp >= 0x1f1e6 && cp <= 0x1f1ff;
|
|
73
|
+
const zeroWidth = (ch, cp) => cp !== 0x00ad && ZERO.test(ch);
|
|
74
|
+
|
|
75
|
+
/** The columns one grapheme cluster takes: 0, 1 or 2. */
|
|
76
|
+
export function clusterWidth(c) {
|
|
77
|
+
const cp0 = c.codePointAt(0);
|
|
78
|
+
if (c.length === 1 && cp0 >= 0x20 && cp0 < 0x7f) return 1;
|
|
79
|
+
if (isRegional(cp0)) return 2;
|
|
80
|
+
let any = false, base = null, vs16 = false;
|
|
81
|
+
for (const ch of c) {
|
|
82
|
+
const cp = ch.codePointAt(0);
|
|
83
|
+
if (isWide(cp)) return 2;
|
|
84
|
+
if (cp === 0xfe0f) { vs16 = true; continue; }
|
|
85
|
+
if (!zeroWidth(ch, cp)) { any = true; base ??= ch; }
|
|
86
|
+
}
|
|
87
|
+
if (vs16 && base !== null && EMOJI.test(base)) return 2;
|
|
88
|
+
return any ? 1 : 0;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const SEG = typeof Intl?.Segmenter === 'function' ? new Intl.Segmenter(undefined, { granularity: 'grapheme' }) : null;
|
|
92
|
+
const PRINTABLE_ASCII = /^[\x20-\x7e]*$/;
|
|
93
|
+
const ASCII_AND_TABS = /^[\t\x20-\x7e]*$/;
|
|
94
|
+
|
|
95
|
+
/** The units of the slices a long text is segmented in (R4: on Node 20, each step of Intl.Segmenter's iteration costs
|
|
96
|
+
* in proportion to the length of the string it walks, so that walking one string is quadratic: 3.4 us a cluster at
|
|
97
|
+
* 10,000 units, 115 at 80,000; R.F1's line of 4,000,000 Hebrew characters was not laid out in ten minutes). */
|
|
98
|
+
export const SLICE = 256;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The grapheme clusters of s, in order: those Intl.Segmenter gives the whole text. A text longer than SLICE units is
|
|
102
|
+
* segmented a slice at a time, from a boundary of the whole text: every cluster of a slice but its last is the whole
|
|
103
|
+
* text's (a boundary depends only on the text before it, from a boundary on, and on the code point at it, and a slice
|
|
104
|
+
* never ends inside a surrogate pair), and the next slice begins where that last cluster begins; a slice that holds one
|
|
105
|
+
* cluster only is made twice as long until it holds two, or reaches the text's end. Without Intl.Segmenter, each code
|
|
106
|
+
* point with the zero-width ones after it.
|
|
107
|
+
*/
|
|
108
|
+
export function* clusters(s) {
|
|
109
|
+
if (SEG && s.length <= SLICE) { for (const { segment } of SEG.segment(s)) yield segment; return; }
|
|
110
|
+
if (SEG) {
|
|
111
|
+
for (let a = 0, n = SLICE; a < s.length;) {
|
|
112
|
+
let e = Math.min(s.length, a + n);
|
|
113
|
+
if (e < s.length && s.charCodeAt(e - 1) >= 0xd800 && s.charCodeAt(e - 1) <= 0xdbff) e++;
|
|
114
|
+
if (e === s.length) { for (const { segment } of SEG.segment(a ? s.slice(a) : s)) yield segment; return; }
|
|
115
|
+
let held = null, at = 0;
|
|
116
|
+
for (const { segment, index } of SEG.segment(s.slice(a, e))) { if (held !== null) yield held; held = segment; at = index; }
|
|
117
|
+
if (at > 0) { a += at; n = SLICE; } else n *= 2;
|
|
118
|
+
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
let cur = '';
|
|
122
|
+
for (const ch of s) {
|
|
123
|
+
if (cur !== '' && (zeroWidth(ch, ch.codePointAt(0)) || ch === '\ufe0f')) { cur += ch; continue; }
|
|
124
|
+
if (cur !== '') yield cur;
|
|
125
|
+
cur = ch;
|
|
126
|
+
}
|
|
127
|
+
if (cur !== '') yield cur;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The columns s takes on one line (s holds no new line; its tabs are expanded). */
|
|
131
|
+
export function width(s) {
|
|
132
|
+
if (PRINTABLE_ASCII.test(s)) return s.length;
|
|
133
|
+
let w = 0;
|
|
134
|
+
for (const c of clusters(s)) w += clusterWidth(c);
|
|
135
|
+
return w;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** The longest start of s made of whole clusters that fits in `cols` columns: { text, width, rest }. */
|
|
139
|
+
export function cut(s, cols) {
|
|
140
|
+
if (PRINTABLE_ASCII.test(s)) { const n = Math.max(0, Math.min(s.length, cols)); return { text: s.slice(0, n), width: n, rest: s.slice(n) }; }
|
|
141
|
+
let w = 0, at = 0;
|
|
142
|
+
for (const c of clusters(s)) {
|
|
143
|
+
const cw = clusterWidth(c);
|
|
144
|
+
if (w + cw > cols) break;
|
|
145
|
+
w += cw; at += c.length;
|
|
146
|
+
}
|
|
147
|
+
return { text: s.slice(0, at), width: w, rest: s.slice(at) };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** s within `cols` columns: as it is when it fits, else cut with … at its end. */
|
|
151
|
+
export function fit(s, cols) {
|
|
152
|
+
if (cols <= 0) return '';
|
|
153
|
+
if (width(s) <= cols) return s;
|
|
154
|
+
return `${cut(s, cols - 1).text}\u2026`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** s padded with spaces to `cols` columns (cut first when it is wider). */
|
|
158
|
+
export function pad(s, cols) {
|
|
159
|
+
const r = cut(s, cols);
|
|
160
|
+
return r.text + ' '.repeat(Math.max(0, cols - r.width));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Whitespace that hangs at the end of a line (V7, round 5): a cluster made only of the spaces of UAX #9's class WS that
|
|
164
|
+
* take a column (U+0020, U+1680, U+2000 to U+200A, U+205F, U+3000: DerivedBidiClass.txt 17.0.0; the form feed and
|
|
165
|
+
* U+2028, of the class too, are shown as escapes), but U+2007 FIGURE SPACE, a no-break space (isGlue: Z2, round 7).
|
|
166
|
+
* UAX #14 keeps a space on the line it follows (LB7, LB18) and does not break before the others but after a space
|
|
167
|
+
* (LB12a, LB21); L1 gives those that end a line the paragraph's level. */
|
|
168
|
+
const HANGS = /^[\u0020\u1680\u2000-\u2006\u2008-\u200a\u205f\u3000]+$/;
|
|
169
|
+
|
|
170
|
+
/** Whether the code unit g begins a cluster that UAX #14 gives no break before but after a space, a character of class
|
|
171
|
+
* BA or a hyphen (LB12a, class GL: LineBreak.txt; ICU 78.3, Unicode 17.0, measured): a no-break space (U+00A0, U+2007
|
|
172
|
+
* FIGURE SPACE, U+202F) or the non-breaking hyphen (U+2011). Z2, round 7: U+2007, UAX #9 whitespace, hung at a break
|
|
173
|
+
* as a space does, so that a line began with the GL character after it, or with U+2007 itself. */
|
|
174
|
+
const isGlue = (g) => g === 0xa0 || g === 0x2007 || g === 0x202f || g === 0x2011;
|
|
175
|
+
/** Whether UAX #14 breaks before a GL character after the code unit g (LB12a): a space (SP), a hyphen-minus (HY), a
|
|
176
|
+
* space of class BA (U+1680, U+2000 to U+2006, U+2008 to U+200A, U+205F, U+3000); a hyphen-minus but after a Hebrew
|
|
177
|
+
* letter (breaksBeforeGlueAt). */
|
|
178
|
+
const breaksBeforeGlue = (g) => g === 0x20 || g === 0x2d || g === 0x1680 || (g >= 0x2000 && g <= 0x200a && g !== 0x2007) || g === 0x205f || g === 0x3000;
|
|
179
|
+
|
|
180
|
+
/** Whether a code point is a Hebrew letter, UAX #14's class HL: the letters (General_Category Lo) of the Hebrew blocks,
|
|
181
|
+
* U+05D0 to U+05EA, U+05EF to U+05F2 and those of U+FB1D to U+FB4F (UnicodeData.txt 17.0.0, test/text.test.mjs; ICU
|
|
182
|
+
* 78.3 gives the same 75 code points the class). */
|
|
183
|
+
export const isHebrewLetter = (cp) => (cp >= 0x5d0 && cp <= 0x5ea) || (cp >= 0x5ef && cp <= 0x5f2) || (cp >= 0xfb1d && cp <= 0xfb4f && cp !== 0xfb1e && cp !== 0xfb29 && cp !== 0xfb37 && cp !== 0xfb3d && cp !== 0xfb3f && cp !== 0xfb42 && cp !== 0xfb45);
|
|
184
|
+
const MARK = /^\p{M}$/u;
|
|
185
|
+
/** The marks (General_Category M) that are not of UAX #14's class CM, which LB9 does not fold into the character before
|
|
186
|
+
* them: 21 of class GL (combining double marks), U+0F7F of class BA, 2 of VF and 7 of VI (viramas). LineBreak.txt 17.0.0;
|
|
187
|
+
* ICU 78.3 gives the same (round 8's probe cmsets.py). */
|
|
188
|
+
const MARK_NOT_CM = new Set([0x35c, 0x35d, 0x35e, 0x35f, 0x360, 0x361, 0x362, 0xf7f, 0x1aeb, 0x1b44, 0x1bf2, 0x1bf3, 0x1dcd, 0x1dfc, 0xa9c0, 0xfe20, 0xfe22, 0xfe24, 0xfe26, 0xfe27, 0xfe29, 0xfe2b, 0xfe2d, 0xfe2e, 0x11046, 0x1107f, 0x1134d, 0x113d0, 0x1193e, 0x11f42, 0x16fe4]);
|
|
189
|
+
/** The code points of class CM that are not marks, ZWJ aside (the same sources): the C0 and C1 controls, ALM, ZWNJ, LRM,
|
|
190
|
+
* RLM, the bidirectional embeddings, overrides and isolates, U+206A to U+206F, U+3035, U+FFF9 to U+FFFB, U+1BCA0 to
|
|
191
|
+
* U+1BCA3, U+1D173 to U+1D17A, the tags. visible() shows most of them as escapes; ALM, ZWNJ, LRM, RLM and 22 others
|
|
192
|
+
* reach the wrapper as they are. */
|
|
193
|
+
const CM_NOT_MARK = [[0x0, 0x8], [0xe, 0x1f], [0x7f, 0x84], [0x86, 0x9f], [0x61c, 0x61c], [0x200c, 0x200c], [0x200e, 0x200f], [0x202a, 0x202e], [0x2066, 0x206f], [0x3035, 0x3035], [0xfff9, 0xfffb], [0x1bca0, 0x1bca3], [0x1d173, 0x1d17a], [0xe0001, 0xe0001], [0xe0020, 0xe007f]];
|
|
194
|
+
/** Whether UAX #14's LB9 folds a code point into the character before it, which it then takes the class of (R8e, round
|
|
195
|
+
* 8): ZWJ, and class CM (class SA's marks too, which LB1 makes CM): the marks but MARK_NOT_CM, and CM_NOT_MARK. */
|
|
196
|
+
export const isLineBreakCombining = (cp) => cp === 0x200d || (MARK.test(String.fromCodePoint(cp)) ? !MARK_NOT_CM.has(cp) : CM_NOT_MARK.some(([a, b]) => cp >= a && cp <= b));
|
|
197
|
+
/** The code point that ends at offset j of s (a surrogate pair read whole), and the offset it begins at. */
|
|
198
|
+
function codePointBefore(s, j) {
|
|
199
|
+
const lo = s.charCodeAt(j - 1), hi = j >= 2 ? s.charCodeAt(j - 2) : 0;
|
|
200
|
+
return lo >= 0xdc00 && lo <= 0xdfff && hi >= 0xd800 && hi <= 0xdbff ? [((hi - 0xd800) << 10) + (lo - 0xdc00) + 0x10000, j - 2] : [lo, j - 1];
|
|
201
|
+
}
|
|
202
|
+
/** The character whose class LB9 gives to what ends at offset j of s, the run of what it folds before j passed over:
|
|
203
|
+
* [its code point, the offset it begins at], or [-1, 0] where only such a run is there. */
|
|
204
|
+
function baseBefore(s, j) {
|
|
205
|
+
while (j > 0) { const [cp, at] = codePointBefore(s, j); if (!isLineBreakCombining(cp)) return [cp, at]; j = at; }
|
|
206
|
+
return [-1, 0];
|
|
207
|
+
}
|
|
208
|
+
/** Whether UAX #14 breaks before a GL character after the code unit at offset i of s (LB12a, breaksBeforeGlue), but where
|
|
209
|
+
* the character there is a hyphen-minus after a Hebrew letter (LB21a: HL HY x, unless a Hebrew letter follows). What
|
|
210
|
+
* LB9 folds into a character (a mark, ZWJ, and the characters of class CM that are not marks: ALM, ZWNJ, LRM, RLM...)
|
|
211
|
+
* takes its class: between the letter and the hyphen, and after the hyphen or the space; but no break after ZWJ
|
|
212
|
+
* (LB8a), and none after such a run where it follows a space (SP) or nothing, as LB9 folds nothing into a space and LB10
|
|
213
|
+
* then takes the run as a letter (AL). Z2, round 7: the reader broke after a Hebrew letter's hyphen as after a Latin
|
|
214
|
+
* letter's (ICU 78.3 breaks before U+00A0 after 'abcd-', and gives no break in a Hebrew word, '-', U+00A0 and a Hebrew
|
|
215
|
+
* word); R8e, round 8: and did where ALM, ZWNJ, LRM or RLM stood between the letter and the hyphen (round 7's check). What
|
|
216
|
+
* is before i is read from s, so that a line laid out from its start is the same as from the text's. */
|
|
217
|
+
function breaksBeforeGlueAt(s, i) {
|
|
218
|
+
let [g, at] = codePointBefore(s, i + 1);
|
|
219
|
+
if (g === 0x200d) return false;
|
|
220
|
+
if (isLineBreakCombining(g)) {
|
|
221
|
+
[g, at] = baseBefore(s, at);
|
|
222
|
+
if (g < 0 || g === 0x20) return false;
|
|
223
|
+
}
|
|
224
|
+
if (!breaksBeforeGlue(g)) return false;
|
|
225
|
+
if (g !== 0x2d) return true;
|
|
226
|
+
return !isHebrewLetter(baseBefore(s, at)[0]);
|
|
227
|
+
}
|
|
228
|
+
/** Whether a grapheme cluster is made only of what LB9 folds into the character before it (LRM after a letter, say, or
|
|
229
|
+
* a mark after LRM, which a cluster boundary follows): it goes to a line with that character. */
|
|
230
|
+
function foldsWhole(c) {
|
|
231
|
+
for (const ch of c) if (!isLineBreakCombining(ch.codePointAt(0))) return false;
|
|
232
|
+
return c !== '';
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Where to end a line that a cluster of class GL (isGlue) does not fit at the end of (Y3 (e), round 6): the index, in
|
|
236
|
+
* the line's clusters (`starts`: where each begins in s; `widths`: its columns), of the first cluster of the next line,
|
|
237
|
+
* which takes the rest of them and the GL cluster (of `cw` columns), at most `n` columns: after the clusters of class GL
|
|
238
|
+
* the line ends in and the character before them, where that is a space, BA or a hyphen (LB12a; a hyphen-minus not
|
|
239
|
+
* after a Hebrew letter, LB21a: breaksBeforeGlueAt), else before that character too, so that the next line does not
|
|
240
|
+
* begin with a GL character UAX #14 gives no break before; -1 where the line ends before the GL cluster all the same:
|
|
241
|
+
* UAX #14 breaks there, or the line would be left empty, or the next would not fit in the columns (a column is the
|
|
242
|
+
* limit, as a cluster wider than the columns is). */
|
|
243
|
+
function glueBreak(s, starts, widths, at, cw, n) {
|
|
244
|
+
let k = starts.length;
|
|
245
|
+
while (k > 0 && isGlue(s.charCodeAt(starts[k - 1]))) k--;
|
|
246
|
+
const run = k < starts.length ? starts[k] : at;
|
|
247
|
+
let cut = run > 0 && breaksBeforeGlueAt(s, run - 1) ? k : k - 1;
|
|
248
|
+
// LB9 (R8e, round 8): a cluster made only of what it folds goes with the cluster it follows, but a space (SP)
|
|
249
|
+
if (cut < k) while (cut > 0 && foldsWhole(s.slice(starts[cut], cut + 1 < starts.length ? starts[cut + 1] : at)) && s.charCodeAt(starts[cut - 1]) !== 0x20) cut--;
|
|
250
|
+
if (cut < 1 || cut === starts.length) return -1;
|
|
251
|
+
let w = cw;
|
|
252
|
+
for (let i = cut; i < widths.length; i++) w += widths[i];
|
|
253
|
+
return w <= n ? cut : -1;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Where the lines of one line of text s (no new line in it) begin and end, [start, end), when it is cut into lines of
|
|
257
|
+
* at most `cols` columns between clusters, a cluster wider than the columns on a line of its own; from offset `from`,
|
|
258
|
+
* where a line begins. Whitespace (HANGS) that does not fit at the end of a line stays at the end of that line, with
|
|
259
|
+
* the whitespace after it, as UAX #14 has a space: it hangs there, past the line's columns, and is in no line (nothing
|
|
260
|
+
* past the screen is drawn); the next line begins after it, so that no line but the first begins with whitespace (V7,
|
|
261
|
+
* round 5: the space that did not fit began the next line). Whitespace that fits ends its line. A no-break space or
|
|
262
|
+
* a non-breaking hyphen (class GL) that does not fit begins the next line with the character before it (glueBreak:
|
|
263
|
+
* UAX #14 gives no break before it but after a space, BA or a hyphen, a hyphen-minus not after a Hebrew letter; Y3 (e),
|
|
264
|
+
* round 6: it began the line by itself; Z2, round 7: after a Hebrew letter's hyphen, and U+2007, it still did).
|
|
265
|
+
* One pair at a time, so that a long text can be laid out without its lines being made; from a line's start, the lines
|
|
266
|
+
* are those from the text's start (what a line holds depends only on the text from its start). An empty s is one empty
|
|
267
|
+
* line. */
|
|
268
|
+
export function* lineSpans(s, cols, from = 0) {
|
|
269
|
+
const n = Math.max(1, cols);
|
|
270
|
+
if (from >= s.length) { if (from === 0) yield [0, 0]; return; }
|
|
271
|
+
if (PRINTABLE_ASCII.test(s)) {
|
|
272
|
+
for (let i = from; i < s.length;) {
|
|
273
|
+
const e = Math.min(s.length, i + n);
|
|
274
|
+
yield [i, e];
|
|
275
|
+
for (i = e; i < s.length && s.charCodeAt(i) === 0x20;) i++;
|
|
276
|
+
}
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
// the line being made: where it begins, its columns, where each of its clusters begins and the columns each takes
|
|
280
|
+
let start = from, at = from, w = 0, hanging = false, starts = [], widths = [];
|
|
281
|
+
for (const c of clusters(from ? s.slice(from) : s)) {
|
|
282
|
+
if (hanging) {
|
|
283
|
+
if (HANGS.test(c)) { at += c.length; continue; }
|
|
284
|
+
hanging = false; start = at; w = 0; starts = []; widths = [];
|
|
285
|
+
}
|
|
286
|
+
const cw = clusterWidth(c);
|
|
287
|
+
if (w + cw > n && at > start) {
|
|
288
|
+
const k = isGlue(c.charCodeAt(0)) ? glueBreak(s, starts, widths, at, cw, n) : -1;
|
|
289
|
+
if (k > 0) {
|
|
290
|
+
// the line ends before its k-th cluster, which begins the next with the rest and c
|
|
291
|
+
yield [start, starts[k]];
|
|
292
|
+
start = starts[k]; starts = starts.slice(k); widths = widths.slice(k); w = 0;
|
|
293
|
+
for (const x of widths) w += x;
|
|
294
|
+
} else {
|
|
295
|
+
yield [start, at];
|
|
296
|
+
if (HANGS.test(c)) { hanging = true; at += c.length; continue; }
|
|
297
|
+
start = at; w = 0; starts = []; widths = [];
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
starts.push(at); widths.push(cw);
|
|
301
|
+
at += c.length; w += cw;
|
|
302
|
+
}
|
|
303
|
+
if (!hanging) yield [start, s.length];
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** One line (no new line in it) cut into lines of at most `cols` columns, between clusters; a cluster wider than the
|
|
307
|
+
* columns goes on a line of its own; whitespace that does not fit at a line's end hangs there, in no line (lineSpans).
|
|
308
|
+
* The lines are slices of s, in order, and only that whitespace is between them. Each line is a slice of s (a line
|
|
309
|
+
* built by adding cluster after cluster cost a string object per cluster: 28 MB for a million characters of Hebrew,
|
|
310
|
+
* R.F1). */
|
|
311
|
+
export function wrap(s, cols) {
|
|
312
|
+
const out = [];
|
|
313
|
+
for (const [a, b] of lineSpans(s, cols)) out.push(s.slice(a, b));
|
|
314
|
+
return out;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** Where the lines of wrapWords(s, cols) begin and end in s, [start, end) each: a paragraph in lines of at most `cols`
|
|
318
|
+
* columns, broken at spaces where a line can be, else by columns (lineSpans). A space a line is broken at is in no line
|
|
319
|
+
* (it hangs at the end of the line before, as UAX #14 has the whitespace at a break, and is not drawn, though the line
|
|
320
|
+
* has room for it: Y3 (c), round 6), and so is the whitespace (HANGS) after it, at the start of the word that begins
|
|
321
|
+
* the next line, and whitespace that hangs inside a word cut by columns, which ends its line: no line but the first
|
|
322
|
+
* begins with whitespace (V7, round 5). Each line is s.slice(start, end), so that its place in its paragraph is known
|
|
323
|
+
* (W1: a line is laid out from its paragraph's levels). */
|
|
324
|
+
export function wordSpans(s, cols) {
|
|
325
|
+
const out = [];
|
|
326
|
+
// the line being made, where it begins in s, where the next word begins, and whether the line can take it (not when
|
|
327
|
+
// whitespace that hangs ends it)
|
|
328
|
+
let line = '', from = 0, pos = 0, open = true;
|
|
329
|
+
for (const whole of s.split(' ')) {
|
|
330
|
+
let at = pos, word = whole;
|
|
331
|
+
pos += whole.length + 1;
|
|
332
|
+
if (line === '' && out.length) { const k = blankPrefix(word); at += k; word = word.slice(k); }
|
|
333
|
+
const next = line === '' ? word : `${line} ${word}`;
|
|
334
|
+
if (open && width(next) <= cols) { if (line === '') from = at; line = next; continue; }
|
|
335
|
+
if (line !== '') out.push([from, from + line.length]);
|
|
336
|
+
if (out.length) { const k = blankPrefix(word); at += k; word = word.slice(k); }
|
|
337
|
+
const pieces = [...lineSpans(word, cols)], [x, y] = pieces.pop();
|
|
338
|
+
for (const [c, d] of pieces) out.push([at + c, at + d]);
|
|
339
|
+
line = word.slice(x, y); from = at + x; open = y === word.length;
|
|
340
|
+
}
|
|
341
|
+
// a paragraph that ends in whitespace at a break ends with its last word: what is left after the break hangs, and
|
|
342
|
+
// makes no empty line (Y3 (d), round 6: it made one, drawn as a blank row); an empty s, or one of spaces only, is one
|
|
343
|
+
// empty line
|
|
344
|
+
if (line !== '' || !out.length) out.push([from, from + line.length]);
|
|
345
|
+
return out;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** How many code units at the start of s are whitespace that hangs (HANGS), in whole clusters. */
|
|
349
|
+
function blankPrefix(s) {
|
|
350
|
+
if (s === '' || !HANGS.test(s[0])) return 0;
|
|
351
|
+
let k = 0;
|
|
352
|
+
for (const c of clusters(s)) { if (!HANGS.test(c)) break; k += c.length; }
|
|
353
|
+
return k;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** The reader's own words (not a transcript's text) in lines of at most `cols` columns, broken at spaces where a
|
|
357
|
+
* line can be, else by columns (wordSpans: each line a slice of s). */
|
|
358
|
+
export function wrapWords(s, cols) {
|
|
359
|
+
return wordSpans(s, cols).map(([a, b]) => s.slice(a, b));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** s with each tab as spaces to the next multiple of `tab` columns, counted by width from the start of s. */
|
|
363
|
+
export function expandTabs(s, tab = 8) {
|
|
364
|
+
if (!s.includes('\t')) return s;
|
|
365
|
+
// the runs between tabs as slices of s, joined once (a tab is a cluster of its own); printable ASCII and tabs alone
|
|
366
|
+
// are counted by their characters, without the segmenter
|
|
367
|
+
if (ASCII_AND_TABS.test(s)) {
|
|
368
|
+
const parts = [];
|
|
369
|
+
let from = 0, col = 0;
|
|
370
|
+
for (let i = s.indexOf('\t'); i >= 0; i = s.indexOf('\t', from)) { col += i - from; const n = tab - (col % tab); parts.push(s.slice(from, i), ' '.repeat(n)); col += n; from = i + 1; }
|
|
371
|
+
parts.push(s.slice(from));
|
|
372
|
+
return parts.join('');
|
|
373
|
+
}
|
|
374
|
+
const parts = [];
|
|
375
|
+
let from = 0, at = 0, col = 0;
|
|
376
|
+
for (const c of clusters(s)) {
|
|
377
|
+
if (c === '\t') { const n = tab - (col % tab); parts.push(s.slice(from, at), ' '.repeat(n)); col += n; at += 1; from = at; continue; }
|
|
378
|
+
at += c.length; col += clusterWidth(c);
|
|
379
|
+
}
|
|
380
|
+
parts.push(s.slice(from));
|
|
381
|
+
return parts.join('');
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** The right-to-left modes there are: "reader" (the reader lays each paragraph out) and "terminal" (as stored). */
|
|
385
|
+
export const RTL_MODES = Object.freeze(['reader', 'terminal']);
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* The right-to-left mode: LTMS_READER_BIDI when it is set ("terminal" or "reader"); else "reader", in every terminal
|
|
389
|
+
* (Y1, round 6: one layout for every terminal, decided S378 on a measurement of Alacritty 0.17.0, Konsole 26.08.3, tmux
|
|
390
|
+
* 3.6 and VS Code 1.146.0's terminal; round 3 gave Konsole, detected by KONSOLE_VERSION, "terminal", which a reader run
|
|
391
|
+
* over ssh from Konsole did not see). Returns { mode, by } or { refused: why }.
|
|
392
|
+
*/
|
|
393
|
+
export function rtlModeOf(env) {
|
|
394
|
+
const v = env.LTMS_READER_BIDI;
|
|
395
|
+
if (typeof v === 'string' && v !== '') {
|
|
396
|
+
if (!RTL_MODES.includes(v)) return { refused: `LTMS_READER_BIDI is ${JSON.stringify(v)}: it takes "reader" (the reader lays right-to-left text out, the default) or "terminal" (the text as stored, for a terminal that lays it out itself)` };
|
|
397
|
+
return { mode: v, by: 'LTMS_READER_BIDI' };
|
|
398
|
+
}
|
|
399
|
+
return { mode: 'reader', by: 'default' };
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/** A text with no character that could be right to left (every R, AL and AN code point is at U+0590 or above). */
|
|
403
|
+
const NO_RTL = /^[\u0000-֏]*$/;
|
|
404
|
+
const RLM = '';
|
|
405
|
+
|
|
406
|
+
// A path or a URL (R1, UAX #9 HL4): a URL (a scheme and ://); a path from the root, the home folder or here (/, ~/, ./,
|
|
407
|
+
// ../) or a drive (C:\ or C:/); or a relative path whose first part is ASCII (src/, docs/: a first part that is not, as
|
|
408
|
+
// in the Hebrew ו/או, "and/or", is not taken for one). It begins at the text's start or after a space, an opening
|
|
409
|
+
// bracket or quote, = or a hyphen, and ends at the next space; the punctuation that ends a sentence, and a closing
|
|
410
|
+
// bracket or quote it does not open, are not part of it.
|
|
411
|
+
const UNIT = /(?<=^|[\s([{<"'=\-־])(?:[A-Za-z][A-Za-z0-9+.-]*:\/\/[^\s]+|(?:~|\.{1,2})?\/[^\s]+|[A-Za-z]:[\\/][^\s]*|[\w.@+-]+\/[^\s]*)/gu;
|
|
412
|
+
const CLOSERS = { ')': '(', ']': '[', '}': '{', '>': '<' };
|
|
413
|
+
|
|
414
|
+
/** The paths and URLs in a text, as [start, end) pairs. */
|
|
415
|
+
export function pathUnits(text) {
|
|
416
|
+
if (!text.includes('/') && !text.includes('\\')) return [];
|
|
417
|
+
const out = [];
|
|
418
|
+
for (const m of text.matchAll(UNIT)) {
|
|
419
|
+
let a = m.index, b = a + m[0].length;
|
|
420
|
+
for (;;) {
|
|
421
|
+
const c = text[b - 1];
|
|
422
|
+
if ('.,;:!?…"\''.includes(c)) { b--; continue; }
|
|
423
|
+
const o = CLOSERS[c];
|
|
424
|
+
if (o) { const u = text.slice(a, b); if (u.split(c).length > u.split(o).length) { b--; continue; } }
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
if (b - a > 1) out.push([a, b]);
|
|
428
|
+
}
|
|
429
|
+
return out;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** A cluster as it is drawn at `level`: at an odd one, its first character's mirrored glyph (L4). */
|
|
433
|
+
const drawn = (c, level) => {
|
|
434
|
+
if (!(level & 1)) return c;
|
|
435
|
+
const cp = c.codePointAt(0), m = mirrorOf(cp);
|
|
436
|
+
return m === cp ? c : String.fromCodePoint(m) + c.slice(cp > 0xffff ? 2 : 1);
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
/** Whether a grapheme cluster is nonspacing marks alone (each of class NSM). */
|
|
440
|
+
const marksAlone = (c) => { for (const ch of c) if (classOf(ch.codePointAt(0)) !== NSM) return false; return true; };
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* The items of a stretch of text s, in order, each laid out whole (Z5, round 7), added to the end of the caller's
|
|
444
|
+
* `items`, and each one's level to `at`: the level of its first character X9 kept (`levelOf(c)`, asked of each cluster
|
|
445
|
+
* c of s in order, -1 where X9 removed them all). Each item is a grapheme cluster, but where a cluster of nonspacing
|
|
446
|
+
* marks alone follows clusters X9 removed whole (a soft hyphen, ZWSP, WJ: the segmenter breaks after each, a control,
|
|
447
|
+
* so that a mark after one is a cluster of its own) that follow a cluster of the stretch: the mark goes with that
|
|
448
|
+
* cluster, and so do the removed characters between, in their order. UAX #9 gives the mark the class of the character
|
|
449
|
+
* before it, the removed ones passed over (W1), and the reader keeps a letter's marks after it, as a terminal draws
|
|
450
|
+
* them; as an item of its own, at an odd level, the mark was reversed with its run, before its letter, on the cell
|
|
451
|
+
* before it (round 6's check: final nun, WJ, WJ, patah was written patah, WJ, WJ, nun). Only after a cluster with a
|
|
452
|
+
* character X9 keeps: a mark after removed characters that begin the stretch, or follow only removed ones, has no
|
|
453
|
+
* letter there, and is an item of its own, at its own level. A cluster X9 removed whole is at the level of the item
|
|
454
|
+
* before it, or at `fallback` when it is the first.
|
|
455
|
+
*/
|
|
456
|
+
function addItems(s, levelOf, items, at, fallback) {
|
|
457
|
+
const held = [];
|
|
458
|
+
// whether the last item is a cluster of this stretch with a character X9 keeps (removed ones after it are held)
|
|
459
|
+
let kept = false;
|
|
460
|
+
const flush = () => { for (const x of held) { at[items.length] = at[items.length - 1]; items.push(x); } held.length = 0; kept = false; };
|
|
461
|
+
for (const c of clusters(s)) {
|
|
462
|
+
const v = levelOf(c);
|
|
463
|
+
if (v < 0 && kept) { held.push(c); continue; }
|
|
464
|
+
if (held.length && marksAlone(c)) { items[items.length - 1] += held.join('') + c; held.length = 0; continue; }
|
|
465
|
+
flush();
|
|
466
|
+
at[items.length] = v >= 0 ? v : items.length ? at[items.length - 1] : fallback;
|
|
467
|
+
items.push(c);
|
|
468
|
+
kept = v >= 0;
|
|
469
|
+
}
|
|
470
|
+
flush();
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/** The clusters of s in visual order by `levels`, the levels of the code units of a text s begins at offset `base` of:
|
|
474
|
+
* a cluster at the level of its first character X9 kept (one X9 removed whole at the level of the cluster before it,
|
|
475
|
+
* the first at `level`; a mark after such ones, with the cluster before them: addItems); L2; L4. */
|
|
476
|
+
function ordered(s, levels, base, level) {
|
|
477
|
+
const cs = [], at = new Int8Array(s.length);
|
|
478
|
+
let k = base;
|
|
479
|
+
addItems(s, (c) => { let v = -1; for (let j = k, e = k + c.length; j < e && v < 0; j++) v = levels[j]; k += c.length; return v; }, cs, at, level);
|
|
480
|
+
let out = '';
|
|
481
|
+
for (const i of visualOrder(at, 0, cs.length)) out += drawn(cs[i], at[i]);
|
|
482
|
+
return out;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** The levels of a path's or a URL's own content, s, laid out on its own, left to right, its separators strong (HL2),
|
|
486
|
+
* per code unit; null when nothing in it is right to left (it reads as stored). */
|
|
487
|
+
function unitLevels(s) {
|
|
488
|
+
if (NO_RTL.test(s)) return null;
|
|
489
|
+
const classes = classesOf(s);
|
|
490
|
+
for (let i = 0; i < s.length; i++) { const c = s.charCodeAt(i); if (c === 0x2f || c === 0x5c) classes[i] = LTR_CLASS; }
|
|
491
|
+
return resolveLevels(s, classes, 0);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* A paragraph's levels, resolved once (W1, round 4; UAX #9 section 3.4: X9, W1 to W7, N0 to N2, I1 and I2 over the whole
|
|
496
|
+
* paragraph; then each line it is wrapped into is reordered from them, by lineOf, never resolved by itself). `level` is
|
|
497
|
+
* the paragraph's (0 or 1; null: by P2 and P3). With `units`, each path or URL in it stands as one isolate (ISO, HL4),
|
|
498
|
+
* and its content is resolved once too, on its own. Returns { text, level, levels, units }: `levels` per code unit
|
|
499
|
+
* (bidi.mjs resolveLevels: -1 where X9 removed a character, at the second unit of a surrogate pair, and inside a unit
|
|
500
|
+
* but at its first unit, which holds the unit's level), or null when nothing outside the units reorders; `units`
|
|
501
|
+
* [{ a, b, inner }], inner the content's levels (unitLevels), or null.
|
|
502
|
+
*/
|
|
503
|
+
function layOut(text, level, units) {
|
|
504
|
+
if (level !== 1 && NO_RTL.test(text)) return { text, level: 0, levels: null, units: [] };
|
|
505
|
+
const spans = units ? pathUnits(text) : [], classes = classesOf(text);
|
|
506
|
+
for (const [a, b] of spans) { classes[a] = ISO; classes.fill(BN, a + 1, b); }
|
|
507
|
+
let rtl = false;
|
|
508
|
+
for (let i = 0; i < classes.length && !rtl; i++) { const k = classes[i]; rtl = k === R || k === AL || k === AN; }
|
|
509
|
+
const p = level ?? paragraphLevel(classes);
|
|
510
|
+
return { text, level: p, levels: rtl || p === 1 ? resolveLevels(text, classes, p) : null, units: spans.map(([a, b]) => ({ a, b, inner: unitLevels(text.slice(a, b)) })) };
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Line [a, b) of a paragraph laid out by layOut (a and b begin clusters), in visual order: L1 over the line (its own
|
|
515
|
+
* characters' classes, the paragraph's levels), each cluster at the level of its first character X9 kept (a mark after
|
|
516
|
+
* characters X9 removed, with the cluster before them: addItems), L2, L4. A
|
|
517
|
+
* path's or a URL's part in the line stands in it as one isolate, at the level the unit has in the paragraph, its own
|
|
518
|
+
* characters in the order of its own levels (a unit the line breaks inside of is drawn in pieces, as an isolate across
|
|
519
|
+
* a line break is: GNU FriBidi's library agrees, test/bidi.test.mjs).
|
|
520
|
+
*/
|
|
521
|
+
function lineOf(P, a, b) {
|
|
522
|
+
const { text, level: p, levels, units } = P;
|
|
523
|
+
// the first unit that ends after a, by a binary search (the units are in order and apart, so their ends are in order
|
|
524
|
+
// too): a line late in a paragraph of many paths is laid out without passing over the units before it (V1, round 5:
|
|
525
|
+
// passed over one by one, a page at the end of 200,000 paths cost seven to fifteen times a page at the start)
|
|
526
|
+
let u = 0;
|
|
527
|
+
for (let hi = units.length; u < hi;) { const mid = (u + hi) >> 1; if (units[mid].b <= a) u = mid + 1; else hi = mid; }
|
|
528
|
+
const piece = (U, s, e) => (U.inner ? ordered(text.slice(s, e), U.inner, s - U.a, 0) : text.slice(s, e));
|
|
529
|
+
if (!levels) {
|
|
530
|
+
// nothing outside the units is reordered: the line as stored, each unit's part in its own order
|
|
531
|
+
let out = '', at = a;
|
|
532
|
+
for (; u < units.length && units[u].a < b; u++) { const U = units[u], s = Math.max(a, U.a), e = Math.min(b, U.b); out += text.slice(at, s) + piece(U, s, e); at = e; }
|
|
533
|
+
return at === a ? text.slice(a, b) : out + text.slice(at, b);
|
|
534
|
+
}
|
|
535
|
+
// L1: the line's own characters' classes (a unit's first unit ISO, the rest of it passed over), the paragraph's levels
|
|
536
|
+
const lv = levels.slice(a, b), cl = classesOf(text.slice(a, b));
|
|
537
|
+
for (let k = u; k < units.length && units[k].a < b; k++) { const s = Math.max(a, units[k].a), e = Math.min(b, units[k].b); cl.fill(BN, s - a, e - a); if (s === units[k].a) cl[s - a] = ISO; }
|
|
538
|
+
lineLevels(cl, lv, p);
|
|
539
|
+
// the stretches between units by their clusters, each unit's part as one item at the unit's level; L2
|
|
540
|
+
const items = [], at = new Int8Array(b - a);
|
|
541
|
+
for (let i = a; i < b;) {
|
|
542
|
+
if (u < units.length && i >= units[u].a) {
|
|
543
|
+
const U = units[u], e = Math.min(b, U.b);
|
|
544
|
+
at[items.length] = levels[U.a]; items.push({ unit: piece(U, i, e) });
|
|
545
|
+
i = e; u++; continue;
|
|
546
|
+
}
|
|
547
|
+
const end = u < units.length && units[u].a < b ? units[u].a : b;
|
|
548
|
+
addItems(text.slice(i, end), (c) => { let v = -1; for (let k = i - a, e = k + c.length; k < e && v < 0; k++) v = lv[k]; i += c.length; return v; }, items, at, p);
|
|
549
|
+
}
|
|
550
|
+
let out = '';
|
|
551
|
+
for (const i of visualOrder(at, 0, items.length)) out += typeof items[i] === 'string' ? drawn(items[i], at[i]) : items[i].unit;
|
|
552
|
+
return out;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// The paragraphs laid out last: the one before (the lines of a paragraph are laid out one after another), and the last
|
|
556
|
+
// KEEP of at least LONG units (a long paragraph is read a page at a time, page after page: R.F1's record is one line of
|
|
557
|
+
// 4,000,000 characters). What is kept is bounded: KEEP + 1 paragraphs, their levels a byte a code unit.
|
|
558
|
+
const LONG = 4096, KEEP = 2, KEPT = [];
|
|
559
|
+
let LAST = null;
|
|
560
|
+
const same = (e, text, level, units) => e.level === level && e.units === units && e.P.text === text;
|
|
561
|
+
|
|
562
|
+
/** layOut(text, level, units), from those kept when it is one of them. */
|
|
563
|
+
function paragraphOf(text, level, units) {
|
|
564
|
+
let e = LAST && same(LAST, text, level, units) ? LAST : KEPT.find((k) => same(k, text, level, units));
|
|
565
|
+
// found: its text the same characters, perhaps another string, which is kept from now on, so that the next look at
|
|
566
|
+
// it compares two strings that are one (a string compared with another of its characters is read whole)
|
|
567
|
+
if (e) e.P.text = text;
|
|
568
|
+
else e = { level, units, P: layOut(text, level, units) };
|
|
569
|
+
if (text.length >= LONG && KEPT[0] !== e) {
|
|
570
|
+
const i = KEPT.indexOf(e);
|
|
571
|
+
if (i >= 0) KEPT.splice(i, 1);
|
|
572
|
+
KEPT.unshift(e);
|
|
573
|
+
if (KEPT.length > KEEP) KEPT.pop();
|
|
574
|
+
}
|
|
575
|
+
LAST = e;
|
|
576
|
+
return e.P;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/** Line [a, b) of the paragraph `text` (its level `level`, 0 or 1; null: by P2 and P3), in visual order, laid out from
|
|
580
|
+
* the paragraph's levels (W1): what the screen writes for a line of a wrapped paragraph, in "reader" mode. a and b
|
|
581
|
+
* begin clusters. With `units`, each path or URL is one isolate (HL4). */
|
|
582
|
+
export function paragraphLine(text, level, a, b, { units = true } = {}) {
|
|
583
|
+
a = Math.max(0, a); b = Math.min(b, text.length);
|
|
584
|
+
if (a >= b) return '';
|
|
585
|
+
return lineOf(paragraphOf(text, level, units), a, b);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/** The paragraph's level of a text (P2, P3: 1 when its first strong character, a path or a URL passed over, is right to
|
|
589
|
+
* left; else 0). */
|
|
590
|
+
export function directionOf(text) {
|
|
591
|
+
if (NO_RTL.test(text)) return 0;
|
|
592
|
+
for (let i = 0; i < text.length;) {
|
|
593
|
+
const cp = text.codePointAt(i), c = classOf(cp);
|
|
594
|
+
if (c === LTR_CLASS || c === R || c === AL) {
|
|
595
|
+
const end = unitEndAt(text, i);
|
|
596
|
+
if (end < 0) return c === LTR_CLASS ? 0 : 1;
|
|
597
|
+
i = end;
|
|
598
|
+
continue;
|
|
599
|
+
}
|
|
600
|
+
i += cp > 0xffff ? 2 : 1;
|
|
601
|
+
}
|
|
602
|
+
return 0;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/** Where the path or URL that holds offset i ends, or -1 when none does: looked for in the run of characters that are
|
|
606
|
+
* not spaces around i (a unit begins at such a run's start or inside it, and ends at its end or before). */
|
|
607
|
+
function unitEndAt(text, i) {
|
|
608
|
+
let a = i, b = i;
|
|
609
|
+
while (a > 0 && !/\s/.test(text[a - 1])) a--;
|
|
610
|
+
while (b < text.length && !/\s/.test(text[b])) b++;
|
|
611
|
+
const run = text.slice(a, b);
|
|
612
|
+
if (!run.includes('/') && !run.includes('\\')) return -1;
|
|
613
|
+
for (const [x, y] of pathUnits(run)) if (a + x <= i && i < a + y) return a + y;
|
|
614
|
+
return -1;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* One line that is a paragraph of its own (logical order, cut to its columns, visible, its tabs as spaces) in visual
|
|
619
|
+
* order, as a terminal that draws text in the order it is written must get it: UAX #9 over its code points, at the
|
|
620
|
+
* paragraph's level (`level`, or the line's own by P2 and P3 when null), then L1 and L2 over its grapheme clusters, each
|
|
621
|
+
* kept whole and in its own order (a mark after its letter, as a terminal draws it: L3), each at an odd level mirrored
|
|
622
|
+
* (L4). With `units`, each path or URL is laid out on its own, left to right, and stands in the line as one isolate
|
|
623
|
+
* (HL4). A line of a paragraph wrapped into several is laid out by paragraphLine instead, from the paragraph's levels.
|
|
624
|
+
*/
|
|
625
|
+
export function visualLine(text, level = null, { units = true } = {}) {
|
|
626
|
+
if (text === '' || (level !== 1 && NO_RTL.test(text))) return text;
|
|
627
|
+
return lineOf(layOut(text, level, units), 0, text.length);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/** Whether a grapheme cluster boundary is at offset i of s (0 < i < s.length), read from the code point before i and the
|
|
631
|
+
* one at i only: exact where either is whitespace, as at a segment's edges below (the rules that read further back, the
|
|
632
|
+
* Indic conjuncts', the emoji sequences' and the flags', join a character to a linker, a ZWJ or a regional indicator
|
|
633
|
+
* before it, never to whitespace, and never join whitespace to what is before it). */
|
|
634
|
+
function boundaryAt(s, i) {
|
|
635
|
+
const lo = s.charCodeAt(i - 1), pair = lo >= 0xdc00 && lo <= 0xdfff && i >= 2 && s.charCodeAt(i - 2) >= 0xd800 && s.charCodeAt(i - 2) <= 0xdbff;
|
|
636
|
+
const from = pair ? i - 2 : i - 1;
|
|
637
|
+
const hi = s.charCodeAt(i), to = Math.min(s.length, i + (hi >= 0xd800 && hi <= 0xdbff ? 2 : 1));
|
|
638
|
+
return clusters(s.slice(from, to)).next().value.length === i - from;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* The whitespace at either end of a segment's text, ECMAScript's as trimStart and trimEnd take it, in whole grapheme
|
|
643
|
+
* clusters made of whitespace only: [a, b), the core between them (V3, round 5: by trimStart, a space carrying a mark at
|
|
644
|
+
* a segment's start was cut from its mark, which was laid out with the rest, on the letter beside it). A character that
|
|
645
|
+
* joins the cluster of a space before it is at U+0300 or above (a mark, ZWJ, a spacing mark); one that joins a space
|
|
646
|
+
* after it, at U+0600 or above (a Prepend character): below those the boundary is not looked up (test/text.test.mjs
|
|
647
|
+
* checks both, by Intl.Segmenter).
|
|
648
|
+
*/
|
|
649
|
+
function blankEdges(text) {
|
|
650
|
+
let a = text.length - text.trimStart().length, b = text.trimEnd().length;
|
|
651
|
+
if (a >= b) return [a, a];
|
|
652
|
+
while (a > 0 && (!SEG || text.charCodeAt(a) >= 0x300) && !boundaryAt(text, a)) a--;
|
|
653
|
+
while (b < text.length && (!SEG || text.charCodeAt(b - 1) >= 0x600) && !boundaryAt(text, b)) b++;
|
|
654
|
+
return [a, b];
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* One screen row's segments ([{ text, style, dir, par, at }], already cut to the row's width) as they are written, in
|
|
659
|
+
* `mode`. A segment is a paragraph of its own, or a line of one: `par` names the paragraph (its text, of which the
|
|
660
|
+
* segment's text is the slice at `at`: a line of a record, of a message, of the reader's own words wrapped into lines,
|
|
661
|
+
* W1). Its direction is `dir` when given (0 or 1: a line's, its paragraph's), else its own (P2, P3).
|
|
662
|
+
* "reader": a line of a paragraph laid out whole from the paragraph's levels (paragraphLine), its whitespace at either
|
|
663
|
+
* end with the rest, as UAX #9's L1 and L2 lay out a line (V7, round 5: they stayed where they were, and the line's
|
|
664
|
+
* text differed from UAX #9's); a paragraph of its own (a cell, a line cut to its columns: R1 (c)) laid out by
|
|
665
|
+
* visualLine but its spaces at either end, in whole clusters (blankEdges), which stay where they are: a cell's are the
|
|
666
|
+
* cell's padding. Either way the columns stay, and the text is aligned left. "terminal": as stored, with an RLM after a
|
|
667
|
+
* right-to-left paragraph's text (before the spaces at its end), so that a terminal that lays the line out with a
|
|
668
|
+
* left-to-right base (Konsole) puts the paragraph's end punctuation at its left (measured on Konsole 26.08.3; a mark at
|
|
669
|
+
* the row's first column it drops); never after a left-to-right one's.
|
|
670
|
+
*/
|
|
671
|
+
export function displayOrder(row, mode = 'reader') {
|
|
672
|
+
if (mode !== 'reader' && mode !== 'terminal') throw new RangeError(`no right-to-left mode ${JSON.stringify(mode)}: the modes are "reader" and "terminal"`);
|
|
673
|
+
let out = null;
|
|
674
|
+
for (let i = 0; i < row.length; i++) {
|
|
675
|
+
const s = row[i], text = s.text;
|
|
676
|
+
// a line of a paragraph: one whose paragraph holds what the segment's text is, at its place
|
|
677
|
+
const line = typeof s.par === 'string' && Number.isInteger(s.at) && s.par.startsWith(text, s.at);
|
|
678
|
+
if (!line && s.dir !== 1 && NO_RTL.test(text)) continue;
|
|
679
|
+
if (line && mode === 'reader') {
|
|
680
|
+
if (text === '') continue;
|
|
681
|
+
const shown = paragraphLine(s.par, s.dir ?? directionOf(s.par), s.at, s.at + text.length);
|
|
682
|
+
if (shown === text) continue;
|
|
683
|
+
out ??= row.slice();
|
|
684
|
+
out[i] = { ...s, text: shown };
|
|
685
|
+
continue;
|
|
686
|
+
}
|
|
687
|
+
const [a, b] = blankEdges(text);
|
|
688
|
+
if (a >= b) continue;
|
|
689
|
+
const core = text.slice(a, b), dir = s.dir ?? (line ? directionOf(s.par) : directionOf(core));
|
|
690
|
+
const shown = mode === 'terminal' ? (dir === 1 ? `${core}${RLM}` : core) : visualLine(core, dir);
|
|
691
|
+
if (shown === core) continue;
|
|
692
|
+
out ??= row.slice();
|
|
693
|
+
out[i] = { ...s, text: text.slice(0, a) + shown + text.slice(b) };
|
|
694
|
+
}
|
|
695
|
+
return out ?? row;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/** Parts of one paragraph each (a cell's own words, then a transcript's text) within `cols` columns: as they are when
|
|
699
|
+
* they fit, else cut in logical order, between clusters of the whole, with … at the end of the last part shown. */
|
|
700
|
+
export function fitParts(parts, cols) {
|
|
701
|
+
if (cols <= 0) return [];
|
|
702
|
+
const whole = parts.join('');
|
|
703
|
+
if (width(whole) <= cols) return parts;
|
|
704
|
+
const kept = cut(whole, cols - 1).text.length, out = [];
|
|
705
|
+
let at = 0;
|
|
706
|
+
for (const p of parts) {
|
|
707
|
+
if (at + p.length <= kept) { out.push(p); at += p.length; continue; }
|
|
708
|
+
out.push(`${p.slice(0, kept - at)}…`);
|
|
709
|
+
break;
|
|
710
|
+
}
|
|
711
|
+
return out;
|
|
712
|
+
}
|