Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove lifetime from Buffer and Mutex from FontSystem #97

Merged
merged 9 commits into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository = "https://github.com/pop-os/cosmic-text"
fontdb = { version = "0.13.0", default-features = false }
libm = "0.2.6"
log = "0.4.17"
ouroboros = "0.15.5"
ouroboros = { version = "0.15.5", default-features = false }
rustybuzz = { version = "0.7.0", default-features = false, features = ["libm"] }
swash = { version = "0.1.6", optional = true }
syntect = { version = "5.0.0", optional = true }
Expand All @@ -35,6 +35,7 @@ no_std = [
std = [
"fontdb/memmap",
"fontdb/std",
"ouroboros/std",
"rustybuzz/std",
"sys-locale",
"unicode-bidi/std",
Expand Down
28 changes: 19 additions & 9 deletions examples/editor-libcosmic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -112,6 +112,8 @@ pub enum Message {
impl Window {
pub fn open(&mut self, path: PathBuf) {
let mut editor = self.editor.lock().unwrap();
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());
Expand All @@ -137,7 +139,10 @@ impl Application for Window {
.family(cosmic_text::Family::Monospace);

let mut editor = SyntaxEditor::new(
Buffer::new(&FONT_SYSTEM, FontSize::Body.to_metrics()),
Buffer::new(
&mut FONT_SYSTEM.lock().unwrap(),
FontSize::Body.to_metrics(),
),
&SYNTAX_SYSTEM,
"base16-eighties.dark",
)
Expand Down Expand Up @@ -169,11 +174,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())
}
}

Expand Down Expand Up @@ -237,13 +242,18 @@ impl Application for Window {
}
Message::FontSizeChanged(font_size) => {
self.font_size = font_size;

let mut editor = self.editor.lock().unwrap();
editor.buffer_mut().set_metrics(font_size.to_metrics());
editor
.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.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();
Expand Down Expand Up @@ -360,13 +370,13 @@ impl Application for Window {
}
}

fn update_attrs<'a, T: Edit<'a>>(editor: &mut T, attrs: Attrs<'a>) {
fn update_attrs<T: Edit>(editor: &mut T, attrs: Attrs) {
editor.buffer_mut().lines.iter_mut().for_each(|line| {
line.set_attrs_list(AttrsList::new(attrs));
});
}

fn update_alignment<'a, T: Edit<'a>>(editor: &mut T, align: Align) {
fn update_alignment<T: Edit>(editor: &mut T, align: Align) {
let current_line = editor.cursor().line;
if let Some(select) = editor.select_opt() {
let (start, end) = match select.line.cmp(&current_line) {
Expand Down
4 changes: 2 additions & 2 deletions examples/editor-libcosmic/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&mut FONT_SYSTEM.lock().unwrap());

let text = Self {
line,
Expand Down Expand Up @@ -186,7 +186,7 @@ where
};

cache.with_pixels(
&FONT_SYSTEM,
&mut FONT_SYSTEM.lock().unwrap(),
cache_key,
glyph_color,
|pixel_x, pixel_y, color| {
Expand Down
22 changes: 16 additions & 6 deletions examples/editor-libcosmic/src/text_box.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::FONT_SYSTEM;

use super::text;
use cosmic::{
iced_native::{
Expand Down Expand Up @@ -66,11 +68,11 @@ pub fn text_box<Editor>(editor: &Mutex<Editor>) -> TextBox<Editor> {
TextBox::new(editor)
}

impl<'a, 'editor, Editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a, Editor>
impl<'a, Editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a, Editor>
where
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
Renderer::Theme: StyleSheet,
Editor: Edit<'editor>,
Editor: Edit,
{
fn tag(&self) -> tree::Tag {
tree::Tag::of::<State>()
Expand All @@ -93,7 +95,10 @@ where

//TODO: allow lazy shape
let mut editor = self.editor.lock().unwrap();
editor.buffer_mut().shape_until(i32::max_value());
editor
.borrow_with(&mut FONT_SYSTEM.lock().unwrap())
.buffer_mut()
.shape_until(i32::max_value());

let mut layout_lines = 0;
for line in editor.buffer().lines.iter() {
Expand Down Expand Up @@ -162,6 +167,10 @@ 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 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.shape_as_needed();
Expand Down Expand Up @@ -232,6 +241,8 @@ where
) -> Status {
let state = tree.state.downcast_mut::<State>();
let mut editor = self.editor.lock().unwrap();
let mut font_system = FONT_SYSTEM.lock().unwrap();
let mut editor = editor.borrow_with(&mut font_system);

let mut status = Status::Ignored;
match event {
Expand Down Expand Up @@ -330,12 +341,11 @@ where
}
}

impl<'a, 'editor, Editor, Message, Renderer> From<TextBox<'a, Editor>>
for Element<'a, Message, Renderer>
impl<'a, Editor, Message, Renderer> From<TextBox<'a, Editor>> for Element<'a, Message, Renderer>
where
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
Renderer::Theme: StyleSheet,
Editor: Edit<'editor>,
Editor: Edit,
{
fn from(text_box: TextBox<'a, Editor>) -> Self {
Self::new(text_box)
Expand Down
6 changes: 4 additions & 2 deletions examples/editor-orbclient/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() {
)
.unwrap();

let font_system = FontSystem::new();
let mut font_system = FontSystem::new();

let syntax_system = SyntaxSystem::new();

Expand All @@ -58,7 +58,7 @@ fn main() {
let line_x = 8.0 * display_scale;

let mut editor = SyntaxEditor::new(
Buffer::new(&font_system, font_sizes[font_size_i]),
Buffer::new(&mut font_system, font_sizes[font_size_i]),
&syntax_system,
"base16-eighties.dark",
)
Expand All @@ -67,6 +67,8 @@ fn main() {
#[cfg(feature = "vi")]
let mut editor = cosmic_text::ViEditor::new(editor);

let mut editor = editor.borrow_with(&mut font_system);

editor
.buffer_mut()
.set_size(window.width() as f32 - line_x * 2.0, window.height() as f32);
Expand Down
20 changes: 15 additions & 5 deletions examples/editor-test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// 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<'_>, swash_cache: &mut SwashCache) {
fn redraw(
window: &mut Window,
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);

Expand All @@ -32,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,
Expand All @@ -54,11 +60,15 @@ fn main() {
];
let font_size_default = 1; // Body

let mut buffer = Buffer::new(&font_system, font_sizes[font_size_default]);
buffer.set_size(window.width() as f32, window.height() as f32);
let mut buffer = Buffer::new(&mut font_system, font_sizes[font_size_default]);
buffer
.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(&mut font_system);

let mut swash_cache = SwashCache::new();

let text = if let Some(arg) = env::args().nth(1) {
Expand Down
6 changes: 4 additions & 2 deletions examples/rich-text/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) => {
Expand All @@ -37,10 +37,12 @@ fn main() {
.unwrap();

let mut editor = Editor::new(Buffer::new(
&font_system,
&mut font_system,
Metrics::new(32.0, 44.0).scale(display_scale),
));

let mut editor = editor.borrow_with(&mut font_system);

editor
.buffer_mut()
.set_size(window.width() as f32, window.height() as f32);
Expand Down
6 changes: 4 additions & 2 deletions examples/terminal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -15,7 +15,9 @@ fn main() {
let metrics = Metrics::new(14.0, 20.0);

// 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(&mut font_system, metrics);

let mut buffer = buffer.borrow_with(&mut font_system);

// Set a size for the text buffer, in pixels
let width = 80u16;
Expand Down
Loading