From bc7da24fe688d35b88487aa39bace31dd023122c Mon Sep 17 00:00:00 2001 From: Rodrigo Tobar Date: Wed, 23 Nov 2022 21:16:52 +0800 Subject: [PATCH] PDFViewer: Let users change clipping paths visibility Now that the Renderer accepts preferences, PDFViewer can offer ways for changing these preferences. The first step in this direction is to add a checkbox that allows toggling whether clipping paths are visible or not. A Config item has also been added to remember this setting. --- Userland/Applications/PDFViewer/PDFViewer.cpp | 15 ++++++++++++--- Userland/Applications/PDFViewer/PDFViewer.h | 4 ++++ .../Applications/PDFViewer/PDFViewerWidget.cpp | 7 +++++++ Userland/Applications/PDFViewer/PDFViewerWidget.h | 4 +++- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Userland/Applications/PDFViewer/PDFViewer.cpp b/Userland/Applications/PDFViewer/PDFViewer.cpp index 3d19c550b27..3fd5d036402 100644 --- a/Userland/Applications/PDFViewer/PDFViewer.cpp +++ b/Userland/Applications/PDFViewer/PDFViewer.cpp @@ -45,6 +45,7 @@ PDFViewer::PDFViewer() start_timer(30'000); m_page_view_mode = static_cast(Config::read_i32("PDFViewer"sv, "Display"sv, "PageMode"sv, 0)); + m_rendering_preferences.show_clipping_paths = Config::read_bool("PDFViewer"sv, "Rendering"sv, "ShowClippingPaths"sv, false); } PDF::PDFErrorOr PDFViewer::set_document(RefPtr document) @@ -66,13 +67,14 @@ PDF::PDFErrorOr PDFViewer::set_document(RefPtr document) PDF::PDFErrorOr> PDFViewer::get_rendered_page(u32 index) { + auto key = m_zoom_level * (static_cast(m_rendering_preferences.show_clipping_paths) + 1); auto& rendered_page_map = m_rendered_page_list[index]; - auto existing_rendered_page = rendered_page_map.get(m_zoom_level); + auto existing_rendered_page = rendered_page_map.get(key); if (existing_rendered_page.has_value() && existing_rendered_page.value().rotation == m_rotations) return existing_rendered_page.value().bitmap; auto rendered_page = TRY(render_page(index)); - rendered_page_map.set(m_zoom_level, { rendered_page, m_rotations }); + rendered_page_map.set(key, { rendered_page, m_rotations }); return rendered_page; } @@ -163,6 +165,13 @@ void PDFViewer::set_current_page(u32 current_page) update(); } +void PDFViewer::set_show_clipping_paths(bool show_clipping_paths) +{ + m_rendering_preferences.show_clipping_paths = show_clipping_paths; + Config::write_bool("PDFViewer"sv, "Rendering"sv, "ShowClippingPaths"sv, show_clipping_paths); + update(); +} + void PDFViewer::resize_event(GUI::ResizeEvent&) { for (auto& map : m_rendered_page_list) @@ -301,7 +310,7 @@ PDF::PDFErrorOr> PDFViewer::render_page(u32 page_inde auto& page_size = m_page_dimension_cache.render_info[page_index].size; auto bitmap = TRY(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, page_size.to_type())); - TRY(PDF::Renderer::render(*m_document, page, bitmap)); + TRY(PDF::Renderer::render(*m_document, page, bitmap, m_rendering_preferences)); if (page.rotate + m_rotations != 0) { int rotation_count = ((page.rotate + m_rotations) / 90) % 4; diff --git a/Userland/Applications/PDFViewer/PDFViewer.h b/Userland/Applications/PDFViewer/PDFViewer.h index 040da7c6d30..894483fd276 100644 --- a/Userland/Applications/PDFViewer/PDFViewer.h +++ b/Userland/Applications/PDFViewer/PDFViewer.h @@ -11,6 +11,7 @@ #include #include #include +#include static constexpr size_t initial_zoom_level = 8; @@ -60,6 +61,8 @@ public: PageViewMode page_view_mode() const { return m_page_view_mode; } void set_page_view_mode(PageViewMode); + bool show_clipping_paths() const { return m_rendering_preferences.show_clipping_paths; } + void set_show_clipping_paths(bool); protected: PDFViewer(); @@ -90,6 +93,7 @@ private: u8 m_zoom_level { initial_zoom_level }; PageDimensionCache m_page_dimension_cache; PageViewMode m_page_view_mode; + PDF::RenderingPreferences m_rendering_preferences; Gfx::IntPoint m_pan_starting_position; int m_rotations { 0 }; diff --git a/Userland/Applications/PDFViewer/PDFViewerWidget.cpp b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp index bfcce0df064..84edea939bc 100644 --- a/Userland/Applications/PDFViewer/PDFViewerWidget.cpp +++ b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp @@ -167,6 +167,12 @@ void PDFViewerWidget::initialize_toolbar(GUI::Toolbar& toolbar) toolbar.add_action(*m_reset_zoom_action); toolbar.add_action(*m_rotate_counterclockwise_action); toolbar.add_action(*m_rotate_clockwise_action); + toolbar.add_separator(); + + m_show_clipping_paths = toolbar.add(); + m_show_clipping_paths->set_text("Show clipping paths"); + m_show_clipping_paths->set_checked(m_viewer->show_clipping_paths(), GUI::AllowCallback::No); + m_show_clipping_paths->on_checked = [&](auto checked) { m_viewer->set_show_clipping_paths(checked); }; } void PDFViewerWidget::open_file(Core::File& file) @@ -214,6 +220,7 @@ void PDFViewerWidget::open_file(Core::File& file) m_reset_zoom_action->set_enabled(true); m_rotate_counterclockwise_action->set_enabled(true); m_rotate_clockwise_action->set_enabled(true); + m_show_clipping_paths->set_checked(m_viewer->show_clipping_paths(), GUI::AllowCallback::No); if (document->outline()) { auto outline = document->outline(); diff --git a/Userland/Applications/PDFViewer/PDFViewerWidget.h b/Userland/Applications/PDFViewer/PDFViewerWidget.h index f3a520bee39..adf42a71446 100644 --- a/Userland/Applications/PDFViewer/PDFViewerWidget.h +++ b/Userland/Applications/PDFViewer/PDFViewerWidget.h @@ -6,12 +6,13 @@ #pragma once +#include "AK/RefPtr.h" #include "NumericInput.h" #include "PDFViewer.h" #include "SidebarWidget.h" #include #include -#include +#include #include class PDFViewer; @@ -45,6 +46,7 @@ private: GUI::ActionGroup m_page_view_action_group; RefPtr m_page_view_mode_single; RefPtr m_page_view_mode_multiple; + RefPtr m_show_clipping_paths; bool m_sidebar_open { false }; ByteBuffer m_buffer;