1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:47:44 +00:00

PDFViewer: Allow zooming in and out + scrolling

When holding ctrl and scrolling, the page will be zoomed in an out.
When zoomed in on a page, scrolling with move vertically up and down
the page (or horizontally if shift is being held).

In order to speed up zooming, zoomed bitmaps are cached per-page at
each distinct zoom level. This cache is cleared every 30 seconds to
prevent OOM problems.
This commit is contained in:
Matthew Olsson 2021-05-10 14:00:06 -07:00 committed by Andreas Kling
parent d5f94aaa7b
commit c3c2121b57
2 changed files with 104 additions and 22 deletions

View file

@ -11,11 +11,35 @@
#include <LibGfx/Bitmap.h>
#include <LibPDF/Document.h>
static constexpr u16 zoom_levels[] = {
17,
21,
26,
33,
41,
51,
64,
80,
100,
120,
144,
173,
207,
249,
299,
358,
430
};
static constexpr size_t number_of_zoom_levels = sizeof(zoom_levels) / sizeof(zoom_levels[0]);
static constexpr size_t initial_zoom_level = 8;
class PDFViewer : public GUI::AbstractScrollableWidget {
C_OBJECT(PDFViewer)
public:
virtual ~PDFViewer() override;
virtual ~PDFViewer() override = default;
void set_document(RefPtr<PDF::Document>);
@ -24,12 +48,18 @@ protected:
virtual void paint_event(GUI::PaintEvent&) override;
virtual void mousewheel_event(GUI::MouseEvent&) override;
virtual void timer_event(Core::TimerEvent&) override;
private:
RefPtr<Gfx::Bitmap> get_rendered_page(u32 index);
RefPtr<Gfx::Bitmap> render_page(const PDF::Page&);
void zoom_in();
void zoom_out();
RefPtr<PDF::Document> m_document;
u32 m_current_page_index { 0 };
HashMap<u32, RefPtr<Gfx::Bitmap>> m_rendered_pages;
Vector<HashMap<u32, RefPtr<Gfx::Bitmap>>> m_rendered_page_list;
u8 m_zoom_level { initial_zoom_level };
};