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 #8445

Merged
merged 4 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -52,6 +52,7 @@ default = [
"bevy_gizmos",
"android_shared_stdcxx",
"tonemapping_luts",
"default_font",
]

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

# Include a default font, containing only ASCII characters, at the cost of a 20kB binary size increase
default_font = ["bevy_internal/default_font"]

[dependencies]
bevy_dylib = { path = "crates/bevy_dylib", version = "0.11.0-dev", default-features = false, optional = true }
bevy_internal = { path = "crates/bevy_internal", version = "0.11.0-dev", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ bevy_render = ["dep:bevy_render", "bevy_scene?/bevy_render"]
# Enable assertions to check the validity of parameters passed to glam
glam_assert = ["bevy_math/glam_assert"]

default_font = ["bevy_text?/default_font"]
Copy link
Contributor

Choose a reason for hiding this comment

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

@mockersf is that ? a typo or a special syntax I don't know? 😊

Copy link
Member Author

@mockersf mockersf Jun 21, 2023

Choose a reason for hiding this comment

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

Not a typo! https://doc.rust-lang.org/cargo/reference/features.html#dependency-features

it means "enable default_font feature of optional crate bevy_text only if the crate is enabled from another feature, otherwise just ignore it"

Copy link
Contributor

Choose a reason for hiding this comment

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

Sweet, I didn't know about that feature, quite useful. Thanks!


[dependencies]
# bevy
bevy_a11y = { path = "../bevy_a11y", version = "0.11.0-dev" }
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 @@ -10,6 +10,7 @@ keywords = ["bevy"]

[features]
subpixel_glyph_atlas = []
default_font = []

[dependencies]
# bevy
Expand Down
Binary file added crates/bevy_text/src/FiraMono-subset.ttf
Binary file not shown.
18 changes: 18 additions & 0 deletions crates/bevy_text/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod pipeline;
mod text;
mod text2d;

#[cfg(feature = "default_font")]
use bevy_reflect::TypeUuid;
mockersf marked this conversation as resolved.
Show resolved Hide resolved
pub use error::*;
pub use font::*;
pub use font_atlas::*;
Expand All @@ -26,7 +28,11 @@ pub mod prelude {
}

use bevy_app::prelude::*;
#[cfg(feature = "default_font")]
use bevy_asset::load_internal_binary_asset;
mockersf marked this conversation as resolved.
Show resolved Hide resolved
use bevy_asset::AddAsset;
#[cfg(feature = "default_font")]
use bevy_asset::HandleUntyped;
use bevy_ecs::prelude::*;
use bevy_render::{camera::CameraUpdateSystem, ExtractSchedule, RenderApp};
use bevy_sprite::SpriteSystem;
Expand Down Expand Up @@ -67,6 +73,10 @@ pub enum YAxisOrientation {
BottomToTop,
}

#[cfg(feature = "default_font")]
pub const DEFAULT_FONT_HANDLE: HandleUntyped =
Copy link
Member

Choose a reason for hiding this comment

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

I agree with @UkoeHB: I think this should not be behind a feature flag so other people can set the default font (without needing to include the fira default font). I think "default fonts" should be supported without flags and the current default_font feature should be default_font_fira_sans or something similar.

HandleUntyped::weak_from_u64(Font::TYPE_UUID, 1491772431825224042);

impl Plugin for TextPlugin {
fn build(&self, app: &mut App) {
app.add_asset::<Font>()
Expand Down Expand Up @@ -98,5 +108,13 @@ impl Plugin for TextPlugin {
extract_text2d_sprite.after(SpriteSystem::ExtractSprites),
);
}

#[cfg(feature = "default_font")]
load_internal_binary_asset!(
app,
DEFAULT_FONT_HANDLE,
"FiraMono-subset.ttf",
|bytes: &[u8]| { Font::try_from_bytes(bytes.to_vec()).unwrap() }
);
}
}
5 changes: 5 additions & 0 deletions crates/bevy_text/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use bevy_utils::default;
use serde::{Deserialize, Serialize};

use crate::Font;
#[cfg(feature = "default_font")]
use crate::DEFAULT_FONT_HANDLE;

#[derive(Component, Debug, Clone, Reflect)]
#[reflect(Component, Default)]
Expand Down Expand Up @@ -167,6 +169,9 @@ pub struct TextStyle {
impl Default for TextStyle {
fn default() -> Self {
Self {
#[cfg(feature = "default_font")]
mockersf marked this conversation as resolved.
Show resolved Hide resolved
font: DEFAULT_FONT_HANDLE.typed(),
#[cfg(not(feature = "default_font"))]
mockersf marked this conversation as resolved.
Show resolved Hide resolved
font: Default::default(),
font_size: 12.0,
color: Color::WHITE,
Expand Down
1 change: 1 addition & 0 deletions docs/cargo_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The default feature set enables most of the expected features of a game engine,
|bevy_text|Provides text functionality|
|bevy_ui|A custom ECS-driven UI framework|
|bevy_winit|winit window and input backend|
|default_font|Include a default font, containing only ASCII characters, at the cost of a 20kB binary size increase|
|filesystem_watcher|Enable watching file system for asset hot reload|
|hdr|HDR image format support|
|ktx2|KTX2 compressed texture support|
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/bloom_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ fn setup(
TextBundle::from_section(
"",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 18.0,
color: Color::WHITE,
..default()
},
)
.with_style(Style {
Expand Down
3 changes: 1 addition & 2 deletions examples/3d/3d_gizmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0., 1.5, 6.).looking_at(Vec3::ZERO, Vec3::Y),
Expand Down Expand Up @@ -48,9 +47,9 @@ fn setup(
commands.spawn(TextBundle::from_section(
"Press 't' to toggle drawing gizmos on top of everything else in the scene",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 24.,
color: Color::WHITE,
..default()
},
));
}
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/anti_aliasing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,9 @@ fn setup(
TextBundle::from_section(
"",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 20.0,
color: Color::BLACK,
..default()
},
)
.with_style(Style {
Expand Down
4 changes: 2 additions & 2 deletions examples/3d/atmospheric_fog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ fn setup_terrain_scene(
));
}

fn setup_instructions(mut commands: Commands, asset_server: Res<AssetServer>) {
fn setup_instructions(mut commands: Commands) {
commands.spawn((TextBundle::from_section(
"Press Spacebar to Toggle Atmospheric Fog.\nPress S to Toggle Directional Light Fog Influence.",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 15.0,
color: Color::WHITE,
..default()
},
)
.with_style(Style {
Expand Down
5 changes: 2 additions & 3 deletions examples/3d/blend_modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
) {
let base_color = Color::rgba(0.9, 0.2, 0.3, 1.0);
let icosphere_mesh = meshes.add(
Expand Down Expand Up @@ -186,15 +185,15 @@ fn setup(

// Controls Text
let text_style = TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 18.0,
color: Color::BLACK,
..default()
};

let label_text_style = TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 25.0,
color: Color::ORANGE,
..default()
};

commands.spawn(
Expand Down
3 changes: 1 addition & 2 deletions examples/3d/bloom_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ fn setup_scene(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
) {
commands.spawn((
Camera3dBundle {
Expand Down Expand Up @@ -96,9 +95,9 @@ fn setup_scene(
TextBundle::from_section(
"",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 18.0,
color: Color::BLACK,
..default()
},
)
.with_style(Style {
Expand Down
4 changes: 2 additions & 2 deletions examples/3d/fog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ fn setup_pyramid_scene(
});
}

fn setup_instructions(mut commands: Commands, asset_server: Res<AssetServer>) {
fn setup_instructions(mut commands: Commands) {
commands.spawn((TextBundle::from_section(
"",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 15.0,
color: Color::WHITE,
..default()
},
)
.with_style(Style {
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/parallax_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ fn setup(
commands.spawn(background_cube_bundle(Vec3::new(0., 0., -45.)));

let style = TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 18.0,
color: Color::WHITE,
..default()
};

commands.spawn(
Expand Down
6 changes: 3 additions & 3 deletions examples/3d/pbr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ fn setup(
TextBundle::from_section(
"Perceptual Roughness",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 36.0,
color: Color::WHITE,
..default()
},
)
.with_style(Style {
Expand All @@ -95,9 +95,9 @@ fn setup(
text: Text::from_section(
"Metallic",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 36.0,
color: Color::WHITE,
..default()
},
),
style: Style {
Expand All @@ -117,9 +117,9 @@ fn setup(
TextBundle::from_section(
"Loading Environment Map...",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 36.0,
color: Color::RED,
..default()
},
)
.with_style(Style {
Expand Down
5 changes: 2 additions & 3 deletions examples/3d/tonemapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ fn setup(
TextBundle::from_section(
"",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 18.0,
color: Color::WHITE,
..default()
},
)
.with_style(Style {
Expand Down Expand Up @@ -243,7 +243,6 @@ fn setup_image_viewer_scene(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
camera_transform: Res<CameraTransform>,
asset_server: Res<AssetServer>,
) {
let mut transform = camera_transform.0;
transform.translation += transform.forward();
Expand Down Expand Up @@ -273,9 +272,9 @@ fn setup_image_viewer_scene(
TextBundle::from_section(
"Drag and drop an HDR or EXR file",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 36.0,
color: Color::BLACK,
..default()
},
)
.with_text_alignment(TextAlignment::Center)
Expand Down
14 changes: 3 additions & 11 deletions examples/async_tasks/external_source_external_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ fn main() {
struct StreamReceiver(Receiver<u32>);
struct StreamEvent(u32);

#[derive(Resource, Deref)]
struct LoadedFont(Handle<Font>);

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());

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

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

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

fn spawn_text(
mut commands: Commands,
mut reader: EventReader<StreamEvent>,
loaded_font: Res<LoadedFont>,
) {
fn spawn_text(mut commands: Commands, mut reader: EventReader<StreamEvent>) {
let text_style = TextStyle {
font: loaded_font.clone(),
font_size: 20.0,
color: Color::WHITE,
..default()
};

for (per_frame, event) in reader.iter().enumerate() {
Expand Down
6 changes: 3 additions & 3 deletions examples/ecs/apply_system_buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct AppleCount;
struct OrangeCount;

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

commands
Expand All @@ -82,9 +82,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
TextBundle::from_section(
"Apple: nothing counted yet".to_string(),
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 80.0,
color: Color::ORANGE,
..default()
},
),
AppleCount,
Expand All @@ -93,9 +93,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
TextBundle::from_section(
"Orange: nothing counted yet".to_string(),
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 80.0,
color: Color::ORANGE,
..default()
},
),
OrangeCount,
Expand Down
4 changes: 2 additions & 2 deletions examples/ecs/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}

fn setup_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
fn setup_menu(mut commands: Commands) {
let button_entity = commands
.spawn(NodeBundle {
style: Style {
Expand Down Expand Up @@ -78,9 +78,9 @@ fn setup_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
parent.spawn(TextBundle::from_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()
},
));
});
Expand Down
Loading