Skip to content

Commit

Permalink
Try to improve support for .notdef glyphs through FontTweak
Browse files Browse the repository at this point in the history
  • Loading branch information
Warpten committed Jun 9, 2024
1 parent 9f12432 commit 4874b90
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
28 changes: 17 additions & 11 deletions crates/epaint/src/text/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub struct FontImpl {
pixels_per_point: f32,
glyph_info_cache: RwLock<ahash::HashMap<char, GlyphInfo>>, // TODO(emilk): standard Mutex
atlas: Arc<Mutex<TextureAtlas>>,

fallbacks: [char; 2],
}

impl FontImpl {
Expand Down Expand Up @@ -132,6 +134,7 @@ impl FontImpl {
pixels_per_point,
glyph_info_cache: Default::default(),
atlas,
fallbacks: tweak.fallbacks.unwrap_or(['◻', '?']),
}
}

Expand Down Expand Up @@ -363,20 +366,23 @@ impl Font {
glyph_info_cache: Default::default(),
};

const PRIMARY_REPLACEMENT_CHAR: char = '◻'; // white medium square
const FALLBACK_REPLACEMENT_CHAR: char = '?'; // fallback for the fallback

let replacement_glyph = slf
.glyph_info_no_cache_or_fallback(PRIMARY_REPLACEMENT_CHAR)
.or_else(|| slf.glyph_info_no_cache_or_fallback(FALLBACK_REPLACEMENT_CHAR))
// For each font, find the first available fallback character
slf.replacement_glyph = slf
.fonts
.iter()
.enumerate()
.find_map(|(font_index, font_impl)| {
font_impl.fallbacks.iter().find_map(|chr| {
font_impl
.glyph_info(*chr)
.map(|glyph_info| (font_index, glyph_info))
})
})
.unwrap_or_else(|| {
#[cfg(feature = "log")]
log::warn!(
"Failed to find replacement characters {PRIMARY_REPLACEMENT_CHAR:?} or {FALLBACK_REPLACEMENT_CHAR:?}. Will use empty glyph."
);
log::warn!("Failed to find replacement characters. Will use empty glyph.");
(0, GlyphInfo::default())
});
slf.replacement_glyph = replacement_glyph;

slf
}
Expand Down Expand Up @@ -434,7 +440,7 @@ impl Font {

/// Can we display this glyph?
pub fn has_glyph(&mut self, c: char) -> bool {
self.glyph_info(c) != self.replacement_glyph // TODO(emilk): this is a false negative if the user asks about the replacement character itself 🤦‍♂️
self.glyph_info_no_cache_or_fallback(c).is_some()
}

/// Can we display all the glyphs in this text?
Expand Down
5 changes: 5 additions & 0 deletions crates/epaint/src/text/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ pub struct FontTweak {
/// A positive value shifts the text downwards.
/// A negative value shifts it upwards.
pub baseline_offset_factor: f32,

/// Fallback characters for this font
/// The default value is handled to be either the square character, or the question mark.
pub fallbacks: Option<[char; 2]>,
}

impl Default for FontTweak {
Expand All @@ -183,6 +187,7 @@ impl Default for FontTweak {
y_offset_factor: 0.0,
y_offset: 0.0,
baseline_offset_factor: -0.0333, // makes the default fonts look more centered in buttons and such
fallbacks: None,
}
}
}
Expand Down

0 comments on commit 4874b90

Please sign in to comment.