From d5f94aaa7b84d313613808f813c97354e504c7ba Mon Sep 17 00:00:00 2001 From: Matthew Olsson Date: Mon, 10 May 2021 11:52:48 -0700 Subject: [PATCH] LibPDF/PDFViewer: Support rotated pages --- Userland/Applications/PDFViewer/PDFViewer.cpp | 11 +++++++++++ Userland/Libraries/LibPDF/Document.cpp | 8 +++++++- Userland/Libraries/LibPDF/Document.h | 6 ++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Userland/Applications/PDFViewer/PDFViewer.cpp b/Userland/Applications/PDFViewer/PDFViewer.cpp index 1a309ab6c5..a69fcb4bfa 100644 --- a/Userland/Applications/PDFViewer/PDFViewer.cpp +++ b/Userland/Applications/PDFViewer/PDFViewer.cpp @@ -87,5 +87,16 @@ RefPtr PDFViewer::render_page(const PDF::Page& page) auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { width, height }); PDF::Renderer::render(*m_document, page, bitmap); + + if (page.rotate != 0) { + int rotation_count = (page.rotate / 90) % 4; + if (rotation_count == 3) { + bitmap = bitmap->rotated(Gfx::RotationDirection::CounterClockwise); + } else { + for (int i = 0; i < rotation_count; i++) + bitmap = bitmap->rotated(Gfx::RotationDirection::Clockwise); + } + } + return bitmap; } diff --git a/Userland/Libraries/LibPDF/Document.cpp b/Userland/Libraries/LibPDF/Document.cpp index b5ff2df818..dfd0e0e786 100644 --- a/Userland/Libraries/LibPDF/Document.cpp +++ b/Userland/Libraries/LibPDF/Document.cpp @@ -88,7 +88,13 @@ Page Document::get_page(u32 index) if (raw_page_object->contains("UserUnit")) user_unit = raw_page_object->get_value("UserUnit").to_float(); - Page page { move(resources), move(contents), media_box, crop_box, user_unit }; + int rotate = 0; + if (raw_page_object->contains("Rotate")) { + rotate = raw_page_object->get_value("Rotate").as_int(); + VERIFY(rotate % 90 == 0); + } + + Page page { move(resources), move(contents), media_box, crop_box, user_unit, rotate }; m_pages.set(index, page); return page; } diff --git a/Userland/Libraries/LibPDF/Document.h b/Userland/Libraries/LibPDF/Document.h index 14463d11d1..343f76f405 100644 --- a/Userland/Libraries/LibPDF/Document.h +++ b/Userland/Libraries/LibPDF/Document.h @@ -28,6 +28,7 @@ struct Page { Rectangle media_box; Rectangle crop_box; float user_unit; + int rotate; }; class Document final : public RefCounted { @@ -121,13 +122,14 @@ template<> struct Formatter : Formatter { void format(FormatBuilder& builder, const PDF::Page& page) { - constexpr auto fmt_string = "Page {{\n resources={}\n contents={}\n media_box={}\n crop_box={}\n user_unit={}\n}}"; + constexpr auto fmt_string = "Page {{\n resources={}\n contents={}\n media_box={}\n crop_box={}\n user_unit={}\n rotate={}\n}}"; auto str = String::formatted(fmt_string, page.resources->to_string(1), page.contents->to_string(1), page.media_box, page.crop_box, - page.user_unit); + page.user_unit, + page.rotate); Formatter::format(builder, str); } };