PDFViewer.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <LibGUI/AbstractScrollableWidget.h>
  9. #include <LibGfx/Bitmap.h>
  10. #include <LibPDF/Document.h>
  11. static constexpr u16 zoom_levels[] = {
  12. 17,
  13. 21,
  14. 26,
  15. 33,
  16. 41,
  17. 51,
  18. 64,
  19. 80,
  20. 100,
  21. 120,
  22. 144,
  23. 173,
  24. 207,
  25. 249,
  26. 299,
  27. 358,
  28. 430
  29. };
  30. static constexpr size_t number_of_zoom_levels = sizeof(zoom_levels) / sizeof(zoom_levels[0]);
  31. static constexpr size_t initial_zoom_level = 8;
  32. class PDFViewer : public GUI::AbstractScrollableWidget {
  33. C_OBJECT(PDFViewer)
  34. public:
  35. virtual ~PDFViewer() override = default;
  36. ALWAYS_INLINE u32 current_page() const { return m_current_page_index; }
  37. ALWAYS_INLINE void set_current_page(u32 current_page) { m_current_page_index = current_page; }
  38. ALWAYS_INLINE const RefPtr<PDF::Document>& document() const { return m_document; }
  39. void set_document(RefPtr<PDF::Document>);
  40. Function<void(u32 new_page)> on_page_change;
  41. void zoom_in();
  42. void zoom_out();
  43. void reset_zoom();
  44. protected:
  45. PDFViewer();
  46. virtual void paint_event(GUI::PaintEvent&) override;
  47. virtual void mousewheel_event(GUI::MouseEvent&) override;
  48. virtual void mousedown_event(GUI::MouseEvent&) override;
  49. virtual void mouseup_event(GUI::MouseEvent&) override;
  50. virtual void mousemove_event(GUI::MouseEvent&) override;
  51. virtual void timer_event(Core::TimerEvent&) override;
  52. private:
  53. RefPtr<Gfx::Bitmap> get_rendered_page(u32 index);
  54. RefPtr<Gfx::Bitmap> render_page(const PDF::Page&);
  55. RefPtr<PDF::Document> m_document;
  56. u32 m_current_page_index { 0 };
  57. Vector<HashMap<u32, RefPtr<Gfx::Bitmap>>> m_rendered_page_list;
  58. u8 m_zoom_level { initial_zoom_level };
  59. Gfx::IntPoint m_pan_starting_position;
  60. };