@kerf-js/text 0.0.0-stage → 0.1.1-nightly.20261112.43
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/LICENSE +38 -0
- package/README.md +442 -2
- package/dist/LICENSE +38 -0
- package/dist/LICENSE-pretext +229 -0
- package/dist/blink/chunk-3C7LNDJ7.js +1 -0
- package/dist/blink/chunk-JHGH3V56.js +1 -0
- package/dist/blink/chunk-M5EVTPT6.js +1 -0
- package/dist/blink/chunk-UDC5PTIR.js +1 -0
- package/dist/blink/chunk-V6DTG3Y5.js +1 -0
- package/dist/blink/chunk-VQNXIKM2.js +1 -0
- package/dist/blink/engine.js +1 -0
- package/dist/blink/exact-KWC4AM4U.js +1 -0
- package/dist/blink/meta.json +1 -0
- package/dist/blink/pack-complex-VY4X2C44.js +1 -0
- package/dist/blink/pack-families-E2YTVA25.js +0 -0
- package/dist/blink/pack-rare-3377IVPF.js +0 -0
- package/dist/blink/pack-scripts-AK6ASBB3.js +1 -0
- package/dist/blink/pack-spacing-C3EMXVNL.js +1 -0
- package/dist/gecko/chunk-3DZRMKQM.js +1 -0
- package/dist/gecko/chunk-5JKLUU2D.js +1 -0
- package/dist/gecko/chunk-R42Y4PIV.js +1 -0
- package/dist/gecko/chunk-VVDGDNRD.js +1 -0
- package/dist/gecko/engine.js +1 -0
- package/dist/gecko/meta.json +1 -0
- package/dist/gecko/pack-complex-SKP6QV5T.js +1 -0
- package/dist/gecko/pack-families-34KO3CLI.js +0 -0
- package/dist/gecko/pack-rare-FDYU357Z.js +0 -0
- package/dist/gecko/pack-scripts-6W5UKVN6.js +1 -0
- package/dist/gecko/pack-spacing-TDCSZXXA.js +1 -0
- package/dist/index.js +1 -0
- package/dist/webkit/chunk-6T4C2AOY.js +1 -0
- package/dist/webkit/chunk-FEELTLSH.js +1 -0
- package/dist/webkit/chunk-J5HZIYT7.js +1 -0
- package/dist/webkit/chunk-M5EVTPT6.js +1 -0
- package/dist/webkit/chunk-PDPZEANK.js +1 -0
- package/dist/webkit/chunk-QWUJTFZO.js +1 -0
- package/dist/webkit/chunk-WYD7FD2R.js +1 -0
- package/dist/webkit/engine.js +1 -0
- package/dist/webkit/exact-CCIFYWVP.js +1 -0
- package/dist/webkit/meta.json +1 -0
- package/dist/webkit/pack-complex-BCS55GNV.js +1 -0
- package/dist/webkit/pack-families-FLTRJH5J.js +1 -0
- package/dist/webkit/pack-rare-SSJ2B7KP.js +0 -0
- package/dist/webkit/pack-scripts-QS74TJNY.js +1 -0
- package/dist/webkit/pack-spacing-CKKMIPWL.js +1 -0
- package/package.json +59 -3
- package/plugin/build.mjs +363 -0
- package/plugin/glyphs.mjs +94 -0
- package/plugin/index.d.ts +55 -0
- package/plugin/index.mjs +181 -0
- package/plugin/measure.mjs +21 -0
- package/vendor/pretext/LICENSE +229 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ravonus
|
|
4
|
+
|
|
5
|
+
This package is a derived build of Pretext (@chenglou/pretext 1.0.0) by Cheng Lou and the Pretext
|
|
6
|
+
contributors, https://github.com/chenglou/pretext, used under its MIT licence. Pretext does the real work:
|
|
7
|
+
its ports of Blink's, WebKit's and Gecko's line breaking and its measurement rules are what this package
|
|
8
|
+
ships, specialised per engine and split into lazy chunks. vendor/pretext/ holds the unmodified source and
|
|
9
|
+
its LICENSE, with the notices of the Chromium, WebKit, ICU, ICU4X and Unicode sources it follows or is
|
|
10
|
+
generated from. Those notices apply to gen/ and dist/ too, and dist/LICENSE-pretext carries them.
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
|
|
30
|
+
------------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
Pretext, vendored in vendor/pretext/ and the source of gen/ and dist/:
|
|
33
|
+
|
|
34
|
+
MIT License
|
|
35
|
+
|
|
36
|
+
Copyright (c) 2026 Pretext contributors
|
|
37
|
+
|
|
38
|
+
The full text, with its third-party notices, is vendor/pretext/LICENSE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,443 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://github.com/Ravonus/kerf">
|
|
3
|
+
<img src="https://raw.githubusercontent.com/Ravonus/kerf/main/site/public/logo.svg" width="88" height="88" alt="Kerf">
|
|
4
|
+
</a>
|
|
5
|
+
</p>
|
|
2
6
|
|
|
3
|
-
|
|
7
|
+
<h1 align="center">@kerf-js/text</h1>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<strong>Fonts declared once, subset and fallback-matched at build time, and exact line breaking in the browser.</strong><br>
|
|
11
|
+
A slim, per-engine build of Pretext measures text by arithmetic instead of DOM reflow.
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
<a href="https://www.npmjs.com/package/@kerf-js/text"><img src="https://img.shields.io/npm/v/@kerf-js/text?color=145c3c&label=npm" alt="npm version"></a>
|
|
16
|
+
<a href="https://github.com/Ravonus/kerf/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-145c3c" alt="MIT license"></a>
|
|
17
|
+
<a href="https://github.com/Ravonus/kerf/actions"><img src="https://img.shields.io/github/actions/workflow/status/Ravonus/kerf/quality.yml?branch=main&label=checks" alt="checks"></a>
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
<p align="center">
|
|
21
|
+
<a href="#quick-start">Quick start</a> ·
|
|
22
|
+
<a href="https://github.com/Ravonus/kerf/tree/main/docs">Docs</a> ·
|
|
23
|
+
<a href="https://github.com/Ravonus/kerf/tree/main/examples">Examples</a> ·
|
|
24
|
+
<a href="https://github.com/Ravonus/kerf/blob/main/CHANGELOG.md">Changelog</a> ·
|
|
25
|
+
<a href="https://github.com/Ravonus/kerf">GitHub</a>
|
|
26
|
+
</p>
|
|
27
|
+
|
|
28
|
+
> [!IMPORTANT]
|
|
29
|
+
> **Early release (0.1.x).** APIs can change between minor versions. Pin the version and read the
|
|
30
|
+
> [changelog](https://github.com/Ravonus/kerf/blob/main/CHANGELOG.md) before you upgrade.
|
|
31
|
+
|
|
32
|
+
Fonts and text for Kerf. An app declares its fonts once, in `text()` in `kerf.config.ts`. The build subsets each face,
|
|
33
|
+
writes a metric-matched fallback face so the swap doesn't shift layout, and preloads what's above the fold. The same
|
|
34
|
+
plugin provides `text.measure`, the line breaking that canvas text and exact row heights need, loaded only when
|
|
35
|
+
something measures.
|
|
36
|
+
|
|
37
|
+
The package has four entries. `text()` (the main entry) is the plugin, `@kerf-js/text/build` is the Node-only font
|
|
38
|
+
build, and `@kerf-js/text/measure` and `@kerf-js/text/engine` are what a browser loads: nothing Node-only reaches them.
|
|
39
|
+
|
|
40
|
+
## Highlights
|
|
41
|
+
|
|
42
|
+
- **One place for fonts.** `text({ fonts })` is the only place font files are declared; a hand-written `@font-face`
|
|
43
|
+
without a fallback face is X020.
|
|
44
|
+
- **No layout shift on swap.** Each family gets a `"<Family> Fallback"` face with `size-adjust` and ascent, descent and
|
|
45
|
+
line-gap overrides computed from the font's metrics (Capsize's method).
|
|
46
|
+
- **Subset per `unicode-range`.** Faces are cut to hashed woff2 files per subset, and cached by content hash.
|
|
47
|
+
- **Trimmed in a build.** Custom axes the source never varies are pinned, and `latin` splits into a preloaded core and
|
|
48
|
+
a lazily fetched rest. Neither cut changes how the page looks or which characters it can show.
|
|
49
|
+
- **Budgets enforced.** Over 100 KB of preloaded fonts, or over 1.5 KB brotli of generated font CSS, is X022.
|
|
50
|
+
- **Measurement that costs nothing until used.** `text.measure` is loaded through `import()` the first time something
|
|
51
|
+
measures; `measure: 'off'` removes it.
|
|
52
|
+
- **Small engine.** The base chunk a page loads is 8.20 KB br on Blink and 9.50 KB on WebKit, against 33.9 KB for
|
|
53
|
+
upstream Pretext in one file (see [Size](#size)).
|
|
54
|
+
- **Zero differences from upstream.** The accuracy run compares line counts with unmodified Pretext over 7,000
|
|
55
|
+
paragraphs in Chrome and WebKit (see [Accuracy](#accuracy)).
|
|
56
|
+
|
|
57
|
+
## Contents
|
|
58
|
+
|
|
59
|
+
- [Install](#install)
|
|
60
|
+
- [Quick start](#quick-start)
|
|
61
|
+
- [Fonts](#fonts)
|
|
62
|
+
- [Options](#options)
|
|
63
|
+
- [Face options](#face-options)
|
|
64
|
+
- [What the build does](#what-the-build-does)
|
|
65
|
+
- [Trimming](#trimming)
|
|
66
|
+
- [Using a face](#using-a-face)
|
|
67
|
+
- [Measuring text](#measuring-text)
|
|
68
|
+
- [The engine](#the-engine)
|
|
69
|
+
- [Engine API](#engine-api)
|
|
70
|
+
- [Exact and estimated handles](#exact-and-estimated-handles)
|
|
71
|
+
- [Size](#size)
|
|
72
|
+
- [API reference](#api-reference)
|
|
73
|
+
- [Troubleshooting](#troubleshooting)
|
|
74
|
+
- [How it is built](#how-it-is-built)
|
|
75
|
+
- [Accuracy](#accuracy)
|
|
76
|
+
- [Staying in sync with upstream](#staying-in-sync-with-upstream)
|
|
77
|
+
- [Related packages](#related-packages)
|
|
78
|
+
- [Contributing](#contributing)
|
|
79
|
+
- [License](#license)
|
|
80
|
+
|
|
81
|
+
## Install
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
# npm
|
|
85
|
+
npm install @kerf-js/text
|
|
86
|
+
|
|
87
|
+
# pnpm
|
|
88
|
+
pnpm add @kerf-js/text
|
|
89
|
+
|
|
90
|
+
# bun
|
|
91
|
+
bun add @kerf-js/text
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Or let the `kerf` CLI install it and add it to `kerf.config.ts` in one step:
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
npx kerf add text
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
`kerf add text` installs the package and adds `text()` to `plugins`. The `landing` template already lists it. `kerf docs fonts` covers what an
|
|
101
|
+
app uses from it.
|
|
102
|
+
|
|
103
|
+
## Quick start
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
// kerf.config.ts
|
|
107
|
+
import { defineConfig } from '@kerf-js/core/config'
|
|
108
|
+
import { text } from '@kerf-js/text'
|
|
109
|
+
|
|
110
|
+
export default defineConfig({
|
|
111
|
+
plugins: [
|
|
112
|
+
text({
|
|
113
|
+
fonts: {
|
|
114
|
+
body: {
|
|
115
|
+
family: 'Inter',
|
|
116
|
+
src: 'assets/fonts/inter.woff2',
|
|
117
|
+
fallback: 'Arial',
|
|
118
|
+
axes: { wght: [100, 900] },
|
|
119
|
+
},
|
|
120
|
+
display: {
|
|
121
|
+
family: 'Fraunces',
|
|
122
|
+
src: 'assets/fonts/fraunces.woff2',
|
|
123
|
+
fallback: 'Georgia',
|
|
124
|
+
weight: 700,
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
}),
|
|
128
|
+
],
|
|
129
|
+
})
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Then name the faces in CSS, each with its fallback second:
|
|
133
|
+
|
|
134
|
+
```css
|
|
135
|
+
body { font-family: "Inter", "Inter Fallback", sans-serif; }
|
|
136
|
+
h1 { font-family: "Fraunces", "Fraunces Fallback", Georgia, serif; }
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
There is nothing to import. `@kerf-js/core/vite` loads the generated CSS in the client entry (Start) or the page (an
|
|
140
|
+
HTML app), and puts the preload links in the head.
|
|
141
|
+
|
|
142
|
+
## Fonts
|
|
143
|
+
|
|
144
|
+
### Options
|
|
145
|
+
|
|
146
|
+
Options of `text()`. Anything else is refused with the option's path (P115).
|
|
147
|
+
|
|
148
|
+
| Option | Type | Default | What it does |
|
|
149
|
+
| --------- | -------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------- |
|
|
150
|
+
| `fonts` | `Record<string, Face>` | `{}` | Role to face. The keys are roles of your choosing; `body` and `display` are preloaded by default. |
|
|
151
|
+
| `subsets` | `Subset[]` | `['latin']` | Unicode ranges to cut every face to: `latin`, `latin-ext`, `cyrillic`, `cyrillic-ext`, `greek`, `greek-ext`, `vietnamese`. Must be non-empty. |
|
|
152
|
+
| `measure` | `'auto' \| 'off'` | `'auto'` | `'auto'` provides `text.measure`, loaded lazily by what uses it; `'off'` leaves the slot unbound. |
|
|
153
|
+
| `engine` | `'slim' \| 'pretext'` | `'slim'` | `'slim'` is `@kerf-js/text/engine`; `'pretext'` is pinned upstream Pretext (33.9 KB br). |
|
|
154
|
+
| `trim` | `boolean` | `true` | Pin custom axes the source never varies and load uncommon latin characters on demand. Builds only. |
|
|
155
|
+
|
|
156
|
+
### Face options
|
|
157
|
+
|
|
158
|
+
| Option | Type | Default | What it does |
|
|
159
|
+
| ---------- | ------------------------------------- | ----------------------------------- | ------------------------------------------------------------------------------------------------ |
|
|
160
|
+
| `family` | `string` | required | The family name CSS uses. |
|
|
161
|
+
| `src` | `string` | required | The font file, relative to the project root: woff2, woff, ttf or otf. |
|
|
162
|
+
| `fallback` | `string` | required | The system face the fallback is matched to: Arial, Helvetica, Times New Roman, Georgia, Courier New, Verdana. |
|
|
163
|
+
| `display` | `'swap' \| 'optional' \| 'fallback'` | `'swap'` | `font-display`. `block` and `auto` are refused: they hide text while the font loads (X021). |
|
|
164
|
+
| `weight` | integer 1–1000 | `400` | The face's weight. A `wght` range in `axes` takes its place in the `@font-face` rule. |
|
|
165
|
+
| `style` | `'normal' \| 'italic'` | `'normal'` | The face's style. |
|
|
166
|
+
| `axes` | `Record<string, [min, max]>` | `{}` | `{ wght: [400, 700] }` sets the weight range. A build cuts each named axis to its range; a custom axis named here is kept whatever the source sets. |
|
|
167
|
+
| `preload` | `boolean` | `true` for `body` and `display` | Preload the face's first latin file. Other roles preload only with `preload: true`. |
|
|
168
|
+
|
|
169
|
+
Two roles on the same face (same family, `src`, weight and style) emit it once; it's preloaded if either asks.
|
|
170
|
+
|
|
171
|
+
### What the build does
|
|
172
|
+
|
|
173
|
+
The build runs inside `@kerf-js/core/vite`, through the `font.build@1` slot. For each face it:
|
|
174
|
+
|
|
175
|
+
1. Subsets the file per `unicode-range` (HarfBuzz, through `subset-font`) to hashed woff2 files. Results are cached
|
|
176
|
+
by content hash, so an unchanged face isn't cut again.
|
|
177
|
+
2. Writes the `@font-face` rules, and a `"<Family> Fallback"` face over `local("<fallback>")` with `size-adjust`,
|
|
178
|
+
`ascent-override`, `descent-override` and `line-gap-override` set so the fallback takes the same space as the font.
|
|
179
|
+
`size-adjust` comes from the ratio of average glyph widths; the vertical overrides are divided by it.
|
|
180
|
+
3. Lists the preloads, which go in the page's head before the first byte.
|
|
181
|
+
4. Checks the budgets: 100 KB of preloaded font bytes and 1.5 KB brotli of generated font CSS (X022). Dev serves whole
|
|
182
|
+
faces, so the preload budget is only checked in a build.
|
|
183
|
+
|
|
184
|
+
The plugin also gives generated share cards the `display` and `body` faces, so their title and text are set in the
|
|
185
|
+
app's fonts.
|
|
186
|
+
|
|
187
|
+
`import 'kerf:fonts'` still works and changes nothing. `import { preloads } from 'kerf:fonts/preloads'` gives the
|
|
188
|
+
preload list to a custom server.
|
|
189
|
+
|
|
190
|
+
### Trimming
|
|
191
|
+
|
|
192
|
+
Font bytes are what hold back the largest paint on a text hero, so a build trims each face without being asked:
|
|
193
|
+
|
|
194
|
+
- **Axes.** A custom axis (`SOFT`, `WONK`, `GRAD`…) is pinned to the one value your `font-variation-settings` give it,
|
|
195
|
+
or to the font's default if the source never sets it. An axis set to two values stays. A value Kerf can't read from
|
|
196
|
+
the source (`var()`, a template, an expression) keeps every axis. `wght`, `wdth`, `opsz`, `slnt` and `ital` are never
|
|
197
|
+
pinned: CSS drives them. An axis named in `axes` is cut to that range, or to one value when the range is a point:
|
|
198
|
+
`axes: { wght: [500, 700] }` drops the rest of the weight axis.
|
|
199
|
+
- **Characters.** `latin` becomes two files. The core is preloaded: ASCII, common punctuation, and every other latin
|
|
200
|
+
character in your source. The rest (`latin-rest`) is fetched through `unicode-range` only by a page that shows one
|
|
201
|
+
of those characters, so text from a database still gets the face, after a fallback-matched swap.
|
|
202
|
+
|
|
203
|
+
> [!WARNING]
|
|
204
|
+
> Only your own source is read. A pinned value applies to every use of the face, so an axis set at runtime or by a
|
|
205
|
+
> dependency's CSS needs declaring: `axes: { SOFT: [0, 100] }`. `trim: false` turns all trimming off.
|
|
206
|
+
|
|
207
|
+
`kerf dev` always serves whole faces, so an axis you are editing responds.
|
|
208
|
+
|
|
209
|
+
### Using a face
|
|
210
|
+
|
|
211
|
+
The build declares faces; it doesn't apply them. Name one in your CSS with its fallback second, as in the
|
|
212
|
+
[Quick start](#quick-start). Use the same family names in `font` strings you pass to measurement, and name a family
|
|
213
|
+
rather than `system-ui` or a generic (X002): canvas and CSS can resolve those differently.
|
|
214
|
+
|
|
215
|
+
## Measuring text
|
|
216
|
+
|
|
217
|
+
`text()` provides the `text.measure@1` slot, on the client and in workers. It is what canvas `s.text()` in a
|
|
218
|
+
`<Surface>`, `measure()`, `lines()` and `fit()` from `@kerf-js/core/text`, and a veneer list given `text` use to know a
|
|
219
|
+
paragraph's height without rendering it. Nothing is loaded until one of them measures. Without the slot, canvas
|
|
220
|
+
`s.text()` fails with P101; on `webgl2` the mirror is the visible text and no plugin is needed. See `kerf docs surface`.
|
|
221
|
+
|
|
222
|
+
`engine: 'slim'` (the default) loads `@kerf-js/text/engine`. `engine: 'pretext'` loads upstream `@chenglou/pretext`
|
|
223
|
+
behind the same calls.
|
|
224
|
+
|
|
225
|
+
### The engine
|
|
226
|
+
|
|
227
|
+
**A slim build of [Pretext](https://github.com/chenglou/pretext) by Cheng Lou (`@chenglou/pretext` 1.0.0, MIT).**
|
|
228
|
+
Pretext does the hard part: it ports Blink's, WebKit's and Gecko's line breaking and their measurement rules to
|
|
229
|
+
JavaScript, so a paragraph's height comes from arithmetic over canvas widths instead of DOM reflow. This package
|
|
230
|
+
changes none of that logic. It specialises Pretext per engine, cuts its tables down to a base alphabet, moves the
|
|
231
|
+
rest into lazy chunks, and puts a small "lite" tier in front for plain Latin and emoji text. Credit for the
|
|
232
|
+
engine goes to Pretext. Bugs in the slicing are ours. Research and numbers: [archived research](https://github.com/Ravonus/kerf/blob/archive/evals-2026-11/docs/research/12-text-engine-slim.md).
|
|
233
|
+
|
|
234
|
+
`loadTextEngine()` picks the engine from the user agent the way Pretext's `getLayoutEngine()` does: `Firefox/` is
|
|
235
|
+
Gecko, `AppleWebKit/537.36` is Blink, any other `AppleWebKit/` is WebKit (Safari, and every iOS browser), and anything
|
|
236
|
+
else is laid out as Blink. Only that engine's chunk loads.
|
|
237
|
+
|
|
238
|
+
### Engine API
|
|
239
|
+
|
|
240
|
+
<!-- example: skip @kerf-js/text/engine ships no type declarations yet, and text, width and lineHeight are yours -->
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
import { loadTextEngine } from '@kerf-js/text/engine'
|
|
244
|
+
// Load only this browser's engine chunk.
|
|
245
|
+
const te = await loadTextEngine()
|
|
246
|
+
const h = te.prepare(text, '16px Inter', { whiteSpace: 'normal' })
|
|
247
|
+
const { height, lineCount } = te.layout(h, width, lineHeight)
|
|
248
|
+
// [{ width, start, end }], cursors as Pretext gives them.
|
|
249
|
+
const ranges = te.lines(h, width)
|
|
250
|
+
// Clear Pretext's cache and the lite tier's caches.
|
|
251
|
+
te.clear()
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
It follows Kerf's `MeasureProvider` contract (`prepare`, `layout`, `lines`, `lineText`, `clear`, see
|
|
255
|
+
[`src/types.ts`](https://github.com/Ravonus/kerf/blob/main/packages/text/src/types.ts)).
|
|
256
|
+
|
|
257
|
+
| Call | Returns | What it does |
|
|
258
|
+
| --------------------------------- | ----------------------------- | ------------------------------------------------------------------------------------------------ |
|
|
259
|
+
| `prepare(text, font, opts?)` | `Handle` | Segments and measures a text in a CSS `font` string. See prepare options below. |
|
|
260
|
+
| `layout(handle, width, lineHeight)` | `{ height, lineCount }` | Breaks the prepared text at `width`. |
|
|
261
|
+
| `lines(handle, width)` | `LineRange[]` | Each line as `{ width, start, end }`: segment and grapheme cursors, and the width without hanging spaces. |
|
|
262
|
+
| `lineText(handle, line)` | `string` | A line's text. Needs a handle prepared with `{ segments: true }`. |
|
|
263
|
+
| `clear()` | `void` | Clears Pretext's cache and the lite tier's caches. |
|
|
264
|
+
| `needs(text, font, opts?)` | `Pack[]` | The lazy packs a text needs beyond the base chunk, from one scan of its code points. |
|
|
265
|
+
| `load(packs)` | `Promise<void>` | Preloads those packs. |
|
|
266
|
+
| `loaded()` | `readonly Pack[]` | The packs loaded so far. |
|
|
267
|
+
| `setLocale(locale?)` | `void` | Passes a locale to Pretext's `setLocale` and the lite tier, now and for packs loaded later. |
|
|
268
|
+
| `engine` | `'blink' \| 'webkit' \| 'gecko'` | Which engine chunk this is. |
|
|
269
|
+
|
|
270
|
+
`needs`, `load`, `loaded`, `setLocale` and `engine` are this engine's extras, not part of the provider contract.
|
|
271
|
+
Packs are `exact` (the exact tier itself), `complex`, `scripts`, `spacing`, `families` and `rare`.
|
|
272
|
+
|
|
273
|
+
Prepare options:
|
|
274
|
+
|
|
275
|
+
| Option | Type | Default | What it does |
|
|
276
|
+
| --------------- | ---------------------------- | ---------- | --------------------------------------------------------------------- |
|
|
277
|
+
| `whiteSpace` | `'normal' \| 'pre-wrap'` | `'normal'` | CSS `white-space`. |
|
|
278
|
+
| `wordBreak` | `'normal' \| 'keep-all'` | `'normal'` | CSS `word-break`. |
|
|
279
|
+
| `letterSpacing` | `number` (px) | none | CSS `letter-spacing`. Loads the `spacing` pack. |
|
|
280
|
+
| `segments` | `boolean` | `false` | Keep each segment's text so `lineText()` works. Costs memory. |
|
|
281
|
+
|
|
282
|
+
`lineText(h, line)` works on both tiers: a lite handle keeps its collapsed text and segment starts, so asking for
|
|
283
|
+
strings doesn't load the exact tier.
|
|
284
|
+
|
|
285
|
+
### Exact and estimated handles
|
|
286
|
+
|
|
287
|
+
A handle has `exact` and `ready`. While a pack it needs is still loading, `layout()` returns an estimate marked
|
|
288
|
+
`exact: false`. Once `ready` settles the same handle is exact, so there's no need to prepare it again. Call
|
|
289
|
+
`load(needs(...))` before preparing to never see an estimate:
|
|
290
|
+
|
|
291
|
+
<!-- example: skip @kerf-js/text/engine ships no type declarations yet, and text, width and lineHeight are yours -->
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
import { loadTextEngine } from '@kerf-js/text/engine'
|
|
295
|
+
|
|
296
|
+
const te = await loadTextEngine()
|
|
297
|
+
const font = '16px Inter'
|
|
298
|
+
await te.load(te.needs(text, font))
|
|
299
|
+
const h = te.prepare(text, font)
|
|
300
|
+
te.layout(h, width, lineHeight) // exact on the first call
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## Size
|
|
304
|
+
|
|
305
|
+
Brotli 11 after terser, measured with `node scripts/specialize.mjs --no-rare` and
|
|
306
|
+
`node scripts/build.mjs --minifier terser`. The "rare" pack is empty and its code is still in the exact tier.
|
|
307
|
+
|
|
308
|
+
| Engine | Base chunk (what a page loads) | Exact tier (lazy) | Script data + CJK/SEA code (lazy) | Other lazy packs |
|
|
309
|
+
| ------ | ----------------------------------------------- | ----------------- | --------------------------------- | -------------------------------------- |
|
|
310
|
+
| Blink | **8.20 KB** | 11.06 KB | 12.32 KB | complex 2.0, spacing 0.8 |
|
|
311
|
+
| WebKit | **9.50 KB** | 10.03 KB | 10.33 KB | complex 2.2, spacing 0.8, families 1.9 |
|
|
312
|
+
| Gecko | 16.57 KB (exact tier in base; no lite tier yet) | n/a | 7.76 KB | complex 2.1, spacing 0.8 |
|
|
313
|
+
|
|
314
|
+
Upstream `prepare` + `layout` + `clearCache` was 33.9 KB br for all engines in one file when research 12 measured it.
|
|
315
|
+
The base figures include the 0.21 KB detector shell (`dist/index.js`). `pnpm --filter @kerf-js/text size` writes the
|
|
316
|
+
current figures to `dist/size.json`.
|
|
317
|
+
|
|
318
|
+
## API reference
|
|
319
|
+
|
|
320
|
+
| Export | Side | What it is |
|
|
321
|
+
| ------------------------ | ----------- | -------------------------------------------------------------------------------------------------- |
|
|
322
|
+
| `@kerf-js/text` | config | `text(options?)`, the plugin for `kerf.config.ts`: provides `font.build@1` and `text.measure@1`. Also `SUBSETS`, every subset the build can cut, and the `TextOptions`, `Face` and `Subset` types. |
|
|
323
|
+
| `@kerf-js/text/build` | Node, build | `createFontBuild({ fonts, subsets, trim })`, the `font.build@1` provider, plus its helpers: `RANGES`, `BUDGET`, `rangeText`, `toRange`, `scanAxes`, `readAxes`, `pinned`, `fallbackFace`. |
|
|
324
|
+
| `@kerf-js/text/measure` | browser | `createMeasure({ engine })`, the `text.measure@1` provider: this package's engine, or upstream Pretext behind the same calls. Reached only through the slot's `import()`. |
|
|
325
|
+
| `@kerf-js/text/engine` | browser | `loadTextEngine(name?)` and `detectEngine(ua?)`: the built per-engine chunks behind a 0.21 KB detector. |
|
|
326
|
+
|
|
327
|
+
<details>
|
|
328
|
+
<summary>Slots provided</summary>
|
|
329
|
+
|
|
330
|
+
| Slot | Sides | Provider | When |
|
|
331
|
+
| ----------------- | ---------------- | ------------------------------------------------ | ----------------------- |
|
|
332
|
+
| `font.build@1` | build | `createFontBuild` from `plugin/build.mjs` | Always |
|
|
333
|
+
| `text.measure@1` | client, worker | `createMeasure` from `@kerf-js/text/measure` | Unless `measure: 'off'` |
|
|
334
|
+
|
|
335
|
+
The plugin asks for read access to `**/*.{woff2,woff,ttf,otf}` and nothing else.
|
|
336
|
+
|
|
337
|
+
</details>
|
|
338
|
+
|
|
339
|
+
## Troubleshooting
|
|
340
|
+
|
|
341
|
+
| Code | What happened | Fix |
|
|
342
|
+
| ----- | ------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
|
|
343
|
+
| P115 | An option `text()` doesn't know, a bad value (`display: 'block'`, a weight outside 1–1000, an unknown subset), or a `fallback` with no known metrics. The message names the path. | Fix the option. Use a common system face as `fallback`: Arial, Helvetica, Times New Roman, Georgia, Courier New, Verdana. |
|
|
344
|
+
| P101 | Canvas `s.text()` with no `text.measure` provider. | Add `text()` to `plugins`, without `measure: 'off'`. |
|
|
345
|
+
| X020 | A hand-written `@font-face` for a local file with no `"<Family> Fallback"` face: the swap shifts layout. | Declare the font in `text({ fonts })`. |
|
|
346
|
+
| X021 | `font-display: block` or `auto`: text is invisible while the font loads. | `swap`, or `optional` for faces that may skip. |
|
|
347
|
+
| X022 | A local `@font-face` without `unicode-range`, or the build over budget: 100 KB of preloads or 1.5 KB of font CSS (brotli). | Preload fewer faces (`preload: false`), use fewer weights, faces or subsets. |
|
|
348
|
+
| X023 | A `<link rel="preload" as="font">` whose file no `@font-face` uses. | Remove the preload. |
|
|
349
|
+
| X001 | `s.text()` predicted one height and its DOM mirror rendered another. | Drop `font` and `lineHeight` so they come from CSS, or make them match. |
|
|
350
|
+
| X002 | `system-ui`, `-apple-system` or a generic-only family on a measured `s.text()`. | Name a family. |
|
|
351
|
+
| X003 | `s.text()` prepared before its font loaded, with no fallback face. | Declare the font in `text({ fonts })`, which generates the fallback. |
|
|
352
|
+
|
|
353
|
+
An axis that stops responding in a build but works in dev was pinned by trimming: name it in `axes`, or set
|
|
354
|
+
`trim: false`.
|
|
355
|
+
|
|
356
|
+
## How it is built
|
|
357
|
+
|
|
358
|
+
- **`vendor/pretext/`** is a mechanically split copy of the pinned npm tarball. Its version, sha256 and integrity are in
|
|
359
|
+
`package.json` under `pretext`; the independent accuracy reference uses the installed pin.
|
|
360
|
+
`node scripts/pretext-split.mjs` checks the split against its pinned input and patch; pass `--write` to regenerate it.
|
|
361
|
+
- **`scripts/specialize.mjs`** writes `gen/<engine>/`:
|
|
362
|
+
- It inlines the split walker, preparation, Gecko analysis and boundary helpers from their vendor source.
|
|
363
|
+
This keeps hot values local in the shipped engine. Shape checks reject unsupported scopes and calls.
|
|
364
|
+
- It folds the engine profile to constants, so esbuild drops the other engines' code.
|
|
365
|
+
- It re-encodes the break data for that engine only, restricted to `src/alphabet.json`. The scan's state machines
|
|
366
|
+
are pruned to the states reachable over the alphabet's categories and then minimised.
|
|
367
|
+
- It moves CJK/Thai/kana, letter-spacing, WebKit generic-family and complex-walker code into lazy packs. Stubs
|
|
368
|
+
throw `PackMissing`, which the provider catches: it loads the pack and prepares again.
|
|
369
|
+
- Prepare-time code that never ran in `scripts/coverage.mjs` goes to a "rare" pack.
|
|
370
|
+
- **`src/lite.ts`** is the lite tier.
|
|
371
|
+
- It reuses Pretext's own break scan and canvas widths, and Pretext's simple line counter with its additions in
|
|
372
|
+
the same order.
|
|
373
|
+
- It runs only inside an envelope where every Pretext correction is zero:
|
|
374
|
+
- the base alphabet and `white-space: normal`;
|
|
375
|
+
- no NBSP or other glue, soft hyphens, bidi controls or unusual spaces;
|
|
376
|
+
- no letter spacing;
|
|
377
|
+
- the font must pass the same probes Pretext runs: space kerning at segment edges (Blink), emoji correction,
|
|
378
|
+
fullwidth curly quotes (Blink Han kerning) and generic families (WebKit).
|
|
379
|
+
- A layout narrower than the widest word needs emergency breaks, so it goes to the exact tier.
|
|
380
|
+
- **`scripts/build.mjs`** bundles each engine with code splitting and reports brotli per chunk.
|
|
381
|
+
|
|
382
|
+
## Accuracy
|
|
383
|
+
|
|
384
|
+
`pnpm --filter @kerf-js/text test:accuracy` runs headless Chrome and WebKit. It compares every paragraph's line count
|
|
385
|
+
with unmodified Pretext at 11 widths and with DOM heights at 320 and 480 px. It also compares `lines()` with
|
|
386
|
+
`walkLineRanges()` on lite handles. The corpus covers Latin, hard Latin, emoji, Arabic, CJK, Thai and mixed text,
|
|
387
|
+
1,000 paragraphs each. Fonts are Arial, Times New Roman, Georgia, Verdana and Helvetica.
|
|
388
|
+
|
|
389
|
+
The bar is zero differences from upstream, and the 2026-10-28 run met it. Every DOM mismatch is one upstream has too.
|
|
390
|
+
|
|
391
|
+
`pnpm --filter @kerf-js/text test` (vitest, no browser) checks:
|
|
392
|
+
|
|
393
|
+
- the restricted break scans against upstream's full ones, on the corpus and a fuzz over the alphabet, with and
|
|
394
|
+
without the rare pack;
|
|
395
|
+
- the lite counter and walker;
|
|
396
|
+
- the alphabet and the engine detector;
|
|
397
|
+
- `text()` options and the font build: subsets, fallback faces, preloads, budgets and axis pinning.
|
|
398
|
+
|
|
399
|
+
## Staying in sync with upstream
|
|
400
|
+
|
|
401
|
+
1. Bump `devDependencies["@chenglou/pretext"]` and run `npm pack @chenglou/pretext@<v>` into a clean folder. Check its
|
|
402
|
+
sha256 and update the `pretext` block.
|
|
403
|
+
2. Replace `vendor/pretext/src` and `vendor/pretext/LICENSE` with the tarball's. Leave out its tests.
|
|
404
|
+
3. Run `pnpm --filter @kerf-js/text gen` and check its output:
|
|
405
|
+
- The specialiser fails loudly if a moved function or a profile field disappeared.
|
|
406
|
+
- The printed fold count must stay explainable.
|
|
407
|
+
- Profile folds only touch `profile.`, `engineProfile.` and `getEngineProfile().`.
|
|
408
|
+
4. Run the package's `coverage`, `test`, `build` and `test:accuracy` scripts.
|
|
409
|
+
- **Merge only with zero differences from upstream** in both browsers.
|
|
410
|
+
- A new correction in Pretext's measurement needs a matching envelope check in `src/lite.ts`. Otherwise the
|
|
411
|
+
accuracy run shows it as diffs on lite handles (`tier: "l"`).
|
|
412
|
+
|
|
413
|
+
## Related packages
|
|
414
|
+
|
|
415
|
+
| Package | What it is |
|
|
416
|
+
| ----------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
|
|
417
|
+
| [`@kerf-js/core`](https://www.npmjs.com/package/@kerf-js/core) | The compiler and runtime. `@kerf-js/core/text` and `<Surface>` use `text.measure`; `@kerf-js/core/vite` loads the font CSS. |
|
|
418
|
+
| [`@kerf-js/cli`](https://www.npmjs.com/package/@kerf-js/cli) | `kerf add text`, `kerf docs fonts`, and `kerf check` for the X codes. |
|
|
419
|
+
| [`@kerf-js/veneer`](https://www.npmjs.com/package/@kerf-js/veneer) | The component library. A veneer list given `text` measures rows through this package. |
|
|
420
|
+
| [`@kerf-js/media`](https://www.npmjs.com/package/@kerf-js/media) | The image build: the other half of a fast first paint. |
|
|
421
|
+
| [`create-kerf`](https://www.npmjs.com/package/create-kerf) | `npm create kerf`; the `landing` template ships with `text()`. |
|
|
422
|
+
|
|
423
|
+
## Contributing
|
|
424
|
+
|
|
425
|
+
See [CONTRIBUTING.md](https://github.com/Ravonus/kerf/blob/main/CONTRIBUTING.md). The repo is one pnpm workspace; from
|
|
426
|
+
its root:
|
|
427
|
+
|
|
428
|
+
| Command | What it does |
|
|
429
|
+
| ---------------------------------------------------- | -------------------------------------------------------------------- |
|
|
430
|
+
| `pnpm --filter @kerf-js/text gen` | Writes `gen/<engine>/` with `scripts/specialize.mjs`. |
|
|
431
|
+
| `pnpm --filter @kerf-js/text test` | Regenerates, then runs the vitest suites. |
|
|
432
|
+
| `pnpm --filter @kerf-js/text build` | Regenerates and bundles `dist/` with terser. |
|
|
433
|
+
| `pnpm --filter @kerf-js/text size` | Writes brotli sizes per chunk to `dist/size.json`. |
|
|
434
|
+
| `pnpm --filter @kerf-js/text coverage` | Finds prepare-time code that never runs, for the "rare" pack. |
|
|
435
|
+
| `pnpm --filter @kerf-js/text typecheck` | Regenerates, then type-checks. |
|
|
436
|
+
| `pnpm --filter @kerf-js/text test:accuracy` | The Chrome and WebKit accuracy run against upstream. |
|
|
437
|
+
| `pnpm --filter @kerf-js/text test:conformance` | Builds, then runs the provider conformance check in Chrome and WebKit. |
|
|
438
|
+
|
|
439
|
+
## License
|
|
440
|
+
|
|
441
|
+
[MIT](https://github.com/Ravonus/kerf/blob/main/LICENSE). The engine is built from
|
|
442
|
+
[Pretext](https://github.com/chenglou/pretext) by Cheng Lou, MIT; its license ships as
|
|
443
|
+
[`vendor/pretext/LICENSE`](https://github.com/Ravonus/kerf/blob/main/packages/text/vendor/pretext/LICENSE).
|
package/dist/LICENSE
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ravonus
|
|
4
|
+
|
|
5
|
+
This package is a derived build of Pretext (@chenglou/pretext 1.0.0) by Cheng Lou and the Pretext
|
|
6
|
+
contributors, https://github.com/chenglou/pretext, used under its MIT licence. Pretext does the real work:
|
|
7
|
+
its ports of Blink's, WebKit's and Gecko's line breaking and its measurement rules are what this package
|
|
8
|
+
ships, specialised per engine and split into lazy chunks. vendor/pretext/ holds the unmodified source and
|
|
9
|
+
its LICENSE, with the notices of the Chromium, WebKit, ICU, ICU4X and Unicode sources it follows or is
|
|
10
|
+
generated from. Those notices apply to gen/ and dist/ too, and dist/LICENSE-pretext carries them.
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
|
|
30
|
+
------------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
Pretext, vendored in vendor/pretext/ and the source of gen/ and dist/:
|
|
33
|
+
|
|
34
|
+
MIT License
|
|
35
|
+
|
|
36
|
+
Copyright (c) 2026 Pretext contributors
|
|
37
|
+
|
|
38
|
+
The full text, with its third-party notices, is vendor/pretext/LICENSE.
|