Embed project images in route-D previews and PDFs

This commit is contained in:
GloamXun committed 2026-10-24 23:29:15 +08:00
1 parent ae69eba64d
commit 0f8aff7290
8 files changed
+407 -18

No files matched your search

+103
View File
@@ -1630,3 +1630,106 @@ fn route_d_preview_reports_declared_paper_size() {
&text[..200.min(text.len())]
);
}
/// A tiny 2x2 24-bit BMP for image embedding tests.
fn two_by_two_bmp() -> Vec<u8> {
let pixel_bytes: u32 = 16;
let file_size: u32 = 54 + pixel_bytes;
let mut bytes = Vec::new();
bytes.extend_from_slice(b"BM");
bytes.extend_from_slice(&file_size.to_le_bytes());
bytes.extend_from_slice(&[0, 0, 0, 0]);
bytes.extend_from_slice(&54u32.to_le_bytes());
bytes.extend_from_slice(&40u32.to_le_bytes());
bytes.extend_from_slice(&2i32.to_le_bytes());
bytes.extend_from_slice(&2i32.to_le_bytes());
bytes.extend_from_slice(&1u16.to_le_bytes());
bytes.extend_from_slice(&24u16.to_le_bytes());
bytes.extend_from_slice(&0u32.to_le_bytes());
bytes.extend_from_slice(&pixel_bytes.to_le_bytes());
bytes.extend_from_slice(&2835i32.to_le_bytes());
bytes.extend_from_slice(&2835i32.to_le_bytes());
bytes.extend_from_slice(&0u32.to_le_bytes());
bytes.extend_from_slice(&0u32.to_le_bytes());
for row in [[0u8, 0, 255, 0, 255, 0], [255, 0, 0, 255, 255, 255]] {
bytes.extend_from_slice(&row);
bytes.extend_from_slice(&[0, 0]);
}
bytes
}
#[test]
fn project_preview_and_pdf_embed_images() {
let server = TestServer::start("route-d-image");
let image = two_by_two_bmp();
let put = server.request(
"PUT",
"/api/projects/hello/file?path=pixel.bmp",
&image,
"image/bmp",
);
assert_eq!(put.status, 200, "{}", put.text());
let main = "\\documentclass{article}\n\\begin{document}\n\\begin{figure}[t]\n\\includegraphics[width=120pt]{pixel.bmp}\n\\caption{Pixel}\n\\end{figure}\n\\end{document}\n";
let put = server.request(
"PUT",
"/api/projects/hello/file?path=hello.tex",
main.as_bytes(),
"text/plain; charset=utf-8",
);
assert_eq!(put.status, 200, "{}", put.text());
let preview = server.request(
"POST",
"/api/projects/hello/preview?engine=route-d",
&[],
"application/json",
);
assert_eq!(preview.status, 200, "{}", preview.text());
let svg = preview.text();
assert!(svg.contains(r##"fill=\"#cccccc\""##), "preview: {svg}");
assert!(svg.contains(">P</text>"), "preview: {svg}");
let pdf = server.request(
"POST",
"/api/projects/hello/pdf?engine=route-d",
&[],
"application/pdf",
);
assert_eq!(pdf.status, 200);
assert!(
pdf.body
.windows("/Subtype /Image".len())
.any(|window| window == b"/Subtype /Image"),
"pdf should embed the raster image"
);
let mut changed = two_by_two_bmp();
changed[54] ^= 0xff;
let put = server.request(
"PUT",
"/api/projects/hello/file?path=pixel.bmp",
&changed,
"image/bmp",
);
assert_eq!(put.status, 200, "{}", put.text());
let preview = server.request(
"POST",
"/api/projects/hello/preview?engine=route-d",
&[],
"application/json",
);
assert_eq!(preview.status, 200, "{}", preview.text());
assert!(
preview.text().contains("\"recomputedPages\":1"),
"editing the image should invalidate the preview cache: {}",
preview.text()
);
let pdf = server.request(
"POST",
"/api/projects/hello/pdf?engine=route-d",
&[],
"application/pdf",
);
assert_eq!(pdf.status, 200);
assert_eq!(pdf.header("x-texrra-cache"), Some("miss"));
}