Merge pull request #2169 from ljkx/indent-to-here

Text: Indent to Here indents the paragraph's later lines to the character (#2080)
This commit is contained in:
Brandon Thomas authored and GitHub committed 2026-11-02 23:23:33 -05:00
commit f87f232724
2 files changed
+185 -5

No files matched your search

+50 -5
View File
@@ -1817,6 +1817,8 @@ fn place_sub_objects(
}
}
/// Sets one paragraph's lines, column after column and frame after frame. It stops when the
/// paragraph ends, when the frames run out, or when an earlier paragraph has to be set again.
fn place_paragraph_lines(shared: PlacementShared<'_, '_>, paragraph: ParagraphLines<'_>) -> LinePlacement {
let PlacementShared {
doc,
@@ -1849,7 +1851,7 @@ fn place_paragraph_lines(shared: PlacementShared<'_, '_>, paragraph: ParagraphLi
let ParagraphLines {
paragraph: pi,
range: prange,
props: pp,
props: para_props,
glyphs,
hyph_after,
drop_cap,
@@ -1878,7 +1880,10 @@ fn place_paragraph_lines(shared: PlacementShared<'_, '_>, paragraph: ParagraphLi
let mut hyphen_at: Option<usize> = None;
let has_tabs = glyphs.iter().any(|g| g.ch == '\t');
let mut first_line_rect: Option<(usize, f64, f64, f64)> = None; // frame, baseline, ascent, x-span
let rtl = pp.direction == designcraft_doc::TextDirection::RightToLeft;
let rtl = para_props.direction == designcraft_doc::TextDirection::RightToLeft;
// The paragraph's settings with the start-side indent an Indent to Here character set for
// the lines after it (InDesign's ^i): those lines start where the character sits.
let mut indented: Option<designcraft_doc::ParaProps> = None;
// Paragraph gyoudori: the (frame, column) of the block and the grid line after it.
let mut para_block: Option<((usize, usize), f64)> = None;
// Segments of a line already placed (beside an object with text on both sides) that the
@@ -1912,6 +1917,10 @@ fn place_paragraph_lines(shared: PlacementShared<'_, '_>, paragraph: ParagraphLi
no_hyph,
});
}
let pp = indented.as_ref().unwrap_or(para_props);
// The Indent to Here character sits past the drop cap, so the lines that start at it
// aren't indented by the drop cap again.
let beside_cap = drop_cap.filter(|_| indented.is_none());
let f = &frames[cur.fi];
let base = cols[cur.fi][cur.col.min(cols[cur.fi].len() - 1)];
let col = cur.clip(base);
@@ -1937,7 +1946,7 @@ fn place_paragraph_lines(shared: PlacementShared<'_, '_>, paragraph: ParagraphLi
props: pp,
slots: &slots,
column: col,
drop_cap,
drop_cap: beside_cap,
line_no,
col_first_line,
rtl,
@@ -1945,6 +1954,8 @@ fn place_paragraph_lines(shared: PlacementShared<'_, '_>, paragraph: ParagraphLi
spacing,
});
let mut moved = false;
// Set by a line with an Indent to Here character, for the lines broken again after it.
let mut reindented = None;
for (k, b) in breaks.iter().enumerate() {
let (s, e) = (g0 + b.start, g0 + b.end);
let line_glyphs = &glyphs[s..e.max(s)];
@@ -2060,7 +2071,7 @@ fn place_paragraph_lines(shared: PlacementShared<'_, '_>, paragraph: ParagraphLi
let col_w = notes.area(&cols[cur.fi], cur.col).map_or(0.0, |r| r.width());
let note_column = NoteColumn { frame_index: cur.fi, column_index: cur.col, width: col_w, frame: f };
// A drop cap sits at the start-side indent; the first-line indent doesn't move it.
let beside = drop_cap.filter(|dc| line_no < dc.lines && col_first_line == 0).map(|dc| dc.indent());
let beside = beside_cap.filter(|dc| line_no < dc.lines && col_first_line == 0).map(|dc| dc.indent());
let first_line = if line_no == 0 { pp.first_line_indent } else { 0.0 };
let (ind_l, ind_r) = match beside {
Some(w) if rtl => (pp.left_indent, pp.right_indent + w),
@@ -2112,6 +2123,7 @@ fn place_paragraph_lines(shared: PlacementShared<'_, '_>, paragraph: ParagraphLi
}
ruby::annotate(db, styles_tab, &story.text, &mut placed, doc.settings.glyph_fallback, f.vertical);
kenten::annotate(db, styles_tab, &mut placed, doc.settings.glyph_fallback);
let here_props = indent_here(&placed, pp, x0, x1, rtl);
let range_end = if last { prange.end } else { glyphs.get(g0 + b.next).map(|g| g.byte).unwrap_or(prange.end) };
let range_start = glyphs.get(s).map(|g| g.byte).unwrap_or(prange.start).min(range_end);
let range_start = if line_no == 0 { prange.start } else { range_start };
@@ -2170,6 +2182,10 @@ fn place_paragraph_lines(shared: PlacementShared<'_, '_>, paragraph: ParagraphLi
hyphen_at =
e.checked_sub(1).filter(|&i| b.hyphen && hyph_after.get(i) == Some(&true)).and_then(|i| glyphs.get(i)).map(|g| g.byte + g.len);
line_no += 1;
// An Indent to Here character in this line moves the start of the lines after it:
// break them again with that indent (it holds into the next columns too).
let reindent = here_props.is_some();
reindented = here_props.or(reindented);
// A column / frame / page break inside the paragraph: the rest of it continues in the
// new column/frame/page, re-broken there. (A break that ends the paragraph moves the
// next paragraph instead.)
@@ -2183,12 +2199,13 @@ fn place_paragraph_lines(shared: PlacementShared<'_, '_>, paragraph: ParagraphLi
}
// An own-story anchored object anchored in this line wraps the lines after it,
// which were broken without it: break them again.
if k + 1 < breaks.len() && line_has_wrapping_exclusion(f, range_start..range_end, col, baseline) {
if k + 1 < breaks.len() && (reindent || line_has_wrapping_exclusion(f, range_start..range_end, col, baseline)) {
g0 += b.next;
moved = true;
break;
}
}
indented = reindented.or(indented);
if !moved {
break;
}
@@ -4949,6 +4966,34 @@ fn distribute(line: &[Glyph], spaces: &[usize], extra: f64, sp: &Spacing, add: &
}
}
/// The paragraph settings for the lines after the `placed` line when it holds an Indent to Here
/// character (the last one, if several): the start-side indent reaches the character, measured
/// from the start edge of the line's segment `x0..x1`. Like the paragraph's own indents it then
/// applies from each later line's segment, so beside a text wrap it starts at the wrap's edge.
/// `None` when there is none or the indent is unchanged.
fn indent_here(placed: &[PlacedGlyph], pp: &designcraft_doc::ParaProps, x0: f64, x1: f64, rtl: bool) -> Option<designcraft_doc::ParaProps> {
// The placed glyphs are in the line's order (bidi only moves them): the drop cap's, then a
// list label's, which is set beside the drop cap, then the text's. A character in the drop
// cap or the label counts too, and one in the label comes after one in the drop cap.
let x = placed.iter().rev().find(|g| g.rendered_char == story::INDENT_HERE)?.x;
let indent = if rtl { x1 - x } else { x - x0 };
if !indent.is_finite() {
return None;
}
let indent = indent.max(0.0);
let current = if rtl { pp.right_indent } else { pp.left_indent };
if (indent - current).abs() < 0.01 {
return None;
}
let mut props = pp.clone();
if rtl {
props.right_indent = indent;
} else {
props.left_indent = indent;
}
Some(props)
}
fn place(g: &Glyph, x: f64) -> PlacedGlyph {
let visible = !(g.ch == '\t'
|| g.ch == '\n'
+135
View File
@@ -1906,6 +1906,141 @@ fn tab_leaders_fill_the_gap() {
assert!(last_dot < num_x, "leaders stop before the page number");
}
#[test]
fn indent_to_here_starts_the_paragraphs_later_lines_at_the_character() {
// "1.", Indent to Here, then text long enough to wrap: every line after the first starts
// where the character sits, in this frame and the next one (#2080).
let text = format!("1.\u{e005}{}", [LOREM; 3].join(" "));
let para = ParaAttrs { left_indent: Some(6.0), ..Default::default() };
let (mut d, sid, a) = doc_with(&text, Rect::new(72.0, 36.0, 272.0, 96.0), para.clone());
let lid = d.default_layer();
let (b, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(72.0, 200.0, 272.0, 800.0), lid, "", ParaFormat::default()).unwrap();
d.thread(a, b).unwrap();
let cs = compose_story(&d, sid, &Default::default());
assert!(!cs.is_overset());
assert!(cs.frames[0].lines.len() > 1 && !cs.frames[1].lines.is_empty(), "the text wraps into the second frame");
let lines = all_lines(&cs);
let here = lines[0].glyphs.iter().find(|g| g.byte == 2 && g.len > 0).unwrap().x;
assert!(here > 72.0 + 6.0 + 1.0, "the character follows the number");
for (i, line) in lines.iter().enumerate().skip(1) {
let start = line.glyphs.iter().find(|g| g.visible).unwrap().x;
assert!((start - here).abs() < 0.01, "line {i} starts at {start}, not at the indent {here}");
assert!(line.end_x <= 272.0 + 0.01, "line {i} fits the narrower measure");
}
// Without the character the later lines keep the paragraph's own indent.
let (plain, psid, _) = doc_with(&text.replace('\u{e005}', " "), Rect::new(72.0, 36.0, 272.0, 400.0), para);
let pcs = compose_story(&plain, psid, &Default::default());
let start = pcs.frames[0].lines[1].glyphs.iter().find(|g| g.visible).unwrap().x;
assert!((start - 78.0).abs() < 0.01, "plain second line starts at {start}");
}
/// Where each line after the first starts, and the Indent to Here character's x on the first.
fn indent_to_here_starts(text: &str, para: ParaAttrs) -> (f64, Vec<f64>) {
let (d, sid, _) = doc_with(text, Rect::new(72.0, 36.0, 272.0, 600.0), para);
let cs = compose_story(&d, sid, &Default::default());
let lines = all_lines(&cs);
assert!(lines.len() > 4, "the text wraps");
let byte = text.find('\u{e005}').unwrap();
let here = lines[0].glyphs.iter().find(|g| g.byte == byte && g.len > 0).unwrap().x;
let starts = lines.iter().skip(1).map(|l| l.glyphs.iter().find(|g| g.visible).unwrap().x).collect();
(here, starts)
}
#[test]
fn indent_to_here_after_a_tab_hangs_the_text_after_the_number() {
// The usual hanging number: "1.", a tab to the next stop, then Indent to Here.
let tabs = vec![designcraft_doc::TabStop { position: 24.0, align: TabAlign::Left, leader: String::new(), align_on: String::new() }];
let (here, starts) = indent_to_here_starts(&format!("1.\t\u{e005}{LOREM}"), ParaAttrs { tabs: Some(tabs), ..Default::default() });
assert!((here - (72.0 + 24.0)).abs() < 0.01, "the character sits at the tab stop: {here}");
for (i, start) in starts.iter().enumerate() {
assert!((start - here).abs() < 0.01, "line {} starts at {start}, not at {here}", i + 1);
}
}
#[test]
fn indent_to_here_beside_a_drop_cap_is_not_indented_by_the_drop_cap_again() {
// A three-line drop cap, then Indent to Here on the first line: the lines beside the drop
// cap and the lines below it all start at the character, none further in.
let (here, starts) = indent_to_here_starts(&format!("Drop \u{e005}{LOREM}"), drop_cap(3, 1));
for (i, start) in starts.iter().enumerate() {
assert!((start - here).abs() < 0.01, "line {} starts at {start}, not at {here}", i + 1);
}
}
#[test]
fn indent_to_here_inside_the_drop_cap_indents_the_lines_below_it() {
// A two-character drop cap whose second character is Indent to Here: the lines beside the
// drop cap and the ones below it all start at the character, at the drop cap's edge.
let (here, starts) = indent_to_here_starts(&format!("A\u{e005}{LOREM}"), drop_cap(3, 2));
assert!(here > 72.0 + 10.0, "the character sits past the drop cap: {here}");
assert!(starts.len() > 3, "lines below the drop cap");
for (i, start) in starts.iter().enumerate() {
assert!((start - here).abs() < 0.01, "line {} starts at {start}, not at {here}", i + 1);
}
}
#[test]
fn indent_to_here_in_a_list_number_indents_to_the_generated_character() {
// "^#.^i^t": the paragraph's later lines start where the number's Indent to Here sits,
// right after "1.", not at the text after the tab.
let mut para = numbered(designcraft_doc::NumberStyle::Arabic, "^#.^i^t");
para.list_tab = Some(Some(36.0));
let (d, sid, _) = doc_with(LOREM, Rect::new(72.0, 36.0, 272.0, 600.0), para);
let cs = compose_story(&d, sid, &Default::default());
let lines = all_lines(&cs);
assert!(lines.len() > 3, "the text wraps");
let label = |c: char| lines[0].glyphs.iter().find(|g| g.len == 0 && g.rendered_char == c).unwrap();
let (dot, here) = (label('.'), label('\u{e005}').x);
assert!((here - (dot.x + dot.adv)).abs() < 0.01, "the character follows the number: {here}");
let text = lines[0].glyphs.iter().find(|g| g.len > 0).unwrap().x;
assert!((text - (72.0 + 36.0)).abs() < 0.01 && here < text - 1.0, "the text after the tab is further in: {text}");
for (i, line) in lines.iter().enumerate().skip(1) {
let start = line.glyphs.iter().find(|g| g.visible).unwrap().x;
assert!((start - here).abs() < 0.01, "line {i} starts at {start}, not at {here}");
}
}
#[test]
fn indent_to_here_in_a_list_number_after_one_in_the_drop_cap_wins() {
// The drop cap "A^i" sits at the indent and the number "1.^i" follows it on the first line,
// so the number's character is the line's last one: the later lines start there, not at
// the drop cap's.
let mut para = ParaAttrs { drop_cap_lines: Some(3), drop_cap_chars: Some(2), ..numbered(designcraft_doc::NumberStyle::Arabic, "^#.^i^t") };
para.list_tab = Some(Some(36.0));
let (d, sid, _) = doc_with(&format!("A\u{e005}{LOREM}"), Rect::new(72.0, 36.0, 272.0, 600.0), para);
let cs = compose_story(&d, sid, &Default::default());
let lines = all_lines(&cs);
assert!(lines.len() > 4, "the text wraps below the drop cap");
let marker = |label: bool| lines[0].glyphs.iter().find(|g| g.rendered_char == '\u{e005}' && (g.len == 0) == label).unwrap().x;
let (in_cap, in_label) = (marker(false), marker(true));
assert!(in_label > in_cap + 1.0, "the number follows the drop cap: {in_cap} then {in_label}");
for (i, line) in lines.iter().enumerate().skip(1) {
let start = line.glyphs.iter().find(|g| g.visible).unwrap().x;
assert!((start - in_label).abs() < 0.01, "line {i} starts at {start}, not at {in_label}");
}
}
#[test]
fn indent_to_here_in_a_right_to_left_paragraph_indents_from_the_right() {
// Hebrew words (the bidi classes decide the order; no Hebrew font has to be present): the
// lines after the first end where the character sits, measured from the right edge.
let words = "שלום עולם ".repeat(40);
let text = format!("שלום \u{e005}{words}");
let para = ParaAttrs { direction: Some(designcraft_doc::TextDirection::RightToLeft), align: Some(Align::Right), ..Default::default() };
let (d, sid, _) = doc_with(&text, Rect::new(72.0, 36.0, 272.0, 600.0), para);
let cs = compose_story(&d, sid, &Default::default());
let lines = all_lines(&cs);
assert!(lines.len() > 3, "the text wraps");
let byte = text.find('\u{e005}').unwrap();
let here = lines[0].glyphs.iter().find(|g| g.byte == byte && g.len > 0).unwrap().x;
assert!(here < 272.0 - 1.0, "the character follows the first word: {here}");
let right_edge = |l: &Line| l.glyphs.iter().filter(|g| g.visible).map(|g| g.x + g.adv).fold(f64::MIN, f64::max);
for (i, line) in lines.iter().enumerate().skip(1) {
let end = right_edge(line);
assert!((end - here).abs() < 0.01, "line {i} ends at {end}, not at {here}");
}
}
#[test]
fn right_indent_tab_leaders_follow_stop_precedence_without_moving_text() {
let text = "Intro\u{e006}12";