Skip to content

Commit 4ffb740

Browse files
committed
add ability to load system fonts via fontdb
1 parent 1fd3bfb commit 4ffb740

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

crates/bevy_text/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,17 @@ pub const DEFAULT_FONT_DATA: &[u8] = include_bytes!("FiraMono-subset.ttf");
7575
///
7676
/// When the `bevy_text` feature is enabled with the `bevy` crate, this
7777
/// plugin is included by default in the `DefaultPlugins`.
78-
#[derive(Default)]
79-
pub struct TextPlugin;
78+
pub struct TextPlugin {
79+
/// If true, the [`CosmicFontSystem`] will load system fonts.
80+
pub load_system_fonts: bool,
81+
}
82+
impl Default for TextPlugin {
83+
fn default() -> Self {
84+
Self {
85+
load_system_fonts: true,
86+
}
87+
}
88+
}
8089

8190
/// System set in [`PostUpdate`] where all 2d text update systems are executed.
8291
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
@@ -92,7 +101,7 @@ impl Plugin for TextPlugin {
92101
.init_asset_loader::<FontLoader>()
93102
.init_resource::<FontAtlasSets>()
94103
.init_resource::<TextPipeline>()
95-
.init_resource::<CosmicFontSystem>()
104+
.insert_resource(CosmicFontSystem::new(self.load_system_fonts))
96105
.init_resource::<SwashCache>()
97106
.init_resource::<TextIterScratch>()
98107
.add_systems(

crates/bevy_text/src/pipeline.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,20 @@ pub struct CosmicFontSystem(pub cosmic_text::FontSystem);
3030

3131
impl Default for CosmicFontSystem {
3232
fn default() -> Self {
33+
Self::new(true)
34+
}
35+
}
36+
37+
impl CosmicFontSystem {
38+
/// Creates a new, wrapped [`cosmic_text::FontSystem`].
39+
///
40+
/// The option to load system fonts is typically provided via [`TextPlugin`](super::TextPlugin).
41+
pub fn new(load_system_fonts: bool) -> Self {
3342
let locale = sys_locale::get_locale().unwrap_or_else(|| String::from("en-US"));
34-
let db = cosmic_text::fontdb::Database::new();
35-
// TODO: consider using `cosmic_text::FontSystem::new()` (load system fonts by default)
43+
let mut db = cosmic_text::fontdb::Database::new();
44+
if load_system_fonts {
45+
db.load_system_fonts();
46+
}
3647
Self(cosmic_text::FontSystem::new_with_locale_and_db(locale, db))
3748
}
3849
}

0 commit comments

Comments
 (0)