Skip to content

Commit ede7a30

Browse files
committed
Fix point Y hit test for top line
It looks like this was never updated for a change in what is considered an anchor point from the baseline to the top of the layout box. This change checks that the hit point Y is past the new anchor point. See: #311 (comment)
1 parent 21fe911 commit ede7a30

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

piet-web/src/text.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,36 +232,40 @@ impl TextLayout for WebTextLayout {
232232

233233
self.ctx.set_font(&self.font_setting);
234234

235-
// check out of bounds above top
236-
// out of bounds on bottom during iteration
237-
let mut is_y_inside = true;
238-
if point.y < -1.0 * first_baseline {
239-
is_y_inside = false
240-
};
235+
// Get the top line metric.
236+
let first_y_offset = self
237+
.line_metrics
238+
.get(0)
239+
.map(|lm| lm.y_offset)
240+
.unwrap_or(0.0);
241+
242+
// Check point Y is within the top bound of the top line.
243+
let mut is_y_inside = point.y >= first_y_offset;
241244

242-
let mut lm = self
245+
// Get the first bottom line metric that overshoots point Y.
246+
let bottom_lm = self
243247
.line_metrics
244248
.iter()
245-
.skip_while(|l| l.y_offset + l.height < point.y);
246-
let lm = lm
247-
.next()
249+
// Find line that overshoots point Y.
250+
.find(|lm| lm.y_offset + lm.height >= point.y)
248251
.or_else(|| {
249-
// This means it went over the last line, so return the last line.
252+
// In this case we went over the last line, so return it.
250253
is_y_inside = false;
251254
self.line_metrics.last()
252255
})
253256
.cloned()
254257
.unwrap_or_else(|| {
258+
// In this case, we have no line metrics, so return a default.
255259
is_y_inside = false;
256260
Default::default()
257261
});
258262

259-
// Then for the line, do hit test point
260-
// Trailing whitespace is remove for the line
261-
let line = &self.text[lm.start_offset..lm.end_offset];
263+
// For the bottom line, hit test the line point.
264+
// Trailing whitespace is removed for the line.
265+
let line = &self.text[bottom_lm.start_offset..bottom_lm.end_offset];
262266

263267
let mut htp = hit_test_line_point(&self.ctx, line, point);
264-
htp.idx += lm.start_offset;
268+
htp.idx += bottom_lm.start_offset;
265269

266270
if !is_y_inside {
267271
htp.is_inside = false;

0 commit comments

Comments
 (0)