diff --git a/wezterm-font/src/lib.rs b/wezterm-font/src/lib.rs index 384026627..3b548ae44 100644 --- a/wezterm-font/src/lib.rs +++ b/wezterm-font/src/lib.rs @@ -40,6 +40,12 @@ pub use crate::shaper::{FallbackIdx, FontMetrics, GlyphInfo}; #[error("Font fallback recalculated")] pub struct ClearShapeCache {} +static FONT_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0); +pub type LoadedFontId = usize; +pub fn alloc_font_id() -> LoadedFontId { + FONT_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed) +} + pub struct LoadedFont { rasterizers: RefCell>>, handles: RefCell>, @@ -50,6 +56,7 @@ pub struct LoadedFont { font_config: Weak, pending_fallback: Arc>>, text_style: TextStyle, + id: LoadedFontId, } impl std::fmt::Debug for LoadedFont { @@ -74,6 +81,10 @@ impl LoadedFont { &self.text_style } + pub fn id(&self) -> LoadedFontId { + self.id + } + fn insert_fallback_handles(&self, extra_handles: Vec) -> anyhow::Result { let mut loaded = false; { @@ -575,6 +586,7 @@ impl FontConfigInner { font_config: Rc::downgrade(myself), pending_fallback: Arc::new(Mutex::new(vec![])), text_style: text_style.clone(), + id: alloc_font_id(), }); title_font.replace(Rc::clone(&loaded)); @@ -741,6 +753,7 @@ impl FontConfigInner { font_config: Rc::downgrade(myself), pending_fallback: Arc::new(Mutex::new(vec![])), text_style: style.clone(), + id: alloc_font_id(), }); fonts.insert(style.clone(), Rc::clone(&loaded)); diff --git a/wezterm-gui/src/glyphcache.rs b/wezterm-gui/src/glyphcache.rs index a649e0acb..9e2ca7394 100644 --- a/wezterm-gui/src/glyphcache.rs +++ b/wezterm-gui/src/glyphcache.rs @@ -24,13 +24,9 @@ use termwiz::color::RgbColor; use termwiz::image::{ImageData, ImageDataType}; use termwiz::surface::CursorShape; use wezterm_font::units::*; -use wezterm_font::{FontConfiguration, GlyphInfo, LoadedFont}; +use wezterm_font::{FontConfiguration, GlyphInfo, LoadedFont, LoadedFontId}; use wezterm_term::Underline; -pub fn rc_to_usize(rc: &Rc) -> usize { - Rc::as_ptr(rc) as usize -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct CellMetricKey { pub pixel_width: u16, @@ -60,8 +56,7 @@ pub struct GlyphKey { pub style: TextStyle, pub followed_by_space: bool, pub metric: CellMetricKey, - /// as produced by rc_to_usize - pub font_ptr: usize, + pub id: LoadedFontId, } /// We'd like to avoid allocating when resolving from the cache @@ -77,8 +72,7 @@ pub struct BorrowedGlyphKey<'a> { pub style: &'a TextStyle, pub followed_by_space: bool, pub metric: CellMetricKey, - /// as produced by rc_to_usize - pub font_ptr: usize, + pub id: LoadedFontId, } impl<'a> BorrowedGlyphKey<'a> { @@ -90,7 +84,7 @@ impl<'a> BorrowedGlyphKey<'a> { style: self.style.clone(), followed_by_space: self.followed_by_space, metric: self.metric, - font_ptr: self.font_ptr, + id: self.id, } } } @@ -108,7 +102,7 @@ impl GlyphKeyTrait for GlyphKey { style: &self.style, followed_by_space: self.followed_by_space, metric: self.metric, - font_ptr: self.font_ptr, + id: self.id, } } } @@ -358,7 +352,7 @@ impl GlyphCache { style, followed_by_space, metric: metrics.into(), - font_ptr: rc_to_usize(font), + id: font.id(), }; if let Some(entry) = self.glyph_cache.get(&key as &dyn GlyphKeyTrait) {