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