From 365ef92f0733521e70480b5d15d038722dde5a7d Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 13 Sep 2012 16:51:33 -0700 Subject: [PATCH] Fix accidental bogus copying of the font binary --- src/servo/text/font.rs | 29 +++++++++++++++++-- src/servo/text/font_library.rs | 4 +-- src/servo/text/native_font.rs | 6 ++-- src/servo/text/native_font/ft_native_font.rs | 22 +++++++------- .../text/native_font/quartz_native_font.rs | 6 ++-- 5 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/servo/text/font.rs b/src/servo/text/font.rs index 9b76fd06729..72dd946bf8b 100644 --- a/src/servo/text/font.rs +++ b/src/servo/text/font.rs @@ -33,10 +33,10 @@ impl Font { } } -fn Font(fontbuf: ~[u8], +native_font: NativeFont) -> Font { +fn Font(fontbuf: @~[u8], +native_font: NativeFont) -> Font { Font { - fontbuf : @fontbuf, - native_font : native_font, + fontbuf : fontbuf, + native_font : move native_font, } } @@ -73,6 +73,29 @@ fn should_get_glyph_advance() { assert x == 15; } +// Testing thread safety +fn should_get_glyph_advance_stress() { + #[test]; + + let mut ports = ~[]; + + for iter::repeat(100) { + let (chan, port) = pipes::stream(); + ports += [@move port]; + do task::spawn { + let lib = FontLibrary(); + let font = lib.get_test_font(); + let x = font.glyph_h_advance(40u); + assert x == 15; + chan.send(()); + } + } + + for ports.each |port| { + port.recv(); + } +} + fn should_be_able_to_create_instances_in_multiple_threads() { #[test]; diff --git a/src/servo/text/font_library.rs b/src/servo/text/font_library.rs index b7472410c4c..066c97a674c 100644 --- a/src/servo/text/font_library.rs +++ b/src/servo/text/font_library.rs @@ -30,8 +30,8 @@ fn FontLibrary() -> FontLibrary { } fn create_font(native_lib: &native::NativeFontLibrary) -> Result<@Font, ()> { - let font_bin = test_font_bin(); - let native_font = native_font::create(native_lib, &font_bin); + let font_bin = @test_font_bin(); + let native_font = native_font::create(native_lib, font_bin); let native_font = if native_font.is_ok() { result::unwrap(native_font) } else { diff --git a/src/servo/text/native_font.rs b/src/servo/text/native_font.rs index 1514e39944f..76f5c6e6950 100644 --- a/src/servo/text/native_font.rs +++ b/src/servo/text/native_font.rs @@ -18,13 +18,13 @@ type NativeFont/& = quartz_native_font::QuartzNativeFont; type NativeFont/& = ft_native_font::FreeTypeNativeFont; #[cfg(target_os = "macos")] -fn create(_native_lib: &NativeFontLibrary, buf: &~[u8]) -> Result { +fn create(_native_lib: &NativeFontLibrary, buf: @~[u8]) -> Result { quartz_native_font::create(buf) } #[cfg(target_os = "linux")] -fn create(native_lib: &NativeFontLibrary, buf: &~[u8]) -> Result { - ft_native_font::create(*native_lib, buf) +fn create(native_lib: &NativeFontLibrary, buf: @~[u8]) -> Result { + ft_native_font::create(native_lib, buf) } #[cfg(target_os = "macos")] diff --git a/src/servo/text/native_font/ft_native_font.rs b/src/servo/text/native_font/ft_native_font.rs index 36506e54c93..43b30c8ef61 100644 --- a/src/servo/text/native_font/ft_native_font.rs +++ b/src/servo/text/native_font/ft_native_font.rs @@ -17,6 +17,8 @@ use freetype::bindgen::{ }; struct FreeTypeNativeFont { + /// The font binary. This must stay valid for the lifetime of the font + buf: @~[u8], face: FT_Face, drop { @@ -27,9 +29,9 @@ struct FreeTypeNativeFont { } } -fn FreeTypeNativeFont(face: FT_Face) -> FreeTypeNativeFont { +fn FreeTypeNativeFont(face: FT_Face, buf: @~[u8]) -> FreeTypeNativeFont { assert face.is_not_null(); - FreeTypeNativeFont { face: face } + FreeTypeNativeFont { buf: buf, face: face } } impl FreeTypeNativeFont { @@ -67,16 +69,16 @@ impl FreeTypeNativeFont { } } -fn create(lib: FT_Library, buf: &~[u8]) -> Result { +fn create(lib: &FT_Library, buf: @~[u8]) -> Result { assert lib.is_not_null(); let face: FT_Face = null(); return vec_as_buf(*buf, |cbuf, _len| { - if FT_New_Memory_Face(lib, cbuf, (*buf).len() as FT_Long, + if FT_New_Memory_Face(*lib, cbuf, (*buf).len() as FT_Long, 0 as FT_Long, addr_of(face)).succeeded() { // FIXME: These values are placeholders let res = FT_Set_Char_Size(face, 0, 20*64, 0, 72); if !res.succeeded() { fail ~"unable to set font char size" } - Ok(FreeTypeNativeFont(face)) + Ok(FreeTypeNativeFont(face, buf)) } else { Err(()) } @@ -96,23 +98,23 @@ fn with_test_native_font(f: fn@(nf: &NativeFont)) { use unwrap_result = result::unwrap; with_lib(|lib| { - let buf = test_font_bin(); - let font = unwrap_result(create(lib, &buf)); + let buf = @test_font_bin(); + let font = unwrap_result(create(lib, move buf)); f(&font); }) } -fn with_lib(f: fn@(FT_Library)) { +fn with_lib(f: fn@((&FT_Library))) { let lib: FT_Library = null(); assert FT_Init_FreeType(addr_of(lib)).succeeded(); - f(lib); + f(&lib); FT_Done_FreeType(lib); } #[test] fn create_should_return_err_if_buf_is_bogus() { with_lib(|lib| { - let buf = &~[]; + let buf = @~[]; assert create(lib, buf).is_err(); }) } diff --git a/src/servo/text/native_font/quartz_native_font.rs b/src/servo/text/native_font/quartz_native_font.rs index 0745b0e076a..7424b87ab41 100644 --- a/src/servo/text/native_font/quartz_native_font.rs +++ b/src/servo/text/native_font/quartz_native_font.rs @@ -127,7 +127,7 @@ fn ctfont_from_cgfont(cgfont: CGFontRef) -> coretext::CTFontRef { CTFontCreateWithGraphicsFont(cgfont, 21f as CGFloat, null(), null()) } -fn create(buf: &~[u8]) -> Result { +fn create(buf: @~[u8]) -> Result { let fontprov = vec::as_imm_buf(*buf, |cbuf, len| { CGDataProviderCreateWithData( null(), @@ -150,8 +150,8 @@ fn with_test_native_font(f: fn@(nf: &NativeFont)) { use font::test_font_bin; use unwrap_result = result::unwrap; - let buf = test_font_bin(); - let res = create(&buf); + let buf = @test_font_bin(); + let res = create(buf); let font = unwrap_result(res); f(&font); }