mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
CI's `cargo fmt --all -- --check` has failed on every run because the repo had no rustfmt config and was never formatted. Use the same settings as EffectCraft/FilmCraft (max_width 160, use_small_heuristics Max), which also keeps the churn small. Formatting only; no code changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
378 lines
16 KiB
Rust
378 lines
16 KiB
Rust
//! Layer comps and artboards ⇄ PSD.
|
|
//!
|
|
//! * Layer comps: image resource 1065 holds a `CompList` descriptor (`list` of `Comp` objects
|
|
//! with `Nm `, `compID`, `capturedInfo` bit flags and an optional `comment`, plus
|
|
//! `lastAppliedComp`). Each layer's state per comp lives in its `shmd` block under the `cmls`
|
|
//! key: `layerSettings`, one object per comp id list with `enab` (visibility) and `Ofst`
|
|
//! (offset from the layer's saved position). Comp id 0 is the Last Document State.
|
|
//! * Artboards: the per-layer `artb` block (older files: `artd`/`abdd`) is an `artboard`
|
|
//! descriptor with `artboardRect`, `guideIndeces`, `artboardPresetName`, `Clr ` and
|
|
//! `artboardBackgroundType` (1 white, 2 black, 3 transparent, 4 other).
|
|
//!
|
|
//! Both are re-emitted verbatim while they still decode to the document's state, and are
|
|
//! regenerated otherwise (comp appearance is not written to PSD; `.pcraft` keeps it).
|
|
|
|
use photocraft_doc::comps::layer_position;
|
|
use photocraft_doc::{Artboard, ArtboardBackground, CompLayerState, Document, Guides, Layer, LayerComp};
|
|
use photocraft_geom::Rect;
|
|
use photocraft_psd::descriptor::{Descriptor, UnicodeString, Value, VersionedDescriptor};
|
|
use photocraft_psd::metadata::{MetadataItem, parse_shmd, write_shmd};
|
|
|
|
use crate::blocks::{color_from_desc, color_to_desc, num};
|
|
|
|
/// Image resource id of the layer comp list.
|
|
pub const LAYER_COMPS: u16 = 1065;
|
|
/// Per-layer artboard block keys, preferred first.
|
|
pub const ARTBOARD_KEYS: [&[u8; 4]; 3] = [b"artb", b"artd", b"abdd"];
|
|
|
|
fn nul_name() -> UnicodeString {
|
|
UnicodeString(vec![0])
|
|
}
|
|
|
|
fn obj(class: &str) -> Descriptor {
|
|
Descriptor { name: nul_name(), ..Descriptor::new(class) }
|
|
}
|
|
|
|
fn pad_even(mut v: Vec<u8>) -> Vec<u8> {
|
|
if v.len() % 2 == 1 {
|
|
v.push(0);
|
|
}
|
|
v
|
|
}
|
|
|
|
fn text(d: &Descriptor, key: &str) -> Option<String> {
|
|
match d.get(key)? {
|
|
Value::Text(t) => Some(t.to_string_lossy()),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn int(d: &Descriptor, key: &str) -> Option<i64> {
|
|
match d.get(key)? {
|
|
Value::Integer(i) => Some(i64::from(*i)),
|
|
Value::LargeInteger(i) => Some(*i),
|
|
Value::Double(f) => Some(f.round() as i64),
|
|
Value::UnitFloat { value, .. } => Some(value.round() as i64),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn desc<'a>(d: &'a Descriptor, key: &str) -> Option<&'a Descriptor> {
|
|
match d.get(key)? {
|
|
Value::Descriptor(x) | Value::GlobalObject(x) => Some(x),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------- artboards
|
|
|
|
/// Decodes an `artb`/`artd`/`abdd` block.
|
|
pub fn parse_artboard(data: &[u8]) -> Option<Artboard> {
|
|
let (vd, _) = VersionedDescriptor::parse_prefix(data).ok()?;
|
|
let d = &vd.descriptor;
|
|
let r = desc(d, "artboardRect")?;
|
|
let g = |k: &str| num(r.get(k)).map(|v| v.round() as i32);
|
|
let rect = Rect::new(g("Left")?, g("Top ")?, g("Rght")?, g("Btom")?);
|
|
let background = match int(d, "artboardBackgroundType").unwrap_or(1) {
|
|
2 => ArtboardBackground::Black,
|
|
3 => ArtboardBackground::Transparent,
|
|
4 => ArtboardBackground::Custom(desc(d, "Clr ").and_then(color_from_desc).unwrap_or(photocraft_doc::Color::WHITE)),
|
|
_ => ArtboardBackground::White,
|
|
};
|
|
let preset = text(d, "artboardPresetName").unwrap_or_default();
|
|
Some(Artboard { rect, background, preset })
|
|
}
|
|
|
|
/// Encodes an `artb` block. `guides` are indices into the document's guide list (resource 1032)
|
|
/// that belong to the board.
|
|
pub fn write_artboard(a: &Artboard, guides: &[i32]) -> Vec<u8> {
|
|
let f = |v: i32| Value::Double(f64::from(v));
|
|
let rect = Descriptor::new("classFloatRect").with("Top ", f(a.rect.y0)).with("Left", f(a.rect.x0)).with("Btom", f(a.rect.y1)).with("Rght", f(a.rect.x1));
|
|
let color = match a.background {
|
|
ArtboardBackground::Custom(c) => c,
|
|
ArtboardBackground::Black => photocraft_doc::Color::BLACK,
|
|
_ => photocraft_doc::Color::WHITE,
|
|
};
|
|
let d = Descriptor::new("artboard")
|
|
.with("artboardRect", Value::Descriptor(rect))
|
|
.with("guideIndeces", Value::List(guides.iter().map(|i| Value::Integer(*i)).collect()))
|
|
.with("artboardPresetName", Value::Text(UnicodeString::new_nul(&a.preset)))
|
|
.with("Clr ", Value::Descriptor(color_to_desc(&color)))
|
|
.with("artboardBackgroundType", Value::Integer(a.background.psd_type()));
|
|
pad_even(VersionedDescriptor::new(d).to_bytes())
|
|
}
|
|
|
|
/// Indices of the document guides (horizontal first, then vertical, as in resource 1032) that
|
|
/// lie inside `r`.
|
|
pub fn guides_in(guides: &Guides, r: Rect) -> Vec<i32> {
|
|
let h = guides.horizontal.iter().map(|y| (*y as f64) > f64::from(r.y0) && (*y as f64) < f64::from(r.y1));
|
|
let v = guides.vertical.iter().map(|x| (*x as f64) > f64::from(r.x0) && (*x as f64) < f64::from(r.x1));
|
|
h.chain(v).enumerate().filter(|(_, inside)| *inside).map(|(i, _)| i as i32).collect()
|
|
}
|
|
|
|
/// Keeps, regenerates or removes the artboard block among a layer's raw blocks.
|
|
pub(crate) fn artboard_block(guides: &Guides, l: &Layer, raw: &mut Vec<([u8; 4], Vec<u8>)>) {
|
|
let pos = raw.iter().position(|(k, _)| ARTBOARD_KEYS.contains(&k));
|
|
match (l.artboard(), pos) {
|
|
(None, Some(_)) => raw.retain(|(k, _)| !ARTBOARD_KEYS.contains(&k)),
|
|
(None, None) => {}
|
|
(Some(a), Some(i)) if parse_artboard(&raw[i].1).as_ref() == Some(a) => {}
|
|
(Some(a), pos) => {
|
|
let data = write_artboard(a, &guides_in(guides, a.rect));
|
|
match pos {
|
|
Some(i) => raw[i] = (*b"artb", data),
|
|
None => raw.push((*b"artb", data)),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------- layer comps
|
|
|
|
/// One `layerSettings` entry of a `cmls` descriptor.
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
struct Setting {
|
|
comps: Vec<u32>,
|
|
visible: Option<bool>,
|
|
offset: Option<(i32, i32)>,
|
|
}
|
|
|
|
fn parse_cmls(data: &[u8]) -> Option<Vec<Setting>> {
|
|
let (vd, _) = VersionedDescriptor::parse_prefix(data).ok()?;
|
|
let Value::List(items) = vd.descriptor.get("layerSettings")? else { return None };
|
|
let mut out = Vec::new();
|
|
for it in items {
|
|
let Value::Descriptor(s) = it else { continue };
|
|
let comps = match s.get("compList") {
|
|
Some(Value::List(ids)) => ids.iter().filter_map(|v| if let Value::Integer(i) = v { Some(*i as u32) } else { None }).collect(),
|
|
_ => Vec::new(),
|
|
};
|
|
let visible = match s.get("enab") {
|
|
Some(Value::Boolean(b)) => Some(*b),
|
|
_ => None,
|
|
};
|
|
let offset = desc(s, "Ofst").and_then(|o| Some((int(o, "Hrzn")? as i32, int(o, "Vrtc")? as i32)));
|
|
out.push(Setting { comps, visible, offset });
|
|
}
|
|
Some(out)
|
|
}
|
|
|
|
/// The `cmls` item data of a layer's preserved `shmd` block.
|
|
fn layer_cmls(l: &Layer) -> Option<Vec<u8>> {
|
|
let (_, shmd) = l.psd_blocks.iter().find(|(k, _)| k == b"shmd")?;
|
|
parse_shmd(shmd).ok()?.into_iter().find(|it| &it.key == b"cmls").map(|it| it.data)
|
|
}
|
|
|
|
/// Comps, last applied comp and Last Document State described by `resource` (1065 data) and the
|
|
/// layers' `cmls` metadata.
|
|
pub fn comps_from_psd(resource: Option<&[u8]>, doc: &Document) -> (Vec<LayerComp>, Option<u32>, Option<LayerComp>) {
|
|
let Some(data) = resource else { return (Vec::new(), None, None) };
|
|
let Ok((vd, _)) = VersionedDescriptor::parse_prefix(data) else { return (Vec::new(), None, None) };
|
|
let d = &vd.descriptor;
|
|
let mut comps: Vec<LayerComp> = Vec::new();
|
|
if let Some(Value::List(items)) = d.get("list") {
|
|
for it in items {
|
|
let Value::Descriptor(c) = it else { continue };
|
|
let Some(id) = int(c, "compID") else { continue };
|
|
let mut comp = LayerComp {
|
|
id: id as u32,
|
|
name: text(c, "Nm ").unwrap_or_default(),
|
|
comment: text(c, "comment").unwrap_or_default(),
|
|
apply_visibility: false,
|
|
apply_position: false,
|
|
apply_appearance: false,
|
|
states: Vec::new(),
|
|
};
|
|
comp.set_captured_info(int(c, "capturedInfo").unwrap_or(0) as i32);
|
|
comps.push(comp);
|
|
}
|
|
}
|
|
let last_applied = int(d, "lastAppliedComp").map(|v| v as u32).filter(|id| comps.iter().any(|c| c.id == *id));
|
|
let mut last = LayerComp {
|
|
id: 0,
|
|
name: "Last Document State".into(),
|
|
comment: String::new(),
|
|
apply_visibility: true,
|
|
apply_position: true,
|
|
apply_appearance: false,
|
|
states: Vec::new(),
|
|
};
|
|
for (_, _, l) in doc.walk() {
|
|
let Some(settings) = layer_cmls(l).and_then(|c| parse_cmls(&c)) else { continue };
|
|
let here = layer_position(l);
|
|
for s in settings {
|
|
if s.visible.is_none() && s.offset.is_none() {
|
|
continue;
|
|
}
|
|
let state = CompLayerState {
|
|
layer: l.id,
|
|
visible: s.visible,
|
|
position: match (here, s.offset) {
|
|
(Some((x, y)), Some((dx, dy))) => Some((x + dx, y + dy)),
|
|
_ => None,
|
|
},
|
|
appearance: None,
|
|
};
|
|
for id in &s.comps {
|
|
let target = if *id == 0 { Some(&mut last) } else { comps.iter_mut().find(|c| c.id == *id) };
|
|
if let Some(c) = target {
|
|
c.states.push(state.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
let last = (!last.states.is_empty()).then_some(last);
|
|
(comps, last_applied, last)
|
|
}
|
|
|
|
/// The 1065 resource data for `doc`'s comps (None when there are none).
|
|
pub fn write_comps_resource(doc: &Document) -> Option<Vec<u8>> {
|
|
if doc.layer_comps.is_empty() {
|
|
return None;
|
|
}
|
|
let list = doc
|
|
.layer_comps
|
|
.iter()
|
|
.map(|c| {
|
|
let mut d = Descriptor::new("Comp").with("Nm ", Value::Text(UnicodeString::new_nul(&c.name)));
|
|
if !c.comment.is_empty() {
|
|
d = d.with("comment", Value::Text(UnicodeString::new_nul(&c.comment)));
|
|
}
|
|
Value::Descriptor(d.with("compID", Value::Integer(c.id as i32)).with("capturedInfo", Value::Integer(c.captured_info())))
|
|
})
|
|
.collect();
|
|
let mut d = Descriptor::new("CompList").with("list", Value::List(list));
|
|
if let Some(id) = doc.last_applied_comp {
|
|
d = d.with("lastAppliedComp", Value::Integer(id as i32));
|
|
}
|
|
Some(VersionedDescriptor::new(d).to_bytes())
|
|
}
|
|
|
|
fn setting_value(ids: &[u32], st: Option<&CompLayerState>, here: Option<(i32, i32)>) -> Value {
|
|
let mut s = obj("null");
|
|
if let Some(st) = st {
|
|
if let Some(v) = st.visible {
|
|
s = s.with("enab", Value::Boolean(v));
|
|
}
|
|
if let (Some((x, y)), Some((hx, hy))) = (st.position, here) {
|
|
s = s.with("Ofst", Value::Descriptor(obj("null").with("Hrzn", Value::Integer(x - hx)).with("Vrtc", Value::Integer(y - hy))));
|
|
}
|
|
}
|
|
Value::Descriptor(s.with("compList", Value::List(ids.iter().map(|i| Value::Integer(*i as i32)).collect())))
|
|
}
|
|
|
|
/// The `cmls` item data for one layer (None when the document has no comps).
|
|
pub fn write_cmls(comps: &[LayerComp], last: Option<&LayerComp>, l: &Layer, psd_id: u32) -> Option<Vec<u8>> {
|
|
if comps.is_empty() {
|
|
return None;
|
|
}
|
|
let here = layer_position(l);
|
|
let mut settings: Vec<Value> = comps.iter().map(|c| setting_value(&[c.id], c.state(l.id), here)).collect();
|
|
settings.push(setting_value(&[0], last.and_then(|c| c.state(l.id)), here));
|
|
let origin = obj("null").with("Hrzn", Value::Double(0.0)).with("Vrtc", Value::Double(0.0));
|
|
let d = Descriptor::new("null")
|
|
.with("origFXRefPoint", Value::Descriptor(origin))
|
|
.with("LyrI", Value::Integer(psd_id as i32))
|
|
.with("layerSettings", Value::List(settings));
|
|
Some(VersionedDescriptor::new(d).to_bytes())
|
|
}
|
|
|
|
/// Whether `doc`'s comps still equal what its preserved PSD data (resource 1065 plus the layers'
|
|
/// `cmls`) decodes to, in which case all of it is written back verbatim.
|
|
pub fn comps_unchanged(doc: &Document) -> bool {
|
|
let raw = doc.metadata.psd_resources.iter().find(|(id, _, _)| *id == LAYER_COMPS).map(|(_, _, d)| d.as_slice());
|
|
let (comps, last_applied, last) = comps_from_psd(raw, doc);
|
|
comps == doc.layer_comps && last_applied == doc.last_applied_comp && last == doc.last_document_state
|
|
}
|
|
|
|
/// Replaces (or removes, with `None`) the `cmls` item of a layer's `shmd` block among its raw
|
|
/// blocks, keeping the other metadata items.
|
|
pub(crate) fn set_cmls(raw: &mut Vec<([u8; 4], Vec<u8>)>, cmls: Option<Vec<u8>>) {
|
|
let pos = raw.iter().position(|(k, _)| k == b"shmd");
|
|
let mut items = pos.and_then(|i| parse_shmd(&raw[i].1).ok()).unwrap_or_default();
|
|
match (items.iter().position(|it| &it.key == b"cmls"), cmls) {
|
|
(Some(i), Some(data)) => items[i] = MetadataItem { data: pad_even(data), ..items[i].clone() },
|
|
(Some(i), None) => {
|
|
items.remove(i);
|
|
}
|
|
(None, Some(data)) => items.insert(0, MetadataItem::new(*b"cmls", data)),
|
|
(None, None) => return,
|
|
}
|
|
match (pos, items.is_empty()) {
|
|
(Some(i), true) => {
|
|
raw.remove(i);
|
|
}
|
|
(Some(i), false) => raw[i].1 = write_shmd(&items),
|
|
(None, false) => raw.push((*b"shmd", write_shmd(&items))),
|
|
(None, true) => {}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use photocraft_doc::{Color, ColorMode, LayerContent, PixelFormat, SampleType, Size};
|
|
|
|
#[test]
|
|
fn artboard_roundtrip() {
|
|
let a = Artboard { rect: Rect::new(10, 20, 110, 220), background: ArtboardBackground::Custom(Color::rgb(0.2, 0.4, 0.6)), preset: "Web".into() };
|
|
let b = write_artboard(&a, &[0, 2]);
|
|
assert_eq!(b.len() % 2, 0);
|
|
let back = parse_artboard(&b).unwrap();
|
|
assert_eq!(back.rect, a.rect);
|
|
assert_eq!(back.preset, "Web");
|
|
let ArtboardBackground::Custom(c) = back.background else { panic!("{back:?}") };
|
|
assert!((c.c[1] - 0.4).abs() < 1e-3);
|
|
for bg in [ArtboardBackground::White, ArtboardBackground::Black, ArtboardBackground::Transparent] {
|
|
let a = Artboard { background: bg, ..Artboard::new(Rect::new(0, 0, 4, 4)) };
|
|
assert_eq!(parse_artboard(&write_artboard(&a, &[])), Some(a));
|
|
}
|
|
assert_eq!(parse_artboard(&[1, 2, 3]), None);
|
|
}
|
|
|
|
#[test]
|
|
fn cmls_setting_roundtrip_and_shmd_merge() {
|
|
let mut d = Document::with_background("t", Size::new(32, 32), ColorMode::Rgb, SampleType::U8, Color::WHITE);
|
|
let mut l = Layer::raster("A", PixelFormat::RGBA8);
|
|
l.surface_mut().unwrap().fill_rect(Rect::new(4, 4, 8, 8), &[1.0, 0.0, 0.0, 1.0]);
|
|
let lid = l.id;
|
|
d.layers.push(l);
|
|
d.layer_comps.push(LayerComp {
|
|
id: 7,
|
|
name: "One".into(),
|
|
comment: "c".into(),
|
|
apply_visibility: true,
|
|
apply_position: true,
|
|
apply_appearance: false,
|
|
states: vec![CompLayerState { layer: lid, visible: Some(false), position: Some((10, 1)), appearance: None }],
|
|
});
|
|
d.last_applied_comp = Some(7);
|
|
let res = write_comps_resource(&d).unwrap();
|
|
let cm = write_cmls(&d.layer_comps, None, d.layer(lid).unwrap(), 3).unwrap();
|
|
// Attach as Photoshop would: shmd with cmls plus another item.
|
|
let mut raw = vec![(*b"shmd", write_shmd(&[MetadataItem::new(*b"cust", vec![1, 2])]))];
|
|
set_cmls(&mut raw, Some(cm));
|
|
let items = parse_shmd(&raw[0].1).unwrap();
|
|
assert_eq!(items.len(), 2);
|
|
let layer = d.layer_mut(lid).unwrap();
|
|
layer.psd_blocks = raw.iter().map(|(k, v)| (*k, std::sync::Arc::new(v.clone()))).collect();
|
|
d.metadata.psd_resources.push((LAYER_COMPS, String::new(), std::sync::Arc::new(res)));
|
|
let (comps, last_applied, last) = comps_from_psd(d.metadata.psd_resources.last().map(|r| r.2.as_slice()), &d);
|
|
assert_eq!(comps, d.layer_comps);
|
|
assert_eq!(last_applied, Some(7));
|
|
assert_eq!(last, None);
|
|
assert!(comps_unchanged(&d));
|
|
// Moving the layer changes the decoded (relative) position: no longer unchanged.
|
|
if let LayerContent::Raster(s) = &mut d.layer_mut(lid).unwrap().content {
|
|
s.fill_rect(Rect::new(0, 0, 2, 2), &[0.0, 0.0, 0.0, 1.0]);
|
|
}
|
|
assert!(!comps_unchanged(&d));
|
|
// Removing cmls keeps the other item; removing everything drops the block.
|
|
set_cmls(&mut raw, None);
|
|
assert_eq!(parse_shmd(&raw[0].1).unwrap().len(), 1);
|
|
let mut only = vec![(*b"shmd", write_shmd(&[MetadataItem::new(*b"cmls", vec![0, 0])]))];
|
|
set_cmls(&mut only, None);
|
|
assert!(only.is_empty());
|
|
}
|
|
}
|