@mark1russell7/typeset 0.1.2 → 0.1.4
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/font.d.ts +25 -0
- package/build/chunks/font.d.ts.map +1 -1
- package/build/chunks/font.js +397 -143
- package/build/chunks/font.js.map +1 -1
- package/build/chunks/fonts.d.ts +25 -2
- package/build/chunks/fonts.d.ts.map +1 -1
- package/build/chunks/fonts.js +141 -3
- package/build/chunks/fonts.js.map +1 -1
- package/build/fonts-inspect.d.ts +5 -0
- package/build/fonts-inspect.d.ts.map +1 -1
- package/build/fonts-inspect.js +1 -1
- package/build/fonts.d.ts +2 -2
- package/build/fonts.js +2 -2
- package/package.json +2 -2
- package/src/fonts/bound.ts +170 -0
- package/src/fonts/font.ts +30 -6
- package/src/fonts/index.ts +1 -0
- package/src/fonts/indic.ts +19 -75
- package/src/fonts/kerning.ts +68 -7
- package/src/fonts/normalize.ts +144 -0
- package/src/fonts/substitution.ts +101 -1
- package/src/fonts/tables.ts +22 -0
- package/src/fonts-inspect.ts +2 -2
- package/src/fonts.ts +1 -0
package/build/chunks/fonts.d.ts
CHANGED
|
@@ -1,8 +1,31 @@
|
|
|
1
1
|
import { a as measureWidth, c as Axis, l as Instance, n as MeasureOptions, o as readFont, r as ReadOptions, t as Font } from "./font.js";
|
|
2
2
|
import { i as isEngineName, n as EngineRules, r as engines, t as EngineName } from "./engines.js";
|
|
3
3
|
import { t as Decompress } from "./sfnt.js";
|
|
4
|
+
/** The limit of `widthBound` for the width of a text over a declared set of characters. It needs no measurement. */
|
|
5
|
+
interface WidthBound {
|
|
6
|
+
/** The largest total advance of one character with all its expansions, in font units. */
|
|
7
|
+
readonly advance: number;
|
|
8
|
+
/** The largest positive kerning of one glyph, in font units. */
|
|
9
|
+
readonly kerning: number;
|
|
10
|
+
/** The largest count of glyphs of one character. */
|
|
11
|
+
readonly glyphs: number;
|
|
12
|
+
/**
|
|
13
|
+
* This function gives the limit in px for a text of `length` UTF-16 units at the size `size`. It takes the length of a
|
|
14
|
+
* string, so it does not read the text. The limit is for a text with only the characters of the set.
|
|
15
|
+
*/
|
|
16
|
+
width(length: number, size: number): number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* This function gives a limit for the width of each text over the characters `characters`, with no measurement. For each such
|
|
20
|
+
* text, `measureWidth(font, text, size, options)` is at most `width(text.length, size)` of the result. Give the same `options`.
|
|
21
|
+
* A text with another character has no limit from this function.
|
|
22
|
+
*
|
|
23
|
+
* The limit is sound, not tight. It follows the widest character of the set. It includes each glyph of the GSUB closure, each
|
|
24
|
+
* dotted circle, each part or composite of the normalization, and the largest positive kerning.
|
|
25
|
+
*/
|
|
26
|
+
declare function widthBound(font: Font, characters: string, options?: MeasureOptions): WidthBound;
|
|
4
27
|
declare namespace fonts_d_exports {
|
|
5
|
-
export { Axis, Decompress, EngineName, EngineRules, Font, Instance, MeasureOptions, ReadOptions, engines, isEngineName, measureWidth, readFont };
|
|
28
|
+
export { Axis, Decompress, EngineName, EngineRules, Font, Instance, MeasureOptions, ReadOptions, WidthBound, engines, isEngineName, measureWidth, readFont, widthBound };
|
|
6
29
|
}
|
|
7
|
-
export { fonts_d_exports as t };
|
|
30
|
+
export { WidthBound as n, widthBound as r, fonts_d_exports as t };
|
|
8
31
|
//# sourceMappingURL=fonts.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fonts.d.ts","names":[],"sources":["../../src/fonts.ts"],"mappings":""}
|
|
1
|
+
{"version":3,"file":"fonts.d.ts","names":[],"sources":["../../src/fonts/bound.ts","../../src/fonts.ts"],"mappings":";;;;UAOiB;;WAEN;;WAEA;;WAEA;;;;;EAKT,MAAM,gBAAgB;;;;;;;;;;iBA6FR,WAAW,MAAM,MAAM,oBAAoB,UAAS,iBAAsB"}
|
package/build/chunks/fonts.js
CHANGED
|
@@ -1,13 +1,151 @@
|
|
|
1
1
|
import { t as __exportAll } from "./rolldown-runtime.js";
|
|
2
|
-
import { n as measureWidth, r as readFont } from "./font.js";
|
|
2
|
+
import { c as joiningStages, d as normalForms, i as shapingOf, l as indicFeatures, n as measureWidth, r as readFont, u as indicForms } from "./font.js";
|
|
3
3
|
import { n as isEngineName, t as engines } from "./engines.js";
|
|
4
|
+
// src/fonts/bound.ts
|
|
5
|
+
/**
|
|
6
|
+
* The largest count of passes over the lookups of a shaper with stages. A pass covers one stage, and no shaper has more stages:
|
|
7
|
+
* the Indic shaper has 13 and the joining has 11. A rule that expands its own glyph has no fixed point, so it stops at this count.
|
|
8
|
+
*/
|
|
9
|
+
const ROUNDS = 16;
|
|
10
|
+
/** The relative error of one rounding to single precision. */
|
|
11
|
+
const FLOAT32 = 2 ** -24;
|
|
12
|
+
/** The relative error of one rounding to double precision. */
|
|
13
|
+
const FLOAT64 = 2 ** -53;
|
|
14
|
+
/** The largest size of a kerning value of a `ValueRecord`, and of its variation delta, in font units: the range of `int16`. */
|
|
15
|
+
const KERNING_RANGE = 2 ** 15;
|
|
16
|
+
function larger(a, b) {
|
|
17
|
+
return {
|
|
18
|
+
advance: Math.max(a.advance, b.advance),
|
|
19
|
+
glyphs: Math.max(a.glyphs, b.glyphs)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function sum(a, b) {
|
|
23
|
+
return {
|
|
24
|
+
advance: a.advance + b.advance,
|
|
25
|
+
glyphs: a.glyphs + b.glyphs
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** This function tells if a rule can apply to the glyphs `reached`. A ligature needs its first glyph and all its components. */
|
|
29
|
+
function applies(rule, reached) {
|
|
30
|
+
return reached.has(rule.from) && (rule.components ?? []).every((glyph) => reached.has(glyph));
|
|
31
|
+
}
|
|
32
|
+
/** This function adds the glyphs that the rules can give to `reached`. */
|
|
33
|
+
function reach(reached, rules) {
|
|
34
|
+
for (let size = -1; size !== reached.size;) {
|
|
35
|
+
size = reached.size;
|
|
36
|
+
for (const rule of rules) if (applies(rule, reached)) for (const glyph of rule.to) reached.add(glyph);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* This function applies one list of rules to the expansions. Each rule uses the values from before the list. A lookup applies
|
|
41
|
+
* once at a position, so its output does not go back into it. The function tells if a value changed.
|
|
42
|
+
*/
|
|
43
|
+
function relax(rules, expansions) {
|
|
44
|
+
const changes = [];
|
|
45
|
+
for (const { from, to } of rules) {
|
|
46
|
+
const before = expansions.get(from);
|
|
47
|
+
const after = to.reduce((total, glyph) => sum(total, expansions.get(glyph)), {
|
|
48
|
+
advance: 0,
|
|
49
|
+
glyphs: 0
|
|
50
|
+
});
|
|
51
|
+
if (after.advance > before.advance || after.glyphs > before.glyphs) changes.push([from, after]);
|
|
52
|
+
}
|
|
53
|
+
for (const [from, after] of changes) expansions.set(from, larger(expansions.get(from), after));
|
|
54
|
+
return changes.length > 0;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* This function gives the largest expansion of each glyph of `reached`. Each ligature charges its first glyph, so each output
|
|
58
|
+
* glyph comes from one input glyph. Each pass goes backward over the lookups: a glyph then gets the expansions of the lookups
|
|
59
|
+
* after the lookup that makes it. A shaper with stages goes over the list again for each stage, so the passes repeat.
|
|
60
|
+
*
|
|
61
|
+
* One context rule can apply its nested lookups in any order. So the lists of a lookup with nested lookups repeat until no
|
|
62
|
+
* value changes, with the same limit. A value that stops changing holds for each order.
|
|
63
|
+
*/
|
|
64
|
+
function expansionsOf(font, reached, lookups, rounds) {
|
|
65
|
+
const expansions = /* @__PURE__ */ new Map();
|
|
66
|
+
for (const glyph of reached) expansions.set(glyph, {
|
|
67
|
+
advance: Math.max(0, font.advance(glyph)),
|
|
68
|
+
glyphs: 1
|
|
69
|
+
});
|
|
70
|
+
const active = lookups.map((lists) => lists.map((rules) => rules.filter((rule) => applies(rule, reached))));
|
|
71
|
+
for (let round = 0; round < rounds; round++) {
|
|
72
|
+
let changed = false;
|
|
73
|
+
for (let i = active.length - 1; i >= 0; i--) {
|
|
74
|
+
const lists = active[i];
|
|
75
|
+
for (let step = 0; step < (lists.length > 1 ? ROUNDS : 1); step++) {
|
|
76
|
+
let moved = false;
|
|
77
|
+
for (const rules of lists) moved = relax(rules, expansions) || moved;
|
|
78
|
+
if (!moved) break;
|
|
79
|
+
changed = true;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (!changed) break;
|
|
83
|
+
}
|
|
84
|
+
return expansions;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* This function gives a limit for the width of each text over the characters `characters`, with no measurement. For each such
|
|
88
|
+
* text, `measureWidth(font, text, size, options)` is at most `width(text.length, size)` of the result. Give the same `options`.
|
|
89
|
+
* A text with another character has no limit from this function.
|
|
90
|
+
*
|
|
91
|
+
* The limit is sound, not tight. It follows the widest character of the set. It includes each glyph of the GSUB closure, each
|
|
92
|
+
* dotted circle, each part or composite of the normalization, and the largest positive kerning.
|
|
93
|
+
*/
|
|
94
|
+
function widthBound(font, characters, options = {}) {
|
|
95
|
+
const { script, features, shaper, kerning: pairLookups } = shapingOf(font, options);
|
|
96
|
+
const set = new Set(Array.from(characters, (character) => character.codePointAt(0)));
|
|
97
|
+
const hasGlyph = (codePoint) => font.glyph(codePoint) !== 0;
|
|
98
|
+
const forms = shaper === "indic" ? indicForms(set, hasGlyph) : normalForms(set, hasGlyph);
|
|
99
|
+
const reached = new Set([...forms.codePoints].map((codePoint) => font.glyph(codePoint)));
|
|
100
|
+
const tags = shaper === "indic" ? indicFeatures(features) : shaper === "joining" ? joiningStages(features, true).flatMap((stage) => stage.features) : features;
|
|
101
|
+
const lookups = font.substitutions(script, tags).map((lookup) => lookup.rules());
|
|
102
|
+
reach(reached, lookups.flat(2));
|
|
103
|
+
const expansions = expansionsOf(font, reached, lookups, shaper === "plain" ? 1 : ROUNDS);
|
|
104
|
+
const form = (codePoint) => (forms.composites.get(codePoint) ?? []).reduce((best, composite) => larger(best, form(composite)), expansions.get(font.glyph(codePoint)));
|
|
105
|
+
const inserted = shaper === "indic" ? expansions.get(font.glyph(9676)) : {
|
|
106
|
+
advance: 0,
|
|
107
|
+
glyphs: 0
|
|
108
|
+
};
|
|
109
|
+
let largest = {
|
|
110
|
+
advance: 0,
|
|
111
|
+
glyphs: 0
|
|
112
|
+
};
|
|
113
|
+
for (const codePoint of set) {
|
|
114
|
+
const decomposed = forms.parts.get(codePoint).reduce((total, part) => sum(total, sum(form(part), inserted)), {
|
|
115
|
+
advance: 0,
|
|
116
|
+
glyphs: 0
|
|
117
|
+
});
|
|
118
|
+
largest = larger(largest, larger(decomposed, sum(form(codePoint), inserted)));
|
|
119
|
+
}
|
|
120
|
+
let kerning = 0;
|
|
121
|
+
for (const lookup of pairLookups) {
|
|
122
|
+
const { first, second } = lookup.largest(reached);
|
|
123
|
+
kerning += first + second;
|
|
124
|
+
}
|
|
125
|
+
const { advance, glyphs } = largest;
|
|
126
|
+
const pixel = options.rounding === "pixel";
|
|
127
|
+
const float32 = options.sum === "float32" && !pixel;
|
|
128
|
+
const pixelMargin = pixel ? .5078125 + (kerning > 0 ? .5 : 0) : 0;
|
|
129
|
+
const positions = pairLookups.length * 2;
|
|
130
|
+
const positionMargin = float32 ? positions * (1.5 + 2 * KERNING_RANGE / font.unitsPerEm) / 65536 : 0;
|
|
131
|
+
const kerningMargin = float32 ? positions * 5 * FLOAT32 * KERNING_RANGE : 0;
|
|
132
|
+
return {
|
|
133
|
+
advance,
|
|
134
|
+
kerning,
|
|
135
|
+
glyphs,
|
|
136
|
+
width(length, size) {
|
|
137
|
+
return length * ((advance + glyphs * (kerning + kerningMargin)) * (size / font.unitsPerEm) + glyphs * (pixelMargin + positionMargin)) * (float32 ? (1 + FLOAT32) ** (length + 6) : 1) * (1 + (8192 + length) * FLOAT64);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
}
|
|
4
141
|
// src/fonts.ts
|
|
5
142
|
var fonts_exports = /* @__PURE__ */ __exportAll({
|
|
6
143
|
engines: () => engines,
|
|
7
144
|
isEngineName: () => isEngineName,
|
|
8
145
|
measureWidth: () => measureWidth,
|
|
9
|
-
readFont: () => readFont
|
|
146
|
+
readFont: () => readFont,
|
|
147
|
+
widthBound: () => widthBound
|
|
10
148
|
});
|
|
11
|
-
export { fonts_exports as t };
|
|
149
|
+
export { widthBound as n, fonts_exports as t };
|
|
12
150
|
|
|
13
151
|
//# sourceMappingURL=fonts.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fonts.js","names":[],"sources":["../../src/fonts.ts"],"sourcesContent":["/**\n * The subpath `./fonts`: the font model of typeset. It reads the tables of a TrueType, OpenType or WOFF2 file. It gives\n * the width of a text as the text engine of a browser gives it. The host gives the bytes of the font files.\n */\nexport { measureWidth, readFont, type Font, type MeasureOptions, type ReadOptions } from \"./fonts/font.ts\";\nexport { engines, isEngineName, type EngineName, type EngineRules } from \"./fonts/engines.ts\";\nexport type { Decompress } from \"./fonts/sfnt.ts\";\nexport type { Axis, Instance } from \"./fonts/variations.ts\";\n"],"mappings":""}
|
|
1
|
+
{"version":3,"file":"fonts.js","names":[],"sources":["../../src/fonts/bound.ts","../../src/fonts.ts"],"sourcesContent":["import { shapingOf, type Font, type MeasureOptions } from \"./font.ts\";\nimport { indicFeatures, indicForms } from \"./indic.ts\";\nimport { joiningStages } from \"./joining.ts\";\nimport { normalForms } from \"./normalize.ts\";\nimport type { SubstitutionRule } from \"./substitution.ts\";\n\n/** The limit of `widthBound` for the width of a text over a declared set of characters. It needs no measurement. */\nexport interface WidthBound {\n /** The largest total advance of one character with all its expansions, in font units. */\n readonly advance: number;\n /** The largest positive kerning of one glyph, in font units. */\n readonly kerning: number;\n /** The largest count of glyphs of one character. */\n readonly glyphs: number;\n /**\n * This function gives the limit in px for a text of `length` UTF-16 units at the size `size`. It takes the length of a\n * string, so it does not read the text. The limit is for a text with only the characters of the set.\n */\n width(length: number, size: number): number;\n}\n\n/**\n * The largest count of passes over the lookups of a shaper with stages. A pass covers one stage, and no shaper has more stages:\n * the Indic shaper has 13 and the joining has 11. A rule that expands its own glyph has no fixed point, so it stops at this count.\n */\nconst ROUNDS = 16;\n/** The relative error of one rounding to single precision. */\nconst FLOAT32 = 2 ** -24;\n/** The relative error of one rounding to double precision. */\nconst FLOAT64 = 2 ** -53;\n/** The largest size of a kerning value of a `ValueRecord`, and of its variation delta, in font units: the range of `int16`. */\nconst KERNING_RANGE = 2 ** 15;\n\n/** The largest expansion of a glyph: the sum of its advances, and the count of its glyphs. */\ninterface Expansion {\n readonly advance: number;\n readonly glyphs: number;\n}\n\nfunction larger(a: Expansion, b: Expansion): Expansion {\n return { advance: Math.max(a.advance, b.advance), glyphs: Math.max(a.glyphs, b.glyphs) };\n}\n\nfunction sum(a: Expansion, b: Expansion): Expansion {\n return { advance: a.advance + b.advance, glyphs: a.glyphs + b.glyphs };\n}\n\n/** This function tells if a rule can apply to the glyphs `reached`. A ligature needs its first glyph and all its components. */\nfunction applies(rule: SubstitutionRule, reached: ReadonlySet<number>): boolean {\n return reached.has(rule.from) && (rule.components ?? []).every((glyph) => reached.has(glyph));\n}\n\n/** This function adds the glyphs that the rules can give to `reached`. */\nfunction reach(reached: Set<number>, rules: readonly SubstitutionRule[]): void {\n for (let size = -1; size !== reached.size; ) {\n size = reached.size;\n for (const rule of rules) if (applies(rule, reached)) for (const glyph of rule.to) reached.add(glyph);\n }\n}\n\n/**\n * This function applies one list of rules to the expansions. Each rule uses the values from before the list. A lookup applies\n * once at a position, so its output does not go back into it. The function tells if a value changed.\n */\nfunction relax(rules: readonly SubstitutionRule[], expansions: Map<number, Expansion>): boolean {\n const changes: (readonly [number, Expansion])[] = [];\n for (const { from, to } of rules) {\n const before = expansions.get(from)!;\n const after = to.reduce((total, glyph) => sum(total, expansions.get(glyph)!), { advance: 0, glyphs: 0 });\n if (after.advance > before.advance || after.glyphs > before.glyphs) changes.push([from, after]);\n }\n for (const [from, after] of changes) expansions.set(from, larger(expansions.get(from)!, after));\n return changes.length > 0;\n}\n\n/**\n * This function gives the largest expansion of each glyph of `reached`. Each ligature charges its first glyph, so each output\n * glyph comes from one input glyph. Each pass goes backward over the lookups: a glyph then gets the expansions of the lookups\n * after the lookup that makes it. A shaper with stages goes over the list again for each stage, so the passes repeat.\n *\n * One context rule can apply its nested lookups in any order. So the lists of a lookup with nested lookups repeat until no\n * value changes, with the same limit. A value that stops changing holds for each order.\n */\nfunction expansionsOf(font: Font, reached: ReadonlySet<number>, lookups: readonly (readonly (readonly SubstitutionRule[])[])[], rounds: number): Map<number, Expansion> {\n const expansions = new Map<number, Expansion>();\n for (const glyph of reached) expansions.set(glyph, { advance: Math.max(0, font.advance(glyph)), glyphs: 1 });\n const active = lookups.map((lists) => lists.map((rules) => rules.filter((rule) => applies(rule, reached))));\n for (let round = 0; round < rounds; round++) {\n let changed = false;\n for (let i = active.length - 1; i >= 0; i--) {\n const lists = active[i]!;\n for (let step = 0; step < (lists.length > 1 ? ROUNDS : 1); step++) {\n let moved = false;\n for (const rules of lists) moved = relax(rules, expansions) || moved;\n if (!moved) break;\n changed = true;\n }\n }\n if (!changed) break;\n }\n return expansions;\n}\n\n/**\n * This function gives a limit for the width of each text over the characters `characters`, with no measurement. For each such\n * text, `measureWidth(font, text, size, options)` is at most `width(text.length, size)` of the result. Give the same `options`.\n * A text with another character has no limit from this function.\n *\n * The limit is sound, not tight. It follows the widest character of the set. It includes each glyph of the GSUB closure, each\n * dotted circle, each part or composite of the normalization, and the largest positive kerning.\n */\nexport function widthBound(font: Font, characters: string, options: MeasureOptions = {}): WidthBound {\n const { script, features, shaper, kerning: pairLookups } = shapingOf(font, options);\n const set = new Set(Array.from(characters, (character) => character.codePointAt(0)!));\n const hasGlyph = (codePoint: number): boolean => font.glyph(codePoint) !== 0;\n const forms = shaper === \"indic\" ? indicForms(set, hasGlyph) : normalForms(set, hasGlyph);\n\n // The roots: the glyph of each code point that the shaper can see. A character with no glyph gives the glyph 0\n const reached = new Set([...forms.codePoints].map((codePoint) => font.glyph(codePoint)));\n // A union of the features gives the same lookups as the union of the stages. Each stage then counts, in the rounds\n const tags = shaper === \"indic\" ? indicFeatures(features) : shaper === \"joining\" ? joiningStages(features, true).flatMap((stage) => stage.features) : features;\n const lookups = font.substitutions(script, tags).map((lookup) => lookup.rules());\n reach(reached, lookups.flat(2));\n // The plain shaper is one stage: it applies each lookup once, in the order of the list\n const expansions = expansionsOf(font, reached, lookups, shaper === \"plain\" ? 1 : ROUNDS);\n\n const form = (codePoint: number): Expansion => (forms.composites.get(codePoint) ?? []).reduce((best, composite) => larger(best, form(composite)), expansions.get(font.glyph(codePoint))!);\n // The Indic shaper can insert a dotted circle before each code point. Without a glyph, the code point U+25CC gives the glyph 0.\n // One code point gets at most one: a circle of the vowel constraints takes the next matra, so that syllable is not broken\n const inserted = shaper === \"indic\" ? expansions.get(font.glyph(0x25cc))! : { advance: 0, glyphs: 0 };\n let largest: Expansion = { advance: 0, glyphs: 0 };\n for (const codePoint of set) {\n // A character with a mark decomposes, and a character with no mark near it keeps its form. So the larger of the two counts\n const decomposed = forms.parts.get(codePoint)!.reduce((total, part) => sum(total, sum(form(part), inserted)), { advance: 0, glyphs: 0 });\n largest = larger(largest, larger(decomposed, sum(form(codePoint), inserted)));\n }\n\n // One glyph gets at most one first value and one second value from each lookup, and each subtable of a lookup is an alternative\n let kerning = 0;\n for (const lookup of pairLookups) {\n const { first, second } = lookup.largest(reached);\n kerning += first + second;\n }\n\n const { advance, glyphs } = largest;\n const pixel = options.rounding === \"pixel\";\n const float32 = options.sum === \"float32\" && !pixel;\n // Pixel: an advance gets at most 1/128 px from the grid of 26.6, and then 0.5 px. A kerning sum gets at most 0.5 px\n const pixelMargin = pixel ? 0.5 + 1 / 128 + (kerning > 0 ? 0.5 : 0) : 0;\n // Float32: each lookup gives a glyph two positions, in units of 1/65536 px. A negative value gets no help from the relative\n // factor below, so it has its own margin. The floor of a value and the round of a delta give 1 unit, the floor of the\n // multiplier gives 0.5 units, and the cut of the scale gives KERNING_RANGE / unitsPerEm units to each of the two parts.\n // The single precision gives FLOAT32 to the value and 4 FLOAT32 to the delta\n const positions = pairLookups.length * 2;\n const positionMargin = float32 ? (positions * (1.5 + (2 * KERNING_RANGE) / font.unitsPerEm)) / 65536 : 0;\n const kerningMargin = float32 ? positions * 5 * FLOAT32 * KERNING_RANGE : 0;\n return {\n advance,\n kerning,\n glyphs,\n width(length: number, size: number): number {\n const total = length * ((advance + glyphs * (kerning + kerningMargin)) * (size / font.unitsPerEm) + glyphs * (pixelMargin + positionMargin));\n // Float32: the fround of a glyph or 4 steps of a positive delta, the fround of a segment, and one step of the sum for\n // each segment. A text has at most one segment for each UTF-16 unit\n const single = float32 ? (1 + FLOAT32) ** (length + 6) : 1;\n // The double precision: the scale and a few products, and one rounding for each step of a sum of fractions\n return total * single * (1 + (2 ** 13 + length) * FLOAT64);\n },\n };\n}\n","/**\n * The subpath `./fonts`: the font model of typeset. It reads the tables of a TrueType, OpenType or WOFF2 file. It gives\n * the width of a text as the text engine of a browser gives it. The host gives the bytes of the font files.\n */\nexport { measureWidth, readFont, type Font, type MeasureOptions, type ReadOptions } from \"./fonts/font.ts\";\nexport { widthBound, type WidthBound } from \"./fonts/bound.ts\";\nexport { engines, isEngineName, type EngineName, type EngineRules } from \"./fonts/engines.ts\";\nexport type { Decompress } from \"./fonts/sfnt.ts\";\nexport type { Axis, Instance } from \"./fonts/variations.ts\";\n"],"mappings":";;;;;;;;AAyBA,MAAM,SAAS;;AAEf,MAAM,UAAU,KAAK;;AAErB,MAAM,UAAU,KAAK;;AAErB,MAAM,gBAAgB,KAAK;AAQ3B,SAAS,OAAO,GAAc,GAAyB;CACrD,OAAO;EAAE,SAAS,KAAK,IAAI,EAAE,SAAS,EAAE,OAAO;EAAG,QAAQ,KAAK,IAAI,EAAE,QAAQ,EAAE,MAAM;CAAE;AACzF;AAEA,SAAS,IAAI,GAAc,GAAyB;CAClD,OAAO;EAAE,SAAS,EAAE,UAAU,EAAE;EAAS,QAAQ,EAAE,SAAS,EAAE;CAAO;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ANGED
|
@@ -6,5 +6,10 @@ import { n as readTables } from "./chunks/sfnt.js";
|
|
|
6
6
|
* Common after it.
|
|
7
7
|
*/
|
|
8
8
|
export declare function canvasWords(text: string): string[];
|
|
9
|
+
/**
|
|
10
|
+
* This function gives the segments of the text of a canvas in the visual order. A segment is a word of `canvasWords`, or a
|
|
11
|
+
* part of a word where the bidi level changes. Blink shapes each bidi run alone and adds the widths of the words in this order.
|
|
12
|
+
*/
|
|
13
|
+
export declare function canvasSegments(text: string): string[];
|
|
9
14
|
export { defaultFeatures, glyphAdvances, readTables, shapingWords };
|
|
10
15
|
//# sourceMappingURL=fonts-inspect.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fonts-inspect.d.ts","names":[],"sources":["../src/fonts/words.ts"],"mappings":";;;;;;;wBAwDgB,YAAY"}
|
|
1
|
+
{"version":3,"file":"fonts-inspect.d.ts","names":[],"sources":["../src/fonts/words.ts"],"mappings":";;;;;;;wBAwDgB,YAAY;;;;;wBAgBZ,eAAe"}
|
package/build/fonts-inspect.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { o as canvasSegments, s as canvasWords, f as defaultFeatures, t as glyphAdvances, p as readTables, a as shapingWords } from "./chunks/font.js";
|
package/build/fonts.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as Decompress } from "./chunks/sfnt.js";
|
|
2
2
|
import { a as measureWidth, c as Axis, l as Instance, n as MeasureOptions, o as readFont, r as ReadOptions, t as Font } from "./chunks/font.js";
|
|
3
|
+
import { n as WidthBound, r as widthBound } from "./chunks/fonts.js";
|
|
3
4
|
import { i as isEngineName, n as EngineRules, r as engines, t as EngineName } from "./chunks/engines.js";
|
|
4
|
-
|
|
5
|
-
export { type Axis, type Decompress, type EngineName, type EngineRules, type Font, type Instance, type MeasureOptions, type ReadOptions, engines, isEngineName, measureWidth, readFont };
|
|
5
|
+
export { type Axis, type Decompress, type EngineName, type EngineRules, type Font, type Instance, type MeasureOptions, type ReadOptions, type WidthBound, engines, isEngineName, measureWidth, readFont, widthBound };
|
package/build/fonts.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { n as measureWidth, r as readFont } from "./chunks/font.js";
|
|
2
|
+
import { n as widthBound } from "./chunks/fonts.js";
|
|
2
3
|
import { n as isEngineName, t as engines } from "./chunks/engines.js";
|
|
3
|
-
|
|
4
|
-
export { engines, isEngineName, measureWidth, readFont };
|
|
4
|
+
export { engines, isEngineName, measureWidth, readFont, widthBound };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/typeset",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.4",
|
|
5
5
|
"description": "Text measurement and line breaking. The subpath ./fonts reads font tables and gives widths, ./measurer gives the advance function of a layout, ./breaking has the line breakers, and ./text has a rope.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"typeset",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"@mark1russell7/rigor-generators": "^0.1.0"
|
|
69
69
|
},
|
|
70
70
|
"devDependencies": {
|
|
71
|
-
"@mark1russell7/cue": "^
|
|
71
|
+
"@mark1russell7/cue": "^2.0.0",
|
|
72
72
|
"@mark1russell7/layout-contract": "^0.3.2",
|
|
73
73
|
"@types/node": "^26.6.5",
|
|
74
74
|
"@typeset/corpus": "0.1.0",
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { shapingOf, type Font, type MeasureOptions } from "./font.ts";
|
|
2
|
+
import { indicFeatures, indicForms } from "./indic.ts";
|
|
3
|
+
import { joiningStages } from "./joining.ts";
|
|
4
|
+
import { normalForms } from "./normalize.ts";
|
|
5
|
+
import type { SubstitutionRule } from "./substitution.ts";
|
|
6
|
+
|
|
7
|
+
/** The limit of `widthBound` for the width of a text over a declared set of characters. It needs no measurement. */
|
|
8
|
+
export interface WidthBound {
|
|
9
|
+
/** The largest total advance of one character with all its expansions, in font units. */
|
|
10
|
+
readonly advance: number;
|
|
11
|
+
/** The largest positive kerning of one glyph, in font units. */
|
|
12
|
+
readonly kerning: number;
|
|
13
|
+
/** The largest count of glyphs of one character. */
|
|
14
|
+
readonly glyphs: number;
|
|
15
|
+
/**
|
|
16
|
+
* This function gives the limit in px for a text of `length` UTF-16 units at the size `size`. It takes the length of a
|
|
17
|
+
* string, so it does not read the text. The limit is for a text with only the characters of the set.
|
|
18
|
+
*/
|
|
19
|
+
width(length: number, size: number): number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The largest count of passes over the lookups of a shaper with stages. A pass covers one stage, and no shaper has more stages:
|
|
24
|
+
* the Indic shaper has 13 and the joining has 11. A rule that expands its own glyph has no fixed point, so it stops at this count.
|
|
25
|
+
*/
|
|
26
|
+
const ROUNDS = 16;
|
|
27
|
+
/** The relative error of one rounding to single precision. */
|
|
28
|
+
const FLOAT32 = 2 ** -24;
|
|
29
|
+
/** The relative error of one rounding to double precision. */
|
|
30
|
+
const FLOAT64 = 2 ** -53;
|
|
31
|
+
/** The largest size of a kerning value of a `ValueRecord`, and of its variation delta, in font units: the range of `int16`. */
|
|
32
|
+
const KERNING_RANGE = 2 ** 15;
|
|
33
|
+
|
|
34
|
+
/** The largest expansion of a glyph: the sum of its advances, and the count of its glyphs. */
|
|
35
|
+
interface Expansion {
|
|
36
|
+
readonly advance: number;
|
|
37
|
+
readonly glyphs: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function larger(a: Expansion, b: Expansion): Expansion {
|
|
41
|
+
return { advance: Math.max(a.advance, b.advance), glyphs: Math.max(a.glyphs, b.glyphs) };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function sum(a: Expansion, b: Expansion): Expansion {
|
|
45
|
+
return { advance: a.advance + b.advance, glyphs: a.glyphs + b.glyphs };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** This function tells if a rule can apply to the glyphs `reached`. A ligature needs its first glyph and all its components. */
|
|
49
|
+
function applies(rule: SubstitutionRule, reached: ReadonlySet<number>): boolean {
|
|
50
|
+
return reached.has(rule.from) && (rule.components ?? []).every((glyph) => reached.has(glyph));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** This function adds the glyphs that the rules can give to `reached`. */
|
|
54
|
+
function reach(reached: Set<number>, rules: readonly SubstitutionRule[]): void {
|
|
55
|
+
for (let size = -1; size !== reached.size; ) {
|
|
56
|
+
size = reached.size;
|
|
57
|
+
for (const rule of rules) if (applies(rule, reached)) for (const glyph of rule.to) reached.add(glyph);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* This function applies one list of rules to the expansions. Each rule uses the values from before the list. A lookup applies
|
|
63
|
+
* once at a position, so its output does not go back into it. The function tells if a value changed.
|
|
64
|
+
*/
|
|
65
|
+
function relax(rules: readonly SubstitutionRule[], expansions: Map<number, Expansion>): boolean {
|
|
66
|
+
const changes: (readonly [number, Expansion])[] = [];
|
|
67
|
+
for (const { from, to } of rules) {
|
|
68
|
+
const before = expansions.get(from)!;
|
|
69
|
+
const after = to.reduce((total, glyph) => sum(total, expansions.get(glyph)!), { advance: 0, glyphs: 0 });
|
|
70
|
+
if (after.advance > before.advance || after.glyphs > before.glyphs) changes.push([from, after]);
|
|
71
|
+
}
|
|
72
|
+
for (const [from, after] of changes) expansions.set(from, larger(expansions.get(from)!, after));
|
|
73
|
+
return changes.length > 0;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* This function gives the largest expansion of each glyph of `reached`. Each ligature charges its first glyph, so each output
|
|
78
|
+
* glyph comes from one input glyph. Each pass goes backward over the lookups: a glyph then gets the expansions of the lookups
|
|
79
|
+
* after the lookup that makes it. A shaper with stages goes over the list again for each stage, so the passes repeat.
|
|
80
|
+
*
|
|
81
|
+
* One context rule can apply its nested lookups in any order. So the lists of a lookup with nested lookups repeat until no
|
|
82
|
+
* value changes, with the same limit. A value that stops changing holds for each order.
|
|
83
|
+
*/
|
|
84
|
+
function expansionsOf(font: Font, reached: ReadonlySet<number>, lookups: readonly (readonly (readonly SubstitutionRule[])[])[], rounds: number): Map<number, Expansion> {
|
|
85
|
+
const expansions = new Map<number, Expansion>();
|
|
86
|
+
for (const glyph of reached) expansions.set(glyph, { advance: Math.max(0, font.advance(glyph)), glyphs: 1 });
|
|
87
|
+
const active = lookups.map((lists) => lists.map((rules) => rules.filter((rule) => applies(rule, reached))));
|
|
88
|
+
for (let round = 0; round < rounds; round++) {
|
|
89
|
+
let changed = false;
|
|
90
|
+
for (let i = active.length - 1; i >= 0; i--) {
|
|
91
|
+
const lists = active[i]!;
|
|
92
|
+
for (let step = 0; step < (lists.length > 1 ? ROUNDS : 1); step++) {
|
|
93
|
+
let moved = false;
|
|
94
|
+
for (const rules of lists) moved = relax(rules, expansions) || moved;
|
|
95
|
+
if (!moved) break;
|
|
96
|
+
changed = true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (!changed) break;
|
|
100
|
+
}
|
|
101
|
+
return expansions;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* This function gives a limit for the width of each text over the characters `characters`, with no measurement. For each such
|
|
106
|
+
* text, `measureWidth(font, text, size, options)` is at most `width(text.length, size)` of the result. Give the same `options`.
|
|
107
|
+
* A text with another character has no limit from this function.
|
|
108
|
+
*
|
|
109
|
+
* The limit is sound, not tight. It follows the widest character of the set. It includes each glyph of the GSUB closure, each
|
|
110
|
+
* dotted circle, each part or composite of the normalization, and the largest positive kerning.
|
|
111
|
+
*/
|
|
112
|
+
export function widthBound(font: Font, characters: string, options: MeasureOptions = {}): WidthBound {
|
|
113
|
+
const { script, features, shaper, kerning: pairLookups } = shapingOf(font, options);
|
|
114
|
+
const set = new Set(Array.from(characters, (character) => character.codePointAt(0)!));
|
|
115
|
+
const hasGlyph = (codePoint: number): boolean => font.glyph(codePoint) !== 0;
|
|
116
|
+
const forms = shaper === "indic" ? indicForms(set, hasGlyph) : normalForms(set, hasGlyph);
|
|
117
|
+
|
|
118
|
+
// The roots: the glyph of each code point that the shaper can see. A character with no glyph gives the glyph 0
|
|
119
|
+
const reached = new Set([...forms.codePoints].map((codePoint) => font.glyph(codePoint)));
|
|
120
|
+
// A union of the features gives the same lookups as the union of the stages. Each stage then counts, in the rounds
|
|
121
|
+
const tags = shaper === "indic" ? indicFeatures(features) : shaper === "joining" ? joiningStages(features, true).flatMap((stage) => stage.features) : features;
|
|
122
|
+
const lookups = font.substitutions(script, tags).map((lookup) => lookup.rules());
|
|
123
|
+
reach(reached, lookups.flat(2));
|
|
124
|
+
// The plain shaper is one stage: it applies each lookup once, in the order of the list
|
|
125
|
+
const expansions = expansionsOf(font, reached, lookups, shaper === "plain" ? 1 : ROUNDS);
|
|
126
|
+
|
|
127
|
+
const form = (codePoint: number): Expansion => (forms.composites.get(codePoint) ?? []).reduce((best, composite) => larger(best, form(composite)), expansions.get(font.glyph(codePoint))!);
|
|
128
|
+
// The Indic shaper can insert a dotted circle before each code point. Without a glyph, the code point U+25CC gives the glyph 0.
|
|
129
|
+
// One code point gets at most one: a circle of the vowel constraints takes the next matra, so that syllable is not broken
|
|
130
|
+
const inserted = shaper === "indic" ? expansions.get(font.glyph(0x25cc))! : { advance: 0, glyphs: 0 };
|
|
131
|
+
let largest: Expansion = { advance: 0, glyphs: 0 };
|
|
132
|
+
for (const codePoint of set) {
|
|
133
|
+
// A character with a mark decomposes, and a character with no mark near it keeps its form. So the larger of the two counts
|
|
134
|
+
const decomposed = forms.parts.get(codePoint)!.reduce((total, part) => sum(total, sum(form(part), inserted)), { advance: 0, glyphs: 0 });
|
|
135
|
+
largest = larger(largest, larger(decomposed, sum(form(codePoint), inserted)));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// One glyph gets at most one first value and one second value from each lookup, and each subtable of a lookup is an alternative
|
|
139
|
+
let kerning = 0;
|
|
140
|
+
for (const lookup of pairLookups) {
|
|
141
|
+
const { first, second } = lookup.largest(reached);
|
|
142
|
+
kerning += first + second;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const { advance, glyphs } = largest;
|
|
146
|
+
const pixel = options.rounding === "pixel";
|
|
147
|
+
const float32 = options.sum === "float32" && !pixel;
|
|
148
|
+
// Pixel: an advance gets at most 1/128 px from the grid of 26.6, and then 0.5 px. A kerning sum gets at most 0.5 px
|
|
149
|
+
const pixelMargin = pixel ? 0.5 + 1 / 128 + (kerning > 0 ? 0.5 : 0) : 0;
|
|
150
|
+
// Float32: each lookup gives a glyph two positions, in units of 1/65536 px. A negative value gets no help from the relative
|
|
151
|
+
// factor below, so it has its own margin. The floor of a value and the round of a delta give 1 unit, the floor of the
|
|
152
|
+
// multiplier gives 0.5 units, and the cut of the scale gives KERNING_RANGE / unitsPerEm units to each of the two parts.
|
|
153
|
+
// The single precision gives FLOAT32 to the value and 4 FLOAT32 to the delta
|
|
154
|
+
const positions = pairLookups.length * 2;
|
|
155
|
+
const positionMargin = float32 ? (positions * (1.5 + (2 * KERNING_RANGE) / font.unitsPerEm)) / 65536 : 0;
|
|
156
|
+
const kerningMargin = float32 ? positions * 5 * FLOAT32 * KERNING_RANGE : 0;
|
|
157
|
+
return {
|
|
158
|
+
advance,
|
|
159
|
+
kerning,
|
|
160
|
+
glyphs,
|
|
161
|
+
width(length: number, size: number): number {
|
|
162
|
+
const total = length * ((advance + glyphs * (kerning + kerningMargin)) * (size / font.unitsPerEm) + glyphs * (pixelMargin + positionMargin));
|
|
163
|
+
// Float32: the fround of a glyph or 4 steps of a positive delta, the fround of a segment, and one step of the sum for
|
|
164
|
+
// each segment. A text has at most one segment for each UTF-16 unit
|
|
165
|
+
const single = float32 ? (1 + FLOAT32) ** (length + 6) : 1;
|
|
166
|
+
// The double precision: the scale and a few products, and one rounding for each step of a sum of fractions
|
|
167
|
+
return total * single * (1 + (2 ** 13 + length) * FLOAT64);
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|
package/src/fonts/font.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { gvarAdvanceDeltas, readPointCounts, readTransformedPointCounts, type Gl
|
|
|
8
8
|
import { instanceAdvances, normalize, readAxes, readVariationStore, type Axis, type Instance } from "./variations.ts";
|
|
9
9
|
import { indicScripts, indicShaper } from "./indic.ts";
|
|
10
10
|
import { joiningFeatures, joiningForms, joiningScripts, joiningStages } from "./joining.ts";
|
|
11
|
+
import { hasMark, normalizeCodePoints } from "./normalize.ts";
|
|
11
12
|
import { canvasSegments, canvasWords } from "./words.ts";
|
|
12
13
|
|
|
13
14
|
/** The options of `readFont`. */
|
|
@@ -293,18 +294,41 @@ export function shapingWords(text: string): string[] {
|
|
|
293
294
|
return words;
|
|
294
295
|
}
|
|
295
296
|
|
|
296
|
-
/** The
|
|
297
|
-
|
|
297
|
+
/** The choices that a set of options makes for the GSUB pass and for the kerning. */
|
|
298
|
+
export interface Shaping {
|
|
299
|
+
readonly script: string;
|
|
300
|
+
/** The features of the option `features`, or `defaultFeatures`. */
|
|
301
|
+
readonly features: readonly string[];
|
|
302
|
+
readonly shaper: "indic" | "joining" | "plain";
|
|
303
|
+
readonly kerning: readonly PairLookup[];
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** This function gives the choices of a set of options: the script, the features, the shaper and the kerning lookups. */
|
|
307
|
+
export function shapingOf(font: Font, options: MeasureOptions): Shaping {
|
|
298
308
|
const script = options.script ?? "DFLT";
|
|
299
309
|
const features = options.features ?? defaultFeatures;
|
|
300
310
|
const kerning = options.kerning === false ? [] : font.kerning(script);
|
|
301
|
-
if (options.indic !== false && indicScripts.includes(script)) return {
|
|
302
|
-
|
|
311
|
+
if (options.indic !== false && indicScripts.includes(script)) return { script, features, shaper: "indic", kerning };
|
|
312
|
+
return { script, features, shaper: options.joining !== false && joiningScripts.includes(script) ? "joining" : "plain", kerning };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** The GSUB pass of one set of options: a function that gives the glyphs of one word, and the kerning lookups. */
|
|
316
|
+
function shaperOf(font: Font, options: MeasureOptions): { substitute: (word: string) => number[]; kerning: readonly PairLookup[] } {
|
|
317
|
+
const { script, features, shaper, kerning } = shapingOf(font, options);
|
|
318
|
+
if (shaper === "indic") return { substitute: indicShaper(font, script, features), kerning };
|
|
319
|
+
const joins = shaper === "joining";
|
|
303
320
|
const lookups = joins ? [] : font.substitutions(script, features);
|
|
304
321
|
const stages = joins ? joiningStages(features, features.includes("rclt") && font.substitutions(script, ["rclt"]).length > 0).map((stage) => ({ stage, lookups: font.substitutions(script, stage.features) })) : [];
|
|
322
|
+
// The shaper of Hangul has its own normalization in HarfBuzz: the mode `NONE` keeps each character with a glyph
|
|
323
|
+
const normalizes = script !== "hang";
|
|
324
|
+
const rules = { hasGlyph: (codePoint: number) => font.glyph(codePoint) !== 0, shortCircuit: true };
|
|
305
325
|
const substitute = (word: string): number[] => {
|
|
306
|
-
|
|
307
|
-
|
|
326
|
+
let codePoints = [...word].map((character) => character.codePointAt(0)!);
|
|
327
|
+
let wordGlyphs = codePoints.map((codePoint) => font.glyph(codePoint));
|
|
328
|
+
if (normalizes && (hasMark(word) || wordGlyphs.includes(0))) {
|
|
329
|
+
codePoints = normalizeCodePoints(codePoints, rules);
|
|
330
|
+
wordGlyphs = codePoints.map((codePoint) => font.glyph(codePoint));
|
|
331
|
+
}
|
|
308
332
|
for (const lookup of lookups) lookup.apply(wordGlyphs);
|
|
309
333
|
if (joins) {
|
|
310
334
|
// Each glyph has the bit of its joining form, and the bit 7 for the features of all the glyphs
|
package/src/fonts/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { readFont, measureWidth, glyphAdvances, shapingWords, type AdvanceSource, type Font, type ReadOptions, type MeasureOptions } from "./font.ts";
|
|
2
|
+
export { widthBound, type WidthBound } from "./bound.ts";
|
|
2
3
|
export { canvasSegments, canvasWords, isCjkIdeographOrSymbol } from "./words.ts";
|
|
3
4
|
export { bidiLevels, visualOrder } from "./bidi.ts";
|
|
4
5
|
export { indicScripts, indicShaper, indicSyllables, type IndicSyllableType } from "./indic.ts";
|