Merge pull request #87 from geieredgar/swash-cache

Remove lifetime from `SwashCache`
This commit is contained in:
Jeremy Soller 2023-03-02 11:24:36 -07:00 committed by GitHub
commit 9158cb83d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 53 additions and 32 deletions

View file

@ -14,6 +14,8 @@ use cosmic::{
use cosmic_text::{Attrs, AttrsList, BufferLine, Metrics, SwashCache};
use std::{cmp, sync::Mutex, time::Instant};
use crate::FONT_SYSTEM;
pub struct Appearance {
background_color: Option<Color>,
text_color: Color,
@ -183,11 +185,16 @@ where
None => text_color,
};
cache.with_pixels(cache_key, glyph_color, |pixel_x, pixel_y, color| {
let x = x_int + pixel_x;
let y = line_y as i32 + y_int + pixel_y;
draw_pixel(&mut pixels, layout_w as i32, layout_h as i32, x, y, color);
});
cache.with_pixels(
&FONT_SYSTEM,
cache_key,
glyph_color,
|pixel_x, pixel_y, color| {
let x = x_int + pixel_x;
let y = line_y as i32 + y_int + pixel_y;
draw_pixel(&mut pixels, layout_w as i32, layout_h as i32, x, y, color);
},
);
}
line_y += self.metrics.line_height;
}
@ -259,7 +266,7 @@ where
}
pub struct State {
cache: Mutex<SwashCache<'static>>,
cache: Mutex<SwashCache>,
}
impl State {
@ -268,7 +275,7 @@ impl State {
let instant = Instant::now();
let state = State {
cache: Mutex::new(SwashCache::new(&crate::FONT_SYSTEM)),
cache: Mutex::new(SwashCache::new()),
};
log::debug!("created state in {:?}", instant.elapsed());

View file

@ -348,7 +348,7 @@ where
pub struct State {
is_dragging: bool,
cache: Mutex<SwashCache<'static>>,
cache: Mutex<SwashCache>,
}
impl State {
@ -356,7 +356,7 @@ impl State {
pub fn new() -> State {
State {
is_dragging: false,
cache: Mutex::new(SwashCache::new(&crate::FONT_SYSTEM)),
cache: Mutex::new(SwashCache::new()),
}
}
}

View file

@ -79,7 +79,7 @@ fn main() {
}
}
let mut swash_cache = SwashCache::new(&font_system);
let mut swash_cache = SwashCache::new();
let mut ctrl_pressed = false;
let mut mouse_x = -1;

View file

@ -59,7 +59,7 @@ fn main() {
let mut editor = Editor::new(buffer);
let mut swash_cache = SwashCache::new(&font_system);
let mut swash_cache = SwashCache::new();
let text = if let Some(arg) = env::args().nth(1) {
fs::read_to_string(&arg).expect("failed to open file")

View file

@ -144,7 +144,7 @@ fn main() {
.push(BufferLine::new(line_text, attrs_list));
}
let mut swash_cache = SwashCache::new(&font_system);
let mut swash_cache = SwashCache::new();
//TODO: make window not async?
let mut mouse_x = -1;

View file

@ -9,7 +9,7 @@ fn main() {
let font_system = FontSystem::new();
// A SwashCache stores rasterized glyphs, create one per application
let mut swash_cache = SwashCache::new(&font_system);
let mut swash_cache = SwashCache::new();
// Text metrics indicate the font size and line height of a buffer
let metrics = Metrics::new(14.0, 20.0);

View file

@ -705,7 +705,7 @@ impl<'a> Buffer<'a> {
None => color,
};
cache.with_pixels(cache_key, glyph_color, |x, y, color| {
cache.with_pixels(self.font_system, cache_key, glyph_color, |x, y, color| {
f(x_int + x, run.line_y as i32 + y_int + y, 1, 1, color);
});
}

View file

@ -826,9 +826,14 @@ impl<'a> Edit<'a> for Editor<'a> {
None => color,
};
cache.with_pixels(cache_key, glyph_color, |x, y, color| {
f(x_int + x, line_y as i32 + y_int + y, 1, 1, color);
});
cache.with_pixels(
self.buffer.font_system(),
cache_key,
glyph_color,
|x, y, color| {
f(x_int + x, line_y as i32 + y_int + y, 1, 1, color);
},
);
}
}
}

View file

@ -18,7 +18,7 @@
//! let font_system = FontSystem::new();
//!
//! // A SwashCache stores rasterized glyphs, create one per application
//! let mut swash_cache = SwashCache::new(&font_system);
//! let mut swash_cache = SwashCache::new();
//!
//! // Text metrics indicate the font size and line height of a buffer
//! let metrics = Metrics::new(14.0, 20.0);

View file

@ -90,18 +90,16 @@ fn swash_outline_commands(
}
/// Cache for rasterizing with the swash scaler
pub struct SwashCache<'a> {
font_system: &'a FontSystem,
pub struct SwashCache {
context: ScaleContext,
pub image_cache: Map<CacheKey, Option<SwashImage>>,
pub outline_command_cache: Map<CacheKey, Option<Vec<swash::zeno::Command>>>,
}
impl<'a> SwashCache<'a> {
impl SwashCache {
/// Create a new swash cache
pub fn new(font_system: &'a FontSystem) -> Self {
pub fn new() -> Self {
Self {
font_system,
context: ScaleContext::new(),
image_cache: Map::new(),
outline_command_cache: Map::new(),
@ -109,34 +107,45 @@ impl<'a> SwashCache<'a> {
}
/// Create a swash Image from a cache key, without caching results
pub fn get_image_uncached(&mut self, cache_key: CacheKey) -> Option<SwashImage> {
swash_image(self.font_system, &mut self.context, cache_key)
pub fn get_image_uncached(
&mut self,
font_system: &FontSystem,
cache_key: CacheKey,
) -> Option<SwashImage> {
swash_image(font_system, &mut self.context, cache_key)
}
/// Create a swash Image from a cache key, caching results
pub fn get_image(&mut self, cache_key: CacheKey) -> &Option<SwashImage> {
pub fn get_image(
&mut self,
font_system: &FontSystem,
cache_key: CacheKey,
) -> &Option<SwashImage> {
self.image_cache
.entry(cache_key)
.or_insert_with(|| swash_image(self.font_system, &mut self.context, cache_key))
.or_insert_with(|| swash_image(font_system, &mut self.context, cache_key))
}
pub fn get_outline_commands(&mut self, cache_key: CacheKey) -> Option<&[swash::zeno::Command]> {
pub fn get_outline_commands(
&mut self,
font_system: &FontSystem,
cache_key: CacheKey,
) -> Option<&[swash::zeno::Command]> {
self.outline_command_cache
.entry(cache_key)
.or_insert_with(|| {
swash_outline_commands(self.font_system, &mut self.context, cache_key)
})
.or_insert_with(|| swash_outline_commands(font_system, &mut self.context, cache_key))
.as_deref()
}
/// Enumerate pixels in an Image, use `with_image` for better performance
pub fn with_pixels<F: FnMut(i32, i32, Color)>(
&mut self,
font_system: &FontSystem,
cache_key: CacheKey,
base: Color,
mut f: F,
) {
if let Some(image) = self.get_image(cache_key) {
if let Some(image) = self.get_image(font_system, cache_key) {
let x = image.placement.left;
let y = -image.placement.top;