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