mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 21:27:00 +00:00
Address review comments: check pattern tile transparency and skip image scan when unflattened
This commit is contained in:
@@ -53,7 +53,8 @@ mod report;
|
||||
|
||||
pub(crate) use analysis::{carry, outlined_text};
|
||||
pub use analysis::{draws_image, see_through_images};
|
||||
pub(crate) use apply::{expand_transparent_tile_images, flatten_document, flatten_expanded, has_transparent_tile_images};
|
||||
pub use apply::has_transparent_tile_images;
|
||||
pub(crate) use apply::{expand_transparent_tile_images, flatten_document, flatten_expanded};
|
||||
#[cfg(test)]
|
||||
pub(crate) use apply::{with_clips_ignored, with_every_region};
|
||||
pub use commands::specs;
|
||||
|
||||
@@ -723,7 +723,7 @@ pub(super) fn opaque(n: &mut Node) {
|
||||
|
||||
/// Whether `doc` has pattern fills whose image tiles need transparency flattening with `o`
|
||||
/// ([`expand_transparent_tile_images`]).
|
||||
pub(crate) fn has_transparent_tile_images(doc: &Document, o: &FlattenOptions) -> bool {
|
||||
pub fn has_transparent_tile_images(doc: &Document, o: &FlattenOptions) -> bool {
|
||||
let keys = o.no_transparency.clone().unwrap_or_default();
|
||||
let colorize = |d: &Document, n: &Node| o.counts_colorize(d, n);
|
||||
!doc.patterns.is_empty() && crate::cmd::expand::has_transparent_image_patterns(doc, &keys, &colorize)
|
||||
|
||||
@@ -69,13 +69,9 @@ fn confirm_flatten(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String
|
||||
confirm(app, &parent)
|
||||
}
|
||||
|
||||
fn ask_confirm_flatten(app: &mut VectorcraftApp, d: &Dialog) {
|
||||
let name = get(d, "flattenerPreset").as_str().map(str::trim).filter(|n| !n.is_empty()).unwrap_or(pdf::DEFAULT_FLATTENER);
|
||||
let preset_opts = FlattenOptions::preset(name)
|
||||
.or_else(|| app.session.prefs.flattener_presets.iter().find(|p| p.name.eq_ignore_ascii_case(name)).map(|p| p.options.clone()))
|
||||
.unwrap_or_default();
|
||||
let line_ppi = preset_opts.line_art_ppi.round() as u32;
|
||||
let grad_ppi = preset_opts.gradient_ppi.round() as u32;
|
||||
fn ask_confirm_flatten(app: &mut VectorcraftApp, d: &Dialog, o: &FlattenOptions) {
|
||||
let line_ppi = o.line_art_ppi.round() as u32;
|
||||
let grad_ppi = o.gradient_ppi.round() as u32;
|
||||
let message = tl!("The document contains transparency which will be flattened. Do you want to continue?");
|
||||
let detail = crate::i18n::fmt(
|
||||
tl!(
|
||||
@@ -92,8 +88,21 @@ fn ask_confirm_flatten(app: &mut VectorcraftApp, d: &Dialog) {
|
||||
app.ui.dialog = Some(Dialog::new(CONFIRM_FLATTEN, fields));
|
||||
}
|
||||
|
||||
fn doc_has_transparency(doc: &vectorcraft_doc::Document, include_non_printing: bool) -> bool {
|
||||
let see_through = vectorcraft_engine::cmd::flatten::see_through_images(doc);
|
||||
fn doc_has_transparency(doc: &vectorcraft_doc::Document, include_non_printing: bool, o: &vectorcraft_engine::cmd::flatten::FlattenOptions) -> bool {
|
||||
let mut with_images;
|
||||
let o = match &o.no_transparency {
|
||||
Some(_) => o,
|
||||
None => {
|
||||
with_images = o.clone();
|
||||
with_images.no_transparency = Some(vectorcraft_engine::cmd::flatten::see_through_images(doc));
|
||||
&with_images
|
||||
}
|
||||
};
|
||||
if vectorcraft_engine::cmd::flatten::has_transparent_tile_images(doc, o) {
|
||||
return true;
|
||||
}
|
||||
let empty = std::collections::BTreeSet::new();
|
||||
let see_through = o.no_transparency.as_ref().unwrap_or(&empty);
|
||||
fn check_node(n: &vectorcraft_doc::Node, see_through: &std::collections::BTreeSet<String>, include_non_printing: bool) -> bool {
|
||||
if !n.visible {
|
||||
return false;
|
||||
@@ -106,7 +115,7 @@ fn doc_has_transparency(doc: &vectorcraft_doc::Document, include_non_printing: b
|
||||
|| vectorcraft_engine::cmd::flatten::draws_image(n, see_through)
|
||||
|| n.children().into_iter().flatten().any(|c| check_node(c, see_through, include_non_printing))
|
||||
}
|
||||
doc.layers.iter().any(|l| check_node(l, &see_through, include_non_printing))
|
||||
doc.layers.iter().any(|l| check_node(l, see_through, include_non_printing))
|
||||
}
|
||||
|
||||
/// The preset editor's field holding the name of the saved preset it edits (empty: a new one).
|
||||
@@ -220,11 +229,17 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
|
||||
}
|
||||
let flat = choice::<Compatibility>(d, "compatibility") == Some(Compatibility::Pdf13)
|
||||
|| choice::<Standard>(d, "standard").is_some_and(Standard::flattens);
|
||||
let include_non_printing = d.bool("includeNonPrinting") || d.bool("createLayers");
|
||||
let has_transparency = app.session.active().is_some_and(|st| doc_has_transparency(&st.doc, include_non_printing));
|
||||
if flat && has_transparency && !d.bool("__confirmed_flatten") {
|
||||
ask_confirm_flatten(app, d);
|
||||
return Ok(Value::Null);
|
||||
if flat && !d.bool("__confirmed_flatten") {
|
||||
let include_non_printing = d.bool("includeNonPrinting") || d.bool("createLayers");
|
||||
let name = get(d, "flattenerPreset").as_str().map(str::trim).filter(|n| !n.is_empty()).unwrap_or(pdf::DEFAULT_FLATTENER);
|
||||
let mut o = FlattenOptions::for_export(Some(name), d.fields.get("flattener"), &app.session.prefs.flattener_presets).unwrap_or_default();
|
||||
if let Some(st) = app.session.active() {
|
||||
o.no_transparency = Some(vectorcraft_engine::cmd::flatten::see_through_images(&st.doc));
|
||||
if doc_has_transparency(&st.doc, include_non_printing, &o) {
|
||||
ask_confirm_flatten(app, d, &o);
|
||||
return Ok(Value::Null);
|
||||
}
|
||||
}
|
||||
}
|
||||
let r = match SaveMode::of(&d.str("__save")) {
|
||||
Some(mode) => io::save_pdf(app, mode, params(d)),
|
||||
|
||||
@@ -651,3 +651,25 @@ fn save_pdf_does_not_prompt_for_non_printing_layer_transparency() {
|
||||
assert!(app.ui.dialog.is_none());
|
||||
assert_eq!(written.borrow().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn save_pdf_prompts_confirmation_for_transparent_pattern_tile_image() {
|
||||
let (mut app, written, _) = app();
|
||||
let img_bytes = {
|
||||
let mut buf = std::io::Cursor::new(Vec::new());
|
||||
image::RgbaImage::from_pixel(2, 2, image::Rgba([255, 0, 0, 128])).write_to(&mut buf, image::ImageFormat::Png).unwrap();
|
||||
buf.into_inner()
|
||||
};
|
||||
let href = format!("data:image/png;base64,{}", vectorcraft_format::base64_encode(&img_bytes));
|
||||
let svg = format!(
|
||||
r##"<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100" viewBox="0 0 100 100"><defs><pattern id="p" patternUnits="userSpaceOnUse" width="10" height="10"><image width="10" height="10" xlink:href="{href}"/></pattern></defs><rect width="100" height="100" fill="url(#p)"/></svg>"##
|
||||
);
|
||||
app.run("document.open", json!({"name": "pat.svg", "dataBase64": vectorcraft_format::base64_encode(svg.as_bytes())})).unwrap();
|
||||
|
||||
app.run("ui.savePdfDialog", json!({"path": "/tmp/pat.pdf", "compatibility": "1.3"})).unwrap();
|
||||
super::super::confirm(&mut app).unwrap();
|
||||
assert_eq!(app.ui.dialog.as_ref().unwrap().kind, CONFIRM_FLATTEN);
|
||||
super::super::confirm(&mut app).unwrap();
|
||||
assert!(app.ui.dialog.is_none());
|
||||
assert_eq!(written.borrow().len(), 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user