Skip to content

Commit

Permalink
Bound Text contents width
Browse files Browse the repository at this point in the history
The contents of a Text would wander to the left with center and
right alignment because some size calculations don't take the
maximum available width into account.
  • Loading branch information
phi-gamma authored and ogoffart committed Oct 8, 2021
1 parent b99ff1b commit f916ca4
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions sixtyfps_runtime/rendering_backends/gl/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,15 +630,19 @@ pub(crate) fn layout_text_lines(
}
};

let mut process_line =
|text: &str, y: f32, start: usize, line_metrics: &femtovg::TextMetrics| {
let x = match horizontal_alignment {
TextHorizontalAlignment::left => 0.,
TextHorizontalAlignment::center => max_width / 2. - line_metrics.width() / 2.,
TextHorizontalAlignment::right => max_width - line_metrics.width(),
};
layout_line(text, Point::new(x, y), start, line_metrics);
let mut process_line = |text: &str,
y: f32,
start: usize,
line_metrics: &femtovg::TextMetrics| {
let x = match horizontal_alignment {
TextHorizontalAlignment::left => 0.,
TextHorizontalAlignment::center => {
max_width / 2. - f32::min(max_width, line_metrics.width()) / 2.
}
TextHorizontalAlignment::right => max_width - f32::min(max_width, line_metrics.width()),
};
layout_line(text, Point::new(x, y), start, line_metrics);
};

let baseline_y = match vertical_alignment {
TextVerticalAlignment::top => 0.,
Expand Down

0 comments on commit f916ca4

Please sign in to comment.