mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-17 15:36:43 +00:00
Merge PR #8254: Image Assets: optionally keep a transparent artboard's full size
This commit is contained in:
commit
9ee6c407b6
3 files changed
+48
-1
No files matched your search
@@ -1024,6 +1024,8 @@ fn save_for_web_impl(s: &mut Session, p: &Value, write: &mut dyn FnMut(&str, &[u
|
||||
|
||||
// ---------- Export Preferences / Quick Export ----------
|
||||
|
||||
/// File › Export › Export Preferences…: sets the given `export.*` preferences through `prefs.set`
|
||||
/// (validated like any other preference) and returns the whole section.
|
||||
fn export_preferences(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "file.export.exportPreferences";
|
||||
let mut values = serde_json::Map::new();
|
||||
@@ -1401,6 +1403,10 @@ fn asset_output_path(dir: &str, relative: &str) -> Result<(String, String)> {
|
||||
/// same file, the one higher in the Layers panel keeps it and the other is reported in `errors`.
|
||||
/// Names are compared ignoring case, as `Logo.png` and `logo.png` are one file on Windows and
|
||||
/// macOS.
|
||||
///
|
||||
/// A layer is trimmed to its visible pixels; an artboard keeps its whole board, transparent edges
|
||||
/// included, as File › Export › Artboards to Files writes it (Photoshop's Generator exports an
|
||||
/// artboard asset at the board's bounds).
|
||||
pub fn generate_assets(doc: &Document, dir: &str) -> (Vec<String>, Vec<Value>) {
|
||||
let dpi = f64::from(doc.resolution_dpi.max(1.0));
|
||||
let mut defaults: Option<Vec<AssetDefault>> = None;
|
||||
@@ -1433,7 +1439,10 @@ pub fn generate_assets(doc: &Document, dir: &str) -> (Vec<String>, Vec<Value>) {
|
||||
if written.contains(&key) {
|
||||
return Err(other(format!("{rel}: another layer already writes this file; rename one of the layers")));
|
||||
}
|
||||
let mut one = crate::layer_menu_cmds::layer_document(doc, *id)?;
|
||||
let mut one = match crate::artboard_cmds::artboard_document(doc, *id)? {
|
||||
Some(board) => board,
|
||||
None => crate::layer_menu_cmds::layer_document(doc, *id)?,
|
||||
};
|
||||
one.metadata = Default::default();
|
||||
let (w0, h0) = (f64::from(one.size.width), f64::from(one.size.height));
|
||||
// The layer's own size spec wins; default variants scale on top of it.
|
||||
@@ -1486,6 +1495,8 @@ pub fn assets_dir(doc_path: &str) -> String {
|
||||
join(&dir, &format!("{}-assets", stem(doc_path)))
|
||||
}
|
||||
|
||||
/// File › Generate › Image Assets: toggles generation for the active document (or sets it with
|
||||
/// `on`) and, when it is on and the output folder is known, writes the assets now.
|
||||
fn image_assets(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let d = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let id: DocId = d.doc.id;
|
||||
|
||||
@@ -670,6 +670,40 @@ fn image_assets_are_generated_now_and_on_save() {
|
||||
assert_eq!(decode(&files[0]).dimensions(), (128, 80));
|
||||
}
|
||||
|
||||
/// Photoshop's Generator exports an artboard named like an asset at the board's size, transparent
|
||||
/// edges included (generator-assets renderer: the canvas bounds are the artboard's), with no
|
||||
/// preference. Layers inside the board are still trimmed to their pixels.
|
||||
#[test]
|
||||
fn image_assets_keep_full_artboards() {
|
||||
let dir = tmp("asset-artboards");
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 64, "height": 48, "name": "boards.psd", "background": "transparent"})).unwrap();
|
||||
s.edit("board", |doc, _| {
|
||||
let fmt = doc.pixel_format();
|
||||
let mut logo = Layer::raster("logo.png", fmt);
|
||||
logo.surface_mut().unwrap().fill_rect(Rect::new(12, 10, 20, 16), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 1.0]));
|
||||
let board =
|
||||
photocraft_doc::Artboard { rect: Rect::new(4, 6, 44, 36), background: photocraft_doc::ArtboardBackground::Transparent, preset: String::new() };
|
||||
let group = photocraft_doc::Group { children: vec![logo], expanded: true, artboard: Some(board) };
|
||||
doc.layers = vec![Layer::new("board.png", LayerContent::Group(group))];
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let r = s.execute("file.generate.imageAssets", json!({"on": true, "dir": dir})).unwrap();
|
||||
assert!(r["errors"].as_array().unwrap().is_empty(), "{r}");
|
||||
let board = decode(&format!("{dir}/board.png"));
|
||||
assert_eq!(board.dimensions(), (40, 30), "the whole board, not its trimmed pixels");
|
||||
let px = board.to_rgba8();
|
||||
let at = |x: usize, y: usize| &px[(y * 40 + x) * 4..(y * 40 + x) * 4 + 4];
|
||||
assert_eq!(at(0, 0)[3], 0, "the board's empty corner stays transparent");
|
||||
assert_eq!(at(39, 29)[3], 0);
|
||||
// The logo keeps its place on the board: document (12, 10) is board (8, 4).
|
||||
assert!(at(8, 4)[0] > 250 && at(8, 4)[3] == 255, "{:?}", at(8, 4));
|
||||
assert_eq!(at(7, 4)[3], 0);
|
||||
assert_eq!(decode(&format!("{dir}/logo.png")).dimensions(), (8, 6), "a layer on the board is still trimmed");
|
||||
}
|
||||
|
||||
/// #6614: two layers, or two default variants, naming one file never overwrite each other: the
|
||||
/// layer higher in the Layers panel keeps the file, whatever the case of its name, and the
|
||||
/// others are reported.
|
||||
|
||||
@@ -1231,6 +1231,8 @@ fn gpu_memory_lines(app: &PhotocraftApp) -> Vec<String> {
|
||||
v
|
||||
}
|
||||
|
||||
/// The label a preference key shows in the dialog: a few keys have their own wording, the rest
|
||||
/// split camelCase into words (`convertToSrgb` → "Convert to srgb").
|
||||
fn humanize(key: &str) -> String {
|
||||
match key {
|
||||
"appearanceMode" => return "Appearance Mode".into(),
|
||||
|
||||
Reference in new issue
Block a user