When wrapping text, measure the text including the trailing space (if present).

This matches Flash behavior, but breaks an existing test, which I've adjusted appropriately.
This commit is contained in:
David Wendt 2020-06-02 19:04:17 -04:00
parent c67bf0b6b7
commit 2ab85c32e2
1 changed files with 8 additions and 9 deletions

View File

@ -245,12 +245,16 @@ impl<'gc> Font<'gc> {
let mut current_word = &text[0..0]; let mut current_word = &text[0..0];
for word in text.split(' ') { for word in text.split(' ') {
let measure = self.measure(word, font_size);
let line_start = current_word.as_ptr() as usize - text.as_ptr() as usize; let line_start = current_word.as_ptr() as usize - text.as_ptr() as usize;
let line_end = line_start + current_word.len(); let line_end = line_start + current_word.len();
let word_start = word.as_ptr() as usize - text.as_ptr() as usize; let word_start = word.as_ptr() as usize - text.as_ptr() as usize;
let word_end = word_start + word.len(); let word_end = word_start + word.len();
let measure = self.measure(
text.get(word_start..word_end + 1).unwrap_or(word),
font_size,
);
if measure.0 > remaining_width && measure.0 > width { if measure.0 > remaining_width && measure.0 > width {
//Failsafe for if we get a word wider than the field. //Failsafe for if we get a word wider than the field.
//TODO: Flash breaks words here, we should do that too. //TODO: Flash breaks words here, we should do that too.
@ -266,14 +270,9 @@ impl<'gc> Font<'gc> {
//Space remains for our current word, move up the word pointer. //Space remains for our current word, move up the word pointer.
current_word = &text[line_start..word_end]; current_word = &text[line_start..word_end];
let measure_with_space = self.measure(
text.get(word_start..word_end + 1).unwrap_or(word),
font_size,
);
//If the additional space were to cause an overflow, then //If the additional space were to cause an overflow, then
//return now. //return now.
remaining_width -= measure_with_space.0; remaining_width -= measure.0;
if remaining_width < Twips::from_pixels(0.0) { if remaining_width < Twips::from_pixels(0.0) {
return Some(word_end); return Some(word_end);
} }
@ -440,7 +439,7 @@ mod tests {
#[test] #[test]
fn wrap_line_breakpoint_irregular_sized_words() { fn wrap_line_breakpoint_irregular_sized_words() {
with_device_font(|_mc, df| { with_device_font(|_mc, df| {
let string = "abcd i j kl mnop q rstuv"; let string = "abcdi j kl mnop q rstuv";
let mut last_bp = 0; let mut last_bp = 0;
let breakpoint = df.wrap_line( let breakpoint = df.wrap_line(
&string, &string,
@ -449,7 +448,7 @@ mod tests {
Twips::from_pixels(0.0), Twips::from_pixels(0.0),
); );
assert_eq!(Some(6), breakpoint); assert_eq!(Some(5), breakpoint);
last_bp += breakpoint.unwrap() + 1; last_bp += breakpoint.unwrap() + 1;