From 1ee1d0661e50419062735c0a6373e5fabae50354 Mon Sep 17 00:00:00 2001 From: JakkuSakura Date: Wed, 2 Apr 2025 01:24:21 +0800 Subject: [PATCH] Refactor FontExt::new to simplify face parsing logic and fix MacOS font panic --- plotters/src/style/font/ttf.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/plotters/src/style/font/ttf.rs b/plotters/src/style/font/ttf.rs index 5d5a7209..1acf8c69 100644 --- a/plotters/src/style/font/ttf.rs +++ b/plotters/src/style/font/ttf.rs @@ -76,14 +76,13 @@ impl Drop for FontExt { impl FontExt { fn new(font: Font) -> Self { let handle = font.handle(); - let (data, idx) = match handle.as_ref() { - Some(Handle::Memory { bytes, font_index }) => (&bytes[..], *font_index), - _ => unreachable!(), - }; - let face = unsafe { - std::mem::transmute::, Option>>( - ttf_parser::Face::parse(data, idx).ok(), - ) + let face = match handle.as_ref() { + Some(Handle::Memory { bytes, font_index }) => unsafe { + std::mem::transmute::, Option>>( + ttf_parser::Face::parse(bytes, *font_index).ok(), + ) + }, + _ => None, }; Self { inner: font, face } }