From 18e93b3d11b0f94bc4db8d5ad3d473d29ac55c9d Mon Sep 17 00:00:00 2001 From: David Wendt Date: Sat, 23 May 2020 16:38:54 -0400 Subject: [PATCH] Implement tests for `wrap_line`. These tests depend on the particulars of our default device font, Noto Sans. If this test proves to be fragile we may need to create a testing font with a locked width and kerning table... --- core/src/font.rs | 123 +++++++++++++++++++++++++++++++++++++++++++++ core/src/player.rs | 4 +- 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/core/src/font.rs b/core/src/font.rs index 2463b9639..c6d2dad1f 100644 --- a/core/src/font.rs +++ b/core/src/font.rs @@ -282,3 +282,126 @@ impl FontDescriptor { self.is_italic } } + +#[cfg(test)] +mod tests { + use crate::backend::render::{NullRenderer, RenderBackend}; + use crate::font::Font; + use crate::player::{Player, DEVICE_FONT_TAG}; + use gc_arena::{rootless_arena, MutationContext}; + use swf::Twips; + + fn with_device_font(callback: F) + where + F: for<'gc> FnOnce(MutationContext<'gc, '_>, Font<'gc>), + { + rootless_arena(|mc| { + let mut renderer: Box = Box::new(NullRenderer::new()); + let device_font = Player::load_device_font(mc, DEVICE_FONT_TAG, &mut renderer).unwrap(); + + callback(mc, device_font); + }) + } + + #[test] + fn wrap_line_no_breakpoint() { + with_device_font(|_mc, df| { + let string = "abcdefghijklmnopqrstuv"; + let breakpoint = df.wrap_line( + &string, + Twips::from_pixels(12.0), + Twips::from_pixels(200.0), + Twips::from_pixels(0.0), + ); + + assert_eq!(None, breakpoint); + }); + } + + #[test] + fn wrap_line_breakpoint_every_word() { + with_device_font(|_mc, df| { + let string = "abcd efgh ijkl mnop"; + let mut last_bp = 0; + let breakpoint = df.wrap_line( + &string, + Twips::from_pixels(12.0), + Twips::from_pixels(30.0), + Twips::from_pixels(0.0), + ); + + assert_eq!(Some(4), breakpoint); + + last_bp += breakpoint.unwrap() + 1; + + let breakpoint2 = df.wrap_line( + &string[last_bp..], + Twips::from_pixels(12.0), + Twips::from_pixels(30.0), + Twips::from_pixels(0.0), + ); + + assert_eq!(Some(4), breakpoint2); + + last_bp += breakpoint2.unwrap() + 1; + + let breakpoint3 = df.wrap_line( + &string[last_bp..], + Twips::from_pixels(12.0), + Twips::from_pixels(30.0), + Twips::from_pixels(0.0), + ); + + assert_eq!(Some(4), breakpoint3); + + last_bp += breakpoint3.unwrap() + 1; + + let breakpoint4 = df.wrap_line( + &string[last_bp..], + Twips::from_pixels(12.0), + Twips::from_pixels(30.0), + Twips::from_pixels(0.0), + ); + + assert_eq!(None, breakpoint4); + }); + } + + #[test] + fn wrap_line_breakpoint_irregular_sized_words() { + with_device_font(|_mc, df| { + let string = "abcd i j kl mnop q rstuv"; + let mut last_bp = 0; + let breakpoint = df.wrap_line( + &string, + Twips::from_pixels(12.0), + Twips::from_pixels(30.0), + Twips::from_pixels(0.0), + ); + + assert_eq!(Some(11), breakpoint); + + last_bp += breakpoint.unwrap() + 1; + + let breakpoint2 = df.wrap_line( + &string[last_bp..], + Twips::from_pixels(12.0), + Twips::from_pixels(30.0), + Twips::from_pixels(0.0), + ); + + assert_eq!(Some(6), breakpoint2); + + last_bp += breakpoint2.unwrap() + 1; + + let breakpoint3 = df.wrap_line( + &string[last_bp..], + Twips::from_pixels(12.0), + Twips::from_pixels(30.0), + Twips::from_pixels(0.0), + ); + + assert_eq!(None, breakpoint3); + }); + } +} diff --git a/core/src/player.rs b/core/src/player.rs index aa27c4f5d..a06c5603d 100644 --- a/core/src/player.rs +++ b/core/src/player.rs @@ -23,7 +23,7 @@ use std::convert::TryFrom; use std::ops::DerefMut; use std::sync::{Arc, Mutex, Weak}; -static DEVICE_FONT_TAG: &[u8] = include_bytes!("../assets/noto-sans-definefont3.bin"); +pub static DEVICE_FONT_TAG: &[u8] = include_bytes!("../assets/noto-sans-definefont3.bin"); /// The newest known Flash Player version, serves as a default to /// `player_version`. @@ -910,7 +910,7 @@ impl Player { /// Loads font data from the given buffer. /// The buffer should be the `DefineFont3` info for the tag. /// The tag header should not be included. - fn load_device_font<'gc>( + pub fn load_device_font<'gc>( gc_context: gc_arena::MutationContext<'gc, '_>, data: &[u8], renderer: &mut Renderer,