summaryrefslogtreecommitdiffstats
path: root/graphics
diff options
context:
space:
mode:
Diffstat (limited to 'graphics')
-rw-r--r--graphics/src/damage.rs5
-rw-r--r--graphics/src/geometry/text.rs135
-rw-r--r--graphics/src/primitive.rs2
-rw-r--r--graphics/src/text.rs37
-rw-r--r--graphics/src/text/paragraph.rs25
5 files changed, 183 insertions, 21 deletions
diff --git a/graphics/src/damage.rs b/graphics/src/damage.rs
index 595cc274..59e9f5b4 100644
--- a/graphics/src/damage.rs
+++ b/graphics/src/damage.rs
@@ -73,6 +73,11 @@ impl<T: Damage> Damage for Primitive<T> {
bounds.expand(1.5)
}
+ Self::RawText(raw) => {
+ // TODO: Add `size` field to `raw` to compute more accurate
+ // damage bounds (?)
+ raw.clip_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 0bf7ec97..d314e85e 100644
--- a/graphics/src/geometry/text.rs
+++ b/graphics/src/geometry/text.rs
@@ -1,6 +1,8 @@
use crate::core::alignment;
use crate::core::text::{LineHeight, Shaping};
-use crate::core::{Color, Font, Pixels, Point};
+use crate::core::{Color, Font, Pixels, Point, Size, Vector};
+use crate::geometry::Path;
+use crate::text;
/// A bunch of text that can be drawn to a canvas
#[derive(Debug, Clone)]
@@ -32,6 +34,137 @@ pub struct Text {
pub shaping: Shaping,
}
+impl Text {
+ /// Computes the [`Path`]s of the [`Text`] and draws them using
+ /// the given closure.
+ pub fn draw_with(&self, mut f: impl FnMut(Path, Color)) {
+ let mut font_system =
+ text::font_system().write().expect("Write font system");
+
+ let mut buffer = cosmic_text::BufferLine::new(
+ &self.content,
+ cosmic_text::AttrsList::new(text::to_attributes(self.font)),
+ text::to_shaping(self.shaping),
+ );
+
+ let layout = buffer.layout(
+ font_system.raw(),
+ self.size.0,
+ f32::MAX,
+ cosmic_text::Wrap::None,
+ );
+
+ let translation_x = match self.horizontal_alignment {
+ alignment::Horizontal::Left => self.position.x,
+ alignment::Horizontal::Center | alignment::Horizontal::Right => {
+ let mut line_width = 0.0f32;
+
+ for line in layout.iter() {
+ line_width = line_width.max(line.w);
+ }
+
+ if self.horizontal_alignment == alignment::Horizontal::Center {
+ self.position.x - line_width / 2.0
+ } else {
+ self.position.x - line_width
+ }
+ }
+ };
+
+ let translation_y = {
+ let line_height = self.line_height.to_absolute(self.size);
+
+ match self.vertical_alignment {
+ alignment::Vertical::Top => self.position.y,
+ alignment::Vertical::Center => {
+ self.position.y - line_height.0 / 2.0
+ }
+ alignment::Vertical::Bottom => self.position.y - line_height.0,
+ }
+ };
+
+ let mut swash_cache = cosmic_text::SwashCache::new();
+
+ for run in layout.iter() {
+ for glyph in run.glyphs.iter() {
+ let physical_glyph = glyph.physical((0.0, 0.0), 1.0);
+
+ let start_x = translation_x + glyph.x + glyph.x_offset;
+ let start_y = translation_y + glyph.y_offset + self.size.0;
+ let offset = Vector::new(start_x, start_y);
+
+ if let Some(commands) = swash_cache.get_outline_commands(
+ font_system.raw(),
+ physical_glyph.cache_key,
+ ) {
+ let glyph = Path::new(|path| {
+ use cosmic_text::Command;
+
+ for command in commands {
+ match command {
+ Command::MoveTo(p) => {
+ path.move_to(
+ Point::new(p.x, -p.y) + offset,
+ );
+ }
+ Command::LineTo(p) => {
+ path.line_to(
+ Point::new(p.x, -p.y) + offset,
+ );
+ }
+ Command::CurveTo(control_a, control_b, to) => {
+ path.bezier_curve_to(
+ Point::new(control_a.x, -control_a.y)
+ + offset,
+ Point::new(control_b.x, -control_b.y)
+ + offset,
+ Point::new(to.x, -to.y) + offset,
+ );
+ }
+ Command::QuadTo(control, to) => {
+ path.quadratic_curve_to(
+ Point::new(control.x, -control.y)
+ + offset,
+ Point::new(to.x, -to.y) + offset,
+ );
+ }
+ Command::Close => {
+ path.close();
+ }
+ }
+ }
+ });
+
+ f(glyph, self.color);
+ } else {
+ // TODO: Raster image support for `Canvas`
+ let [r, g, b, a] = self.color.into_rgba8();
+
+ swash_cache.with_pixels(
+ font_system.raw(),
+ physical_glyph.cache_key,
+ cosmic_text::Color::rgba(r, g, b, a),
+ |x, y, color| {
+ f(
+ Path::rectangle(
+ Point::new(x as f32, y as f32) + offset,
+ Size::new(1.0, 1.0),
+ ),
+ Color::from_rgba8(
+ color.r(),
+ color.g(),
+ color.b(),
+ color.a() as f32 / 255.0,
+ ),
+ );
+ },
+ );
+ }
+ }
+ }
+ }
+}
+
impl Default for Text {
fn default() -> Text {
Text {
diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs
index ed75776c..20affaaf 100644
--- a/graphics/src/primitive.rs
+++ b/graphics/src/primitive.rs
@@ -57,6 +57,8 @@ pub enum Primitive<T> {
/// The clip bounds of the editor.
clip_bounds: Rectangle,
},
+ /// A raw `cosmic-text` primitive
+ RawText(crate::text::Raw),
/// A quad primitive
Quad {
/// The bounds of the quad
diff --git a/graphics/src/text.rs b/graphics/src/text.rs
index fc7694c2..7c4b5e31 100644
--- a/graphics/src/text.rs
+++ b/graphics/src/text.rs
@@ -9,14 +9,13 @@ 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 crate::core::{Color, Point, Rectangle, Size};
use once_cell::sync::OnceCell;
use std::borrow::Cow;
-use std::sync::{Arc, RwLock};
+use std::sync::{Arc, RwLock, Weak};
/// Returns the global [`FontSystem`].
pub fn font_system() -> &'static RwLock<FontSystem> {
@@ -68,6 +67,29 @@ impl FontSystem {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Version(u32);
+/// A weak reference to a [`cosmic-text::Buffer`] that can be drawn.
+#[derive(Debug, Clone)]
+pub struct Raw {
+ /// A weak reference to a [`cosmic_text::Buffer`].
+ pub buffer: Weak<cosmic_text::Buffer>,
+ /// The position of the text.
+ pub position: Point,
+ /// The color of the text.
+ pub color: Color,
+ /// The clip bounds of the text.
+ pub clip_bounds: Rectangle,
+}
+
+impl PartialEq for Raw {
+ fn eq(&self, _other: &Self) -> bool {
+ // TODO: There is no proper way to compare raw buffers
+ // For now, no two instances of `Raw` text will be equal.
+ // This should be fine, but could trigger unnecessary redraws
+ // in the future.
+ false
+ }
+}
+
/// Measures the dimensions of the given [`cosmic_text::Buffer`].
pub fn measure(buffer: &cosmic_text::Buffer) -> Size {
let (width, total_lines) = buffer
@@ -150,12 +172,7 @@ pub fn to_shaping(shaping: Shaping) -> cosmic_text::Shaping {
/// 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();
+ let [r, g, b, a] = color.into_rgba8();
- cosmic_text::Color::rgba(
- (r * 255.0) as u8,
- (g * 255.0) as u8,
- (b * 255.0) as u8,
- (a * 255.0) as u8,
- )
+ cosmic_text::Color::rgba(r, g, b, a)
}
diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs
index 4a08a8f4..5d027542 100644
--- a/graphics/src/text/paragraph.rs
+++ b/graphics/src/text/paragraph.rs
@@ -187,38 +187,43 @@ impl core::text::Paragraph for Paragraph {
}
fn grapheme_position(&self, line: usize, index: usize) -> Option<Point> {
+ use unicode_segmentation::UnicodeSegmentation;
+
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 last_grapheme_count = 0;
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_grapheme_count = run.text[glyph.start..glyph.end]
+ .graphemes(false)
+ .count();
last_start = Some(glyph.start);
- graphemes_seen += 1;
+ graphemes_seen += last_grapheme_count;
}
- false
+ graphemes_seen >= index
})
.or_else(|| run.glyphs.last())?;
- let advance_last = if index == run.glyphs.len() {
- glyph.w
- } else {
+ let advance = if index == 0 {
0.0
+ } else {
+ glyph.w
+ * (1.0
+ - graphemes_seen.saturating_sub(index) as f32
+ / last_grapheme_count.max(1) as f32)
};
Some(Point::new(
- glyph.x + glyph.x_offset * glyph.font_size + advance_last,
+ glyph.x + glyph.x_offset * glyph.font_size + advance,
glyph.y - glyph.y_offset * glyph.font_size,
))
}