From 6aadfaddac7ae68c3f97c0b9b2fa75033374a650 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Thu, 18 Jan 2024 06:58:47 -0700 Subject: [PATCH] Fix subtraction overflow when comparing weights --- src/font/fallback/mod.rs | 6 +++--- src/font/system.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/font/fallback/mod.rs b/src/font/fallback/mod.rs index 10ed3bd727..d854624b0c 100644 --- a/src/font/fallback/mod.rs +++ b/src/font/fallback/mod.rs @@ -141,7 +141,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 == 0 || is_mono) + .filter(move |m_key| m_key.weight_offset == Some(0) || is_mono) }; while self.default_i < self.default_families.len() { @@ -157,7 +157,7 @@ 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 == 0 { + } else if m_key.weight_offset == Some(0) { // Default font let fallback_info = MonospaceFallbackInfo { weight_offset: None, @@ -194,7 +194,7 @@ impl<'a> Iterator for FontFallbackIter<'a> { .count(); let fallback_info = MonospaceFallbackInfo { - weight_offset: Some(m_key.weight_offset), + weight_offset: m_key.weight_offset, script_non_matches: Some(script_non_matches), id: m_key.id, }; diff --git a/src/font/system.rs b/src/font/system.rs index 7778cd754d..5ca2a00101 100644 --- a/src/font/system.rs +++ b/src/font/system.rs @@ -11,7 +11,7 @@ pub use rustybuzz; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct FontMatchKey { - pub(crate) weight_offset: u16, + pub(crate) weight_offset: Option, pub(crate) id: fontdb::ID, } @@ -144,7 +144,7 @@ impl FontSystem { .faces() .filter(|face| attrs.matches(face)) .map(|face| FontMatchKey { - weight_offset: attrs.weight.0 - face.weight.0, + weight_offset: attrs.weight.0.checked_sub(face.weight.0), id: face.id, }) .collect::>();