From f7d9f44cd3bbb6ca942acf89ccbccbeb6399b3a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 16 Jun 2024 21:42:17 +0200 Subject: [PATCH] Update `cosmic-text` and `resvg` --- Cargo.toml | 4 ++-- graphics/src/geometry/text.rs | 4 +++- graphics/src/text/cache.rs | 4 ++-- graphics/src/text/editor.rs | 16 +++++++++------- graphics/src/text/paragraph.rs | 8 ++++---- tiny_skia/src/engine.rs | 10 +++++++--- tiny_skia/src/text.rs | 8 +++++++- tiny_skia/src/vector.rs | 20 ++++++++------------ wgpu/src/image/vector.rs | 10 ++-------- wgpu/src/text.rs | 8 +++++++- 10 files changed, 51 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d1b83d93ff..3ebe40f032 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -157,7 +157,7 @@ ouroboros = "0.18" palette = "0.7" qrcode = { version = "0.13", default-features = false } raw-window-handle = "0.6" -resvg = "0.41" +resvg = "0.42" rustc-hash = "1.0" smol = "1.0" smol_str = "0.2" @@ -204,4 +204,4 @@ useless_conversion = "deny" broken_intra_doc_links = "forbid" [patch.crates-io] -cosmic-text = { git = "https://github.com/hecrj/cosmic-text.git", rev = "a2b3f4064178afd7f91e0d86407a82bf507da55f" } +cosmic-text = { git = "https://github.com/pop-os/cosmic-text.git", rev = "542b20ca4376a3b5de5fa629db1a4ace44e18e0c" } diff --git a/graphics/src/geometry/text.rs b/graphics/src/geometry/text.rs index 409221805f..90147f8796 100644 --- a/graphics/src/geometry/text.rs +++ b/graphics/src/geometry/text.rs @@ -43,6 +43,7 @@ impl Text { let mut buffer = cosmic_text::BufferLine::new( &self.content, + cosmic_text::LineEnding::default(), cosmic_text::AttrsList::new(text::to_attributes(self.font)), text::to_shaping(self.shaping), ); @@ -50,9 +51,10 @@ impl Text { let layout = buffer.layout( font_system.raw(), self.size.0, - f32::MAX, + None, cosmic_text::Wrap::None, None, + 4, ); let translation_x = match self.horizontal_alignment { diff --git a/graphics/src/text/cache.rs b/graphics/src/text/cache.rs index 822b61c47c..e64d93f166 100644 --- a/graphics/src/text/cache.rs +++ b/graphics/src/text/cache.rs @@ -48,8 +48,8 @@ impl Cache { buffer.set_size( font_system, - key.bounds.width, - key.bounds.height.max(key.line_height), + Some(key.bounds.width), + Some(key.bounds.height.max(key.line_height)), ); buffer.set_text( font_system, diff --git a/graphics/src/text/editor.rs b/graphics/src/text/editor.rs index 83fa7b69c4..0956447a95 100644 --- a/graphics/src/text/editor.rs +++ b/graphics/src/text/editor.rs @@ -136,7 +136,8 @@ impl editor::Editor for Editor { width, y: (visual_line as i32 + visual_lines_offset) as f32 - * line_height, + * line_height + - buffer.scroll().vertical, height: line_height, }) } else { @@ -218,7 +219,8 @@ impl editor::Editor for Editor { Cursor::Caret(Point::new( offset, (visual_lines_offset + visual_line as i32) as f32 - * line_height, + * line_height + - buffer.scroll().vertical, )) } } @@ -482,8 +484,8 @@ impl editor::Editor for Editor { buffer_mut_from_editor(&mut internal.editor).set_size( font_system.raw(), - new_bounds.width, - new_bounds.height, + Some(new_bounds.width), + Some(new_bounds.height), ); internal.bounds = new_bounds; @@ -513,7 +515,8 @@ impl editor::Editor for Editor { let buffer = buffer_from_editor(&internal.editor); let scroll = buffer.scroll(); - let mut window = buffer.visible_lines(); + let mut window = (internal.bounds.height / buffer.metrics().line_height) + .ceil() as i32; let last_visible_line = buffer.lines[scroll.line..] .iter() @@ -715,8 +718,7 @@ fn visual_lines_offset(line: usize, buffer: &cosmic_text::Buffer) -> i32 { }) .sum(); - (visual_lines_offset as i32 - scroll.layout) - * if scroll.line < line { 1 } else { -1 } + visual_lines_offset as i32 * if scroll.line < line { 1 } else { -1 } } fn to_motion(motion: Motion) -> cosmic_text::Motion { diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index 31a323ac90..a5fefe8fed 100644 --- a/graphics/src/text/paragraph.rs +++ b/graphics/src/text/paragraph.rs @@ -77,8 +77,8 @@ impl core::text::Paragraph for Paragraph { buffer.set_size( font_system.raw(), - text.bounds.width, - text.bounds.height, + Some(text.bounds.width), + Some(text.bounds.height), ); buffer.set_text( @@ -116,8 +116,8 @@ impl core::text::Paragraph for Paragraph { internal.buffer.set_size( font_system.raw(), - new_bounds.width, - new_bounds.height, + Some(new_bounds.width), + Some(new_bounds.height), ); internal.bounds = new_bounds; diff --git a/tiny_skia/src/engine.rs b/tiny_skia/src/engine.rs index 028b304fb3..898657c8ee 100644 --- a/tiny_skia/src/engine.rs +++ b/tiny_skia/src/engine.rs @@ -439,9 +439,13 @@ impl Engine { let transformation = transformation * *local_transformation; let (width, height) = buffer.size(); - let physical_bounds = - Rectangle::new(raw.position, Size::new(width, height)) - * transformation; + let physical_bounds = Rectangle::new( + raw.position, + Size::new( + width.unwrap_or(clip_bounds.width), + height.unwrap_or(clip_bounds.height), + ), + ) * transformation; if !clip_bounds.intersects(&physical_bounds) { return; diff --git a/tiny_skia/src/text.rs b/tiny_skia/src/text.rs index c71deb105f..0fc3d1f71b 100644 --- a/tiny_skia/src/text.rs +++ b/tiny_skia/src/text.rs @@ -169,7 +169,13 @@ impl Pipeline { font_system.raw(), &mut self.glyph_cache, buffer, - Rectangle::new(position, Size::new(width, height)), + Rectangle::new( + position, + Size::new( + width.unwrap_or(pixels.width() as f32), + height.unwrap_or(pixels.height() as f32), + ), + ), color, alignment::Horizontal::Left, alignment::Vertical::Top, diff --git a/tiny_skia/src/vector.rs b/tiny_skia/src/vector.rs index 95fece4797..dec3a3c1c4 100644 --- a/tiny_skia/src/vector.rs +++ b/tiny_skia/src/vector.rs @@ -1,6 +1,5 @@ use crate::core::svg::{Data, Handle}; use crate::core::{Color, Rectangle, Size}; -use crate::graphics::text; use resvg::usvg; use rustc_hash::{FxHashMap, FxHashSet}; @@ -83,26 +82,23 @@ impl Cache { let id = handle.id(); if let hash_map::Entry::Vacant(entry) = self.trees.entry(id) { - let mut font_system = - text::font_system().write().expect("Write font system"); - let mut svg = match handle.data() { Data::Path(path) => { fs::read_to_string(path).ok().and_then(|contents| { usvg::Tree::from_str( &contents, - &usvg::Options::default(), - font_system.raw().db(), + &usvg::Options::default(), // TODO: Set usvg::Options::fontdb ) .ok() }) } - Data::Bytes(bytes) => usvg::Tree::from_data( - bytes, - &usvg::Options::default(), - font_system.raw().db(), - ) - .ok(), + Data::Bytes(bytes) => { + usvg::Tree::from_data( + bytes, + &usvg::Options::default(), // TODO: Set usvg::Options::fontdb + ) + .ok() + } }; if let Some(svg) = &mut svg { diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 041ad9de6d..74e9924dd6 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -1,6 +1,5 @@ use crate::core::svg; use crate::core::{Color, Size}; -use crate::graphics::text; use crate::image::atlas::{self, Atlas}; use resvg::tiny_skia; @@ -49,17 +48,13 @@ impl Cache { return self.svgs.get(&handle.id()).unwrap(); } - let mut font_system = - text::font_system().write().expect("Write font system"); - let svg = match handle.data() { svg::Data::Path(path) => fs::read_to_string(path) .ok() .and_then(|contents| { usvg::Tree::from_str( &contents, - &usvg::Options::default(), - font_system.raw().db(), + &usvg::Options::default(), // TODO: Set usvg::Options::fontdb ) .ok() }) @@ -68,8 +63,7 @@ impl Cache { svg::Data::Bytes(bytes) => { match usvg::Tree::from_data( bytes, - &usvg::Options::default(), - font_system.raw().db(), + &usvg::Options::default(), // TODO: Set usvg::Options::fontdb ) { Ok(tree) => Svg::Loaded(tree), Err(_) => Svg::NotFound, diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs index 05db5f8069..bf7eae180f 100644 --- a/wgpu/src/text.rs +++ b/wgpu/src/text.rs @@ -585,7 +585,13 @@ fn prepare( ( buffer.as_ref(), - Rectangle::new(raw.position, Size::new(width, height)), + Rectangle::new( + raw.position, + Size::new( + width.unwrap_or(layer_bounds.width), + height.unwrap_or(layer_bounds.height), + ), + ), alignment::Horizontal::Left, alignment::Vertical::Top, raw.color,