Skip to content

Custom fonts #32

@tomassedovic

Description

@tomassedovic

Egui currently ships with built-in fonts and (as far as I could see) there's no way for the library user to switch those out.

Font customisation is done via ctx.set_fonts which accepts a FontDefinitions. But that requires a FontFamily which is an enum containing the hardcoded fonts and a no way to change them.

Would bringing your own font be within Egui's scope? The built-in fonts are great for e.g. debugging, level editor etc. UIs, but for a user-facing one (e.g. in-game menu, windows) the fonts often need to match the look & feel of the game.

If this is welcome, I'll be happy to work on it (albeit slowly) and follow any design considerations you might have.

My initial thought is adding a third variant FontFamily::Custom(u32) and provide a way to register a new font e.g.: impl Context { pub add_custom_font(font: &[u8]) -> u8; }

Where the function and the enum variant would both use a font ID (or maybe a hashed name) to support multiple fonts at the same time.

For reference, this is a commit I'm maintaining in my Egui fork to be able to use a (single) custom font:

tomassedovic@47c7942

And it's used like this in my game:

let font_definitions = {
    use egui::paint::fonts::{FontFamily, TextStyle};
    let family = FontFamily::Mononoki;

    let mut def = egui::paint::FontDefinitions::default();
    def.fonts.insert(TextStyle::Body, (family, font_size_px));
    def.fonts.insert(TextStyle::Button, (family, font_size_px));
    def.fonts.insert(TextStyle::Heading, (family, font_size_px));
    def.fonts
        .insert(TextStyle::Monospace, (family, font_size_px));
    def
};
ctx.set_fonts(font_definitions);

Metadata

Metadata

Assignees

No one assigned

    Labels

    designSome architectual design work neededfeatureNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions