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;
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();
}
@ -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();

View file

@ -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| {

View file

@ -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::<State>();
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 {

View file

@ -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()

View file

@ -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<Editor>,
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() {

View file

@ -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()

View file

@ -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;

View file

@ -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,

View file

@ -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,

View file

@ -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> {

View file

@ -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();