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

  • cli: Builds the webfont-generator CLI binary (adds clap dependency). Not enabled by default — use cargo install webfont-generator --features cli.

Node.js bindings are built by the separate, unpublished webfont-generator-napi crate.

Structs§

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.
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.
SvgFormatOptions
SVG-format–specific options for the intermediate SVG font and the per-glyph path processing that feeds every other format.
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.
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.
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.

Traits§

GenerationHooks
Async hooks used by crate::generate_with_hooks.

Functions§

generate
Generate webfonts from SVG files.
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.