Skip to content

Commit c63bfe5

Browse files
committed
feat: add line height to TextFont
1 parent f9c8f51 commit c63bfe5

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

crates/bevy_text/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ impl Plugin for TextPlugin {
113113
app.init_asset::<Font>()
114114
.register_type::<Text2d>()
115115
.register_type::<TextFont>()
116+
.register_type::<LineHeight>()
116117
.register_type::<TextColor>()
117118
.register_type::<TextSpan>()
118119
.register_type::<TextBounds>()

crates/bevy_text/src/pipeline.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ impl TextPipeline {
9898
// Collect span information into a vec. This is necessary because font loading requires mut access
9999
// to FontSystem, which the cosmic-text Buffer also needs.
100100
let mut font_size: f32 = 0.;
101+
let mut line_height: f32 = 0.0;
101102
let mut spans: Vec<(usize, &str, &TextFont, FontFaceInfo, Color)> =
102103
core::mem::take(&mut self.spans_buffer)
103104
.into_iter()
@@ -130,6 +131,7 @@ impl TextPipeline {
130131

131132
// Get max font size for use in cosmic Metrics.
132133
font_size = font_size.max(text_font.font_size);
134+
line_height = line_height.max(text_font.line_height.eval(text_font.font_size));
133135

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

149-
let line_height = font_size * 1.2;
150151
let mut metrics = Metrics::new(font_size, line_height).scale(scale_factor as f32);
151152
// Metrics of 0.0 cause `Buffer::set_metrics` to panic. We hack around this by 'falling
152153
// through' to call `Buffer::set_rich_text` with zero spans so any cached text will be cleared without
@@ -486,7 +487,13 @@ fn get_attrs<'a>(
486487
.stretch(face_info.stretch)
487488
.style(face_info.style)
488489
.weight(face_info.weight)
489-
.metrics(Metrics::relative(text_font.font_size, 1.2).scale(scale_factor as f32))
490+
.metrics(
491+
Metrics {
492+
font_size: text_font.font_size,
493+
line_height: text_font.line_height.eval(text_font.font_size),
494+
}
495+
.scale(scale_factor as f32),
496+
)
490497
.color(cosmic_text::Color(color.to_linear().as_u32()));
491498
attrs
492499
}

crates/bevy_text/src/text.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ pub struct TextFont {
285285
/// A new font atlas is generated for every combination of font handle and scaled font size
286286
/// which can have a strong performance impact.
287287
pub font_size: f32,
288+
/// The vertical height of a line of text, from the top of one line to the top of the
289+
/// next.
290+
///
291+
/// Defaults to `LineHeight::Scalar(1.2)`
292+
pub line_height: LineHeight,
288293
/// The antialiasing method to use when rendering text.
289294
pub font_smoothing: FontSmoothing,
290295
}
@@ -324,11 +329,39 @@ impl Default for TextFont {
324329
Self {
325330
font: Default::default(),
326331
font_size: 20.0,
332+
line_height: LineHeight::default(),
327333
font_smoothing: Default::default(),
328334
}
329335
}
330336
}
331337

338+
/// Specifies the height of each line of text for `Text` and `Text2d`
339+
///
340+
/// Default is 1.2x the font size
341+
#[derive(Debug, Clone, Copy, Reflect)]
342+
#[reflect(Debug)]
343+
pub enum LineHeight {
344+
/// Set line height to a specific number of pixels
345+
Px(f32),
346+
/// Set line height to a multiple of the font size
347+
Scalar(f32),
348+
}
349+
350+
impl LineHeight {
351+
pub(crate) fn eval(self, font_size: f32) -> f32 {
352+
match self {
353+
LineHeight::Px(px) => px,
354+
LineHeight::Scalar(scale) => scale * font_size,
355+
}
356+
}
357+
}
358+
359+
impl Default for LineHeight {
360+
fn default() -> Self {
361+
LineHeight::Scalar(1.2)
362+
}
363+
}
364+
332365
/// The color of the text for this section.
333366
#[derive(Component, Copy, Clone, Debug, Deref, DerefMut, Reflect)]
334367
#[reflect(Component, Default, Debug)]

examples/dev_tools/fps_overlay.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn main() {
2626
font: default(),
2727
// We could also disable font smoothing,
2828
font_smoothing: FontSmoothing::default(),
29+
..default()
2930
},
3031
// We can also change color of the overlay
3132
text_color: OverlayColor::GREEN,

0 commit comments

Comments
 (0)