From 5a9ab37c0183fffe4ab6652f8e3d874f6a62209c Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Mon, 26 Oct 2020 14:08:54 -0400 Subject: [PATCH] Make sure we can round trip data fonts through descriptors. --- core-text/src/font.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/core-text/src/font.rs b/core-text/src/font.rs index 1c68abe..9341312 100644 --- a/core-text/src/font.rs +++ b/core-text/src/font.rs @@ -162,6 +162,13 @@ impl CTFont { } } + pub fn copy_descriptor(&self) -> CTFontDescriptor { + unsafe { + let desc = CTFontCopyFontDescriptor(self.0); + CTFontDescriptor::wrap_under_create_rule(desc) + } + } + pub fn clone_with_font_size(&self, size: f64) -> CTFont { unsafe { let font_ref = CTFontCreateCopyWithAttributes(self.0, @@ -545,7 +552,7 @@ extern { //fn CTFontCreateForString /* Getting Font Data */ - //fn CTFontCopyFontDescriptor(font: CTFontRef) -> CTFontDescriptorRef; + fn CTFontCopyFontDescriptor(font: CTFontRef) -> CTFontDescriptorRef; fn CTFontCopyAttribute(font: CTFontRef, attribute: CFStringRef) -> CFTypeRef; fn CTFontGetSize(font: CTFontRef) -> CGFloat; //fn CTFontGetMatrix @@ -638,3 +645,18 @@ extern { fn CTFontGetTypeID() -> CFTypeID; } +#[test] +fn copy_font() { + use std::io::Read; + let mut f = std::fs::File::open("/System/Library/Fonts/ZapfDingbats.ttf").unwrap(); + let mut font_data = Vec::new(); + f.read_to_end(&mut font_data).unwrap(); + let desc = crate::font_manager::create_font_descriptor(&font_data).unwrap(); + let font = new_from_descriptor(&desc, 12.); + drop(desc); + let desc = font.copy_descriptor(); + drop(font); + let font = new_from_descriptor(&desc, 14.); + assert_eq!(font.family_name(), "Zapf Dingbats"); +} +