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