@kerf-js/text 0.1.0 → 0.1.1-nightly.20261115.46

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 CHANGED
@@ -163,7 +163,7 @@ Options of `text()`. Anything else is refused with the option's path (P115).
163
163
  | `display` | `'swap' \| 'optional' \| 'fallback'` | `'swap'` | `font-display`. `block` and `auto` are refused: they hide text while the font loads (X021). |
164
164
  | `weight` | integer 1–1000 | `400` | The face's weight. A `wght` range in `axes` takes its place in the `@font-face` rule. |
165
165
  | `style` | `'normal' \| 'italic'` | `'normal'` | The face's style. |
166
- | `axes` | `Record<string, [min, max]>` | `{}` | `{ wght: [400, 700] }` sets the weight range. A custom axis named here is kept whatever the source sets. |
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
167
  | `preload` | `boolean` | `true` for `body` and `display` | Preload the face's first latin file. Other roles preload only with `preload: true`. |
168
168
 
169
169
  Two roles on the same face (same family, `src`, weight and style) emit it once; it's preloaded if either asks.
@@ -194,7 +194,8 @@ Font bytes are what hold back the largest paint on a text hero, so a build trims
194
194
  - **Axes.** A custom axis (`SOFT`, `WONK`, `GRAD`…) is pinned to the one value your `font-variation-settings` give it,
195
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
196
  the source (`var()`, a template, an expression) keeps every axis. `wght`, `wdth`, `opsz`, `slnt` and `ital` are never
197
- pinned: CSS drives them.
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.
198
199
  - **Characters.** `latin` becomes two files. The core is preloaded: ASCII, common punctuation, and every other latin
199
200
  character in your source. The rest (`latin-rest`) is fetched through `unicode-range` only by a page that shows one
200
201
  of those characters, so text from a database still gets the face, after a fallback-matched swap.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kerf-js/text",
3
- "version": "0.1.0",
3
+ "version": "0.1.1-nightly.20261115.46",
4
4
  "description": "kerf's text: fonts for the build (subsets, matched fallbacks) and exact line breaking in the browser, on Pretext by Cheng Lou (@chenglou/pretext 1.0.0, MIT)",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -41,7 +41,7 @@
41
41
  "subset-font": "2.9.0"
42
42
  },
43
43
  "peerDependencies": {
44
- "@kerf-js/core": "^0.1.0"
44
+ "@kerf-js/core": "^0.1.1-nightly.20261115.46"
45
45
  },
46
46
  "engines": {
47
47
  "node": ">=24"
package/plugin/build.mjs CHANGED
@@ -3,8 +3,8 @@
3
3
  // then the vertical overrides divided by it), and list the preloads. Cached by content hash under `cacheDir`.
4
4
  //
5
5
  // Trimming (on in a build, off in dev, `trim: false` turns it off): font bytes are what set LCP on a text hero. Two
6
- // cuts keep the design and every character. Custom axes (SOFT, WONK, GRAD…) the source never varies are pinned, and
7
- // `latin` splits in two: a preloaded core (ASCII, common punctuation, what the source uses) and the rest, which a
6
+ // cuts keep the design and every character. Custom axes (SOFT, WONK, GRAD…) the source never varies are pinned, an
7
+ // axis `axes` declares is cut to that range, and `latin` splits in two: a preloaded core (ASCII, common punctuation, what the source uses) and the rest, which a
8
8
  // browser fetches through `unicode-range` only when a page holds one of those characters, so text from a database
9
9
  // still gets the face.
10
10
  import { createHash } from 'node:crypto'
@@ -124,6 +124,24 @@ export function pinned(axes, declared, { values, dynamic }) {
124
124
  return pins
125
125
  }
126
126
 
127
+ /**
128
+ * The axes `declared` narrows, cut to the declared range: `{ min, max }`, or one value when the range is a point.
129
+ * The face's `@font-face` rule already keeps CSS inside that range, so the deltas outside it never draw.
130
+ */
131
+ export function limited(axes, declared) {
132
+ const limits = {}
133
+ for (const a of axes) {
134
+ const range = declared[a.tag]
135
+ if (!range) continue
136
+ const clamp = (n) => Math.min(a.max, Math.max(a.min, n))
137
+ const min = clamp(Math.min(...range))
138
+ const max = clamp(Math.max(...range))
139
+ if (min === a.min && max === a.max) continue
140
+ limits[a.tag] = min === max ? min : { min, max }
141
+ }
142
+ return limits
143
+ }
144
+
127
145
  const camel = (name) => name.toLowerCase().replace(/[^a-z0-9]+(.)/g, (_, c) => c.toUpperCase())
128
146
  const pct = (n) => `${Math.round(n * 10000) / 100}%`
129
147
  const slug = (s) =>
@@ -196,13 +214,15 @@ export function createFontBuild({ fonts, subsets, trim = true }) {
196
214
  })
197
215
 
198
216
  const hash = createHash('sha256').update(bytes).digest('hex').slice(0, 16)
199
- const pins = trimming ? pinned(await readAxes(bytes), f.axes, scan) : {}
200
- entry.pins = pins
217
+ const axes = trimming ? await readAxes(bytes) : []
218
+ entry.pins = pinned(axes, f.axes, scan)
219
+ entry.limits = limited(axes, f.axes)
220
+ const variationAxes = { ...entry.pins, ...entry.limits }
201
221
  const cuts = faceCuts(f, { source, entry }) ?? subsets.flatMap(parts)
202
222
  for (const { subset, text, range } of cuts) {
203
223
  const inputs = createHash('sha256')
204
224
  .update(text)
205
- .update(JSON.stringify(pins))
225
+ .update(JSON.stringify(variationAxes))
206
226
  .digest('hex')
207
227
  .slice(0, 8)
208
228
  const cached = join(cacheDir, `${hash}-${subset}-${inputs}-v${VERSION}.woff2`)
@@ -211,7 +231,7 @@ export function createFontBuild({ fonts, subsets, trim = true }) {
211
231
  else {
212
232
  out = await subsetFont(bytes, text, {
213
233
  targetFormat: 'woff2',
214
- ...(Object.keys(pins).length && { variationAxes: pins }),
234
+ ...(Object.keys(variationAxes).length && { variationAxes }),
215
235
  })
216
236
  writeFileSync(cached, out)
217
237
  }
@@ -336,6 +356,7 @@ function faceReport(faces) {
336
356
  family: f.family,
337
357
  role: f.role,
338
358
  pinned: f.pins,
359
+ ...(Object.keys(f.limits).length && { limited: f.limits }),
339
360
  ...(f.unread && { glyphs: `kept whole: ${f.unread}` }),
340
361
  files: f.files.map(({ subset, bytes }) => ({ subset, bytes })),
341
362
  }))