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 line height to TextFont #16614

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions crates/bevy_text/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl Plugin for TextPlugin {
app.init_asset::<Font>()
.register_type::<Text2d>()
.register_type::<TextFont>()
.register_type::<LineHeight>()
.register_type::<TextColor>()
.register_type::<TextSpan>()
.register_type::<TextBounds>()
Expand Down
11 changes: 9 additions & 2 deletions crates/bevy_text/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl TextPipeline {
// Collect span information into a vec. This is necessary because font loading requires mut access
// to FontSystem, which the cosmic-text Buffer also needs.
let mut font_size: f32 = 0.;
let mut line_height: f32 = 0.0;
let mut spans: Vec<(usize, &str, &TextFont, FontFaceInfo, Color)> =
core::mem::take(&mut self.spans_buffer)
.into_iter()
Expand Down Expand Up @@ -130,6 +131,7 @@ impl TextPipeline {

// Get max font size for use in cosmic Metrics.
font_size = font_size.max(text_font.font_size);
line_height = line_height.max(text_font.line_height.eval(text_font.font_size));

// Load Bevy fonts into cosmic-text's font system.
let face_info = load_font_to_fontdb(
Expand All @@ -146,7 +148,6 @@ impl TextPipeline {
spans.push((span_index, span, text_font, face_info, color));
}

let line_height = font_size * 1.2;
let mut metrics = Metrics::new(font_size, line_height).scale(scale_factor as f32);
// Metrics of 0.0 cause `Buffer::set_metrics` to panic. We hack around this by 'falling
// through' to call `Buffer::set_rich_text` with zero spans so any cached text will be cleared without
Expand Down Expand Up @@ -486,7 +487,13 @@ fn get_attrs<'a>(
.stretch(face_info.stretch)
.style(face_info.style)
.weight(face_info.weight)
.metrics(Metrics::relative(text_font.font_size, 1.2).scale(scale_factor as f32))
.metrics(
Metrics {
font_size: text_font.font_size,
line_height: text_font.line_height.eval(text_font.font_size),
}
.scale(scale_factor as f32),
)
.color(cosmic_text::Color(color.to_linear().as_u32()));
attrs
}
Expand Down
33 changes: 33 additions & 0 deletions crates/bevy_text/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ pub struct TextFont {
/// A new font atlas is generated for every combination of font handle and scaled font size
/// which can have a strong performance impact.
pub font_size: f32,
/// The vertical height of a line of text, from the top of one line to the top of the
/// next.
///
/// Defaults to `LineHeight::Scalar(1.2)`
pub line_height: LineHeight,
/// The antialiasing method to use when rendering text.
pub font_smoothing: FontSmoothing,
}
Expand Down Expand Up @@ -324,11 +329,39 @@ impl Default for TextFont {
Self {
font: Default::default(),
font_size: 20.0,
line_height: LineHeight::default(),
font_smoothing: Default::default(),
}
}
}

/// Specifies the height of each line of text for `Text` and `Text2d`
///
/// Default is 1.2x the font size
#[derive(Debug, Clone, Copy, Reflect)]
Cyborus04 marked this conversation as resolved.
Show resolved Hide resolved
#[reflect(Debug)]
pub enum LineHeight {
/// Set line height to a specific number of pixels
Px(f32),
/// Set line height to a multiple of the font size
Scalar(f32),
}

impl LineHeight {
pub(crate) fn eval(self, font_size: f32) -> f32 {
match self {
LineHeight::Px(px) => px,
LineHeight::Scalar(scale) => scale * font_size,
}
}
}

impl Default for LineHeight {
fn default() -> Self {
LineHeight::Scalar(1.2)
Copy link
Contributor

Choose a reason for hiding this comment

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

To make it easier to expand the feature set in the future (by propagating inherited line height across a hierarchy etc) without having to do a breaking change, would it make sense to have a separate LineHeight::Auto variant as the default?

Copy link
Author

Choose a reason for hiding this comment

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

What would that do?

}
}

/// The color of the text for this section.
#[derive(Component, Copy, Clone, Debug, Deref, DerefMut, Reflect)]
#[reflect(Component, Default, Debug)]
Expand Down
1 change: 1 addition & 0 deletions examples/dev_tools/fps_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn main() {
font: default(),
// We could also disable font smoothing,
font_smoothing: FontSmoothing::default(),
..default()
},
// We can also change color of the overlay
text_color: OverlayColor::GREEN,
Expand Down
Loading