diff options
author | 2023-09-09 23:05:44 +0200 | |
---|---|---|
committer | 2023-09-09 23:05:44 +0200 | |
commit | bbb9c2d92819f2ff96e51773037138fef488290a (patch) | |
tree | 0b47e4d8f1ea8c99364ea74457a6322fca373aaf /graphics | |
parent | 3cc605b70f543313cb665465ac169d0c85c446ab (diff) | |
download | iced-bbb9c2d92819f2ff96e51773037138fef488290a.tar.gz iced-bbb9c2d92819f2ff96e51773037138fef488290a.tar.bz2 iced-bbb9c2d92819f2ff96e51773037138fef488290a.zip |
Count grapheme clusters in `Paragraph::grapheme_position`
Diffstat (limited to '')
-rw-r--r-- | graphics/src/text/paragraph.rs | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index cd12bc8f..e4350cff 100644 --- a/graphics/src/text/paragraph.rs +++ b/graphics/src/text/paragraph.rs @@ -189,8 +189,27 @@ impl core::text::Paragraph for Paragraph { fn grapheme_position(&self, line: usize, index: usize) -> Option<Point> { let run = self.internal().buffer.layout_runs().nth(line)?; - // TODO: Index represents a grapheme, not a glyph - let glyph = run.glyphs.get(index).or_else(|| run.glyphs.last())?; + // 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 graphemes_seen = 0; + + let glyph = run + .glyphs + .iter() + .find(|glyph| { + if graphemes_seen == index { + return true; + } + + if Some(glyph.start) != last_start { + last_start = Some(glyph.start); + graphemes_seen += 1; + } + + false + }) + .or_else(|| run.glyphs.last())?; let advance_last = if index == run.glyphs.len() { glyph.w |