Skip to content

More variation tests #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion core-graphics/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType};
use core_foundation::base::{CFRelease, CFRetain, CFType, CFTypeID, TCFType};
use core_foundation::array::{CFArray, CFArrayRef};
use core_foundation::data::{CFData, CFDataRef};
use core_foundation::number::CFNumber;
Expand Down Expand Up @@ -122,6 +122,25 @@ impl CGFont {
None
}
}

pub fn copy_variations(&self) -> Option<CFDictionary<CFString, CFNumber>> {
let variations = unsafe { CGFontCopyVariations(self.as_ptr()) };
if !variations.is_null() {
Some(unsafe { TCFType::wrap_under_create_rule(variations) })
} else {
None
}
}

pub fn copy_variation_axis(&self) -> Option<CFArray<CFDictionary<CFString, CFType>>> {
unsafe {
let axes = CGFontCopyVariationAxes(self.as_ptr());
if axes.is_null() {
return None;
}
Some(TCFType::wrap_under_create_rule(axes))
}
}
}

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

fn CGFontCopyTableTags(font: ::sys::CGFontRef) -> CFArrayRef;
fn CGFontCopyTableForTag(font: ::sys::CGFontRef, tag: u32) -> CFDataRef;
fn CGFontCopyVariations(font: ::sys::CGFontRef) -> CFDictionaryRef;
fn CGFontCopyVariationAxes(font: ::sys::CGFontRef) -> CFArrayRef;
}
36 changes: 35 additions & 1 deletion core-text/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,4 +741,38 @@ fn copy_system_font() {
assert!(matching.attributes().find(CFString::from_static_string("NSFontSizeAttribute")).is_none());

assert_eq!(small.postscript_name(), cgfont.postscript_name());
}
}

#[test]
fn variations_dict() {
let mut vals_str: Vec<(CFString, CFNumber)> = Vec::new();
let system_font = unsafe {
CTFont::wrap_under_create_rule(
CTFontCreateUIFontForLanguage(kCTFontEmphasizedSystemDetailFontType, 19., std::ptr::null())
)
};
let font = system_font.copy_to_CGFont();
vals_str.push((CFString::new("Weight"), (700.).into()) );
let vars = CFDictionary::from_CFType_pairs(&vals_str);
let var_font = CGFont::create_copy_from_variations(&font, &vars).unwrap();
match macos_version() {
(10, 11, _) => {
assert!(font.copy_variation_axis().is_none());
return;
}
_ => {}
}
let vars = var_font.copy_variations().unwrap();
let ct_font = new_from_CGFont_with_variations(&var_font.clone(), 19., &vars);

// check if our variations worked
let var = ct_font.copy_descriptor().attributes().find(CFString::from_static_string("NSCTFontVariationAttribute"))
.unwrap()
.downcast::<CFDictionary>()
.unwrap();
let var: CFDictionary<CFNumber, CFNumber> = unsafe { std::mem::transmute(var) };
match macos_version() {
(10, 12, _) => assert!(var.find(CFNumber::from(0x77676874)).is_none()), // XXX: I'm not sure why this is
_ => assert!(var.find(CFNumber::from(0x77676874)).is_some()),
}
}