mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
make font/platform/gfx/font_context safe
This commit is contained in:
parent
279a82bdd3
commit
55771bc307
1 changed files with 12 additions and 35 deletions
|
@ -11,30 +11,22 @@ use freetype::freetype::{FTErrorMethods, FT_Library};
|
||||||
use freetype::freetype::{FT_Done_FreeType, FT_Init_FreeType};
|
use freetype::freetype::{FT_Done_FreeType, FT_Init_FreeType};
|
||||||
|
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
use extra::arc::Arc;
|
||||||
|
|
||||||
#[deriving(Clone)]
|
#[deriving(Clone)]
|
||||||
struct FreeTypeLibraryHandle {
|
struct FreeTypeLibraryHandle {
|
||||||
ctx: FT_Library,
|
ctx: FT_Library,
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME(ksh8281) this value have to use atomic operation for counting ref
|
|
||||||
static mut font_context_ref_count: uint = 0;
|
|
||||||
static mut ft_pointer: Option<FT_Library> = None;
|
|
||||||
pub struct FontContextHandle {
|
pub struct FontContextHandle {
|
||||||
ctx: FreeTypeLibraryHandle,
|
ctx: Arc<FreeTypeLibraryHandle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for FontContextHandle {
|
impl Drop for FreeTypeLibraryHandle {
|
||||||
#[fixed_stack_segment]
|
#[fixed_stack_segment]
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
assert!(self.ctx.ctx.is_not_null());
|
assert!(self.ctx.is_not_null());
|
||||||
unsafe {
|
unsafe { FT_Done_FreeType(self.ctx) };
|
||||||
assert!(font_context_ref_count >= 1);
|
|
||||||
font_context_ref_count = font_context_ref_count - 1;
|
|
||||||
if font_context_ref_count == 0 {
|
|
||||||
FT_Done_FreeType(self.ctx.ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,23 +34,11 @@ impl FontContextHandle {
|
||||||
#[fixed_stack_segment]
|
#[fixed_stack_segment]
|
||||||
pub fn new() -> FontContextHandle {
|
pub fn new() -> FontContextHandle {
|
||||||
unsafe {
|
unsafe {
|
||||||
match ft_pointer {
|
|
||||||
Some(ref ctx) => {
|
|
||||||
font_context_ref_count = font_context_ref_count + 1;
|
|
||||||
FontContextHandle {
|
|
||||||
ctx: FreeTypeLibraryHandle { ctx: ctx.clone() },
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
let ctx: FT_Library = ptr::null();
|
let ctx: FT_Library = ptr::null();
|
||||||
let result = FT_Init_FreeType(ptr::to_unsafe_ptr(&ctx));
|
let result = FT_Init_FreeType(ptr::to_unsafe_ptr(&ctx));
|
||||||
if !result.succeeded() { fail!("Unable to initialize FreeType library"); }
|
if !result.succeeded() { fail!("Unable to initialize FreeType library"); }
|
||||||
ft_pointer = Some(ctx);
|
|
||||||
font_context_ref_count = font_context_ref_count + 1;
|
|
||||||
FontContextHandle {
|
FontContextHandle {
|
||||||
ctx: FreeTypeLibraryHandle { ctx: ctx },
|
ctx: Arc::new(FreeTypeLibraryHandle { ctx: ctx }),
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,13 +46,10 @@ impl FontContextHandle {
|
||||||
|
|
||||||
impl FontContextHandleMethods for FontContextHandle {
|
impl FontContextHandleMethods for FontContextHandle {
|
||||||
fn clone(&self) -> FontContextHandle {
|
fn clone(&self) -> FontContextHandle {
|
||||||
unsafe {
|
|
||||||
font_context_ref_count = font_context_ref_count + 1;
|
|
||||||
FontContextHandle {
|
FontContextHandle {
|
||||||
ctx: self.ctx.clone()
|
ctx: self.ctx.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn create_font_from_identifier(&self, name: ~str, style: UsedFontStyle)
|
fn create_font_from_identifier(&self, name: ~str, style: UsedFontStyle)
|
||||||
-> Result<FontHandle, ()> {
|
-> Result<FontHandle, ()> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue