]> www.fi.muni.cz Git - evince.git/blob - pdf/ev-poppler.cc
Cr 31 13:27:50 2005 Jonathan Blandford <jrb@redhat.com>
[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 char *
265 pdf_document_get_page_label (EvDocument *document,
266                              int         page)
267 {
268         PopplerPage *poppler_page = NULL;
269         char *label = NULL;
270
271         if (page == -1)
272                 poppler_page = PDF_DOCUMENT (document)->page;
273         else
274                 poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
275                                                           page);
276
277         g_object_get (poppler_page,
278                       "label", &label,
279                       NULL);
280
281         return label;
282 }
283
284 static GList *
285 pdf_document_get_links (EvDocument *document)
286 {
287         PdfDocument *pdf_document;
288         GList *retval = NULL;
289         GList *mapping_list;
290         GList *list;
291         gint height;
292
293         pdf_document = PDF_DOCUMENT (document);
294         g_return_val_if_fail (pdf_document->page != NULL, NULL);
295
296         mapping_list = poppler_page_get_link_mapping (pdf_document->page);
297         get_size_from_page (pdf_document->page, 1.0, NULL, &height);
298
299         for (list = mapping_list; list; list = list->next) {
300                 PopplerLinkMapping *link_mapping;
301                 EvLinkMapping *ev_link_mapping;
302
303                 link_mapping = (PopplerLinkMapping *)list->data;
304                 ev_link_mapping = g_new (EvLinkMapping, 1);
305                 ev_link_mapping->link = ev_link_from_action (link_mapping->action);
306                 ev_link_mapping->x1 = link_mapping->x1;
307                 ev_link_mapping->x2 = link_mapping->x2;
308                 /* Invert this for X-style coordinates */
309                 ev_link_mapping->y1 = height - link_mapping->y2;
310                 ev_link_mapping->y2 = height - link_mapping->y1;
311
312                 retval = g_list_prepend (retval, ev_link_mapping);
313         }
314
315         poppler_page_free_link_mapping (mapping_list);
316
317         return g_list_reverse (retval);
318 }
319                         
320
321 static GdkPixbuf *
322 pdf_document_render_pixbuf (EvDocument   *document)
323 {
324         PdfDocument *pdf_document;
325         GdkPixbuf *pixbuf;
326         gint width, height;
327
328         pdf_document = PDF_DOCUMENT (document);
329         g_return_val_if_fail (pdf_document->page != NULL, NULL);
330
331         get_size_from_page (pdf_document->page,
332                             pdf_document->scale,
333                             &width, &height);
334
335         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
336                                  FALSE, 8,
337                                  width, height);
338
339         poppler_page_render_to_pixbuf (pdf_document->page,
340                                        0, 0,
341                                        width, height,
342                                        pdf_document->scale,
343                                        pixbuf,
344                                        0, 0);
345
346         return pixbuf;
347 }
348
349 /* EvDocumentSecurity */
350
351 static gboolean
352 pdf_document_has_document_security (EvDocumentSecurity *document_security)
353 {
354         /* FIXME: do we really need to have this? */
355         return FALSE;
356 }
357
358 static void
359 pdf_document_set_password (EvDocumentSecurity *document_security,
360                            const char         *password)
361 {
362         PdfDocument *document = PDF_DOCUMENT (document_security);
363
364         if (document->password)
365                 g_free (document->password);
366
367         document->password = g_strdup (password);
368 }
369
370
371
372 static void
373 pdf_document_document_iface_init (EvDocumentIface *iface)
374 {
375         iface->save = pdf_document_save;
376         iface->load = pdf_document_load;
377         iface->get_n_pages = pdf_document_get_n_pages;
378         iface->set_page = pdf_document_set_page;
379         iface->get_page = pdf_document_get_page;
380         iface->set_scale = pdf_document_set_scale;
381         iface->get_page_size = pdf_document_get_page_size;
382         iface->get_page_label = pdf_document_get_page_label;
383         iface->get_links = pdf_document_get_links;
384         iface->render_pixbuf = pdf_document_render_pixbuf;
385 };
386
387 static void
388 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
389 {
390         iface->has_document_security = pdf_document_has_document_security;
391         iface->set_password = pdf_document_set_password;
392 }
393
394 static gboolean
395 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
396 {
397         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
398         PopplerIndexIter *iter;
399
400         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
401
402         iter = poppler_index_iter_new (pdf_document->document);
403         if (iter == NULL)
404                 return FALSE;
405         poppler_index_iter_free (iter);
406
407         return TRUE;
408 }
409
410 static EvLink *
411 ev_link_from_action (PopplerAction *action)
412 {
413         EvLink *link;
414         const char *title;
415
416         title = action->any.title;
417         
418         if (action->type == POPPLER_ACTION_GOTO_DEST) {
419                 link = ev_link_new_page (title, action->goto_dest.dest->page_num - 1);
420         } else if (action->type == POPPLER_ACTION_URI) {
421                 link = ev_link_new_external (title, action->uri.uri);
422         } else {
423                 link = ev_link_new_title (title);
424         }
425
426         return link;    
427 }
428
429
430 static void
431 build_tree (PdfDocument      *pdf_document,
432             GtkTreeModel     *model,
433             GtkTreeIter      *parent,
434             PopplerIndexIter *iter)
435 {
436
437         do {
438                 GtkTreeIter tree_iter;
439                 PopplerIndexIter *child;
440                 PopplerAction *action;
441                 EvLink *link;
442                 
443                 action = poppler_index_iter_get_action (iter);
444                 if (action) {
445                         gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
446                         link = ev_link_from_action (action);
447                         poppler_action_free (action);
448
449                         gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
450                                             EV_DOCUMENT_LINKS_COLUMN_MARKUP, ev_link_get_title (link),
451                                             EV_DOCUMENT_LINKS_COLUMN_LINK, link,
452                                             -1);
453                         child = poppler_index_iter_get_child (iter);
454                         if (child)
455                                 build_tree (pdf_document, model, &tree_iter, child);
456                         poppler_index_iter_free (child);
457                 }
458         } while (poppler_index_iter_next (iter));
459 }
460
461
462 static GtkTreeModel *
463 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
464 {
465         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
466         GtkTreeModel *model = NULL;
467         PopplerIndexIter *iter;
468
469         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
470
471         iter = poppler_index_iter_new (pdf_document->document);
472         /* Create the model iff we have items*/
473         if (iter != NULL) {
474                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
475                                                              G_TYPE_STRING,
476                                                              G_TYPE_POINTER);
477                 build_tree (pdf_document, model, NULL, iter);
478                 poppler_index_iter_free (iter);
479         }
480         
481
482         return model;
483 }
484
485
486 static void
487 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
488 {
489         iface->has_document_links = pdf_document_links_has_document_links;
490         iface->get_links_model = pdf_document_links_get_links_model;
491 }
492
493
494 static GdkPixbuf *
495 make_thumbnail_for_size (PdfDocument *pdf_document,
496                          gint         page,
497                          gint         size,
498                          gboolean     border)
499 {
500         PopplerPage *poppler_page;
501         GdkPixbuf *pixbuf;
502         int width, height;
503         int x_offset, y_offset;
504         double scale;
505         gdouble unscaled_width, unscaled_height;
506
507         poppler_page = poppler_document_get_page (pdf_document->document, page);
508
509         g_return_val_if_fail (poppler_page != NULL, NULL);
510
511         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document), page, size, &width, &height);
512         poppler_page_get_size (poppler_page, &unscaled_width, &unscaled_height);
513         scale = width / unscaled_width;
514
515         if (border) {
516                 pixbuf = ev_document_misc_get_thumbnail_frame (width, height, NULL);
517                 x_offset = 1;
518                 y_offset = 1;
519         } else {
520                 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
521                                          width, height);
522                 gdk_pixbuf_fill (pixbuf, 0xffffffff);
523                 x_offset = 0;
524                 y_offset = 0;
525         }
526
527         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
528                                        width, height,
529                                        scale, pixbuf,
530                                        x_offset, y_offset);
531
532         return pixbuf;
533 }
534
535 static GdkPixbuf *
536 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
537                                        gint                  page,
538                                        gint                  size,
539                                        gboolean              border)
540 {
541         PdfDocument *pdf_document;
542         PopplerPage *poppler_page;
543         GdkPixbuf *pixbuf;
544
545         pdf_document = PDF_DOCUMENT (document_thumbnails);
546
547         poppler_page = poppler_document_get_page (pdf_document->document, page);
548         g_return_val_if_fail (poppler_page != NULL, NULL);
549
550         pixbuf = poppler_page_get_thumbnail (poppler_page);
551         if (pixbuf != NULL) {
552                 /* The document provides its own thumbnails. */
553                 if (border) {
554                         GdkPixbuf *real_pixbuf;
555
556                         real_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
557                         g_object_unref (pixbuf);
558                         pixbuf = real_pixbuf;
559                 }
560         } else {
561                 /* There is no provided thumbnail.  We need to make one. */
562                 pixbuf = make_thumbnail_for_size (pdf_document, page, size, border);
563         }
564         return pixbuf;
565 }
566
567 static void
568 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
569                                         gint                  page,
570                                         gint                  size,
571                                         gint                 *width,
572                                         gint                 *height)
573 {
574         PdfDocument *pdf_document;
575         PopplerPage *poppler_page;
576         gint has_thumb;
577         
578         pdf_document = PDF_DOCUMENT (document_thumbnails);
579         poppler_page = poppler_document_get_page (pdf_document->document, page);
580
581         g_return_if_fail (width != NULL);
582         g_return_if_fail (height != NULL);
583         g_return_if_fail (poppler_page != NULL);
584
585         has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
586
587         if (!has_thumb) {
588                 int page_width, page_height;
589
590                 get_size_from_page (poppler_page, 1.0, &page_width, &page_height);
591
592                 if (page_width > page_height) {
593                         *width = size;
594                         *height = (int) (size * page_height / page_width);
595                 } else {
596                         *width = (int) (size * page_width / page_height);
597                         *height = size;
598                 }
599         }
600 }
601
602 static void
603 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
604 {
605         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
606         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
607 }
608
609 PdfDocument *
610 pdf_document_new (void)
611 {
612         return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));
613 }