summaryrefslogtreecommitdiff
path: root/Userland/Applications
diff options
context:
space:
mode:
authorMatthew Olsson <matthewcolsson@gmail.com>2021-05-10 11:50:15 -0700
committerAndreas Kling <kling@serenityos.org>2021-05-18 16:35:23 +0200
commitf7ea1eb6101617e127605173deb381bee84e72e5 (patch)
tree5ce04823ae4676f53ccb198fdc09b40f592995b9 /Userland/Applications
parent309105678b64839c671b6fa2450b500276692664 (diff)
downloadserenity-f7ea1eb6101617e127605173deb381bee84e72e5.zip
Applications: Add a very simple PDFViewer
Diffstat (limited to 'Userland/Applications')
-rw-r--r--Userland/Applications/CMakeLists.txt1
-rw-r--r--Userland/Applications/PDFViewer/CMakeLists.txt8
-rw-r--r--Userland/Applications/PDFViewer/PDFViewer.cpp91
-rw-r--r--Userland/Applications/PDFViewer/PDFViewer.h35
-rw-r--r--Userland/Applications/PDFViewer/PDFViewerWidget.cpp48
-rw-r--r--Userland/Applications/PDFViewer/PDFViewerWidget.h27
-rw-r--r--Userland/Applications/PDFViewer/main.cpp34
7 files changed, 244 insertions, 0 deletions
diff --git a/Userland/Applications/CMakeLists.txt b/Userland/Applications/CMakeLists.txt
index 8e4af34454..f2dd51efd3 100644
--- a/Userland/Applications/CMakeLists.txt
+++ b/Userland/Applications/CMakeLists.txt
@@ -16,6 +16,7 @@ add_subdirectory(KeyboardMapper)
add_subdirectory(KeyboardSettings)
add_subdirectory(Magnifier)
add_subdirectory(MouseSettings)
+add_subdirectory(PDFViewer)
add_subdirectory(Piano)
add_subdirectory(PixelPaint)
add_subdirectory(Run)
diff --git a/Userland/Applications/PDFViewer/CMakeLists.txt b/Userland/Applications/PDFViewer/CMakeLists.txt
new file mode 100644
index 0000000000..c6c437552b
--- /dev/null
+++ b/Userland/Applications/PDFViewer/CMakeLists.txt
@@ -0,0 +1,8 @@
+set(SOURCES
+ PDFViewer.cpp
+ PDFViewerWidget.cpp
+ main.cpp
+ )
+
+serenity_app(PDFViewer ICON app-pdf-viewer)
+target_link_libraries(PDFViewer LibGUI LibPDF)
diff --git a/Userland/Applications/PDFViewer/PDFViewer.cpp b/Userland/Applications/PDFViewer/PDFViewer.cpp
new file mode 100644
index 0000000000..1a309ab6c5
--- /dev/null
+++ b/Userland/Applications/PDFViewer/PDFViewer.cpp
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "PDFViewer.h"
+#include <LibGUI/Action.h>
+#include <LibGUI/Painter.h>
+#include <LibPDF/Renderer.h>
+
+PDFViewer::PDFViewer()
+{
+ set_should_hide_unnecessary_scrollbars(true);
+ set_focus_policy(GUI::FocusPolicy::StrongFocus);
+ set_scrollbars_enabled(true);
+}
+
+PDFViewer::~PDFViewer()
+{
+}
+
+void PDFViewer::set_document(RefPtr<PDF::Document> document)
+{
+ m_document = document;
+ m_current_page_index = document->get_first_page_index();
+ update();
+}
+
+RefPtr<Gfx::Bitmap> PDFViewer::get_rendered_page(u32 index)
+{
+ auto existing_rendered_page = m_rendered_pages.get(index);
+ if (existing_rendered_page.has_value())
+ return existing_rendered_page.value();
+
+ auto rendered_page = render_page(m_document->get_page(index));
+ m_rendered_pages.set(index, rendered_page);
+ return rendered_page;
+}
+
+void PDFViewer::paint_event(GUI::PaintEvent& event)
+{
+ GUI::Frame::paint_event(event);
+
+ GUI::Painter painter(*this);
+ painter.add_clip_rect(widget_inner_rect());
+ painter.add_clip_rect(event.rect());
+ painter.fill_rect(event.rect(), Color(0x80, 0x80, 0x80));
+
+ if (!m_document)
+ return;
+
+ painter.translate(frame_thickness(), frame_thickness());
+ painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
+
+ auto page = get_rendered_page(m_current_page_index);
+
+ auto total_width = width() - frame_thickness() * 2;
+ auto total_height = height() - frame_thickness() * 2;
+ auto bitmap_width = page->width();
+ auto bitmap_height = page->height();
+
+ Gfx::IntPoint p { (total_width - bitmap_width) / 2, (total_height - bitmap_height) / 2 };
+
+ painter.blit(p, *page, page->rect());
+}
+
+void PDFViewer::mousewheel_event(GUI::MouseEvent& event)
+{
+ if (event.wheel_delta() > 0) {
+ if (m_current_page_index < m_document->get_page_count() - 1)
+ m_current_page_index++;
+ } else if (m_current_page_index > 0) {
+ m_current_page_index--;
+ }
+ update();
+}
+
+RefPtr<Gfx::Bitmap> PDFViewer::render_page(const PDF::Page& page)
+{
+ float page_width = page.media_box.upper_right_x - page.media_box.lower_left_x;
+ float page_height = page.media_box.upper_right_y - page.media_box.lower_left_y;
+ float page_scale_factor = page_height / page_width;
+
+ float width = 300.0f;
+ float height = width * page_scale_factor;
+ auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { width, height });
+
+ PDF::Renderer::render(*m_document, page, bitmap);
+ return bitmap;
+}
diff --git a/Userland/Applications/PDFViewer/PDFViewer.h b/Userland/Applications/PDFViewer/PDFViewer.h
new file mode 100644
index 0000000000..528bfa12f2
--- /dev/null
+++ b/Userland/Applications/PDFViewer/PDFViewer.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/HashMap.h>
+#include <LibGUI/AbstractScrollableWidget.h>
+#include <LibGfx/Bitmap.h>
+#include <LibPDF/Document.h>
+
+class PDFViewer : public GUI::AbstractScrollableWidget {
+ C_OBJECT(PDFViewer)
+
+public:
+ virtual ~PDFViewer() override;
+
+ void set_document(RefPtr<PDF::Document>);
+
+protected:
+ PDFViewer();
+
+ virtual void paint_event(GUI::PaintEvent&) override;
+ virtual void mousewheel_event(GUI::MouseEvent&) override;
+
+private:
+ RefPtr<Gfx::Bitmap> get_rendered_page(u32 index);
+ RefPtr<Gfx::Bitmap> render_page(const PDF::Page&);
+
+ RefPtr<PDF::Document> m_document;
+ u32 m_current_page_index { 0 };
+ HashMap<u32, RefPtr<Gfx::Bitmap>> m_rendered_pages;
+};
diff --git a/Userland/Applications/PDFViewer/PDFViewerWidget.cpp b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp
new file mode 100644
index 0000000000..6eb0e52b42
--- /dev/null
+++ b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "PDFViewerWidget.h"
+#include <LibCore/File.h>
+#include <LibGUI/Action.h>
+#include <LibGUI/Application.h>
+#include <LibGUI/BoxLayout.h>
+#include <LibGUI/FilePicker.h>
+#include <LibGUI/Menu.h>
+#include <LibGUI/Menubar.h>
+
+PDFViewerWidget::PDFViewerWidget()
+{
+ set_fill_with_background_color(true);
+ set_layout<GUI::VerticalBoxLayout>();
+
+ m_viewer = add<PDFViewer>();
+}
+
+PDFViewerWidget::~PDFViewerWidget()
+{
+}
+
+void PDFViewerWidget::initialize_menubar(GUI::Menubar& menubar)
+{
+ auto& file_menu = menubar.add_menu("&File");
+ file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
+ Optional<String> open_path = GUI::FilePicker::get_open_filepath(window());
+ if (open_path.has_value())
+ open_file(open_path.value());
+ }));
+ file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
+ GUI::Application::the()->quit();
+ }));
+}
+
+void PDFViewerWidget::open_file(const String& path)
+{
+ window()->set_title(String::formatted("{} - PDFViewer", path));
+ auto file_result = Core::File::open(path, Core::OpenMode::ReadOnly);
+ VERIFY(!file_result.is_error());
+ m_buffer = file_result.value()->read_all();
+ m_viewer->set_document(adopt_ref(*new PDF::Document(m_buffer)));
+}
diff --git a/Userland/Applications/PDFViewer/PDFViewerWidget.h b/Userland/Applications/PDFViewer/PDFViewerWidget.h
new file mode 100644
index 0000000000..8f897d9720
--- /dev/null
+++ b/Userland/Applications/PDFViewer/PDFViewerWidget.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include "PDFViewer.h"
+#include <LibGUI/Widget.h>
+
+class PDFViewer;
+
+class PDFViewerWidget final : public GUI::Widget {
+ C_OBJECT(PDFViewerWidget)
+public:
+ ~PDFViewerWidget() override;
+ void open_file(const String& path);
+ void initialize_menubar(GUI::Menubar&);
+
+private:
+ PDFViewerWidget();
+
+ RefPtr<PDFViewer> m_viewer;
+ ByteBuffer m_buffer;
+ RefPtr<GUI::Action> m_open_action;
+};
diff --git a/Userland/Applications/PDFViewer/main.cpp b/Userland/Applications/PDFViewer/main.cpp
new file mode 100644
index 0000000000..0f5e0ed38d
--- /dev/null
+++ b/Userland/Applications/PDFViewer/main.cpp
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "PDFViewerWidget.h"
+#include <LibGUI/Application.h>
+#include <LibGUI/Icon.h>
+#include <LibGUI/Menubar.h>
+#include <LibGUI/Window.h>
+
+int main(int argc, char** argv)
+{
+ auto app = GUI::Application::construct(argc, argv);
+ auto app_icon = GUI::Icon::default_icon("app-pdf-viewer");
+
+ auto window = GUI::Window::construct();
+ window->set_title("PDFViewer");
+ window->resize(640, 400);
+
+ auto& pdf_viewer_widget = window->set_main_widget<PDFViewerWidget>();
+
+ auto menubar = GUI::Menubar::construct();
+ pdf_viewer_widget.initialize_menubar(menubar);
+ window->set_menubar(menubar);
+ window->show();
+ window->set_icon(app_icon.bitmap_for_size(16));
+
+ if (argc >= 2)
+ pdf_viewer_widget.open_file(argv[1]);
+
+ return app->exec();
+}