Address review comments: recursive container check and render assertions

This commit is contained in:
el-j
2026-11-16 11:20:30 +01:00
parent 9b80bb7807
commit 3f9c5a17a3
2 changed files with 14 additions and 8 deletions
+4 -1
View File
@@ -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
+10 -7
View File
@@ -760,11 +760,16 @@ pub(crate) fn flatten_document(doc: &Document, o: &FlattenOptions) -> Result<Opt
/// ([`expand_transparent_tile_images`]).
pub(crate) fn flatten_expanded(doc: &Document, o: &FlattenOptions) -> Result<Option<Document>> {
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);