mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Export Paths to File: Document Bounds writes the frame alone (#13771)
file.export.pathsToIllustrator takes paths "documentBounds": the file frame with the canvas in points as bounding box and no path block, reply paths: 0. The menu entry is enabled for every open document, with or without paths. The dialog's dropdown starts with Document Bounds and, in a document without paths, holds only it, selected.
This commit is contained in:
1 parent
9a5fcdaa83
commit
7d7c693683
7 files changed
+193
-14
No files matched your search
@@ -1531,8 +1531,9 @@ fn ps_comment_text(s: &str) -> String {
|
||||
s.chars().map(|c| if c.is_control() { ' ' } else { c }).collect()
|
||||
}
|
||||
|
||||
/// The document's paths as an Illustrator 3 PostScript file. `which`: "all", "work" or a path
|
||||
/// name. Coordinates are points (pixels at the document resolution), y up.
|
||||
/// The document's paths as an Illustrator 3 PostScript file. `which`: "documentBounds" (the
|
||||
/// frame alone, no path: header, bounding box and trailer), "all", "work" or a path name.
|
||||
/// Coordinates are points (pixels at the document resolution), y up.
|
||||
pub fn paths_to_ai(doc: &Document, which: &str) -> Result<(String, usize)> {
|
||||
let dpi = f64::from(doc.resolution_dpi.max(1.0));
|
||||
let k = 72.0 / dpi;
|
||||
@@ -1546,6 +1547,7 @@ pub fn paths_to_ai(doc: &Document, which: &str) -> Result<(String, usize)> {
|
||||
}
|
||||
}
|
||||
"work" => chosen.extend(doc.work_path.iter()),
|
||||
"documentBounds" => {}
|
||||
name => chosen.extend(doc.paths.iter().filter(|p| p.name == name).map(|p| &p.path)),
|
||||
}
|
||||
let mut s = String::new();
|
||||
@@ -1610,7 +1612,8 @@ fn paths_to_illustrator(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let d = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let which = p.get("paths").and_then(Value::as_str).unwrap_or("all");
|
||||
let (text, n) = paths_to_ai(&d.doc, which)?;
|
||||
if n == 0 {
|
||||
// Document Bounds writes the frame alone (#13771); any other choice must match a path.
|
||||
if n == 0 && which != "documentBounds" {
|
||||
return Err(bad(cmd, format!("no paths match `{which}`")));
|
||||
}
|
||||
match p.get("path").and_then(Value::as_str).filter(|v| !v.is_empty()) {
|
||||
@@ -1622,9 +1625,9 @@ fn paths_to_illustrator(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
|
||||
fn has_paths(s: &Session) -> std::result::Result<(), String> {
|
||||
let d = s.active().ok_or("no document open")?;
|
||||
if d.doc.paths.is_empty() && d.doc.work_path.is_none() { Err("the document has no paths".into()) } else { Ok(()) }
|
||||
/// Enabled for every open document, with or without paths: Document Bounds needs none (#13771).
|
||||
fn has_document(s: &Session) -> std::result::Result<(), String> {
|
||||
s.active().map(|_| ()).ok_or_else(|| "no document open".into())
|
||||
}
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
@@ -1690,8 +1693,8 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Paths to Illustrator…",
|
||||
&["File", "Export"],
|
||||
None,
|
||||
r##"{"path":str? (.ai; omit to return the text),"paths":"all|work|<path name>"="all"} → {path, paths}"##,
|
||||
has_paths,
|
||||
r##"{"path":str? (.ai; omit to return the text),"paths":"documentBounds|all|work|<path name>"="all" (documentBounds: the file frame with the canvas as bounding box and no path)} → {path, paths}"##,
|
||||
has_document,
|
||||
paths_to_illustrator
|
||||
),
|
||||
]
|
||||
@@ -1704,3 +1707,7 @@ mod tests;
|
||||
#[cfg(test)]
|
||||
#[path = "print_cmds/paths_export_path_blocks_tests.rs"]
|
||||
mod paths_export_path_blocks_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "print_cmds/paths_export_document_bounds_tests.rs"]
|
||||
mod paths_export_document_bounds_tests;
|
||||
@@ -0,0 +1,111 @@
|
||||
//! #13771: Paths to Illustrator's Document Bounds choice writes the file frame with no path (the
|
||||
//! canvas in points as bounding box), and the menu entry is enabled for every open document,
|
||||
//! with or without paths.
|
||||
|
||||
use super::*;
|
||||
use photocraft_doc::{NamedPath, Path, Subpath};
|
||||
|
||||
const CMD: &str = "file.export.pathsToIllustrator";
|
||||
const BEGIN: &str = "%Adobe_Photoshop_Path_Begin";
|
||||
|
||||
/// A 300 x 200 document at `dpi` without any path.
|
||||
fn plain(dpi: f32) -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 300, "height": 200, "background": "#ffffff", "name": "Bounds.psd"})).unwrap();
|
||||
s.edit("resolution", |d, _| {
|
||||
d.resolution_dpi = dpi;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn add_path(s: &mut Session, name: &str) {
|
||||
s.edit("paths", |d, _| {
|
||||
let tri = Subpath::polygon(&[(10.0, 10.0), (100.0, 10.0), (50.0, 90.0)]);
|
||||
d.paths.push(NamedPath { name: name.into(), path: Path::new(vec![tri]), psd_raw: None });
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn bounds(s: &mut Session) -> String {
|
||||
let r = s.execute(CMD, json!({"paths": "documentBounds"})).unwrap();
|
||||
assert_eq!(r["paths"], 0);
|
||||
r["ai"].as_str().unwrap().to_owned()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn document_bounds_writes_the_frame_without_a_path() {
|
||||
let mut s = plain(72.0);
|
||||
let ai = bounds(&mut s);
|
||||
assert!(ai.starts_with("%!PS-Adobe-2.0 EPSF-1.2\n"), "{ai}");
|
||||
assert!(ai.contains("%%BoundingBox: 0 0 300 200\n"), "{ai}");
|
||||
assert!(ai.contains("%%EndSetup\n%%PageTrailer\n%%Trailer\n%%EOF\n"), "nothing between setup and trailer: {ai}");
|
||||
assert!(!ai.contains(BEGIN));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn document_bounds_leaves_out_the_documents_paths() {
|
||||
let mut s = plain(72.0);
|
||||
add_path(&mut s, "Rect A");
|
||||
s.edit("work path", |d, _| {
|
||||
d.work_path = Some(Path::new(vec![Subpath::polygon(&[(1.0, 1.0), (9.0, 1.0), (5.0, 9.0)])]));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let ai = bounds(&mut s);
|
||||
assert!(!ai.contains(BEGIN), "{ai}");
|
||||
assert!(!ai.contains(" m\n"), "no path operators: {ai}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_bounding_box_is_the_canvas_in_points() {
|
||||
assert!(bounds(&mut plain(72.0)).contains("%%BoundingBox: 0 0 300 200\n"));
|
||||
assert!(bounds(&mut plain(300.0)).contains("%%BoundingBox: 0 0 72 48\n"));
|
||||
let mut s = plain(72.0);
|
||||
s.execute("image.canvasSize", json!({"width": 400, "height": 300, "anchor": "center"})).unwrap();
|
||||
assert!(bounds(&mut s).contains("%%BoundingBox: 0 0 400 300\n"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn document_bounds_writes_the_file_and_reports_no_paths() {
|
||||
let mut s = plain(72.0);
|
||||
let dir = std::env::temp_dir().join(format!("photocraft-paths-bounds-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let file = dir.join("Bounds.ai").to_string_lossy().replace('\\', "/");
|
||||
let r = s.execute(CMD, json!({"path": file, "paths": "documentBounds"})).unwrap();
|
||||
assert_eq!(r["paths"], 0);
|
||||
let written = std::fs::read_to_string(&file).unwrap();
|
||||
assert!(written.contains("%%BoundingBox: 0 0 300 200\n") && written.ends_with("%%EOF\n"), "{written}");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_entry_is_enabled_with_or_without_paths() {
|
||||
let mut s = Session::new();
|
||||
assert!(!s.is_enabled(CMD), "greyed without a document");
|
||||
s.execute("file.new", json!({"width": 300, "height": 200})).unwrap();
|
||||
assert!(s.is_enabled(CMD), "a document without paths");
|
||||
add_path(&mut s, "Rect A");
|
||||
assert!(s.is_enabled(CMD), "a document with a path");
|
||||
let mut bitmap = Session::new();
|
||||
bitmap.execute("file.new", json!({"width": 300, "height": 200, "mode": "gray", "background": "#ffffff"})).unwrap();
|
||||
bitmap.execute("image.mode.bitmap", json!({})).unwrap();
|
||||
assert!(bitmap.is_enabled(CMD), "a Bitmap-mode document");
|
||||
assert_eq!(bitmap.execute(CMD, json!({"paths": "documentBounds"})).unwrap()["paths"], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_path_name_that_does_not_exist_is_still_an_error() {
|
||||
let mut s = plain(72.0);
|
||||
add_path(&mut s, "Rect A");
|
||||
let err = s.execute(CMD, json!({"paths": "Nope"})).unwrap_err().to_string();
|
||||
assert!(err.contains("no paths match `Nope`"), "{err}");
|
||||
// The reference application's dropdown label is not a choice the command takes.
|
||||
assert!(s.execute(CMD, json!({"paths": "Document Bounds"})).is_err());
|
||||
// Without any path, All Paths and Work Path still match nothing (not measured: kept as today).
|
||||
let mut empty = plain(72.0);
|
||||
assert!(empty.execute(CMD, json!({"paths": "all"})).is_err());
|
||||
assert!(empty.execute(CMD, json!({"paths": "work"})).is_err());
|
||||
}
|
||||
@@ -639,7 +639,8 @@ fn package_lists_a_missing_link_once() {
|
||||
#[test]
|
||||
fn paths_export_as_illustrator_postscript() {
|
||||
let mut s = session("rgb", 8);
|
||||
assert!(!s.is_enabled("file.export.pathsToIllustrator"));
|
||||
// Enabled without paths too: Document Bounds needs none (#13771).
|
||||
assert!(s.is_enabled("file.export.pathsToIllustrator"));
|
||||
s.edit("paths", |d, _| {
|
||||
let tri = Subpath::polygon(&[(10.0, 10.0), (100.0, 10.0), (50.0, 90.0)]);
|
||||
let mut curve = Subpath::polyline(&[(0.0, 0.0), (300.0, 150.0)]);
|
||||
|
||||
@@ -1348,6 +1348,8 @@ pub mod open_prompt;
|
||||
pub mod palette;
|
||||
#[cfg(test)]
|
||||
mod paths_export_default_tests;
|
||||
#[cfg(test)]
|
||||
mod paths_export_document_bounds_tests;
|
||||
pub(crate) mod pattern_fill_ui;
|
||||
pub mod preset_group_prompt;
|
||||
pub mod rasterize_prompt;
|
||||
|
||||
@@ -136,17 +136,20 @@ pub fn invoke(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, params: &V
|
||||
}
|
||||
"file.package" => form(app, id, "Package", json!({"dir": dir}), json!({})),
|
||||
"file.export.pathsToIllustrator" => {
|
||||
let mut names = vec!["all".to_string()];
|
||||
if let Some(d) = app.session.active() {
|
||||
// Document Bounds comes first; a document without paths offers only it, selected (#13771).
|
||||
let mut names = vec!["documentBounds".to_string()];
|
||||
if let Some(d) = app.session.active().filter(|d| d.doc.work_path.is_some() || !d.doc.paths.is_empty()) {
|
||||
names.push("all".into());
|
||||
if d.doc.work_path.is_some() {
|
||||
names.push("work".into());
|
||||
}
|
||||
names.extend(d.doc.paths.iter().map(|p| p.name.clone()));
|
||||
}
|
||||
let fallback = if names.len() > 1 { "all" } else { "documentBounds" };
|
||||
// Opens on the Paths panel's selected row, as the reference application does (#13777).
|
||||
// A shape layer's path ("layer") is not an entry yet, and a row the document no longer
|
||||
// has falls back to All Paths.
|
||||
let paths = app.ui.selected_path.as_deref().filter(|s| *s != "layer" && names.iter().any(|n| n == s)).unwrap_or("all").to_string();
|
||||
// has falls back to All Paths (Document Bounds without paths).
|
||||
let paths = app.ui.selected_path.as_deref().filter(|s| *s != "layer" && names.iter().any(|n| n == s)).unwrap_or(fallback).to_string();
|
||||
form(app, id, "Export Paths to File", json!({"path": format!("{dir}/{stem}.ai"), "paths": paths}), json!({"paths": names}))
|
||||
}
|
||||
_ => return None,
|
||||
|
||||
@@ -85,5 +85,6 @@ fn the_entries_keep_their_parameter_values() {
|
||||
let mut app = app_with_paths();
|
||||
let (id, _) = open(&mut app);
|
||||
let choices = app.ui.dialog_mut(id).unwrap().fields["__choices"]["paths"].clone();
|
||||
assert_eq!(choices, json!(["all", "work", "Curve A", "Curve B"]));
|
||||
// Document Bounds comes first (#13771).
|
||||
assert_eq!(choices, json!(["documentBounds", "all", "work", "Curve A", "Curve B"]));
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
//! #13771: Export Paths to File's dropdown starts with Document Bounds, and in a document
|
||||
//! without paths it holds only Document Bounds, selected.
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
const COMMAND: &str = "file.export.pathsToIllustrator";
|
||||
|
||||
fn app() -> PhotocraftApp {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
|
||||
app
|
||||
}
|
||||
|
||||
/// Opens the dialog as the menu item does and returns the dropdown's entries and selection.
|
||||
fn open(app: &mut PhotocraftApp) -> (Vec<String>, Value) {
|
||||
let r = super::file_ui::invoke(app, &egui::Context::default(), COMMAND, &json!({})).unwrap().unwrap();
|
||||
let fields = &app.ui.dialog_mut(r["dialog"].as_u64().unwrap()).unwrap().fields;
|
||||
let names = fields["__choices"]["paths"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_owned()).collect();
|
||||
(names, fields["paths"].clone())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_document_without_paths_offers_only_document_bounds() {
|
||||
let mut app = app();
|
||||
assert!(app.session.is_enabled(COMMAND));
|
||||
let (names, paths) = open(&mut app);
|
||||
assert_eq!(names, ["documentBounds"]);
|
||||
assert_eq!(paths, "documentBounds");
|
||||
// A Paths panel selection the document doesn't have changes nothing.
|
||||
app.ui.selected_path = Some("work".into());
|
||||
assert_eq!(open(&mut app), (vec!["documentBounds".to_owned()], json!("documentBounds")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn document_bounds_comes_before_the_paths() {
|
||||
let mut app = app();
|
||||
let path = json!({"subpaths": [{"closed": true, "knots": [[10.0, 10.0], [50.0, 10.0], [50.0, 50.0]]}]});
|
||||
app.run("path.set", json!({"name": "Rect A", "path": path})).unwrap();
|
||||
app.run("path.set", json!({"name": "work", "path": path})).unwrap();
|
||||
app.ui.selected_path = None;
|
||||
let (names, paths) = open(&mut app);
|
||||
assert_eq!(names, ["documentBounds", "all", "work", "Rect A"]);
|
||||
assert_eq!(paths, "all", "the default stays All Paths (#13777)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ok_with_document_bounds_exports_the_frame() {
|
||||
let mut app = app();
|
||||
let (_, paths) = open(&mut app);
|
||||
let r = app.run(COMMAND, json!({"paths": paths})).unwrap();
|
||||
assert_eq!(r["paths"], 0);
|
||||
assert!(r["ai"].as_str().unwrap().contains("%%BoundingBox: 0 0 300 200\n"));
|
||||
}
|
||||
Reference in new issue
Block a user