Skip to content

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

Parent-page composition: the page's name is part of the cache signature

Sean Hammond5898c084b6d5
diff --git a/crates/compose/src/cache.rs b/crates/compose/src/cache.rs
index ef06d016d..8aabbf0f1 100644
--- a/crates/compose/src/cache.rs
+++ b/crates/compose/src/cache.rs
@@ -64,6 +64,15 @@ impl Cache {
let Some(story) = doc.stories.get(&sid) else { return Arc::new(ComposedStory { story: sid, ..Default::default() }) };
let (mut sig, keep_items) = signature(doc, story);
let key = (sid, page);
+ // The key holds the page's index, which a renumbering doesn't change: its name (section
+ // start number, style, prefix) is part of the signature.
+ let page_name = page.map(|p| doc.page_name(p));
+ if let Some(name) = &page_name {
+ use std::hash::{Hash, Hasher};
+ let mut h = std::collections::hash_map::DefaultHasher::new();
+ name.hash(&mut h);
+ sig.push(h.finish() as usize);
+ }
let with_vars = crate::vars::has_vars(story);
let running = with_vars && crate::vars::has_running(doc, story);
if with_vars {
@@ -130,7 +139,6 @@ impl Cache {
}
}
// Parent-page items are composed per page: markers show its name, variables need its index.
- let page_name = page.map(|p| doc.page_name(p));
let page = page.filter(|_| with_vars);
let running = running.then(|| self.running_index(doc));
let out = Arc::new(crate::compose_story(
diff --git a/crates/engine/src/cmd/variables.rs b/crates/engine/src/cmd/variables.rs
index b665d3161..e7772d4ea 100644
--- a/crates/engine/src/cmd/variables.rs
+++ b/crates/engine/src/cmd/variables.rs
@@ -129,6 +129,30 @@ fn define(s: &mut Session, p: &Value) -> Result<Value> {
mod tests {
use super::*;
+ /// A parent-page page-number marker follows Numbering & Section Options: changing a section's
+ /// start number renames its pages without changing their index or the number of sections, and
+ /// the marker composed for a page shows the new name.
+ #[test]
+ fn parent_page_number_follows_section_renumbering() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({"pages": 3, "facingPages": false})).unwrap();
+ let marker = designcraft_doc::story::PAGE_NUMBER.to_string();
+ let r = s
+ .execute("frame.create", &json!({"spread": {"kind": "parent", "index": 0}, "rect": [72, 30, 500, 60], "content": "text", "text": marker}))
+ .unwrap();
+ let sid = s.doc().unwrap().doc.item(designcraft_doc::ItemId(r["id"].as_u64().unwrap())).unwrap().text_frame().unwrap().story;
+ assert_eq!(s.doc().unwrap().doc.story(sid).unwrap().text, marker);
+ let shown = |s: &Session, page: usize| -> usize {
+ let d = s.doc().unwrap().doc.clone();
+ let cs = s.cache.get(&d, sid, Some(page));
+ cs.frames.iter().flat_map(|f| f.lines.iter()).map(|l| l.glyphs.iter().filter(|g| g.visible && g.adv > 0.0).count()).sum()
+ };
+ assert_eq!(shown(&s, 2), 1, "page 3");
+ s.execute("layout.section", &json!({"page": 1, "startNumber": 100})).unwrap();
+ assert_eq!(s.doc().unwrap().doc.page_name(2), "102");
+ assert_eq!(shown(&s, 2), 3, "page 102 after renumbering");
+ }
+
/// Two sections that both number from 1 repeat page names ("1, 2, 1, 2"). A parent-page running
/// header is composed for the page it is shown on, not the first page with that name.
#[test]