From c07355265fcc8dd4bc3cf05279b4b321fb68d476 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Tue, 30 Jan 2024 11:16:43 -0700 Subject: [PATCH] Add logging for the font_descriptor panic (#7097) Release Notes: - Fixed a panic caused by an inconsistency in font metrics. --- crates/gpui/src/platform/mac/text_system.rs | 39 ++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/crates/gpui/src/platform/mac/text_system.rs b/crates/gpui/src/platform/mac/text_system.rs index 053e5859be07acddc93e35a1464daa391dd63b85..24ab8346f41c1f2a66db9fce2c6390b7a05229d3 100644 --- a/crates/gpui/src/platform/mac/text_system.rs +++ b/crates/gpui/src/platform/mac/text_system.rs @@ -10,6 +10,7 @@ use core_foundation::{ array::CFIndex, attributed_string::{CFAttributedStringRef, CFMutableAttributedString}, base::{CFRange, TCFType}, + number::CFNumber, string::CFString, }; use core_graphics::{ @@ -17,7 +18,14 @@ use core_graphics::{ color_space::CGColorSpace, context::CGContext, }; -use core_text::{font::CTFont, line::CTLine, string_attributes::kCTFontAttributeName}; +use core_text::{ + font::CTFont, + font_descriptor::{ + kCTFontSlantTrait, kCTFontSymbolicTrait, kCTFontWeightTrait, kCTFontWidthTrait, + }, + line::CTLine, + string_attributes::kCTFontAttributeName, +}; use font_kit::{ font::Font as FontKitFont, handle::Handle, @@ -219,6 +227,35 @@ impl MacTextSystemState { let Some(_) = font.glyph_for_char('m') else { continue; }; + // We've seen a number of panics in production caused by calling font.properties() + // which unwraps a downcast to CFNumber. This is an attempt to avoid the panic, + // and to try and identify the incalcitrant font. + let traits = font.native_font().all_traits(); + if unsafe { + !(traits + .get(kCTFontSymbolicTrait) + .downcast::() + .is_some() + && traits + .get(kCTFontWidthTrait) + .downcast::() + .is_some() + && traits + .get(kCTFontWeightTrait) + .downcast::() + .is_some() + && traits + .get(kCTFontSlantTrait) + .downcast::() + .is_some()) + } { + log::error!( + "Failed to read traits for font {:?}", + font.postscript_name().unwrap() + ); + continue; + } + let font_id = FontId(self.fonts.len()); font_ids.push(font_id); let postscript_name = font.postscript_name().unwrap();