]> www.fi.muni.cz Git - evince.git/blob - backend/pdf/ev-poppler.cc
[pdf] Implement EvDocumentText interface
[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 void
802 pdf_document_class_init (PdfDocumentClass *klass)
803 {
804         GObjectClass    *g_object_class = G_OBJECT_CLASS (klass);
805         EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
806
807         g_object_class->dispose = pdf_document_dispose;
808
809         ev_document_class->save = pdf_document_save;
810         ev_document_class->load = pdf_document_load;
811         ev_document_class->get_n_pages = pdf_document_get_n_pages;
812         ev_document_class->get_page = pdf_document_get_page;
813         ev_document_class->get_page_size = pdf_document_get_page_size;
814         ev_document_class->get_page_label = pdf_document_get_page_label;
815         ev_document_class->render = pdf_document_render;
816         ev_document_class->get_info = pdf_document_get_info;
817         ev_document_class->get_backend_info = pdf_document_get_backend_info;
818 }
819
820 /* EvDocumentSecurity */
821 static gboolean
822 pdf_document_has_document_security (EvDocumentSecurity *document_security)
823 {
824         /* FIXME: do we really need to have this? */
825         return FALSE;
826 }
827
828 static void
829 pdf_document_set_password (EvDocumentSecurity *document_security,
830                            const char         *password)
831 {
832         PdfDocument *document = PDF_DOCUMENT (document_security);
833
834         if (document->password)
835                 g_free (document->password);
836
837         document->password = g_strdup (password);
838 }
839
840 static void
841 pdf_document_security_iface_init (EvDocumentSecurityInterface *iface)
842 {
843         iface->has_document_security = pdf_document_has_document_security;
844         iface->set_password = pdf_document_set_password;
845 }
846
847 static gdouble
848 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
849 {
850         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
851         int n_pages;
852
853         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
854
855         return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
856 }
857
858 static gboolean
859 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
860                          int              n_pages)
861 {
862         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
863         gboolean result;
864
865         g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
866
867         if (pdf_document->font_info == NULL) { 
868                 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
869         }
870
871         if (pdf_document->fonts_iter) {
872                 poppler_fonts_iter_free (pdf_document->fonts_iter);
873         }
874
875         pdf_document->fonts_scanned_pages += n_pages;
876
877         result = poppler_font_info_scan (pdf_document->font_info, n_pages,
878                                          &pdf_document->fonts_iter);
879         if (!result) {
880                 pdf_document->fonts_scanned_pages = 0;
881                 poppler_font_info_free (pdf_document->font_info);
882                 pdf_document->font_info = NULL; 
883         }
884
885         return result;
886 }
887
888 static const char *
889 font_type_to_string (PopplerFontType type)
890 {
891         switch (type) {
892                 case POPPLER_FONT_TYPE_TYPE1:
893                         return _("Type 1");
894                 case POPPLER_FONT_TYPE_TYPE1C:
895                         return _("Type 1C");
896                 case POPPLER_FONT_TYPE_TYPE3:
897                         return _("Type 3");
898                 case POPPLER_FONT_TYPE_TRUETYPE:
899                         return _("TrueType");
900                 case POPPLER_FONT_TYPE_CID_TYPE0:
901                         return _("Type 1 (CID)");
902                 case POPPLER_FONT_TYPE_CID_TYPE0C:
903                         return _("Type 1C (CID)");
904                 case POPPLER_FONT_TYPE_CID_TYPE2:
905                         return _("TrueType (CID)");
906                 default:
907                         return _("Unknown font type");
908         }
909 }
910
911 static void
912 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
913                                GtkTreeModel    *model)
914 {
915         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
916         PopplerFontsIter *iter = pdf_document->fonts_iter;
917
918         g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
919
920         if (!iter)
921                 return;
922
923         do {
924                 GtkTreeIter list_iter;
925                 const char *name;
926                 const char *type;
927                 const char *embedded;
928                 char *details;
929                 
930                 name = poppler_fonts_iter_get_name (iter);
931
932                 if (name == NULL) {
933                         name = _("No name");
934                 }
935
936                 type = font_type_to_string (
937                         poppler_fonts_iter_get_font_type (iter));
938
939                 if (poppler_fonts_iter_is_embedded (iter)) {
940                         if (poppler_fonts_iter_is_subset (iter))
941                                 embedded = _("Embedded subset");
942                         else
943                                 embedded = _("Embedded");
944                 } else {
945                         embedded = _("Not embedded");
946                 }
947
948                 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
949
950                 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
951                 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
952                                     EV_DOCUMENT_FONTS_COLUMN_NAME, name,
953                                     EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
954                                     -1);
955
956                 g_free (details);
957         } while (poppler_fonts_iter_next (iter));
958 }
959
960 static void
961 pdf_document_document_fonts_iface_init (EvDocumentFontsInterface *iface)
962 {
963         iface->fill_model = pdf_document_fonts_fill_model;
964         iface->scan = pdf_document_fonts_scan;
965         iface->get_progress = pdf_document_fonts_get_progress;
966 }
967
968 static gboolean
969 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
970 {
971         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
972         PopplerIndexIter *iter;
973
974         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
975
976         iter = poppler_index_iter_new (pdf_document->document);
977         if (iter == NULL)
978                 return FALSE;
979         poppler_index_iter_free (iter);
980
981         return TRUE;
982 }
983
984 static EvLinkDest *
985 ev_link_dest_from_dest (PdfDocument *pdf_document,
986                         PopplerDest *dest)
987 {
988         EvLinkDest *ev_dest = NULL;
989         const char *unimplemented_dest = NULL;
990
991         g_assert (dest != NULL);
992
993         switch (dest->type) {
994                 case POPPLER_DEST_XYZ: {
995                         PopplerPage *poppler_page;
996                         double height;
997
998                         poppler_page = poppler_document_get_page (pdf_document->document,
999                                                                   MAX (0, dest->page_num - 1));
1000                         poppler_page_get_size (poppler_page, NULL, &height);
1001                         ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
1002                                                         dest->left,
1003                                                         height - MIN (height, dest->top),
1004                                                         dest->zoom,
1005                                                         dest->change_left,
1006                                                         dest->change_top,
1007                                                         dest->change_zoom);
1008                         g_object_unref (poppler_page);
1009                 }
1010                         break;
1011                 case POPPLER_DEST_FITB:
1012                 case POPPLER_DEST_FIT:
1013                         ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
1014                         break;
1015                 case POPPLER_DEST_FITBH:
1016                 case POPPLER_DEST_FITH: {
1017                         PopplerPage *poppler_page;
1018                         double height;
1019
1020                         poppler_page = poppler_document_get_page (pdf_document->document,
1021                                                                   MAX (0, dest->page_num - 1));
1022                         poppler_page_get_size (poppler_page, NULL, &height);
1023                         ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
1024                                                          height - MIN (height, dest->top),
1025                                                          dest->change_top);
1026                         g_object_unref (poppler_page);
1027                 }
1028                         break;
1029                 case POPPLER_DEST_FITBV:
1030                 case POPPLER_DEST_FITV:
1031                         ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
1032                                                          dest->left,
1033                                                          dest->change_left);
1034                         break;
1035                 case POPPLER_DEST_FITR: {
1036                         PopplerPage *poppler_page;
1037                         double height;
1038
1039                         poppler_page = poppler_document_get_page (pdf_document->document,
1040                                                                   MAX (0, dest->page_num - 1));
1041                         poppler_page_get_size (poppler_page, NULL, &height);
1042                         ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
1043                                                          dest->left,
1044                                                          height - MIN (height, dest->bottom),
1045                                                          dest->right,
1046                                                          height - MIN (height, dest->top));
1047                         g_object_unref (poppler_page);
1048                 }
1049                         break;
1050                 case POPPLER_DEST_NAMED:
1051                         ev_dest = ev_link_dest_new_named (dest->named_dest);
1052                         break;
1053                 case POPPLER_DEST_UNKNOWN:
1054                         unimplemented_dest = "POPPLER_DEST_UNKNOWN";
1055                         break;
1056         }
1057
1058         if (unimplemented_dest) {
1059                 g_warning ("Unimplemented destination: %s, please post a "
1060                            "bug report in Evince bugzilla "
1061                            "(http://bugzilla.gnome.org) with a testcase.",
1062                            unimplemented_dest);
1063         }
1064
1065         if (!ev_dest)
1066                 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
1067         
1068         return ev_dest;
1069 }
1070
1071 static EvLink *
1072 ev_link_from_action (PdfDocument   *pdf_document,
1073                      PopplerAction *action)
1074 {
1075         EvLink       *link = NULL;
1076         EvLinkAction *ev_action = NULL;
1077         const char   *unimplemented_action = NULL;
1078
1079         switch (action->type) {
1080                 case POPPLER_ACTION_NONE:
1081                         break;
1082                 case POPPLER_ACTION_GOTO_DEST: {
1083                         EvLinkDest *dest;
1084                         
1085                         dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
1086                         ev_action = ev_link_action_new_dest (dest);
1087                 }
1088                         break;
1089                 case POPPLER_ACTION_GOTO_REMOTE: {
1090                         EvLinkDest *dest;
1091                         
1092                         dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
1093                         ev_action = ev_link_action_new_remote (dest, 
1094                                                                action->goto_remote.file_name);
1095                         
1096                 }
1097                         break;
1098                 case POPPLER_ACTION_LAUNCH:
1099                         ev_action = ev_link_action_new_launch (action->launch.file_name,
1100                                                                action->launch.params);
1101                         break;
1102                 case POPPLER_ACTION_URI:
1103                         ev_action = ev_link_action_new_external_uri (action->uri.uri);
1104                         break;
1105                 case POPPLER_ACTION_NAMED:
1106                         ev_action = ev_link_action_new_named (action->named.named_dest);
1107                         break;
1108                 case POPPLER_ACTION_MOVIE:
1109                         unimplemented_action = "POPPLER_ACTION_MOVIE";
1110                         break;
1111 #if POPPLER_CHECK_VERSION (0, 13, 2)
1112                 case POPPLER_ACTION_RENDITION:
1113                         unimplemented_action = "POPPLER_ACTION_RENDITION";
1114                         break;
1115                 case POPPLER_ACTION_OCG_STATE:
1116                         unimplemented_action = "POPPLER_ACTION_OCG_STATE";
1117                         break;
1118 #endif
1119                 case POPPLER_ACTION_UNKNOWN:
1120                         unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1121         }
1122         
1123         if (unimplemented_action) {
1124                 g_warning ("Unimplemented action: %s, please post a bug report "
1125                            "in Evince bugzilla (http://bugzilla.gnome.org) "
1126                            "with a testcase.", unimplemented_action);
1127         }
1128         
1129         link = ev_link_new (action->any.title, ev_action);
1130         
1131         return link;    
1132 }
1133
1134 static void
1135 build_tree (PdfDocument      *pdf_document,
1136             GtkTreeModel     *model,
1137             GtkTreeIter      *parent,
1138             PopplerIndexIter *iter)
1139 {
1140         
1141         do {
1142                 GtkTreeIter tree_iter;
1143                 PopplerIndexIter *child;
1144                 PopplerAction *action;
1145                 EvLink *link = NULL;
1146                 gboolean expand;
1147                 char *title_markup;
1148                 
1149                 action = poppler_index_iter_get_action (iter);
1150                 expand = poppler_index_iter_is_open (iter);
1151
1152                 if (!action)
1153                         continue;
1154
1155                 switch (action->type) {
1156                         case POPPLER_ACTION_GOTO_DEST: {
1157                                 /* For bookmarks, solve named destinations */
1158                                 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1159                                         PopplerDest *dest;
1160                                         EvLinkDest *ev_dest = NULL;
1161                                         EvLinkAction *ev_action;
1162                                         
1163                                         dest = poppler_document_find_dest (pdf_document->document,
1164                                                                            action->goto_dest.dest->named_dest);
1165                                         if (!dest) {
1166                                                 link = ev_link_from_action (pdf_document, action);
1167                                                 break;
1168                                         }
1169                                         
1170                                         ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1171                                         poppler_dest_free (dest);
1172                                         
1173                                         ev_action = ev_link_action_new_dest (ev_dest);
1174                                         link = ev_link_new (action->any.title, ev_action);
1175                                 } else {
1176                                         link = ev_link_from_action (pdf_document, action);
1177                                 }
1178                         }
1179                                 break;
1180                         default:
1181                                 link = ev_link_from_action (pdf_document, action);
1182                                 break;
1183                 }
1184                 
1185                 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1186                         poppler_action_free (action);
1187                         if (link)
1188                                 g_object_unref (link);
1189                         
1190                         continue;
1191                 }
1192
1193                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1194                 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1195                 
1196                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1197                                     EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1198                                     EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1199                                     EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1200                                     -1);
1201                 
1202                 g_free (title_markup);
1203                 g_object_unref (link);
1204                 
1205                 child = poppler_index_iter_get_child (iter);
1206                 if (child)
1207                         build_tree (pdf_document, model, &tree_iter, child);
1208                 poppler_index_iter_free (child);
1209                 poppler_action_free (action);
1210                 
1211         } while (poppler_index_iter_next (iter));
1212 }
1213
1214 static GtkTreeModel *
1215 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1216 {
1217         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1218         GtkTreeModel *model = NULL;
1219         PopplerIndexIter *iter;
1220         
1221         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1222
1223         iter = poppler_index_iter_new (pdf_document->document);
1224         /* Create the model if we have items*/
1225         if (iter != NULL) {
1226                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1227                                                              G_TYPE_STRING,
1228                                                              G_TYPE_OBJECT,
1229                                                              G_TYPE_BOOLEAN,
1230                                                              G_TYPE_STRING);
1231                 build_tree (pdf_document, model, NULL, iter);
1232                 poppler_index_iter_free (iter);
1233         }
1234         
1235         return model;
1236 }
1237
1238 static GList *
1239 pdf_document_links_get_links (EvDocumentLinks *document_links,
1240                               EvPage          *page)
1241 {
1242         PdfDocument *pdf_document;
1243         PopplerPage *poppler_page;
1244         GList *retval = NULL;
1245         GList *mapping_list;
1246         GList *list;
1247         double height;
1248
1249         pdf_document = PDF_DOCUMENT (document_links);
1250         poppler_page = POPPLER_PAGE (page->backend_page);
1251         mapping_list = poppler_page_get_link_mapping (poppler_page);
1252         poppler_page_get_size (poppler_page, NULL, &height);
1253
1254         for (list = mapping_list; list; list = list->next) {
1255                 PopplerLinkMapping *link_mapping;
1256                 EvMapping *ev_link_mapping;
1257
1258                 link_mapping = (PopplerLinkMapping *)list->data;
1259                 ev_link_mapping = g_new (EvMapping, 1);
1260                 ev_link_mapping->data = ev_link_from_action (pdf_document,
1261                                                              link_mapping->action);
1262                 ev_link_mapping->area.x1 = link_mapping->area.x1;
1263                 ev_link_mapping->area.x2 = link_mapping->area.x2;
1264                 /* Invert this for X-style coordinates */
1265                 ev_link_mapping->area.y1 = height - link_mapping->area.y2;
1266                 ev_link_mapping->area.y2 = height - link_mapping->area.y1;
1267
1268                 retval = g_list_prepend (retval, ev_link_mapping);
1269         }
1270
1271         poppler_page_free_link_mapping (mapping_list);
1272
1273         return g_list_reverse (retval);
1274 }
1275
1276 static EvLinkDest *
1277 pdf_document_links_find_link_dest (EvDocumentLinks  *document_links,
1278                                    const gchar      *link_name)
1279 {
1280         PdfDocument *pdf_document;
1281         PopplerDest *dest;
1282         EvLinkDest *ev_dest = NULL;
1283
1284         pdf_document = PDF_DOCUMENT (document_links);
1285         dest = poppler_document_find_dest (pdf_document->document,
1286                                            link_name);
1287         if (dest) {
1288                 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1289                 poppler_dest_free (dest);
1290         }
1291
1292         return ev_dest;
1293 }
1294
1295 static void
1296 pdf_document_document_links_iface_init (EvDocumentLinksInterface *iface)
1297 {
1298         iface->has_document_links = pdf_document_links_has_document_links;
1299         iface->get_links_model = pdf_document_links_get_links_model;
1300         iface->get_links = pdf_document_links_get_links;
1301         iface->find_link_dest = pdf_document_links_find_link_dest;
1302 }
1303
1304 static GList *
1305 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1306                                        EvPage           *page)
1307 {
1308         GList *retval = NULL;
1309         PdfDocument *pdf_document;
1310         PopplerPage *poppler_page;
1311         GList *mapping_list;
1312         GList *list;
1313
1314         pdf_document = PDF_DOCUMENT (document_images);
1315         poppler_page = POPPLER_PAGE (page->backend_page);
1316         mapping_list = poppler_page_get_image_mapping (poppler_page);
1317
1318         for (list = mapping_list; list; list = list->next) {
1319                 PopplerImageMapping *image_mapping;
1320                 EvMapping *ev_image_mapping;
1321
1322                 image_mapping = (PopplerImageMapping *)list->data;
1323
1324                 ev_image_mapping = g_new (EvMapping, 1);
1325                 
1326                 ev_image_mapping->data = ev_image_new (page->index, image_mapping->image_id);
1327                 ev_image_mapping->area.x1 = image_mapping->area.x1;
1328                 ev_image_mapping->area.y1 = image_mapping->area.y1;
1329                 ev_image_mapping->area.x2 = image_mapping->area.x2;
1330                 ev_image_mapping->area.y2 = image_mapping->area.y2;
1331
1332                 retval = g_list_prepend (retval, ev_image_mapping);
1333         }
1334
1335         poppler_page_free_image_mapping (mapping_list);
1336
1337         return g_list_reverse (retval);
1338 }
1339
1340 GdkPixbuf *
1341 pdf_document_images_get_image (EvDocumentImages *document_images,
1342                                EvImage          *image)
1343 {
1344         GdkPixbuf       *retval = NULL;
1345         PdfDocument     *pdf_document;
1346         PopplerPage     *poppler_page;
1347         cairo_surface_t *surface;
1348
1349         pdf_document = PDF_DOCUMENT (document_images);
1350         poppler_page = poppler_document_get_page (pdf_document->document,
1351                                                   ev_image_get_page (image));
1352
1353         surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1354         if (surface) {
1355                 retval = ev_document_misc_pixbuf_from_surface (surface);
1356                 cairo_surface_destroy (surface);
1357         }
1358
1359         g_object_unref (poppler_page);
1360
1361         return retval;
1362 }
1363
1364 static void
1365 pdf_document_document_images_iface_init (EvDocumentImagesInterface *iface)
1366 {
1367         iface->get_image_mapping = pdf_document_images_get_image_mapping;
1368         iface->get_image = pdf_document_images_get_image;
1369 }
1370
1371 static GdkPixbuf *
1372 make_thumbnail_for_page (PopplerPage     *poppler_page,
1373                          EvRenderContext *rc,
1374                          gint             width,
1375                          gint             height)
1376 {
1377         GdkPixbuf *pixbuf;
1378
1379 #ifdef POPPLER_WITH_GDK
1380         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1381                                  width, height);
1382         gdk_pixbuf_fill (pixbuf, 0xffffffff);
1383
1384         ev_document_fc_mutex_lock ();
1385         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1386                                        width, height,
1387                                        rc->scale, rc->rotation, pixbuf);
1388         ev_document_fc_mutex_unlock ();
1389 #else
1390         cairo_surface_t *surface;
1391
1392         ev_document_fc_mutex_lock ();
1393         surface = pdf_page_render (poppler_page, width, height, rc);
1394         ev_document_fc_mutex_unlock ();
1395         
1396         pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1397         cairo_surface_destroy (surface);
1398 #endif /* POPPLER_WITH_GDK */
1399
1400         return pixbuf;
1401 }
1402
1403 static GdkPixbuf *
1404 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1405                                        EvRenderContext      *rc, 
1406                                        gboolean              border)
1407 {
1408         PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1409         PopplerPage *poppler_page;
1410         GdkPixbuf *pixbuf = NULL;
1411         GdkPixbuf *border_pixbuf;
1412         gint width, height;
1413
1414         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1415
1416         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1417                                                 rc, &width, &height);
1418         
1419 #ifdef POPPLER_WITH_GDK
1420         pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1421 #else
1422         cairo_surface_t *surface;
1423         
1424         surface = poppler_page_get_thumbnail (poppler_page);
1425         if (surface) {
1426                 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1427                 cairo_surface_destroy (surface);
1428         }
1429 #endif /* POPPLER_WITH_GDK */
1430
1431         if (pixbuf != NULL) {
1432                 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1433                         gdk_pixbuf_get_height (pixbuf) :
1434                         gdk_pixbuf_get_width (pixbuf);
1435
1436                 if (thumb_width == width) {
1437                         GdkPixbuf *rotated_pixbuf;
1438
1439                         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1440                                                                    (GdkPixbufRotation) (360 - rc->rotation));
1441                         g_object_unref (pixbuf);
1442                         pixbuf = rotated_pixbuf;
1443                 } else {
1444                         /* The provided thumbnail has a different size */
1445                         g_object_unref (pixbuf);
1446                         pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1447                 }
1448         } else {
1449                 /* There is no provided thumbnail. We need to make one. */
1450                 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1451         }
1452
1453         if (border && pixbuf) {
1454                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1455                 g_object_unref (pixbuf);
1456                 pixbuf = border_pixbuf;
1457         }               
1458
1459         return pixbuf;
1460 }
1461
1462 static void
1463 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1464                                         EvRenderContext      *rc,
1465                                         gint                 *width,
1466                                         gint                 *height)
1467 {
1468         double page_width, page_height;
1469         
1470         poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1471                                &page_width, &page_height);
1472         
1473         *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1474         *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1475
1476         if (rc->rotation == 90 || rc->rotation == 270) {
1477                 gint  temp;
1478
1479                 temp = *width;
1480                 *width = *height;
1481                 *height = temp;
1482         }
1483 }
1484
1485 static void
1486 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface)
1487 {
1488         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1489         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1490 }
1491
1492
1493 static GList *
1494 pdf_document_find_find_text (EvDocumentFind *document_find,
1495                              EvPage         *page,
1496                              const gchar    *text,
1497                              gboolean        case_sensitive)
1498 {
1499         GList *matches, *l;
1500         PopplerPage *poppler_page;
1501         gdouble height;
1502         GList *retval = NULL;
1503
1504         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1505         g_return_val_if_fail (text != NULL, NULL);
1506
1507         poppler_page = POPPLER_PAGE (page->backend_page);
1508         
1509         matches = poppler_page_find_text (poppler_page, text);
1510         if (!matches)
1511                 return NULL;
1512
1513         poppler_page_get_size (poppler_page, NULL, &height);
1514         for (l = matches; l && l->data; l = g_list_next (l)) {
1515                 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1516                 EvRectangle      *ev_rect;
1517
1518                 ev_rect = ev_rectangle_new ();
1519                 ev_rect->x1 = rect->x1;
1520                 ev_rect->x2 = rect->x2;
1521                 /* Invert this for X-style coordinates */
1522                 ev_rect->y1 = height - rect->y2;
1523                 ev_rect->y2 = height - rect->y1;
1524
1525                 retval = g_list_prepend (retval, ev_rect);
1526         }
1527
1528         g_list_foreach (matches, (GFunc)poppler_rectangle_free, NULL);
1529         g_list_free (matches);
1530
1531         return g_list_reverse (retval);
1532 }
1533
1534 static void
1535 pdf_document_find_iface_init (EvDocumentFindInterface *iface)
1536 {
1537         iface->find_text = pdf_document_find_find_text;
1538 }
1539
1540 static void
1541 pdf_print_context_free (PdfPrintContext *ctx)
1542 {
1543         if (!ctx)
1544                 return;
1545
1546 #ifdef HAVE_CAIRO_PRINT
1547         if (ctx->cr) {
1548                 cairo_destroy (ctx->cr);
1549                 ctx->cr = NULL;
1550         }
1551 #else
1552         if (ctx->ps_file) {
1553                 poppler_ps_file_free (ctx->ps_file);
1554                 ctx->ps_file = NULL;
1555         }
1556 #endif
1557         g_free (ctx);
1558 }
1559
1560 static void
1561 pdf_document_file_exporter_begin (EvFileExporter        *exporter,
1562                                   EvFileExporterContext *fc)
1563 {
1564         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1565         PdfPrintContext *ctx;
1566 #ifdef HAVE_CAIRO_PRINT
1567         gdouble width, height;
1568         cairo_surface_t *surface = NULL;
1569 #endif
1570         
1571         if (pdf_document->print_ctx)
1572                 pdf_print_context_free (pdf_document->print_ctx);
1573         pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1574         ctx = pdf_document->print_ctx;
1575         ctx->format = fc->format;
1576         
1577 #ifdef HAVE_CAIRO_PRINT
1578         ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1579
1580         ctx->paper_width = fc->paper_width;
1581         ctx->paper_height = fc->paper_height;
1582         
1583         switch (fc->pages_per_sheet) {
1584                 default:
1585                 case 1:
1586                         ctx->pages_x = 1;
1587                         ctx->pages_y = 1;
1588                         break;
1589                 case 2:
1590                         ctx->pages_x = 1;
1591                         ctx->pages_y = 2;
1592                         break;
1593                 case 4:
1594                         ctx->pages_x = 2;
1595                         ctx->pages_y = 2;
1596                         break;
1597                 case 6:
1598                         ctx->pages_x = 2;
1599                         ctx->pages_y = 3;
1600                         break;
1601                 case 9:
1602                         ctx->pages_x = 3;
1603                         ctx->pages_y = 3;
1604                         break;
1605                 case 16:
1606                         ctx->pages_x = 4;
1607                         ctx->pages_y = 4;
1608                         break;
1609         }
1610
1611         ctx->pages_printed = 0;
1612         
1613         switch (fc->format) {
1614                 case EV_FILE_FORMAT_PS:
1615 #ifdef HAVE_CAIRO_PS
1616                         surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1617 #endif
1618                         break;
1619                 case EV_FILE_FORMAT_PDF:
1620 #ifdef HAVE_CAIRO_PDF
1621                         surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1622 #endif
1623                         break;
1624                 default:
1625                         g_assert_not_reached ();
1626         }
1627
1628         ctx->cr = cairo_create (surface);
1629         cairo_surface_destroy (surface);
1630
1631 #else /* HAVE_CAIRO_PRINT */
1632         if (ctx->format == EV_FILE_FORMAT_PS) {
1633                 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1634                                                     fc->filename, fc->first_page,
1635                                                     fc->last_page - fc->first_page + 1);
1636                 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1637                 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1638         }
1639 #endif /* HAVE_CAIRO_PRINT */
1640 }
1641
1642 static void
1643 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1644 {
1645         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1646         PdfPrintContext *ctx = pdf_document->print_ctx;
1647         
1648         g_return_if_fail (pdf_document->print_ctx != NULL);
1649
1650         ctx->pages_printed = 0;
1651         
1652 #ifdef HAVE_CAIRO_PRINT
1653         if (ctx->paper_width > ctx->paper_height) {
1654                 if (ctx->format == EV_FILE_FORMAT_PS) {
1655                         cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1656                                                    ctx->paper_height,
1657                                                    ctx->paper_width);
1658                 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1659                         cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1660                                                     ctx->paper_height,
1661                                                     ctx->paper_width);
1662                 }
1663         }
1664 #endif /* HAVE_CAIRO_PRINT */
1665 }
1666
1667 static void
1668 pdf_document_file_exporter_do_page (EvFileExporter  *exporter,
1669                                     EvRenderContext *rc)
1670 {
1671         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1672         PdfPrintContext *ctx = pdf_document->print_ctx;
1673         PopplerPage *poppler_page;
1674 #ifdef HAVE_CAIRO_PRINT
1675         gdouble  page_width, page_height;
1676         gint     x, y;
1677         gboolean rotate;
1678         gdouble  width, height;
1679         gdouble  pwidth, pheight;
1680         gdouble  xscale, yscale;
1681 #endif
1682
1683         g_return_if_fail (pdf_document->print_ctx != NULL);
1684
1685         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1686         
1687 #ifdef HAVE_CAIRO_PRINT
1688         x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1689         y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1690         poppler_page_get_size (poppler_page, &page_width, &page_height);
1691
1692         if (page_width > page_height && page_width > ctx->paper_width) {
1693                 rotate = TRUE;
1694         } else {
1695                 rotate = FALSE;
1696         }
1697
1698         /* Use always portrait mode and rotate when necessary */
1699         if (ctx->paper_width > ctx->paper_height) {
1700                 width = ctx->paper_height;
1701                 height = ctx->paper_width;
1702                 rotate = !rotate;
1703         } else {
1704                 width = ctx->paper_width;
1705                 height = ctx->paper_height;
1706         }
1707
1708         if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1709                 rotate = !rotate;
1710         }       
1711
1712         if (rotate) {
1713                 gint tmp1;
1714                 gdouble tmp2;
1715
1716                 tmp1 = x;
1717                 x = y;
1718                 y = tmp1;
1719
1720                 tmp2 = page_width;
1721                 page_width = page_height;
1722                 page_height = tmp2;
1723         }
1724
1725         pwidth = width / ctx->pages_x;
1726         pheight = height / ctx->pages_y;
1727
1728         if ((page_width > pwidth || page_height > pheight) ||
1729             (page_width < pwidth && page_height < pheight)) {
1730                 xscale = pwidth / page_width;
1731                 yscale = pheight / page_height;
1732                 
1733                 if (yscale < xscale) {
1734                         xscale = yscale;
1735                 } else {
1736                         yscale = xscale;
1737                 }
1738                 
1739         } else {        
1740                 xscale = yscale = 1;
1741         }
1742
1743         /* TODO: center */
1744
1745         cairo_save (ctx->cr);
1746         if (rotate) {
1747                 cairo_matrix_t matrix;
1748                 
1749                 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1750                 cairo_matrix_init (&matrix,
1751                                    0,  1,
1752                                    -1,  0,
1753                                    0,  0);
1754                 cairo_transform (ctx->cr, &matrix);
1755         }
1756         
1757         cairo_translate (ctx->cr,
1758                          x * (rotate ? pheight : pwidth),
1759                          y * (rotate ? pwidth : pheight));
1760         cairo_scale (ctx->cr, xscale, yscale);
1761
1762         poppler_page_render_for_printing (poppler_page, ctx->cr);
1763
1764         ctx->pages_printed++;
1765                         
1766         cairo_restore (ctx->cr);
1767 #else /* HAVE_CAIRO_PRINT */
1768         if (ctx->format == EV_FILE_FORMAT_PS)
1769                 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1770 #endif /* HAVE_CAIRO_PRINT */
1771 }
1772
1773 static void
1774 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1775 {
1776         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1777         PdfPrintContext *ctx = pdf_document->print_ctx;
1778         
1779         g_return_if_fail (pdf_document->print_ctx != NULL);
1780
1781 #ifdef HAVE_CAIRO_PRINT
1782         cairo_show_page (ctx->cr);
1783 #endif
1784 }
1785
1786 static void
1787 pdf_document_file_exporter_end (EvFileExporter *exporter)
1788 {
1789         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1790
1791         pdf_print_context_free (pdf_document->print_ctx);
1792         pdf_document->print_ctx = NULL;
1793 }
1794
1795 static EvFileExporterCapabilities
1796 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1797 {
1798         return  (EvFileExporterCapabilities) (
1799                 EV_FILE_EXPORTER_CAN_PAGE_SET |
1800                 EV_FILE_EXPORTER_CAN_COPIES |
1801                 EV_FILE_EXPORTER_CAN_COLLATE |
1802                 EV_FILE_EXPORTER_CAN_REVERSE |
1803                 EV_FILE_EXPORTER_CAN_SCALE |
1804 #ifdef HAVE_CAIRO_PRINT
1805                 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1806 #endif
1807                 
1808 #ifdef HAVE_CAIRO_PDF
1809                 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1810 #endif
1811                 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1812 }
1813
1814 static void
1815 pdf_document_file_exporter_iface_init (EvFileExporterInterface *iface)
1816 {
1817         iface->begin = pdf_document_file_exporter_begin;
1818         iface->begin_page = pdf_document_file_exporter_begin_page;
1819         iface->do_page = pdf_document_file_exporter_do_page;
1820         iface->end_page = pdf_document_file_exporter_end_page;
1821         iface->end = pdf_document_file_exporter_end;
1822         iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1823 }
1824
1825 /* EvDocumentPrint */
1826 static void
1827 pdf_document_print_print_page (EvDocumentPrint *document,
1828                                EvPage          *page,
1829                                cairo_t         *cr)
1830 {
1831         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1832
1833         poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1834 }
1835
1836 static void
1837 pdf_document_document_print_iface_init (EvDocumentPrintInterface *iface)
1838 {
1839         iface->print_page = pdf_document_print_print_page;
1840 }
1841
1842 static void
1843 pdf_selection_render_selection (EvSelection      *selection,
1844                                 EvRenderContext  *rc,
1845                                 cairo_surface_t **surface,
1846                                 EvRectangle      *points,
1847                                 EvRectangle      *old_points,
1848                                 EvSelectionStyle  style,
1849                                 GdkColor         *text,
1850                                 GdkColor         *base)
1851 {
1852         PopplerPage *poppler_page;
1853         cairo_t *cr;
1854         PopplerColor text_color, base_color;
1855         double width_points, height_points;
1856         gint width, height;
1857
1858         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1859
1860         poppler_page_get_size (poppler_page,
1861                                &width_points, &height_points);
1862         width = (int) ((width_points * rc->scale) + 0.5);
1863         height = (int) ((height_points * rc->scale) + 0.5);
1864
1865         text_color.red = text->red;
1866         text_color.green = text->green;
1867         text_color.blue = text->blue;
1868
1869         base_color.red = base->red;
1870         base_color.green = base->green;
1871         base_color.blue = base->blue;
1872
1873         if (*surface == NULL) {
1874                 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1875                                                        width, height);
1876                 
1877         }
1878
1879         cr = cairo_create (*surface);
1880         cairo_scale (cr, rc->scale, rc->scale);
1881         cairo_surface_set_device_offset (*surface, 0, 0);
1882         memset (cairo_image_surface_get_data (*surface), 0x00,
1883                 cairo_image_surface_get_height (*surface) *
1884                 cairo_image_surface_get_stride (*surface));
1885         poppler_page_render_selection (poppler_page,
1886                                        cr,
1887                                        (PopplerRectangle *)points,
1888                                        (PopplerRectangle *)old_points,
1889                                        (PopplerSelectionStyle)style,
1890                                        &text_color,
1891                                        &base_color);
1892         cairo_destroy (cr);
1893 }
1894
1895 static gchar *
1896 pdf_selection_get_selected_text (EvSelection     *selection,
1897                                  EvPage          *page,
1898                                  EvSelectionStyle style,
1899                                  EvRectangle     *points)
1900 {
1901         PopplerPage *poppler_page;
1902         PopplerRectangle r;
1903         double height;
1904         char *retval;
1905         
1906         poppler_page = POPPLER_PAGE (page->backend_page);
1907
1908         poppler_page_get_size (poppler_page, NULL, &height);
1909         r.x1 = points->x1;
1910         r.y1 = height - points->y2;
1911         r.x2 = points->x2;
1912         r.y2 = height - points->y1;
1913
1914         retval = poppler_page_get_text (poppler_page,
1915                                         (PopplerSelectionStyle)style,
1916                                         &r);
1917
1918         return retval;
1919 }
1920
1921 static GdkRegion *
1922 create_gdk_region_from_poppler_region (GList *region)
1923 {
1924         GList *l;
1925         GdkRegion *retval;
1926         
1927         retval = gdk_region_new ();
1928         
1929         for (l = region; l; l = g_list_next (l)) {
1930                 PopplerRectangle *rectangle;
1931                 GdkRectangle      rect;
1932                 
1933                 rectangle = (PopplerRectangle *)l->data;
1934                 
1935                 rect.x = (gint) rectangle->x1;
1936                 rect.y = (gint) rectangle->y1;
1937                 rect.width  = (gint) (rectangle->x2 - rectangle->x1);
1938                 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1939                 gdk_region_union_with_rect (retval, &rect);
1940                 
1941                 poppler_rectangle_free (rectangle);
1942         }
1943
1944         return retval;
1945 }
1946
1947 static GdkRegion *
1948 pdf_selection_get_selection_region (EvSelection     *selection,
1949                                     EvRenderContext *rc,
1950                                     EvSelectionStyle style,
1951                                     EvRectangle     *points)
1952 {
1953         PopplerPage *poppler_page;
1954         GdkRegion   *retval;
1955         GList       *region;
1956
1957         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1958         
1959         region = poppler_page_get_selection_region (poppler_page,
1960                                                     rc->scale,
1961                                                     (PopplerSelectionStyle)style,
1962                                                     (PopplerRectangle *) points);
1963         retval = create_gdk_region_from_poppler_region (region);
1964         g_list_free (region);
1965         
1966         return retval;
1967 }
1968
1969 static void
1970 pdf_selection_iface_init (EvSelectionInterface *iface)
1971 {
1972         iface->render_selection = pdf_selection_render_selection;
1973         iface->get_selected_text = pdf_selection_get_selected_text;
1974         iface->get_selection_region = pdf_selection_get_selection_region;
1975 }
1976
1977
1978 /* EvDocumentText */
1979 static GdkRegion *
1980 pdf_document_text_get_text_mapping (EvDocumentText *document_text,
1981                                     EvPage         *page)
1982 {
1983         PopplerPage *poppler_page;
1984         PopplerRectangle points;
1985         GList *region;
1986         GdkRegion *retval;
1987
1988         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1989
1990         poppler_page = POPPLER_PAGE (page->backend_page);
1991
1992         points.x1 = 0.0;
1993         points.y1 = 0.0;
1994         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1995
1996         region = poppler_page_get_selection_region (poppler_page, 1.0,
1997                                                     POPPLER_SELECTION_GLYPH,
1998                                                     &points);
1999         retval = create_gdk_region_from_poppler_region (region);
2000         g_list_free (region);
2001
2002         return retval;
2003 }
2004
2005 static gchar *
2006 pdf_document_text_get_text (EvDocumentText  *selection,
2007                             EvPage          *page)
2008 {
2009         PopplerPage *poppler_page;
2010         PopplerRectangle r;
2011
2012         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2013
2014         poppler_page = POPPLER_PAGE (page->backend_page);
2015
2016         r.x1 = 0;
2017         r.y1 = 0;
2018         poppler_page_get_size (poppler_page, &(r.x2), &(r.y2));
2019
2020         return poppler_page_get_text (poppler_page,
2021                                       POPPLER_SELECTION_WORD,
2022                                       &r);
2023 }
2024
2025 static gboolean
2026 pdf_document_text_get_text_layout (EvDocumentText  *selection,
2027                                    EvPage          *page,
2028                                    EvRectangle    **areas,
2029                                    guint           *n_areas)
2030 {
2031         PopplerPage *poppler_page;
2032
2033         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2034
2035         poppler_page = POPPLER_PAGE (page->backend_page);
2036
2037         return poppler_page_get_text_layout (poppler_page, (PopplerRectangle **)areas, n_areas);
2038 }
2039
2040 static void
2041 pdf_document_text_iface_init (EvDocumentTextInterface *iface)
2042 {
2043         iface->get_text_mapping = pdf_document_text_get_text_mapping;
2044         iface->get_text = pdf_document_text_get_text;
2045 #ifdef HAVE_POPPLER_PAGE_GET_TEXT_LAYOUT
2046         iface->get_text_layout = pdf_document_text_get_text_layout;
2047 #endif
2048 }
2049
2050 /* Page Transitions */
2051 static gdouble
2052 pdf_document_get_page_duration (EvDocumentTransition *trans,
2053                                 gint                  page)
2054 {
2055         PdfDocument *pdf_document;
2056         PopplerPage *poppler_page;
2057         gdouble      duration = -1;
2058
2059         pdf_document = PDF_DOCUMENT (trans);
2060         poppler_page = poppler_document_get_page (pdf_document->document, page);
2061         if (!poppler_page)
2062                 return -1;
2063
2064         duration = poppler_page_get_duration (poppler_page);
2065         g_object_unref (poppler_page);
2066
2067         return duration;
2068 }
2069
2070 static EvTransitionEffect *
2071 pdf_document_get_effect (EvDocumentTransition *trans,
2072                          gint                  page)
2073 {
2074         PdfDocument            *pdf_document;
2075         PopplerPage            *poppler_page;
2076         PopplerPageTransition  *page_transition;
2077         EvTransitionEffect     *effect;
2078
2079         pdf_document = PDF_DOCUMENT (trans);
2080         poppler_page = poppler_document_get_page (pdf_document->document, page);
2081
2082         if (!poppler_page)
2083                 return NULL;
2084
2085         page_transition = poppler_page_get_transition (poppler_page);
2086
2087         if (!page_transition) {
2088                 g_object_unref (poppler_page);
2089                 return NULL;
2090         }
2091
2092         /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2093         effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2094                                            "alignment", page_transition->alignment,
2095                                            "direction", page_transition->direction,
2096                                            "duration", page_transition->duration,
2097                                            "angle", page_transition->angle,
2098                                            "scale", page_transition->scale,
2099                                            "rectangular", page_transition->rectangular,
2100                                            NULL);
2101
2102         poppler_page_transition_free (page_transition);
2103         g_object_unref (poppler_page);
2104
2105         return effect;
2106 }
2107
2108 static void
2109 pdf_document_page_transition_iface_init (EvDocumentTransitionInterface *iface)
2110 {
2111         iface->get_page_duration = pdf_document_get_page_duration;
2112         iface->get_effect = pdf_document_get_effect;
2113 }
2114
2115 /* Forms */
2116 static void
2117 pdf_document_get_crop_box (EvDocument  *document, 
2118                            int          page, 
2119                            EvRectangle *rect)
2120 {
2121         PdfDocument *pdf_document;
2122         PopplerPage *poppler_page;
2123         PopplerRectangle poppler_rect;
2124
2125         pdf_document = PDF_DOCUMENT (document);
2126         poppler_page = poppler_document_get_page (pdf_document->document, page);
2127         poppler_page_get_crop_box (poppler_page, &poppler_rect);
2128         rect->x1 = poppler_rect.x1;
2129         rect->x2 = poppler_rect.x2;
2130         rect->y1 = poppler_rect.y1;
2131         rect->y2 = poppler_rect.y2;
2132 }
2133
2134 static EvFormField *
2135 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2136 {
2137         EvFormField *ev_field = NULL;
2138         gint         id;
2139         gdouble      font_size;
2140         gboolean     is_read_only;
2141
2142         id = poppler_form_field_get_id (poppler_field);
2143         font_size = poppler_form_field_get_font_size (poppler_field);
2144         is_read_only = poppler_form_field_is_read_only (poppler_field);
2145
2146         switch (poppler_form_field_get_field_type (poppler_field)) {
2147                 case POPPLER_FORM_FIELD_TEXT: {
2148                         EvFormFieldText    *field_text;
2149                         EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2150
2151                         switch (poppler_form_field_text_get_text_type (poppler_field)) {
2152                                 case POPPLER_FORM_TEXT_NORMAL:
2153                                         ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2154                                         break;
2155                                 case POPPLER_FORM_TEXT_MULTILINE:
2156                                         ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2157                                         break;
2158                                 case POPPLER_FORM_TEXT_FILE_SELECT:
2159                                         ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2160                                         break;
2161                         }
2162                         
2163                         ev_field = ev_form_field_text_new (id, ev_text_type);
2164                         field_text = EV_FORM_FIELD_TEXT (ev_field);
2165
2166                         field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2167                         field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2168                         field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2169                         field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2170                         field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2171                         field_text->text = poppler_form_field_text_get_text (poppler_field);
2172
2173                 }
2174                         break;
2175                 case POPPLER_FORM_FIELD_BUTTON: {
2176                         EvFormFieldButton    *field_button;
2177                         EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2178
2179                         switch (poppler_form_field_button_get_button_type (poppler_field)) {
2180                                 case POPPLER_FORM_BUTTON_PUSH:
2181                                         ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2182                                         break;
2183                                 case POPPLER_FORM_BUTTON_CHECK:
2184                                         ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2185                                         break;
2186                                 case POPPLER_FORM_BUTTON_RADIO:
2187                                         ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2188                                         break;
2189                         }
2190
2191                         ev_field = ev_form_field_button_new (id, ev_button_type);
2192                         field_button = EV_FORM_FIELD_BUTTON (ev_field);
2193                         
2194                         field_button->state = poppler_form_field_button_get_state (poppler_field);
2195                 }
2196                         break;
2197                 case POPPLER_FORM_FIELD_CHOICE: {
2198                         EvFormFieldChoice    *field_choice;
2199                         EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2200
2201                         switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2202                                 case POPPLER_FORM_CHOICE_COMBO:
2203                                         ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2204                                         break;
2205                                 case EV_FORM_FIELD_CHOICE_LIST:
2206                                         ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2207                                         break;
2208                         }
2209
2210                         ev_field = ev_form_field_choice_new (id, ev_choice_type);
2211                         field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2212
2213                         field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2214                         field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2215                         field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2216                         field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2217
2218                         /* TODO: we need poppler_form_field_choice_get_selected_items in poppler 
2219                         field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2220                         if (field_choice->is_editable)
2221                                 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2222                 }
2223                         break;
2224                 case POPPLER_FORM_FIELD_SIGNATURE:
2225                         /* TODO */
2226                         ev_field = ev_form_field_signature_new (id);
2227                         break;
2228                 case POPPLER_FORM_FIELD_UNKNOWN:
2229                         return NULL;
2230         }
2231
2232         ev_field->font_size = font_size;
2233         ev_field->is_read_only = is_read_only;
2234
2235         return ev_field;
2236 }
2237
2238 static GList *
2239 pdf_document_forms_get_form_fields (EvDocumentForms *document, 
2240                                     EvPage          *page)
2241 {
2242         PopplerPage *poppler_page;
2243         GList *retval = NULL;
2244         GList *fields;
2245         GList *list;
2246         double height;
2247
2248         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2249         
2250         poppler_page = POPPLER_PAGE (page->backend_page);
2251         fields = poppler_page_get_form_field_mapping (poppler_page);
2252         poppler_page_get_size (poppler_page, NULL, &height);
2253
2254         for (list = fields; list; list = list->next) {
2255                 PopplerFormFieldMapping *mapping;
2256                 EvMapping *field_mapping;
2257                 EvFormField *ev_field;
2258
2259                 mapping = (PopplerFormFieldMapping *)list->data;
2260
2261                 ev_field = ev_form_field_from_poppler_field (mapping->field);
2262                 if (!ev_field)
2263                         continue;
2264
2265                 field_mapping = g_new0 (EvMapping, 1);
2266                 field_mapping->area.x1 = mapping->area.x1;
2267                 field_mapping->area.x2 = mapping->area.x2;
2268                 field_mapping->area.y1 = height - mapping->area.y2;
2269                 field_mapping->area.y2 = height - mapping->area.y1;
2270                 field_mapping->data = ev_field;
2271                 ev_field->page = EV_PAGE (g_object_ref (page));
2272
2273                 g_object_set_data_full (G_OBJECT (ev_field),
2274                                         "poppler-field",
2275                                         g_object_ref (mapping->field),
2276                                         (GDestroyNotify) g_object_unref);
2277                 
2278                 retval = g_list_prepend (retval, field_mapping);
2279         }
2280         
2281         poppler_page_free_form_field_mapping (fields);
2282
2283         return g_list_reverse (retval);
2284 }
2285
2286 static gchar *
2287 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2288                                              EvFormField     *field)
2289         
2290 {
2291         PopplerFormField *poppler_field;
2292         gchar *text;
2293
2294         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2295         if (!poppler_field)
2296                 return NULL;
2297         
2298         text = poppler_form_field_text_get_text (poppler_field);
2299
2300         return text;
2301 }
2302
2303 static void
2304 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document, 
2305                                              EvFormField     *field,
2306                                              const gchar     *text)
2307 {
2308         PopplerFormField *poppler_field;
2309
2310         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2311         if (!poppler_field)
2312                 return;
2313         
2314         poppler_form_field_text_set_text (poppler_field, text);
2315         PDF_DOCUMENT (document)->modified = TRUE;
2316 }
2317
2318 static void
2319 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document, 
2320                                                 EvFormField     *field,
2321                                                 gboolean         state)
2322 {
2323         PopplerFormField *poppler_field;
2324
2325         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2326         if (!poppler_field)
2327                 return;
2328         
2329         poppler_form_field_button_set_state (poppler_field, state);
2330         PDF_DOCUMENT (document)->modified = TRUE;
2331 }
2332
2333 static gboolean
2334 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document, 
2335                                                 EvFormField     *field)
2336 {
2337         PopplerFormField *poppler_field;
2338         gboolean state;
2339
2340         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2341         if (!poppler_field)
2342                 return FALSE;
2343
2344         state = poppler_form_field_button_get_state (poppler_field);
2345
2346         return state;
2347 }
2348
2349 static gchar *
2350 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document, 
2351                                                EvFormField     *field,
2352                                                gint             index)
2353 {
2354         PopplerFormField *poppler_field;
2355         gchar *text;
2356
2357         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2358         if (!poppler_field)
2359                 return NULL;
2360
2361         text = poppler_form_field_choice_get_item (poppler_field, index);
2362
2363         return text;
2364 }
2365
2366 static int
2367 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document, 
2368                                                   EvFormField     *field)
2369 {
2370         PopplerFormField *poppler_field;
2371         gint n_items;
2372
2373         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2374         if (!poppler_field)
2375                 return -1;
2376         
2377         n_items = poppler_form_field_choice_get_n_items (poppler_field);
2378
2379         return n_items;
2380 }
2381
2382 static gboolean
2383 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document, 
2384                                                        EvFormField     *field,
2385                                                        gint             index)
2386 {
2387         PopplerFormField *poppler_field;
2388         gboolean selected;
2389
2390         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2391         if (!poppler_field)
2392                 return FALSE;
2393
2394         selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2395
2396         return selected;
2397 }
2398
2399 static void
2400 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document, 
2401                                                   EvFormField     *field,
2402                                                   gint             index)
2403 {
2404         PopplerFormField *poppler_field;
2405
2406         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2407         if (!poppler_field)
2408                 return;
2409
2410         poppler_form_field_choice_select_item (poppler_field, index);
2411         PDF_DOCUMENT (document)->modified = TRUE;
2412 }
2413
2414 static void
2415 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document, 
2416                                                   EvFormField     *field,
2417                                                   gint             index)
2418 {
2419         PopplerFormField *poppler_field;
2420
2421         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2422         if (!poppler_field)
2423                 return;
2424
2425         poppler_form_field_choice_toggle_item (poppler_field, index);
2426         PDF_DOCUMENT (document)->modified = TRUE;
2427 }
2428
2429 static void
2430 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document, 
2431                                                    EvFormField     *field)
2432 {
2433         PopplerFormField *poppler_field;
2434
2435         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2436         if (!poppler_field)
2437                 return;
2438         
2439         poppler_form_field_choice_unselect_all (poppler_field);
2440         PDF_DOCUMENT (document)->modified = TRUE;
2441 }
2442
2443 static void
2444 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2445                                                EvFormField     *field,
2446                                                const gchar     *text)
2447 {
2448         PopplerFormField *poppler_field;
2449
2450         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2451         if (!poppler_field)
2452                 return;
2453         
2454         poppler_form_field_choice_set_text (poppler_field, text);
2455         PDF_DOCUMENT (document)->modified = TRUE;
2456 }
2457
2458 static gchar *
2459 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2460                                                EvFormField     *field)
2461 {
2462         PopplerFormField *poppler_field;
2463         gchar *text;
2464
2465         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2466         if (!poppler_field)
2467                 return NULL;
2468
2469         text = poppler_form_field_choice_get_text (poppler_field);
2470
2471         return text;
2472 }
2473
2474 static void
2475 pdf_document_document_forms_iface_init (EvDocumentFormsInterface *iface)
2476 {
2477         iface->get_form_fields = pdf_document_forms_get_form_fields;
2478         iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2479         iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2480         iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2481         iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2482         iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2483         iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2484         iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2485         iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2486         iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2487         iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2488         iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2489         iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2490 }
2491
2492 /* Annotations */
2493 static void
2494 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2495                                   GdkColor     *color)
2496 {
2497         PopplerColor *poppler_color;
2498
2499         poppler_color = poppler_annot_get_color (poppler_annot);
2500         if (poppler_color) {
2501                 color->red = poppler_color->red;
2502                 color->green = poppler_color->green;
2503                 color->blue = poppler_color->blue;
2504
2505                 g_free (poppler_color);
2506         } /* TODO: else use a default color */
2507 }
2508
2509 static EvAnnotation *
2510 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2511                              EvPage       *page)
2512 {
2513         EvAnnotation *ev_annot = NULL;
2514         const gchar  *unimplemented_annot = NULL;
2515
2516         switch (poppler_annot_get_annot_type (poppler_annot)) {
2517                 case POPPLER_ANNOT_TEXT: {
2518                         PopplerAnnotText *poppler_text;
2519                         EvAnnotationText *ev_annot_text;
2520
2521                         poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2522
2523                         ev_annot = ev_annotation_text_new (page);
2524
2525                         ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2526                         ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2527                 }
2528                         break;
2529                 case POPPLER_ANNOT_FILE_ATTACHMENT: {
2530                         PopplerAnnotFileAttachment *poppler_annot_attachment;
2531                         EvAnnotationAttachment     *ev_annot_attachment;
2532                         PopplerAttachment          *poppler_attachment;
2533                         gchar                      *data = NULL;
2534                         gsize                       size;
2535                         GError                     *error = NULL;
2536
2537                         poppler_annot_attachment = POPPLER_ANNOT_FILE_ATTACHMENT (poppler_annot);
2538                         poppler_attachment = poppler_annot_file_attachment_get_attachment (poppler_annot_attachment);
2539
2540                         if (poppler_attachment &&
2541                             attachment_save_to_buffer (poppler_attachment, &data, &size, &error)) {
2542                                 EvAttachment *ev_attachment;
2543
2544                                 ev_attachment = ev_attachment_new (poppler_attachment->name,
2545                                                                    poppler_attachment->description,
2546                                                                    poppler_attachment->mtime,
2547                                                                    poppler_attachment->ctime,
2548                                                                    size, data);
2549                                 ev_annot = ev_annotation_attachment_new (page, ev_attachment);
2550                                 g_object_unref (ev_attachment);
2551                         } else if (error) {
2552                                 g_warning ("%s", error->message);
2553                                 g_error_free (error);
2554                         }
2555
2556                         if (poppler_attachment)
2557                                 g_object_unref (poppler_attachment);
2558                 }
2559                         break;
2560                 case POPPLER_ANNOT_LINK:
2561                 case POPPLER_ANNOT_WIDGET:
2562                         /* Ignore link and widgets annots since they are already handled */
2563                         break;
2564                 default: {
2565                         GEnumValue *enum_value;
2566
2567                         enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2568                                                        poppler_annot_get_annot_type (poppler_annot));
2569                         unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2570                 }
2571         }
2572
2573         if (unimplemented_annot) {
2574                 g_warning ("Unimplemented annotation: %s, please post a "
2575                            "bug report in Evince bugzilla "
2576                            "(http://bugzilla.gnome.org) with a testcase.",
2577                            unimplemented_annot);
2578         }
2579
2580         if (ev_annot) {
2581                 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2582                 ev_annot->name = poppler_annot_get_name (poppler_annot);
2583                 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2584                 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2585
2586                 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2587                         PopplerAnnotMarkup *markup;
2588                         gchar *label;
2589                         gdouble opacity;
2590                         PopplerRectangle poppler_rect;
2591
2592                         markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2593
2594                         if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2595                                 EvRectangle ev_rect;
2596                                 gboolean is_open;
2597                                 gdouble height;
2598
2599                                 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2600                                                        NULL, &height);
2601                                 ev_rect.x1 = poppler_rect.x1;
2602                                 ev_rect.x2 = poppler_rect.x2;
2603                                 ev_rect.y1 = height - poppler_rect.y2;
2604                                 ev_rect.y2 = height - poppler_rect.y1;
2605
2606                                 is_open = poppler_annot_markup_get_popup_is_open (markup);
2607
2608                                 g_object_set (ev_annot,
2609                                               "rectangle", &ev_rect,
2610                                               "is_open", is_open,
2611                                               "has_popup", TRUE,
2612                                               NULL);
2613                         } else {
2614                                 /* FIXME: Use poppler_annot_markup_has_popup() when
2615                                  * new poppler is released.
2616                                  */
2617                                 g_object_set (ev_annot,
2618                                               "has_popup", FALSE,
2619                                               NULL);
2620                         }
2621
2622                         label = poppler_annot_markup_get_label (markup);
2623                         opacity = poppler_annot_markup_get_opacity (markup);
2624
2625                         g_object_set (ev_annot,
2626                                       "label", label,
2627                                       "opacity", opacity,
2628                                       NULL);
2629
2630                         g_free (label);
2631                 }
2632         }
2633
2634         return ev_annot;
2635 }
2636
2637 static GList *
2638 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2639                                           EvPage                *page)
2640 {
2641         GList *retval = NULL;
2642         PdfDocument *pdf_document;
2643         PopplerPage *poppler_page;
2644         GList *annots;
2645         GList *list;
2646         gdouble height;
2647         gint i = 0;
2648
2649         pdf_document = PDF_DOCUMENT (document_annotations);
2650         poppler_page = POPPLER_PAGE (page->backend_page);
2651         annots = poppler_page_get_annot_mapping (poppler_page);
2652         poppler_page_get_size (poppler_page, NULL, &height);
2653
2654         for (list = annots; list; list = list->next) {
2655                 PopplerAnnotMapping *mapping;
2656                 EvMapping *annot_mapping;
2657                 EvAnnotation        *ev_annot;
2658
2659                 mapping = (PopplerAnnotMapping *)list->data;
2660
2661                 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2662                 if (!ev_annot)
2663                         continue;
2664
2665                 i++;
2666
2667                 /* Make sure annot has a unique name */
2668                 if (!ev_annot->name)
2669                         ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2670
2671                 annot_mapping = g_new (EvMapping, 1);
2672                 annot_mapping->area.x1 = mapping->area.x1;
2673                 annot_mapping->area.x2 = mapping->area.x2;
2674                 annot_mapping->area.y1 = height - mapping->area.y2;
2675                 annot_mapping->area.y2 = height - mapping->area.y1;
2676                 annot_mapping->data = ev_annot;
2677
2678                 g_object_set_data_full (G_OBJECT (ev_annot),
2679                                         "poppler-annot",
2680                                         g_object_ref (mapping->annot),
2681                                         (GDestroyNotify) g_object_unref);
2682
2683                 retval = g_list_prepend (retval, annot_mapping);
2684         }
2685
2686         poppler_page_free_annot_mapping (annots);
2687
2688         return g_list_reverse (retval);
2689 }
2690
2691 static void
2692 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2693                                                   EvAnnotation          *annot,
2694                                                   const gchar           *contents)
2695 {
2696         PopplerAnnot *poppler_annot;
2697
2698         poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2699         if (!poppler_annot)
2700                 return;
2701
2702         poppler_annot_set_contents (poppler_annot, contents);
2703         PDF_DOCUMENT (document)->modified = TRUE;
2704 }
2705
2706 static void
2707 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsInterface *iface)
2708 {
2709         iface->get_annotations = pdf_document_annotations_get_annotations;
2710         iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2711 }
2712
2713 /* Attachments */
2714 struct SaveToBufferData {
2715         gchar *buffer;
2716         gsize len, max;
2717 };
2718
2719 static gboolean
2720 attachment_save_to_buffer_callback (const gchar  *buf,
2721                                     gsize         count,
2722                                     gpointer      user_data,
2723                                     GError      **error)
2724 {
2725         struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
2726         gchar *new_buffer;
2727         gsize new_max;
2728
2729         if (sdata->len + count > sdata->max) {
2730                 new_max = MAX (sdata->max * 2, sdata->len + count);
2731                 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
2732
2733                 sdata->buffer = new_buffer;
2734                 sdata->max = new_max;
2735         }
2736
2737         memcpy (sdata->buffer + sdata->len, buf, count);
2738         sdata->len += count;
2739
2740         return TRUE;
2741 }
2742
2743 static gboolean
2744 attachment_save_to_buffer (PopplerAttachment  *attachment,
2745                            gchar             **buffer,
2746                            gsize              *buffer_size,
2747                            GError            **error)
2748 {
2749         static const gint initial_max = 1024;
2750         struct SaveToBufferData sdata;
2751
2752         *buffer = NULL;
2753         *buffer_size = 0;
2754
2755         sdata.buffer = (gchar *) g_malloc (initial_max);
2756         sdata.max = initial_max;
2757         sdata.len = 0;
2758
2759         if (! poppler_attachment_save_to_callback (attachment,
2760                                                    attachment_save_to_buffer_callback,
2761                                                    &sdata,
2762                                                    error)) {
2763                 g_free (sdata.buffer);
2764                 return FALSE;
2765         }
2766
2767         *buffer = sdata.buffer;
2768         *buffer_size = sdata.len;
2769
2770         return TRUE;
2771 }
2772
2773 static GList *
2774 pdf_document_attachments_get_attachments (EvDocumentAttachments *document)
2775 {
2776         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2777         GList *attachments;
2778         GList *list;
2779         GList *retval = NULL;
2780
2781         attachments = poppler_document_get_attachments (pdf_document->document);
2782
2783         for (list = attachments; list; list = list->next) {
2784                 PopplerAttachment *attachment;
2785                 EvAttachment *ev_attachment;
2786                 gchar *data = NULL;
2787                 gsize size;
2788                 GError *error = NULL;
2789
2790                 attachment = (PopplerAttachment *) list->data;
2791
2792                 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
2793                         ev_attachment = ev_attachment_new (attachment->name,
2794                                                            attachment->description,
2795                                                            attachment->mtime,
2796                                                            attachment->ctime,
2797                                                            size, data);
2798
2799                         retval = g_list_prepend (retval, ev_attachment);
2800                 } else {
2801                         if (error) {
2802                                 g_warning ("%s", error->message);
2803                                 g_error_free (error);
2804
2805                                 g_free (data);
2806                         }
2807                 }
2808
2809                 g_object_unref (attachment);
2810         }
2811
2812         return g_list_reverse (retval);
2813 }
2814
2815 static gboolean
2816 pdf_document_attachments_has_attachments (EvDocumentAttachments *document)
2817 {
2818         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2819
2820         return poppler_document_has_attachments (pdf_document->document);
2821 }
2822
2823 static void
2824 pdf_document_document_attachments_iface_init (EvDocumentAttachmentsInterface *iface)
2825 {
2826         iface->has_attachments = pdf_document_attachments_has_attachments;
2827         iface->get_attachments = pdf_document_attachments_get_attachments;
2828 }
2829
2830 /* Layers */
2831 static gboolean
2832 pdf_document_layers_has_layers (EvDocumentLayers *document)
2833 {
2834         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2835         PopplerLayersIter *iter;
2836
2837         iter = poppler_layers_iter_new (pdf_document->document);
2838         if (!iter)
2839                 return FALSE;
2840         poppler_layers_iter_free (iter);
2841
2842         return TRUE;
2843 }
2844
2845 static void
2846 build_layers_tree (PdfDocument       *pdf_document,
2847                    GtkTreeModel      *model,
2848                    GtkTreeIter       *parent,
2849                    PopplerLayersIter *iter)
2850 {
2851         do {
2852                 GtkTreeIter        tree_iter;
2853                 PopplerLayersIter *child;
2854                 PopplerLayer      *layer;
2855                 EvLayer           *ev_layer = NULL;
2856                 gboolean           visible;
2857                 gchar             *markup;
2858                 gint               rb_group = 0;
2859
2860                 layer = poppler_layers_iter_get_layer (iter);
2861                 if (layer) {
2862                         markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2863                         visible = poppler_layer_is_visible (layer);
2864                         rb_group = poppler_layer_get_radio_button_group_id (layer);
2865                         pdf_document->layers = g_list_append (pdf_document->layers,
2866                                                               g_object_ref (layer));
2867                         ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2868                                                  poppler_layer_is_parent (layer),
2869                                                  rb_group);
2870                 } else {
2871                         gchar *title;
2872
2873                         title = poppler_layers_iter_get_title (iter);
2874                         markup = g_markup_escape_text (title, -1);
2875                         g_free (title);
2876
2877                         visible = FALSE;
2878                         layer = NULL;
2879                 }
2880
2881                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2882                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2883                                     EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2884                                     EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2885                                     EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2886                                     EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2887                                     EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2888                                     EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2889                                     -1);
2890                 if (ev_layer)
2891                         g_object_unref (ev_layer);
2892                 g_free (markup);
2893
2894                 child = poppler_layers_iter_get_child (iter);
2895                 if (child)
2896                         build_layers_tree (pdf_document, model, &tree_iter, child);
2897                 poppler_layers_iter_free (child);
2898         } while (poppler_layers_iter_next (iter));
2899 }
2900
2901 static GtkTreeModel *
2902 pdf_document_layers_get_layers (EvDocumentLayers *document)
2903 {
2904         GtkTreeModel *model = NULL;
2905         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2906         PopplerLayersIter *iter;
2907
2908         iter = poppler_layers_iter_new (pdf_document->document);
2909         if (iter) {
2910                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2911                                                              G_TYPE_STRING,  /* TITLE */
2912                                                              G_TYPE_OBJECT,  /* LAYER */
2913                                                              G_TYPE_BOOLEAN, /* VISIBLE */
2914                                                              G_TYPE_BOOLEAN, /* ENABLED */
2915                                                              G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2916                                                              G_TYPE_INT);    /* RBGROUP */
2917                 build_layers_tree (pdf_document, model, NULL, iter);
2918                 poppler_layers_iter_free (iter);
2919         }
2920         return model;
2921 }
2922
2923 static void
2924 pdf_document_layers_show_layer (EvDocumentLayers *document,
2925                                 EvLayer          *layer)
2926 {
2927         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2928         guint        layer_id = ev_layer_get_id (layer);
2929
2930         poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2931 }
2932
2933 static void
2934 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2935                                 EvLayer          *layer)
2936 {
2937         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2938         guint        layer_id = ev_layer_get_id (layer);
2939
2940         poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2941 }
2942
2943 static gboolean
2944 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2945                                       EvLayer          *layer)
2946 {
2947         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2948         guint        layer_id = ev_layer_get_id (layer);
2949
2950         return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2951 }
2952
2953 static void
2954 pdf_document_document_layers_iface_init (EvDocumentLayersInterface *iface)
2955 {
2956         iface->has_layers = pdf_document_layers_has_layers;
2957         iface->get_layers = pdf_document_layers_get_layers;
2958         iface->show_layer = pdf_document_layers_show_layer;
2959         iface->hide_layer = pdf_document_layers_hide_layer;
2960         iface->layer_is_visible = pdf_document_layers_layer_is_visible;
2961 }