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.
  • napi: Enables Node.js NAPI bindings for use as a native addon.

Structs§

CssContext
Guaranteed 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.
FormatOptions
Per-format configuration object. Each field carries options that only apply to the corresponding output format.
GenerateWebfontsOptions
Top-level options controlling webfont generation. Only dest and files are required; every other field has a sensible default. See the per-field docs for defaults and units.
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.
HtmlContext
Guaranteed 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.
SvgFormatOptions
SVG-format–specific options for the intermediate SVG font and the per-glyph path processing that feeds every other format.
TtfFormatOptions
TTF-format–specific options. Populates fields in the generated TTF name and head tables.
WoffFormatOptions
WOFF-format–specific options. Affects only WOFF1 output; WOFF2 ignores these.

Enums§

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.

Functions§

generate
Generate webfonts from SVG files.
generate_sync
Synchronous version of generate. Spawns a tokio runtime internally.

Type Aliases§

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