Skip to content

Commit 6b175cf

Browse files
authored
Round sub pixel font sizes (#22646)
# Objective Sub-pixel font sizes don't provide much benefit with bitmap fonts and rounding them to the nearest pixel massively reduces the number of font atlases when a text entity's font size is naively interpolated. fixes #22626 ## Solution Round the font sizes before adding them to the Cosmic Text `Attrs`. ## Testing ``` cargo run --example animated_ui ``` In main, the window is blank and it rapidly generates gigabytes of font atlase images. With this PR, the text is visible and animated, and memory usage remains stable.
1 parent 789891f commit 6b175cf

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

crates/bevy_text/src/pipeline.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -592,19 +592,22 @@ fn get_attrs<'a>(
592592
family: Family<'a>,
593593
scale_factor: f64,
594594
) -> Attrs<'a> {
595+
let font_size = (text_font.font_size * scale_factor as f32).round();
596+
let line_height = match line_height {
597+
LineHeight::Px(px) => px * scale_factor as f32,
598+
LineHeight::RelativeToFont(s) => s * font_size,
599+
};
600+
595601
Attrs::new()
596602
.metadata(span_index)
597603
.family(family)
598604
.stretch(text_font.width.into())
599605
.style(text_font.style.into())
600606
.weight(text_font.weight.into())
601-
.metrics(
602-
Metrics {
603-
font_size: text_font.font_size,
604-
line_height: line_height.eval(text_font.font_size),
605-
}
606-
.scale(scale_factor as f32),
607-
)
607+
.metrics(Metrics {
608+
font_size,
609+
line_height,
610+
})
608611
.font_features((&text_font.font_features).into())
609612
}
610613

crates/bevy_text/src/text.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,9 @@ pub struct TextFont {
368368
pub font: FontSource,
369369
/// The vertical height of rasterized glyphs in the font atlas in pixels.
370370
///
371-
/// This is multiplied by the window scale factor and `UiScale`, but not the text entity
372-
/// transform or camera projection.
371+
/// This is multiplied by the window scale factor and `UiScale`, but not the text entity's
372+
/// transform or camera projection. Then, the scaled font size is rounded to the nearest pixel
373+
/// to produce the final font size used during glyph layout.
373374
///
374375
/// A new font atlas is generated for every combination of font handle and scaled font size
375376
/// which can have a strong performance impact.
@@ -794,15 +795,6 @@ pub enum LineHeight {
794795
RelativeToFont(f32),
795796
}
796797

797-
impl LineHeight {
798-
pub(crate) fn eval(self, font_size: f32) -> f32 {
799-
match self {
800-
LineHeight::Px(px) => px,
801-
LineHeight::RelativeToFont(scale) => scale * font_size,
802-
}
803-
}
804-
}
805-
806798
impl Default for LineHeight {
807799
fn default() -> Self {
808800
LineHeight::RelativeToFont(1.2)

0 commit comments

Comments
 (0)