Skip to content

Commit 5954c1b

Browse files
kjaroshtorokati44
authored andcommitted
text: Return text width only in Font::measure
This patch simplifies code, as text height is not needed anymore after recent text refactors.
1 parent a255bc6 commit 5954c1b

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

core/src/font.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use ruffle_render::shape_utils::{DrawCommand, FillRule};
99
use ruffle_render::transform::Transform;
1010
use std::borrow::Cow;
1111
use std::cell::{OnceCell, RefCell};
12-
use std::cmp::max;
1312
use std::hash::{Hash, Hasher};
1413
use swf::FillStyle;
1514

@@ -618,35 +617,20 @@ impl<'gc> Font<'gc> {
618617
}
619618
}
620619

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

627624
self.evaluate(
628625
text,
629626
Default::default(),
630627
params,
631-
|_pos, transform, _glyph, advance, _x| {
632-
let tx = transform.matrix.tx;
633-
let ty = transform.matrix.ty;
634-
635-
if round {
636-
width = width.max((tx + advance).trunc_to_pixel());
637-
height = height.max(ty.trunc_to_pixel());
638-
} else {
639-
width = width.max(tx + advance);
640-
height = height.max(ty);
641-
}
628+
|_pos, _transform, _glyph, advance, x| {
629+
width = width.max(x + advance);
642630
},
643631
);
644632

645-
if text.is_empty() {
646-
height = max(height, params.height);
647-
}
648-
649-
(width, height)
633+
width
650634
}
651635

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

693-
if is_start_of_line && measure.0 > remaining_width {
677+
if is_start_of_line && measure > remaining_width {
694678
//Failsafe for if we get a word wider than the field.
695-
let mut last_passing_breakpoint = (Twips::ZERO, Twips::ZERO);
679+
let mut last_passing_breakpoint = Twips::ZERO;
696680

697681
let cur_slice = &text[word_start..];
698682
let mut char_iter = cur_slice.char_indices();
699683
let mut prev_char_index = word_start;
700684
let mut prev_frag_end = 0;
701685

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

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

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

725709
//If the additional space were to cause an overflow, then
726710
//return now.
727-
remaining_width -= measure.0;
711+
remaining_width -= measure;
728712
if remaining_width < Twips::from_pixels(0.0) {
729713
return Some(word_end);
730714
}

core/src/html/layout.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> {
274274
if self.current_line_span.align != swf::TextAlign::Left {
275275
linebox.bounds = linebox
276276
.bounds
277-
.with_width(font.measure(text.trim_end(), params).0);
277+
.with_width(font.measure(text.trim_end(), params));
278278
}
279279

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

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

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

653653
let pos = self.last_box_end_position();
654654
let mut new_bullet = LayoutBox::from_bullet(pos, bullet_font, span);
655655
new_bullet.bounds = BoxBounds::from_position_and_size(
656656
box_origin,
657-
Size::from((text_size.width(), ascent + descent)),
657+
Size::from((text_width, ascent + descent)),
658658
);
659659

660660
self.append_box(new_bullet);

0 commit comments

Comments
 (0)