Skip to content

Commit a849ee5

Browse files
committed
Test the creation of variable CTFonts from CGFonts
This also adds some methods to CGFont for dealing with variations.
1 parent 78634a7 commit a849ee5

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

core-graphics/src/font.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType};
10+
use core_foundation::base::{CFRelease, CFRetain, CFType, CFTypeID, TCFType};
1111
use core_foundation::array::{CFArray, CFArrayRef};
1212
use core_foundation::data::{CFData, CFDataRef};
1313
use core_foundation::number::CFNumber;
@@ -122,6 +122,25 @@ impl CGFont {
122122
None
123123
}
124124
}
125+
126+
pub fn copy_variations(&self) -> Option<CFDictionary<CFString, CFNumber>> {
127+
let variations = unsafe { CGFontCopyVariations(self.as_ptr()) };
128+
if !variations.is_null() {
129+
Some(unsafe { TCFType::wrap_under_create_rule(variations) })
130+
} else {
131+
None
132+
}
133+
}
134+
135+
pub fn copy_variation_axis(&self) -> Option<CFArray<CFDictionary<CFString, CFType>>> {
136+
unsafe {
137+
let axes = CGFontCopyVariationAxes(self.as_ptr());
138+
if axes.is_null() {
139+
return None;
140+
}
141+
Some(TCFType::wrap_under_create_rule(axes))
142+
}
143+
}
125144
}
126145

127146
#[link(name = "CoreGraphics", kind = "framework")]
@@ -153,4 +172,6 @@ extern {
153172

154173
fn CGFontCopyTableTags(font: ::sys::CGFontRef) -> CFArrayRef;
155174
fn CGFontCopyTableForTag(font: ::sys::CGFontRef, tag: u32) -> CFDataRef;
175+
fn CGFontCopyVariations(font: ::sys::CGFontRef) -> CFDictionaryRef;
176+
fn CGFontCopyVariationAxes(font: ::sys::CGFontRef) -> CFArrayRef;
156177
}

core-text/src/font.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,4 +741,38 @@ fn copy_system_font() {
741741
assert!(matching.attributes().find(CFString::from_static_string("NSFontSizeAttribute")).is_none());
742742

743743
assert_eq!(small.postscript_name(), cgfont.postscript_name());
744-
}
744+
}
745+
746+
#[test]
747+
fn variations_dict() {
748+
let mut vals_str: Vec<(CFString, CFNumber)> = Vec::new();
749+
let system_font = unsafe {
750+
CTFont::wrap_under_create_rule(
751+
CTFontCreateUIFontForLanguage(kCTFontEmphasizedSystemDetailFontType, 19., std::ptr::null())
752+
)
753+
};
754+
let font = system_font.copy_to_CGFont();
755+
vals_str.push((CFString::new("Weight"), (700.).into()) );
756+
let vars = CFDictionary::from_CFType_pairs(&vals_str);
757+
let var_font = CGFont::create_copy_from_variations(&font, &vars).unwrap();
758+
match macos_version() {
759+
(10, 11, _) => {
760+
assert!(font.copy_variation_axis().is_none());
761+
return;
762+
}
763+
_ => {}
764+
}
765+
let vars = var_font.copy_variations().unwrap();
766+
let ct_font = new_from_CGFont_with_variations(&var_font.clone(), 19., &vars);
767+
768+
// check if our variations worked
769+
let var = ct_font.copy_descriptor().attributes().find(CFString::from_static_string("NSCTFontVariationAttribute"))
770+
.unwrap()
771+
.downcast::<CFDictionary>()
772+
.unwrap();
773+
let var: CFDictionary<CFNumber, CFNumber> = unsafe { std::mem::transmute(var) };
774+
match macos_version() {
775+
(10, 12, _) => assert!(var.find(CFNumber::from(0x77676874)).is_none()), // XXX: I'm not sure why this is
776+
_ => assert!(var.find(CFNumber::from(0x77676874)).is_some()),
777+
}
778+
}

0 commit comments

Comments
 (0)