#font #svg #macro #api-bindings #text

macro font-map-macros

Code generation macros for font-map

7 releases

new 0.2.4 Feb 4, 2025
0.2.3 Feb 4, 2025
0.1.1 Jan 29, 2025

#134 in #macros

Download history 405/week @ 2025-01-29

405 downloads per month
Used in 3 crates (via font-map)

MIT license

105KB
2K SLoC

font-map

Font parser / enumerator with support for code generation

Crates.io Build Status docs.rs License

This crate provides functionality for parsing font files and enumerating the glyphs they contain.

The base usecase for this crate is to create an enum of all the glyphs in a font file,
for use in fontend projects, where you want to refer to glyphs by name rather than by codepoint:

use font_map::font;

font!(Icon, "google_material_symbols/font.ttf");

const DELETE: Icon = Icon::Delete;

The generated code includes information for each glyph, such as:

  • codepoint, and postfix-name
  • Plus a generated SVG preview image visible on hover

You can also access Icon::FONT_FAMILY to simplify font usage in your frontend.


Another use is to use it for introspection of font files:

use font_map::font::Font;

let font = Font::from_file("google_material_symbols/font.ttf")?;
if let Some(glyph) = font.glyph_named("delete") {
    let codepoint = glyph.codepoint();
    let svg = glyph.svg_outline();
}

Features

  • macros - Enables the font! macro for code generation
  • codegen - Enables the FontCodegenExt trait for runtime code generation
  • extended-svg - Enables compressed and base64 encoded SVG data in the generated code (Needed for image previews)

Known Limitations

This crate was made for a very specific use-case, and as such currently has a few limitations:

  • Only supports TTF fonts
  • And even then, only a subset of the spec, namely:
  • Only formats 0 and 4 of the cmap table
  • Only Unicode, or MS encoding 1 and 10, and Macintosh::0 of the name table
  • Only formats 2.5 or below of the post table

Dependencies

~4MB
~138K SLoC