Skip to content

Commit 0bcad1e

Browse files
Fix unwrap panic in extract_number_for_key in impl TraitAccessorPrivate for CTFontTraits on macOS Ventura. (#735)
* fix ventura crash * formatting --------- Co-authored-by: alokedesai <aloke@warp.dev>
1 parent 13cd114 commit 0bcad1e

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

core-text/src/font_descriptor.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use core_foundation::url::{CFURLRef, CFURL};
1919
use core_foundation::{declare_TCFType, impl_CFTypeDescription, impl_TCFType};
2020
use core_graphics::base::CGFloat;
2121

22+
use core_foundation::boolean::CFBoolean;
2223
use std::path::PathBuf;
2324

2425
/*
@@ -144,7 +145,23 @@ trait TraitAccessorPrivate {
144145
impl TraitAccessorPrivate for CTFontTraits {
145146
fn extract_number_for_key(&self, key: CFStringRef) -> CFNumber {
146147
let cftype = self.get(key);
147-
cftype.downcast::<CFNumber>().unwrap()
148+
let number = cftype.downcast::<CFNumber>();
149+
match number {
150+
Some(number) => number,
151+
None => {
152+
// The value was not able to be converted to a CFNumber, this violates the Core
153+
// Foundation's docs (see https://developer.apple.com/documentation/coretext/kctfontsymbolictrait)
154+
// but can occur in practice with certain fonts in MacOS 13 (Ventura). When this
155+
// does occur in Ventura, the value returned is always a CFBoolean, so we attempt to
156+
// convert into a boolean and create a number from there.
157+
let value_as_bool = bool::from(
158+
cftype
159+
.downcast::<CFBoolean>()
160+
.expect("Should be able to convert value into CFBoolean"),
161+
);
162+
CFNumber::from(value_as_bool as i32)
163+
}
164+
}
148165
}
149166
}
150167

0 commit comments

Comments
 (0)