Skip to main content

Crate webfont_generator

Crate webfont_generator 

Source
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 cli

Then 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. Enables async.
  • 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 with in_memory to run source generation on Tokio’s blocking pool.
  • cli: Builds the webfont-generator CLI binary (adds clap dependency). Enables fs; not enabled by default — use cargo install webfont-generator --features cli.
  • bench: Internal benchmark helpers; enables fs and in_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§

ClearCacheReport
Counts reported by clear_cache.
CollectedOutput
A generated payload with no filesystem destination.
CssContext
Fields supplied to a cssContext callback. Additional keys from user-supplied templateOptions are merged into the same object at runtime, so the JS-side type widens this with an open-ended index signature.
FontVariant
One named SVG design in a multi-variant icon family.
FormatOptions
Per-format configuration object. Each field carries options that only apply to the corresponding output format.
GenerateWebfontsFromSourcesOptions
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.
GenerateWebfontsFromSourcesResult
Font buffers and independently lazy companion renders from SVG text. No method on this result reads or writes files. Settings and templates stay fixed.
GenerateWebfontsOptions
Top-level options controlling webfont generation. dest and exactly one source, ordinary files or variants, are required. Every other field has a sensible default. With the cli feature, options deserialize from camelCase JSON with unknown fields rejected.
GenerateWebfontsResult
Result of a successful generateWebfonts call. Exposes the generated font bytes (or null for formats that were not requested) and methods to render the CSS and HTML preview.
GlyphChangeEntry
One entry in the changes array passed to the Node binding’s regenerate. The complete ordered file list passed alongside it controls final glyph order; this only describes which files need re-reading, renaming, or removal.
HtmlContext
Fields supplied to an htmlContext callback. Additional keys from user-supplied templateOptions are merged into the same object at runtime, so the JS-side type widens this with an open-ended index signature.
MissingGlyphOptions
Family-wide missing-glyph policy for multi-variant generation.
RegenerateError
Failure from asynchronous regeneration. Ordinary regeneration errors retain the input result so callers can recover it with RegenerateError::into_result and retry.
SourceFontVariant
One ordered design in a text-input variant family.
SourceRegenerateError
An async update failure with the previous usable result, except on worker panic/cancellation.
SvgFormatOptions
SVG-format–specific options for the intermediate SVG font and the per-glyph path processing that feeds every other format.
SvgSource
Named SVG text. id is an opaque, nonempty cache key, never a file path.
TemplateVariant
Resolved variant metadata supplied to CSS and HTML template callbacks.
TtfFormatOptions
TTF-format–specific options. Populates fields in the generated TTF name and head tables.
VariantFileSet
Complete file set for one configured variant during regeneration.
VariantSourceSet
Complete ordered ID membership for a named variant.
Woff2FormatOptions
WOFF2-format–specific options. Affects only WOFF2 output.
WoffFormatOptions
WOFF-format–specific options. Affects only WOFF1 output; WOFF2 ignores these.

Enums§

ColorGlyphSelection
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.
FontType
Font output format. Used in the types and order options to control which formats are generated and the order they appear in the CSS @font-face src: descriptor.
GlyphChange
What happened to a file, for crate::GenerateWebfontsResult::regenerate. name is the caller-resolved glyph name (the rename callback, if any, is applied by the caller).
MissingGlyphBehavior
Family-wide behavior when a logical glyph is absent from a variant.
OutputContents
Shared output payload. Cloning it does not copy font bytes or rendered text.
OutputKind
Format of a destination-free generated output.
RegenerationFiles
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.
SourceChange
A changed source only; unchanged SVG text stays retained by the result. Deserializes objects tagged with type: added, changed, or removed.
SourceMembership
Complete membership after applying a source change batch. Deserializes either { "sources": [...] } or { "variants": [...] }.

Traits§

GenerationHooks
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_sync on 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§

RenameFn
A glyph rename function that maps file stems to custom glyph names.