From 4107c2985e82633fee60664306337f0684afb089 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 19 Dec 2023 19:01:12 -0500 Subject: [PATCH] Tests: Add a PDF rendering test Having some rendering test coverage is motivated by #22362, but this test wouldn't have found the crashes over there (since colorspaces.pdf does not contain pattern color spaces). Still, good to have some in-repo test coverage of PDF rendering. --- Tests/LibPDF/CMakeLists.txt | 2 +- Tests/LibPDF/TestPDF.cpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Tests/LibPDF/CMakeLists.txt b/Tests/LibPDF/CMakeLists.txt index 10c10f4176..8ed4779bc4 100644 --- a/Tests/LibPDF/CMakeLists.txt +++ b/Tests/LibPDF/CMakeLists.txt @@ -3,7 +3,7 @@ set(TEST_SOURCES ) foreach(source IN LISTS TEST_SOURCES) - serenity_test("${source}" LibPDF LIBS LibCore LibPDF) + serenity_test("${source}" LibPDF LIBS LibCore LibGfx LibPDF) endforeach() set(TEST_FILES diff --git a/Tests/LibPDF/TestPDF.cpp b/Tests/LibPDF/TestPDF.cpp index b5e99815be..319aa37488 100644 --- a/Tests/LibPDF/TestPDF.cpp +++ b/Tests/LibPDF/TestPDF.cpp @@ -7,9 +7,11 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -267,3 +269,34 @@ TEST_CASE(postscript) check_evaluate("{ 3 1 roll }"sv, { 0.5f, 1.0f, 2.0f }, { 2.0f, 0.5f, 1.0f }); check_evaluate("{ 3 -1 roll }"sv, { 0.5f, 1.0f, 2.0f }, { 1.0f, 2.0f, 0.5f }); } + +TEST_CASE(render) +{ + auto file = MUST(Core::MappedFile::map("colorspaces.pdf"sv)); + auto document = MUST(PDF::Document::create(file->bytes())); + MUST(document->initialize()); + EXPECT_EQ(document->get_page_count(), 1U); + + auto page = MUST(document->get_page(0)); + auto page_size = Gfx::IntSize { 310, 370 }; + auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, page_size)); + MUST(PDF::Renderer::render(document, page, bitmap, Color::White, PDF::RenderingPreferences {})); + + // DeviceGray + EXPECT_EQ(bitmap->get_pixel(270, 370 - 20), Gfx::Color::NamedColor::Black); + + // MyCalRGB + EXPECT_EQ(bitmap->get_pixel(270, 370 - 80), Gfx::Color::NamedColor::Black); + + // DeviceRGB + EXPECT_EQ(bitmap->get_pixel(270, 370 - 140), Gfx::Color::NamedColor::Black); + + // DeviceCMYK (note: black one box further left) + EXPECT_EQ(bitmap->get_pixel(220, 370 - 200), Gfx::Color::NamedColor::Black); + + // MyLab + EXPECT_EQ(bitmap->get_pixel(270, 370 - 260), Gfx::Color::NamedColor::Black); + + // MyCalGray + EXPECT_EQ(bitmap->get_pixel(270, 370 - 320), Gfx::Color::NamedColor::Black); +}