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