@mark1russell7/typeset 0.1.8 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -70,6 +70,8 @@ interface Item {
70
70
  readonly east: boolean;
71
71
  /** This value is `true` when the last code point of the item is U+200D. */
72
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;
73
75
  }
74
76
 
75
77
  const breakClasses = new Set(["BK", "CR", "LF", "NL", "SP", "ZW"]);
@@ -85,8 +87,9 @@ function itemsOf(text: string): Item[] {
85
87
  const combining = name === "CM" || name === "ZWJ";
86
88
  const last = items.at(-1);
87
89
  if (combining && last !== undefined && !breakClasses.has(last.name)) last.zwj = name === "ZWJ";
88
- else if (combining) items.push({ name: "AL", code: 0x41, start: offset, east: false, zwj: name === "ZWJ" });
89
- else items.push({ name, code, start: offset, east: code < 128 ? asciiEast[code]! : 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 });
90
93
  offset += character.length;
91
94
  }
92
95
  return items;
@@ -194,15 +197,29 @@ function ruleAt(items: readonly Item[], i: number): 0 | 1 | 2 {
194
197
  return 1;
195
198
  }
196
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
+
197
210
  /**
198
- * 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
199
- * 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.
200
216
  *
201
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
202
- * end after each run of spaces (LB18), and at no other place (LB7, LB23, LB25 and LB28).
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.
203
220
  */
204
- export function lineBreaks(text: string): readonly LineBreak[] {
205
- return simple.test(text) ? simpleBreaks(text) : ruleBreaks(text);
221
+ export function lineBreaks(text: string, options: LineBreakOptions = {}): readonly LineBreak[] {
222
+ return simple.test(text) ? simpleBreaks(text) : ruleBreaks(text, options);
206
223
  }
207
224
 
208
225
  /** The texts of the short path of `lineBreaks`. */
@@ -220,13 +237,30 @@ function simpleBreaks(text: string): LineBreak[] {
220
237
  * This function gives the places of a text by the rules of UAX #14, with no short path. The tests compare `lineBreaks` with
221
238
  * it. The entry `./segmentation` does not export it.
222
239
  */
223
- export function ruleBreaks(text: string): readonly LineBreak[] {
240
+ export function ruleBreaks(text: string, options: LineBreakOptions = {}): readonly LineBreak[] {
224
241
  const items = itemsOf(text);
242
+ const inside = options.complex === undefined ? undefined : complexPlaces(text, items, options.complex);
225
243
  const breaks: LineBreak[] = [];
226
244
  for (let i = 1; i < items.length; i++) {
227
245
  const rule = ruleAt(items, i);
228
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 });
229
248
  }
230
249
  if (text.length > 0) breaks.push({ offset: text.length, required: true });
231
250
  return breaks;
232
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
+ }
@@ -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";