From ed3454301e663a7cb7d73cd56b57b188f4d14a2f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 30 Aug 2023 04:31:21 +0200 Subject: Implement explicit text caching in the widget state tree --- graphics/Cargo.toml | 11 ++ graphics/fonts/Iced-Icons.ttf | Bin 0 -> 5108 bytes graphics/src/backend.rs | 69 ++---------- graphics/src/damage.rs | 26 +++++ graphics/src/geometry/text.rs | 6 +- graphics/src/lib.rs | 3 +- graphics/src/primitive.rs | 14 ++- graphics/src/renderer.rs | 125 ++++++++++----------- graphics/src/text.rs | 113 +++++++++++++++++++ graphics/src/text/cache.rs | 120 ++++++++++++++++++++ graphics/src/text/paragraph.rs | 246 +++++++++++++++++++++++++++++++++++++++++ 11 files changed, 596 insertions(+), 137 deletions(-) create mode 100644 graphics/fonts/Iced-Icons.ttf create mode 100644 graphics/src/text.rs create mode 100644 graphics/src/text/cache.rs create mode 100644 graphics/src/text/paragraph.rs (limited to 'graphics') diff --git a/graphics/Cargo.toml b/graphics/Cargo.toml index ca7bf61a..442eb007 100644 --- a/graphics/Cargo.toml +++ b/graphics/Cargo.toml @@ -23,6 +23,9 @@ log = "0.4" raw-window-handle = "0.5" thiserror = "1.0" bitflags = "1.2" +cosmic-text = "0.9" +rustc-hash = "1.1" +unicode-segmentation = "1.6" [dependencies.bytemuck] version = "1.4" @@ -32,6 +35,14 @@ features = ["derive"] version = "0.10" path = "../core" +[dependencies.twox-hash] +version = "1.6" +default-features = false + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies.twox-hash] +version = "1.6.1" +features = ["std"] + [dependencies.image] version = "0.24" optional = true diff --git a/graphics/fonts/Iced-Icons.ttf b/graphics/fonts/Iced-Icons.ttf new file mode 100644 index 00000000..e3273141 Binary files /dev/null and b/graphics/fonts/Iced-Icons.ttf differ diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index 59e95bf8..6774b9ca 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -1,8 +1,8 @@ //! Write a graphics backend. -use iced_core::image; -use iced_core::svg; -use iced_core::text; -use iced_core::{Font, Point, Size}; +use crate::core::image; +use crate::core::svg; +use crate::core::Size; +use crate::text; use std::borrow::Cow; @@ -12,70 +12,15 @@ use std::borrow::Cow; pub trait Backend { /// The custom kind of primitives this [`Backend`] supports. type Primitive; - - /// 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. - const ICON_FONT: Font; - - /// The `char` representing a ✔ icon in the [`ICON_FONT`]. - /// - /// [`ICON_FONT`]: Self::ICON_FONT - const CHECKMARK_ICON: char; - - /// The `char` representing a ▼ icon in the built-in [`ICON_FONT`]. - /// - /// [`ICON_FONT`]: Self::ICON_FONT - const ARROW_DOWN_ICON: char; - - /// Returns the default [`Font`]. - fn default_font(&self) -> Font; - - /// Returns the default size of text. - fn default_size(&self) -> f32; - - /// Measures the text contents with the given size and font, - /// returning the size of a laid out paragraph that fits in the provided - /// bounds. - fn measure( - &self, - contents: &str, - size: f32, - line_height: text::LineHeight, - font: Font, - bounds: Size, - shaping: text::Shaping, - ) -> Size; - - /// Tests whether the provided point is within the boundaries of [`Text`] - /// laid out with the given parameters, returning information about - /// the nearest character. - /// - /// If nearest_only is true, the hit test does not consider whether the - /// the point is interior to any glyph bounds, returning only the character - /// with the nearest centeroid. - fn hit_test( - &self, - contents: &str, - size: f32, - line_height: text::LineHeight, - font: Font, - bounds: Size, - shaping: text::Shaping, - point: Point, - nearest_only: bool, - ) -> Option; - /// Loads a [`Font`] from its bytes. fn load_font(&mut self, font: Cow<'static, [u8]>); + + /// Returns the [`cosmic_text::FontSystem`] of the [`Backend`]. + fn font_system(&self) -> &text::FontSystem; } /// A graphics backend that supports image rendering. diff --git a/graphics/src/damage.rs b/graphics/src/damage.rs index 2f29956e..3276c2d4 100644 --- a/graphics/src/damage.rs +++ b/graphics/src/damage.rs @@ -40,6 +40,32 @@ impl Damage for Primitive { bounds.expand(1.5) } + Self::Paragraph { + paragraph, + position, + .. + } => { + let mut bounds = + Rectangle::new(*position, paragraph.min_bounds); + + bounds.x = match paragraph.horizontal_alignment { + alignment::Horizontal::Left => bounds.x, + alignment::Horizontal::Center => { + bounds.x - bounds.width / 2.0 + } + alignment::Horizontal::Right => bounds.x - bounds.width, + }; + + bounds.y = match paragraph.vertical_alignment { + alignment::Vertical::Top => bounds.y, + alignment::Vertical::Center => { + bounds.y - bounds.height / 2.0 + } + alignment::Vertical::Bottom => bounds.y - bounds.height, + }; + + bounds.expand(1.5) + } Self::Quad { bounds, .. } | Self::Image { bounds, .. } | Self::Svg { bounds, .. } => bounds.expand(1.0), diff --git a/graphics/src/geometry/text.rs b/graphics/src/geometry/text.rs index c584f3cd..0bf7ec97 100644 --- a/graphics/src/geometry/text.rs +++ b/graphics/src/geometry/text.rs @@ -1,6 +1,6 @@ use crate::core::alignment; use crate::core::text::{LineHeight, Shaping}; -use crate::core::{Color, Font, Point}; +use crate::core::{Color, Font, Pixels, Point}; /// A bunch of text that can be drawn to a canvas #[derive(Debug, Clone)] @@ -19,7 +19,7 @@ pub struct Text { /// The color of the text pub color: Color, /// The size of the text - pub size: f32, + pub size: Pixels, /// The line height of the text. pub line_height: LineHeight, /// The font of the text @@ -38,7 +38,7 @@ impl Default for Text { content: String::new(), position: Point::ORIGIN, color: Color::BLACK, - size: 16.0, + size: Pixels(16.0), line_height: LineHeight::Relative(1.2), font: Font::default(), horizontal_alignment: alignment::Horizontal::Left, diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index af374a2f..902eb5b0 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -9,7 +9,7 @@ )] #![deny( missing_debug_implementations, - missing_docs, + //missing_docs, unsafe_code, unused_results, clippy::extra_unused_lifetimes, @@ -34,6 +34,7 @@ pub mod damage; pub mod gradient; pub mod mesh; pub mod renderer; +pub mod text; #[cfg(feature = "geometry")] pub mod geometry; diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index 7592a410..cdc8923e 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -3,7 +3,8 @@ use crate::core::alignment; use crate::core::image; use crate::core::svg; use crate::core::text; -use crate::core::{Background, Color, Font, Rectangle, Vector}; +use crate::core::{Background, Color, Font, Pixels, Point, Rectangle, Vector}; +use crate::text::paragraph; use std::sync::Arc; @@ -19,7 +20,7 @@ pub enum Primitive { /// The color of the text color: Color, /// The size of the text in logical pixels - size: f32, + size: Pixels, /// The line height of the text line_height: text::LineHeight, /// The font of the text @@ -31,6 +32,15 @@ pub enum Primitive { /// The shaping strategy of the text. shaping: text::Shaping, }, + /// A paragraph primitive + Paragraph { + /// The [`Paragraph`]. + paragraph: paragraph::Weak, + /// The position of the [`Paragraph`]. + position: Point, + /// The color of the [`Paragraph`]. + color: Color, + }, /// A quad primitive Quad { /// The bounds of the quad diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index c0cec60a..f93f4a6d 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -1,15 +1,15 @@ //! Create a renderer from a [`Backend`]. use crate::backend::{self, Backend}; -use crate::Primitive; - -use iced_core::image; -use iced_core::layout; -use iced_core::renderer; -use iced_core::svg; -use iced_core::text::{self, Text}; -use iced_core::{ - Background, Color, Element, Font, Point, Rectangle, Size, Vector, +use crate::core; +use crate::core::image; +use crate::core::renderer; +use crate::core::svg; +use crate::core::text::Text; +use crate::core::{ + Background, Color, Font, Pixels, Point, Rectangle, Size, Vector, }; +use crate::text; +use crate::Primitive; use std::borrow::Cow; use std::marker::PhantomData; @@ -18,15 +18,23 @@ use std::marker::PhantomData; #[derive(Debug)] pub struct Renderer { backend: B, + default_font: Font, + default_text_size: Pixels, primitives: Vec>, theme: PhantomData, } impl Renderer { /// Creates a new [`Renderer`] from the given [`Backend`]. - pub fn new(backend: B) -> Self { + pub fn new( + backend: B, + default_font: Font, + default_text_size: Pixels, + ) -> Self { Self { backend, + default_font, + default_text_size, primitives: Vec::new(), theme: PhantomData, } @@ -88,16 +96,6 @@ impl Renderer { impl iced_core::Renderer for Renderer { type Theme = T; - fn layout( - &mut self, - element: &Element<'_, Message, Self>, - limits: &layout::Limits, - ) -> layout::Node { - self.backend.trim_measurements(); - - element.as_widget().layout(self, limits) - } - fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)) { let current = self.start_layer(); @@ -137,77 +135,66 @@ impl iced_core::Renderer for Renderer { } } -impl text::Renderer for Renderer +impl core::text::Renderer for Renderer where B: Backend + backend::Text, { type Font = Font; + type Paragraph = text::Paragraph; - const ICON_FONT: Font = B::ICON_FONT; - const CHECKMARK_ICON: char = B::CHECKMARK_ICON; - const ARROW_DOWN_ICON: char = B::ARROW_DOWN_ICON; + const ICON_FONT: Font = Font::with_name("Iced-Icons"); + const CHECKMARK_ICON: char = '\u{f00c}'; + const ARROW_DOWN_ICON: char = '\u{e800}'; fn default_font(&self) -> Self::Font { - self.backend().default_font() + self.default_font } - fn default_size(&self) -> f32 { - self.backend().default_size() + fn default_size(&self) -> Pixels { + self.default_text_size } - fn measure( - &self, - content: &str, - size: f32, - line_height: text::LineHeight, - font: Font, - bounds: Size, - shaping: text::Shaping, - ) -> Size { - self.backend().measure( - content, - size, - line_height, - font, - bounds, - shaping, - ) + fn load_font(&mut self, bytes: Cow<'static, [u8]>) { + self.backend.load_font(bytes); + } + + fn create_paragraph(&self, text: Text<'_, Self::Font>) -> text::Paragraph { + text::Paragraph::with_text(text, self.backend.font_system()) } - fn hit_test( + fn resize_paragraph( &self, - content: &str, - size: f32, - line_height: text::LineHeight, - font: Font, - bounds: Size, - shaping: text::Shaping, - point: Point, - nearest_only: bool, - ) -> Option { - self.backend().hit_test( - content, - size, - line_height, - font, - bounds, - shaping, - point, - nearest_only, - ) + paragraph: &mut Self::Paragraph, + new_bounds: Size, + ) { + paragraph.resize(new_bounds, self.backend.font_system()); } - fn load_font(&mut self, bytes: Cow<'static, [u8]>) { - self.backend.load_font(bytes); + fn fill_paragraph( + &mut self, + paragraph: &Self::Paragraph, + position: Point, + color: Color, + ) { + self.primitives.push(Primitive::Paragraph { + paragraph: paragraph.downgrade(), + position, + color, + }); } - fn fill_text(&mut self, text: Text<'_, Self::Font>) { + fn fill_text( + &mut self, + text: Text<'_, Self::Font>, + position: Point, + color: Color, + ) { self.primitives.push(Primitive::Text { content: text.content.to_string(), - bounds: text.bounds, + bounds: Rectangle::new(position, text.bounds), size: text.size, line_height: text.line_height, - color: text.color, + color, font: text.font, horizontal_alignment: text.horizontal_alignment, vertical_alignment: text.vertical_alignment, diff --git a/graphics/src/text.rs b/graphics/src/text.rs new file mode 100644 index 00000000..bbe9d7cb --- /dev/null +++ b/graphics/src/text.rs @@ -0,0 +1,113 @@ +pub mod cache; +pub mod paragraph; + +pub use cache::Cache; +pub use paragraph::Paragraph; + +pub use cosmic_text; + +use crate::core::font::{self, Font}; +use crate::core::text::Shaping; +use crate::core::Size; + +use std::sync::{self, Arc, RwLock}; + +#[allow(missing_debug_implementations)] +pub struct FontSystem(RwLock); + +impl FontSystem { + pub fn new() -> Self { + FontSystem(RwLock::new(cosmic_text::FontSystem::new_with_fonts( + [cosmic_text::fontdb::Source::Binary(Arc::new( + include_bytes!("../fonts/Iced-Icons.ttf").as_slice(), + ))] + .into_iter(), + ))) + } + + pub fn get_mut(&mut self) -> &mut cosmic_text::FontSystem { + self.0.get_mut().expect("Lock font system") + } + + pub fn write(&self) -> sync::RwLockWriteGuard<'_, cosmic_text::FontSystem> { + self.0.write().expect("Write font system") + } +} + +impl Default for FontSystem { + fn default() -> Self { + Self::new() + } +} + +pub fn measure(buffer: &cosmic_text::Buffer) -> Size { + let (width, total_lines) = buffer + .layout_runs() + .fold((0.0, 0usize), |(width, total_lines), run| { + (run.line_w.max(width), total_lines + 1) + }); + + Size::new(width, total_lines as f32 * buffer.metrics().line_height) +} + +pub fn to_attributes(font: Font) -> cosmic_text::Attrs<'static> { + cosmic_text::Attrs::new() + .family(to_family(font.family)) + .weight(to_weight(font.weight)) + .stretch(to_stretch(font.stretch)) + .style(to_style(font.style)) +} + +fn to_family(family: font::Family) -> cosmic_text::Family<'static> { + match family { + font::Family::Name(name) => cosmic_text::Family::Name(name), + font::Family::SansSerif => cosmic_text::Family::SansSerif, + font::Family::Serif => cosmic_text::Family::Serif, + font::Family::Cursive => cosmic_text::Family::Cursive, + font::Family::Fantasy => cosmic_text::Family::Fantasy, + font::Family::Monospace => cosmic_text::Family::Monospace, + } +} + +fn to_weight(weight: font::Weight) -> cosmic_text::Weight { + match weight { + font::Weight::Thin => cosmic_text::Weight::THIN, + font::Weight::ExtraLight => cosmic_text::Weight::EXTRA_LIGHT, + font::Weight::Light => cosmic_text::Weight::LIGHT, + font::Weight::Normal => cosmic_text::Weight::NORMAL, + font::Weight::Medium => cosmic_text::Weight::MEDIUM, + font::Weight::Semibold => cosmic_text::Weight::SEMIBOLD, + font::Weight::Bold => cosmic_text::Weight::BOLD, + font::Weight::ExtraBold => cosmic_text::Weight::EXTRA_BOLD, + font::Weight::Black => cosmic_text::Weight::BLACK, + } +} + +fn to_stretch(stretch: font::Stretch) -> cosmic_text::Stretch { + match stretch { + font::Stretch::UltraCondensed => cosmic_text::Stretch::UltraCondensed, + font::Stretch::ExtraCondensed => cosmic_text::Stretch::ExtraCondensed, + font::Stretch::Condensed => cosmic_text::Stretch::Condensed, + font::Stretch::SemiCondensed => cosmic_text::Stretch::SemiCondensed, + font::Stretch::Normal => cosmic_text::Stretch::Normal, + font::Stretch::SemiExpanded => cosmic_text::Stretch::SemiExpanded, + font::Stretch::Expanded => cosmic_text::Stretch::Expanded, + font::Stretch::ExtraExpanded => cosmic_text::Stretch::ExtraExpanded, + font::Stretch::UltraExpanded => cosmic_text::Stretch::UltraExpanded, + } +} + +fn to_style(style: font::Style) -> cosmic_text::Style { + match style { + font::Style::Normal => cosmic_text::Style::Normal, + font::Style::Italic => cosmic_text::Style::Italic, + font::Style::Oblique => cosmic_text::Style::Oblique, + } +} + +pub fn to_shaping(shaping: Shaping) -> cosmic_text::Shaping { + match shaping { + Shaping::Basic => cosmic_text::Shaping::Basic, + Shaping::Advanced => cosmic_text::Shaping::Advanced, + } +} diff --git a/graphics/src/text/cache.rs b/graphics/src/text/cache.rs new file mode 100644 index 00000000..8aea6715 --- /dev/null +++ b/graphics/src/text/cache.rs @@ -0,0 +1,120 @@ +use crate::core::{Font, Size}; +use crate::text; + +use rustc_hash::{FxHashMap, FxHashSet}; +use std::collections::hash_map; +use std::hash::{BuildHasher, Hash, Hasher}; + +#[allow(missing_debug_implementations)] +#[derive(Default)] +pub struct Cache { + entries: FxHashMap, + aliases: FxHashMap, + recently_used: FxHashSet, + hasher: HashBuilder, +} + +#[cfg(not(target_arch = "wasm32"))] +type HashBuilder = twox_hash::RandomXxHashBuilder64; + +#[cfg(target_arch = "wasm32")] +type HashBuilder = std::hash::BuildHasherDefault; + +impl Cache { + pub fn new() -> Self { + Self::default() + } + + pub fn get(&self, key: &KeyHash) -> Option<&cosmic_text::Buffer> { + self.entries.get(key) + } + + pub fn allocate( + &mut self, + font_system: &mut cosmic_text::FontSystem, + key: Key<'_>, + ) -> (KeyHash, &mut cosmic_text::Buffer) { + let hash = key.hash(self.hasher.build_hasher()); + + if let Some(hash) = self.aliases.get(&hash) { + let _ = self.recently_used.insert(*hash); + + return (*hash, self.entries.get_mut(hash).unwrap()); + } + + if let hash_map::Entry::Vacant(entry) = self.entries.entry(hash) { + let metrics = cosmic_text::Metrics::new(key.size, key.line_height); + let mut buffer = cosmic_text::Buffer::new(font_system, metrics); + + buffer.set_size( + font_system, + key.bounds.width, + key.bounds.height.max(key.line_height), + ); + buffer.set_text( + font_system, + key.content, + text::to_attributes(key.font), + text::to_shaping(key.shaping), + ); + + let bounds = text::measure(&buffer); + let _ = entry.insert(buffer); + + for bounds in [ + bounds, + Size { + width: key.bounds.width, + ..bounds + }, + ] { + if key.bounds != bounds { + let _ = self.aliases.insert( + Key { bounds, ..key }.hash(self.hasher.build_hasher()), + hash, + ); + } + } + } + + let _ = self.recently_used.insert(hash); + + (hash, self.entries.get_mut(&hash).unwrap()) + } + + pub fn trim(&mut self) { + self.entries + .retain(|key, _| self.recently_used.contains(key)); + + self.aliases + .retain(|_, value| self.recently_used.contains(value)); + + self.recently_used.clear(); + } +} + +#[derive(Debug, Clone, Copy)] +pub struct Key<'a> { + pub content: &'a str, + pub size: f32, + pub line_height: f32, + pub font: Font, + pub bounds: Size, + pub shaping: text::Shaping, +} + +impl Key<'_> { + fn hash(self, mut hasher: H) -> KeyHash { + self.content.hash(&mut hasher); + self.size.to_bits().hash(&mut hasher); + self.line_height.to_bits().hash(&mut hasher); + self.font.hash(&mut hasher); + self.bounds.width.to_bits().hash(&mut hasher); + self.bounds.height.to_bits().hash(&mut hasher); + self.shaping.hash(&mut hasher); + + hasher.finish() + } +} + +pub type KeyHash = u64; diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs new file mode 100644 index 00000000..7b70376a --- /dev/null +++ b/graphics/src/text/paragraph.rs @@ -0,0 +1,246 @@ +use crate::core; +use crate::core::alignment; +use crate::core::text::{Hit, LineHeight, Shaping, Text}; +use crate::core::{Font, Pixels, Point, Size}; +use crate::text::{self, FontSystem}; + +use std::fmt; +use std::sync::{self, Arc}; + +#[derive(Clone, PartialEq, Default)] +pub struct Paragraph(Arc); + +struct Internal { + buffer: cosmic_text::Buffer, + content: String, // TODO: Reuse from `buffer` (?) + font: Font, + shaping: Shaping, + horizontal_alignment: alignment::Horizontal, + vertical_alignment: alignment::Vertical, + bounds: Size, + min_bounds: Size, +} + +impl Paragraph { + pub fn new() -> Self { + Self::default() + } + + pub fn with_text(text: Text<'_, Font>, font_system: &FontSystem) -> Self { + let mut font_system = font_system.write(); + + let mut buffer = cosmic_text::Buffer::new( + &mut font_system, + cosmic_text::Metrics::new( + text.size.into(), + text.line_height.to_absolute(text.size).into(), + ), + ); + + buffer.set_size( + &mut font_system, + text.bounds.width, + text.bounds.height, + ); + + buffer.set_text( + &mut font_system, + text.content, + text::to_attributes(text.font), + text::to_shaping(text.shaping), + ); + + let min_bounds = text::measure(&buffer); + + Self(Arc::new(Internal { + buffer, + content: text.content.to_owned(), + font: text.font, + horizontal_alignment: text.horizontal_alignment, + vertical_alignment: text.vertical_alignment, + shaping: text.shaping, + bounds: text.bounds, + min_bounds, + })) + } + + pub fn buffer(&self) -> &cosmic_text::Buffer { + &self.0.buffer + } + + pub fn downgrade(&self) -> Weak { + Weak { + raw: Arc::downgrade(&self.0), + min_bounds: self.0.min_bounds, + horizontal_alignment: self.0.horizontal_alignment, + vertical_alignment: self.0.vertical_alignment, + } + } + + pub fn resize(&mut self, new_bounds: Size, font_system: &FontSystem) { + if let Some(internal) = Arc::get_mut(&mut self.0) { + // If there is no strong reference holding on to the paragraph, we + // resize the buffer in-place + internal.buffer.set_size( + &mut font_system.write(), + new_bounds.width, + new_bounds.height, + ); + + internal.bounds = new_bounds; + internal.min_bounds = text::measure(&internal.buffer); + } else { + let metrics = self.0.buffer.metrics(); + + // If there is a strong reference somewhere, we recompute the buffer + // from scratch + *self = Self::with_text( + Text { + content: &self.0.content, + bounds: self.0.bounds, + size: Pixels(metrics.font_size), + line_height: LineHeight::Absolute(Pixels( + metrics.line_height, + )), + font: self.0.font, + horizontal_alignment: self.0.horizontal_alignment, + vertical_alignment: self.0.vertical_alignment, + shaping: self.0.shaping, + }, + font_system, + ); + } + } +} + +impl core::text::Paragraph for Paragraph { + type Font = Font; + + fn content(&self) -> &str { + &self.0.content + } + + fn text_size(&self) -> Pixels { + Pixels(self.0.buffer.metrics().font_size) + } + + fn line_height(&self) -> LineHeight { + LineHeight::Absolute(Pixels(self.0.buffer.metrics().line_height)) + } + + fn font(&self) -> Font { + self.0.font + } + + fn shaping(&self) -> Shaping { + self.0.shaping + } + + fn horizontal_alignment(&self) -> alignment::Horizontal { + self.0.horizontal_alignment + } + + fn vertical_alignment(&self) -> alignment::Vertical { + self.0.vertical_alignment + } + + fn bounds(&self) -> Size { + self.0.bounds + } + + fn min_bounds(&self) -> Size { + self.0.min_bounds + } + + fn hit_test(&self, point: Point) -> Option { + let cursor = self.0.buffer.hit(point.x, point.y)?; + + Some(Hit::CharOffset(cursor.index)) + } + + fn grapheme_position(&self, line: usize, index: usize) -> Option { + let run = self.0.buffer.layout_runs().nth(line)?; + + // TODO: Index represents a grapheme, not a glyph + let glyph = run.glyphs.get(index).or_else(|| run.glyphs.last())?; + + let advance_last = if index == run.glyphs.len() { + glyph.w + } else { + 0.0 + }; + + Some(Point::new( + glyph.x + glyph.x_offset * glyph.font_size + advance_last, + glyph.y - glyph.y_offset * glyph.font_size, + )) + } +} + +impl PartialEq for Internal { + fn eq(&self, other: &Self) -> bool { + self.content == other.content + && self.font == other.font + && self.shaping == other.shaping + && self.horizontal_alignment == other.horizontal_alignment + && self.vertical_alignment == other.vertical_alignment + && self.bounds == other.bounds + && self.min_bounds == other.min_bounds + && self.buffer.metrics() == other.buffer.metrics() + } +} + +impl Default for Internal { + fn default() -> Self { + Self { + buffer: cosmic_text::Buffer::new_empty(cosmic_text::Metrics { + font_size: 1.0, + line_height: 1.0, + }), + content: String::new(), + font: Font::default(), + shaping: Shaping::default(), + horizontal_alignment: alignment::Horizontal::Left, + vertical_alignment: alignment::Vertical::Top, + bounds: Size::ZERO, + min_bounds: Size::ZERO, + } + } +} + +impl fmt::Debug for Paragraph { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Paragraph") + .field("content", &self.0.content) + .field("font", &self.0.font) + .field("shaping", &self.0.shaping) + .field("horizontal_alignment", &self.0.horizontal_alignment) + .field("vertical_alignment", &self.0.vertical_alignment) + .field("bounds", &self.0.bounds) + .field("min_bounds", &self.0.min_bounds) + .finish() + } +} + +#[derive(Debug, Clone)] +pub struct Weak { + raw: sync::Weak, + pub min_bounds: Size, + pub horizontal_alignment: alignment::Horizontal, + pub vertical_alignment: alignment::Vertical, +} + +impl Weak { + pub fn upgrade(&self) -> Option { + self.raw.upgrade().map(Paragraph) + } +} + +impl PartialEq for Weak { + fn eq(&self, other: &Self) -> bool { + match (self.raw.upgrade(), other.raw.upgrade()) { + (Some(p1), Some(p2)) => p1 == p2, + _ => false, + } + } +} -- cgit From 89acf0217e0acd92a82bff1fd516cd4266c0878a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 30 Aug 2023 05:06:08 +0200 Subject: Use `min_bounds` for cached text --- graphics/src/text/cache.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'graphics') diff --git a/graphics/src/text/cache.rs b/graphics/src/text/cache.rs index 8aea6715..9e4fbf65 100644 --- a/graphics/src/text/cache.rs +++ b/graphics/src/text/cache.rs @@ -8,7 +8,7 @@ use std::hash::{BuildHasher, Hash, Hasher}; #[allow(missing_debug_implementations)] #[derive(Default)] pub struct Cache { - entries: FxHashMap, + entries: FxHashMap, aliases: FxHashMap, recently_used: FxHashSet, hasher: HashBuilder, @@ -25,7 +25,7 @@ impl Cache { Self::default() } - pub fn get(&self, key: &KeyHash) -> Option<&cosmic_text::Buffer> { + pub fn get(&self, key: &KeyHash) -> Option<&Entry> { self.entries.get(key) } @@ -33,7 +33,7 @@ impl Cache { &mut self, font_system: &mut cosmic_text::FontSystem, key: Key<'_>, - ) -> (KeyHash, &mut cosmic_text::Buffer) { + ) -> (KeyHash, &mut Entry) { let hash = key.hash(self.hasher.build_hasher()); if let Some(hash) = self.aliases.get(&hash) { @@ -59,7 +59,10 @@ impl Cache { ); let bounds = text::measure(&buffer); - let _ = entry.insert(buffer); + let _ = entry.insert(Entry { + buffer, + min_bounds: bounds, + }); for bounds in [ bounds, @@ -118,3 +121,9 @@ impl Key<'_> { } pub type KeyHash = u64; + +#[allow(missing_debug_implementations)] +pub struct Entry { + pub buffer: cosmic_text::Buffer, + pub min_bounds: Size, +} -- cgit From ce22d661fa2f4b50d2e7d9ba6bd07e625ec6789e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 1 Sep 2023 03:25:29 +0200 Subject: Remove `Clone` implementation for `Paragraph` --- graphics/src/text/paragraph.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index 7b70376a..f04b1e69 100644 --- a/graphics/src/text/paragraph.rs +++ b/graphics/src/text/paragraph.rs @@ -7,7 +7,7 @@ use crate::text::{self, FontSystem}; use std::fmt; use std::sync::{self, Arc}; -#[derive(Clone, PartialEq, Default)] +#[derive(PartialEq, Default)] pub struct Paragraph(Arc); struct Internal { -- cgit From 935c722a278071d3bcd0bf499a0ef8f687ed51ad Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 1 Sep 2023 03:59:57 +0200 Subject: Use `Arc::try_unwrap` in `Paragraph` We use `MaybeUninit` here instead of `Option` to save some cycles, but I will most likely change it for an `Option` since unsafe code is quite scary. --- graphics/src/text/paragraph.rs | 154 +++++++++++++++++++++++++---------------- 1 file changed, 95 insertions(+), 59 deletions(-) (limited to 'graphics') diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index f04b1e69..c6921005 100644 --- a/graphics/src/text/paragraph.rs +++ b/graphics/src/text/paragraph.rs @@ -5,10 +5,10 @@ use crate::core::{Font, Pixels, Point, Size}; use crate::text::{self, FontSystem}; use std::fmt; +use std::mem::MaybeUninit; use std::sync::{self, Arc}; -#[derive(PartialEq, Default)] -pub struct Paragraph(Arc); +pub struct Paragraph(MaybeUninit>); struct Internal { buffer: cosmic_text::Buffer, @@ -21,6 +21,12 @@ struct Internal { min_bounds: Size, } +impl Default for Paragraph { + fn default() -> Self { + Self(MaybeUninit::new(Arc::new(Internal::default()))) + } +} + impl Paragraph { pub fn new() -> Self { Self::default() @@ -52,7 +58,7 @@ impl Paragraph { let min_bounds = text::measure(&buffer); - Self(Arc::new(Internal { + Self(MaybeUninit::new(Arc::new(Internal { buffer, content: text.content.to_owned(), font: text.font, @@ -61,54 +67,80 @@ impl Paragraph { shaping: text.shaping, bounds: text.bounds, min_bounds, - })) + }))) } pub fn buffer(&self) -> &cosmic_text::Buffer { - &self.0.buffer + #[allow(unsafe_code)] + &unsafe { self.0.assume_init_ref() }.buffer } pub fn downgrade(&self) -> Weak { + #[allow(unsafe_code)] + let paragraph = unsafe { self.0.assume_init_ref() }; + Weak { - raw: Arc::downgrade(&self.0), - min_bounds: self.0.min_bounds, - horizontal_alignment: self.0.horizontal_alignment, - vertical_alignment: self.0.vertical_alignment, + raw: Arc::downgrade(paragraph), + min_bounds: paragraph.min_bounds, + horizontal_alignment: paragraph.horizontal_alignment, + vertical_alignment: paragraph.vertical_alignment, } } pub fn resize(&mut self, new_bounds: Size, font_system: &FontSystem) { - if let Some(internal) = Arc::get_mut(&mut self.0) { - // If there is no strong reference holding on to the paragraph, we - // resize the buffer in-place - internal.buffer.set_size( - &mut font_system.write(), - new_bounds.width, - new_bounds.height, - ); - - internal.bounds = new_bounds; - internal.min_bounds = text::measure(&internal.buffer); - } else { - let metrics = self.0.buffer.metrics(); - - // If there is a strong reference somewhere, we recompute the buffer - // from scratch - *self = Self::with_text( - Text { - content: &self.0.content, - bounds: self.0.bounds, - size: Pixels(metrics.font_size), - line_height: LineHeight::Absolute(Pixels( - metrics.line_height, - )), - font: self.0.font, - horizontal_alignment: self.0.horizontal_alignment, - vertical_alignment: self.0.vertical_alignment, - shaping: self.0.shaping, - }, - font_system, - ); + // Place uninit for now, we always write to `self.0` in the end + let paragraph = std::mem::replace(&mut self.0, MaybeUninit::uninit()); + + // Mutable self guarantees unique access and `uninit` call only happens + // in this method. + #[allow(unsafe_code)] + let paragraph = unsafe { paragraph.assume_init() }; + + match Arc::try_unwrap(paragraph) { + Ok(mut internal) => { + internal.buffer.set_size( + &mut font_system.write(), + new_bounds.width, + new_bounds.height, + ); + + internal.bounds = new_bounds; + internal.min_bounds = text::measure(&internal.buffer); + + let _ = self.0.write(Arc::new(internal)); + } + Err(internal) => { + let metrics = internal.buffer.metrics(); + + // If there is a strong reference somewhere, we recompute the + // buffer from scratch + let new_paragraph = Self::with_text( + Text { + content: &internal.content, + bounds: internal.bounds, + size: Pixels(metrics.font_size), + line_height: LineHeight::Absolute(Pixels( + metrics.line_height, + )), + font: internal.font, + horizontal_alignment: internal.horizontal_alignment, + vertical_alignment: internal.vertical_alignment, + shaping: internal.shaping, + }, + font_system, + ); + + // New paragraph should always be initialized + #[allow(unsafe_code)] + let _ = self.0.write(unsafe { new_paragraph.0.assume_init() }); + } + } + } + + fn internal_ref(&self) -> &Internal { + #[allow(unsafe_code)] + unsafe { + self.0.assume_init_ref() } } } @@ -117,49 +149,51 @@ impl core::text::Paragraph for Paragraph { type Font = Font; fn content(&self) -> &str { - &self.0.content + &self.internal_ref().content } fn text_size(&self) -> Pixels { - Pixels(self.0.buffer.metrics().font_size) + Pixels(self.internal_ref().buffer.metrics().font_size) } fn line_height(&self) -> LineHeight { - LineHeight::Absolute(Pixels(self.0.buffer.metrics().line_height)) + LineHeight::Absolute(Pixels( + self.internal_ref().buffer.metrics().line_height, + )) } fn font(&self) -> Font { - self.0.font + self.internal_ref().font } fn shaping(&self) -> Shaping { - self.0.shaping + self.internal_ref().shaping } fn horizontal_alignment(&self) -> alignment::Horizontal { - self.0.horizontal_alignment + self.internal_ref().horizontal_alignment } fn vertical_alignment(&self) -> alignment::Vertical { - self.0.vertical_alignment + self.internal_ref().vertical_alignment } fn bounds(&self) -> Size { - self.0.bounds + self.internal_ref().bounds } fn min_bounds(&self) -> Size { - self.0.min_bounds + self.internal_ref().min_bounds } fn hit_test(&self, point: Point) -> Option { - let cursor = self.0.buffer.hit(point.x, point.y)?; + let cursor = self.internal_ref().buffer.hit(point.x, point.y)?; Some(Hit::CharOffset(cursor.index)) } fn grapheme_position(&self, line: usize, index: usize) -> Option { - let run = self.0.buffer.layout_runs().nth(line)?; + let run = self.internal_ref().buffer.layout_runs().nth(line)?; // TODO: Index represents a grapheme, not a glyph let glyph = run.glyphs.get(index).or_else(|| run.glyphs.last())?; @@ -210,14 +244,16 @@ impl Default for Internal { impl fmt::Debug for Paragraph { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let paragraph = self.internal_ref(); + f.debug_struct("Paragraph") - .field("content", &self.0.content) - .field("font", &self.0.font) - .field("shaping", &self.0.shaping) - .field("horizontal_alignment", &self.0.horizontal_alignment) - .field("vertical_alignment", &self.0.vertical_alignment) - .field("bounds", &self.0.bounds) - .field("min_bounds", &self.0.min_bounds) + .field("content", ¶graph.content) + .field("font", ¶graph.font) + .field("shaping", ¶graph.shaping) + .field("horizontal_alignment", ¶graph.horizontal_alignment) + .field("vertical_alignment", ¶graph.vertical_alignment) + .field("bounds", ¶graph.bounds) + .field("min_bounds", ¶graph.min_bounds) .finish() } } @@ -232,7 +268,7 @@ pub struct Weak { impl Weak { pub fn upgrade(&self) -> Option { - self.raw.upgrade().map(Paragraph) + self.raw.upgrade().map(MaybeUninit::new).map(Paragraph) } } -- cgit From 51e69d7040c943aad8453bc03031c75b4cb302bb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 1 Sep 2023 04:04:15 +0200 Subject: Replace `MaybeUninit` with `Option` in `paragraph` --- graphics/src/text/paragraph.rs | 72 ++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 44 deletions(-) (limited to 'graphics') diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index c6921005..8e8907f9 100644 --- a/graphics/src/text/paragraph.rs +++ b/graphics/src/text/paragraph.rs @@ -5,10 +5,10 @@ use crate::core::{Font, Pixels, Point, Size}; use crate::text::{self, FontSystem}; use std::fmt; -use std::mem::MaybeUninit; use std::sync::{self, Arc}; -pub struct Paragraph(MaybeUninit>); +#[derive(Clone, PartialEq, Default)] +pub struct Paragraph(Option>); struct Internal { buffer: cosmic_text::Buffer, @@ -21,12 +21,6 @@ struct Internal { min_bounds: Size, } -impl Default for Paragraph { - fn default() -> Self { - Self(MaybeUninit::new(Arc::new(Internal::default()))) - } -} - impl Paragraph { pub fn new() -> Self { Self::default() @@ -58,7 +52,7 @@ impl Paragraph { let min_bounds = text::measure(&buffer); - Self(MaybeUninit::new(Arc::new(Internal { + Self(Some(Arc::new(Internal { buffer, content: text.content.to_owned(), font: text.font, @@ -71,13 +65,11 @@ impl Paragraph { } pub fn buffer(&self) -> &cosmic_text::Buffer { - #[allow(unsafe_code)] - &unsafe { self.0.assume_init_ref() }.buffer + &self.internal().buffer } pub fn downgrade(&self) -> Weak { - #[allow(unsafe_code)] - let paragraph = unsafe { self.0.assume_init_ref() }; + let paragraph = self.internal(); Weak { raw: Arc::downgrade(paragraph), @@ -88,13 +80,10 @@ impl Paragraph { } pub fn resize(&mut self, new_bounds: Size, font_system: &FontSystem) { - // Place uninit for now, we always write to `self.0` in the end - let paragraph = std::mem::replace(&mut self.0, MaybeUninit::uninit()); - - // Mutable self guarantees unique access and `uninit` call only happens - // in this method. - #[allow(unsafe_code)] - let paragraph = unsafe { paragraph.assume_init() }; + let paragraph = self + .0 + .take() + .expect("paragraph should always be initialized"); match Arc::try_unwrap(paragraph) { Ok(mut internal) => { @@ -107,14 +96,14 @@ impl Paragraph { internal.bounds = new_bounds; internal.min_bounds = text::measure(&internal.buffer); - let _ = self.0.write(Arc::new(internal)); + self.0 = Some(Arc::new(internal)); } Err(internal) => { let metrics = internal.buffer.metrics(); // If there is a strong reference somewhere, we recompute the // buffer from scratch - let new_paragraph = Self::with_text( + *self = Self::with_text( Text { content: &internal.content, bounds: internal.bounds, @@ -129,19 +118,14 @@ impl Paragraph { }, font_system, ); - - // New paragraph should always be initialized - #[allow(unsafe_code)] - let _ = self.0.write(unsafe { new_paragraph.0.assume_init() }); } } } - fn internal_ref(&self) -> &Internal { - #[allow(unsafe_code)] - unsafe { - self.0.assume_init_ref() - } + fn internal(&self) -> &Arc { + self.0 + .as_ref() + .expect("paragraph should always be initialized") } } @@ -149,51 +133,51 @@ impl core::text::Paragraph for Paragraph { type Font = Font; fn content(&self) -> &str { - &self.internal_ref().content + &self.internal().content } fn text_size(&self) -> Pixels { - Pixels(self.internal_ref().buffer.metrics().font_size) + Pixels(self.internal().buffer.metrics().font_size) } fn line_height(&self) -> LineHeight { LineHeight::Absolute(Pixels( - self.internal_ref().buffer.metrics().line_height, + self.internal().buffer.metrics().line_height, )) } fn font(&self) -> Font { - self.internal_ref().font + self.internal().font } fn shaping(&self) -> Shaping { - self.internal_ref().shaping + self.internal().shaping } fn horizontal_alignment(&self) -> alignment::Horizontal { - self.internal_ref().horizontal_alignment + self.internal().horizontal_alignment } fn vertical_alignment(&self) -> alignment::Vertical { - self.internal_ref().vertical_alignment + self.internal().vertical_alignment } fn bounds(&self) -> Size { - self.internal_ref().bounds + self.internal().bounds } fn min_bounds(&self) -> Size { - self.internal_ref().min_bounds + self.internal().min_bounds } fn hit_test(&self, point: Point) -> Option { - let cursor = self.internal_ref().buffer.hit(point.x, point.y)?; + let cursor = self.internal().buffer.hit(point.x, point.y)?; Some(Hit::CharOffset(cursor.index)) } fn grapheme_position(&self, line: usize, index: usize) -> Option { - let run = self.internal_ref().buffer.layout_runs().nth(line)?; + let run = self.internal().buffer.layout_runs().nth(line)?; // TODO: Index represents a grapheme, not a glyph let glyph = run.glyphs.get(index).or_else(|| run.glyphs.last())?; @@ -244,7 +228,7 @@ impl Default for Internal { impl fmt::Debug for Paragraph { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let paragraph = self.internal_ref(); + let paragraph = self.internal(); f.debug_struct("Paragraph") .field("content", ¶graph.content) @@ -268,7 +252,7 @@ pub struct Weak { impl Weak { pub fn upgrade(&self) -> Option { - self.raw.upgrade().map(MaybeUninit::new).map(Paragraph) + self.raw.upgrade().map(Some).map(Paragraph) } } -- cgit From 6758de2b4348d8990205721027524cf87a9db128 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 1 Sep 2023 04:14:06 +0200 Subject: Fix `Default` implementation for `Paragraph` --- graphics/src/text/paragraph.rs | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'graphics') diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index 8e8907f9..d99b8412 100644 --- a/graphics/src/text/paragraph.rs +++ b/graphics/src/text/paragraph.rs @@ -7,7 +7,7 @@ use crate::text::{self, FontSystem}; use std::fmt; use std::sync::{self, Arc}; -#[derive(Clone, PartialEq, Default)] +#[derive(Clone, PartialEq)] pub struct Paragraph(Option>); struct Internal { @@ -195,6 +195,28 @@ impl core::text::Paragraph for Paragraph { } } +impl Default for Paragraph { + fn default() -> Self { + Self(Some(Arc::new(Internal::default()))) + } +} + +impl fmt::Debug for Paragraph { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let paragraph = self.internal(); + + f.debug_struct("Paragraph") + .field("content", ¶graph.content) + .field("font", ¶graph.font) + .field("shaping", ¶graph.shaping) + .field("horizontal_alignment", ¶graph.horizontal_alignment) + .field("vertical_alignment", ¶graph.vertical_alignment) + .field("bounds", ¶graph.bounds) + .field("min_bounds", ¶graph.min_bounds) + .finish() + } +} + impl PartialEq for Internal { fn eq(&self, other: &Self) -> bool { self.content == other.content @@ -226,22 +248,6 @@ impl Default for Internal { } } -impl fmt::Debug for Paragraph { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let paragraph = self.internal(); - - f.debug_struct("Paragraph") - .field("content", ¶graph.content) - .field("font", ¶graph.font) - .field("shaping", ¶graph.shaping) - .field("horizontal_alignment", ¶graph.horizontal_alignment) - .field("vertical_alignment", ¶graph.vertical_alignment) - .field("bounds", ¶graph.bounds) - .field("min_bounds", ¶graph.min_bounds) - .finish() - } -} - #[derive(Debug, Clone)] pub struct Weak { raw: sync::Weak, -- cgit From 34495bba1c1ffaa4ea2bab46103b5d66e333c51e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 4 Sep 2023 02:55:09 +0200 Subject: Introduce `keyed::Column` widget --- graphics/src/text/paragraph.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'graphics') diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index d99b8412..ee7c04c8 100644 --- a/graphics/src/text/paragraph.rs +++ b/graphics/src/text/paragraph.rs @@ -27,6 +27,8 @@ impl Paragraph { } pub fn with_text(text: Text<'_, Font>, font_system: &FontSystem) -> Self { + log::trace!("\nAllocating paragraph: {}", text.content); + let mut font_system = font_system.write(); let mut buffer = cosmic_text::Buffer::new( -- cgit From 3450987355be7fe029db112474d06613929b54c7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 9 Sep 2023 11:21:32 +0200 Subject: Invalidate existing paragraphs when new fonts are loaded --- graphics/src/renderer.rs | 23 ++++++++++++++++++++++ graphics/src/text.rs | 44 ++++++++++++++++++++++++++++++++---------- graphics/src/text/paragraph.rs | 15 +++++++++++--- 3 files changed, 69 insertions(+), 13 deletions(-) (limited to 'graphics') diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index f93f4a6d..d4df29a5 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -162,6 +162,29 @@ where text::Paragraph::with_text(text, self.backend.font_system()) } + fn update_paragraph( + &self, + paragraph: &mut Self::Paragraph, + text: Text<'_, Self::Font>, + ) { + let font_system = self.backend.font_system(); + + if paragraph.version() != font_system.version() { + // The font system has changed, paragraph fonts may be outdated + *paragraph = self.create_paragraph(text); + } else { + match core::text::compare(paragraph, text) { + core::text::Difference::None => {} + core::text::Difference::Bounds => { + self.resize_paragraph(paragraph, text.bounds); + } + core::text::Difference::Shape => { + *paragraph = self.create_paragraph(text); + } + } + } + } + fn resize_paragraph( &self, paragraph: &mut Self::Paragraph, diff --git a/graphics/src/text.rs b/graphics/src/text.rs index bbe9d7cb..bc06aa3c 100644 --- a/graphics/src/text.rs +++ b/graphics/src/text.rs @@ -10,30 +10,54 @@ use crate::core::font::{self, Font}; use crate::core::text::Shaping; use crate::core::Size; +use std::borrow::Cow; use std::sync::{self, Arc, RwLock}; #[allow(missing_debug_implementations)] -pub struct FontSystem(RwLock); +pub struct FontSystem { + raw: RwLock, + version: Version, +} impl FontSystem { pub fn new() -> Self { - FontSystem(RwLock::new(cosmic_text::FontSystem::new_with_fonts( - [cosmic_text::fontdb::Source::Binary(Arc::new( - include_bytes!("../fonts/Iced-Icons.ttf").as_slice(), - ))] - .into_iter(), - ))) + FontSystem { + raw: RwLock::new(cosmic_text::FontSystem::new_with_fonts( + [cosmic_text::fontdb::Source::Binary(Arc::new( + include_bytes!("../fonts/Iced-Icons.ttf").as_slice(), + ))] + .into_iter(), + )), + version: Version::default(), + } } pub fn get_mut(&mut self) -> &mut cosmic_text::FontSystem { - self.0.get_mut().expect("Lock font system") + self.raw.get_mut().expect("Lock font system") + } + + pub fn write( + &self, + ) -> (sync::RwLockWriteGuard<'_, cosmic_text::FontSystem>, Version) { + (self.raw.write().expect("Write font system"), self.version) } - pub fn write(&self) -> sync::RwLockWriteGuard<'_, cosmic_text::FontSystem> { - self.0.write().expect("Write font system") + pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) { + let _ = self.get_mut().db_mut().load_font_source( + cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())), + ); + + self.version = Version(self.version.0 + 1); + } + + pub fn version(&self) -> Version { + self.version } } +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +pub struct Version(u32); + impl Default for FontSystem { fn default() -> Self { Self::new() diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index ee7c04c8..cd12bc8f 100644 --- a/graphics/src/text/paragraph.rs +++ b/graphics/src/text/paragraph.rs @@ -19,6 +19,7 @@ struct Internal { vertical_alignment: alignment::Vertical, bounds: Size, min_bounds: Size, + version: text::Version, } impl Paragraph { @@ -27,9 +28,9 @@ impl Paragraph { } pub fn with_text(text: Text<'_, Font>, font_system: &FontSystem) -> Self { - log::trace!("\nAllocating paragraph: {}", text.content); + log::trace!("Allocating paragraph: {}", text.content); - let mut font_system = font_system.write(); + let (mut font_system, version) = font_system.write(); let mut buffer = cosmic_text::Buffer::new( &mut font_system, @@ -63,6 +64,7 @@ impl Paragraph { shaping: text.shaping, bounds: text.bounds, min_bounds, + version, }))) } @@ -70,6 +72,10 @@ impl Paragraph { &self.internal().buffer } + pub fn version(&self) -> text::Version { + self.internal().version + } + pub fn downgrade(&self) -> Weak { let paragraph = self.internal(); @@ -89,8 +95,10 @@ impl Paragraph { match Arc::try_unwrap(paragraph) { Ok(mut internal) => { + let (mut font_system, _) = font_system.write(); + internal.buffer.set_size( - &mut font_system.write(), + &mut font_system, new_bounds.width, new_bounds.height, ); @@ -246,6 +254,7 @@ impl Default for Internal { vertical_alignment: alignment::Vertical::Top, bounds: Size::ZERO, min_bounds: Size::ZERO, + version: text::Version::default(), } } } -- cgit From bbb9c2d92819f2ff96e51773037138fef488290a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 9 Sep 2023 23:05:44 +0200 Subject: Count grapheme clusters in `Paragraph::grapheme_position` --- graphics/src/text/paragraph.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'graphics') diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index cd12bc8f..e4350cff 100644 --- a/graphics/src/text/paragraph.rs +++ b/graphics/src/text/paragraph.rs @@ -189,8 +189,27 @@ impl core::text::Paragraph for Paragraph { fn grapheme_position(&self, line: usize, index: usize) -> Option { let run = self.internal().buffer.layout_runs().nth(line)?; - // TODO: Index represents a grapheme, not a glyph - let glyph = run.glyphs.get(index).or_else(|| run.glyphs.last())?; + // index represents a grapheme, not a glyph + // Let's find the first glyph for the given grapheme cluster + let mut last_start = None; + let mut graphemes_seen = 0; + + let glyph = run + .glyphs + .iter() + .find(|glyph| { + if graphemes_seen == index { + return true; + } + + if Some(glyph.start) != last_start { + last_start = Some(glyph.start); + graphemes_seen += 1; + } + + false + }) + .or_else(|| run.glyphs.last())?; let advance_last = if index == run.glyphs.len() { glyph.w -- cgit From 956512338905bac0b156fdaf16fe3c3e07e97a84 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 9 Sep 2023 23:06:06 +0200 Subject: Remove unused `unicode-segmentation` dependency from `iced_graphics` --- graphics/Cargo.toml | 1 - 1 file changed, 1 deletion(-) (limited to 'graphics') diff --git a/graphics/Cargo.toml b/graphics/Cargo.toml index 442eb007..6fc6ab00 100644 --- a/graphics/Cargo.toml +++ b/graphics/Cargo.toml @@ -25,7 +25,6 @@ thiserror = "1.0" bitflags = "1.2" cosmic-text = "0.9" rustc-hash = "1.1" -unicode-segmentation = "1.6" [dependencies.bytemuck] version = "1.4" -- cgit From b42b24b79a097aab10006490cd2ec8a2332fd5d0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 10 Sep 2023 00:55:46 +0200 Subject: Fix (more) broken intradoc links Good thing I just set up CI earlier for this :sweat_smile: --- graphics/src/backend.rs | 2 +- graphics/src/primitive.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'graphics') diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index 6774b9ca..c2ac82ba 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -16,7 +16,7 @@ pub trait Backend { /// A graphics backend that supports text rendering. pub trait Text { - /// Loads a [`Font`] from its bytes. + /// Loads a font from its bytes. fn load_font(&mut self, font: Cow<'static, [u8]>); /// Returns the [`cosmic_text::FontSystem`] of the [`Backend`]. diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index cdc8923e..8a97e6e7 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -34,11 +34,11 @@ pub enum Primitive { }, /// A paragraph primitive Paragraph { - /// The [`Paragraph`]. + /// The [`paragraph::Weak`] reference. paragraph: paragraph::Weak, - /// The position of the [`Paragraph`]. + /// The position of the paragraph. position: Point, - /// The color of the [`Paragraph`]. + /// The color of the paragraph. color: Color, }, /// A quad primitive -- cgit