From 1993281ca6bc3df73dacb369e45fe66cd859bc17 Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 14 Sep 2017 23:27:22 +0100 Subject: [PATCH] Use foreign-types for CGFont --- src/font.rs | 90 ++++++++++++----------------------------------------- src/sys.rs | 4 +++ 2 files changed, 24 insertions(+), 70 deletions(-) diff --git a/src/font.rs b/src/font.rs index 42a8329..798dd46 100644 --- a/src/font.rs +++ b/src/font.rs @@ -7,89 +7,39 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType}; use core_foundation::string::{CFString, CFStringRef}; use data_provider::CGDataProvider; + use foreign_types::ForeignType; use libc; -use std::mem; -use std::ptr; pub type CGGlyph = libc::c_ushort; -// This is an enum due to zero-sized types warnings. -// For more details see https://github.com/rust-lang/rust/issues/27303 -pub enum __CGFont {} - -pub type CGFontRef = *const __CGFont; - -pub struct CGFont { - obj: CGFontRef, +foreign_type! { + 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 Clone for CGFont { - #[inline] - fn clone(&self) -> CGFont { - unsafe { - TCFType::wrap_under_get_rule(self.obj) - } - } -} - -impl Drop for CGFont { - fn drop(&mut self) { - unsafe { - let ptr = self.as_CFTypeRef(); - assert!(ptr != ptr::null()); - CFRelease(ptr); - } - } -} - -impl TCFType for CGFont { - #[inline] - fn as_concrete_TypeRef(&self) -> CGFontRef { - self.obj - } - - #[inline] - unsafe fn wrap_under_get_rule(reference: CGFontRef) -> CGFont { - let reference: CGFontRef = mem::transmute(CFRetain(mem::transmute(reference))); - TCFType::wrap_under_create_rule(reference) - } - - #[inline] - fn as_CFTypeRef(&self) -> CFTypeRef { - unsafe { - mem::transmute(self.as_concrete_TypeRef()) - } - } - - #[inline] - unsafe fn wrap_under_create_rule(obj: CGFontRef) -> CGFont { - CGFont { - obj: obj, - } - } - - #[inline] - fn type_id() -> CFTypeID { +impl CGFont { + pub fn type_id() -> CFTypeID { unsafe { CGFontGetTypeID() } } -} -impl CGFont { pub fn from_data_provider(provider: CGDataProvider) -> Result { unsafe { let font_ref = CGFontCreateWithDataProvider(provider.as_ptr()); - if font_ref != ptr::null() { - Ok(TCFType::wrap_under_create_rule(font_ref)) + if !font_ref.is_null() { + Ok(CGFont::from_ptr(font_ref)) } else { Err(()) } @@ -99,8 +49,8 @@ impl CGFont { pub fn from_name(name: &CFString) -> Result { unsafe { let font_ref = CGFontCreateWithFontName(name.as_concrete_TypeRef()); - if font_ref != ptr::null() { - Ok(TCFType::wrap_under_create_rule(font_ref)) + if !font_ref.is_null() { + Ok(CGFont::from_ptr(font_ref)) } else { Err(()) } @@ -109,7 +59,7 @@ impl CGFont { pub fn postscript_name(&self) -> CFString { unsafe { - let string_ref = CGFontCopyPostScriptName(self.obj); + let string_ref = CGFontCopyPostScriptName(self.as_ptr()); TCFType::wrap_under_create_rule(string_ref) } } @@ -118,14 +68,14 @@ impl CGFont { #[link(name = "ApplicationServices", kind = "framework")] extern { // TODO: basically nothing has bindings (even commented-out) besides what we use. - fn CGFontCreateWithDataProvider(provider: ::sys::CGDataProviderRef) -> CGFontRef; - fn CGFontCreateWithFontName(name: CFStringRef) -> CGFontRef; + fn CGFontCreateWithDataProvider(provider: ::sys::CGDataProviderRef) -> ::sys::CGFontRef; + fn CGFontCreateWithFontName(name: CFStringRef) -> ::sys::CGFontRef; fn CGFontGetTypeID() -> CFTypeID; - fn CGFontCopyPostScriptName(font: CGFontRef) -> CFStringRef; + fn CGFontCopyPostScriptName(font: ::sys::CGFontRef) -> CFStringRef; // These do the same thing as CFRetain/CFRelease, except // gracefully handle a NULL argument. We don't use them. - //fn CGFontRetain(font: CGFontRef); - //fn CGFontRelease(font: CGFontRef); + //fn CGFontRetain(font: ::sys::CGFontRef); + //fn CGFontRelease(font: ::sys::CGFontRef); } diff --git a/src/sys.rs b/src/sys.rs index 7d3c619..f709bb6 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -7,3 +7,7 @@ pub type CGColorSpaceRef = *mut CGColorSpace; pub enum CGDataProvider {} pub type CGDataProviderRef = *mut CGDataProvider; +pub enum CGFont {} +pub type CGFontRef = *mut CGFont; + +