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