Make BorrowedWithFontSystem borrow FontSystem mutably

This commit is contained in:
Edgar Geier 2023-03-12 10:30:09 +01:00
parent 057b5b6fa9
commit 46e9ef0246
No known key found for this signature in database
GPG key ID: DE2B55319457EB56
11 changed files with 37 additions and 31 deletions

View file

@ -23,7 +23,7 @@ use self::text_box::text_box;
mod text_box; mod text_box;
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref FONT_SYSTEM: FontSystem = FontSystem::new(); static ref FONT_SYSTEM: Mutex<FontSystem> = Mutex::new(FontSystem::new());
static ref SYNTAX_SYSTEM: SyntaxSystem = SyntaxSystem::new(); static ref SYNTAX_SYSTEM: SyntaxSystem = SyntaxSystem::new();
} }
@ -112,7 +112,8 @@ pub enum Message {
impl Window { impl Window {
pub fn open(&mut self, path: PathBuf) { pub fn open(&mut self, path: PathBuf) {
let mut editor = self.editor.lock().unwrap(); 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) { match editor.load_text(&path, self.attrs) {
Ok(()) => { Ok(()) => {
log::info!("opened '{}'", path.display()); log::info!("opened '{}'", path.display());
@ -138,7 +139,7 @@ impl Application for Window {
.family(cosmic_text::Family::Monospace); .family(cosmic_text::Family::Monospace);
let mut editor = SyntaxEditor::new( 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, &SYNTAX_SYSTEM,
"base16-eighties.dark", "base16-eighties.dark",
) )
@ -170,11 +171,11 @@ impl Application for Window {
if let Some(path) = &self.path_opt { if let Some(path) = &self.path_opt {
format!( format!(
"COSMIC Text - {} - {}", "COSMIC Text - {} - {}",
FONT_SYSTEM.locale(), FONT_SYSTEM.lock().unwrap().locale(),
path.display() path.display()
) )
} else { } 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; self.font_size = font_size;
let mut editor = self.editor.lock().unwrap(); let mut editor = self.editor.lock().unwrap();
editor editor
.borrow_with(&FONT_SYSTEM) .borrow_with(&mut FONT_SYSTEM.lock().unwrap())
.buffer_mut() .buffer_mut()
.set_metrics(font_size.to_metrics()); .set_metrics(font_size.to_metrics());
} }
Message::WrapChanged(wrap) => { Message::WrapChanged(wrap) => {
let mut editor = self.editor.lock().unwrap(); 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) => { Message::AlignmentChanged(align) => {
let mut editor = self.editor.lock().unwrap(); let mut editor = self.editor.lock().unwrap();

View file

@ -53,7 +53,7 @@ impl Text {
let mut line = BufferLine::new(string, AttrsList::new(Attrs::new())); let mut line = BufferLine::new(string, AttrsList::new(Attrs::new()));
//TODO: do we have to immediately shape? //TODO: do we have to immediately shape?
line.shape(&crate::FONT_SYSTEM); line.shape(&FONT_SYSTEM.lock().unwrap());
let text = Self { let text = Self {
line, line,
@ -186,7 +186,7 @@ where
}; };
cache.with_pixels( cache.with_pixels(
&FONT_SYSTEM, &FONT_SYSTEM.lock().unwrap(),
cache_key, cache_key,
glyph_color, glyph_color,
|pixel_x, pixel_y, color| { |pixel_x, pixel_y, color| {

View file

@ -96,7 +96,7 @@ where
//TODO: allow lazy shape //TODO: allow lazy shape
let mut editor = self.editor.lock().unwrap(); let mut editor = self.editor.lock().unwrap();
editor editor
.borrow_with(&FONT_SYSTEM) .borrow_with(&mut FONT_SYSTEM.lock().unwrap())
.buffer_mut() .buffer_mut()
.shape_until(i32::max_value()); .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_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 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); editor.buffer_mut().set_size(view_w, view_h);
@ -239,7 +241,8 @@ where
) -> Status { ) -> Status {
let state = tree.state.downcast_mut::<State>(); let state = tree.state.downcast_mut::<State>();
let mut editor = self.editor.lock().unwrap(); 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; let mut status = Status::Ignored;
match event { match event {

View file

@ -40,7 +40,7 @@ fn main() {
) )
.unwrap(); .unwrap();
let font_system = FontSystem::new(); let mut font_system = FontSystem::new();
let syntax_system = SyntaxSystem::new(); let syntax_system = SyntaxSystem::new();
@ -67,7 +67,7 @@ fn main() {
#[cfg(feature = "vi")] #[cfg(feature = "vi")]
let mut editor = cosmic_text::ViEditor::new(editor); 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 editor
.buffer_mut() .buffer_mut()

View file

@ -1,21 +1,20 @@
// SPDX-License-Identifier: MIT OR Apache-2.0 // 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 orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{env, fs, process, time::Instant}; use std::{env, fs, process, time::Instant};
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
fn redraw( fn redraw(
window: &mut Window, window: &mut Window,
editor: &mut Editor, editor: &mut BorrowedWithFontSystem<Editor>,
font_system: &FontSystem,
swash_cache: &mut SwashCache, swash_cache: &mut SwashCache,
) { ) {
let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34); let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
let font_color = Color::rgb(0xFF, 0xFF, 0xFF); let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
let mut editor = editor.borrow_with(font_system);
editor.shape_as_needed(); editor.shape_as_needed();
if editor.buffer().redraw() { if editor.buffer().redraw() {
let instant = Instant::now(); let instant = Instant::now();
@ -39,7 +38,7 @@ fn main() {
env_logger::init(); env_logger::init();
let display_scale = 1.0; let display_scale = 1.0;
let font_system = FontSystem::new(); let mut font_system = FontSystem::new();
let mut window = Window::new_flags( let mut window = Window::new_flags(
-1, -1,
@ -63,12 +62,12 @@ fn main() {
let mut buffer = Buffer::new(&font_system, font_sizes[font_size_default]); let mut buffer = Buffer::new(&font_system, font_sizes[font_size_default]);
buffer buffer
.borrow_with(&font_system) .borrow_with(&mut font_system)
.set_size(window.width() as f32, window.height() as f32); .set_size(window.width() as f32, window.height() as f32);
let mut editor = Editor::new(buffer); 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(); let mut swash_cache = SwashCache::new();
@ -139,7 +138,7 @@ fn main() {
// Finally, normal enter // Finally, normal enter
editor.action(Action::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() { for event in window.events() {
if let EventOption::Quit(_) = event.to_option() { if let EventOption::Quit(_) = event.to_option() {

View file

@ -13,7 +13,7 @@ use std::{
fn main() { fn main() {
env_logger::init(); env_logger::init();
let font_system = FontSystem::new(); let mut font_system = FontSystem::new();
let display_scale = match orbclient::get_display_size() { let display_scale = match orbclient::get_display_size() {
Ok((w, h)) => { Ok((w, h)) => {
@ -41,7 +41,7 @@ fn main() {
Metrics::new(32.0, 44.0).scale(display_scale), 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 editor
.buffer_mut() .buffer_mut()

View file

@ -6,7 +6,7 @@ use termion::{color, cursor};
fn main() { fn main() {
// A FontSystem provides access to detected system fonts, create one per application // 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 // A SwashCache stores rasterized glyphs, create one per application
let mut swash_cache = SwashCache::new(); 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 // 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::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 // Set a size for the text buffer, in pixels
let width = 80u16; let width = 80u16;

View file

@ -335,7 +335,7 @@ impl Buffer {
pub fn borrow_with<'a>( pub fn borrow_with<'a>(
&'a mut self, &'a mut self,
font_system: &'a FontSystem, font_system: &'a mut FontSystem,
) -> BorrowedWithFontSystem<'a, Buffer> { ) -> BorrowedWithFontSystem<'a, Buffer> {
BorrowedWithFontSystem { BorrowedWithFontSystem {
inner: self, inner: self,

View file

@ -81,7 +81,7 @@ pub enum Action {
pub trait Edit { pub trait Edit {
fn borrow_with<'a>( fn borrow_with<'a>(
&'a mut self, &'a mut self,
font_system: &'a FontSystem, font_system: &'a mut FontSystem,
) -> BorrowedWithFontSystem<'a, Self> ) -> BorrowedWithFontSystem<'a, Self>
where where
Self: Sized, Self: Sized,

View file

@ -15,7 +15,7 @@ pub use fontdb;
pub struct BorrowedWithFontSystem<'a, T> { pub struct BorrowedWithFontSystem<'a, T> {
pub(crate) inner: &'a mut 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> { impl<'a, T> Deref for BorrowedWithFontSystem<'a, T> {

View file

@ -35,7 +35,7 @@ fn shape_fallback(
let rtl = matches!(buffer.direction(), rustybuzz::Direction::RightToLeft); let rtl = matches!(buffer.direction(), rustybuzz::Direction::RightToLeft);
assert_eq!(rtl, span_rtl); 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_infos = glyph_buffer.glyph_infos();
let glyph_positions = glyph_buffer.glyph_positions(); let glyph_positions = glyph_buffer.glyph_positions();