@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
package/README.md
CHANGED
|
@@ -6,6 +6,8 @@ This package measures text and breaks it into lines. Each area has its own subpa
|
|
|
6
6
|
| --- | --- |
|
|
7
7
|
| `@mark1russell7/typeset/breaking` | The paragraph model of boxes, glue and penalties, and the greedy, total fit and balanced line breakers. |
|
|
8
8
|
| `@mark1russell7/typeset/fonts` | The font model: it reads a font file and gives the width of a text as a browser engine gives it. |
|
|
9
|
+
| `@mark1russell7/typeset/hyphen` | `hyphenator`: the matcher of Liang, with no language data. |
|
|
10
|
+
| `@mark1russell7/typeset/hyphen/en-us`, `/de`, `/fr` | The hyphen patterns of one language, for the hook `hyphenate`. |
|
|
9
11
|
| `@mark1russell7/typeset/measurer` | `fontAdvance`: the advance function of a layout engine, from the font files of the host. |
|
|
10
12
|
| `@mark1russell7/typeset/text` | The `Text` rope: a text with its width, and edits that measure only the changed parts. |
|
|
11
13
|
|
|
@@ -21,7 +23,30 @@ const lines = linesOf(paragraph, knuthPlass.breaks(paragraph, 120));
|
|
|
21
23
|
|
|
22
24
|
- Each breaker takes any width function. The function gives the width of a run of text in any unit.
|
|
23
25
|
- `greedy` fills each line as a browser does. `knuthPlass` chooses the breaks with the least sum of demerits. `balanced` keeps the greedy line count with more equal lines.
|
|
24
|
-
- The option `hyphenate` gives the offsets of the hyphen breaks of a word.
|
|
26
|
+
- The option `hyphenate` gives the offsets of the hyphen breaks of a word. A language subpath gives a function for this option.
|
|
27
|
+
- The options `stretch` and `shrink` make a justified paragraph. Total fit then uses the adjustment ratio of Knuth and Plass.
|
|
28
|
+
- `worstRatio` measures the justified edge. `docs/BREAKING.md` in the repository defines it.
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { knuthPlass, linesOf, paragraphOf, worstRatio } from "@mark1russell7/typeset/breaking";
|
|
32
|
+
import { hyphenate } from "@mark1russell7/typeset/hyphen/en-us";
|
|
33
|
+
|
|
34
|
+
const paragraph = paragraphOf("The quick brown fox jumps over the lazy dog", { widthOf, hyphenate, stretch: 4, shrink: 2.5 });
|
|
35
|
+
const lines = linesOf(paragraph, knuthPlass.breaks(paragraph, 120));
|
|
36
|
+
const edge = worstRatio(paragraph, lines, 120);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### The sizes and the licenses of the language sets
|
|
40
|
+
|
|
41
|
+
Each set is on its own subpath, so an application pays only for the sets that it imports. The bytes are for one bundled set with the matcher, minified, at gzip level 9.
|
|
42
|
+
|
|
43
|
+
| Subpath | Gzip bytes | Source file of `tex-hyphen` | License |
|
|
44
|
+
| --- | --- | --- | --- |
|
|
45
|
+
| `./hyphen/en-us` | 18202 | `hyph-en-us.tex` | The all-permissive notice of Gerard D.C. Kuiken |
|
|
46
|
+
| `./hyphen/de` | 123851 | `hyph-de-1996.tex` | MIT |
|
|
47
|
+
| `./hyphen/fr` | 5568 | `hyph-fr.tex` | MIT |
|
|
48
|
+
|
|
49
|
+
The license of a pattern file applies to its data. The header of each file stays at the top of its module and in a bundle. `docs/HYPHENATION.md` in the repository has the sources and the results.
|
|
25
50
|
|
|
26
51
|
## The Text rope
|
|
27
52
|
|
package/build/bidi.d.ts.map
CHANGED
|
@@ -1 +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;;;;;;
|
|
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;;;;;;wBAiTG,cAAc,kBAAkB,aAAa,8BAAkC,YAAW,qBAA8B;;;;;wBAkExH,YAAY,GAAG,gBAAgB,KAAK,4BAA4B;;wBAgBhE,gBAAgB,QAAQ,cAAc;;wBC7ZtC,UAAU,oBAAoB;;wBAa9B,YAAY,cAAc,YAAW,qBAA8B;qBCtCtE"}
|
package/build/bidi.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as visualOrder, c as classRuns, i as visualIndexes, l as unicodeVersion, n as removed, o as bracketOf, r as resolveLevels, s as classNames } from "./chunks/resolve.js";
|
|
2
2
|
// src/bidi/classes.ts
|
|
3
3
|
/**
|
|
4
4
|
* The class and the paired bracket of a code point. These values come from a table that the tool in `packages/conformance`
|
package/build/breaking.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as Paragraph, c as WidthOf, d as
|
|
2
|
-
import { a as
|
|
3
|
-
export { type BalancedOptions, type Box, type Breaker, type Glue, type Item, type Line, type Paragraph, type ParagraphOptions, type Penalty, type TotalFitOptions, type WidthOf, balanced, balancedBreaker, breakers, greedy, isBreak, knuthPlass, lineStart, lineWidth, linesOf, paragraphOf, raggedness, spread, totalFit };
|
|
1
|
+
import { a as Paragraph, c as WidthOf, d as lineStart, f as lineWidth, i as Line, l as adjustmentRatio, m as paragraphOf, n as Glue, o as ParagraphOptions, p as linesOf, r as Item, s as Penalty, t as Box, u as isBreak } from "./chunks/paragraph.js";
|
|
2
|
+
import { a as worstRatio, c as TotalFitOptions, d as breakers, f as greedy, i as spread, l as balanced, m as totalFit, n as adjustmentRatios, o as BalancedOptions, p as knuthPlass, r as raggedness, s as Breaker, u as balancedBreaker } from "./chunks/breaking.js";
|
|
3
|
+
export { type BalancedOptions, type Box, type Breaker, type Glue, type Item, type Line, type Paragraph, type ParagraphOptions, type Penalty, type TotalFitOptions, type WidthOf, adjustmentRatio, adjustmentRatios, balanced, balancedBreaker, breakers, greedy, isBreak, knuthPlass, lineStart, lineWidth, linesOf, paragraphOf, raggedness, spread, totalFit, worstRatio };
|
package/build/breaking.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { f as adjustmentRatio, n as adjustmentRatios, o as balanced, s as balancedBreaker, c as breakers, l as greedy, p as isBreak, u as knuthPlass, m as lineStart, h as lineWidth, g as linesOf, _ as paragraphOf, r as raggedness, i as spread, d as totalFit, a as worstRatio } from "./chunks/breaking.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as Paragraph, c as WidthOf, d as
|
|
1
|
+
import { a as Paragraph, c as WidthOf, d as lineStart, f as lineWidth, i as Line, l as adjustmentRatio, m as paragraphOf, n as Glue, o as ParagraphOptions, p as linesOf, r as Item, s as Penalty, t as Box, u as isBreak } from "./paragraph.js";
|
|
2
2
|
/**
|
|
3
3
|
* A breaker: a strategy that chooses the breaks of a paragraph for the line width `width`. It gives the indices of the
|
|
4
4
|
* items where the lines end, in order. A breaker reads only the items, so any width function and any builder work with it.
|
|
@@ -20,9 +20,14 @@ interface TotalFitOptions {
|
|
|
20
20
|
readonly flaggedCost?: number;
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
23
|
-
* This function gives the total fit breaker of Knuth and Plass
|
|
24
|
-
* the
|
|
25
|
-
* cost of its penalty. The ratio `r` is the free space
|
|
23
|
+
* This function gives the total fit breaker of Knuth and Plass. It chooses the breaks with the least sum of demerits over all
|
|
24
|
+
* the lines. A paragraph with no stretch and no shrink has a ragged right edge. The demerits of a line are then
|
|
25
|
+
* `(linePenalty + 100 r²)²` plus the square of the cost of its penalty. The ratio `r` is the free space divided by the width.
|
|
26
|
+
* The last line has `r = 0`.
|
|
27
|
+
*
|
|
28
|
+
* A paragraph with glue that has stretch or shrink has a justified edge. The demerits then use `100 |r|³` with the ratio of
|
|
29
|
+
* `adjustmentRatio`, and the last line does not stretch. A line that needs more shrink than it has counts first. Thus the
|
|
30
|
+
* breaker keeps such a line only when each set of breaks has one.
|
|
26
31
|
*/
|
|
27
32
|
declare function totalFit(options?: TotalFitOptions): Breaker;
|
|
28
33
|
/** The total fit breaker with the default options. */
|
|
@@ -48,8 +53,19 @@ declare const breakers: Readonly<Record<"greedy" | "total-fit" | "balanced", Bre
|
|
|
48
53
|
declare function raggedness(lines: readonly Line[], width: number): number;
|
|
49
54
|
/** This function gives the spread of the lines: the width of the widest line less the width of the narrowest line, the last line included. */
|
|
50
55
|
declare function spread(lines: readonly Line[]): number;
|
|
56
|
+
/**
|
|
57
|
+
* This function gives the adjustment ratio of each line for the width `width`. A renderer sets each glue of a line with a
|
|
58
|
+
* ratio `r` to its width plus `r` times its stretch. When `r` is negative, the glue gets its width plus `r` times its shrink.
|
|
59
|
+
* The last line does not stretch, so its ratio is not more than 0.
|
|
60
|
+
*/
|
|
61
|
+
declare function adjustmentRatios(paragraph: Paragraph, lines: readonly Line[], width: number): number[];
|
|
62
|
+
/**
|
|
63
|
+
* This function gives the measure of the justified edge: the largest absolute adjustment ratio of the lines. A value of 1 or
|
|
64
|
+
* less means that the glue of each line reaches the edge within its stretch and its shrink.
|
|
65
|
+
*/
|
|
66
|
+
declare function worstRatio(paragraph: Paragraph, lines: readonly Line[], width: number): number;
|
|
51
67
|
declare namespace breaking_d_exports {
|
|
52
|
-
export { BalancedOptions, Box, Breaker, Glue, Item, Line, Paragraph, ParagraphOptions, Penalty, TotalFitOptions, WidthOf, balanced, balancedBreaker, breakers, greedy, isBreak, knuthPlass, lineStart, lineWidth, linesOf, paragraphOf, raggedness, spread, totalFit };
|
|
68
|
+
export { BalancedOptions, Box, Breaker, Glue, Item, Line, Paragraph, ParagraphOptions, Penalty, TotalFitOptions, WidthOf, adjustmentRatio, adjustmentRatios, balanced, balancedBreaker, breakers, greedy, isBreak, knuthPlass, lineStart, lineWidth, linesOf, paragraphOf, raggedness, spread, totalFit, worstRatio };
|
|
53
69
|
}
|
|
54
|
-
export {
|
|
70
|
+
export { worstRatio as a, TotalFitOptions as c, breakers as d, greedy as f, spread as i, balanced as l, totalFit as m, adjustmentRatios as n, BalancedOptions as o, knuthPlass as p, raggedness as r, Breaker as s, breaking_d_exports as t, balancedBreaker as u };
|
|
55
71
|
//# sourceMappingURL=breaking.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"breaking.d.ts","names":[],"sources":["../../src/breaking/breakers.ts","../../src/breaking/measures.ts","../../src/breaking.ts"],"mappings":";;;;;UAMiB;WACN;EACT,OAAO,WAAW,WAAW;;;;;;cAOlB,QAAQ;;UAsCJ;;WAEN;;WAEA
|
|
1
|
+
{"version":3,"file":"breaking.d.ts","names":[],"sources":["../../src/breaking/breakers.ts","../../src/breaking/measures.ts","../../src/breaking.ts"],"mappings":";;;;;UAMiB;WACN;EACT,OAAO,WAAW,WAAW;;;;;;cAOlB,QAAQ;;UAsCJ;;WAEN;;WAEA;;;;;;;;;;;;iBAaK,SAAS,UAAS,kBAAuB;;cAU5C,YAAY;;UAiER;;WAEN;;;;;;iBAOK,gBAAgB,UAAS,kBAAuB;;cAmBnD,UAAU;;cAGV,UAAU,SAAS,4CAA4C;;;;;iBC1K5D,WAAW,gBAAgB,QAAQ;;iBAWnC,OAAO,gBAAgB;;;;;;iBAgBvB,iBAAiB,WAAW,WAAW,gBAAgB,QAAQ;;;;;iBAW/D,WAAW,WAAW,WAAW,gBAAgB,QAAQ"}
|
package/build/chunks/breaking.js
CHANGED
|
@@ -11,14 +11,22 @@ function paragraphOf(text, options) {
|
|
|
11
11
|
const hyphenCost = options.hyphenCost ?? 50;
|
|
12
12
|
const space = widthOf(" ");
|
|
13
13
|
const hyphenWidth = options.hyphenate === void 0 ? 0 : widthOf(hyphen);
|
|
14
|
+
const { stretch = 0, shrink = 0 } = options;
|
|
15
|
+
const glue = stretch > 0 || shrink > 0 ? {
|
|
16
|
+
type: "glue",
|
|
17
|
+
text: " ",
|
|
18
|
+
width: space,
|
|
19
|
+
stretch,
|
|
20
|
+
shrink
|
|
21
|
+
} : {
|
|
22
|
+
type: "glue",
|
|
23
|
+
text: " ",
|
|
24
|
+
width: space
|
|
25
|
+
};
|
|
14
26
|
const items = [];
|
|
15
27
|
for (const word of text.split(whiteSpace)) {
|
|
16
28
|
if (word === "") continue;
|
|
17
|
-
if (items.length > 0) items.push(
|
|
18
|
-
type: "glue",
|
|
19
|
-
text: " ",
|
|
20
|
-
width: space
|
|
21
|
-
});
|
|
29
|
+
if (items.length > 0) items.push(glue);
|
|
22
30
|
const free = new Set(options.breaks?.(word) ?? []);
|
|
23
31
|
const offsets = [.../* @__PURE__ */ new Set([...options.hyphenate?.(word) ?? [], ...free])].filter((o) => o > 0 && o < word.length).sort((a, b) => a - b);
|
|
24
32
|
let start = 0;
|
|
@@ -76,6 +84,21 @@ function lineWidth(items, start, end) {
|
|
|
76
84
|
return last?.type === "penalty" ? width + last.width : width;
|
|
77
85
|
}
|
|
78
86
|
/**
|
|
87
|
+
* This function gives the adjustment ratio of Knuth and Plass for the line from the item `start` to a break at `end`. The
|
|
88
|
+
* 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
|
|
89
|
+
* needs more shrink than the line has. The ratio is infinite when the line needs stretch or shrink and has none.
|
|
90
|
+
*/
|
|
91
|
+
function adjustmentRatio(items, start, end, width) {
|
|
92
|
+
const free = width - lineWidth(items, start, end);
|
|
93
|
+
if (free === 0) return 0;
|
|
94
|
+
let room = 0;
|
|
95
|
+
for (let i = start; i < end; i++) {
|
|
96
|
+
const item = items[i];
|
|
97
|
+
if (item.type === "glue") room += (free > 0 ? item.stretch : item.shrink) ?? 0;
|
|
98
|
+
}
|
|
99
|
+
return room > 0 ? free / room : free * Infinity;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
79
102
|
* 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.
|
|
80
103
|
* The last line ends at the end of the items.
|
|
81
104
|
*/
|
|
@@ -139,9 +162,14 @@ function greedyBreaks(paragraph, width) {
|
|
|
139
162
|
return breaks;
|
|
140
163
|
}
|
|
141
164
|
/**
|
|
142
|
-
* This function gives the total fit breaker of Knuth and Plass
|
|
143
|
-
* the
|
|
144
|
-
* cost of its penalty. The ratio `r` is the free space
|
|
165
|
+
* This function gives the total fit breaker of Knuth and Plass. It chooses the breaks with the least sum of demerits over all
|
|
166
|
+
* the lines. A paragraph with no stretch and no shrink has a ragged right edge. The demerits of a line are then
|
|
167
|
+
* `(linePenalty + 100 r²)²` plus the square of the cost of its penalty. The ratio `r` is the free space divided by the width.
|
|
168
|
+
* The last line has `r = 0`.
|
|
169
|
+
*
|
|
170
|
+
* A paragraph with glue that has stretch or shrink has a justified edge. The demerits then use `100 |r|³` with the ratio of
|
|
171
|
+
* `adjustmentRatio`, and the last line does not stretch. A line that needs more shrink than it has counts first. Thus the
|
|
172
|
+
* breaker keeps such a line only when each set of breaks has one.
|
|
145
173
|
*/
|
|
146
174
|
function totalFit(options = {}) {
|
|
147
175
|
const linePenalty = options.linePenalty ?? 10;
|
|
@@ -153,6 +181,8 @@ function totalFit(options = {}) {
|
|
|
153
181
|
}
|
|
154
182
|
/** The total fit breaker with the default options. */
|
|
155
183
|
const knuthPlass = totalFit();
|
|
184
|
+
/** The largest badness of a justified line. An infinite ratio gets this value, so the sum of the demerits stays finite. */
|
|
185
|
+
const worstBadness = 1e8;
|
|
156
186
|
/** This function gives the breaks of the total fit breaker, by dynamic programming over the break opportunities. */
|
|
157
187
|
function totalFitBreaks(paragraph, width, linePenalty, flaggedCost) {
|
|
158
188
|
const { items } = paragraph;
|
|
@@ -161,6 +191,9 @@ function totalFitBreaks(paragraph, width, linePenalty, flaggedCost) {
|
|
|
161
191
|
nodes.push(items.length);
|
|
162
192
|
const best = new Array(nodes.length).fill(Infinity);
|
|
163
193
|
const previous = new Array(nodes.length).fill(-1);
|
|
194
|
+
const justified = items.some((item) => item.type === "glue" && ((item.stretch ?? 0) > 0 || (item.shrink ?? 0) > 0));
|
|
195
|
+
const over = new Array(nodes.length).fill(justified ? Infinity : 0);
|
|
196
|
+
over[0] = 0;
|
|
164
197
|
best[0] = 0;
|
|
165
198
|
for (let a = 0; a < nodes.length - 1; a++) {
|
|
166
199
|
if (best[a] === Infinity) continue;
|
|
@@ -170,7 +203,8 @@ function totalFitBreaks(paragraph, width, linePenalty, flaggedCost) {
|
|
|
170
203
|
const end = nodes[b];
|
|
171
204
|
const last = b === nodes.length - 1;
|
|
172
205
|
const used = lineWidth(items, start, end);
|
|
173
|
-
const
|
|
206
|
+
const fit = justified ? adjustmentRatio(items, start, end, width) : 0;
|
|
207
|
+
const overflow = justified ? fit < -1 : used > width;
|
|
174
208
|
if (overflow && b > a + 1) {
|
|
175
209
|
if (items[end]?.type === "penalty") continue;
|
|
176
210
|
break;
|
|
@@ -178,12 +212,14 @@ function totalFitBreaks(paragraph, width, linePenalty, flaggedCost) {
|
|
|
178
212
|
const penalty = items[end];
|
|
179
213
|
const cost = penalty?.type === "penalty" ? penalty.cost : 0;
|
|
180
214
|
const ratio = last || overflow ? 0 : (width - used) / width;
|
|
181
|
-
let demerits = (linePenalty + 100 * ratio * ratio) ** 2;
|
|
215
|
+
let demerits = (linePenalty + (justified ? Math.min(100 * Math.abs(last ? Math.min(fit, 0) : fit) ** 3, worstBadness) : 100 * ratio * ratio)) ** 2;
|
|
182
216
|
if (cost > 0) demerits += cost * cost;
|
|
183
217
|
else if (cost > -Infinity && cost < 0) demerits -= cost * cost;
|
|
184
218
|
if (flaggedBefore && isFlagged(paragraph, end)) demerits += flaggedCost;
|
|
185
219
|
const total = best[a] + demerits;
|
|
186
|
-
|
|
220
|
+
const count = over[a] + (justified && overflow ? 1 : 0);
|
|
221
|
+
if (count < over[b] || count === over[b] && total < best[b]) {
|
|
222
|
+
over[b] = count;
|
|
187
223
|
best[b] = total;
|
|
188
224
|
previous[b] = a;
|
|
189
225
|
}
|
|
@@ -253,8 +289,28 @@ function spread(lines) {
|
|
|
253
289
|
}
|
|
254
290
|
return widest - narrowest;
|
|
255
291
|
}
|
|
292
|
+
/**
|
|
293
|
+
* This function gives the adjustment ratio of each line for the width `width`. A renderer sets each glue of a line with a
|
|
294
|
+
* ratio `r` to its width plus `r` times its stretch. When `r` is negative, the glue gets its width plus `r` times its shrink.
|
|
295
|
+
* The last line does not stretch, so its ratio is not more than 0.
|
|
296
|
+
*/
|
|
297
|
+
function adjustmentRatios(paragraph, lines, width) {
|
|
298
|
+
return lines.map((line, index) => {
|
|
299
|
+
const ratio = adjustmentRatio(paragraph.items, line.start, line.end, width);
|
|
300
|
+
return index === lines.length - 1 ? Math.min(ratio, 0) : ratio;
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* This function gives the measure of the justified edge: the largest absolute adjustment ratio of the lines. A value of 1 or
|
|
305
|
+
* less means that the glue of each line reaches the edge within its stretch and its shrink.
|
|
306
|
+
*/
|
|
307
|
+
function worstRatio(paragraph, lines, width) {
|
|
308
|
+
return Math.max(0, ...adjustmentRatios(paragraph, lines, width).map(Math.abs));
|
|
309
|
+
}
|
|
256
310
|
// src/breaking.ts
|
|
257
311
|
var breaking_exports = /* @__PURE__ */ __exportAll({
|
|
312
|
+
adjustmentRatio: () => adjustmentRatio,
|
|
313
|
+
adjustmentRatios: () => adjustmentRatios,
|
|
258
314
|
balanced: () => balanced,
|
|
259
315
|
balancedBreaker: () => balancedBreaker,
|
|
260
316
|
breakers: () => breakers,
|
|
@@ -267,8 +323,9 @@ var breaking_exports = /* @__PURE__ */ __exportAll({
|
|
|
267
323
|
paragraphOf: () => paragraphOf,
|
|
268
324
|
raggedness: () => raggedness,
|
|
269
325
|
spread: () => spread,
|
|
270
|
-
totalFit: () => totalFit
|
|
326
|
+
totalFit: () => totalFit,
|
|
327
|
+
worstRatio: () => worstRatio
|
|
271
328
|
});
|
|
272
|
-
export {
|
|
329
|
+
export { paragraphOf as _, worstRatio as a, breakers as c, totalFit as d, adjustmentRatio as f, linesOf as g, lineWidth as h, spread as i, greedy as l, lineStart as m, adjustmentRatios as n, balanced as o, isBreak as p, raggedness as r, balancedBreaker as s, breaking_exports as t, knuthPlass as u };
|
|
273
330
|
|
|
274
331
|
//# sourceMappingURL=breaking.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"breaking.js","names":[],"sources":["../../src/breaking/paragraph.ts","../../src/breaking/breakers.ts","../../src/breaking/measures.ts","../../src/breaking.ts"],"sourcesContent":["/**\n * The paragraph model of the breakers: a list of boxes, glue and penalties, as Knuth and Plass define it. A builder makes\n * the items of a text. The breakers read only the items, so a new rule of break opportunities is a new builder.\n */\n\n/** A width function: the width of a run of text, in any unit. The breakers add the widths and compare them. */\nexport type WidthOf = (text: string) => number;\n\n/** A box: text that the breaker does not break. */\nexport interface Box {\n readonly type: \"box\";\n readonly text: string;\n readonly width: number;\n}\n\n/** 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. */\nexport interface Glue {\n readonly type: \"glue\";\n readonly text: string;\n readonly width: number;\n}\n\n/**\n * A penalty: a break opportunity with a cost. A line that ends at the penalty shows its text, for example a hyphen. A cost of\n * `Infinity` forbids the break, and a cost of `-Infinity` forces it.\n */\nexport interface Penalty {\n readonly type: \"penalty\";\n readonly text: string;\n readonly width: number;\n readonly cost: number;\n /** A flagged penalty is a hyphen. Total fit adds a cost to two flagged breaks in sequence. */\n readonly flagged: boolean;\n}\n\n/** An item of a paragraph. */\nexport type Item = Box | Glue | Penalty;\n\n/** A paragraph: the items in the order of the text. */\nexport interface Paragraph {\n readonly items: readonly Item[];\n}\n\n/** The options of `paragraphOf`. */\nexport interface ParagraphOptions {\n /** The width function of the boxes, the spaces and the hyphens. */\n readonly widthOf: WidthOf;\n /**\n * The hyphenation hook. It gives the offsets in a word where a break with a hyphen is possible. The default gives no\n * offset. The width of each part of a word comes from `widthOf`, so the kerning across an offset is not in the sum.\n */\n readonly hyphenate?: (word: string) => readonly number[];\n /**\n * The hook of the break places inside a word, for example `lineBreaks` of `./segmentation`. It gives offsets in a word where\n * a line can end with no hyphen and no cost. The default gives no offset. This hook wins at an offset that `hyphenate` also gives.\n */\n readonly breaks?: (word: string) => readonly number[];\n /** The text that a line shows at a hyphen break. The default is \"-\". */\n readonly hyphen?: string;\n /** The cost of a hyphen break. The default is 50, as in TeX. */\n readonly hyphenCost?: number;\n}\n\nconst whiteSpace = /\\s+/u;\n\n/**\n * This function makes the paragraph of a text with the break opportunities of CSS `white-space: normal`. Each run of white\n * space becomes one glue of the width of a space. Each word between two runs becomes a box, or boxes and hyphen penalties.\n */\nexport function paragraphOf(text: string, options: ParagraphOptions): Paragraph {\n const { widthOf } = options;\n const hyphen = options.hyphen ?? \"-\";\n const hyphenCost = options.hyphenCost ?? 50;\n const space = widthOf(\" \");\n const hyphenWidth = options.hyphenate === undefined ? 0 : widthOf(hyphen);\n const items: Item[] = [];\n for (const word of text.split(whiteSpace)) {\n if (word === \"\") continue;\n if (items.length > 0) items.push({ type: \"glue\", text: \" \", width: space });\n const free = new Set(options.breaks?.(word) ?? []);\n const offsets = [...new Set([...(options.hyphenate?.(word) ?? []), ...free])].filter((o) => o > 0 && o < word.length).sort((a, b) => a - b);\n let start = 0;\n for (const offset of offsets) {\n const part = word.slice(start, offset);\n items.push({ type: \"box\", text: part, width: widthOf(part) });\n items.push(free.has(offset) ? { type: \"penalty\", text: \"\", width: 0, cost: 0, flagged: false } : { type: \"penalty\", text: hyphen, width: hyphenWidth, cost: hyphenCost, flagged: true });\n start = offset;\n }\n const last = word.slice(start);\n items.push({ type: \"box\", text: last, width: widthOf(last) });\n }\n return { items };\n}\n\n/** A line of a broken paragraph. */\nexport interface Line {\n readonly text: string;\n /** The width of the line: its boxes, the glue between them and the text of a penalty at its end. */\n readonly width: number;\n /** The index of the first item of the line. */\n readonly start: number;\n /** The index after the last item of the line. A break at glue or at a penalty has the index of that item. */\n readonly end: number;\n}\n\n/** This function tells if a line can end at the item `index`: glue after a box, or a penalty with a cost below `Infinity`. */\nexport function isBreak(items: readonly Item[], index: number): boolean {\n const item = items[index]!;\n if (item.type === \"glue\") return index > 0 && items[index - 1]!.type === \"box\";\n return item.type === \"penalty\" && item.cost < Infinity;\n}\n\n/** This function gives the index of the first item of a line after a break at `index`. It skips the glue and the penalties. */\nexport function lineStart(items: readonly Item[], index: number): number {\n let i = index + 1;\n while (i < items.length && items[i]!.type !== \"box\") i++;\n return i;\n}\n\n/** This function gives the width of the line from the item `start` to a break at `end`, with the text of a penalty at `end`. */\nexport function lineWidth(items: readonly Item[], start: number, end: number): number {\n let width = 0;\n for (let i = start; i < end; i++) {\n const item = items[i]!;\n if (item.type !== \"penalty\") width += item.width;\n }\n const last = items[end];\n return last?.type === \"penalty\" ? width + last.width : width;\n}\n\n/**\n * 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.\n * The last line ends at the end of the items.\n */\nexport function linesOf(paragraph: Paragraph, breaks: readonly number[]): Line[] {\n const { items } = paragraph;\n const lines: Line[] = [];\n let start = 0;\n for (const end of [...breaks, items.length]) {\n let text = \"\";\n for (let i = start; i < end; i++) {\n const item = items[i]!;\n if (item.type !== \"penalty\") text += item.text;\n }\n const last = items[end];\n if (last?.type === \"penalty\") text += last.text;\n lines.push({ text, width: lineWidth(items, start, end), start, end });\n start = lineStart(items, end);\n }\n return lines;\n}\n","import { isBreak, lineStart, lineWidth, type Paragraph } from \"./paragraph.ts\";\n\n/**\n * A breaker: a strategy that chooses the breaks of a paragraph for the line width `width`. It gives the indices of the\n * items where the lines end, in order. A breaker reads only the items, so any width function and any builder work with it.\n */\nexport interface Breaker {\n readonly name: string;\n breaks(paragraph: Paragraph, width: number): number[];\n}\n\n/**\n * The greedy breaker: first fit, as a browser with `text-wrap: wrap`. Each line takes the items while they fit in the width.\n * A box that is wider than a line stays whole on its own line.\n */\nexport const greedy: Breaker = {\n name: \"greedy\",\n breaks: (paragraph, width) => greedyBreaks(paragraph, width),\n};\n\n/**\n * This function gives the breaks of the greedy breaker. After each break, it reads the items again from the start of the new\n * line, so that each break opportunity of the new line counts.\n */\nfunction greedyBreaks(paragraph: Paragraph, width: number): number[] {\n const { items } = paragraph;\n const breaks: number[] = [];\n let used = 0;\n let candidate = -1;\n const breakAt = (index: number): number => {\n breaks.push(index);\n used = 0;\n candidate = -1;\n return lineStart(items, index) - 1;\n };\n for (let i = 0; i < items.length; i++) {\n const item = items[i]!;\n if (item.type === \"box\") {\n used += item.width;\n if (used > width && candidate >= 0) i = breakAt(candidate);\n } else if (item.type === \"penalty\" && item.cost === -Infinity) {\n i = breakAt(i);\n } else if (item.type === \"glue\") {\n if (isBreak(items, i)) candidate = i;\n used += item.width;\n } else if (isBreak(items, i) && used + item.width <= width) {\n candidate = i;\n }\n }\n return breaks;\n}\n\n/** The options of the total fit breaker. */\nexport interface TotalFitOptions {\n /** The cost of each line, as `\\linepenalty` of TeX. A larger value gives fewer lines. The default is 10. */\n readonly linePenalty?: number;\n /** The cost of two hyphen breaks in sequence. The default is 3000, as `\\doublehyphendemerits` of TeX. */\n readonly flaggedCost?: number;\n}\n\n/**\n * This function gives the total fit breaker of Knuth and Plass for text with a ragged right edge. It chooses the breaks with\n * the least sum of demerits over all the lines. The demerits of a line are `(linePenalty + 100 r²)²` plus the square of the\n * cost of its penalty. The ratio `r` is the free space of the line divided by the width. The last line has `r = 0`.\n */\nexport function totalFit(options: TotalFitOptions = {}): Breaker {\n const linePenalty = options.linePenalty ?? 10;\n const flaggedCost = options.flaggedCost ?? 3000;\n return {\n name: \"total-fit\",\n breaks: (paragraph, width) => totalFitBreaks(paragraph, width, linePenalty, flaggedCost),\n };\n}\n\n/** The total fit breaker with the default options. */\nexport const knuthPlass: Breaker = totalFit();\n\n/** This function gives the breaks of the total fit breaker, by dynamic programming over the break opportunities. */\nfunction totalFitBreaks(paragraph: Paragraph, width: number, linePenalty: number, flaggedCost: number): number[] {\n const { items } = paragraph;\n // The nodes: the start of the paragraph (-1), each break opportunity, and the end (items.length)\n const nodes = [-1];\n for (let i = 0; i < items.length; i++) if (isBreak(items, i)) nodes.push(i);\n nodes.push(items.length);\n const best = new Array<number>(nodes.length).fill(Infinity);\n const previous = new Array<number>(nodes.length).fill(-1);\n best[0] = 0;\n for (let a = 0; a < nodes.length - 1; a++) {\n if (best[a] === Infinity) continue;\n const start = a === 0 ? 0 : lineStart(items, nodes[a]!);\n const flaggedBefore = a > 0 && isFlagged(paragraph, nodes[a]!);\n for (let b = a + 1; b < nodes.length; b++) {\n const end = nodes[b]!;\n const last = b === nodes.length - 1;\n const used = lineWidth(items, start, end);\n const overflow = used > width;\n // A line wider than the width is possible only when no break opportunity is inside it\n if (overflow && b > a + 1) {\n if (items[end]?.type === \"penalty\") continue;\n break;\n }\n const penalty = items[end];\n const cost = penalty?.type === \"penalty\" ? penalty.cost : 0;\n const ratio = last || overflow ? 0 : (width - used) / width;\n let demerits = (linePenalty + 100 * ratio * ratio) ** 2;\n if (cost > 0) demerits += cost * cost;\n else if (cost > -Infinity && cost < 0) demerits -= cost * cost;\n if (flaggedBefore && isFlagged(paragraph, end)) demerits += flaggedCost;\n const total = best[a]! + demerits;\n if (total < best[b]!) {\n best[b] = total;\n previous[b] = a;\n }\n // A forced break ends each line that comes to it\n if (cost === -Infinity) break;\n }\n }\n const breaks: number[] = [];\n for (let b = previous[nodes.length - 1]!; b > 0; b = previous[b]!) breaks.push(nodes[b]!);\n return breaks.reverse();\n}\n\n/** This function tells if the item `index` is a flagged penalty. */\nfunction isFlagged(paragraph: Paragraph, index: number): boolean {\n const item = paragraph.items[index];\n return item?.type === \"penalty\" && item.flagged;\n}\n\n/** The options of the balanced breaker. */\nexport interface BalancedOptions {\n /** The steps of the search for the width. The default is 32. */\n readonly steps?: number;\n}\n\n/**\n * This function gives the balanced breaker, as a browser with `text-wrap: balance`. It keeps the line count of the greedy\n * breaker, and it finds the least width that gives that count. Thus the lines have more equal widths.\n */\nexport function balancedBreaker(options: BalancedOptions = {}): Breaker {\n const steps = options.steps ?? 32;\n return {\n name: \"balanced\",\n breaks: (paragraph, width) => {\n const count = greedyBreaks(paragraph, width).length;\n let low = 0;\n let high = width;\n for (let step = 0; step < steps; step++) {\n const middle = (low + high) / 2;\n if (greedyBreaks(paragraph, middle).length === count) high = middle;\n else low = middle;\n }\n return greedyBreaks(paragraph, high);\n },\n };\n}\n\n/** The balanced breaker with the default options. */\nexport const balanced: Breaker = balancedBreaker();\n\n/** The breakers of the package, by name. */\nexport const breakers: Readonly<Record<\"greedy\" | \"total-fit\" | \"balanced\", Breaker>> = { greedy, \"total-fit\": knuthPlass, balanced };\n","import type { Line } from \"./paragraph.ts\";\n\n/**\n * This function gives the raggedness of the lines for the width `width`: the mean of `((width - line) / width)²` over the\n * lines before the last line. It is 0 for one line. `@mark1russell7/layout-contract/text` uses the same measure.\n */\nexport function raggedness(lines: readonly Line[], width: number): number {\n if (lines.length < 2) return 0;\n let sum = 0;\n for (let i = 0; i < lines.length - 1; i++) {\n const free = (width - lines[i]!.width) / width;\n sum += free * free;\n }\n return sum / (lines.length - 1);\n}\n\n/** This function gives the spread of the lines: the width of the widest line less the width of the narrowest line, the last line included. */\nexport function spread(lines: readonly Line[]): number {\n if (lines.length === 0) return 0;\n let widest = -Infinity;\n let narrowest = Infinity;\n for (const line of lines) {\n widest = Math.max(widest, line.width);\n narrowest = Math.min(narrowest, line.width);\n }\n return widest - narrowest;\n}\n","export { balanced, balancedBreaker, breakers, greedy, knuthPlass, totalFit, type BalancedOptions, type Breaker, type TotalFitOptions } from \"./breaking/breakers.ts\";\nexport { raggedness, spread } from \"./breaking/measures.ts\";\nexport { 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\";\n"],"mappings":";;AA+DA,MAAM,aAAa;;;;;AAMnB,SAAgB,YAAY,MAAc,SAAsC;CAC9E,MAAM,EAAE,Y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AAQ,SAAS;CAC/B,OAAO;EACL,MAAM;EACN,SAAS,WAAW,UAAU;GAC5B,MAAM,QAAQ,aAAa,WAAW,KAAK,CAAC,CAAC;GAC7C,IAAI,MAAM;GACV,IAAI,OAAO;GACX,KAAK,IAAI,OAAO,GAAG,OAAO,OAAO,QAAQ;IACvC,MAAM,UAAU,MAAM,QAAQ;IAC9B,IAAI,aAAa,WAAW,MAAM,CAAC,CAAC,WAAW,OAAO,OAAO;SACxD,MAAM;GACb;GACA,OAAO,aAAa,WAAW,IAAI;EACrC;CACF;AACF;;AAGA,MAAa,WAAoB,gBAAgB;;AAGjD,MAAa,WAA2E;CAAE;CAAQ,aAAa;CAAY;AAAS;;;;;;AC1JpI,SAAgB,WAAW,OAAwB,OAAuB;CACxE,IAAI,MAAM,SAAS,GAAG,OAAO;CAC7B,IAAI,MAAM;CACV,KAAK,IAAI,IAAI,GAAG,IAAI,MAAM,SAAS,GAAG,KAAK;EACzC,MAAM,QAAQ,QAAQ,MAAM,EAAE,CAAE,SAAS;EACzC,OAAO,OAAO;CAChB;CACA,OAAO,OAAO,MAAM,SAAS;AAC/B;;AAGA,SAAgB,OAAO,OAAgC;CACrD,IAAI,MAAM,WAAW,GAAG,OAAO;CAC/B,IAAI,SAAS;CACb,IAAI,YAAY;CAChB,KAAK,MAAM,QAAQ,OAAO;EACxB,SAAS,KAAK,IAAI,QAAQ,KAAK,KAAK;EACpC,YAAY,KAAK,IAAI,WAAW,KAAK,KAAK;CAC5C;CACA,OAAO,SAAS;AAClB"}
|
|
1
|
+
{"version":3,"file":"breaking.js","names":[],"sources":["../../src/breaking/paragraph.ts","../../src/breaking/breakers.ts","../../src/breaking/measures.ts","../../src/breaking.ts"],"sourcesContent":["/**\n * The paragraph model of the breakers: a list of boxes, glue and penalties, as Knuth and Plass define it. A builder makes\n * the items of a text. The breakers read only the items, so a new rule of break opportunities is a new builder.\n */\n\n/** A width function: the width of a run of text, in any unit. The breakers add the widths and compare them. */\nexport type WidthOf = (text: string) => number;\n\n/** A box: text that the breaker does not break. */\nexport interface Box {\n readonly type: \"box\";\n readonly text: string;\n readonly width: number;\n}\n\n/**\n * 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\n * justified line sets each glue to its width plus a part of its stretch, or less a part of its shrink.\n */\nexport interface Glue {\n readonly type: \"glue\";\n readonly text: string;\n readonly width: number;\n /** The width that the glue can add to a justified line. The default is 0. */\n readonly stretch?: number;\n /** The width that the glue can take from a justified line, at most its width. The default is 0. */\n readonly shrink?: number;\n}\n\n/**\n * A penalty: a break opportunity with a cost. A line that ends at the penalty shows its text, for example a hyphen. A cost of\n * `Infinity` forbids the break, and a cost of `-Infinity` forces it.\n */\nexport interface Penalty {\n readonly type: \"penalty\";\n readonly text: string;\n readonly width: number;\n readonly cost: number;\n /** A flagged penalty is a hyphen. Total fit adds a cost to two flagged breaks in sequence. */\n readonly flagged: boolean;\n}\n\n/** An item of a paragraph. */\nexport type Item = Box | Glue | Penalty;\n\n/** A paragraph: the items in the order of the text. */\nexport interface Paragraph {\n readonly items: readonly Item[];\n}\n\n/** The options of `paragraphOf`. */\nexport interface ParagraphOptions {\n /** The width function of the boxes, the spaces and the hyphens. */\n readonly widthOf: WidthOf;\n /**\n * The hyphenation hook. It gives the offsets in a word where a break with a hyphen is possible. The default gives no\n * offset. The width of each part of a word comes from `widthOf`, so the kerning across an offset is not in the sum.\n */\n readonly hyphenate?: (word: string) => readonly number[];\n /**\n * The hook of the break places inside a word, for example `lineBreaks` of `./segmentation`. It gives offsets in a word where\n * a line can end with no hyphen and no cost. The default gives no offset. This hook wins at an offset that `hyphenate` also gives.\n */\n readonly breaks?: (word: string) => readonly number[];\n /** The text that a line shows at a hyphen break. The default is \"-\". */\n readonly hyphen?: string;\n /** The cost of a hyphen break. The default is 50, as in TeX. */\n readonly hyphenCost?: number;\n /** The stretch of each glue, in the unit of `widthOf`. The default is 0. TeX uses a half of a space. */\n readonly stretch?: number;\n /** The shrink of each glue, in the unit of `widthOf`. The default is 0. TeX uses a third of a space. */\n readonly shrink?: number;\n}\n\nconst whiteSpace = /\\s+/u;\n\n/**\n * This function makes the paragraph of a text with the break opportunities of CSS `white-space: normal`. Each run of white\n * space becomes one glue of the width of a space. Each word between two runs becomes a box, or boxes and hyphen penalties.\n */\nexport function paragraphOf(text: string, options: ParagraphOptions): Paragraph {\n const { widthOf } = options;\n const hyphen = options.hyphen ?? \"-\";\n const hyphenCost = options.hyphenCost ?? 50;\n const space = widthOf(\" \");\n const hyphenWidth = options.hyphenate === undefined ? 0 : widthOf(hyphen);\n const { stretch = 0, shrink = 0 } = options;\n const glue: Glue = stretch > 0 || shrink > 0 ? { type: \"glue\", text: \" \", width: space, stretch, shrink } : { type: \"glue\", text: \" \", width: space };\n const items: Item[] = [];\n for (const word of text.split(whiteSpace)) {\n if (word === \"\") continue;\n if (items.length > 0) items.push(glue);\n const free = new Set(options.breaks?.(word) ?? []);\n const offsets = [...new Set([...(options.hyphenate?.(word) ?? []), ...free])].filter((o) => o > 0 && o < word.length).sort((a, b) => a - b);\n let start = 0;\n for (const offset of offsets) {\n const part = word.slice(start, offset);\n items.push({ type: \"box\", text: part, width: widthOf(part) });\n items.push(free.has(offset) ? { type: \"penalty\", text: \"\", width: 0, cost: 0, flagged: false } : { type: \"penalty\", text: hyphen, width: hyphenWidth, cost: hyphenCost, flagged: true });\n start = offset;\n }\n const last = word.slice(start);\n items.push({ type: \"box\", text: last, width: widthOf(last) });\n }\n return { items };\n}\n\n/** A line of a broken paragraph. */\nexport interface Line {\n readonly text: string;\n /** The width of the line: its boxes, the glue between them and the text of a penalty at its end. */\n readonly width: number;\n /** The index of the first item of the line. */\n readonly start: number;\n /** The index after the last item of the line. A break at glue or at a penalty has the index of that item. */\n readonly end: number;\n}\n\n/** This function tells if a line can end at the item `index`: glue after a box, or a penalty with a cost below `Infinity`. */\nexport function isBreak(items: readonly Item[], index: number): boolean {\n const item = items[index]!;\n if (item.type === \"glue\") return index > 0 && items[index - 1]!.type === \"box\";\n return item.type === \"penalty\" && item.cost < Infinity;\n}\n\n/** This function gives the index of the first item of a line after a break at `index`. It skips the glue and the penalties. */\nexport function lineStart(items: readonly Item[], index: number): number {\n let i = index + 1;\n while (i < items.length && items[i]!.type !== \"box\") i++;\n return i;\n}\n\n/** This function gives the width of the line from the item `start` to a break at `end`, with the text of a penalty at `end`. */\nexport function lineWidth(items: readonly Item[], start: number, end: number): number {\n let width = 0;\n for (let i = start; i < end; i++) {\n const item = items[i]!;\n if (item.type !== \"penalty\") width += item.width;\n }\n const last = items[end];\n return last?.type === \"penalty\" ? width + last.width : width;\n}\n\n/**\n * This function gives the adjustment ratio of Knuth and Plass for the line from the item `start` to a break at `end`. The\n * 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\n * needs more shrink than the line has. The ratio is infinite when the line needs stretch or shrink and has none.\n */\nexport function adjustmentRatio(items: readonly Item[], start: number, end: number, width: number): number {\n const free = width - lineWidth(items, start, end);\n if (free === 0) return 0;\n let room = 0;\n for (let i = start; i < end; i++) {\n const item = items[i]!;\n if (item.type === \"glue\") room += (free > 0 ? item.stretch : item.shrink) ?? 0;\n }\n return room > 0 ? free / room : free * Infinity;\n}\n\n/**\n * 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.\n * The last line ends at the end of the items.\n */\nexport function linesOf(paragraph: Paragraph, breaks: readonly number[]): Line[] {\n const { items } = paragraph;\n const lines: Line[] = [];\n let start = 0;\n for (const end of [...breaks, items.length]) {\n let text = \"\";\n for (let i = start; i < end; i++) {\n const item = items[i]!;\n if (item.type !== \"penalty\") text += item.text;\n }\n const last = items[end];\n if (last?.type === \"penalty\") text += last.text;\n lines.push({ text, width: lineWidth(items, start, end), start, end });\n start = lineStart(items, end);\n }\n return lines;\n}\n","import { adjustmentRatio, isBreak, lineStart, lineWidth, type Paragraph } from \"./paragraph.ts\";\n\n/**\n * A breaker: a strategy that chooses the breaks of a paragraph for the line width `width`. It gives the indices of the\n * items where the lines end, in order. A breaker reads only the items, so any width function and any builder work with it.\n */\nexport interface Breaker {\n readonly name: string;\n breaks(paragraph: Paragraph, width: number): number[];\n}\n\n/**\n * The greedy breaker: first fit, as a browser with `text-wrap: wrap`. Each line takes the items while they fit in the width.\n * A box that is wider than a line stays whole on its own line.\n */\nexport const greedy: Breaker = {\n name: \"greedy\",\n breaks: (paragraph, width) => greedyBreaks(paragraph, width),\n};\n\n/**\n * This function gives the breaks of the greedy breaker. After each break, it reads the items again from the start of the new\n * line, so that each break opportunity of the new line counts.\n */\nfunction greedyBreaks(paragraph: Paragraph, width: number): number[] {\n const { items } = paragraph;\n const breaks: number[] = [];\n let used = 0;\n let candidate = -1;\n const breakAt = (index: number): number => {\n breaks.push(index);\n used = 0;\n candidate = -1;\n return lineStart(items, index) - 1;\n };\n for (let i = 0; i < items.length; i++) {\n const item = items[i]!;\n if (item.type === \"box\") {\n used += item.width;\n if (used > width && candidate >= 0) i = breakAt(candidate);\n } else if (item.type === \"penalty\" && item.cost === -Infinity) {\n i = breakAt(i);\n } else if (item.type === \"glue\") {\n if (isBreak(items, i)) candidate = i;\n used += item.width;\n } else if (isBreak(items, i) && used + item.width <= width) {\n candidate = i;\n }\n }\n return breaks;\n}\n\n/** The options of the total fit breaker. */\nexport interface TotalFitOptions {\n /** The cost of each line, as `\\linepenalty` of TeX. A larger value gives fewer lines. The default is 10. */\n readonly linePenalty?: number;\n /** The cost of two hyphen breaks in sequence. The default is 3000, as `\\doublehyphendemerits` of TeX. */\n readonly flaggedCost?: number;\n}\n\n/**\n * This function gives the total fit breaker of Knuth and Plass. It chooses the breaks with the least sum of demerits over all\n * the lines. A paragraph with no stretch and no shrink has a ragged right edge. The demerits of a line are then\n * `(linePenalty + 100 r²)²` plus the square of the cost of its penalty. The ratio `r` is the free space divided by the width.\n * The last line has `r = 0`.\n *\n * A paragraph with glue that has stretch or shrink has a justified edge. The demerits then use `100 |r|³` with the ratio of\n * `adjustmentRatio`, and the last line does not stretch. A line that needs more shrink than it has counts first. Thus the\n * breaker keeps such a line only when each set of breaks has one.\n */\nexport function totalFit(options: TotalFitOptions = {}): Breaker {\n const linePenalty = options.linePenalty ?? 10;\n const flaggedCost = options.flaggedCost ?? 3000;\n return {\n name: \"total-fit\",\n breaks: (paragraph, width) => totalFitBreaks(paragraph, width, linePenalty, flaggedCost),\n };\n}\n\n/** The total fit breaker with the default options. */\nexport const knuthPlass: Breaker = totalFit();\n\n/** The largest badness of a justified line. An infinite ratio gets this value, so the sum of the demerits stays finite. */\nconst worstBadness = 1e8;\n\n/** This function gives the breaks of the total fit breaker, by dynamic programming over the break opportunities. */\nfunction totalFitBreaks(paragraph: Paragraph, width: number, linePenalty: number, flaggedCost: number): number[] {\n const { items } = paragraph;\n // The nodes: the start of the paragraph (-1), each break opportunity, and the end (items.length)\n const nodes = [-1];\n for (let i = 0; i < items.length; i++) if (isBreak(items, i)) nodes.push(i);\n nodes.push(items.length);\n const best = new Array<number>(nodes.length).fill(Infinity);\n const previous = new Array<number>(nodes.length).fill(-1);\n const justified = items.some((item) => item.type === \"glue\" && ((item.stretch ?? 0) > 0 || (item.shrink ?? 0) > 0));\n // The count of lines over their shrink comes before the demerits. It stays 0 for a ragged edge.\n const over = new Array<number>(nodes.length).fill(justified ? Infinity : 0);\n over[0] = 0;\n best[0] = 0;\n for (let a = 0; a < nodes.length - 1; a++) {\n if (best[a] === Infinity) continue;\n const start = a === 0 ? 0 : lineStart(items, nodes[a]!);\n const flaggedBefore = a > 0 && isFlagged(paragraph, nodes[a]!);\n for (let b = a + 1; b < nodes.length; b++) {\n const end = nodes[b]!;\n const last = b === nodes.length - 1;\n const used = lineWidth(items, start, end);\n const fit = justified ? adjustmentRatio(items, start, end, width) : 0;\n const overflow = justified ? fit < -1 : used > width;\n // A line wider than the width is possible only when no break opportunity is inside it\n if (overflow && b > a + 1) {\n if (items[end]?.type === \"penalty\") continue;\n break;\n }\n const penalty = items[end];\n const cost = penalty?.type === \"penalty\" ? penalty.cost : 0;\n const ratio = last || overflow ? 0 : (width - used) / width;\n const badness = justified ? Math.min(100 * Math.abs(last ? Math.min(fit, 0) : fit) ** 3, worstBadness) : 100 * ratio * ratio;\n let demerits = (linePenalty + badness) ** 2;\n if (cost > 0) demerits += cost * cost;\n else if (cost > -Infinity && cost < 0) demerits -= cost * cost;\n if (flaggedBefore && isFlagged(paragraph, end)) demerits += flaggedCost;\n const total = best[a]! + demerits;\n const count = over[a]! + (justified && overflow ? 1 : 0);\n if (count < over[b]! || (count === over[b]! && total < best[b]!)) {\n over[b] = count;\n best[b] = total;\n previous[b] = a;\n }\n // A forced break ends each line that comes to it\n if (cost === -Infinity) break;\n }\n }\n const breaks: number[] = [];\n for (let b = previous[nodes.length - 1]!; b > 0; b = previous[b]!) breaks.push(nodes[b]!);\n return breaks.reverse();\n}\n\n/** This function tells if the item `index` is a flagged penalty. */\nfunction isFlagged(paragraph: Paragraph, index: number): boolean {\n const item = paragraph.items[index];\n return item?.type === \"penalty\" && item.flagged;\n}\n\n/** The options of the balanced breaker. */\nexport interface BalancedOptions {\n /** The steps of the search for the width. The default is 32. */\n readonly steps?: number;\n}\n\n/**\n * This function gives the balanced breaker, as a browser with `text-wrap: balance`. It keeps the line count of the greedy\n * breaker, and it finds the least width that gives that count. Thus the lines have more equal widths.\n */\nexport function balancedBreaker(options: BalancedOptions = {}): Breaker {\n const steps = options.steps ?? 32;\n return {\n name: \"balanced\",\n breaks: (paragraph, width) => {\n const count = greedyBreaks(paragraph, width).length;\n let low = 0;\n let high = width;\n for (let step = 0; step < steps; step++) {\n const middle = (low + high) / 2;\n if (greedyBreaks(paragraph, middle).length === count) high = middle;\n else low = middle;\n }\n return greedyBreaks(paragraph, high);\n },\n };\n}\n\n/** The balanced breaker with the default options. */\nexport const balanced: Breaker = balancedBreaker();\n\n/** The breakers of the package, by name. */\nexport const breakers: Readonly<Record<\"greedy\" | \"total-fit\" | \"balanced\", Breaker>> = { greedy, \"total-fit\": knuthPlass, balanced };\n","import { adjustmentRatio, type Line, type Paragraph } from \"./paragraph.ts\";\n\n/**\n * This function gives the raggedness of the lines for the width `width`: the mean of `((width - line) / width)²` over the\n * lines before the last line. It is 0 for one line. `@mark1russell7/layout-contract/text` uses the same measure.\n */\nexport function raggedness(lines: readonly Line[], width: number): number {\n if (lines.length < 2) return 0;\n let sum = 0;\n for (let i = 0; i < lines.length - 1; i++) {\n const free = (width - lines[i]!.width) / width;\n sum += free * free;\n }\n return sum / (lines.length - 1);\n}\n\n/** This function gives the spread of the lines: the width of the widest line less the width of the narrowest line, the last line included. */\nexport function spread(lines: readonly Line[]): number {\n if (lines.length === 0) return 0;\n let widest = -Infinity;\n let narrowest = Infinity;\n for (const line of lines) {\n widest = Math.max(widest, line.width);\n narrowest = Math.min(narrowest, line.width);\n }\n return widest - narrowest;\n}\n\n/**\n * This function gives the adjustment ratio of each line for the width `width`. A renderer sets each glue of a line with a\n * ratio `r` to its width plus `r` times its stretch. When `r` is negative, the glue gets its width plus `r` times its shrink.\n * The last line does not stretch, so its ratio is not more than 0.\n */\nexport function adjustmentRatios(paragraph: Paragraph, lines: readonly Line[], width: number): number[] {\n return lines.map((line, index) => {\n const ratio = adjustmentRatio(paragraph.items, line.start, line.end, width);\n return index === lines.length - 1 ? Math.min(ratio, 0) : ratio;\n });\n}\n\n/**\n * This function gives the measure of the justified edge: the largest absolute adjustment ratio of the lines. A value of 1 or\n * less means that the glue of each line reaches the edge within its stretch and its shrink.\n */\nexport function worstRatio(paragraph: Paragraph, lines: readonly Line[], width: number): number {\n return Math.max(0, ...adjustmentRatios(paragraph, lines, width).map(Math.abs));\n}\n","export { balanced, balancedBreaker, breakers, greedy, knuthPlass, totalFit, type BalancedOptions, type Breaker, type TotalFitOptions } from \"./breaking/breakers.ts\";\nexport { adjustmentRatios, raggedness, spread, worstRatio } from \"./breaking/measures.ts\";\nexport { adjustmentRatio, isBreak, lineStart, lineWidth, linesOf, paragraphOf, type Box, type Glue, type Item, type Line, type Paragraph, type ParagraphOptions, type Penalty, type Wid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E"}
|
package/build/chunks/font.d.ts
CHANGED
|
@@ -201,7 +201,7 @@ interface MeasureOptions {
|
|
|
201
201
|
readonly indic?: boolean;
|
|
202
202
|
/**
|
|
203
203
|
* The shaping of the browser to model. `dom` (the default) shapes the words of `shapingWords` and then kerns all the
|
|
204
|
-
* text. `canvas` shapes and kerns each
|
|
204
|
+
* text. `canvas` shapes and kerns each segment of `canvasRuns` alone, as Blink does for the text of a canvas.
|
|
205
205
|
*/
|
|
206
206
|
readonly shaping?: "dom" | "canvas";
|
|
207
207
|
/**
|
|
@@ -218,8 +218,9 @@ declare function shapingWords(text: string): string[];
|
|
|
218
218
|
/**
|
|
219
219
|
* This function gives the glyphs of a text and their advances in font units. With the shaping `dom`, it applies the GSUB
|
|
220
220
|
* substitutions to each word of `shapingWords`, and then the pair kerning to all the text. With the shaping `canvas`, it
|
|
221
|
-
* applies the substitutions and the kerning to each
|
|
222
|
-
* the joining forms of each word, in the stages of `joiningStages`. A script of
|
|
221
|
+
* applies the substitutions and the kerning to each segment of `canvasRuns` alone, after the changes of `canvasText`. A
|
|
222
|
+
* script of `joiningScripts` also gets the joining forms of each word, in the stages of `joiningStages`. A script of
|
|
223
|
+
* `indicScripts` uses `indicShaper`.
|
|
223
224
|
*/
|
|
224
225
|
declare function glyphAdvances(font: Font, text: string, options?: MeasureOptions): {
|
|
225
226
|
glyphs: number[];
|
|
@@ -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;;;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
|
|
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;;;;;;;;iBAyFb,cAAc,MAAM,MAAM,cAAc,UAAS;EAAwB;EAAkB;;;;;;iBAwG3F,aAAa,MAAM,MAAM,cAAc,cAAc,UAAS"}
|
package/build/chunks/font.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as visualOrder, o as bracketOf, r as resolveLevels, t as icuDirection } from "./resolve.js";
|
|
2
2
|
import { fnv1a32 } from "@mark1russell7/rigor-generators";
|
|
3
3
|
// src/fonts/tables.ts
|
|
4
4
|
/** This function reads the header fields from `head`, `hhea` and `maxp`. */
|
|
@@ -3297,11 +3297,19 @@ function bidiLevels(text) {
|
|
|
3297
3297
|
else level = levels[i];
|
|
3298
3298
|
return levels;
|
|
3299
3299
|
}
|
|
3300
|
+
/**
|
|
3301
|
+
* This function tells if ICU gives the direction `mixed` to a text in a paragraph with the base level 0. Blink divides
|
|
3302
|
+
* 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.
|
|
3303
|
+
*/
|
|
3304
|
+
function isMixed(text) {
|
|
3305
|
+
return icuDirection([...text].map(bidiClass), 0) === "mixed";
|
|
3306
|
+
}
|
|
3300
3307
|
// src/fonts/words.ts
|
|
3301
3308
|
/**
|
|
3302
3309
|
* The words of the text of a canvas. Blink shapes the text of `fillText` and `measureText` one word at a time, so no GSUB
|
|
3303
3310
|
* context and no pair kerning goes across two words. This module follows `NextWordEndIndex` of
|
|
3304
|
-
* `third_party/blink/renderer/platform/fonts/plain_text_node.cc
|
|
3311
|
+
* `third_party/blink/renderer/platform/fonts/plain_text_node.cc`, the CJK table of `character_property_data.h` and
|
|
3312
|
+
* `TreatAsZeroWidthSpaceInComplexScriptLegacy` of `character.h`.
|
|
3305
3313
|
* The Chromium Authors wrote these files under a BSD-style license.
|
|
3306
3314
|
*/
|
|
3307
3315
|
/** The single code points of the CJK table of Blink. */
|
|
@@ -3547,33 +3555,60 @@ function isCjkIdeographOrSymbol(codePoint) {
|
|
|
3547
3555
|
function isDelimiter(character) {
|
|
3548
3556
|
return character === " " || character === " " || character === "";
|
|
3549
3557
|
}
|
|
3558
|
+
/** The space characters of the canvas text preparation: U+0020, U+0009, U+000A, U+000C and U+000D. */
|
|
3559
|
+
const canvasSpaces = /[ \t\n\f\r]/u;
|
|
3560
|
+
/** The characters of `TreatAsZeroWidthSpaceInComplexScriptLegacy` in Blink that are not canvas spaces. */
|
|
3561
|
+
const zeroWidthSpaceLike = /[-]/u;
|
|
3562
|
+
/**
|
|
3563
|
+
* This function gives the character that Blink shapes in place of the character `character` of a canvas text. A canvas
|
|
3564
|
+
* 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
|
|
3565
|
+
* that ends a word.
|
|
3566
|
+
*/
|
|
3567
|
+
function canvasCharacter(character) {
|
|
3568
|
+
if (canvasSpaces.test(character)) return " ";
|
|
3569
|
+
return zeroWidthSpaceLike.test(character) ? "" : character;
|
|
3570
|
+
}
|
|
3571
|
+
/**
|
|
3572
|
+
* This function gives the text that Blink shapes for the text of a canvas, as `NormalizeSpacesAndMaybeBidi` of
|
|
3573
|
+
* `plain_text_node.cc` gives it. It changes only characters of the Basic Multilingual Plane, so the length stays.
|
|
3574
|
+
*/
|
|
3575
|
+
function canvasText(text) {
|
|
3576
|
+
return text.replace(/[ \t\n\f\r-]/gu, canvasCharacter);
|
|
3577
|
+
}
|
|
3550
3578
|
/**
|
|
3551
3579
|
* This function divides the text of a canvas into the words that Blink shapes one at a time. Each space, tab and zero
|
|
3552
|
-
* width space is a word alone. Each
|
|
3553
|
-
* Common after it.
|
|
3580
|
+
* width space is a word alone. Each character that `canvasText` changes into one of them is also a word alone. Each CJK
|
|
3581
|
+
* character starts a word, with its marks and the CJK characters of the script Common after it.
|
|
3554
3582
|
*/
|
|
3555
3583
|
function canvasWords(text) {
|
|
3556
3584
|
const characters = [...text];
|
|
3585
|
+
const shaped = characters.map(canvasCharacter);
|
|
3557
3586
|
const words = [];
|
|
3558
3587
|
let start = 0;
|
|
3559
3588
|
while (start < characters.length) {
|
|
3560
|
-
const end = wordEnd(
|
|
3589
|
+
const end = wordEnd(shaped, start);
|
|
3561
3590
|
words.push(characters.slice(start, end).join(""));
|
|
3562
3591
|
start = end;
|
|
3563
3592
|
}
|
|
3564
3593
|
return words;
|
|
3565
3594
|
}
|
|
3566
3595
|
/**
|
|
3567
|
-
* This function gives the segments of the text of a canvas in the
|
|
3568
|
-
* part of a word where the bidi level changes. Blink
|
|
3596
|
+
* This function gives the segments of the text of a canvas and their bidi levels, in the logical order. A segment is a word
|
|
3597
|
+
* of `canvasWords`, or a part of a word where the bidi level changes. Blink divides a text into bidi runs only when ICU
|
|
3598
|
+
* gives the paragraph the direction `mixed`. Else each segment is a word at the level 0.
|
|
3569
3599
|
*/
|
|
3570
|
-
function
|
|
3600
|
+
function canvasRuns(text) {
|
|
3601
|
+
const words = canvasWords(text);
|
|
3602
|
+
if (!isMixed(text)) return {
|
|
3603
|
+
segments: words,
|
|
3604
|
+
levels: words.map(() => 0)
|
|
3605
|
+
};
|
|
3571
3606
|
const characters = [...text];
|
|
3572
3607
|
const levels = bidiLevels(text);
|
|
3573
3608
|
const segments = [];
|
|
3574
3609
|
const segmentLevels = [];
|
|
3575
3610
|
let at = 0;
|
|
3576
|
-
for (const word of
|
|
3611
|
+
for (const word of words) {
|
|
3577
3612
|
const end = at + [...word].length;
|
|
3578
3613
|
let start = at;
|
|
3579
3614
|
for (let i = at + 1; i <= end; i++) {
|
|
@@ -3584,7 +3619,18 @@ function canvasSegments(text) {
|
|
|
3584
3619
|
}
|
|
3585
3620
|
at = end;
|
|
3586
3621
|
}
|
|
3587
|
-
return
|
|
3622
|
+
return {
|
|
3623
|
+
segments,
|
|
3624
|
+
levels: segmentLevels
|
|
3625
|
+
};
|
|
3626
|
+
}
|
|
3627
|
+
/**
|
|
3628
|
+
* This function gives the segments of `canvasRuns` in the visual order. Blink shapes each bidi run alone and adds the
|
|
3629
|
+
* widths of the words in this order.
|
|
3630
|
+
*/
|
|
3631
|
+
function canvasSegments(text) {
|
|
3632
|
+
const { segments, levels } = canvasRuns(text);
|
|
3633
|
+
return visualOrder(segments, levels);
|
|
3588
3634
|
}
|
|
3589
3635
|
/** This function gives the index after the last character of the word that starts at `start`. */
|
|
3590
3636
|
function wordEnd(characters, start) {
|
|
@@ -3877,8 +3923,9 @@ function shaperOf(font, options) {
|
|
|
3877
3923
|
/**
|
|
3878
3924
|
* This function gives the glyphs of a text and their advances in font units. With the shaping `dom`, it applies the GSUB
|
|
3879
3925
|
* substitutions to each word of `shapingWords`, and then the pair kerning to all the text. With the shaping `canvas`, it
|
|
3880
|
-
* applies the substitutions and the kerning to each
|
|
3881
|
-
* the joining forms of each word, in the stages of `joiningStages`. A script of
|
|
3926
|
+
* applies the substitutions and the kerning to each segment of `canvasRuns` alone, after the changes of `canvasText`. A
|
|
3927
|
+
* script of `joiningScripts` also gets the joining forms of each word, in the stages of `joiningStages`. A script of
|
|
3928
|
+
* `indicScripts` uses `indicShaper`.
|
|
3882
3929
|
*/
|
|
3883
3930
|
function glyphAdvances(font, text, options = {}) {
|
|
3884
3931
|
const { glyphs, advances } = shapedAdvances(font, text, options);
|
|
@@ -3893,8 +3940,8 @@ function shapedAdvances(font, text, options) {
|
|
|
3893
3940
|
const canvas = options.shaping === "canvas";
|
|
3894
3941
|
const glyphs = [];
|
|
3895
3942
|
const advances = [];
|
|
3896
|
-
for (const word of canvas ?
|
|
3897
|
-
const wordGlyphs = substitute(word);
|
|
3943
|
+
for (const word of canvas ? canvasRuns(text).segments : shapingWords(text)) {
|
|
3944
|
+
const wordGlyphs = substitute(canvas ? canvasText(word) : word);
|
|
3898
3945
|
const wordAdvances = wordGlyphs.map((glyph) => font.advance(glyph));
|
|
3899
3946
|
if (canvas) kern(font, kerning, wordGlyphs, wordAdvances);
|
|
3900
3947
|
glyphs.push(...wordGlyphs);
|
|
@@ -3964,7 +4011,7 @@ function float32Width(font, text, size, options) {
|
|
|
3964
4011
|
const pxPerUnit = size / font.unitsPerEm;
|
|
3965
4012
|
let total = 0;
|
|
3966
4013
|
for (const segment of canvasSegments(text)) {
|
|
3967
|
-
const glyphs = substitute(segment);
|
|
4014
|
+
const glyphs = substitute(canvasText(segment));
|
|
3968
4015
|
const positions = glyphs.map((glyph) => Math.trunc(Math.fround(font.advance(glyph) * pxPerUnit) * 65536));
|
|
3969
4016
|
kern(font, kerning, glyphs, glyphs.map((glyph) => font.advance(glyph)), positions, scale);
|
|
3970
4017
|
let units = 0;
|