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