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