Work on implementing NativeFont

This commit is contained in:
Brian Anderson 2012-06-22 17:21:30 -07:00
parent a6d9b123e4
commit 2967527abb
3 changed files with 73 additions and 15 deletions

View file

@ -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")] #[cfg(target_os = "macos")]
type NativeFont/& = quartz_native_font::NativeFont; type NativeFont/& = quartz_native_font::QuartzNativeFont;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
type NativeFont/& = ft_native_font::NativeFont; type NativeFont/& = ft_native_font::FreeTypeNativeFont;
fn create_test_native_font() -> NativeFont { fn with_test_native_font(f: fn@(NativeFont)) {
fail; fail
} }
#[test] #[test]
#[ignore] #[ignore]
fn should_get_glyph_indexes() { fn should_get_glyph_indexes() {
let font = create_test_native_font(); with_test_native_font { |font|
let idx = font.glyph_index('w'); let idx = font.glyph_index('w');
assert idx == some(40u); assert idx == some(40u);
} }
}
#[test] #[test]
#[ignore] #[ignore]
fn should_get_glyph_h_advance() { fn should_get_glyph_h_advance() {
let font = create_test_native_font(); with_test_native_font { |font|
let adv = font.glyph_h_advance(40u); let adv = font.glyph_h_advance(40u);
assert adv == 15; assert adv == 15;
} }
}

View file

@ -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 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/& { class FreeTypeNativeFont/& {
let bogus: int; 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<GlyphIndex> { fn glyph_index(_codepoint: char) -> option<GlyphIndex> {
fail; fail;
@ -14,3 +37,36 @@ class NativeFont/& {
fail; fail;
} }
} }
fn create(lib: FT_Library, buf: &[u8]) -> result<FreeTypeNativeFont, ()> {
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();
}
}

View file

@ -1,6 +1,6 @@
import glyph::GlyphIndex; import glyph::GlyphIndex;
class NativeFont/& { class QuartzNativeFont/& {
let bogus: int; let bogus: int;
new() { self.bogus = 0; } new() { self.bogus = 0; }