bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.46k stars 3.6k forks source link

Adds basic support for working with system fonts #16365

Open bushrat011899 opened 2 weeks ago

bushrat011899 commented 2 weeks ago

Objective

Solution

Testing


Showcase

Users now have access to the FontLibrary system parameter, which allows searching for available fonts. This helps with using the new system_fonts feature, which will load system fonts at runtime.

In the below snippets, fonts is of type FontLibrary

// Find a monospaced font
let font: Option<Handle<Font>> = fonts.find(|font| font.monospaced);

// Find a font with a particular name
let font: Option<Handle<Font>> = fonts.find(|font| {
    let family_name = &font.families[0].0;
    family_name.contains("Courier")
});

system_font_demo

Notes

TurtIeSocks commented 2 weeks ago

Awesome! I swapped to this branch locally with my playground example and every locale now displays without the need to load any external font files 🎊

I suppose this does increase the need for users to have the ability to control cosmic_text fallbacks. I should have time later in the week to dig into that and see what's viable.

Bcompartment commented 1 week ago

If it's enabled by default, add an example that shows how to disable this feature. Reasoning is simple: if an app is shipped with a font, why waste time loading all system fonts? IMO add something like:  fn setup(....) {    ....    let system_fonts: SystemFonts = asset_loader.load_sys_fonts(); .... }

So user will have simple way, and control when to load.

bushrat011899 commented 1 week ago

If it's enabled by default...

I've left it is a feature that's disabled by default. To enable system fonts, you enable the system_font feature in Bevy. This feature does have a startup time cost and I completely agree that the functionality should be opt-in, at least while it has a noticeable impact.

TurtIeSocks commented 1 week ago

I've just noticed #14150, was that effort abandoned?

bushrat011899 commented 1 week ago

I never saw that! Looks like it wasn't linked to the original issue. I'm happy to incorporate changes from that PR, or close this out in favour of that one, I'll leave that up to the reviewers.