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.
21 #include "image-load-pdf.h"
24 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <glib-object.h>
31 #include "image-load.h"
36 struct ImageLoaderPDF : public ImageLoaderBackend
39 ~ImageLoaderPDF() override;
41 void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
42 gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
43 GdkPixbuf *get_pixbuf() override;
44 gchar *get_format_name() override;
45 gchar **get_format_mime_types() override;
46 void set_page_num(gint page_num) override;
47 gint get_page_total() override;
50 AreaUpdatedCb area_updated_cb;
58 gboolean ImageLoaderPDF::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
60 GError *poppler_error = nullptr;
62 PopplerDocument *document;
65 cairo_surface_t *surface;
70 #if POPPLER_CHECK_VERSION(0,82,0)
71 GBytes *bytes = g_bytes_new_static(buf, count);
72 document = poppler_document_new_from_bytes(bytes, nullptr, &poppler_error);
74 document = poppler_document_new_from_data((gchar *)(buf), count, nullptr, &poppler_error);
79 log_printf("warning: pdf reader error: %s\n", poppler_error->message);
80 g_error_free(poppler_error);
84 page_total = poppler_document_get_n_pages(document);
87 this->page_total = page_total;
90 page = poppler_document_get_page(document, page_num);
91 poppler_page_get_size(page, &width, &height);
93 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
94 cr = cairo_create(surface);
95 poppler_page_render(page, cr);
97 cairo_set_operator(cr, CAIRO_OPERATOR_DEST_OVER);
98 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
101 pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, width, height);
102 area_updated_cb(nullptr, 0, 0, width, height, data);
105 cairo_surface_destroy(surface);
106 g_object_unref(page);
111 g_object_unref(document);
112 #if POPPLER_CHECK_VERSION(0,82,0)
113 g_bytes_unref(bytes);
119 void ImageLoaderPDF::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
121 this->area_updated_cb = area_updated_cb;
126 GdkPixbuf *ImageLoaderPDF::get_pixbuf()
131 gchar *ImageLoaderPDF::get_format_name()
133 return g_strdup("pdf");
136 gchar **ImageLoaderPDF::get_format_mime_types()
138 static const gchar *mime[] = {"application/pdf", nullptr};
139 return g_strdupv(const_cast<gchar **>(mime));
142 void ImageLoaderPDF::set_page_num(gint page_num)
144 this->page_num = page_num;
147 gint ImageLoaderPDF::get_page_total()
152 ImageLoaderPDF::~ImageLoaderPDF()
154 if (pixbuf) g_object_unref(pixbuf);
159 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_pdf()
161 return std::make_unique<ImageLoaderPDF>();
164 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */