@mark1russell7/typeset 0.1.5 → 0.1.6
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/chunks/breaking.js +9 -2
- package/build/chunks/breaking.js.map +1 -1
- package/build/chunks/engines.js +1 -1
- package/build/chunks/font.js +174 -35
- package/build/chunks/font.js.map +1 -1
- package/build/chunks/fonts.js +1 -1
- package/build/chunks/measurer.d.ts.map +1 -1
- package/build/chunks/measurer.js +2 -0
- package/build/chunks/measurer.js.map +1 -1
- package/build/chunks/paragraph.d.ts +5 -0
- package/build/chunks/paragraph.d.ts.map +1 -1
- package/build/fonts-inspect.d.ts +11 -1
- package/build/fonts-inspect.d.ts.map +1 -1
- package/build/fonts-inspect.js +1 -1
- package/build/segmentation.d.ts +19 -0
- package/build/segmentation.d.ts.map +1 -0
- package/build/segmentation.js +267 -0
- package/build/segmentation.js.map +1 -0
- package/package.json +7 -3
- package/src/breaking/paragraph.ts +8 -2
- package/src/fonts/indic.ts +49 -41
- package/src/fonts/machine.ts +133 -0
- package/src/fonts-inspect.ts +2 -1
- package/src/measurer/advance.ts +4 -0
- package/src/segmentation/line-table.ts +5 -0
- package/src/segmentation/line.ts +203 -0
- package/src/segmentation/segments.ts +18 -0
- package/src/segmentation.ts +3 -0
|
@@ -50,6 +50,11 @@ export interface ParagraphOptions {
|
|
|
50
50
|
* offset. The width of each part of a word comes from `widthOf`, so the kerning across an offset is not in the sum.
|
|
51
51
|
*/
|
|
52
52
|
readonly hyphenate?: (word: string) => readonly number[];
|
|
53
|
+
/**
|
|
54
|
+
* The hook of the break places inside a word, for example `lineBreaks` of `./segmentation`. It gives offsets in a word where
|
|
55
|
+
* a line can end with no hyphen and no cost. The default gives no offset. This hook wins at an offset that `hyphenate` also gives.
|
|
56
|
+
*/
|
|
57
|
+
readonly breaks?: (word: string) => readonly number[];
|
|
53
58
|
/** The text that a line shows at a hyphen break. The default is "-". */
|
|
54
59
|
readonly hyphen?: string;
|
|
55
60
|
/** The cost of a hyphen break. The default is 50, as in TeX. */
|
|
@@ -72,12 +77,13 @@ export function paragraphOf(text: string, options: ParagraphOptions): Paragraph
|
|
|
72
77
|
for (const word of text.split(whiteSpace)) {
|
|
73
78
|
if (word === "") continue;
|
|
74
79
|
if (items.length > 0) items.push({ type: "glue", text: " ", width: space });
|
|
75
|
-
const
|
|
80
|
+
const free = new Set(options.breaks?.(word) ?? []);
|
|
81
|
+
const offsets = [...new Set([...(options.hyphenate?.(word) ?? []), ...free])].filter((o) => o > 0 && o < word.length).sort((a, b) => a - b);
|
|
76
82
|
let start = 0;
|
|
77
83
|
for (const offset of offsets) {
|
|
78
84
|
const part = word.slice(start, offset);
|
|
79
85
|
items.push({ type: "box", text: part, width: widthOf(part) });
|
|
80
|
-
items.push({ type: "penalty", text: hyphen, width: hyphenWidth, cost: hyphenCost, flagged: true });
|
|
86
|
+
items.push(free.has(offset) ? { type: "penalty", text: "", width: 0, cost: 0, flagged: false } : { type: "penalty", text: hyphen, width: hyphenWidth, cost: hyphenCost, flagged: true });
|
|
81
87
|
start = offset;
|
|
82
88
|
}
|
|
83
89
|
const last = word.slice(start);
|
package/src/fonts/indic.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* and the rules of the mode `COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT`. The table of the categories comes from `hb-ot-shaper-indic-table.cc`.
|
|
5
5
|
*/
|
|
6
6
|
import type { Font } from "./font.ts";
|
|
7
|
+
import { alt, any, compile, longestMatch, opt, seq, set, star, type Machine, type Pattern } from "./machine.ts";
|
|
7
8
|
import { normalForms, normalizeCodePoints, type NormalForms } from "./normalize.ts";
|
|
8
9
|
import { LIGATED, MULTIPLIED, SUBSTITUTED, type GlyphInfo } from "./substitution.ts";
|
|
9
10
|
import { isHidden } from "./normalize.ts";
|
|
@@ -129,55 +130,62 @@ const letters = new Map<number, string>([
|
|
|
129
130
|
[X, "x"], [C, "c"], [V, "v"], [N, "n"], [H, "h"], [ZWNJ, "w"], [ZWJ, "j"], [M, "m"], [SM, "s"], [A, "a"], [PLACEHOLDER, "p"],
|
|
130
131
|
[DOTTEDCIRCLE, "d"], [RS, "r"], [MPST, "q"], [REPHA, "f"], [RA, "R"], [CM, "k"], [SYMBOL, "y"], [CS, "S"], [SMPST, "t"],
|
|
131
132
|
]);
|
|
132
|
-
const c = "
|
|
133
|
-
const n = "
|
|
134
|
-
const z = "
|
|
135
|
-
const reph = "
|
|
136
|
-
const sm = "
|
|
137
|
-
const cn =
|
|
138
|
-
const matraGroup =
|
|
139
|
-
const syllableTail =
|
|
140
|
-
const halantGroup =
|
|
141
|
-
const finalHalantGroup =
|
|
142
|
-
const complexSyllableTail =
|
|
143
|
-
/** The rules of the machine in the order of HarfBuzz. A tie of two lengths goes to the first rule. */
|
|
144
|
-
const
|
|
145
|
-
[
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
133
|
+
const c = set("cR");
|
|
134
|
+
const n = seq(opt(seq(opt("w"), "r")), opt(seq("n", opt("n"))));
|
|
135
|
+
const z = set("wj");
|
|
136
|
+
const reph = alt("Rh", "f");
|
|
137
|
+
const sm = set("st");
|
|
138
|
+
const cn = seq(c, opt("j"), n);
|
|
139
|
+
const matraGroup = seq(star(z), alt("m", seq(opt(sm), "q")), opt("n"), opt("h"));
|
|
140
|
+
const syllableTail = seq(opt(seq(opt(z), sm, opt(sm), opt("w"))), star("a"));
|
|
141
|
+
const halantGroup = seq(opt(z), "h", opt(seq("j", opt("n"))));
|
|
142
|
+
const finalHalantGroup = alt(halantGroup, "hw");
|
|
143
|
+
const complexSyllableTail = seq(star(seq(halantGroup, cn)), opt("k"), alt(finalHalantGroup, star(matraGroup)), syllableTail);
|
|
144
|
+
/** The rules of the machine in the order of HarfBuzz, with the type of each. A tie of two lengths goes to the first rule. */
|
|
145
|
+
const indicRules: readonly (readonly [Pattern, number])[] = [
|
|
146
|
+
[seq(opt(set("fS")), cn, complexSyllableTail), CONSONANT_SYLLABLE],
|
|
147
|
+
[seq(opt(reph), "v", n, alt("j", complexSyllableTail)), VOWEL_SYLLABLE],
|
|
148
|
+
[seq(alt(seq(opt(set("fS")), "p"), seq(opt(reph), "d")), n, complexSyllableTail), STANDALONE_CLUSTER],
|
|
149
|
+
[seq("y", opt("n"), syllableTail), SYMBOL_CLUSTER],
|
|
150
|
+
["t", NON_INDIC_CLUSTER],
|
|
151
|
+
[seq(opt(reph), n, complexSyllableTail), BROKEN_CLUSTER],
|
|
152
|
+
[any, NON_INDIC_CLUSTER],
|
|
153
|
+
];
|
|
154
|
+
const machines: readonly Machine[] = indicRules.map(([rule]) => compile(rule));
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* This function divides a text of category letters into syllables, as the machine of HarfBuzz does. At each position,
|
|
158
|
+
* it takes the longest match of the rules, in one pass. It gives the length and the type of each syllable.
|
|
159
|
+
*/
|
|
160
|
+
export function letterSyllables(text: string): (readonly [length: number, type: number])[] {
|
|
161
|
+
const out: (readonly [number, number])[] = [];
|
|
162
|
+
let at = 0;
|
|
163
|
+
while (at < text.length) {
|
|
164
|
+
// No rule but the last one matches the category X, so a syllable stops before it
|
|
165
|
+
const next = text.indexOf("x", at + 1);
|
|
166
|
+
const match = longestMatch(machines, text, at, next < 0 ? text.length : next);
|
|
167
|
+
const length = match?.length ?? 1;
|
|
168
|
+
out.push([length, match === undefined ? NON_INDIC_CLUSTER : indicRules[match.rule]![1]]);
|
|
169
|
+
at += length;
|
|
170
|
+
}
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** This function gives the category letter of each code point of a text. The tests use it. */
|
|
175
|
+
export function indicLetters(text: string): string {
|
|
176
|
+
return Array.from(text, (character) => letters.get(indicProperties(character.codePointAt(0)!)[0]) ?? "x").join("");
|
|
177
|
+
}
|
|
155
178
|
|
|
156
179
|
/**
|
|
157
|
-
* This function divides the glyphs into syllables
|
|
158
|
-
*
|
|
159
|
-
* its low 4 bits are the type. The function tells if a syllable is a broken cluster.
|
|
180
|
+
* This function divides the glyphs into syllables. Each glyph gets the syllable byte. Its high 4 bits are a serial
|
|
181
|
+
* number from 1 to 15, and its low 4 bits are the type. The function tells if a syllable is a broken cluster.
|
|
160
182
|
*/
|
|
161
183
|
function findSyllables(infos: GlyphInfo[]): boolean {
|
|
162
184
|
const text = infos.map((info) => letters.get(info.category) ?? "x").join("");
|
|
163
185
|
let serial = 1;
|
|
164
186
|
let broken = false;
|
|
165
187
|
let at = 0;
|
|
166
|
-
|
|
167
|
-
// No rule but the last one matches the category X, so a syllable stops before it
|
|
168
|
-
const next = text.indexOf("x", at + 1);
|
|
169
|
-
let length = 1;
|
|
170
|
-
let type = NON_INDIC_CLUSTER;
|
|
171
|
-
search: for (let end = next < 0 ? text.length : next; end > at; end--) {
|
|
172
|
-
const piece = text.slice(at, end);
|
|
173
|
-
for (const [rule, kind] of machine) {
|
|
174
|
-
if (rule.test(piece)) {
|
|
175
|
-
length = end - at;
|
|
176
|
-
type = kind;
|
|
177
|
-
break search;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
188
|
+
for (const [length, type] of letterSyllables(text)) {
|
|
181
189
|
if (type === BROKEN_CLUSTER) broken = true;
|
|
182
190
|
for (let i = at; i < at + length; i++) infos[i]!.syllable = (serial << 4) | type;
|
|
183
191
|
serial = serial === 15 ? 1 : serial + 1;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small rule machine over a text of letters. Each rule is a pattern, and the machine finds the longest match of all
|
|
3
|
+
* rules at a position in one pass. It moves an automaton of each rule over the letters, so the time is linear in the
|
|
4
|
+
* length of the match. A regular expression with `^` and `$` gives the same matches, but a search over each end is
|
|
5
|
+
* quadratic. This module is internal to typeset.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** A pattern of a rule. A string is a sequence of its letters. */
|
|
9
|
+
export type Pattern =
|
|
10
|
+
| string
|
|
11
|
+
| { readonly kind: "set"; readonly letters: string }
|
|
12
|
+
| { readonly kind: "any" }
|
|
13
|
+
| { readonly kind: "seq"; readonly parts: readonly Pattern[] }
|
|
14
|
+
| { readonly kind: "alt"; readonly parts: readonly Pattern[] }
|
|
15
|
+
| { readonly kind: "star"; readonly part: Pattern }
|
|
16
|
+
| { readonly kind: "opt"; readonly part: Pattern };
|
|
17
|
+
|
|
18
|
+
/** This function gives a pattern that matches one of the letters. */
|
|
19
|
+
export const set = (letters: string): Pattern => ({ kind: "set", letters });
|
|
20
|
+
/** The pattern that matches any one letter. */
|
|
21
|
+
export const any: Pattern = { kind: "any" };
|
|
22
|
+
/** This function gives a pattern that matches the parts one after the other. */
|
|
23
|
+
export const seq = (...parts: Pattern[]): Pattern => ({ kind: "seq", parts });
|
|
24
|
+
/** This function gives a pattern that matches one of the parts. */
|
|
25
|
+
export const alt = (...parts: Pattern[]): Pattern => ({ kind: "alt", parts });
|
|
26
|
+
/** This function gives a pattern that matches the part 0 or more times. */
|
|
27
|
+
export const star = (part: Pattern): Pattern => ({ kind: "star", part });
|
|
28
|
+
/** This function gives a pattern that matches the part 0 or 1 times. */
|
|
29
|
+
export const opt = (part: Pattern): Pattern => ({ kind: "opt", part });
|
|
30
|
+
|
|
31
|
+
/** This function escapes a letter for a regular expression. */
|
|
32
|
+
const escape = (letter: string): string => letter.replace(/[\\^$.*+?()[\]{}|/-]/g, "\\$&");
|
|
33
|
+
|
|
34
|
+
/** This function gives the source of a regular expression with the same matches as a pattern. The tests use it. */
|
|
35
|
+
export function patternSource(pattern: Pattern): string {
|
|
36
|
+
if (typeof pattern === "string") return [...pattern].map(escape).join("");
|
|
37
|
+
switch (pattern.kind) {
|
|
38
|
+
case "set":
|
|
39
|
+
return `[${[...pattern.letters].map(escape).join("")}]`;
|
|
40
|
+
case "any":
|
|
41
|
+
return "[^]";
|
|
42
|
+
case "seq":
|
|
43
|
+
return pattern.parts.map((part) => `(?:${patternSource(part)})`).join("");
|
|
44
|
+
case "alt":
|
|
45
|
+
return `(?:${pattern.parts.map(patternSource).join("|")})`;
|
|
46
|
+
case "star":
|
|
47
|
+
return `(?:${patternSource(pattern.part)})*`;
|
|
48
|
+
case "opt":
|
|
49
|
+
return `(?:${patternSource(pattern.part)})?`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The automaton of one rule. A state with `letters` steps to `next` on one of its letters, or on any letter when
|
|
55
|
+
* `letters` is `null`. A state with `split` has steps with no letter to each of its states. State 0 accepts.
|
|
56
|
+
*/
|
|
57
|
+
interface State {
|
|
58
|
+
readonly letters?: string | null;
|
|
59
|
+
next?: number;
|
|
60
|
+
split?: number[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The automaton of one rule, with its start state. */
|
|
64
|
+
export interface Machine {
|
|
65
|
+
readonly states: readonly State[];
|
|
66
|
+
readonly start: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** This function builds the automaton of a pattern, by the construction of Thompson. */
|
|
70
|
+
export function compile(pattern: Pattern): Machine {
|
|
71
|
+
const states: State[] = [{}];
|
|
72
|
+
const build = (p: Pattern, next: number): number => {
|
|
73
|
+
if (typeof p === "string") return [...p].reduceRight((to, letter) => states.push({ letters: letter, next: to }) - 1, next);
|
|
74
|
+
switch (p.kind) {
|
|
75
|
+
case "set":
|
|
76
|
+
return states.push({ letters: p.letters, next }) - 1;
|
|
77
|
+
case "any":
|
|
78
|
+
return states.push({ letters: null, next }) - 1;
|
|
79
|
+
case "seq":
|
|
80
|
+
return p.parts.reduceRight((to, part) => build(part, to), next);
|
|
81
|
+
case "alt":
|
|
82
|
+
return states.push({ split: p.parts.map((part) => build(part, next)) }) - 1;
|
|
83
|
+
case "opt":
|
|
84
|
+
return states.push({ split: [build(p.part, next), next] }) - 1;
|
|
85
|
+
case "star": {
|
|
86
|
+
const loop = states.push({ split: [] }) - 1;
|
|
87
|
+
states[loop]!.split = [build(p.part, loop), next];
|
|
88
|
+
return loop;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
return { states, start: build(pattern, 0) };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** This function adds a state and each state that it reaches with no letter to a set. */
|
|
96
|
+
function close(machine: Machine, state: number, into: Set<number>): void {
|
|
97
|
+
const stack = [state];
|
|
98
|
+
while (stack.length > 0) {
|
|
99
|
+
const s = stack.pop()!;
|
|
100
|
+
if (into.has(s)) continue;
|
|
101
|
+
into.add(s);
|
|
102
|
+
for (const to of machine.states[s]!.split ?? []) stack.push(to);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* This function gives the longest match of the rules in `text` from `at`, which ends at `end` or before it. A tie of two
|
|
108
|
+
* rules goes to the first rule. A match has at least one letter. It gives `undefined` when no rule matches.
|
|
109
|
+
*/
|
|
110
|
+
export function longestMatch(machines: readonly Machine[], text: string, at: number, end: number): { readonly length: number; readonly rule: number } | undefined {
|
|
111
|
+
let best: { length: number; rule: number } | undefined;
|
|
112
|
+
let current = machines.map((machine) => {
|
|
113
|
+
const states = new Set<number>();
|
|
114
|
+
close(machine, machine.start, states);
|
|
115
|
+
return states;
|
|
116
|
+
});
|
|
117
|
+
for (let position = at; position < end && current.some((states) => states.size > 0); position++) {
|
|
118
|
+
const letter = text[position]!;
|
|
119
|
+
current = current.map((states, rule) => {
|
|
120
|
+
const machine = machines[rule]!;
|
|
121
|
+
const next = new Set<number>();
|
|
122
|
+
for (const s of states) {
|
|
123
|
+
const state = machine.states[s]!;
|
|
124
|
+
if (state.letters !== undefined && (state.letters === null || state.letters.includes(letter))) close(machine, state.next!, next);
|
|
125
|
+
}
|
|
126
|
+
return next;
|
|
127
|
+
});
|
|
128
|
+
const length = position + 1 - at;
|
|
129
|
+
const rule = current.findIndex((states) => states.has(0));
|
|
130
|
+
if (rule >= 0) best = { length, rule };
|
|
131
|
+
}
|
|
132
|
+
return best;
|
|
133
|
+
}
|
package/src/fonts-inspect.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The subpath `./fonts/inspect`: the parts of the font model that a tool uses to examine a width. They give the glyphs,
|
|
3
|
-
* the shaping words, the segments of a canvas, the default features and the tables of a font file.
|
|
3
|
+
* the shaping words, the Indic syllables, the segments of a canvas, the default features and the tables of a font file.
|
|
4
4
|
*/
|
|
5
5
|
export { glyphAdvances, shapingWords } from "./fonts/font.ts";
|
|
6
|
+
export { indicSyllables, type IndicSyllableType } from "./fonts/indic.ts";
|
|
6
7
|
export { canvasSegments, canvasWords } from "./fonts/words.ts";
|
|
7
8
|
export { defaultFeatures } from "./fonts/substitution.ts";
|
|
8
9
|
export { readTables } from "./fonts/sfnt.ts";
|
package/src/measurer/advance.ts
CHANGED
|
@@ -112,6 +112,8 @@ export function parseFont(font: string): FontSpec {
|
|
|
112
112
|
let rest = tokens.slice(index + 1).join(" ");
|
|
113
113
|
// A line height after a space: "16px / 1.2" or "16px /1.2"
|
|
114
114
|
if (size[3] === undefined && rest.startsWith("/")) rest = rest.slice(1).trim().replace(/^\S+\s*/, "");
|
|
115
|
+
// A line height after a slash and a space: "16px/ 1.2"
|
|
116
|
+
else if (size[3] === "/") rest = rest.replace(/^\S+\s*/, "");
|
|
115
117
|
const families = rest
|
|
116
118
|
.split(",")
|
|
117
119
|
.map((family) => family.trim().replace(/^(["'])(.*)\1$/, "$2").trim())
|
|
@@ -288,6 +290,8 @@ export function fontAdvance(options: FontAdvanceOptions): (text: string, font: s
|
|
|
288
290
|
let font = instances.get(key);
|
|
289
291
|
if (font === undefined) {
|
|
290
292
|
font = Object.keys(instance).length === 0 ? chosen.font : chosen.font.at(instance);
|
|
293
|
+
// A key holds the size for an axis opsz, so this cache has a bound, as the other caches have
|
|
294
|
+
if (instances.size >= 256) instances.clear();
|
|
291
295
|
instances.set(key, font);
|
|
292
296
|
}
|
|
293
297
|
return font;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Generated by `pnpm --filter @typeset/conformance tables` from Unicode 17.0.0. Do not edit.
|
|
2
|
+
export const unicodeVersion = "17.0.0";
|
|
3
|
+
export const classNames: readonly string[] = "AL CM BA LF BK CR SP EX QU PR PO OP CP IS HY SY NU CL NL GL BB HH HL JL JV JT NS AK VI AS ID VF ZW ZWJ B2 IN WJ EB H2 H3 CB AP RI EM".split(" ");
|
|
4
|
+
export const classRuns = "9B1C1D2E1FiB1G1H1I1A1J1K1A1I1L1M1A1J1N1O1N1PaQ2N3A1HrA1L1J1MtA1L1C1R1A6B1SqB1T1L1K3J5A1I1A1C2A1K1J2A1U6A1I3A1LegA1U3A1UiA1UwA2kB7TdBeA1N78A7B73A1N1V4A1J1A19B1V1B1A2B1A2B1H1B8ArW4A4WdA6Q3A3K2N2AbB1H1B3H17AlBaQ1K2Q3A1B2rA1H1A7B1Q1A6B2A2B1A4B2AaQnA1BuArB2jAbBfAaQxA9B4A1N1H3A1B2JmA4B1A9B1A3B1A5B17A3B1gA2Q5A9B16AoB1QxB1iA3B1AiB1A7BaA2B2CaQhA3B1kA1B1A7B2A2B2A3B9A1BaA2B2AaQ2A2K5A1K1A1J2A1B2A3B1kA1B1A5B4A2B2A3B3A1BkAaQ2B3A1BbA3B1kA1B1A8B1A3B1A3BkA2B2AaQ1A1J8A6B1A3B1kA1B1A7B2A2B2A3B7A3BaA2B2AaQiA1B1nA5B3A3B1A4B9A1BeAaQ9A1J6A5B1jA1B1A7B1A3B1A4B7A2BbA2B2AaQ7A1U9A3B1U1jA1B1A7B1A3B1A4B7A2BbA2B2AaQ3A1BcA4B1jA2B1A7B1A3B1A4B9A1BaA2B2AaQ9A1K7A3B1yA1B4A6B1A1B1A8B6AaQ2A2B1pA1B2A7B4A1J7A8B1AaQ2C2dA1B2A9BbA7B1AaQ13A4U1A2U1T2U1C1T5H1T1A1H3A2B6AaQaA1C1B1A1B1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m 1 5a 1 2 a 2t 3 d 18 4 9 7 2 e 6 4a x c 9 1 1y 1 m c 17 4 5 c h 3 1 3 1z 1 1 1 57 2 1r d 4 1 o i 1 q 2 d 1 2e 2d 1c 1y 6 1 3 3 2 4 3 4 b 2 7 9 6b c 4 1 7v 1b 1 a 1 55 34 d 3 b 3 1l 1 1 4 g 2 c 4 a zr 1eke 2 1eke i9du";
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The line break algorithm of UAX #14. The rules run in their order, and the first rule that matches decides. The class of a
|
|
3
|
+
* code point comes from a table that the tool in `packages/conformance` makes from the property files of Unicode.
|
|
4
|
+
*/
|
|
5
|
+
import { classNames, classRuns, eastAsianRuns } from "./line-table.ts";
|
|
6
|
+
|
|
7
|
+
/** A place where a line can end. */
|
|
8
|
+
export interface LineBreak {
|
|
9
|
+
/** The offset in UTF-16 units of the first character after the break. */
|
|
10
|
+
readonly offset: number;
|
|
11
|
+
/** This value is `true` when a line must end here, as at a line feed or at the end of the text. */
|
|
12
|
+
readonly required: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface Table {
|
|
16
|
+
readonly starts: Uint32Array;
|
|
17
|
+
readonly classes: Uint8Array;
|
|
18
|
+
readonly east: Uint32Array;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+-./:;<=>?@[]^_{|}~";
|
|
22
|
+
let table: Table | undefined;
|
|
23
|
+
|
|
24
|
+
/** This function decodes the runs one time, at the first use, so an import of the entry costs no work. */
|
|
25
|
+
function tableOf(): Table {
|
|
26
|
+
if (table !== undefined) return table;
|
|
27
|
+
const runs = [...classRuns.matchAll(/([0-9a-z]+)([^0-9a-z])/gu)];
|
|
28
|
+
const starts = new Uint32Array(runs.length + 1);
|
|
29
|
+
const classes = new Uint8Array(runs.length);
|
|
30
|
+
let start = 0;
|
|
31
|
+
runs.forEach((run, index) => {
|
|
32
|
+
starts[index] = start;
|
|
33
|
+
classes[index] = alphabet.indexOf(run[2]!);
|
|
34
|
+
start += Number.parseInt(run[1]!, 36);
|
|
35
|
+
});
|
|
36
|
+
starts[runs.length] = start;
|
|
37
|
+
const lengths = eastAsianRuns.split(" ").map((length) => Number.parseInt(length, 36));
|
|
38
|
+
const east = new Uint32Array(lengths.length + 1);
|
|
39
|
+
lengths.forEach((length, index) => {
|
|
40
|
+
east[index + 1] = east[index]! + length;
|
|
41
|
+
});
|
|
42
|
+
table = { starts, classes, east };
|
|
43
|
+
return table;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** This function gives the index of the last start that is not above `code`. */
|
|
47
|
+
function indexOfRun(starts: Uint32Array, code: number): number {
|
|
48
|
+
let low = 0;
|
|
49
|
+
let high = starts.length - 1;
|
|
50
|
+
while (high - low > 1) {
|
|
51
|
+
const middle = (low + high) >>> 1;
|
|
52
|
+
if (starts[middle]! <= code) low = middle;
|
|
53
|
+
else high = middle;
|
|
54
|
+
}
|
|
55
|
+
return low;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** One base character and its combining marks, as the rule LB9 treats them. */
|
|
59
|
+
interface Item {
|
|
60
|
+
readonly name: string;
|
|
61
|
+
readonly code: number;
|
|
62
|
+
readonly start: number;
|
|
63
|
+
readonly east: boolean;
|
|
64
|
+
/** This value is `true` when the last code point of the item is U+200D. */
|
|
65
|
+
zwj: boolean;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const breakClasses = new Set(["BK", "CR", "LF", "NL", "SP", "ZW"]);
|
|
69
|
+
|
|
70
|
+
/** This function makes the items of a text. It applies the rules LB9 and LB10. */
|
|
71
|
+
function itemsOf(text: string): Item[] {
|
|
72
|
+
const { starts, classes, east } = tableOf();
|
|
73
|
+
const items: Item[] = [];
|
|
74
|
+
let offset = 0;
|
|
75
|
+
for (const character of text) {
|
|
76
|
+
const code = character.codePointAt(0)!;
|
|
77
|
+
const name = classNames[classes[indexOfRun(starts, code)]!]!;
|
|
78
|
+
const combining = name === "CM" || name === "ZWJ";
|
|
79
|
+
const last = items.at(-1);
|
|
80
|
+
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 });
|
|
83
|
+
offset += character.length;
|
|
84
|
+
}
|
|
85
|
+
return items;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const initial = /^\p{Pi}$/u;
|
|
89
|
+
const final = /^\p{Pf}$/u;
|
|
90
|
+
const pictographic = /^\p{Extended_Pictographic}$/u;
|
|
91
|
+
const unassigned = /^\p{Cn}$/u;
|
|
92
|
+
const of = (item: Item): string => String.fromCodePoint(item.code);
|
|
93
|
+
const isPi = (item: Item): boolean => initial.test(of(item));
|
|
94
|
+
const isPf = (item: Item): boolean => final.test(of(item));
|
|
95
|
+
const inClass = (item: Item | undefined, names: Set<string>): boolean => item !== undefined && names.has(item.name);
|
|
96
|
+
const isName = (item: Item | undefined, name: string): boolean => item?.name === name;
|
|
97
|
+
|
|
98
|
+
const beforeOpening = new Set(["BK", "CR", "LF", "NL", "OP", "QU", "GL", "SP", "ZW"]);
|
|
99
|
+
const afterClosing = new Set(["SP", "GL", "WJ", "CL", "QU", "CP", "EX", "IS", "SY", "BK", "CR", "LF", "NL", "ZW"]);
|
|
100
|
+
const beforeHyphen = new Set(["BK", "CR", "LF", "NL", "SP", "ZW", "CB", "GL"]);
|
|
101
|
+
const alphabetic = new Set(["AL", "HL"]);
|
|
102
|
+
const hyphens = new Set(["HY", "HH"]);
|
|
103
|
+
const emoji = new Set(["ID", "EB", "EM"]);
|
|
104
|
+
const prefix = new Set(["PR", "PO"]);
|
|
105
|
+
const closing = new Set(["CL", "CP"]);
|
|
106
|
+
const separators = new Set(["SY", "IS"]);
|
|
107
|
+
const hangul = new Set(["JL", "JV", "JT", "H2", "H3"]);
|
|
108
|
+
const aksara = (item: Item | undefined): boolean => item !== undefined && (item.name === "AK" || item.name === "AS" || item.code === 0x25cc);
|
|
109
|
+
const aksaraStart = (item: Item | undefined): boolean => item !== undefined && (item.name === "AK" || item.code === 0x25cc);
|
|
110
|
+
|
|
111
|
+
/** This function tells if the items before `index` end with `NU (SY | IS)*`. */
|
|
112
|
+
function afterNumber(items: readonly Item[], index: number): boolean {
|
|
113
|
+
let i = index;
|
|
114
|
+
while (i >= 0 && separators.has(items[i]!.name)) i--;
|
|
115
|
+
return isName(items[i], "NU");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** This function applies the rules LB4 to LB31 at the start of the item `i`. It gives 0 for no break, 1 for a break and 2 for a necessary break. */
|
|
119
|
+
function ruleAt(items: readonly Item[], i: number): 0 | 1 | 2 {
|
|
120
|
+
const a = items[i - 1]!;
|
|
121
|
+
const b = items[i]!;
|
|
122
|
+
const c = a.name;
|
|
123
|
+
const d = b.name;
|
|
124
|
+
const earlier = items[i - 2];
|
|
125
|
+
const later = items[i + 1];
|
|
126
|
+
if (c === "BK" || c === "NL" || c === "LF") return 2;
|
|
127
|
+
if (c === "CR") return d === "LF" ? 0 : 2;
|
|
128
|
+
if (d === "BK" || d === "CR" || d === "LF" || d === "NL" || d === "SP" || d === "ZW") return 0;
|
|
129
|
+
let last = i - 1;
|
|
130
|
+
while (last >= 0 && items[last]!.name === "SP") last--;
|
|
131
|
+
const beforeSpaces = items[last];
|
|
132
|
+
if (isName(beforeSpaces, "ZW")) return 1;
|
|
133
|
+
if (a.zwj) return 0;
|
|
134
|
+
if (d === "WJ" || c === "WJ" || c === "GL") return 0;
|
|
135
|
+
if (d === "GL" && c !== "SP" && c !== "BA" && !hyphens.has(c)) return 0;
|
|
136
|
+
if (d === "CL" || d === "CP" || d === "EX" || d === "SY") return 0;
|
|
137
|
+
if (isName(beforeSpaces, "OP")) return 0;
|
|
138
|
+
if (beforeSpaces !== undefined && beforeSpaces.name === "QU" && isPi(beforeSpaces)) {
|
|
139
|
+
const before = items[last - 1];
|
|
140
|
+
if (before === undefined || beforeOpening.has(before.name)) return 0;
|
|
141
|
+
}
|
|
142
|
+
if (d === "QU" && isPf(b) && (later === undefined || afterClosing.has(later.name))) return 0;
|
|
143
|
+
if (c === "SP" && d === "IS" && isName(later, "NU")) return 1;
|
|
144
|
+
if (d === "IS") return 0;
|
|
145
|
+
if (d === "NS" && inClass(beforeSpaces, closing)) return 0;
|
|
146
|
+
if (d === "B2" && isName(beforeSpaces, "B2")) return 0;
|
|
147
|
+
if (c === "SP") return 1;
|
|
148
|
+
if (d === "QU" && !isPi(b)) return 0;
|
|
149
|
+
if (c === "QU" && !isPf(a)) return 0;
|
|
150
|
+
if (d === "QU" && (!a.east || later === undefined || !later.east)) return 0;
|
|
151
|
+
if (c === "QU" && (!b.east || earlier === undefined || !earlier.east)) return 0;
|
|
152
|
+
if (d === "CB" || c === "CB") return 1;
|
|
153
|
+
if (hyphens.has(c) && alphabetic.has(d) && (earlier === undefined || beforeHyphen.has(earlier.name))) return 0;
|
|
154
|
+
if (d === "BA" || d === "HH" || d === "HY" || d === "NS" || c === "BB") return 0;
|
|
155
|
+
if (isName(earlier, "HL") && hyphens.has(c) && d !== "HL") return 0;
|
|
156
|
+
if (c === "SY" && d === "HL") return 0;
|
|
157
|
+
if (d === "IN") return 0;
|
|
158
|
+
if (alphabetic.has(c) && d === "NU") return 0;
|
|
159
|
+
if (c === "NU" && alphabetic.has(d)) return 0;
|
|
160
|
+
if (c === "PR" && emoji.has(d)) return 0;
|
|
161
|
+
if (emoji.has(c) && d === "PO") return 0;
|
|
162
|
+
if (prefix.has(c) && alphabetic.has(d)) return 0;
|
|
163
|
+
if (alphabetic.has(c) && prefix.has(d)) return 0;
|
|
164
|
+
if (prefix.has(d) && afterNumber(items, closing.has(c) ? i - 2 : i - 1)) return 0;
|
|
165
|
+
if (d === "NU" && afterNumber(items, i - 1)) return 0;
|
|
166
|
+
if (prefix.has(c) && (d === "NU" || (d === "OP" && (isName(later, "NU") || (isName(later, "IS") && isName(items[i + 2], "NU")))))) return 0;
|
|
167
|
+
if ((c === "HY" || c === "IS") && d === "NU") return 0;
|
|
168
|
+
if (c === "JL" && (d === "JL" || d === "JV" || d === "H2" || d === "H3")) return 0;
|
|
169
|
+
if ((c === "JV" || c === "H2") && (d === "JV" || d === "JT")) return 0;
|
|
170
|
+
if ((c === "JT" || c === "H3") && d === "JT") return 0;
|
|
171
|
+
if (hangul.has(c) && d === "PO") return 0;
|
|
172
|
+
if (c === "PR" && hangul.has(d)) return 0;
|
|
173
|
+
if (alphabetic.has(c) && alphabetic.has(d)) return 0;
|
|
174
|
+
if (c === "AP" && aksara(b)) return 0;
|
|
175
|
+
if (aksara(a) && (d === "VF" || d === "VI")) return 0;
|
|
176
|
+
if (c === "VI" && aksara(earlier) && aksaraStart(b)) return 0;
|
|
177
|
+
if (aksara(a) && aksara(b) && isName(later, "VF")) return 0;
|
|
178
|
+
if (c === "IS" && alphabetic.has(d)) return 0;
|
|
179
|
+
if ((alphabetic.has(c) || c === "NU") && d === "OP" && !b.east) return 0;
|
|
180
|
+
if (c === "CP" && !a.east && (alphabetic.has(d) || d === "NU")) return 0;
|
|
181
|
+
if (c === "RI" && d === "RI") {
|
|
182
|
+
let count = 0;
|
|
183
|
+
for (let k = i - 1; k >= 0 && items[k]!.name === "RI"; k--) count++;
|
|
184
|
+
if (count % 2 === 1) return 0;
|
|
185
|
+
}
|
|
186
|
+
if (d === "EM" && (c === "EB" || (pictographic.test(of(a)) && unassigned.test(of(a))))) return 0;
|
|
187
|
+
return 1;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* This function gives the places where a line of a text can end, by the rules of UAX #14 without tailoring. The last place is
|
|
192
|
+
* the end of the text, and an empty text has no place.
|
|
193
|
+
*/
|
|
194
|
+
export function lineBreaks(text: string): readonly LineBreak[] {
|
|
195
|
+
const items = itemsOf(text);
|
|
196
|
+
const breaks: LineBreak[] = [];
|
|
197
|
+
for (let i = 1; i < items.length; i++) {
|
|
198
|
+
const rule = ruleAt(items, i);
|
|
199
|
+
if (rule !== 0) breaks.push({ offset: items[i]!.start, required: rule === 2 });
|
|
200
|
+
}
|
|
201
|
+
if (text.length > 0) breaks.push({ offset: text.length, required: true });
|
|
202
|
+
return breaks;
|
|
203
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The segments of UAX #29, from `Intl.Segmenter`. The results follow the Unicode version of the runtime. The locale is the root
|
|
3
|
+
* locale, so the language of the host has no effect.
|
|
4
|
+
*/
|
|
5
|
+
const graphemeSegmenter = new Intl.Segmenter("und", { granularity: "grapheme" });
|
|
6
|
+
const wordSegmenter = new Intl.Segmenter("und", { granularity: "word" });
|
|
7
|
+
|
|
8
|
+
/** This function gives the grapheme clusters of a text, in their order. */
|
|
9
|
+
export function graphemes(text: string): readonly string[] {
|
|
10
|
+
return Array.from(graphemeSegmenter.segment(text), (part) => part.segment);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** This function gives the offset in UTF-16 units of each word boundary of a text, with the end of the text and without its start. */
|
|
14
|
+
export function wordBreaks(text: string): readonly number[] {
|
|
15
|
+
const offsets = Array.from(wordSegmenter.segment(text), (part) => part.index).slice(1);
|
|
16
|
+
if (text.length > 0) offsets.push(text.length);
|
|
17
|
+
return offsets;
|
|
18
|
+
}
|