]> www.fi.muni.cz Git - evince.git/blob - backend/pdf/ev-poppler.cc
a3549b248b55d949ce44909763c049bda84c2cd6
[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 #include <glib/gi18n.h>
32
33 #include "ev-poppler.h"
34 #include "ev-file-exporter.h"
35 #include "ev-document-find.h"
36 #include "ev-document-misc.h"
37 #include "ev-document-links.h"
38 #include "ev-document-images.h"
39 #include "ev-document-fonts.h"
40 #include "ev-document-security.h"
41 #include "ev-document-thumbnails.h"
42 #include "ev-document-transition.h"
43 #include "ev-selection.h"
44 #include "ev-attachment.h"
45 #include "ev-image.h"
46
47 typedef struct {
48         PdfDocument *document;
49         char *text;
50         GList **pages;
51         guint idle;
52         int start_page;
53         int search_page;
54 } PdfDocumentSearch;
55
56 typedef struct {
57         EvFileExporterFormat format;
58         PopplerPSFile *ps_file;
59 #ifdef HAVE_CAIRO_PDF
60         cairo_t *pdf_cairo;
61 #endif
62 } PdfPrintContext;
63
64 struct _PdfDocumentClass
65 {
66         GObjectClass parent_class;
67 };
68
69 struct _PdfDocument
70 {
71         GObject parent_instance;
72
73         PopplerDocument *document;
74         gchar *password;
75
76         PopplerFontInfo *font_info;
77         PopplerFontsIter *fonts_iter;
78         int fonts_scanned_pages;
79
80         PdfDocumentSearch *search;
81         PdfPrintContext *print_ctx;
82 };
83
84 static void pdf_document_document_iface_init            (EvDocumentIface           *iface);
85 static void pdf_document_security_iface_init            (EvDocumentSecurityIface   *iface);
86 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
87 static void pdf_document_document_links_iface_init      (EvDocumentLinksIface      *iface);
88 static void pdf_document_document_images_iface_init     (EvDocumentImagesIface     *iface);
89 static void pdf_document_document_fonts_iface_init      (EvDocumentFontsIface      *iface);
90 static void pdf_document_find_iface_init                (EvDocumentFindIface       *iface);
91 static void pdf_document_file_exporter_iface_init       (EvFileExporterIface       *iface);
92 static void pdf_selection_iface_init                    (EvSelectionIface          *iface);
93 static void pdf_document_page_transition_iface_init     (EvDocumentTransitionIface *iface);
94 static void pdf_document_thumbnails_get_dimensions      (EvDocumentThumbnails      *document_thumbnails,
95                                                          EvRenderContext           *rc,
96                                                          gint                      *width,
97                                                          gint                      *height);
98 static int  pdf_document_get_n_pages                    (EvDocument                *document);
99
100 static EvLinkDest *ev_link_dest_from_dest   (PdfDocument       *pdf_document,
101                                              PopplerDest       *dest);
102 static EvLink     *ev_link_from_action      (PdfDocument       *pdf_document,
103                                              PopplerAction     *action);
104 static void        pdf_document_search_free (PdfDocumentSearch *search);
105 static void        pdf_print_context_free   (PdfPrintContext   *ctx);
106
107
108 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
109                          {
110                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
111                                                         pdf_document_document_iface_init);
112                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
113                                                         pdf_document_security_iface_init);
114                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
115                                                         pdf_document_document_thumbnails_iface_init);
116                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
117                                                         pdf_document_document_links_iface_init);
118                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
119                                                         pdf_document_document_images_iface_init);
120                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
121                                                         pdf_document_document_fonts_iface_init);
122                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
123                                                         pdf_document_find_iface_init);
124                                  G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
125                                                         pdf_document_file_exporter_iface_init);
126                                  G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
127                                                         pdf_selection_iface_init);
128                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
129                                                         pdf_document_page_transition_iface_init);
130                          });
131
132
133 static void
134 set_rc_data (PdfDocument     *pdf_document,
135              EvRenderContext *rc)
136 {
137         if (rc->data == NULL) {
138                 rc->data = poppler_document_get_page (pdf_document->document,
139                                                       rc->page);
140                 rc->destroy = g_object_unref;
141         } else {
142                 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
143         }
144 }
145
146 static void
147 pdf_document_search_free (PdfDocumentSearch   *search)
148 {
149         PdfDocument *pdf_document = search->document;
150         int n_pages;
151         int i;
152
153         if (search->idle != 0)
154                 g_source_remove (search->idle);
155
156         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
157         for (i = 0; i < n_pages; i++) {
158                 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
159                 g_list_free (search->pages[i]);
160         }
161         g_free (search->pages);
162         
163         g_free (search->text);
164         g_free (search);
165 }
166
167 static void
168 pdf_document_dispose (GObject *object)
169 {
170         PdfDocument *pdf_document = PDF_DOCUMENT(object);
171
172         if (pdf_document->print_ctx) {
173                 pdf_print_context_free (pdf_document->print_ctx);
174                 pdf_document->print_ctx = NULL;
175         }
176         
177         if (pdf_document->search) {
178                 pdf_document_search_free (pdf_document->search);
179                 pdf_document->search = NULL;
180         }
181
182         if (pdf_document->document) {
183                 g_object_unref (pdf_document->document);
184         }
185
186         if (pdf_document->font_info) { 
187                 poppler_font_info_free (pdf_document->font_info);
188         }
189
190         if (pdf_document->fonts_iter) {
191                 poppler_fonts_iter_free (pdf_document->fonts_iter);
192         }
193
194         G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
195 }
196
197 static void
198 pdf_document_class_init (PdfDocumentClass *klass)
199 {
200         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
201
202         g_object_class->dispose = pdf_document_dispose;
203 }
204
205 static void
206 pdf_document_init (PdfDocument *pdf_document)
207 {
208         pdf_document->password = NULL;
209 }
210
211 static void
212 convert_error (GError  *poppler_error,
213                GError **error)
214 {
215         if (poppler_error == NULL)
216                 return;
217
218         if (poppler_error->domain == POPPLER_ERROR) {
219                 /* convert poppler errors into EvDocument errors */
220                 gint code = EV_DOCUMENT_ERROR_INVALID;
221                 if (poppler_error->code == POPPLER_ERROR_INVALID)
222                         code = EV_DOCUMENT_ERROR_INVALID;
223                 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
224                         code = EV_DOCUMENT_ERROR_ENCRYPTED;
225                         
226
227                 g_set_error (error,
228                              EV_DOCUMENT_ERROR,
229                              code,
230                              poppler_error->message,
231                              NULL);
232         } else {
233                 g_propagate_error (error, poppler_error);
234         }
235 }
236
237
238 /* EvDocument */
239 static gboolean
240 pdf_document_save (EvDocument  *document,
241                    const char  *uri,
242                    GError     **error)
243 {
244         gboolean retval;
245         GError *poppler_error = NULL;
246
247         retval = poppler_document_save (PDF_DOCUMENT (document)->document,
248                                         uri,
249                                         &poppler_error);
250         if (! retval)
251                 convert_error (poppler_error, error);
252
253         return retval;
254 }
255
256 static gboolean
257 pdf_document_load (EvDocument   *document,
258                    const char   *uri,
259                    GError      **error)
260 {
261         GError *poppler_error = NULL;
262         PdfDocument *pdf_document = PDF_DOCUMENT (document);
263
264         pdf_document->document =
265                 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
266
267         if (pdf_document->document == NULL) {
268                 convert_error (poppler_error, error);
269                 return FALSE;
270         }
271
272         return TRUE;
273 }
274
275 static int
276 pdf_document_get_n_pages (EvDocument *document)
277 {
278         return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
279 }
280
281 static void
282 pdf_document_get_page_size (EvDocument   *document,
283                             int           page,
284                             double       *width,
285                             double       *height)
286 {
287         PdfDocument *pdf_document = PDF_DOCUMENT (document);
288         PopplerPage *poppler_page;
289
290         poppler_page = poppler_document_get_page (pdf_document->document, page);
291         poppler_page_get_size (poppler_page, width, height);
292         g_object_unref (poppler_page);
293 }
294
295 static char *
296 pdf_document_get_page_label (EvDocument *document,
297                              int         page)
298 {
299         PopplerPage *poppler_page;
300         char *label = NULL;
301
302         poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
303                                                   page);
304
305         g_object_get (G_OBJECT (poppler_page),
306                       "label", &label,
307                       NULL);
308         g_object_unref (poppler_page);
309
310         return label;
311 }
312
313 static gboolean
314 pdf_document_has_attachments (EvDocument *document)
315 {
316         PdfDocument *pdf_document;
317
318         pdf_document = PDF_DOCUMENT (document);
319
320         return poppler_document_has_attachments (pdf_document->document);
321 }
322
323 struct SaveToBufferData {
324         gchar *buffer;
325         gsize len, max;
326 };
327
328 static gboolean
329 attachment_save_to_buffer_callback (const gchar  *buf,
330                                     gsize         count,
331                                     gpointer      user_data,
332                                     GError      **error)
333 {
334         struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
335         gchar *new_buffer;
336         gsize new_max;
337
338         if (sdata->len + count > sdata->max) {
339                 new_max = MAX (sdata->max * 2, sdata->len + count);
340                 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
341
342                 sdata->buffer = new_buffer;
343                 sdata->max = new_max;
344         }
345         
346         memcpy (sdata->buffer + sdata->len, buf, count);
347         sdata->len += count;
348         
349         return TRUE;
350 }
351
352 static gboolean
353 attachment_save_to_buffer (PopplerAttachment  *attachment,
354                            gchar             **buffer,
355                            gsize              *buffer_size,
356                            GError            **error)
357 {
358         static const gint initial_max = 1024;
359         struct SaveToBufferData sdata;
360
361         *buffer = NULL;
362         *buffer_size = 0;
363
364         sdata.buffer = (gchar *) g_malloc (initial_max);
365         sdata.max = initial_max;
366         sdata.len = 0;
367
368         if (! poppler_attachment_save_to_callback (attachment,
369                                                    attachment_save_to_buffer_callback,
370                                                    &sdata,
371                                                    error)) {
372                 g_free (sdata.buffer);
373                 return FALSE;
374         }
375
376         *buffer = sdata.buffer;
377         *buffer_size = sdata.len;
378         
379         return TRUE;
380 }
381
382 static GList *
383 pdf_document_get_attachments (EvDocument *document)
384 {
385         PdfDocument *pdf_document;
386         GList *attachments;
387         GList *list;
388         GList *retval = NULL;
389
390         pdf_document = PDF_DOCUMENT (document);
391
392         if (!pdf_document_has_attachments (document))
393                 return NULL;
394
395         attachments = poppler_document_get_attachments (pdf_document->document);
396         
397         for (list = attachments; list; list = list->next) {
398                 PopplerAttachment *attachment;
399                 EvAttachment *ev_attachment;
400                 gchar *data = NULL;
401                 gsize size;
402                 GError *error = NULL;
403
404                 attachment = (PopplerAttachment *) list->data;
405
406                 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
407                         ev_attachment = ev_attachment_new (attachment->name,
408                                                            attachment->description,
409                                                            attachment->mtime,
410                                                            attachment->ctime,
411                                                            size, data);
412                         
413                         retval = g_list_prepend (retval, ev_attachment);
414                 } else {
415                         if (error) {
416                                 g_warning ("%s", error->message);
417                                 g_error_free (error);
418
419                                 g_free (data);
420                         }
421                 }
422
423                 g_object_unref (attachment);
424         }
425
426         return g_list_reverse (retval);
427 }
428
429 static GdkPixbuf *
430 pdf_document_render_pixbuf (EvDocument   *document,
431                             EvRenderContext *rc)
432 {
433         PdfDocument *pdf_document;
434         GdkPixbuf *pixbuf;
435         double width_points, height_points;
436         gint width, height;
437
438         pdf_document = PDF_DOCUMENT (document);
439
440         set_rc_data (pdf_document, rc);
441
442         poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
443
444         if (rc->rotation == 90 || rc->rotation == 270) {
445                 width = (int) ((height_points * rc->scale) + 0.5);
446                 height = (int) ((width_points * rc->scale) + 0.5);
447         } else {
448                 width = (int) ((width_points * rc->scale) + 0.5);
449                 height = (int) ((height_points * rc->scale) + 0.5);
450         }
451
452         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
453                                  FALSE, 8,
454                                  width, height);
455
456         poppler_page_render_to_pixbuf (POPPLER_PAGE (rc->data),
457                                        0, 0,
458                                        width, height,
459                                        rc->scale,
460                                        rc->rotation,
461                                        pixbuf);
462         
463         
464         return pixbuf;
465 }
466
467 /* EvDocumentSecurity */
468
469 static gboolean
470 pdf_document_has_document_security (EvDocumentSecurity *document_security)
471 {
472         /* FIXME: do we really need to have this? */
473         return FALSE;
474 }
475
476 static void
477 pdf_document_set_password (EvDocumentSecurity *document_security,
478                            const char         *password)
479 {
480         PdfDocument *document = PDF_DOCUMENT (document_security);
481
482         if (document->password)
483                 g_free (document->password);
484
485         document->password = g_strdup (password);
486 }
487
488 static gboolean
489 pdf_document_can_get_text (EvDocument *document)
490 {
491         return TRUE;
492 }
493
494 static EvDocumentInfo *
495 pdf_document_get_info (EvDocument *document)
496 {
497         EvDocumentInfo *info;
498         PopplerPageLayout layout;
499         PopplerPageMode mode;
500         PopplerViewerPreferences view_prefs;
501         PopplerPermissions permissions;
502
503         info = g_new0 (EvDocumentInfo, 1);
504
505         info->fields_mask = EV_DOCUMENT_INFO_TITLE |
506                             EV_DOCUMENT_INFO_FORMAT |
507                             EV_DOCUMENT_INFO_AUTHOR |
508                             EV_DOCUMENT_INFO_SUBJECT |
509                             EV_DOCUMENT_INFO_KEYWORDS |
510                             EV_DOCUMENT_INFO_LAYOUT |
511                             EV_DOCUMENT_INFO_START_MODE |
512                             EV_DOCUMENT_INFO_PERMISSIONS |
513                             EV_DOCUMENT_INFO_UI_HINTS |
514                             EV_DOCUMENT_INFO_CREATOR |
515                             EV_DOCUMENT_INFO_PRODUCER |
516                             EV_DOCUMENT_INFO_CREATION_DATE |
517                             EV_DOCUMENT_INFO_MOD_DATE |
518                             EV_DOCUMENT_INFO_LINEARIZED |
519                             EV_DOCUMENT_INFO_N_PAGES |
520                             EV_DOCUMENT_INFO_SECURITY | 
521                             EV_DOCUMENT_INFO_PAPER_SIZE;
522
523         g_object_get (PDF_DOCUMENT (document)->document,
524                       "title", &(info->title),
525                       "format", &(info->format),
526                       "author", &(info->author),
527                       "subject", &(info->subject),
528                       "keywords", &(info->keywords),
529                       "page-mode", &mode,
530                       "page-layout", &layout,
531                       "viewer-preferences", &view_prefs,
532                       "permissions", &permissions,
533                       "creator", &(info->creator),
534                       "producer", &(info->producer),
535                       "creation-date", &(info->creation_date),
536                       "mod-date", &(info->modified_date),
537                       "linearized", &(info->linearized),
538                       NULL);
539
540         pdf_document_get_page_size(document, 0,
541                                    &(info->paper_width),
542                                    &(info->paper_height));
543
544         // Convert to mm.
545         info->paper_width = info->paper_width / 72.0f * 25.4f;
546         info->paper_height = info->paper_height / 72.0f * 25.4f;
547
548         switch (layout) {
549                 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
550                         info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
551                         break;
552                 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
553                         info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
554                         break;
555                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
556                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
557                         break;
558                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
559                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
560                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
561                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
562                         break;
563                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
564                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
565                         break;
566                 default:
567                         break;
568         }
569
570         switch (mode) {
571                 case POPPLER_PAGE_MODE_NONE:
572                         info->mode = EV_DOCUMENT_MODE_NONE;
573                         break;
574                 case POPPLER_PAGE_MODE_USE_THUMBS:
575                         info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
576                         break;
577                 case POPPLER_PAGE_MODE_USE_OC:
578                         info->mode = EV_DOCUMENT_MODE_USE_OC;
579                         break;
580                 case POPPLER_PAGE_MODE_FULL_SCREEN:
581                         info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
582                         break;
583                 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
584                         info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
585                 default:
586                         break;
587         }
588
589         info->ui_hints = 0;
590         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
591                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
592         }
593         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
594                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
595         }
596         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
597                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
598         }
599         if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
600                 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
601         }
602         if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
603                 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
604         }
605         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
606                 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
607         }
608         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
609                 info->ui_hints |=  EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
610         }
611
612         info->permissions = 0;
613         if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
614                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
615         }
616         if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
617                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
618         }
619         if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
620                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
621         }
622         if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
623                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
624         }
625
626         info->n_pages = ev_document_get_n_pages (document);
627
628         if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
629                 /* translators: this is the document security state */
630                 info->security = g_strdup (_("Yes"));
631         } else {
632                 /* translators: this is the document security state */
633                 info->security = g_strdup (_("No"));
634         }
635
636         return info;
637 }
638
639 static char *
640 pdf_document_get_text (EvDocument *document, int page, EvRectangle *rect)
641 {
642         PdfDocument *pdf_document = PDF_DOCUMENT (document);
643         PopplerPage *poppler_page;
644         PopplerRectangle r;
645         double height;
646         char *text;
647         
648         poppler_page = poppler_document_get_page (pdf_document->document, page);
649         g_return_val_if_fail (poppler_page != NULL, NULL);
650
651         poppler_page_get_size (poppler_page, NULL, &height);
652         r.x1 = rect->x1;
653         r.y1 = height - rect->y2;
654         r.x2 = rect->x2;
655         r.y2 = height - rect->y1;
656
657         text = poppler_page_get_text (poppler_page, &r);
658
659         g_object_unref (poppler_page);
660
661         return text;
662 }
663
664 static void
665 pdf_document_document_iface_init (EvDocumentIface *iface)
666 {
667         iface->save = pdf_document_save;
668         iface->load = pdf_document_load;
669         iface->get_n_pages = pdf_document_get_n_pages;
670         iface->get_page_size = pdf_document_get_page_size;
671         iface->get_page_label = pdf_document_get_page_label;
672         iface->has_attachments = pdf_document_has_attachments;
673         iface->get_attachments = pdf_document_get_attachments;
674         iface->render_pixbuf = pdf_document_render_pixbuf;
675         iface->get_text = pdf_document_get_text;
676         iface->can_get_text = pdf_document_can_get_text;
677         iface->get_info = pdf_document_get_info;
678 };
679
680 static void
681 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
682 {
683         iface->has_document_security = pdf_document_has_document_security;
684         iface->set_password = pdf_document_set_password;
685 }
686
687 static gdouble
688 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
689 {
690         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
691         int n_pages;
692
693         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
694
695         return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
696 }
697
698 static gboolean
699 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
700                          int              n_pages)
701 {
702         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
703         gboolean result;
704
705         g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
706
707         if (pdf_document->font_info == NULL) { 
708                 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
709         }
710
711         if (pdf_document->fonts_iter) {
712                 poppler_fonts_iter_free (pdf_document->fonts_iter);
713         }
714
715         pdf_document->fonts_scanned_pages += n_pages;
716
717         result = poppler_font_info_scan (pdf_document->font_info, n_pages,
718                                          &pdf_document->fonts_iter);
719         if (!result) {
720                 pdf_document->fonts_scanned_pages = 0;
721                 poppler_font_info_free (pdf_document->font_info);
722                 pdf_document->font_info = NULL; 
723         }
724
725         return result;
726 }
727
728 static const char *
729 font_type_to_string (PopplerFontType type)
730 {
731         switch (type) {
732                 case POPPLER_FONT_TYPE_TYPE1:
733                         return _("Type 1");
734                 case POPPLER_FONT_TYPE_TYPE1C:
735                         return _("Type 1C");
736                 case POPPLER_FONT_TYPE_TYPE3:
737                         return _("Type 3");
738                 case POPPLER_FONT_TYPE_TRUETYPE:
739                         return _("TrueType");
740                 case POPPLER_FONT_TYPE_CID_TYPE0:
741                         return _("Type 1 (CID)");
742                 case POPPLER_FONT_TYPE_CID_TYPE0C:
743                         return _("Type 1C (CID)");
744                 case POPPLER_FONT_TYPE_CID_TYPE2:
745                         return _("TrueType (CID)");
746                 default:
747                         return _("Unknown font type");
748         }
749 }
750
751 static void
752 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
753                                GtkTreeModel    *model)
754 {
755         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
756         PopplerFontsIter *iter = pdf_document->fonts_iter;
757
758         g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
759
760         if (!iter)
761                 return;
762
763         do {
764                 GtkTreeIter list_iter;
765                 const char *name;
766                 const char *type;
767                 const char *embedded;
768                 char *details;
769                 
770                 name = poppler_fonts_iter_get_name (iter);
771
772                 if (name == NULL) {
773                         name = _("No name");
774                 }
775
776                 type = font_type_to_string (
777                         poppler_fonts_iter_get_font_type (iter));
778
779                 if (poppler_fonts_iter_is_embedded (iter)) {
780                         if (poppler_fonts_iter_is_subset (iter))
781                                 embedded = _("Embedded subset");
782                         else
783                                 embedded = _("Embedded");
784                 } else {
785                         embedded = _("Not embedded");
786                 }
787
788                 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
789
790                 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
791                 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
792                                     EV_DOCUMENT_FONTS_COLUMN_NAME, name,
793                                     EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
794                                     -1);
795
796                 g_free (details);
797         } while (poppler_fonts_iter_next (iter));
798 }
799
800 static void
801 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
802 {
803         iface->fill_model = pdf_document_fonts_fill_model;
804         iface->scan = pdf_document_fonts_scan;
805         iface->get_progress = pdf_document_fonts_get_progress;
806 }
807
808 static gboolean
809 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
810 {
811         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
812         PopplerIndexIter *iter;
813
814         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
815
816         iter = poppler_index_iter_new (pdf_document->document);
817         if (iter == NULL)
818                 return FALSE;
819         poppler_index_iter_free (iter);
820
821         return TRUE;
822 }
823
824 static EvLinkDest *
825 ev_link_dest_from_dest (PdfDocument *pdf_document,
826                         PopplerDest *dest)
827 {
828         EvLinkDest *ev_dest = NULL;
829         const char *unimplemented_dest = NULL;
830
831         g_assert (dest != NULL);
832
833         switch (dest->type) {
834                 case POPPLER_DEST_XYZ: {
835                         PopplerPage *poppler_page;
836                         double height;
837
838                         poppler_page = poppler_document_get_page (pdf_document->document,
839                                                                   MAX (0, dest->page_num - 1));
840                         poppler_page_get_size (poppler_page, NULL, &height);
841                         ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
842                                                         dest->left,
843                                                         height - dest->top,
844                                                         dest->zoom);
845                         g_object_unref (poppler_page);
846                 }
847                         break;
848                 case POPPLER_DEST_FIT:
849                         ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
850                         break;
851                 case POPPLER_DEST_FITH: {
852                         PopplerPage *poppler_page;
853                         double height;
854
855                         poppler_page = poppler_document_get_page (pdf_document->document,
856                                                                   MAX (0, dest->page_num - 1));
857                         poppler_page_get_size (poppler_page, NULL, &height);
858                         ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
859                                                          height - dest->top);
860                         g_object_unref (poppler_page);
861                 }
862                         break;
863                 case POPPLER_DEST_FITV:
864                         ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
865                                                          dest->left);
866                         break;
867                 case POPPLER_DEST_FITR: {
868                         PopplerPage *poppler_page;
869                         double height;
870
871                         poppler_page = poppler_document_get_page (pdf_document->document,
872                                                                   MAX (0, dest->page_num - 1));
873                         poppler_page_get_size (poppler_page, NULL, &height);
874                         ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
875                                                          dest->left,
876                                                          height - dest->bottom,
877                                                          dest->right,
878                                                          height - dest->top);
879                         g_object_unref (poppler_page);
880                 }
881                         break;
882                 case POPPLER_DEST_FITB:
883                         unimplemented_dest = "POPPLER_DEST_FITB";
884                         break;
885                 case POPPLER_DEST_FITBH:
886                         unimplemented_dest = "POPPLER_DEST_FITBH";
887                         break;
888                 case POPPLER_DEST_FITBV:
889                         unimplemented_dest = "POPPLER_DEST_FITBV";
890                         break;
891                 case POPPLER_DEST_NAMED:
892                         ev_dest = ev_link_dest_new_named (dest->named_dest);
893                         break;
894                 case POPPLER_DEST_UNKNOWN:
895                         unimplemented_dest = "POPPLER_DEST_UNKNOWN";
896                         break;
897         }
898
899         if (unimplemented_dest) {
900                 g_warning ("Unimplemented named action: %s, please post a "
901                            "bug report in Evince bugzilla "
902                            "(http://bugzilla.gnome.org) with a testcase.",
903                            unimplemented_dest);
904         }
905
906         if (!ev_dest)
907                 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
908         
909         return ev_dest;
910 }
911
912 static EvLink *
913 ev_link_from_action (PdfDocument   *pdf_document,
914                      PopplerAction *action)
915 {
916         EvLink       *link = NULL;
917         EvLinkAction *ev_action = NULL;
918         const char   *unimplemented_action = NULL;
919
920         switch (action->type) {
921                 case POPPLER_ACTION_GOTO_DEST: {
922                         EvLinkDest *dest;
923                         
924                         dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
925                         ev_action = ev_link_action_new_dest (dest);
926                 }
927                         break;
928                 case POPPLER_ACTION_GOTO_REMOTE: {
929                         EvLinkDest *dest;
930                         
931                         dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
932                         ev_action = ev_link_action_new_remote (dest, 
933                                                                action->goto_remote.file_name);
934                         
935                 }
936                         break;
937                 case POPPLER_ACTION_LAUNCH:
938                         ev_action = ev_link_action_new_launch (action->launch.file_name,
939                                                                action->launch.params);
940                         break;
941                 case POPPLER_ACTION_URI:
942                         ev_action = ev_link_action_new_external_uri (action->uri.uri);
943                         break;
944                 case POPPLER_ACTION_NAMED:
945                         ev_action = ev_link_action_new_named (action->named.named_dest);
946                         break;
947                 case POPPLER_ACTION_MOVIE:
948                         unimplemented_action = "POPPLER_ACTION_MOVIE";
949                         break;
950                 case POPPLER_ACTION_UNKNOWN:
951                         unimplemented_action = "POPPLER_ACTION_UNKNOWN";
952         }
953         
954         if (unimplemented_action) {
955                 g_warning ("Unimplemented action: %s, please post a bug report with a testcase.",
956                            unimplemented_action);
957         }
958         
959         link = ev_link_new (action->any.title, ev_action);
960         
961         return link;    
962 }
963
964 static void
965 build_tree (PdfDocument      *pdf_document,
966             GtkTreeModel     *model,
967             GtkTreeIter      *parent,
968             PopplerIndexIter *iter)
969 {
970         
971         do {
972                 GtkTreeIter tree_iter;
973                 PopplerIndexIter *child;
974                 PopplerAction *action;
975                 EvLink *link = NULL;
976                 gboolean expand;
977                 char *title_markup;
978                 
979                 action = poppler_index_iter_get_action (iter);
980                 expand = poppler_index_iter_is_open (iter);
981
982                 if (!action)
983                         continue;
984
985                 switch (action->type) {
986                         case POPPLER_ACTION_GOTO_DEST: {
987                                 /* For bookmarks, solve named destinations */
988                                 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
989                                         PopplerDest *dest;
990                                         EvLinkDest *ev_dest = NULL;
991                                         EvLinkAction *ev_action;
992                                         
993                                         dest = poppler_document_find_dest (pdf_document->document,
994                                                                            action->goto_dest.dest->named_dest);
995                                         if (!dest) {
996                                                 link = ev_link_from_action (pdf_document, action);
997                                                 break;
998                                         }
999                                         
1000                                         ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1001                                         poppler_dest_free (dest);
1002                                         
1003                                         ev_action = ev_link_action_new_dest (ev_dest);
1004                                         link = ev_link_new (action->any.title, ev_action);
1005                                 } else {
1006                                         link = ev_link_from_action (pdf_document, action);
1007                                 }
1008                         }
1009                                 break;
1010                         default:
1011                                 link = ev_link_from_action (pdf_document, action);
1012                                 break;
1013                 }
1014                 
1015                 if (!link) {
1016                         poppler_action_free (action);
1017                         continue;
1018                 }
1019
1020                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1021                 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1022                 
1023                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1024                                     EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1025                                     EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1026                                     EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1027                                     -1);
1028                 
1029                 g_free (title_markup);
1030                 g_object_unref (link);
1031                 
1032                 child = poppler_index_iter_get_child (iter);
1033                 if (child)
1034                         build_tree (pdf_document, model, &tree_iter, child);
1035                 poppler_index_iter_free (child);
1036                 poppler_action_free (action);
1037                 
1038         } while (poppler_index_iter_next (iter));
1039 }
1040
1041 static GtkTreeModel *
1042 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1043 {
1044         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1045         GtkTreeModel *model = NULL;
1046         PopplerIndexIter *iter;
1047         
1048         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1049
1050         iter = poppler_index_iter_new (pdf_document->document);
1051         /* Create the model if we have items*/
1052         if (iter != NULL) {
1053                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1054                                                              G_TYPE_STRING,
1055                                                              G_TYPE_OBJECT,
1056                                                              G_TYPE_BOOLEAN,
1057                                                              G_TYPE_STRING);
1058                 build_tree (pdf_document, model, NULL, iter);
1059                 poppler_index_iter_free (iter);
1060         }
1061         
1062         return model;
1063 }
1064
1065 static GList *
1066 pdf_document_links_get_links (EvDocumentLinks *document_links,
1067                               gint             page)
1068 {
1069         PdfDocument *pdf_document;
1070         PopplerPage *poppler_page;
1071         GList *retval = NULL;
1072         GList *mapping_list;
1073         GList *list;
1074         double height;
1075
1076         pdf_document = PDF_DOCUMENT (document_links);
1077         poppler_page = poppler_document_get_page (pdf_document->document,
1078                                                   page);
1079         mapping_list = poppler_page_get_link_mapping (poppler_page);
1080         poppler_page_get_size (poppler_page, NULL, &height);
1081
1082         for (list = mapping_list; list; list = list->next) {
1083                 PopplerLinkMapping *link_mapping;
1084                 EvLinkMapping *ev_link_mapping;
1085
1086                 link_mapping = (PopplerLinkMapping *)list->data;
1087                 ev_link_mapping = g_new (EvLinkMapping, 1);
1088                 ev_link_mapping->link = ev_link_from_action (pdf_document,
1089                                                              link_mapping->action);
1090                 ev_link_mapping->x1 = link_mapping->area.x1;
1091                 ev_link_mapping->x2 = link_mapping->area.x2;
1092                 /* Invert this for X-style coordinates */
1093                 ev_link_mapping->y1 = height - link_mapping->area.y2;
1094                 ev_link_mapping->y2 = height - link_mapping->area.y1;
1095
1096                 retval = g_list_prepend (retval, ev_link_mapping);
1097         }
1098
1099         poppler_page_free_link_mapping (mapping_list);
1100         g_object_unref (poppler_page);
1101
1102         return g_list_reverse (retval);
1103 }
1104
1105 static EvLinkDest *
1106 pdf_document_links_find_link_dest (EvDocumentLinks  *document_links,
1107                                    const gchar      *link_name)
1108 {
1109         PdfDocument *pdf_document;
1110         PopplerDest *dest;
1111         EvLinkDest *ev_dest = NULL;
1112
1113         pdf_document = PDF_DOCUMENT (document_links);
1114         dest = poppler_document_find_dest (pdf_document->document,
1115                                            link_name);
1116         if (dest) {
1117                 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1118                 poppler_dest_free (dest);
1119         }
1120
1121         return ev_dest;
1122 }
1123
1124 static void
1125 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1126 {
1127         iface->has_document_links = pdf_document_links_has_document_links;
1128         iface->get_links_model = pdf_document_links_get_links_model;
1129         iface->get_links = pdf_document_links_get_links;
1130         iface->find_link_dest = pdf_document_links_find_link_dest;
1131 }
1132
1133 static GList *
1134 pdf_document_images_get_images (EvDocumentImages *document_images,
1135                                 gint              page)
1136 {
1137         GList *retval = NULL;
1138         PdfDocument *pdf_document;
1139         PopplerPage *poppler_page;
1140         GList *mapping_list;
1141         GList *list;
1142
1143         pdf_document = PDF_DOCUMENT (document_images);
1144         poppler_page = poppler_document_get_page (pdf_document->document, page);
1145         mapping_list = poppler_page_get_image_mapping (poppler_page);
1146
1147         for (list = mapping_list; list; list = list->next) {
1148                 PopplerImageMapping *image_mapping;
1149                 EvImageMapping *ev_image_mapping;
1150
1151                 image_mapping = (PopplerImageMapping *)list->data;
1152
1153                 ev_image_mapping = g_new (EvImageMapping, 1);
1154                 
1155                 ev_image_mapping->image = ev_image_new_from_pixbuf (image_mapping->image);
1156                 ev_image_mapping->x1 = image_mapping->area.x1;
1157                 ev_image_mapping->x2 = image_mapping->area.x2;
1158                 ev_image_mapping->y1 = image_mapping->area.y1;
1159                 ev_image_mapping->y2 = image_mapping->area.y2;
1160
1161                 retval = g_list_prepend (retval, ev_image_mapping);
1162         }
1163
1164         poppler_page_free_image_mapping (mapping_list);
1165         g_object_unref (poppler_page);
1166
1167         return retval;
1168 }
1169
1170 static void
1171 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1172 {
1173         iface->get_images = pdf_document_images_get_images;
1174 }
1175
1176 static GdkPixbuf *
1177 make_thumbnail_for_page (PdfDocument     *pdf_document,
1178                          PopplerPage     *poppler_page, 
1179                          EvRenderContext *rc)
1180 {
1181         GdkPixbuf *pixbuf;
1182         int width, height;
1183
1184         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1185                                                 rc, &width, &height);
1186
1187         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1188                                  width, height);
1189         gdk_pixbuf_fill (pixbuf, 0xffffffff);
1190
1191         ev_document_fc_mutex_lock ();
1192         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1193                                        width, height,
1194                                        rc->scale, rc->rotation, pixbuf);
1195         ev_document_fc_mutex_unlock ();
1196
1197         return pixbuf;
1198 }
1199
1200 static GdkPixbuf *
1201 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1202                                        EvRenderContext      *rc, 
1203                                        gboolean              border)
1204 {
1205         PdfDocument *pdf_document;
1206         PopplerPage *poppler_page;
1207         GdkPixbuf *pixbuf;
1208         GdkPixbuf *border_pixbuf;
1209
1210         pdf_document = PDF_DOCUMENT (document_thumbnails);
1211
1212         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1213         g_return_val_if_fail (poppler_page != NULL, NULL);
1214
1215         pixbuf = poppler_page_get_thumbnail (poppler_page);
1216         if (!pixbuf) {
1217                 /* There is no provided thumbnail.  We need to make one. */
1218                 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1219         }
1220
1221         if (border) {           
1222                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1223                 g_object_unref (pixbuf);
1224                 pixbuf = border_pixbuf;
1225         }               
1226
1227         g_object_unref (poppler_page);
1228         
1229         return pixbuf;
1230 }
1231
1232 static void
1233 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1234                                         EvRenderContext      *rc,
1235                                         gint                 *width,
1236                                         gint                 *height)
1237 {
1238         PdfDocument *pdf_document;
1239         PopplerPage *poppler_page;
1240         gint has_thumb;
1241         
1242         pdf_document = PDF_DOCUMENT (document_thumbnails);
1243         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1244
1245         g_return_if_fail (poppler_page != NULL);
1246
1247         has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1248
1249         if (!has_thumb) {
1250                 double page_width, page_height;
1251
1252                 poppler_page_get_size (poppler_page, &page_width, &page_height);
1253
1254                 *width = (gint) (page_width * rc->scale);
1255                 *height = (gint) (page_height * rc->scale);
1256         }
1257
1258         if (rc->rotation == 90 || rc->rotation == 270) {
1259                 gint  temp;
1260
1261                 temp = *width;
1262                 *width = *height;
1263                 *height = temp;
1264         }
1265         
1266         g_object_unref (poppler_page);
1267 }
1268
1269 static void
1270 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1271 {
1272         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1273         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1274 }
1275
1276
1277 static gboolean
1278 pdf_document_search_idle_callback (void *data)
1279 {
1280         PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1281         PdfDocument *pdf_document = search->document;
1282         int n_pages;
1283         GList *matches;
1284         PopplerPage *page;
1285
1286         page = poppler_document_get_page (search->document->document,
1287                                           search->search_page);
1288
1289         ev_document_doc_mutex_lock ();
1290         matches = poppler_page_find_text (page, search->text);
1291         ev_document_doc_mutex_unlock ();
1292
1293         g_object_unref (page);
1294
1295         search->pages[search->search_page] = matches;
1296         ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1297                                   search->search_page);
1298
1299         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1300         search->search_page += 1;
1301         if (search->search_page == n_pages) {
1302                 /* wrap around */
1303                 search->search_page = 0;
1304         }
1305
1306         if (search->search_page != search->start_page) {
1307                 return TRUE;
1308         }
1309
1310         /* We're done. */
1311         search->idle = 0; /* will return FALSE to remove */
1312         return FALSE;
1313 }
1314
1315
1316 static PdfDocumentSearch *
1317 pdf_document_search_new (PdfDocument *pdf_document,
1318                          int          start_page,
1319                          const char  *text)
1320 {
1321         PdfDocumentSearch *search;
1322         int n_pages;
1323         int i;
1324
1325         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1326
1327         search = g_new0 (PdfDocumentSearch, 1);
1328
1329         search->text = g_strdup (text);
1330         search->pages = g_new0 (GList *, n_pages);
1331         search->document = pdf_document;
1332
1333         /* We add at low priority so the progress bar repaints */
1334         search->idle = g_idle_add_full (G_PRIORITY_LOW,
1335                                         pdf_document_search_idle_callback,
1336                                         search,
1337                                         NULL);
1338
1339         search->start_page = start_page;
1340         search->search_page = start_page;
1341
1342         return search;
1343 }
1344
1345 static void
1346 pdf_document_find_begin (EvDocumentFind   *document,
1347                          int               page,
1348                          const char       *search_string,
1349                          gboolean          case_sensitive)
1350 {
1351         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1352
1353         /* FIXME handle case_sensitive (right now XPDF
1354          * code is always case insensitive for ASCII
1355          * and case sensitive for all other languaages)
1356          */
1357
1358         if (pdf_document->search &&
1359             strcmp (search_string, pdf_document->search->text) == 0)
1360                 return;
1361
1362         if (pdf_document->search)
1363                 pdf_document_search_free (pdf_document->search);
1364
1365         pdf_document->search = pdf_document_search_new (pdf_document,
1366                                                         page,
1367                                                         search_string);
1368 }
1369
1370 static int
1371 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1372 {
1373         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1374
1375         if (search) {
1376                 return g_list_length (search->pages[page]);
1377         } else {
1378                 return 0;
1379         }
1380 }
1381
1382 static gboolean
1383 pdf_document_find_get_result (EvDocumentFind *document_find,
1384                               int             page,
1385                               int             n_result,
1386                               EvRectangle    *rectangle)
1387 {
1388         PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1389         PdfDocumentSearch *search = pdf_document->search;
1390         PopplerPage *poppler_page;
1391         PopplerRectangle *r;
1392         double height;
1393
1394         if (search == NULL)
1395                 return FALSE;
1396
1397         r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1398                                                   n_result);
1399         if (r == NULL)
1400                 return FALSE;
1401
1402         poppler_page = poppler_document_get_page (pdf_document->document, page);
1403         poppler_page_get_size (poppler_page, NULL, &height);
1404         rectangle->x1 = r->x1;
1405         rectangle->y1 = height - r->y2;
1406         rectangle->x2 = r->x2;
1407         rectangle->y2 = height - r->y1;
1408         g_object_unref (poppler_page);
1409                 
1410         return TRUE;
1411 }
1412
1413 static int
1414 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1415                                     int             page)
1416 {
1417         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1418
1419         return search && search->pages[page] != NULL;
1420 }
1421
1422 static double
1423 pdf_document_find_get_progress (EvDocumentFind *document_find)
1424 {
1425         PdfDocumentSearch *search;
1426         int n_pages, pages_done;
1427
1428         search = PDF_DOCUMENT (document_find)->search;
1429
1430         if (search == NULL) {
1431                 return 0;
1432         }
1433
1434         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1435         if (search->search_page > search->start_page) {
1436                 pages_done = search->search_page - search->start_page + 1;
1437         } else if (search->search_page == search->start_page) {
1438                 pages_done = n_pages;
1439         } else {
1440                 pages_done = n_pages - search->start_page + search->search_page;
1441         }
1442
1443         return pages_done / (double) n_pages;
1444 }
1445
1446 static void
1447 pdf_document_find_cancel (EvDocumentFind *document)
1448 {
1449         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1450
1451         if (pdf_document->search) {
1452                 pdf_document_search_free (pdf_document->search);
1453                 pdf_document->search = NULL;
1454         }
1455 }
1456
1457 static void
1458 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1459 {
1460         iface->begin = pdf_document_find_begin;
1461         iface->get_n_results = pdf_document_find_get_n_results;
1462         iface->get_result = pdf_document_find_get_result;
1463         iface->page_has_results = pdf_document_find_page_has_results;
1464         iface->get_progress = pdf_document_find_get_progress;
1465         iface->cancel = pdf_document_find_cancel;
1466 }
1467
1468 static const gboolean supported_formats[] = {
1469         TRUE, /* EV_FILE_FORMAT_PS */
1470 #ifdef HAVE_CAIRO_PDF
1471 #ifdef HAVE_POPPLER_PAGE_RENDER
1472         TRUE, /* EV_FILE_FORMAT_PDF */
1473 #else
1474         FALSE, /* EV_FILE_FORMAT_PDF */
1475 #endif
1476 #endif
1477 };
1478
1479 static void
1480 pdf_print_context_free (PdfPrintContext *ctx)
1481 {
1482         if (!ctx)
1483                 return;
1484
1485         if (ctx->ps_file) {
1486                 poppler_ps_file_free (ctx->ps_file);
1487                 ctx->ps_file = NULL;
1488         }
1489 #ifdef HAVE_CAIRO_PDF
1490         if (ctx->pdf_cairo) {
1491                 cairo_destroy (ctx->pdf_cairo);
1492                 ctx->pdf_cairo = NULL;
1493         }
1494 #endif
1495         g_free (ctx);
1496 }
1497
1498 static gboolean
1499 pdf_document_file_exporter_format_supported (EvFileExporter      *exporter,
1500                                              EvFileExporterFormat format)
1501 {
1502         return supported_formats[format];
1503 }
1504
1505 static void
1506 pdf_document_file_exporter_begin (EvFileExporter      *exporter,
1507                                   EvFileExporterFormat format,
1508                                   const char          *filename,
1509                                   int                  first_page,
1510                                   int                  last_page,
1511                                   double               width,
1512                                   double               height,
1513                                   gboolean             duplex)
1514 {
1515         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1516         PdfPrintContext *ctx;
1517
1518         if (pdf_document->print_ctx)
1519                 pdf_print_context_free (pdf_document->print_ctx);
1520         pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1521         ctx = pdf_document->print_ctx;
1522         ctx->format = format;
1523         
1524         switch (format) {
1525                 case EV_FILE_FORMAT_PS:
1526                         ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1527                                                             filename, first_page,
1528                                                             last_page - first_page + 1);
1529                         poppler_ps_file_set_paper_size (ctx->ps_file, width, height);
1530                         poppler_ps_file_set_duplex (ctx->ps_file, duplex);
1531
1532                         break;
1533                 case EV_FILE_FORMAT_PDF: {
1534 #ifdef HAVE_CAIRO_PDF
1535                         cairo_surface_t *surface;
1536                         
1537                         surface = cairo_pdf_surface_create (filename, width, height);
1538                         ctx->pdf_cairo = cairo_create (surface);
1539                         cairo_surface_destroy (surface);
1540 #endif
1541                 }
1542                         break;
1543                 default:
1544                         g_assert_not_reached ();
1545         }
1546 }
1547
1548 static void
1549 pdf_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1550 {
1551         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1552         PdfPrintContext *ctx = pdf_document->print_ctx;
1553         PopplerPage *poppler_page;
1554
1555         g_return_if_fail (pdf_document->print_ctx != NULL);
1556
1557         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1558
1559         switch (ctx->format) {
1560                 case EV_FILE_FORMAT_PS:
1561                         poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1562                         break;
1563                 case EV_FILE_FORMAT_PDF:
1564 #ifdef HAVE_CAIRO_PDF
1565                         cairo_save (ctx->pdf_cairo);
1566 #endif
1567 #ifdef HAVE_POPPLER_PAGE_RENDER
1568                         poppler_page_render (poppler_page, ctx->pdf_cairo);
1569 #endif
1570 #ifdef HAVE_CAIRO_PDF
1571                         cairo_show_page (ctx->pdf_cairo);
1572                         cairo_restore (ctx->pdf_cairo);
1573 #endif
1574                         break;
1575                 default:
1576                         g_assert_not_reached ();
1577         }
1578         
1579         g_object_unref (poppler_page);
1580 }
1581
1582 static void
1583 pdf_document_file_exporter_end (EvFileExporter *exporter)
1584 {
1585         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1586
1587         pdf_print_context_free (pdf_document->print_ctx);
1588         pdf_document->print_ctx = NULL;
1589 }
1590
1591 static void
1592 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1593 {
1594         iface->format_supported = pdf_document_file_exporter_format_supported;
1595         iface->begin = pdf_document_file_exporter_begin;
1596         iface->do_page = pdf_document_file_exporter_do_page;
1597         iface->end = pdf_document_file_exporter_end;
1598 }
1599
1600 static void
1601 pdf_selection_render_selection (EvSelection      *selection,
1602                                 EvRenderContext  *rc,
1603                                 GdkPixbuf       **pixbuf,
1604                                 EvRectangle      *points,
1605                                 EvRectangle      *old_points,
1606                                 GdkColor        *text,
1607                                 GdkColor        *base)
1608 {
1609         PdfDocument *pdf_document;
1610         double width_points, height_points;
1611         gint width, height;
1612
1613         pdf_document = PDF_DOCUMENT (selection);
1614         set_rc_data (pdf_document, rc);
1615
1616         poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
1617         width = (int) ((width_points * rc->scale) + 0.5);
1618         height = (int) ((height_points * rc->scale) + 0.5);
1619
1620         if (*pixbuf == NULL) {
1621                 * pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1622                                            TRUE, 8,
1623                                            width, height);
1624         }
1625
1626         poppler_page_render_selection_to_pixbuf (POPPLER_PAGE (rc->data),
1627                                                  rc->scale, rc->rotation, *pixbuf,
1628                                                  (PopplerRectangle *)points,
1629                                                  (PopplerRectangle *)old_points,
1630                                                  POPPLER_SELECTION_NORMAL, /* SelectionStyle */
1631                                                  text,
1632                                                  base);
1633 }
1634
1635
1636 static GdkRegion *
1637 pdf_selection_get_selection_region (EvSelection     *selection,
1638                                     EvRenderContext *rc,
1639                                     EvRectangle     *points)
1640 {
1641         PdfDocument *pdf_document;
1642         GdkRegion *retval;
1643
1644         pdf_document = PDF_DOCUMENT (selection);
1645
1646         set_rc_data (pdf_document, rc);
1647
1648         retval = poppler_page_get_selection_region ((PopplerPage *)rc->data, rc->scale, (PopplerRectangle *) points);
1649
1650         return retval;
1651 }
1652
1653 static GdkRegion *
1654 pdf_selection_get_selection_map (EvSelection     *selection,
1655                                  EvRenderContext *rc)
1656 {
1657         PdfDocument *pdf_document;
1658         PopplerPage *poppler_page;
1659         PopplerRectangle points;
1660         GdkRegion *retval;
1661
1662         pdf_document = PDF_DOCUMENT (selection);
1663         poppler_page = poppler_document_get_page (pdf_document->document,
1664                                                   rc->page);
1665
1666         points.x1 = 0.0;
1667         points.y1 = 0.0;
1668         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1669         retval = poppler_page_get_selection_region (poppler_page, 1.0, &points);
1670         g_object_unref (poppler_page);
1671
1672         return retval;
1673 }
1674
1675 static void
1676 pdf_selection_iface_init (EvSelectionIface *iface)
1677 {
1678         iface->render_selection = pdf_selection_render_selection;
1679         iface->get_selection_region = pdf_selection_get_selection_region;
1680         iface->get_selection_map = pdf_selection_get_selection_map;
1681 }
1682
1683 /* Page Transitions */
1684 static gdouble
1685 pdf_document_get_page_duration (EvDocumentTransition *trans,
1686                                 gint                  page)
1687 {
1688         PdfDocument *pdf_document;
1689         PopplerPage *poppler_page;
1690         gdouble      duration = -1;
1691
1692         pdf_document = PDF_DOCUMENT (trans);
1693         poppler_page = poppler_document_get_page (pdf_document->document, page);
1694         if (!poppler_page)
1695                 return -1;
1696
1697         duration = poppler_page_get_duration (poppler_page);
1698         g_object_unref (poppler_page);
1699
1700         return duration;
1701 }
1702
1703 static void
1704 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1705 {
1706         iface->get_page_duration = pdf_document_get_page_duration;
1707 }
1708
1709 PdfDocument *
1710 pdf_document_new (void)
1711 {
1712         return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));
1713 }