@mark1russell7/typeset 0.1.3 → 0.1.5
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/engines.js +46 -1
- package/build/chunks/engines.js.map +1 -1
- package/build/chunks/font.d.ts +6 -0
- package/build/chunks/font.d.ts.map +1 -1
- package/build/chunks/font.js +568 -454
- package/build/chunks/font.js.map +1 -1
- package/build/chunks/fonts.d.ts +13 -3
- package/build/chunks/fonts.d.ts.map +1 -1
- package/build/chunks/fonts.js +13 -11
- package/build/chunks/fonts.js.map +1 -1
- package/build/chunks/measurer.d.ts +6 -2
- package/build/chunks/measurer.d.ts.map +1 -1
- package/build/chunks/measurer.js +43 -8
- package/build/chunks/measurer.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/build/measurer.d.ts +2 -2
- package/package.json +1 -1
- package/src/fonts/bound.ts +10 -7
- package/src/fonts/context.ts +48 -0
- package/src/fonts/font.ts +61 -14
- package/src/fonts/index.ts +1 -0
- package/src/fonts/indic.ts +10 -106
- package/src/fonts/joining.ts +2 -2
- package/src/fonts/kerning.ts +48 -34
- package/src/fonts/normalize.ts +171 -0
- package/src/fonts/risks.ts +48 -0
- package/src/fonts/substitution.ts +15 -44
- package/src/fonts-inspect.ts +2 -2
- package/src/fonts.ts +1 -0
- package/src/measurer/advance.ts +42 -1
- package/src/measurer.ts +1 -1
package/build/chunks/engines.js
CHANGED
|
@@ -1,3 +1,48 @@
|
|
|
1
|
+
import { d as hasDefaultIgnorable, i as shapingOf, p as normalizeCodePoints, s as canvasWords, t as glyphAdvances } from "./font.js";
|
|
2
|
+
// src/fonts/risks.ts
|
|
3
|
+
/** The risks of shaping: the text that the model cannot measure with trust. */
|
|
4
|
+
const unshaped = /[\p{sc=Beng}\p{sc=Guru}\p{sc=Gujr}\p{sc=Orya}\p{sc=Taml}\p{sc=Telu}\p{sc=Knda}\p{sc=Mlym}\p{sc=Sinh}\p{sc=Khmr}\p{sc=Mymr}\p{sc=Tibt}\p{sc=Mong}\p{sc=Nkoo}\p{sc=Mand}]+/gu;
|
|
5
|
+
const pictograph = String.raw`\p{ExtPict}(?:\uFE0F|[\u{1F3FB}-\u{1F3FF}])?`;
|
|
6
|
+
const sequences = new RegExp(String.raw`(\p{ExtPict}[\u{E0020}-\u{E007E}]+\u{E007F})|(${pictograph}(?:\u200D${pictograph})+)|([0-9#*]\uFE0F?\u20E3)|(\p{RI}\p{RI})|([\uFE00-\uFE0F\u{E0100}-\u{E01EF}])`, "gu");
|
|
7
|
+
const sequenceCodes = [
|
|
8
|
+
"tag",
|
|
9
|
+
"emoji-zwj",
|
|
10
|
+
"keycap",
|
|
11
|
+
"flag",
|
|
12
|
+
"variation-selector"
|
|
13
|
+
];
|
|
14
|
+
const clusters = /\P{M}\p{M}*|\p{M}+/gu;
|
|
15
|
+
const control = /\p{Cc}/u;
|
|
16
|
+
/** This function gives the risks of a text in text order. An empty list is not a proof of a correct width. */
|
|
17
|
+
function shapingRisks(font, text, options = {}) {
|
|
18
|
+
const risks = [];
|
|
19
|
+
const add = (code, index, { length }) => risks.push({
|
|
20
|
+
code,
|
|
21
|
+
index,
|
|
22
|
+
length
|
|
23
|
+
});
|
|
24
|
+
const absent = (codePoint) => {
|
|
25
|
+
const character = String.fromCodePoint(codePoint);
|
|
26
|
+
return font.glyph(codePoint) === 0 && !control.test(character) && !hasDefaultIgnorable(character);
|
|
27
|
+
};
|
|
28
|
+
const hasGlyph = (codePoint) => font.glyph(codePoint) !== 0;
|
|
29
|
+
for (const { 0: cluster, index } of text.matchAll(clusters)) if (normalizeCodePoints(Array.from(cluster, (character) => character.codePointAt(0)), {
|
|
30
|
+
hasGlyph,
|
|
31
|
+
shortCircuit: true
|
|
32
|
+
}).some(absent)) add("missing-glyph", index, cluster);
|
|
33
|
+
for (const { 0: part, index } of text.matchAll(unshaped)) add("unshaped-script", index, part);
|
|
34
|
+
for (const match of text.matchAll(sequences)) add(sequenceCodes[match.slice(1).findIndex(Boolean)], match.index, match[0]);
|
|
35
|
+
const tests = options.kerning === false ? [] : font.contextKerning?.(shapingOf(font, options).script) ?? [];
|
|
36
|
+
if (tests.length > 0) {
|
|
37
|
+
let index = 0;
|
|
38
|
+
for (const word of options.shaping === "canvas" ? canvasWords(text) : [text]) {
|
|
39
|
+
const { glyphs } = glyphAdvances(font, word, options);
|
|
40
|
+
if (tests.some((test) => test(glyphs))) add("positioning", index, word);
|
|
41
|
+
index += word.length;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return risks.sort((a, b) => a.index - b.index);
|
|
45
|
+
}
|
|
1
46
|
// src/fonts/engines.ts
|
|
2
47
|
/**
|
|
3
48
|
* The rules of the text engines of Chromium, one row for each engine. The fonts runs of typeset#27 measured each row
|
|
@@ -30,6 +75,6 @@ const engines = {
|
|
|
30
75
|
function isEngineName(name) {
|
|
31
76
|
return Object.hasOwn(engines, name);
|
|
32
77
|
}
|
|
33
|
-
export { isEngineName as n, engines as t };
|
|
78
|
+
export { isEngineName as n, shapingRisks as r, engines as t };
|
|
34
79
|
|
|
35
80
|
//# sourceMappingURL=engines.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engines.js","names":[],"sources":["../../src/fonts/engines.ts"],"sourcesContent":["/**\n * The rules of one text engine for the widths of a font. The fields `deltas` and `metrics` are options of `readFont`, and\n * the fields `rounding` and `sum` are options of `measureWidth`. Thus a caller spreads the same row into the two calls.\n */\nexport interface EngineRules {\n /** The rounding of the `HVAR` and `gvar` deltas of an instance. */\n readonly deltas: \"round\" | \"exact\";\n /** The advance of a composite glyph: its own advance, or the advance of its component with the flag USE_MY_METRICS. */\n readonly metrics: \"own\" | \"component\";\n /** The rounding of each advance: `pixel` rounds it to whole px, and `exact` keeps the fraction. */\n readonly rounding: \"exact\" | \"pixel\";\n /**\n * The sum of the advances: `exact`, or `float32` as Blink adds the widths of the words of a canvas in single precision.\n * A sum of whole px is exact in single precision, so the rounding `pixel` needs no `float32` sum.\n */\n readonly sum: \"exact\" | \"float32\";\n}\n\n/**\n * The rules of the text engines of Chromium, one row for each engine. The fonts runs of typeset#27 measured each row\n * against the canvas, and the T1d diagnosis of the sum measured the field `sum`. A new engine is a new row.\n */\nexport const engines: { readonly freetype: EngineRules; readonly coretext: EngineRules; readonly directwrite: EngineRules } = {\n /** FreeType, as Chromium on Linux uses it: rounded deltas, and each advance in whole px. */\n freetype: { deltas: \"round\", metrics: \"own\", rounding: \"pixel\", sum: \"exact\" },\n /** CoreText on macOS: it keeps the fraction of the deltas. */\n coretext: { deltas: \"exact\", metrics: \"own\", rounding: \"exact\", sum: \"float32\" },\n /** DirectWrite on Windows: a composite glyph has the advance of its USE_MY_METRICS component. */\n directwrite: { deltas: \"round\", metrics: \"component\", rounding: \"exact\", sum: \"float32\" },\n};\n\n/** The name of a text engine of the table `engines`. */\nexport type EngineName = keyof typeof engines;\n\n/** This function tells if a text is the name of an engine of the table `engines`. */\nexport function isEngineName(name: string): name is EngineName {\n return Object.hasOwn(engines, name);\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"engines.js","names":[],"sources":["../../src/fonts/risks.ts","../../src/fonts/engines.ts"],"sourcesContent":["/** The risks of shaping: the text that the model cannot measure with trust. */\nimport { glyphAdvances, shapingOf, type Font, type MeasureOptions } from \"./font.ts\";\nimport { hasDefaultIgnorable, normalizeCodePoints } from \"./normalize.ts\";\nimport { canvasWords } from \"./words.ts\";\n\n/** The kinds of risk. */\nexport type ShapingRiskCode = \"missing-glyph\" | \"unshaped-script\" | \"emoji-zwj\" | \"variation-selector\" | \"keycap\" | \"flag\" | \"tag\" | \"positioning\";\n\n/** One part of a text that the model does not shape as HarfBuzz does, in UTF-16 units. */\nexport interface ShapingRisk {\n readonly code: ShapingRiskCode;\n readonly index: number;\n readonly length: number;\n}\n\nconst unshaped = /[\\p{sc=Beng}\\p{sc=Guru}\\p{sc=Gujr}\\p{sc=Orya}\\p{sc=Taml}\\p{sc=Telu}\\p{sc=Knda}\\p{sc=Mlym}\\p{sc=Sinh}\\p{sc=Khmr}\\p{sc=Mymr}\\p{sc=Tibt}\\p{sc=Mong}\\p{sc=Nkoo}\\p{sc=Mand}]+/gu;\nconst pictograph = String.raw`\\p{ExtPict}(?:\\uFE0F|[\\u{1F3FB}-\\u{1F3FF}])?`;\n// The longest forms come first\nconst sequences = new RegExp(String.raw`(\\p{ExtPict}[\\u{E0020}-\\u{E007E}]+\\u{E007F})|(${pictograph}(?:\\u200D${pictograph})+)|([0-9#*]\\uFE0F?\\u20E3)|(\\p{RI}\\p{RI})|([\\uFE00-\\uFE0F\\u{E0100}-\\u{E01EF}])`, \"gu\");\nconst sequenceCodes: readonly ShapingRiskCode[] = [\"tag\", \"emoji-zwj\", \"keycap\", \"flag\", \"variation-selector\"];\nconst clusters = /\\P{M}\\p{M}*|\\p{M}+/gu;\nconst control = /\\p{Cc}/u;\n\n/** This function gives the risks of a text in text order. An empty list is not a proof of a correct width. */\nexport function shapingRisks(font: Font, text: string, options: MeasureOptions = {}): ShapingRisk[] {\n const risks: ShapingRisk[] = [];\n const add = (code: ShapingRiskCode, index: number, { length }: string): number => risks.push({ code, index, length });\n const absent = (codePoint: number): boolean => {\n const character = String.fromCodePoint(codePoint);\n return font.glyph(codePoint) === 0 && !control.test(character) && !hasDefaultIgnorable(character);\n };\n const hasGlyph = (codePoint: number): boolean => font.glyph(codePoint) !== 0;\n for (const { 0: cluster, index } of text.matchAll(clusters)) {\n if (normalizeCodePoints(Array.from(cluster, (character) => character.codePointAt(0)!), { hasGlyph, shortCircuit: true }).some(absent)) add(\"missing-glyph\", index, cluster);\n }\n for (const { 0: part, index } of text.matchAll(unshaped)) add(\"unshaped-script\", index, part);\n for (const match of text.matchAll(sequences)) add(sequenceCodes[match.slice(1).findIndex(Boolean)]!, match.index, match[0]);\n const tests = options.kerning === false ? [] : font.contextKerning?.(shapingOf(font, options).script) ?? [];\n if (tests.length > 0) {\n let index = 0;\n for (const word of options.shaping === \"canvas\" ? canvasWords(text) : [text]) {\n const { glyphs } = glyphAdvances(font, word, options);\n if (tests.some((test) => test(glyphs))) add(\"positioning\", index, word);\n index += word.length;\n }\n }\n return risks.sort((a, b) => a.index - b.index);\n}\n","/**\n * The rules of one text engine for the widths of a font. The fields `deltas` and `metrics` are options of `readFont`, and\n * the fields `rounding` and `sum` are options of `measureWidth`. Thus a caller spreads the same row into the two calls.\n */\nexport interface EngineRules {\n /** The rounding of the `HVAR` and `gvar` deltas of an instance. */\n readonly deltas: \"round\" | \"exact\";\n /** The advance of a composite glyph: its own advance, or the advance of its component with the flag USE_MY_METRICS. */\n readonly metrics: \"own\" | \"component\";\n /** The rounding of each advance: `pixel` rounds it to whole px, and `exact` keeps the fraction. */\n readonly rounding: \"exact\" | \"pixel\";\n /**\n * The sum of the advances: `exact`, or `float32` as Blink adds the widths of the words of a canvas in single precision.\n * A sum of whole px is exact in single precision, so the rounding `pixel` needs no `float32` sum.\n */\n readonly sum: \"exact\" | \"float32\";\n}\n\n/**\n * The rules of the text engines of Chromium, one row for each engine. The fonts runs of typeset#27 measured each row\n * against the canvas, and the T1d diagnosis of the sum measured the field `sum`. A new engine is a new row.\n */\nexport const engines: { readonly freetype: EngineRules; readonly coretext: EngineRules; readonly directwrite: EngineRules } = {\n /** FreeType, as Chromium on Linux uses it: rounded deltas, and each advance in whole px. */\n freetype: { deltas: \"round\", metrics: \"own\", rounding: \"pixel\", sum: \"exact\" },\n /** CoreText on macOS: it keeps the fraction of the deltas. */\n coretext: { deltas: \"exact\", metrics: \"own\", rounding: \"exact\", sum: \"float32\" },\n /** DirectWrite on Windows: a composite glyph has the advance of its USE_MY_METRICS component. */\n directwrite: { deltas: \"round\", metrics: \"component\", rounding: \"exact\", sum: \"float32\" },\n};\n\n/** The name of a text engine of the table `engines`. */\nexport type EngineName = keyof typeof engines;\n\n/** This function tells if a text is the name of an engine of the table `engines`. */\nexport function isEngineName(name: string): name is EngineName {\n return Object.hasOwn(engines, name);\n}\n"],"mappings":";;;AAeA,MAAM,WAAW;AACjB,MAAM,aAAa,OAAO,GAAG;AAE7B,MAAM,YAAY,IAAI,OAAO,OAAO,GAAG,iDAAiD,WAAW,WAAW,WAAW,iFAAiF,IAAI;AAC9M,MAAM,gBAA4C;CAAC;CAAO;CAAa;CAAU;CAAQ;AAAoB;AAC7G,MAAM,WAAW;AACjB,MAAM,UAAU;;AAGhB,SAAgB,aAAa,MAAY,MAAc,UAA0B,CAAC,GAAkB;CAClG,MAAM,QAAuB,CAAC;CAC9B,MAAM,OAAO,MAAuB,OAAe,EAAE,aAA6B,MAAM,KAAK;EAAE;EAAM;EAAO;CAAO,CAAC;CACpH,MAAM,UAAU,cAA+B;EAC7C,MAAM,YAAY,OAAO,cAAc,SAAS;EAChD,OAAO,KAAK,MAAM,SAAS,MAAM,KAAK,CAAC,QAAQ,KAAK,SAAS,KAAK,CAAC,oBAAoB,SAAS;CAClG;CACA,MAAM,YAAY,cAA+B,KAAK,MAAM,SAAS,MAAM;CAC3E,KAAK,MAAM,EAAE,GAAG,SAAS,WAAW,KAAK,SAAS,QAAQ,GACxD,IAAI,oBAAoB,MAAM,KAAK,UAAU,cAAc,UAAU,YAAY,CAAC,CAAE,GAAG;EAAE;EAAU,cAAc;CAAK,CAAC,CAAC,CAAC,KAAK,MAAM,GAAG,IAAI,iBAAiB,OAAO,OAAO;CAE5K,KAAK,MAAM,EAAE,GAAG,MAAM,WAAW,KAAK,SAAS,QAAQ,GAAG,IAAI,mBAAmB,OAAO,IAAI;CAC5F,KAAK,MAAM,SAAS,KAAK,SAAS,SAAS,GAAG,IAAI,cAAc,MAAM,MAAM,CAAC,CAAC,CAAC,UAAU,OAAO,IAAK,MAAM,OAAO,MAAM,EAAE;CAC1H,MAAM,QAAQ,QAAQ,YAAY,QAAQ,CAAC,IAAI,KAAK,iBAAiB,UAAU,MAAM,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC;CAC1G,IAAI,MAAM,SAAS,GAAG;EACpB,IAAI,QAAQ;EACZ,KAAK,MAAM,QAAQ,QAAQ,YAAY,WAAW,YAAY,IAAI,IAAI,CAAC,IAAI,GAAG;GAC5E,MAAM,EAAE,WAAW,cAAc,MAAM,MAAM,OAAO;GACpD,IAAI,MAAM,MAAM,SAAS,KAAK,MAAM,CAAC,GAAG,IAAI,eAAe,OAAO,IAAI;GACtE,SAAS,KAAK;EAChB;CACF;CACA,OAAO,MAAM,MAAM,GAAG,MAAM,EAAE,QAAQ,EAAE,KAAK;AAC/C;;;;;;ACzBA,MAAa,UAAiH;;CAE5H,UAAU;EAAE,QAAQ;EAAS,SAAS;EAAO,UAAU;EAAS,KAAK;CAAQ;;CAE7E,UAAU;EAAE,QAAQ;EAAS,SAAS;EAAO,UAAU;EAAS,KAAK;CAAU;;CAE/E,aAAa;EAAE,QAAQ;EAAS,SAAS;EAAa,UAAU;EAAS,KAAK;CAAU;AAC1F;;AAMA,SAAgB,aAAa,MAAkC;CAC7D,OAAO,OAAO,OAAO,SAAS,IAAI;AACpC"}
|
package/build/chunks/font.d.ts
CHANGED
|
@@ -150,6 +150,10 @@ interface Font {
|
|
|
150
150
|
/** The source of the advances of this instance. The default instance and a font that is not variable use `hmtx`. */
|
|
151
151
|
readonly advanceSource: AdvanceSource;
|
|
152
152
|
readonly numGlyphs: number;
|
|
153
|
+
/** This value tells if the font has a GPOS table. */
|
|
154
|
+
readonly hasPositioning?: boolean;
|
|
155
|
+
/** This value tells if GDEF has glyph classes. */
|
|
156
|
+
readonly hasGlyphClasses?: boolean;
|
|
153
157
|
/** This function gives the glyph of a code point, or 0 when the font has no glyph for it. */
|
|
154
158
|
glyph(codePoint: number): number;
|
|
155
159
|
/** This function gives the advance of a glyph in font units, as the shaper uses it. A mark has the advance 0. */
|
|
@@ -158,6 +162,8 @@ interface Font {
|
|
|
158
162
|
glyphClass(glyph: number): number;
|
|
159
163
|
/** This function gives the pair lookups of the kerning for an OpenType script tag, for example `latn`. */
|
|
160
164
|
kerning(script: string): readonly PairLookup[];
|
|
165
|
+
/** This function gives a test for each lookup of `kern` and `dist` that the model does not apply. */
|
|
166
|
+
contextKerning?(script: string): readonly ((glyphs: readonly number[]) => boolean)[];
|
|
161
167
|
/** This function gives the GSUB lookups of a list of feature tags for an OpenType script tag, in the order of the lookup list. */
|
|
162
168
|
substitutions(script: string, features: readonly string[]): readonly SubstitutionLookup[];
|
|
163
169
|
/** This function gives the same font at another instance. It uses the tables that are already read. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"font.d.ts","names":[],"sources":["../../src/fonts/substitution.ts","../../src/fonts/variations.ts","../../src/fonts/kerning.ts","../../src/fonts/font.ts"],"mappings":";;;;;cAQa;;;;;UA6CI;;EAEf;;EAEA;;EAEA;;EAEA;;EAEA;;WAES;;;UAIM;;WAEN;;WAEA;;WAEA;;;UAIM;;WAEN;;WAEA;;WAEA;;WAEA;;;;;;EAMT,MAAM,kBAAkB,kBAAkB;;;;;;EAM1C,WAAW,kBAAkB,OAAO,aAAa,cAAc;;;;;EAK/D,WAAW;;;;;;EAMX,4BAA4B;;;UC3Gb;WACN;WACA;WACA;WACA;;;KAIC,WAAW,SAAS;;UCLf;WACN;WACA;;WAEA;;WAEA;;WAEA;;;UAIM;WACN;;;;;WAKA,QAAQ;;EAEjB,KAAK,eAAe,iBAAiB;;;;;EAKrC,QAAQ,QAAQ,sBAAsB;;;UAIvB;WACN;WACA;;;
|
|
1
|
+
{"version":3,"file":"font.d.ts","names":[],"sources":["../../src/fonts/substitution.ts","../../src/fonts/variations.ts","../../src/fonts/kerning.ts","../../src/fonts/font.ts"],"mappings":";;;;;cAQa;;;;;UA6CI;;EAEf;;EAEA;;EAEA;;EAEA;;EAEA;;WAES;;;UAIM;;WAEN;;WAEA;;WAEA;;;UAIM;;WAEN;;WAEA;;WAEA;;WAEA;;;;;;EAMT,MAAM,kBAAkB,kBAAkB;;;;;;EAM1C,WAAW,kBAAkB,OAAO,aAAa,cAAc;;;;;EAK/D,WAAW;;;;;;EAMX,4BAA4B;;;UC3Gb;WACN;WACA;WACA;WACA;;;KAIC,WAAW,SAAS;;UCLf;WACN;WACA;;WAEA;;WAEA;;WAEA;;;UAIM;WACN;;;;;WAKA,QAAQ;;EAEjB,KAAK,eAAe,iBAAiB;;;;;EAKrC,QAAQ,QAAQ,sBAAsB;;;UAIvB;WACN;WACA;;;UCtBM;;WAEN,aAAa;;;;;WAKb,WAAW;;;;;WAKX;;;;;WAKA;;;;;WAKA;;;KAIC;;UAGK;;;;;WAKN;WACA;;WAEA,eAAe;;WAEf,UAAU;;WAEV;;WAEA,eAAe;WACf;;WAEA;;WAEA;;EAET,MAAM;;EAEN,QAAQ;;EAER,WAAW;;EAEX,QAAQ,0BAA0B;;EAElC,gBAAgB,4BAA4B;;EAE5C,cAAc,gBAAgB,uCAAuC;;EAErE,GAAG,UAAU,WAAW;;;iBAIV,SAAS,OAAO,YAAY,UAAS,cAAmB;;UAuKvD;;WAEN;;;;;WAKA;;WAEA;;;;;;WAMA;;;;;;WAMA;;;;;WAKA;;;;;WAKA;;;;;WAKA;;;;;;iBAYK,aAAa;;;;;;;iBAwFb,cAAc,MAAM,MAAM,cAAc,UAAS;EAAwB;EAAkB;;;;;;iBAwG3F,aAAa,MAAM,MAAM,cAAc,cAAc,UAAS"}
|