use crate::backend::render::MAX_SVG_BYTES;
use crate::backend::svg::{ReadySvg, SVG_PNG_SCALE};
use crate::backend::within_pixel_cap;
use crate::model::{Picture, PictureFormat};
use crate::word::fmt::model::FImage;
use sha2::{Digest, Sha256};
use std::collections::VecDeque;
use std::sync::Mutex;
pub(crate) const MAX_PICTURE_BYTES: usize = 256 * 1024 * 1024;
pub(crate) const PT_PER_PX: f32 = 0.75;
pub(crate) const FALLBACK_PICTURE_PT: (f32, f32) = (300.0, 200.0);
pub(crate) const MAX_PICTURE_SIDE_PT: f32 = 1584.0;
const PNG_MAGIC: &[u8] = b"\x89PNG";
const JPEG_MAGIC: &[u8] = &[0xFF, 0xD8, 0xFF];
const GIF_MAGIC: &[u8] = b"GIF8";
const BMP_MAGIC: &[u8] = b"BM";
const TIFF_MAGICS: [&[u8]; 2] = [b"II*\0", b"MM\0*"];
const GZIP_MAGIC: &[u8] = &[0x1f, 0x8b];
const MAX_RECENT_VECTOR_BYTES: usize = 64 * 1024 * 1024;
const PX_PER_SVG_PT: f32 = 4.0 / 3.0;
const RIFF_MAGIC: &[u8] = b"RIFF";
const WEBP_FORM: &[u8] = b"WEBP";
const SVG_SNIFF_BYTES: usize = 512;
pub(crate) fn store(pictures: &mut Vec<Picture>, data: Vec<u8>, budget: usize) -> Option<usize> {
let used: usize = pictures.iter().map(|picture| picture.data.len()).sum();
if used >= budget {
return None;
}
let picture = normalize(data)?;
if used + picture.data.len() > budget {
return None;
}
pictures.push(picture);
Some(pictures.len() - 1)
}
pub(crate) fn store_chart(
pictures: &mut Vec<Picture>,
svg: String,
size_pt: (f32, f32),
budget: usize,
) -> Option<usize> {
let used: usize = pictures.iter().map(|picture| picture.data.len()).sum();
if svg.len() > MAX_SVG_BYTES || used + svg.len() > budget {
return None;
}
pictures.push(chart_picture(svg, size_pt));
Some(pictures.len() - 1)
}
pub(crate) fn store_chart_for_layout(
images: &mut Vec<FImage>,
svg: String,
size_pt: (f32, f32),
budget: usize,
) -> Option<usize> {
let size = |image: &FImage| image.data.len() + image.bare.as_ref().map_or(0, Vec::len);
let used: usize = images.iter().map(size).sum();
if svg.len() > MAX_SVG_BYTES || used + svg.len() > budget {
return None;
}
let picture = chart_picture(svg, size_pt);
images.push(FImage {
data: picture.data,
format: picture.format,
bare: None,
texts: Vec::new(),
});
Some(images.len() - 1)
}
fn chart_picture(svg: String, (width, height): (f32, f32)) -> Picture {
let side = |points: f32| (points * PX_PER_SVG_PT * SVG_PNG_SCALE).ceil().max(1.0) as u32;
let (png_width, png_height) = (side(width), side(height));
let fits = u64::from(png_width) * u64::from(png_height) <= crate::backend::MAX_RASTER_PIXELS;
Picture {
data: svg.into_bytes(),
format: PictureFormat::Svg,
png_size: fits.then_some((png_width, png_height)),
outline_text: false,
}
}
pub(crate) fn store_for_layout(images: &mut Vec<FImage>, data: Vec<u8>, budget: usize) -> Option<usize> {
let size = |image: &FImage| image.data.len() + image.bare.as_ref().map_or(0, Vec::len);
let used: usize = images.iter().map(size).sum();
if used >= budget {
return None;
}
let image = for_layout(data)?;
if used + size(&image) > budget {
return None;
}
images.push(image);
Some(images.len() - 1)
}
pub(crate) fn bounded_size((width, height): (f32, f32)) -> Option<(f32, f32)> {
if !(width.is_finite() && height.is_finite() && width > 0.0 && height > 0.0) {
return None;
}
let shrink = (MAX_PICTURE_SIDE_PT / width.max(height)).min(1.0);
Some((width * shrink, height * shrink))
}
pub(crate) fn pixel_size_pt(picture: &Picture) -> Option<(f32, f32)> {
let (width, height) = match picture.format {
PictureFormat::Svg => picture.png_size?,
PictureFormat::Png | PictureFormat::Jpeg => crate::backend::declared_size(&picture.data)?,
};
bounded_size((width as f32 * PT_PER_PX, height as f32 * PT_PER_PX))
}
pub(crate) fn normalize(data: Vec<u8>) -> Option<Picture> {
if !within_pixel_cap(&data) {
return None;
}
if data.starts_with(PNG_MAGIC) {
return Some(Picture::bitmap(data, PictureFormat::Png));
}
if data.starts_with(JPEG_MAGIC) {
return Some(Picture::bitmap(data, PictureFormat::Jpeg));
}
let webp = data.starts_with(RIFF_MAGIC) && data.get(8..12) == Some(WEBP_FORM);
if data.starts_with(GIF_MAGIC)
|| data.starts_with(BMP_MAGIC)
|| webp
|| TIFF_MAGICS.iter().any(|magic| data.starts_with(magic))
{
return to_png(&data);
}
if super::metafile::is_metafile(&data) {
return made_once(&data, || svg_picture(super::metafile::to_svg(&data)?.as_bytes()));
}
if is_svg(&data) {
return made_once(&data, || svg_picture(&data));
}
None
}
fn made_once(data: &[u8], make: impl FnOnce() -> Option<Picture>) -> Option<Picture> {
type Recent = VecDeque<([u8; 32], Option<Picture>)>;
static RECENT: Mutex<Recent> = Mutex::new(VecDeque::new());
let key: [u8; 32] = Sha256::digest(data).into();
let found = RECENT.lock().ok().and_then(|recent| {
recent
.iter()
.find(|(made, _)| *made == key)
.map(|(_, picture)| picture.clone())
});
if let Some(picture) = found {
return picture;
}
let picture = make();
if let Ok(mut recent) = RECENT.lock() {
let size = |picture: &Option<Picture>| picture.as_ref().map_or(0, |picture| picture.data.len());
recent.push_back((key, picture.clone()));
while recent.iter().map(|(_, picture)| size(picture)).sum::<usize>() > MAX_RECENT_VECTOR_BYTES {
recent.pop_front();
}
}
picture
}
pub(crate) fn for_layout(data: Vec<u8>) -> Option<FImage> {
let parts = super::metafile::to_svg_and_text(&data)
.filter(|(_, texts)| !texts.is_empty())
.and_then(|(svg, texts)| Some((crate::backend::sanitize_svg(svg.as_bytes())?, texts)));
let picture = normalize(data)?;
let (bare, texts) = parts.map_or((None, Vec::new()), |(svg, texts)| (Some(svg.into_bytes()), texts));
Some(FImage {
data: picture.data,
format: picture.format,
bare,
texts,
})
}
fn svg_picture(data: &[u8]) -> Option<Picture> {
Some(match crate::backend::prepare_svg(data)? {
ReadySvg::Svg { svg, png_size, .. } => Picture {
data: svg.into_bytes(),
format: PictureFormat::Svg,
png_size,
outline_text: true,
},
ReadySvg::Png(png) => Picture::bitmap(png, PictureFormat::Png),
})
}
fn to_png(data: &[u8]) -> Option<Picture> {
let image = image::load_from_memory(data).ok()?;
let mut png = Vec::new();
image
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
.ok()?;
Some(Picture::bitmap(png, PictureFormat::Png))
}
fn is_svg(data: &[u8]) -> bool {
if data.starts_with(GZIP_MAGIC) {
return true;
}
let head = String::from_utf8_lossy(&data[..data.len().min(SVG_SNIFF_BYTES)]);
head.trim_start().starts_with('<') && head.contains("<svg")
}
#[cfg(test)]
mod tests {
use super::*;
fn png_header(width: u32, height: u32) -> Vec<u8> {
let mut chunk = b"IHDR".to_vec();
chunk.extend(width.to_be_bytes());
chunk.extend(height.to_be_bytes());
chunk.extend([8, 6, 0, 0, 0]);
let mut crc = !0u32;
for &byte in &chunk {
crc ^= u32::from(byte);
for _ in 0..8 {
crc = if crc & 1 == 1 {
(crc >> 1) ^ 0xEDB8_8320
} else {
crc >> 1
};
}
}
let mut data = b"\x89PNG\r\n\x1a\n\0\0\0\x0d".to_vec();
data.extend(chunk);
data.extend((!crc).to_be_bytes());
data
}
#[test]
fn a_tiff_picture_becomes_a_png() {
let mut tiff = Vec::new();
image::RgbImage::from_pixel(3, 2, image::Rgb([200, 30, 30]))
.write_to(&mut std::io::Cursor::new(&mut tiff), image::ImageFormat::Tiff)
.expect("a TIFF");
let picture = normalize(tiff).expect("a PNG");
assert_eq!(picture.format, PictureFormat::Png);
assert_eq!(crate::backend::declared_size(&picture.data), Some((3, 2)));
}
#[test]
fn a_raster_picture_over_the_pixel_cap_is_skipped() {
assert!(normalize(png_header(100_000, 100_000)).is_none());
assert!(for_layout(png_header(100_000, 100_000)).is_none());
assert!(normalize(png_header(400, 300)).is_some());
}
#[test]
fn a_huge_svg_picture_stays_svg_and_is_sized_as_its_png_would_be() {
let svg = br#"<svg xmlns="http://www.w3.org/2000/svg" width="30000" height="30000"><rect width="10" height="10"/></svg>"#;
let picture = normalize(svg.to_vec()).expect("kept");
assert_eq!(picture.format, PictureFormat::Svg);
let (width, height) = pixel_size_pt(&picture).expect("a size");
assert_eq!((width, height), (MAX_PICTURE_SIDE_PT, MAX_PICTURE_SIDE_PT));
let small =
br#"<svg xmlns="http://www.w3.org/2000/svg" width="40" height="20"><rect width="10" height="10"/></svg>"#;
let small = normalize(small.to_vec()).unwrap();
assert_eq!(
pixel_size_pt(&small),
Some((40.0 * 2.0 * PT_PER_PX, 20.0 * 2.0 * PT_PER_PX))
);
}
#[test]
fn the_size_of_a_large_picture_keeps_its_aspect() {
let png = |width, height| Picture::bitmap(png_header(width, height), PictureFormat::Png);
let (width, height) = pixel_size_pt(&png(4000, 1000)).unwrap();
assert_eq!((width, height), (MAX_PICTURE_SIDE_PT, MAX_PICTURE_SIDE_PT / 4.0));
assert_eq!(pixel_size_pt(&png(400, 200)), Some((300.0, 150.0)));
let damaged = Picture::bitmap(b"not a picture".to_vec(), PictureFormat::Png);
assert_eq!(pixel_size_pt(&damaged), None);
}
#[test]
fn pictures_past_the_byte_budget_are_skipped() {
let mut pictures = Vec::new();
let png = png_header(4, 2);
let budget = png.len() * 2;
assert_eq!(store(&mut pictures, png.clone(), budget), Some(0));
assert_eq!(store(&mut pictures, png.clone(), budget), Some(1));
assert_eq!(store(&mut pictures, png.clone(), budget), None);
assert_eq!(pictures.len(), 2);
}
#[test]
fn formats() {
let png = b"\x89PNG\r\n\x1a\nrest".to_vec();
let picture = normalize(png.clone()).unwrap();
assert_eq!((picture.data, picture.format), (png, PictureFormat::Png));
assert_eq!(
normalize(vec![0xFF, 0xD8, 0xFF, 0xE0]).unwrap().format,
PictureFormat::Jpeg
);
assert!(normalize(vec![1, 0, 0, 0, 0x6C, 0, 0, 0]).is_none());
let svg = b"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"4\" height=\"2\" onload=\"alert(1)\"><script>alert(2)</script><rect width=\"4\" height=\"2\"/></svg>";
let picture = normalize(svg.to_vec()).unwrap();
assert_eq!(picture.format, PictureFormat::Svg);
let text = String::from_utf8(picture.data).unwrap();
assert!(text.contains("<path") && !text.contains("alert"), "{text}");
}
#[test]
fn a_vector_picture_is_made_ready_once_for_both_readings() {
let defs: String = (0..100)
.map(|i| format!(r#"<filter id="f{i}"><feGaussianBlur stdDeviation="3"/></filter>"#))
.collect();
let rects: String = (0..100)
.map(|i| format!(r#"<rect filter="url(#f{i})" x="{i}" width="2000" height="1000"/>"#))
.collect();
let svg = format!(r#"<svg xmlns="http://www.w3.org/2000/svg" width="2000" height="1000">{defs}{rects}</svg>"#);
let start = std::time::Instant::now();
let first = normalize(svg.clone().into_bytes()).expect("a PNG");
let once = start.elapsed();
let start = std::time::Instant::now();
let image = for_layout(svg.into_bytes()).expect("a PNG");
let again = start.elapsed();
assert_eq!((first.data, first.format), (image.data, image.format));
assert!(again * 10 < once, "{once:?} then {again:?}");
}
#[test]
fn a_compressed_svg_is_read() {
use std::io::Write;
let svg = br#"<svg xmlns="http://www.w3.org/2000/svg" width="4" height="2"><rect width="4" height="2"/></svg>"#;
let mut gzip = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
gzip.write_all(svg).unwrap();
let picture = normalize(gzip.finish().unwrap()).expect("an SVG");
assert_eq!(picture.format, PictureFormat::Svg);
assert_eq!(picture.png_size, Some((8, 4)));
}
#[test]
fn an_svg_of_many_copies_is_kept_as_a_png() {
let leaf = format!("M0 0{}", " l1 1".repeat(200));
let groups: String = (1..=4)
.map(|level| {
format!(
r##"<g id="a{level}">{}</g>"##,
format!(r##"<use href="#a{}"/>"##, level - 1).repeat(10)
)
})
.collect();
let svg = format!(
r##"<svg xmlns="http://www.w3.org/2000/svg" width="100" height="50"><defs><path id="a0" d="{leaf}"/>{groups}</defs><use href="#a4"/></svg>"##
);
let picture = normalize(svg.into_bytes()).expect("a PNG");
assert_eq!(picture.format, PictureFormat::Png);
assert!(picture.data.starts_with(PNG_MAGIC));
}
#[test]
fn a_metafile_becomes_svg() {
let mut wmf = Vec::new();
wmf.extend_from_slice(&0x9AC6_CDD7u32.to_le_bytes());
for value in [0u16, 0, 0, 1000, 500, 1000, 0, 0, 0] {
wmf.extend_from_slice(&value.to_le_bytes());
}
for value in [1u16, 9, 0x300, 0, 0, 3, 0, 0, 0] {
wmf.extend_from_slice(&value.to_le_bytes());
}
let mut record = |function: u16, params: &[i16]| {
wmf.extend_from_slice(&(3 + params.len() as u32).to_le_bytes());
wmf.extend_from_slice(&function.to_le_bytes());
for param in params {
wmf.extend_from_slice(¶m.to_le_bytes());
}
};
record(0x020B, &[0, 0]);
record(0x020C, &[500, 1000]);
record(0x041B, &[400, 900, 100, 100]);
record(0x0521, &[2, i16::from_le_bytes(*b"Hi"), 300, 200]);
record(0, &[]);
let picture = normalize(wmf.clone()).expect("an SVG");
assert_eq!(picture.format, PictureFormat::Svg);
let image = for_layout(wmf).expect("an SVG");
assert_eq!(image.format, PictureFormat::Svg);
let bare = String::from_utf8(image.bare.expect("the picture without its text")).unwrap();
assert!(bare.starts_with("<svg") && bare.contains("<path"), "{bare}");
assert_eq!(image.texts.len(), 1);
}
}