Count grapheme clusters in Paragraph::grapheme_position

This commit is contained in:
Héctor Ramón Jiménez 2023-09-09 23:05:44 +02:00
parent 3cc605b70f
commit bbb9c2d928
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -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