diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2022-03-05 18:12:58 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-07 10:53:57 +0100 |
commit | b240d23a8749da290d26c2990fa27fc9b68af376 (patch) | |
tree | 9c4a6b4e6aff69c9bd0bbe367ad74a51e0973175 /Userland/Applications | |
parent | d82bd885cec366043605d7dc49ca42215618ef86 (diff) | |
download | serenity-b240d23a8749da290d26c2990fa27fc9b68af376.zip |
LibPDF: Propagate errors in Renderer/PDFViewer
Diffstat (limited to 'Userland/Applications')
-rw-r--r-- | Userland/Applications/PDFViewer/PDFViewer.cpp | 22 | ||||
-rw-r--r-- | Userland/Applications/PDFViewer/PDFViewer.h | 8 |
2 files changed, 19 insertions, 11 deletions
diff --git a/Userland/Applications/PDFViewer/PDFViewer.cpp b/Userland/Applications/PDFViewer/PDFViewer.cpp index b7b6b5308f..b756ea3dd8 100644 --- a/Userland/Applications/PDFViewer/PDFViewer.cpp +++ b/Userland/Applications/PDFViewer/PDFViewer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org> + * Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org> * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause @@ -8,6 +8,7 @@ #include "PDFViewer.h" #include <AK/Array.h> #include <LibGUI/Action.h> +#include <LibGUI/MessageBox.h> #include <LibGUI/Painter.h> #include <LibPDF/Renderer.h> @@ -56,15 +57,15 @@ void PDFViewer::set_document(RefPtr<PDF::Document> document) update(); } -RefPtr<Gfx::Bitmap> PDFViewer::get_rendered_page(u32 index) +PDF::PDFErrorOr<NonnullRefPtr<Gfx::Bitmap>> PDFViewer::get_rendered_page(u32 index) { auto& rendered_page_map = m_rendered_page_list[index]; auto existing_rendered_page = rendered_page_map.get(m_zoom_level); if (existing_rendered_page.has_value() && existing_rendered_page.value().rotation == m_rotations) return existing_rendered_page.value().bitmap; - // FIXME: Propogate errors in the Renderer - auto rendered_page = render_page(MUST(m_document->get_page(index))); + auto page = TRY(m_document->get_page(index)); + auto rendered_page = TRY(render_page(page)); rendered_page_map.set(m_zoom_level, { rendered_page, m_rotations }); return rendered_page; } @@ -81,7 +82,14 @@ void PDFViewer::paint_event(GUI::PaintEvent& event) if (!m_document) return; - auto page = get_rendered_page(m_current_page_index); + auto maybe_page = get_rendered_page(m_current_page_index); + if (maybe_page.is_error()) { + auto error = maybe_page.release_error(); + GUI::MessageBox::show_error(nullptr, String::formatted("Error rendering page:\n{}", error.message())); + return; + } + + auto page = maybe_page.release_value(); set_content_size(page->size()); painter.translate(frame_thickness(), frame_thickness()); @@ -196,7 +204,7 @@ void PDFViewer::rotate(int degrees) update(); } -RefPtr<Gfx::Bitmap> PDFViewer::render_page(const PDF::Page& page) +PDF::PDFErrorOr<NonnullRefPtr<Gfx::Bitmap>> PDFViewer::render_page(const PDF::Page& page) { auto zoom_scale_factor = static_cast<float>(zoom_levels[m_zoom_level]) / 100.0f; @@ -208,7 +216,7 @@ RefPtr<Gfx::Bitmap> PDFViewer::render_page(const PDF::Page& page) auto width = height / page_scale_factor; auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height }).release_value_but_fixme_should_propagate_errors(); - PDF::Renderer::render(*m_document, page, bitmap); + TRY(PDF::Renderer::render(*m_document, page, bitmap)); 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 180a4b952e..12f4a17cb2 100644 --- a/Userland/Applications/PDFViewer/PDFViewer.h +++ b/Userland/Applications/PDFViewer/PDFViewer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org> + * Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org> * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause @@ -45,12 +45,12 @@ protected: private: struct RenderedPage { - RefPtr<Gfx::Bitmap> bitmap; + NonnullRefPtr<Gfx::Bitmap> bitmap; int rotation; }; - RefPtr<Gfx::Bitmap> get_rendered_page(u32 index); - RefPtr<Gfx::Bitmap> render_page(const PDF::Page&); + PDF::PDFErrorOr<NonnullRefPtr<Gfx::Bitmap>> get_rendered_page(u32 index); + PDF::PDFErrorOr<NonnullRefPtr<Gfx::Bitmap>> render_page(const PDF::Page&); RefPtr<PDF::Document> m_document; u32 m_current_page_index { 0 }; |