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