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