Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(type): navigate adjacent line with large leading (#3066) (#3141)

Sir Starchf155374ac329
diff --git a/crates/text/src/layout.rs b/crates/text/src/layout.rs
index 6ba13ef9e..f7494bd99 100644
--- a/crates/text/src/layout.rs
+++ b/crates/text/src/layout.rs
@@ -253,11 +253,16 @@ impl TextLayout {
/// Byte offset of the caret nearest to a line-space point.
pub fn hit_test_line(&self, x: f32, y: f32) -> usize {
- let Some((li, line)) = self.closest_line(y) else {
+ let Some((li, _)) = self.closest_line(y) else {
return 0;
};
+ self.hit_test_x_on_line(li, x)
+ }
+
+ fn hit_test_x_on_line(&self, line_index: usize, x: f32) -> usize {
+ let Some(line) = self.lines.get(line_index) else { return 0 };
let mut best = (f32::MAX, line.range.end);
- for c in self.clusters.iter().filter(|c| c.line == li) {
+ for c in self.clusters.iter().filter(|c| c.line == line_index) {
let mid = c.x + c.advance / 2.0;
let (before, after) = if c.rtl { (c.range.end, c.range.start) } else { (c.range.start, c.range.end) };
let (dist, off) = if x < mid { ((x - c.x).abs(), before) } else { ((x - c.x - c.advance).abs(), after) };
@@ -370,17 +375,18 @@ pub fn word_boundary(text: &str, idx: usize, forward: bool) -> usize {
pub fn line_step(layout: &TextLayout, text: &str, idx: usize, x: f32, dir: i32) -> usize {
let n = text.chars().count();
let idx = idx.min(n);
- let (_, top, bottom) = layout.caret(byte_index(text, idx));
- let h = (bottom - top).max(1.0);
- let y = if dir < 0 { top - h * 0.5 } else { bottom + h * 0.5 };
- let Some(bounds) = layout.line_bounds() else { return idx };
- if y < bounds[1] {
- return 0;
+ if layout.lines.is_empty() {
+ return idx;
}
- if y > bounds[3] {
- return n;
- }
- char_index(text, layout.hit_test_line(x, y))
+ let current_line = line_index(layout, byte_index(text, idx));
+ let target_line = if dir < 0 {
+ let Some(target) = current_line.checked_sub(1) else { return 0 };
+ target
+ } else {
+ let Some(target) = current_line.checked_add(1).filter(|target| *target < layout.lines.len()) else { return n };
+ target
+ };
+ char_index(text, layout.hit_test_x_on_line(target_line, x))
}
/// Line start (`end` false) or end for the line containing `idx`, as a character index.
diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs
index e89d32ac3..e966cf686 100644
--- a/crates/text/src/tests.rs
+++ b/crates/text/src/tests.rs
@@ -1353,6 +1353,24 @@ fn word_and_line_navigation() {
assert!(jumped > kept);
}
+#[test]
+fn line_navigation_reaches_adjacent_line_with_large_leading() {
+ use crate::layout::{byte_index, line_step};
+
+ let mut e = TextEngine::new();
+ let text = "ab\ncd";
+ let mut t = point(text, 12.0);
+ t.runs = vec![TextRun { len: text.len(), style: CharStyle { font_family: "Inter".into(), size_pt: 12.0, leading_pt: Some(40.0), ..Default::default() } }];
+ let l = e.layout(&t, 72.0);
+
+ assert_eq!(l.lines.len(), 2);
+ assert!((l.lines[1].baseline - l.lines[0].baseline - 40.0).abs() < 0.01);
+ let next = line_step(&l, text, 1, l.caret(byte_index(text, 1)).0, 1);
+ assert_eq!(next, 4, "lineNext must reach the second line");
+ let prev = line_step(&l, text, 4, l.caret(byte_index(text, 4)).0, -1);
+ assert_eq!(prev, 1, "linePrev must reach the first line");
+}
+
/// Thai text sample with above/below marks (sara i, mai ek, mai tho, mai han-akat, sara u).
const THAI_SAMPLE: &str = "ภาษาไทย สวัสดีครับ ผู้ที่น้ำ";