mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #13670 from NaufalDzz/fix/issue-13643
Artboards to PDF: pages go bottom artboard first (#13643)
This commit is contained in:
2 files changed
+65
-2
No files matched your search
@@ -752,7 +752,9 @@ fn artboards_to_pdf(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let doc = d.doc.clone();
|
||||
let quality = f64_param(p, "quality").or(Some(10.0));
|
||||
let mut pages = Vec::new();
|
||||
for one in chosen_documents(&doc, p)? {
|
||||
// Pages go bottom artboard of the Layers panel first, as in the reference application (#13643);
|
||||
// `chosen_documents` lists them top first, the order Artboards to Files keeps.
|
||||
for one in chosen_documents(&doc, p)?.into_iter().rev() {
|
||||
let buf = photocraft_compose::flatten(&one).over_background([1.0, 1.0, 1.0]);
|
||||
let fmt = PixelFormat::new(ColorMode::Rgb, SampleType::U8, true);
|
||||
let mut flat = Document::new(&one.name, one.size, ColorMode::Rgb, SampleType::U8);
|
||||
@@ -837,7 +839,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"file.export.artboardsToPdf",
|
||||
"Artboards to PDF…",
|
||||
&["File", "Export"],
|
||||
r##"{"path":str (.pdf),"artboards":[id]?,"quality":0..12=10} → {path, pages} (one raster page per board)"##,
|
||||
r##"{"path":str (.pdf),"artboards":[id]?,"quality":0..12=10} → {path, pages} (one raster page per board, bottom artboard of the Layers panel first)"##,
|
||||
export_artboards,
|
||||
artboards_to_pdf
|
||||
),
|
||||
@@ -848,5 +850,8 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
#[path = "artboard_cmds/first_board_background_tests.rs"]
|
||||
mod first_board_background_tests;
|
||||
#[cfg(test)]
|
||||
#[path = "artboard_cmds/pdf_page_order_tests.rs"]
|
||||
mod pdf_page_order_tests;
|
||||
#[cfg(test)]
|
||||
#[path = "artboard_cmds/tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,58 @@
|
||||
//! Artboards to PDF writes its pages bottom artboard of the Layers panel first (#13643), on the
|
||||
//! measured document: 800 x 400 with Red, Blue and Empty Board, bottom to top.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// The measured document, and the ids of Red, Blue and Empty Board (bottom to top).
|
||||
fn session() -> (Session, [u64; 3]) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 800, "height": 400, "depth": 8, "resolution": 72})).unwrap();
|
||||
let mut board = |rect: [i64; 4], name: &str| {
|
||||
let r = s.execute("layer.new.artboard", json!({"rect": rect, "name": name})).unwrap();
|
||||
r["layer"].as_u64().unwrap()
|
||||
};
|
||||
let ids = [board([0, 0, 200, 150], "Red Board"), board([120, 30, 200, 200], "Blue Board"), board([600, 0, 100, 100], "Empty Board")];
|
||||
(s, ids)
|
||||
}
|
||||
|
||||
/// Each page's size in points, in page order, from the exported PDF.
|
||||
fn page_sizes(s: &mut Session, params: Value) -> Vec<(u32, u32)> {
|
||||
let dir = std::env::temp_dir().join(format!("photocraft-pdf-order-{}-{}", std::process::id(), params["artboards"]));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = join(&dir.to_string_lossy(), "boards.pdf");
|
||||
let mut params = params;
|
||||
params["path"] = json!(path);
|
||||
s.execute("file.export.artboardsToPdf", params).unwrap();
|
||||
let text = String::from_utf8_lossy(&std::fs::read(&path).unwrap()).into_owned();
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
text.split("/MediaBox [0 0 ")
|
||||
.skip(1)
|
||||
.map(|rest| {
|
||||
let mut n = rest.split([' ', ']']).map(|v| v.parse::<f64>().unwrap().round() as u32);
|
||||
(n.next().unwrap(), n.next().unwrap())
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_bottom_artboard_is_the_first_page() {
|
||||
let (mut s, _) = session();
|
||||
assert_eq!(page_sizes(&mut s, json!({})), [(200, 150), (200, 200), (100, 100)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chosen_artboards_keep_the_bottom_first_order_whatever_the_id_order() {
|
||||
let (mut s, [red, blue, _]) = session();
|
||||
assert_eq!(page_sizes(&mut s, json!({"artboards": [blue, red]})), [(200, 150), (200, 200)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn artboards_to_files_keeps_its_top_first_order() {
|
||||
let (mut s, _) = session();
|
||||
let dir = std::env::temp_dir().join(format!("photocraft-pdf-order-files-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let r = s.execute("file.export.artboardsToFiles", json!({"dir": dir.to_string_lossy(), "prefix": "p"})).unwrap();
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let files: Vec<&str> = r["files"].as_array().unwrap().iter().filter_map(Value::as_str).collect();
|
||||
assert!(files[0].ends_with("p_0000_Empty Board.png") && files[2].ends_with("p_0002_Red Board.png"), "{files:?}");
|
||||
}
|
||||
Reference in new issue
Block a user