Merge main into fix/issue-6636

This commit is contained in:
dotCooCoo committed 2026-11-14 12:11:53 -08:00
commit e6e8c61dbf
231 files changed
+1822 -48090

No files matched your search

+1 -5
View File
@@ -282,11 +282,7 @@ impl Exporter<'_> {
if !(w.is_finite() && h.is_finite() && w > 0.0 && h > 0.0) {
return;
}
let dims = self
.doc
.media
.get(&pic.media)
.and_then(|b| image::ImageReader::new(std::io::Cursor::new(b.as_slice())).with_guessed_format().ok()?.into_dimensions().ok());
let dims = self.doc.media.get(&pic.media).and_then(|b| wordcraft_geom::picture::dimensions(b.as_slice()));
// Centred once at its natural size (an ODT page background that doesn't repeat).
if let Some((pw, ph)) = dims.filter(|_| pic.fill_type.eq_ignore_ascii_case("center")) {
let (iw, ih) = centred_size(pw, ph, w, h);
+1 -1
View File
@@ -185,7 +185,7 @@ impl Exporter<'_> {
pub(crate) fn vector_text(&mut self, s: &mut Surface, t: &craft_metafile::Text, tf: kurbo::Affine) {
let (face, shaped) = wordcraft_layout::metafile_text_run(t);
let glyphs: Vec<_> = shaped.iter().map(|g| (g.cluster, f64::from(g.x_advance), f64::from(g.x_offset), f64::from(g.y_offset))).collect();
let Some(lay) = wordcraft_layout::metafile_text_layout(t, face.upem, face.ascent, face.descent, &glyphs) else { return };
let Some(lay) = wordcraft_layout::layout_metafile_text(t, face.upem, face.ascent, face.descent, &glyphs) else { return };
let font = self.font(&face);
if font.is_none() && !self.outlined.contains(&face.id()) {
return;
+1 -1
View File
@@ -236,7 +236,7 @@ pub(crate) fn load_image(data: &Arc<Vec<u8>>, fx: PicFx) -> Option<Image> {
/// [`load_image`] with an explicit RGBA byte budget (a small one in tests).
pub(crate) fn load_image_within(data: &Arc<Vec<u8>>, fx: PicFx, budget: u64) -> Option<Image> {
let fmt = image::guess_format(data).ok()?;
let fmt = wordcraft_geom::picture::guess_format(data)?;
// The decision to draw or not is the shared one (`wordcraft_geom::picture`), the one strict
// export checks with.
let dec = wordcraft_geom::picture::decode_for_drawing(data, budget, MAX_IMAGE_SIDE).ok()?;
+18 -23
View File
@@ -54,9 +54,8 @@ fn shape_line_caps_reach_pdf_content() {
fn png() -> Vec<u8> {
let img = image::RgbaImage::from_fn(16, 8, |x, y| image::Rgba([(x * 15) as u8, (y * 30) as u8, 200, 255]));
let mut buf = Vec::new();
img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png).unwrap();
buf
wordcraft_geom::picture::encode_format(&image::DynamicImage::from(img.clone()), image::ImageFormat::Png).unwrap()
}
/// Headings, a bold run, a hyperlink, an internal link, a list, a table and a picture.
@@ -1117,8 +1116,8 @@ fn a_crop_keeping_a_sliver_draws_the_source_that_much_wider() {
/// A JPEG whose headers say progressive (SOF2, a DC-only first scan) and `w` × `h`, its three
/// components unsubsampled, with no pixel data that size.
fn progressive_jpeg_header(w: u16, h: u16) -> Vec<u8> {
let mut jpeg = Vec::new();
image::RgbImage::new(16, 16).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap();
let mut jpeg =
wordcraft_geom::picture::encode_format(&image::DynamicImage::from(image::RgbImage::new(16, 16).clone()), image::ImageFormat::Jpeg).unwrap();
let sof = jpeg.windows(2).position(|m| m == [0xFF, 0xC0]).unwrap();
jpeg[sof + 1] = 0xC2;
jpeg[sof + 5..sof + 7].copy_from_slice(&h.to_be_bytes());
@@ -1137,8 +1136,8 @@ fn progressive_jpeg_header(w: u16, h: u16) -> Vec<u8> {
fn progressive_jpeg_coefficients_count_toward_the_image_budget() {
assert!(wordcraft_geom::decode_fits(7900, 7900, wordcraft_geom::MAX_DECODED_BYTES));
assert!(load_image(&Arc::new(progressive_jpeg_header(7900, 7900)), PicFx::default()).is_none());
let mut baseline = Vec::new();
image::RgbImage::new(64, 64).write_to(&mut std::io::Cursor::new(&mut baseline), image::ImageFormat::Jpeg).unwrap();
let baseline =
wordcraft_geom::picture::encode_format(&image::DynamicImage::from(image::RgbImage::new(64, 64).clone()), image::ImageFormat::Jpeg).unwrap();
assert!(load_image(&Arc::new(baseline), PicFx::default()).is_some(), "other JPEGs still embed");
}
@@ -1418,15 +1417,12 @@ fn emf_plus_only_bitmap_formats_are_embedded() {
let le32 = |vals: &[i32]| -> Vec<u8> { vals.iter().flat_map(|v| v.to_le_bytes()).collect() };
let encoded = |png: bool| {
let red = [255u8, 0, 0].repeat(15);
let mut out = Vec::new();
if png {
let enc = image::codecs::png::PngEncoder::new(&mut out);
image::ImageEncoder::write_image(enc, &red, 5, 3, image::ExtendedColorType::Rgb8).unwrap();
let img = image::RgbImage::from_raw(5, 3, red).unwrap();
wordcraft_geom::picture::encode_png(&image::DynamicImage::from(img)).unwrap()
} else {
let enc = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 100);
image::ImageEncoder::write_image(enc, &red, 5, 3, image::ExtendedColorType::Rgb8).unwrap();
wordcraft_geom::picture::encode_jpeg(&red, 5, 3, 100).unwrap()
}
out
};
// Name, width, height, stride, pixel format, bitmap type (0 pixels, 1 compressed) and data.
let cases: [(&str, i32, i32, i32, u32, i32, Vec<u8>); 5] = [
@@ -2428,12 +2424,12 @@ fn pdf_fill_opacities(bytes: &[u8]) -> Vec<f32> {
fn picture_fx_is_baked_into_the_embedded_image() {
use wordcraft_doc::picfx::PicFx;
let img = image::RgbImage::from_fn(32, 32, |x, _| image::Rgb([if x < 16 { 100 } else { 0 }, 100, 255]));
let mut png = Vec::new();
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut jpeg = Vec::new();
image::RgbImage::from_pixel(32, 32, image::Rgb([100, 100, 100]))
.write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg)
.unwrap();
let png = wordcraft_geom::picture::encode_format(&image::DynamicImage::from(img.clone()), image::ImageFormat::Png).unwrap();
let jpeg = wordcraft_geom::picture::encode_format(
&image::DynamicImage::from(image::RgbImage::from_pixel(32, 32, image::Rgb([100, 100, 100])).clone()),
image::ImageFormat::Jpeg,
)
.unwrap();
let export_with = |bytes: &[u8], ext: &str, fxs: &[PicFx]| {
let mut d = Document::new();
let media = d.add_media(bytes.to_vec(), ext);
@@ -2515,7 +2511,7 @@ fn hostile_picture_header_is_refused_without_allocating() {
png.extend_from_slice(&0u32.to_be_bytes());
png.extend_from_slice(b"IDAT");
png.extend_from_slice(&crc32(b"IDAT").to_be_bytes());
assert_eq!(image::ImageReader::new(std::io::Cursor::new(&png)).with_guessed_format().unwrap().into_dimensions().unwrap(), (20_000, 20_000));
assert_eq!(wordcraft_geom::picture::dimensions(&png).ok_or("bad image").unwrap(), (20_000, 20_000));
let data = Arc::new(png.clone());
// Refused from the header by the shared decode budget, before anything is decoded, not by
// the missing pixel data that follows.
@@ -2736,15 +2732,14 @@ fn image_budget_is_checked_from_the_header() {
assert!(load_image_within(&data, PicFx::default(), 1024).is_some());
assert!(load_image_within(&data, PicFx::default(), 1023).is_none());
// 16-bit RGBA decodes to 8 bytes a pixel, alive with its RGBA8 conversion: 12 bytes a pixel.
let mut wide = Vec::new();
let img16 = image::ImageBuffer::<image::Rgba<u16>, _>::from_fn(16, 8, |x, y| image::Rgba([x as u16 * 999, y as u16, 9, 65535]));
image::DynamicImage::ImageRgba16(img16).write_to(&mut std::io::Cursor::new(&mut wide), image::ImageFormat::Png).unwrap();
let wide = wordcraft_geom::picture::encode_format(&image::DynamicImage::ImageRgba16(img16).clone(), image::ImageFormat::Png).unwrap();
let wide = Arc::new(wide);
assert!(load_image_within(&wide, PicFx::default(), 1024).is_none(), "two RGBA8 buffers' worth is not enough for RGBA16");
assert!(load_image_within(&wide, PicFx::default(), 1536).is_some());
// A header (valid CRC) claiming 30000x30000 with no pixel data reads fine, then the budget refuses it.
let hdr = ihdr_only_png(30_000, 30_000);
let dims = image::ImageReader::new(std::io::Cursor::new(hdr.as_slice())).with_guessed_format().unwrap().into_dimensions().unwrap();
let dims = wordcraft_geom::picture::dimensions(hdr.as_slice()).unwrap();
assert_eq!(dims, (30_000, 30_000));
assert!(load_image(&Arc::new(hdr), PicFx::default()).is_none());
}