diff --git a/crates/engine/src/cmd/citations.rs b/crates/engine/src/cmd/citations.rs index 87e83fac77..f889790a56 100644 --- a/crates/engine/src/cmd/citations.rs +++ b/crates/engine/src/cmd/citations.rs @@ -19,6 +19,7 @@ mod sources; mod styles; use cross_refs::cross_ref; +pub(crate) use cross_refs::leading_breaks; pub use cross_refs::{caption_labels, ref_targets}; #[cfg(test)] use generated::ENTRY_STYLE; diff --git a/crates/engine/src/cmd/citations/cross_refs.rs b/crates/engine/src/cmd/citations/cross_refs.rs index 265a73fabc..6c35c27797 100644 --- a/crates/engine/src/cmd/citations/cross_refs.rs +++ b/crates/engine/src/cmd/citations/cross_refs.rs @@ -1,5 +1,5 @@ -use super::generated::page_of; use super::{CharProps, CmdError, CmdResult, Document, InlineObject, Paragraph, Pos, Selection, Session, StoryRef, Value, delete_selection, json, p}; +use crate::cmd::references::page_label; pub(super) fn cross_ref(s: &mut Session, v: &Value) -> CmdResult { let to = p::str(v, "to").unwrap_or("heading"); @@ -10,9 +10,10 @@ pub(super) fn cross_ref(s: &mut Session, v: &Value) -> CmdResult { let find = |ts: Vec<(String, Pos)>| ts.into_iter().find(|(t, _)| t == target || t.starts_with(target)); let (text, pos) = find(targets).ok_or_else(|| CmdError::Params(format!("no {to} `{target}`")))?; let show = p::str(v, "show").unwrap_or("text"); - let page = page_of(s, &pos); let result = match show { - "page" => page.to_string(), + // The page the target's text starts on (after its leading page breaks), as its footer + // shows it. + "page" => page_label(s, &Pos { off: leading_breaks(&s.doc, &pos), ..pos.clone() }), "number" => text.split(':').next().unwrap_or(&text).trim().to_string(), "aboveBelow" => { if pos < s.sel.focus { @@ -152,6 +153,23 @@ pub fn ref_targets(s: &Session, to: &str) -> Vec<(String, Pos)> { targets } +/// The offset just past the page breaks at the start of the paragraph at `pos`, skipping bookmark +/// marks among them (a `_Ref` bookmark may start before the breaks): its text starts there. +pub(crate) fn leading_breaks(doc: &Document, pos: &Pos) -> usize { + let Some(p) = doc.para(pos.story, &pos.path) else { return 0 }; + let mut after = 0; + for (i, c) in p.text.char_indices() { + if c == '\u{0C}' { + after = i.saturating_add(c.len_utf8()); + } else if c != wordcraft_doc::para::OBJ + || !matches!(p.object_at(i), Some(InlineObject::BookmarkStart { .. } | InlineObject::BookmarkEnd { .. })) + { + break; + } + } + after +} + /// The hidden `_Ref` bookmark around the target paragraph at `pos` (its label and number only /// when `number_only`), reusing one already there. Returns its name and `at` moved past the /// bookmark marks inserted before it. @@ -159,16 +177,22 @@ fn ref_bookmark(s: &mut Session, pos: &Pos, number_only: bool, mut at: Pos) -> R const MARK: usize = wordcraft_doc::para::OBJ.len_utf8(); let para = s.doc.para(pos.story, &pos.path).ok_or_else(|| CmdError::Params("cross-reference target is gone".into()))?; let end = if number_only { para.text.find(':').unwrap_or(para.len()) } else { para.len() }; - if let Some(InlineObject::BookmarkStart { name }) = para.object_at(0) - && name.starts_with("_Ref") - { - // Reuse it when it covers the same extent: up to the colon, or the whole paragraph. - let same = para.object_offsets().into_iter().any(|o| { - matches!(para.object_at(o), Some(InlineObject::BookmarkEnd { name: n }) if n == name) - && para.text.get(o.saturating_add(MARK)..).is_some_and(|rest| if number_only { rest.starts_with(':') } else { rest.is_empty() }) - }); - if same { - return Ok((name.clone(), at)); + // The bookmark starts after the leading page breaks, so `PAGEREF` reads the page the text is on. + let start = leading_breaks(&s.doc, pos).min(end); + // Reuse one that starts there or at the paragraph's start (before the breaks): the field's + // result still reads the page the text is on. + for from in [start, 0] { + if let Some(InlineObject::BookmarkStart { name }) = para.object_at(from) + && name.starts_with("_Ref") + { + // Reuse it when it covers the same extent: up to the colon, or the whole paragraph. + let same = para.object_offsets().into_iter().any(|o| { + matches!(para.object_at(o), Some(InlineObject::BookmarkEnd { name: n }) if n == name) + && para.text.get(o.saturating_add(MARK)..).is_some_and(|rest| if number_only { rest.starts_with(':') } else { rest.is_empty() }) + }); + if same { + return Ok((name.clone(), at)); + } } } let next = @@ -176,11 +200,11 @@ fn ref_bookmark(s: &mut Session, pos: &Pos, number_only: bool, mut at: Pos) -> R let name = format!("_Ref{next:09}"); let props = CharProps::default(); s.doc.insert_object(&Pos { off: end, ..pos.clone() }, InlineObject::BookmarkEnd { name: name.clone() }, &props)?; - s.doc.insert_object(&Pos { off: 0, ..pos.clone() }, InlineObject::BookmarkStart { name: name.clone() }, &props)?; + s.doc.insert_object(&Pos { off: start, ..pos.clone() }, InlineObject::BookmarkStart { name: name.clone() }, &props)?; if at.story == pos.story && at.path == pos.path { if at.off >= end { at.off = at.off.saturating_add(2 * MARK); - } else if at.off > 0 { + } else if at.off > start { at.off = at.off.saturating_add(MARK); } } diff --git a/crates/engine/src/cmd/citations/generated.rs b/crates/engine/src/cmd/citations/generated.rs index 60b0b89882..b234948c68 100644 --- a/crates/engine/src/cmd/citations/generated.rs +++ b/crates/engine/src/cmd/citations/generated.rs @@ -6,7 +6,9 @@ use super::{ Block, CharProps, CmdError, CmdResult, Document, FieldCode, InlineObject, Paragraph, Path, Pos, Session, SortKey, StoryRef, TabAlign, TabLeader, TabStop, Value, delete_selection, heading_letter, p, sel_result, sort_key, }; +use crate::cmd::references::{page_at, page_formats, page_text}; use std::sync::Arc; +use wordcraft_doc::section::NumFormat; /// The TOC field for `references.tableOfFigures` (ECMA-376 ยง17.16.5.68): `\c "