mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-06 02:55:49 +03:00
Applications: Add a very simple PDFViewer
This commit is contained in:
parent
309105678b
commit
f7ea1eb610
Notes:
sideshowbarker
2024-07-18 17:51:04 +09:00
Author: https://github.com/mattco98 Commit: https://github.com/SerenityOS/serenity/commit/f7ea1eb6101 Pull-request: https://github.com/SerenityOS/serenity/pull/7018 Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/bcoles Reviewed-by: https://github.com/tomuta
@ -16,6 +16,7 @@ txt=/bin/TextEditor
|
||||
font=/bin/FontEditor
|
||||
sheets=/bin/Spreadsheet
|
||||
gml=/bin/Playground
|
||||
pdf=/bin/PDFViewer
|
||||
*=/bin/TextEditor
|
||||
|
||||
[Protocol]
|
||||
|
4
Base/res/apps/PDFViewer.af
Normal file
4
Base/res/apps/PDFViewer.af
Normal file
@ -0,0 +1,4 @@
|
||||
[App]
|
||||
Name=PDFViewer
|
||||
Executable=/bin/PDFViewer
|
||||
Category=Graphics
|
BIN
Base/res/icons/16x16/app-pdf-viewer.png
Normal file
BIN
Base/res/icons/16x16/app-pdf-viewer.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 218 B |
BIN
Base/res/icons/32x32/app-pdf-viewer.png
Normal file
BIN
Base/res/icons/32x32/app-pdf-viewer.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 257 B |
@ -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)
|
||||
|
8
Userland/Applications/PDFViewer/CMakeLists.txt
Normal file
8
Userland/Applications/PDFViewer/CMakeLists.txt
Normal file
@ -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)
|
91
Userland/Applications/PDFViewer/PDFViewer.cpp
Normal file
91
Userland/Applications/PDFViewer/PDFViewer.cpp
Normal file
@ -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;
|
||||
}
|
35
Userland/Applications/PDFViewer/PDFViewer.h
Normal file
35
Userland/Applications/PDFViewer/PDFViewer.h
Normal file
@ -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;
|
||||
};
|
48
Userland/Applications/PDFViewer/PDFViewerWidget.cpp
Normal file
48
Userland/Applications/PDFViewer/PDFViewerWidget.cpp
Normal file
@ -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)));
|
||||
}
|
27
Userland/Applications/PDFViewer/PDFViewerWidget.h
Normal file
27
Userland/Applications/PDFViewer/PDFViewerWidget.h
Normal file
@ -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;
|
||||
};
|
34
Userland/Applications/PDFViewer/main.cpp
Normal file
34
Userland/Applications/PDFViewer/main.cpp
Normal file
@ -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();
|
||||
}
|
@ -115,23 +115,6 @@ Value Document::resolve(const Value& value)
|
||||
return obj;
|
||||
}
|
||||
|
||||
template<IsValueType T>
|
||||
UnwrappedValueType<T> Document::resolve_to(const Value& value)
|
||||
{
|
||||
auto resolved = resolve(value);
|
||||
|
||||
if constexpr (IsSame<T, bool>)
|
||||
return resolved.as_bool();
|
||||
if constexpr (IsSame<T, int>)
|
||||
return resolved.as_int();
|
||||
if constexpr (IsSame<T, float>)
|
||||
return resolved.as_float();
|
||||
if constexpr (IsObject<T>)
|
||||
return object_cast<T>(resolved.as_object());
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
void Document::build_page_tree()
|
||||
{
|
||||
auto page_tree = m_catalog->get_dict(this, "Pages");
|
||||
|
@ -65,7 +65,21 @@ public:
|
||||
// Like resolve, but unwraps the Value into the given type. Accepts
|
||||
// any object type, and the three primitive Value types.
|
||||
template<IsValueType T>
|
||||
UnwrappedValueType<T> resolve_to(const Value& value);
|
||||
UnwrappedValueType<T> resolve_to(const Value& value)
|
||||
{
|
||||
auto resolved = resolve(value);
|
||||
|
||||
if constexpr (IsSame<T, bool>)
|
||||
return resolved.as_bool();
|
||||
if constexpr (IsSame<T, int>)
|
||||
return resolved.as_int();
|
||||
if constexpr (IsSame<T, float>)
|
||||
return resolved.as_float();
|
||||
if constexpr (IsObject<T>)
|
||||
return object_cast<T>(resolved.as_object());
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
private:
|
||||
// FIXME: Currently, to improve performance, we don't load any pages at Document
|
||||
|
Loading…
Reference in New Issue
Block a user