Skip to content

Commit

Permalink
cache the monospace fallbacks buffer in FontSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
UkoeHB committed Sep 2, 2024
1 parent 0935f54 commit 6c7ced3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/font/fallback/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use alloc::collections::BTreeSet;
use alloc::sync::Arc;
use alloc::vec::Vec;
use fontdb::Family;
Expand Down Expand Up @@ -35,7 +34,7 @@ use log::warn as missing_warn;
// 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 {
pub(crate) struct MonospaceFallbackInfo {
font_weight_diff: Option<u16>,
codepoint_non_matches: Option<usize>,
font_weight: u16,
Expand All @@ -46,7 +45,6 @@ pub struct FontFallbackIter<'a> {
font_system: &'a mut FontSystem,
font_match_keys: &'a [FontMatchKey],
default_families: &'a [&'a Family<'a>],
monospace_fallbacks: BTreeSet<MonospaceFallbackInfo>,
default_i: usize,
scripts: &'a [Script],
word: &'a str,
Expand All @@ -64,11 +62,11 @@ impl<'a> FontFallbackIter<'a> {
scripts: &'a [Script],
word: &'a str,
) -> Self {
font_system.monospace_fallbacks_buffer.clear();
Self {
font_system,
font_match_keys,
default_families,
monospace_fallbacks: BTreeSet::new(),
default_i: 0,
scripts,
word,
Expand Down Expand Up @@ -145,7 +143,7 @@ impl<'a> FontFallbackIter<'a> {
impl<'a> Iterator for FontFallbackIter<'a> {
type Item = Arc<Font>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(fallback_info) = self.monospace_fallbacks.pop_first() {
if let Some(fallback_info) = self.font_system.monospace_fallbacks_buffer.pop_first() {
if let Some(font) = self.font_system.get_font(fallback_info.id) {
return Some(font);
}
Expand Down Expand Up @@ -204,7 +202,10 @@ impl<'a> Iterator for FontFallbackIter<'a> {
return Some(font);
}
} else {
assert!(self.monospace_fallbacks.insert(fallback_info));
assert!(self
.font_system
.monospace_fallbacks_buffer
.insert(fallback_info));
}
}
}
Expand Down Expand Up @@ -242,13 +243,16 @@ impl<'a> Iterator for FontFallbackIter<'a> {
font_weight: m_key.font_weight,
id: m_key.id,
};
assert!(self.monospace_fallbacks.insert(fallback_info));
assert!(self
.font_system
.monospace_fallbacks_buffer
.insert(fallback_info));
}
}
}
}
// If default family is Monospace fallback to first monospaced font
if let Some(fallback_info) = self.monospace_fallbacks.pop_first() {
if let Some(fallback_info) = self.font_system.monospace_fallbacks_buffer.pop_first() {
if let Some(font) = self.font_system.get_font(fallback_info.id) {
return Some(font);
}
Expand Down
7 changes: 7 additions & 0 deletions src/font/system.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{Attrs, Font, FontMatchAttrs, HashMap, ShapePlanCache};
use alloc::collections::BTreeSet;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
Expand All @@ -9,6 +10,8 @@ use core::ops::{Deref, DerefMut};
pub use fontdb;
pub use rustybuzz;

use super::fallback::MonospaceFallbackInfo;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FontMatchKey {
pub(crate) font_weight_diff: u16,
Expand Down Expand Up @@ -103,6 +106,9 @@ pub struct FontSystem {
/// Cache for rustybuzz shape plans.
shape_plan_cache: ShapePlanCache,

/// Buffer for use in FontFallbackIter.
pub(crate) monospace_fallbacks_buffer: BTreeSet<MonospaceFallbackInfo>,

/// Cache for shaped runs
#[cfg(feature = "shape-run-cache")]
pub shape_run_cache: crate::ShapeRunCache,
Expand Down Expand Up @@ -169,6 +175,7 @@ impl FontSystem {
font_matches_cache: Default::default(),
font_codepoint_support_info_cache: Default::default(),
shape_plan_cache: ShapePlanCache::default(),
monospace_fallbacks_buffer: BTreeSet::default(),
#[cfg(feature = "shape-run-cache")]
shape_run_cache: crate::ShapeRunCache::default(),
};
Expand Down

0 comments on commit 6c7ced3

Please sign in to comment.