2021-05-10 18:50:15 +00:00
|
|
|
/*
|
2022-03-06 01:12:58 +00:00
|
|
|
* Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
|
2022-02-10 00:45:15 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2021-05-10 18:50:15 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <LibGUI/AbstractScrollableWidget.h>
|
|
|
|
#include <LibGfx/Bitmap.h>
|
|
|
|
#include <LibPDF/Document.h>
|
|
|
|
|
2021-05-10 21:00:06 +00:00
|
|
|
static constexpr size_t initial_zoom_level = 8;
|
|
|
|
|
2022-03-30 04:27:17 +00:00
|
|
|
struct PageDimensionCache {
|
|
|
|
// Fixed for a given document
|
|
|
|
struct PageInfo {
|
|
|
|
Gfx::FloatSize size;
|
|
|
|
int rotation;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Based on PageInfo, also depends on some dynamic factors like
|
|
|
|
// zoom level and app size
|
|
|
|
struct RenderInfo {
|
|
|
|
Gfx::FloatSize size;
|
|
|
|
float total_height_before_this_page;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<PageInfo> page_info;
|
|
|
|
Vector<RenderInfo> render_info;
|
|
|
|
float max_width;
|
|
|
|
float total_height;
|
|
|
|
};
|
|
|
|
|
2021-05-10 18:50:15 +00:00
|
|
|
class PDFViewer : public GUI::AbstractScrollableWidget {
|
|
|
|
C_OBJECT(PDFViewer)
|
|
|
|
|
|
|
|
public:
|
2022-03-27 16:11:02 +00:00
|
|
|
enum class PageViewMode {
|
|
|
|
Single,
|
|
|
|
Multiple,
|
|
|
|
};
|
|
|
|
|
2021-05-10 21:00:06 +00:00
|
|
|
virtual ~PDFViewer() override = default;
|
2021-05-10 18:50:15 +00:00
|
|
|
|
2021-05-24 05:34:44 +00:00
|
|
|
ALWAYS_INLINE u32 current_page() const { return m_current_page_index; }
|
2022-04-01 01:01:07 +00:00
|
|
|
void set_current_page(u32 current_page);
|
2021-05-24 05:34:44 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE RefPtr<PDF::Document> const& document() const { return m_document; }
|
2022-03-30 04:27:17 +00:00
|
|
|
PDF::PDFErrorOr<void> set_document(RefPtr<PDF::Document>);
|
2021-05-10 18:50:15 +00:00
|
|
|
|
2021-06-20 19:07:44 +00:00
|
|
|
Function<void(u32 new_page)> on_page_change;
|
|
|
|
|
2022-01-02 20:58:51 +00:00
|
|
|
void zoom_in();
|
|
|
|
void zoom_out();
|
|
|
|
void reset_zoom();
|
2022-01-02 23:24:11 +00:00
|
|
|
void rotate(int degrees);
|
2022-01-02 20:58:51 +00:00
|
|
|
|
2022-03-29 04:19:45 +00:00
|
|
|
PageViewMode page_view_mode() const { return m_page_view_mode; }
|
|
|
|
void set_page_view_mode(PageViewMode);
|
|
|
|
|
2021-05-10 18:50:15 +00:00
|
|
|
protected:
|
|
|
|
PDFViewer();
|
|
|
|
|
|
|
|
virtual void paint_event(GUI::PaintEvent&) override;
|
2022-03-27 16:11:02 +00:00
|
|
|
virtual void resize_event(GUI::ResizeEvent&) override;
|
2021-05-10 18:50:15 +00:00
|
|
|
virtual void mousewheel_event(GUI::MouseEvent&) override;
|
2022-01-02 22:30:13 +00:00
|
|
|
virtual void mousedown_event(GUI::MouseEvent&) override;
|
|
|
|
virtual void mouseup_event(GUI::MouseEvent&) override;
|
|
|
|
virtual void mousemove_event(GUI::MouseEvent&) override;
|
2021-05-10 21:00:06 +00:00
|
|
|
virtual void timer_event(Core::TimerEvent&) override;
|
2021-05-10 18:50:15 +00:00
|
|
|
|
|
|
|
private:
|
2022-01-02 23:24:11 +00:00
|
|
|
struct RenderedPage {
|
2022-03-06 01:12:58 +00:00
|
|
|
NonnullRefPtr<Gfx::Bitmap> bitmap;
|
2022-01-02 23:24:11 +00:00
|
|
|
int rotation;
|
|
|
|
};
|
|
|
|
|
2022-03-06 01:12:58 +00:00
|
|
|
PDF::PDFErrorOr<NonnullRefPtr<Gfx::Bitmap>> get_rendered_page(u32 index);
|
2022-03-30 04:27:17 +00:00
|
|
|
PDF::PDFErrorOr<NonnullRefPtr<Gfx::Bitmap>> render_page(u32 page_index);
|
|
|
|
PDF::PDFErrorOr<void> cache_page_dimensions(bool recalculate_fixed_info = false);
|
|
|
|
void change_page(u32 new_page);
|
2021-05-10 18:50:15 +00:00
|
|
|
|
|
|
|
RefPtr<PDF::Document> m_document;
|
|
|
|
u32 m_current_page_index { 0 };
|
2022-01-02 23:24:11 +00:00
|
|
|
Vector<HashMap<u32, RenderedPage>> m_rendered_page_list;
|
2021-05-10 21:00:06 +00:00
|
|
|
|
|
|
|
u8 m_zoom_level { initial_zoom_level };
|
2022-03-30 04:27:17 +00:00
|
|
|
PageDimensionCache m_page_dimension_cache;
|
2022-03-29 04:19:45 +00:00
|
|
|
PageViewMode m_page_view_mode;
|
2022-01-02 22:30:13 +00:00
|
|
|
|
|
|
|
Gfx::IntPoint m_pan_starting_position;
|
2022-01-02 23:24:11 +00:00
|
|
|
int m_rotations { 0 };
|
2021-05-10 18:50:15 +00:00
|
|
|
};
|