From 3f9c5a17a3d99082b11389ee767cda5eada26a73 Mon Sep 17 00:00:00 2001 From: el-j Date: Mon, 16 Nov 2026 11:20:30 +0100 Subject: [PATCH] Address review comments: recursive container check and render assertions --- crates/engine/src/cmd/fileio/tests_pdfx.rs | 5 ++++- crates/engine/src/cmd/flatten/apply.rs | 17 ++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/cmd/fileio/tests_pdfx.rs b/crates/engine/src/cmd/fileio/tests_pdfx.rs index 33555436e2..dcd3cc9082 100644 --- a/crates/engine/src/cmd/fileio/tests_pdfx.rs +++ b/crates/engine/src/cmd/fileio/tests_pdfx.rs @@ -737,11 +737,14 @@ fn pdfx_flattens_layer_and_sublayer_transparency() { for no in ["/SMask", "/S/Transparency", "/ca ", "/CA "] { assert!(!text.contains(no), "{standard}: forbidden token '{no}' found in text:\n{text}"); } - // Verify it can be imported back + // Verify it can be imported back and renders artwork let v = s.execute("document.exportPdf", &json!({"standard": standard})).unwrap(); let bytes = vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap(); let imported = vectorcraft_pdf::import(&bytes).unwrap(); assert!(!imported.layers.is_empty()); + let rendered = drawn(&imported); + let non_white = rendered.pixels.as_chunks::<4>().0.iter().filter(|p| p[..3].iter().any(|c| *c < 240)).count(); + assert!(non_white > 1000, "{standard}: flattened artwork failed to render, only {non_white} pixels"); } // PDF 1.3 export with compatibility: 1.3 diff --git a/crates/engine/src/cmd/flatten/apply.rs b/crates/engine/src/cmd/flatten/apply.rs index ed1820cf57..782b2f0c2e 100644 --- a/crates/engine/src/cmd/flatten/apply.rs +++ b/crates/engine/src/cmd/flatten/apply.rs @@ -760,11 +760,16 @@ pub(crate) fn flatten_document(doc: &Document, o: &FlattenOptions) -> Result Result> { let outlining = o.text_to_outlines || o.strokes_to_outlines || !o.preserve_overprints; + let has_layer_container_transparency = |n: &Node| -> bool { + fn check(n: &Node) -> bool { + !n.has_default_transparency() || n.children().into_iter().flatten().any(|c| c.is_layer() && check(c)) + } + check(n) + }; let needs_flatten = outlining - || doc - .layers - .iter() - .any(|l| (o.no_transparency.is_some() && !l.has_default_transparency()) || o.shows_transparency(doc, l) || o.draws_see_through_image(l)); + || doc.layers.iter().any(|l| { + (o.no_transparency.is_some() && has_layer_container_transparency(l)) || o.shows_transparency(doc, l) || o.draws_see_through_image(l) + }); if !needs_flatten { return Ok(None); } @@ -821,9 +826,7 @@ fn normalize_node_transparency(d: &mut Document, n: &mut Node, o: &FlattenOption } } - let has_transparency = !n.has_default_transparency() - || !n.appearance.effects.is_empty() - || n.appearance.items.iter().any(|i| i.visible() && (i.opacity() < 1.0 || i.blend() != BlendMode::Normal || !i.effects().is_empty())); + let has_transparency = !n.has_default_transparency() || n.shows_own_transparency(); if is_layer && has_transparency { let children = n.children().map_or(&[][..], Vec::as_slice);