mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 00:34:25 +00:00
* craft-psd: Photoshop PSD/PSB reader and writer, moved from PhotoCraft PhotoCraft's `photocraft-psd` crate (crates/psd at storytold/photocraft 63b641b55e54bf8f5c83efd977fdb16a2c255431) as `craft-psd`: the lossless PSD/PSB model, reader, writer and builder, descriptors, the Photoshop resource files that share its primitives (.abr, .asl, .aco, .act, .ase, .grd, .pat, .atn, .csh, .shc) and the Photoshop blocks of a layered TIFF. The crate had no dependency on any other PhotoCraft crate. Code is unchanged apart from the crate name, craft-libs' rustfmt width, comments that named other PhotoCraft crates, issue references (now `photocraft#N`), and the corpus tests' directory (`CRAFT_PSD_CORPUS` or `corpus/` at the workspace root). Tests, proptests, fuzz targets and fixtures move with it. Part of #778, #786. * craft-psd: engine_data, the text engine document of type layers The EngineData reader and writer from PhotoCraft's photocraft-text (crates/text/src/engine_data.rs at storytold/photocraft 63b641b5): a tree of values that is written back in Photoshop's layout, with strings stored as plain bytes kept as plain bytes. From EffectCraft's effectcraft-psd (crates/psd/src/engine_data.rs at storytold/effectcraft 072e1af9), ported onto that tree: `first_style`, the first style run as a typed `TextStyle` with bounded sizes and spacings, and `minimal`, a one-run document for writers and fixtures. `Value::index` is new. Both apps had their own parser for the same syntax. * craft-psd: engine_data::parse_lossy reads what it can of a damaged document EffectCraft's EngineData reader was best effort: a truncated or damaged document still gave the text and style it held. `parse_lossy` does that on the same parser: a truncated dictionary, array or string keeps what it has, a byte that starts no key is skipped, a token that is no number becomes a name, and garbage gives an empty dictionary. `parse` stays strict and is unchanged in what it accepts. The parser's `value` is split into one function per kind of value, and reads with `get` instead of indexing. * craft-psd: typed views of type layers, smart objects and adjustments Read-only views EffectCraft's effectcraft-psd had over the blocks this crate keeps raw (storytold/effectcraft f334bcf3, crates/psd/src/parse.rs and placed.rs). The blocks stay raw and write back unchanged. - `text`: `TypeLayer` from `TySh`: text, transform (the identity when not finite), bounds, the first style run (read with `engine_data::parse_lossy`) and the text descriptor. `LayerRecord::type_layer`. - `smart`: `SmartObject` from `SoLd`/`SoLE` (id, quad, non-affine transform, size, warp) and `PlLd`; `Warp` with the named styles and the custom quilt mesh; the quad geometry (`map`, `placed_bounds`, `placed_size`, `bake_scale`, `placement`). `LinkedFile` and `linked_files` for `lnk2`/`lnkD`/`lnk3`/`lnkE`, `PsdFile::linked_files` and `PsdFile::linked_data`. `LayerRecord::smart_object`. - `adjust`: `Adjustment` from `nvrt`, `brit`, `CgEd`, `hue2`/`hue `, `levl`, `expA`, and the other adjustment keys by name; `LayerRecord::adjustment`, `solid_color` (`SoCo`) and `effects` (`lfx2`). - `Descriptor`: `object`, `number`, `numbers`, `boolean`, `text`, `enumerated`, `raw_data`, `color`; `Value::as_number`. Linked-file data is a range of the block's data, checked against the record, instead of a range of the whole file. * craft-psd: shared descriptor lookups Descriptor::get_bool, get_number, get_text, get_enum and get_object, used by asl, grd and slices in place of their own copies. Ported from storytold/photocraft#9552 (its crates/psd part). * craft-psd: the typed views use the shared descriptor lookups One set of Descriptor accessors: the get_* lookups from photocraft#9552. numbers, raw_data and color move next to them as get_numbers, get_raw_data and get_color. --------- Co-authored-by: Omar Saidahmed <osaidahm@uwaterloo.ca>
153 lines
6.9 KiB
Rust
153 lines
6.9 KiB
Rust
//! Type layers: the `TySh` block as a read-only view.
|
|
//!
|
|
//! Layout (Adobe's specification, "Type tool object setting"): a version, six doubles of
|
|
//! transform, a text version, a descriptor version and the text descriptor, whose `Txt ` is the
|
|
//! text, `EngineData` the text engine document ([`crate::engine_data`]) and `bounds` /
|
|
//! `boundingBox` the text's extent. The warp descriptor and the four bounds that follow are not
|
|
//! read here. The block stays raw in the model and writes back unchanged. Came from EffectCraft's
|
|
//! `effectcraft-psd`.
|
|
|
|
use crate::descriptor::Descriptor;
|
|
use crate::engine_data::{self, TextStyle};
|
|
use crate::error::Result;
|
|
use crate::io::Reader;
|
|
use crate::layer::LayerRecord;
|
|
|
|
/// What a type layer says: its text, where it sits and how its first style run looks.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct TypeLayer {
|
|
/// The text, with `\n` line breaks and without the line break that ends it.
|
|
pub text: String,
|
|
/// Affine transform (xx, xy, yx, yy, tx, ty): the text origin sits at (tx, ty). The identity
|
|
/// when the file's transform is not finite.
|
|
pub transform: [f64; 6],
|
|
/// The first style run, read best effort from the text engine document.
|
|
pub style: TextStyle,
|
|
/// Text bounds (left, top, right, bottom) relative to the origin, when stored.
|
|
pub bounds: Option<[f64; 4]>,
|
|
/// The text descriptor, for everything else.
|
|
pub descriptor: Descriptor,
|
|
}
|
|
|
|
impl TypeLayer {
|
|
/// Reads the data of a `TySh` block.
|
|
pub fn from_bytes(data: &[u8]) -> Result<Self> {
|
|
let mut r = Reader::new(data);
|
|
let _version = r.u16()?;
|
|
let mut transform = [0.0; 6];
|
|
for v in &mut transform {
|
|
*v = r.f64()?;
|
|
}
|
|
// A transform with NaN or infinity is unusable: fall back to the identity.
|
|
if transform.iter().any(|v| !v.is_finite()) {
|
|
transform = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0];
|
|
}
|
|
let _text_version = r.u16()?;
|
|
let _descriptor_version = r.u32()?;
|
|
let descriptor = Descriptor::read(&mut r)?;
|
|
let text = descriptor.get_text("Txt ").unwrap_or_default().replace("\r\n", "\n").replace('\r', "\n");
|
|
let text = text.strip_suffix('\n').map(str::to_string).unwrap_or(text);
|
|
let style = descriptor.get_raw_data("EngineData").map(|e| engine_data::first_style(&engine_data::parse_lossy(e))).unwrap_or_default();
|
|
let bounds = descriptor
|
|
.get_object("bounds")
|
|
.or_else(|| descriptor.get_object("boundingBox"))
|
|
.and_then(|o| Some([o.get_number("Left")?, o.get_number("Top ")?, o.get_number("Rght")?, o.get_number("Btom")?]));
|
|
Ok(TypeLayer { text, transform, style, bounds, descriptor })
|
|
}
|
|
}
|
|
|
|
impl LayerRecord {
|
|
/// The layer's type (`TySh`), when it is a type layer. A malformed block counts as absent.
|
|
pub fn type_layer(&self) -> Option<TypeLayer> {
|
|
TypeLayer::from_bytes(&self.block(b"TySh")?.data).ok()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::descriptor::{UnicodeString, Value};
|
|
use crate::io::WriteExt;
|
|
use crate::tagged::TaggedBlock;
|
|
|
|
/// The data of a `TySh` block.
|
|
fn type_block(transform: [f64; 6], text: &str, engine: Option<Vec<u8>>, bounds_key: Option<&str>) -> Vec<u8> {
|
|
let mut d = Descriptor::new("TxLr").with("Txt ", Value::Text(UnicodeString::new_nul(text)));
|
|
if let Some(e) = engine {
|
|
d = d.with("EngineData", Value::RawData(e));
|
|
}
|
|
if let Some(key) = bounds_key {
|
|
let b = ["Left", "Top ", "Rght", "Btom"]
|
|
.iter()
|
|
.zip([-1.0, -20.0, 80.0, 5.0])
|
|
.fold(Descriptor::new("bounds"), |b, (k, v)| b.with(k, Value::UnitFloat { unit: *b"#Pnt", value: v }));
|
|
d = d.with(key, Value::Descriptor(b));
|
|
}
|
|
let mut out = Vec::new();
|
|
out.put_u16(1);
|
|
for v in transform {
|
|
out.put_f64(v);
|
|
}
|
|
out.put_u16(50);
|
|
out.put_u32(16);
|
|
d.write(&mut out);
|
|
// The warp version, descriptor version and descriptor, and the four bounds that follow.
|
|
out.put_u16(1);
|
|
out.put_u32(16);
|
|
Descriptor::new("warp").write(&mut out);
|
|
out.extend_from_slice(&[0; 32]);
|
|
out
|
|
}
|
|
|
|
#[test]
|
|
fn a_type_block_gives_text_transform_style_and_bounds() {
|
|
let engine = engine_data::minimal("Hello\rWorld", "Arial-BoldMT", 36.0, [1.0, 0.5, 0.0], 2);
|
|
let data = type_block([1.0, 0.0, 0.0, 1.0, 40.0, 60.0], "Hello\rWorld\r", Some(engine), Some("bounds"));
|
|
let t = TypeLayer::from_bytes(&data).unwrap();
|
|
assert_eq!(t.text, "Hello\nWorld");
|
|
assert_eq!(t.transform, [1.0, 0.0, 0.0, 1.0, 40.0, 60.0]);
|
|
assert_eq!(
|
|
(t.style.font.as_deref(), t.style.size, t.style.color, t.style.justification),
|
|
(Some("Arial-BoldMT"), Some(36.0), Some([1.0, 0.5, 0.0]), Some(2))
|
|
);
|
|
assert_eq!(t.bounds, Some([-1.0, -20.0, 80.0, 5.0]));
|
|
assert!(t.descriptor.get("EngineData").is_some());
|
|
let layer = LayerRecord { blocks: vec![TaggedBlock::new(*b"TySh", data.clone())], ..Default::default() };
|
|
assert_eq!(layer.type_layer(), Some(t));
|
|
// Cut anywhere inside the text descriptor: an error, and the layer reads as no type layer.
|
|
let descriptor_end = data.len() - 32 - 6 - Descriptor::new("warp").to_bytes().len();
|
|
for cut in 0..descriptor_end {
|
|
assert!(TypeLayer::from_bytes(&data[..cut]).is_err(), "cut {cut}");
|
|
}
|
|
let cut = LayerRecord { blocks: vec![TaggedBlock::new(*b"TySh", data[..40].to_vec())], ..Default::default() };
|
|
assert_eq!(cut.type_layer(), None);
|
|
assert_eq!(LayerRecord::default().type_layer(), None);
|
|
}
|
|
|
|
#[test]
|
|
fn windows_line_breaks_a_bounding_box_and_missing_engine_data_read() {
|
|
let t = TypeLayer::from_bytes(&type_block([2.0, 0.0, 0.0, 2.0, 0.0, 0.0], "a\r\nb", None, Some("boundingBox"))).unwrap();
|
|
assert_eq!(t.text, "a\nb");
|
|
assert_eq!(t.style, TextStyle::default());
|
|
assert_eq!(t.bounds, Some([-1.0, -20.0, 80.0, 5.0]));
|
|
assert_eq!(TypeLayer::from_bytes(&type_block([1.0; 6], "x", None, None)).unwrap().bounds, None);
|
|
}
|
|
|
|
#[test]
|
|
fn a_transform_that_is_not_finite_becomes_the_identity() {
|
|
for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
|
|
let t = TypeLayer::from_bytes(&type_block([1.0, 0.0, bad, 1.0, 5.0, 5.0], "x", None, None)).unwrap();
|
|
assert_eq!(t.transform, [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn damaged_engine_data_still_gives_the_style_it_holds() {
|
|
let mut engine = engine_data::minimal("x", "Courier", 12.0, [0.0, 0.0, 0.0], 0);
|
|
let keep = engine.windows(8).position(|w| w == b"/FontSet").unwrap() + 40;
|
|
engine.truncate(keep.min(engine.len() - 4));
|
|
let t = TypeLayer::from_bytes(&type_block([1.0, 0.0, 0.0, 1.0, 0.0, 0.0], "x", Some(engine), None)).unwrap();
|
|
assert_eq!(t.style.size, Some(12.0));
|
|
}
|
|
}
|