Browse Source

LibPDF/PDFViewer: Support rotated pages

Matthew Olsson 4 years ago
parent
commit
d5f94aaa7b

+ 11 - 0
Userland/Applications/PDFViewer/PDFViewer.cpp

@@ -87,5 +87,16 @@ RefPtr<Gfx::Bitmap> PDFViewer::render_page(const PDF::Page& page)
     auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { width, height });
     auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { width, height });
 
 
     PDF::Renderer::render(*m_document, page, bitmap);
     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;
     return bitmap;
 }
 }

+ 7 - 1
Userland/Libraries/LibPDF/Document.cpp

@@ -88,7 +88,13 @@ Page Document::get_page(u32 index)
     if (raw_page_object->contains("UserUnit"))
     if (raw_page_object->contains("UserUnit"))
         user_unit = raw_page_object->get_value("UserUnit").to_float();
         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);
     m_pages.set(index, page);
     return page;
     return page;
 }
 }

+ 4 - 2
Userland/Libraries/LibPDF/Document.h

@@ -28,6 +28,7 @@ struct Page {
     Rectangle media_box;
     Rectangle media_box;
     Rectangle crop_box;
     Rectangle crop_box;
     float user_unit;
     float user_unit;
+    int rotate;
 };
 };
 
 
 class Document final : public RefCounted<Document> {
 class Document final : public RefCounted<Document> {
@@ -121,13 +122,14 @@ template<>
 struct Formatter<PDF::Page> : Formatter<StringView> {
 struct Formatter<PDF::Page> : Formatter<StringView> {
     void format(FormatBuilder& builder, const PDF::Page& page)
     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,
         auto str = String::formatted(fmt_string,
             page.resources->to_string(1),
             page.resources->to_string(1),
             page.contents->to_string(1),
             page.contents->to_string(1),
             page.media_box,
             page.media_box,
             page.crop_box,
             page.crop_box,
-            page.user_unit);
+            page.user_unit,
+            page.rotate);
         Formatter<StringView>::format(builder, str);
         Formatter<StringView>::format(builder, str);
     }
     }
 };
 };