diff --git a/crates/engine/src/cmd/fileio/tests_pdfx.rs b/crates/engine/src/cmd/fileio/tests_pdfx.rs index 68df66bd93..33555436e2 100644 --- a/crates/engine/src/cmd/fileio/tests_pdfx.rs +++ b/crates/engine/src/cmd/fileio/tests_pdfx.rs @@ -699,3 +699,56 @@ fn flattening_keeps_the_group_in_the_clipping_layer_when_the_layer_below_differs } assert!(wrong.is_empty(), "{wrong:#?}"); } + +/// #6861: Layer opacity, blend modes, sublayers with transparency, and empty transparent layers: +/// PDF/X-1a, PDF/X-3, and PDF 1.3 automatically flatten and reduce transparency without failing, +/// and the output contains no transparency dictionary or soft mask. +#[test] +fn pdfx_flattens_layer_and_sublayer_transparency() { + let _g = crate::tests_colormgmt::GLOBAL.lock().unwrap_or_else(|e| e.into_inner()); + let mut s = Session::new(); + run(&mut s, "file.new", json!({"width": 200, "height": 200, "colorMode": "cmyk"})); + + // Bottom layer: base artwork + let _layer1 = s.doc().unwrap().doc.layers[0].id.0; + run(&mut s, "shape.rectangle", json!({"x": 10, "y": 10, "width": 80, "height": 80})); + run(&mut s, "paint.setFill", json!({"color": {"c": 1.0, "m": 0.0, "y": 0.0, "k": 0.0}})); + + // Middle layer with 50% opacity and Multiply blend mode + let layer2 = run(&mut s, "layer.new", json!({}))["id"].as_u64().unwrap(); + run(&mut s, "shape.rectangle", json!({"x": 40, "y": 40, "width": 80, "height": 80})); + run(&mut s, "paint.setFill", json!({"color": {"c": 0.0, "m": 1.0, "y": 0.0, "k": 0.0}})); + run(&mut s, "transparency.set", json!({"ids": [layer2], "opacity": 50, "blendMode": "multiply"})); + + // Sublayer inside layer2 with 70% opacity + let sublayer = run(&mut s, "layer.newSublayer", json!({"parent": layer2}))["id"].as_u64().unwrap(); + run(&mut s, "shape.rectangle", json!({"x": 60, "y": 60, "width": 80, "height": 80})); + run(&mut s, "paint.setFill", json!({"color": {"c": 0.0, "m": 0.0, "y": 1.0, "k": 0.0}})); + run(&mut s, "transparency.set", json!({"ids": [sublayer], "opacity": 70})); + + // Empty layer with opacity (must not leave /CA or /ca in output) + let empty_layer = run(&mut s, "layer.new", json!({}))["id"].as_u64().unwrap(); + run(&mut s, "transparency.set", json!({"ids": [empty_layer], "opacity": 40})); + + for standard in ["pdfX1a", "pdfX3"] { + let (text, warnings) = pdf(&mut s, json!({"standard": standard})); + assert!(text.starts_with("%PDF-1.3"), "{standard}"); + assert!(warnings.iter().any(|w| w.contains("flattened")), "{standard}: expected flattening warning"); + 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 + 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()); + } + + // PDF 1.3 export with compatibility: 1.3 + let (text, warnings) = pdf(&mut s, json!({"compatibility": "1.3", "preserveEditing": false})); + assert!(text.starts_with("%PDF-1.3")); + assert!(warnings.iter().any(|w| w.contains("flattened"))); + for no in ["/SMask", "/S/Transparency", "/ca ", "/CA "] { + assert!(!text.contains(no), "PDF 1.3: forbidden token '{no}' found in text"); + } +} diff --git a/crates/engine/src/cmd/flatten/apply.rs b/crates/engine/src/cmd/flatten/apply.rs index b8aebf8324..ed1820cf57 100644 --- a/crates/engine/src/cmd/flatten/apply.rs +++ b/crates/engine/src/cmd/flatten/apply.rs @@ -5,8 +5,8 @@ use super::compose::{Builder, CLEAR, Composer, Elem, Fill, Kind, Px, WHITE}; use super::options::FlattenOptions; use super::{ Affine, Appearance, AppearanceItem, Arc, BlendMode, Color, ColorMode, Document, EngineError, FillLayer, FillRule, ImageBlob, ImageObject, Inks, - MAX_PIXELS, Node, NodeId, NodeKind, Paint, PathData, Rect, Result, composite, effects, isolated_doc, leaves, outline_strokes, planes_cmyk, po, - separate, shape_node, shapes, unique_key, + Knockout, MAX_PIXELS, Node, NodeId, NodeKind, Paint, PathData, Rect, Result, composite, effects, isolated_doc, leaves, outline_strokes, + planes_cmyk, po, separate, shape_node, shapes, unique_key, }; // ---------- plan ---------- @@ -760,23 +760,147 @@ pub(crate) fn flatten_document(doc: &Document, o: &FlattenOptions) -> Result Result> { let outlining = o.text_to_outlines || o.strokes_to_outlines || !o.preserve_overprints; - if !outlining && !doc.layers.iter().any(|l| o.shows_transparency(doc, l) || o.draws_see_through_image(l)) { - return Ok(None); - } - let mut roots = vec![]; - for l in doc.layers.iter().filter(|l| l.visible && !matches!(l.kind, NodeKind::Layer { template: true, .. })) { - let children = l.children().map_or(&[][..], Vec::as_slice); - roots.extend(children.iter().skip(usize::from(l.clips())).filter(|c| c.visible && !c.is_ruler_guide()).map(|c| c.id)); - } - let plan = plan(doc, &roots, o, true)?; - if plan.flat.is_empty() && !plan.work.iter().any(|k| k.changes) { + 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)); + if !needs_flatten { return Ok(None); } let mut d = doc.clone(); + let changed = normalize_layer_transparency(&mut d, o); + let mut roots = vec![]; + for l in d.layers.iter().filter(|l| l.visible && !matches!(l.kind, NodeKind::Layer { template: true, .. })) { + let children = l.children().map_or(&[][..], Vec::as_slice); + roots.extend(children.iter().skip(usize::from(l.clips())).filter(|c| c.visible && !c.is_ruler_guide()).map(|c| c.id)); + } + let plan = plan(&d, &roots, o, true)?; + if plan.flat.is_empty() && !plan.work.iter().any(|k| k.changes) { + if changed { + if o.no_transparency.is_some() { + for l in &mut d.layers { + sanitize_container_transparency(Arc::make_mut(l)); + } + } + return Ok(Some(d)); + } + return Ok(None); + } apply(&mut d, &mut vectorcraft_doc::Selection::default(), plan, o)?; + if o.no_transparency.is_some() { + for l in &mut d.layers { + sanitize_container_transparency(Arc::make_mut(l)); + } + } Ok(Some(d)) } +/// Flatten and normalize container-level transparency (on layers and sublayers) into groups +/// so that the flattener's plan includes them and leaves no container transparency behind. +fn normalize_layer_transparency(d: &mut Document, o: &FlattenOptions) -> bool { + let mut changed = false; + let mut layers = std::mem::take(&mut d.layers); + for l in &mut layers { + changed |= normalize_node_transparency(d, Arc::make_mut(l), o); + } + d.layers = layers; + changed +} + +fn normalize_node_transparency(d: &mut Document, n: &mut Node, o: &FlattenOptions) -> bool { + let mut changed = false; + let is_layer = n.is_layer(); + let clips = n.clips(); + + if let Some(children) = n.children_mut() { + for c in children.iter_mut() { + if c.is_layer() { + changed |= normalize_node_transparency(d, Arc::make_mut(c), o); + } + } + } + + 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())); + + if is_layer && has_transparency { + let children = n.children().map_or(&[][..], Vec::as_slice); + let (guides, art): (Vec<_>, Vec<_>) = children.iter().cloned().partition(|c| c.is_ruler_guide()); + + if art.is_empty() { + n.opacity = 1.0; + n.blend = BlendMode::Normal; + n.isolate = false; + n.knockout = Knockout::Neutral; + n.knockout_shape = false; + n.extras_mut().mask = None; + n.appearance = Appearance::default().into(); + n.set_clips(false); + changed = true; + } else { + let mut g = Node::group(d.alloc_id(), art); + if clips { + g.set_clips(true); + } + g.opacity = n.opacity; + g.blend = n.blend; + g.isolate = n.isolate; + g.knockout = n.knockout; + g.knockout_shape = n.knockout_shape; + g.appearance = n.appearance.clone(); + g.extras_mut().mask = n.extras_mut().mask.take(); + + n.set_clips(false); + n.opacity = 1.0; + n.blend = BlendMode::Normal; + n.isolate = false; + n.knockout = Knockout::Neutral; + n.knockout_shape = false; + n.appearance = Appearance::default().into(); + + let mut new_children = vec![Arc::new(g)]; + new_children.extend(guides); + if let Some(ch) = n.children_mut() { + *ch = new_children; + } + changed = true; + } + } else if is_layer && o.no_transparency.is_some() && !n.visible { + n.opacity = 1.0; + n.blend = BlendMode::Normal; + n.isolate = false; + n.knockout = Knockout::Neutral; + n.knockout_shape = false; + n.extras_mut().mask = None; + n.appearance = Appearance::default().into(); + } + + changed +} + +fn sanitize_container_transparency(n: &mut Node) { + if n.is_container() { + n.opacity = 1.0; + n.blend = BlendMode::Normal; + n.isolate = false; + n.knockout = Knockout::Neutral; + n.knockout_shape = false; + if let Some(m) = n.extras_mut().mask.as_mut() { + m.disabled = true; + } + n.extras_mut().mask = None; + if let Some(children) = n.children_mut() { + for c in children.iter_mut() { + if c.is_container() { + sanitize_container_transparency(Arc::make_mut(c)); + } + } + } + } +} + // ---------- apply ---------- pub(super) fn apply(d: &mut Document, sel: &mut vectorcraft_doc::Selection, plan: Plan, o: &FlattenOptions) -> Result<(Vec, usize, usize)> { diff --git a/crates/pdf/src/export/writer.rs b/crates/pdf/src/export/writer.rs index ca70ef2695..e11cda9acd 100644 --- a/crates/pdf/src/export/writer.rs +++ b/crates/pdf/src/export/writer.rs @@ -152,7 +152,7 @@ impl Writer { // readers (CoreGraphics) drop most of a page in a non-isolated group (#2156). let doc = ex.doc; let page_group = fits && !self.pdf13 && (doc.page_isolate || doc.page_knockout); - let cmyk_page_group = fits && self.cmyk && !page_group && ex.shows_transparency(); + let cmyk_page_group = fits && !self.pdf13 && self.cmyk && !page_group && ex.shows_transparency(); // Each copy draws within an equal share of the symbol objects the export has left // ([`vectorcraft_doc::MAX_SYMBOL_NODES`]), so every copy leaves out the same instances. let limit = ex.symbol_limit;