fonts: replace font_ptr with a font id

I'm running down a weird thing where the main font renders weirdly
when the title font is 12 pt vs the main font 10 pt.

I thought there might have been a cache invalidation issue and
realized that we could have an A-B-A style issue with the font_ptr
stuff, so I replaced it with an incrementing id.

That didn't fix the thing I was looking at, but does feel a bit
nicer overall.
This commit is contained in:
Wez Furlong committed 2021-12-30 19:09:30 -07:00
1 parent a322c3e8d2
commit 1e7d552844
2 files changed
+19 -12

No files matched your search

+13
View File
@@ -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<HashMap<FallbackIdx, Box<dyn FontRasterizer>>>,
handles: RefCell<Vec<ParsedFont>>,
@@ -50,6 +56,7 @@ pub struct LoadedFont {
font_config: Weak<FontConfigInner>,
pending_fallback: Arc<Mutex<Vec<ParsedFont>>>,
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<ParsedFont>) -> anyhow::Result<bool> {
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));
+6 -12
View File
@@ -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<T>(rc: &Rc<T>) -> 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<T: Texture2d> GlyphCache<T> {
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) {