diff --git a/core-text/src/font.rs b/core-text/src/font.rs index 53860171d..edbc42761 100644 --- a/core-text/src/font.rs +++ b/core-text/src/font.rs @@ -12,6 +12,7 @@ use font_descriptor; use font_descriptor::{CTFontDescriptor, CTFontDescriptorRef, CTFontOrientation}; use font_descriptor::{CTFontSymbolicTraits, CTFontTraits, SymbolicTraitAccessors, TraitAccessors}; +use font_manager::{create_font_descriptor, CTFontManagerIsSupportedFont}; use core_foundation::array::{CFArray, CFArrayRef}; use core_foundation::base::{CFIndex, CFOptionFlags, CFType, CFTypeID, CFTypeRef, TCFType}; @@ -30,6 +31,7 @@ use foreign_types::ForeignType; use libc::{self, size_t}; use std::os::raw::c_void; use std::ptr; +use std::path::Path; type CGContextRef = *mut ::CType; type CGFontRef = *mut ::CType; @@ -124,6 +126,11 @@ pub fn new_from_descriptor(desc: &CTFontDescriptor, pt_size: f64) -> CTFont { } } +pub fn new_from_buffer(buffer: &[u8]) -> Result { + let ct_font_descriptor = create_font_descriptor(buffer)?; + Ok(new_from_descriptor(&ct_font_descriptor, 16.0)) +} + pub fn new_from_name(name: &str, pt_size: f64) -> Result { unsafe { let name: CFString = name.parse().unwrap(); diff --git a/core-text/src/font_manager.rs b/core-text/src/font_manager.rs index 686704454..933c8da0b 100644 --- a/core-text/src/font_manager.rs +++ b/core-text/src/font_manager.rs @@ -11,6 +11,8 @@ use core_foundation::array::{CFArray, CFArrayRef}; use core_foundation::base::TCFType; use core_foundation::string::CFString; use core_foundation::url::CFURLRef; +use core_foundation::data::{CFDataRef, CFData}; +use crate::font_descriptor::{CTFontDescriptorRef, CTFontDescriptor}; pub fn copy_available_font_family_names() -> CFArray { unsafe { @@ -18,6 +20,17 @@ pub fn copy_available_font_family_names() -> CFArray { } } +pub fn create_font_descriptor(buffer: &[u8]) -> Result { + let cf_data = CFData::from_buffer(buffer); + unsafe { + let ct_font_descriptor_ref = CTFontManagerCreateFontDescriptorFromData(cf_data.as_concrete_TypeRef()); + if ct_font_descriptor_ref.is_null() { + return Err(()); + } + Ok(CTFontDescriptor::wrap_under_create_rule(ct_font_descriptor_ref)) + } +} + extern { /* * CTFontManager.h @@ -31,6 +44,7 @@ extern { pub fn CTFontManagerCopyAvailableFontFamilyNames() -> CFArrayRef; pub fn CTFontManagerCopyAvailablePostScriptNames() -> CFArrayRef; pub fn CTFontManagerCreateFontDescriptorsFromURL(fileURL: CFURLRef) -> CFArrayRef; + pub fn CTFontManagerCreateFontDescriptorFromData(data: CFDataRef) -> CTFontDescriptorRef; //pub fn CTFontManagerCreateFontRequestRunLoopSource //pub fn CTFontManagerEnableFontDescriptors //pub fn CTFontManagerGetAutoActivationSetting