Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

M7: Table of Contents (generate/update, TOC Title/Level styles, page names via composition); tab leaders in the composer

Brandon Thomasdf7f506b05de
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index c4924347a..f61972434 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -902,10 +902,16 @@ fn layout_line(
let measure = x1 - x0;
let mut x = 0.0;
let mut i = 0;
+ let mut leaders: Vec<(usize, String)> = Vec::new();
while i < line.len() {
if line[i].ch == '\t' {
let abs = x0 + x - tab_origin;
let stop = pp.tabs.iter().find(|t| t.position > abs + 0.01).cloned();
+ if let Some(t) = &stop
+ && !t.leader.is_empty()
+ {
+ leaders.push((i, t.leader.clone()));
+ }
let (pos, align) = match &stop {
Some(t) => (t.position, t.align),
None => (((abs / DEFAULT_TAB).floor() + 1.0) * DEFAULT_TAB, TabAlign::Left),
@@ -1024,12 +1030,54 @@ fn layout_line(
p.adv *= scale[i];
}
p.adv += add[i];
+ let leader = leaders.iter().find(|(k, _)| *k == i).map(|(_, l)| l.as_str());
+ let (tab_x, tab_w) = (x, p.adv);
x += p.adv;
out.push(p);
+ if let Some(l) = leader {
+ tab_leader(g, l, tab_x, tab_w, tab_origin, &mut out);
+ }
}
(out, x, ratio)
}
+/// Fill a tab's gap with repeats of its leader, on a grid shared by all lines (so dots align).
+fn tab_leader(tab: &Glyph, leader: &str, x: f64, w: f64, origin: f64, out: &mut Vec<PlacedGlyph>) {
+ let glyphs: Vec<(u32, f64)> = leader
+ .chars()
+ .map(|c| {
+ let gid = designcraft_fonts::first_glyph(&tab.face, &[c]);
+ (gid, tab.face.advance(gid) * tab.sx)
+ })
+ .collect();
+ let pw: f64 = glyphs.iter().map(|g| g.1).sum();
+ if pw <= 0.01 || w < pw {
+ return;
+ }
+ let mut at = origin + ((x - origin) / pw).ceil() * pw;
+ let end = x + w;
+ let mut n = 0;
+ while at + pw <= end + 0.01 && n < 2000 {
+ for &(gid, adv) in &glyphs {
+ out.push(PlacedGlyph {
+ face: tab.face.clone(),
+ gid,
+ x: at,
+ y: -tab.shift + tab.dy,
+ adv: 0.0,
+ sx: tab.sx,
+ sy: tab.sy,
+ style: tab.style,
+ byte: tab.byte,
+ len: 0,
+ visible: true,
+ });
+ at += adv;
+ }
+ n += 1;
+ }
+}
+
/// Share `extra` points among word spaces, letter gaps and glyph scaling (see [`layout_line`]).
fn distribute(line: &[Glyph], spaces: &[usize], extra: f64, sp: &Spacing, add: &mut [f64], scale: &mut [f64]) {
let stretch = extra >= 0.0;
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index 9378d8678..3db8ebf94 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -569,3 +569,25 @@ fn tabs_without_stops_use_default_half_inch() {
let b = l.glyphs.iter().find(|g| g.byte == 2).expect("B");
assert!((b.x - 36.0).abs() < 0.5, "B at {}", b.x);
}
+
+#[test]
+fn tab_leaders_fill_the_gap() {
+ let mut doc = designcraft_doc::Document::new(&designcraft_doc::build::NewDocument::default());
+ let lid = doc.default_layer();
+ let pf = designcraft_doc::ParaFormat {
+ para: designcraft_doc::ParaAttrs {
+ tabs: Some(vec![designcraft_doc::TabStop { position: 200.0, align: TabAlign::Right, leader: ".".into(), align_on: String::new() }]),
+ ..Default::default()
+ },
+ ..Default::default()
+ };
+ let (_, sid) =
+ doc.add_text_frame(designcraft_doc::SpreadRef::Doc(0), designcraft_geom::Rect::new(0.0, 0.0, 300.0, 100.0), lid, "Intro\t12", pf).unwrap();
+ let cs = crate::compose_story(&doc, sid, &Default::default());
+ let line = &cs.frames[0].lines[0];
+ let dots: Vec<&crate::PlacedGlyph> = line.glyphs.iter().filter(|g| g.len == 0 && g.visible).collect();
+ assert!(dots.len() > 20, "{} leader glyphs", dots.len());
+ let last_dot = dots.iter().map(|g| g.x).fold(0.0, f64::max);
+ let num_x = line.glyphs.iter().find(|g| g.byte == 6).unwrap().x;
+ assert!(last_dot < num_x, "leaders stop before the page number");
+}
diff --git a/crates/doc/src/build.rs b/crates/doc/src/build.rs
index 582dd2848..605655334 100644
--- a/crates/doc/src/build.rs
+++ b/crates/doc/src/build.rs
@@ -138,6 +138,7 @@ impl Document {
hyperlinks: vec![],
bookmarks: vec![],
user_words: vec![],
+ toc: None,
next_id: 0,
};
let lid = LayerId(d.alloc());
diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs
index e8daf17b2..3450f8d50 100644
--- a/crates/doc/src/lib.rs
+++ b/crates/doc/src/lib.rs
@@ -235,6 +235,9 @@ pub struct Document {
/// Words added to the document's user dictionary (spelling).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub user_words: Vec<String>,
+ /// The generated table of contents (Layout → Table of Contents), kept for Update.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub toc: Option<Toc>,
pub next_id: u64,
}
@@ -265,6 +268,30 @@ pub struct Hyperlink {
pub dest: HyperlinkDest,
}
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct TocEntry {
+ /// Paragraph style whose paragraphs are listed.
+ pub style: String,
+ /// 1-based nesting level.
+ #[serde(default = "one")]
+ pub level: u8,
+}
+
+fn one() -> u8 {
+ 1
+}
+
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct Toc {
+ pub story: StoryId,
+ pub title: String,
+ pub entries: Vec<TocEntry>,
+ #[serde(default)]
+ pub page_numbers: bool,
+}
+
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Bookmark {
diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs
index 08d1e36b2..2584d0bcd 100644
--- a/crates/engine/src/cmd/mod.rs
+++ b/crates/engine/src/cmd/mod.rs
@@ -14,6 +14,7 @@ pub mod preflight;
pub mod spelling;
mod style;
pub mod text;
+mod toc;
use designcraft_doc::{ItemId, SpreadRef};
use designcraft_geom::{Point, Rect};
@@ -116,6 +117,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
v.extend(layout::specs());
v.extend(inspect::specs());
v.extend(find::specs());
+ v.extend(toc::specs());
v.extend(spelling::specs());
v.extend(datamerge::specs());
v.extend(interactive::specs());
diff --git a/crates/engine/src/cmd/toc.rs b/crates/engine/src/cmd/toc.rs
new file mode 100644
index 000000000..7083e497e
--- /dev/null
+++ b/crates/engine/src/cmd/toc.rs
@@ -0,0 +1,240 @@
+//! Layout → Table of Contents: list the paragraphs of chosen styles with their page names, as a
+//! story with "TOC Title" / "TOC Level n" styles (right tab with dot leader). Update Table of
+//! Contents regenerates the same story after edits.
+
+use designcraft_compose::{ComposeOptions, caret, compose_story};
+use designcraft_doc::{Align, Document, ParaAttrs, ParaFormat, ParagraphStyle, SpreadRef, StoryId, TabAlign, TabStop, Toc, TocEntry};
+use designcraft_geom::Rect;
+use serde_json::{Value, json};
+
+use super::{CommandSpec, bad, cmd, has_doc, str_param};
+use crate::{Result, Session};
+
+pub fn specs() -> Vec<CommandSpec> {
+ vec![
+ cmd!(
+ "toc.generate",
+ "Table of Contents…",
+ ["Layout"],
+ None,
+ "{entries: [{style, level?: 1}] | styles: [names], title?: \"Contents\", pageNumbers?: true, page?: 1-based page for a new frame (its margins), rect?: [x0,y0,x1,y1] (spread coords)} → {story, entries}",
+ has_doc,
+ generate
+ ),
+ cmd!("toc.update", "Update Table of Contents", ["Layout"], None, "{} — regenerate the existing table of contents", has_toc, |s, _| {
+ s.edit(|d, _| {
+ let toc = d.toc.clone().ok_or_else(|| bad("toc.update", "no table of contents"))?;
+ let n = fill(d, &toc, None)?;
+ Ok(json!({"story": toc.story.0, "entries": n}))
+ })
+ }),
+ cmd!(query "toc.entries", "Table of Contents Entries", [], None, "{entries | styles} → [{level, text, page}] without changing the document", has_doc, |s, p| {
+ let entries = entries_param(p)?;
+ let d = &s.doc()?.doc;
+ let skip = d.toc.as_ref().map(|t| t.story);
+ Ok(json!(collect(d, &entries, skip).into_iter().map(|e| json!({"level": e.level, "text": e.text, "page": e.page})).collect::<Vec<_>>()))
+ }),
+ ]
+}
+
+fn has_toc(s: &Session) -> std::result::Result<(), String> {
+ match s.doc() {
+ Ok(st) if st.doc.toc.as_ref().is_some_and(|t| st.doc.story(t.story).is_some()) => Ok(()),
+ _ => Err("the document has no table of contents".into()),
+ }
+}
+
+fn entries_param(p: &Value) -> Result<Vec<TocEntry>> {
+ if let Some(v) = p.get("entries") {
+ return serde_json::from_value(v.clone()).map_err(|e| bad("toc.generate", e.to_string()));
+ }
+ if let Some(a) = p.get("styles").and_then(Value::as_array) {
+ return Ok(a.iter().enumerate().filter_map(|(i, v)| v.as_str().map(|s| TocEntry { style: s.into(), level: (i + 1).min(9) as u8 })).collect());
+ }
+ Err(bad("toc.generate", "give `entries` or `styles`"))
+}
+
+pub struct Found {
+ pub level: u8,
+ pub text: String,
+ pub page: String,
+ key: (usize, i64, i64),
+}
+
+/// Paragraphs in `entries` styles across the document, in page order.
+pub fn collect(d: &Document, entries: &[TocEntry], skip: Option<StoryId>) -> Vec<Found> {
+ let mut out = Vec::new();
+ for (sid, st) in &d.stories {
+ if Some(*sid) == skip || !st.paras.iter().any(|f| entries.iter().any(|e| e.style == f.style)) {
+ continue;
+ }
+ let cs = compose_story(d, *sid, &ComposeOptions::default());
+ for (pi, r) in st.para_ranges().into_iter().enumerate() {
+ let Some(e) = entries.iter().find(|e| e.style == st.paras[pi].style) else { continue };
+ let text: String = st.text[r.clone()]
+ .chars()
+ .filter(|c| !('\u{E000}'..='\u{E0FF}').contains(c))
+ .map(|c| if c == '\t' || c == '\u{2028}' { ' ' } else { c })
+ .collect();
+ let text = text.trim().to_string();
+ if text.is_empty() {
+ continue;
+ }
+ // Overset paragraphs have no page and are left out (as InDesign does).
+ let Some((fi, x, baseline, ..)) = caret(&cs, r.start) else { continue };
+ let Some(ft) = cs.frames.get(fi) else { continue };
+ if r.start >= ft.range.end && r.start != ft.range.start {
+ continue;
+ }
+ let Some(loc) = d.find(ft.frame) else { continue };
+ let SpreadRef::Doc(si) = loc.spread else { continue };
+ let Some(item) = d.item(ft.frame) else { continue };
+ let b = item.bounds();
+ let in_spread = d.spreads[si].page_at_x(b.center().x).unwrap_or(0);
+ let abs = d.first_page_of_spread(si) + in_spread;
+ out.push(Found {
+ level: e.level,
+ text,
+ page: d.page_name(abs),
+ key: (abs, ((b.y0 + baseline) * 10.0) as i64, ((b.x0 + x) * 10.0) as i64),
+ });
+ }
+ }
+ out.sort_by_key(|f| f.key);
+ out
+}
+
+fn ensure_styles(d: &mut Document, levels: u8, width: f64) {
+ let mut want = vec![("TOC Title".to_string(), ParaAttrs { space_after: Some(12.0), ..Default::default() }, Some(16.0))];
+ for l in 1..=levels.max(1) {
+ let indent = (l as f64 - 1.0) * 12.0;
+ want.push((
+ format!("TOC Level {l}"),
+ ParaAttrs {
+ left_indent: Some(indent),
+ space_before: Some(if l == 1 { 4.0 } else { 0.0 }),
+ align: Some(Align::Left),
+ tabs: Some(vec![TabStop { position: width.max(36.0), align: TabAlign::Right, leader: ". ".into(), align_on: String::new() }]),
+ ..Default::default()
+ },
+ None,
+ ));
+ }
+ let missing: Vec<_> = want.into_iter().filter(|(n, ..)| d.styles.para(n).is_none()).collect();
+ if missing.is_empty() {
+ return;
+ }
+ let st = d.styles_mut();
+ for (name, para, size) in missing {
+ let chars = designcraft_doc::CharAttrs { size, ..Default::default() };
+ st.paragraph.push(ParagraphStyle {
+ name,
+ based_on: Some(designcraft_doc::story::BASIC_PARAGRAPH.into()),
+ next_style: None,
+ para,
+ chars,
+ shortcut: String::new(),
+ });
+ }
+}
+
+/// Rewrite the TOC story; returns the number of entries.
+fn fill(d: &mut Document, toc: &Toc, width: Option<f64>) -> Result<usize> {
+ let found = collect(d, &toc.entries, Some(toc.story));
+ let levels = toc.entries.iter().map(|e| e.level).max().unwrap_or(1);
+ let width = width.unwrap_or_else(|| {
+ d.story(toc.story).and_then(|st| st.frames.first()).and_then(|f| d.item(*f)).map(|i| i.text_area().width()).unwrap_or(300.0)
+ });
+ ensure_styles(d, levels, width);
+ let mut text = String::new();
+ let mut styles = Vec::new();
+ if !toc.title.is_empty() {
+ text.push_str(&toc.title);
+ styles.push("TOC Title".to_string());
+ }
+ for f in &found {
+ if !text.is_empty() {
+ text.push('\n');
+ }
+ text.push_str(&f.text);
+ if toc.page_numbers {
+ text.push('\t');
+ text.push_str(&f.page);
+ }
+ styles.push(format!("TOC Level {}", f.level.max(1)));
+ }
+ let st = d.story_mut(toc.story).ok_or(designcraft_doc::DocError::NoStory(toc.story))?;
+ let len = st.len();
+ st.replace(0..len, &text);
+ for (pi, r) in st.para_ranges().into_iter().enumerate() {
+ if let Some(name) = styles.get(pi) {
+ st.format_paras(r.start..r.start, |f| *f = ParaFormat { style: name.clone(), ..Default::default() });
+ }
+ }
+ Ok(found.len())
+}
+
+fn generate(s: &mut Session, p: &Value) -> Result<Value> {
+ let entries = entries_param(p)?;
+ if entries.is_empty() {
+ return Err(bad("toc.generate", "no entries"));
+ }
+ let title = str_param(p, "title").unwrap_or("Contents").to_string();
+ let page_numbers = p.get("pageNumbers").and_then(Value::as_bool).unwrap_or(true);
+ let rect = super::rect_param(p, "rect");
+ let page = p.get("page").and_then(Value::as_u64).unwrap_or(1).max(1) as usize - 1;
+ s.edit(|d, sel| {
+ // Reuse the existing TOC story when there is one; otherwise make a frame.
+ let existing = d.toc.as_ref().map(|t| t.story).filter(|sid| d.story(*sid).is_some());
+ let (story, width) = match existing {
+ Some(sid) => (sid, None),
+ None => {
+ let (si, pi) = d.page_loc(page).ok_or_else(|| bad("toc.generate", format!("no page {}", page + 1)))?;
+ let r: Rect = rect.unwrap_or_else(|| d.spreads[si].pages[pi].margin_rect());
+ let layer = d.default_layer();
+ let (fid, sid) = d.add_text_frame(SpreadRef::Doc(si), r, layer, "", ParaFormat::default())?;
+ *sel = designcraft_doc::Selection::items(vec![fid]);
+ let w = d.item(fid).map(|i| i.text_area().width());
+ (sid, w)
+ }
+ };
+ let toc = Toc { story, title: title.clone(), entries: entries.clone(), page_numbers };
+ let n = fill(d, &toc, width)?;
+ d.toc = Some(toc);
+ Ok(json!({"story": story.0, "entries": n}))
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn toc_lists_headings_in_page_order_and_updates() {
+ let mut s = Session::new();
+ s.execute("file.newSample", &json!({})).unwrap();
+ let pages = s.doc().unwrap().doc.page_count();
+ s.execute("layout.pages.insert", &json!({"count": 1})).unwrap();
+ let r = s.execute("toc.generate", &json!({"styles": ["Headline", "Cover Title"], "page": pages + 1})).unwrap();
+ assert!(r["entries"].as_u64().unwrap() >= 2, "{r}");
+ let d = &s.doc().unwrap().doc;
+ let sid = StoryId(r["story"].as_u64().unwrap());
+ let text = d.story(sid).unwrap().text.clone();
+ let lines: Vec<&str> = text.split('\n').collect();
+ assert_eq!(lines[0], "Contents");
+ // Cover title (page 1) comes before the feature headline (page 2+).
+ let cover = lines.iter().position(|l| l.starts_with("The Quiet")).unwrap();
+ let head = lines.iter().position(|l| l.starts_with("Notes on the Grid")).unwrap();
+ assert!(cover < head);
+ assert!(lines[head].ends_with("\t2") || lines[head].contains('\t'), "{}", lines[head]);
+ assert_eq!(d.story(sid).unwrap().paras[head].style, "TOC Level 1");
+ assert!(d.styles.para("TOC Level 2").is_some());
+ // Edit a heading, then update.
+ let hid = d.stories.iter().find(|(_, st)| st.text.starts_with("Notes on the Grid")).map(|(id, _)| *id).unwrap();
+ s.execute("story.replaceRange", &json!({"story": hid.0, "start": 0, "end": 5, "text": "Thoughts"})).unwrap();
+ s.execute("toc.update", &json!({})).unwrap();
+ let text = s.doc().unwrap().doc.story(sid).unwrap().text.clone();
+ assert!(text.contains("Thoughts on the Grid"), "{text}");
+ s.doc().unwrap().doc.check().unwrap();
+ }
+}
diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rs
index 3fc536751..ca67d76cd 100644
--- a/crates/idml/src/import.rs
+++ b/crates/idml/src/import.rs
@@ -1354,6 +1354,7 @@ impl<'r> Importer<'r> {
hyperlinks: vec![],
bookmarks: vec![],
user_words: vec![],
+ toc: None,
next_id: self.next_id,
};
Ok(d)