diff --git a/Userland/Libraries/LibPDF/Renderer.cpp b/Userland/Libraries/LibPDF/Renderer.cpp index 08f33a3c1a9..d5ddb961145 100644 --- a/Userland/Libraries/LibPDF/Renderer.cpp +++ b/Userland/Libraries/LibPDF/Renderer.cpp @@ -21,6 +21,30 @@ namespace PDF { +// Use a RAII object to restore the graphics state, to make sure it gets restored even if +// a TRY(handle_operator()) causes us to exit the operators loop early. +// Explicitly resize stack size at the end so that if the recursive document contains +// `q q unsupportedop Q Q`, we undo the stack pushes from the inner `q q` even if +// `unsupportedop` terminates processing the inner instruction stream before `Q Q` +// would normally pop state. +class Renderer::ScopedState { +public: + ScopedState(Renderer& renderer) + : m_renderer(renderer) + , m_starting_stack_depth(m_renderer.m_graphics_state_stack.size()) + { + MUST(m_renderer.handle_save_state({})); + } + ~ScopedState() + { + m_renderer.m_graphics_state_stack.shrink(m_starting_stack_depth); + } + +private: + Renderer& m_renderer; + size_t m_starting_stack_depth; +}; + PDFErrorsOr Renderer::render(Document& document, Page const& page, RefPtr bitmap, RenderingPreferences rendering_preferences) { return Renderer(document, page, bitmap, rendering_preferences).render(); @@ -655,29 +679,6 @@ RENDERER_HANDLER(paint_xobject) return {}; } - // Use a RAII object to restore the graphics state, to make sure it gets restored even if - // a TRY(handle_operator()) causes us to exit the operators loop early. - // Explicitly resize stack size at the end so that if the recursive document contains - // `q q unsupportedop Q Q`, we undo the stack pushes from the inner `q q` even if - // `unsupportedop` terminates processing the inner instruction stream before `Q Q` - // would normally pop state. - class ScopedState { - public: - ScopedState(Renderer& renderer) - : m_renderer(renderer) - , m_starting_stack_depth(m_renderer.m_graphics_state_stack.size()) - { - MUST(m_renderer.handle_save_state({})); - } - ~ScopedState() - { - m_renderer.m_graphics_state_stack.shrink(m_starting_stack_depth); - } - - private: - Renderer& m_renderer; - size_t m_starting_stack_depth; - }; ScopedState scoped_state { *this }; Vector matrix; diff --git a/Userland/Libraries/LibPDF/Renderer.h b/Userland/Libraries/LibPDF/Renderer.h index 53ab30e4258..02a174046b5 100644 --- a/Userland/Libraries/LibPDF/Renderer.h +++ b/Userland/Libraries/LibPDF/Renderer.h @@ -150,6 +150,8 @@ private: PDFErrorOr> get_font(FontCacheKey const&); + class ScopedState; + RefPtr m_document; RefPtr m_bitmap; Page const& m_page;