Dynamic Text: a leading on a range inside one character leaves Line Gap

text.setStyle, text.setRangeStyle and charStyle.apply style a range
with both ends moved back to character boundaries, so a range inside
one multibyte character styles nothing. The Line Gap now follows the
same range and stays as it is when no character was styled.
This commit is contained in:
dotCooCoo committed 2026-11-21 03:29:21 -08:00
1 parent c3905aee4f
commit e7bafe3d17
4 files changed
+36 -6

No files matched your search

+2 -2
View File
@@ -1252,8 +1252,8 @@ fn set_range_style(s: &mut Session, p: &Value) -> Result<Value> {
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 };
+1 -1
View File
@@ -1043,7 +1043,7 @@ fn apply(s: &mut Session, p: &Value, kind: Kind) -> Result<Value> {
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();
+12 -3
View File
@@ -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<usize> {
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<Value> {
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),
@@ -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() {