egui-phosphor

repository·main·Indexed 41 days ago

https://github.com/amperl/egui-phosphor

A Rust crate providing Phosphor icons for egui applications. Version 0.14.0 includes font fallback management, support for multiple variants (thin, light, regular, bold, fill), and a `subset` feature with a `subset!` macro to reduce binary size by including only required icons at compile time.

Tokens
1.9K
Snippets
8
Records
11
Agent score
49%

What's inside egui-phosphor

  1. How to use multiple Phosphor variants

    main

    Since all Phosphor variants share the same codepoints, only one variant can be the default fallback for the Proportional font. To use multiple variants (e.g., Regular and Fill):

    1. Add the primary variant using add_to_fonts (this sets it as the fallback).
    2. Add additional variants using add_as_family.
    3. Access additional variants using their specific rich helper or by selecting the family.

    Example with subsetting:

    egui_phosphor::subset! {
        pub mod icons {
            use regular::{GEAR, HOUSE};
            use fill::{GEAR, HEART};
        }
    }
    
    icons::regular::add_to_fonts(&mut fonts);
    icons::fill::add_as_family(&mut fonts);
    
    // Using the fallback variant:
    ui.label(format!("{} Settings", icons::regular::GEAR));
    
    // Using an additional variant via the rich helper:
    ui.label(icons::fill::rich(format!("{} Favourite", icons::fill::HEART)));
  2. Use the subsetting feature to reduce font size

    main

    The subset feature allows you to reduce the font size at compile time by including only the specific icons you need. This reduces the final binary size but adds approximately 0.3s to the crate rebuild time when the subset changes.

    1. Enable the subset feature in Cargo.toml:
    egui-phosphor = { version = "0.14", features = ["subset"] }
    1. Use the subset! macro to define a module containing only your required icons:
    egui_phosphor::subset! {
        pub mod icons {
            use regular::{GEAR, HOUSE, TRASH};
        }
    }
    1. Register the subsetted icons and use them in your UI:
    // On startup:
    let mut fonts = egui::FontDefinitions::default();
    icons::regular::add_to_fonts(&mut fonts);
    cc.egui_ctx.set_fonts(fonts);
    
    // In your UI:
    ui.label(format!("{} Settings", icons::regular::GEAR));
  3. Configure egui fonts for Phosphor icons

    main

    To ensure icons render correctly alongside text, you must update your egui::FontDefinitions on startup. Use egui_phosphor::add_to_fonts to add your chosen variant as a fallback to the primary Proportional font. This allows the icon font to take over automatically when an icon character is encountered in a text string.

    let mut fonts = egui::FontDefinitions::default();
    egui_phosphor::add_to_fonts(&mut fonts, egui_phosphor::Variant::Regular);
    
    cc.egui_ctx.set_fonts(fonts);
  4. Select a Phosphor icon font variant via Cargo features

    main

    The egui-phosphor crate requires you to select at least one font variant as a Cargo feature. If no variant is selected, the crate will fail to compile.

    Available variants (as Cargo features):

    • thin
    • light
    • regular
    • bold
    • fill

    If you are unsure which one to use, enable default features in your Cargo.toml to use the standard variant.

    [dependencies]
    egui-phosphor = { version = "0.14.0", features = ["regular"] }
  5. Use Phosphor icons in your UI

    main

    Access icons via the constants provided by the crate. You must use the constants corresponding to the specific variant you have loaded (e.g., regular::ICON_NAME for Variant::Regular).

    ui.label(egui::RichText::new(format!("FILE_CODE {}", egui_phosphor::regular::FILE_CODE)).size(32.0));
  6. Use the `Variant` enum to load font data

    main

    The Variant enum allows you to programmatically access the raw font bytes or convert them into egui::FontData for use in your egui application.

    Note that the available variants in the enum are determined by the Cargo features enabled at compile time. If a feature is not enabled, its corresponding Variant variant will not be available.

    use egui_phosphor::Variant;
    
    // Get the raw bytes for the selected variant
    let bytes = Variant::Regular.font_bytes();
    
    // Convert directly to egui::FontData for integration with egui
    let font_data = Variant::Regular.font_data();
  7. Add Phosphor icons to egui FontDefinitions

    main

    Use add_to_fonts to quickly register a Phosphor icon variant into your egui::FontDefinitions. This function registers the font under the name "phosphor" and adds it to the FontFamily::Proportional list as a fallback. This allows you to mix icons directly into ordinary text labels without specifying a font family explicitly.

    // Assuming you have egui::FontDefinitions initialized
    // and have imported the necessary types from egui_phosphor
    
    egui_phosphor::add_to_fonts(&mut fonts, egui_phosphor::Variant::Regular);
  8. Add Phosphor icon bytes to fonts with fallback support

    main

    The add_font_bytes_to_fonts function inserts raw font bytes under a specific name and makes it a fallback for FontFamily::Proportional.

    Behavior Details:

    • It inserts the font at index 1 (or index 0 if the list is empty) of the Proportional family. This ensures it does not shadow the primary text font but remains available for icon resolution in standard labels.
    • name must be unique; subsequent calls with the same name will overwrite previous ones.
    • Because all Phosphor variants share the same codepoints, Proportional can only resolve to one variant. For multiple variants, use add_font_bytes_as_family and select them explicitly via FontFamily::Name.
    egui_phosphor::add_font_bytes_to_fonts(&mut fonts, "my_custom_name", bytes);
  9. Register a Phosphor font variant as a specific FontFamily

    main

    Use add_font_bytes_as_family to register font bytes under a unique name without adding them to the global FontFamily::Proportional fallback list. This is the recommended way to handle multiple Phosphor variants (e.g., Thin, Bold, Duotone) in the same application.

    How it works:

    1. It inserts the bytes into fonts.font_data using the provided name.
    2. It creates a new FontFamily::Name(name) that contains the existing Proportional fonts plus the new icon font.
    3. The icon font is inserted at index 1 of this new family to ensure it resolves icons correctly without blanking out ordinary lowercase text (as the subsetting mechanism maps a-z to blank glyphs used for ligatures).

    To use the font, you must explicitly reference it using egui::FontFamily::Name("your_name".into()) when rendering text.

    egui_phosphor::add_font_bytes_as_family(&mut fonts, "phosphor_bold", bytes);
    
    // When rendering, use the specific family name:
    // ui.label(egui::RichText::new("icon_char").font(egui::FontFamily::Name("phosphor_bold".into())));
  10. Reference: `Variant` enum methods

    main

    The Variant enum provides the following methods to access font assets:

    MethodReturn TypeDescription
    font_bytes(&self) -> &'static [u8]&'static [u8]Returns the raw byte slice of the selected font file.
    font_data(&self) -> egui::FontDataegui::FontDataReturns the font bytes wrapped in an egui::FontData object for easy use with egui font loaders.