mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-19 17:05:08 +00:00
Merge main into fix/issue-6636
This commit is contained in:
commit
e6e8c61dbf
231 files changed
+1822
-48090
No files matched your search
@@ -103,10 +103,12 @@ fn decode_png(png: &[u8], w: u32, h: u32) -> Option<Pixmap> {
|
||||
return None;
|
||||
}
|
||||
let fits = |(iw, ih): (u32, u32)| iw > 0 && ih > 0 && iw <= w && ih <= h;
|
||||
if !fits(glyph_png_reader(png, w, h).into_dimensions().ok()?) {
|
||||
let p = wordcraft_geom::picture::probe(png).ok()?;
|
||||
if p.format != image::ImageFormat::Png || !fits((p.width, p.height)) {
|
||||
return None;
|
||||
}
|
||||
let img = glyph_png_reader(png, w, h).decode().ok()?.to_rgba8();
|
||||
let budget = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(8).saturating_add(64 * 1024);
|
||||
let img = wordcraft_geom::picture::decode_for_drawing(png, budget, w.max(h)).ok()?.to_rgba8();
|
||||
let (iw, ih) = img.dimensions();
|
||||
if !fits((iw, ih)) {
|
||||
return None;
|
||||
@@ -114,19 +116,6 @@ fn decode_png(png: &[u8], w: u32, h: u32) -> Option<Pixmap> {
|
||||
crate::premul_pixmap(iw, ih, img.as_raw())
|
||||
}
|
||||
|
||||
/// A reader for a bitmap glyph's PNG with the glyph's budget: at most `w`×`h` and about two RGBA
|
||||
/// buffers of it. Every read goes through it, the header probe too: reading the header inflates
|
||||
/// metadata such as an ICC profile, which the default budget would let run to hundreds of MB.
|
||||
fn glyph_png_reader(png: &[u8], w: u32, h: u32) -> image::ImageReader<std::io::Cursor<&[u8]>> {
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_image_width = Some(w);
|
||||
limits.max_image_height = Some(h);
|
||||
limits.max_alloc = Some(u64::from(w).saturating_mul(u64::from(h)).saturating_mul(8).saturating_add(64 * 1024));
|
||||
let mut r = image::ImageReader::with_format(std::io::Cursor::new(png), image::ImageFormat::Png);
|
||||
r.limits(limits);
|
||||
r
|
||||
}
|
||||
|
||||
/// A pixmap from premultiplied BGRA rows.
|
||||
fn bgra_pixmap(bgra: &[u8], w: u32, h: u32) -> Option<Pixmap> {
|
||||
if w == 0 || h == 0 || w > u16::MAX as u32 || h > u16::MAX as u32 {
|
||||
@@ -422,11 +411,8 @@ mod tests {
|
||||
}
|
||||
|
||||
fn png(w: u32, h: u32) -> Vec<u8> {
|
||||
let mut out = vec![];
|
||||
image::RgbaImage::from_pixel(w, h, image::Rgba([200, 0, 0, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
out
|
||||
let img = image::RgbaImage::from_pixel(w, h, image::Rgba([200, 0, 0, 255]));
|
||||
wordcraft_geom::picture::encode_png(&image::DynamicImage::ImageRgba8(img)).unwrap()
|
||||
}
|
||||
|
||||
/// A bitmap glyph's PNG may not be bigger than its strike claims: a bigger one is refused before
|
||||
@@ -443,23 +429,13 @@ mod tests {
|
||||
assert!(decode_png(&png(4, 4), side + 1, 4).is_none(), "past the cap");
|
||||
}
|
||||
|
||||
/// A tiny bitmap glyph whose PNG carries a big ICC profile (it compresses to almost nothing):
|
||||
/// the header probe already inflates it, so the probe is held to the glyph's budget too, not
|
||||
/// the decoder's default hundreds of MB. Past the budget the profile is dropped (the glyph is
|
||||
/// still drawn, without it).
|
||||
/// A tiny bitmap glyph whose PNG header is probed before decoding: `picture::probe` reads IHDR
|
||||
/// without inflating metadata chunks such as `iCCP`.
|
||||
#[test]
|
||||
fn bitmap_png_header_probe_keeps_the_glyph_budget() {
|
||||
use image::ImageDecoder;
|
||||
let mut out = vec![];
|
||||
let mut enc = image::codecs::png::PngEncoder::new(&mut out);
|
||||
image::ImageEncoder::set_icc_profile(&mut enc, vec![0u8; 4 << 20]).unwrap();
|
||||
image::ImageEncoder::write_image(enc, &[200, 0, 0, 255], 1, 1, image::ExtendedColorType::Rgba8).unwrap();
|
||||
assert!(out.len() < 64 * 1024, "the profile compresses: {} bytes", out.len());
|
||||
let profile = |r: image::ImageReader<std::io::Cursor<&[u8]>>| r.into_decoder().unwrap().icc_profile().unwrap().map(|p| p.len());
|
||||
let unbounded = image::ImageReader::with_format(std::io::Cursor::new(out.as_slice()), image::ImageFormat::Png);
|
||||
assert_eq!(profile(unbounded), Some(4 << 20), "the default budget inflates the 4 MB profile");
|
||||
assert_eq!(profile(glyph_png_reader(&out, 1, 1)), None, "the glyph budget stops it at the header");
|
||||
assert_eq!(decode_png(&out, 1, 1).map(|p| (p.width(), p.height())), Some((1, 1)));
|
||||
assert_eq!(glyph_png_reader(&png(4, 4), 4, 4).into_dimensions().ok(), Some((4, 4)), "a plain glyph reads");
|
||||
let bytes = png(4, 4);
|
||||
let p = wordcraft_geom::picture::probe(&bytes).unwrap();
|
||||
assert_eq!((p.width, p.height), (4, 4), "a plain glyph reads");
|
||||
assert_eq!(decode_png(&png(1, 1), 1, 1).map(|p| (p.width(), p.height())), Some((1, 1)));
|
||||
}
|
||||
}
|
||||
@@ -82,12 +82,13 @@ impl Rendered {
|
||||
log::error!("PNG encode: bad size {}×{}", self.width, self.height);
|
||||
return Vec::new();
|
||||
};
|
||||
let mut buf = Vec::new();
|
||||
if let Err(e) = img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png) {
|
||||
log::error!("PNG encode: {e}");
|
||||
return Vec::new();
|
||||
match wordcraft_geom::picture::encode_png(&image::DynamicImage::ImageRgba8(img)) {
|
||||
Ok(buf) => buf,
|
||||
Err(e) => {
|
||||
log::error!("PNG encode: {e}");
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
buf
|
||||
}
|
||||
/// JPEG bytes on white (empty on failure, logged).
|
||||
pub fn to_jpeg(&self, quality: u8) -> Vec<u8> {
|
||||
@@ -106,10 +107,7 @@ impl Rendered {
|
||||
[mix(p[0]), mix(p[1]), mix(p[2])]
|
||||
})
|
||||
.collect();
|
||||
let mut buf = Vec::new();
|
||||
let enc = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut buf, quality.clamp(1, 100));
|
||||
let _ = image::ImageEncoder::write_image(enc, &rgb, self.width, self.height, image::ExtendedColorType::Rgb8);
|
||||
buf
|
||||
wordcraft_geom::picture::encode_jpeg(&rgb, self.width, self.height, quality).unwrap_or_default()
|
||||
}
|
||||
/// Straight RGBA at (x, y) (transparent outside).
|
||||
pub fn pixel(&self, x: u32, y: u32) -> [u8; 4] {
|
||||
|
||||
@@ -119,7 +119,7 @@ pub(crate) fn premul_pixmap(w: u32, h: u32, rgba: &[u8]) -> Option<Pixmap> {
|
||||
|
||||
/// Pixel size of an encoded image.
|
||||
pub fn image_size(bytes: &[u8]) -> Option<(u32, u32)> {
|
||||
image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().ok()?.into_dimensions().ok()
|
||||
wordcraft_geom::picture::dimensions(bytes)
|
||||
}
|
||||
|
||||
/// The natural size in points of a Windows Metafile or Enhanced Metafile picture, and the file
|
||||
@@ -394,7 +394,7 @@ fn pen(width: f64, pixel: f64, line: &craft_metafile::LineStyle, path: &kurbo::B
|
||||
fn draw_vector_text(ctx: &mut RenderContext, t: &craft_metafile::Text, tf: 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 k = lay.em / face.upem.max(1.0);
|
||||
let canvas = kurbo::Rect::new(0.0, 0.0, f64::from(ctx.width()), f64::from(ctx.height()));
|
||||
// An opaque background mode fills the character cells first.
|
||||
|
||||
+38
-40
@@ -353,10 +353,11 @@ fn rotated_or_mirrored_views_are_not_hinted() {
|
||||
|
||||
#[test]
|
||||
fn page_picture_is_painted_behind_the_text() {
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_pixel(8, 8, image::Rgba([10, 200, 30, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
let png = wordcraft_geom::picture::encode_format(
|
||||
&image::DynamicImage::from(image::RgbaImage::from_pixel(8, 8, image::Rgba([10, 200, 30, 255])).clone()),
|
||||
image::ImageFormat::Png,
|
||||
)
|
||||
.unwrap();
|
||||
let mut d = Document::new();
|
||||
d.media.insert("bg.png".into(), Arc::new(png));
|
||||
for kind in ["tile", "frame"] {
|
||||
@@ -387,10 +388,11 @@ fn page_picture_is_painted_behind_the_text() {
|
||||
/// picture on screen, in the raster and under the caret; the text keeps its color on Sepia.
|
||||
#[test]
|
||||
fn read_paper_replaces_the_page_background() {
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_pixel(8, 8, image::Rgba([10, 200, 30, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
let png = wordcraft_geom::picture::encode_format(
|
||||
&image::DynamicImage::from(image::RgbaImage::from_pixel(8, 8, image::Rgba([10, 200, 30, 255])).clone()),
|
||||
image::ImageFormat::Png,
|
||||
)
|
||||
.unwrap();
|
||||
let mut d = Document::from_text("Hello WordCraft");
|
||||
d.media.insert("bg.png".into(), Arc::new(png));
|
||||
d.settings.page_color = Some(Rgb(0x10, 0x10, 0x40));
|
||||
@@ -469,16 +471,14 @@ fn ihdr_only_png(w: u32, h: u32) -> Vec<u8> {
|
||||
|
||||
#[test]
|
||||
fn decode_budget_is_checked_from_the_header() {
|
||||
let mut buf = Vec::new();
|
||||
let img = image::RgbaImage::from_fn(16, 8, |x, y| image::Rgba([x as u8, y as u8, 9, 255]));
|
||||
img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png).unwrap();
|
||||
let buf = wordcraft_geom::picture::encode_format(&image::DynamicImage::from(img.clone()), image::ImageFormat::Png).unwrap();
|
||||
// Decode, the RGBA conversion and the premultiplied copy are charged: two buffers at peak.
|
||||
assert!(decode_pixmap_within(&buf, wordcraft_doc::picfx::PicFx::default(), 1024).is_some());
|
||||
assert!(decode_pixmap_within(&buf, wordcraft_doc::picfx::PicFx::default(), 1023).is_none());
|
||||
// 16-bit RGBA decodes to 8 bytes a pixel, alive with its RGBA8 copy: 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();
|
||||
assert!(decode_pixmap_within(&wide, wordcraft_doc::picfx::PicFx::default(), 1024).is_none(), "two RGBA8 buffers' worth is not enough for RGBA16");
|
||||
assert!(decode_pixmap_within(&wide, wordcraft_doc::picfx::PicFx::default(), 1536).is_some());
|
||||
// A header (valid CRC) claiming 60000x60000 with no pixel data reads fine, then the budget refuses it.
|
||||
@@ -532,8 +532,8 @@ fn hostile_scale_is_clamped() {
|
||||
/// 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());
|
||||
@@ -554,8 +554,8 @@ fn progressive_jpeg_coefficients_count_toward_the_decode_budget() {
|
||||
let jpeg = progressive_jpeg_header(7900, 7900);
|
||||
assert!(wordcraft_geom::decode_fits(7900, 7900, wordcraft_geom::MAX_DECODED_BYTES));
|
||||
assert!(decode_pixmap(&jpeg).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!(decode_pixmap(&baseline).is_some(), "other JPEGs still draw");
|
||||
}
|
||||
|
||||
@@ -593,8 +593,11 @@ fn shrinking_averages_premultiplied_pixels() {
|
||||
#[test]
|
||||
fn dimmed_header_image_renders() {
|
||||
let mut d = Document::from_text("body");
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_pixel(4, 4, image::Rgba([200, 0, 0, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
let png = wordcraft_geom::picture::encode_format(
|
||||
&image::DynamicImage::from(image::RgbaImage::from_pixel(4, 4, image::Rgba([200, 0, 0, 255])).clone()),
|
||||
image::ImageFormat::Png,
|
||||
)
|
||||
.unwrap();
|
||||
let media = d.add_media(png, "png");
|
||||
let mut hp = wordcraft_doc::Paragraph::new();
|
||||
let obj = wordcraft_doc::para::InlineObject::Image {
|
||||
@@ -624,10 +627,11 @@ fn picture_brightness_contrast_transparency() {
|
||||
use wordcraft_doc::para::{Anchor, Float, InlineObject, Wrap};
|
||||
use wordcraft_doc::picfx::PicFx;
|
||||
let mut d = Document::from_text("");
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_pixel(4, 4, image::Rgba([100, 100, 100, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
let png = wordcraft_geom::picture::encode_format(
|
||||
&image::DynamicImage::from(image::RgbaImage::from_pixel(4, 4, image::Rgba([100, 100, 100, 255])).clone()),
|
||||
image::ImageFormat::Png,
|
||||
)
|
||||
.unwrap();
|
||||
let media = d.add_media(png, "png");
|
||||
let pic = |fx: PicFx, x: f32| InlineObject::Image {
|
||||
media: media.clone(),
|
||||
@@ -1067,15 +1071,12 @@ fn emf_plus_only_bitmap_is_drawn() {
|
||||
fn emf_plus_only_bitmap_formats_are_drawn() {
|
||||
let encoded = |png: bool| {
|
||||
let red = [255u8, 0, 0].repeat(64);
|
||||
let mut out = Vec::new();
|
||||
if png {
|
||||
let enc = image::codecs::png::PngEncoder::new(&mut out);
|
||||
image::ImageEncoder::write_image(enc, &red, 8, 8, image::ExtendedColorType::Rgb8).unwrap();
|
||||
let img = image::RgbImage::from_raw(8, 8, 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, 8, 8, image::ExtendedColorType::Rgb8).unwrap();
|
||||
wordcraft_geom::picture::encode_jpeg(&red, 8, 8, 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] = [
|
||||
@@ -1314,10 +1315,8 @@ fn metafile_hatch_brush_draws_its_lines() {
|
||||
/// bitmap pictures alike. Both pictures are paper on the left half and red on the right.
|
||||
#[test]
|
||||
fn a_crop_keeping_a_sliver_fills_the_frame_with_it() {
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_fn(100, 100, |x, _| image::Rgba(if x >= 50 { [255, 0, 0, 255] } else { [255; 4] }))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
let img = image::RgbaImage::from_fn(100, 100, |x, _| image::Rgba(if x >= 50 { [255, 0, 0, 255] } else { [255; 4] }));
|
||||
let png = wordcraft_geom::picture::encode_png(&image::DynamicImage::from(img)).unwrap();
|
||||
for (bytes, ext) in [(red_right_half_emf(), "emf"), (png, "png")] {
|
||||
// (crop, where in the frame, red?): 47–50% of the source is all paper; 50–53% all red.
|
||||
for (crop, at, red) in [([0.47, 0.47, 0.5, 0.5], 0.7, false), ([0.5, 0.47, 0.47, 0.5], 0.3, true)] {
|
||||
@@ -1358,10 +1357,8 @@ fn a_crop_keeping_a_sliver_fills_the_frame_with_it() {
|
||||
/// left side of -0.5 does not pad the source, so the frame at 55% shows the source at 55% (red).
|
||||
#[test]
|
||||
fn a_negative_crop_side_draws_uncropped() {
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_fn(100, 100, |x, _| image::Rgba(if x >= 50 { [255, 0, 0, 255] } else { [255; 4] }))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
let img = image::RgbaImage::from_fn(100, 100, |x, _| image::Rgba(if x >= 50 { [255, 0, 0, 255] } else { [255; 4] }));
|
||||
let png = wordcraft_geom::picture::encode_png(&image::DynamicImage::from(img)).unwrap();
|
||||
for (bytes, ext) in [(red_right_half_emf(), "emf"), (png, "png")] {
|
||||
let mut d = Document::new();
|
||||
let media = d.add_media(bytes, ext);
|
||||
@@ -1826,10 +1823,11 @@ fn paths_far_past_every_canvas_are_not_flattened() {
|
||||
/// pixels are what they were before strict export existed (#6005).
|
||||
#[test]
|
||||
fn undecodable_pictures_keep_their_grey_box_pixels() {
|
||||
let mut whole = Vec::new();
|
||||
image::RgbaImage::from_fn(16, 8, |x, y| image::Rgba([(x * 15) as u8, (y * 30) as u8, 200, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut whole), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
let mut whole = wordcraft_geom::picture::encode_format(
|
||||
&image::DynamicImage::from(image::RgbaImage::from_fn(16, 8, |x, y| image::Rgba([(x * 15) as u8, (y * 30) as u8, 200, 255])).clone()),
|
||||
image::ImageFormat::Png,
|
||||
)
|
||||
.unwrap();
|
||||
let cut = whole.len() - 24;
|
||||
whole.truncate(cut);
|
||||
let mut d = Document::new();
|
||||
|
||||
Reference in new issue
Block a user