webfont 12.2.0 → 12.4.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 +94 -4
- package/dist/cli.mjs +267 -256
- package/dist/index.js +1 -1
- package/dist/index.mjs +142 -132
- package/dist/src/index.d.mts +2 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/lib/ttfEncode.d.mts +3 -0
- package/dist/src/lib/ttfEncode.d.ts +3 -0
- package/dist/src/standalone/inputMode.d.mts +1 -1
- package/dist/src/standalone/inputMode.d.ts +1 -1
- package/dist/src/types/TranscodedFont.d.mts +1 -0
- package/dist/src/types/TranscodedFont.d.ts +1 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ See **[FEATURES.md](./FEATURES.md)** for the canonical capability list (what is
|
|
|
16
16
|
## Features
|
|
17
17
|
|
|
18
18
|
- **SVG icon pipeline** (default): `.svg` icons → `svg`, `ttf`, `eot`, `woff`, `woff2` (not `otf`);
|
|
19
|
-
- **TTF encoding**: one or more `.ttf` files → `ttf` (pass-through), `eot`, `woff`, and/or `woff2` (auto-detected — no extra flag);
|
|
19
|
+
- **TTF encoding**: one or more `.ttf` files → `ttf` (pass-through), `svg` (SVG font), `eot`, `woff`, and/or `woff2` (auto-detected — no extra flag);
|
|
20
20
|
- **Webfont decompression**: one `.woff` or `.woff2` file → `ttf` and/or `otf` matching the **embedded SFNT flavor** (decompress only — not TTF ↔ OTF transcoding);
|
|
21
21
|
- Config files: `JavaScript`, `JSON`, or `YAML` via [cosmiconfig](https://github.com/cosmiconfig/cosmiconfig);
|
|
22
22
|
- Built-in and custom CSS templates (`css`, `scss`, [`styl`](https://github.com/itgalaxy/webfont/pull/164/));
|
|
@@ -30,7 +30,7 @@ webfont runs one of three pipelines depending on matched input files. They canno
|
|
|
30
30
|
| Mode | Input | Outputs | Notes |
|
|
31
31
|
|------|--------|---------|--------|
|
|
32
32
|
| **SVG icons** | One or more `.svg` files | `svg`, `ttf`, `eot`, `woff`, `woff2` | Default. Builds TrueType via `svg2ttf`. **`otf` is rejected** — use `ttf`. |
|
|
33
|
-
| **TTF encoding** | One or more `.ttf` files | `ttf`, `eot`, `woff`, and/or `woff2` per input | Auto-detected. Default when SVG-pipeline `formats` are still configured: `woff` + `woff2
|
|
33
|
+
| **TTF encoding** | One or more `.ttf` files | `ttf`, `svg` (SVG font), `eot`, `woff`, and/or `woff2` per input | Auto-detected. Default when SVG-pipeline `formats` are still configured: `woff` + `woff2` (`svg` is opt-in). No templates. |
|
|
34
34
|
| **Webfont decompress** | One or more `.woff` / `.woff2` paths, globs, or `http(s)` URLs | `ttf` and/or `otf` per input | One output file per source (basename from filename; collisions get `-woff`/`-woff2`). Not a single merged font. |
|
|
35
35
|
|
|
36
36
|
**Not supported today**
|
|
@@ -157,6 +157,13 @@ const subset = await webfont({
|
|
|
157
157
|
formats: ["woff2"],
|
|
158
158
|
});
|
|
159
159
|
|
|
160
|
+
// TTF → SVG font (legacy format; opt-in, pure JS via fonteditor-core)
|
|
161
|
+
const svgFont = await webfont({
|
|
162
|
+
files: "path/to/font.ttf",
|
|
163
|
+
formats: ["svg"],
|
|
164
|
+
});
|
|
165
|
+
// svgFont.svg → SVG font as a string
|
|
166
|
+
|
|
160
167
|
// Batch: multiple TTF files in one run
|
|
161
168
|
const batch = await webfont({
|
|
162
169
|
files: ["fonts/Inter-Regular.ttf", "fonts/Inter-Bold.ttf"],
|
|
@@ -190,6 +197,71 @@ const remote = await webfont({
|
|
|
190
197
|
});
|
|
191
198
|
```
|
|
192
199
|
|
|
200
|
+
### Autohinting
|
|
201
|
+
|
|
202
|
+
webfont does not bundle a hinting engine — [`ttfautohint`](https://www.freetype.org/ttfautohint/) is a native binary, so it stays out of the core install. Run it yourself through the [`ttfPostProcess`](#ttfpostprocess) hook: it receives the generated **TTF before** WOFF/WOFF2/EOT are derived, so the hinted outlines flow into every format.
|
|
203
|
+
|
|
204
|
+
**Option A — npm wrapper (no system install).** The [`ttfautohint`](https://www.npmjs.com/package/ttfautohint) package ships a prebuilt binary and a Buffer API:
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
npm i -D ttfautohint
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
```js
|
|
211
|
+
import { webfont } from "webfont";
|
|
212
|
+
import TTFAutohint from "ttfautohint";
|
|
213
|
+
|
|
214
|
+
await webfont({
|
|
215
|
+
files: "src/svg-icons/**/*.svg",
|
|
216
|
+
formats: ["ttf", "woff", "woff2", "eot"],
|
|
217
|
+
// `icon: true` applies icon-font-tuned hinting metrics
|
|
218
|
+
ttfPostProcess: (ttf) => TTFAutohint.transform(ttf, { icon: true }),
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
`TTFAutohint.transform(buffer, options)` returns the hinted `Buffer`, which webfont uses as the TTF and as the source for the other formats.
|
|
223
|
+
|
|
224
|
+
**Option B — system binary (no extra npm dependency).** If `ttfautohint` is already installed (Homebrew, apt, …), pipe the buffer through it:
|
|
225
|
+
|
|
226
|
+
```js
|
|
227
|
+
import { execFileSync } from "node:child_process";
|
|
228
|
+
import { webfont } from "webfont";
|
|
229
|
+
|
|
230
|
+
const autohint = (ttf) =>
|
|
231
|
+
execFileSync("ttfautohint", ["-W", "-i", "-s", "-x", "24", "-l", "12", "-r", "48"], {
|
|
232
|
+
input: ttf,
|
|
233
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
await webfont({
|
|
237
|
+
files: "src/svg-icons/**/*.svg",
|
|
238
|
+
formats: ["ttf", "woff2"],
|
|
239
|
+
ttfPostProcess: (ttf) => autohint(ttf),
|
|
240
|
+
});
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
**CLI.** Flags can't carry a function, but webfont loads a JS config via cosmiconfig — put the hook in `webfont.config.js`:
|
|
244
|
+
|
|
245
|
+
```js
|
|
246
|
+
// webfont.config.js
|
|
247
|
+
const TTFAutohint = require("ttfautohint");
|
|
248
|
+
|
|
249
|
+
module.exports = {
|
|
250
|
+
files: "src/svg-icons/**/*.svg",
|
|
251
|
+
formats: ["ttf", "woff2"],
|
|
252
|
+
ttfPostProcess: (ttf) => TTFAutohint.transform(ttf, { icon: true }),
|
|
253
|
+
};
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
then run `webfont`.
|
|
257
|
+
|
|
258
|
+
Notes:
|
|
259
|
+
|
|
260
|
+
- The hook is **opt-in**; webfont core stays free of native dependencies.
|
|
261
|
+
- CI must have the binary available — the npm wrapper installs one; the system-binary option requires `ttfautohint` on `PATH`.
|
|
262
|
+
- The callback may be async: return a `Promise<Buffer | Uint8Array>` if your tool is asynchronous.
|
|
263
|
+
- See [#749](https://github.com/itgalaxy/webfont/issues/749) for autohinting tracking and a possible first-party companion package.
|
|
264
|
+
|
|
193
265
|
### Options
|
|
194
266
|
|
|
195
267
|
#### `files`
|
|
@@ -227,7 +299,7 @@ const remote = await webfont({
|
|
|
227
299
|
- Possible values: `svg`, `ttf`, `otf`, `eot`, `woff`, `woff2`
|
|
228
300
|
- Description: Font file types to generate.
|
|
229
301
|
- **SVG input**: `svg`, `ttf`, `eot`, `woff`, `woff2` only. **`otf` is not supported** (pipeline uses TrueType outlines).
|
|
230
|
-
- **TTF input**: `ttf`, `eot`, `woff`, and/or `woff2`
|
|
302
|
+
- **TTF input**: `svg` (SVG font), `ttf`, `eot`, `woff`, and/or `woff2`. `svg` is **opt-in** (not in the default `woff` + `woff2` set); **`otf` is not produced** in this mode.
|
|
231
303
|
- **WOFF/WOFF2 input**: `ttf` and/or `otf` only — must match the decompressed SFNT flavor inside the file (not arbitrary transcoding). `eot`, `woff`, `woff2`, and `svg` are not produced in this mode.
|
|
232
304
|
- CLI: pass `-f` / `--formats` as a JSON array (for example `'["woff2"]'`) or as a comma-separated list (for example `woff2` or `svg, ttf, woff2`). Invalid format names throw an error.
|
|
233
305
|
- API and config files: `formats` must be an array of the values above; unknown names (for example `icon`) throw before the pipeline runs.
|
|
@@ -439,7 +511,7 @@ Do **not** use `Math.random()` in `fontName` — that renames both font files an
|
|
|
439
511
|
|
|
440
512
|
- Type: `function`
|
|
441
513
|
- Default: `undefined`
|
|
442
|
-
- Description: Post-process the generated **TTF** buffer **after** it is built and **before** webfont derives WOFF/WOFF2/EOT from it (SVG pipeline only). The callback receives `(ttf, { fontName, formats })` and returns the new font bytes (`Buffer` or `Uint8Array`, sync or async). Every derived format is produced from the returned buffer. This is a **caller-owned** extension point — webfont bundles nothing here, so optional/native steps such as **autohinting** (`ttfautohint`) live in **your** project or a separate package, keeping the core free of native dependencies (same philosophy as [`glyphContentTransformFn`](#glyphcontenttransformfn) and [ADR 0011](docs/adr/0011-no-svg-outline-stroke-dependency.md); see [#749](https://github.com/itgalaxy/webfont/issues/749)).
|
|
514
|
+
- Description: Post-process the generated **TTF** buffer **after** it is built and **before** webfont derives WOFF/WOFF2/EOT from it (SVG pipeline only). The callback receives `(ttf, { fontName, formats })` and returns the new font bytes (`Buffer` or `Uint8Array`, sync or async). Every derived format is produced from the returned buffer. This is a **caller-owned** extension point — webfont bundles nothing here, so optional/native steps such as **autohinting** (`ttfautohint`) live in **your** project or a separate package, keeping the core free of native dependencies (same philosophy as [`glyphContentTransformFn`](#glyphcontenttransformfn) and [ADR 0011](docs/adr/0011-no-svg-outline-stroke-dependency.md); see [#749](https://github.com/itgalaxy/webfont/issues/749)). See the [Autohinting](#autohinting) recipe for npm-wrapper, system-binary, and CLI variants.
|
|
443
515
|
- Example (autohinting — **install the hinting tool in your app**, not in webfont):
|
|
444
516
|
|
|
445
517
|
```js
|
|
@@ -480,6 +552,24 @@ Do **not** use `Math.random()` in `fontName` — that renames both font files an
|
|
|
480
552
|
|
|
481
553
|
`webfont()` resolves to an object with generated font buffers (and optional `template` output). The `config` property contains the **effective options** used for the run (defaults, discovered config, and any options you passed in), plus optional **output metadata** when a configuration file was found or loaded.
|
|
482
554
|
|
|
555
|
+
#### TypeScript
|
|
556
|
+
|
|
557
|
+
`Result` and `ResultConfig` are exported from the package entry, so you can annotate `webfont()` output directly instead of relying on `ReturnType` inference:
|
|
558
|
+
|
|
559
|
+
```ts
|
|
560
|
+
import { webfont, type Result, type ResultConfig } from "webfont";
|
|
561
|
+
|
|
562
|
+
const result: Result = await webfont({
|
|
563
|
+
files: "src/svg-icons/**/*.svg",
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
const config: ResultConfig | undefined = result.config;
|
|
567
|
+
|
|
568
|
+
if (config?.filePath) {
|
|
569
|
+
console.log(`Loaded config from ${config.filePath}`);
|
|
570
|
+
}
|
|
571
|
+
```
|
|
572
|
+
|
|
483
573
|
#### `result.config.filePath`
|
|
484
574
|
|
|
485
575
|
- Type: `string` | `undefined`
|