Add tests

This commit is contained in:
Laurenz Stampfl 2025-07-20 15:13:34 +02:00
parent 62eab1dc9f
commit 2f72a1f197
6 changed files with 20 additions and 5 deletions

View File

@ -310,10 +310,12 @@ impl Packed<ImageElem> {
let num_pages = document.len();
let Some(pdf_image) = PdfImage::new(document, page_idx) else {
let pages = if num_pages == 1 { "page" } else { "pages" };
bail!(
span,
"page {page_num} doesn't exist";
hint: "the document only has {num_pages} pages"
hint: "the document only has {num_pages} {pages}"
);
};

View File

@ -58,8 +58,8 @@ impl Hash for ImageRepr {
pub struct PdfImage(Arc<ImageRepr>);
impl PdfImage {
/// Create a new PDF image.
///
/// Create a new PDF image.
///
/// Returns `None` if the page index is not valid.
#[comemo::memoize]
pub fn new(document: PdfDocument, page: usize) -> Option<PdfImage> {

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

BIN
tests/ref/image-pdf.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

View File

@ -198,7 +198,7 @@ fn library() -> Library {
// exactly 100pt wide. Page height is unbounded and font size is 10pt so
// that it multiplies to nice round numbers.
let mut lib = Library::builder()
.with_features([Feature::Html].into_iter().collect())
.with_features([Feature::Html, Feature::PdfEmbedding].into_iter().collect())
.build();
// Hook up helpers into the global scope.

View File

@ -258,7 +258,7 @@ A #box(image("/assets/images/tiger.jpg", height: 1cm, width: 80%)) B
--- image-png-but-pixmap-format ---
#image(
read("/assets/images/tiger.jpg", encoding: none),
// Error: 11-18 expected "png", "jpg", "gif", "webp", dictionary, "svg", or auto
// Error: 11-18 expected "png", "jpg", "gif", "webp", dictionary, "svg", "pdf", or auto
format: "rgba8",
)
@ -289,3 +289,16 @@ A #box(image("/assets/images/tiger.jpg", height: 1cm, width: 80%)) B
..rotations.map(v => raw(str(v), lang: "typc")),
..rotations.map(rotated)
)
--- image-pdf ---
#image("/assets/images/matplotlib.pdf")
--- image-pdf-invalid-page ---
// Error: 2-49 page 2 doesn't exist
// Hint: 2-49 the document only has 1 page
#image("/assets/images/matplotlib.pdf", page: 2)
--- image-multiple-pages ---
#image("/assets/images/diagrams.pdf", page: 1)
#image("/assets/images/diagrams.pdf", page: 3)
#image("/assets/images/diagrams.pdf", page: 2)