Remove lifetime from SwashCache

This commit is contained in:
Edgar Geier 2023-03-01 22:41:59 +01:00
parent 2b991129e3
commit 4e93853765
No known key found for this signature in database
GPG key ID: 7A65B51FD6B75EF5
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 cosmic_text::{Attrs, AttrsList, BufferLine, Metrics, SwashCache};
use std::{cmp, sync::Mutex, time::Instant}; use std::{cmp, sync::Mutex, time::Instant};
use crate::FONT_SYSTEM;
pub struct Appearance { pub struct Appearance {
background_color: Option<Color>, background_color: Option<Color>,
text_color: Color, text_color: Color,
@ -183,11 +185,16 @@ where
None => text_color, None => text_color,
}; };
cache.with_pixels(cache_key, glyph_color, |pixel_x, pixel_y, color| { cache.with_pixels(
let x = x_int + pixel_x; &FONT_SYSTEM,
let y = line_y as i32 + y_int + pixel_y; cache_key,
draw_pixel(&mut pixels, layout_w as i32, layout_h as i32, x, y, color); 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; line_y += self.metrics.line_height;
} }
@ -259,7 +266,7 @@ where
} }
pub struct State { pub struct State {
cache: Mutex<SwashCache<'static>>, cache: Mutex<SwashCache>,
} }
impl State { impl State {
@ -268,7 +275,7 @@ impl State {
let instant = Instant::now(); let instant = Instant::now();
let state = State { let state = State {
cache: Mutex::new(SwashCache::new(&crate::FONT_SYSTEM)), cache: Mutex::new(SwashCache::new()),
}; };
log::debug!("created state in {:?}", instant.elapsed()); log::debug!("created state in {:?}", instant.elapsed());

View file

@ -348,7 +348,7 @@ where
pub struct State { pub struct State {
is_dragging: bool, is_dragging: bool,
cache: Mutex<SwashCache<'static>>, cache: Mutex<SwashCache>,
} }
impl State { impl State {
@ -356,7 +356,7 @@ impl State {
pub fn new() -> State { pub fn new() -> State {
State { State {
is_dragging: false, 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 ctrl_pressed = false;
let mut mouse_x = -1; let mut mouse_x = -1;

View file

@ -59,7 +59,7 @@ fn main() {
let mut editor = Editor::new(buffer); 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) { let text = if let Some(arg) = env::args().nth(1) {
fs::read_to_string(&arg).expect("failed to open file") 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)); .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? //TODO: make window not async?
let mut mouse_x = -1; let mut mouse_x = -1;

View file

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

View file

@ -705,7 +705,7 @@ impl<'a> Buffer<'a> {
None => color, 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); 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, None => color,
}; };
cache.with_pixels(cache_key, glyph_color, |x, y, color| { cache.with_pixels(
f(x_int + x, line_y as i32 + y_int + y, 1, 1, color); 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(); //! let font_system = FontSystem::new();
//! //!
//! // A SwashCache stores rasterized glyphs, create one per application //! // 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 //! // Text metrics indicate the font size and line height of a buffer
//! let metrics = Metrics::new(14.0, 20.0); //! 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 /// Cache for rasterizing with the swash scaler
pub struct SwashCache<'a> { pub struct SwashCache {
font_system: &'a FontSystem,
context: ScaleContext, context: ScaleContext,
pub image_cache: Map<CacheKey, Option<SwashImage>>, pub image_cache: Map<CacheKey, Option<SwashImage>>,
pub outline_command_cache: Map<CacheKey, Option<Vec<swash::zeno::Command>>>, pub outline_command_cache: Map<CacheKey, Option<Vec<swash::zeno::Command>>>,
} }
impl<'a> SwashCache<'a> { impl SwashCache {
/// Create a new swash cache /// Create a new swash cache
pub fn new(font_system: &'a FontSystem) -> Self { pub fn new() -> Self {
Self { Self {
font_system,
context: ScaleContext::new(), context: ScaleContext::new(),
image_cache: Map::new(), image_cache: Map::new(),
outline_command_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 /// Create a swash Image from a cache key, without caching results
pub fn get_image_uncached(&mut self, cache_key: CacheKey) -> Option<SwashImage> { pub fn get_image_uncached(
swash_image(self.font_system, &mut self.context, cache_key) &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 /// 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 self.image_cache
.entry(cache_key) .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 self.outline_command_cache
.entry(cache_key) .entry(cache_key)
.or_insert_with(|| { .or_insert_with(|| swash_outline_commands(font_system, &mut self.context, cache_key))
swash_outline_commands(self.font_system, &mut self.context, cache_key)
})
.as_deref() .as_deref()
} }
/// Enumerate pixels in an Image, use `with_image` for better performance /// Enumerate pixels in an Image, use `with_image` for better performance
pub fn with_pixels<F: FnMut(i32, i32, Color)>( pub fn with_pixels<F: FnMut(i32, i32, Color)>(
&mut self, &mut self,
font_system: &FontSystem,
cache_key: CacheKey, cache_key: CacheKey,
base: Color, base: Color,
mut f: F, 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 x = image.placement.left;
let y = -image.placement.top; let y = -image.placement.top;