Files
vectorcraft/crates/engine/src/cmd/fileio/affinity/import.rs
T
FredApps 31658546a8 fix(codecs): build the cutover against craft-libs d6cd7836d
- Test files whose image→craft-image-codecs rewrites didn't compile against
  main's newer tests go back to main's versions (engine fileio/tests.rs,
  tests_tiffbmp.rs, tests/menucmds.rs, eps tests.rs); they still use the image
  crate, which keeps its format features.
- craft_jxl::encode, craft_image_codecs::encode and Image::icc at d6cd7836d.
- jp2read and pdf export pass DecodeOptions, not Limits.
- EPS DCTDecode: grey JPEGs no longer use the image after moving it, and keep
  main's one-channel output.
- svg keeps its direct usvg dependency for main's image href code; pdf keeps
  jpeg-encoder for CMYK JPEGs.
- clippy -D warnings and rustfmt clean.
2026-11-15 18:05:45 +01:00

47 lines
2.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use super::{Builder, Document, Result, err, raster_doc};
/// What an Affinity file opened as.
pub(in super::super) struct Imported {
pub doc: Document,
pub warnings: Vec<String>,
/// Only the embedded preview: never placed, used as a template or mined for styles.
pub preview_only: bool,
}
pub(in super::super) fn import(name: &str, bytes: &[u8]) -> Result<Imported> {
match vectorcraft_affinity::read(bytes, vectorcraft_affinity::Limits::default()) {
Ok(d) => {
let mut b = Builder::new(72.0 / d.dpi);
b.document(&d)?;
b.doc.limit_depth_noting(&mut b.warnings);
let mut warnings = d.warnings.iter().map(|w| format!("Affinity: not imported or approximated: {w}.")).collect::<Vec<_>>();
warnings.extend(b.warnings.iter().map(|w| format!("Affinity: {w}.")));
Ok(Imported { doc: b.doc, warnings, preview_only: false })
}
Err(native) => {
let (doc, w) = preview(name, bytes, &native)?;
Ok(Imported { doc, warnings: vec![w], preview_only: true })
}
}
}
/// The embedded preview, for files whose native document can't be read.
fn preview(name: &str, bytes: &[u8], native: &vectorcraft_affinity::Error) -> Result<(Document, String)> {
let p = vectorcraft_affinity::preview(bytes).map_err(|e| err(format!("{native}; its embedded preview can't be read either ({e})")))?;
// `raster_doc` normally probes PNG dimensions and keeps its encoded bytes. Verify this
// untrusted preview's complete pixels first, so corrupt data cannot create a blank image.
let mut opts = craft_image_codecs::DecodeOptions::default();
opts.limits.max_width = 4096;
opts.limits.max_height = 4096;
opts.limits.max_alloc = 128 << 20;
let _pixels = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, p.png, &opts).map_err(err)?;
let d = raster_doc(name, p.png)?;
Ok((
d,
format!(
"The native Affinity document could not be read ({native}). Opened only its embedded {}×{} PNG preview, which may be smaller than the document: no layers, vectors, text or pages. Save a new copy; the Affinity source cannot be saved back. Export SVG or PDF from Affinity for editable vectors.",
p.width, p.height
),
))
}