Expand description
§webfont-generator
Generate webfonts (SVG, TTF, EOT, WOFF, WOFF2) from SVG icon files.
§Library usage
use webfont_generator::{GenerateWebfontsOptions, FontType};
// Async API (requires a tokio runtime)
let options = GenerateWebfontsOptions {
dest: "output".to_owned(),
files: vec!["icons/add.svg".to_owned(), "icons/remove.svg".to_owned()],
font_name: Some("my-icons".to_owned()),
types: Some(vec![FontType::Woff2, FontType::Woff]),
..Default::default()
};
let result = webfont_generator::generate(options, None).await?;
if let Some(woff2) = result.woff2_bytes() {
println!("Generated WOFF2: {} bytes", woff2.len());
}use webfont_generator::{GenerateWebfontsOptions, FontType};
// Synchronous API
let options = GenerateWebfontsOptions {
dest: "output".to_owned(),
files: vec!["icons/add.svg".to_owned()],
write_files: Some(false),
..Default::default()
};
let result = webfont_generator::generate_sync(options, None).unwrap();§CLI
Install the CLI binary with:
cargo install webfont-generator --features cliThen run:
webfont-generator --dest ./dist/fonts ./icons/§Feature flags
The default features are fs, in_memory, and async.
fs: File-based generation, hooks, templates, and output writing. Enablesasync.in_memory: Synchronous generation and regeneration from named SVG text, without filesystem access or a runtime requirement.async: Tokio-backed asynchronous wrappers for enabled input APIs. Combine within_memoryto run source generation on Tokio’s blocking pool.cli: Builds thewebfont-generatorCLI binary (addsclapdependency). Enablesfs; not enabled by default — usecargo install webfont-generator --features cli.bench: Internal benchmark helpers; enablesfsandin_memory.
With no input features enabled, common option and output types remain available.
Node.js bindings are built by the separate, unpublished webfont-generator-napi crate.
Structs§
- Clear
Cache Report - Counts reported by
clear_cache. - Collected
Output - A generated payload with no filesystem destination.
- CssContext
- Fields supplied to a
cssContextcallback. Additional keys from user-suppliedtemplateOptionsare merged into the same object at runtime, so the JS-side type widens this with an open-ended index signature. - Font
Variant - One named SVG design in a multi-variant icon family.
- Format
Options - Per-format configuration object. Each field carries options that only apply to the corresponding output format.
- Generate
Webfonts From Sources Options - Filesystem-free generation settings. Defaults match the file API’s
GenerateWebfontsOptions. Templates are text (Some("")renders empty), not paths. No output is written. Deserializes camelCase fields, rejecting unknown fields and defaulting omitted settings. - Generate
Webfonts From Sources Result - Font buffers and independently lazy companion renders from SVG text. No method on this result reads or writes files. Settings and templates stay fixed.
- Generate
Webfonts Options - Top-level options controlling webfont generation.
destand exactly one source, ordinaryfilesorvariants, are required. Every other field has a sensible default. With theclifeature, options deserialize from camelCase JSON with unknown fields rejected. - Generate
Webfonts Result - Result of a successful
generateWebfontscall. Exposes the generated font bytes (ornullfor formats that were not requested) and methods to render the CSS and HTML preview. - Glyph
Change Entry - One entry in the
changesarray passed to the Node binding’sregenerate. The complete ordered file list passed alongside it controls final glyph order; this only describes which files need re-reading, renaming, or removal. - Html
Context - Fields supplied to an
htmlContextcallback. Additional keys from user-suppliedtemplateOptionsare merged into the same object at runtime, so the JS-side type widens this with an open-ended index signature. - Missing
Glyph Options - Family-wide missing-glyph policy for multi-variant generation.
- Regenerate
Error - Failure from asynchronous regeneration. Ordinary regeneration errors retain the input result
so callers can recover it with
RegenerateError::into_resultand retry. - Source
Font Variant - One ordered design in a text-input variant family.
- Source
Regenerate Error - An async update failure with the previous usable result, except on worker panic/cancellation.
- SvgFormat
Options - SVG-format–specific options for the intermediate SVG font and the per-glyph path processing that feeds every other format.
- SvgSource
- Named SVG text.
idis an opaque, nonempty cache key, never a file path. - Template
Variant - Resolved variant metadata supplied to CSS and HTML template callbacks.
- TtfFormat
Options - TTF-format–specific options. Populates fields in the generated TTF
nameandheadtables. - Variant
File Set - Complete file set for one configured variant during regeneration.
- Variant
Source Set - Complete ordered ID membership for a named variant.
- Woff2
Format Options - WOFF2-format–specific options. Affects only WOFF2 output.
- Woff
Format Options - WOFF-format–specific options. Affects only WOFF1 output; WOFF2 ignores these.
Enums§
- Color
Glyph Selection - Select logical glyphs whose solid SVG fills are preserved as COLR v1 paint. Names refer to the final names after rename hooks, across all variants.
- Font
Type - Font output format. Used in the
typesandorderoptions to control which formats are generated and the order they appear in the CSS@font-facesrc:descriptor. - Glyph
Change - What happened to a file, for
crate::GenerateWebfontsResult::regenerate.nameis the caller-resolved glyph name (therenamecallback, if any, is applied by the caller). - Missing
Glyph Behavior - Family-wide behavior when a logical glyph is absent from a variant.
- Output
Contents - Shared output payload. Cloning it does not copy font bytes or rendered text.
- Output
Kind - Format of a destination-free generated output.
- Regeneration
Files - Complete ordered input membership for a regeneration operation. The case must match the result’s generation mode. Synchronous methods borrow this value; async methods consume it.
- Source
Change - A changed source only; unchanged SVG text stays retained by the result.
Deserializes objects tagged with
type:added,changed, orremoved. - Source
Membership - Complete membership after applying a source change batch.
Deserializes either
{ "sources": [...] }or{ "variants": [...] }.
Traits§
- Generation
Hooks - Async hooks used by
crate::generate_with_hooks.
Functions§
- clear_
cache - Remove every file the generation cache created in
cache_location, including temporary files left by interrupted writes. Other files in the directory are left untouched, and a missing directory is not an error. - generate
- Generate webfonts from SVG files.
- generate_
from_ sources - Run
generate_from_sources_syncon Tokio’s blocking pool. Requires an active Tokio runtime; scheduling/join failures are returned as errors. - generate_
from_ sources_ sync - Generate fonts directly from named SVG text, without a runtime or filesystem access. Both ordinary and variant builds retain the engine’s Rayon parallelism.
- generate_
sync - Synchronous version of
generate. Spawns a tokio runtime internally and has the same source preparation behavior for multi-variant input. - generate_
with_ hooks - Generate a webfont from a set of SVG files.
Type Aliases§
- Rename
Fn - A glyph rename function that maps file stems to custom glyph names.