Skip to content

Commit

Permalink
Auto merge of #410 - kaiwk:create-font-descriptor, r=jdm
Browse files Browse the repository at this point in the history
Add `CTFontManagerCreateFontDescriptorFromData` for avoid CGFont
  • Loading branch information
bors-servo authored Sep 13, 2020
2 parents bc3de2c + d76d3de commit adf0c6a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core-text/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 <CGContext as ForeignType>::CType;
type CGFontRef = *mut <CGFont as ForeignType>::CType;
Expand Down Expand Up @@ -124,6 +126,11 @@ pub fn new_from_descriptor(desc: &CTFontDescriptor, pt_size: f64) -> CTFont {
}
}

pub fn new_from_buffer(buffer: &[u8]) -> Result<CTFont, ()> {
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<CTFont, ()> {
unsafe {
let name: CFString = name.parse().unwrap();
Expand Down
14 changes: 14 additions & 0 deletions core-text/src/font_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@ 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<CFString> {
unsafe {
TCFType::wrap_under_create_rule(CTFontManagerCopyAvailableFontFamilyNames())
}
}

pub fn create_font_descriptor(buffer: &[u8]) -> Result<CTFontDescriptor, ()> {
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
Expand All @@ -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
Expand Down

0 comments on commit adf0c6a

Please sign in to comment.