From 8191f2b47a9c446dcfed1d291a28554e8674d071 Mon Sep 17 00:00:00 2001 From: Kyle Pereira Date: Thu, 7 Dec 2023 12:53:13 +0000 Subject: [PATCH] LibPDF: Add parameter for background color of render --- Meta/Lagom/Contrib/MacPDF/MacPDFView.mm | 2 +- Userland/Applications/PDFViewer/PDFViewer.cpp | 2 +- Userland/Libraries/LibPDF/Renderer.cpp | 8 ++++---- Userland/Libraries/LibPDF/Renderer.h | 4 ++-- Userland/Utilities/pdf.cpp | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm b/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm index 359d3c6d6ab..cc5d4c7d544 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm @@ -29,7 +29,7 @@ static PDF::PDFErrorOr> render(PDF::Document& documen auto bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, page_size)); - auto errors = PDF::Renderer::render(document, page, bitmap, PDF::RenderingPreferences {}); + auto errors = PDF::Renderer::render(document, page, bitmap, Color::White, PDF::RenderingPreferences {}); if (errors.is_error()) { for (auto const& error : errors.error().errors()) NSLog(@"warning: %@", @(error.message().characters())); diff --git a/Userland/Applications/PDFViewer/PDFViewer.cpp b/Userland/Applications/PDFViewer/PDFViewer.cpp index ae3cc54b3e8..ca1fd031aff 100644 --- a/Userland/Applications/PDFViewer/PDFViewer.cpp +++ b/Userland/Applications/PDFViewer/PDFViewer.cpp @@ -319,7 +319,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::create(Gfx::BitmapFormat::BGRA8888, page_size.to_type())); - auto maybe_errors = PDF::Renderer::render(*m_document, page, bitmap, m_rendering_preferences); + auto maybe_errors = PDF::Renderer::render(*m_document, page, bitmap, Color::White, m_rendering_preferences); if (maybe_errors.is_error()) { auto errors = maybe_errors.release_error(); on_render_errors(page_index, errors); diff --git a/Userland/Libraries/LibPDF/Renderer.cpp b/Userland/Libraries/LibPDF/Renderer.cpp index 87396f70725..0ebc9ebdd20 100644 --- a/Userland/Libraries/LibPDF/Renderer.cpp +++ b/Userland/Libraries/LibPDF/Renderer.cpp @@ -45,9 +45,9 @@ private: size_t m_starting_stack_depth; }; -PDFErrorsOr Renderer::render(Document& document, Page const& page, RefPtr bitmap, RenderingPreferences rendering_preferences) +PDFErrorsOr Renderer::render(Document& document, Page const& page, RefPtr bitmap, Color background_color, RenderingPreferences rendering_preferences) { - return Renderer(document, page, bitmap, rendering_preferences).render(); + return Renderer(document, page, bitmap, background_color, rendering_preferences).render(); } static void rect_path(Gfx::Path& path, float x, float y, float width, float height) @@ -73,7 +73,7 @@ static Gfx::Path rect_path(Gfx::Rect const& rect) return path; } -Renderer::Renderer(RefPtr document, Page const& page, RefPtr bitmap, RenderingPreferences rendering_preferences) +Renderer::Renderer(RefPtr document, Page const& page, RefPtr bitmap, Color background_color, RenderingPreferences rendering_preferences) : m_document(document) , m_bitmap(bitmap) , m_page(page) @@ -104,7 +104,7 @@ Renderer::Renderer(RefPtr document, Page const& page, RefPtrfill(Gfx::Color::NamedColor::White); + m_bitmap->fill(background_color); } PDFErrorsOr Renderer::render() diff --git a/Userland/Libraries/LibPDF/Renderer.h b/Userland/Libraries/LibPDF/Renderer.h index 757a5c8ca76..2f302fe5d52 100644 --- a/Userland/Libraries/LibPDF/Renderer.h +++ b/Userland/Libraries/LibPDF/Renderer.h @@ -98,7 +98,7 @@ struct RenderingPreferences { class Renderer { public: - static PDFErrorsOr render(Document&, Page const&, RefPtr, RenderingPreferences preferences); + static PDFErrorsOr render(Document&, Page const&, RefPtr, Color background_color, RenderingPreferences preferences); struct FontCacheKey { NonnullRefPtr font_dictionary; @@ -115,7 +115,7 @@ public: PDFErrorOr render_type3_glyph(Gfx::FloatPoint, StreamObject const&, Gfx::AffineTransform const&, Optional>); private: - Renderer(RefPtr, Page const&, RefPtr, RenderingPreferences); + Renderer(RefPtr, Page const&, RefPtr, Color background_color, RenderingPreferences); PDFErrorsOr render(); diff --git a/Userland/Utilities/pdf.cpp b/Userland/Utilities/pdf.cpp index c5b74e6829f..13345665498 100644 --- a/Userland/Utilities/pdf.cpp +++ b/Userland/Utilities/pdf.cpp @@ -55,7 +55,7 @@ static PDF::PDFErrorOr> render_page(PDF::Document& do auto bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, page_size)); - auto errors = PDF::Renderer::render(document, page, bitmap, PDF::RenderingPreferences {}); + auto errors = PDF::Renderer::render(document, page, bitmap, Color::White, PDF::RenderingPreferences {}); if (errors.is_error()) { for (auto const& error : errors.error().errors()) warnln("warning: {}", error.message()); @@ -128,7 +128,7 @@ static PDF::PDFErrorOr print_debugging_stats(PDF::Document& document, bool auto page = TRY(document.get_page(page_number - 1)); auto page_size = Gfx::IntSize { 200, round_to(200 * page.media_box.height() / page.media_box.width()) }; auto bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, page_size)); - auto errors = PDF::Renderer::render(document, page, bitmap, PDF::RenderingPreferences {}); + auto errors = PDF::Renderer::render(document, page, bitmap, Color::White, PDF::RenderingPreferences {}); if (errors.is_error()) { for (auto const& error : errors.error().errors()) diags_to_pages.ensure(error.message()).append(page_number);