diff --git a/examples/editor-libcosmic/src/text.rs b/examples/editor-libcosmic/src/text.rs index 0392842..f7ff7cc 100644 --- a/examples/editor-libcosmic/src/text.rs +++ b/examples/editor-libcosmic/src/text.rs @@ -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, 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>, + cache: Mutex, } 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()); diff --git a/examples/editor-libcosmic/src/text_box.rs b/examples/editor-libcosmic/src/text_box.rs index d10081e..e29c172 100644 --- a/examples/editor-libcosmic/src/text_box.rs +++ b/examples/editor-libcosmic/src/text_box.rs @@ -348,7 +348,7 @@ where pub struct State { is_dragging: bool, - cache: Mutex>, + cache: Mutex, } 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()), } } } diff --git a/examples/editor-orbclient/src/main.rs b/examples/editor-orbclient/src/main.rs index d203d97..ca70d5f 100644 --- a/examples/editor-orbclient/src/main.rs +++ b/examples/editor-orbclient/src/main.rs @@ -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; diff --git a/examples/editor-test/src/main.rs b/examples/editor-test/src/main.rs index 2144240..0499356 100644 --- a/examples/editor-test/src/main.rs +++ b/examples/editor-test/src/main.rs @@ -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") diff --git a/examples/rich-text/src/main.rs b/examples/rich-text/src/main.rs index bfafaeb..fa5132c 100644 --- a/examples/rich-text/src/main.rs +++ b/examples/rich-text/src/main.rs @@ -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; diff --git a/examples/terminal/src/main.rs b/examples/terminal/src/main.rs index bf6a610..b35ea58 100644 --- a/examples/terminal/src/main.rs +++ b/examples/terminal/src/main.rs @@ -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); diff --git a/src/buffer.rs b/src/buffer.rs index ac1729b..1b4b800 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -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); }); } diff --git a/src/edit/editor.rs b/src/edit/editor.rs index 189aefb..4fbab85 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -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); + }, + ); } } } diff --git a/src/lib.rs b/src/lib.rs index 06ff8f9..1f82538 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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); diff --git a/src/swash.rs b/src/swash.rs index 5e84354..f951c3e 100644 --- a/src/swash.rs +++ b/src/swash.rs @@ -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>, pub outline_command_cache: Map>>, } -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 { - swash_image(self.font_system, &mut self.context, cache_key) + pub fn get_image_uncached( + &mut self, + font_system: &FontSystem, + cache_key: CacheKey, + ) -> Option { + 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 { + pub fn get_image( + &mut self, + font_system: &FontSystem, + cache_key: CacheKey, + ) -> &Option { 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( &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;