Files
designcraft/crates/compose/src/xref.rs
T
Claude 4e263ad821 Cross-references keep edited text and break at spaces
InDesign keeps text typed over a cross-reference's generated text (until the
cross-reference is updated), with its page number still live. The IDML
importer dropped that text and generated it again from the format, so a
credits list whose format reads "Last, First. ... YYYY. Volume X, issue Y."
showed those placeholders instead of the names and dates typed in.

- CrossRef gains `text` (edited text, `<pageNum />` for the live page) and
  `page` (the page last shown). Import keeps saved text that no longer
  reads as its format produces it; export writes it back with InDesign's
  XRefPageNumber variable. Setting a format drops the edits, as InDesign does.
- Edited text whose destination is gone shows its saved page, not "??".
- Spaces in a cross-reference's text are word spaces the line may break at
  (it was set as one unbreakable piece, so long ones broke mid-word).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CgGsJkVe5GYG9qK6VFdECa
2026-10-17 20:00:06 +00:00

83 lines
3.3 KiB
Rust

//! Cross-reference resolution: the page each text anchor lands on (from composition) and the
//! generated text of each cross-reference.
use std::collections::HashMap;
use std::sync::Arc;
use designcraft_doc::{Document, Story, StoryId, XREF_MARK, xref};
use crate::ComposedStory;
/// Page name of every text anchor in the document.
#[derive(Debug, Default)]
pub struct XrefIndex {
pages: HashMap<u64, (usize, String)>,
}
pub fn has_xrefs(st: &Story) -> bool {
!st.xrefs.is_empty()
}
impl XrefIndex {
/// `get` composes a story (normally through the cache). Stories that hold cross-references or
/// running headers are composed without resolving them (their anchors' pages hardly move):
/// through the cache they would build this index again, directly or by way of the running-header index.
pub fn build(doc: &Document, get: &dyn Fn(StoryId) -> Arc<ComposedStory>) -> XrefIndex {
let mut pages = HashMap::new();
for (sid, st) in &doc.stories {
if st.anchors.is_empty() {
continue;
}
let indexed = has_xrefs(st) || crate::vars::has_running(doc, st);
let cs = if indexed { Arc::new(crate::compose_story(doc, *sid, &Default::default())) } else { get(*sid) };
for a in &st.anchors {
let Some(pos) = st.anchor_pos(a.id) else { continue };
if let Some((page, ..)) = crate::vars::place(doc, &cs, pos) {
pages.insert(a.id, (page, doc.page_name(page)));
}
}
}
XrefIndex { pages }
}
/// Page name of an anchor.
pub fn page(&self, anchor: u64) -> Option<&str> {
self.pages.get(&anchor).map(|p| p.1.as_str())
}
/// Absolute page index of an anchor.
pub fn page_index(&self, anchor: u64) -> Option<usize> {
self.pages.get(&anchor).map(|p| p.0)
}
}
/// The text a cross-reference shows ("??" when its destination is gone, like an unresolved
/// reference in typesetting, unless the text was edited).
pub fn xref_text(doc: &Document, x: &xref::CrossRef, index: Option<&XrefIndex>) -> String {
// Edited text stays as typed; its page numbers still follow the destination, or show the
// page last shown when the destination is gone (as in InDesign).
if let Some(t) = &x.text {
let live = index.filter(|_| doc.xref_values(x.target).is_some()).and_then(|i| i.page(x.target));
return xref::expand_edited(t, live.or(x.page.as_deref()).unwrap_or("?"));
}
let Some(mut v) = doc.xref_values(x.target) else { return "??".into() };
v.page = index.and_then(|i| i.page(x.target)).unwrap_or("?").to_string();
let def = doc.xref_format(&x.format).map_or("<fullPara />", |f| f.definition.as_str());
xref::expand(def, &v)
}
/// Generated text of the cross-references in `range` of `story`, by byte of their mark.
pub fn texts_in(doc: &Document, story: &Story, range: std::ops::Range<usize>, index: Option<&XrefIndex>) -> HashMap<usize, String> {
let mut out = HashMap::new();
if story.xrefs.is_empty() {
return out;
}
let before = story.text[..range.start].matches(XREF_MARK).count();
for (k, (i, _)) in story.text[range.clone()].match_indices(XREF_MARK).enumerate() {
if let Some(x) = story.xrefs.get(before + k) {
out.insert(range.start + i, xref_text(doc, x, index));
}
}
out
}