Build: restore designcraft_format::write for recovery; render decodes without naming the image crate

This commit is contained in:
aiongg committed 2026-11-14 04:37:22 +00:00
1 parent 7107a64d6c
commit 078f5994c4
5 files changed
+19 -12

No files matched your search

+10
View File
@@ -36,6 +36,16 @@ pub fn save_with_preview(doc: &Document, preview: Option<&[u8]>) -> Result<Vec<u
craft_designcraft_format::save(&compact, concat!("DesignCraft ", env!("CARGO_PKG_VERSION")), preview, &assets)
}
/// [`save_with_preview`] into `out`, in chunks, so a writer that stops on cancellation stops the
/// write at its next chunk.
pub fn write<W: std::io::Write>(out: &mut W, doc: &Document, preview: Option<&[u8]>) -> Result<(), FormatError> {
let bytes = save_with_preview(doc, preview)?;
for chunk in bytes.chunks(1 << 20) {
out.write_all(chunk).map_err(|e| FormatError::Io(e.to_string()))?;
}
Ok(())
}
/// Required native format version for a document. Kept for app-side compatibility; inference is
/// performed by the shared codec over the serialized neutral document schema.
#[cfg(test)]
+1 -3
View File
@@ -1,7 +1,5 @@
use super::common::{affine_s, bool_s, pct, uid, with_props};
use super::{
Affine, ColumnsKind, Content, El, Ex, Item, Node, OBJECT_BUILTINS, PathData, Shape, TextFrameOptions, base64_encode, names, num, pt,
};
use super::{Affine, ColumnsKind, Content, El, Ex, Item, Node, OBJECT_BUILTINS, PathData, Shape, TextFrameOptions, base64_encode, names, num, pt};
impl<'a> Ex<'a> {
pub(super) fn item_el(&mut self, it: &Item, xf: Affine) -> El {
+5 -5
View File
@@ -18,22 +18,22 @@ pub fn decode_pixmap_page(bytes: &[u8], page: u32) -> Option<Pixmap> {
let (px, w, h) = designcraft_images::render_svg(bytes, 3000)?;
return Some(Pixmap::from_parts(premul_pixels(px), w.min(u16::MAX as u32) as u16, h.min(u16::MAX as u32) as u16));
}
premultiplied(designcraft_images::decode_rgba(bytes)?)
let img = designcraft_images::decode_rgba(bytes)?;
premultiplied(img.width(), img.height(), img.into_raw())
}
/// [`decode_pixmap`] of a JPEG at 1/2^`shrink` of its size, `shrink` 1 to
/// [`designcraft_images::MAX_REDUCED_SHRINK`], without a full-size decode. `None` where only a
/// full decode applies (other formats, CMYK JPEGs).
pub(crate) fn decode_pixmap_reduced(bytes: &[u8], shrink: u8) -> Option<Pixmap> {
premultiplied(designcraft_images::decode_rgba_reduced(bytes, shrink)?)
let img = designcraft_images::decode_rgba_reduced(bytes, shrink)?;
premultiplied(img.width(), img.height(), img.into_raw())
}
fn premultiplied(img: image::RgbaImage) -> Option<Pixmap> {
let (w, h) = img.dimensions();
fn premultiplied(w: u32, h: u32, mut px: Vec<u8>) -> Option<Pixmap> {
if w == 0 || h == 0 || w > u16::MAX as u32 || h > u16::MAX as u32 {
return None;
}
let mut px = img.into_raw();
px.truncate(w as usize * h as usize * 4);
Some(Pixmap::from_parts(premultiply(px), w as u16, h as u16))
}
+1 -3
View File
@@ -6,9 +6,7 @@ use std::collections::{HashMap, HashSet};
use craft_wordmodel::Block;
use craft_wordmodel::numbering::LevelSuffix;
use craft_wordmodel::para::{InlineObject, NoteKind, Paragraph};
use craft_wordmodel::props::{
Align as WordAlign, CharProps as WordCharProps, NumRef, ParaProps as WordParaProps, Underline, VertAlign,
};
use craft_wordmodel::props::{Align as WordAlign, CharProps as WordCharProps, NumRef, ParaProps as WordParaProps, Underline, VertAlign};
use craft_wordmodel::section::NumFormat;
use craft_wordmodel::styles::{StyleKind, StyleSheet};
use designcraft_doc::story::{BASIC_PARAGRAPH, NO_CHAR_STYLE};
+2 -1
View File
@@ -60,7 +60,8 @@ fn docx_parts(parts: &[(&str, &str)]) -> Vec<u8> {
}
if !rels.is_empty() {
w.start_file("word/_rels/document.xml.rels", o).unwrap();
let rels_doc = format!(r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">{}</Relationships>"#, rels.join(""));
let rels_doc =
format!(r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">{}</Relationships>"#, rels.join(""));
w.write_all(rels_doc.as_bytes()).unwrap();
}
w.finish().unwrap().into_inner()