Use min_bounds for cached text

This commit is contained in:
Héctor Ramón Jiménez 2023-08-30 05:06:08 +02:00
parent ed3454301e
commit 89acf0217e
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
3 changed files with 27 additions and 15 deletions

View file

@ -8,7 +8,7 @@ use std::hash::{BuildHasher, Hash, Hasher};
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
#[derive(Default)] #[derive(Default)]
pub struct Cache { pub struct Cache {
entries: FxHashMap<KeyHash, cosmic_text::Buffer>, entries: FxHashMap<KeyHash, Entry>,
aliases: FxHashMap<KeyHash, KeyHash>, aliases: FxHashMap<KeyHash, KeyHash>,
recently_used: FxHashSet<KeyHash>, recently_used: FxHashSet<KeyHash>,
hasher: HashBuilder, hasher: HashBuilder,
@ -25,7 +25,7 @@ impl Cache {
Self::default() Self::default()
} }
pub fn get(&self, key: &KeyHash) -> Option<&cosmic_text::Buffer> { pub fn get(&self, key: &KeyHash) -> Option<&Entry> {
self.entries.get(key) self.entries.get(key)
} }
@ -33,7 +33,7 @@ impl Cache {
&mut self, &mut self,
font_system: &mut cosmic_text::FontSystem, font_system: &mut cosmic_text::FontSystem,
key: Key<'_>, key: Key<'_>,
) -> (KeyHash, &mut cosmic_text::Buffer) { ) -> (KeyHash, &mut Entry) {
let hash = key.hash(self.hasher.build_hasher()); let hash = key.hash(self.hasher.build_hasher());
if let Some(hash) = self.aliases.get(&hash) { if let Some(hash) = self.aliases.get(&hash) {
@ -59,7 +59,10 @@ impl Cache {
); );
let bounds = text::measure(&buffer); let bounds = text::measure(&buffer);
let _ = entry.insert(buffer); let _ = entry.insert(Entry {
buffer,
min_bounds: bounds,
});
for bounds in [ for bounds in [
bounds, bounds,
@ -118,3 +121,9 @@ impl Key<'_> {
} }
pub type KeyHash = u64; pub type KeyHash = u64;
#[allow(missing_debug_implementations)]
pub struct Entry {
pub buffer: cosmic_text::Buffer,
pub min_bounds: Size,
}

View file

@ -77,10 +77,10 @@ impl Pipeline {
shaping, shaping,
}; };
let (_, buffer) = self.cache.get_mut().allocate(font_system, key); let (_, entry) = self.cache.get_mut().allocate(font_system, key);
let max_width = bounds.width * scale_factor; let max_width = entry.min_bounds.width * scale_factor;
let total_height = bounds.height * scale_factor; let total_height = entry.min_bounds.height * scale_factor;
let bounds = bounds * scale_factor; let bounds = bounds * scale_factor;
@ -98,7 +98,7 @@ impl Pipeline {
let mut swash = cosmic_text::SwashCache::new(); let mut swash = cosmic_text::SwashCache::new();
for run in buffer.layout_runs() { for run in entry.buffer.layout_runs() {
for glyph in run.glyphs { for glyph in run.glyphs {
let physical_glyph = glyph.physical((x, y), scale_factor); let physical_glyph = glyph.physical((x, y), scale_factor);

View file

@ -59,7 +59,7 @@ impl Pipeline {
device: &wgpu::Device, device: &wgpu::Device,
queue: &wgpu::Queue, queue: &wgpu::Queue,
sections: &[Text<'_>], sections: &[Text<'_>],
bounds: Rectangle, layer_bounds: Rectangle,
scale_factor: f32, scale_factor: f32,
target_size: Size<u32>, target_size: Size<u32>,
) { ) {
@ -98,8 +98,8 @@ impl Pipeline {
), ),
font: text.font, font: text.font,
bounds: Size { bounds: Size {
width: bounds.width, width: text.bounds.width,
height: bounds.height, height: text.bounds.height,
}, },
shaping: text.shaping, shaping: text.shaping,
}, },
@ -110,7 +110,7 @@ impl Pipeline {
}) })
.collect(); .collect();
let layer_bounds = bounds * scale_factor; let layer_bounds = layer_bounds * scale_factor;
let text_areas = sections.iter().zip(allocations.iter()).filter_map( let text_areas = sections.iter().zip(allocations.iter()).filter_map(
|(section, allocation)| { |(section, allocation)| {
@ -144,11 +144,14 @@ impl Pipeline {
return None; return None;
}; };
let buffer = cache.get(key).expect("Get cached buffer"); let entry = cache.get(key).expect("Get cached buffer");
( (
buffer, &entry.buffer,
text.bounds, Rectangle::new(
text.bounds.position(),
entry.min_bounds,
),
text.horizontal_alignment, text.horizontal_alignment,
text.vertical_alignment, text.vertical_alignment,
text.color, text.color,