@mark1russell7/typeset 0.1.7 → 0.1.9
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/build/bidi.d.ts +50 -0
- package/build/bidi.d.ts.map +1 -0
- package/build/bidi.js +41 -0
- package/build/bidi.js.map +1 -0
- package/build/chunks/font.js +37 -71
- package/build/chunks/font.js.map +1 -1
- package/build/chunks/resolve.js +382 -0
- package/build/chunks/resolve.js.map +1 -0
- package/build/segmentation.d.ts +32 -4
- package/build/segmentation.d.ts.map +1 -1
- package/build/segmentation.js +116 -12
- package/build/segmentation.js.map +1 -1
- package/package.json +6 -2
- package/src/bidi/brackets.ts +23 -0
- package/src/bidi/classes.ts +43 -0
- package/src/bidi/resolve.ts +380 -0
- package/src/bidi/table.ts +5 -0
- package/src/bidi.ts +4 -0
- package/src/fonts/bidi.ts +26 -86
- package/src/segmentation/line-table.ts +2 -2
- package/src/segmentation/line.ts +71 -8
- package/src/segmentation/segments.ts +47 -0
- package/src/segmentation.ts +2 -2
package/src/segmentation/line.ts
CHANGED
|
@@ -16,6 +16,10 @@ interface Table {
|
|
|
16
16
|
readonly starts: Uint32Array;
|
|
17
17
|
readonly classes: Uint8Array;
|
|
18
18
|
readonly east: Uint32Array;
|
|
19
|
+
/** The class name of each ASCII code point, from `starts` and `classes`. */
|
|
20
|
+
readonly asciiNames: readonly string[];
|
|
21
|
+
/** This value is `true` for an ASCII code point that is wide in East Asian text, from `east`. */
|
|
22
|
+
readonly asciiEast: readonly boolean[];
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+-./:;<=>?@[]^_{|}~";
|
|
@@ -39,7 +43,10 @@ function tableOf(): Table {
|
|
|
39
43
|
lengths.forEach((length, index) => {
|
|
40
44
|
east[index + 1] = east[index]! + length;
|
|
41
45
|
});
|
|
42
|
-
|
|
46
|
+
const codes = Array.from({ length: 128 }, (_, code) => code);
|
|
47
|
+
const asciiNames = codes.map((code) => classNames[classes[indexOfRun(starts, code)]!]!);
|
|
48
|
+
const asciiEast = codes.map((code) => indexOfRun(east, code) % 2 === 1);
|
|
49
|
+
table = { starts, classes, east, asciiNames, asciiEast };
|
|
43
50
|
return table;
|
|
44
51
|
}
|
|
45
52
|
|
|
@@ -63,23 +70,26 @@ interface Item {
|
|
|
63
70
|
readonly east: boolean;
|
|
64
71
|
/** This value is `true` when the last code point of the item is U+200D. */
|
|
65
72
|
zwj: boolean;
|
|
73
|
+
/** This value is `true` when the base character has the class SA. The rules read the item as AL. */
|
|
74
|
+
readonly complex: boolean;
|
|
66
75
|
}
|
|
67
76
|
|
|
68
77
|
const breakClasses = new Set(["BK", "CR", "LF", "NL", "SP", "ZW"]);
|
|
69
78
|
|
|
70
79
|
/** This function makes the items of a text. It applies the rules LB9 and LB10. */
|
|
71
80
|
function itemsOf(text: string): Item[] {
|
|
72
|
-
const { starts, classes, east } = tableOf();
|
|
81
|
+
const { starts, classes, east, asciiNames, asciiEast } = tableOf();
|
|
73
82
|
const items: Item[] = [];
|
|
74
83
|
let offset = 0;
|
|
75
84
|
for (const character of text) {
|
|
76
85
|
const code = character.codePointAt(0)!;
|
|
77
|
-
const name = classNames[classes[indexOfRun(starts, code)]!]!;
|
|
86
|
+
const name = code < 128 ? asciiNames[code]! : classNames[classes[indexOfRun(starts, code)]!]!;
|
|
78
87
|
const combining = name === "CM" || name === "ZWJ";
|
|
79
88
|
const last = items.at(-1);
|
|
80
89
|
if (combining && last !== undefined && !breakClasses.has(last.name)) last.zwj = name === "ZWJ";
|
|
81
|
-
else if (combining) items.push({ name: "AL", code: 0x41, start: offset, east: false, zwj: name === "ZWJ" });
|
|
82
|
-
else items.push({ name, code, start: offset, east: indexOfRun(east, code) % 2 === 1, zwj: false });
|
|
90
|
+
else if (combining) items.push({ name: "AL", code: 0x41, start: offset, east: false, zwj: name === "ZWJ", complex: false });
|
|
91
|
+
else if (name === "SA") items.push({ name: "AL", code, start: offset, east: indexOfRun(east, code) % 2 === 1, zwj: false, complex: true });
|
|
92
|
+
else items.push({ name, code, start: offset, east: code < 128 ? asciiEast[code]! : indexOfRun(east, code) % 2 === 1, zwj: false, complex: false });
|
|
83
93
|
offset += character.length;
|
|
84
94
|
}
|
|
85
95
|
return items;
|
|
@@ -187,17 +197,70 @@ function ruleAt(items: readonly Item[], i: number): 0 | 1 | 2 {
|
|
|
187
197
|
return 1;
|
|
188
198
|
}
|
|
189
199
|
|
|
200
|
+
/** The options of `lineBreaks`. */
|
|
201
|
+
export interface LineBreakOptions {
|
|
202
|
+
/**
|
|
203
|
+
* The hook gives the break places inside a run of class SA (Thai, Lao, Khmer, Myanmar and others). It gets the text of the
|
|
204
|
+
* run and gives offsets in the run. `complexBreaks` is a hook. With no hook, the rule LB1 reads the run as AL, so no break
|
|
205
|
+
* is inside it, as in `LineBreakTest`.
|
|
206
|
+
*/
|
|
207
|
+
readonly complex?: (run: string) => readonly number[];
|
|
208
|
+
}
|
|
209
|
+
|
|
190
210
|
/**
|
|
191
|
-
* This function gives the places where a line of a text can end, by the rules of UAX #14
|
|
192
|
-
* the end of the text, and an empty text has no place.
|
|
211
|
+
* This function gives the places where a line of a text can end, by the rules of UAX #14. With no option, it applies no
|
|
212
|
+
* tailoring. The last place is the end of the text, and an empty text has no place.
|
|
213
|
+
*
|
|
214
|
+
* The hook `complex` adds places inside each run of class SA. The function keeps only the places at the start of an item,
|
|
215
|
+
* so no place is before a combining mark. The places outside the runs do not change.
|
|
216
|
+
*
|
|
217
|
+
* A text of only ASCII letters, digits and spaces takes a short path, because the rules give it a simple result. A line can
|
|
218
|
+
* end after each run of spaces (LB18), and at no other place (LB7, LB23, LB25 and LB28). Such a text has no run of class
|
|
219
|
+
* SA, so the hook adds no place to it.
|
|
193
220
|
*/
|
|
194
|
-
export function lineBreaks(text: string): readonly LineBreak[] {
|
|
221
|
+
export function lineBreaks(text: string, options: LineBreakOptions = {}): readonly LineBreak[] {
|
|
222
|
+
return simple.test(text) ? simpleBreaks(text) : ruleBreaks(text, options);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** The texts of the short path of `lineBreaks`. */
|
|
226
|
+
const simple = /^[A-Za-z0-9 ]*$/u;
|
|
227
|
+
|
|
228
|
+
/** This function gives the places of a text of only ASCII letters, digits and spaces: after each run of spaces, and at the end. */
|
|
229
|
+
function simpleBreaks(text: string): LineBreak[] {
|
|
230
|
+
const breaks: LineBreak[] = [];
|
|
231
|
+
for (let i = 1; i < text.length; i++) if (text.charCodeAt(i - 1) === 32 && text.charCodeAt(i) !== 32) breaks.push({ offset: i, required: false });
|
|
232
|
+
if (text.length > 0) breaks.push({ offset: text.length, required: true });
|
|
233
|
+
return breaks;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* This function gives the places of a text by the rules of UAX #14, with no short path. The tests compare `lineBreaks` with
|
|
238
|
+
* it. The entry `./segmentation` does not export it.
|
|
239
|
+
*/
|
|
240
|
+
export function ruleBreaks(text: string, options: LineBreakOptions = {}): readonly LineBreak[] {
|
|
195
241
|
const items = itemsOf(text);
|
|
242
|
+
const inside = options.complex === undefined ? undefined : complexPlaces(text, items, options.complex);
|
|
196
243
|
const breaks: LineBreak[] = [];
|
|
197
244
|
for (let i = 1; i < items.length; i++) {
|
|
198
245
|
const rule = ruleAt(items, i);
|
|
199
246
|
if (rule !== 0) breaks.push({ offset: items[i]!.start, required: rule === 2 });
|
|
247
|
+
else if (inside?.has(i)) breaks.push({ offset: items[i]!.start, required: false });
|
|
200
248
|
}
|
|
201
249
|
if (text.length > 0) breaks.push({ offset: text.length, required: true });
|
|
202
250
|
return breaks;
|
|
203
251
|
}
|
|
252
|
+
|
|
253
|
+
/** This function gives the indexes of the items inside the runs of class SA where the hook gives a place. */
|
|
254
|
+
function complexPlaces(text: string, items: readonly Item[], complex: (run: string) => readonly number[]): Set<number> {
|
|
255
|
+
const places = new Set<number>();
|
|
256
|
+
for (let i = 0; i < items.length; i++) {
|
|
257
|
+
if (!items[i]!.complex) continue;
|
|
258
|
+
let last = i;
|
|
259
|
+
while (last + 1 < items.length && items[last + 1]!.complex) last++;
|
|
260
|
+
const start = items[i]!.start;
|
|
261
|
+
const offsets = new Set(complex(text.slice(start, items[last + 1]?.start ?? text.length)));
|
|
262
|
+
for (let k = i + 1; k <= last; k++) if (offsets.has(items[k]!.start - start)) places.add(k);
|
|
263
|
+
i = last;
|
|
264
|
+
}
|
|
265
|
+
return places;
|
|
266
|
+
}
|
|
@@ -16,3 +16,50 @@ export function wordBreaks(text: string): readonly number[] {
|
|
|
16
16
|
if (text.length > 0) offsets.push(text.length);
|
|
17
17
|
return offsets;
|
|
18
18
|
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* This function gives the word boundaries of each word of a list. Each entry equals `wordBreaks` of its word. The function
|
|
22
|
+
* segments the words in one pass, joined by line feeds, because UAX #29 breaks before and after each line feed (WB3a, WB3b).
|
|
23
|
+
* A word that ends in a carriage return gets its own pass, because a carriage return and a line feed stay together (WB3).
|
|
24
|
+
*/
|
|
25
|
+
export function wordBreaksOf(words: readonly string[]): readonly (readonly number[])[] {
|
|
26
|
+
const result: (readonly number[])[] = [];
|
|
27
|
+
const lists: number[][] = [];
|
|
28
|
+
const starts: number[] = [];
|
|
29
|
+
const parts: string[] = [];
|
|
30
|
+
let offset = 0;
|
|
31
|
+
for (let index = 0; index < words.length; index++) {
|
|
32
|
+
const word = words[index]!;
|
|
33
|
+
if (word.endsWith("\r")) {
|
|
34
|
+
result[index] = wordBreaks(word);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const own: number[] = [];
|
|
38
|
+
result[index] = own;
|
|
39
|
+
lists.push(own);
|
|
40
|
+
starts.push(offset);
|
|
41
|
+
parts.push(word);
|
|
42
|
+
offset += word.length + 1;
|
|
43
|
+
}
|
|
44
|
+
const text = parts.join("\n");
|
|
45
|
+
let at = 0;
|
|
46
|
+
// The boundaries come in order, so one sweep gives each boundary to its word. The boundary at the end of a word is the
|
|
47
|
+
// start of the next line feed. The boundary after that line feed is the start of the next word, so no word gets it.
|
|
48
|
+
for (const part of wordSegmenter.segment(text)) {
|
|
49
|
+
const boundary = part.index;
|
|
50
|
+
while (at < lists.length && boundary > starts[at]! + parts[at]!.length) at++;
|
|
51
|
+
if (at < lists.length && boundary > starts[at]!) lists[at]!.push(boundary - starts[at]!);
|
|
52
|
+
}
|
|
53
|
+
const last = lists.length - 1;
|
|
54
|
+
if (last >= 0 && parts[last]!.length > 0) lists[last]!.push(parts[last]!.length);
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* This function is a hook `complex` for `lineBreaks`. It gives the word boundaries inside a run of class SA, from
|
|
60
|
+
* `Intl.Segmenter` of the runtime. The runtime has the dictionary, so the entry ships no dictionary. In a browser, the places
|
|
61
|
+
* follow the dictionary of that browser.
|
|
62
|
+
*/
|
|
63
|
+
export function complexBreaks(run: string): readonly number[] {
|
|
64
|
+
return wordBreaks(run).slice(0, -1);
|
|
65
|
+
}
|
package/src/segmentation.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { lineBreaks, type LineBreak } from "./segmentation/line.ts";
|
|
1
|
+
export { lineBreaks, type LineBreak, type LineBreakOptions } from "./segmentation/line.ts";
|
|
2
2
|
export { unicodeVersion } from "./segmentation/line-table.ts";
|
|
3
|
-
export { graphemes, wordBreaks } from "./segmentation/segments.ts";
|
|
3
|
+
export { complexBreaks, graphemes, wordBreaks, wordBreaksOf } from "./segmentation/segments.ts";
|