DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
A frame that crosses into an exported page is checked on it
Hamza2d5af1bd0d54
diff --git a/crates/engine/src/cmd/export/pdf.rs b/crates/engine/src/cmd/export/pdf.rsindex b5f16c110..3466a18a3 100644--- a/crates/engine/src/cmd/export/pdf.rs+++ b/crates/engine/src/cmd/export/pdf.rs@@ -541,6 +541,50 @@ mod overset_tests {assert!(story["overset"].is_null() && story["oversetChars"].is_null(), "{story}");}+ /// A sheet draws its spread's items without asking which page each one sits on, so a frame+ /// straddling two pages is drawn, in part, on both. Choosing the page by the frame's centre+ /// alone meant a frame centred on a page the export leaves out had its warning dropped while+ /// the part on an exported page went into the file (#1493 review).+ #[test]+ fn an_export_warns_about_a_frame_that_only_crosses_into_one_of_its_pages() {+ let mut s = Session::new();+ // Facing pages, so pages 2 and 3 are one spread and a frame can straddle their boundary.+ s.execute("file.new", &json!({"width": 300, "height": 200, "pages": 4, "facingPages": true, "margins": 0})).unwrap();+ let text = "First line is visible.\nSecond paragraph does not fit: this sentence carries the legal notice and must not vanish from the PDF without a warning.";+ // Spread 1 is pages 2-3; its page 2 runs x 0..300 and page 3 runs x 300..600. The frame+ // is centred on page 3 and reaches back over the boundary into page 2.+ let frame = json!({"spread": 1, "rect": [260, 20, 420, 60], "content": "text", "text": text});+ let id = s.execute("frame.create", &frame).unwrap()["id"].as_u64().unwrap();++ let about = |s: &mut Session, pages: serde_json::Value| {+ let params = if pages.is_null() { json!({}) } else { json!({"pages": pages}) };+ let r = s.execute("file.exportPdf", ¶ms).unwrap();+ let ws = r["warnings"].as_array().cloned().unwrap_or_default();+ ws.iter().filter_map(|w| w.as_str()).filter(|w| w.contains("not in the export")).map(str::to_owned).collect::<Vec<_>>()+ };++ // The whole document: reported once, at the page the frame is centred on.+ let all = about(&mut s, serde_json::Value::Null);+ assert_eq!(all.len(), 1, "{all:?}");+ assert!(all[0].contains("page 3") && all[0].contains(&format!("frame {id}")), "{}", all[0]);++ // Exporting page 3 alone: the centre page, reported as before.+ let three = about(&mut s, json!([3]));+ assert_eq!(three.len(), 1, "{three:?}");+ assert!(three[0].contains("page 3"), "{}", three[0]);++ // Exporting page 2 alone: the frame's centre is on page 3, which is not in the export --+ // but part of the frame is drawn on page 2, so it is still reported, and the page named is+ // one the export has.+ let two = about(&mut s, json!([2]));+ assert_eq!(two.len(), 1, "a frame crossing into page 2 is reported: {two:?}");+ assert!(two[0].contains("page 2"), "and the page it names is an exported one: {}", two[0]);+ assert!(two[0].contains(&format!("frame {id}")), "{}", two[0]);++ // A page the frame does not reach at all: nothing to say.+ assert_eq!(about(&mut s, json!([4])), Vec::<String>::new());+ }+/// `overset` indexes `text` in UTF-8 bytes, not characters: a client that slices a JavaScript/// string at it cuts the wrong place once the text is not ASCII (noted in review).#[test]diff --git a/crates/engine/src/cmd/preflight.rs b/crates/engine/src/cmd/preflight.rsindex bf98f9ab9..45fb8bc40 100644--- a/crates/engine/src/cmd/preflight.rs+++ b/crates/engine/src/cmd/preflight.rs@@ -38,6 +38,25 @@ fn page_of(d: &Document, sr: SpreadRef, it: &Item, parent: Affine) -> Option<usiSome(d.first_page_of_spread(si) + sp.page_at_x(bounds.center().x).unwrap_or(0))}+/// Every page of the spread the frame reaches, not just the one its centre is on.+///+/// A sheet draws the items of its spread without testing which page they sit on, so a frame that+/// straddles two pages is drawn, in part, on both. Choosing by the centre alone dropped the+/// warning for a frame whose centre is on a page the export leaves out while the part on an+/// exported page went into the file (noted in review). Empty when the frame is on no document+/// spread, which is the same answer `page_of` gives.+fn pages_of(d: &Document, sr: SpreadRef, it: &Item, parent: Affine) -> Vec<usize> {+ let SpreadRef::Doc(si) = sr else { return vec![] };+ let Some(sp) = d.spreads.get(si) else { return vec![] };+ let bounds = parent.transform_rect_bbox(it.bounds());+ let first = d.first_page_of_spread(si);+ let touched: Vec<usize> =+ sp.pages.iter().enumerate().filter(|(_, pg)| bounds.x1 > pg.x && bounds.x0 < pg.x + pg.width).map(|(i, _)| first + i).collect();+ // A frame wholly on the pasteboard touches no page's span; it still belongs to the page+ // `page_at_x` nominates, so the two functions agree about where it is.+ if touched.is_empty() { page_of(d, sr, it, parent).into_iter().collect() } else { touched }+}+/// Characters a missing-glyph report names before "and N more".const MISSING_GLYPHS_LISTED: usize = 20;@@ -74,11 +93,19 @@ fn missing_glyphs(}}-/// Locate an embedded frame at the outer page frame that actually renders it.-fn overset_location(s: &Session, target: designcraft_doc::ItemId) -> Option<(designcraft_doc::ItemId, Option<usize>)> {+/// Locate an embedded frame at the outer page frame that actually renders it: the frame, the page+/// its centre is on, and every page it reaches (see [`pages_of`]).+type Located = (designcraft_doc::ItemId, Option<usize>, Vec<usize>);++fn overset_location(s: &Session, target: designcraft_doc::ItemId) -> Option<Located> {let d = &s.active()?.doc;+ let at = |id: designcraft_doc::ItemId, loc: &designcraft_doc::ItemLoc| {+ let it = d.item(id);+ let xf = d.parent_xf(loc);+ (id, it.and_then(|it| page_of(d, loc.spread, it, xf)), it.map(|it| pages_of(d, loc.spread, it, xf)).unwrap_or_default())+ };if let Some(loc) = d.find(target) {- return Some((target, d.item(target).and_then(|it| page_of(d, loc.spread, it, d.parent_xf(&loc)))));+ return Some(at(target, &loc));}fn contains(root: &designcraft_compose::FrameText, target: designcraft_doc::ItemId) -> bool {let mut frames = vec![root];@@ -107,7 +134,7 @@ fn overset_location(s: &Session, target: designcraft_doc::ItemId) -> Option<(deslet frame = cs.frames.iter().find(|f| contains(f, current))?;current = frame.frame;if let Some(loc) = d.find(current) {- return Some((current, d.item(current).and_then(|it| page_of(d, loc.spread, it, d.parent_xf(&loc)))));+ return Some(at(current, &loc));}}None@@ -199,13 +226,28 @@ pub fn overset_warnings(s: &Session, pages: Option<&[usize]>) -> Vec<String> {continue;}let location = last.and_then(|f| overset_location(s, f));- let page = location.and_then(|(_, page)| page);- if let (Some(page), Some(pages)) = (page, pages)- && !pages.contains(&page)- {- continue;- }- let item = location.map(|(item, _)| item).or(last);+ let centre = location.as_ref().and_then(|(_, page, _)| *page);+ let touched: &[usize] = location.as_ref().map(|(_, _, ps)| ps.as_slice()).unwrap_or_default();+ // A sheet draws its spread's items without asking which page they are on, so a frame+ // straddling two pages is drawn in part on both. Testing the centre page alone dropped+ // the warning for a frame centred on a page the export leaves out while the part on an+ // exported page went into the file (noted in review).+ //+ // The page named is one the export has: the centre page when that is exported, so an+ // ordinary frame reads as before, otherwise the first exported page the frame reaches.+ let page = match pages {+ None => centre,+ Some(sel) => {+ let exported: Vec<usize> = touched.iter().copied().filter(|p| sel.contains(p)).collect();+ match centre.filter(|p| sel.contains(p)).or_else(|| exported.first().copied()) {+ Some(p) => Some(p),+ // Nothing of this frame is on an exported page.+ None if centre.is_some() || !touched.is_empty() => continue,+ None => None,+ }+ }+ };+ let item = location.as_ref().map(|(item, _, _)| *item).or(last);out.push(match (page, item) {(Some(page), Some(item)) => {format!("page {}: {n} character(s) of the story in frame {} do not fit it and are not in the export", page + 1, item.0)@@ -242,8 +284,8 @@ pub fn check(s: &Session, min_ppi: f64) -> Vec<Issue> {} else {format!("Overset text: {n} characters")},- item: location.map(|(item, _)| item.0).or(last.map(|i| i.0)),- page: location.and_then(|(_, page)| page),+ item: location.as_ref().map(|(item, _, _)| item.0).or(last.map(|i| i.0)),+ page: location.as_ref().and_then(|(_, page, _)| *page),});}}