Fix segfault in QuartzNativeFont.create; improve error handling slightly

This commit is contained in:
Brian J. Burg 2012-09-10 22:35:17 -07:00
parent 2d8e764c9d
commit 1292fa2965

View file

@ -4,7 +4,6 @@ export QuartzNativeFont, with_test_native_font, create;
import libc::size_t; import libc::size_t;
import ptr::null; import ptr::null;
import unsafe::reinterpret_cast;
import glyph::GlyphIndex; import glyph::GlyphIndex;
import cocoa::cg::{ import cocoa::cg::{
CGDataProviderRef, CGDataProviderRef,
@ -16,6 +15,7 @@ import cocoa::cg::cg::{
CGFontCreateWithDataProvider, CGFontCreateWithDataProvider,
CGFontRelease CGFontRelease
}; };
use unsafe::transmute;
mod coretext { mod coretext {
@ -49,7 +49,7 @@ mod coretext {
} }
} }
struct QuartzNativeFont/& { struct QuartzNativeFont {
fontprov: CGDataProviderRef, fontprov: CGDataProviderRef,
cgfont: CGFontRef, cgfont: CGFontRef,
@ -119,7 +119,7 @@ impl QuartzNativeFont {
} }
} }
fn ctfont_from_cgfont(+cgfont: CGFontRef) -> coretext::CTFontRef unsafe { fn ctfont_from_cgfont(cgfont: CGFontRef) -> coretext::CTFontRef {
import coretext::CGFloat; import coretext::CGFloat;
import coretext::coretext::CTFontCreateWithGraphicsFont; import coretext::coretext::CTFontCreateWithGraphicsFont;
@ -131,17 +131,19 @@ fn create(buf: &~[u8]) -> Result<QuartzNativeFont, ()> {
let fontprov = vec::as_buf(*buf, |cbuf, len| { let fontprov = vec::as_buf(*buf, |cbuf, len| {
CGDataProviderCreateWithData( CGDataProviderCreateWithData(
null(), null(),
unsafe { reinterpret_cast(&cbuf) }, unsafe { transmute(&cbuf) },
len as size_t, len as size_t,
null()) null())
}); });
// FIXME: Error handling // FIXME: Error handling
assert fontprov.is_not_null(); assert fontprov.is_not_null();
let cgfont = CGFontCreateWithDataProvider(fontprov); let cgfont = CGFontCreateWithDataProvider(fontprov);
// FIXME: Error handling
assert cgfont.is_not_null();
return Ok(QuartzNativeFont(fontprov, cgfont)); match cgfont.is_not_null() {
true => Ok(QuartzNativeFont(fontprov, cgfont)),
false => Err(())
}
} }
fn with_test_native_font(f: fn@(nf: &NativeFont)) { fn with_test_native_font(f: fn@(nf: &NativeFont)) {