]> www.fi.muni.cz Git - evince.git/blob - backend/pdf/ev-poppler.cc
Remove attachments from EvDocument interface and use EvDocumentAttachments instead
[evince.git] / backend / 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 "config.h"
21
22 #include <math.h>
23 #include <string.h>
24 #include <gtk/gtk.h>
25 #include <poppler.h>
26 #include <poppler-document.h>
27 #include <poppler-page.h>
28 #ifdef HAVE_CAIRO_PDF
29 #include <cairo-pdf.h>
30 #endif
31 #ifdef HAVE_CAIRO_PS
32 #include <cairo-ps.h>
33 #endif
34 #include <glib/gi18n-lib.h>
35
36 #include "ev-poppler.h"
37 #include "ev-file-exporter.h"
38 #include "ev-mapping.h"
39 #include "ev-document-find.h"
40 #include "ev-document-misc.h"
41 #include "ev-document-links.h"
42 #include "ev-document-images.h"
43 #include "ev-document-fonts.h"
44 #include "ev-document-security.h"
45 #include "ev-document-thumbnails.h"
46 #include "ev-document-transition.h"
47 #include "ev-document-forms.h"
48 #include "ev-document-layers.h"
49 #include "ev-document-print.h"
50 #include "ev-document-annotations.h"
51 #include "ev-document-attachments.h"
52 #include "ev-selection.h"
53 #include "ev-transition-effect.h"
54 #include "ev-attachment.h"
55 #include "ev-image.h"
56
57 #include <libxml/tree.h>
58 #include <libxml/parser.h>
59 #include <libxml/xpath.h>
60 #include <libxml/xpathInternals.h>
61
62 #if (defined (HAVE_POPPLER_PAGE_RENDER)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS))
63 #define HAVE_CAIRO_PRINT
64 #endif
65
66 typedef struct {
67         PdfDocument *document;
68         char *text;
69         GList **pages;
70         guint idle;
71         int start_page;
72         int search_page;
73 } PdfDocumentSearch;
74
75 typedef struct {
76         EvFileExporterFormat format;
77
78         /* Pages per sheet */
79         gint pages_per_sheet;
80         gint pages_printed;
81         gint pages_x;
82         gint pages_y;
83         gdouble paper_width;
84         gdouble paper_height;
85         
86 #ifdef HAVE_CAIRO_PRINT
87         cairo_t *cr;
88 #else
89         PopplerPSFile *ps_file;
90 #endif
91 } PdfPrintContext;
92
93 struct _PdfDocumentClass
94 {
95         GObjectClass parent_class;
96 };
97
98 struct _PdfDocument
99 {
100         GObject parent_instance;
101
102         PopplerDocument *document;
103         gchar *password;
104         gboolean modified;
105
106         PopplerFontInfo *font_info;
107         PopplerFontsIter *fonts_iter;
108         int fonts_scanned_pages;
109
110         PdfDocumentSearch *search;
111         PdfPrintContext *print_ctx;
112
113         GList *layers;
114 };
115
116 static void pdf_document_document_iface_init             (EvDocumentIface            *iface);
117 static void pdf_document_security_iface_init             (EvDocumentSecurityIface    *iface);
118 static void pdf_document_document_thumbnails_iface_init  (EvDocumentThumbnailsIface  *iface);
119 static void pdf_document_document_links_iface_init       (EvDocumentLinksIface       *iface);
120 static void pdf_document_document_images_iface_init      (EvDocumentImagesIface      *iface);
121 static void pdf_document_document_forms_iface_init       (EvDocumentFormsIface       *iface);
122 static void pdf_document_document_fonts_iface_init       (EvDocumentFontsIface       *iface);
123 static void pdf_document_document_layers_iface_init      (EvDocumentLayersIface      *iface);
124 #ifdef HAVE_POPPLER_PAGE_RENDER
125 static void pdf_document_document_print_iface_init       (EvDocumentPrintIface       *iface);
126 #endif
127 static void pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface);
128 static void pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface);
129 static void pdf_document_find_iface_init                 (EvDocumentFindIface        *iface);
130 static void pdf_document_file_exporter_iface_init        (EvFileExporterIface        *iface);
131 static void pdf_selection_iface_init                     (EvSelectionIface           *iface);
132 static void pdf_document_page_transition_iface_init      (EvDocumentTransitionIface  *iface);
133 static void pdf_document_thumbnails_get_dimensions       (EvDocumentThumbnails       *document_thumbnails,
134                                                           EvRenderContext            *rc,
135                                                           gint                       *width,
136                                                           gint                       *height);
137 static int  pdf_document_get_n_pages                     (EvDocument                 *document);
138
139 static EvLinkDest *ev_link_dest_from_dest   (PdfDocument       *pdf_document,
140                                              PopplerDest       *dest);
141 static EvLink     *ev_link_from_action      (PdfDocument       *pdf_document,
142                                              PopplerAction     *action);
143 static void        pdf_document_search_free (PdfDocumentSearch *search);
144 static void        pdf_print_context_free   (PdfPrintContext   *ctx);
145
146 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
147                          {
148                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
149                                                                  pdf_document_security_iface_init);
150                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
151                                                                  pdf_document_document_thumbnails_iface_init);
152                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
153                                                                  pdf_document_document_links_iface_init);
154                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
155                                                                  pdf_document_document_images_iface_init);
156                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
157                                                                  pdf_document_document_forms_iface_init);
158                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
159                                                                  pdf_document_document_fonts_iface_init);
160                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
161                                                                  pdf_document_document_layers_iface_init);
162 #ifdef HAVE_POPPLER_PAGE_RENDER
163                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
164                                                                  pdf_document_document_print_iface_init);
165 #endif
166                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS,
167                                                                  pdf_document_document_annotations_iface_init);
168                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ATTACHMENTS,
169                                                                  pdf_document_document_attachments_iface_init);
170                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
171                                                                  pdf_document_find_iface_init);
172                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
173                                                                  pdf_document_file_exporter_iface_init);
174                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
175                                                                  pdf_selection_iface_init);
176                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
177                                                                  pdf_document_page_transition_iface_init);
178                          });
179
180 static void
181 pdf_document_search_free (PdfDocumentSearch   *search)
182 {
183         PdfDocument *pdf_document = search->document;
184         int n_pages;
185         int i;
186
187         if (search->idle != 0)
188                 g_source_remove (search->idle);
189
190         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
191         for (i = 0; i < n_pages; i++) {
192                 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
193                 g_list_free (search->pages[i]);
194         }
195         g_free (search->pages);
196         
197         g_free (search->text);
198         g_free (search);
199 }
200
201 static void
202 pdf_document_dispose (GObject *object)
203 {
204         PdfDocument *pdf_document = PDF_DOCUMENT(object);
205
206         if (pdf_document->print_ctx) {
207                 pdf_print_context_free (pdf_document->print_ctx);
208                 pdf_document->print_ctx = NULL;
209         }
210         
211         if (pdf_document->search) {
212                 pdf_document_search_free (pdf_document->search);
213                 pdf_document->search = NULL;
214         }
215
216         if (pdf_document->document) {
217                 g_object_unref (pdf_document->document);
218         }
219
220         if (pdf_document->font_info) { 
221                 poppler_font_info_free (pdf_document->font_info);
222         }
223
224         if (pdf_document->fonts_iter) {
225                 poppler_fonts_iter_free (pdf_document->fonts_iter);
226         }
227
228         if (pdf_document->layers) {
229                 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
230                 g_list_free (pdf_document->layers);
231         }
232
233         G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
234 }
235
236 static void
237 pdf_document_class_init (PdfDocumentClass *klass)
238 {
239         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
240
241         g_object_class->dispose = pdf_document_dispose;
242 }
243
244 static void
245 pdf_document_init (PdfDocument *pdf_document)
246 {
247         pdf_document->password = NULL;
248 }
249
250 static void
251 convert_error (GError  *poppler_error,
252                GError **error)
253 {
254         if (poppler_error == NULL)
255                 return;
256
257         if (poppler_error->domain == POPPLER_ERROR) {
258                 /* convert poppler errors into EvDocument errors */
259                 gint code = EV_DOCUMENT_ERROR_INVALID;
260                 if (poppler_error->code == POPPLER_ERROR_INVALID)
261                         code = EV_DOCUMENT_ERROR_INVALID;
262                 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
263                         code = EV_DOCUMENT_ERROR_ENCRYPTED;
264                         
265                 g_set_error_literal (error,
266                                      EV_DOCUMENT_ERROR,
267                                      code,
268                                      poppler_error->message);
269
270                 g_error_free (poppler_error);
271         } else {
272                 g_propagate_error (error, poppler_error);
273         }
274 }
275
276
277 /* EvDocument */
278 static gboolean
279 pdf_document_save (EvDocument  *document,
280                    const char  *uri,
281                    GError     **error)
282 {
283         PdfDocument *pdf_document = PDF_DOCUMENT (document);
284         gboolean retval;
285         GError *poppler_error = NULL;
286
287         if (pdf_document->modified) {
288                 retval = poppler_document_save (pdf_document->document,
289                                                 uri, &poppler_error);
290         } else {
291                 retval = poppler_document_save_a_copy (pdf_document->document,
292                                                        uri, &poppler_error);
293         }
294                                                        
295         if (! retval)
296                 convert_error (poppler_error, error);
297
298         return retval;
299 }
300
301 static gboolean
302 pdf_document_load (EvDocument   *document,
303                    const char   *uri,
304                    GError      **error)
305 {
306         GError *poppler_error = NULL;
307         PdfDocument *pdf_document = PDF_DOCUMENT (document);
308
309         pdf_document->document =
310                 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
311
312         if (pdf_document->document == NULL) {
313                 convert_error (poppler_error, error);
314                 return FALSE;
315         }
316
317         return TRUE;
318 }
319
320 static int
321 pdf_document_get_n_pages (EvDocument *document)
322 {
323         return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
324 }
325
326 static EvPage *
327 pdf_document_get_page (EvDocument *document,
328                        gint        index)
329 {
330         PdfDocument *pdf_document = PDF_DOCUMENT (document);
331         PopplerPage *poppler_page;
332         EvPage      *page;
333
334         poppler_page = poppler_document_get_page (pdf_document->document, index);
335         page = ev_page_new (index);
336         page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
337         page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
338         g_object_unref (poppler_page);
339
340         return page;
341 }
342
343 static void
344 pdf_document_get_page_size (EvDocument *document,
345                             EvPage     *page,
346                             double     *width,
347                             double     *height)
348 {
349         g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
350         
351         poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
352 }
353
354 static char *
355 pdf_document_get_page_label (EvDocument *document,
356                              EvPage     *page)
357 {
358         char *label = NULL;
359
360         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
361
362         g_object_get (G_OBJECT (page->backend_page),
363                       "label", &label,
364                       NULL);
365         return label;
366 }
367
368 static cairo_surface_t *
369 pdf_page_render (PopplerPage     *page,
370                  gint             width,
371                  gint             height,
372                  EvRenderContext *rc)
373 {
374         cairo_surface_t *surface;
375
376 #ifdef HAVE_POPPLER_PAGE_RENDER
377         cairo_t *cr;
378
379         surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
380                                               width, height);
381         cr = cairo_create (surface);
382
383         switch (rc->rotation) {
384                 case 90:
385                         cairo_translate (cr, width, 0);
386                         break;
387                 case 180:
388                         cairo_translate (cr, width, height);
389                         break;
390                 case 270:
391                         cairo_translate (cr, 0, height);
392                         break;
393                 default:
394                         cairo_translate (cr, 0, 0);
395         }
396         cairo_scale (cr, rc->scale, rc->scale);
397         cairo_rotate (cr, rc->rotation * G_PI / 180.0);
398         poppler_page_render (page, cr);
399
400         cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
401         cairo_set_source_rgb (cr, 1., 1., 1.);
402         cairo_paint (cr);
403
404         cairo_destroy (cr);
405 #else /* HAVE_POPPLER_PAGE_RENDER */
406         GdkPixbuf *pixbuf;
407         
408         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
409                                  FALSE, 8,
410                                  width, height);
411
412         poppler_page_render_to_pixbuf (page,
413                                        0, 0,
414                                        width, height,
415                                        rc->scale,
416                                        rc->rotation,
417                                        pixbuf);
418         surface = ev_document_misc_surface_from_pixbuf (pixbuf);
419         g_object_unref (pixbuf);
420 #endif /* HAVE_POPPLER_PAGE_RENDER */
421
422         return surface; 
423 }
424
425 static cairo_surface_t *
426 pdf_document_render (EvDocument      *document,
427                      EvRenderContext *rc)
428 {
429         PdfDocument *pdf_document;
430         PopplerPage *poppler_page;
431         double width_points, height_points;
432         gint width, height;
433
434         poppler_page = POPPLER_PAGE (rc->page->backend_page);
435
436         poppler_page_get_size (poppler_page,
437                                &width_points, &height_points);
438         
439         if (rc->rotation == 90 || rc->rotation == 270) {
440                 width = (int) ((height_points * rc->scale) + 0.5);
441                 height = (int) ((width_points * rc->scale) + 0.5);
442         } else {
443                 width = (int) ((width_points * rc->scale) + 0.5);
444                 height = (int) ((height_points * rc->scale) + 0.5);
445         }
446         
447         return pdf_page_render (poppler_page,
448                                 width, height, rc);
449 }
450
451 /* EvDocumentSecurity */
452
453 static gboolean
454 pdf_document_has_document_security (EvDocumentSecurity *document_security)
455 {
456         /* FIXME: do we really need to have this? */
457         return FALSE;
458 }
459
460 static void
461 pdf_document_set_password (EvDocumentSecurity *document_security,
462                            const char         *password)
463 {
464         PdfDocument *document = PDF_DOCUMENT (document_security);
465
466         if (document->password)
467                 g_free (document->password);
468
469         document->password = g_strdup (password);
470 }
471
472
473 /* reference:
474 http://www.pdfa.org/lib/exe/fetch.php?id=pdfa%3Aen%3Atechdoc&cache=cache&media=pdfa:techdoc:tn0001_pdfa-1_and_namespaces_2008-03-18.pdf */
475 static char *
476 pdf_document_get_format_from_metadata (const char *metadata)
477 {
478         xmlDocPtr doc;
479         xmlXPathContextPtr xpathCtx;
480         xmlXPathObjectPtr xpathObj;
481         xmlChar *part = NULL;
482         xmlChar *conf = NULL;
483         char *result = NULL;
484         int i;
485
486         doc = xmlParseMemory (metadata, strlen (metadata));
487         if (doc == NULL)
488                 return NULL;      /* invalid xml metadata */
489
490         xpathCtx = xmlXPathNewContext (doc);
491         if (xpathCtx == NULL) {
492                 xmlFreeDoc (doc);
493                 return NULL;      /* invalid xpath context */
494         }
495
496         /* add pdf/a namespaces */
497         xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
498         xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
499         xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
500
501         /* reads pdf/a part */
502         /* first syntax: child node */
503         xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
504         if (xpathObj != NULL) {
505                 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
506                         part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
507
508                 xmlXPathFreeObject (xpathObj);
509         }
510         if (part == NULL) {
511                 /* second syntax: attribute */
512                 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
513                 if (xpathObj != NULL) {
514                         if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
515                                 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
516
517                         xmlXPathFreeObject (xpathObj);
518                 }
519         }
520
521         /* reads pdf/a conformance */
522         /* first syntax: child node */
523         xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
524         if (xpathObj != NULL) {
525                 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
526                         conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
527
528                 xmlXPathFreeObject (xpathObj);
529         }
530         if (conf == NULL) {
531                 /* second syntax: attribute */
532                 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
533                 if (xpathObj != NULL) {
534                         if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
535                                 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
536
537                         xmlXPathFreeObject (xpathObj);
538                 }
539         }
540
541         if (part != NULL && conf != NULL) {
542                 /* makes conf lowercase */
543                 for (i = 0; conf[i]; i++)
544                         conf[i] = g_ascii_tolower (conf[i]);
545
546                 /* return buffer */
547                 result = g_strdup_printf ("PDF/A - %s%s", part, conf);
548         }
549
550         /* Cleanup */
551         xmlFree (part);
552         xmlFree (conf);
553         xmlXPathFreeContext (xpathCtx);
554         xmlFreeDoc (doc);
555
556         return result;
557 }
558
559
560 static EvDocumentInfo *
561 pdf_document_get_info (EvDocument *document)
562 {
563         EvDocumentInfo *info;
564         PopplerPageLayout layout;
565         PopplerPageMode mode;
566         PopplerViewerPreferences view_prefs;
567         PopplerPermissions permissions;
568         EvPage *page;
569         char *metadata;
570         char *fmt;
571
572         info = g_new0 (EvDocumentInfo, 1);
573
574         info->fields_mask = EV_DOCUMENT_INFO_TITLE |
575                             EV_DOCUMENT_INFO_FORMAT |
576                             EV_DOCUMENT_INFO_AUTHOR |
577                             EV_DOCUMENT_INFO_SUBJECT |
578                             EV_DOCUMENT_INFO_KEYWORDS |
579                             EV_DOCUMENT_INFO_LAYOUT |
580                             EV_DOCUMENT_INFO_START_MODE |
581                             EV_DOCUMENT_INFO_PERMISSIONS |
582                             EV_DOCUMENT_INFO_UI_HINTS |
583                             EV_DOCUMENT_INFO_CREATOR |
584                             EV_DOCUMENT_INFO_PRODUCER |
585                             EV_DOCUMENT_INFO_CREATION_DATE |
586                             EV_DOCUMENT_INFO_MOD_DATE |
587                             EV_DOCUMENT_INFO_LINEARIZED |
588                             EV_DOCUMENT_INFO_N_PAGES |
589                             EV_DOCUMENT_INFO_SECURITY | 
590                             EV_DOCUMENT_INFO_PAPER_SIZE;
591
592         g_object_get (PDF_DOCUMENT (document)->document,
593                       "title", &(info->title),
594                       "format", &(info->format),
595                       "author", &(info->author),
596                       "subject", &(info->subject),
597                       "keywords", &(info->keywords),
598                       "page-mode", &mode,
599                       "page-layout", &layout,
600                       "viewer-preferences", &view_prefs,
601                       "permissions", &permissions,
602                       "creator", &(info->creator),
603                       "producer", &(info->producer),
604                       "creation-date", &(info->creation_date),
605                       "mod-date", &(info->modified_date),
606                       "linearized", &(info->linearized),
607                       "metadata", &metadata,
608                       NULL);
609
610         if (metadata != NULL) {
611                 fmt = pdf_document_get_format_from_metadata(metadata);
612                 if (fmt != NULL) {
613                         g_free (info->format);
614                         info->format = fmt;
615                 }
616                 g_free (metadata);
617         }
618
619         info->n_pages = ev_document_get_n_pages (document);
620
621         if (info->n_pages > 0) {
622                 page = ev_document_get_page (document, 0);
623                 ev_document_get_page_size (document, page,
624                                            &(info->paper_width),
625                                            &(info->paper_height));
626                 g_object_unref (page);
627                 
628
629                 // Convert to mm.
630                 info->paper_width = info->paper_width / 72.0f * 25.4f;
631                 info->paper_height = info->paper_height / 72.0f * 25.4f;
632         }
633
634         switch (layout) {
635                 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
636                         info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
637                         break;
638                 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
639                         info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
640                         break;
641                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
642                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
643                         break;
644                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
645                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
646                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
647                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
648                         break;
649                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
650                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
651                         break;
652                 default:
653                         break;
654         }
655
656         switch (mode) {
657                 case POPPLER_PAGE_MODE_NONE:
658                         info->mode = EV_DOCUMENT_MODE_NONE;
659                         break;
660                 case POPPLER_PAGE_MODE_USE_THUMBS:
661                         info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
662                         break;
663                 case POPPLER_PAGE_MODE_USE_OC:
664                         info->mode = EV_DOCUMENT_MODE_USE_OC;
665                         break;
666                 case POPPLER_PAGE_MODE_FULL_SCREEN:
667                         info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
668                         break;
669                 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
670                         info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
671                 default:
672                         break;
673         }
674
675         info->ui_hints = 0;
676         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
677                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
678         }
679         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
680                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
681         }
682         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
683                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
684         }
685         if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
686                 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
687         }
688         if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
689                 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
690         }
691         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
692                 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
693         }
694         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
695                 info->ui_hints |=  EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
696         }
697
698         info->permissions = 0;
699         if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
700                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
701         }
702         if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
703                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
704         }
705         if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
706                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
707         }
708         if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
709                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
710         }
711
712         if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
713                 /* translators: this is the document security state */
714                 info->security = g_strdup (_("Yes"));
715         } else {
716                 /* translators: this is the document security state */
717                 info->security = g_strdup (_("No"));
718         }
719
720         return info;
721 }
722
723 static void
724 pdf_document_document_iface_init (EvDocumentIface *iface)
725 {
726         iface->save = pdf_document_save;
727         iface->load = pdf_document_load;
728         iface->get_n_pages = pdf_document_get_n_pages;
729         iface->get_page = pdf_document_get_page;
730         iface->get_page_size = pdf_document_get_page_size;
731         iface->get_page_label = pdf_document_get_page_label;
732         iface->render = pdf_document_render;
733         iface->get_info = pdf_document_get_info;
734 };
735
736 static void
737 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
738 {
739         iface->has_document_security = pdf_document_has_document_security;
740         iface->set_password = pdf_document_set_password;
741 }
742
743 static gdouble
744 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
745 {
746         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
747         int n_pages;
748
749         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
750
751         return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
752 }
753
754 static gboolean
755 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
756                          int              n_pages)
757 {
758         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
759         gboolean result;
760
761         g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
762
763         if (pdf_document->font_info == NULL) { 
764                 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
765         }
766
767         if (pdf_document->fonts_iter) {
768                 poppler_fonts_iter_free (pdf_document->fonts_iter);
769         }
770
771         pdf_document->fonts_scanned_pages += n_pages;
772
773         result = poppler_font_info_scan (pdf_document->font_info, n_pages,
774                                          &pdf_document->fonts_iter);
775         if (!result) {
776                 pdf_document->fonts_scanned_pages = 0;
777                 poppler_font_info_free (pdf_document->font_info);
778                 pdf_document->font_info = NULL; 
779         }
780
781         return result;
782 }
783
784 static const char *
785 font_type_to_string (PopplerFontType type)
786 {
787         switch (type) {
788                 case POPPLER_FONT_TYPE_TYPE1:
789                         return _("Type 1");
790                 case POPPLER_FONT_TYPE_TYPE1C:
791                         return _("Type 1C");
792                 case POPPLER_FONT_TYPE_TYPE3:
793                         return _("Type 3");
794                 case POPPLER_FONT_TYPE_TRUETYPE:
795                         return _("TrueType");
796                 case POPPLER_FONT_TYPE_CID_TYPE0:
797                         return _("Type 1 (CID)");
798                 case POPPLER_FONT_TYPE_CID_TYPE0C:
799                         return _("Type 1C (CID)");
800                 case POPPLER_FONT_TYPE_CID_TYPE2:
801                         return _("TrueType (CID)");
802                 default:
803                         return _("Unknown font type");
804         }
805 }
806
807 static void
808 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
809                                GtkTreeModel    *model)
810 {
811         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
812         PopplerFontsIter *iter = pdf_document->fonts_iter;
813
814         g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
815
816         if (!iter)
817                 return;
818
819         do {
820                 GtkTreeIter list_iter;
821                 const char *name;
822                 const char *type;
823                 const char *embedded;
824                 char *details;
825                 
826                 name = poppler_fonts_iter_get_name (iter);
827
828                 if (name == NULL) {
829                         name = _("No name");
830                 }
831
832                 type = font_type_to_string (
833                         poppler_fonts_iter_get_font_type (iter));
834
835                 if (poppler_fonts_iter_is_embedded (iter)) {
836                         if (poppler_fonts_iter_is_subset (iter))
837                                 embedded = _("Embedded subset");
838                         else
839                                 embedded = _("Embedded");
840                 } else {
841                         embedded = _("Not embedded");
842                 }
843
844                 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
845
846                 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
847                 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
848                                     EV_DOCUMENT_FONTS_COLUMN_NAME, name,
849                                     EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
850                                     -1);
851
852                 g_free (details);
853         } while (poppler_fonts_iter_next (iter));
854 }
855
856 static void
857 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
858 {
859         iface->fill_model = pdf_document_fonts_fill_model;
860         iface->scan = pdf_document_fonts_scan;
861         iface->get_progress = pdf_document_fonts_get_progress;
862 }
863
864 static gboolean
865 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
866 {
867         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
868         PopplerIndexIter *iter;
869
870         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
871
872         iter = poppler_index_iter_new (pdf_document->document);
873         if (iter == NULL)
874                 return FALSE;
875         poppler_index_iter_free (iter);
876
877         return TRUE;
878 }
879
880 static EvLinkDest *
881 ev_link_dest_from_dest (PdfDocument *pdf_document,
882                         PopplerDest *dest)
883 {
884         EvLinkDest *ev_dest = NULL;
885         const char *unimplemented_dest = NULL;
886
887         g_assert (dest != NULL);
888
889         switch (dest->type) {
890                 case POPPLER_DEST_XYZ: {
891                         PopplerPage *poppler_page;
892                         double height;
893
894                         poppler_page = poppler_document_get_page (pdf_document->document,
895                                                                   MAX (0, dest->page_num - 1));
896                         poppler_page_get_size (poppler_page, NULL, &height);
897                         ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
898                                                         dest->left,
899                                                         height - MIN (height, dest->top),
900                                                         dest->zoom,
901                                                         dest->change_left,
902                                                         dest->change_top,
903                                                         dest->change_zoom);
904                         g_object_unref (poppler_page);
905                 }
906                         break;
907                 case POPPLER_DEST_FITB:
908                 case POPPLER_DEST_FIT:
909                         ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
910                         break;
911                 case POPPLER_DEST_FITBH:
912                 case POPPLER_DEST_FITH: {
913                         PopplerPage *poppler_page;
914                         double height;
915
916                         poppler_page = poppler_document_get_page (pdf_document->document,
917                                                                   MAX (0, dest->page_num - 1));
918                         poppler_page_get_size (poppler_page, NULL, &height);
919                         ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
920                                                          height - MIN (height, dest->top),
921                                                          dest->change_top);
922                         g_object_unref (poppler_page);
923                 }
924                         break;
925                 case POPPLER_DEST_FITBV:
926                 case POPPLER_DEST_FITV:
927                         ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
928                                                          dest->left,
929                                                          dest->change_left);
930                         break;
931                 case POPPLER_DEST_FITR: {
932                         PopplerPage *poppler_page;
933                         double height;
934
935                         poppler_page = poppler_document_get_page (pdf_document->document,
936                                                                   MAX (0, dest->page_num - 1));
937                         poppler_page_get_size (poppler_page, NULL, &height);
938                         ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
939                                                          dest->left,
940                                                          height - MIN (height, dest->bottom),
941                                                          dest->right,
942                                                          height - MIN (height, dest->top));
943                         g_object_unref (poppler_page);
944                 }
945                         break;
946                 case POPPLER_DEST_NAMED:
947                         ev_dest = ev_link_dest_new_named (dest->named_dest);
948                         break;
949                 case POPPLER_DEST_UNKNOWN:
950                         unimplemented_dest = "POPPLER_DEST_UNKNOWN";
951                         break;
952         }
953
954         if (unimplemented_dest) {
955                 g_warning ("Unimplemented destination: %s, please post a "
956                            "bug report in Evince bugzilla "
957                            "(http://bugzilla.gnome.org) with a testcase.",
958                            unimplemented_dest);
959         }
960
961         if (!ev_dest)
962                 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
963         
964         return ev_dest;
965 }
966
967 static EvLink *
968 ev_link_from_action (PdfDocument   *pdf_document,
969                      PopplerAction *action)
970 {
971         EvLink       *link = NULL;
972         EvLinkAction *ev_action = NULL;
973         const char   *unimplemented_action = NULL;
974
975         switch (action->type) {
976                 case POPPLER_ACTION_NONE:
977                         break;
978                 case POPPLER_ACTION_GOTO_DEST: {
979                         EvLinkDest *dest;
980                         
981                         dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
982                         ev_action = ev_link_action_new_dest (dest);
983                 }
984                         break;
985                 case POPPLER_ACTION_GOTO_REMOTE: {
986                         EvLinkDest *dest;
987                         
988                         dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
989                         ev_action = ev_link_action_new_remote (dest, 
990                                                                action->goto_remote.file_name);
991                         
992                 }
993                         break;
994                 case POPPLER_ACTION_LAUNCH:
995                         ev_action = ev_link_action_new_launch (action->launch.file_name,
996                                                                action->launch.params);
997                         break;
998                 case POPPLER_ACTION_URI:
999                         ev_action = ev_link_action_new_external_uri (action->uri.uri);
1000                         break;
1001                 case POPPLER_ACTION_NAMED:
1002                         ev_action = ev_link_action_new_named (action->named.named_dest);
1003                         break;
1004                 case POPPLER_ACTION_MOVIE:
1005                         unimplemented_action = "POPPLER_ACTION_MOVIE";
1006                         break;
1007                 case POPPLER_ACTION_UNKNOWN:
1008                         unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1009         }
1010         
1011         if (unimplemented_action) {
1012                 g_warning ("Unimplemented action: %s, please post a bug report "
1013                            "in Evince bugzilla (http://bugzilla.gnome.org) "
1014                            "with a testcase.", unimplemented_action);
1015         }
1016         
1017         link = ev_link_new (action->any.title, ev_action);
1018         
1019         return link;    
1020 }
1021
1022 static void
1023 build_tree (PdfDocument      *pdf_document,
1024             GtkTreeModel     *model,
1025             GtkTreeIter      *parent,
1026             PopplerIndexIter *iter)
1027 {
1028         
1029         do {
1030                 GtkTreeIter tree_iter;
1031                 PopplerIndexIter *child;
1032                 PopplerAction *action;
1033                 EvLink *link = NULL;
1034                 gboolean expand;
1035                 char *title_markup;
1036                 
1037                 action = poppler_index_iter_get_action (iter);
1038                 expand = poppler_index_iter_is_open (iter);
1039
1040                 if (!action)
1041                         continue;
1042
1043                 switch (action->type) {
1044                         case POPPLER_ACTION_GOTO_DEST: {
1045                                 /* For bookmarks, solve named destinations */
1046                                 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1047                                         PopplerDest *dest;
1048                                         EvLinkDest *ev_dest = NULL;
1049                                         EvLinkAction *ev_action;
1050                                         
1051                                         dest = poppler_document_find_dest (pdf_document->document,
1052                                                                            action->goto_dest.dest->named_dest);
1053                                         if (!dest) {
1054                                                 link = ev_link_from_action (pdf_document, action);
1055                                                 break;
1056                                         }
1057                                         
1058                                         ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1059                                         poppler_dest_free (dest);
1060                                         
1061                                         ev_action = ev_link_action_new_dest (ev_dest);
1062                                         link = ev_link_new (action->any.title, ev_action);
1063                                 } else {
1064                                         link = ev_link_from_action (pdf_document, action);
1065                                 }
1066                         }
1067                                 break;
1068                         default:
1069                                 link = ev_link_from_action (pdf_document, action);
1070                                 break;
1071                 }
1072                 
1073                 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1074                         poppler_action_free (action);
1075                         if (link)
1076                                 g_object_unref (link);
1077                         
1078                         continue;
1079                 }
1080
1081                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1082                 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1083                 
1084                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1085                                     EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1086                                     EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1087                                     EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1088                                     -1);
1089                 
1090                 g_free (title_markup);
1091                 g_object_unref (link);
1092                 
1093                 child = poppler_index_iter_get_child (iter);
1094                 if (child)
1095                         build_tree (pdf_document, model, &tree_iter, child);
1096                 poppler_index_iter_free (child);
1097                 poppler_action_free (action);
1098                 
1099         } while (poppler_index_iter_next (iter));
1100 }
1101
1102 static GtkTreeModel *
1103 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1104 {
1105         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1106         GtkTreeModel *model = NULL;
1107         PopplerIndexIter *iter;
1108         
1109         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1110
1111         iter = poppler_index_iter_new (pdf_document->document);
1112         /* Create the model if we have items*/
1113         if (iter != NULL) {
1114                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1115                                                              G_TYPE_STRING,
1116                                                              G_TYPE_OBJECT,
1117                                                              G_TYPE_BOOLEAN,
1118                                                              G_TYPE_STRING);
1119                 build_tree (pdf_document, model, NULL, iter);
1120                 poppler_index_iter_free (iter);
1121         }
1122         
1123         return model;
1124 }
1125
1126 static GList *
1127 pdf_document_links_get_links (EvDocumentLinks *document_links,
1128                               gint             page)
1129 {
1130         PdfDocument *pdf_document;
1131         PopplerPage *poppler_page;
1132         GList *retval = NULL;
1133         GList *mapping_list;
1134         GList *list;
1135         double height;
1136
1137         pdf_document = PDF_DOCUMENT (document_links);
1138         poppler_page = poppler_document_get_page (pdf_document->document,
1139                                                   page);
1140         mapping_list = poppler_page_get_link_mapping (poppler_page);
1141         poppler_page_get_size (poppler_page, NULL, &height);
1142
1143         for (list = mapping_list; list; list = list->next) {
1144                 PopplerLinkMapping *link_mapping;
1145                 EvMapping *ev_link_mapping;
1146
1147                 link_mapping = (PopplerLinkMapping *)list->data;
1148                 ev_link_mapping = g_new (EvMapping, 1);
1149                 ev_link_mapping->data = ev_link_from_action (pdf_document,
1150                                                              link_mapping->action);
1151                 ev_link_mapping->area.x1 = link_mapping->area.x1;
1152                 ev_link_mapping->area.x2 = link_mapping->area.x2;
1153                 /* Invert this for X-style coordinates */
1154                 ev_link_mapping->area.y1 = height - link_mapping->area.y2;
1155                 ev_link_mapping->area.y2 = height - link_mapping->area.y1;
1156
1157                 retval = g_list_prepend (retval, ev_link_mapping);
1158         }
1159
1160         poppler_page_free_link_mapping (mapping_list);
1161         g_object_unref (poppler_page);
1162
1163         return g_list_reverse (retval);
1164 }
1165
1166 static EvLinkDest *
1167 pdf_document_links_find_link_dest (EvDocumentLinks  *document_links,
1168                                    const gchar      *link_name)
1169 {
1170         PdfDocument *pdf_document;
1171         PopplerDest *dest;
1172         EvLinkDest *ev_dest = NULL;
1173
1174         pdf_document = PDF_DOCUMENT (document_links);
1175         dest = poppler_document_find_dest (pdf_document->document,
1176                                            link_name);
1177         if (dest) {
1178                 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1179                 poppler_dest_free (dest);
1180         }
1181
1182         return ev_dest;
1183 }
1184
1185 static void
1186 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1187 {
1188         iface->has_document_links = pdf_document_links_has_document_links;
1189         iface->get_links_model = pdf_document_links_get_links_model;
1190         iface->get_links = pdf_document_links_get_links;
1191         iface->find_link_dest = pdf_document_links_find_link_dest;
1192 }
1193
1194 static GList *
1195 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1196                                        gint              page)
1197 {
1198         GList *retval = NULL;
1199         PdfDocument *pdf_document;
1200         PopplerPage *poppler_page;
1201         GList *mapping_list;
1202         GList *list;
1203
1204         pdf_document = PDF_DOCUMENT (document_images);
1205         poppler_page = poppler_document_get_page (pdf_document->document, page);
1206         mapping_list = poppler_page_get_image_mapping (poppler_page);
1207
1208         for (list = mapping_list; list; list = list->next) {
1209                 PopplerImageMapping *image_mapping;
1210                 EvMapping *ev_image_mapping;
1211
1212                 image_mapping = (PopplerImageMapping *)list->data;
1213
1214                 ev_image_mapping = g_new (EvMapping, 1);
1215                 
1216                 ev_image_mapping->data = ev_image_new (page, image_mapping->image_id);
1217                 ev_image_mapping->area.x1 = image_mapping->area.x1;
1218                 ev_image_mapping->area.y1 = image_mapping->area.y1;
1219                 ev_image_mapping->area.x2 = image_mapping->area.x2;
1220                 ev_image_mapping->area.y2 = image_mapping->area.y2;
1221
1222                 retval = g_list_prepend (retval, ev_image_mapping);
1223         }
1224
1225         poppler_page_free_image_mapping (mapping_list);
1226         g_object_unref (poppler_page);
1227
1228         return g_list_reverse (retval);
1229 }
1230
1231 GdkPixbuf *
1232 pdf_document_images_get_image (EvDocumentImages *document_images,
1233                                EvImage          *image)
1234 {
1235         GdkPixbuf       *retval = NULL;
1236 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1237         PdfDocument     *pdf_document;
1238         PopplerPage     *poppler_page;
1239         cairo_surface_t *surface;
1240
1241         pdf_document = PDF_DOCUMENT (document_images);
1242         poppler_page = poppler_document_get_page (pdf_document->document,
1243                                                   ev_image_get_page (image));
1244
1245         surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1246         if (surface) {
1247                 retval = ev_document_misc_pixbuf_from_surface (surface);
1248                 cairo_surface_destroy (surface);
1249         }
1250
1251         g_object_unref (poppler_page);
1252 #endif
1253         return retval;
1254 }
1255
1256 static void
1257 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1258 {
1259         iface->get_image_mapping = pdf_document_images_get_image_mapping;
1260         iface->get_image = pdf_document_images_get_image;
1261 }
1262
1263 static GdkPixbuf *
1264 make_thumbnail_for_page (PopplerPage     *poppler_page,
1265                          EvRenderContext *rc,
1266                          gint             width,
1267                          gint             height)
1268 {
1269         GdkPixbuf *pixbuf;
1270
1271 #ifdef POPPLER_WITH_GDK
1272         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1273                                  width, height);
1274         gdk_pixbuf_fill (pixbuf, 0xffffffff);
1275
1276         ev_document_fc_mutex_lock ();
1277         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1278                                        width, height,
1279                                        rc->scale, rc->rotation, pixbuf);
1280         ev_document_fc_mutex_unlock ();
1281 #else
1282         cairo_surface_t *surface;
1283
1284         ev_document_fc_mutex_lock ();
1285         surface = pdf_page_render (poppler_page, width, height, rc);
1286         ev_document_fc_mutex_unlock ();
1287         
1288         pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1289         cairo_surface_destroy (surface);
1290 #endif /* POPPLER_WITH_GDK */
1291
1292         return pixbuf;
1293 }
1294
1295 static GdkPixbuf *
1296 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1297                                        EvRenderContext      *rc, 
1298                                        gboolean              border)
1299 {
1300         PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1301         PopplerPage *poppler_page;
1302         GdkPixbuf *pixbuf = NULL;
1303         GdkPixbuf *border_pixbuf;
1304         gint width, height;
1305
1306         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1307
1308         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1309                                                 rc, &width, &height);
1310         
1311 #ifdef POPPLER_WITH_GDK
1312         pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1313 #else
1314         cairo_surface_t *surface;
1315         
1316         surface = poppler_page_get_thumbnail (poppler_page);
1317         if (surface) {
1318                 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1319                 cairo_surface_destroy (surface);
1320         }
1321 #endif /* POPPLER_WITH_GDK */
1322
1323         if (pixbuf != NULL) {
1324                 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1325                         gdk_pixbuf_get_height (pixbuf) :
1326                         gdk_pixbuf_get_width (pixbuf);
1327
1328                 if (thumb_width == width) {
1329                         GdkPixbuf *rotated_pixbuf;
1330
1331                         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1332                                                                    (GdkPixbufRotation) (360 - rc->rotation));
1333                         g_object_unref (pixbuf);
1334                         pixbuf = rotated_pixbuf;
1335                 } else {
1336                         /* The provided thumbnail has a different size */
1337                         g_object_unref (pixbuf);
1338                         pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1339                 }
1340         } else {
1341                 /* There is no provided thumbnail. We need to make one. */
1342                 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1343         }
1344
1345         if (border && pixbuf) {
1346                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1347                 g_object_unref (pixbuf);
1348                 pixbuf = border_pixbuf;
1349         }               
1350
1351         return pixbuf;
1352 }
1353
1354 static void
1355 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1356                                         EvRenderContext      *rc,
1357                                         gint                 *width,
1358                                         gint                 *height)
1359 {
1360         double page_width, page_height;
1361         
1362         poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1363                                &page_width, &page_height);
1364         
1365         *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1366         *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1367
1368         if (rc->rotation == 90 || rc->rotation == 270) {
1369                 gint  temp;
1370
1371                 temp = *width;
1372                 *width = *height;
1373                 *height = temp;
1374         }
1375 }
1376
1377 static void
1378 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1379 {
1380         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1381         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1382 }
1383
1384
1385 static GList *
1386 pdf_document_find_find_text (EvDocumentFind *document_find,
1387                              EvPage         *page,
1388                              const gchar    *text,
1389                              gboolean        case_sensitive)
1390 {
1391         GList *matches, *l;
1392         PopplerPage *poppler_page;
1393         gdouble height;
1394         
1395         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1396         g_return_val_if_fail (text != NULL, NULL);
1397
1398         poppler_page = POPPLER_PAGE (page->backend_page);
1399         
1400         matches = poppler_page_find_text (poppler_page, text);
1401         if (!matches)
1402                 return NULL;
1403
1404         poppler_page_get_size (poppler_page, NULL, &height);
1405         for (l = matches; l && l->data; l = g_list_next (l)) {
1406                 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1407                 gdouble           tmp;
1408
1409                 tmp = rect->y1;
1410                 rect->y1 = height - rect->y2;
1411                 rect->y2 = height - tmp;
1412         }
1413         
1414         return matches;
1415 }
1416
1417 static void
1418 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1419 {
1420         iface->find_text = pdf_document_find_find_text;
1421 }
1422
1423 static void
1424 pdf_print_context_free (PdfPrintContext *ctx)
1425 {
1426         if (!ctx)
1427                 return;
1428
1429 #ifdef HAVE_CAIRO_PRINT
1430         if (ctx->cr) {
1431                 cairo_destroy (ctx->cr);
1432                 ctx->cr = NULL;
1433         }
1434 #else
1435         if (ctx->ps_file) {
1436                 poppler_ps_file_free (ctx->ps_file);
1437                 ctx->ps_file = NULL;
1438         }
1439 #endif
1440         g_free (ctx);
1441 }
1442
1443 static void
1444 pdf_document_file_exporter_begin (EvFileExporter        *exporter,
1445                                   EvFileExporterContext *fc)
1446 {
1447         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1448         PdfPrintContext *ctx;
1449 #ifdef HAVE_CAIRO_PRINT
1450         gdouble width, height;
1451         cairo_surface_t *surface = NULL;
1452 #endif
1453         
1454         if (pdf_document->print_ctx)
1455                 pdf_print_context_free (pdf_document->print_ctx);
1456         pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1457         ctx = pdf_document->print_ctx;
1458         ctx->format = fc->format;
1459         
1460 #ifdef HAVE_CAIRO_PRINT
1461         ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1462
1463         ctx->paper_width = fc->paper_width;
1464         ctx->paper_height = fc->paper_height;
1465         
1466         switch (fc->pages_per_sheet) {
1467                 default:
1468                 case 1:
1469                         ctx->pages_x = 1;
1470                         ctx->pages_y = 1;
1471                         break;
1472                 case 2:
1473                         ctx->pages_x = 1;
1474                         ctx->pages_y = 2;
1475                         break;
1476                 case 4:
1477                         ctx->pages_x = 2;
1478                         ctx->pages_y = 2;
1479                         break;
1480                 case 6:
1481                         ctx->pages_x = 2;
1482                         ctx->pages_y = 3;
1483                         break;
1484                 case 9:
1485                         ctx->pages_x = 3;
1486                         ctx->pages_y = 3;
1487                         break;
1488                 case 16:
1489                         ctx->pages_x = 4;
1490                         ctx->pages_y = 4;
1491                         break;
1492         }
1493
1494         ctx->pages_printed = 0;
1495         
1496         switch (fc->format) {
1497                 case EV_FILE_FORMAT_PS:
1498 #ifdef HAVE_CAIRO_PS
1499                         surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1500 #endif
1501                         break;
1502                 case EV_FILE_FORMAT_PDF:
1503 #ifdef HAVE_CAIRO_PDF
1504                         surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1505 #endif
1506                         break;
1507                 default:
1508                         g_assert_not_reached ();
1509         }
1510
1511         ctx->cr = cairo_create (surface);
1512         cairo_surface_destroy (surface);
1513
1514 #else /* HAVE_CAIRO_PRINT */
1515         if (ctx->format == EV_FILE_FORMAT_PS) {
1516                 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1517                                                     fc->filename, fc->first_page,
1518                                                     fc->last_page - fc->first_page + 1);
1519                 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1520                 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1521         }
1522 #endif /* HAVE_CAIRO_PRINT */
1523 }
1524
1525 static void
1526 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1527 {
1528         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1529         PdfPrintContext *ctx = pdf_document->print_ctx;
1530         
1531         g_return_if_fail (pdf_document->print_ctx != NULL);
1532
1533         ctx->pages_printed = 0;
1534         
1535 #ifdef HAVE_CAIRO_PRINT
1536         if (ctx->paper_width > ctx->paper_height) {
1537                 if (ctx->format == EV_FILE_FORMAT_PS) {
1538                         cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1539                                                    ctx->paper_height,
1540                                                    ctx->paper_width);
1541                 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1542                         cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1543                                                     ctx->paper_height,
1544                                                     ctx->paper_width);
1545                 }
1546         }
1547 #endif /* HAVE_CAIRO_PRINT */
1548 }
1549
1550 static void
1551 pdf_document_file_exporter_do_page (EvFileExporter  *exporter,
1552                                     EvRenderContext *rc)
1553 {
1554         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1555         PdfPrintContext *ctx = pdf_document->print_ctx;
1556         PopplerPage *poppler_page;
1557 #ifdef HAVE_CAIRO_PRINT
1558         gdouble  page_width, page_height;
1559         gint     x, y;
1560         gboolean rotate;
1561         gdouble  width, height;
1562         gdouble  pwidth, pheight;
1563         gdouble  xscale, yscale;
1564 #endif
1565
1566         g_return_if_fail (pdf_document->print_ctx != NULL);
1567
1568         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1569         
1570 #ifdef HAVE_CAIRO_PRINT
1571         x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1572         y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1573         poppler_page_get_size (poppler_page, &page_width, &page_height);
1574
1575         if (page_width > page_height && page_width > ctx->paper_width) {
1576                 rotate = TRUE;
1577         } else {
1578                 rotate = FALSE;
1579         }
1580
1581         /* Use always portrait mode and rotate when necessary */
1582         if (ctx->paper_width > ctx->paper_height) {
1583                 width = ctx->paper_height;
1584                 height = ctx->paper_width;
1585                 rotate = !rotate;
1586         } else {
1587                 width = ctx->paper_width;
1588                 height = ctx->paper_height;
1589         }
1590
1591         if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1592                 rotate = !rotate;
1593         }       
1594
1595         if (rotate) {
1596                 gint tmp1;
1597                 gdouble tmp2;
1598
1599                 tmp1 = x;
1600                 x = y;
1601                 y = tmp1;
1602
1603                 tmp2 = page_width;
1604                 page_width = page_height;
1605                 page_height = tmp2;
1606         }
1607
1608         pwidth = width / ctx->pages_x;
1609         pheight = height / ctx->pages_y;
1610
1611         if ((page_width > pwidth || page_height > pheight) ||
1612             (page_width < pwidth && page_height < pheight)) {
1613                 xscale = pwidth / page_width;
1614                 yscale = pheight / page_height;
1615                 
1616                 if (yscale < xscale) {
1617                         xscale = yscale;
1618                 } else {
1619                         yscale = xscale;
1620                 }
1621                 
1622         } else {        
1623                 xscale = yscale = 1;
1624         }
1625
1626         /* TODO: center */
1627
1628         cairo_save (ctx->cr);
1629         if (rotate) {
1630                 cairo_matrix_t matrix;
1631                 
1632                 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1633                 cairo_matrix_init (&matrix,
1634                                    0,  1,
1635                                    -1,  0,
1636                                    0,  0);
1637                 cairo_transform (ctx->cr, &matrix);
1638         }
1639         
1640         cairo_translate (ctx->cr,
1641                          x * (rotate ? pheight : pwidth),
1642                          y * (rotate ? pwidth : pheight));
1643         cairo_scale (ctx->cr, xscale, yscale);
1644
1645         poppler_page_render_for_printing (poppler_page, ctx->cr);
1646
1647         ctx->pages_printed++;
1648                         
1649         cairo_restore (ctx->cr);
1650 #else /* HAVE_CAIRO_PRINT */
1651         if (ctx->format == EV_FILE_FORMAT_PS)
1652                 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1653 #endif /* HAVE_CAIRO_PRINT */
1654 }
1655
1656 static void
1657 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1658 {
1659         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1660         PdfPrintContext *ctx = pdf_document->print_ctx;
1661         
1662         g_return_if_fail (pdf_document->print_ctx != NULL);
1663
1664 #ifdef HAVE_CAIRO_PRINT
1665         cairo_show_page (ctx->cr);
1666 #endif
1667 }
1668
1669 static void
1670 pdf_document_file_exporter_end (EvFileExporter *exporter)
1671 {
1672         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1673
1674         pdf_print_context_free (pdf_document->print_ctx);
1675         pdf_document->print_ctx = NULL;
1676 }
1677
1678 static EvFileExporterCapabilities
1679 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1680 {
1681         return  (EvFileExporterCapabilities) (
1682                 EV_FILE_EXPORTER_CAN_PAGE_SET |
1683                 EV_FILE_EXPORTER_CAN_COPIES |
1684                 EV_FILE_EXPORTER_CAN_COLLATE |
1685                 EV_FILE_EXPORTER_CAN_REVERSE |
1686                 EV_FILE_EXPORTER_CAN_SCALE |
1687 #ifdef HAVE_CAIRO_PRINT
1688 #ifdef HAVE_POPPLER_PAGE_RENDER
1689                 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1690 #endif
1691 #endif
1692                 
1693 #ifdef HAVE_CAIRO_PDF
1694 #ifdef HAVE_POPPLER_PAGE_RENDER
1695                 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1696 #endif
1697 #endif
1698                 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1699 }
1700
1701 static void
1702 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1703 {
1704         iface->begin = pdf_document_file_exporter_begin;
1705         iface->begin_page = pdf_document_file_exporter_begin_page;
1706         iface->do_page = pdf_document_file_exporter_do_page;
1707         iface->end_page = pdf_document_file_exporter_end_page;
1708         iface->end = pdf_document_file_exporter_end;
1709         iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1710 }
1711
1712 #ifdef HAVE_POPPLER_PAGE_RENDER
1713 /* EvDocumentPrint */
1714 static void
1715 pdf_document_print_print_page (EvDocumentPrint *document,
1716                                EvPage          *page,
1717                                cairo_t         *cr)
1718 {
1719         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1720
1721         poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1722 }
1723
1724 static void
1725 pdf_document_document_print_iface_init (EvDocumentPrintIface *iface)
1726 {
1727         iface->print_page = pdf_document_print_print_page;
1728 }
1729 #endif /* HAVE_POPPLER_PAGE_RENDER */
1730
1731 static void
1732 pdf_selection_render_selection (EvSelection      *selection,
1733                                 EvRenderContext  *rc,
1734                                 cairo_surface_t **surface,
1735                                 EvRectangle      *points,
1736                                 EvRectangle      *old_points,
1737                                 EvSelectionStyle  style,
1738                                 GdkColor         *text,
1739                                 GdkColor         *base)
1740 {
1741         PopplerPage *poppler_page;
1742         double width_points, height_points;
1743         gint width, height;
1744
1745         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1746
1747         poppler_page_get_size (poppler_page,
1748                                &width_points, &height_points);
1749         width = (int) ((width_points * rc->scale) + 0.5);
1750         height = (int) ((height_points * rc->scale) + 0.5);
1751
1752 #ifdef HAVE_POPPLER_PAGE_RENDER
1753         cairo_t *cr;
1754         PopplerColor text_color, base_color;
1755         
1756         text_color.red = text->red;
1757         text_color.green = text->green;
1758         text_color.blue = text->blue;
1759
1760         base_color.red = base->red;
1761         base_color.green = base->green;
1762         base_color.blue = base->blue;
1763
1764         if (*surface == NULL) {
1765                 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1766                                                        width, height);
1767                 
1768         }
1769
1770         cr = cairo_create (*surface);
1771         cairo_scale (cr, rc->scale, rc->scale);
1772         cairo_surface_set_device_offset (*surface, 0, 0);
1773         memset (cairo_image_surface_get_data (*surface), 0x00,
1774                 cairo_image_surface_get_height (*surface) *
1775                 cairo_image_surface_get_stride (*surface));
1776         poppler_page_render_selection (poppler_page,
1777                                        cr,
1778                                        (PopplerRectangle *)points,
1779                                        (PopplerRectangle *)old_points,
1780                                        (PopplerSelectionStyle)style,
1781                                        &text_color,
1782                                        &base_color);
1783         cairo_destroy (cr);
1784 #else /* HAVE_POPPLER_PAGE_RENDER */
1785         GdkPixbuf *pixbuf;
1786         
1787         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1788                                  TRUE, 8,
1789                                  width, height);
1790
1791         poppler_page_render_selection_to_pixbuf (poppler_page,
1792                                                  rc->scale, rc->rotation, pixbuf,
1793                                                  (PopplerRectangle *)points,
1794                                                  (PopplerRectangle *)old_points,
1795                                                  (PopplerSelectionStyle)style,
1796                                                  text,
1797                                                  base);
1798         if (*surface)
1799                 cairo_surface_destroy (*surface);
1800         *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1801         g_object_unref (pixbuf);
1802 #endif /* HAVE_POPPLER_PAGE_RENDER */
1803 }
1804
1805 static gchar *
1806 pdf_selection_get_selected_text (EvSelection     *selection,
1807                                  EvRenderContext *rc,
1808                                  EvSelectionStyle style,
1809                                  EvRectangle     *points)
1810 {
1811         PopplerPage *poppler_page;
1812         PopplerRectangle r;
1813         double height;
1814         char *retval;
1815         
1816         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1817
1818         poppler_page_get_size (poppler_page, NULL, &height);
1819         r.x1 = points->x1;
1820         r.y1 = height - points->y2;
1821         r.x2 = points->x2;
1822         r.y2 = height - points->y1;
1823
1824         retval = poppler_page_get_text (poppler_page,
1825                                         (PopplerSelectionStyle)style,
1826                                         &r);
1827
1828         return retval;
1829 }
1830
1831 static GdkRegion *
1832 create_gdk_region_from_poppler_region (GList *region)
1833 {
1834         GList *l;
1835         GdkRegion *retval;
1836         
1837         retval = gdk_region_new ();
1838         
1839         for (l = region; l; l = g_list_next (l)) {
1840                 PopplerRectangle *rectangle;
1841                 GdkRectangle      rect;
1842                 
1843                 rectangle = (PopplerRectangle *)l->data;
1844                 
1845                 rect.x = (gint) rectangle->x1;
1846                 rect.y = (gint) rectangle->y1;
1847                 rect.width  = (gint) (rectangle->x2 - rectangle->x1);
1848                 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1849                 gdk_region_union_with_rect (retval, &rect);
1850                 
1851                 poppler_rectangle_free (rectangle);
1852         }
1853
1854         return retval;
1855 }
1856
1857 static GdkRegion *
1858 pdf_selection_get_selection_region (EvSelection     *selection,
1859                                     EvRenderContext *rc,
1860                                     EvSelectionStyle style,
1861                                     EvRectangle     *points)
1862 {
1863         PopplerPage *poppler_page;
1864         GdkRegion   *retval;
1865         GList       *region;
1866
1867         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1868         
1869         region = poppler_page_get_selection_region (poppler_page,
1870                                                     rc->scale,
1871                                                     (PopplerSelectionStyle)style,
1872                                                     (PopplerRectangle *) points);
1873         retval = create_gdk_region_from_poppler_region (region);
1874         g_list_free (region);
1875         
1876         return retval;
1877 }
1878
1879 static GdkRegion *
1880 pdf_selection_get_selection_map (EvSelection     *selection,
1881                                  EvRenderContext *rc)
1882 {
1883         PopplerPage *poppler_page;
1884         PopplerRectangle points;
1885         GList *region;
1886         GdkRegion *retval;
1887
1888         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1889
1890         points.x1 = 0.0;
1891         points.y1 = 0.0;
1892         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1893         
1894         region = poppler_page_get_selection_region (poppler_page, 1.0,
1895                                                     POPPLER_SELECTION_GLYPH,
1896                                                     &points);
1897         retval = create_gdk_region_from_poppler_region (region);
1898         g_list_free (region);
1899
1900         return retval;
1901 }
1902
1903 static void
1904 pdf_selection_iface_init (EvSelectionIface *iface)
1905 {
1906         iface->render_selection = pdf_selection_render_selection;
1907         iface->get_selected_text = pdf_selection_get_selected_text;
1908         iface->get_selection_region = pdf_selection_get_selection_region;
1909         iface->get_selection_map = pdf_selection_get_selection_map;
1910 }
1911
1912 /* Page Transitions */
1913 static gdouble
1914 pdf_document_get_page_duration (EvDocumentTransition *trans,
1915                                 gint                  page)
1916 {
1917         PdfDocument *pdf_document;
1918         PopplerPage *poppler_page;
1919         gdouble      duration = -1;
1920
1921         pdf_document = PDF_DOCUMENT (trans);
1922         poppler_page = poppler_document_get_page (pdf_document->document, page);
1923         if (!poppler_page)
1924                 return -1;
1925
1926         duration = poppler_page_get_duration (poppler_page);
1927         g_object_unref (poppler_page);
1928
1929         return duration;
1930 }
1931
1932 static EvTransitionEffect *
1933 pdf_document_get_effect (EvDocumentTransition *trans,
1934                          gint                  page)
1935 {
1936         PdfDocument            *pdf_document;
1937         PopplerPage            *poppler_page;
1938         PopplerPageTransition  *page_transition;
1939         EvTransitionEffect     *effect;
1940
1941         pdf_document = PDF_DOCUMENT (trans);
1942         poppler_page = poppler_document_get_page (pdf_document->document, page);
1943
1944         if (!poppler_page)
1945                 return NULL;
1946
1947         page_transition = poppler_page_get_transition (poppler_page);
1948
1949         if (!page_transition) {
1950                 g_object_unref (poppler_page);
1951                 return NULL;
1952         }
1953
1954         /* enums in PopplerPageTransition match the EvTransitionEffect ones */
1955         effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
1956                                            "alignment", page_transition->alignment,
1957                                            "direction", page_transition->direction,
1958                                            "duration", page_transition->duration,
1959                                            "angle", page_transition->angle,
1960                                            "scale", page_transition->scale,
1961                                            "rectangular", page_transition->rectangular,
1962                                            NULL);
1963
1964         poppler_page_transition_free (page_transition);
1965         g_object_unref (poppler_page);
1966
1967         return effect;
1968 }
1969
1970 static void
1971 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1972 {
1973         iface->get_page_duration = pdf_document_get_page_duration;
1974         iface->get_effect = pdf_document_get_effect;
1975 }
1976
1977 /* Forms */
1978 static void
1979 pdf_document_get_crop_box (EvDocument  *document, 
1980                            int          page, 
1981                            EvRectangle *rect)
1982 {
1983         PdfDocument *pdf_document;
1984         PopplerPage *poppler_page;
1985         PopplerRectangle poppler_rect;
1986
1987         pdf_document = PDF_DOCUMENT (document);
1988         poppler_page = poppler_document_get_page (pdf_document->document, page);
1989         poppler_page_get_crop_box (poppler_page, &poppler_rect);
1990         rect->x1 = poppler_rect.x1;
1991         rect->x2 = poppler_rect.x2;
1992         rect->y1 = poppler_rect.y1;
1993         rect->y2 = poppler_rect.y2;
1994 }
1995
1996 static EvFormField *
1997 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1998 {
1999         EvFormField *ev_field = NULL;
2000         gint         id;
2001         gdouble      font_size;
2002         gboolean     is_read_only;
2003
2004         id = poppler_form_field_get_id (poppler_field);
2005         font_size = poppler_form_field_get_font_size (poppler_field);
2006         is_read_only = poppler_form_field_is_read_only (poppler_field);
2007
2008         switch (poppler_form_field_get_field_type (poppler_field)) {
2009                 case POPPLER_FORM_FIELD_TEXT: {
2010                         EvFormFieldText    *field_text;
2011                         EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2012
2013                         switch (poppler_form_field_text_get_text_type (poppler_field)) {
2014                                 case POPPLER_FORM_TEXT_NORMAL:
2015                                         ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2016                                         break;
2017                                 case POPPLER_FORM_TEXT_MULTILINE:
2018                                         ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2019                                         break;
2020                                 case POPPLER_FORM_TEXT_FILE_SELECT:
2021                                         ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2022                                         break;
2023                         }
2024                         
2025                         ev_field = ev_form_field_text_new (id, ev_text_type);
2026                         field_text = EV_FORM_FIELD_TEXT (ev_field);
2027
2028                         field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2029                         field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2030                         field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2031                         field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2032                         field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2033                         field_text->text = poppler_form_field_text_get_text (poppler_field);
2034
2035                 }
2036                         break;
2037                 case POPPLER_FORM_FIELD_BUTTON: {
2038                         EvFormFieldButton    *field_button;
2039                         EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2040
2041                         switch (poppler_form_field_button_get_button_type (poppler_field)) {
2042                                 case POPPLER_FORM_BUTTON_PUSH:
2043                                         ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2044                                         break;
2045                                 case POPPLER_FORM_BUTTON_CHECK:
2046                                         ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2047                                         break;
2048                                 case POPPLER_FORM_BUTTON_RADIO:
2049                                         ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2050                                         break;
2051                         }
2052
2053                         ev_field = ev_form_field_button_new (id, ev_button_type);
2054                         field_button = EV_FORM_FIELD_BUTTON (ev_field);
2055                         
2056                         field_button->state = poppler_form_field_button_get_state (poppler_field);
2057                 }
2058                         break;
2059                 case POPPLER_FORM_FIELD_CHOICE: {
2060                         EvFormFieldChoice    *field_choice;
2061                         EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2062
2063                         switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2064                                 case POPPLER_FORM_CHOICE_COMBO:
2065                                         ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2066                                         break;
2067                                 case EV_FORM_FIELD_CHOICE_LIST:
2068                                         ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2069                                         break;
2070                         }
2071
2072                         ev_field = ev_form_field_choice_new (id, ev_choice_type);
2073                         field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2074
2075                         field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2076                         field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2077                         field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2078                         field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2079
2080                         /* TODO: we need poppler_form_field_choice_get_selected_items in poppler 
2081                         field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2082                         if (field_choice->is_editable)
2083                                 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2084                 }
2085                         break;
2086                 case POPPLER_FORM_FIELD_SIGNATURE:
2087                         /* TODO */
2088                         ev_field = ev_form_field_signature_new (id);
2089                         break;
2090                 case POPPLER_FORM_FIELD_UNKNOWN:
2091                         return NULL;
2092         }
2093
2094         ev_field->font_size = font_size;
2095         ev_field->is_read_only = is_read_only;
2096
2097         return ev_field;
2098 }
2099
2100 static GList *
2101 pdf_document_forms_get_form_fields (EvDocumentForms *document, 
2102                                     EvPage          *page)
2103 {
2104         PopplerPage *poppler_page;
2105         GList *retval = NULL;
2106         GList *fields;
2107         GList *list;
2108         double height;
2109
2110         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2111         
2112         poppler_page = POPPLER_PAGE (page->backend_page);
2113         fields = poppler_page_get_form_field_mapping (poppler_page);
2114         poppler_page_get_size (poppler_page, NULL, &height);
2115
2116         for (list = fields; list; list = list->next) {
2117                 PopplerFormFieldMapping *mapping;
2118                 EvMapping *field_mapping;
2119                 EvFormField *ev_field;
2120
2121                 mapping = (PopplerFormFieldMapping *)list->data;
2122
2123                 ev_field = ev_form_field_from_poppler_field (mapping->field);
2124                 if (!ev_field)
2125                         continue;
2126
2127                 field_mapping = g_new0 (EvMapping, 1);
2128                 field_mapping->area.x1 = mapping->area.x1;
2129                 field_mapping->area.x2 = mapping->area.x2;
2130                 field_mapping->area.y1 = height - mapping->area.y2;
2131                 field_mapping->area.y2 = height - mapping->area.y1;
2132                 field_mapping->data = ev_field;
2133                 ev_field->page = EV_PAGE (g_object_ref (page));
2134
2135                 g_object_set_data_full (G_OBJECT (ev_field),
2136                                         "poppler-field",
2137                                         g_object_ref (mapping->field),
2138                                         (GDestroyNotify) g_object_unref);
2139                 
2140                 retval = g_list_prepend (retval, field_mapping);
2141         }
2142         
2143         poppler_page_free_form_field_mapping (fields);
2144
2145         return g_list_reverse (retval);
2146 }
2147
2148 static gchar *
2149 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2150                                              EvFormField     *field)
2151         
2152 {
2153         PopplerFormField *poppler_field;
2154         gchar *text;
2155
2156         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2157         if (!poppler_field)
2158                 return NULL;
2159         
2160         text = poppler_form_field_text_get_text (poppler_field);
2161
2162         return text;
2163 }
2164
2165 static void
2166 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document, 
2167                                              EvFormField     *field,
2168                                              const gchar     *text)
2169 {
2170         PopplerFormField *poppler_field;
2171
2172         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2173         if (!poppler_field)
2174                 return;
2175         
2176         poppler_form_field_text_set_text (poppler_field, text);
2177         PDF_DOCUMENT (document)->modified = TRUE;
2178 }
2179
2180 static void
2181 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document, 
2182                                                 EvFormField     *field,
2183                                                 gboolean         state)
2184 {
2185         PopplerFormField *poppler_field;
2186
2187         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2188         if (!poppler_field)
2189                 return;
2190         
2191         poppler_form_field_button_set_state (poppler_field, state);
2192         PDF_DOCUMENT (document)->modified = TRUE;
2193 }
2194
2195 static gboolean
2196 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document, 
2197                                                 EvFormField     *field)
2198 {
2199         PopplerFormField *poppler_field;
2200         gboolean state;
2201
2202         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2203         if (!poppler_field)
2204                 return FALSE;
2205
2206         state = poppler_form_field_button_get_state (poppler_field);
2207
2208         return state;
2209 }
2210
2211 static gchar *
2212 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document, 
2213                                                EvFormField     *field,
2214                                                gint             index)
2215 {
2216         PopplerFormField *poppler_field;
2217         gchar *text;
2218
2219         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2220         if (!poppler_field)
2221                 return NULL;
2222
2223         text = poppler_form_field_choice_get_item (poppler_field, index);
2224
2225         return text;
2226 }
2227
2228 static int
2229 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document, 
2230                                                   EvFormField     *field)
2231 {
2232         PopplerFormField *poppler_field;
2233         gint n_items;
2234
2235         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2236         if (!poppler_field)
2237                 return -1;
2238         
2239         n_items = poppler_form_field_choice_get_n_items (poppler_field);
2240
2241         return n_items;
2242 }
2243
2244 static gboolean
2245 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document, 
2246                                                        EvFormField     *field,
2247                                                        gint             index)
2248 {
2249         PopplerFormField *poppler_field;
2250         gboolean selected;
2251
2252         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2253         if (!poppler_field)
2254                 return FALSE;
2255
2256         selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2257
2258         return selected;
2259 }
2260
2261 static void
2262 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document, 
2263                                                   EvFormField     *field,
2264                                                   gint             index)
2265 {
2266         PopplerFormField *poppler_field;
2267
2268         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2269         if (!poppler_field)
2270                 return;
2271
2272         poppler_form_field_choice_select_item (poppler_field, index);
2273         PDF_DOCUMENT (document)->modified = TRUE;
2274 }
2275
2276 static void
2277 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document, 
2278                                                   EvFormField     *field,
2279                                                   gint             index)
2280 {
2281         PopplerFormField *poppler_field;
2282
2283         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2284         if (!poppler_field)
2285                 return;
2286
2287         poppler_form_field_choice_toggle_item (poppler_field, index);
2288         PDF_DOCUMENT (document)->modified = TRUE;
2289 }
2290
2291 static void
2292 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document, 
2293                                                    EvFormField     *field)
2294 {
2295         PopplerFormField *poppler_field;
2296
2297         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2298         if (!poppler_field)
2299                 return;
2300         
2301         poppler_form_field_choice_unselect_all (poppler_field);
2302         PDF_DOCUMENT (document)->modified = TRUE;
2303 }
2304
2305 static void
2306 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2307                                                EvFormField     *field,
2308                                                const gchar     *text)
2309 {
2310         PopplerFormField *poppler_field;
2311
2312         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2313         if (!poppler_field)
2314                 return;
2315         
2316         poppler_form_field_choice_set_text (poppler_field, text);
2317         PDF_DOCUMENT (document)->modified = TRUE;
2318 }
2319
2320 static gchar *
2321 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2322                                                EvFormField     *field)
2323 {
2324         PopplerFormField *poppler_field;
2325         gchar *text;
2326
2327         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2328         if (!poppler_field)
2329                 return NULL;
2330
2331         text = poppler_form_field_choice_get_text (poppler_field);
2332
2333         return text;
2334 }
2335
2336 static void
2337 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2338 {
2339         iface->get_form_fields = pdf_document_forms_get_form_fields;
2340         iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2341         iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2342         iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2343         iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2344         iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2345         iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2346         iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2347         iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2348         iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2349         iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2350         iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2351         iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2352 }
2353
2354 /* Annotations */
2355 static void
2356 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2357                                   GdkColor     *color)
2358 {
2359         PopplerColor *poppler_color;
2360
2361         poppler_color = poppler_annot_get_color (poppler_annot);
2362         if (poppler_color) {
2363                 color->red = poppler_color->red;
2364                 color->green = poppler_color->green;
2365                 color->blue = poppler_color->blue;
2366
2367                 g_free (poppler_color);
2368         } /* TODO: else use a default color */
2369 }
2370
2371 static EvAnnotation *
2372 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2373                              EvPage       *page)
2374 {
2375         EvAnnotation *ev_annot = NULL;
2376         const gchar  *unimplemented_annot = NULL;
2377
2378         switch (poppler_annot_get_annot_type (poppler_annot)) {
2379                 case POPPLER_ANNOT_TEXT:
2380                         PopplerAnnotText *poppler_text;
2381                         EvAnnotationText *ev_annot_text;
2382
2383                         poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2384
2385                         ev_annot = ev_annotation_text_new (page);
2386
2387                         ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2388                         ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2389
2390                         break;
2391                 case POPPLER_ANNOT_LINK:
2392                 case POPPLER_ANNOT_WIDGET:
2393                         /* Ignore link and widgets annots since they are already handled */
2394                         break;
2395                 default: {
2396                         GEnumValue *enum_value;
2397
2398                         enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2399                                                        poppler_annot_get_annot_type (poppler_annot));
2400                         unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2401                 }
2402         }
2403
2404         if (unimplemented_annot) {
2405                 g_warning ("Unimplemented annotation: %s, please post a "
2406                            "bug report in Evince bugzilla "
2407                            "(http://bugzilla.gnome.org) with a testcase.",
2408                            unimplemented_annot);
2409         }
2410
2411         if (ev_annot) {
2412                 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2413                 ev_annot->name = poppler_annot_get_name (poppler_annot);
2414                 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2415                 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2416
2417                 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2418                         PopplerAnnotMarkup *markup;
2419                         gchar *label;
2420                         gdouble opacity;
2421                         PopplerRectangle poppler_rect;
2422
2423                         markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2424
2425                         if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2426                                 EvRectangle ev_rect;
2427                                 gboolean is_open;
2428                                 gdouble height;
2429
2430                                 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2431                                                        NULL, &height);
2432                                 ev_rect.x1 = poppler_rect.x1;
2433                                 ev_rect.x2 = poppler_rect.x2;
2434                                 ev_rect.y1 = height - poppler_rect.y2;
2435                                 ev_rect.y2 = height - poppler_rect.y1;
2436
2437                                 is_open = poppler_annot_markup_get_popup_is_open (markup);
2438
2439                                 g_object_set (ev_annot,
2440                                               "rectangle", &ev_rect,
2441                                               "is_open", is_open,
2442                                               "has_popup", TRUE,
2443                                               NULL);
2444                         } else {
2445                                 /* FIXME: Use poppler_annot_markup_has_popup() when
2446                                  * new poppler is released.
2447                                  */
2448                                 g_object_set (ev_annot,
2449                                               "has_popup", FALSE,
2450                                               NULL);
2451                         }
2452
2453                         label = poppler_annot_markup_get_label (markup);
2454                         opacity = poppler_annot_markup_get_opacity (markup);
2455
2456                         g_object_set (ev_annot,
2457                                       "label", label,
2458                                       "opacity", opacity,
2459                                       NULL);
2460
2461                         g_free (label);
2462                 }
2463         }
2464
2465         return ev_annot;
2466 }
2467
2468 static GList *
2469 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2470                                           EvPage                *page)
2471 {
2472         GList *retval = NULL;
2473         PdfDocument *pdf_document;
2474         PopplerPage *poppler_page;
2475         GList *annots;
2476         GList *list;
2477         gdouble height;
2478         gint i = 0;
2479
2480         pdf_document = PDF_DOCUMENT (document_annotations);
2481         poppler_page = POPPLER_PAGE (page->backend_page);
2482         annots = poppler_page_get_annot_mapping (poppler_page);
2483         poppler_page_get_size (poppler_page, NULL, &height);
2484
2485         for (list = annots; list; list = list->next) {
2486                 PopplerAnnotMapping *mapping;
2487                 EvMapping *annot_mapping;
2488                 EvAnnotation        *ev_annot;
2489
2490                 mapping = (PopplerAnnotMapping *)list->data;
2491
2492                 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2493                 if (!ev_annot)
2494                         continue;
2495
2496                 i++;
2497
2498                 /* Make sure annot has a unique name */
2499                 if (!ev_annot->name)
2500                         ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2501
2502                 annot_mapping = g_new (EvMapping, 1);
2503                 annot_mapping->area.x1 = mapping->area.x1;
2504                 annot_mapping->area.x2 = mapping->area.x2;
2505                 annot_mapping->area.y1 = height - mapping->area.y2;
2506                 annot_mapping->area.y2 = height - mapping->area.y1;
2507                 annot_mapping->data = ev_annot;
2508
2509                 g_object_set_data_full (G_OBJECT (ev_annot),
2510                                         "poppler-annot",
2511                                         g_object_ref (mapping->annot),
2512                                         (GDestroyNotify) g_object_unref);
2513
2514                 retval = g_list_prepend (retval, annot_mapping);
2515         }
2516
2517         poppler_page_free_annot_mapping (annots);
2518
2519         return g_list_reverse (retval);
2520 }
2521
2522 static void
2523 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2524                                                   EvAnnotation          *annot,
2525                                                   const gchar           *contents)
2526 {
2527         PopplerAnnot *poppler_annot;
2528
2529         poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2530         if (!poppler_annot)
2531                 return;
2532
2533         poppler_annot_set_contents (poppler_annot, contents);
2534         PDF_DOCUMENT (document)->modified = TRUE;
2535 }
2536
2537 static void
2538 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface)
2539 {
2540         iface->get_annotations = pdf_document_annotations_get_annotations;
2541         iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2542 }
2543
2544 /* Attachments */
2545 struct SaveToBufferData {
2546         gchar *buffer;
2547         gsize len, max;
2548 };
2549
2550 static gboolean
2551 attachment_save_to_buffer_callback (const gchar  *buf,
2552                                     gsize         count,
2553                                     gpointer      user_data,
2554                                     GError      **error)
2555 {
2556         struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
2557         gchar *new_buffer;
2558         gsize new_max;
2559
2560         if (sdata->len + count > sdata->max) {
2561                 new_max = MAX (sdata->max * 2, sdata->len + count);
2562                 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
2563
2564                 sdata->buffer = new_buffer;
2565                 sdata->max = new_max;
2566         }
2567
2568         memcpy (sdata->buffer + sdata->len, buf, count);
2569         sdata->len += count;
2570
2571         return TRUE;
2572 }
2573
2574 static gboolean
2575 attachment_save_to_buffer (PopplerAttachment  *attachment,
2576                            gchar             **buffer,
2577                            gsize              *buffer_size,
2578                            GError            **error)
2579 {
2580         static const gint initial_max = 1024;
2581         struct SaveToBufferData sdata;
2582
2583         *buffer = NULL;
2584         *buffer_size = 0;
2585
2586         sdata.buffer = (gchar *) g_malloc (initial_max);
2587         sdata.max = initial_max;
2588         sdata.len = 0;
2589
2590         if (! poppler_attachment_save_to_callback (attachment,
2591                                                    attachment_save_to_buffer_callback,
2592                                                    &sdata,
2593                                                    error)) {
2594                 g_free (sdata.buffer);
2595                 return FALSE;
2596         }
2597
2598         *buffer = sdata.buffer;
2599         *buffer_size = sdata.len;
2600
2601         return TRUE;
2602 }
2603
2604 static GList *
2605 pdf_document_attachments_get_attachments (EvDocumentAttachments *document)
2606 {
2607         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2608         GList *attachments;
2609         GList *list;
2610         GList *retval = NULL;
2611
2612         attachments = poppler_document_get_attachments (pdf_document->document);
2613
2614         for (list = attachments; list; list = list->next) {
2615                 PopplerAttachment *attachment;
2616                 EvAttachment *ev_attachment;
2617                 gchar *data = NULL;
2618                 gsize size;
2619                 GError *error = NULL;
2620
2621                 attachment = (PopplerAttachment *) list->data;
2622
2623                 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
2624                         ev_attachment = ev_attachment_new (attachment->name,
2625                                                            attachment->description,
2626                                                            attachment->mtime,
2627                                                            attachment->ctime,
2628                                                            size, data);
2629
2630                         retval = g_list_prepend (retval, ev_attachment);
2631                 } else {
2632                         if (error) {
2633                                 g_warning ("%s", error->message);
2634                                 g_error_free (error);
2635
2636                                 g_free (data);
2637                         }
2638                 }
2639
2640                 g_object_unref (attachment);
2641         }
2642
2643         return g_list_reverse (retval);
2644 }
2645
2646 static gboolean
2647 pdf_document_attachments_has_attachments (EvDocumentAttachments *document)
2648 {
2649         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2650
2651         return poppler_document_has_attachments (pdf_document->document);
2652 }
2653
2654 static void
2655 pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface)
2656 {
2657         iface->has_attachments = pdf_document_attachments_has_attachments;
2658         iface->get_attachments = pdf_document_attachments_get_attachments;
2659 }
2660
2661 /* Layers */
2662 static gboolean
2663 pdf_document_layers_has_layers (EvDocumentLayers *document)
2664 {
2665         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2666         PopplerLayersIter *iter;
2667
2668         iter = poppler_layers_iter_new (pdf_document->document);
2669         if (!iter)
2670                 return FALSE;
2671         poppler_layers_iter_free (iter);
2672
2673         return TRUE;
2674 }
2675
2676 static void
2677 build_layers_tree (PdfDocument       *pdf_document,
2678                    GtkTreeModel      *model,
2679                    GtkTreeIter       *parent,
2680                    PopplerLayersIter *iter)
2681 {
2682         do {
2683                 GtkTreeIter        tree_iter;
2684                 PopplerLayersIter *child;
2685                 PopplerLayer      *layer;
2686                 EvLayer           *ev_layer = NULL;
2687                 gboolean           visible;
2688                 gchar             *markup;
2689                 gint               rb_group = 0;
2690
2691                 layer = poppler_layers_iter_get_layer (iter);
2692                 if (layer) {
2693                         markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2694                         visible = poppler_layer_is_visible (layer);
2695                         rb_group = poppler_layer_get_radio_button_group_id (layer);
2696                         pdf_document->layers = g_list_append (pdf_document->layers,
2697                                                               g_object_ref (layer));
2698                         ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2699                                                  poppler_layer_is_parent (layer),
2700                                                  rb_group);
2701                 } else {
2702                         gchar *title;
2703
2704                         title = poppler_layers_iter_get_title (iter);
2705                         markup = g_markup_escape_text (title, -1);
2706                         g_free (title);
2707
2708                         visible = FALSE;
2709                         layer = NULL;
2710                 }
2711
2712                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2713                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2714                                     EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2715                                     EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2716                                     EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2717                                     EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2718                                     EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2719                                     EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2720                                     -1);
2721                 if (ev_layer)
2722                         g_object_unref (ev_layer);
2723                 g_free (markup);
2724
2725                 child = poppler_layers_iter_get_child (iter);
2726                 if (child)
2727                         build_layers_tree (pdf_document, model, &tree_iter, child);
2728                 poppler_layers_iter_free (child);
2729         } while (poppler_layers_iter_next (iter));
2730 }
2731
2732 static GtkTreeModel *
2733 pdf_document_layers_get_layers (EvDocumentLayers *document)
2734 {
2735         GtkTreeModel *model = NULL;
2736         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2737         PopplerLayersIter *iter;
2738
2739         iter = poppler_layers_iter_new (pdf_document->document);
2740         if (iter) {
2741                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2742                                                              G_TYPE_STRING,  /* TITLE */
2743                                                              G_TYPE_OBJECT,  /* LAYER */
2744                                                              G_TYPE_BOOLEAN, /* VISIBLE */
2745                                                              G_TYPE_BOOLEAN, /* ENABLED */
2746                                                              G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2747                                                              G_TYPE_INT);    /* RBGROUP */
2748                 build_layers_tree (pdf_document, model, NULL, iter);
2749                 poppler_layers_iter_free (iter);
2750         }
2751         return model;
2752 }
2753
2754 static void
2755 pdf_document_layers_show_layer (EvDocumentLayers *document,
2756                                 EvLayer          *layer)
2757 {
2758         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2759         guint        layer_id = ev_layer_get_id (layer);
2760
2761         poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2762 }
2763
2764 static void
2765 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2766                                 EvLayer          *layer)
2767 {
2768         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2769         guint        layer_id = ev_layer_get_id (layer);
2770
2771         poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2772 }
2773
2774 static gboolean
2775 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2776                                       EvLayer          *layer)
2777 {
2778         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2779         guint        layer_id = ev_layer_get_id (layer);
2780
2781         return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2782 }
2783
2784 static void
2785 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2786 {
2787         iface->has_layers = pdf_document_layers_has_layers;
2788         iface->get_layers = pdf_document_layers_get_layers;
2789         iface->show_layer = pdf_document_layers_show_layer;
2790         iface->hide_layer = pdf_document_layers_hide_layer;
2791         iface->layer_is_visible = pdf_document_layers_layer_is_visible;
2792 }