Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

fix(type): kerning at the end of a paragraph or story is kept for typing, not applied to the last character

aiongg314e535c37a8
diff --git a/crates/engine/src/cmd/text.rs b/crates/engine/src/cmd/text.rs
index 64142cdd9..94a9323af 100644
--- a/crates/engine/src/cmd/text.rs
+++ b/crates/engine/src/cmd/text.rs
@@ -1162,10 +1162,12 @@ fn kern_pair(s: &mut Session, p: &Value) -> Result<Value> {
format_chars(s, &json!({ "kerning": kerning }))
}
-/// Byte offset of the character before `pos` when it starts a kerning pair with the text at `pos`
-/// (not a paragraph break).
+/// Byte offset of the character before `pos` when it starts a kerning pair with the character at
+/// `pos`: both exist and neither is a paragraph or line break.
fn pair_start(text: &str, pos: usize) -> Option<usize> {
- let c = text.get(..pos)?.chars().next_back().filter(|c| *c != '\n')?;
+ let is_text = |c: &char| !matches!(*c, '\n' | designcraft_doc::story::FORCED_LINE_BREAK);
+ text.get(pos..)?.chars().next().filter(is_text)?;
+ let c = text.get(..pos)?.chars().next_back().filter(is_text)?;
pos.checked_sub(c.len_utf8())
}
@@ -3143,4 +3145,19 @@ mod kern_pair_tests {
s.execute("text.insert", &json!({"text": "X"})).unwrap();
assert_eq!(kern_at(&s, sid, 0), Some(Kerning::None));
}
+
+ #[test]
+ fn kerning_at_the_end_of_a_paragraph_or_story_leaves_the_text_alone() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let r = s.execute("frame.create", &json!({"rect": [72, 72, 300, 200], "content": "text", "text": "AV\nTA"})).unwrap();
+ let sid = StoryId(r["story"].as_u64().unwrap());
+ for caret in [2, 5] {
+ s.execute("text.select", &json!({"story": sid.0, "anchor": caret, "focus": caret})).unwrap();
+ let steps = s.doc().unwrap().history.undo.len();
+ s.execute("type.kernPair", &json!({"kerning": {"kind": "manual", "value": -80}})).unwrap();
+ assert_eq!(s.doc().unwrap().history.undo.len(), steps, "no pair at {caret}: nothing is edited");
+ assert_eq!((kern_at(&s, sid, 1), kern_at(&s, sid, 4)), (None, None), "caret {caret}");
+ }
+ }
}