diff options
Diffstat (limited to '')
| -rw-r--r-- | graphics/src/text/paragraph.rs | 50 | 
1 files changed, 35 insertions, 15 deletions
| diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index 37fa97f2..da703ceb 100644 --- a/graphics/src/text/paragraph.rs +++ b/graphics/src/text/paragraph.rs @@ -100,8 +100,8 @@ impl core::text::Paragraph for Paragraph {          }))      } -    fn with_spans(text: Text<&[Span<'_>]>) -> Self { -        log::trace!("Allocating rich paragraph: {:?}", text.content); +    fn with_spans<Link>(text: Text<&[Span<'_, Link>]>) -> Self { +        log::trace!("Allocating rich paragraph: {} spans", text.content.len());          let mut font_system =              text::font_system().write().expect("Write font system"); @@ -122,18 +122,8 @@ impl core::text::Paragraph for Paragraph {          buffer.set_rich_text(              font_system.raw(), -            text.content.iter().map(|span| { -                let attrs = cosmic_text::Attrs::new(); - -                let attrs = if let Some(font) = span.font { -                    attrs -                        .family(text::to_family(font.family)) -                        .weight(text::to_weight(font.weight)) -                        .stretch(text::to_stretch(font.stretch)) -                        .style(text::to_style(font.style)) -                } else { -                    text::to_attributes(text.font) -                }; +            text.content.iter().enumerate().map(|(i, span)| { +                let attrs = text::to_attributes(span.font.unwrap_or(text.font));                  let attrs = match (span.size, span.line_height) {                      (None, None) => attrs, @@ -156,7 +146,7 @@ impl core::text::Paragraph for Paragraph {                      attrs                  }; -                (span.text.as_ref(), attrs) +                (span.text.as_ref(), attrs.metadata(i))              }),              text::to_attributes(text.font),              text::to_shaping(text.shaping), @@ -231,6 +221,36 @@ impl core::text::Paragraph for Paragraph {          Some(Hit::CharOffset(cursor.index))      } +    fn hit_span(&self, point: Point) -> Option<usize> { +        let internal = self.internal(); + +        let cursor = internal.buffer.hit(point.x, point.y)?; +        let line = internal.buffer.lines.get(cursor.line)?; + +        let mut last_glyph = None; +        let mut glyphs = line +            .layout_opt() +            .as_ref()? +            .iter() +            .flat_map(|line| line.glyphs.iter()) +            .peekable(); + +        while let Some(glyph) = glyphs.peek() { +            if glyph.start <= cursor.index && cursor.index < glyph.end { +                break; +            } + +            last_glyph = glyphs.next(); +        } + +        let glyph = match cursor.affinity { +            cosmic_text::Affinity::Before => last_glyph, +            cosmic_text::Affinity::After => glyphs.next(), +        }?; + +        Some(glyph.metadata) +    } +      fn grapheme_position(&self, line: usize, index: usize) -> Option<Point> {          use unicode_segmentation::UnicodeSegmentation; | 
