Fix accidental bogus copying of the font binary

This commit is contained in:
Brian Anderson 2012-09-13 16:51:33 -07:00
parent 413d458857
commit 365ef92f07
5 changed files with 46 additions and 21 deletions

View file

@ -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];

View file

@ -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 {

View file

@ -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<NativeFont, ()> {
fn create(_native_lib: &NativeFontLibrary, buf: @~[u8]) -> Result<NativeFont, ()> {
quartz_native_font::create(buf)
}
#[cfg(target_os = "linux")]
fn create(native_lib: &NativeFontLibrary, buf: &~[u8]) -> Result<NativeFont, ()> {
ft_native_font::create(*native_lib, buf)
fn create(native_lib: &NativeFontLibrary, buf: @~[u8]) -> Result<NativeFont, ()> {
ft_native_font::create(native_lib, buf)
}
#[cfg(target_os = "macos")]

View file

@ -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<FreeTypeNativeFont, ()> {
fn create(lib: &FT_Library, buf: @~[u8]) -> Result<FreeTypeNativeFont, ()> {
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();
})
}

View file

@ -127,7 +127,7 @@ fn ctfont_from_cgfont(cgfont: CGFontRef) -> coretext::CTFontRef {
CTFontCreateWithGraphicsFont(cgfont, 21f as CGFloat, null(), null())
}
fn create(buf: &~[u8]) -> Result<QuartzNativeFont, ()> {
fn create(buf: @~[u8]) -> Result<QuartzNativeFont, ()> {
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);
}