Save PDF: show rasterization DPI and prompt confirmation when flattening transparency (#8097)

This commit is contained in:
el-j
2026-11-16 11:33:39 +01:00
parent ed6f22a84c
commit 7fa3ceb635
3 changed files with 123 additions and 1 deletions
+1
View File
@@ -378,6 +378,7 @@ registry! {
WarichuSettings: [warichu::KIND] => warichu::SPEC,
CompositeFonts: [composite_fonts::KIND] => composite_fonts::SPEC,
SavePdf: [save_pdf::KIND] => save_pdf::SPEC,
ConfirmFlattenPdf: [save_pdf::CONFIRM_FLATTEN] => save_pdf::CONFIRM_FLATTEN_SPEC,
SvgOptions: [svg_options::KIND] => svg_options::SPEC,
JavaScriptFiles: [javascript_files::KIND] => javascript_files::SPEC,
SvgFilter: [svg_filter::KIND] => svg_filter::SPEC,
+76 -1
View File
@@ -44,6 +44,61 @@ pub(super) const PRESET_SPEC: DialogSpec = DialogSpec {
..DialogSpec::FORM
};
/// The dialog kind of the confirmation prompt when saving with transparency reduction.
pub(crate) const CONFIRM_FLATTEN: &str = "confirmFlattenPdf";
pub(crate) const CONFIRM_FLATTEN_SPEC: DialogSpec = DialogSpec {
heading: |d| d.str("message"),
body: |_, ui, d| {
let detail = d.str("detail");
if !detail.is_empty() {
ui.label(egui::RichText::new(detail).color(Tokens::get(ui.ctx()).text_dim));
}
false
},
confirm: confirm_flatten,
ok: Some("Continue"),
max_width: Some(440.0),
..DialogSpec::FORM
};
fn confirm_flatten(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
let mut parent = super::parent(d).filter(|p| p.kind == KIND).ok_or("the Save PDF dialog is no longer open")?;
parent.fields.insert("__confirmed_flatten".into(), json!(true));
app.ui.dialog = Some(parent.clone());
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;
let message = tl!("The document contains transparency which will be flattened. Do you want to continue?");
let detail = crate::i18n::fmt(
tl!(
"Transparent objects will be reduced into opaque paths and images at {line_ppi} ppi (line art and text) and {grad_ppi} ppi (gradients and meshes). Reopening the PDF may not restore separate transparent objects."
),
&[("line_ppi", &line_ppi.to_string()), ("grad_ppi", &grad_ppi.to_string())],
);
let under = serde_json::to_value(d).ok();
let fields = json!({
"message": message,
"detail": detail,
super::PARENT: under,
});
app.ui.dialog = Some(Dialog::new(CONFIRM_FLATTEN, fields));
}
fn doc_has_transparency(doc: &vectorcraft_doc::Document) -> bool {
fn check_node(n: &vectorcraft_doc::Node) -> bool {
!n.has_default_transparency() || n.shows_transparency() || n.children().into_iter().flatten().any(|c| check_node(c))
}
doc.layers.iter().any(|l| check_node(l))
}
/// The preset editor's field holding the name of the saved preset it edits (empty: a new one).
const EDITING: &str = "__editing";
/// Save PDF's field holding the name typed for Save Preset… (present while that row shows).
@@ -153,6 +208,13 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
app.ui.dialog = Some(d);
return r;
}
let flat = choice::<Compatibility>(d, "compatibility") == Some(Compatibility::Pdf13)
|| choice::<Standard>(d, "standard").is_some_and(Standard::flattens);
let has_transparency = app.session.active().is_some_and(|st| doc_has_transparency(&st.doc));
if flat && has_transparency && !d.bool("__confirmed_flatten") {
ask_confirm_flatten(app, d);
return Ok(Value::Null);
}
let r = match SaveMode::of(&d.str("__save")) {
Some(mode) => io::save_pdf(app, mode, params(d)),
None if d.bool(ART_BOUNDS) => export_art(app, d),
@@ -732,7 +794,20 @@ fn advanced(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) {
}
});
});
if !flat {
if flat {
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;
note(
ui,
&crate::i18n::fmt(
tl!("Raster resolution: {line_ppi} ppi line art and text, {grad_ppi} ppi gradient and mesh"),
&[("line_ppi", &line_ppi.to_string()), ("grad_ppi", &grad_ppi.to_string())],
),
);
note(ui, tl!("PDF 1.3 files have no transparency: the preset flattens it."));
}
}
@@ -507,3 +507,49 @@ fn pdf_1_3_takes_a_flattener_preset_in_advanced() {
let saved = app.session.prefs.pdf_presets.iter().find(|p| p.name == "Old").unwrap();
assert_eq!((saved.settings.compatibility, saved.settings.flattener_preset.as_str()), (Compatibility::Pdf13, "Low Resolution"));
}
#[test]
fn save_pdf_prompts_confirmation_when_document_has_transparency_and_is_flattened() {
let (mut app, written, _) = app();
// Add transparent artwork to the document
app.run("transparency.set", json!({"opacity": 50})).unwrap();
app.run("ui.savePdfDialog", json!({"path": "/tmp/transparent.pdf", "compatibility": "1.3"})).unwrap();
assert_eq!(app.ui.dialog.as_ref().unwrap().kind, KIND);
// Clicking Save PDF triggers the confirmFlattenPdf modal prompt
super::super::confirm(&mut app).unwrap();
let prompt = app.ui.dialog.as_ref().expect("confirm dialog open");
assert_eq!(prompt.kind, CONFIRM_FLATTEN);
assert!(prompt.str("message").contains("transparency which will be flattened"));
assert!(prompt.str("detail").contains("1200 ppi"));
assert!(prompt.str("detail").contains("300 ppi"));
// Cancel on confirmation modal restores the Save PDF dialog
crate::dialogs::cancel(&mut app);
assert_eq!(app.ui.dialog.as_ref().expect("Save PDF restored").kind, KIND);
assert!(written.borrow().is_empty(), "nothing written on cancel");
// Click Save PDF again, then confirm the prompt
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(), "dialog closed after save");
assert_eq!(written.borrow().len(), 1);
assert!(written.borrow()[0].1.starts_with(b"%PDF-1.3"));
}
#[test]
fn save_pdf_does_not_prompt_when_compatibility_is_not_flattened() {
let (mut app, written, _) = app();
app.run("transparency.set", json!({"opacity": 50})).unwrap();
app.run("ui.savePdfDialog", json!({"path": "/tmp/live.pdf", "compatibility": "1.7"})).unwrap();
assert_eq!(app.ui.dialog.as_ref().unwrap().kind, KIND);
// Saving with PDF 1.7 (supports live transparency) does not prompt confirmation
super::super::confirm(&mut app).unwrap();
assert!(app.ui.dialog.is_none());
assert_eq!(written.borrow().len(), 1);
assert!(written.borrow()[0].1.starts_with(b"%PDF-1.7"));
}