Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to improve support for .notdef glyphs through FontTweak #4644

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
})
})
Comment on lines +369 to +380
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not call glyph_info_no_cache_or_fallback here like before?

.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()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seemed a nice "bonus" to-do solving, slightly unrelated to the issue at hand; I'm suspecting there's a reason you chose not to do this in the first place?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, glyph_info_no_cache_or_fallback doesn't use the cache, so it is marginally slower.

}

/// 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
Loading