@mark1russell7/typeset 0.2.0 → 0.3.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.
- package/README.md +26 -1
- package/build/bidi.d.ts.map +1 -1
- package/build/bidi.js +1 -1
- package/build/breaking.d.ts +3 -3
- package/build/breaking.js +1 -1
- package/build/chunks/breaking.d.ts +22 -6
- package/build/chunks/breaking.d.ts.map +1 -1
- package/build/chunks/breaking.js +70 -13
- package/build/chunks/breaking.js.map +1 -1
- package/build/chunks/font.d.ts +4 -3
- package/build/chunks/font.d.ts.map +1 -1
- package/build/chunks/font.js +62 -15
- package/build/chunks/font.js.map +1 -1
- package/build/chunks/hyphen.d.ts +46 -0
- package/build/chunks/hyphen.d.ts.map +1 -0
- package/build/chunks/hyphen.js +55 -0
- package/build/chunks/hyphen.js.map +1 -0
- package/build/chunks/paragraph.d.ts +19 -2
- package/build/chunks/paragraph.d.ts.map +1 -1
- package/build/chunks/resolve.js +68 -7
- package/build/chunks/resolve.js.map +1 -1
- package/build/fonts-inspect.d.ts +4 -4
- package/build/fonts-inspect.d.ts.map +1 -1
- package/build/hyphen/de.d.ts +6 -0
- package/build/hyphen/de.d.ts.map +1 -0
- package/build/hyphen/de.js +76 -0
- package/build/hyphen/de.js.map +1 -0
- package/build/hyphen/en-us.d.ts +6 -0
- package/build/hyphen/en-us.d.ts.map +1 -0
- package/build/hyphen/en-us.js +45 -0
- package/build/hyphen/en-us.js.map +1 -0
- package/build/hyphen/fr.d.ts +6 -0
- package/build/hyphen/fr.d.ts.map +1 -0
- package/build/hyphen/fr.js +71 -0
- package/build/hyphen/fr.js.map +1 -0
- package/build/hyphen.d.ts +2 -0
- package/build/hyphen.js +1 -0
- package/package.json +18 -2
- package/src/bidi/resolve.ts +72 -3
- package/src/breaking/breakers.ts +23 -7
- package/src/breaking/measures.ts +21 -1
- package/src/breaking/paragraph.ts +31 -2
- package/src/breaking.ts +2 -2
- package/src/fonts/bidi.ts +9 -1
- package/src/fonts/font.ts +8 -7
- package/src/fonts/words.ts +47 -10
- package/src/hyphen/de.ts +57 -0
- package/src/hyphen/en-us.ts +26 -0
- package/src/hyphen/fr.ts +52 -0
- package/src/hyphen/liang.ts +98 -0
- package/src/hyphen.ts +1 -0
|
@@ -13,11 +13,18 @@ export interface Box {
|
|
|
13
13
|
readonly width: number;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Glue: a space between two boxes. A line can end at glue that comes after a box. The line then does not show the glue. A
|
|
18
|
+
* justified line sets each glue to its width plus a part of its stretch, or less a part of its shrink.
|
|
19
|
+
*/
|
|
17
20
|
export interface Glue {
|
|
18
21
|
readonly type: "glue";
|
|
19
22
|
readonly text: string;
|
|
20
23
|
readonly width: number;
|
|
24
|
+
/** The width that the glue can add to a justified line. The default is 0. */
|
|
25
|
+
readonly stretch?: number;
|
|
26
|
+
/** The width that the glue can take from a justified line, at most its width. The default is 0. */
|
|
27
|
+
readonly shrink?: number;
|
|
21
28
|
}
|
|
22
29
|
|
|
23
30
|
/**
|
|
@@ -59,6 +66,10 @@ export interface ParagraphOptions {
|
|
|
59
66
|
readonly hyphen?: string;
|
|
60
67
|
/** The cost of a hyphen break. The default is 50, as in TeX. */
|
|
61
68
|
readonly hyphenCost?: number;
|
|
69
|
+
/** The stretch of each glue, in the unit of `widthOf`. The default is 0. TeX uses a half of a space. */
|
|
70
|
+
readonly stretch?: number;
|
|
71
|
+
/** The shrink of each glue, in the unit of `widthOf`. The default is 0. TeX uses a third of a space. */
|
|
72
|
+
readonly shrink?: number;
|
|
62
73
|
}
|
|
63
74
|
|
|
64
75
|
const whiteSpace = /\s+/u;
|
|
@@ -73,10 +84,12 @@ export function paragraphOf(text: string, options: ParagraphOptions): Paragraph
|
|
|
73
84
|
const hyphenCost = options.hyphenCost ?? 50;
|
|
74
85
|
const space = widthOf(" ");
|
|
75
86
|
const hyphenWidth = options.hyphenate === undefined ? 0 : widthOf(hyphen);
|
|
87
|
+
const { stretch = 0, shrink = 0 } = options;
|
|
88
|
+
const glue: Glue = stretch > 0 || shrink > 0 ? { type: "glue", text: " ", width: space, stretch, shrink } : { type: "glue", text: " ", width: space };
|
|
76
89
|
const items: Item[] = [];
|
|
77
90
|
for (const word of text.split(whiteSpace)) {
|
|
78
91
|
if (word === "") continue;
|
|
79
|
-
if (items.length > 0) items.push(
|
|
92
|
+
if (items.length > 0) items.push(glue);
|
|
80
93
|
const free = new Set(options.breaks?.(word) ?? []);
|
|
81
94
|
const offsets = [...new Set([...(options.hyphenate?.(word) ?? []), ...free])].filter((o) => o > 0 && o < word.length).sort((a, b) => a - b);
|
|
82
95
|
let start = 0;
|
|
@@ -128,6 +141,22 @@ export function lineWidth(items: readonly Item[], start: number, end: number): n
|
|
|
128
141
|
return last?.type === "penalty" ? width + last.width : width;
|
|
129
142
|
}
|
|
130
143
|
|
|
144
|
+
/**
|
|
145
|
+
* This function gives the adjustment ratio of Knuth and Plass for the line from the item `start` to a break at `end`. The
|
|
146
|
+
* ratio is the free width divided by the stretch of the line, or by its shrink when the line is too wide. A ratio below -1
|
|
147
|
+
* needs more shrink than the line has. The ratio is infinite when the line needs stretch or shrink and has none.
|
|
148
|
+
*/
|
|
149
|
+
export function adjustmentRatio(items: readonly Item[], start: number, end: number, width: number): number {
|
|
150
|
+
const free = width - lineWidth(items, start, end);
|
|
151
|
+
if (free === 0) return 0;
|
|
152
|
+
let room = 0;
|
|
153
|
+
for (let i = start; i < end; i++) {
|
|
154
|
+
const item = items[i]!;
|
|
155
|
+
if (item.type === "glue") room += (free > 0 ? item.stretch : item.shrink) ?? 0;
|
|
156
|
+
}
|
|
157
|
+
return room > 0 ? free / room : free * Infinity;
|
|
158
|
+
}
|
|
159
|
+
|
|
131
160
|
/**
|
|
132
161
|
* This function gives the lines of a paragraph from the indices of its breaks. Each break is the index of glue or of a penalty.
|
|
133
162
|
* The last line ends at the end of the items.
|
package/src/breaking.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { balanced, balancedBreaker, breakers, greedy, knuthPlass, totalFit, type BalancedOptions, type Breaker, type TotalFitOptions } from "./breaking/breakers.ts";
|
|
2
|
-
export { raggedness, spread } from "./breaking/measures.ts";
|
|
3
|
-
export { isBreak, lineStart, lineWidth, linesOf, paragraphOf, type Box, type Glue, type Item, type Line, type Paragraph, type ParagraphOptions, type Penalty, type WidthOf } from "./breaking/paragraph.ts";
|
|
2
|
+
export { adjustmentRatios, raggedness, spread, worstRatio } from "./breaking/measures.ts";
|
|
3
|
+
export { adjustmentRatio, isBreak, lineStart, lineWidth, linesOf, paragraphOf, type Box, type Glue, type Item, type Line, type Paragraph, type ParagraphOptions, type Penalty, type WidthOf } from "./breaking/paragraph.ts";
|
package/src/fonts/bidi.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* category, so that this entry has no class table. `@mark1russell7/typeset/bidi` has the exact classes.
|
|
6
6
|
*/
|
|
7
7
|
import { bracketOf } from "../bidi/brackets.ts";
|
|
8
|
-
import { removed, resolveLevels, type BidiClass } from "../bidi/resolve.ts";
|
|
8
|
+
import { icuDirection, removed, resolveLevels, type BidiClass } from "../bidi/resolve.ts";
|
|
9
9
|
|
|
10
10
|
const nonSpacingMark = /[\p{Mn}\p{Me}]/u;
|
|
11
11
|
const currency = /\p{Sc}/u;
|
|
@@ -62,4 +62,12 @@ export function bidiLevels(text: string): number[] {
|
|
|
62
62
|
return levels;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
+
/**
|
|
66
|
+
* This function tells if ICU gives the direction `mixed` to a text in a paragraph with the base level 0. Blink divides
|
|
67
|
+
* the text of a canvas into bidi runs only then. ICU reads the classes R, AL and AN and the controls RLE, RLO and RLI.
|
|
68
|
+
*/
|
|
69
|
+
export function isMixed(text: string): boolean {
|
|
70
|
+
return icuDirection([...text].map(bidiClass), 0) === "mixed";
|
|
71
|
+
}
|
|
72
|
+
|
|
65
73
|
export { visualOrder } from "../bidi/resolve.ts";
|
package/src/fonts/font.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { instanceAdvances, normalize, readAxes, readVariationStore, type Axis, t
|
|
|
10
10
|
import { indicScripts, indicShaper } from "./indic.ts";
|
|
11
11
|
import { joiningFeatures, joiningForms, joiningScripts, joiningStages } from "./joining.ts";
|
|
12
12
|
import { hasDefaultIgnorable, hasMark, isHidden, normalizeCodePoints, zeroedMarks } from "./normalize.ts";
|
|
13
|
-
import { canvasSegments,
|
|
13
|
+
import { canvasRuns, canvasSegments, canvasText } from "./words.ts";
|
|
14
14
|
|
|
15
15
|
/** The options of `readFont`. */
|
|
16
16
|
export interface ReadOptions {
|
|
@@ -275,7 +275,7 @@ export interface MeasureOptions {
|
|
|
275
275
|
readonly indic?: boolean;
|
|
276
276
|
/**
|
|
277
277
|
* The shaping of the browser to model. `dom` (the default) shapes the words of `shapingWords` and then kerns all the
|
|
278
|
-
* text. `canvas` shapes and kerns each
|
|
278
|
+
* text. `canvas` shapes and kerns each segment of `canvasRuns` alone, as Blink does for the text of a canvas.
|
|
279
279
|
*/
|
|
280
280
|
readonly shaping?: "dom" | "canvas";
|
|
281
281
|
/**
|
|
@@ -379,8 +379,9 @@ function shaperOf(font: Font, options: MeasureOptions): { substitute: (word: str
|
|
|
379
379
|
/**
|
|
380
380
|
* This function gives the glyphs of a text and their advances in font units. With the shaping `dom`, it applies the GSUB
|
|
381
381
|
* substitutions to each word of `shapingWords`, and then the pair kerning to all the text. With the shaping `canvas`, it
|
|
382
|
-
* applies the substitutions and the kerning to each
|
|
383
|
-
* the joining forms of each word, in the stages of `joiningStages`. A script of
|
|
382
|
+
* applies the substitutions and the kerning to each segment of `canvasRuns` alone, after the changes of `canvasText`. A
|
|
383
|
+
* script of `joiningScripts` also gets the joining forms of each word, in the stages of `joiningStages`. A script of
|
|
384
|
+
* `indicScripts` uses `indicShaper`.
|
|
384
385
|
*/
|
|
385
386
|
export function glyphAdvances(font: Font, text: string, options: MeasureOptions = {}): { glyphs: number[]; advances: number[] } {
|
|
386
387
|
const { glyphs, advances } = shapedAdvances(font, text, options);
|
|
@@ -393,8 +394,8 @@ function shapedAdvances(font: Font, text: string, options: MeasureOptions): { gl
|
|
|
393
394
|
const canvas = options.shaping === "canvas";
|
|
394
395
|
const glyphs: number[] = [];
|
|
395
396
|
const advances: number[] = [];
|
|
396
|
-
for (const word of canvas ?
|
|
397
|
-
const wordGlyphs = substitute(word);
|
|
397
|
+
for (const word of canvas ? canvasRuns(text).segments : shapingWords(text)) {
|
|
398
|
+
const wordGlyphs = substitute(canvas ? canvasText(word) : word);
|
|
398
399
|
const wordAdvances = wordGlyphs.map((glyph) => font.advance(glyph));
|
|
399
400
|
if (canvas) kern(font, kerning, wordGlyphs, wordAdvances);
|
|
400
401
|
glyphs.push(...wordGlyphs);
|
|
@@ -471,7 +472,7 @@ function float32Width(font: Font, text: string, size: number, options: MeasureOp
|
|
|
471
472
|
const pxPerUnit = size / font.unitsPerEm;
|
|
472
473
|
let total = 0;
|
|
473
474
|
for (const segment of canvasSegments(text)) {
|
|
474
|
-
const glyphs = substitute(segment);
|
|
475
|
+
const glyphs = substitute(canvasText(segment));
|
|
475
476
|
// Skia gives each advance in single precision, and Blink cuts it to the 16.16 fixed point of HarfBuzz
|
|
476
477
|
const positions = glyphs.map((glyph) => Math.trunc(Math.fround(font.advance(glyph) * pxPerUnit) * 65536));
|
|
477
478
|
kern(font, kerning, glyphs, glyphs.map((glyph) => font.advance(glyph)), positions, scale);
|
package/src/fonts/words.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The words of the text of a canvas. Blink shapes the text of `fillText` and `measureText` one word at a time, so no GSUB
|
|
3
3
|
* context and no pair kerning goes across two words. This module follows `NextWordEndIndex` of
|
|
4
|
-
* `third_party/blink/renderer/platform/fonts/plain_text_node.cc
|
|
4
|
+
* `third_party/blink/renderer/platform/fonts/plain_text_node.cc`, the CJK table of `character_property_data.h` and
|
|
5
|
+
* `TreatAsZeroWidthSpaceInComplexScriptLegacy` of `character.h`.
|
|
5
6
|
* The Chromium Authors wrote these files under a BSD-style license.
|
|
6
7
|
*/
|
|
7
|
-
import { bidiLevels, visualOrder } from "./bidi.ts";
|
|
8
|
+
import { bidiLevels, isMixed, visualOrder } from "./bidi.ts";
|
|
8
9
|
|
|
9
10
|
/** The single code points of the CJK table of Blink. */
|
|
10
11
|
const cjkValues: ReadonlySet<number> = new Set([
|
|
@@ -49,17 +50,41 @@ function isDelimiter(character: string): boolean {
|
|
|
49
50
|
return character === " " || character === "\t" || character === "";
|
|
50
51
|
}
|
|
51
52
|
|
|
53
|
+
/** The space characters of the canvas text preparation: U+0020, U+0009, U+000A, U+000C and U+000D. */
|
|
54
|
+
const canvasSpaces = /[ \t\n\f\r]/u;
|
|
55
|
+
/** The characters of `TreatAsZeroWidthSpaceInComplexScriptLegacy` in Blink that are not canvas spaces. */
|
|
56
|
+
const zeroWidthSpaceLike = /[-]/u;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* This function gives the character that Blink shapes in place of the character `character` of a canvas text. A canvas
|
|
60
|
+
* space becomes U+0020. U+00AD, U+FEFF, U+FFFC, the marks LRM and RLM and the controls LRE to RLO become U+200B, and
|
|
61
|
+
* that ends a word.
|
|
62
|
+
*/
|
|
63
|
+
function canvasCharacter(character: string): string {
|
|
64
|
+
if (canvasSpaces.test(character)) return " ";
|
|
65
|
+
return zeroWidthSpaceLike.test(character) ? "" : character;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* This function gives the text that Blink shapes for the text of a canvas, as `NormalizeSpacesAndMaybeBidi` of
|
|
70
|
+
* `plain_text_node.cc` gives it. It changes only characters of the Basic Multilingual Plane, so the length stays.
|
|
71
|
+
*/
|
|
72
|
+
export function canvasText(text: string): string {
|
|
73
|
+
return text.replace(/[ \t\n\f\r-]/gu, canvasCharacter);
|
|
74
|
+
}
|
|
75
|
+
|
|
52
76
|
/**
|
|
53
77
|
* This function divides the text of a canvas into the words that Blink shapes one at a time. Each space, tab and zero
|
|
54
|
-
* width space is a word alone. Each
|
|
55
|
-
* Common after it.
|
|
78
|
+
* width space is a word alone. Each character that `canvasText` changes into one of them is also a word alone. Each CJK
|
|
79
|
+
* character starts a word, with its marks and the CJK characters of the script Common after it.
|
|
56
80
|
*/
|
|
57
81
|
export function canvasWords(text: string): string[] {
|
|
58
82
|
const characters = [...text];
|
|
83
|
+
const shaped = characters.map(canvasCharacter);
|
|
59
84
|
const words: string[] = [];
|
|
60
85
|
let start = 0;
|
|
61
86
|
while (start < characters.length) {
|
|
62
|
-
const end = wordEnd(
|
|
87
|
+
const end = wordEnd(shaped, start);
|
|
63
88
|
words.push(characters.slice(start, end).join(""));
|
|
64
89
|
start = end;
|
|
65
90
|
}
|
|
@@ -67,16 +92,19 @@ export function canvasWords(text: string): string[] {
|
|
|
67
92
|
}
|
|
68
93
|
|
|
69
94
|
/**
|
|
70
|
-
* This function gives the segments of the text of a canvas in the
|
|
71
|
-
* part of a word where the bidi level changes. Blink
|
|
95
|
+
* This function gives the segments of the text of a canvas and their bidi levels, in the logical order. A segment is a word
|
|
96
|
+
* of `canvasWords`, or a part of a word where the bidi level changes. Blink divides a text into bidi runs only when ICU
|
|
97
|
+
* gives the paragraph the direction `mixed`. Else each segment is a word at the level 0.
|
|
72
98
|
*/
|
|
73
|
-
export function
|
|
99
|
+
export function canvasRuns(text: string): { segments: string[]; levels: number[] } {
|
|
100
|
+
const words = canvasWords(text);
|
|
101
|
+
if (!isMixed(text)) return { segments: words, levels: words.map(() => 0) };
|
|
74
102
|
const characters = [...text];
|
|
75
103
|
const levels = bidiLevels(text);
|
|
76
104
|
const segments: string[] = [];
|
|
77
105
|
const segmentLevels: number[] = [];
|
|
78
106
|
let at = 0;
|
|
79
|
-
for (const word of
|
|
107
|
+
for (const word of words) {
|
|
80
108
|
const end = at + [...word].length;
|
|
81
109
|
let start = at;
|
|
82
110
|
for (let i = at + 1; i <= end; i++) {
|
|
@@ -87,7 +115,16 @@ export function canvasSegments(text: string): string[] {
|
|
|
87
115
|
}
|
|
88
116
|
at = end;
|
|
89
117
|
}
|
|
90
|
-
return
|
|
118
|
+
return { segments, levels: segmentLevels };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* This function gives the segments of `canvasRuns` in the visual order. Blink shapes each bidi run alone and adds the
|
|
123
|
+
* widths of the words in this order.
|
|
124
|
+
*/
|
|
125
|
+
export function canvasSegments(text: string): string[] {
|
|
126
|
+
const { segments, levels } = canvasRuns(text);
|
|
127
|
+
return visualOrder(segments, levels);
|
|
91
128
|
}
|
|
92
129
|
|
|
93
130
|
/** This function gives the index after the last character of the word that starts at `start`. */
|