From a7cc03d640dbeee61c681319e2a5854e166bea2f Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Sat, 27 Jan 2018 15:44:01 -0500 Subject: [PATCH] Use macros from core-foundation for declaring types This cleans up the code a lot and gives an implementation of Clone. --- src/font.rs | 99 ++++++++++-------------------------------- src/font_collection.rs | 55 +++-------------------- src/font_descriptor.rs | 56 +++--------------------- src/lib.rs | 1 + 4 files changed, 37 insertions(+), 174 deletions(-) diff --git a/src/font.rs b/src/font.rs index 053f2e6..c73d0e9 100644 --- a/src/font.rs +++ b/src/font.rs @@ -13,7 +13,7 @@ use font_descriptor::{CTFontDescriptor, CTFontDescriptorRef, CTFontOrientation}; use font_descriptor::{CTFontSymbolicTraits, CTFontTraits, SymbolicTraitAccessors, TraitAccessors}; use core_foundation::array::{CFArray, CFArrayRef}; -use core_foundation::base::{CFIndex, CFOptionFlags, CFTypeID, CFRelease, CFRetain, CFTypeRef, TCFType}; +use core_foundation::base::{CFIndex, CFOptionFlags, CFTypeID, CFTypeRef, TCFType}; use core_foundation::data::{CFData, CFDataRef}; use core_foundation::dictionary::CFDictionaryRef; use core_foundation::string::{CFString, CFStringRef, UniChar}; @@ -26,7 +26,6 @@ use core_graphics::path::CGPath; use foreign_types::ForeignType; use libc::{self, size_t, c_void}; -use std::mem; use std::ptr; type CGContextRef = *mut ::CType; @@ -80,63 +79,11 @@ pub struct __CTFont(c_void); pub type CTFontRef = *const __CTFont; -#[derive(Debug)] -pub struct CTFont { - obj: CTFontRef, -} - -impl Drop for CTFont { - fn drop(&mut self) { - unsafe { - CFRelease(self.as_CFTypeRef()) - } - } -} - -impl Clone for CTFont { - #[inline] - fn clone(&self) -> CTFont { - unsafe { - TCFType::wrap_under_get_rule(self.obj) - } - } -} - -impl TCFType for CTFont { - type Ref = CTFontRef; - - #[inline] - fn as_concrete_TypeRef(&self) -> CTFontRef { - self.obj - } - - #[inline] - unsafe fn wrap_under_get_rule(reference: CTFontRef) -> CTFont { - let reference: CTFontRef = mem::transmute(CFRetain(mem::transmute(reference))); - TCFType::wrap_under_create_rule(reference) - } - - #[inline] - unsafe fn wrap_under_create_rule(obj: CTFontRef) -> CTFont { - CTFont { - obj: obj, - } - } - - #[inline] - fn as_CFTypeRef(&self) -> CFTypeRef { - unsafe { - mem::transmute(self.as_concrete_TypeRef()) - } - } - - #[inline] - fn type_id() -> CFTypeID { - unsafe { - CTFontGetTypeID() - } - } +declare_TCFType! { + CTFont, CTFontRef } +impl_TCFType!(CTFont, CTFontRef, CTFontGetTypeID); +impl_CFTypeDescription!(CTFont); pub fn new_from_CGFont(cgfont: &CGFont, pt_size: f64) -> CTFont { unsafe { @@ -175,7 +122,7 @@ impl CTFont { // Properties pub fn symbolic_traits(&self) -> CTFontSymbolicTraits { unsafe { - CTFontGetSymbolicTraits(self.obj) + CTFontGetSymbolicTraits(self.0) } } } @@ -184,14 +131,14 @@ impl CTFont { // Creation methods pub fn copy_to_CGFont(&self) -> CGFont { unsafe { - let cgfont_ref = CTFontCopyGraphicsFont(self.obj, ptr::null_mut()); + let cgfont_ref = CTFontCopyGraphicsFont(self.0, ptr::null_mut()); CGFont::from_ptr(cgfont_ref as *mut _) } } pub fn clone_with_font_size(&self, size: f64) -> CTFont { unsafe { - let font_ref = CTFontCreateCopyWithAttributes(self.obj, + let font_ref = CTFontCreateCopyWithAttributes(self.0, size as CGFloat, ptr::null(), ptr::null()); @@ -230,69 +177,69 @@ impl CTFont { pub fn all_traits(&self) -> CTFontTraits { unsafe { - TCFType::wrap_under_create_rule(CTFontCopyTraits(self.obj)) + TCFType::wrap_under_create_rule(CTFontCopyTraits(self.0)) } } // Font metrics pub fn ascent(&self) -> CGFloat { unsafe { - CTFontGetAscent(self.obj) + CTFontGetAscent(self.0) } } pub fn descent(&self) -> CGFloat { unsafe { - CTFontGetDescent(self.obj) + CTFontGetDescent(self.0) } } pub fn underline_thickness(&self) -> CGFloat { unsafe { - CTFontGetUnderlineThickness(self.obj) + CTFontGetUnderlineThickness(self.0) } } pub fn underline_position(&self) -> CGFloat { unsafe { - CTFontGetUnderlinePosition(self.obj) + CTFontGetUnderlinePosition(self.0) } } pub fn bounding_box(&self) -> CGRect { unsafe { - CTFontGetBoundingBox(self.obj) + CTFontGetBoundingBox(self.0) } } pub fn leading(&self) -> CGFloat { unsafe { - CTFontGetLeading(self.obj) + CTFontGetLeading(self.0) } } pub fn units_per_em(&self) -> libc::c_uint { unsafe { - CTFontGetUnitsPerEm(self.obj) + CTFontGetUnitsPerEm(self.0) } } pub fn x_height(&self) -> CGFloat { unsafe { - CTFontGetXHeight(self.obj) + CTFontGetXHeight(self.0) } } pub fn pt_size(&self) -> CGFloat { unsafe { - CTFontGetSize(self.obj) + CTFontGetSize(self.0) } } pub fn get_glyphs_for_characters(&self, characters: *const UniChar, glyphs: *mut CGGlyph, count: CFIndex) -> bool { unsafe { - CTFontGetGlyphsForCharacters(self.obj, characters, glyphs, count) + CTFontGetGlyphsForCharacters(self.0, characters, glyphs, count) } } @@ -303,13 +250,13 @@ impl CTFont { count: CFIndex) -> f64 { unsafe { - CTFontGetAdvancesForGlyphs(self.obj, orientation, glyphs, advances, count) as f64 + CTFontGetAdvancesForGlyphs(self.0, orientation, glyphs, advances, count) as f64 } } pub fn get_font_table(&self, tag: u32) -> Option { unsafe { - let result = CTFontCopyTable(self.obj, + let result = CTFontCopyTable(self.0, tag as CTFontTableTag, kCTFontTableOptionsExcludeSynthetic); if result.is_null() { @@ -345,7 +292,7 @@ impl CTFont { pub fn url(&self) -> Option { unsafe { - let result = CTFontCopyAttribute(self.obj, kCTFontURLAttribute); + let result = CTFontCopyAttribute(self.0, kCTFontURLAttribute); if result.is_null() { None } else { @@ -357,7 +304,7 @@ impl CTFont { pub fn create_path_for_glyph(&self, glyph: CGGlyph, matrix: &CGAffineTransform) -> Result { unsafe { - let path = CTFontCreatePathForGlyph(self.obj, glyph, matrix); + let path = CTFontCreatePathForGlyph(self.0, glyph, matrix); if path.is_null() { Err(()) } else { diff --git a/src/font_collection.rs b/src/font_collection.rs index df2235a..041d330 100644 --- a/src/font_collection.rs +++ b/src/font_collection.rs @@ -12,14 +12,13 @@ use font_descriptor::{CTFontDescriptor, CTFontDescriptorCreateMatchingFontDescri use font_manager::CTFontManagerCopyAvailableFontFamilyNames; use core_foundation::array::{CFArray, CFArrayRef}; -use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType}; +use core_foundation::base::{CFTypeID, TCFType}; use core_foundation::dictionary::{CFDictionary, CFDictionaryRef}; use core_foundation::number::CFNumber; use core_foundation::set::CFSet; use core_foundation::string::{CFString, CFStringRef}; use libc::c_void; -use std::mem; use std::ptr; #[repr(C)] @@ -27,61 +26,19 @@ pub struct __CTFontCollection(c_void); pub type CTFontCollectionRef = *const __CTFontCollection; -#[derive(Debug)] -pub struct CTFontCollection { - obj: CTFontCollectionRef, +declare_TCFType! { + CTFontCollection, CTFontCollectionRef } +impl_TCFType!(CTFontCollection, CTFontCollectionRef, CTFontCollectionGetTypeID); +impl_CFTypeDescription!(CTFontCollection); -impl Drop for CTFontCollection { - fn drop(&mut self) { - unsafe { - CFRelease(self.as_CFTypeRef()) - } - } -} - -impl TCFType for CTFontCollection { - type Ref = CTFontCollectionRef; - - #[inline] - fn as_concrete_TypeRef(&self) -> CTFontCollectionRef { - self.obj - } - - #[inline] - unsafe fn wrap_under_get_rule(reference: CTFontCollectionRef) -> CTFontCollection { - let reference: CTFontCollectionRef = mem::transmute(CFRetain(mem::transmute(reference))); - TCFType::wrap_under_create_rule(reference) - } - - #[inline] - unsafe fn wrap_under_create_rule(obj: CTFontCollectionRef) -> CTFontCollection { - CTFontCollection { - obj: obj, - } - } - - #[inline] - fn as_CFTypeRef(&self) -> CFTypeRef { - unsafe { - mem::transmute(self.as_concrete_TypeRef()) - } - } - - #[inline] - fn type_id() -> CFTypeID { - unsafe { - CTFontCollectionGetTypeID() - } - } -} impl CTFontCollection { pub fn get_descriptors(&self) -> CFArray { // surprise! this function follows the Get rule, despite being named *Create*. // So we have to addRef it to avoid CTFontCollection from double freeing it later. unsafe { - TCFType::wrap_under_get_rule(CTFontCollectionCreateMatchingFontDescriptors(self.obj)) + TCFType::wrap_under_get_rule(CTFontCollectionCreateMatchingFontDescriptors(self.0)) } } } diff --git a/src/font_descriptor.rs b/src/font_descriptor.rs index c10874f..ce7ce09 100644 --- a/src/font_descriptor.rs +++ b/src/font_descriptor.rs @@ -10,7 +10,7 @@ #![allow(non_upper_case_globals)] use core_foundation::array::CFArrayRef; -use core_foundation::base::{CFRelease, CFRetain, CFType, CFTypeID, CFTypeRef, TCFType}; +use core_foundation::base::{CFType, CFTypeID, CFTypeRef, TCFType}; use core_foundation::dictionary::{CFDictionary, CFDictionaryRef}; use core_foundation::number::{CFNumber, CFNumberRef}; use core_foundation::set::CFSetRef; @@ -187,59 +187,17 @@ pub struct __CTFontDescriptor(c_void); pub type CTFontDescriptorRef = *const __CTFontDescriptor; -#[derive(Debug)] -pub struct CTFontDescriptor { - obj: CTFontDescriptorRef, +declare_TCFType! { + CTFontDescriptor, CTFontDescriptorRef } +impl_TCFType!(CTFontDescriptor, CTFontDescriptorRef, CTFontDescriptorGetTypeID); +impl_CFTypeDescription!(CTFontDescriptor); -impl Drop for CTFontDescriptor { - fn drop(&mut self) { - unsafe { - CFRelease(self.as_CFTypeRef()) - } - } -} - -impl TCFType for CTFontDescriptor { - type Ref = CTFontDescriptorRef; - - #[inline] - fn as_concrete_TypeRef(&self) -> CTFontDescriptorRef { - self.obj - } - - #[inline] - unsafe fn wrap_under_get_rule(reference: CTFontDescriptorRef) -> CTFontDescriptor { - let reference: CTFontDescriptorRef = 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: CTFontDescriptorRef) -> CTFontDescriptor { - CTFontDescriptor { - obj: obj, - } - } - - #[inline] - fn type_id() -> CFTypeID { - unsafe { - CTFontDescriptorGetTypeID() - } - } -} impl CTFontDescriptor { fn get_string_attribute(&self, attribute: CFStringRef) -> Option { unsafe { - let value = CTFontDescriptorCopyAttribute(self.obj, attribute); + let value = CTFontDescriptorCopyAttribute(self.0, attribute); if value.is_null() { return None } @@ -284,7 +242,7 @@ impl CTFontDescriptor { pub fn font_path(&self) -> Option { unsafe { - let value = CTFontDescriptorCopyAttribute(self.obj, kCTFontURLAttribute); + let value = CTFontDescriptorCopyAttribute(self.0, kCTFontURLAttribute); if value.is_null() { return None; } diff --git a/src/lib.rs b/src/lib.rs index 03d8c3e..401146a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,7 @@ extern crate foreign_types; extern crate libc; +#[macro_use] extern crate core_foundation; extern crate core_graphics;