diff options
author | 2024-07-28 14:40:58 +0200 | |
---|---|---|
committer | 2024-07-28 14:40:58 +0200 | |
commit | c47a6ed7b639cf76086554fe2b65a8acecb61ea2 (patch) | |
tree | abb30c0620ccab372ed6058f1fd5a2b04e1e4726 /graphics/src | |
parent | 23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7 (diff) | |
parent | 41a7318e5df3a49bf6e7fc2110155f2f22ff7e60 (diff) | |
download | iced-c47a6ed7b639cf76086554fe2b65a8acecb61ea2.tar.gz iced-c47a6ed7b639cf76086554fe2b65a8acecb61ea2.tar.bz2 iced-c47a6ed7b639cf76086554fe2b65a8acecb61ea2.zip |
Merge pull request #2516 from tarkah/feat/span-background
Add background styling to span / rich text
Diffstat (limited to 'graphics/src')
-rw-r--r-- | graphics/src/text/paragraph.rs | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index da703ceb..b9f9c833 100644 --- a/graphics/src/text/paragraph.rs +++ b/graphics/src/text/paragraph.rs @@ -2,7 +2,7 @@ use crate::core; use crate::core::alignment; use crate::core::text::{Hit, Shaping, Span, Text}; -use crate::core::{Font, Point, Size}; +use crate::core::{Font, Point, Rectangle, Size}; use crate::text; use std::fmt; @@ -251,6 +251,57 @@ impl core::text::Paragraph for Paragraph { Some(glyph.metadata) } + fn span_bounds(&self, index: usize) -> Vec<Rectangle> { + let internal = self.internal(); + + let mut bounds = Vec::new(); + let mut current_bounds = None; + + let glyphs = internal + .buffer + .layout_runs() + .flat_map(|run| { + let line_top = run.line_top; + let line_height = run.line_height; + + run.glyphs + .iter() + .map(move |glyph| (line_top, line_height, glyph)) + }) + .skip_while(|(_, _, glyph)| glyph.metadata != index) + .take_while(|(_, _, glyph)| glyph.metadata == index); + + for (line_top, line_height, glyph) in glyphs { + let y = line_top + glyph.y; + + let new_bounds = || { + Rectangle::new( + Point::new(glyph.x, y), + Size::new( + glyph.w, + glyph.line_height_opt.unwrap_or(line_height), + ), + ) + }; + + match current_bounds.as_mut() { + None => { + current_bounds = Some(new_bounds()); + } + Some(current_bounds) if y != current_bounds.y => { + bounds.push(*current_bounds); + *current_bounds = new_bounds(); + } + Some(current_bounds) => { + current_bounds.width += glyph.w; + } + } + } + + bounds.extend(current_bounds); + bounds + } + fn grapheme_position(&self, line: usize, index: usize) -> Option<Point> { use unicode_segmentation::UnicodeSegmentation; |