@atlowchemi/webfont-generator
A native Rust NAPI addon that generates webfonts (SVG, TTF, EOT, WOFF, WOFF2) and their companion CSS/HTML from a set of SVG icon files.
This is a ground-up rewrite of @vusion/webfonts-generator in Rust — the original package and its authors deserve credit for the API design and template system that this project builds on. The JS implementation is unmaintained, so this package reimplements the same pipeline natively for better performance and long-term maintainability.
The API is largely compatible with @vusion/webfonts-generator, with a few differences:
- The
cssContextandhtmlContextcallbacks receive only the context object. The original also passedoptionsand thehandlebarsinstance as additional arguments — those are no longer available. formatOptionsis now strictly typed as{ svg?: SvgFormatOptions; ttf?: TtfFormatOptions; woff?: WoffFormatOptions; woff2?: Woff2FormatOptions }. The original accepted arbitrary{ [format]: unknown }; theeotkey is no longer accepted (EOT is derived from the TTF output).- A new
optimizeOutputoption runs an SVG path optimizer over each glyph before assembling the font. Defaults tofalse; opt in for smaller output bytes at a small build-time cost. Also available asformatOptions.svg.optimizeOutput. formatOptions.woff2.compressionQualitysets the Brotli compression quality (0–11) for WOFF2 output. Defaults to11(smallest output); lower it (e.g. to10) for faster encoding at a marginal size cost.- A new
incrementaloption (defaultfalse) retains parsed glyph data on the result soresult.regenerate(files, changes?)can rebuild after file changes without re-parsing the glyphs that didn't change — for dev/watch rebuilds. It refreshes the outputs in memory and, when the result was generated withwriteFiles, writes refreshed fonts to disk too while skipping unchanged CSS/HTML companion files. You pass the full file set (in the order a fresh build would use) plus what changed, or omitchangesto re-read/hash the full set and infer changes; the result is byte-identical to a freshgenerateWebfonts()of that set, additions included. - Generated font binaries (TTF, WOFF, etc.) may differ at the byte level because a different encoder is used, but the fonts are equally valid.
- CSS, HTML, and template output is identical.
Performance scales better with glyph count — for larger icon sets the native pipeline is significantly faster.
Incremental regeneration
let files = ;
const result = await ;
// On a watch event, rebuild reusing cached geometry for unchanged glyphs. Pass the full file set
// (in fresh-build order) so additions land in the right position, plus what changed:
result.;
// Or omit changes when watcher hints are unavailable/untrusted:
result.;
result.; // refreshed bytes
The first argument is the complete file set after the change, in the order a fresh build would use (e.g. your glob result) — any file omitted from it is dropped. Each change is { path, changeType: 'added' | 'changed' | 'removed', name? }, where name is the resolved glyph name if you apply a custom rename; otherwise added files derive their name from the file stem, changed files keep their current name, and removed files ignore it. When changes is omitted or null, every current file is re-read and hashed to detect added/changed/removed paths automatically. Results generated with cssContext or htmlContext callbacks cannot be regenerated because those JavaScript callbacks cannot be re-run by the synchronous method.
Node.js (npm)
Pre-built binaries are published for the following targets:
| Platform | Architecture |
|---|---|
| macOS | x64, arm64 |
| Linux (glibc) | x64, arm64, armv7 |
| Linux (musl) | x64, arm64 |
| Windows (MSVC) | x64, arm64 |
import from '@atlowchemi/webfont-generator';
const result = await ;
const css = result.;
const html = result.;
Rust library (crates.io)
use ;
let result = generate_sync.unwrap;
let css = result.generate_css_pure.unwrap;
An async API (webfont_generator::generate) is also available for use with tokio.
CLI
The CLI is available as an opt-in feature (to avoid pulling in clap for library users):
Usage
webfont-generator [OPTIONS] --dest <DEST> <FILES>...
Examples
# Generate default formats (eot, woff, woff2) from a directory of SVGs
# Generate specific formats with a custom font name
# Generate fonts with an HTML preview page
Options
Arguments:
... SVG files or directories containing SVG files
Options:
-d, --dest Output directory
-n, --font-name Font name [default: iconfont]
-t, --types Font types to generate [possible values: svg, ttf, eot, woff, woff2]
--css Generate CSS (default)
--no-css Skip CSS generation
--html Generate HTML preview
--no-html Skip HTML generation (default)
--css-template Custom CSS template path
--html-template Custom HTML template path
--css-fonts-url CSS fonts URL prefix
--write Write output files to disk (default)
--no-write Do not write output files (dry run)
--ligature Enable ligatures (default)
--no-ligature Disable ligatures
--font-height Font height
--ascent Ascent value
--descent Descent value
--start-codepoint Start codepoint (hex, e.g. 0xF101)
-h, --help Print help
-V, --version Print version
Templates
Default CSS, SCSS, and HTML templates are available via the /templates export:
import from '@atlowchemi/webfont-generator/templates';
console.log; // path to default CSS template
console.log; // path to default SCSS template
console.log; // path to default HTML template