diff --git a/crates/engine/src/cmd/references/toc.rs b/crates/engine/src/cmd/references/toc.rs index d0c889a5ed..a267f9187f 100644 --- a/crates/engine/src/cmd/references/toc.rs +++ b/crates/engine/src/cmd/references/toc.rs @@ -67,13 +67,15 @@ pub fn toc_instr(v: &Value) -> String { s } -/// A TOC entry: the paragraph it points at, its level (0-based), its text, and the leading page -/// breaks of that paragraph (its page is where the text after them lands). +/// A TOC entry: the paragraph it points at, its level (0-based), its text, the leading page +/// breaks of that paragraph (its page is where the text after them lands), and, for a number +/// followed by a tab, how far the heading's text starts after where its number starts. struct TocEntry { path: Path, level: u8, text: String, lead: usize, + number_gap: Option, } /// The headings of the body, in document order, as (body block, level 1–9, text, leading page @@ -117,9 +119,13 @@ fn toc_entries(s: &Session, f: &FieldCode) -> Vec { .and_then(|n| counters.next_label(&s.doc.numbering, n.num, n.level)) .filter(|(label, def)| !label.is_empty() && !matches!(def.format, NumFormat::Bullet | NumFormat::None)) .map(|(label, def)| match def.suffix { - LevelSuffix::Tab => format!("{label}\t"), - LevelSuffix::Space => format!("{label} "), - LevelSuffix::Nothing => label, + LevelSuffix::Tab => { + let start = def.indent - def.hanging; + let gap = def.tab.filter(|t| *t > start).unwrap_or(def.indent) - start; + (format!("{label}\t"), Some(gap).filter(|g| g.is_finite() && *g > 0.0)) + } + LevelSuffix::Space => (format!("{label} "), None), + LevelSuffix::Nothing => (label, None), }); let style = p.props.style.as_deref().unwrap_or("Normal"); if style.starts_with("TOC") { @@ -144,8 +150,9 @@ fn toc_entries(s: &Session, f: &FieldCode) -> Vec { if let Some(l) = by_style.or(by_outline).or(if hidden { None } else { by_heading }) { let t = p.plain_text().replace(['\u{000C}', '\n', '\t'], " ").trim().to_string(); if !t.is_empty() { - let text = number.map_or_else(|| t.clone(), |n| format!("{n}{t}")); - v.push(TocEntry { path: path.clone(), level: l - 1, text, lead }); + let text = number.as_ref().map_or_else(|| t.clone(), |(n, _)| format!("{n}{t}")); + let number_gap = number.as_ref().and_then(|(_, g)| *g); + v.push(TocEntry { path: path.clone(), level: l - 1, text, lead, number_gap }); } } if let Some(id) = &tc { @@ -159,7 +166,7 @@ fn toc_entries(s: &Session, f: &FieldCode) -> Vec { let l = c.arg('l').and_then(|l| l.trim().parse::().ok()).unwrap_or(1).clamp(1, 9); let text = c.args.first().map(|t| t.trim().to_string()).unwrap_or_default(); if (id.is_empty() || *id == kind) && (tc_levels.0..=tc_levels.1).contains(&l) && !text.is_empty() { - v.push(TocEntry { path: path.clone(), level: l - 1, text, lead: off }); + v.push(TocEntry { path: path.clone(), level: l - 1, text, lead: off, number_gap: None }); } } } @@ -595,6 +602,16 @@ fn regenerate_toc(s: &mut Session, leader: Option, tabs: &[(String, V para.mark = CharProps::default(); if e.text.contains('\t') { para.props.tabs = tabs.iter().find(|(style, _)| Some(style) == para.props.style.as_ref()).map(|(_, tabs)| tabs.clone()); + // Without a stop for the number (LibreOffice's Contents styles have only the page + // number's right stop), its tab would run to the page number: the entry gets a + // left stop as far after its indent as the heading's text is after its number. + let style = s.doc.styles.resolve_para(¶.props); + let has_stop = + style.tabs.iter().any(|t| t.pos < width - 1.0 && matches!(t.align, TabAlign::Left | TabAlign::Center | TabAlign::Decimal)); + if let (None, false, Some(gap)) = (¶.props.tabs, has_stop, e.number_gap) { + let pos = style.indent_left.max(0.0) + gap; + para.props.tabs = (pos < width - 1.0).then(|| vec![TabStop { pos, align: TabAlign::Left, leader: TabLeader::None }]); + } } if show_page && sep.contains('\t') { let right = TabStop { pos: width - 0.5, align: TabAlign::Right, leader }; diff --git a/crates/engine/tests/it/odt_toc.rs b/crates/engine/tests/it/odt_toc.rs index 09bd1a63c7..703b63804c 100644 --- a/crates/engine/tests/it/odt_toc.rs +++ b/crates/engine/tests/it/odt_toc.rs @@ -103,3 +103,58 @@ fn an_odt_bibliography_updates_from_its_citations_sources() { assert!(text.iter().any(|t| t.contains("Kilns and Fire")), "{text:?}"); assert!(!text.iter().any(|t| t.contains("stale")), "{text:?}"); } + +/// #6486: LibreOffice numbers headings through its outline style, the number followed by a tab, +/// and its Contents styles carry only the right-aligned dotted stop for the page number. After +/// Update Table, the tab after an entry's number stops near the number, not at the page-number +/// stop: the title stays at the left and each entry stays on one line. +#[test] +fn an_odt_toc_with_numbered_headings_keeps_its_entries_on_one_line() { + let outline = |level: u8, cm: f32| { + format!( + r#""# + ) + }; + let contents = |n: u8, indent: f32| { + format!( + r#""# + ) + }; + let styles = format!( + r#"{}{}{}{}"#, + contents(1, 0.0), + contents(2, 0.499), + outline(1, 0.762), + outline(2, 1.016) + ); + let content = format!( + r#" + 1. Planning11.1. Budget1 + PlanningText. + BudgetMore text. + "# + ); + let mut z = craft_zip::Writer::new(); + for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("styles.xml", styles.as_str()), ("content.xml", content.as_str())] + { + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); + } + let mut s = Session::new(io::open_bytes("toc.odt", &z.finish().unwrap()).unwrap()); + s.run("references.updateToc", &json!({})).unwrap(); + let layout = s.layout(); + let mut checked = 0; + for (i, b) in s.doc.body.iter().enumerate() { + let Some(p) = b.as_para().filter(|p| p.props.style.as_deref().is_some_and(|st| st.starts_with("TOC") && st != "TOCHeading")) else { + continue; + }; + let Some(tab) = p.text.find('\t') else { continue }; + let pos = |off| wordcraft_doc::Pos { story: wordcraft_doc::StoryRef::Body, path: wordcraft_doc::Path::top(i), off }; + let title = layout.caret(&pos(tab + 1)).unwrap(); + let end = layout.caret(&pos(p.len())).unwrap(); + assert!(p.plain_text().contains("Planning") || p.plain_text().contains("Budget"), "{:?}", p.text); + assert_eq!((title.page, title.top), (end.page, end.top), "{:?} wraps", p.text); + assert!(title.x < 200.0, "{:?}: the title starts at {}", p.text, title.x); + checked += 1; + } + assert_eq!(checked, 2); +}