]> www.fi.muni.cz Git - evince.git/blob - pdf/ev-poppler.cc
Remove pixbuf backend for now
[evince.git] / pdf / ev-poppler.cc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* pdfdocument.h: Implementation of EvDocument for PDF
3  * Copyright (C) 2004, Red Hat, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <gtk/gtk.h>
21 #include <poppler.h>
22 #include <poppler-document.h>
23 #include <poppler-page.h>
24
25 #include "ev-poppler.h"
26 #include "ev-ps-exporter.h"
27 #include "ev-document-find.h"
28 #include "ev-document-misc.h"
29 #include "ev-document-links.h"
30 #include "ev-document-security.h"
31 #include "ev-document-thumbnails.h"
32
33
34 enum {
35         PROP_0,
36         PROP_TITLE
37 };
38
39
40 struct _PdfDocumentClass
41 {
42         GObjectClass parent_class;
43 };
44
45 struct _PdfDocument
46 {
47         GObject parent_instance;
48
49         PopplerDocument *document;
50         PopplerPage *page;
51         double scale;
52         gchar *password;
53 };
54
55 static void pdf_document_document_iface_init            (EvDocumentIface           *iface);
56 static void pdf_document_security_iface_init            (EvDocumentSecurityIface   *iface);
57 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
58 static void pdf_document_document_links_iface_init      (EvDocumentLinksIface      *iface);
59 static void pdf_document_thumbnails_get_dimensions      (EvDocumentThumbnails      *document_thumbnails,
60                                                          gint                       page,
61                                                          gint                       size,
62                                                          gint                      *width,
63                                                          gint                      *height);
64 static EvLink * ev_link_from_action (PopplerAction *action);
65
66
67 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
68                          {
69                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
70                                                         pdf_document_document_iface_init);
71                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
72                                                         pdf_document_security_iface_init);
73                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
74                                                         pdf_document_document_thumbnails_iface_init);
75                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
76                                                         pdf_document_document_links_iface_init);
77 #if 0
78                                  G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
79                                                         pdf_document_ps_exporter_iface_init);
80                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
81                                                         pdf_document_find_iface_init);
82 #endif
83                          });
84
85
86
87
88
89
90 static void
91 pdf_document_get_property (GObject *object,
92                            guint prop_id,
93                            GValue *value,
94                            GParamSpec *pspec)
95 {
96         PdfDocument *pdf_document = PDF_DOCUMENT (object);
97
98         switch (prop_id)
99         {
100                 case PROP_TITLE:
101                         if (pdf_document->document == NULL)
102                                 g_value_set_string (value, NULL);
103                         else
104                                 g_object_get_property (G_OBJECT (pdf_document->document), "title", value);
105                         break;
106         }
107 }
108
109 static void
110 pdf_document_class_init (PdfDocumentClass *klass)
111 {
112         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
113
114         gobject_class->get_property = pdf_document_get_property;
115
116         g_object_class_override_property (gobject_class, PROP_TITLE, "title");
117 }
118
119 static void
120 pdf_document_init (PdfDocument *pdf_document)
121 {
122         pdf_document->page = NULL;
123         pdf_document->scale = 1.0;
124         pdf_document->password = NULL;
125 }
126
127 static void
128 convert_error (GError  *poppler_error,
129                GError **error)
130 {
131         if (poppler_error == NULL)
132                 return;
133
134         if (poppler_error->domain == POPPLER_ERROR) {
135                 /* convert poppler errors into EvDocument errors */
136                 gint code = EV_DOCUMENT_ERROR_INVALID;
137                 if (poppler_error->code == POPPLER_ERROR_INVALID)
138                         code = EV_DOCUMENT_ERROR_INVALID;
139                 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
140                         code = EV_DOCUMENT_ERROR_ENCRYPTED;
141                         
142
143                 g_set_error (error,
144                              EV_DOCUMENT_ERROR,
145                              code,
146                              poppler_error->message,
147                              NULL);
148         } else {
149                 g_propagate_error (error, poppler_error);
150         }
151 }
152
153
154 /* EvDocument */
155 static gboolean
156 pdf_document_save (EvDocument  *document,
157                    const char  *uri,
158                    GError     **error)
159 {
160         gboolean retval;
161         GError *poppler_error = NULL;
162
163         retval = poppler_document_save (PDF_DOCUMENT (document)->document,
164                                         uri,
165                                         &poppler_error);
166         if (! retval)
167                 convert_error (poppler_error, error);
168
169         return retval;
170 }
171
172 static gboolean
173 pdf_document_load (EvDocument   *document,
174                    const char   *uri,
175                    GError      **error)
176 {
177         GError *poppler_error = NULL;
178         PdfDocument *pdf_document = PDF_DOCUMENT (document);
179
180         pdf_document->document =
181                 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
182
183         if (pdf_document->document == NULL) {
184                 convert_error (poppler_error, error);
185                 return FALSE;
186         }
187
188         return TRUE;
189 }
190
191 static int
192 pdf_document_get_n_pages (EvDocument *document)
193 {
194         return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
195 }
196
197 static void
198 pdf_document_set_page (EvDocument   *document,
199                        int           page)
200 {
201         page = CLAMP (page, 0, poppler_document_get_n_pages (PDF_DOCUMENT (document)->document) - 1);
202
203         PDF_DOCUMENT (document)->page = poppler_document_get_page (PDF_DOCUMENT (document)->document, page);
204 }
205
206 static int
207 pdf_document_get_page (EvDocument   *document)
208 {
209         PdfDocument *pdf_document;
210
211         pdf_document = PDF_DOCUMENT (document);
212
213         if (pdf_document->page)
214                 return poppler_page_get_index (pdf_document->page);
215
216         return 1;
217 }
218
219 static void 
220 pdf_document_set_scale (EvDocument   *document,
221                         double        scale)
222 {
223         PDF_DOCUMENT (document)->scale = scale;
224 }
225
226
227 static void
228 get_size_from_page (PopplerPage *poppler_page,
229                     double       scale,
230                     int         *width,
231                     int         *height)
232 {
233         gdouble width_d, height_d;
234         poppler_page_get_size (poppler_page, &width_d, &height_d);
235         if (width)
236                 *width = (int) (width_d * scale);
237         if (height)
238                 *height = (int) (height_d * scale);
239
240 }
241
242 static void
243 pdf_document_get_page_size (EvDocument   *document,
244                             int           page,
245                             int          *width,
246                             int          *height)
247 {
248         PopplerPage *poppler_page = NULL;
249
250         if (page == -1)
251                 poppler_page = PDF_DOCUMENT (document)->page;
252         else
253                 poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
254                                                           page);
255
256         if (poppler_page == NULL)
257                 poppler_document_get_page (PDF_DOCUMENT (document)->document, 0);
258
259         get_size_from_page (poppler_page,
260                             PDF_DOCUMENT (document)->scale,
261                             width, height);
262 }
263
264 static GList *
265 pdf_document_get_links (EvDocument *document)
266 {
267         PdfDocument *pdf_document;
268         GList *retval = NULL;
269         GList *mapping_list;
270         GList *list;
271         gint height;
272
273         pdf_document = PDF_DOCUMENT (document);
274         g_return_val_if_fail (pdf_document->page != NULL, NULL);
275
276         mapping_list = poppler_page_get_link_mapping (pdf_document->page);
277         get_size_from_page (pdf_document->page, 1.0, NULL, &height);
278
279         for (list = mapping_list; list; list = list->next) {
280                 PopplerLinkMapping *link_mapping;
281                 EvLinkMapping *ev_link_mapping;
282
283                 link_mapping = (PopplerLinkMapping *)list->data;
284                 ev_link_mapping = g_new (EvLinkMapping, 1);
285                 ev_link_mapping->link = ev_link_from_action (link_mapping->action);
286                 ev_link_mapping->x1 = link_mapping->x1;
287                 ev_link_mapping->x2 = link_mapping->x2;
288                 /* Invert this for X-style coordinates */
289                 ev_link_mapping->y1 = height - link_mapping->y2;
290                 ev_link_mapping->y2 = height - link_mapping->y1;
291
292                 retval = g_list_prepend (retval, ev_link_mapping);
293         }
294
295         poppler_page_free_link_mapping (mapping_list);
296
297         return g_list_reverse (retval);
298 }
299                         
300
301 static GdkPixbuf *
302 pdf_document_render_pixbuf (EvDocument   *document)
303 {
304         PdfDocument *pdf_document;
305         GdkPixbuf *pixbuf;
306         gint width, height;
307
308         pdf_document = PDF_DOCUMENT (document);
309         g_return_val_if_fail (pdf_document->page != NULL, NULL);
310
311         get_size_from_page (pdf_document->page,
312                             pdf_document->scale,
313                             &width, &height);
314
315         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
316                                  FALSE, 8,
317                                  width, height);
318
319         poppler_page_render_to_pixbuf (pdf_document->page,
320                                        0, 0,
321                                        width, height,
322                                        pdf_document->scale,
323                                        pixbuf,
324                                        0, 0);
325
326         return pixbuf;
327 }
328
329 /* EvDocumentSecurity */
330
331 static gboolean
332 pdf_document_has_document_security (EvDocumentSecurity *document_security)
333 {
334         /* FIXME: do we really need to have this? */
335         return FALSE;
336 }
337
338 static void
339 pdf_document_set_password (EvDocumentSecurity *document_security,
340                            const char         *password)
341 {
342         PdfDocument *document = PDF_DOCUMENT (document_security);
343
344         if (document->password)
345                 g_free (document->password);
346
347         document->password = g_strdup (password);
348 }
349
350
351
352 static void
353 pdf_document_document_iface_init (EvDocumentIface *iface)
354 {
355         iface->save = pdf_document_save;
356         iface->load = pdf_document_load;
357         iface->get_n_pages = pdf_document_get_n_pages;
358         iface->set_page = pdf_document_set_page;
359         iface->get_page = pdf_document_get_page;
360         iface->set_scale = pdf_document_set_scale;
361         iface->get_page_size = pdf_document_get_page_size;
362         iface->get_links = pdf_document_get_links;
363         iface->render_pixbuf = pdf_document_render_pixbuf;
364 };
365
366 static void
367 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
368 {
369         iface->has_document_security = pdf_document_has_document_security;
370         iface->set_password = pdf_document_set_password;
371 }
372
373 static gboolean
374 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
375 {
376         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
377         PopplerIndexIter *iter;
378
379         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
380
381         iter = poppler_index_iter_new (pdf_document->document);
382         if (iter == NULL)
383                 return FALSE;
384         poppler_index_iter_free (iter);
385
386         return TRUE;
387 }
388
389 static EvLink *
390 ev_link_from_action (PopplerAction *action)
391 {
392         EvLink *link;
393         const char *title;
394
395         title = action->any.title;
396         
397         if (action->type == POPPLER_ACTION_GOTO_DEST) {
398                 link = ev_link_new_page (title, action->goto_dest.dest->page_num - 1);
399         } else if (action->type == POPPLER_ACTION_URI) {
400                 link = ev_link_new_external (title, action->uri.uri);
401         } else {
402                 link = ev_link_new_title (title);
403         }
404
405         return link;    
406 }
407
408
409 static void
410 build_tree (PdfDocument      *pdf_document,
411             GtkTreeModel     *model,
412             GtkTreeIter      *parent,
413             PopplerIndexIter *iter)
414 {
415
416         do {
417                 GtkTreeIter tree_iter;
418                 PopplerIndexIter *child;
419                 PopplerAction *action;
420                 EvLink *link;
421                 
422                 action = poppler_index_iter_get_action (iter);
423                 if (action) {
424                         gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
425                         link = ev_link_from_action (action);
426                         poppler_action_free (action);
427
428                         gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
429                                             EV_DOCUMENT_LINKS_COLUMN_MARKUP, ev_link_get_title (link),
430                                             EV_DOCUMENT_LINKS_COLUMN_LINK, link,
431                                             -1);
432                         child = poppler_index_iter_get_child (iter);
433                         if (child)
434                                 build_tree (pdf_document, model, &tree_iter, child);
435                         poppler_index_iter_free (child);
436                 }
437         } while (poppler_index_iter_next (iter));
438 }
439
440
441 static GtkTreeModel *
442 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
443 {
444         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
445         GtkTreeModel *model = NULL;
446         PopplerIndexIter *iter;
447
448         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
449
450         iter = poppler_index_iter_new (pdf_document->document);
451         /* Create the model iff we have items*/
452         if (iter != NULL) {
453                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
454                                                              G_TYPE_STRING,
455                                                              G_TYPE_POINTER);
456                 build_tree (pdf_document, model, NULL, iter);
457                 poppler_index_iter_free (iter);
458         }
459         
460
461         return model;
462 }
463
464
465 static void
466 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
467 {
468         iface->has_document_links = pdf_document_links_has_document_links;
469         iface->get_links_model = pdf_document_links_get_links_model;
470 }
471
472
473 static GdkPixbuf *
474 make_thumbnail_for_size (PdfDocument *pdf_document,
475                          gint         page,
476                          gint         size,
477                          gboolean     border)
478 {
479         PopplerPage *poppler_page;
480         GdkPixbuf *pixbuf;
481         int width, height;
482         int x_offset, y_offset;
483         double scale;
484         gdouble unscaled_width, unscaled_height;
485
486         poppler_page = poppler_document_get_page (pdf_document->document, page);
487
488         g_return_val_if_fail (poppler_page != NULL, NULL);
489
490         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document), page, size, &width, &height);
491         poppler_page_get_size (poppler_page, &unscaled_width, &unscaled_height);
492         scale = width / unscaled_width;
493
494         if (border) {
495                 pixbuf = ev_document_misc_get_thumbnail_frame (width, height, NULL);
496                 x_offset = 1;
497                 y_offset = 1;
498         } else {
499                 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
500                                          width, height);
501                 gdk_pixbuf_fill (pixbuf, 0xffffffff);
502                 x_offset = 0;
503                 y_offset = 0;
504         }
505
506         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
507                                        width, height,
508                                        scale, pixbuf,
509                                        x_offset, y_offset);
510
511         return pixbuf;
512 }
513
514 static GdkPixbuf *
515 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
516                                        gint                  page,
517                                        gint                  size,
518                                        gboolean              border)
519 {
520         PdfDocument *pdf_document;
521         PopplerPage *poppler_page;
522         GdkPixbuf *pixbuf;
523
524         pdf_document = PDF_DOCUMENT (document_thumbnails);
525
526         poppler_page = poppler_document_get_page (pdf_document->document, page);
527         g_return_val_if_fail (poppler_page != NULL, NULL);
528
529         pixbuf = poppler_page_get_thumbnail (poppler_page);
530         if (pixbuf != NULL) {
531                 /* The document provides its own thumbnails. */
532                 if (border) {
533                         GdkPixbuf *real_pixbuf;
534
535                         real_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
536                         g_object_unref (pixbuf);
537                         pixbuf = real_pixbuf;
538                 }
539         } else {
540                 /* There is no provided thumbnail.  We need to make one. */
541                 pixbuf = make_thumbnail_for_size (pdf_document, page, size, border);
542         }
543         return pixbuf;
544 }
545
546 static void
547 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
548                                         gint                  page,
549                                         gint                  size,
550                                         gint                 *width,
551                                         gint                 *height)
552 {
553         PdfDocument *pdf_document;
554         PopplerPage *poppler_page;
555         gint has_thumb;
556         
557         pdf_document = PDF_DOCUMENT (document_thumbnails);
558         poppler_page = poppler_document_get_page (pdf_document->document, page);
559
560         g_return_if_fail (width != NULL);
561         g_return_if_fail (height != NULL);
562         g_return_if_fail (poppler_page != NULL);
563
564         has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
565
566         if (!has_thumb) {
567                 int page_width, page_height;
568
569                 get_size_from_page (poppler_page, 1.0, &page_width, &page_height);
570
571                 if (page_width > page_height) {
572                         *width = size;
573                         *height = (int) (size * page_height / page_width);
574                 } else {
575                         *width = (int) (size * page_width / page_height);
576                         *height = size;
577                 }
578         }
579 }
580
581 static void
582 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
583 {
584         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
585         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
586 }
587
588 PdfDocument *
589 pdf_document_new (void)
590 {
591         return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));
592 }