| 1 |
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 5 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 7 |
// option. This file may not be copied, modified, or distributed |
| 8 |
// except according to those terms. |
| 9 |
|
| 10 |
use font_descriptor; |
| 11 |
use font_descriptor::{CTFontDescriptor, CTFontDescriptorCreateMatchingFontDescriptors}; |
| 12 |
use font_manager::{ |
| 13 |
CTFontManagerCopyAvailableFontFamilyNames, CTFontManagerCopyAvailablePostScriptNames, |
| 14 |
}; |
| 15 |
|
| 16 |
use core_foundation::array::{CFArray, CFArrayRef}; |
| 17 |
use core_foundation::base::{CFTypeID, TCFType}; |
| 18 |
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef}; |
| 19 |
use core_foundation::number::CFNumber; |
| 20 |
use core_foundation::set::CFSet; |
| 21 |
use core_foundation::string::{CFString, CFStringRef}; |
| 22 |
|
| 23 |
use std::os::raw::c_void; |
| 24 |
|
| 25 |
#[repr(C)] |
| 26 |
pub struct __CTFontCollection(c_void); |
| 27 |
|
| 28 |
pub type CTFontCollectionRef = *const __CTFontCollection; |
| 29 |
|
| 30 |
declare_TCFType! { |
| 31 |
CTFontCollection, CTFontCollectionRef |
| 32 |
} |
| 33 |
impl_TCFType!( |
| 34 |
CTFontCollection, |
| 35 |
CTFontCollectionRef, |
| 36 |
CTFontCollectionGetTypeID |
| 37 |
); |
| 38 |
impl_CFTypeDescription!(CTFontCollection); |
| 39 |
|
| 40 |
impl CTFontCollection { |
| 41 |
pub fn get_descriptors(&self) -> Option<CFArray<CTFontDescriptor>> { |
| 42 |
// surprise! this function follows the Get rule, despite being named *Create*. |
| 43 |
// So we have to addRef it to avoid CTFontCollection from double freeing it later. |
| 44 |
unsafe { |
| 45 |
let font_descriptors = CTFontCollectionCreateMatchingFontDescriptors(self.0); |
| 46 |
if font_descriptors.is_null() { |
| 47 |
// This returns null if there are no matching font descriptors. |
| 48 |
None |
| 49 |
} else { |
| 50 |
Some(CFArray::wrap_under_get_rule(font_descriptors)) |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
pub fn new_from_descriptors(descs: &CFArray<CTFontDescriptor>) -> CTFontCollection { |
| 57 |
unsafe { |
| 58 |
let key = CFString::wrap_under_get_rule(kCTFontCollectionRemoveDuplicatesOption); |
| 59 |
let value = CFNumber::from(1i64); |
| 60 |
let options = CFDictionary::from_CFType_pairs(&[(key.as_CFType(), value.as_CFType())]); |
| 61 |
let font_collection_ref = CTFontCollectionCreateWithFontDescriptors( |
| 62 |
descs.as_concrete_TypeRef(), |
| 63 |
options.as_concrete_TypeRef(), |
| 64 |
); |
| 65 |
CTFontCollection::wrap_under_create_rule(font_collection_ref) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
pub fn create_for_all_families() -> CTFontCollection { |
| 70 |
unsafe { |
| 71 |
let key = CFString::wrap_under_get_rule(kCTFontCollectionRemoveDuplicatesOption); |
| 72 |
let value = CFNumber::from(1i64); |
| 73 |
let options = CFDictionary::from_CFType_pairs(&[(key.as_CFType(), value.as_CFType())]); |
| 74 |
let font_collection_ref = |
| 75 |
CTFontCollectionCreateFromAvailableFonts(options.as_concrete_TypeRef()); |
| 76 |
CTFontCollection::wrap_under_create_rule(font_collection_ref) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
pub fn create_for_family(family: &str) -> Option<CTFontCollection> { |
| 81 |
use font_descriptor::kCTFontFamilyNameAttribute; |
| 82 |
|
| 83 |
unsafe { |
| 84 |
let family_attr = CFString::wrap_under_get_rule(kCTFontFamilyNameAttribute); |
| 85 |
let family_name: CFString = family.parse().unwrap(); |
| 86 |
let specified_attrs = |
| 87 |
CFDictionary::from_CFType_pairs(&[(family_attr.clone(), family_name.as_CFType())]); |
| 88 |
|
| 89 |
let wildcard_desc: CTFontDescriptor = |
| 90 |
font_descriptor::new_from_attributes(&specified_attrs); |
| 91 |
let mandatory_attrs = CFSet::from_slice(&[family_attr.as_CFType()]); |
| 92 |
let matched_descs = CTFontDescriptorCreateMatchingFontDescriptors( |
| 93 |
wildcard_desc.as_concrete_TypeRef(), |
| 94 |
mandatory_attrs.as_concrete_TypeRef(), |
| 95 |
); |
| 96 |
if matched_descs.is_null() { |
| 97 |
return None; |
| 98 |
} |
| 99 |
let matched_descs = CFArray::wrap_under_create_rule(matched_descs); |
| 100 |
// I suppose one doesn't even need the CTFontCollection object at this point. |
| 101 |
// But we stick descriptors into and out of it just to provide a nice wrapper API. |
| 102 |
Some(new_from_descriptors(&matched_descs)) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
pub fn get_family_names() -> CFArray<CFString> { |
| 107 |
unsafe { CFArray::wrap_under_create_rule(CTFontManagerCopyAvailableFontFamilyNames()) } |
| 108 |
} |
| 109 |
|
| 110 |
pub fn get_postscript_names() -> CFArray<CFString> { |
| 111 |
unsafe { CFArray::wrap_under_create_rule(CTFontManagerCopyAvailablePostScriptNames()) } |
| 112 |
} |
| 113 |
|
| 114 |
extern "C" { |
| 115 |
/* |
| 116 |
* CTFontCollection.h |
| 117 |
*/ |
| 118 |
|
| 119 |
static kCTFontCollectionRemoveDuplicatesOption: CFStringRef; |
| 120 |
|
| 121 |
//fn CTFontCollectionCreateCopyWithFontDescriptors(original: CTFontCollectionRef, |
| 122 |
// descriptors: CFArrayRef, |
| 123 |
// options: CFDictionaryRef) -> CTFontCollectionRef; |
| 124 |
fn CTFontCollectionCreateFromAvailableFonts(options: CFDictionaryRef) -> CTFontCollectionRef; |
| 125 |
// this stupid function doesn't actually do any wildcard expansion; |
| 126 |
// it just chooses the best match. Use |
| 127 |
// CTFontDescriptorCreateMatchingDescriptors instead. |
| 128 |
fn CTFontCollectionCreateMatchingFontDescriptors(collection: CTFontCollectionRef) |
| 129 |
-> CFArrayRef; |
| 130 |
fn CTFontCollectionCreateWithFontDescriptors( |
| 131 |
descriptors: CFArrayRef, |
| 132 |
options: CFDictionaryRef, |
| 133 |
) -> CTFontCollectionRef; |
| 134 |
//fn CTFontCollectionCreateMatchingFontDescriptorsSortedWithCallback; |
| 135 |
fn CTFontCollectionGetTypeID() -> CFTypeID; |
| 136 |
} |