From ddcf02f9d0377afe6a35dbbb09a29b4bd52efe2e Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Tue, 23 Jul 2024 13:36:40 -0700 Subject: Add background styling to span / rich text --- graphics/src/text/paragraph.rs | 53 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index da703ceb..540e669b 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 { + let internal = self.internal(); + + let mut current_bounds = None; + + let mut bounds = 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) + .fold(vec![], |mut spans, (line_top, line_height, glyph)| { + 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(bounds) if y != bounds.y => { + spans.push(*bounds); + *bounds = new_bounds(); + } + Some(bounds) => { + bounds.width += glyph.w; + } + } + + spans + }); + + bounds.extend(current_bounds); + bounds + } + fn grapheme_position(&self, line: usize, index: usize) -> Option { use unicode_segmentation::UnicodeSegmentation; -- cgit From 2e4c55bbffb45e7112e753fd77d78071acb252b1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 28 Jul 2024 14:17:59 +0200 Subject: Use `for` loop instead of `fold` in `span_bounds` --- graphics/src/text/paragraph.rs | 58 +++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 29 deletions(-) (limited to 'graphics') diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index 540e669b..b9f9c833 100644 --- a/graphics/src/text/paragraph.rs +++ b/graphics/src/text/paragraph.rs @@ -254,9 +254,10 @@ impl core::text::Paragraph for Paragraph { fn span_bounds(&self, index: usize) -> Vec { let internal = self.internal(); + let mut bounds = Vec::new(); let mut current_bounds = None; - let mut bounds = internal + let glyphs = internal .buffer .layout_runs() .flat_map(|run| { @@ -268,35 +269,34 @@ impl core::text::Paragraph for Paragraph { .map(move |glyph| (line_top, line_height, glyph)) }) .skip_while(|(_, _, glyph)| glyph.metadata != index) - .take_while(|(_, _, glyph)| glyph.metadata == index) - .fold(vec![], |mut spans, (line_top, line_height, glyph)| { - 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(bounds) if y != bounds.y => { - spans.push(*bounds); - *bounds = new_bounds(); - } - Some(bounds) => { - bounds.width += glyph.w; - } + .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()); } - - spans - }); + 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 -- cgit