Skip to content

Commit 2fe1643

Browse files
committed
fix: update documentation, add some customization
TextPlugin now includes general family face overrides
1 parent 6ed8787 commit 2fe1643

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

crates/bevy_text/src/lib.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,33 @@ pub const DEFAULT_FONT_DATA: &[u8] = include_bytes!("FiraMono-subset.ttf");
7979
/// When the `bevy_text` feature is enabled with the `bevy` crate, this
8080
/// plugin is included by default in the `DefaultPlugins`.
8181
pub struct TextPlugin {
82-
/// If true, the [`CosmicFontSystem`] will load system fonts.
82+
/// If `true`, the [`CosmicFontSystem`] will load system fonts.
83+
///
84+
/// Supports Windows, Linux, and MacOS.
85+
///
86+
/// See [`cosmic_text::fontdb::Database::load_system_fonts`] for details.
8387
pub load_system_fonts: bool,
88+
89+
/// Override the family identifier for the system general Serif font
90+
pub family_serif: Option<String>,
91+
/// Override the default identifier for the general Sans-Serif font
92+
pub family_sans_serif: Option<String>,
93+
/// Override the default identifier for the general Cursive font
94+
pub family_cursive: Option<String>,
95+
/// Override the default identifier for the general Fantasy font
96+
pub family_fantasy: Option<String>,
97+
/// Override the default identifier for the general Monospace font
98+
pub family_monospace: Option<String>,
8499
}
85100
impl Default for TextPlugin {
86101
fn default() -> Self {
87102
Self {
88103
load_system_fonts: true,
104+
family_serif: None,
105+
family_sans_serif: None,
106+
family_cursive: None,
107+
family_fantasy: None,
108+
family_monospace: None,
89109
}
90110
}
91111
}
@@ -104,7 +124,7 @@ impl Plugin for TextPlugin {
104124
.init_asset_loader::<FontLoader>()
105125
.init_resource::<FontAtlasSets>()
106126
.init_resource::<TextPipeline>()
107-
.insert_resource(CosmicFontSystem::new(self.load_system_fonts))
127+
.insert_resource(CosmicFontSystem::new_with_settings(self))
108128
.init_resource::<SwashCache>()
109129
.init_resource::<TextIterScratch>()
110130
.add_systems(

crates/bevy_text/src/pipeline.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use cosmic_text::{Attrs, Buffer, Metrics, Shaping, Wrap};
1717

1818
use crate::{
1919
error::TextError, ComputedTextBlock, Font, FontAtlasSets, FontSmoothing, Justify, LineBreak,
20-
PositionedGlyph, TextBounds, TextEntity, TextFont, TextLayout,
20+
PositionedGlyph, TextBounds, TextEntity, TextFont, TextLayout, TextPlugin,
2121
};
2222

2323
/// A wrapper resource around a [`cosmic_text::FontSystem`]
@@ -30,20 +30,39 @@ pub struct CosmicFontSystem(pub cosmic_text::FontSystem);
3030

3131
impl Default for CosmicFontSystem {
3232
fn default() -> Self {
33-
Self::new(true)
33+
Self::new_with_settings(&TextPlugin::default())
3434
}
3535
}
3636

3737
impl CosmicFontSystem {
3838
/// Creates a new, wrapped [`cosmic_text::FontSystem`].
3939
///
40-
/// The option to load system fonts is typically provided via [`TextPlugin`](super::TextPlugin).
41-
pub fn new(load_system_fonts: bool) -> Self {
40+
/// The option to load system fonts is typically provided via the values in [`TextPlugin`].
41+
pub fn new_with_settings(plugin_settings: &TextPlugin) -> Self {
4242
let locale = sys_locale::get_locale().unwrap_or_else(|| String::from("en-US"));
4343
let mut db = cosmic_text::fontdb::Database::new();
44-
if load_system_fonts {
44+
if plugin_settings.load_system_fonts {
4545
db.load_system_fonts();
4646
}
47+
if let Some(family_serif) = &plugin_settings.family_serif {
48+
db.set_serif_family(family_serif.to_string());
49+
}
50+
51+
if let Some(family_sans_serif) = &plugin_settings.family_sans_serif {
52+
db.set_serif_family(family_sans_serif.to_string());
53+
}
54+
55+
if let Some(family_cursive) = &plugin_settings.family_cursive {
56+
db.set_serif_family(family_cursive.to_string());
57+
}
58+
59+
if let Some(family_fantasy) = &plugin_settings.family_fantasy {
60+
db.set_serif_family(family_fantasy.to_string());
61+
}
62+
63+
if let Some(family_monospace) = &plugin_settings.family_monospace {
64+
db.set_serif_family(family_monospace.to_string());
65+
}
4766

4867
Self(cosmic_text::FontSystem::new_with_locale_and_db(locale, db))
4968
}

0 commit comments

Comments
 (0)