summaryrefslogtreecommitdiffstats
path: root/glow
diff options
context:
space:
mode:
authorLibravatar Tom <twitchyliquid64@ciphersink.net>2021-08-21 10:31:26 -0700
committerLibravatar Tom <twitchyliquid64@ciphersink.net>2021-08-21 10:31:26 -0700
commitaa63841e2c80ca8130adf41d25e5d731409b92f4 (patch)
tree4dab3f0405dc002a06f0e36ec40f2f74ff07212b /glow
parent8333b8f88ceaa53c361eb6726b2b7dac6cd2c402 (diff)
downloadiced-aa63841e2c80ca8130adf41d25e5d731409b92f4.tar.gz
iced-aa63841e2c80ca8130adf41d25e5d731409b92f4.tar.bz2
iced-aa63841e2c80ca8130adf41d25e5d731409b92f4.zip
Implement textual hit testing
Diffstat (limited to 'glow')
-rw-r--r--glow/src/backend.rs23
-rw-r--r--glow/src/text.rs92
2 files changed, 114 insertions, 1 deletions
diff --git a/glow/src/backend.rs b/glow/src/backend.rs
index 1680fc00..d97cbceb 100644
--- a/glow/src/backend.rs
+++ b/glow/src/backend.rs
@@ -7,7 +7,9 @@ use iced_graphics::font;
use iced_graphics::Layer;
use iced_graphics::Primitive;
use iced_native::mouse;
-use iced_native::{Font, HorizontalAlignment, Size, VerticalAlignment};
+use iced_native::{
+ Font, HitTestResult, HorizontalAlignment, Size, VerticalAlignment,
+};
/// A [`glow`] graphics backend for [`iced`].
///
@@ -211,6 +213,25 @@ impl backend::Text for Backend {
) -> (f32, f32) {
self.text_pipeline.measure(contents, size, font, bounds)
}
+
+ fn hit_test(
+ &self,
+ contents: &str,
+ size: f32,
+ font: Font,
+ bounds: Size,
+ point: iced_native::Point,
+ nearest_only: bool,
+ ) -> HitTestResult {
+ self.text_pipeline.hit_test(
+ contents,
+ size,
+ font,
+ bounds,
+ point,
+ nearest_only,
+ )
+ }
}
#[cfg(feature = "image")]
diff --git a/glow/src/text.rs b/glow/src/text.rs
index a4c39dfe..bb882594 100644
--- a/glow/src/text.rs
+++ b/glow/src/text.rs
@@ -1,6 +1,7 @@
use crate::Transformation;
use glow_glyph::ab_glyph;
use iced_graphics::font;
+use iced_native::HitTestResult;
use std::{cell::RefCell, collections::HashMap};
#[derive(Debug)]
@@ -109,6 +110,97 @@ impl Pipeline {
}
}
+ pub fn hit_test(
+ &self,
+ content: &str,
+ size: f32,
+ font: iced_native::Font,
+ bounds: iced_native::Size,
+ point: iced_native::Point,
+ nearest_only: bool,
+ ) -> HitTestResult {
+ use glow_glyph::GlyphCruncher;
+
+ let glow_glyph::FontId(font_id) = self.find_font(font);
+
+ let section = glow_glyph::Section {
+ bounds: (bounds.width, bounds.height),
+ text: vec![glow_glyph::Text {
+ text: content,
+ scale: size.into(),
+ font_id: glow_glyph::FontId(font_id),
+ extra: glow_glyph::Extra::default(),
+ }],
+ ..Default::default()
+ };
+
+ let mut mb = self.measure_brush.borrow_mut();
+
+ // The underlying type is FontArc, so clones are cheap.
+ use ab_glyph::{Font, ScaleFont};
+ let font = mb.fonts()[font_id].clone().into_scaled(size);
+
+ // Implements an iterator over the glyph bounding boxes.
+ let bounds = mb.glyphs(section).map(
+ |glow_glyph::SectionGlyph {
+ byte_index, glyph, ..
+ }| {
+ (
+ *byte_index,
+ iced_native::Rectangle::new(
+ iced_native::Point::new(
+ glyph.position.x - font.h_side_bearing(glyph.id),
+ glyph.position.y - font.ascent(),
+ ),
+ iced_native::Size::new(
+ font.h_advance(glyph.id),
+ font.ascent() - font.descent(),
+ ),
+ ),
+ )
+ },
+ );
+
+ // Implements computation of the character index based on the byte index
+ // within the input string.
+ let char_index = |byte_index| {
+ let mut b_count = 0;
+ for (i, utf8_len) in
+ content.chars().map(|c| c.len_utf8()).enumerate()
+ {
+ if byte_index < (b_count + utf8_len) {
+ return i;
+ }
+ b_count += utf8_len;
+ }
+ return byte_index;
+ };
+
+ if !nearest_only {
+ for (idx, bounds) in bounds.clone() {
+ if bounds.contains(point) {
+ return HitTestResult::CharOffset(char_index(idx));
+ }
+ }
+ }
+
+ let (idx, nearest) = bounds.fold(
+ (0usize, iced_native::Point::ORIGIN),
+ |acc: (usize, iced_native::Point), (idx, bounds)| {
+ if bounds.center().distance(point) < acc.1.distance(point) {
+ (idx, bounds.center())
+ } else {
+ acc
+ }
+ },
+ );
+
+ HitTestResult::NearestCharOffset(
+ char_index(idx),
+ (point - nearest).into(),
+ )
+ }
+
pub fn trim_measurement_cache(&mut self) {
// TODO: We should probably use a `GlyphCalculator` for this. However,
// it uses a lifetimed `GlyphCalculatorGuard` with side-effects on drop.