@atlowchemi/webfont-generator

中文文档
0.10.0发布于 2026/10/19MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 7503
原始使用文档
文档目录5 节
  1. Installation and usage
  2. Incremental regeneration
  3. Templates
  4. Documentation
  5. License

@atlowchemi/webfont-generator

webfont-generator logo

npm license

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.

The addon selects the Rust engine's fs capability (including its required async support) with engine defaults disabled. Cargo feature selection is internal to the addon; npm consumers use the file-based JavaScript API documented here. Native WOFF compression uses the engine's static zlib backend. Incremental results also support single-file add, update, and remove helpers without resending membership, plus addAsync/updateAsync/removeAsync returning replacement results. See single-file actions. For browser applications, use @atlowchemi/webfont-browser-generator, which ships precompiled WASM and runs SVG-text generation in workers.

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 cssContext and htmlContext callbacks receive only the context object. The original also passed options and the handlebars instance as additional arguments — those are no longer available.
  • formatOptions is now strictly typed as { svg?: SvgFormatOptions; ttf?: TtfFormatOptions; woff?: WoffFormatOptions; woff2?: Woff2FormatOptions }. The original accepted arbitrary { [format]: unknown }; the eot key is no longer accepted (EOT is derived from the TTF output).
  • A new optimizeOutput option runs an SVG path optimizer over each glyph before assembling the font. Defaults to false; optimization may reduce SVG path data, but does not guarantee smaller binary font output, so compare the generated sizes for your input set. Also available as formatOptions.svg.optimizeOutput.
  • formatOptions.woff2.compressionQuality sets the Brotli compression quality (0–11) for WOFF2 output. Defaults to 11 (smallest output); lower it (e.g. to 10) for faster encoding at a marginal size cost.
  • A new incremental option (default false) retains parsed glyph data on the result so result.regenerateAsync(files, changes?) can rebuild after file changes without re-parsing the glyphs that didn't change or blocking the Node.js event loop. It refreshes the outputs in memory and, when the result was generated with writeFiles, 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 omit changes to re-read/hash the full set and infer changes; the result is byte-identical to a fresh generateWebfonts() 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.
  • A new variants option supports multi-weight families with discrete designs.
  • A new colorGlyphs option preserves solid SVG colors for selected glyphs in TTF, WOFF, and WOFF2, disabled by default.
  • colorSlots, for example { secondary: '#ff9900' }, reserves named palette entries for CSS light/dark themes. Requires active colorGlyphs; slots use distinct #RRGGBB colors and ASCII alphanumeric/hyphen/underscore names. Indices follow ASCII name order and are exposed in the CSS context's colorSlots map. See the live example.

Performance scales better with glyph count — for larger icon sets the native pipeline is significantly faster.

Installation and usage

npm install @atlowchemi/webfont-generator

Consumers on the following platforms do not need Rust or Cargo. Keep optional dependencies enabled so your package manager installs the matching pre-built native binary.

PlatformArchitecture
macOSx64, arm64
Linux (glibc)x64, arm64, armv7
Linux (musl)x64, arm64
Windows (MSVC)x64, arm64
import { generateWebfonts } from '@atlowchemi/webfont-generator';

const result = await generateWebfonts({
    files: ['./icons/home.svg', './icons/search.svg'],
    dest: './dist/fonts',
    fontName: 'my-icons',
    types: ['woff2', 'woff'],
});

const css = result.generateCss();
const html = result.generateHtml();

Incremental regeneration

Multi-weight families also support incremental builds. Set incremental: true, then use regenerateAsync({ variants: [{ variant: 'bold', files: [...] }, ...] }, changes) with every configured design's complete file list. Omit changes to re-diff every design. See the variant regeneration reference for input shapes, synchronous alternatives, and failure semantics.

let files = ['./icons/home.svg', './icons/search.svg'];
let result = await generateWebfonts({ files, dest, fontName: 'my-icons', incremental: true });

// 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 = await result.regenerateAsync({ files }, [{ path: './icons/home.svg', changeType: 'changed' }]);
// Or omit changes when watcher hints are unavailable/untrusted:
result = await result.regenerateAsync({ files });
result.woff2; // 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, and changes are inferred automatically. regenerateAsync() returns a replacement result; the receiver stays readable and unchanged while work runs and after failure. Assign the replacement before starting another rebuild because overlapping calls from the same result lineage are rejected. Disk writes are not transactional. The synchronous, mutating regenerate() method remains available when blocking the event loop is acceptable. Results generated with cssContext or htmlContext callbacks cannot be regenerated because those JavaScript callbacks cannot be re-run during a rebuild.

Templates

Default CSS, SCSS, and HTML templates are available via the /templates export:

import * as templates from '@atlowchemi/webfont-generator/templates';

console.log(templates.css); // path to default CSS template
console.log(templates.scss); // path to default SCSS template
console.log(templates.html); // path to default HTML template

Documentation

See the Node.js reference for the complete API. For direct Rust or command-line use, see the separate engine crate.

License

MIT

版本与发布记录

@atlowchemi/webfont-generator 共发布 14 个版本,首次发布于 2026/04/14;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.10.0latest2026/10/1919 天前
0.9.02026/10/0929 天前
0.8.02026/10/041 个月前
0.7.02026/09/161 个月前
0.6.12026/08/252 个月前
0.6.02026/08/102 个月前

其他发布通道:alpha → 0.1.0-alpha.1。

关于 @atlowchemi/webfont-generator 的常见问题

如何安装 @atlowchemi/webfont-generator?

在项目目录运行 npm install @atlowchemi/webfont-generator,或使用 pnpm add / yarn add / bun add。

@atlowchemi/webfont-generator 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@atlowchemi/webfont-generator 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@atlowchemi/webfont-generator 可以商用吗?

@atlowchemi/webfont-generator 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@atlowchemi/webfont-generator 的下载量有多少?

根据 npm 官方下载统计,@atlowchemi/webfont-generator 在 2026/10/07 – 2026/11/05 这 30 天内被下载 7,503 次,日均约 250 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@atlowchemi/webfont-generator 还在维护吗?

最新版本 0.10.0 发布于 2026/10/19(19 天前);至今共发布 14 个版本,其中过去 12 个月发布了 14 个;首次发布于 2026/04/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。