iced_fontello

Crates.ioiced_fontello
lib.rsiced_fontello
version
sourcesrc
created_at2024-12-06 23:10:10.759267
updated_at2024-12-08 20:21:19.341531
descriptionGenerate type-safe icon fonts for `iced` at compile time
homepage
repositoryhttps://github.com/hecrj/iced_fontello
max_upload_size
id1474813
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Héctor Ramón (hecrj)

documentation

README

iced_fontello

Documentation Crates.io License Downloads Test Status Discourse Discord Server

A compile-time, type-safe icon font generator for iced. Powered by Fontello.

Usage

Create a .toml file somewhere in your crate with the font definition:

# fonts/example-icons.toml
module = "icon"

[glyphs]
edit = "fontawesome-pencil"
save = "entypo-floppy"
trash = "typicons-trash"

The module value defines the Rust module that will be generated in your src directory containing a type-safe API to use the font.

Each entry in the [glyphs] section corresponds to an icon. The keys will be used as names for the functions of the module of the font; while the values specify the glyph for that key using the format: <font>-<glyph>. You can browse the available glyphs in Fontello or the fonts.json file.

Next, add iced_fontello to your build-dependencies:

[build-dependencies]
iced_fontello = "0.13"

Then, call iced_fontello::build in your build script, passing the path of your font definition:

pub fn main() {
    println!("cargo::rerun-if-changed=fonts/example-icons.toml");
    iced_fontello::build("fonts/example-icons.toml").expect("Build example-icons font");
}

The library will generate the font and save its .ttf file right next to its definition. In this example, the library would generate fonts/example-icons.ttf.

Finally, it will generate a type-safe iced API that lets you use the font. In our example:

// Generated automatically by iced_fontello at build time.
// Do not edit manually.
// d24460a00249b2acd0ccc64c3176452c546ad12d1038974e974d7bdb4cdb4a8f
use iced::widget::{text, Text};
use iced::Font;

pub const FONT: &[u8] = include_bytes!("../fonts/example-icons.ttf");

pub fn edit<'a>() -> Text<'a> {
    icon("\u{270E}")
}

pub fn save<'a>() -> Text<'a> {
    icon("\u{1F4BE}")
}

pub fn trash<'a>() -> Text<'a> {
    icon("\u{E10A}")
}

fn icon<'a>(codepoint: &'a str) -> Text<'a> {
    text(codepoint).font(Font::with_name("example-icons"))
}

Now you can simply add mod icon; to your lib.rs or main.rs file and enjoy your new font:

mod icon;

use iced::widget::row;

// ...

row![icon::edit(), icon::save(), icon::trash()].spacing(10)

// ...

Check out the full example to see it all in action.

Packaging

If you plan to package your crate, you must make sure you include the generated module and font file in the final package. build is effectively a no-op when the module and the font already exist and are up-to-date.

Commit count: 13

cargo fmt