use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType};
use core_foundation::string::{CFString, CFStringRef};
use data_provider::CGDataProvider;
use foreign_types::ForeignType;
use libc;
pub type CGGlyph = libc::c_ushort;
foreign_type! {
#[doc(hidden)]
type CType = ::sys::CGFont;
fn drop = |p| CFRelease(p as *mut _);
fn clone = |p| CFRetain(p as *const _) as *mut _;
pub struct CGFont;
pub struct CGFontRef;
}
unsafe impl Send for CGFont {}
unsafe impl Sync for CGFont {}
impl CGFont {
pub fn type_id() -> CFTypeID {
unsafe {
CGFontGetTypeID()
}
}
pub fn from_data_provider(provider: CGDataProvider) -> Result<CGFont, ()> {
unsafe {
let font_ref = CGFontCreateWithDataProvider(provider.as_ptr());
if !font_ref.is_null() {
Ok(CGFont::from_ptr(font_ref))
} else {
Err(())
}
}
}
pub fn from_name(name: &CFString) -> Result<CGFont, ()> {
unsafe {
let font_ref = CGFontCreateWithFontName(name.as_concrete_TypeRef());
if !font_ref.is_null() {
Ok(CGFont::from_ptr(font_ref))
} else {
Err(())
}
}
}
pub fn postscript_name(&self) -> CFString {
unsafe {
let string_ref = CGFontCopyPostScriptName(self.as_ptr());
TCFType::wrap_under_create_rule(string_ref)
}
}
}
#[link(name = "ApplicationServices", kind = "framework")]
extern {
fn CGFontCreateWithDataProvider(provider: ::sys::CGDataProviderRef) -> ::sys::CGFontRef;
fn CGFontCreateWithFontName(name: CFStringRef) -> ::sys::CGFontRef;
fn CGFontGetTypeID() -> CFTypeID;
fn CGFontCopyPostScriptName(font: ::sys::CGFontRef) -> CFStringRef;
}