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