From 5bc7cbf5bca039ec3a4cbe82b161c087a4b39680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 20 Jun 2023 06:22:17 +0200 Subject: [PATCH 1/2] Use subpixel glyph positioning and layout linearity ... for offsetting and scaling text --- graphics/src/backend.rs | 12 ---------- graphics/src/lib.rs | 1 - graphics/src/renderer.rs | 28 ++++++++--------------- renderer/src/backend.rs | 6 ----- tiny_skia/Cargo.toml | 2 +- tiny_skia/src/backend.rs | 6 ----- tiny_skia/src/text.rs | 49 +++++++++++++++------------------------- wgpu/Cargo.toml | 2 +- wgpu/src/backend.rs | 6 ----- wgpu/src/text.rs | 43 +++++++++++++++-------------------- 10 files changed, 47 insertions(+), 108 deletions(-) diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index ae89da061c..6a08257651 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -6,18 +6,6 @@ use iced_core::{Font, Point, Size}; use std::borrow::Cow; -/// The graphics backend of a [`Renderer`]. -/// -/// [`Renderer`]: crate::Renderer -pub trait Backend { - /// Trims the measurements cache. - /// - /// This method is currently necessary to properly trim the text cache in - /// `iced_wgpu` and `iced_glow` because of limitations in the text rendering - /// pipeline. It will be removed in the future. - fn trim_measurements(&mut self) {} -} - /// A graphics backend that supports text rendering. pub trait Text { /// The icon font of the backend. diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index f6bc87fbde..226b245b07 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -41,7 +41,6 @@ pub mod geometry; pub mod image; pub use antialiasing::Antialiasing; -pub use backend::Backend; pub use compositor::Compositor; pub use error::Error; pub use gradient::Gradient; diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index de90550399..4241d45ce6 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -1,5 +1,5 @@ //! Create a renderer from a [`Backend`]. -use crate::backend::{self, Backend}; +use crate::backend; use crate::Primitive; use iced_core::image; @@ -16,13 +16,13 @@ use std::marker::PhantomData; /// A backend-agnostic renderer that supports all the built-in widgets. #[derive(Debug)] -pub struct Renderer { +pub struct Renderer { backend: B, primitives: Vec, theme: PhantomData, } -impl Renderer { +impl Renderer { /// Creates a new [`Renderer`] from the given [`Backend`]. pub fn new(backend: B) -> Self { Self { @@ -52,10 +52,7 @@ impl Renderer { } } -impl iced_core::Renderer for Renderer -where - B: Backend, -{ +impl iced_core::Renderer for Renderer { type Theme = T; fn layout( @@ -63,11 +60,7 @@ where element: &Element<'_, Message, Self>, limits: &layout::Limits, ) -> layout::Node { - let layout = element.as_widget().layout(self, limits); - - self.backend.trim_measurements(); - - layout + element.as_widget().layout(self, limits) } fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)) { @@ -116,7 +109,7 @@ where impl text::Renderer for Renderer where - B: Backend + backend::Text, + B: backend::Text, { type Font = Font; @@ -195,7 +188,7 @@ where impl image::Renderer for Renderer where - B: Backend + backend::Image, + B: backend::Image, { type Handle = image::Handle; @@ -210,7 +203,7 @@ where impl svg::Renderer for Renderer where - B: Backend + backend::Svg, + B: backend::Svg, { fn dimensions(&self, handle: &svg::Handle) -> Size { self.backend().viewport_dimensions(handle) @@ -231,10 +224,7 @@ where } #[cfg(feature = "geometry")] -impl crate::geometry::Renderer for Renderer -where - B: Backend, -{ +impl crate::geometry::Renderer for Renderer { fn draw(&mut self, layers: Vec) { self.primitives .extend(layers.into_iter().map(crate::Geometry::into)); diff --git a/renderer/src/backend.rs b/renderer/src/backend.rs index c9d798517e..18f9f3fc69 100644 --- a/renderer/src/backend.rs +++ b/renderer/src/backend.rs @@ -21,12 +21,6 @@ macro_rules! delegate { }; } -impl iced_graphics::Backend for Backend { - fn trim_measurements(&mut self) { - delegate!(self, backend, backend.trim_measurements()); - } -} - impl backend::Text for Backend { const ICON_FONT: Font = Font::with_name("Iced-Icons"); const CHECKMARK_ICON: char = '\u{f00c}'; diff --git a/tiny_skia/Cargo.toml b/tiny_skia/Cargo.toml index 1571afb8f5..ef993fb9db 100644 --- a/tiny_skia/Cargo.toml +++ b/tiny_skia/Cargo.toml @@ -24,7 +24,7 @@ features = ["tiny-skia"] [dependencies.cosmic-text] git = "https://github.com/hecrj/cosmic-text.git" -rev = "e8b10fd675832cb9c1cc9de30922beb4cf883876" +rev = "c3cd24dc972bb8fd55d016c81ac9fa637e0a4ada" [dependencies.twox-hash] version = "1.6" diff --git a/tiny_skia/src/backend.rs b/tiny_skia/src/backend.rs index 9d0fc527da..bcc667f91c 100644 --- a/tiny_skia/src/backend.rs +++ b/tiny_skia/src/backend.rs @@ -658,12 +658,6 @@ fn adjust_clip_mask(clip_mask: &mut tiny_skia::Mask, bounds: Rectangle) { ); } -impl iced_graphics::Backend for Backend { - fn trim_measurements(&mut self) { - self.text_pipeline.trim_measurement_cache(); - } -} - impl backend::Text for Backend { const ICON_FONT: Font = Font::with_name("Iced-Icons"); const CHECKMARK_ICON: char = '\u{f00c}'; diff --git a/tiny_skia/src/text.rs b/tiny_skia/src/text.rs index a34c731797..be5fc4e1d7 100644 --- a/tiny_skia/src/text.rs +++ b/tiny_skia/src/text.rs @@ -14,8 +14,7 @@ use std::sync::Arc; pub struct Pipeline { font_system: RefCell, glyph_cache: GlyphCache, - measurement_cache: RefCell, - render_cache: Cache, + cache: RefCell, } impl Pipeline { @@ -29,8 +28,7 @@ impl Pipeline { .into_iter(), )), glyph_cache: GlyphCache::new(), - measurement_cache: RefCell::new(Cache::new()), - render_cache: Cache::new(), + cache: RefCell::new(Cache::new()), } } @@ -55,20 +53,11 @@ impl Pipeline { pixels: &mut tiny_skia::PixmapMut<'_>, clip_mask: Option<&tiny_skia::Mask>, ) { - let line_height = - f32::from(line_height.to_absolute(Pixels(size))) * scale_factor; - - let bounds = bounds * scale_factor; - let size = size * scale_factor; + let line_height = f32::from(line_height.to_absolute(Pixels(size))); let font_system = self.font_system.get_mut(); let key = Key { - bounds: { - let size = bounds.size(); - - // TODO: Reuse buffers from layouting - Size::new(size.width.ceil(), size.height.ceil()) - }, + bounds: bounds.size(), content, font, size, @@ -76,7 +65,7 @@ impl Pipeline { shaping, }; - let (_, buffer) = self.render_cache.allocate(font_system, key); + let (_, buffer) = self.cache.get_mut().allocate(font_system, key); let (total_lines, max_width) = buffer .layout_runs() @@ -85,7 +74,10 @@ impl Pipeline { (i + 1, buffer.line_w.max(max)) }); - let total_height = total_lines as f32 * line_height; + let total_height = total_lines as f32 * line_height * scale_factor; + let max_width = max_width * scale_factor; + + let bounds = bounds * scale_factor; let x = match horizontal_alignment { alignment::Horizontal::Left => bounds.x, @@ -99,16 +91,14 @@ impl Pipeline { alignment::Vertical::Bottom => bounds.y - total_height, }; - // TODO: Subpixel glyph positioning - let x = x.round() as i32; - let y = y.round() as i32; - let mut swash = cosmic_text::SwashCache::new(); for run in buffer.layout_runs() { for glyph in run.glyphs { + let physical_glyph = glyph.physical((x, y), scale_factor); + if let Some((buffer, placement)) = self.glyph_cache.allocate( - glyph.cache_key, + physical_glyph.cache_key, color, font_system, &mut swash, @@ -121,8 +111,9 @@ impl Pipeline { .expect("Create glyph pixel map"); pixels.draw_pixmap( - x + glyph.x_int + placement.left, - y - glyph.y_int - placement.top + run.line_y as i32, + physical_glyph.x + placement.left, + physical_glyph.y - placement.top + + (run.line_y * scale_factor).round() as i32, pixmap, &tiny_skia::PixmapPaint::default(), tiny_skia::Transform::identity(), @@ -134,7 +125,7 @@ impl Pipeline { } pub fn trim_cache(&mut self) { - self.render_cache.trim(); + self.cache.get_mut().trim(); self.glyph_cache.trim(); } @@ -147,7 +138,7 @@ impl Pipeline { bounds: Size, shaping: Shaping, ) -> (f32, f32) { - let mut measurement_cache = self.measurement_cache.borrow_mut(); + let mut measurement_cache = self.cache.borrow_mut(); let line_height = f32::from(line_height.to_absolute(Pixels(size))); @@ -184,7 +175,7 @@ impl Pipeline { point: Point, _nearest_only: bool, ) -> Option { - let mut measurement_cache = self.measurement_cache.borrow_mut(); + let mut measurement_cache = self.cache.borrow_mut(); let line_height = f32::from(line_height.to_absolute(Pixels(size))); @@ -204,10 +195,6 @@ impl Pipeline { Some(Hit::CharOffset(cursor.index)) } - - pub fn trim_measurement_cache(&mut self) { - self.measurement_cache.borrow_mut().trim(); - } } fn to_family(family: font::Family) -> cosmic_text::Family<'static> { diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index f3a83acb2f..15db5b5d41 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -45,7 +45,7 @@ path = "../graphics" [dependencies.glyphon] version = "0.2" git = "https://github.com/hecrj/glyphon.git" -rev = "8dbf36020e5759fa9144517b321372266160113e" +rev = "8324f20158a62f8520bad4ed09f6aa5552f8f2a6" [dependencies.glam] version = "0.24" diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index b524c6152a..eecba2f1fe 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -334,12 +334,6 @@ impl Backend { } } -impl iced_graphics::Backend for Backend { - fn trim_measurements(&mut self) { - self.text_pipeline.trim_measurement_cache() - } -} - impl backend::Text for Backend { const ICON_FONT: Font = Font::with_name("Iced-Icons"); const CHECKMARK_ICON: char = '\u{f00c}'; diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs index 6a55227076..71dcc249ad 100644 --- a/wgpu/src/text.rs +++ b/wgpu/src/text.rs @@ -18,8 +18,7 @@ pub struct Pipeline { renderers: Vec, atlas: glyphon::TextAtlas, prepare_layer: usize, - measurement_cache: RefCell, - render_cache: Cache, + cache: RefCell, } impl Pipeline { @@ -47,8 +46,7 @@ impl Pipeline { }, ), prepare_layer: 0, - measurement_cache: RefCell::new(Cache::new()), - render_cache: Cache::new(), + cache: RefCell::new(Cache::new()), } } @@ -78,25 +76,25 @@ impl Pipeline { let font_system = self.font_system.get_mut(); let renderer = &mut self.renderers[self.prepare_layer]; + let cache = self.cache.get_mut(); let keys: Vec<_> = sections .iter() .map(|section| { - let (key, _) = self.render_cache.allocate( + let (key, _) = cache.allocate( font_system, Key { content: section.content, - size: section.size * scale_factor, + size: section.size, line_height: f32::from( section .line_height .to_absolute(Pixels(section.size)), - ) * scale_factor, + ), font: section.font, bounds: Size { - width: (section.bounds.width * scale_factor).ceil(), - height: (section.bounds.height * scale_factor) - .ceil(), + width: section.bounds.width, + height: section.bounds.height, }, shaping: section.shaping, }, @@ -113,14 +111,16 @@ impl Pipeline { .iter() .zip(keys.iter()) .filter_map(|(section, key)| { - let buffer = - self.render_cache.get(key).expect("Get cached buffer"); - - let (max_width, total_height) = measure(buffer); + let buffer = cache.get(key).expect("Get cached buffer"); let x = section.bounds.x * scale_factor; let y = section.bounds.y * scale_factor; + let (max_width, total_height) = measure(buffer); + + let max_width = max_width * scale_factor; + let total_height = total_height * scale_factor; + let left = match section.horizontal_alignment { alignment::Horizontal::Left => x, alignment::Horizontal::Center => x - max_width / 2.0, @@ -142,14 +142,11 @@ impl Pipeline { let clip_bounds = bounds.intersection(§ion_bounds)?; - // TODO: Subpixel glyph positioning - let left = left.round() as i32; - let top = top.round() as i32; - Some(glyphon::TextArea { buffer, left, top, + scale: scale_factor, bounds: glyphon::TextBounds { left: clip_bounds.x as i32, top: clip_bounds.y as i32, @@ -227,7 +224,7 @@ impl Pipeline { pub fn end_frame(&mut self) { self.atlas.trim(); - self.render_cache.trim(); + self.cache.get_mut().trim(); self.prepare_layer = 0; } @@ -241,7 +238,7 @@ impl Pipeline { bounds: Size, shaping: Shaping, ) -> (f32, f32) { - let mut measurement_cache = self.measurement_cache.borrow_mut(); + let mut measurement_cache = self.cache.borrow_mut(); let line_height = f32::from(line_height.to_absolute(Pixels(size))); @@ -271,7 +268,7 @@ impl Pipeline { point: Point, _nearest_only: bool, ) -> Option { - let mut measurement_cache = self.measurement_cache.borrow_mut(); + let mut measurement_cache = self.cache.borrow_mut(); let line_height = f32::from(line_height.to_absolute(Pixels(size))); @@ -291,10 +288,6 @@ impl Pipeline { Some(Hit::CharOffset(cursor.index)) } - - pub fn trim_measurement_cache(&mut self) { - self.measurement_cache.borrow_mut().trim(); - } } fn measure(buffer: &glyphon::Buffer) -> (f32, f32) { From ef87ff1e8ab24279a551b3eef6bacd8813712530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 21 Jun 2023 21:47:29 +0200 Subject: [PATCH 2/2] Clear text caches after a font is loaded --- tiny_skia/src/text.rs | 2 ++ wgpu/src/text.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tiny_skia/src/text.rs b/tiny_skia/src/text.rs index be5fc4e1d7..6ff797db59 100644 --- a/tiny_skia/src/text.rs +++ b/tiny_skia/src/text.rs @@ -36,6 +36,8 @@ impl Pipeline { self.font_system.get_mut().db_mut().load_font_source( cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())), ); + + self.cache = RefCell::new(Cache::new()); } pub fn draw( diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs index 71dcc249ad..c9188bd1aa 100644 --- a/wgpu/src/text.rs +++ b/wgpu/src/text.rs @@ -54,6 +54,8 @@ impl Pipeline { let _ = self.font_system.get_mut().db_mut().load_font_source( glyphon::fontdb::Source::Binary(Arc::new(bytes.into_owned())), ); + + self.cache = RefCell::new(Cache::new()); } pub fn prepare(