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