egui_nerdfonts

Crates.ioegui_nerdfonts
lib.rsegui_nerdfonts
version0.1.3
sourcesrc
created_at2023-07-28 12:47:33.024833
updated_at2024-06-12 10:23:27.178517
descriptionNerdfonts icons for egui
homepagehttps://github.com/bernsteining/egui_nerdfonts
repositoryhttps://github.com/bernsteining/egui_nerdfonts
max_upload_size
id928487
size4,212,027
bernsteining (bernsteining)

documentation

README

egui_nerdfonts

Bundles Nerd Fonts icons for egui.

NerdFonts

Installation

Add the crate as a dependency in Cargo.toml:

egui_nerdfonts = "0.1.3"

or type cargo add egui_nerdfonts, in your project.

Usage

First, update the fonts in your egui context:

let mut fonts = egui::FontDefinitions::default();
egui_nerdfonts::add_to_fonts(&mut fonts, egui_nerdfonts::Variant::Regular);

let egui_ctx = Context::default();
egui_ctx.set_fonts(fonts);

Choose nerdfonts icons you want to use among these.

Then use nerdfonts icons as follow:

ui.label(format!("{}", egui_nerdfonts::regular::NF_DEV_RUST));

Example

cargo run --example rust_logo

output

About

Got inspired by egui_phosphor, code uses the same structure.

The .ttf used is this one, and the src/variants/regular.rs was generated with the following python script, with the nerdfonts_regular.ttf as first argument:

from itertools import chain
from fontTools.ttLib import TTFont
from fontTools.unicode import Unicode
import sys

with TTFont(
    sys.argv[1], 0, allowVID=0, ignoreDecompileErrors=True, fontNumber=-1
) as ttf:
    chars = chain.from_iterable(
        [y + (Unicode[y[0]],) for y in x.cmap.items()] for x in ttf["cmap"].tables
    )
    for char in chars:
        symbol_name = char[1].upper().replace('-', '_').replace(' ', '_').replace('#', '_').replace('!', '')
        code = r"\u" + "{" + f"{char[0]:X}" + "}"
        print(f"pub const {symbol_name}: &str = \"{code}\";")
Commit count: 7

cargo fmt