]> www.fi.muni.cz Git - evince.git/blob - backend/pdf/ev-poppler.cc
[pdf] Update to poppler api changes
[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 #ifdef HAVE_POPPLER_DOCUMENT_IS_LINEARIZED
726         gboolean linearized;
727 #endif
728
729         info = g_new0 (EvDocumentInfo, 1);
730
731         info->fields_mask = EV_DOCUMENT_INFO_TITLE |
732                             EV_DOCUMENT_INFO_FORMAT |
733                             EV_DOCUMENT_INFO_AUTHOR |
734                             EV_DOCUMENT_INFO_SUBJECT |
735                             EV_DOCUMENT_INFO_KEYWORDS |
736                             EV_DOCUMENT_INFO_LAYOUT |
737                             EV_DOCUMENT_INFO_START_MODE |
738                             EV_DOCUMENT_INFO_PERMISSIONS |
739                             EV_DOCUMENT_INFO_UI_HINTS |
740                             EV_DOCUMENT_INFO_CREATOR |
741                             EV_DOCUMENT_INFO_PRODUCER |
742                             EV_DOCUMENT_INFO_CREATION_DATE |
743                             EV_DOCUMENT_INFO_MOD_DATE |
744                             EV_DOCUMENT_INFO_LINEARIZED |
745                             EV_DOCUMENT_INFO_N_PAGES |
746                             EV_DOCUMENT_INFO_SECURITY | 
747                             EV_DOCUMENT_INFO_PAPER_SIZE |
748                             EV_DOCUMENT_INFO_LICENSE;
749
750         g_object_get (PDF_DOCUMENT (document)->document,
751                       "title", &(info->title),
752                       "format", &(info->format),
753                       "author", &(info->author),
754                       "subject", &(info->subject),
755                       "keywords", &(info->keywords),
756                       "page-mode", &mode,
757                       "page-layout", &layout,
758                       "viewer-preferences", &view_prefs,
759                       "permissions", &permissions,
760                       "creator", &(info->creator),
761                       "producer", &(info->producer),
762                       "creation-date", &(info->creation_date),
763                       "mod-date", &(info->modified_date),
764 #ifdef HAVE_POPPLER_DOCUMENT_IS_LINEARIZED
765                       "linearized", &linearized,
766 #else
767                       "linearized", &(info->linearized),
768 #endif
769                       "metadata", &metadata,
770                       NULL);
771
772         if (metadata != NULL) {
773                 pdf_document_parse_metadata (metadata, info);
774                 g_free (metadata);
775         }
776
777         info->n_pages = ev_document_get_n_pages (document);
778
779         if (info->n_pages > 0) {
780                 ev_document_get_page_size (document, 0,
781                                            &(info->paper_width),
782                                            &(info->paper_height));
783                 // Convert to mm.
784                 info->paper_width = info->paper_width / 72.0f * 25.4f;
785                 info->paper_height = info->paper_height / 72.0f * 25.4f;
786         }
787
788         switch (layout) {
789                 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
790                         info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
791                         break;
792                 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
793                         info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
794                         break;
795                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
796                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
797                         break;
798                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
799                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
800                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
801                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
802                         break;
803                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
804                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
805                         break;
806                 default:
807                         break;
808         }
809
810         switch (mode) {
811                 case POPPLER_PAGE_MODE_NONE:
812                         info->mode = EV_DOCUMENT_MODE_NONE;
813                         break;
814                 case POPPLER_PAGE_MODE_USE_THUMBS:
815                         info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
816                         break;
817                 case POPPLER_PAGE_MODE_USE_OC:
818                         info->mode = EV_DOCUMENT_MODE_USE_OC;
819                         break;
820                 case POPPLER_PAGE_MODE_FULL_SCREEN:
821                         info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
822                         break;
823                 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
824                         info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
825                 default:
826                         break;
827         }
828
829         info->ui_hints = 0;
830         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
831                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
832         }
833         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
834                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
835         }
836         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
837                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
838         }
839         if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
840                 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
841         }
842         if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
843                 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
844         }
845         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
846                 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
847         }
848         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
849                 info->ui_hints |=  EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
850         }
851
852         info->permissions = 0;
853         if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
854                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
855         }
856         if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
857                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
858         }
859         if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
860                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
861         }
862         if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
863                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
864         }
865
866         if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
867                 /* translators: this is the document security state */
868                 info->security = g_strdup (_("Yes"));
869         } else {
870                 /* translators: this is the document security state */
871                 info->security = g_strdup (_("No"));
872         }
873
874 #ifdef HAVE_POPPLER_DOCUMENT_IS_LINEARIZED
875         info->linearized = linearized ? g_strdup (_("Yes")) : g_strdup (_("No"));
876 #endif
877
878         return info;
879 }
880
881 static gboolean
882 pdf_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
883 {
884         PopplerBackend backend;
885
886         backend = poppler_get_backend ();
887         switch (backend) {
888                 case POPPLER_BACKEND_CAIRO:
889                         info->name = "poppler/cairo";
890                         break;
891                 case POPPLER_BACKEND_SPLASH:
892                         info->name = "poppler/splash";
893                         break;
894                 default:
895                         info->name = "poppler/unknown";
896                         break;
897         }
898
899         info->version = poppler_get_version ();
900
901         return TRUE;
902 }
903
904 static gboolean
905 pdf_document_support_synctex (EvDocument *document)
906 {
907         return TRUE;
908 }
909
910 static void
911 pdf_document_class_init (PdfDocumentClass *klass)
912 {
913         GObjectClass    *g_object_class = G_OBJECT_CLASS (klass);
914         EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
915
916         g_object_class->dispose = pdf_document_dispose;
917
918         ev_document_class->save = pdf_document_save;
919         ev_document_class->load = pdf_document_load;
920         ev_document_class->get_n_pages = pdf_document_get_n_pages;
921         ev_document_class->get_page = pdf_document_get_page;
922         ev_document_class->get_page_size = pdf_document_get_page_size;
923         ev_document_class->get_page_label = pdf_document_get_page_label;
924         ev_document_class->render = pdf_document_render;
925         ev_document_class->get_thumbnail = pdf_document_get_thumbnail;
926         ev_document_class->get_info = pdf_document_get_info;
927         ev_document_class->get_backend_info = pdf_document_get_backend_info;
928         ev_document_class->support_synctex = pdf_document_support_synctex;
929 }
930
931 /* EvDocumentSecurity */
932 static gboolean
933 pdf_document_has_document_security (EvDocumentSecurity *document_security)
934 {
935         /* FIXME: do we really need to have this? */
936         return FALSE;
937 }
938
939 static void
940 pdf_document_set_password (EvDocumentSecurity *document_security,
941                            const char         *password)
942 {
943         PdfDocument *document = PDF_DOCUMENT (document_security);
944
945         if (document->password)
946                 g_free (document->password);
947
948         document->password = g_strdup (password);
949 }
950
951 static void
952 pdf_document_security_iface_init (EvDocumentSecurityInterface *iface)
953 {
954         iface->has_document_security = pdf_document_has_document_security;
955         iface->set_password = pdf_document_set_password;
956 }
957
958 static gdouble
959 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
960 {
961         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
962         int n_pages;
963
964         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
965
966         return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
967 }
968
969 static gboolean
970 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
971                          int              n_pages)
972 {
973         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
974         gboolean result;
975
976         g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
977
978         if (pdf_document->font_info == NULL) { 
979                 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
980         }
981
982         if (pdf_document->fonts_iter) {
983                 poppler_fonts_iter_free (pdf_document->fonts_iter);
984         }
985
986         pdf_document->fonts_scanned_pages += n_pages;
987
988         result = poppler_font_info_scan (pdf_document->font_info, n_pages,
989                                          &pdf_document->fonts_iter);
990         if (!result) {
991                 pdf_document->fonts_scanned_pages = 0;
992                 poppler_font_info_free (pdf_document->font_info);
993                 pdf_document->font_info = NULL; 
994         }
995
996         return result;
997 }
998
999 static const char *
1000 font_type_to_string (PopplerFontType type)
1001 {
1002         switch (type) {
1003                 case POPPLER_FONT_TYPE_TYPE1:
1004                         return _("Type 1");
1005                 case POPPLER_FONT_TYPE_TYPE1C:
1006                         return _("Type 1C");
1007                 case POPPLER_FONT_TYPE_TYPE3:
1008                         return _("Type 3");
1009                 case POPPLER_FONT_TYPE_TRUETYPE:
1010                         return _("TrueType");
1011                 case POPPLER_FONT_TYPE_CID_TYPE0:
1012                         return _("Type 1 (CID)");
1013                 case POPPLER_FONT_TYPE_CID_TYPE0C:
1014                         return _("Type 1C (CID)");
1015                 case POPPLER_FONT_TYPE_CID_TYPE2:
1016                         return _("TrueType (CID)");
1017                 default:
1018                         return _("Unknown font type");
1019         }
1020 }
1021
1022 static void
1023 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
1024                                GtkTreeModel    *model)
1025 {
1026         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
1027         PopplerFontsIter *iter = pdf_document->fonts_iter;
1028
1029         g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
1030
1031         if (!iter)
1032                 return;
1033
1034         do {
1035                 GtkTreeIter list_iter;
1036                 const char *name;
1037                 const char *type;
1038                 const char *embedded;
1039                 char *details;
1040                 
1041                 name = poppler_fonts_iter_get_name (iter);
1042
1043                 if (name == NULL) {
1044                         name = _("No name");
1045                 }
1046
1047                 type = font_type_to_string (
1048                         poppler_fonts_iter_get_font_type (iter));
1049
1050                 if (poppler_fonts_iter_is_embedded (iter)) {
1051                         if (poppler_fonts_iter_is_subset (iter))
1052                                 embedded = _("Embedded subset");
1053                         else
1054                                 embedded = _("Embedded");
1055                 } else {
1056                         embedded = _("Not embedded");
1057                 }
1058
1059                 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
1060
1061                 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
1062                 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
1063                                     EV_DOCUMENT_FONTS_COLUMN_NAME, name,
1064                                     EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
1065                                     -1);
1066
1067                 g_free (details);
1068         } while (poppler_fonts_iter_next (iter));
1069 }
1070
1071 static void
1072 pdf_document_document_fonts_iface_init (EvDocumentFontsInterface *iface)
1073 {
1074         iface->fill_model = pdf_document_fonts_fill_model;
1075         iface->scan = pdf_document_fonts_scan;
1076         iface->get_progress = pdf_document_fonts_get_progress;
1077 }
1078
1079 static gboolean
1080 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
1081 {
1082         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1083         PopplerIndexIter *iter;
1084
1085         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
1086
1087         iter = poppler_index_iter_new (pdf_document->document);
1088         if (iter == NULL)
1089                 return FALSE;
1090         poppler_index_iter_free (iter);
1091
1092         return TRUE;
1093 }
1094
1095 static EvLinkDest *
1096 ev_link_dest_from_dest (PdfDocument *pdf_document,
1097                         PopplerDest *dest)
1098 {
1099         EvLinkDest *ev_dest = NULL;
1100         const char *unimplemented_dest = NULL;
1101
1102         g_assert (dest != NULL);
1103
1104         switch (dest->type) {
1105                 case POPPLER_DEST_XYZ: {
1106                         PopplerPage *poppler_page;
1107                         double height;
1108
1109                         poppler_page = poppler_document_get_page (pdf_document->document,
1110                                                                   MAX (0, dest->page_num - 1));
1111                         poppler_page_get_size (poppler_page, NULL, &height);
1112                         ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
1113                                                         dest->left,
1114                                                         height - MIN (height, dest->top),
1115                                                         dest->zoom,
1116                                                         dest->change_left,
1117                                                         dest->change_top,
1118                                                         dest->change_zoom);
1119                         g_object_unref (poppler_page);
1120                 }
1121                         break;
1122                 case POPPLER_DEST_FITB:
1123                 case POPPLER_DEST_FIT:
1124                         ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
1125                         break;
1126                 case POPPLER_DEST_FITBH:
1127                 case POPPLER_DEST_FITH: {
1128                         PopplerPage *poppler_page;
1129                         double height;
1130
1131                         poppler_page = poppler_document_get_page (pdf_document->document,
1132                                                                   MAX (0, dest->page_num - 1));
1133                         poppler_page_get_size (poppler_page, NULL, &height);
1134                         ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
1135                                                          height - MIN (height, dest->top),
1136                                                          dest->change_top);
1137                         g_object_unref (poppler_page);
1138                 }
1139                         break;
1140                 case POPPLER_DEST_FITBV:
1141                 case POPPLER_DEST_FITV:
1142                         ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
1143                                                          dest->left,
1144                                                          dest->change_left);
1145                         break;
1146                 case POPPLER_DEST_FITR: {
1147                         PopplerPage *poppler_page;
1148                         double height;
1149
1150                         poppler_page = poppler_document_get_page (pdf_document->document,
1151                                                                   MAX (0, dest->page_num - 1));
1152                         poppler_page_get_size (poppler_page, NULL, &height);
1153                         ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
1154                                                          dest->left,
1155                                                          height - MIN (height, dest->bottom),
1156                                                          dest->right,
1157                                                          height - MIN (height, dest->top));
1158                         g_object_unref (poppler_page);
1159                 }
1160                         break;
1161                 case POPPLER_DEST_NAMED:
1162                         ev_dest = ev_link_dest_new_named (dest->named_dest);
1163                         break;
1164                 case POPPLER_DEST_UNKNOWN:
1165                         unimplemented_dest = "POPPLER_DEST_UNKNOWN";
1166                         break;
1167         }
1168
1169         if (unimplemented_dest) {
1170                 g_warning ("Unimplemented destination: %s, please post a "
1171                            "bug report in Evince bugzilla "
1172                            "(http://bugzilla.gnome.org) with a testcase.",
1173                            unimplemented_dest);
1174         }
1175
1176         if (!ev_dest)
1177                 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
1178         
1179         return ev_dest;
1180 }
1181
1182 static EvLink *
1183 ev_link_from_action (PdfDocument   *pdf_document,
1184                      PopplerAction *action)
1185 {
1186         EvLink       *link = NULL;
1187         EvLinkAction *ev_action = NULL;
1188         const char   *unimplemented_action = NULL;
1189
1190         switch (action->type) {
1191                 case POPPLER_ACTION_NONE:
1192                         break;
1193                 case POPPLER_ACTION_GOTO_DEST: {
1194                         EvLinkDest *dest;
1195                         
1196                         dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
1197                         ev_action = ev_link_action_new_dest (dest);
1198                 }
1199                         break;
1200                 case POPPLER_ACTION_GOTO_REMOTE: {
1201                         EvLinkDest *dest;
1202                         
1203                         dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
1204                         ev_action = ev_link_action_new_remote (dest, 
1205                                                                action->goto_remote.file_name);
1206                         
1207                 }
1208                         break;
1209                 case POPPLER_ACTION_LAUNCH:
1210                         ev_action = ev_link_action_new_launch (action->launch.file_name,
1211                                                                action->launch.params);
1212                         break;
1213                 case POPPLER_ACTION_URI:
1214                         ev_action = ev_link_action_new_external_uri (action->uri.uri);
1215                         break;
1216                 case POPPLER_ACTION_NAMED:
1217                         ev_action = ev_link_action_new_named (action->named.named_dest);
1218                         break;
1219                 case POPPLER_ACTION_MOVIE:
1220                         unimplemented_action = "POPPLER_ACTION_MOVIE";
1221                         break;
1222 #if POPPLER_CHECK_VERSION (0, 13, 2)
1223                 case POPPLER_ACTION_RENDITION:
1224                         unimplemented_action = "POPPLER_ACTION_RENDITION";
1225                         break;
1226                 case POPPLER_ACTION_OCG_STATE: {
1227                         GList *on_list = NULL;
1228                         GList *off_list = NULL;
1229                         GList *toggle_list = NULL;
1230                         GList *l, *m;
1231
1232                         for (l = action->ocg_state.state_list; l; l = g_list_next (l)) {
1233                                 PopplerActionLayer *action_layer = (PopplerActionLayer *)l->data;
1234
1235                                 for (m = action_layer->layers; m; m = g_list_next (m)) {
1236                                         PopplerLayer *layer = (PopplerLayer *)m->data;
1237                                         EvLayer      *ev_layer;
1238
1239                                         ev_layer = ev_layer_new (poppler_layer_is_parent (layer),
1240                                                                  poppler_layer_get_radio_button_group_id (layer));
1241                                         g_object_set_data_full (G_OBJECT (ev_layer),
1242                                                                 "poppler-layer",
1243                                                                 g_object_ref (layer),
1244                                                                 (GDestroyNotify)g_object_unref);
1245
1246                                         switch (action_layer->action) {
1247                                         case POPPLER_ACTION_LAYER_ON:
1248                                                 on_list = g_list_prepend (on_list, ev_layer);
1249                                                 break;
1250                                         case POPPLER_ACTION_LAYER_OFF:
1251                                                 off_list = g_list_prepend (off_list, ev_layer);
1252                                                 break;
1253                                         case POPPLER_ACTION_LAYER_TOGGLE:
1254                                                 toggle_list = g_list_prepend (toggle_list, ev_layer);
1255                                                 break;
1256                                         }
1257                                 }
1258                         }
1259
1260                         /* The action takes the ownership of the lists */
1261                         ev_action = ev_link_action_new_layers_state (g_list_reverse (on_list),
1262                                                                      g_list_reverse (off_list),
1263                                                                      g_list_reverse (toggle_list));
1264
1265
1266                 }
1267                         break;
1268 #endif
1269                 case POPPLER_ACTION_UNKNOWN:
1270                         unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1271         }
1272         
1273         if (unimplemented_action) {
1274                 g_warning ("Unimplemented action: %s, please post a bug report "
1275                            "in Evince bugzilla (http://bugzilla.gnome.org) "
1276                            "with a testcase.", unimplemented_action);
1277         }
1278         
1279         link = ev_link_new (action->any.title, ev_action);
1280         
1281         return link;    
1282 }
1283
1284 static void
1285 build_tree (PdfDocument      *pdf_document,
1286             GtkTreeModel     *model,
1287             GtkTreeIter      *parent,
1288             PopplerIndexIter *iter)
1289 {
1290         
1291         do {
1292                 GtkTreeIter tree_iter;
1293                 PopplerIndexIter *child;
1294                 PopplerAction *action;
1295                 EvLink *link = NULL;
1296                 gboolean expand;
1297                 char *title_markup;
1298                 
1299                 action = poppler_index_iter_get_action (iter);
1300                 expand = poppler_index_iter_is_open (iter);
1301
1302                 if (!action)
1303                         continue;
1304
1305                 link = ev_link_from_action (pdf_document, action);
1306                 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1307                         poppler_action_free (action);
1308                         if (link)
1309                                 g_object_unref (link);
1310                         
1311                         continue;
1312                 }
1313
1314                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1315                 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1316                 
1317                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1318                                     EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1319                                     EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1320                                     EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1321                                     -1);
1322                 
1323                 g_free (title_markup);
1324                 g_object_unref (link);
1325                 
1326                 child = poppler_index_iter_get_child (iter);
1327                 if (child)
1328                         build_tree (pdf_document, model, &tree_iter, child);
1329                 poppler_index_iter_free (child);
1330                 poppler_action_free (action);
1331                 
1332         } while (poppler_index_iter_next (iter));
1333 }
1334
1335 static GtkTreeModel *
1336 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1337 {
1338         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1339         GtkTreeModel *model = NULL;
1340         PopplerIndexIter *iter;
1341         
1342         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1343
1344         iter = poppler_index_iter_new (pdf_document->document);
1345         /* Create the model if we have items*/
1346         if (iter != NULL) {
1347                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1348                                                              G_TYPE_STRING,
1349                                                              G_TYPE_OBJECT,
1350                                                              G_TYPE_BOOLEAN,
1351                                                              G_TYPE_STRING);
1352                 build_tree (pdf_document, model, NULL, iter);
1353                 poppler_index_iter_free (iter);
1354         }
1355         
1356         return model;
1357 }
1358
1359 static EvMappingList *
1360 pdf_document_links_get_links (EvDocumentLinks *document_links,
1361                               EvPage          *page)
1362 {
1363         PdfDocument *pdf_document;
1364         PopplerPage *poppler_page;
1365         GList *retval = NULL;
1366         GList *mapping_list;
1367         GList *list;
1368         double height;
1369
1370         pdf_document = PDF_DOCUMENT (document_links);
1371         poppler_page = POPPLER_PAGE (page->backend_page);
1372         mapping_list = poppler_page_get_link_mapping (poppler_page);
1373         poppler_page_get_size (poppler_page, NULL, &height);
1374
1375         for (list = mapping_list; list; list = list->next) {
1376                 PopplerLinkMapping *link_mapping;
1377                 EvMapping *ev_link_mapping;
1378
1379                 link_mapping = (PopplerLinkMapping *)list->data;
1380                 ev_link_mapping = g_new (EvMapping, 1);
1381                 ev_link_mapping->data = ev_link_from_action (pdf_document,
1382                                                              link_mapping->action);
1383                 ev_link_mapping->area.x1 = link_mapping->area.x1;
1384                 ev_link_mapping->area.x2 = link_mapping->area.x2;
1385                 /* Invert this for X-style coordinates */
1386                 ev_link_mapping->area.y1 = height - link_mapping->area.y2;
1387                 ev_link_mapping->area.y2 = height - link_mapping->area.y1;
1388
1389                 retval = g_list_prepend (retval, ev_link_mapping);
1390         }
1391
1392         poppler_page_free_link_mapping (mapping_list);
1393
1394         return ev_mapping_list_new (page->index, g_list_reverse (retval), (GDestroyNotify)g_object_unref);
1395 }
1396
1397 static EvLinkDest *
1398 pdf_document_links_find_link_dest (EvDocumentLinks  *document_links,
1399                                    const gchar      *link_name)
1400 {
1401         PdfDocument *pdf_document;
1402         PopplerDest *dest;
1403         EvLinkDest *ev_dest = NULL;
1404
1405         pdf_document = PDF_DOCUMENT (document_links);
1406         dest = poppler_document_find_dest (pdf_document->document,
1407                                            link_name);
1408         if (dest) {
1409                 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1410                 poppler_dest_free (dest);
1411         }
1412
1413         return ev_dest;
1414 }
1415
1416 static gint
1417 pdf_document_links_find_link_page (EvDocumentLinks  *document_links,
1418                                    const gchar      *link_name)
1419 {
1420         PdfDocument *pdf_document;
1421         PopplerDest *dest;
1422         gint         retval = -1;
1423
1424         pdf_document = PDF_DOCUMENT (document_links);
1425         dest = poppler_document_find_dest (pdf_document->document,
1426                                            link_name);
1427         if (dest) {
1428                 retval = dest->page_num - 1;
1429                 poppler_dest_free (dest);
1430         }
1431
1432         return retval;
1433 }
1434
1435 static void
1436 pdf_document_document_links_iface_init (EvDocumentLinksInterface *iface)
1437 {
1438         iface->has_document_links = pdf_document_links_has_document_links;
1439         iface->get_links_model = pdf_document_links_get_links_model;
1440         iface->get_links = pdf_document_links_get_links;
1441         iface->find_link_dest = pdf_document_links_find_link_dest;
1442         iface->find_link_page = pdf_document_links_find_link_page;
1443 }
1444
1445 static EvMappingList *
1446 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1447                                        EvPage           *page)
1448 {
1449         GList *retval = NULL;
1450         PdfDocument *pdf_document;
1451         PopplerPage *poppler_page;
1452         GList *mapping_list;
1453         GList *list;
1454
1455         pdf_document = PDF_DOCUMENT (document_images);
1456         poppler_page = POPPLER_PAGE (page->backend_page);
1457         mapping_list = poppler_page_get_image_mapping (poppler_page);
1458
1459         for (list = mapping_list; list; list = list->next) {
1460                 PopplerImageMapping *image_mapping;
1461                 EvMapping *ev_image_mapping;
1462
1463                 image_mapping = (PopplerImageMapping *)list->data;
1464
1465                 ev_image_mapping = g_new (EvMapping, 1);
1466                 
1467                 ev_image_mapping->data = ev_image_new (page->index, image_mapping->image_id);
1468                 ev_image_mapping->area.x1 = image_mapping->area.x1;
1469                 ev_image_mapping->area.y1 = image_mapping->area.y1;
1470                 ev_image_mapping->area.x2 = image_mapping->area.x2;
1471                 ev_image_mapping->area.y2 = image_mapping->area.y2;
1472
1473                 retval = g_list_prepend (retval, ev_image_mapping);
1474         }
1475
1476         poppler_page_free_image_mapping (mapping_list);
1477
1478         return ev_mapping_list_new (page->index, g_list_reverse (retval), (GDestroyNotify)g_object_unref);
1479 }
1480
1481 GdkPixbuf *
1482 pdf_document_images_get_image (EvDocumentImages *document_images,
1483                                EvImage          *image)
1484 {
1485         GdkPixbuf       *retval = NULL;
1486         PdfDocument     *pdf_document;
1487         PopplerPage     *poppler_page;
1488         cairo_surface_t *surface;
1489
1490         pdf_document = PDF_DOCUMENT (document_images);
1491         poppler_page = poppler_document_get_page (pdf_document->document,
1492                                                   ev_image_get_page (image));
1493
1494         surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1495         if (surface) {
1496                 retval = ev_document_misc_pixbuf_from_surface (surface);
1497                 cairo_surface_destroy (surface);
1498         }
1499
1500         g_object_unref (poppler_page);
1501
1502         return retval;
1503 }
1504
1505 static void
1506 pdf_document_document_images_iface_init (EvDocumentImagesInterface *iface)
1507 {
1508         iface->get_image_mapping = pdf_document_images_get_image_mapping;
1509         iface->get_image = pdf_document_images_get_image;
1510 }
1511
1512 static GList *
1513 pdf_document_find_find_text (EvDocumentFind *document_find,
1514                              EvPage         *page,
1515                              const gchar    *text,
1516                              gboolean        case_sensitive)
1517 {
1518         GList *matches, *l;
1519         PopplerPage *poppler_page;
1520         gdouble height;
1521         GList *retval = NULL;
1522
1523         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1524         g_return_val_if_fail (text != NULL, NULL);
1525
1526         poppler_page = POPPLER_PAGE (page->backend_page);
1527         
1528         matches = poppler_page_find_text (poppler_page, text);
1529         if (!matches)
1530                 return NULL;
1531
1532         poppler_page_get_size (poppler_page, NULL, &height);
1533         for (l = matches; l && l->data; l = g_list_next (l)) {
1534                 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1535                 EvRectangle      *ev_rect;
1536
1537                 ev_rect = ev_rectangle_new ();
1538                 ev_rect->x1 = rect->x1;
1539                 ev_rect->x2 = rect->x2;
1540                 /* Invert this for X-style coordinates */
1541                 ev_rect->y1 = height - rect->y2;
1542                 ev_rect->y2 = height - rect->y1;
1543
1544                 retval = g_list_prepend (retval, ev_rect);
1545         }
1546
1547         g_list_foreach (matches, (GFunc)poppler_rectangle_free, NULL);
1548         g_list_free (matches);
1549
1550         return g_list_reverse (retval);
1551 }
1552
1553 static void
1554 pdf_document_find_iface_init (EvDocumentFindInterface *iface)
1555 {
1556         iface->find_text = pdf_document_find_find_text;
1557 }
1558
1559 static void
1560 pdf_print_context_free (PdfPrintContext *ctx)
1561 {
1562         if (!ctx)
1563                 return;
1564
1565 #ifdef HAVE_CAIRO_PRINT
1566         if (ctx->cr) {
1567                 cairo_destroy (ctx->cr);
1568                 ctx->cr = NULL;
1569         }
1570 #else
1571         if (ctx->ps_file) {
1572                 poppler_ps_file_free (ctx->ps_file);
1573                 ctx->ps_file = NULL;
1574         }
1575 #endif
1576         g_free (ctx);
1577 }
1578
1579 static void
1580 pdf_document_file_exporter_begin (EvFileExporter        *exporter,
1581                                   EvFileExporterContext *fc)
1582 {
1583         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1584         PdfPrintContext *ctx;
1585 #ifdef HAVE_CAIRO_PRINT
1586         gdouble width, height;
1587         cairo_surface_t *surface = NULL;
1588 #endif
1589         
1590         if (pdf_document->print_ctx)
1591                 pdf_print_context_free (pdf_document->print_ctx);
1592         pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1593         ctx = pdf_document->print_ctx;
1594         ctx->format = fc->format;
1595         
1596 #ifdef HAVE_CAIRO_PRINT
1597         ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1598
1599         ctx->paper_width = fc->paper_width;
1600         ctx->paper_height = fc->paper_height;
1601         
1602         switch (fc->pages_per_sheet) {
1603                 default:
1604                 case 1:
1605                         ctx->pages_x = 1;
1606                         ctx->pages_y = 1;
1607                         break;
1608                 case 2:
1609                         ctx->pages_x = 1;
1610                         ctx->pages_y = 2;
1611                         break;
1612                 case 4:
1613                         ctx->pages_x = 2;
1614                         ctx->pages_y = 2;
1615                         break;
1616                 case 6:
1617                         ctx->pages_x = 2;
1618                         ctx->pages_y = 3;
1619                         break;
1620                 case 9:
1621                         ctx->pages_x = 3;
1622                         ctx->pages_y = 3;
1623                         break;
1624                 case 16:
1625                         ctx->pages_x = 4;
1626                         ctx->pages_y = 4;
1627                         break;
1628         }
1629
1630         ctx->pages_printed = 0;
1631         
1632         switch (fc->format) {
1633                 case EV_FILE_FORMAT_PS:
1634 #ifdef HAVE_CAIRO_PS
1635                         surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1636 #endif
1637                         break;
1638                 case EV_FILE_FORMAT_PDF:
1639 #ifdef HAVE_CAIRO_PDF
1640                         surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1641 #endif
1642                         break;
1643                 default:
1644                         g_assert_not_reached ();
1645         }
1646
1647         ctx->cr = cairo_create (surface);
1648         cairo_surface_destroy (surface);
1649
1650 #else /* HAVE_CAIRO_PRINT */
1651         if (ctx->format == EV_FILE_FORMAT_PS) {
1652                 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1653                                                     fc->filename, fc->first_page,
1654                                                     fc->last_page - fc->first_page + 1);
1655                 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1656                 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1657         }
1658 #endif /* HAVE_CAIRO_PRINT */
1659 }
1660
1661 static void
1662 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1663 {
1664         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1665         PdfPrintContext *ctx = pdf_document->print_ctx;
1666         
1667         g_return_if_fail (pdf_document->print_ctx != NULL);
1668
1669         ctx->pages_printed = 0;
1670         
1671 #ifdef HAVE_CAIRO_PRINT
1672         if (ctx->paper_width > ctx->paper_height) {
1673                 if (ctx->format == EV_FILE_FORMAT_PS) {
1674                         cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1675                                                    ctx->paper_height,
1676                                                    ctx->paper_width);
1677                 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1678                         cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1679                                                     ctx->paper_height,
1680                                                     ctx->paper_width);
1681                 }
1682         }
1683 #endif /* HAVE_CAIRO_PRINT */
1684 }
1685
1686 static void
1687 pdf_document_file_exporter_do_page (EvFileExporter  *exporter,
1688                                     EvRenderContext *rc)
1689 {
1690         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1691         PdfPrintContext *ctx = pdf_document->print_ctx;
1692         PopplerPage *poppler_page;
1693 #ifdef HAVE_CAIRO_PRINT
1694         gdouble  page_width, page_height;
1695         gint     x, y;
1696         gboolean rotate;
1697         gdouble  width, height;
1698         gdouble  pwidth, pheight;
1699         gdouble  xscale, yscale;
1700 #endif
1701
1702         g_return_if_fail (pdf_document->print_ctx != NULL);
1703
1704         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1705         
1706 #ifdef HAVE_CAIRO_PRINT
1707         x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1708         y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1709         poppler_page_get_size (poppler_page, &page_width, &page_height);
1710
1711         if (page_width > page_height && page_width > ctx->paper_width) {
1712                 rotate = TRUE;
1713         } else {
1714                 rotate = FALSE;
1715         }
1716
1717         /* Use always portrait mode and rotate when necessary */
1718         if (ctx->paper_width > ctx->paper_height) {
1719                 width = ctx->paper_height;
1720                 height = ctx->paper_width;
1721                 rotate = !rotate;
1722         } else {
1723                 width = ctx->paper_width;
1724                 height = ctx->paper_height;
1725         }
1726
1727         if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1728                 rotate = !rotate;
1729         }       
1730
1731         if (rotate) {
1732                 gint tmp1;
1733                 gdouble tmp2;
1734
1735                 tmp1 = x;
1736                 x = y;
1737                 y = tmp1;
1738
1739                 tmp2 = page_width;
1740                 page_width = page_height;
1741                 page_height = tmp2;
1742         }
1743
1744         pwidth = width / ctx->pages_x;
1745         pheight = height / ctx->pages_y;
1746
1747         if ((page_width > pwidth || page_height > pheight) ||
1748             (page_width < pwidth && page_height < pheight)) {
1749                 xscale = pwidth / page_width;
1750                 yscale = pheight / page_height;
1751                 
1752                 if (yscale < xscale) {
1753                         xscale = yscale;
1754                 } else {
1755                         yscale = xscale;
1756                 }
1757                 
1758         } else {        
1759                 xscale = yscale = 1;
1760         }
1761
1762         /* TODO: center */
1763
1764         cairo_save (ctx->cr);
1765         if (rotate) {
1766                 cairo_matrix_t matrix;
1767                 
1768                 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1769                 cairo_matrix_init (&matrix,
1770                                    0,  1,
1771                                    -1,  0,
1772                                    0,  0);
1773                 cairo_transform (ctx->cr, &matrix);
1774         }
1775         
1776         cairo_translate (ctx->cr,
1777                          x * (rotate ? pheight : pwidth),
1778                          y * (rotate ? pwidth : pheight));
1779         cairo_scale (ctx->cr, xscale, yscale);
1780
1781         poppler_page_render_for_printing (poppler_page, ctx->cr);
1782
1783         ctx->pages_printed++;
1784                         
1785         cairo_restore (ctx->cr);
1786 #else /* HAVE_CAIRO_PRINT */
1787         if (ctx->format == EV_FILE_FORMAT_PS)
1788                 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1789 #endif /* HAVE_CAIRO_PRINT */
1790 }
1791
1792 static void
1793 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1794 {
1795         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1796         PdfPrintContext *ctx = pdf_document->print_ctx;
1797         
1798         g_return_if_fail (pdf_document->print_ctx != NULL);
1799
1800 #ifdef HAVE_CAIRO_PRINT
1801         cairo_show_page (ctx->cr);
1802 #endif
1803 }
1804
1805 static void
1806 pdf_document_file_exporter_end (EvFileExporter *exporter)
1807 {
1808         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1809
1810         pdf_print_context_free (pdf_document->print_ctx);
1811         pdf_document->print_ctx = NULL;
1812 }
1813
1814 static EvFileExporterCapabilities
1815 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1816 {
1817         return  (EvFileExporterCapabilities) (
1818                 EV_FILE_EXPORTER_CAN_PAGE_SET |
1819                 EV_FILE_EXPORTER_CAN_COPIES |
1820                 EV_FILE_EXPORTER_CAN_COLLATE |
1821                 EV_FILE_EXPORTER_CAN_REVERSE |
1822                 EV_FILE_EXPORTER_CAN_SCALE |
1823 #ifdef HAVE_CAIRO_PRINT
1824                 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1825 #endif
1826                 
1827 #ifdef HAVE_CAIRO_PDF
1828                 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1829 #endif
1830                 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1831 }
1832
1833 static void
1834 pdf_document_file_exporter_iface_init (EvFileExporterInterface *iface)
1835 {
1836         iface->begin = pdf_document_file_exporter_begin;
1837         iface->begin_page = pdf_document_file_exporter_begin_page;
1838         iface->do_page = pdf_document_file_exporter_do_page;
1839         iface->end_page = pdf_document_file_exporter_end_page;
1840         iface->end = pdf_document_file_exporter_end;
1841         iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1842 }
1843
1844 /* EvDocumentPrint */
1845 static void
1846 pdf_document_print_print_page (EvDocumentPrint *document,
1847                                EvPage          *page,
1848                                cairo_t         *cr)
1849 {
1850         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1851
1852         poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1853 }
1854
1855 static void
1856 pdf_document_document_print_iface_init (EvDocumentPrintInterface *iface)
1857 {
1858         iface->print_page = pdf_document_print_print_page;
1859 }
1860
1861 static void
1862 pdf_selection_render_selection (EvSelection      *selection,
1863                                 EvRenderContext  *rc,
1864                                 cairo_surface_t **surface,
1865                                 EvRectangle      *points,
1866                                 EvRectangle      *old_points,
1867                                 EvSelectionStyle  style,
1868                                 GdkColor         *text,
1869                                 GdkColor         *base)
1870 {
1871         PopplerPage *poppler_page;
1872         cairo_t *cr;
1873         PopplerColor text_color, base_color;
1874         double width_points, height_points;
1875         gint width, height;
1876
1877         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1878
1879         poppler_page_get_size (poppler_page,
1880                                &width_points, &height_points);
1881         width = (int) ((width_points * rc->scale) + 0.5);
1882         height = (int) ((height_points * rc->scale) + 0.5);
1883
1884         text_color.red = text->red;
1885         text_color.green = text->green;
1886         text_color.blue = text->blue;
1887
1888         base_color.red = base->red;
1889         base_color.green = base->green;
1890         base_color.blue = base->blue;
1891
1892         if (*surface == NULL) {
1893                 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1894                                                        width, height);
1895                 
1896         }
1897
1898         cr = cairo_create (*surface);
1899         cairo_scale (cr, rc->scale, rc->scale);
1900         cairo_surface_set_device_offset (*surface, 0, 0);
1901         memset (cairo_image_surface_get_data (*surface), 0x00,
1902                 cairo_image_surface_get_height (*surface) *
1903                 cairo_image_surface_get_stride (*surface));
1904         poppler_page_render_selection (poppler_page,
1905                                        cr,
1906                                        (PopplerRectangle *)points,
1907                                        (PopplerRectangle *)old_points,
1908                                        (PopplerSelectionStyle)style,
1909                                        &text_color,
1910                                        &base_color);
1911         cairo_destroy (cr);
1912 }
1913
1914 static gchar *
1915 pdf_selection_get_selected_text (EvSelection     *selection,
1916                                  EvPage          *page,
1917                                  EvSelectionStyle style,
1918                                  EvRectangle     *points)
1919 {
1920         PopplerPage *poppler_page;
1921         char *retval;
1922
1923         poppler_page = POPPLER_PAGE (page->backend_page);
1924
1925 #ifdef HAVE_POPPLER_PAGE_GET_SELECTED_TEXT
1926         retval = poppler_page_get_selected_text (poppler_page,
1927                                                  (PopplerSelectionStyle)style,
1928                                                  (PopplerRectangle *)points);
1929 #else
1930         PopplerRectangle r;
1931         double height;
1932
1933         poppler_page_get_size (poppler_page, NULL, &height);
1934         r.x1 = points->x1;
1935         r.y1 = height - points->y2;
1936         r.x2 = points->x2;
1937         r.y2 = height - points->y1;
1938
1939         retval = poppler_page_get_text (poppler_page,
1940                                         (PopplerSelectionStyle)style,
1941                                         &r);
1942 #endif /* HAVE_POPPLER_PAGE_GET_SELECTED_TEXT */
1943
1944         return retval;
1945 }
1946
1947 static cairo_region_t *
1948 create_region_from_poppler_region (GList *region, gdouble scale)
1949 {
1950         GList *l;
1951         cairo_region_t *retval;
1952
1953         retval = cairo_region_create ();
1954
1955         for (l = region; l; l = g_list_next (l)) {
1956                 PopplerRectangle   *rectangle;
1957                 cairo_rectangle_int_t rect;
1958
1959                 rectangle = (PopplerRectangle *)l->data;
1960
1961                 rect.x = (gint) ((rectangle->x1 * scale) + 0.5);
1962                 rect.y = (gint) ((rectangle->y1 * scale) + 0.5);
1963                 rect.width  = (gint) (((rectangle->x2 - rectangle->x1) * scale) + 0.5);
1964                 rect.height = (gint) (((rectangle->y2 - rectangle->y1) * scale) + 0.5);
1965                 cairo_region_union_rectangle (retval, &rect);
1966
1967                 poppler_rectangle_free (rectangle);
1968         }
1969
1970         return retval;
1971 }
1972
1973 static cairo_region_t *
1974 pdf_selection_get_selection_region (EvSelection     *selection,
1975                                     EvRenderContext *rc,
1976                                     EvSelectionStyle style,
1977                                     EvRectangle     *points)
1978 {
1979         PopplerPage    *poppler_page;
1980         cairo_region_t *retval;
1981         GList          *region;
1982
1983         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1984         region = poppler_page_get_selection_region (poppler_page,
1985                                                     1.0,
1986                                                     (PopplerSelectionStyle)style,
1987                                                     (PopplerRectangle *) points);
1988         retval = create_region_from_poppler_region (region, rc->scale);
1989         g_list_free (region);
1990         
1991         return retval;
1992 }
1993
1994 static void
1995 pdf_selection_iface_init (EvSelectionInterface *iface)
1996 {
1997         iface->render_selection = pdf_selection_render_selection;
1998         iface->get_selected_text = pdf_selection_get_selected_text;
1999         iface->get_selection_region = pdf_selection_get_selection_region;
2000 }
2001
2002
2003 /* EvDocumentText */
2004 static cairo_region_t *
2005 pdf_document_text_get_text_mapping (EvDocumentText *document_text,
2006                                     EvPage         *page)
2007 {
2008         PopplerPage *poppler_page;
2009         PopplerRectangle points;
2010         GList *region;
2011         cairo_region_t *retval;
2012
2013         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2014
2015         poppler_page = POPPLER_PAGE (page->backend_page);
2016
2017         points.x1 = 0.0;
2018         points.y1 = 0.0;
2019         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2020
2021         region = poppler_page_get_selection_region (poppler_page, 1.0,
2022                                                     POPPLER_SELECTION_GLYPH,
2023                                                     &points);
2024         retval = create_region_from_poppler_region (region, 1.0);
2025         g_list_free (region);
2026
2027         return retval;
2028 }
2029
2030 #ifdef HAVE_POPPLER_PAGE_GET_SELECTED_TEXT
2031 static gchar *
2032 pdf_document_text_get_text (EvDocumentText  *selection,
2033                             EvPage          *page)
2034 {
2035         PopplerPage *poppler_page;
2036
2037         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2038
2039         poppler_page = POPPLER_PAGE (page->backend_page);
2040
2041         return poppler_page_get_text (poppler_page);
2042 }
2043 #else
2044 static gchar *
2045 pdf_document_text_get_text (EvDocumentText  *selection,
2046                             EvPage          *page)
2047 {
2048         PopplerPage *poppler_page;
2049         PopplerRectangle r;
2050
2051         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2052
2053         poppler_page = POPPLER_PAGE (page->backend_page);
2054
2055         r.x1 = 0;
2056         r.y1 = 0;
2057         poppler_page_get_size (poppler_page, &(r.x2), &(r.y2));
2058
2059         return poppler_page_get_text (poppler_page,
2060                                       POPPLER_SELECTION_WORD,
2061                                       &r);
2062 }
2063 #endif /* HAVE_POPPLER_PAGE_GET_SELECTED_TEXT */
2064
2065 #ifdef HAVE_POPPLER_PAGE_GET_TEXT_LAYOUT
2066 static gboolean
2067 pdf_document_text_get_text_layout (EvDocumentText  *selection,
2068                                    EvPage          *page,
2069                                    EvRectangle    **areas,
2070                                    guint           *n_areas)
2071 {
2072         PopplerPage *poppler_page;
2073
2074         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2075
2076         poppler_page = POPPLER_PAGE (page->backend_page);
2077
2078         return poppler_page_get_text_layout (poppler_page, (PopplerRectangle **)areas, n_areas);
2079 }
2080 #endif
2081
2082 static void
2083 pdf_document_text_iface_init (EvDocumentTextInterface *iface)
2084 {
2085         iface->get_text_mapping = pdf_document_text_get_text_mapping;
2086         iface->get_text = pdf_document_text_get_text;
2087 #ifdef HAVE_POPPLER_PAGE_GET_TEXT_LAYOUT
2088         iface->get_text_layout = pdf_document_text_get_text_layout;
2089 #endif
2090 }
2091
2092 /* Page Transitions */
2093 static gdouble
2094 pdf_document_get_page_duration (EvDocumentTransition *trans,
2095                                 gint                  page)
2096 {
2097         PdfDocument *pdf_document;
2098         PopplerPage *poppler_page;
2099         gdouble      duration = -1;
2100
2101         pdf_document = PDF_DOCUMENT (trans);
2102         poppler_page = poppler_document_get_page (pdf_document->document, page);
2103         if (!poppler_page)
2104                 return -1;
2105
2106         duration = poppler_page_get_duration (poppler_page);
2107         g_object_unref (poppler_page);
2108
2109         return duration;
2110 }
2111
2112 static EvTransitionEffect *
2113 pdf_document_get_effect (EvDocumentTransition *trans,
2114                          gint                  page)
2115 {
2116         PdfDocument            *pdf_document;
2117         PopplerPage            *poppler_page;
2118         PopplerPageTransition  *page_transition;
2119         EvTransitionEffect     *effect;
2120
2121         pdf_document = PDF_DOCUMENT (trans);
2122         poppler_page = poppler_document_get_page (pdf_document->document, page);
2123
2124         if (!poppler_page)
2125                 return NULL;
2126
2127         page_transition = poppler_page_get_transition (poppler_page);
2128
2129         if (!page_transition) {
2130                 g_object_unref (poppler_page);
2131                 return NULL;
2132         }
2133
2134         /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2135         effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2136                                            "alignment", page_transition->alignment,
2137                                            "direction", page_transition->direction,
2138                                            "duration", page_transition->duration,
2139                                            "angle", page_transition->angle,
2140                                            "scale", page_transition->scale,
2141                                            "rectangular", page_transition->rectangular,
2142                                            NULL);
2143
2144         poppler_page_transition_free (page_transition);
2145         g_object_unref (poppler_page);
2146
2147         return effect;
2148 }
2149
2150 static void
2151 pdf_document_page_transition_iface_init (EvDocumentTransitionInterface *iface)
2152 {
2153         iface->get_page_duration = pdf_document_get_page_duration;
2154         iface->get_effect = pdf_document_get_effect;
2155 }
2156
2157 /* Forms */
2158 static void
2159 pdf_document_get_crop_box (EvDocument  *document, 
2160                            int          page, 
2161                            EvRectangle *rect)
2162 {
2163         PdfDocument *pdf_document;
2164         PopplerPage *poppler_page;
2165         PopplerRectangle poppler_rect;
2166
2167         pdf_document = PDF_DOCUMENT (document);
2168         poppler_page = poppler_document_get_page (pdf_document->document, page);
2169         poppler_page_get_crop_box (poppler_page, &poppler_rect);
2170         rect->x1 = poppler_rect.x1;
2171         rect->x2 = poppler_rect.x2;
2172         rect->y1 = poppler_rect.y1;
2173         rect->y2 = poppler_rect.y2;
2174 }
2175
2176 static EvFormField *
2177 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2178 {
2179         EvFormField *ev_field = NULL;
2180         gint         id;
2181         gdouble      font_size;
2182         gboolean     is_read_only;
2183
2184         id = poppler_form_field_get_id (poppler_field);
2185         font_size = poppler_form_field_get_font_size (poppler_field);
2186         is_read_only = poppler_form_field_is_read_only (poppler_field);
2187
2188         switch (poppler_form_field_get_field_type (poppler_field)) {
2189                 case POPPLER_FORM_FIELD_TEXT: {
2190                         EvFormFieldText    *field_text;
2191                         EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2192
2193                         switch (poppler_form_field_text_get_text_type (poppler_field)) {
2194                                 case POPPLER_FORM_TEXT_NORMAL:
2195                                         ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2196                                         break;
2197                                 case POPPLER_FORM_TEXT_MULTILINE:
2198                                         ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2199                                         break;
2200                                 case POPPLER_FORM_TEXT_FILE_SELECT:
2201                                         ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2202                                         break;
2203                         }
2204                         
2205                         ev_field = ev_form_field_text_new (id, ev_text_type);
2206                         field_text = EV_FORM_FIELD_TEXT (ev_field);
2207
2208                         field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2209                         field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2210                         field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2211                         field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2212                         field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2213                         field_text->text = poppler_form_field_text_get_text (poppler_field);
2214
2215                 }
2216                         break;
2217                 case POPPLER_FORM_FIELD_BUTTON: {
2218                         EvFormFieldButton    *field_button;
2219                         EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2220
2221                         switch (poppler_form_field_button_get_button_type (poppler_field)) {
2222                                 case POPPLER_FORM_BUTTON_PUSH:
2223                                         ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2224                                         break;
2225                                 case POPPLER_FORM_BUTTON_CHECK:
2226                                         ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2227                                         break;
2228                                 case POPPLER_FORM_BUTTON_RADIO:
2229                                         ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2230                                         break;
2231                         }
2232
2233                         ev_field = ev_form_field_button_new (id, ev_button_type);
2234                         field_button = EV_FORM_FIELD_BUTTON (ev_field);
2235                         
2236                         field_button->state = poppler_form_field_button_get_state (poppler_field);
2237                 }
2238                         break;
2239                 case POPPLER_FORM_FIELD_CHOICE: {
2240                         EvFormFieldChoice    *field_choice;
2241                         EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2242
2243                         switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2244                                 case POPPLER_FORM_CHOICE_COMBO:
2245                                         ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2246                                         break;
2247                                 case EV_FORM_FIELD_CHOICE_LIST:
2248                                         ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2249                                         break;
2250                         }
2251
2252                         ev_field = ev_form_field_choice_new (id, ev_choice_type);
2253                         field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2254
2255                         field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2256                         field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2257                         field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2258                         field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2259
2260                         /* TODO: we need poppler_form_field_choice_get_selected_items in poppler 
2261                         field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2262                         if (field_choice->is_editable)
2263                                 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2264                 }
2265                         break;
2266                 case POPPLER_FORM_FIELD_SIGNATURE:
2267                         /* TODO */
2268                         ev_field = ev_form_field_signature_new (id);
2269                         break;
2270                 case POPPLER_FORM_FIELD_UNKNOWN:
2271                         return NULL;
2272         }
2273
2274         ev_field->font_size = font_size;
2275         ev_field->is_read_only = is_read_only;
2276
2277         return ev_field;
2278 }
2279
2280 static EvMappingList *
2281 pdf_document_forms_get_form_fields (EvDocumentForms *document, 
2282                                     EvPage          *page)
2283 {
2284         PopplerPage *poppler_page;
2285         GList *retval = NULL;
2286         GList *fields;
2287         GList *list;
2288         double height;
2289
2290         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2291         
2292         poppler_page = POPPLER_PAGE (page->backend_page);
2293         fields = poppler_page_get_form_field_mapping (poppler_page);
2294         poppler_page_get_size (poppler_page, NULL, &height);
2295
2296         for (list = fields; list; list = list->next) {
2297                 PopplerFormFieldMapping *mapping;
2298                 EvMapping *field_mapping;
2299                 EvFormField *ev_field;
2300
2301                 mapping = (PopplerFormFieldMapping *)list->data;
2302
2303                 ev_field = ev_form_field_from_poppler_field (mapping->field);
2304                 if (!ev_field)
2305                         continue;
2306
2307                 field_mapping = g_new0 (EvMapping, 1);
2308                 field_mapping->area.x1 = mapping->area.x1;
2309                 field_mapping->area.x2 = mapping->area.x2;
2310                 field_mapping->area.y1 = height - mapping->area.y2;
2311                 field_mapping->area.y2 = height - mapping->area.y1;
2312                 field_mapping->data = ev_field;
2313                 ev_field->page = EV_PAGE (g_object_ref (page));
2314
2315                 g_object_set_data_full (G_OBJECT (ev_field),
2316                                         "poppler-field",
2317                                         g_object_ref (mapping->field),
2318                                         (GDestroyNotify) g_object_unref);
2319                 
2320                 retval = g_list_prepend (retval, field_mapping);
2321         }
2322         
2323         poppler_page_free_form_field_mapping (fields);
2324
2325         return retval ? ev_mapping_list_new (page->index,
2326                                              g_list_reverse (retval),
2327                                              (GDestroyNotify)g_object_unref) : NULL;
2328 }
2329
2330 static gboolean
2331 pdf_document_forms_document_is_modified (EvDocumentForms *document)
2332 {
2333         return PDF_DOCUMENT (document)->forms_modified;
2334 }
2335
2336 static gchar *
2337 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2338                                              EvFormField     *field)
2339         
2340 {
2341         PopplerFormField *poppler_field;
2342         gchar *text;
2343
2344         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2345         if (!poppler_field)
2346                 return NULL;
2347         
2348         text = poppler_form_field_text_get_text (poppler_field);
2349
2350         return text;
2351 }
2352
2353 static void
2354 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document, 
2355                                              EvFormField     *field,
2356                                              const gchar     *text)
2357 {
2358         PopplerFormField *poppler_field;
2359
2360         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2361         if (!poppler_field)
2362                 return;
2363         
2364         poppler_form_field_text_set_text (poppler_field, text);
2365         PDF_DOCUMENT (document)->forms_modified = TRUE;
2366 }
2367
2368 static void
2369 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document, 
2370                                                 EvFormField     *field,
2371                                                 gboolean         state)
2372 {
2373         PopplerFormField *poppler_field;
2374
2375         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2376         if (!poppler_field)
2377                 return;
2378         
2379         poppler_form_field_button_set_state (poppler_field, state);
2380         PDF_DOCUMENT (document)->forms_modified = TRUE;
2381 }
2382
2383 static gboolean
2384 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document, 
2385                                                 EvFormField     *field)
2386 {
2387         PopplerFormField *poppler_field;
2388         gboolean state;
2389
2390         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2391         if (!poppler_field)
2392                 return FALSE;
2393
2394         state = poppler_form_field_button_get_state (poppler_field);
2395
2396         return state;
2397 }
2398
2399 static gchar *
2400 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document, 
2401                                                EvFormField     *field,
2402                                                gint             index)
2403 {
2404         PopplerFormField *poppler_field;
2405         gchar *text;
2406
2407         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2408         if (!poppler_field)
2409                 return NULL;
2410
2411         text = poppler_form_field_choice_get_item (poppler_field, index);
2412
2413         return text;
2414 }
2415
2416 static int
2417 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document, 
2418                                                   EvFormField     *field)
2419 {
2420         PopplerFormField *poppler_field;
2421         gint n_items;
2422
2423         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2424         if (!poppler_field)
2425                 return -1;
2426         
2427         n_items = poppler_form_field_choice_get_n_items (poppler_field);
2428
2429         return n_items;
2430 }
2431
2432 static gboolean
2433 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document, 
2434                                                        EvFormField     *field,
2435                                                        gint             index)
2436 {
2437         PopplerFormField *poppler_field;
2438         gboolean selected;
2439
2440         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2441         if (!poppler_field)
2442                 return FALSE;
2443
2444         selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2445
2446         return selected;
2447 }
2448
2449 static void
2450 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document, 
2451                                                   EvFormField     *field,
2452                                                   gint             index)
2453 {
2454         PopplerFormField *poppler_field;
2455
2456         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2457         if (!poppler_field)
2458                 return;
2459
2460         poppler_form_field_choice_select_item (poppler_field, index);
2461         PDF_DOCUMENT (document)->forms_modified = TRUE;
2462 }
2463
2464 static void
2465 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document, 
2466                                                   EvFormField     *field,
2467                                                   gint             index)
2468 {
2469         PopplerFormField *poppler_field;
2470
2471         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2472         if (!poppler_field)
2473                 return;
2474
2475         poppler_form_field_choice_toggle_item (poppler_field, index);
2476         PDF_DOCUMENT (document)->forms_modified = TRUE;
2477 }
2478
2479 static void
2480 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document, 
2481                                                    EvFormField     *field)
2482 {
2483         PopplerFormField *poppler_field;
2484
2485         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2486         if (!poppler_field)
2487                 return;
2488         
2489         poppler_form_field_choice_unselect_all (poppler_field);
2490         PDF_DOCUMENT (document)->forms_modified = TRUE;
2491 }
2492
2493 static void
2494 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2495                                                EvFormField     *field,
2496                                                const gchar     *text)
2497 {
2498         PopplerFormField *poppler_field;
2499
2500         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2501         if (!poppler_field)
2502                 return;
2503         
2504         poppler_form_field_choice_set_text (poppler_field, text);
2505         PDF_DOCUMENT (document)->forms_modified = TRUE;
2506 }
2507
2508 static gchar *
2509 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2510                                                EvFormField     *field)
2511 {
2512         PopplerFormField *poppler_field;
2513         gchar *text;
2514
2515         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2516         if (!poppler_field)
2517                 return NULL;
2518
2519         text = poppler_form_field_choice_get_text (poppler_field);
2520
2521         return text;
2522 }
2523
2524 static void
2525 pdf_document_document_forms_iface_init (EvDocumentFormsInterface *iface)
2526 {
2527         iface->get_form_fields = pdf_document_forms_get_form_fields;
2528         iface->document_is_modified = pdf_document_forms_document_is_modified;
2529         iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2530         iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2531         iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2532         iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2533         iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2534         iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2535         iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2536         iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2537         iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2538         iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2539         iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2540         iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2541 }
2542
2543 /* Annotations */
2544 static void
2545 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2546                                   GdkColor     *color)
2547 {
2548         PopplerColor *poppler_color;
2549
2550         poppler_color = poppler_annot_get_color (poppler_annot);
2551         if (poppler_color) {
2552                 color->red = poppler_color->red;
2553                 color->green = poppler_color->green;
2554                 color->blue = poppler_color->blue;
2555
2556                 g_free (poppler_color);
2557         } /* TODO: else use a default color */
2558 }
2559
2560 static EvAnnotationTextIcon
2561 get_annot_text_icon (PopplerAnnotText *poppler_annot)
2562 {
2563 #ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
2564         gchar *icon = poppler_annot_text_get_icon (poppler_annot);
2565         EvAnnotationTextIcon retval;
2566
2567         if (!icon)
2568                 return EV_ANNOTATION_TEXT_ICON_UNKNOWN;
2569
2570         if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_NOTE) == 0)
2571                 retval = EV_ANNOTATION_TEXT_ICON_NOTE;
2572         else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_COMMENT) == 0)
2573                 retval = EV_ANNOTATION_TEXT_ICON_COMMENT;
2574         else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_KEY) == 0)
2575                 retval = EV_ANNOTATION_TEXT_ICON_KEY;
2576         else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_HELP) == 0)
2577                 retval = EV_ANNOTATION_TEXT_ICON_HELP;
2578         else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_NEW_PARAGRAPH) == 0)
2579                 retval = EV_ANNOTATION_TEXT_ICON_NEW_PARAGRAPH;
2580         else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_PARAGRAPH) == 0)
2581                 retval = EV_ANNOTATION_TEXT_ICON_PARAGRAPH;
2582         else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_INSERT) == 0)
2583                 retval = EV_ANNOTATION_TEXT_ICON_INSERT;
2584         else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_CROSS) == 0)
2585                 retval = EV_ANNOTATION_TEXT_ICON_CROSS;
2586         else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_CIRCLE) == 0)
2587                 retval = EV_ANNOTATION_TEXT_ICON_CIRCLE;
2588         else
2589                 retval = EV_ANNOTATION_TEXT_ICON_UNKNOWN;
2590
2591         g_free (icon);
2592
2593         return retval;
2594 #else
2595         return EV_ANNOTATION_TEXT_ICON_UNKNOWN;
2596 #endif
2597 }
2598
2599 static const gchar *
2600 get_poppler_annot_text_icon (EvAnnotationTextIcon icon)
2601 {
2602 #ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
2603         switch (icon) {
2604         case EV_ANNOTATION_TEXT_ICON_NOTE:
2605                 return POPPLER_ANNOT_TEXT_ICON_NOTE;
2606         case EV_ANNOTATION_TEXT_ICON_COMMENT:
2607                 return POPPLER_ANNOT_TEXT_ICON_COMMENT;
2608         case EV_ANNOTATION_TEXT_ICON_KEY:
2609                 return POPPLER_ANNOT_TEXT_ICON_KEY;
2610         case EV_ANNOTATION_TEXT_ICON_HELP:
2611                 return POPPLER_ANNOT_TEXT_ICON_HELP;
2612         case EV_ANNOTATION_TEXT_ICON_NEW_PARAGRAPH:
2613                 return POPPLER_ANNOT_TEXT_ICON_NEW_PARAGRAPH;
2614         case EV_ANNOTATION_TEXT_ICON_PARAGRAPH:
2615                 return POPPLER_ANNOT_TEXT_ICON_PARAGRAPH;
2616         case EV_ANNOTATION_TEXT_ICON_INSERT:
2617                 return POPPLER_ANNOT_TEXT_ICON_INSERT;
2618         case EV_ANNOTATION_TEXT_ICON_CROSS:
2619                 return POPPLER_ANNOT_TEXT_ICON_CROSS;
2620         case EV_ANNOTATION_TEXT_ICON_CIRCLE:
2621                 return POPPLER_ANNOT_TEXT_ICON_CIRCLE;
2622         case EV_ANNOTATION_TEXT_ICON_UNKNOWN:
2623         default:
2624                 return POPPLER_ANNOT_TEXT_ICON_NOTE;
2625         }
2626 #else
2627         return "Note";
2628 #endif
2629 }
2630
2631 static EvAnnotation *
2632 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2633                              EvPage       *page)
2634 {
2635         EvAnnotation *ev_annot = NULL;
2636         const gchar  *unimplemented_annot = NULL;
2637
2638         switch (poppler_annot_get_annot_type (poppler_annot)) {
2639                 case POPPLER_ANNOT_TEXT: {
2640                         PopplerAnnotText *poppler_text;
2641                         EvAnnotationText *ev_annot_text;
2642
2643                         poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2644
2645                         ev_annot = ev_annotation_text_new (page);
2646
2647                         ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2648                         ev_annotation_text_set_is_open (ev_annot_text,
2649                                                         poppler_annot_text_get_is_open (poppler_text));
2650                         ev_annotation_text_set_icon (ev_annot_text, get_annot_text_icon (poppler_text));
2651                 }
2652                         break;
2653                 case POPPLER_ANNOT_FILE_ATTACHMENT: {
2654                         PopplerAnnotFileAttachment *poppler_annot_attachment;
2655                         EvAnnotationAttachment     *ev_annot_attachment;
2656                         PopplerAttachment          *poppler_attachment;
2657                         gchar                      *data = NULL;
2658                         gsize                       size;
2659                         GError                     *error = NULL;
2660
2661                         poppler_annot_attachment = POPPLER_ANNOT_FILE_ATTACHMENT (poppler_annot);
2662                         poppler_attachment = poppler_annot_file_attachment_get_attachment (poppler_annot_attachment);
2663
2664                         if (poppler_attachment &&
2665                             attachment_save_to_buffer (poppler_attachment, &data, &size, &error)) {
2666                                 EvAttachment *ev_attachment;
2667
2668                                 ev_attachment = ev_attachment_new (poppler_attachment->name,
2669                                                                    poppler_attachment->description,
2670                                                                    poppler_attachment->mtime,
2671                                                                    poppler_attachment->ctime,
2672                                                                    size, data);
2673                                 ev_annot = ev_annotation_attachment_new (page, ev_attachment);
2674                                 g_object_unref (ev_attachment);
2675                         } else if (error) {
2676                                 g_warning ("%s", error->message);
2677                                 g_error_free (error);
2678                         }
2679
2680                         if (poppler_attachment)
2681                                 g_object_unref (poppler_attachment);
2682                 }
2683                         break;
2684                 case POPPLER_ANNOT_LINK:
2685                 case POPPLER_ANNOT_WIDGET:
2686                         /* Ignore link and widgets annots since they are already handled */
2687                         break;
2688                 default: {
2689                         GEnumValue *enum_value;
2690
2691                         enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2692                                                        poppler_annot_get_annot_type (poppler_annot));
2693                         unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2694                 }
2695         }
2696
2697         if (unimplemented_annot) {
2698                 g_warning ("Unimplemented annotation: %s, please post a "
2699                            "bug report in Evince bugzilla "
2700                            "(http://bugzilla.gnome.org) with a testcase.",
2701                            unimplemented_annot);
2702         }
2703
2704         if (ev_annot) {
2705                 time_t   utime;
2706                 gchar   *modified;
2707                 gchar   *contents;
2708                 gchar   *name;
2709                 GdkColor color;
2710
2711                 contents = poppler_annot_get_contents (poppler_annot);
2712                 if (contents) {
2713                         ev_annotation_set_contents (ev_annot, contents);
2714                         g_free (contents);
2715                 }
2716
2717                 name = poppler_annot_get_name (poppler_annot);
2718                 if (name) {
2719                         ev_annotation_set_name (ev_annot, name);
2720                         g_free (name);
2721                 }
2722
2723                 modified = poppler_annot_get_modified (poppler_annot);
2724                 if (poppler_date_parse (modified, &utime)) {
2725                         ev_annotation_set_modified_from_time (ev_annot, utime);
2726                 } else {
2727                         ev_annotation_set_modified (ev_annot, modified);
2728                 }
2729                 g_free (modified);
2730
2731                 poppler_annot_color_to_gdk_color (poppler_annot, &color);
2732                 ev_annotation_set_color (ev_annot, &color);
2733
2734                 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2735                         PopplerAnnotMarkup *markup;
2736                         gchar *label;
2737                         gdouble opacity;
2738                         PopplerRectangle poppler_rect;
2739
2740                         markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2741
2742                         if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2743                                 EvRectangle ev_rect;
2744                                 gboolean is_open;
2745                                 gdouble height;
2746
2747                                 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2748                                                        NULL, &height);
2749                                 ev_rect.x1 = poppler_rect.x1;
2750                                 ev_rect.x2 = poppler_rect.x2;
2751                                 ev_rect.y1 = height - poppler_rect.y2;
2752                                 ev_rect.y2 = height - poppler_rect.y1;
2753
2754                                 is_open = poppler_annot_markup_get_popup_is_open (markup);
2755
2756                                 g_object_set (ev_annot,
2757                                               "rectangle", &ev_rect,
2758                                               "popup_is_open", is_open,
2759                                               "has_popup", TRUE,
2760                                               NULL);
2761                         } else {
2762                                 g_object_set (ev_annot,
2763                                               "has_popup", FALSE,
2764                                               NULL);
2765                         }
2766
2767                         label = poppler_annot_markup_get_label (markup);
2768                         opacity = poppler_annot_markup_get_opacity (markup);
2769
2770                         g_object_set (ev_annot,
2771                                       "label", label,
2772                                       "opacity", opacity,
2773                                       NULL);
2774
2775                         g_free (label);
2776                 }
2777         }
2778
2779         return ev_annot;
2780 }
2781
2782 static EvMappingList *
2783 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2784                                           EvPage                *page)
2785 {
2786         GList *retval = NULL;
2787         PdfDocument *pdf_document;
2788         PopplerPage *poppler_page;
2789         EvMappingList *mapping_list;
2790         GList *annots;
2791         GList *list;
2792         gdouble height;
2793         gint i = 0;
2794
2795         pdf_document = PDF_DOCUMENT (document_annotations);
2796         poppler_page = POPPLER_PAGE (page->backend_page);
2797
2798         if (pdf_document->annots) {
2799                 mapping_list = (EvMappingList *)g_hash_table_lookup (pdf_document->annots,
2800                                                                      GINT_TO_POINTER (page->index));
2801                 if (mapping_list)
2802                         return ev_mapping_list_ref (mapping_list);
2803         }
2804
2805         annots = poppler_page_get_annot_mapping (poppler_page);
2806         poppler_page_get_size (poppler_page, NULL, &height);
2807
2808         for (list = annots; list; list = list->next) {
2809                 PopplerAnnotMapping *mapping;
2810                 EvMapping           *annot_mapping;
2811                 EvAnnotation        *ev_annot;
2812
2813                 mapping = (PopplerAnnotMapping *)list->data;
2814
2815                 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2816                 if (!ev_annot)
2817                         continue;
2818
2819                 i++;
2820
2821                 /* Make sure annot has a unique name */
2822                 if (!ev_annotation_get_name (ev_annot)) {
2823                         gchar *name = g_strdup_printf ("annot-%d-%d", page->index, i);
2824
2825                         ev_annotation_set_name (ev_annot, name);
2826                         g_free (name);
2827                 }
2828
2829                 annot_mapping = g_new (EvMapping, 1);
2830                 annot_mapping->area.x1 = mapping->area.x1;
2831                 annot_mapping->area.x2 = mapping->area.x2;
2832                 annot_mapping->area.y1 = height - mapping->area.y2;
2833                 annot_mapping->area.y2 = height - mapping->area.y1;
2834                 annot_mapping->data = ev_annot;
2835
2836                 g_object_set_data_full (G_OBJECT (ev_annot),
2837                                         "poppler-annot",
2838                                         g_object_ref (mapping->annot),
2839                                         (GDestroyNotify) g_object_unref);
2840
2841                 retval = g_list_prepend (retval, annot_mapping);
2842         }
2843
2844         poppler_page_free_annot_mapping (annots);
2845
2846         if (!retval)
2847                 return NULL;
2848
2849         if (!pdf_document->annots) {
2850                 pdf_document->annots = g_hash_table_new_full (g_direct_hash,
2851                                                               g_direct_equal,
2852                                                               (GDestroyNotify)NULL,
2853                                                               (GDestroyNotify)ev_mapping_list_unref);
2854         }
2855
2856         mapping_list = ev_mapping_list_new (page->index, g_list_reverse (retval), (GDestroyNotify)g_object_unref);
2857         g_hash_table_insert (pdf_document->annots,
2858                              GINT_TO_POINTER (page->index),
2859                              ev_mapping_list_ref (mapping_list));
2860
2861         return mapping_list;
2862 }
2863
2864 static gboolean
2865 pdf_document_annotations_document_is_modified (EvDocumentAnnotations *document_annotations)
2866 {
2867         return PDF_DOCUMENT (document_annotations)->annots_modified;
2868 }
2869
2870 #ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
2871 static void
2872 pdf_document_annotations_add_annotation (EvDocumentAnnotations *document_annotations,
2873                                          EvAnnotation          *annot,
2874                                          EvRectangle           *rect)
2875 {
2876         PopplerAnnot    *poppler_annot;
2877         PdfDocument     *pdf_document;
2878         EvPage          *page;
2879         PopplerPage     *poppler_page;
2880         GList           *list = NULL;
2881         EvMappingList   *mapping_list;
2882         EvMapping       *annot_mapping;
2883         PopplerRectangle poppler_rect;
2884         gdouble          height;
2885         PopplerColor     poppler_color;
2886         GdkColor         color;
2887         time_t           utime;
2888         gchar           *modified;
2889         gchar           *name;
2890
2891         pdf_document = PDF_DOCUMENT (document_annotations);
2892         page = ev_annotation_get_page (annot);
2893         poppler_page = POPPLER_PAGE (page->backend_page);
2894
2895         poppler_page_get_size (poppler_page, NULL, &height);
2896         poppler_rect.x1 = rect->x1;
2897         poppler_rect.x2 = rect->x2;
2898         poppler_rect.y1 = height - rect->y2;
2899         poppler_rect.y2 = height - rect->y1;
2900         poppler_annot = poppler_annot_text_new (pdf_document->document, &poppler_rect);
2901
2902         ev_annotation_get_color (annot, &color);
2903         poppler_color.red = color.red;
2904         poppler_color.green = color.green;
2905         poppler_color.blue = color.blue;
2906         poppler_annot_set_color (poppler_annot, &poppler_color);
2907
2908         if (EV_IS_ANNOTATION_MARKUP (annot)) {
2909                 EvAnnotationMarkup *markup = EV_ANNOTATION_MARKUP (annot);
2910                 const gchar *label;
2911
2912                 if (ev_annotation_markup_has_popup (markup)) {
2913                         EvRectangle popup_rect;
2914
2915                         ev_annotation_markup_get_rectangle (markup, &popup_rect);
2916                         poppler_rect.x1 = popup_rect.x1;
2917                         poppler_rect.x2 = popup_rect.x2;
2918                         poppler_rect.y1 = height - popup_rect.y2;
2919                         poppler_rect.y2 = height - popup_rect.y1;
2920                         poppler_annot_markup_set_popup (POPPLER_ANNOT_MARKUP (poppler_annot), &poppler_rect);
2921                         poppler_annot_markup_set_popup_is_open (POPPLER_ANNOT_MARKUP (poppler_annot),
2922                                                                 ev_annotation_markup_get_popup_is_open (markup));
2923                 }
2924
2925                 label = ev_annotation_markup_get_label (markup);
2926                 if (label)
2927                         poppler_annot_markup_set_label (POPPLER_ANNOT_MARKUP (poppler_annot), label);
2928         }
2929
2930         if (EV_IS_ANNOTATION_TEXT (annot)) {
2931                 EvAnnotationText    *text = EV_ANNOTATION_TEXT (annot);
2932                 EvAnnotationTextIcon icon;
2933
2934                 icon = ev_annotation_text_get_icon (text);
2935                 poppler_annot_text_set_icon (POPPLER_ANNOT_TEXT (poppler_annot),
2936                                              get_poppler_annot_text_icon (icon));
2937         }
2938         poppler_page_add_annot (poppler_page, poppler_annot);
2939
2940         annot_mapping = g_new (EvMapping, 1);
2941         annot_mapping->area = *rect;
2942         annot_mapping->data = annot;
2943         g_object_set_data_full (G_OBJECT (annot),
2944                                 "poppler-annot",
2945                                 g_object_ref (poppler_annot),
2946                                 (GDestroyNotify) g_object_unref);
2947
2948         if (pdf_document->annots) {
2949                 mapping_list = (EvMappingList *)g_hash_table_lookup (pdf_document->annots,
2950                                                                      GINT_TO_POINTER (page->index));
2951                 list = ev_mapping_list_get_list (mapping_list);
2952                 name = g_strdup_printf ("annot-%d-%d", page->index, g_list_length (list) + 1);
2953                 ev_annotation_set_name (annot, name);
2954                 g_free (name);
2955                 list = g_list_append (list, annot_mapping);
2956         } else {
2957                 pdf_document->annots = g_hash_table_new_full (g_direct_hash,
2958                                                               g_direct_equal,
2959                                                               (GDestroyNotify)NULL,
2960                                                               (GDestroyNotify)ev_mapping_list_unref);
2961                 name = g_strdup_printf ("annot-%d-0", page->index);
2962                 ev_annotation_set_name (annot, name);
2963                 g_free (name);
2964                 list = g_list_append (list, annot_mapping);
2965                 mapping_list = ev_mapping_list_new (page->index, list, (GDestroyNotify)g_object_unref);
2966                 g_hash_table_insert (pdf_document->annots,
2967                                      GINT_TO_POINTER (page->index),
2968                                      ev_mapping_list_ref (mapping_list));
2969         }
2970
2971         pdf_document->annots_modified = TRUE;
2972 }
2973 #endif /* HAVE_POPPLER_PAGE_ADD_ANNOT */
2974
2975 static void
2976 pdf_document_annotations_save_annotation (EvDocumentAnnotations *document_annotations,
2977                                           EvAnnotation          *annot,
2978                                           EvAnnotationsSaveMask  mask)
2979 {
2980         PopplerAnnot *poppler_annot;
2981
2982         poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2983         if (!poppler_annot)
2984                 return;
2985
2986         if (mask & EV_ANNOTATIONS_SAVE_CONTENTS)
2987                 poppler_annot_set_contents (poppler_annot,
2988                                             ev_annotation_get_contents (annot));
2989
2990 #ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
2991         if (mask & EV_ANNOTATIONS_SAVE_COLOR) {
2992                 PopplerColor color;
2993                 GdkColor     ev_color;
2994
2995                 ev_annotation_get_color (annot, &ev_color);
2996                 color.red = ev_color.red;
2997                 color.green = ev_color.green;
2998                 color.blue = ev_color.blue;
2999                 poppler_annot_set_color (poppler_annot, &color);
3000         }
3001
3002         if (EV_IS_ANNOTATION_MARKUP (annot)) {
3003                 EvAnnotationMarkup *ev_markup = EV_ANNOTATION_MARKUP (annot);
3004                 PopplerAnnotMarkup *markup = POPPLER_ANNOT_MARKUP (poppler_annot);
3005
3006                 if (mask & EV_ANNOTATIONS_SAVE_LABEL)
3007                         poppler_annot_markup_set_label (markup, ev_annotation_markup_get_label (ev_markup));
3008                 if (mask & EV_ANNOTATIONS_SAVE_OPACITY)
3009                         poppler_annot_markup_set_opacity (markup, ev_annotation_markup_get_opacity (ev_markup));
3010                 if (mask & EV_ANNOTATIONS_SAVE_POPUP_IS_OPEN)
3011                         poppler_annot_markup_set_popup_is_open (markup, ev_annotation_markup_get_popup_is_open (ev_markup));
3012         }
3013
3014         if (EV_IS_ANNOTATION_TEXT (annot)) {
3015                 EvAnnotationText *ev_text = EV_ANNOTATION_TEXT (annot);
3016                 PopplerAnnotText *text = POPPLER_ANNOT_TEXT (poppler_annot);
3017
3018                 if (mask & EV_ANNOTATIONS_SAVE_TEXT_IS_OPEN) {
3019                         poppler_annot_text_set_is_open (text,
3020                                                         ev_annotation_text_get_is_open (ev_text));
3021                 }
3022                 if (mask & EV_ANNOTATIONS_SAVE_TEXT_ICON) {
3023                         EvAnnotationTextIcon icon;
3024
3025                         icon = ev_annotation_text_get_icon (ev_text);
3026                         poppler_annot_text_set_icon (text, get_poppler_annot_text_icon (icon));
3027                 }
3028         }
3029 #endif /* HAVE_POPPLER_PAGE_ADD_ANNOT */
3030         PDF_DOCUMENT (document_annotations)->annots_modified = TRUE;
3031 }
3032
3033 static void
3034 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsInterface *iface)
3035 {
3036         iface->get_annotations = pdf_document_annotations_get_annotations;
3037         iface->document_is_modified = pdf_document_annotations_document_is_modified;
3038 #ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
3039         iface->add_annotation = pdf_document_annotations_add_annotation;
3040 #endif
3041         iface->save_annotation = pdf_document_annotations_save_annotation;
3042 }
3043
3044 /* Attachments */
3045 struct SaveToBufferData {
3046         gchar *buffer;
3047         gsize len, max;
3048 };
3049
3050 static gboolean
3051 attachment_save_to_buffer_callback (const gchar  *buf,
3052                                     gsize         count,
3053                                     gpointer      user_data,
3054                                     GError      **error)
3055 {
3056         struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
3057         gchar *new_buffer;
3058         gsize new_max;
3059
3060         if (sdata->len + count > sdata->max) {
3061                 new_max = MAX (sdata->max * 2, sdata->len + count);
3062                 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
3063
3064                 sdata->buffer = new_buffer;
3065                 sdata->max = new_max;
3066         }
3067
3068         memcpy (sdata->buffer + sdata->len, buf, count);
3069         sdata->len += count;
3070
3071         return TRUE;
3072 }
3073
3074 static gboolean
3075 attachment_save_to_buffer (PopplerAttachment  *attachment,
3076                            gchar             **buffer,
3077                            gsize              *buffer_size,
3078                            GError            **error)
3079 {
3080         static const gint initial_max = 1024;
3081         struct SaveToBufferData sdata;
3082
3083         *buffer = NULL;
3084         *buffer_size = 0;
3085
3086         sdata.buffer = (gchar *) g_malloc (initial_max);
3087         sdata.max = initial_max;
3088         sdata.len = 0;
3089
3090         if (! poppler_attachment_save_to_callback (attachment,
3091                                                    attachment_save_to_buffer_callback,
3092                                                    &sdata,
3093                                                    error)) {
3094                 g_free (sdata.buffer);
3095                 return FALSE;
3096         }
3097
3098         *buffer = sdata.buffer;
3099         *buffer_size = sdata.len;
3100
3101         return TRUE;
3102 }
3103
3104 static GList *
3105 pdf_document_attachments_get_attachments (EvDocumentAttachments *document)
3106 {
3107         PdfDocument *pdf_document = PDF_DOCUMENT (document);
3108         GList *attachments;
3109         GList *list;
3110         GList *retval = NULL;
3111
3112         attachments = poppler_document_get_attachments (pdf_document->document);
3113
3114         for (list = attachments; list; list = list->next) {
3115                 PopplerAttachment *attachment;
3116                 EvAttachment *ev_attachment;
3117                 gchar *data = NULL;
3118                 gsize size;
3119                 GError *error = NULL;
3120
3121                 attachment = (PopplerAttachment *) list->data;
3122
3123                 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
3124                         ev_attachment = ev_attachment_new (attachment->name,
3125                                                            attachment->description,
3126                                                            attachment->mtime,
3127                                                            attachment->ctime,
3128                                                            size, data);
3129
3130                         retval = g_list_prepend (retval, ev_attachment);
3131                 } else {
3132                         if (error) {
3133                                 g_warning ("%s", error->message);
3134                                 g_error_free (error);
3135
3136                                 g_free (data);
3137                         }
3138                 }
3139
3140                 g_object_unref (attachment);
3141         }
3142
3143         return g_list_reverse (retval);
3144 }
3145
3146 static gboolean
3147 pdf_document_attachments_has_attachments (EvDocumentAttachments *document)
3148 {
3149         PdfDocument *pdf_document = PDF_DOCUMENT (document);
3150
3151         return poppler_document_has_attachments (pdf_document->document);
3152 }
3153
3154 static void
3155 pdf_document_document_attachments_iface_init (EvDocumentAttachmentsInterface *iface)
3156 {
3157         iface->has_attachments = pdf_document_attachments_has_attachments;
3158         iface->get_attachments = pdf_document_attachments_get_attachments;
3159 }
3160
3161 /* Layers */
3162 static gboolean
3163 pdf_document_layers_has_layers (EvDocumentLayers *document)
3164 {
3165         PdfDocument *pdf_document = PDF_DOCUMENT (document);
3166         PopplerLayersIter *iter;
3167
3168         iter = poppler_layers_iter_new (pdf_document->document);
3169         if (!iter)
3170                 return FALSE;
3171         poppler_layers_iter_free (iter);
3172
3173         return TRUE;
3174 }
3175
3176 static void
3177 build_layers_tree (PdfDocument       *pdf_document,
3178                    GtkTreeModel      *model,
3179                    GtkTreeIter       *parent,
3180                    PopplerLayersIter *iter)
3181 {
3182         do {
3183                 GtkTreeIter        tree_iter;
3184                 PopplerLayersIter *child;
3185                 PopplerLayer      *layer;
3186                 EvLayer           *ev_layer = NULL;
3187                 gboolean           visible;
3188                 gchar             *markup;
3189                 gint               rb_group = 0;
3190
3191                 layer = poppler_layers_iter_get_layer (iter);
3192                 if (layer) {
3193                         markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
3194                         visible = poppler_layer_is_visible (layer);
3195                         rb_group = poppler_layer_get_radio_button_group_id (layer);
3196                         ev_layer = ev_layer_new (poppler_layer_is_parent (layer),
3197                                                  rb_group);
3198                         g_object_set_data_full (G_OBJECT (ev_layer),
3199                                                 "poppler-layer",
3200                                                 g_object_ref (layer),
3201                                                 (GDestroyNotify) g_object_unref);
3202                 } else {
3203                         gchar *title;
3204
3205                         title = poppler_layers_iter_get_title (iter);
3206                         markup = g_markup_escape_text (title, -1);
3207                         g_free (title);
3208
3209                         visible = FALSE;
3210                         layer = NULL;
3211                 }
3212
3213                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
3214                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
3215                                     EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
3216                                     EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
3217                                     EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
3218                                     EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
3219                                     EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
3220                                     EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
3221                                     -1);
3222                 if (ev_layer)
3223                         g_object_unref (ev_layer);
3224                 g_free (markup);
3225
3226                 child = poppler_layers_iter_get_child (iter);
3227                 if (child)
3228                         build_layers_tree (pdf_document, model, &tree_iter, child);
3229                 poppler_layers_iter_free (child);
3230         } while (poppler_layers_iter_next (iter));
3231 }
3232
3233 static GtkTreeModel *
3234 pdf_document_layers_get_layers (EvDocumentLayers *document)
3235 {
3236         GtkTreeModel *model = NULL;
3237         PdfDocument *pdf_document = PDF_DOCUMENT (document);
3238         PopplerLayersIter *iter;
3239
3240         iter = poppler_layers_iter_new (pdf_document->document);
3241         if (iter) {
3242                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
3243                                                              G_TYPE_STRING,  /* TITLE */
3244                                                              G_TYPE_OBJECT,  /* LAYER */
3245                                                              G_TYPE_BOOLEAN, /* VISIBLE */
3246                                                              G_TYPE_BOOLEAN, /* ENABLED */
3247                                                              G_TYPE_BOOLEAN, /* SHOWTOGGLE */
3248                                                              G_TYPE_INT);    /* RBGROUP */
3249                 build_layers_tree (pdf_document, model, NULL, iter);
3250                 poppler_layers_iter_free (iter);
3251         }
3252         return model;
3253 }
3254
3255 static void
3256 pdf_document_layers_show_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_show (poppler_layer);
3264 }
3265
3266 static void
3267 pdf_document_layers_hide_layer (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         poppler_layer_hide (poppler_layer);
3275 }
3276
3277 static gboolean
3278 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
3279                                       EvLayer          *layer)
3280 {
3281         PdfDocument  *pdf_document = PDF_DOCUMENT (document);
3282         PopplerLayer *poppler_layer;
3283
3284         poppler_layer = POPPLER_LAYER (g_object_get_data (G_OBJECT (layer), "poppler-layer"));
3285         return poppler_layer_is_visible (poppler_layer);
3286 }
3287
3288 static void
3289 pdf_document_document_layers_iface_init (EvDocumentLayersInterface *iface)
3290 {
3291         iface->has_layers = pdf_document_layers_has_layers;
3292         iface->get_layers = pdf_document_layers_get_layers;
3293         iface->show_layer = pdf_document_layers_show_layer;
3294         iface->hide_layer = pdf_document_layers_hide_layer;
3295         iface->layer_is_visible = pdf_document_layers_layer_is_visible;
3296 }