Skip to content

Commit

Permalink
feat: add line height to TextFont
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyborus04 committed Dec 2, 2024
1 parent 793e2f2 commit 2904af4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
17 changes: 15 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: Option<f32> = None;
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,12 @@ impl TextPipeline {

// Get max font size for use in cosmic Metrics.
font_size = font_size.max(text_font.font_size);
line_height = match (line_height, text_font.line_height) {
(None, None) => None,
(Some(x), None) => Some(x),
(None, Some(y)) => Some(y),
(Some(x), Some(y)) => Some(x.max(y)),
};

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

let line_height = font_size * 1.2;
let line_height = line_height.unwrap_or(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 +493,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.unwrap_or(text_font.font_size * 1.2),
}
.scale(scale_factor as f32),
)
.color(cosmic_text::Color(color.to_linear().as_u32()));
attrs
}
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_text/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,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 in pixels, from the top of one line to the top of the
/// next.
///
/// If not set, the line height will be 1.2 * `font_size`.
pub line_height: Option<f32>,
/// The antialiasing method to use when rendering text.
pub font_smoothing: FontSmoothing,
}
Expand Down Expand Up @@ -323,6 +328,7 @@ impl Default for TextFont {
Self {
font: Default::default(),
font_size: 20.0,
line_height: None,
font_smoothing: Default::default(),
}
}
Expand Down

0 comments on commit 2904af4

Please sign in to comment.