summaryrefslogtreecommitdiffstats
path: root/graphics/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2023-12-02 16:10:42 +0100
committerLibravatar GitHub <noreply@github.com>2023-12-02 16:10:42 +0100
commit8727b3fc50ec251d9c117c51ca1289be5ba9b117 (patch)
tree802fef9f0b54b6f9cbbeedff14d7f57169db7d6b /graphics/src
parent7f8b17604a31e00becc43130ec516c1a53552c88 (diff)
parentb526ce4958b28208395276dd4078ffe0d780e1d7 (diff)
downloadiced-8727b3fc50ec251d9c117c51ca1289be5ba9b117.tar.gz
iced-8727b3fc50ec251d9c117c51ca1289be5ba9b117.tar.bz2
iced-8727b3fc50ec251d9c117c51ca1289be5ba9b117.zip
Merge pull request #2154 from iced-rs/fix/text-clipping
Fix text clipping
Diffstat (limited to 'graphics/src')
-rw-r--r--graphics/src/primitive.rs26
-rw-r--r--graphics/src/renderer.rs6
-rw-r--r--graphics/src/text.rs7
3 files changed, 28 insertions, 11 deletions
diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs
index 4ed512c1..ed75776c 100644
--- a/graphics/src/primitive.rs
+++ b/graphics/src/primitive.rs
@@ -14,24 +14,26 @@ use std::sync::Arc;
pub enum Primitive<T> {
/// A text primitive
Text {
- /// The contents of the text
+ /// The contents of the text.
content: String,
- /// The bounds of the text
+ /// The bounds of the text.
bounds: Rectangle,
- /// The color of the text
+ /// The color of the text.
color: Color,
- /// The size of the text in logical pixels
+ /// The size of the text in logical pixels.
size: Pixels,
- /// The line height of the text
+ /// The line height of the text.
line_height: text::LineHeight,
- /// The font of the text
+ /// The font of the text.
font: Font,
- /// The horizontal alignment of the text
+ /// The horizontal alignment of the text.
horizontal_alignment: alignment::Horizontal,
- /// The vertical alignment of the text
+ /// The vertical alignment of the text.
vertical_alignment: alignment::Vertical,
/// The shaping strategy of the text.
shaping: text::Shaping,
+ /// The clip bounds of the text.
+ clip_bounds: Rectangle,
},
/// A paragraph primitive
Paragraph {
@@ -41,15 +43,19 @@ pub enum Primitive<T> {
position: Point,
/// The color of the paragraph.
color: Color,
+ /// The clip bounds of the paragraph.
+ clip_bounds: Rectangle,
},
/// An editor primitive
Editor {
/// The [`editor::Weak`] reference.
editor: editor::Weak,
- /// The position of the paragraph.
+ /// The position of the editor.
position: Point,
- /// The color of the paragraph.
+ /// The color of the editor.
color: Color,
+ /// The clip bounds of the editor.
+ clip_bounds: Rectangle,
},
/// A quad primitive
Quad {
diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs
index d7613e36..1b0f5c5b 100644
--- a/graphics/src/renderer.rs
+++ b/graphics/src/renderer.rs
@@ -164,11 +164,13 @@ where
paragraph: &Self::Paragraph,
position: Point,
color: Color,
+ clip_bounds: Rectangle,
) {
self.primitives.push(Primitive::Paragraph {
paragraph: paragraph.downgrade(),
position,
color,
+ clip_bounds,
});
}
@@ -177,11 +179,13 @@ where
editor: &Self::Editor,
position: Point,
color: Color,
+ clip_bounds: Rectangle,
) {
self.primitives.push(Primitive::Editor {
editor: editor.downgrade(),
position,
color,
+ clip_bounds,
});
}
@@ -190,6 +194,7 @@ where
text: Text<'_, Self::Font>,
position: Point,
color: Color,
+ clip_bounds: Rectangle,
) {
self.primitives.push(Primitive::Text {
content: text.content.to_string(),
@@ -201,6 +206,7 @@ where
horizontal_alignment: text.horizontal_alignment,
vertical_alignment: text.vertical_alignment,
shaping: text.shaping,
+ clip_bounds,
});
}
}
diff --git a/graphics/src/text.rs b/graphics/src/text.rs
index 7261900e..fc7694c2 100644
--- a/graphics/src/text.rs
+++ b/graphics/src/text.rs
@@ -76,7 +76,12 @@ pub fn measure(buffer: &cosmic_text::Buffer) -> Size {
(run.line_w.max(width), total_lines + 1)
});
- Size::new(width, total_lines as f32 * buffer.metrics().line_height)
+ let (max_width, max_height) = buffer.size();
+
+ Size::new(
+ width.min(max_width),
+ (total_lines as f32 * buffer.metrics().line_height).min(max_height),
+ )
}
/// Returns the attributes of the given [`Font`].