@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.
@@ -0,0 +1,50 @@
1
+ /**
2
+ * This function gives the value of a code point for the `brackets` of `resolveLevels`. The value is above 0 for an opening
3
+ * paired bracket, below 0 for a closing paired bracket, and 0 for each other code point. U+3009 and U+232A have the same value.
4
+ */
5
+ export declare function bracketOf(codePoint: number): number;
6
+ /**
7
+ * The rules of the Unicode Bidirectional Algorithm (UAX #9). This module works on bidi classes and has no table, so `./fonts`
8
+ * can use it with its own classes. The comments give the names of the rules. This module treats each paragraph as one line.
9
+ */
10
+ /** A bidi class of UAX #9. */
11
+ type BidiClass = "L" | "R" | "AL" | "EN" | "ES" | "ET" | "AN" | "CS" | "NSM" | "BN" | "B" | "S" | "WS" | "ON" | "LRE" | "LRO" | "RLE" | "RLO" | "PDF" | "LRI" | "RLI" | "FSI" | "PDI";
12
+ /** The paragraph direction: `0` is left to right, `1` is right to left, and `"auto"` applies the rules P2 and P3. */
13
+ type ParagraphDirection = 0 | 1 | "auto";
14
+ /** One paragraph of a resolved text. */
15
+ interface BidiParagraph {
16
+ /** The index of the first character. */
17
+ readonly start: number;
18
+ /** The index after the last character. */
19
+ readonly end: number;
20
+ /** The paragraph embedding level. */
21
+ readonly level: number;
22
+ }
23
+ /** The result of `resolveLevels`. */
24
+ interface ResolvedBidi {
25
+ /** The level of each character. The value is `removed` for a character that the rule X9 removes. */
26
+ readonly levels: number[];
27
+ readonly paragraphs: readonly BidiParagraph[];
28
+ }
29
+ /** This value is the level of a character that the rule X9 removes. */
30
+ export declare const removed = -1;
31
+ /**
32
+ * This function gives the levels of a text from its classes, with the rules P1 to L1 of UAX #9. A class `B` ends a paragraph.
33
+ * A value of `brackets` is 0 for a character that is not a paired bracket. It is above 0 at an opening bracket and below 0
34
+ * at the closing bracket with the same absolute value. `bracketOf` of `./bidi` gives these values.
35
+ */
36
+ export declare function resolveLevels(classes: readonly BidiClass[], brackets?: readonly number[], direction?: ParagraphDirection): ResolvedBidi;
37
+ /**
38
+ * This function puts items in the visual order of the rule L2 of UAX #9. Each item has the level `levels[i]`. From the
39
+ * highest level to the level 1, it reverses each sequence of items at that level or higher.
40
+ */
41
+ export declare function visualOrder<T>(items: readonly T[], levels: readonly number[]): T[];
42
+ /** This function gives the indexes of the characters in the visual order, for each paragraph as one line. It omits each removed character. */
43
+ export declare function visualIndexes({ levels, paragraphs }: ResolvedBidi): number[];
44
+ /** This function gives the bidi class of a code point. */
45
+ export declare function bidiClass(codePoint: number): BidiClass;
46
+ /** This function gives the levels of each code point of a text. It applies `resolveLevels` with the exact classes and brackets. */
47
+ export declare function resolveText(text: string, direction?: ParagraphDirection): ResolvedBidi;
48
+ export declare const unicodeVersion = "17.0.0";
49
+ export type { BidiClass, BidiParagraph, ParagraphDirection, ResolvedBidi };
50
+ //# sourceMappingURL=bidi.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bidi.d.ts","names":[],"sources":["../src/bidi/brackets.ts","../src/bidi/resolve.ts","../src/bidi/classes.ts","../src/bidi/table.ts"],"mappings":";;;;wBAQgB,UAAU;;;;;;KCFd;;KA0BA;;UAGK;;WAEN;;WAEA;;WAEA;;;UAIM;;WAEN;WACA,qBAAqB;;;qBAInB;;;;;;wBAmRG,cAAc,kBAAkB,aAAa,8BAAkC,YAAW,qBAA8B;;;;;wBA2BxH,YAAY,GAAG,gBAAgB,KAAK,4BAA4B;;wBAgBhE,gBAAgB,QAAQ,cAAc;;wBCxVtC,UAAU,oBAAoB;;wBAa9B,YAAY,cAAc,YAAW,qBAA8B;qBCtCtE"}
package/build/bidi.js ADDED
@@ -0,0 +1,41 @@
1
+ import { a as bracketOf, c as unicodeVersion, i as visualOrder, n as resolveLevels, o as classNames, r as visualIndexes, s as classRuns, t as removed } from "./chunks/resolve.js";
2
+ // src/bidi/classes.ts
3
+ /**
4
+ * The class and the paired bracket of a code point. These values come from a table that the tool in `packages/conformance`
5
+ * makes from the property files of Unicode.
6
+ */
7
+ const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+-./:;<=>?@[]^_{|}~";
8
+ let starts;
9
+ let runClasses;
10
+ /** This function decodes the runs one time, at the first use, so an import of the entry costs no work. */
11
+ function decodeRuns() {
12
+ const runs = [...classRuns.matchAll(/([0-9a-z]+)([^0-9a-z])/gu)];
13
+ starts = new Uint32Array(runs.length);
14
+ runClasses = new Uint8Array(runs.length);
15
+ let start = 0;
16
+ runs.forEach((run, index) => {
17
+ starts[index] = start;
18
+ runClasses[index] = alphabet.indexOf(run[2]);
19
+ start += Number.parseInt(run[1], 36);
20
+ });
21
+ }
22
+ /** This function gives the bidi class of a code point. */
23
+ function bidiClass(codePoint) {
24
+ if (starts === void 0) decodeRuns();
25
+ let low = 0;
26
+ let high = starts.length;
27
+ while (high - low > 1) {
28
+ const middle = low + high >>> 1;
29
+ if (starts[middle] <= codePoint) low = middle;
30
+ else high = middle;
31
+ }
32
+ return classNames[runClasses[low]];
33
+ }
34
+ /** This function gives the levels of each code point of a text. It applies `resolveLevels` with the exact classes and brackets. */
35
+ function resolveText(text, direction = "auto") {
36
+ const codePoints = Array.from(text, (character) => character.codePointAt(0));
37
+ return resolveLevels(codePoints.map(bidiClass), codePoints.map(bracketOf), direction);
38
+ }
39
+ export { bidiClass, bracketOf, removed, resolveLevels, resolveText, unicodeVersion, visualIndexes, visualOrder };
40
+
41
+ //# sourceMappingURL=bidi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bidi.js","names":[],"sources":["../src/bidi/classes.ts"],"sourcesContent":["/**\n * The class and the paired bracket of a code point. These values come from a table that the tool in `packages/conformance`\n * makes from the property files of Unicode.\n */\nimport { bracketOf } from \"./brackets.ts\";\nimport { resolveLevels, type BidiClass, type ParagraphDirection, type ResolvedBidi } from \"./resolve.ts\";\nimport { classNames, classRuns } from \"./table.ts\";\n\nconst alphabet = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+-./:;<=>?@[]^_{|}~\";\nlet starts: Uint32Array | undefined;\nlet runClasses: Uint8Array | undefined;\n\n/** This function decodes the runs one time, at the first use, so an import of the entry costs no work. */\nfunction decodeRuns(): void {\n const runs = [...classRuns.matchAll(/([0-9a-z]+)([^0-9a-z])/gu)];\n starts = new Uint32Array(runs.length);\n runClasses = new Uint8Array(runs.length);\n let start = 0;\n runs.forEach((run, index) => {\n starts![index] = start;\n runClasses![index] = alphabet.indexOf(run[2]!);\n start += Number.parseInt(run[1]!, 36);\n });\n}\n\n/** This function gives the bidi class of a code point. */\nexport function bidiClass(codePoint: number): BidiClass {\n if (starts === undefined) decodeRuns();\n let low = 0;\n let high = starts!.length;\n while (high - low > 1) {\n const middle = (low + high) >>> 1;\n if (starts![middle]! <= codePoint) low = middle;\n else high = middle;\n }\n return classNames[runClasses![low]!]! as BidiClass;\n}\n\n/** This function gives the levels of each code point of a text. It applies `resolveLevels` with the exact classes and brackets. */\nexport function resolveText(text: string, direction: ParagraphDirection = \"auto\"): ResolvedBidi {\n const codePoints = Array.from(text, (character) => character.codePointAt(0)!);\n return resolveLevels(codePoints.map(bidiClass), codePoints.map(bracketOf), direction);\n}\n"],"mappings":";;;;;;AAQA,MAAM,WAAW;AACjB,IAAI;AACJ,IAAI;;AAGJ,SAAS,aAAmB;CAC1B,MAAM,OAAO,CAAC,GAAG,UAAU,SAAS,0BAA0B,CAAC;CAC/D,SAAS,IAAI,YAAY,KAAK,MAAM;CACpC,aAAa,IAAI,WAAW,KAAK,MAAM;CACvC,IAAI,QAAQ;CACZ,KAAK,SAAS,KAAK,UAAU;EAC3B,OAAQ,SAAS;EACjB,WAAY,SAAS,SAAS,QAAQ,IAAI,EAAG;EAC7C,SAAS,OAAO,SAAS,IAAI,IAAK,EAAE;CACtC,CAAC;AACH;;AAGA,SAAgB,UAAU,WAA8B;CACtD,IAAI,WAAW,KAAA,GAAW,WAAW;CACrC,IAAI,MAAM;CACV,IAAI,OAAO,OAAQ;CACnB,OAAO,OAAO,MAAM,GAAG;EACrB,MAAM,SAAU,MAAM,SAAU;EAChC,IAAI,OAAQ,WAAY,WAAW,MAAM;OACpC,OAAO;CACd;CACA,OAAO,WAAW,WAAY;AAChC;;AAGA,SAAgB,YAAY,MAAc,YAAgC,QAAsB;CAC9F,MAAM,aAAa,MAAM,KAAK,OAAO,cAAc,UAAU,YAAY,CAAC,CAAE;CAC5E,OAAO,cAAc,WAAW,IAAI,SAAS,GAAG,WAAW,IAAI,SAAS,GAAG,SAAS;AACtF"}
@@ -1,3 +1,4 @@
1
+ import { a as bracketOf, i as visualOrder, n as resolveLevels } from "./resolve.js";
1
2
  import { fnv1a32 } from "@mark1russell7/rigor-generators";
2
3
  // src/fonts/tables.ts
3
4
  /** This function reads the header fields from `head`, `hhea` and `maxp`. */
@@ -3222,6 +3223,12 @@ function joiningStages(features, hasRclt) {
3222
3223
  return stages.filter((stage) => stage.features.length > 0);
3223
3224
  }
3224
3225
  // src/fonts/bidi.ts
3226
+ /**
3227
+ * The Unicode Bidirectional Algorithm (UAX #9) for the text of a canvas, as one paragraph with the base level 0. Blink adds
3228
+ * the widths of the words of a canvas in the visual order, so the single-precision sum needs this order. The rules are the
3229
+ * shared rules of `../bidi/resolve.ts`. This module gets the bidi class of a character from its script and its general
3230
+ * category, so that this entry has no class table. `@mark1russell7/typeset/bidi` has the exact classes.
3231
+ */
3225
3232
  const nonSpacingMark = /[\p{Mn}\p{Me}]/u;
3226
3233
  const currency = /\p{Sc}/u;
3227
3234
  const format = /\p{Cf}/u;
@@ -3238,6 +3245,19 @@ const commonSeparators = /* @__PURE__ */ new Set([
3238
3245
  8239,
3239
3246
  8260
3240
3247
  ]);
3248
+ const embeddings = [
3249
+ "LRE",
3250
+ "RLE",
3251
+ "PDF",
3252
+ "LRO",
3253
+ "RLO"
3254
+ ];
3255
+ const isolates = [
3256
+ "LRI",
3257
+ "RLI",
3258
+ "FSI",
3259
+ "PDI"
3260
+ ];
3241
3261
  /** This function gives the bidi class of a character, from its code point, its script and its general category. */
3242
3262
  function bidiClass(character) {
3243
3263
  const c = character.codePointAt(0);
@@ -3250,87 +3270,33 @@ function bidiClass(character) {
3250
3270
  if (c === 9 || c === 11 || c === 31) return "S";
3251
3271
  if (c === 10 || c === 13 || c >= 28 && c <= 30 || c === 133 || c === 8233) return "B";
3252
3272
  if (c === 32 || c === 12 || c >= 8192 && c <= 8202 || c === 8232 || c === 12288) return "WS";
3273
+ if (c >= 8234 && c <= 8238) return embeddings[c - 8234];
3274
+ if (c >= 8294 && c <= 8297) return isolates[c - 8294];
3275
+ if (c === 8206) return "L";
3276
+ if (c === 8207) return "R";
3277
+ if (c === 1564) return "AL";
3253
3278
  if (format.test(character)) return "BN";
3254
3279
  if (hebrew.test(character)) return "R";
3255
3280
  if (arabicLetter.test(character)) return "AL";
3256
3281
  return letter.test(character) ? "L" : "ON";
3257
3282
  }
3258
- /** This function gives the direction of a strong class for the rule N1: `EN` and `AN` count as `R`. */
3259
- function strongDirection(type) {
3260
- if (type === "L") return "L";
3261
- return type === "R" || type === "EN" || type === "AN" ? "R" : void 0;
3262
- }
3263
3283
  /**
3264
- * This function gives the bidi level of each code point of a text, in a paragraph with the base level 0. It applies the
3265
- * rules W1 to W7, N1, N2, I1 and L1 of UAX #9.
3284
+ * This function gives the bidi level of each code point of a text, in a paragraph with the base level 0. A character that
3285
+ * the rule X9 removes takes the level of the character before it. Before white space or a separator, it takes the base level.
3266
3286
  */
3267
3287
  function bidiLevels(text) {
3268
- const original = [...text].map(bidiClass);
3269
- const types = [...original];
3270
- const n = types.length;
3271
- for (let i = 0; i < n; i++) if (types[i] === "NSM") types[i] = i === 0 || types[i - 1] === "BN" ? "L" : types[i - 1];
3272
- let strong = "L";
3273
- for (let i = 0; i < n; i++) {
3274
- const type = types[i];
3275
- if (type === "L" || type === "R" || type === "AL") strong = type;
3276
- else if (type === "EN" && strong === "AL") types[i] = "AN";
3277
- }
3278
- for (let i = 0; i < n; i++) if (types[i] === "AL") types[i] = "R";
3279
- for (let i = 1; i + 1 < n; i++) {
3280
- const before = types[i - 1];
3281
- if (types[i] === "ES" && before === "EN" && types[i + 1] === "EN") types[i] = "EN";
3282
- else if (types[i] === "CS" && (before === "EN" || before === "AN") && types[i + 1] === before) types[i] = before;
3283
- }
3284
- for (let i = 0; i < n; i++) {
3285
- if (types[i] !== "ET") continue;
3286
- let end = i;
3287
- while (end < n && types[end] === "ET") end++;
3288
- if (i > 0 && types[i - 1] === "EN" || end < n && types[end] === "EN") types.fill("EN", i, end);
3289
- i = end - 1;
3290
- }
3291
- for (let i = 0; i < n; i++) if (types[i] === "ES" || types[i] === "ET" || types[i] === "CS") types[i] = "ON";
3292
- strong = "L";
3293
- for (let i = 0; i < n; i++) {
3294
- const type = types[i];
3295
- if (type === "L" || type === "R") strong = type;
3296
- else if (type === "EN" && strong === "L") types[i] = "L";
3297
- }
3298
- for (let i = 0; i < n; i++) {
3299
- if (strongDirection(types[i]) !== void 0) continue;
3300
- let end = i;
3301
- while (end < n && strongDirection(types[end]) === void 0) end++;
3302
- const before = i === 0 ? "L" : strongDirection(types[i - 1]);
3303
- const after = end === n ? "L" : strongDirection(types[end]);
3304
- types.fill(before === after ? before : "L", i, end);
3305
- i = end - 1;
3306
- }
3307
- const levels = types.map((type) => type === "R" ? 1 : type === "AN" || type === "EN" ? 2 : 0);
3308
- const isSpace = (i) => original[i] === "WS" || original[i] === "BN";
3309
- for (let i = n - 1; i >= 0 && isSpace(i); i--) levels[i] = 0;
3310
- for (let i = 0; i < n; i++) {
3311
- if (original[i] !== "S" && original[i] !== "B") continue;
3312
- levels[i] = 0;
3313
- for (let k = i - 1; k >= 0 && isSpace(k); k--) levels[k] = 0;
3314
- }
3288
+ const characters = [...text];
3289
+ const classes = characters.map(bidiClass);
3290
+ const { levels } = resolveLevels(classes, characters.map((character) => bracketOf(character.codePointAt(0))), 0);
3291
+ const n = levels.length;
3292
+ let atEnd = true;
3293
+ for (let i = n - 1; i >= 0; i--) if (levels[i] !== -1) atEnd = classes[i] === "WS" || classes[i] === "S" || classes[i] === "B";
3294
+ else if (atEnd) levels[i] = 0;
3295
+ let level = 0;
3296
+ for (let i = 0; i < n; i++) if (levels[i] === -1) levels[i] = level;
3297
+ else level = levels[i];
3315
3298
  return levels;
3316
3299
  }
3317
- /**
3318
- * This function puts items in the visual order of the rule L2 of UAX #9. Each item has the level `levels[i]`. From the
3319
- * highest level to the level 1, it reverses each sequence of items at that level or higher.
3320
- */
3321
- function visualOrder(items, levels) {
3322
- const order = items.map((_, i) => i);
3323
- const at = [...levels];
3324
- for (let level = Math.max(0, ...levels); level >= 1; level--) for (let i = 0; i < order.length; i++) {
3325
- if (at[i] < level) continue;
3326
- let end = i;
3327
- while (end < order.length && at[end] >= level) end++;
3328
- order.splice(i, end - i, ...order.slice(i, end).reverse());
3329
- at.splice(i, end - i, ...at.slice(i, end).reverse());
3330
- i = end;
3331
- }
3332
- return order.map((i) => items[i]);
3333
- }
3334
3300
  // src/fonts/words.ts
3335
3301
  /**
3336
3302
  * The words of the text of a canvas. Blink shapes the text of `fillText` and `measureText` one word at a time, so no GSUB