2 * Copyright (C) 20018 - The Geeqie Team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "image-load-pdf.h"
27 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <glib-object.h>
34 #include "image-load.h"
39 struct ImageLoaderPDF : public ImageLoaderBackend
42 ~ImageLoaderPDF() override;
44 void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
45 gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
46 GdkPixbuf *get_pixbuf() override;
47 gchar *get_format_name() override;
48 gchar **get_format_mime_types() override;
49 void set_page_num(gint page_num) override;
50 gint get_page_total() override;
53 AreaUpdatedCb area_updated_cb;
61 gboolean ImageLoaderPDF::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
63 GError *poppler_error = nullptr;
65 PopplerDocument *document;
68 cairo_surface_t *surface;
73 #if POPPLER_CHECK_VERSION(0,82,0)
74 GBytes *bytes = g_bytes_new_static(buf, count);
75 document = poppler_document_new_from_bytes(bytes, nullptr, &poppler_error);
77 document = poppler_document_new_from_data((gchar *)(buf), count, nullptr, &poppler_error);
82 log_printf("warning: pdf reader error: %s\n", poppler_error->message);
83 g_error_free(poppler_error);
87 page_total = poppler_document_get_n_pages(document);
90 this->page_total = page_total;
93 page = poppler_document_get_page(document, page_num);
94 poppler_page_get_size(page, &width, &height);
96 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
97 cr = cairo_create(surface);
98 poppler_page_render(page, cr);
100 cairo_set_operator(cr, CAIRO_OPERATOR_DEST_OVER);
101 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
104 pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, width, height);
105 area_updated_cb(nullptr, 0, 0, width, height, data);
108 cairo_surface_destroy(surface);
109 g_object_unref(page);
114 g_object_unref(document);
115 #if POPPLER_CHECK_VERSION(0,82,0)
116 g_bytes_unref(bytes);
122 void ImageLoaderPDF::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
124 this->area_updated_cb = area_updated_cb;
129 GdkPixbuf *ImageLoaderPDF::get_pixbuf()
134 gchar *ImageLoaderPDF::get_format_name()
136 return g_strdup("pdf");
139 gchar **ImageLoaderPDF::get_format_mime_types()
141 static const gchar *mime[] = {"application/pdf", nullptr};
142 return g_strdupv(const_cast<gchar **>(mime));
145 void ImageLoaderPDF::set_page_num(gint page_num)
147 this->page_num = page_num;
150 gint ImageLoaderPDF::get_page_total()
155 ImageLoaderPDF::~ImageLoaderPDF()
157 if (pixbuf) g_object_unref(pixbuf);
162 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_pdf()
164 return std::make_unique<ImageLoaderPDF>();
168 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */