]> www.fi.muni.cz Git - evince.git/blob - pdf/ev-poppler.cc
[dualscreen] fix crash on ctrl+w and fix control window closing
[evince.git] / 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 cairo_surface_t *
430 pdf_document_render (EvDocument      *document,
431                      EvRenderContext *rc)
432 {
433         PdfDocument *pdf_document;
434         cairo_surface_t *surface;
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 #ifdef HAVE_POPPLER_PAGE_RENDER
453         cairo_t *cr;
454         
455         surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
456                                               width, height);
457         memset (cairo_image_surface_get_data (surface), 0xff,
458                 cairo_image_surface_get_height (surface) *
459                 cairo_image_surface_get_stride (surface));
460         
461         cr = cairo_create (surface);
462         switch (rc->rotation) {
463                 case 90:
464                         cairo_translate (cr, width, 0);
465                         break;
466                 case 180:
467                         cairo_translate (cr, width, height);
468                         break;
469                 case 270:
470                         cairo_translate (cr, 0, height);
471                         break;
472                 default:
473                         cairo_translate (cr, 0, 0);
474         }
475         cairo_scale (cr, rc->scale, rc->scale);
476         cairo_rotate (cr, rc->rotation * G_PI / 180.0);
477         poppler_page_render (POPPLER_PAGE (rc->data), cr);
478         cairo_destroy (cr);
479 #else /* HAVE_POPPLER_PAGE_RENDER */
480         GdkPixbuf *pixbuf;
481         
482         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
483                                  FALSE, 8,
484                                  width, height);
485
486         poppler_page_render_to_pixbuf (POPPLER_PAGE (rc->data),
487                                        0, 0,
488                                        width, height,
489                                        rc->scale,
490                                        rc->rotation,
491                                        pixbuf);
492         surface = ev_document_misc_surface_from_pixbuf (pixbuf);
493         g_object_unref (pixbuf);
494 #endif /* HAVE_POPPLER_PAGE_RENDER */
495
496         return surface;
497 }
498
499 /* EvDocumentSecurity */
500
501 static gboolean
502 pdf_document_has_document_security (EvDocumentSecurity *document_security)
503 {
504         /* FIXME: do we really need to have this? */
505         return FALSE;
506 }
507
508 static void
509 pdf_document_set_password (EvDocumentSecurity *document_security,
510                            const char         *password)
511 {
512         PdfDocument *document = PDF_DOCUMENT (document_security);
513
514         if (document->password)
515                 g_free (document->password);
516
517         document->password = g_strdup (password);
518 }
519
520 static gboolean
521 pdf_document_can_get_text (EvDocument *document)
522 {
523         return TRUE;
524 }
525
526 static EvDocumentInfo *
527 pdf_document_get_info (EvDocument *document)
528 {
529         EvDocumentInfo *info;
530         PopplerPageLayout layout;
531         PopplerPageMode mode;
532         PopplerViewerPreferences view_prefs;
533         PopplerPermissions permissions;
534
535         info = g_new0 (EvDocumentInfo, 1);
536
537         info->fields_mask = EV_DOCUMENT_INFO_TITLE |
538                             EV_DOCUMENT_INFO_FORMAT |
539                             EV_DOCUMENT_INFO_AUTHOR |
540                             EV_DOCUMENT_INFO_SUBJECT |
541                             EV_DOCUMENT_INFO_KEYWORDS |
542                             EV_DOCUMENT_INFO_LAYOUT |
543                             EV_DOCUMENT_INFO_START_MODE |
544                             EV_DOCUMENT_INFO_PERMISSIONS |
545                             EV_DOCUMENT_INFO_UI_HINTS |
546                             EV_DOCUMENT_INFO_CREATOR |
547                             EV_DOCUMENT_INFO_PRODUCER |
548                             EV_DOCUMENT_INFO_CREATION_DATE |
549                             EV_DOCUMENT_INFO_MOD_DATE |
550                             EV_DOCUMENT_INFO_LINEARIZED |
551                             EV_DOCUMENT_INFO_N_PAGES |
552                             EV_DOCUMENT_INFO_SECURITY | 
553                             EV_DOCUMENT_INFO_PAPER_SIZE;
554
555         g_object_get (PDF_DOCUMENT (document)->document,
556                       "title", &(info->title),
557                       "format", &(info->format),
558                       "author", &(info->author),
559                       "subject", &(info->subject),
560                       "keywords", &(info->keywords),
561                       "page-mode", &mode,
562                       "page-layout", &layout,
563                       "viewer-preferences", &view_prefs,
564                       "permissions", &permissions,
565                       "creator", &(info->creator),
566                       "producer", &(info->producer),
567                       "creation-date", &(info->creation_date),
568                       "mod-date", &(info->modified_date),
569                       "linearized", &(info->linearized),
570                       NULL);
571
572         pdf_document_get_page_size(document, 0,
573                                    &(info->paper_width),
574                                    &(info->paper_height));
575
576         // Convert to mm.
577         info->paper_width = info->paper_width / 72.0f * 25.4f;
578         info->paper_height = info->paper_height / 72.0f * 25.4f;
579
580         switch (layout) {
581                 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
582                         info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
583                         break;
584                 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
585                         info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
586                         break;
587                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
588                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
589                         break;
590                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
591                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
592                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
593                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
594                         break;
595                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
596                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
597                         break;
598                 default:
599                         break;
600         }
601
602         switch (mode) {
603                 case POPPLER_PAGE_MODE_NONE:
604                         info->mode = EV_DOCUMENT_MODE_NONE;
605                         break;
606                 case POPPLER_PAGE_MODE_USE_THUMBS:
607                         info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
608                         break;
609                 case POPPLER_PAGE_MODE_USE_OC:
610                         info->mode = EV_DOCUMENT_MODE_USE_OC;
611                         break;
612                 case POPPLER_PAGE_MODE_FULL_SCREEN:
613                         info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
614                         break;
615                 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
616                         info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
617                 default:
618                         break;
619         }
620
621         info->ui_hints = 0;
622         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
623                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
624         }
625         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
626                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
627         }
628         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
629                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
630         }
631         if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
632                 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
633         }
634         if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
635                 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
636         }
637         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
638                 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
639         }
640         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
641                 info->ui_hints |=  EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
642         }
643
644         info->permissions = 0;
645         if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
646                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
647         }
648         if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
649                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
650         }
651         if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
652                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
653         }
654         if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
655                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
656         }
657
658         info->n_pages = ev_document_get_n_pages (document);
659
660         if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
661                 /* translators: this is the document security state */
662                 info->security = g_strdup (_("Yes"));
663         } else {
664                 /* translators: this is the document security state */
665                 info->security = g_strdup (_("No"));
666         }
667
668         return info;
669 }
670
671 static char *
672 pdf_document_get_text (EvDocument *document, int page, EvRectangle *rect)
673 {
674         PdfDocument *pdf_document = PDF_DOCUMENT (document);
675         PopplerPage *poppler_page;
676         PopplerRectangle r;
677         double height;
678         char *text;
679         
680         poppler_page = poppler_document_get_page (pdf_document->document, page);
681         g_return_val_if_fail (poppler_page != NULL, NULL);
682
683         poppler_page_get_size (poppler_page, NULL, &height);
684         r.x1 = rect->x1;
685         r.y1 = height - rect->y2;
686         r.x2 = rect->x2;
687         r.y2 = height - rect->y1;
688
689         text = poppler_page_get_text (poppler_page, &r);
690
691         g_object_unref (poppler_page);
692
693         return text;
694 }
695
696 static void
697 pdf_document_document_iface_init (EvDocumentIface *iface)
698 {
699         iface->save = pdf_document_save;
700         iface->load = pdf_document_load;
701         iface->get_n_pages = pdf_document_get_n_pages;
702         iface->get_page_size = pdf_document_get_page_size;
703         iface->get_page_label = pdf_document_get_page_label;
704         iface->has_attachments = pdf_document_has_attachments;
705         iface->get_attachments = pdf_document_get_attachments;
706         iface->render = pdf_document_render;
707         iface->get_text = pdf_document_get_text;
708         iface->can_get_text = pdf_document_can_get_text;
709         iface->get_info = pdf_document_get_info;
710 };
711
712 static void
713 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
714 {
715         iface->has_document_security = pdf_document_has_document_security;
716         iface->set_password = pdf_document_set_password;
717 }
718
719 static gdouble
720 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
721 {
722         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
723         int n_pages;
724
725         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
726
727         return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
728 }
729
730 static gboolean
731 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
732                          int              n_pages)
733 {
734         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
735         gboolean result;
736
737         g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
738
739         if (pdf_document->font_info == NULL) { 
740                 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
741         }
742
743         if (pdf_document->fonts_iter) {
744                 poppler_fonts_iter_free (pdf_document->fonts_iter);
745         }
746
747         pdf_document->fonts_scanned_pages += n_pages;
748
749         result = poppler_font_info_scan (pdf_document->font_info, n_pages,
750                                          &pdf_document->fonts_iter);
751         if (!result) {
752                 pdf_document->fonts_scanned_pages = 0;
753                 poppler_font_info_free (pdf_document->font_info);
754                 pdf_document->font_info = NULL; 
755         }
756
757         return result;
758 }
759
760 static const char *
761 font_type_to_string (PopplerFontType type)
762 {
763         switch (type) {
764                 case POPPLER_FONT_TYPE_TYPE1:
765                         return _("Type 1");
766                 case POPPLER_FONT_TYPE_TYPE1C:
767                         return _("Type 1C");
768                 case POPPLER_FONT_TYPE_TYPE3:
769                         return _("Type 3");
770                 case POPPLER_FONT_TYPE_TRUETYPE:
771                         return _("TrueType");
772                 case POPPLER_FONT_TYPE_CID_TYPE0:
773                         return _("Type 1 (CID)");
774                 case POPPLER_FONT_TYPE_CID_TYPE0C:
775                         return _("Type 1C (CID)");
776                 case POPPLER_FONT_TYPE_CID_TYPE2:
777                         return _("TrueType (CID)");
778                 default:
779                         return _("Unknown font type");
780         }
781 }
782
783 static void
784 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
785                                GtkTreeModel    *model)
786 {
787         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
788         PopplerFontsIter *iter = pdf_document->fonts_iter;
789
790         g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
791
792         if (!iter)
793                 return;
794
795         do {
796                 GtkTreeIter list_iter;
797                 const char *name;
798                 const char *type;
799                 const char *embedded;
800                 char *details;
801                 
802                 name = poppler_fonts_iter_get_name (iter);
803
804                 if (name == NULL) {
805                         name = _("No name");
806                 }
807
808                 type = font_type_to_string (
809                         poppler_fonts_iter_get_font_type (iter));
810
811                 if (poppler_fonts_iter_is_embedded (iter)) {
812                         if (poppler_fonts_iter_is_subset (iter))
813                                 embedded = _("Embedded subset");
814                         else
815                                 embedded = _("Embedded");
816                 } else {
817                         embedded = _("Not embedded");
818                 }
819
820                 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
821
822                 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
823                 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
824                                     EV_DOCUMENT_FONTS_COLUMN_NAME, name,
825                                     EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
826                                     -1);
827
828                 g_free (details);
829         } while (poppler_fonts_iter_next (iter));
830 }
831
832 static void
833 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
834 {
835         iface->fill_model = pdf_document_fonts_fill_model;
836         iface->scan = pdf_document_fonts_scan;
837         iface->get_progress = pdf_document_fonts_get_progress;
838 }
839
840 static gboolean
841 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
842 {
843         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
844         PopplerIndexIter *iter;
845
846         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
847
848         iter = poppler_index_iter_new (pdf_document->document);
849         if (iter == NULL)
850                 return FALSE;
851         poppler_index_iter_free (iter);
852
853         return TRUE;
854 }
855
856 static EvLinkDest *
857 ev_link_dest_from_dest (PdfDocument *pdf_document,
858                         PopplerDest *dest)
859 {
860         EvLinkDest *ev_dest = NULL;
861         const char *unimplemented_dest = NULL;
862
863         g_assert (dest != NULL);
864
865         switch (dest->type) {
866                 case POPPLER_DEST_XYZ: {
867                         PopplerPage *poppler_page;
868                         double height;
869
870                         poppler_page = poppler_document_get_page (pdf_document->document,
871                                                                   MAX (0, dest->page_num - 1));
872                         poppler_page_get_size (poppler_page, NULL, &height);
873                         ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
874                                                         dest->left,
875                                                         height - dest->top,
876                                                         dest->zoom);
877                         g_object_unref (poppler_page);
878                 }
879                         break;
880                 case POPPLER_DEST_FIT:
881                         ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
882                         break;
883                 case POPPLER_DEST_FITH: {
884                         PopplerPage *poppler_page;
885                         double height;
886
887                         poppler_page = poppler_document_get_page (pdf_document->document,
888                                                                   MAX (0, dest->page_num - 1));
889                         poppler_page_get_size (poppler_page, NULL, &height);
890                         ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
891                                                          height - dest->top);
892                         g_object_unref (poppler_page);
893                 }
894                         break;
895                 case POPPLER_DEST_FITV:
896                         ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
897                                                          dest->left);
898                         break;
899                 case POPPLER_DEST_FITR: {
900                         PopplerPage *poppler_page;
901                         double height;
902
903                         poppler_page = poppler_document_get_page (pdf_document->document,
904                                                                   MAX (0, dest->page_num - 1));
905                         poppler_page_get_size (poppler_page, NULL, &height);
906                         ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
907                                                          dest->left,
908                                                          height - dest->bottom,
909                                                          dest->right,
910                                                          height - dest->top);
911                         g_object_unref (poppler_page);
912                 }
913                         break;
914                 case POPPLER_DEST_FITB:
915                         unimplemented_dest = "POPPLER_DEST_FITB";
916                         break;
917                 case POPPLER_DEST_FITBH:
918                         unimplemented_dest = "POPPLER_DEST_FITBH";
919                         break;
920                 case POPPLER_DEST_FITBV:
921                         unimplemented_dest = "POPPLER_DEST_FITBV";
922                         break;
923                 case POPPLER_DEST_NAMED:
924                         ev_dest = ev_link_dest_new_named (dest->named_dest);
925                         break;
926                 case POPPLER_DEST_UNKNOWN:
927                         unimplemented_dest = "POPPLER_DEST_UNKNOWN";
928                         break;
929         }
930
931         if (unimplemented_dest) {
932                 g_warning ("Unimplemented named action: %s, please post a "
933                            "bug report in Evince bugzilla "
934                            "(http://bugzilla.gnome.org) with a testcase.",
935                            unimplemented_dest);
936         }
937
938         if (!ev_dest)
939                 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
940         
941         return ev_dest;
942 }
943
944 static EvLink *
945 ev_link_from_action (PdfDocument   *pdf_document,
946                      PopplerAction *action)
947 {
948         EvLink       *link = NULL;
949         EvLinkAction *ev_action = NULL;
950         const char   *unimplemented_action = NULL;
951
952         switch (action->type) {
953                 case POPPLER_ACTION_GOTO_DEST: {
954                         EvLinkDest *dest;
955                         
956                         dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
957                         ev_action = ev_link_action_new_dest (dest);
958                 }
959                         break;
960                 case POPPLER_ACTION_GOTO_REMOTE: {
961                         EvLinkDest *dest;
962                         
963                         dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
964                         ev_action = ev_link_action_new_remote (dest, 
965                                                                action->goto_remote.file_name);
966                         
967                 }
968                         break;
969                 case POPPLER_ACTION_LAUNCH:
970                         ev_action = ev_link_action_new_launch (action->launch.file_name,
971                                                                action->launch.params);
972                         break;
973                 case POPPLER_ACTION_URI:
974                         ev_action = ev_link_action_new_external_uri (action->uri.uri);
975                         break;
976                 case POPPLER_ACTION_NAMED:
977                         ev_action = ev_link_action_new_named (action->named.named_dest);
978                         break;
979                 case POPPLER_ACTION_MOVIE:
980                         unimplemented_action = "POPPLER_ACTION_MOVIE";
981                         break;
982                 case POPPLER_ACTION_UNKNOWN:
983                         unimplemented_action = "POPPLER_ACTION_UNKNOWN";
984         }
985         
986         if (unimplemented_action) {
987                 g_warning ("Unimplemented action: %s, please post a bug report with a testcase.",
988                            unimplemented_action);
989         }
990         
991         link = ev_link_new (action->any.title, ev_action);
992         
993         return link;    
994 }
995
996 static void
997 build_tree (PdfDocument      *pdf_document,
998             GtkTreeModel     *model,
999             GtkTreeIter      *parent,
1000             PopplerIndexIter *iter)
1001 {
1002         
1003         do {
1004                 GtkTreeIter tree_iter;
1005                 PopplerIndexIter *child;
1006                 PopplerAction *action;
1007                 EvLink *link = NULL;
1008                 gboolean expand;
1009                 char *title_markup;
1010                 
1011                 action = poppler_index_iter_get_action (iter);
1012                 expand = poppler_index_iter_is_open (iter);
1013
1014                 if (!action)
1015                         continue;
1016
1017                 switch (action->type) {
1018                         case POPPLER_ACTION_GOTO_DEST: {
1019                                 /* For bookmarks, solve named destinations */
1020                                 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1021                                         PopplerDest *dest;
1022                                         EvLinkDest *ev_dest = NULL;
1023                                         EvLinkAction *ev_action;
1024                                         
1025                                         dest = poppler_document_find_dest (pdf_document->document,
1026                                                                            action->goto_dest.dest->named_dest);
1027                                         if (!dest) {
1028                                                 link = ev_link_from_action (pdf_document, action);
1029                                                 break;
1030                                         }
1031                                         
1032                                         ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1033                                         poppler_dest_free (dest);
1034                                         
1035                                         ev_action = ev_link_action_new_dest (ev_dest);
1036                                         link = ev_link_new (action->any.title, ev_action);
1037                                 } else {
1038                                         link = ev_link_from_action (pdf_document, action);
1039                                 }
1040                         }
1041                                 break;
1042                         default:
1043                                 link = ev_link_from_action (pdf_document, action);
1044                                 break;
1045                 }
1046                 
1047                 if (!link) {
1048                         poppler_action_free (action);
1049                         continue;
1050                 }
1051
1052                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1053                 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1054                 
1055                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1056                                     EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1057                                     EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1058                                     EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1059                                     -1);
1060                 
1061                 g_free (title_markup);
1062                 g_object_unref (link);
1063                 
1064                 child = poppler_index_iter_get_child (iter);
1065                 if (child)
1066                         build_tree (pdf_document, model, &tree_iter, child);
1067                 poppler_index_iter_free (child);
1068                 poppler_action_free (action);
1069                 
1070         } while (poppler_index_iter_next (iter));
1071 }
1072
1073 static GtkTreeModel *
1074 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1075 {
1076         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1077         GtkTreeModel *model = NULL;
1078         PopplerIndexIter *iter;
1079         
1080         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1081
1082         iter = poppler_index_iter_new (pdf_document->document);
1083         /* Create the model if we have items*/
1084         if (iter != NULL) {
1085                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1086                                                              G_TYPE_STRING,
1087                                                              G_TYPE_OBJECT,
1088                                                              G_TYPE_BOOLEAN,
1089                                                              G_TYPE_STRING);
1090                 build_tree (pdf_document, model, NULL, iter);
1091                 poppler_index_iter_free (iter);
1092         }
1093         
1094         return model;
1095 }
1096
1097 static GList *
1098 pdf_document_links_get_links (EvDocumentLinks *document_links,
1099                               gint             page)
1100 {
1101         PdfDocument *pdf_document;
1102         PopplerPage *poppler_page;
1103         GList *retval = NULL;
1104         GList *mapping_list;
1105         GList *list;
1106         double height;
1107
1108         pdf_document = PDF_DOCUMENT (document_links);
1109         poppler_page = poppler_document_get_page (pdf_document->document,
1110                                                   page);
1111         mapping_list = poppler_page_get_link_mapping (poppler_page);
1112         poppler_page_get_size (poppler_page, NULL, &height);
1113
1114         for (list = mapping_list; list; list = list->next) {
1115                 PopplerLinkMapping *link_mapping;
1116                 EvLinkMapping *ev_link_mapping;
1117
1118                 link_mapping = (PopplerLinkMapping *)list->data;
1119                 ev_link_mapping = g_new (EvLinkMapping, 1);
1120                 ev_link_mapping->link = ev_link_from_action (pdf_document,
1121                                                              link_mapping->action);
1122                 ev_link_mapping->x1 = link_mapping->area.x1;
1123                 ev_link_mapping->x2 = link_mapping->area.x2;
1124                 /* Invert this for X-style coordinates */
1125                 ev_link_mapping->y1 = height - link_mapping->area.y2;
1126                 ev_link_mapping->y2 = height - link_mapping->area.y1;
1127
1128                 retval = g_list_prepend (retval, ev_link_mapping);
1129         }
1130
1131         poppler_page_free_link_mapping (mapping_list);
1132         g_object_unref (poppler_page);
1133
1134         return g_list_reverse (retval);
1135 }
1136
1137 static EvLinkDest *
1138 pdf_document_links_find_link_dest (EvDocumentLinks  *document_links,
1139                                    const gchar      *link_name)
1140 {
1141         PdfDocument *pdf_document;
1142         PopplerDest *dest;
1143         EvLinkDest *ev_dest = NULL;
1144
1145         pdf_document = PDF_DOCUMENT (document_links);
1146         dest = poppler_document_find_dest (pdf_document->document,
1147                                            link_name);
1148         if (dest) {
1149                 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1150                 poppler_dest_free (dest);
1151         }
1152
1153         return ev_dest;
1154 }
1155
1156 static void
1157 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1158 {
1159         iface->has_document_links = pdf_document_links_has_document_links;
1160         iface->get_links_model = pdf_document_links_get_links_model;
1161         iface->get_links = pdf_document_links_get_links;
1162         iface->find_link_dest = pdf_document_links_find_link_dest;
1163 }
1164
1165 static GList *
1166 pdf_document_images_get_images (EvDocumentImages *document_images,
1167                                 gint              page)
1168 {
1169         GList *retval = NULL;
1170         PdfDocument *pdf_document;
1171         PopplerPage *poppler_page;
1172         GList *mapping_list;
1173         GList *list;
1174
1175         pdf_document = PDF_DOCUMENT (document_images);
1176         poppler_page = poppler_document_get_page (pdf_document->document, page);
1177         mapping_list = poppler_page_get_image_mapping (poppler_page);
1178
1179         for (list = mapping_list; list; list = list->next) {
1180                 PopplerImageMapping *image_mapping;
1181                 EvImageMapping *ev_image_mapping;
1182
1183                 image_mapping = (PopplerImageMapping *)list->data;
1184
1185                 ev_image_mapping = g_new (EvImageMapping, 1);
1186                 
1187                 ev_image_mapping->image = ev_image_new_from_pixbuf (image_mapping->image);
1188                 ev_image_mapping->x1 = image_mapping->area.x1;
1189                 ev_image_mapping->x2 = image_mapping->area.x2;
1190                 ev_image_mapping->y1 = image_mapping->area.y1;
1191                 ev_image_mapping->y2 = image_mapping->area.y2;
1192
1193                 retval = g_list_prepend (retval, ev_image_mapping);
1194         }
1195
1196         poppler_page_free_image_mapping (mapping_list);
1197         g_object_unref (poppler_page);
1198
1199         return retval;
1200 }
1201
1202 static void
1203 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1204 {
1205         iface->get_images = pdf_document_images_get_images;
1206 }
1207
1208 static GdkPixbuf *
1209 make_thumbnail_for_page (PdfDocument     *pdf_document,
1210                          PopplerPage     *poppler_page, 
1211                          EvRenderContext *rc)
1212 {
1213         GdkPixbuf *pixbuf;
1214         int width, height;
1215
1216         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1217                                                 rc, &width, &height);
1218
1219         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1220                                  width, height);
1221         gdk_pixbuf_fill (pixbuf, 0xffffffff);
1222
1223         ev_document_fc_mutex_lock ();
1224         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1225                                        width, height,
1226                                        rc->scale, rc->rotation, pixbuf);
1227         ev_document_fc_mutex_unlock ();
1228
1229         return pixbuf;
1230 }
1231
1232 static GdkPixbuf *
1233 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1234                                        EvRenderContext      *rc, 
1235                                        gboolean              border)
1236 {
1237         PdfDocument *pdf_document;
1238         PopplerPage *poppler_page;
1239         GdkPixbuf *pixbuf;
1240         GdkPixbuf *border_pixbuf;
1241
1242         pdf_document = PDF_DOCUMENT (document_thumbnails);
1243
1244         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1245         g_return_val_if_fail (poppler_page != NULL, NULL);
1246
1247         pixbuf = poppler_page_get_thumbnail (poppler_page);
1248         if (!pixbuf) {
1249                 /* There is no provided thumbnail.  We need to make one. */
1250                 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1251         }
1252
1253         if (border) {           
1254                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1255                 g_object_unref (pixbuf);
1256                 pixbuf = border_pixbuf;
1257         }               
1258
1259         g_object_unref (poppler_page);
1260         
1261         return pixbuf;
1262 }
1263
1264 static void
1265 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1266                                         EvRenderContext      *rc,
1267                                         gint                 *width,
1268                                         gint                 *height)
1269 {
1270         PdfDocument *pdf_document;
1271         PopplerPage *poppler_page;
1272         gint has_thumb;
1273         
1274         pdf_document = PDF_DOCUMENT (document_thumbnails);
1275         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1276
1277         g_return_if_fail (poppler_page != NULL);
1278
1279         has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1280
1281         if (!has_thumb) {
1282                 double page_width, page_height;
1283
1284                 poppler_page_get_size (poppler_page, &page_width, &page_height);
1285
1286                 *width = (gint) MAX (page_width * rc->scale, 1);
1287                 *height = (gint) MAX (page_height * rc->scale, 1);
1288         }
1289         
1290         if (rc->rotation == 90 || rc->rotation == 270) {
1291                 gint  temp;
1292
1293                 temp = *width;
1294                 *width = *height;
1295                 *height = temp;
1296         }
1297         
1298         g_object_unref (poppler_page);
1299 }
1300
1301 static void
1302 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1303 {
1304         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1305         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1306 }
1307
1308
1309 static gboolean
1310 pdf_document_search_idle_callback (void *data)
1311 {
1312         PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1313         PdfDocument *pdf_document = search->document;
1314         int n_pages;
1315         GList *matches;
1316         PopplerPage *page;
1317
1318         page = poppler_document_get_page (search->document->document,
1319                                           search->search_page);
1320
1321         ev_document_doc_mutex_lock ();
1322         matches = poppler_page_find_text (page, search->text);
1323         ev_document_doc_mutex_unlock ();
1324
1325         g_object_unref (page);
1326
1327         search->pages[search->search_page] = matches;
1328         ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1329                                   search->search_page);
1330
1331         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1332         search->search_page += 1;
1333         if (search->search_page == n_pages) {
1334                 /* wrap around */
1335                 search->search_page = 0;
1336         }
1337
1338         if (search->search_page != search->start_page) {
1339                 return TRUE;
1340         }
1341
1342         /* We're done. */
1343         search->idle = 0; /* will return FALSE to remove */
1344         return FALSE;
1345 }
1346
1347
1348 static PdfDocumentSearch *
1349 pdf_document_search_new (PdfDocument *pdf_document,
1350                          int          start_page,
1351                          const char  *text)
1352 {
1353         PdfDocumentSearch *search;
1354         int n_pages;
1355         int i;
1356
1357         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1358
1359         search = g_new0 (PdfDocumentSearch, 1);
1360
1361         search->text = g_strdup (text);
1362         search->pages = g_new0 (GList *, n_pages);
1363         search->document = pdf_document;
1364
1365         /* We add at low priority so the progress bar repaints */
1366         search->idle = g_idle_add_full (G_PRIORITY_LOW,
1367                                         pdf_document_search_idle_callback,
1368                                         search,
1369                                         NULL);
1370
1371         search->start_page = start_page;
1372         search->search_page = start_page;
1373
1374         return search;
1375 }
1376
1377 static void
1378 pdf_document_find_begin (EvDocumentFind   *document,
1379                          int               page,
1380                          const char       *search_string,
1381                          gboolean          case_sensitive)
1382 {
1383         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1384
1385         /* FIXME handle case_sensitive (right now XPDF
1386          * code is always case insensitive for ASCII
1387          * and case sensitive for all other languaages)
1388          */
1389
1390         if (pdf_document->search &&
1391             strcmp (search_string, pdf_document->search->text) == 0)
1392                 return;
1393
1394         if (pdf_document->search)
1395                 pdf_document_search_free (pdf_document->search);
1396
1397         pdf_document->search = pdf_document_search_new (pdf_document,
1398                                                         page,
1399                                                         search_string);
1400 }
1401
1402 static int
1403 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1404 {
1405         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1406
1407         if (search) {
1408                 return g_list_length (search->pages[page]);
1409         } else {
1410                 return 0;
1411         }
1412 }
1413
1414 static gboolean
1415 pdf_document_find_get_result (EvDocumentFind *document_find,
1416                               int             page,
1417                               int             n_result,
1418                               EvRectangle    *rectangle)
1419 {
1420         PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1421         PdfDocumentSearch *search = pdf_document->search;
1422         PopplerPage *poppler_page;
1423         PopplerRectangle *r;
1424         double height;
1425
1426         if (search == NULL)
1427                 return FALSE;
1428
1429         r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1430                                                   n_result);
1431         if (r == NULL)
1432                 return FALSE;
1433
1434         poppler_page = poppler_document_get_page (pdf_document->document, page);
1435         poppler_page_get_size (poppler_page, NULL, &height);
1436         rectangle->x1 = r->x1;
1437         rectangle->y1 = height - r->y2;
1438         rectangle->x2 = r->x2;
1439         rectangle->y2 = height - r->y1;
1440         g_object_unref (poppler_page);
1441                 
1442         return TRUE;
1443 }
1444
1445 static int
1446 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1447                                     int             page)
1448 {
1449         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1450
1451         return search && search->pages[page] != NULL;
1452 }
1453
1454 static double
1455 pdf_document_find_get_progress (EvDocumentFind *document_find)
1456 {
1457         PdfDocumentSearch *search;
1458         int n_pages, pages_done;
1459
1460         search = PDF_DOCUMENT (document_find)->search;
1461
1462         if (search == NULL) {
1463                 return 0;
1464         }
1465
1466         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1467         if (search->search_page > search->start_page) {
1468                 pages_done = search->search_page - search->start_page + 1;
1469         } else if (search->search_page == search->start_page) {
1470                 pages_done = n_pages;
1471         } else {
1472                 pages_done = n_pages - search->start_page + search->search_page;
1473         }
1474
1475         return pages_done / (double) n_pages;
1476 }
1477
1478 static void
1479 pdf_document_find_cancel (EvDocumentFind *document)
1480 {
1481         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1482
1483         if (pdf_document->search) {
1484                 pdf_document_search_free (pdf_document->search);
1485                 pdf_document->search = NULL;
1486         }
1487 }
1488
1489 static void
1490 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1491 {
1492         iface->begin = pdf_document_find_begin;
1493         iface->get_n_results = pdf_document_find_get_n_results;
1494         iface->get_result = pdf_document_find_get_result;
1495         iface->page_has_results = pdf_document_find_page_has_results;
1496         iface->get_progress = pdf_document_find_get_progress;
1497         iface->cancel = pdf_document_find_cancel;
1498 }
1499
1500 static const gboolean supported_formats[] = {
1501         TRUE, /* EV_FILE_FORMAT_PS */
1502 #ifdef HAVE_CAIRO_PDF
1503 #ifdef HAVE_POPPLER_PAGE_RENDER
1504         TRUE, /* EV_FILE_FORMAT_PDF */
1505 #else
1506         FALSE, /* EV_FILE_FORMAT_PDF */
1507 #endif
1508 #endif
1509 };
1510
1511 static void
1512 pdf_print_context_free (PdfPrintContext *ctx)
1513 {
1514         if (!ctx)
1515                 return;
1516
1517         if (ctx->ps_file) {
1518                 poppler_ps_file_free (ctx->ps_file);
1519                 ctx->ps_file = NULL;
1520         }
1521 #ifdef HAVE_CAIRO_PDF
1522         if (ctx->pdf_cairo) {
1523                 cairo_destroy (ctx->pdf_cairo);
1524                 ctx->pdf_cairo = NULL;
1525         }
1526 #endif
1527         g_free (ctx);
1528 }
1529
1530 static gboolean
1531 pdf_document_file_exporter_format_supported (EvFileExporter      *exporter,
1532                                              EvFileExporterFormat format)
1533 {
1534         return supported_formats[format];
1535 }
1536
1537 static void
1538 pdf_document_file_exporter_begin (EvFileExporter      *exporter,
1539                                   EvFileExporterFormat format,
1540                                   const char          *filename,
1541                                   int                  first_page,
1542                                   int                  last_page,
1543                                   double               width,
1544                                   double               height,
1545                                   gboolean             duplex)
1546 {
1547         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1548         PdfPrintContext *ctx;
1549
1550         if (pdf_document->print_ctx)
1551                 pdf_print_context_free (pdf_document->print_ctx);
1552         pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1553         ctx = pdf_document->print_ctx;
1554         ctx->format = format;
1555         
1556         switch (format) {
1557                 case EV_FILE_FORMAT_PS:
1558                         ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1559                                                             filename, first_page,
1560                                                             last_page - first_page + 1);
1561                         poppler_ps_file_set_paper_size (ctx->ps_file, width, height);
1562                         poppler_ps_file_set_duplex (ctx->ps_file, duplex);
1563
1564                         break;
1565                 case EV_FILE_FORMAT_PDF: {
1566 #ifdef HAVE_CAIRO_PDF
1567                         cairo_surface_t *surface;
1568                         
1569                         surface = cairo_pdf_surface_create (filename, width, height);
1570                         ctx->pdf_cairo = cairo_create (surface);
1571                         cairo_surface_destroy (surface);
1572 #endif
1573                 }
1574                         break;
1575                 default:
1576                         g_assert_not_reached ();
1577         }
1578 }
1579
1580 static void
1581 pdf_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1582 {
1583         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1584         PdfPrintContext *ctx = pdf_document->print_ctx;
1585         PopplerPage *poppler_page;
1586
1587         g_return_if_fail (pdf_document->print_ctx != NULL);
1588
1589         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1590
1591         switch (ctx->format) {
1592                 case EV_FILE_FORMAT_PS:
1593                         poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1594                         break;
1595                 case EV_FILE_FORMAT_PDF:
1596 #ifdef HAVE_CAIRO_PDF
1597                         cairo_save (ctx->pdf_cairo);
1598 #endif
1599 #ifdef HAVE_POPPLER_PAGE_RENDER
1600                         poppler_page_render (poppler_page, ctx->pdf_cairo);
1601 #endif
1602 #ifdef HAVE_CAIRO_PDF
1603                         cairo_show_page (ctx->pdf_cairo);
1604                         cairo_restore (ctx->pdf_cairo);
1605 #endif
1606                         break;
1607                 default:
1608                         g_assert_not_reached ();
1609         }
1610         
1611         g_object_unref (poppler_page);
1612 }
1613
1614 static void
1615 pdf_document_file_exporter_end (EvFileExporter *exporter)
1616 {
1617         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1618
1619         pdf_print_context_free (pdf_document->print_ctx);
1620         pdf_document->print_ctx = NULL;
1621 }
1622
1623 static void
1624 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1625 {
1626         iface->format_supported = pdf_document_file_exporter_format_supported;
1627         iface->begin = pdf_document_file_exporter_begin;
1628         iface->do_page = pdf_document_file_exporter_do_page;
1629         iface->end = pdf_document_file_exporter_end;
1630 }
1631
1632 static void
1633 pdf_selection_render_selection (EvSelection      *selection,
1634                                 EvRenderContext  *rc,
1635                                 cairo_surface_t **surface,
1636                                 EvRectangle      *points,
1637                                 EvRectangle      *old_points,
1638                                 GdkColor         *text,
1639                                 GdkColor         *base)
1640 {
1641         PdfDocument *pdf_document;
1642         double width_points, height_points;
1643         gint width, height;
1644
1645         pdf_document = PDF_DOCUMENT (selection);
1646         set_rc_data (pdf_document, rc);
1647
1648         poppler_page_get_size (POPPLER_PAGE (rc->data),
1649                                &width_points, &height_points);
1650         width = (int) ((width_points * rc->scale) + 0.5);
1651         height = (int) ((height_points * rc->scale) + 0.5);
1652
1653
1654 #ifdef HAVE_POPPLER_PAGE_RENDER
1655         cairo_t *cr;
1656
1657         if (*surface == NULL) {
1658                 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1659                                                        width, height);
1660                 
1661         }
1662
1663         cr = cairo_create (*surface);
1664         cairo_scale (cr, rc->scale, rc->scale);
1665         cairo_surface_set_device_offset (*surface, 0, 0);
1666         memset (cairo_image_surface_get_data (*surface), 0x00,
1667                 cairo_image_surface_get_height (*surface) *
1668                 cairo_image_surface_get_stride (*surface));
1669         poppler_page_render_selection (POPPLER_PAGE (rc->data),
1670                                        cr,
1671                                        (PopplerRectangle *)points,
1672                                        (PopplerRectangle *)old_points,
1673                                        POPPLER_SELECTION_NORMAL, /* SelectionStyle */
1674                                        text,
1675                                        base);
1676         cairo_destroy (cr);
1677 #else /* HAVE_POPPLER_PAGE_RENDER */
1678         GdkPixbuf *pixbuf;
1679         
1680         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1681                                  TRUE, 8,
1682                                  width, height);
1683
1684         poppler_page_render_selection_to_pixbuf (POPPLER_PAGE (rc->data),
1685                                                  rc->scale, rc->rotation, pixbuf,
1686                                                  (PopplerRectangle *)points,
1687                                                  (PopplerRectangle *)old_points,
1688                                                  POPPLER_SELECTION_NORMAL, /* SelectionStyle */
1689                                                  text,
1690                                                  base);
1691         if (*surface)
1692                 cairo_surface_destroy (*surface);
1693         *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1694         g_object_unref (pixbuf);
1695 #endif /* HAVE_POPPLER_PAGE_RENDER */
1696 }
1697
1698
1699 static GdkRegion *
1700 pdf_selection_get_selection_region (EvSelection     *selection,
1701                                     EvRenderContext *rc,
1702                                     EvRectangle     *points)
1703 {
1704         PdfDocument *pdf_document;
1705         GdkRegion *retval;
1706
1707         pdf_document = PDF_DOCUMENT (selection);
1708
1709         set_rc_data (pdf_document, rc);
1710
1711         retval = poppler_page_get_selection_region ((PopplerPage *)rc->data,
1712                                                     rc->scale,
1713                                                     (PopplerRectangle *) points);
1714         return retval;
1715 }
1716
1717 static GdkRegion *
1718 pdf_selection_get_selection_map (EvSelection     *selection,
1719                                  EvRenderContext *rc)
1720 {
1721         PdfDocument *pdf_document;
1722         PopplerPage *poppler_page;
1723         PopplerRectangle points;
1724         GdkRegion *retval;
1725
1726         pdf_document = PDF_DOCUMENT (selection);
1727         poppler_page = poppler_document_get_page (pdf_document->document,
1728                                                   rc->page);
1729
1730         points.x1 = 0.0;
1731         points.y1 = 0.0;
1732         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1733         retval = poppler_page_get_selection_region (poppler_page, 1.0, &points);
1734         g_object_unref (poppler_page);
1735
1736         return retval;
1737 }
1738
1739 static void
1740 pdf_selection_iface_init (EvSelectionIface *iface)
1741 {
1742         iface->render_selection = pdf_selection_render_selection;
1743         iface->get_selection_region = pdf_selection_get_selection_region;
1744         iface->get_selection_map = pdf_selection_get_selection_map;
1745 }
1746
1747 /* Page Transitions */
1748 static gdouble
1749 pdf_document_get_page_duration (EvDocumentTransition *trans,
1750                                 gint                  page)
1751 {
1752         PdfDocument *pdf_document;
1753         PopplerPage *poppler_page;
1754         gdouble      duration = -1;
1755
1756         pdf_document = PDF_DOCUMENT (trans);
1757         poppler_page = poppler_document_get_page (pdf_document->document, page);
1758         if (!poppler_page)
1759                 return -1;
1760
1761         duration = poppler_page_get_duration (poppler_page);
1762         g_object_unref (poppler_page);
1763
1764         return duration;
1765 }
1766
1767 static void
1768 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1769 {
1770         iface->get_page_duration = pdf_document_get_page_duration;
1771 }
1772
1773 PdfDocument *
1774 pdf_document_new (void)
1775 {
1776         return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));
1777 }