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

Use weight absolute difference in monospace fallback matching #224

Merged
merged 1 commit into from
Jan 31, 2024
Merged
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
5 changes: 1 addition & 4 deletions src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,7 @@ impl<'a> Attrs<'a> {
pub fn matches(&self, face: &fontdb::FaceInfo) -> bool {
//TODO: smarter way of including emoji
face.post_script_name.contains("Emoji")
|| (face.style == self.style
// Relax exact weight matching for the Monospace fallback use-case
&& face.weight <= self.weight
&& face.stretch == self.stretch)
|| (face.style == self.style && face.stretch == self.stretch)
}

/// Check if this set of attributes can be shaped with another
Expand Down
15 changes: 9 additions & 6 deletions src/font/fallback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ use log::debug as missing_warn;
#[cfg(feature = "warn_on_missing_glyphs")]
use log::warn as missing_warn;

// Match on lowest weight_offset, then script_non_matches
// Match on lowest font_weight_diff, then script_non_matches, then font_weight
// Default font gets None for both `weight_offset` and `script_non_matches`, and thus, it is
// always the first to be popped from the set.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct MonospaceFallbackInfo {
weight_offset: Option<u16>,
font_weight_diff: Option<u16>,
codepoint_non_matches: Option<usize>,
font_weight: u16,
id: fontdb::ID,
}

Expand Down Expand Up @@ -144,7 +145,7 @@ impl<'a> Iterator for FontFallbackIter<'a> {
let font_match_keys_iter = |is_mono| {
self.font_match_keys
.iter()
.filter(move |m_key| m_key.weight_offset == Some(0) || is_mono)
.filter(move |m_key| m_key.font_weight_diff == 0 || is_mono)
};

while self.default_i < self.default_families.len() {
Expand All @@ -160,11 +161,12 @@ impl<'a> Iterator for FontFallbackIter<'a> {
if let Some(font) = self.font_system.get_font(m_key.id) {
if !is_mono {
return Some(font);
} else if m_key.weight_offset == Some(0) {
} else if m_key.font_weight_diff == 0 {
// Default font
let fallback_info = MonospaceFallbackInfo {
weight_offset: None,
font_weight_diff: None,
codepoint_non_matches: None,
font_weight: m_key.font_weight,
id: m_key.id,
};
assert!(self.monospace_fallbacks.insert(fallback_info));
Expand All @@ -187,8 +189,9 @@ impl<'a> Iterator for FontFallbackIter<'a> {
.count();

let fallback_info = MonospaceFallbackInfo {
weight_offset: m_key.weight_offset,
font_weight_diff: Some(m_key.font_weight_diff),
codepoint_non_matches: Some(codepoint_non_matches),
font_weight: m_key.font_weight,
id: m_key.id,
};
assert!(self.monospace_fallbacks.insert(fallback_info));
Expand Down
6 changes: 4 additions & 2 deletions src/font/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub use rustybuzz;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FontMatchKey {
pub(crate) weight_offset: Option<u16>,
pub(crate) font_weight_diff: u16,
pub(crate) font_weight: u16,
pub(crate) id: fontdb::ID,
}

Expand Down Expand Up @@ -151,7 +152,8 @@ impl FontSystem {
.faces()
.filter(|face| attrs.matches(face))
.map(|face| FontMatchKey {
weight_offset: attrs.weight.0.checked_sub(face.weight.0),
font_weight_diff: attrs.weight.0.abs_diff(face.weight.0),
font_weight: face.weight.0,
id: face.id,
})
.collect::<Vec<_>>();
Expand Down
Loading