Merge pull request #2712 from glennw/freetype-allocators

Linux freetype context uses custom allocator callbacks to enable memory ...
This commit is contained in:
glennw 2014-06-25 15:25:06 +10:00
commit c983b2b97e
2 changed files with 47 additions and 4 deletions

View file

@ -7,12 +7,41 @@ use platform::font::FontHandle;
use font_context::FontContextHandleMethods;
use platform::font_list::path_from_identifier;
use freetype::freetype::{FTErrorMethods, FT_Library};
use freetype::freetype::{FT_Done_FreeType, FT_Init_FreeType};
use freetype::freetype::FTErrorMethods;
use freetype::freetype::FT_Add_Default_Modules;
use freetype::freetype::FT_Done_FreeType;
use freetype::freetype::FT_Library;
use freetype::freetype::FT_Memory;
use freetype::freetype::FT_New_Library;
use freetype::freetype::struct_FT_MemoryRec_;
use std::ptr;
use std::rc::Rc;
use libc;
use libc::{c_void, c_long, size_t, malloc};
use std::mem;
extern fn ft_alloc(_mem: FT_Memory, size: c_long) -> *c_void {
unsafe {
let ptr = libc::malloc(size as size_t);
ptr as *c_void
}
}
extern fn ft_free(_mem: FT_Memory, block: *c_void) {
unsafe {
libc::free(block as *mut c_void);
}
}
extern fn ft_realloc(_mem: FT_Memory, _cur_size: c_long, new_size: c_long, block: *c_void) -> *c_void {
unsafe {
let ptr = libc::realloc(block as *mut c_void, new_size as size_t);
ptr as *c_void
}
}
#[deriving(Clone)]
pub struct FreeTypeLibraryHandle {
pub ctx: FT_Library,
@ -33,9 +62,23 @@ impl Drop for FreeTypeLibraryHandle {
impl FontContextHandle {
pub fn new() -> FontContextHandle {
unsafe {
let ptr = libc::malloc(mem::size_of::<struct_FT_MemoryRec_>() as size_t);
let allocator: &mut struct_FT_MemoryRec_ = mem::transmute(ptr);
mem::overwrite(allocator, struct_FT_MemoryRec_ {
user: ptr::null(),
alloc: ft_alloc,
free: ft_free,
realloc: ft_realloc,
});
let ctx: FT_Library = ptr::null();
let result = FT_Init_FreeType(&ctx);
let result = FT_New_Library(ptr as FT_Memory, &ctx);
if !result.succeeded() { fail!("Unable to initialize FreeType library"); }
FT_Add_Default_Modules(ctx);
FontContextHandle {
ctx: Rc::new(FreeTypeLibraryHandle { ctx: ctx }),
}

@ -1 +1 @@
Subproject commit cc667034983c88bc8e1b5be7e1091bff23888734
Subproject commit 4fbb9c38fc06d3116f44f4da36dd378d73f71b58