summaryrefslogtreecommitdiffstats
path: root/graphics/src/text.rs
diff options
context:
space:
mode:
Diffstat (limited to 'graphics/src/text.rs')
-rw-r--r--graphics/src/text.rs129
1 files changed, 128 insertions, 1 deletions
diff --git a/graphics/src/text.rs b/graphics/src/text.rs
index 0310ead7..30269e69 100644
--- a/graphics/src/text.rs
+++ b/graphics/src/text.rs
@@ -9,14 +9,141 @@ pub use paragraph::Paragraph;
pub use cosmic_text;
+use crate::core::alignment;
use crate::core::font::{self, Font};
use crate::core::text::Shaping;
-use crate::core::{Color, Point, Rectangle, Size};
+use crate::core::{Color, Pixels, Point, Rectangle, Size, Transformation};
use once_cell::sync::OnceCell;
use std::borrow::Cow;
use std::sync::{Arc, RwLock, Weak};
+/// A text primitive.
+#[derive(Debug, Clone, PartialEq)]
+pub enum Text {
+ /// A paragraph.
+ #[allow(missing_docs)]
+ Paragraph {
+ paragraph: paragraph::Weak,
+ position: Point,
+ color: Color,
+ clip_bounds: Rectangle,
+ transformation: Transformation,
+ },
+ /// An editor.
+ #[allow(missing_docs)]
+ Editor {
+ editor: editor::Weak,
+ position: Point,
+ color: Color,
+ clip_bounds: Rectangle,
+ transformation: Transformation,
+ },
+ /// Some cached text.
+ Cached {
+ /// The contents of the text.
+ content: String,
+ /// The bounds of the text.
+ bounds: Rectangle,
+ /// The color of the text.
+ color: Color,
+ /// The size of the text in logical pixels.
+ size: Pixels,
+ /// The line height of the text.
+ line_height: Pixels,
+ /// The font of the text.
+ font: Font,
+ /// The horizontal alignment of the text.
+ horizontal_alignment: alignment::Horizontal,
+ /// The vertical alignment of the text.
+ vertical_alignment: alignment::Vertical,
+ /// The shaping strategy of the text.
+ shaping: Shaping,
+ /// The clip bounds of the text.
+ clip_bounds: Rectangle,
+ },
+ /// Some raw text.
+ #[allow(missing_docs)]
+ Raw {
+ raw: Raw,
+ transformation: Transformation,
+ },
+}
+
+impl Text {
+ /// Returns the visible bounds of the [`Text`].
+ pub fn visible_bounds(&self) -> Option<Rectangle> {
+ let (bounds, horizontal_alignment, vertical_alignment) = match self {
+ Text::Paragraph {
+ position,
+ paragraph,
+ clip_bounds,
+ transformation,
+ ..
+ } => (
+ Rectangle::new(*position, paragraph.min_bounds)
+ .intersection(clip_bounds)
+ .map(|bounds| bounds * *transformation),
+ Some(paragraph.horizontal_alignment),
+ Some(paragraph.vertical_alignment),
+ ),
+ Text::Editor {
+ editor,
+ position,
+ clip_bounds,
+ transformation,
+ ..
+ } => (
+ Rectangle::new(*position, editor.bounds)
+ .intersection(clip_bounds)
+ .map(|bounds| bounds * *transformation),
+ None,
+ None,
+ ),
+ Text::Cached {
+ bounds,
+ clip_bounds,
+ horizontal_alignment,
+ vertical_alignment,
+ ..
+ } => (
+ bounds.intersection(clip_bounds),
+ Some(*horizontal_alignment),
+ Some(*vertical_alignment),
+ ),
+ Text::Raw { raw, .. } => (Some(raw.clip_bounds), None, None),
+ };
+
+ let mut bounds = bounds?;
+
+ if let Some(alignment) = horizontal_alignment {
+ match alignment {
+ alignment::Horizontal::Left => {}
+ alignment::Horizontal::Center => {
+ bounds.x -= bounds.width / 2.0;
+ }
+ alignment::Horizontal::Right => {
+ bounds.x -= bounds.width;
+ }
+ }
+ }
+
+ if let Some(alignment) = vertical_alignment {
+ match alignment {
+ alignment::Vertical::Top => {}
+ alignment::Vertical::Center => {
+ bounds.y -= bounds.height / 2.0;
+ }
+ alignment::Vertical::Bottom => {
+ bounds.y -= bounds.height;
+ }
+ }
+ }
+
+ Some(bounds)
+ }
+}
+
/// The regular variant of the [Fira Sans] font.
///
/// It is loaded as part of the default fonts in Wasm builds.