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