mirror of
https://github.com/bevyengine/bevy
synced 2025-02-16 22:18:33 +00:00
# Objective - Fixes #10720 - Adds the ability to control font smoothing of rendered text ## Solution - Introduce the `FontSmoothing` enum, with two possible variants (`FontSmoothing::None` and `FontSmoothing::AntiAliased`): - This is based on `-webkit-font-smoothing`, in line with our practice of adopting CSS-like properties/names for UI; - I could have gone instead for the [`font-smooth` property](https://developer.mozilla.org/en-US/docs/Web/CSS/font-smooth) that's also supported by browsers, but didn't since it's also non-standard, has an uglier name, and doesn't allow controlling the type of antialias applied. - Having an enum instead of e.g. a boolean, leaves the path open for adding `FontSmoothing::SubpixelAntiAliased` in the future, without a breaking change; - Add all the necessary plumbing to get the `FontSmoothing` information to where we rasterize the glyphs and store them in the atlas; - Change the font atlas key to also take into account the smoothing setting, not only font and font size; - Since COSMIC Text [doesn't support controlling font smoothing](https://github.com/pop-os/cosmic-text/issues/279), we roll out our own threshold-based “implementation”: - This has the downside of **looking ugly for “regular” vector fonts** ⚠️, since it doesn't properly take the hinting information into account like a proper implementation on the rasterizer side would. - However, **for fonts that have been specifically authored to be pixel fonts, (a common use case in games!) this is not as big of a problem**, since all lines are vertical/horizontal, and close to the final pixel boundaries (as long as the font is used at a multiple of the size originally intended by the author) - Once COSMIC exposes this functionality, we can switch to using it directly, and get better results; - Use a nearest neighbor sampler for atlases with font smoothing disabled, so that you can scale the text via transform and still get the pixelated look; - Add a convenience method to `Text` for setting the font smoothing; - Add a demonstration of using the `FontSmoothing` property to the `text2d` example. ## Testing - Did you test these changes? If so, how? - Yes. Via the `text2d`example, and also in my game. - Are there any parts that need more testing? - I'd like help from someone for testing this on devices/OSs with fractional scaling (Android/Windows) - How can other people (reviewers) test your changes? Is there anything specific they need to know? - Both via the `text2d` example and also by using it directly on your projects. - If relevant, what platforms did you test these changes on, and are there any important ones you can't test? - macOS --- ## Showcase ```rust commands.spawn(Text2dBundle { text: Text::from_section("Hello, World!", default()) .with_font_smoothing(FontSmoothing::None), ..default() }); ``` ![Screenshot 2024-09-22 at 12 33 39](https://github.com/user-attachments/assets/93e19672-b8c0-4cba-a8a3-4525fe2ae1cb) <img width="740" alt="image" src="https://github.com/user-attachments/assets/b881b02c-4e43-410b-902f-6985c25140fc"> ## Migration Guide - `Text` now contains a `font_smoothing: FontSmoothing` property, make sure to include it or add `..default()` when using the struct directly; - `FontSizeKey` has been renamed to `FontAtlasKey`, and now also contains the `FontSmoothing` setting; - The following methods now take an extra `font_smoothing: FontSmoothing` argument: - `FontAtlas::new()` - `FontAtlasSet::add_glyph_to_atlas()` - `FontAtlasSet::get_glyph_atlas_info()` - `FontAtlasSet::get_outlined_glyph_texture()`
96 lines
2.7 KiB
Rust
96 lines
2.7 KiB
Rust
//! Simple text rendering benchmark.
|
|
//!
|
|
//! Creates a `Text` with a single `TextSection` containing `100_000` glyphs,
|
|
//! and renders it with the UI in a white color and with Text2d in a red color.
|
|
//!
|
|
//! To recompute all text each frame run
|
|
//! `cargo run --example many_glyphs --release recompute-text`
|
|
use bevy::{
|
|
color::palettes::basic::RED,
|
|
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
|
prelude::*,
|
|
text::{BreakLineOn, TextBounds},
|
|
window::{PresentMode, WindowResolution},
|
|
winit::{UpdateMode, WinitSettings},
|
|
};
|
|
|
|
fn main() {
|
|
let mut app = App::new();
|
|
app.add_plugins((
|
|
DefaultPlugins.set(WindowPlugin {
|
|
primary_window: Some(Window {
|
|
present_mode: PresentMode::AutoNoVsync,
|
|
resolution: WindowResolution::new(1920.0, 1080.0).with_scale_factor_override(1.0),
|
|
..default()
|
|
}),
|
|
..default()
|
|
}),
|
|
FrameTimeDiagnosticsPlugin,
|
|
LogDiagnosticsPlugin::default(),
|
|
))
|
|
.insert_resource(WinitSettings {
|
|
focused_mode: UpdateMode::Continuous,
|
|
unfocused_mode: UpdateMode::Continuous,
|
|
})
|
|
.add_systems(Startup, setup);
|
|
|
|
if std::env::args().any(|arg| arg == "recompute-text") {
|
|
app.add_systems(Update, force_text_recomputation);
|
|
}
|
|
|
|
app.run();
|
|
}
|
|
|
|
fn setup(mut commands: Commands) {
|
|
warn!(include_str!("warning_string.txt"));
|
|
|
|
commands.spawn(Camera2dBundle::default());
|
|
let mut text = Text {
|
|
sections: vec![TextSection {
|
|
value: "0123456789".repeat(10_000),
|
|
style: TextStyle {
|
|
font_size: 4.,
|
|
..default()
|
|
},
|
|
}],
|
|
justify: JustifyText::Left,
|
|
linebreak_behavior: BreakLineOn::AnyCharacter,
|
|
..default()
|
|
};
|
|
|
|
commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
width: Val::Percent(100.),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
},
|
|
..default()
|
|
})
|
|
.with_children(|commands| {
|
|
commands.spawn(TextBundle {
|
|
text: text.clone(),
|
|
style: Style {
|
|
width: Val::Px(1000.),
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
});
|
|
});
|
|
|
|
text.sections[0].style.color = RED.into();
|
|
|
|
commands.spawn(Text2dBundle {
|
|
text,
|
|
text_anchor: bevy::sprite::Anchor::Center,
|
|
text_2d_bounds: TextBounds::new_horizontal(1000.),
|
|
..Default::default()
|
|
});
|
|
}
|
|
|
|
fn force_text_recomputation(mut text_query: Query<&mut Text>) {
|
|
for mut text in &mut text_query {
|
|
text.set_changed();
|
|
}
|
|
}
|