Search code examples
gtkcairogtkmmpoppler

How to draw a poppler document in a gtkmm DrawingArea


I am trying to draw a PDF with poppler gtk and a gtkmm DrawingArea, but it is not working, I am not sure what is wrong. The drawing area does not draw the document. I know the drawing area works otherwise with Cairo::Context::stroke(). Do I need to use a more gtk approach and wrap widget's to gtkmm? Code:

// PdfViewer.h    
#include <gtkmm.h>
#include <poppler.h>
#include "DrawingAreaFoo.h"

class PdfViewer: public Gtk::Box
{
public:
   PdfViewer();
   virtual ~PdfViewer();
private:
   PopplerDocument *m_document;
   PopplerPage *m_page;
   DrawingAreaFoo m_drawingArea;

 };


// PdfViewer.cpp
#include "PdfViewer.h"

 PdfViewer::PdfViewer():
 {
    const char * uri = "file:////path/to/file/pdf.pdf";
    m_document = poppler_document_new_from_file (uri, NULL, NULL);
    auto total_pages = poppler_document_get_n_pages (m_document);

    pack_start(m_drawingArea, TRUE, TRUE);

    int w, h;
    double width, height;

    m_page = poppler_document_get_page (m_document, 0);
    poppler_page_get_size (m_page, &width, &height);
    w = (int) ceil(width);
    h = (int) ceil(height);
    cairo_surface_t * surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);

    m_drawingArea.drawSurface(surface);
}

PdfViewer::~PdfViewer(){}

// DrawinAreFoo.h
#include <gtkmm.h>

 class DrawingAreaFoo : public Gtk::DrawingArea
 {
 public:
    DrawingAreaFoo();
    virtual ~DrawingAreaFoo();
    void drawSurface (cairo_surface_t * surface);
 protected:
    bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override;
    Cairo::RefPtr<Cairo::Surface> m_refSurface;
 };

 // DrawinAreFoo.cpp
 #include "DrawingAreaFoo.h"

 DrawingAreaFoo::DrawingAreaFoo() {}

 DrawingAreaFoo::~DrawingAreaFoo() {}

 bool DrawingAreaFoo::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
 {
    if (m_refSurface)
    {
       cr->set_source(m_refSurface, 0, 0);
       cr->paint();
    }
    return true;
 }

 void DrawingAreaFoo::drawSurface (cairo_surface_t * surface)
 {
    m_refSurface = Cairo::RefPtr<Cairo::Surface>{new Cairo::Surface(surface)} ;
    Glib::RefPtr<Gdk::Window> win = get_window();
    if (win)
    {
       Gdk::Rectangle r(0, 0, get_allocation().get_width(), get_allocation().get_height());
       win->invalidate_rect(r, false);
     }
  }

UPDATE The following after cairo_image_surface_create makes the code work.

cairo_t *cr = cairo_create (surface);
poppler_page_render (m_page, cr);
cairo_destroy (cr);

Solution

  • Added

    cairo_t *cr = cairo_create (surface);
    poppler_page_render (m_page, cr);
    cairo_destroy (cr);
    

    after

        cairo_surface_t * surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);