mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-25 02:07:09 -06:00
Open and Place read Affinity documents natively (.af from Affinity 3, .afdesign/.afphoto/.afpub from Affinity 1 and 2): layers and sublayers, groups, artboards with their names and backgrounds, Publisher pages side by side, curves with live corners, parametric shapes, compound shapes, clipping by nesting, vector and pixel masks, solid and gradient fills, strokes, fill layers, artistic and frame text, placed images (their original JPEG/PNG), pixel layers, embedded documents as their cached picture, opacity, visibility, locking and blend modes, at the document's resolution in points. Unsupported content (effects, adjustments, brushes, master pages, special shapes, Affinity-only blend modes...) is listed in the import warning, one line per kind. A file whose native data can't be read opens as its embedded preview with the reason, and that preview cannot be placed or used as a template or library. Save never writes back over the Affinity file and there is no Affinity export. The document semantics were fitted to the thumbnails Affinity embeds in 176 public documents. `cargo xtask corpus --affinity` fetches 22 public CC0/MIT/Apache-2.0 documents at pinned commits, sha256-verified; the new engine test renders each and compares it with Affinity's own thumbnail (0-4.4 of 255 mean difference, per-file ceilings). import_fuzz mutates a synthetic native document's stream and container through open, render and export; the fuzz crate gains a stream target. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GQPh8UDgMyARR5Hat9UchC
52 lines
1.7 KiB
Rust
52 lines
1.7 KiB
Rust
//! Reader for native Affinity documents (`.af`, and `.afdesign`/`.afphoto`/`.afpub` from
|
|
//! Affinity 1 and 2): the archive container, the tagged object stream in `doc.dat`, and the
|
|
//! subset of the document model VectorCraft imports.
|
|
//!
|
|
//! Layouts follow the MIT-licensed afread reference and were checked against public documents
|
|
//! saved by Affinity 1.x to 3.x (container versions 8 to 12); see the crate README for provenance
|
|
//! and the exact supported subset. Nothing here writes Affinity files.
|
|
#![forbid(unsafe_code)]
|
|
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
|
|
|
use std::fmt;
|
|
|
|
mod access;
|
|
pub mod container;
|
|
mod geometry;
|
|
pub mod model;
|
|
pub mod paint;
|
|
pub mod preview;
|
|
mod raster;
|
|
mod shapes;
|
|
pub mod stream;
|
|
#[cfg(any(test, feature = "synth"))]
|
|
pub mod synth;
|
|
mod text;
|
|
|
|
pub use container::{Archive, Limits, MAGIC, is_affinity};
|
|
pub use model::{Document, read};
|
|
pub use preview::{MAX_PREVIEW_BYTES, MAX_PREVIEW_DIMENSION, Preview, preview};
|
|
|
|
/// Why a file could not be read. Messages are short reasons; callers add advice.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum Error {
|
|
Malformed(&'static str),
|
|
Unsupported(&'static str),
|
|
Limit(&'static str),
|
|
}
|
|
|
|
impl fmt::Display for Error {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
Self::Malformed(s) => write!(f, "damaged Affinity file: {s}"),
|
|
Self::Unsupported(s) => write!(f, "unsupported Affinity file: {s}"),
|
|
Self::Limit(s) => write!(f, "Affinity file exceeds a safety limit: {s}"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for Error {}
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|