Skip to content

Commit e0e5f3a

Browse files
authoredApr 21, 2023
add a default font (bevyengine#8445)
# Objective - Have a default font ## Solution - Add a font based on FiraMono containing only ASCII characters and use it as the default font - It is behind a feature `default_font` enabled by default - I also updated examples to use it, but not UI examples to still show how to use a custom font --- ## Changelog * If you display text without using the default handle provided by `TextStyle`, the text will be displayed
1 parent ddefc24 commit e0e5f3a

34 files changed

+90
-124
lines changed
 

‎Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ default = [
5252
"bevy_gizmos",
5353
"android_shared_stdcxx",
5454
"tonemapping_luts",
55+
"default_font",
5556
]
5657

5758
# Force dynamic linking, which improves iterative compile times
@@ -225,6 +226,9 @@ accesskit_unix = ["bevy_internal/accesskit_unix"]
225226
# Enable assertions to check the validity of parameters passed to glam
226227
glam_assert = ["bevy_internal/glam_assert"]
227228

229+
# Include a default font, containing only ASCII characters, at the cost of a 20kB binary size increase
230+
default_font = ["bevy_internal/default_font"]
231+
228232
[dependencies]
229233
bevy_dylib = { path = "crates/bevy_dylib", version = "0.11.0-dev", default-features = false, optional = true }
230234
bevy_internal = { path = "crates/bevy_internal", version = "0.11.0-dev", default-features = false }

‎crates/bevy_internal/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ bevy_render = ["dep:bevy_render", "bevy_scene?/bevy_render"]
9797
# Enable assertions to check the validity of parameters passed to glam
9898
glam_assert = ["bevy_math/glam_assert"]
9999

100+
default_font = ["bevy_text?/default_font"]
101+
100102
[dependencies]
101103
# bevy
102104
bevy_a11y = { path = "../bevy_a11y", version = "0.11.0-dev" }

‎crates/bevy_text/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ keywords = ["bevy"]
1010

1111
[features]
1212
subpixel_glyph_atlas = []
13+
default_font = []
1314

1415
[dependencies]
1516
# bevy
18.4 KB
Binary file not shown.

‎crates/bevy_text/src/lib.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ pub mod prelude {
2626
}
2727

2828
use bevy_app::prelude::*;
29-
use bevy_asset::AddAsset;
29+
#[cfg(feature = "default_font")]
30+
use bevy_asset::load_internal_binary_asset;
31+
use bevy_asset::{AddAsset, HandleUntyped};
3032
use bevy_ecs::prelude::*;
33+
use bevy_reflect::TypeUuid;
3134
use bevy_render::{camera::CameraUpdateSystem, ExtractSchedule, RenderApp};
3235
use bevy_sprite::SpriteSystem;
3336
use std::num::NonZeroUsize;
@@ -67,6 +70,9 @@ pub enum YAxisOrientation {
6770
BottomToTop,
6871
}
6972

73+
pub const DEFAULT_FONT_HANDLE: HandleUntyped =
74+
HandleUntyped::weak_from_u64(Font::TYPE_UUID, 1491772431825224042);
75+
7076
impl Plugin for TextPlugin {
7177
fn build(&self, app: &mut App) {
7278
app.add_asset::<Font>()
@@ -98,5 +104,13 @@ impl Plugin for TextPlugin {
98104
extract_text2d_sprite.after(SpriteSystem::ExtractSprites),
99105
);
100106
}
107+
108+
#[cfg(feature = "default_font")]
109+
load_internal_binary_asset!(
110+
app,
111+
DEFAULT_FONT_HANDLE,
112+
"FiraMono-subset.ttf",
113+
|bytes: &[u8]| { Font::try_from_bytes(bytes.to_vec()).unwrap() }
114+
);
101115
}
102116
}

‎crates/bevy_text/src/text.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use bevy_utils::default;
66
use serde::{Deserialize, Serialize};
77

88
use crate::Font;
9+
#[cfg(feature = "default_font")]
10+
use crate::DEFAULT_FONT_HANDLE;
911

1012
#[derive(Component, Debug, Clone, Reflect)]
1113
#[reflect(Component, Default)]
@@ -167,7 +169,7 @@ pub struct TextStyle {
167169
impl Default for TextStyle {
168170
fn default() -> Self {
169171
Self {
170-
font: Default::default(),
172+
font: DEFAULT_FONT_HANDLE.typed(),
171173
font_size: 12.0,
172174
color: Color::WHITE,
173175
}

‎docs/cargo_features.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The default feature set enables most of the expected features of a game engine,
2727
|bevy_text|Provides text functionality|
2828
|bevy_ui|A custom ECS-driven UI framework|
2929
|bevy_winit|winit window and input backend|
30+
|default_font|Include a default font, containing only ASCII characters, at the cost of a 20kB binary size increase|
3031
|filesystem_watcher|Enable watching file system for asset hot reload|
3132
|hdr|HDR image format support|
3233
|ktx2|KTX2 compressed texture support|

‎examples/2d/bloom_2d.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ fn setup(
7272
TextBundle::from_section(
7373
"",
7474
TextStyle {
75-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
7675
font_size: 18.0,
7776
color: Color::WHITE,
77+
..default()
7878
},
7979
)
8080
.with_style(Style {

‎examples/3d/3d_gizmos.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ fn setup(
1515
mut commands: Commands,
1616
mut meshes: ResMut<Assets<Mesh>>,
1717
mut materials: ResMut<Assets<StandardMaterial>>,
18-
asset_server: Res<AssetServer>,
1918
) {
2019
commands.spawn(Camera3dBundle {
2120
transform: Transform::from_xyz(0., 1.5, 6.).looking_at(Vec3::ZERO, Vec3::Y),
@@ -48,9 +47,9 @@ fn setup(
4847
commands.spawn(TextBundle::from_section(
4948
"Press 't' to toggle drawing gizmos on top of everything else in the scene",
5049
TextStyle {
51-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
5250
font_size: 24.,
5351
color: Color::WHITE,
52+
..default()
5453
},
5554
));
5655
}

‎examples/3d/anti_aliasing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ fn setup(
329329
TextBundle::from_section(
330330
"",
331331
TextStyle {
332-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
333332
font_size: 20.0,
334333
color: Color::BLACK,
334+
..default()
335335
},
336336
)
337337
.with_style(Style {

‎examples/3d/atmospheric_fog.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ fn setup_terrain_scene(
9393
));
9494
}
9595

96-
fn setup_instructions(mut commands: Commands, asset_server: Res<AssetServer>) {
96+
fn setup_instructions(mut commands: Commands) {
9797
commands.spawn((TextBundle::from_section(
9898
"Press Spacebar to Toggle Atmospheric Fog.\nPress S to Toggle Directional Light Fog Influence.",
9999
TextStyle {
100-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
101100
font_size: 15.0,
102101
color: Color::WHITE,
102+
..default()
103103
},
104104
)
105105
.with_style(Style {

‎examples/3d/blend_modes.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ fn setup(
3434
mut commands: Commands,
3535
mut meshes: ResMut<Assets<Mesh>>,
3636
mut materials: ResMut<Assets<StandardMaterial>>,
37-
asset_server: Res<AssetServer>,
3837
) {
3938
let base_color = Color::rgba(0.9, 0.2, 0.3, 1.0);
4039
let icosphere_mesh = meshes.add(
@@ -186,15 +185,15 @@ fn setup(
186185

187186
// Controls Text
188187
let text_style = TextStyle {
189-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
190188
font_size: 18.0,
191189
color: Color::BLACK,
190+
..default()
192191
};
193192

194193
let label_text_style = TextStyle {
195-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
196194
font_size: 25.0,
197195
color: Color::ORANGE,
196+
..default()
198197
};
199198

200199
commands.spawn(

‎examples/3d/bloom_3d.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ fn setup_scene(
2525
mut commands: Commands,
2626
mut meshes: ResMut<Assets<Mesh>>,
2727
mut materials: ResMut<Assets<StandardMaterial>>,
28-
asset_server: Res<AssetServer>,
2928
) {
3029
commands.spawn((
3130
Camera3dBundle {
@@ -96,9 +95,9 @@ fn setup_scene(
9695
TextBundle::from_section(
9796
"",
9897
TextStyle {
99-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
10098
font_size: 18.0,
10199
color: Color::BLACK,
100+
..default()
102101
},
103102
)
104103
.with_style(Style {

‎examples/3d/fog.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ fn setup_pyramid_scene(
137137
});
138138
}
139139

140-
fn setup_instructions(mut commands: Commands, asset_server: Res<AssetServer>) {
140+
fn setup_instructions(mut commands: Commands) {
141141
commands.spawn((TextBundle::from_section(
142142
"",
143143
TextStyle {
144-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
145144
font_size: 15.0,
146145
color: Color::WHITE,
146+
..default()
147147
},
148148
)
149149
.with_style(Style {

‎examples/3d/parallax_mapping.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,9 @@ fn setup(
315315
commands.spawn(background_cube_bundle(Vec3::new(0., 0., -45.)));
316316

317317
let style = TextStyle {
318-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
319318
font_size: 18.0,
320319
color: Color::WHITE,
320+
..default()
321321
};
322322

323323
commands.spawn(

‎examples/3d/pbr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ fn setup(
7878
TextBundle::from_section(
7979
"Perceptual Roughness",
8080
TextStyle {
81-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
8281
font_size: 36.0,
8382
color: Color::WHITE,
83+
..default()
8484
},
8585
)
8686
.with_style(Style {
@@ -95,9 +95,9 @@ fn setup(
9595
text: Text::from_section(
9696
"Metallic",
9797
TextStyle {
98-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
9998
font_size: 36.0,
10099
color: Color::WHITE,
100+
..default()
101101
},
102102
),
103103
style: Style {
@@ -117,9 +117,9 @@ fn setup(
117117
TextBundle::from_section(
118118
"Loading Environment Map...",
119119
TextStyle {
120-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
121120
font_size: 36.0,
122121
color: Color::RED,
122+
..default()
123123
},
124124
)
125125
.with_style(Style {

‎examples/3d/tonemapping.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ fn setup(
7575
TextBundle::from_section(
7676
"",
7777
TextStyle {
78-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
7978
font_size: 18.0,
8079
color: Color::WHITE,
80+
..default()
8181
},
8282
)
8383
.with_style(Style {
@@ -243,7 +243,6 @@ fn setup_image_viewer_scene(
243243
mut meshes: ResMut<Assets<Mesh>>,
244244
mut materials: ResMut<Assets<StandardMaterial>>,
245245
camera_transform: Res<CameraTransform>,
246-
asset_server: Res<AssetServer>,
247246
) {
248247
let mut transform = camera_transform.0;
249248
transform.translation += transform.forward();
@@ -273,9 +272,9 @@ fn setup_image_viewer_scene(
273272
TextBundle::from_section(
274273
"Drag and drop an HDR or EXR file",
275274
TextStyle {
276-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
277275
font_size: 36.0,
278276
color: Color::BLACK,
277+
..default()
279278
},
280279
)
281280
.with_text_alignment(TextAlignment::Center)

‎examples/async_tasks/external_source_external_thread.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ fn main() {
1919
struct StreamReceiver(Receiver<u32>);
2020
struct StreamEvent(u32);
2121

22-
#[derive(Resource, Deref)]
23-
struct LoadedFont(Handle<Font>);
24-
25-
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
22+
fn setup(mut commands: Commands) {
2623
commands.spawn(Camera2dBundle::default());
2724

2825
let (tx, rx) = bounded::<u32>(10);
@@ -40,7 +37,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
4037
});
4138

4239
commands.insert_resource(StreamReceiver(rx));
43-
commands.insert_resource(LoadedFont(asset_server.load("fonts/FiraSans-Bold.ttf")));
4440
}
4541

4642
// This system reads from the receiver and sends events to Bevy
@@ -50,15 +46,11 @@ fn read_stream(receiver: Res<StreamReceiver>, mut events: EventWriter<StreamEven
5046
}
5147
}
5248

53-
fn spawn_text(
54-
mut commands: Commands,
55-
mut reader: EventReader<StreamEvent>,
56-
loaded_font: Res<LoadedFont>,
57-
) {
49+
fn spawn_text(mut commands: Commands, mut reader: EventReader<StreamEvent>) {
5850
let text_style = TextStyle {
59-
font: loaded_font.clone(),
6051
font_size: 20.0,
6152
color: Color::WHITE,
53+
..default()
6254
};
6355

6456
for (per_frame, event) in reader.iter().enumerate() {

‎examples/ecs/apply_system_buffers.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct AppleCount;
6363
struct OrangeCount;
6464

6565
// Setup the counters in the UI.
66-
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
66+
fn setup(mut commands: Commands) {
6767
commands.spawn(Camera2dBundle::default());
6868

6969
commands
@@ -82,9 +82,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
8282
TextBundle::from_section(
8383
"Apple: nothing counted yet".to_string(),
8484
TextStyle {
85-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
8685
font_size: 80.0,
8786
color: Color::ORANGE,
87+
..default()
8888
},
8989
),
9090
AppleCount,
@@ -93,9 +93,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
9393
TextBundle::from_section(
9494
"Orange: nothing counted yet".to_string(),
9595
TextStyle {
96-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
9796
font_size: 80.0,
9897
color: Color::ORANGE,
98+
..default()
9999
},
100100
),
101101
OrangeCount,

‎examples/ecs/state.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn setup(mut commands: Commands) {
4848
commands.spawn(Camera2dBundle::default());
4949
}
5050

51-
fn setup_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
51+
fn setup_menu(mut commands: Commands) {
5252
let button_entity = commands
5353
.spawn(NodeBundle {
5454
style: Style {
@@ -78,9 +78,9 @@ fn setup_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
7878
parent.spawn(TextBundle::from_section(
7979
"Play",
8080
TextStyle {
81-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
8281
font_size: 40.0,
8382
color: Color::rgb(0.9, 0.9, 0.9),
83+
..default()
8484
},
8585
));
8686
});

‎examples/games/alien_cake_addict.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
168168
TextBundle::from_section(
169169
"Score:",
170170
TextStyle {
171-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
172171
font_size: 40.0,
173172
color: Color::rgb(0.5, 0.5, 1.0),
173+
..default()
174174
},
175175
)
176176
.with_style(Style {
@@ -384,7 +384,7 @@ fn gameover_keyboard(
384384
}
385385

386386
// display the number of cake eaten before losing
387-
fn display_score(mut commands: Commands, asset_server: Res<AssetServer>, game: Res<Game>) {
387+
fn display_score(mut commands: Commands, game: Res<Game>) {
388388
commands
389389
.spawn(NodeBundle {
390390
style: Style {
@@ -399,9 +399,9 @@ fn display_score(mut commands: Commands, asset_server: Res<AssetServer>, game: R
399399
parent.spawn(TextBundle::from_section(
400400
format!("Cake eaten: {}", game.cake_eaten),
401401
TextStyle {
402-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
403402
font_size: 80.0,
404403
color: Color::rgb(0.5, 0.5, 1.0),
404+
..default()
405405
},
406406
));
407407
});

‎examples/games/breakout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,15 @@ fn setup(
223223
TextSection::new(
224224
"Score: ",
225225
TextStyle {
226-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
227226
font_size: SCOREBOARD_FONT_SIZE,
228227
color: TEXT_COLOR,
228+
..default()
229229
},
230230
),
231231
TextSection::from_style(TextStyle {
232-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
233232
font_size: SCOREBOARD_FONT_SIZE,
234233
color: SCORE_COLOR,
234+
..default()
235235
}),
236236
])
237237
.with_style(Style {

‎examples/games/game_menu.rs

+12-24
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,9 @@ mod game {
145145

146146
fn game_setup(
147147
mut commands: Commands,
148-
asset_server: Res<AssetServer>,
149148
display_quality: Res<DisplayQuality>,
150149
volume: Res<Volume>,
151150
) {
152-
let font = asset_server.load("fonts/FiraSans-Bold.ttf");
153-
154151
commands
155152
.spawn((
156153
NodeBundle {
@@ -187,9 +184,9 @@ mod game {
187184
TextBundle::from_section(
188185
"Will be back to the menu shortly...",
189186
TextStyle {
190-
font: font.clone(),
191187
font_size: 80.0,
192188
color: TEXT_COLOR,
189+
..default()
193190
},
194191
)
195192
.with_style(Style {
@@ -202,25 +199,25 @@ mod game {
202199
TextSection::new(
203200
format!("quality: {:?}", *display_quality),
204201
TextStyle {
205-
font: font.clone(),
206202
font_size: 60.0,
207203
color: Color::BLUE,
204+
..default()
208205
},
209206
),
210207
TextSection::new(
211208
" - ",
212209
TextStyle {
213-
font: font.clone(),
214210
font_size: 60.0,
215211
color: TEXT_COLOR,
212+
..default()
216213
},
217214
),
218215
TextSection::new(
219216
format!("volume: {:?}", *volume),
220217
TextStyle {
221-
font: font.clone(),
222218
font_size: 60.0,
223219
color: Color::GREEN,
220+
..default()
224221
},
225222
),
226223
])
@@ -398,7 +395,6 @@ mod menu {
398395
}
399396

400397
fn main_menu_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
401-
let font = asset_server.load("fonts/FiraSans-Bold.ttf");
402398
// Common style for all buttons on the screen
403399
let button_style = Style {
404400
size: Size::new(Val::Px(250.0), Val::Px(65.0)),
@@ -417,9 +413,9 @@ mod menu {
417413
..default()
418414
};
419415
let button_text_style = TextStyle {
420-
font: font.clone(),
421416
font_size: 40.0,
422417
color: TEXT_COLOR,
418+
..default()
423419
};
424420

425421
commands
@@ -452,9 +448,9 @@ mod menu {
452448
TextBundle::from_section(
453449
"Bevy Game Menu UI",
454450
TextStyle {
455-
font: font.clone(),
456451
font_size: 80.0,
457452
color: TEXT_COLOR,
453+
..default()
458454
},
459455
)
460456
.with_style(Style {
@@ -531,7 +527,7 @@ mod menu {
531527
});
532528
}
533529

534-
fn settings_menu_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
530+
fn settings_menu_setup(mut commands: Commands) {
535531
let button_style = Style {
536532
size: Size::new(Val::Px(200.0), Val::Px(65.0)),
537533
margin: UiRect::all(Val::Px(20.0)),
@@ -541,9 +537,9 @@ mod menu {
541537
};
542538

543539
let button_text_style = TextStyle {
544-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
545540
font_size: 40.0,
546541
color: TEXT_COLOR,
542+
..default()
547543
};
548544

549545
commands
@@ -596,11 +592,7 @@ mod menu {
596592
});
597593
}
598594

599-
fn display_settings_menu_setup(
600-
mut commands: Commands,
601-
asset_server: Res<AssetServer>,
602-
display_quality: Res<DisplayQuality>,
603-
) {
595+
fn display_settings_menu_setup(mut commands: Commands, display_quality: Res<DisplayQuality>) {
604596
let button_style = Style {
605597
size: Size::new(Val::Px(200.0), Val::Px(65.0)),
606598
margin: UiRect::all(Val::Px(20.0)),
@@ -609,9 +601,9 @@ mod menu {
609601
..default()
610602
};
611603
let button_text_style = TextStyle {
612-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
613604
font_size: 40.0,
614605
color: TEXT_COLOR,
606+
..default()
615607
};
616608

617609
commands
@@ -698,11 +690,7 @@ mod menu {
698690
});
699691
}
700692

701-
fn sound_settings_menu_setup(
702-
mut commands: Commands,
703-
asset_server: Res<AssetServer>,
704-
volume: Res<Volume>,
705-
) {
693+
fn sound_settings_menu_setup(mut commands: Commands, volume: Res<Volume>) {
706694
let button_style = Style {
707695
size: Size::new(Val::Px(200.0), Val::Px(65.0)),
708696
margin: UiRect::all(Val::Px(20.0)),
@@ -711,9 +699,9 @@ mod menu {
711699
..default()
712700
};
713701
let button_text_style = TextStyle {
714-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
715702
font_size: 40.0,
716703
color: TEXT_COLOR,
704+
..default()
717705
};
718706

719707
commands

‎examples/mobile/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ fn setup_scene(
5555
mut commands: Commands,
5656
mut meshes: ResMut<Assets<Mesh>>,
5757
mut materials: ResMut<Assets<StandardMaterial>>,
58-
asset_server: Res<AssetServer>,
5958
) {
6059
// plane
6160
commands.spawn(PbrBundle {
@@ -122,9 +121,9 @@ fn setup_scene(
122121
TextBundle::from_section(
123122
"Test Button",
124123
TextStyle {
125-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
126124
font_size: 30.0,
127125
color: Color::BLACK,
126+
..default()
128127
},
129128
)
130129
.with_text_alignment(TextAlignment::Center),

‎examples/scene/scene.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,15 @@ fn save_scene_system(world: &mut World) {
140140

141141
// This is only necessary for the info message in the UI. See examples/ui/text.rs for a standalone
142142
// text example.
143-
fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
143+
fn infotext_system(mut commands: Commands) {
144144
commands.spawn(Camera2dBundle::default());
145145
commands.spawn(
146146
TextBundle::from_section(
147147
"Nothing to see in this window! Check the console output!",
148148
TextStyle {
149-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
150149
font_size: 50.0,
151150
color: Color::WHITE,
151+
..default()
152152
},
153153
)
154154
.with_style(Style {

‎examples/shader/shader_prepass.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ fn setup(
126126
});
127127

128128
let style = TextStyle {
129-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
130129
font_size: 18.0,
131130
color: Color::WHITE,
131+
..default()
132132
};
133133

134134
commands.spawn(

‎examples/stress_tests/bevymark.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
103103
TextSection::new(
104104
value,
105105
TextStyle {
106-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
107106
font_size: 40.0,
108107
color,
108+
..default()
109109
},
110110
)
111111
};

‎examples/stress_tests/many_buttons.rs

+3-23
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ fn main() {
3232
}))
3333
.add_plugin(FrameTimeDiagnosticsPlugin::default())
3434
.add_plugin(LogDiagnosticsPlugin::default())
35-
.init_resource::<UiFont>()
3635
.add_systems(Startup, setup)
3736
.add_systems(Update, button_system);
3837

@@ -69,17 +68,7 @@ fn button_system(
6968
}
7069
}
7170

72-
#[derive(Resource)]
73-
struct UiFont(Handle<Font>);
74-
75-
impl FromWorld for UiFont {
76-
fn from_world(world: &mut World) -> Self {
77-
let asset_server = world.resource::<AssetServer>();
78-
UiFont(asset_server.load("fonts/FiraSans-Bold.ttf"))
79-
}
80-
}
81-
82-
fn setup(mut commands: Commands, font: Res<UiFont>) {
71+
fn setup(mut commands: Commands) {
8372
let count = ROW_COLUMN_COUNT;
8473
let count_f = count as f32;
8574
let as_rainbow = |i: usize| Color::hsl((i as f32 / count_f) * 360.0, 0.9, 0.8);
@@ -97,23 +86,14 @@ fn setup(mut commands: Commands, font: Res<UiFont>) {
9786
for i in 0..count {
9887
for j in 0..count {
9988
let color = as_rainbow(j % i.max(1)).into();
100-
spawn_button(
101-
commands,
102-
font.0.clone_weak(),
103-
color,
104-
count_f,
105-
i,
106-
j,
107-
spawn_text,
108-
);
89+
spawn_button(commands, color, count_f, i, j, spawn_text);
10990
}
11091
}
11192
});
11293
}
11394

11495
fn spawn_button(
11596
commands: &mut ChildBuilder,
116-
font: Handle<Font>,
11797
color: BackgroundColor,
11898
total: f32,
11999
i: usize,
@@ -142,9 +122,9 @@ fn spawn_button(
142122
commands.spawn(TextBundle::from_section(
143123
format!("{i}, {j}"),
144124
TextStyle {
145-
font,
146125
font_size: FONT_SIZE,
147126
color: Color::rgb(0.2, 0.2, 0.2),
127+
..default()
148128
},
149129
));
150130
});

‎examples/stress_tests/many_gizmos.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn system(config: Res<Config>, time: Res<Time>, mut draw: Gizmos) {
7373
}
7474
}
7575

76-
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
76+
fn setup(mut commands: Commands) {
7777
warn!(include_str!("warning_string.txt"));
7878

7979
commands.spawn(Camera3dBundle {
@@ -84,7 +84,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
8484
commands.spawn(TextBundle::from_section(
8585
"",
8686
TextStyle {
87-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
8887
font_size: 30.,
8988
..default()
9089
},

‎examples/stress_tests/many_glyphs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ fn main() {
2424
.run();
2525
}
2626

27-
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
27+
fn setup(mut commands: Commands) {
2828
commands.spawn(Camera2dBundle::default());
2929
let mut text = Text {
3030
sections: vec![TextSection {
3131
value: "0123456789".repeat(10_000),
3232
style: TextStyle {
33-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
3433
font_size: 4.,
3534
color: Color::WHITE,
35+
..default()
3636
},
3737
}],
3838
alignment: TextAlignment::Left,

‎examples/tools/gamepad_viewer.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,6 @@ impl FromWorld for ButtonMeshes {
8181
}
8282
}
8383
}
84-
#[derive(Resource, Deref)]
85-
struct FontHandle(Handle<Font>);
86-
impl FromWorld for FontHandle {
87-
fn from_world(world: &mut World) -> Self {
88-
let asset_server = world.resource::<AssetServer>();
89-
Self(asset_server.load("fonts/FiraSans-Bold.ttf"))
90-
}
91-
}
9284

9385
#[derive(Bundle)]
9486
struct GamepadButtonBundle {
@@ -126,7 +118,6 @@ fn main() {
126118
.add_plugins(DefaultPlugins)
127119
.init_resource::<ButtonMaterials>()
128120
.init_resource::<ButtonMeshes>()
129-
.init_resource::<FontHandle>()
130121
.add_systems(
131122
Startup,
132123
(setup, setup_sticks, setup_triggers, setup_connected),
@@ -273,7 +264,6 @@ fn setup_sticks(
273264
meshes: Res<ButtonMeshes>,
274265
materials: Res<ButtonMaterials>,
275266
gamepad_settings: Res<GamepadSettings>,
276-
font: Res<FontHandle>,
277267
) {
278268
let dead_upper =
279269
STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.deadzone_upperbound();
@@ -329,7 +319,7 @@ fn setup_sticks(
329319
let style = TextStyle {
330320
font_size: 16.,
331321
color: TEXT_COLOR,
332-
font: font.clone(),
322+
..default()
333323
};
334324
parent.spawn((
335325
Text2dBundle {
@@ -392,7 +382,6 @@ fn setup_triggers(
392382
mut commands: Commands,
393383
meshes: Res<ButtonMeshes>,
394384
materials: Res<ButtonMaterials>,
395-
font: Res<FontHandle>,
396385
) {
397386
let mut spawn_trigger = |x, y, button_type| {
398387
commands
@@ -410,9 +399,9 @@ fn setup_triggers(
410399
text: Text::from_section(
411400
format!("{:.3}", 0.),
412401
TextStyle {
413-
font: font.clone(),
414402
font_size: 16.,
415403
color: TEXT_COLOR,
404+
..default()
416405
},
417406
),
418407
..default()
@@ -434,11 +423,11 @@ fn setup_triggers(
434423
);
435424
}
436425

437-
fn setup_connected(mut commands: Commands, font: Res<FontHandle>) {
426+
fn setup_connected(mut commands: Commands) {
438427
let style = TextStyle {
439428
color: TEXT_COLOR,
440429
font_size: 30.,
441-
font: font.clone(),
430+
..default()
442431
};
443432
commands.spawn((
444433
TextBundle::from_sections([

‎examples/window/low_power.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ pub(crate) mod test_setup {
148148
mut meshes: ResMut<Assets<Mesh>>,
149149
mut materials: ResMut<Assets<StandardMaterial>>,
150150
mut event: EventWriter<RequestRedraw>,
151-
asset_server: Res<AssetServer>,
152151
) {
153152
commands.spawn((
154153
PbrBundle {
@@ -177,28 +176,28 @@ pub(crate) mod test_setup {
177176
TextSection::new(
178177
"Press spacebar to cycle modes\n",
179178
TextStyle {
180-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
181179
font_size: 50.0,
182180
color: Color::WHITE,
181+
..default()
183182
},
184183
),
185184
TextSection::from_style(TextStyle {
186-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
187185
font_size: 50.0,
188186
color: Color::GREEN,
187+
..default()
189188
}),
190189
TextSection::new(
191190
"\nFrame: ",
192191
TextStyle {
193-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
194192
font_size: 50.0,
195193
color: Color::YELLOW,
194+
..default()
196195
},
197196
),
198197
TextSection::from_style(TextStyle {
199-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
200198
font_size: 50.0,
201199
color: Color::YELLOW,
200+
..default()
202201
}),
203202
])
204203
.with_style(Style {

‎examples/window/scale_factor_override.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main() {
2020
.run();
2121
}
2222

23-
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
23+
fn setup(mut commands: Commands) {
2424
// camera
2525
commands.spawn(Camera2dBundle::default());
2626
// root node
@@ -50,9 +50,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
5050
TextBundle::from_section(
5151
"Example text",
5252
TextStyle {
53-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
5453
font_size: 30.0,
5554
color: Color::WHITE,
55+
..default()
5656
},
5757
)
5858
.with_style(Style {

‎examples/window/window_resizing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn setup_camera(mut cmd: Commands) {
3232
}
3333

3434
// Spawns the UI
35-
fn setup_ui(mut cmd: Commands, asset_server: Res<AssetServer>) {
35+
fn setup_ui(mut cmd: Commands) {
3636
// Node that fills entire background
3737
cmd.spawn(NodeBundle {
3838
style: Style {
@@ -47,9 +47,9 @@ fn setup_ui(mut cmd: Commands, asset_server: Res<AssetServer>) {
4747
TextBundle::from_section(
4848
"Resolution",
4949
TextStyle {
50-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
5150
font_size: 50.0,
5251
color: Color::BLACK,
52+
..default()
5353
},
5454
),
5555
ResolutionText,

0 commit comments

Comments
 (0)
Please sign in to comment.