Browse Source

LibPDF: Clip images too

Since we can't clip against a general path yet, this clips images
against the bounding box of the current clip path as well.

Clips for images are often rectangular, so this works out well.

(We wastefully still decode and color-convert the entire image.
In a follow-up, we could consider only converting the unclipped
part.)
Nico Weber 1 year ago
parent
commit
2d8a22f4b4
1 changed files with 15 additions and 0 deletions
  1. 15 0
      Userland/Libraries/LibPDF/Renderer.cpp

+ 15 - 0
Userland/Libraries/LibPDF/Renderer.cpp

@@ -1225,6 +1225,21 @@ PDFErrorOr<void> Renderer::show_image(NonnullRefPtr<StreamObject> image)
     auto width = TRY(m_document->resolve_to<int>(image_dict->get_value(CommonNames::Width)));
     auto height = TRY(m_document->resolve_to<int>(image_dict->get_value(CommonNames::Height)));
 
+    class ClipRAII {
+    public:
+        ClipRAII(Renderer& renderer)
+            : m_renderer(renderer)
+        {
+            m_renderer.activate_clip();
+        }
+        ~ClipRAII() { m_renderer.deactivate_clip(); }
+
+    private:
+        Renderer& m_renderer;
+    };
+
+    ClipRAII clip_raii(*this);
+
     if (!m_rendering_preferences.show_images) {
         show_empty_image(width, height);
         return {};