Skip to content

Commit b2f1089

Browse files
committed
Test that copying system fonts doesn't change the url
1 parent e7c5320 commit b2f1089

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

core-text/src/font.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ extern {
539539
fn CTFontCreateWithFontDescriptor(descriptor: CTFontDescriptorRef, size: CGFloat,
540540
matrix: *const CGAffineTransform) -> CTFontRef;
541541
//fn CTFontCreateWithFontDescriptorAndOptions
542-
//fn CTFontCreateUIFontForLanguage
542+
#[cfg(test)]
543+
fn CTFontCreateUIFontForLanguage(uiType: CTFontUIFontType, size: CGFloat, language: CFStringRef) -> CTFontRef;
543544
fn CTFontCreateCopyWithAttributes(font: CTFontRef, size: CGFloat, matrix: *const CGAffineTransform,
544545
attributes: CTFontDescriptorRef) -> CTFontRef;
545546
fn CTFontCreateCopyWithSymbolicTraits(font: CTFontRef,
@@ -660,3 +661,70 @@ fn copy_font() {
660661
assert_eq!(font.family_name(), "Zapf Dingbats");
661662
}
662663

664+
#[cfg(test)]
665+
fn macos_version() -> (i32, i32, i32) {
666+
use std::io::Read;
667+
668+
let file = "/System/Library/CoreServices/SystemVersion.plist";
669+
let mut f = std::fs::File::open(file).unwrap();
670+
let mut system_version_data = Vec::new();
671+
f.read_to_end(&mut system_version_data).unwrap();
672+
673+
use core_foundation::propertylist;
674+
let (list, _) = propertylist::create_with_data(core_foundation::data::CFData::from_buffer(&system_version_data), propertylist::kCFPropertyListImmutable).unwrap();
675+
let k = unsafe { propertylist::CFPropertyList::wrap_under_create_rule(list) };
676+
677+
let dict = unsafe { std::mem::transmute::<_, CFDictionary<CFType, CFType>>(k.downcast::<CFDictionary>().unwrap()) };
678+
679+
let version = dict.find(&CFString::new("ProductVersion").as_CFType())
680+
.as_ref().unwrap()
681+
.downcast::<CFString>().unwrap()
682+
.to_string();
683+
684+
match version.split(".").map(|x| x.parse().unwrap()).collect::<Vec<_>>()[..] {
685+
[a, b, c] => (a, b, c),
686+
[a, b] => (a, b, 0),
687+
_ => panic!()
688+
}
689+
}
690+
691+
#[test]
692+
fn copy_system_font() {
693+
let small = unsafe {
694+
CTFont::wrap_under_create_rule(
695+
CTFontCreateUIFontForLanguage(kCTFontSystemDetailFontType, 19., std::ptr::null())
696+
)
697+
};
698+
let big = small.clone_with_font_size(20.);
699+
700+
// ensure that we end up with different fonts for the different sizes before 10.15
701+
if macos_version() < (10, 15, 0) {
702+
assert_ne!(big.url(), small.url());
703+
} else {
704+
assert_eq!(big.url(), small.url());
705+
}
706+
707+
let ps = small.postscript_name();
708+
let desc = small.copy_descriptor();
709+
710+
// check that we can construct a new vesion by descriptor
711+
let ctfont = new_from_descriptor(&desc, 20.);
712+
assert_eq!(big.postscript_name(), ctfont.postscript_name());
713+
714+
// on newer versions of macos we can't construct by name anymore
715+
if macos_version() < (10, 13, 0) {
716+
let ui_font_by_name = new_from_name(&small.postscript_name(), 19.).unwrap();
717+
assert_eq!(ui_font_by_name.postscript_name(), small.postscript_name());
718+
719+
let ui_font_by_name = new_from_name(&small.postscript_name(), 20.).unwrap();
720+
assert_eq!(ui_font_by_name.postscript_name(), small.postscript_name());
721+
722+
let ui_font_by_name = new_from_name(&big.postscript_name(), 20.).unwrap();
723+
assert_eq!(ui_font_by_name.postscript_name(), big.postscript_name());
724+
}
725+
726+
// but we can still construct the CGFont by name
727+
let cgfont = CGFont::from_name(&CFString::new(&ps)).unwrap();
728+
let cgfont = new_from_CGFont(&cgfont, 20.);
729+
assert_eq!(small.postscript_name(), cgfont.postscript_name());
730+
}

0 commit comments

Comments
 (0)