diff options
author | 2023-11-29 22:28:31 +0100 | |
---|---|---|
committer | 2023-11-29 22:28:31 +0100 | |
commit | e09b4e24dda51b8212d8ece52431dacaa3922a7b (patch) | |
tree | 7005e181528134ebdde5bbbe5909273db9f30174 /graphics | |
parent | 83c7870c569a2976923ee6243a19813094d44673 (diff) | |
parent | 7f8b17604a31e00becc43130ec516c1a53552c88 (diff) | |
download | iced-e09b4e24dda51b8212d8ece52431dacaa3922a7b.tar.gz iced-e09b4e24dda51b8212d8ece52431dacaa3922a7b.tar.bz2 iced-e09b4e24dda51b8212d8ece52431dacaa3922a7b.zip |
Merge branch 'master' into feat/multi-window-support
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/Cargo.toml | 75 | ||||
-rw-r--r-- | graphics/fonts/Iced-Icons.ttf | bin | 0 -> 5108 bytes | |||
-rw-r--r-- | graphics/src/backend.rs | 67 | ||||
-rw-r--r-- | graphics/src/compositor.rs | 4 | ||||
-rw-r--r-- | graphics/src/damage.rs | 33 | ||||
-rw-r--r-- | graphics/src/geometry.rs | 4 | ||||
-rw-r--r-- | graphics/src/geometry/fill.rs | 4 | ||||
-rw-r--r-- | graphics/src/geometry/path/arc.rs | 12 | ||||
-rw-r--r-- | graphics/src/geometry/path/builder.rs | 2 | ||||
-rw-r--r-- | graphics/src/geometry/stroke.rs | 4 | ||||
-rw-r--r-- | graphics/src/geometry/text.rs | 6 | ||||
-rw-r--r-- | graphics/src/gradient.rs | 15 | ||||
-rw-r--r-- | graphics/src/image.rs | 2 | ||||
-rw-r--r-- | graphics/src/lib.rs | 10 | ||||
-rw-r--r-- | graphics/src/mesh.rs | 2 | ||||
-rw-r--r-- | graphics/src/primitive.rs | 26 | ||||
-rw-r--r-- | graphics/src/renderer.rs | 152 | ||||
-rw-r--r-- | graphics/src/text.rs | 156 | ||||
-rw-r--r-- | graphics/src/text/cache.rs | 147 | ||||
-rw-r--r-- | graphics/src/text/editor.rs | 779 | ||||
-rw-r--r-- | graphics/src/text/paragraph.rs | 307 |
21 files changed, 1591 insertions, 216 deletions
diff --git a/graphics/Cargo.toml b/graphics/Cargo.toml index 15d26346..6741d7cf 100644 --- a/graphics/Cargo.toml +++ b/graphics/Cargo.toml @@ -1,14 +1,18 @@ [package] name = "iced_graphics" -version = "0.8.0" -authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"] -edition = "2021" -description = "A bunch of backend-agnostic types that can be leveraged to build a renderer for Iced" -license = "MIT" -repository = "https://github.com/iced-rs/iced" -documentation = "https://docs.rs/iced_graphics" -keywords = ["gui", "ui", "graphics", "interface", "widgets"] -categories = ["gui"] +description = "A bunch of backend-agnostic types that can be leveraged to build a renderer for iced" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +homepage.workspace = true +categories.workspace = true +keywords.workspace = true + +[package.metadata.docs.rs] +rustdoc-args = ["--cfg", "docsrs"] +all-features = true [features] geometry = ["lyon_path"] @@ -16,33 +20,26 @@ image = ["dep:image", "kamadak-exif"] web-colors = [] [dependencies] -glam = "0.24" -half = "2.2.1" -log = "0.4" -raw-window-handle = "0.5" -thiserror = "1.0" -bitflags = "1.2" - -[dependencies.bytemuck] -version = "1.4" -features = ["derive"] - -[dependencies.iced_core] -version = "0.9" -path = "../core" - -[dependencies.image] -version = "0.24" -optional = true - -[dependencies.kamadak-exif] -version = "0.5" -optional = true - -[dependencies.lyon_path] -version = "1" -optional = true - -[package.metadata.docs.rs] -rustdoc-args = ["--cfg", "docsrs"] -all-features = true +iced_core.workspace = true + +bitflags.workspace = true +bytemuck.workspace = true +cosmic-text.workspace = true +glam.workspace = true +half.workspace = true +log.workspace = true +once_cell.workspace = true +raw-window-handle.workspace = true +rustc-hash.workspace = true +thiserror.workspace = true +unicode-segmentation.workspace = true +xxhash-rust.workspace = true + +image.workspace = true +image.optional = true + +kamadak-exif.workspace = true +kamadak-exif.optional = true + +lyon_path.workspace = true +lyon_path.optional = true diff --git a/graphics/fonts/Iced-Icons.ttf b/graphics/fonts/Iced-Icons.ttf Binary files differnew file mode 100644 index 00000000..e3273141 --- /dev/null +++ b/graphics/fonts/Iced-Icons.ttf diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index 59e95bf8..10eb337f 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -1,8 +1,7 @@ //! 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 std::borrow::Cow; @@ -12,69 +11,11 @@ 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<text::Hit>; - - /// Loads a [`Font`] from its bytes. + /// Loads a font from its bytes. fn load_font(&mut self, font: Cow<'static, [u8]>); } diff --git a/graphics/src/compositor.rs b/graphics/src/compositor.rs index 32111008..e0b1e20f 100644 --- a/graphics/src/compositor.rs +++ b/graphics/src/compositor.rs @@ -64,9 +64,9 @@ pub trait Compositor: Sized { ) -> Result<(), SurfaceError>; /// Screenshots the current [`Renderer`] primitives to an offscreen texture, and returns the bytes of - /// the texture ordered as `RGBA` in the sRGB color space. + /// the texture ordered as `RGBA` in the `sRGB` color space. /// - /// [`Renderer`]: Self::Renderer; + /// [`Renderer`]: Self::Renderer fn screenshot<T: AsRef<str>>( &mut self, renderer: &mut Self::Renderer, diff --git a/graphics/src/damage.rs b/graphics/src/damage.rs index 2f29956e..595cc274 100644 --- a/graphics/src/damage.rs +++ b/graphics/src/damage.rs @@ -40,6 +40,39 @@ impl<T: Damage> Damage for Primitive<T> { 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::Editor { + editor, position, .. + } => { + let bounds = Rectangle::new(*position, editor.bounds); + + bounds.expand(1.5) + } Self::Quad { bounds, .. } | Self::Image { bounds, .. } | Self::Svg { bounds, .. } => bounds.expand(1.0), diff --git a/graphics/src/geometry.rs b/graphics/src/geometry.rs index 7cd3dd3a..d7d6a0aa 100644 --- a/graphics/src/geometry.rs +++ b/graphics/src/geometry.rs @@ -14,11 +14,11 @@ pub use text::Text; pub use crate::gradient::{self, Gradient}; -/// A renderer capable of drawing some [`Geometry`]. +/// A renderer capable of drawing some [`Self::Geometry`]. pub trait Renderer: crate::core::Renderer { /// The kind of geometry this renderer can draw. type Geometry; - /// Draws the given layers of [`Geometry`]. + /// Draws the given layers of [`Self::Geometry`]. fn draw(&mut self, layers: Vec<Self::Geometry>); } diff --git a/graphics/src/geometry/fill.rs b/graphics/src/geometry/fill.rs index b773c99b..670fbc12 100644 --- a/graphics/src/geometry/fill.rs +++ b/graphics/src/geometry/fill.rs @@ -1,4 +1,6 @@ -//! Fill [crate::widget::canvas::Geometry] with a certain style. +//! Fill [`Geometry`] with a certain style. +//! +//! [`Geometry`]: super::Renderer::Geometry pub use crate::geometry::Style; use crate::core::Color; diff --git a/graphics/src/geometry/path/arc.rs b/graphics/src/geometry/path/arc.rs index 2cdebb66..dd4fcf33 100644 --- a/graphics/src/geometry/path/arc.rs +++ b/graphics/src/geometry/path/arc.rs @@ -8,9 +8,9 @@ pub struct Arc { pub center: Point, /// The radius of the arc. pub radius: f32, - /// The start of the segment's angle, clockwise rotation. + /// The start of the segment's angle in radians, clockwise rotation from positive x-axis. pub start_angle: f32, - /// The end of the segment's angle, clockwise rotation. + /// The end of the segment's angle in radians, clockwise rotation from positive x-axis. pub end_angle: f32, } @@ -19,13 +19,13 @@ pub struct Arc { pub struct Elliptical { /// The center of the arc. pub center: Point, - /// The radii of the arc's ellipse, defining its axes. + /// The radii of the arc's ellipse. The horizontal and vertical half-dimensions of the ellipse will match the x and y values of the radii vector. pub radii: Vector, - /// The rotation of the arc's ellipse. + /// The clockwise rotation of the arc's ellipse. pub rotation: f32, - /// The start of the segment's angle, clockwise rotation. + /// The start of the segment's angle in radians, clockwise rotation from positive x-axis. pub start_angle: f32, - /// The end of the segment's angle, clockwise rotation. + /// The end of the segment's angle in radians, clockwise rotation from positive x-axis. pub end_angle: f32, } diff --git a/graphics/src/geometry/path/builder.rs b/graphics/src/geometry/path/builder.rs index 794dd3bc..b0959fbf 100644 --- a/graphics/src/geometry/path/builder.rs +++ b/graphics/src/geometry/path/builder.rs @@ -174,7 +174,7 @@ impl Builder { /// the starting point. #[inline] pub fn close(&mut self) { - self.raw.close() + self.raw.close(); } /// Builds the [`Path`] of this [`Builder`]. diff --git a/graphics/src/geometry/stroke.rs b/graphics/src/geometry/stroke.rs index 69a76e1c..aff49ab3 100644 --- a/graphics/src/geometry/stroke.rs +++ b/graphics/src/geometry/stroke.rs @@ -1,4 +1,6 @@ -//! Create lines from a [crate::widget::canvas::Path] and assigns them various attributes/styles. +//! Create lines from a [`Path`] and assigns them various attributes/styles. +//! +//! [`Path`]: super::Path pub use crate::geometry::Style; use iced_core::Color; 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/gradient.rs b/graphics/src/gradient.rs index 4db565d8..603f1b4a 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -1,8 +1,6 @@ -//! A gradient that can be used as a [`Fill`] for some geometry. +//! A gradient that can be used as a fill for some geometry. //! //! For a gradient that you can use as a background variant for a widget, see [`Gradient`]. -//! -//! [`Gradient`]: crate::core::Gradient; use crate::color; use crate::core::gradient::ColorStop; use crate::core::{self, Color, Point, Rectangle}; @@ -36,10 +34,7 @@ impl Gradient { } } -/// A linear gradient that can be used in the style of [`Fill`] or [`Stroke`]. -/// -/// [`Fill`]: crate::geometry::Fill; -/// [`Stroke`]: crate::geometry::Stroke; +/// A linear gradient. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Linear { /// The absolute starting position of the gradient. @@ -53,7 +48,7 @@ pub struct Linear { } impl Linear { - /// Creates a new [`Builder`]. + /// Creates a new [`Linear`] builder. pub fn new(start: Point, end: Point) -> Self { Self { start, @@ -92,8 +87,8 @@ impl Linear { mut self, stops: impl IntoIterator<Item = ColorStop>, ) -> Self { - for stop in stops.into_iter() { - self = self.add_stop(stop.offset, stop.color) + for stop in stops { + self = self.add_stop(stop.offset, stop.color); } self diff --git a/graphics/src/image.rs b/graphics/src/image.rs index 6b43f4a8..d89caace 100644 --- a/graphics/src/image.rs +++ b/graphics/src/image.rs @@ -79,7 +79,7 @@ impl Operation { use image::imageops; if self.contains(Self::FLIP_DIAGONALLY) { - imageops::flip_vertical_in_place(&mut image) + imageops::flip_vertical_in_place(&mut image); } if self.contains(Self::ROTATE_180) { diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index af374a2f..7a213909 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -7,19 +7,14 @@ #![doc( html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" )] +#![forbid(rust_2018_idioms)] #![deny( missing_debug_implementations, missing_docs, unsafe_code, unused_results, - clippy::extra_unused_lifetimes, - clippy::from_over_into, - clippy::needless_borrow, - clippy::new_without_default, - clippy::useless_conversion + rustdoc::broken_intra_doc_links )] -#![forbid(rust_2018_idioms)] -#![allow(clippy::inherent_to_string, clippy::type_complexity)] #![cfg_attr(docsrs, feature(doc_auto_cfg))] mod antialiasing; mod error; @@ -34,6 +29,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/mesh.rs b/graphics/src/mesh.rs index cfb5a60f..041986cf 100644 --- a/graphics/src/mesh.rs +++ b/graphics/src/mesh.rs @@ -41,7 +41,7 @@ impl Damage for Mesh { } } -/// A set of [`Vertex2D`] and indices representing a list of triangles. +/// A set of vertices and indices representing a list of triangles. #[derive(Clone, Debug, PartialEq, Eq)] pub struct Indexed<T> { /// The vertices of the mesh diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index 7592a410..4ed512c1 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -3,7 +3,9 @@ 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::editor; +use crate::text::paragraph; use std::sync::Arc; @@ -19,7 +21,7 @@ pub enum Primitive<T> { /// 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 +33,24 @@ pub enum Primitive<T> { /// The shaping strategy of the text. shaping: text::Shaping, }, + /// A paragraph primitive + Paragraph { + /// The [`paragraph::Weak`] reference. + paragraph: paragraph::Weak, + /// The position of the paragraph. + position: Point, + /// The color of the paragraph. + color: Color, + }, + /// An editor primitive + Editor { + /// The [`editor::Weak`] reference. + editor: editor::Weak, + /// The position of the paragraph. + position: Point, + /// The color of the paragraph. + color: Color, + }, /// A quad primitive Quad { /// The bounds of the quad @@ -48,6 +68,8 @@ pub enum Primitive<T> { Image { /// The handle of the image handle: image::Handle, + /// The filter method of the image + filter_method: image::FilterMethod, /// The bounds of the image bounds: Rectangle, }, diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index c0cec60a..d7613e36 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<B: Backend, Theme> { backend: B, + default_font: Font, + default_text_size: Pixels, primitives: Vec<Primitive<B::Primitive>>, theme: PhantomData<Theme>, } impl<B: Backend, T> Renderer<B, T> { /// 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<B: Backend, T> Renderer<B, T> { impl<B: Backend, T> iced_core::Renderer for Renderer<B, T> { type Theme = T; - fn layout<Message>( - &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,68 @@ impl<B: Backend, T> iced_core::Renderer for Renderer<B, T> { } } -impl<B, T> text::Renderer for Renderer<B, T> +impl<B, T> core::text::Renderer for Renderer<B, T> where B: Backend + backend::Text, { type Font = Font; + type Paragraph = text::Paragraph; + type Editor = text::Editor; - 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() - } - - fn default_size(&self) -> f32 { - self.backend().default_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 hit_test( - &self, - content: &str, - size: f32, - line_height: text::LineHeight, - font: Font, - bounds: Size, - shaping: text::Shaping, - point: Point, - nearest_only: bool, - ) -> Option<text::Hit> { - self.backend().hit_test( - content, - size, - line_height, - font, - bounds, - shaping, - point, - nearest_only, - ) + self.default_font + } + + fn default_size(&self) -> Pixels { + self.default_text_size } fn load_font(&mut self, bytes: Cow<'static, [u8]>) { self.backend.load_font(bytes); } - fn fill_text(&mut self, text: Text<'_, Self::Font>) { + fn fill_paragraph( + &mut self, + paragraph: &Self::Paragraph, + position: Point, + color: Color, + ) { + self.primitives.push(Primitive::Paragraph { + paragraph: paragraph.downgrade(), + position, + color, + }); + } + + fn fill_editor( + &mut self, + editor: &Self::Editor, + position: Point, + color: Color, + ) { + self.primitives.push(Primitive::Editor { + editor: editor.downgrade(), + position, + color, + }); + } + + 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, @@ -226,8 +215,17 @@ where self.backend().dimensions(handle) } - fn draw(&mut self, handle: image::Handle, bounds: Rectangle) { - self.primitives.push(Primitive::Image { handle, bounds }) + fn draw( + &mut self, + handle: image::Handle, + filter_method: image::FilterMethod, + bounds: Rectangle, + ) { + self.primitives.push(Primitive::Image { + handle, + filter_method, + bounds, + }); } } @@ -249,6 +247,6 @@ where handle, color, bounds, - }) + }); } } diff --git a/graphics/src/text.rs b/graphics/src/text.rs new file mode 100644 index 00000000..7261900e --- /dev/null +++ b/graphics/src/text.rs @@ -0,0 +1,156 @@ +//! Draw text. +pub mod cache; +pub mod editor; +pub mod paragraph; + +pub use cache::Cache; +pub use editor::Editor; +pub use paragraph::Paragraph; + +pub use cosmic_text; + +use crate::color; +use crate::core::font::{self, Font}; +use crate::core::text::Shaping; +use crate::core::{Color, Size}; + +use once_cell::sync::OnceCell; +use std::borrow::Cow; +use std::sync::{Arc, RwLock}; + +/// Returns the global [`FontSystem`]. +pub fn font_system() -> &'static RwLock<FontSystem> { + static FONT_SYSTEM: OnceCell<RwLock<FontSystem>> = OnceCell::new(); + + FONT_SYSTEM.get_or_init(|| { + RwLock::new(FontSystem { + raw: cosmic_text::FontSystem::new_with_fonts([ + cosmic_text::fontdb::Source::Binary(Arc::new( + include_bytes!("../fonts/Iced-Icons.ttf").as_slice(), + )), + ]), + version: Version::default(), + }) + }) +} + +/// A set of system fonts. +#[allow(missing_debug_implementations)] +pub struct FontSystem { + raw: cosmic_text::FontSystem, + version: Version, +} + +impl FontSystem { + /// Returns the raw [`cosmic_text::FontSystem`]. + pub fn raw(&mut self) -> &mut cosmic_text::FontSystem { + &mut self.raw + } + + /// Loads a font from its bytes. + pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) { + let _ = self.raw.db_mut().load_font_source( + cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())), + ); + + self.version = Version(self.version.0 + 1); + } + + /// Returns the current [`Version`] of the [`FontSystem`]. + /// + /// Loading a font will increase the version of a [`FontSystem`]. + pub fn version(&self) -> Version { + self.version + } +} + +/// A version number. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +pub struct Version(u32); + +/// Measures the dimensions of the given [`cosmic_text::Buffer`]. +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) +} + +/// Returns the attributes of the given [`Font`]. +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, + } +} + +/// Converts some [`Shaping`] strategy to a [`cosmic_text::Shaping`] strategy. +pub fn to_shaping(shaping: Shaping) -> cosmic_text::Shaping { + match shaping { + Shaping::Basic => cosmic_text::Shaping::Basic, + Shaping::Advanced => cosmic_text::Shaping::Advanced, + } +} + +/// Converts some [`Color`] to a [`cosmic_text::Color`]. +pub fn to_color(color: Color) -> cosmic_text::Color { + let [r, g, b, a] = color::pack(color).components(); + + cosmic_text::Color::rgba( + (r * 255.0) as u8, + (g * 255.0) as u8, + (b * 255.0) as u8, + (a * 255.0) as u8, + ) +} diff --git a/graphics/src/text/cache.rs b/graphics/src/text/cache.rs new file mode 100644 index 00000000..7fb33567 --- /dev/null +++ b/graphics/src/text/cache.rs @@ -0,0 +1,147 @@ +//! Cache text. +use crate::core::{Font, Size}; +use crate::text; + +use rustc_hash::{FxHashMap, FxHashSet}; +use std::collections::hash_map; +use std::hash::{BuildHasher, Hash, Hasher}; + +/// A store of recently used sections of text. +#[allow(missing_debug_implementations)] +#[derive(Default)] +pub struct Cache { + entries: FxHashMap<KeyHash, Entry>, + aliases: FxHashMap<KeyHash, KeyHash>, + recently_used: FxHashSet<KeyHash>, + hasher: HashBuilder, +} + +type HashBuilder = xxhash_rust::xxh3::Xxh3Builder; + +impl Cache { + /// Creates a new empty [`Cache`]. + pub fn new() -> Self { + Self::default() + } + + /// Gets the text [`Entry`] with the given [`KeyHash`]. + pub fn get(&self, key: &KeyHash) -> Option<&Entry> { + self.entries.get(key) + } + + /// Allocates a text [`Entry`] if it is not already present in the [`Cache`]. + pub fn allocate( + &mut self, + font_system: &mut cosmic_text::FontSystem, + key: Key<'_>, + ) -> (KeyHash, &mut Entry) { + 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.max(f32::MIN_POSITIVE), + ); + 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(Entry { + buffer, + min_bounds: bounds, + }); + + 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()) + } + + /// Trims the [`Cache`]. + /// + /// This will clear the sections of text that have not been used since the last `trim`. + 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(); + } +} + +/// A cache key representing a section of text. +#[derive(Debug, Clone, Copy)] +pub struct Key<'a> { + /// The content of the text. + pub content: &'a str, + /// The size of the text. + pub size: f32, + /// The line height of the text. + pub line_height: f32, + /// The [`Font`] of the text. + pub font: Font, + /// The bounds of the text. + pub bounds: Size, + /// The shaping strategy of the text. + pub shaping: text::Shaping, +} + +impl Key<'_> { + fn hash<H: Hasher>(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() + } +} + +/// The hash of a [`Key`]. +pub type KeyHash = u64; + +/// A cache entry. +#[allow(missing_debug_implementations)] +pub struct Entry { + /// The buffer of text, ready for drawing. + pub buffer: cosmic_text::Buffer, + /// The minimum bounds of the text. + pub min_bounds: Size, +} diff --git a/graphics/src/text/editor.rs b/graphics/src/text/editor.rs new file mode 100644 index 00000000..d5262ae8 --- /dev/null +++ b/graphics/src/text/editor.rs @@ -0,0 +1,779 @@ +//! Draw and edit text. +use crate::core::text::editor::{ + self, Action, Cursor, Direction, Edit, Motion, +}; +use crate::core::text::highlighter::{self, Highlighter}; +use crate::core::text::LineHeight; +use crate::core::{Font, Pixels, Point, Rectangle, Size}; +use crate::text; + +use cosmic_text::Edit as _; + +use std::fmt; +use std::sync::{self, Arc}; + +/// A multi-line text editor. +#[derive(Debug, PartialEq)] +pub struct Editor(Option<Arc<Internal>>); + +struct Internal { + editor: cosmic_text::Editor, + font: Font, + bounds: Size, + topmost_line_changed: Option<usize>, + version: text::Version, +} + +impl Editor { + /// Creates a new empty [`Editor`]. + pub fn new() -> Self { + Self::default() + } + + /// Returns the buffer of the [`Editor`]. + pub fn buffer(&self) -> &cosmic_text::Buffer { + self.internal().editor.buffer() + } + + /// Creates a [`Weak`] reference to the [`Editor`]. + /// + /// This is useful to avoid cloning the [`Editor`] when + /// referential guarantees are unnecessary. For instance, + /// when creating a rendering tree. + pub fn downgrade(&self) -> Weak { + let editor = self.internal(); + + Weak { + raw: Arc::downgrade(editor), + bounds: editor.bounds, + } + } + + fn internal(&self) -> &Arc<Internal> { + self.0 + .as_ref() + .expect("Editor should always be initialized") + } +} + +impl editor::Editor for Editor { + type Font = Font; + + fn with_text(text: &str) -> Self { + let mut buffer = cosmic_text::Buffer::new_empty(cosmic_text::Metrics { + font_size: 1.0, + line_height: 1.0, + }); + + let mut font_system = + text::font_system().write().expect("Write font system"); + + buffer.set_text( + font_system.raw(), + text, + cosmic_text::Attrs::new(), + cosmic_text::Shaping::Advanced, + ); + + Editor(Some(Arc::new(Internal { + editor: cosmic_text::Editor::new(buffer), + version: font_system.version(), + ..Default::default() + }))) + } + + fn line(&self, index: usize) -> Option<&str> { + self.buffer() + .lines + .get(index) + .map(cosmic_text::BufferLine::text) + } + + fn line_count(&self) -> usize { + self.buffer().lines.len() + } + + fn selection(&self) -> Option<String> { + self.internal().editor.copy_selection() + } + + fn cursor(&self) -> editor::Cursor { + let internal = self.internal(); + + let cursor = internal.editor.cursor(); + let buffer = internal.editor.buffer(); + + match internal.editor.select_opt() { + Some(selection) => { + let (start, end) = if cursor < selection { + (cursor, selection) + } else { + (selection, cursor) + }; + + let line_height = buffer.metrics().line_height; + let selected_lines = end.line - start.line + 1; + + let visual_lines_offset = + visual_lines_offset(start.line, buffer); + + let regions = buffer + .lines + .iter() + .skip(start.line) + .take(selected_lines) + .enumerate() + .flat_map(|(i, line)| { + highlight_line( + line, + if i == 0 { start.index } else { 0 }, + if i == selected_lines - 1 { + end.index + } else { + line.text().len() + }, + ) + }) + .enumerate() + .filter_map(|(visual_line, (x, width))| { + if width > 0.0 { + Some(Rectangle { + x, + width, + y: (visual_line as i32 + visual_lines_offset) + as f32 + * line_height, + height: line_height, + }) + } else { + None + } + }) + .collect(); + + Cursor::Selection(regions) + } + _ => { + let line_height = buffer.metrics().line_height; + + let visual_lines_offset = + visual_lines_offset(cursor.line, buffer); + + let line = buffer + .lines + .get(cursor.line) + .expect("Cursor line should be present"); + + let layout = line + .layout_opt() + .as_ref() + .expect("Line layout should be cached"); + + let mut lines = layout.iter().enumerate(); + + let (visual_line, offset) = lines + .find_map(|(i, line)| { + let start = line + .glyphs + .first() + .map(|glyph| glyph.start) + .unwrap_or(0); + let end = line + .glyphs + .last() + .map(|glyph| glyph.end) + .unwrap_or(0); + + let is_cursor_before_start = start > cursor.index; + + let is_cursor_before_end = match cursor.affinity { + cosmic_text::Affinity::Before => { + cursor.index <= end + } + cosmic_text::Affinity::After => cursor.index < end, + }; + + if is_cursor_before_start { + // Sometimes, the glyph we are looking for is right + // between lines. This can happen when a line wraps + // on a space. + // In that case, we can assume the cursor is at the + // end of the previous line. + // i is guaranteed to be > 0 because `start` is always + // 0 for the first line, so there is no way for the + // cursor to be before it. + Some((i - 1, layout[i - 1].w)) + } else if is_cursor_before_end { + let offset = line + .glyphs + .iter() + .take_while(|glyph| cursor.index > glyph.start) + .map(|glyph| glyph.w) + .sum(); + + Some((i, offset)) + } else { + None + } + }) + .unwrap_or(( + layout.len().saturating_sub(1), + layout.last().map(|line| line.w).unwrap_or(0.0), + )); + + Cursor::Caret(Point::new( + offset, + (visual_lines_offset + visual_line as i32) as f32 + * line_height, + )) + } + } + } + + fn cursor_position(&self) -> (usize, usize) { + let cursor = self.internal().editor.cursor(); + + (cursor.line, cursor.index) + } + + fn perform(&mut self, action: Action) { + let mut font_system = + text::font_system().write().expect("Write font system"); + + let editor = + self.0.take().expect("Editor should always be initialized"); + + // TODO: Handle multiple strong references somehow + let mut internal = Arc::try_unwrap(editor) + .expect("Editor cannot have multiple strong references"); + + let editor = &mut internal.editor; + + match action { + // Motion events + Action::Move(motion) => { + if let Some(selection) = editor.select_opt() { + let cursor = editor.cursor(); + + let (left, right) = if cursor < selection { + (cursor, selection) + } else { + (selection, cursor) + }; + + editor.set_select_opt(None); + + match motion { + // These motions are performed as-is even when a selection + // is present + Motion::Home + | Motion::End + | Motion::DocumentStart + | Motion::DocumentEnd => { + editor.action( + font_system.raw(), + motion_to_action(motion), + ); + } + // Other motions simply move the cursor to one end of the selection + _ => editor.set_cursor(match motion.direction() { + Direction::Left => left, + Direction::Right => right, + }), + } + } else { + editor.action(font_system.raw(), motion_to_action(motion)); + } + } + + // Selection events + Action::Select(motion) => { + let cursor = editor.cursor(); + + if editor.select_opt().is_none() { + editor.set_select_opt(Some(cursor)); + } + + editor.action(font_system.raw(), motion_to_action(motion)); + + // Deselect if selection matches cursor position + if let Some(selection) = editor.select_opt() { + let cursor = editor.cursor(); + + if cursor.line == selection.line + && cursor.index == selection.index + { + editor.set_select_opt(None); + } + } + } + Action::SelectWord => { + use unicode_segmentation::UnicodeSegmentation; + + let cursor = editor.cursor(); + + if let Some(line) = editor.buffer().lines.get(cursor.line) { + let (start, end) = + UnicodeSegmentation::unicode_word_indices(line.text()) + // Split words with dots + .flat_map(|(i, word)| { + word.split('.').scan(i, |current, word| { + let start = *current; + *current += word.len() + 1; + + Some((start, word)) + }) + }) + // Turn words into ranges + .map(|(i, word)| (i, i + word.len())) + // Find the word at cursor + .find(|&(start, end)| { + start <= cursor.index && cursor.index < end + }) + // Cursor is not in a word. Let's select its punctuation cluster. + .unwrap_or_else(|| { + let start = line.text()[..cursor.index] + .char_indices() + .rev() + .take_while(|(_, c)| { + c.is_ascii_punctuation() + }) + .map(|(i, _)| i) + .last() + .unwrap_or(cursor.index); + + let end = line.text()[cursor.index..] + .char_indices() + .skip_while(|(_, c)| { + c.is_ascii_punctuation() + }) + .map(|(i, _)| i + cursor.index) + .next() + .unwrap_or(cursor.index); + + (start, end) + }); + + if start != end { + editor.set_cursor(cosmic_text::Cursor { + index: start, + ..cursor + }); + + editor.set_select_opt(Some(cosmic_text::Cursor { + index: end, + ..cursor + })); + } + } + } + Action::SelectLine => { + let cursor = editor.cursor(); + + if let Some(line_length) = editor + .buffer() + .lines + .get(cursor.line) + .map(|line| line.text().len()) + { + editor + .set_cursor(cosmic_text::Cursor { index: 0, ..cursor }); + + editor.set_select_opt(Some(cosmic_text::Cursor { + index: line_length, + ..cursor + })); + } + } + + // Editing events + Action::Edit(edit) => { + match edit { + Edit::Insert(c) => { + editor.action( + font_system.raw(), + cosmic_text::Action::Insert(c), + ); + } + Edit::Paste(text) => { + editor.insert_string(&text, None); + } + Edit::Enter => { + editor.action( + font_system.raw(), + cosmic_text::Action::Enter, + ); + } + Edit::Backspace => { + editor.action( + font_system.raw(), + cosmic_text::Action::Backspace, + ); + } + Edit::Delete => { + editor.action( + font_system.raw(), + cosmic_text::Action::Delete, + ); + } + } + + let cursor = editor.cursor(); + let selection = editor.select_opt().unwrap_or(cursor); + + internal.topmost_line_changed = + Some(cursor.min(selection).line); + } + + // Mouse events + Action::Click(position) => { + editor.action( + font_system.raw(), + cosmic_text::Action::Click { + x: position.x as i32, + y: position.y as i32, + }, + ); + } + Action::Drag(position) => { + editor.action( + font_system.raw(), + cosmic_text::Action::Drag { + x: position.x as i32, + y: position.y as i32, + }, + ); + + // Deselect if selection matches cursor position + if let Some(selection) = editor.select_opt() { + let cursor = editor.cursor(); + + if cursor.line == selection.line + && cursor.index == selection.index + { + editor.set_select_opt(None); + } + } + } + Action::Scroll { lines } => { + editor.action( + font_system.raw(), + cosmic_text::Action::Scroll { lines }, + ); + } + } + + self.0 = Some(Arc::new(internal)); + } + + fn bounds(&self) -> Size { + self.internal().bounds + } + + fn update( + &mut self, + new_bounds: Size, + new_font: Font, + new_size: Pixels, + new_line_height: LineHeight, + new_highlighter: &mut impl Highlighter, + ) { + let editor = + self.0.take().expect("Editor should always be initialized"); + + let mut internal = Arc::try_unwrap(editor) + .expect("Editor cannot have multiple strong references"); + + let mut font_system = + text::font_system().write().expect("Write font system"); + + if font_system.version() != internal.version { + log::trace!("Updating `FontSystem` of `Editor`..."); + + for line in internal.editor.buffer_mut().lines.iter_mut() { + line.reset(); + } + + internal.version = font_system.version(); + internal.topmost_line_changed = Some(0); + } + + if new_font != internal.font { + log::trace!("Updating font of `Editor`..."); + + for line in internal.editor.buffer_mut().lines.iter_mut() { + let _ = line.set_attrs_list(cosmic_text::AttrsList::new( + text::to_attributes(new_font), + )); + } + + internal.font = new_font; + internal.topmost_line_changed = Some(0); + } + + let metrics = internal.editor.buffer().metrics(); + let new_line_height = new_line_height.to_absolute(new_size); + + if new_size.0 != metrics.font_size + || new_line_height.0 != metrics.line_height + { + log::trace!("Updating `Metrics` of `Editor`..."); + + internal.editor.buffer_mut().set_metrics( + font_system.raw(), + cosmic_text::Metrics::new(new_size.0, new_line_height.0), + ); + } + + if new_bounds != internal.bounds { + log::trace!("Updating size of `Editor`..."); + + internal.editor.buffer_mut().set_size( + font_system.raw(), + new_bounds.width, + new_bounds.height, + ); + + internal.bounds = new_bounds; + } + + if let Some(topmost_line_changed) = internal.topmost_line_changed.take() + { + log::trace!( + "Notifying highlighter of line change: {topmost_line_changed}" + ); + + new_highlighter.change_line(topmost_line_changed); + } + + internal.editor.shape_as_needed(font_system.raw()); + + self.0 = Some(Arc::new(internal)); + } + + fn highlight<H: Highlighter>( + &mut self, + font: Self::Font, + highlighter: &mut H, + format_highlight: impl Fn(&H::Highlight) -> highlighter::Format<Self::Font>, + ) { + let internal = self.internal(); + let buffer = internal.editor.buffer(); + + let mut window = buffer.scroll() + buffer.visible_lines(); + + let last_visible_line = buffer + .lines + .iter() + .enumerate() + .find_map(|(i, line)| { + let visible_lines = line + .layout_opt() + .as_ref() + .expect("Line layout should be cached") + .len() as i32; + + if window > visible_lines { + window -= visible_lines; + None + } else { + Some(i) + } + }) + .unwrap_or(buffer.lines.len().saturating_sub(1)); + + let current_line = highlighter.current_line(); + + if current_line > last_visible_line { + return; + } + + let editor = + self.0.take().expect("Editor should always be initialized"); + + let mut internal = Arc::try_unwrap(editor) + .expect("Editor cannot have multiple strong references"); + + let mut font_system = + text::font_system().write().expect("Write font system"); + + let attributes = text::to_attributes(font); + + for line in &mut internal.editor.buffer_mut().lines + [current_line..=last_visible_line] + { + let mut list = cosmic_text::AttrsList::new(attributes); + + for (range, highlight) in highlighter.highlight_line(line.text()) { + let format = format_highlight(&highlight); + + if format.color.is_some() || format.font.is_some() { + list.add_span( + range, + cosmic_text::Attrs { + color_opt: format.color.map(text::to_color), + ..if let Some(font) = format.font { + text::to_attributes(font) + } else { + attributes + } + }, + ); + } + } + + let _ = line.set_attrs_list(list); + } + + internal.editor.shape_as_needed(font_system.raw()); + + self.0 = Some(Arc::new(internal)); + } +} + +impl Default for Editor { + fn default() -> Self { + Self(Some(Arc::new(Internal::default()))) + } +} + +impl PartialEq for Internal { + fn eq(&self, other: &Self) -> bool { + self.font == other.font + && self.bounds == other.bounds + && self.editor.buffer().metrics() == other.editor.buffer().metrics() + } +} + +impl Default for Internal { + fn default() -> Self { + Self { + editor: cosmic_text::Editor::new(cosmic_text::Buffer::new_empty( + cosmic_text::Metrics { + font_size: 1.0, + line_height: 1.0, + }, + )), + font: Font::default(), + bounds: Size::ZERO, + topmost_line_changed: None, + version: text::Version::default(), + } + } +} + +impl fmt::Debug for Internal { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Internal") + .field("font", &self.font) + .field("bounds", &self.bounds) + .finish() + } +} + +/// A weak reference to an [`Editor`]. +#[derive(Debug, Clone)] +pub struct Weak { + raw: sync::Weak<Internal>, + /// The bounds of the [`Editor`]. + pub bounds: Size, +} + +impl Weak { + /// Tries to update the reference into an [`Editor`]. + pub fn upgrade(&self) -> Option<Editor> { + self.raw.upgrade().map(Some).map(Editor) + } +} + +impl PartialEq for Weak { + fn eq(&self, other: &Self) -> bool { + match (self.raw.upgrade(), other.raw.upgrade()) { + (Some(p1), Some(p2)) => p1 == p2, + _ => false, + } + } +} + +fn highlight_line( + line: &cosmic_text::BufferLine, + from: usize, + to: usize, +) -> impl Iterator<Item = (f32, f32)> + '_ { + let layout = line + .layout_opt() + .as_ref() + .expect("Line layout should be cached"); + + layout.iter().map(move |visual_line| { + let start = visual_line + .glyphs + .first() + .map(|glyph| glyph.start) + .unwrap_or(0); + let end = visual_line + .glyphs + .last() + .map(|glyph| glyph.end) + .unwrap_or(0); + + let range = start.max(from)..end.min(to); + + if range.is_empty() { + (0.0, 0.0) + } else if range.start == start && range.end == end { + (0.0, visual_line.w) + } else { + let first_glyph = visual_line + .glyphs + .iter() + .position(|glyph| range.start <= glyph.start) + .unwrap_or(0); + + let mut glyphs = visual_line.glyphs.iter(); + + let x = + glyphs.by_ref().take(first_glyph).map(|glyph| glyph.w).sum(); + + let width: f32 = glyphs + .take_while(|glyph| range.end > glyph.start) + .map(|glyph| glyph.w) + .sum(); + + (x, width) + } + }) +} + +fn visual_lines_offset(line: usize, buffer: &cosmic_text::Buffer) -> i32 { + let visual_lines_before_start: usize = buffer + .lines + .iter() + .take(line) + .map(|line| { + line.layout_opt() + .as_ref() + .expect("Line layout should be cached") + .len() + }) + .sum(); + + visual_lines_before_start as i32 - buffer.scroll() +} + +fn motion_to_action(motion: Motion) -> cosmic_text::Action { + match motion { + Motion::Left => cosmic_text::Action::Left, + Motion::Right => cosmic_text::Action::Right, + Motion::Up => cosmic_text::Action::Up, + Motion::Down => cosmic_text::Action::Down, + Motion::WordLeft => cosmic_text::Action::LeftWord, + Motion::WordRight => cosmic_text::Action::RightWord, + Motion::Home => cosmic_text::Action::Home, + Motion::End => cosmic_text::Action::End, + Motion::PageUp => cosmic_text::Action::PageUp, + Motion::PageDown => cosmic_text::Action::PageDown, + Motion::DocumentStart => cosmic_text::Action::BufferStart, + Motion::DocumentEnd => cosmic_text::Action::BufferEnd, + } +} diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs new file mode 100644 index 00000000..4a08a8f4 --- /dev/null +++ b/graphics/src/text/paragraph.rs @@ -0,0 +1,307 @@ +//! Draw paragraphs. +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; + +use std::fmt; +use std::sync::{self, Arc}; + +/// A bunch of text. +#[derive(Clone, PartialEq)] +pub struct Paragraph(Option<Arc<Internal>>); + +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, + version: text::Version, +} + +impl Paragraph { + /// Creates a new empty [`Paragraph`]. + pub fn new() -> Self { + Self::default() + } + + /// Returns the buffer of the [`Paragraph`]. + pub fn buffer(&self) -> &cosmic_text::Buffer { + &self.internal().buffer + } + + /// Creates a [`Weak`] reference to the [`Paragraph`]. + /// + /// This is useful to avoid cloning the [`Paragraph`] when + /// referential guarantees are unnecessary. For instance, + /// when creating a rendering tree. + pub fn downgrade(&self) -> Weak { + let paragraph = self.internal(); + + Weak { + raw: Arc::downgrade(paragraph), + min_bounds: paragraph.min_bounds, + horizontal_alignment: paragraph.horizontal_alignment, + vertical_alignment: paragraph.vertical_alignment, + } + } + + fn internal(&self) -> &Arc<Internal> { + self.0 + .as_ref() + .expect("paragraph should always be initialized") + } +} + +impl core::text::Paragraph for Paragraph { + type Font = Font; + + fn with_text(text: Text<'_, Font>) -> Self { + log::trace!("Allocating paragraph: {}", text.content); + + let mut font_system = + text::font_system().write().expect("Write font system"); + + let mut buffer = cosmic_text::Buffer::new( + font_system.raw(), + cosmic_text::Metrics::new( + text.size.into(), + text.line_height.to_absolute(text.size).into(), + ), + ); + + buffer.set_size( + font_system.raw(), + text.bounds.width, + text.bounds.height, + ); + + buffer.set_text( + font_system.raw(), + text.content, + text::to_attributes(text.font), + text::to_shaping(text.shaping), + ); + + let min_bounds = text::measure(&buffer); + + Self(Some(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, + version: font_system.version(), + }))) + } + + fn resize(&mut self, new_bounds: Size) { + let paragraph = self + .0 + .take() + .expect("paragraph should always be initialized"); + + match Arc::try_unwrap(paragraph) { + Ok(mut internal) => { + let mut font_system = + text::font_system().write().expect("Write font system"); + + internal.buffer.set_size( + font_system.raw(), + new_bounds.width, + new_bounds.height, + ); + + internal.bounds = new_bounds; + internal.min_bounds = text::measure(&internal.buffer); + + 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 + *self = 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, + }); + } + } + } + + fn compare(&self, text: Text<'_, Font>) -> core::text::Difference { + let font_system = text::font_system().read().expect("Read font system"); + let paragraph = self.internal(); + let metrics = paragraph.buffer.metrics(); + + if paragraph.version != font_system.version + || paragraph.content != text.content + || metrics.font_size != text.size.0 + || metrics.line_height != text.line_height.to_absolute(text.size).0 + || paragraph.font != text.font + || paragraph.shaping != text.shaping + || paragraph.horizontal_alignment != text.horizontal_alignment + || paragraph.vertical_alignment != text.vertical_alignment + { + core::text::Difference::Shape + } else if paragraph.bounds != text.bounds { + core::text::Difference::Bounds + } else { + core::text::Difference::None + } + } + + fn horizontal_alignment(&self) -> alignment::Horizontal { + self.internal().horizontal_alignment + } + + fn vertical_alignment(&self) -> alignment::Vertical { + self.internal().vertical_alignment + } + + fn min_bounds(&self) -> Size { + self.internal().min_bounds + } + + fn hit_test(&self, point: Point) -> Option<Hit> { + let cursor = self.internal().buffer.hit(point.x, point.y)?; + + Some(Hit::CharOffset(cursor.index)) + } + + fn grapheme_position(&self, line: usize, index: usize) -> Option<Point> { + let run = self.internal().buffer.layout_runs().nth(line)?; + + // 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 + } 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 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 + && 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, + version: text::Version::default(), + } + } +} + +/// A weak reference to a [`Paragraph`]. +#[derive(Debug, Clone)] +pub struct Weak { + raw: sync::Weak<Internal>, + /// The minimum bounds of the [`Paragraph`]. + pub min_bounds: Size, + /// The horizontal alignment of the [`Paragraph`]. + pub horizontal_alignment: alignment::Horizontal, + /// The vertical alignment of the [`Paragraph`]. + pub vertical_alignment: alignment::Vertical, +} + +impl Weak { + /// Tries to update the reference into a [`Paragraph`]. + pub fn upgrade(&self) -> Option<Paragraph> { + self.raw.upgrade().map(Some).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, + } + } +} |