Blind fix macOS build
[geeqie.git] / src / image-load-pdf.cc
1 /*
2  * Copyright (C) 20018 - The Geeqie Team
3  *
4  * Author: Colin Clark
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "image-load-pdf.h"
22
23 #include <cairo.h>
24 #include <gdk-pixbuf/gdk-pixbuf.h>
25 #include <gdk/gdk.h>
26 #include <glib-object.h>
27 #include <glib.h>
28 #include <poppler.h>
29
30 #include "debug.h"
31 #include "image-load.h"
32
33 namespace
34 {
35
36 struct ImageLoaderPDF : public ImageLoaderBackend
37 {
38 public:
39         ~ImageLoaderPDF() override;
40
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;
48
49 private:
50         AreaUpdatedCb area_updated_cb;
51         gpointer data;
52
53         GdkPixbuf *pixbuf;
54         gint page_num;
55         gint page_total;
56 };
57
58 gboolean ImageLoaderPDF::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
59 {
60         GError *poppler_error = nullptr;
61         PopplerPage *page;
62         PopplerDocument *document;
63         gdouble width;
64         gdouble height;
65         cairo_surface_t *surface;
66         cairo_t *cr;
67         gboolean ret = FALSE;
68         gint page_total;
69
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);
73 #else
74         document = poppler_document_new_from_data((gchar *)(buf), count, nullptr, &poppler_error);
75 #endif
76
77         if (poppler_error)
78                 {
79                 log_printf("warning: pdf reader error: %s\n", poppler_error->message);
80                 g_error_free(poppler_error);
81                 }
82         else
83                 {
84                 page_total = poppler_document_get_n_pages(document);
85                 if (page_total > 0)
86                         {
87                         this->page_total = page_total;
88                         }
89
90                 page = poppler_document_get_page(document, page_num);
91                 poppler_page_get_size(page, &width, &height);
92
93                 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
94                 cr = cairo_create(surface);
95                 poppler_page_render(page, cr);
96
97                 cairo_set_operator(cr, CAIRO_OPERATOR_DEST_OVER);
98                 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
99                 cairo_paint(cr);
100
101                 pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, width, height);
102                 area_updated_cb(nullptr, 0, 0, width, height, data);
103
104                 cairo_destroy (cr);
105                 cairo_surface_destroy(surface);
106                 g_object_unref(page);
107                 chunk_size = count;
108                 ret = TRUE;
109                 }
110
111         g_object_unref(document);
112 #if POPPLER_CHECK_VERSION(0,82,0)
113         g_bytes_unref(bytes);
114 #endif
115
116         return ret;
117 }
118
119 void ImageLoaderPDF::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
120 {
121         this->area_updated_cb = area_updated_cb;
122         this->data = data;
123         page_num = 0;
124 }
125
126 GdkPixbuf *ImageLoaderPDF::get_pixbuf()
127 {
128         return pixbuf;
129 }
130
131 gchar *ImageLoaderPDF::get_format_name()
132 {
133         return g_strdup("pdf");
134 }
135
136 gchar **ImageLoaderPDF::get_format_mime_types()
137 {
138         static const gchar *mime[] = {"application/pdf", nullptr};
139         return g_strdupv(const_cast<gchar **>(mime));
140 }
141
142 void ImageLoaderPDF::set_page_num(gint page_num)
143 {
144         this->page_num = page_num;
145 }
146
147 gint ImageLoaderPDF::get_page_total()
148 {
149         return page_total;
150 }
151
152 ImageLoaderPDF::~ImageLoaderPDF()
153 {
154         if (pixbuf) g_object_unref(pixbuf);
155 }
156
157 } // namespace
158
159 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_pdf()
160 {
161         return std::make_unique<ImageLoaderPDF>();
162 }
163
164 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */