Skip to content

Commit

Permalink
text: Return text width only in Font::measure
Browse files Browse the repository at this point in the history
This patch simplifies code, as text height is not needed anymore
after recent text refactors.
  • Loading branch information
kjarosh authored and torokati44 committed Nov 9, 2024
1 parent a255bc6 commit 5954c1b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
36 changes: 10 additions & 26 deletions core/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use ruffle_render::shape_utils::{DrawCommand, FillRule};
use ruffle_render::transform::Transform;
use std::borrow::Cow;
use std::cell::{OnceCell, RefCell};
use std::cmp::max;
use std::hash::{Hash, Hasher};
use swf::FillStyle;

Expand Down Expand Up @@ -618,35 +617,20 @@ impl<'gc> Font<'gc> {
}
}

/// Measure a particular string's metrics (width and height).
pub fn measure(&self, text: &WStr, params: EvalParameters) -> (Twips, Twips) {
let round = false;
/// Measure a particular string's width.
pub fn measure(&self, text: &WStr, params: EvalParameters) -> Twips {
let mut width = Twips::ZERO;
let mut height = Twips::ZERO;

self.evaluate(
text,
Default::default(),
params,
|_pos, transform, _glyph, advance, _x| {
let tx = transform.matrix.tx;
let ty = transform.matrix.ty;

if round {
width = width.max((tx + advance).trunc_to_pixel());
height = height.max(ty.trunc_to_pixel());
} else {
width = width.max(tx + advance);
height = height.max(ty);
}
|_pos, _transform, _glyph, advance, x| {
width = width.max(x + advance);
},
);

if text.is_empty() {
height = max(height, params.height);
}

(width, height)
width
}

/// Given a line of text, find the first breakpoint within the text.
Expand Down Expand Up @@ -690,17 +674,17 @@ impl<'gc> Font<'gc> {
params,
);

if is_start_of_line && measure.0 > remaining_width {
if is_start_of_line && measure > remaining_width {
//Failsafe for if we get a word wider than the field.
let mut last_passing_breakpoint = (Twips::ZERO, Twips::ZERO);
let mut last_passing_breakpoint = Twips::ZERO;

let cur_slice = &text[word_start..];
let mut char_iter = cur_slice.char_indices();
let mut prev_char_index = word_start;
let mut prev_frag_end = 0;

char_iter.next(); // No need to check cur_slice[0..0]
while last_passing_breakpoint.0 < remaining_width {
while last_passing_breakpoint < remaining_width {
prev_char_index = word_start + prev_frag_end;

if let Some((frag_end, _)) = char_iter.next() {
Expand All @@ -713,7 +697,7 @@ impl<'gc> Font<'gc> {
}

return Some(prev_char_index);
} else if measure.0 > remaining_width {
} else if measure > remaining_width {
//The word is wider than our remaining width, return the end of
//the line.
return Some(line_end);
Expand All @@ -724,7 +708,7 @@ impl<'gc> Font<'gc> {

//If the additional space were to cause an overflow, then
//return now.
remaining_width -= measure.0;
remaining_width -= measure;
if remaining_width < Twips::from_pixels(0.0) {
return Some(word_end);
}
Expand Down
12 changes: 6 additions & 6 deletions core/src/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> {
if self.current_line_span.align != swf::TextAlign::Left {
linebox.bounds = linebox
.bounds
.with_width(font.measure(text.trim_end(), params).0);
.with_width(font.measure(text.trim_end(), params));
}

if let Some(line_bounds) = &mut line_bounds {
Expand Down Expand Up @@ -610,16 +610,16 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> {
let params = EvalParameters::from_span(span);
let ascent = font.get_baseline_for_height(params.height());
let descent = font.get_descent_for_height(params.height());
let text_size = Size::from(font.measure(text, params));
let text_width = font.measure(text, params);
let box_origin = self.cursor - (Twips::ZERO, ascent).into();

let mut new_box = LayoutBox::from_text(text, start, end, font, span);
new_box.bounds = BoxBounds::from_position_and_size(
box_origin,
Size::from((text_size.width(), ascent + descent)),
Size::from((text_width, ascent + descent)),
);

self.cursor += (text_size.width(), Twips::ZERO).into();
self.cursor += (text_width, Twips::ZERO).into();
self.append_box(new_box);
}
}
Expand Down Expand Up @@ -647,14 +647,14 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> {
let ascent = bullet_font.get_baseline_for_height(params.height());
let descent = bullet_font.get_descent_for_height(params.height());
let bullet = WStr::from_units(&[0x2022u16]);
let text_size = Size::from(bullet_font.measure(bullet, params));
let text_width = bullet_font.measure(bullet, params);
let box_origin = bullet_cursor - (Twips::ZERO, ascent).into();

let pos = self.last_box_end_position();
let mut new_bullet = LayoutBox::from_bullet(pos, bullet_font, span);
new_bullet.bounds = BoxBounds::from_position_and_size(
box_origin,
Size::from((text_size.width(), ascent + descent)),
Size::from((text_width, ascent + descent)),
);

self.append_box(new_bullet);
Expand Down

0 comments on commit 5954c1b

Please sign in to comment.