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