Skip to content

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

fix: グループ変換をHTML・EPUBの出力順に反映 #314

haranishi1b6a03e350e9
diff --git a/ROADMAP.md b/ROADMAP.md
index 8a0f82071..539bb28ed 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -2,7 +2,7 @@
**Stage: pre-alpha** · next: alpha, ~0% and ~15–25 h away; beta ~28 points and ~420–750 h beyond that (the core-workflow gate, not the percentage: ready for real work is already ~47%)
-> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-12 · **Change:** minor (cancelable macOS Dock Quit) · **Target:** Adobe InDesign 2026
+> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (grouped HTML/EPUB reading order) · **Target:** Adobe InDesign 2026
DesignCraft aims at full Adobe InDesign parity, and to be better: faster, open (documented JSON
format + IDML), scriptable by agents (MCP), and available on the web.
@@ -129,6 +129,7 @@ list) · [roadmap.md](docs/roadmap.md) (milestones, plan) · [parity-checklist.m
Newest first.
+- **2026-10-13** · HTML and reflowable EPUB order grouped stories and graphics by their transformed spread position, including nested groups and rotated frames. Explicit Article order is unchanged. No readiness percentage change.
- **2026-10-12** · macOS Dock Quit uses the same unsaved-document prompts as window and menu Quit. Repeated requests keep the current answers; Cancel retains every tab and unsaved recovery checkpoint, and completed Save/Don't Save answers exit through the ordinary event loop. No readiness percentage change.
- **2026-10-12** · Orderly native exit writes the latest recovery state after finalizing current quit approvals; incomplete or stale plans keep their documents, and Undo to the saved state clears stale recovery. Unavailable or failed recovery is reported. Cancelable macOS Dock Quit remains open (#544). No readiness percentage change.
- **2026-10-12** · Recovered documents keep a checkpoint until saved, closed or reverted, including a second crash before autosave. Restarted document IDs and explicit recovery folders preserve other snapshots; failed republishing retains the original. No readiness percentage change.
diff --git a/crates/epub/src/lib.rs b/crates/epub/src/lib.rs
index 77b509d0b..a985f6298 100644
--- a/crates/epub/src/lib.rs
+++ b/crates/epub/src/lib.rs
@@ -11,6 +11,7 @@ use std::collections::BTreeMap;
use std::fmt::Write as _;
use std::io::Write;
+use designcraft_doc::geom::{Affine, Rect};
use designcraft_doc::{Align, CharAttrs, CharProps, Content, Document, Item, ItemId, ParaProps, SpreadRef, StoryId, Styles, story};
#[derive(Clone, Debug)]
@@ -514,29 +515,41 @@ pub fn reading_order(doc: &Document) -> Vec<Block> {
let mut seen_stories = Vec::new();
for (si, sp) in doc.spreads.iter().enumerate() {
let first = doc.first_page_of_spread(si);
- let mut items: Vec<&Item> = Vec::new();
- for top in &sp.items {
- top.walk(&mut |i| items.push(i));
- }
- for it in items {
+ // Preserve traversal order while carrying each group's transform to spread space.
+ // The iterator stack grows with nesting depth, without repeated document lookups.
+ let mut stack = vec![(sp.items.iter(), Affine::IDENTITY)];
+ while let Some((items, parent)) = stack.last_mut() {
+ let Some(it) = items.next() else {
+ stack.pop();
+ continue;
+ };
+ let xf = *parent * it.xf;
+ let children = it.children();
+ if !children.is_empty() {
+ stack.push((children.iter(), xf));
+ }
if it.hidden {
continue;
}
- let b = it.bounds();
- let page = first + sp.page_at_x(b.center().x).unwrap_or(0);
- let key = (page, (b.y0 * 10.0) as i64, (b.x0 * 10.0) as i64);
- match &it.content {
+ let block = match &it.content {
Content::Text(tf) => {
let Some(story) = doc.story(tf.story) else { continue };
// A story is placed where its first frame is.
- if story.frames.first() == Some(&it.id) && !seen_stories.contains(&tf.story) && !story.text.trim().is_empty() {
- seen_stories.push(tf.story);
- keyed.push((key, Block::Story(tf.story)));
+ if story.frames.first() != Some(&it.id) || seen_stories.contains(&tf.story) || story.text.trim().is_empty() {
+ continue;
}
+ seen_stories.push(tf.story);
+ Block::Story(tf.story)
}
- Content::Graphic(_) => keyed.push((key, Block::Image(it.id))),
- _ => {}
- }
+ Content::Graphic(_) => Block::Image(it.id),
+ _ => continue,
+ };
+ // Transform the path once: rotating a local axis-aligned box again would
+ // enlarge it and give a different top-left from the actual spread geometry.
+ let b = it.path.transformed(xf).bounds().unwrap_or(Rect::ZERO);
+ let page = first + sp.page_at_x(b.center().x).unwrap_or(0);
+ let key = (page, (b.y0 * 10.0) as i64, (b.x0 * 10.0) as i64);
+ keyed.push((key, block));
}
}
let _ = SpreadRef::Doc(0);
diff --git a/crates/epub/tests/grouped_reading_order.rs b/crates/epub/tests/grouped_reading_order.rs
new file mode 100644
index 000000000..c6f2226ef
--- /dev/null
+++ b/crates/epub/tests/grouped_reading_order.rs
@@ -0,0 +1,159 @@
+use std::io::{Cursor, Read};
+use std::sync::Arc;
+
+use designcraft_doc::build::NewDocument;
+use designcraft_doc::geom::{Affine, PathData, Rect, shapes};
+use designcraft_doc::{Article, Asset, AssetId, Content, Document, Graphic, Item, ItemId, ParaFormat, Shape, SpreadRef, StoryId};
+use designcraft_epub::{Block, EpubOptions, HtmlOptions, export_epub, export_html, reading_order};
+
+fn text(doc: &mut Document, spread: usize, x: f64, y: f64, value: &str) -> (ItemId, StoryId) {
+ doc.add_text_frame(SpreadRef::Doc(spread), Rect::new(x, y, x + 100.0, y + 10.0), doc.default_layer(), value, ParaFormat::default()).unwrap()
+}
+
+fn group(doc: &mut Document, spread: usize, ids: &[ItemId], xf: Affine) -> ItemId {
+ let children = {
+ let sp = doc.spread_mut(SpreadRef::Doc(spread)).unwrap();
+ let children: Vec<_> = ids.iter().map(|id| sp.items.iter().find(|it| it.id == *id).unwrap().clone()).collect();
+ sp.items.retain(|it| !ids.contains(&it.id));
+ children
+ };
+ let id = ItemId(doc.alloc());
+ let mut item = Item::new(id, doc.default_layer(), Shape::Group, PathData::default());
+ item.content = Content::Group { items: children };
+ item.xf = xf;
+ doc.insert_item(SpreadRef::Doc(spread), item, None).unwrap()
+}
+
+fn outputs(doc: &Document) -> [String; 2] {
+ let html = export_html(doc, &HtmlOptions::default());
+ let bytes = export_epub(doc, &EpubOptions::default()).unwrap();
+ let mut zip = zip::ZipArchive::new(Cursor::new(bytes)).unwrap();
+ let mut xhtml = String::new();
+ zip.by_name("OEBPS/content.xhtml").unwrap().read_to_string(&mut xhtml).unwrap();
+ [html, xhtml]
+}
+
+fn assert_output_order(doc: &Document, values: &[&str]) {
+ let positions: Vec<_> = ["HTML", "EPUB"]
+ .into_iter()
+ .zip(outputs(doc))
+ .map(|(format, output)| {
+ let positions: Vec<_> =
+ values.iter().map(|value| output.find(value).unwrap_or_else(|| panic!("{value} missing from {format}"))).collect();
+ for value in values {
+ assert_eq!(output.matches(value).count(), 1, "{value} should occur once in {format}");
+ }
+ (format, positions)
+ })
+ .collect();
+ assert!(positions.iter().all(|(_, p)| p.windows(2).all(|p| p[0] < p[1])), "wrong export order for {values:?}: {positions:?}");
+}
+
+#[test]
+fn translated_group_uses_its_spread_y_position_in_both_exports() {
+ let mut doc = Document::new(&NewDocument::default());
+ let (child, _) = text(&mut doc, 0, 36.0, 50.0, "Grouped second");
+ group(&mut doc, 0, &[child], Affine::translate((0.0, 300.0)));
+ text(&mut doc, 0, 36.0, 200.0, "Ungrouped first");
+ assert_output_order(&doc, &["Ungrouped first", "Grouped second"]);
+}
+
+#[test]
+fn translated_group_uses_its_spread_x_position_on_the_same_line() {
+ let mut doc = Document::new(&NewDocument::default());
+ let (child, _) = text(&mut doc, 0, 10.0, 200.0, "Grouped right");
+ group(&mut doc, 0, &[child], Affine::translate((350.0, 0.0)));
+ text(&mut doc, 0, 200.0, 200.0, "Ungrouped left");
+ assert_output_order(&doc, &["Ungrouped left", "Grouped right"]);
+}
+
+#[test]
+fn translated_group_uses_the_page_at_its_spread_position() {
+ let mut doc = Document::new(&NewDocument { pages: 3, facing_pages: true, ..Default::default() });
+ assert_eq!(doc.spreads[1].pages.len(), 2);
+ let width = doc.spreads[1].pages[0].width;
+ let (child, _) = text(&mut doc, 1, 36.0, 36.0, "Grouped right page");
+ group(&mut doc, 1, &[child], Affine::translate((width, 0.0)));
+ text(&mut doc, 1, 36.0, 400.0, "Ungrouped left page");
+ assert_output_order(&doc, &["Ungrouped left page", "Grouped right page"]);
+}
+
+#[test]
+fn nested_and_item_rotations_are_combined_before_measuring_graphics() {
+ let mut doc = Document::new(&NewDocument::default());
+ let aid = AssetId(doc.alloc());
+ // Original one-pixel SVG, constructed in the test rather than copied from a fixture.
+ doc.assets.insert(
+ aid,
+ Arc::new(Asset {
+ id: aid,
+ name: "Grouped image".into(),
+ mime: "image/svg+xml".into(),
+ link: None,
+ data: Arc::new(br#"<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"><rect width="1" height="1" fill="red"/></svg>"#.to_vec()),
+ pixels: None,
+ page: 0,
+ }),
+ );
+ let image = ItemId(doc.alloc());
+ let mut item = Item::new(image, doc.default_layer(), Shape::Rectangle, shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 10.0)));
+ item.content =
+ Content::Graphic(Graphic { asset: aid, size: (1.0, 1.0), xf: Affine::IDENTITY, auto_fit: Default::default(), fit_align: 4, crop: [0.0; 4] });
+ item.xf = Affine::rotate(std::f64::consts::FRAC_PI_4);
+ doc.insert_item(SpreadRef::Doc(0), item, None).unwrap();
+ let inner = group(&mut doc, 0, &[image], Affine::rotate(-std::f64::consts::FRAC_PI_4));
+ group(&mut doc, 0, &[inner], Affine::translate((50.0, 100.0)));
+ text(&mut doc, 0, 50.0, 75.0, "Text above image");
+ // The two rotations cancel: the image's top is y=100, not the y=50 obtained
+ // by rotating the already axis-aligned bounds of its rotated local path.
+ assert_output_order(&doc, &["Text above image", "Grouped image"]);
+}
+
+#[test]
+fn a_threaded_story_keeps_the_position_of_its_first_frame() {
+ let mut doc = Document::new(&NewDocument::default());
+ let (head, sid) = text(&mut doc, 0, 36.0, 50.0, "Threaded story");
+ let (tail, _) = text(&mut doc, 0, 36.0, 10.0, "");
+ doc.thread(head, tail).unwrap();
+ group(&mut doc, 0, &[head], Affine::translate((0.0, 300.0)));
+ let (_, first) = text(&mut doc, 0, 36.0, 200.0, "Standalone first");
+ assert_output_order(&doc, &["Standalone first", "Threaded story"]);
+ assert_eq!(reading_order(&doc), vec![Block::Story(first), Block::Story(sid)]);
+}
+
+#[test]
+fn articles_keep_their_explicit_order_and_exclusions() {
+ let mut doc = Document::new(&NewDocument::default());
+ let (a, _) = text(&mut doc, 0, 36.0, 50.0, "Article first");
+ let (b, _) = text(&mut doc, 0, 36.0, 30.0, "Article second");
+ let grouped = group(&mut doc, 0, &[a, b], Affine::translate((0.0, 300.0)));
+ let (c, _) = text(&mut doc, 0, 36.0, 10.0, "Article third");
+ let (omitted, _) = text(&mut doc, 0, 36.0, 5.0, "Excluded story");
+ doc.articles.push(Article { name: "Unused".into(), items: vec![omitted], export: false });
+ doc.articles.push(Article { name: "Chosen".into(), items: vec![grouped, c, grouped], export: true });
+ assert_output_order(&doc, &["Article first", "Article second", "Article third"]);
+ for output in outputs(&doc) {
+ assert!(!output.contains("Excluded story"));
+ }
+}
+
+#[test]
+fn stable_ties_and_the_existing_item_hidden_filter_are_preserved() {
+ let mut doc = Document::new(&NewDocument::default());
+ let (a, _) = text(&mut doc, 0, 36.0, 100.0, "First tie");
+ let (hidden, _) = text(&mut doc, 0, 36.0, 100.0, "Hidden child");
+ doc.item_mut(hidden).unwrap().hidden = true;
+ group(&mut doc, 0, &[a, hidden], Affine::IDENTITY);
+ text(&mut doc, 0, 36.0, 100.0, "Second tie");
+ let (hidden, _) = text(&mut doc, 0, 36.0, 50.0, "Hidden root");
+ doc.item_mut(hidden).unwrap().hidden = true;
+ text(&mut doc, 0, 36.0, 5.0, "");
+ let parent = doc.default_layer();
+ doc.add_text_frame(SpreadRef::Parent(0), Rect::new(36.0, 0.0, 136.0, 10.0), parent, "Parent text", ParaFormat::default()).unwrap();
+ assert_output_order(&doc, &["First tie", "Second tie"]);
+ for output in outputs(&doc) {
+ for omitted in ["Hidden child", "Hidden root", "Parent text"] {
+ assert!(!output.contains(omitted), "{omitted} should remain excluded");
+ }
+ }
+}