Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a default font #1790

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ members = ["crates/*", "examples/ios", "tools/ci"]
[features]
default = [
"bevy_audio",
"bevy_default_assets",
"bevy_dynamic_plugin",
"bevy_gilrs",
"bevy_gltf",
Expand All @@ -48,6 +49,9 @@ bevy_gltf = ["bevy_internal/bevy_gltf"]
bevy_wgpu = ["bevy_internal/bevy_wgpu"]
bevy_winit = ["bevy_internal/bevy_winit"]

# Include default bevy assets
bevy_default_assets = ["render", "bevy_internal/bevy_default_assets"]

trace_chrome = ["bevy_internal/trace_chrome"]
trace = ["bevy_internal/trace"]
wgpu_trace = ["bevy_internal/wgpu_trace"]
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ x11 = ["bevy_winit/x11"]
# enable rendering of font glyphs using subpixel accuracy
subpixel_glyph_atlas = ["bevy_text/subpixel_glyph_atlas"]

# include default bevy assets
bevy_default_assets = ["bevy_text/bevy_default_assets"]

[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.4.0" }
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_text/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ keywords = ["bevy"]

[features]
subpixel_glyph_atlas = []
bevy_default_assets = []

[dependencies]
# bevy
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions crates/bevy_text/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@ impl Plugin for TextPlugin {
.insert_resource(DefaultTextPipeline::default())
.add_system_to_stage(CoreStage::PostUpdate, text2d_system.system())
.add_system_to_stage(RenderStage::Draw, text2d::draw_text2d_system.system());

#[cfg(feature = "bevy_default_assets")]
text::default_font::load_default_font(app);
}
}
29 changes: 28 additions & 1 deletion crates/bevy_text/src/text.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy_asset::Handle;
use bevy_asset::{Handle, HandleUntyped};
use bevy_math::Size;
use bevy_reflect::TypeUuid;
use bevy_render::color::Color;
use glyph_brush_layout::{HorizontalAlign, VerticalAlign};

Expand Down Expand Up @@ -91,9 +92,16 @@ pub struct TextStyle {
pub color: Color,
}

#[cfg(feature = "bevy_default_assets")]
pub const DEFAULT_FONT_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(crate::font::Font::TYPE_UUID, 12210261929130131812);

impl Default for TextStyle {
fn default() -> Self {
Self {
#[cfg(feature = "bevy_default_assets")]
font: DEFAULT_FONT_HANDLE.typed(),
#[cfg(not(feature = "bevy_default_assets"))]
font: Default::default(),
font_size: 12.0,
color: Color::WHITE,
Expand All @@ -105,3 +113,22 @@ impl Default for TextStyle {
pub struct Text2dSize {
pub size: Size,
}

#[cfg(feature = "bevy_default_assets")]
pub(crate) mod default_font {
use crate::{Font, DEFAULT_FONT_HANDLE};
use ab_glyph::{FontArc, FontRef};
use bevy_app::AppBuilder;
use bevy_asset::Assets;

pub(crate) fn load_default_font(app: &mut AppBuilder) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not a startup system?

let world = app.world_mut();
let world_cell = world.cell();
let mut fonts = world_cell.get_resource_mut::<Assets<Font>>().unwrap();
let font_bytes = include_bytes!("assets/fonts/FiraSans-Bold.ttf");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds ~500KB to the size of the bevy_text package (currently lib.rs thinks it is 1.5MB, so an additional 1/3).

In theory, we should have this in a different crate, however because the amount of metadata needed for creating a crate anyway, it might not be worthwhile, if 95% of people leave the default font in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a default assets crate? if other fonts / binary assets get added, could go in the same one?

let font = FontRef::try_from_slice(font_bytes).unwrap();
let font = FontArc::new(font);
let font = Font { font };
fonts.set_untracked(DEFAULT_FONT_HANDLE, font);
}
}
2 changes: 1 addition & 1 deletion crates/bevy_ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ bevy_utils = { path = "../bevy_utils", version = "0.4.0" }
# other
stretch = "0.3"
serde = {version = "1", features = ["derive"]}
smallvec = "1.4"
smallvec = "1.4"
4 changes: 2 additions & 2 deletions examples/2d/contributors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,17 @@ fn setup(
TextSection {
value: "Contributor showcase".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 60.0,
color: Color::WHITE,
..Default::default()
},
},
TextSection {
value: "".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 60.0,
color: Color::WHITE,
..Default::default()
},
},
],
Expand Down
4 changes: 2 additions & 2 deletions examples/2d/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ fn main() {
.run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
fn setup(mut commands: Commands) {
// 2d camera
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(Text2dBundle {
text: Text::with_section(
"This text is in the 2D scene.",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 60.0,
color: Color::WHITE,
..Default::default()
},
TextAlignment {
vertical: VerticalAlign::Center,
Expand Down
8 changes: 2 additions & 6 deletions examples/ecs/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ struct MenuData {
button_entity: Entity,
}

fn setup_menu(
mut commands: Commands,
asset_server: Res<AssetServer>,
button_materials: Res<ButtonMaterials>,
) {
fn setup_menu(mut commands: Commands, button_materials: Res<ButtonMaterials>) {
// ui camera
commands.spawn_bundle(UiCameraBundle::default());
let button_entity = commands
Expand All @@ -56,9 +52,9 @@ fn setup_menu(
text: Text::with_section(
"Play",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
..Default::default()
},
Default::default(),
),
Expand Down
5 changes: 2 additions & 3 deletions examples/game/alien_cake_addict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
text: Text::with_section(
"Score:",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 40.0,
color: Color::rgb(0.5, 0.5, 1.0),
..Default::default()
},
Default::default(),
),
Expand Down Expand Up @@ -365,7 +365,6 @@ fn gameover_keyboard(mut state: ResMut<State<GameState>>, keyboard_input: Res<In
// display the number of cake eaten before losing
fn display_score(
mut commands: Commands,
asset_server: Res<AssetServer>,
game: Res<Game>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
Expand All @@ -385,9 +384,9 @@ fn display_score(
text: Text::with_section(
format!("Cake eaten: {}", game.cake_eaten),
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 80.0,
color: Color::rgb(0.5, 0.5, 1.0),
..Default::default()
},
Default::default(),
),
Expand Down
2 changes: 1 addition & 1 deletion examples/game/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ fn setup(
TextSection {
value: "Score: ".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 40.0,
color: Color::rgb(0.5, 0.5, 1.0),
..Default::default()
},
},
TextSection {
Expand Down
4 changes: 2 additions & 2 deletions examples/scene/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn save_scene_system(world: &mut World) {

// This is only necessary for the info message in the UI. See examples/ui/text.rs for a standalone
// text example.
fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
fn infotext_system(mut commands: Commands) {
commands.spawn_bundle(UiCameraBundle::default());
commands.spawn_bundle(TextBundle {
style: Style {
Expand All @@ -112,9 +112,9 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
text: Text::with_section(
"Nothing to see in this window! Check the console output!",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 50.0,
color: Color::WHITE,
..Default::default()
},
Default::default(),
),
Expand Down
10 changes: 5 additions & 5 deletions examples/tools/bevymark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn main() {
.run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
fn setup(mut commands: Commands) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(UiCameraBundle::default());
commands.spawn_bundle(TextBundle {
Expand All @@ -61,33 +61,33 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
TextSection {
value: "Bird Count: ".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 40.0,
color: Color::rgb(0.0, 1.0, 0.0),
..Default::default()
},
},
TextSection {
value: "".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 40.0,
color: Color::rgb(0.0, 1.0, 1.0),
..Default::default()
},
},
TextSection {
value: "\nAverage FPS: ".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 40.0,
color: Color::rgb(0.0, 1.0, 0.0),
..Default::default()
},
},
TextSection {
value: "".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 40.0,
color: Color::rgb(0.0, 1.0, 1.0),
..Default::default()
},
},
],
Expand Down
8 changes: 2 additions & 6 deletions examples/ui/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ fn button_system(
}
}

fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
button_materials: Res<ButtonMaterials>,
) {
fn setup(mut commands: Commands, button_materials: Res<ButtonMaterials>) {
// ui camera
commands.spawn_bundle(UiCameraBundle::default());
commands
Expand All @@ -82,9 +78,9 @@ fn setup(
text: Text::with_section(
"Button",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
..Default::default()
},
Default::default(),
),
Expand Down
8 changes: 4 additions & 4 deletions examples/ui/font_atlas_debug.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy::text::DEFAULT_FONT_HANDLE;
use bevy::{prelude::*, text::FontAtlasSet};

// TODO: This is now broken. See #1243
Expand Down Expand Up @@ -77,17 +78,16 @@ fn text_update_system(mut state: ResMut<State>, time: Res<Time>, mut query: Quer
}
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut state: ResMut<State>) {
let font_handle = asset_server.load("fonts/FiraSans-Bold.ttf");
state.handle = font_handle.clone();
fn setup(mut commands: Commands, mut state: ResMut<State>) {
state.handle = DEFAULT_FONT_HANDLE.typed();
commands.spawn_bundle(UiCameraBundle::default());
commands.spawn_bundle(TextBundle {
text: Text::with_section(
"a",
TextStyle {
font: font_handle,
font_size: 60.0,
color: Color::YELLOW,
..Default::default()
},
Default::default(),
),
Expand Down
4 changes: 2 additions & 2 deletions examples/ui/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Accepts a `String` or any type that converts into a `String`, such as `&str`
"hello\nbevy!",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 100.0,
color: Color::WHITE,
..Default::default()
},
// Note: You can use `Default::default()` in place of the `TextAlignment`
TextAlignment {
Expand All @@ -70,9 +70,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
TextSection {
value: "FPS: ".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 60.0,
color: Color::WHITE,
..Default::default()
},
},
TextSection {
Expand Down
5 changes: 3 additions & 2 deletions examples/ui/text_debug.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy::text::DEFAULT_FONT_HANDLE;
use bevy::{
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
prelude::*,
Expand All @@ -19,8 +20,8 @@ fn main() {

struct TextChanges;

fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
let font = asset_server.load("fonts/FiraSans-Bold.ttf");
fn infotext_system(mut commands: Commands) {
let font = DEFAULT_FONT_HANDLE.typed();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this variable is no longer needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want to change something back to that font youd need it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can replace its TextStyle with a style using ..TextStyle::Default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess? i don't really see why it matters / quad_handle works the same way, why not just leave it exposed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's just that the mechanism is weird, especially the .typed() thing

I'm not saying not to leave it exposed, I'm more suggesting not to use it in the example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, fair point

commands.spawn_bundle(UiCameraBundle::default());
commands.spawn_bundle(TextBundle {
style: Style {
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ fn setup(
text: Text::with_section(
"Text Example",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 30.0,
color: Color::WHITE,
..Default::default()
},
Default::default(),
),
Expand Down
8 changes: 2 additions & 6 deletions examples/window/scale_factor_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ fn main() {
.run();
}

fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
fn setup(mut commands: Commands, mut materials: ResMut<Assets<ColorMaterial>>) {
// ui camera
commands.spawn_bundle(UiCameraBundle::default());
// root node
Expand Down Expand Up @@ -54,9 +50,9 @@ fn setup(
text: Text::with_section(
"Example text",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 30.0,
color: Color::WHITE,
..Default::default()
},
Default::default(),
),
Expand Down