diff --git a/crates/engine/src/cmd/textedit.rs b/crates/engine/src/cmd/textedit.rs index 2d6661f5aa..cf012da405 100644 --- a/crates/engine/src/cmd/textedit.rs +++ b/crates/engine/src/cmd/textedit.rs @@ -1252,8 +1252,8 @@ fn set_range_style(s: &mut Session, p: &Value) -> Result { set.extend(styled.iter().flat_map(super::fonts::run_fonts)); } // With Block on, the leading goes to the frame's line gap. - match change.leading.filter(|_| !keep_line_gap) { - Some(l) => super::typecmd::refresh_with_leading(t, a, l), + match change.leading.filter(|_| !keep_line_gap).and_then(|l| super::typecmd::styled_start(t, a, b).map(|at| (at, l))) { + Some((at, l)) => super::typecmd::refresh_with_leading(t, at, l), None => refresh_bounds(t), } n = if *f == id { t.runs.len() } else { n }; diff --git a/crates/engine/src/cmd/textstyles.rs b/crates/engine/src/cmd/textstyles.rs index 426410b661..248eb6aff7 100644 --- a/crates/engine/src/cmd/textstyles.rs +++ b/crates/engine/src/cmd/textstyles.rs @@ -1043,7 +1043,7 @@ fn apply(s: &mut Session, p: &Value, kind: Kind) -> Result { if let Some(e) = err { return Err(e); } - leading_at = (sets_leading && (a != b || len == 0)).then_some(a.min(b)); + leading_at = sets_leading.then(|| super::typecmd::styled_start(t, a.min(b), a.max(b))).flatten(); } Kind::Para => { let mut v = t.paragraph_styles(); diff --git a/crates/engine/src/cmd/typecmd.rs b/crates/engine/src/cmd/typecmd.rs index 240fb37c1f..995ec8d249 100644 --- a/crates/engine/src/cmd/typecmd.rs +++ b/crates/engine/src/cmd/typecmd.rs @@ -358,6 +358,15 @@ pub(crate) fn refresh_with_leading(t: &mut TextObject, at: usize, leading: Optio } } +/// The first character a style change over `a..b` of `t` reaches, with both ends on character +/// boundaries as [`vectorcraft_text::edit::style_range`] sets them; None when the range styles no +/// character (empty, or inside one character, in text that is not empty). +pub(crate) fn styled_start(t: &TextObject, a: usize, b: usize) -> Option { + let plain = t.plain_text(); + let (a, b) = (vectorcraft_text::edit::floor_char(&plain, a), vectorcraft_text::edit::floor_char(&plain, b)); + (a != b || plain.is_empty()).then_some(a.min(b)) +} + /// [`refresh_with_leading`] with the leading the character at `at` was just given (a character /// style, Text Tweaker). pub(crate) fn refresh_with_given_leading(t: &mut TextObject, at: usize) { @@ -1060,9 +1069,9 @@ fn set_style(s: &mut Session, p: &Value) -> Result { t.edit_paras(span, |pa| pa.justify = j); } // With Block on, the leading goes to the frame's line gap; an empty range styles nothing. - let changed = leading - .filter(|_| !keep_line_gap) - .and_then(|l| range.map_or(Some(0), |r| Some(r.bytes(t)).filter(|(a, b)| a < b).map(|(a, _)| a)).map(|at| (at, l))); + let changed = leading.filter(|_| !keep_line_gap).and_then(|l| { + range.map_or(Some(0), |r| Some(r.bytes(t)).filter(|(a, b)| a < b).and_then(|(a, b)| styled_start(t, a, b))).map(|at| (at, l)) + }); match changed { Some((at, l)) => refresh_with_leading(t, at, l), None => refresh_bounds(t), diff --git a/crates/engine/src/cmd/typecmd/block_tests.rs b/crates/engine/src/cmd/typecmd/block_tests.rs index 84d52e2cb1..e899d1e8e5 100644 --- a/crates/engine/src/cmd/typecmd/block_tests.rs +++ b/crates/engine/src/cmd/typecmd/block_tests.rs @@ -836,6 +836,27 @@ fn a_range_leading_change_with_block_on_moves_the_line_gap() { assert!(s.execute("text.setStyle", &json!({"ids": [id], "leading": 30, "keepLineGap": 1})).is_err(), "keepLineGap takes a bool"); } +/// A range inside one character styles nothing, so it leaves the line gap. +#[test] +fn a_leading_on_a_range_inside_one_character_leaves_the_line_gap() { + let mut s = session(); + let id = area(&mut s, 300.0, 400.0, "\u{e9}t\u{e9}\nsecond line"); + s.execute("type.dynamicText", &json!({"on": true})).unwrap(); + s.execute("charStyle.new", &json!({"name": "Loose", "attrs": {"leading": 50}})).unwrap(); + let gap = text(&s, id).area.line_gap; + let range = json!({"start": 0, "end": 1}); + let mut setstyle = json!({"ids": [id], "leading": 50}); + let mut setrange = json!({"id": id, "leading": 50}); + let mut apply = json!({"name": "Loose", "id": id}); + for p in [&mut setstyle, &mut setrange, &mut apply] { + p.as_object_mut().unwrap().extend(range.as_object().unwrap().clone()); + } + for (c, p) in [("text.setStyle", setstyle), ("text.setRangeStyle", setrange), ("charStyle.apply", apply)] { + s.execute(c, &p).unwrap_or_else(|e| panic!("{c}: {e}")); + assert_eq!(text(&s, id).area.line_gap, gap, "{c}"); + } +} + /// `type.step {attribute: "leading"}` (Alt+Up/Down) on Block text moves the line gap by the step. #[test] fn a_leading_step_with_block_on_moves_the_line_gap() {