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