diff --git a/examples/editor-libcosmic/src/main.rs b/examples/editor-libcosmic/src/main.rs index 0a10124..a28f826 100644 --- a/examples/editor-libcosmic/src/main.rs +++ b/examples/editor-libcosmic/src/main.rs @@ -23,7 +23,7 @@ use self::text_box::text_box; mod text_box; lazy_static::lazy_static! { - static ref FONT_SYSTEM: FontSystem = FontSystem::new(); + static ref FONT_SYSTEM: Mutex = Mutex::new(FontSystem::new()); static ref SYNTAX_SYSTEM: SyntaxSystem = SyntaxSystem::new(); } @@ -112,7 +112,8 @@ pub enum Message { impl Window { pub fn open(&mut self, path: PathBuf) { let mut editor = self.editor.lock().unwrap(); - let mut editor = editor.borrow_with(&FONT_SYSTEM); + let mut font_system = FONT_SYSTEM.lock().unwrap(); + let mut editor = editor.borrow_with(&mut font_system); match editor.load_text(&path, self.attrs) { Ok(()) => { log::info!("opened '{}'", path.display()); @@ -138,7 +139,7 @@ impl Application for Window { .family(cosmic_text::Family::Monospace); let mut editor = SyntaxEditor::new( - Buffer::new(&FONT_SYSTEM, FontSize::Body.to_metrics()), + Buffer::new(&FONT_SYSTEM.lock().unwrap(), FontSize::Body.to_metrics()), &SYNTAX_SYSTEM, "base16-eighties.dark", ) @@ -170,11 +171,11 @@ impl Application for Window { if let Some(path) = &self.path_opt { format!( "COSMIC Text - {} - {}", - FONT_SYSTEM.locale(), + FONT_SYSTEM.lock().unwrap().locale(), path.display() ) } else { - format!("COSMIC Text - {}", FONT_SYSTEM.locale()) + format!("COSMIC Text - {}", FONT_SYSTEM.lock().unwrap().locale()) } } @@ -240,13 +241,16 @@ impl Application for Window { self.font_size = font_size; let mut editor = self.editor.lock().unwrap(); editor - .borrow_with(&FONT_SYSTEM) + .borrow_with(&mut FONT_SYSTEM.lock().unwrap()) .buffer_mut() .set_metrics(font_size.to_metrics()); } Message::WrapChanged(wrap) => { let mut editor = self.editor.lock().unwrap(); - editor.borrow_with(&FONT_SYSTEM).buffer_mut().set_wrap(wrap); + editor + .borrow_with(&mut FONT_SYSTEM.lock().unwrap()) + .buffer_mut() + .set_wrap(wrap); } Message::AlignmentChanged(align) => { let mut editor = self.editor.lock().unwrap(); diff --git a/examples/editor-libcosmic/src/text.rs b/examples/editor-libcosmic/src/text.rs index 2ffc3af..76c1cea 100644 --- a/examples/editor-libcosmic/src/text.rs +++ b/examples/editor-libcosmic/src/text.rs @@ -53,7 +53,7 @@ impl Text { let mut line = BufferLine::new(string, AttrsList::new(Attrs::new())); //TODO: do we have to immediately shape? - line.shape(&crate::FONT_SYSTEM); + line.shape(&FONT_SYSTEM.lock().unwrap()); let text = Self { line, @@ -186,7 +186,7 @@ where }; cache.with_pixels( - &FONT_SYSTEM, + &FONT_SYSTEM.lock().unwrap(), cache_key, glyph_color, |pixel_x, pixel_y, color| { diff --git a/examples/editor-libcosmic/src/text_box.rs b/examples/editor-libcosmic/src/text_box.rs index f98ccfd..a5463e6 100644 --- a/examples/editor-libcosmic/src/text_box.rs +++ b/examples/editor-libcosmic/src/text_box.rs @@ -96,7 +96,7 @@ where //TODO: allow lazy shape let mut editor = self.editor.lock().unwrap(); editor - .borrow_with(&FONT_SYSTEM) + .borrow_with(&mut FONT_SYSTEM.lock().unwrap()) .buffer_mut() .shape_until(i32::max_value()); @@ -167,7 +167,9 @@ where let view_w = viewport.width.min(layout.bounds().width) - self.padding.horizontal() as f32; let view_h = viewport.height.min(layout.bounds().height) - self.padding.vertical() as f32; - let mut editor = editor.borrow_with(&FONT_SYSTEM); + + let mut font_system = FONT_SYSTEM.lock().unwrap(); + let mut editor = editor.borrow_with(&mut font_system); editor.buffer_mut().set_size(view_w, view_h); @@ -239,7 +241,8 @@ where ) -> Status { let state = tree.state.downcast_mut::(); let mut editor = self.editor.lock().unwrap(); - let mut editor = editor.borrow_with(&FONT_SYSTEM); + let mut font_system = FONT_SYSTEM.lock().unwrap(); + let mut editor = editor.borrow_with(&mut font_system); let mut status = Status::Ignored; match event { diff --git a/examples/editor-orbclient/src/main.rs b/examples/editor-orbclient/src/main.rs index b056c74..396be80 100644 --- a/examples/editor-orbclient/src/main.rs +++ b/examples/editor-orbclient/src/main.rs @@ -40,7 +40,7 @@ fn main() { ) .unwrap(); - let font_system = FontSystem::new(); + let mut font_system = FontSystem::new(); let syntax_system = SyntaxSystem::new(); @@ -67,7 +67,7 @@ fn main() { #[cfg(feature = "vi")] let mut editor = cosmic_text::ViEditor::new(editor); - let mut editor = editor.borrow_with(&font_system); + let mut editor = editor.borrow_with(&mut font_system); editor .buffer_mut() diff --git a/examples/editor-test/src/main.rs b/examples/editor-test/src/main.rs index 0f300a4..69a5498 100644 --- a/examples/editor-test/src/main.rs +++ b/examples/editor-test/src/main.rs @@ -1,21 +1,20 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 -use cosmic_text::{Action, Buffer, Color, Edit, Editor, FontSystem, Metrics, SwashCache}; +use cosmic_text::{ + Action, BorrowedWithFontSystem, Buffer, Color, Edit, Editor, FontSystem, Metrics, SwashCache, +}; use orbclient::{EventOption, Renderer, Window, WindowFlag}; use std::{env, fs, process, time::Instant}; use unicode_segmentation::UnicodeSegmentation; fn redraw( window: &mut Window, - editor: &mut Editor, - font_system: &FontSystem, + editor: &mut BorrowedWithFontSystem, swash_cache: &mut SwashCache, ) { let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34); let font_color = Color::rgb(0xFF, 0xFF, 0xFF); - let mut editor = editor.borrow_with(font_system); - editor.shape_as_needed(); if editor.buffer().redraw() { let instant = Instant::now(); @@ -39,7 +38,7 @@ fn main() { env_logger::init(); let display_scale = 1.0; - let font_system = FontSystem::new(); + let mut font_system = FontSystem::new(); let mut window = Window::new_flags( -1, @@ -63,12 +62,12 @@ fn main() { let mut buffer = Buffer::new(&font_system, font_sizes[font_size_default]); buffer - .borrow_with(&font_system) + .borrow_with(&mut font_system) .set_size(window.width() as f32, window.height() as f32); let mut editor = Editor::new(buffer); - let mut editor = editor.borrow_with(&font_system); + let mut editor = editor.borrow_with(&mut font_system); let mut swash_cache = SwashCache::new(); @@ -139,7 +138,7 @@ fn main() { // Finally, normal enter editor.action(Action::Enter); - redraw(&mut window, &mut editor, &font_system, &mut swash_cache); + redraw(&mut window, &mut editor, &mut swash_cache); for event in window.events() { if let EventOption::Quit(_) = event.to_option() { diff --git a/examples/rich-text/src/main.rs b/examples/rich-text/src/main.rs index a3e6be8..e8e8213 100644 --- a/examples/rich-text/src/main.rs +++ b/examples/rich-text/src/main.rs @@ -13,7 +13,7 @@ use std::{ fn main() { env_logger::init(); - let font_system = FontSystem::new(); + let mut font_system = FontSystem::new(); let display_scale = match orbclient::get_display_size() { Ok((w, h)) => { @@ -41,7 +41,7 @@ fn main() { Metrics::new(32.0, 44.0).scale(display_scale), )); - let mut editor = editor.borrow_with(&font_system); + let mut editor = editor.borrow_with(&mut font_system); editor .buffer_mut() diff --git a/examples/terminal/src/main.rs b/examples/terminal/src/main.rs index 2069ba7..eae3ffe 100644 --- a/examples/terminal/src/main.rs +++ b/examples/terminal/src/main.rs @@ -6,7 +6,7 @@ use termion::{color, cursor}; fn main() { // A FontSystem provides access to detected system fonts, create one per application - let font_system = FontSystem::new(); + let mut font_system = FontSystem::new(); // A SwashCache stores rasterized glyphs, create one per application let mut swash_cache = SwashCache::new(); @@ -17,7 +17,7 @@ fn main() { // A Buffer provides shaping and layout for a UTF-8 string, create one per text widget let mut buffer = Buffer::new(&font_system, metrics); - let mut buffer = buffer.borrow_with(&font_system); + let mut buffer = buffer.borrow_with(&mut font_system); // Set a size for the text buffer, in pixels let width = 80u16; diff --git a/src/buffer.rs b/src/buffer.rs index 4dbef2a..937a60b 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -335,7 +335,7 @@ impl Buffer { pub fn borrow_with<'a>( &'a mut self, - font_system: &'a FontSystem, + font_system: &'a mut FontSystem, ) -> BorrowedWithFontSystem<'a, Buffer> { BorrowedWithFontSystem { inner: self, diff --git a/src/edit/mod.rs b/src/edit/mod.rs index 06a06db..b7ddee8 100644 --- a/src/edit/mod.rs +++ b/src/edit/mod.rs @@ -81,7 +81,7 @@ pub enum Action { pub trait Edit { fn borrow_with<'a>( &'a mut self, - font_system: &'a FontSystem, + font_system: &'a mut FontSystem, ) -> BorrowedWithFontSystem<'a, Self> where Self: Sized, diff --git a/src/font/system/mod.rs b/src/font/system/mod.rs index e508e1f..2a87474 100644 --- a/src/font/system/mod.rs +++ b/src/font/system/mod.rs @@ -15,7 +15,7 @@ pub use fontdb; pub struct BorrowedWithFontSystem<'a, T> { pub(crate) inner: &'a mut T, - pub(crate) font_system: &'a FontSystem, + pub(crate) font_system: &'a mut FontSystem, } impl<'a, T> Deref for BorrowedWithFontSystem<'a, T> { diff --git a/src/shape.rs b/src/shape.rs index cb6039c..4cc0346 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -35,7 +35,7 @@ fn shape_fallback( let rtl = matches!(buffer.direction(), rustybuzz::Direction::RightToLeft); assert_eq!(rtl, span_rtl); - let glyph_buffer = rustybuzz::shape(&font.rustybuzz(), &[], buffer); + let glyph_buffer = rustybuzz::shape(font.rustybuzz(), &[], buffer); let glyph_infos = glyph_buffer.glyph_infos(); let glyph_positions = glyph_buffer.glyph_positions();