diff --git a/src/servo/text/native_font.rs b/src/servo/text/native_font.rs index 3789f4aeed3..1c6287e72cc 100644 --- a/src/servo/text/native_font.rs +++ b/src/servo/text/native_font.rs @@ -7,30 +7,32 @@ font resources needed by the graphics layer to draw glyphs. "]; -export NativeFont, create_test_native_font; +export NativeFont; #[cfg(target_os = "macos")] -type NativeFont/& = quartz_native_font::NativeFont; +type NativeFont/& = quartz_native_font::QuartzNativeFont; #[cfg(target_os = "linux")] -type NativeFont/& = ft_native_font::NativeFont; +type NativeFont/& = ft_native_font::FreeTypeNativeFont; -fn create_test_native_font() -> NativeFont { - fail; +fn with_test_native_font(f: fn@(NativeFont)) { + fail } #[test] #[ignore] fn should_get_glyph_indexes() { - let font = create_test_native_font(); - let idx = font.glyph_index('w'); - assert idx == some(40u); + with_test_native_font { |font| + let idx = font.glyph_index('w'); + assert idx == some(40u); + } } #[test] #[ignore] fn should_get_glyph_h_advance() { - let font = create_test_native_font(); - let adv = font.glyph_h_advance(40u); - assert adv == 15; + with_test_native_font { |font| + let adv = font.glyph_h_advance(40u); + assert adv == 15; + } } diff --git a/src/servo/text/native_font/ft_native_font.rs b/src/servo/text/native_font/ft_native_font.rs index 9a940eaa4b5..c670f1ddad4 100644 --- a/src/servo/text/native_font/ft_native_font.rs +++ b/src/servo/text/native_font/ft_native_font.rs @@ -1,9 +1,32 @@ +export FreeTypeNativeFont; + +import vec_as_buf = vec::as_buf; +import result::{result, ok, err}; +import ptr::{addr_of, null}; import glyph::GlyphIndex; +import azure::freetype; +import freetype::{ FT_Error, FT_Library, FT_Face, FT_Long }; +import freetype::bindgen::{ + FT_Init_FreeType, + FT_Done_FreeType, + FT_New_Memory_Face, + FT_Done_Face +}; -class NativeFont/& { - let bogus: int; +class FreeTypeNativeFont/& { + let face: FT_Face; - new() { self.bogus = 0; } + new(face: FT_Face) { + assert face.is_not_null(); + self.face = face; + } + + drop { + assert self.face.is_not_null(); + if !FT_Done_Face(self.face).succeeded() { + fail "FT_Done_Face failed"; + } + } fn glyph_index(_codepoint: char) -> option { fail; @@ -14,3 +37,36 @@ class NativeFont/& { fail; } } + +fn create(lib: FT_Library, buf: &[u8]) -> result { + assert lib.is_not_null(); + let face: FT_Face = null(); + ret vec_as_buf(*buf) {|cbuf| + if FT_New_Memory_Face(lib, cbuf, (*buf).len() as FT_Long, + 0 as FT_Long, addr_of(face)).succeeded() { + ok(FreeTypeNativeFont(face)) + } else { + err(()) + } + } +} + +impl methods for FT_Error { + fn succeeded() -> bool { self == 0 as FT_Error } +} + +#[cfg(test)] +fn with_lib(f: fn@(FT_Library)) { + let lib: FT_Library = null(); + assert FT_Init_FreeType(addr_of(lib)).succeeded(); + f(lib); + FT_Done_FreeType(lib); +} + +#[test] +fn create_should_return_err_if_buf_is_bogus() { + with_lib { |lib| + 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 9a940eaa4b5..05e5bfbb2c6 100644 --- a/src/servo/text/native_font/quartz_native_font.rs +++ b/src/servo/text/native_font/quartz_native_font.rs @@ -1,6 +1,6 @@ import glyph::GlyphIndex; -class NativeFont/& { +class QuartzNativeFont/& { let bogus: int; new() { self.bogus = 0; }