]> www.fi.muni.cz Git - evince.git/blob - pdf/ev-poppler.cc
Separate scan and fill so that we can incrementally fill from the main
[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 <math.h>
21 #include <string.h>
22 #include <gtk/gtk.h>
23 #include <poppler.h>
24 #include <poppler-document.h>
25 #include <poppler-page.h>
26 #include <glib/gi18n.h>
27
28 #include "ev-poppler.h"
29 #include "ev-ps-exporter.h"
30 #include "ev-document-find.h"
31 #include "ev-document-misc.h"
32 #include "ev-document-links.h"
33 #include "ev-document-fonts.h"
34 #include "ev-document-security.h"
35 #include "ev-document-thumbnails.h"
36
37 typedef struct {
38         PdfDocument *document;
39         char *text;
40         GList **pages;
41         guint idle;
42         int start_page;
43         int search_page;
44 } PdfDocumentSearch;
45
46 struct _PdfDocumentClass
47 {
48         GObjectClass parent_class;
49 };
50
51 struct _PdfDocument
52 {
53         GObject parent_instance;
54
55         PopplerDocument *document;
56         PopplerPSFile *ps_file;
57         gchar *password;
58
59         PopplerFontInfo *font_info;
60         PopplerFontsIter *fonts_iter;
61         int fonts_scanned_pages;
62
63         PdfDocumentSearch *search;
64 };
65
66 static void pdf_document_document_iface_init            (EvDocumentIface           *iface);
67 static void pdf_document_security_iface_init            (EvDocumentSecurityIface   *iface);
68 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
69 static void pdf_document_document_links_iface_init      (EvDocumentLinksIface      *iface);
70 static void pdf_document_document_fonts_iface_init      (EvDocumentFontsIface      *iface);
71 static void pdf_document_find_iface_init                (EvDocumentFindIface       *iface);
72 static void pdf_document_ps_exporter_iface_init         (EvPSExporterIface         *iface);
73 static void pdf_document_thumbnails_get_dimensions      (EvDocumentThumbnails      *document_thumbnails,
74                                                          gint                       page,
75                                                          gint                       size,
76                                                          gint                      *width,
77                                                          gint                      *height);
78 static EvLink * ev_link_from_action (PopplerAction *action);
79
80
81 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
82                          {
83                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
84                                                         pdf_document_document_iface_init);
85                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
86                                                         pdf_document_security_iface_init);
87                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
88                                                         pdf_document_document_thumbnails_iface_init);
89                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
90                                                         pdf_document_document_links_iface_init);
91                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
92                                                         pdf_document_document_fonts_iface_init);
93                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
94                                                         pdf_document_find_iface_init);
95                                  G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
96                                                         pdf_document_ps_exporter_iface_init);
97                          });
98
99 static void
100 pdf_document_dispose (GObject *object)
101 {
102         PdfDocument *pdf_document = PDF_DOCUMENT(object);
103
104         if (pdf_document->document) {
105                 g_object_unref (pdf_document->document);
106         }
107
108         if (pdf_document->font_info) { 
109                 poppler_font_info_free (pdf_document->font_info);
110         }
111
112         if (pdf_document->fonts_iter) {
113                 poppler_fonts_iter_free (pdf_document->fonts_iter);
114         }
115 }
116
117 static void
118 pdf_document_class_init (PdfDocumentClass *klass)
119 {
120         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
121
122         g_object_class->dispose = pdf_document_dispose;
123 }
124
125 static void
126 pdf_document_init (PdfDocument *pdf_document)
127 {
128         pdf_document->password = NULL;
129 }
130
131 static void
132 convert_error (GError  *poppler_error,
133                GError **error)
134 {
135         if (poppler_error == NULL)
136                 return;
137
138         if (poppler_error->domain == POPPLER_ERROR) {
139                 /* convert poppler errors into EvDocument errors */
140                 gint code = EV_DOCUMENT_ERROR_INVALID;
141                 if (poppler_error->code == POPPLER_ERROR_INVALID)
142                         code = EV_DOCUMENT_ERROR_INVALID;
143                 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
144                         code = EV_DOCUMENT_ERROR_ENCRYPTED;
145                         
146
147                 g_set_error (error,
148                              EV_DOCUMENT_ERROR,
149                              code,
150                              poppler_error->message,
151                              NULL);
152         } else {
153                 g_propagate_error (error, poppler_error);
154         }
155 }
156
157
158 /* EvDocument */
159 static gboolean
160 pdf_document_save (EvDocument  *document,
161                    const char  *uri,
162                    GError     **error)
163 {
164         gboolean retval;
165         GError *poppler_error = NULL;
166
167         retval = poppler_document_save (PDF_DOCUMENT (document)->document,
168                                         uri,
169                                         &poppler_error);
170         if (! retval)
171                 convert_error (poppler_error, error);
172
173         return retval;
174 }
175
176 static gboolean
177 pdf_document_load (EvDocument   *document,
178                    const char   *uri,
179                    GError      **error)
180 {
181         GError *poppler_error = NULL;
182         PdfDocument *pdf_document = PDF_DOCUMENT (document);
183
184         pdf_document->document =
185                 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
186
187         if (pdf_document->document == NULL) {
188                 convert_error (poppler_error, error);
189                 return FALSE;
190         }
191
192         return TRUE;
193 }
194
195 static int
196 pdf_document_get_n_pages (EvDocument *document)
197 {
198         return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
199 }
200
201 static void
202 pdf_document_get_page_size (EvDocument   *document,
203                             int           page,
204                             double       *width,
205                             double       *height)
206 {
207         PopplerPage *poppler_page;
208
209         poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
210                                                   page);
211
212         poppler_page_get_size (poppler_page, width, height);
213 }
214
215 static char *
216 pdf_document_get_page_label (EvDocument *document,
217                              int         page)
218 {
219         PopplerPage *poppler_page;
220         char *label = NULL;
221
222         poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
223                                                   page);
224
225         g_object_get (G_OBJECT (poppler_page),
226                       "label", &label,
227                       NULL);
228
229         return label;
230 }
231
232 static GList *
233 pdf_document_get_links (EvDocument *document,
234                         int         page)
235 {
236         PdfDocument *pdf_document;
237         PopplerPage *poppler_page;
238         GList *retval = NULL;
239         GList *mapping_list;
240         GList *list;
241         double height;
242
243         pdf_document = PDF_DOCUMENT (document);
244         poppler_page = poppler_document_get_page (pdf_document->document,
245                                                   page);
246         mapping_list = poppler_page_get_link_mapping (poppler_page);
247         poppler_page_get_size (poppler_page, NULL, &height);
248
249         for (list = mapping_list; list; list = list->next) {
250                 PopplerLinkMapping *link_mapping;
251                 EvLinkMapping *ev_link_mapping;
252
253                 link_mapping = (PopplerLinkMapping *)list->data;
254                 ev_link_mapping = g_new (EvLinkMapping, 1);
255                 ev_link_mapping->link = ev_link_from_action (link_mapping->action);
256                 ev_link_mapping->x1 = link_mapping->area.x1;
257                 ev_link_mapping->x2 = link_mapping->area.x2;
258                 /* Invert this for X-style coordinates */
259                 ev_link_mapping->y1 = height - link_mapping->area.y2;
260                 ev_link_mapping->y2 = height - link_mapping->area.y1;
261
262                 retval = g_list_prepend (retval, ev_link_mapping);
263         }
264
265         poppler_page_free_link_mapping (mapping_list);
266
267         return g_list_reverse (retval);
268 }
269                         
270
271 static GdkPixbuf *
272 pdf_document_render_pixbuf (EvDocument   *document,
273                             int           page,
274                             double        scale)
275 {
276         PdfDocument *pdf_document;
277         PopplerPage *poppler_page;
278         GdkPixbuf *pixbuf;
279         double width_points, height_points;
280         gint width, height;
281
282         pdf_document = PDF_DOCUMENT (document);
283         poppler_page = poppler_document_get_page (pdf_document->document,
284                                                   page);
285
286         poppler_page_get_size (poppler_page, &width_points, &height_points);
287         width = (int) ((width_points * scale) + 0.5);
288         height = (int) ((height_points * scale) + 0.5);
289
290         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
291                                  FALSE, 8,
292                                  width, height);
293
294         poppler_page_render_to_pixbuf (poppler_page,
295                                        0, 0,
296                                        width, height,
297                                        scale,
298                                        pixbuf,
299                                        0, 0);
300
301         return pixbuf;
302 }
303
304 /* EvDocumentSecurity */
305
306 static gboolean
307 pdf_document_has_document_security (EvDocumentSecurity *document_security)
308 {
309         /* FIXME: do we really need to have this? */
310         return FALSE;
311 }
312
313 static void
314 pdf_document_set_password (EvDocumentSecurity *document_security,
315                            const char         *password)
316 {
317         PdfDocument *document = PDF_DOCUMENT (document_security);
318
319         if (document->password)
320                 g_free (document->password);
321
322         document->password = g_strdup (password);
323 }
324
325 static gboolean
326 pdf_document_can_get_text (EvDocument *document)
327 {
328         return TRUE;
329 }
330
331 static EvDocumentInfo *
332 pdf_document_get_info (EvDocument *document)
333 {
334         EvDocumentInfo *info;
335         PopplerPageLayout layout;
336         PopplerPageMode mode;
337         PopplerViewerPreferences view_prefs;
338         PopplerPermissions permissions;
339
340         info = g_new0 (EvDocumentInfo, 1);
341
342         info->fields_mask = EV_DOCUMENT_INFO_TITLE |
343                             EV_DOCUMENT_INFO_FORMAT |
344                             EV_DOCUMENT_INFO_AUTHOR |
345                             EV_DOCUMENT_INFO_SUBJECT |
346                             EV_DOCUMENT_INFO_KEYWORDS |
347                             EV_DOCUMENT_INFO_LAYOUT |
348                             EV_DOCUMENT_INFO_START_MODE |
349                             EV_DOCUMENT_INFO_PERMISSIONS |
350                             EV_DOCUMENT_INFO_UI_HINTS |
351                             EV_DOCUMENT_INFO_CREATOR |
352                             EV_DOCUMENT_INFO_PRODUCER |
353                             EV_DOCUMENT_INFO_CREATION_DATE |
354                             EV_DOCUMENT_INFO_MOD_DATE |
355                             EV_DOCUMENT_INFO_LINEARIZED |
356                             EV_DOCUMENT_INFO_N_PAGES |
357                             EV_DOCUMENT_INFO_SECURITY;
358
359
360         g_object_get (PDF_DOCUMENT (document)->document,
361                       "title", &(info->title),
362                       "format", &(info->format),
363                       "author", &(info->author),
364                       "subject", &(info->subject),
365                       "keywords", &(info->keywords),
366                       "page-mode", &mode,
367                       "page-layout", &layout,
368                       "viewer-preferences", &view_prefs,
369                       "permissions", &permissions,
370                       "creator", &(info->creator),
371                       "producer", &(info->producer),
372                       "creation-date", &(info->creation_date),
373                       "mod-date", &(info->modified_date),
374                       "linearized", &(info->linearized),
375                       NULL);
376
377         switch (layout) {
378                 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
379                         info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
380                         break;
381                 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
382                         info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
383                         break;
384                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
385                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
386                         break;
387                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
388                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
389                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
390                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
391                         break;
392                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
393                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
394                         break;
395                 default:
396                         break;
397         }
398
399         switch (mode) {
400                 case POPPLER_PAGE_MODE_NONE:
401                         info->mode = EV_DOCUMENT_MODE_NONE;
402                         break;
403                 case POPPLER_PAGE_MODE_USE_THUMBS:
404                         info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
405                         break;
406                 case POPPLER_PAGE_MODE_USE_OC:
407                         info->mode = EV_DOCUMENT_MODE_USE_OC;
408                         break;
409                 case POPPLER_PAGE_MODE_FULL_SCREEN:
410                         info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
411                         break;
412                 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
413                         info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
414                 default:
415                         break;
416         }
417
418         info->ui_hints = 0;
419         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
420                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
421         }
422         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
423                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
424         }
425         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
426                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
427         }
428         if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
429                 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
430         }
431         if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
432                 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
433         }
434         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
435                 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
436         }
437         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
438                 info->ui_hints |=  EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
439         }
440
441         info->permissions = 0;
442         if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
443                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
444         }
445         if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
446                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
447         }
448         if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
449                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
450         }
451         if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
452                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
453         }
454
455         info->n_pages = ev_document_get_n_pages (document);
456
457         if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
458                 /* translators: this is the document security state */
459                 info->security = g_strdup (_("Yes"));
460         } else {
461                 /* translators: this is the document security state */
462                 info->security = g_strdup (_("No"));
463         }
464
465         return info;
466 }
467
468 static char *
469 pdf_document_get_text (EvDocument *document, int page, EvRectangle *rect)
470 {
471         PdfDocument *pdf_document = PDF_DOCUMENT (document);
472         PopplerPage *poppler_page;
473         PopplerRectangle r;
474         double height;
475         
476         poppler_page = poppler_document_get_page (pdf_document->document, page);
477         g_return_val_if_fail (poppler_page != NULL, NULL);
478
479         poppler_page_get_size (poppler_page, NULL, &height);
480         r.x1 = rect->x1;
481         r.y1 = height - rect->y2;
482         r.x2 = rect->x2;
483         r.y2 = height - rect->y1;
484
485         return poppler_page_get_text (poppler_page, &r);
486 }
487
488 static void
489 pdf_document_document_iface_init (EvDocumentIface *iface)
490 {
491         iface->save = pdf_document_save;
492         iface->load = pdf_document_load;
493         iface->get_n_pages = pdf_document_get_n_pages;
494         iface->get_page_size = pdf_document_get_page_size;
495         iface->get_page_label = pdf_document_get_page_label;
496         iface->get_links = pdf_document_get_links;
497         iface->render_pixbuf = pdf_document_render_pixbuf;
498         iface->get_text = pdf_document_get_text;
499         iface->can_get_text = pdf_document_can_get_text;
500         iface->get_info = pdf_document_get_info;
501 };
502
503 static void
504 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
505 {
506         iface->has_document_security = pdf_document_has_document_security;
507         iface->set_password = pdf_document_set_password;
508 }
509
510 static gdouble
511 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
512 {
513         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
514         int n_pages;
515
516         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
517
518         return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
519 }
520
521 static gboolean
522 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
523                          int              n_pages)
524 {
525         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
526         gboolean result;
527
528         g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
529
530         if (pdf_document->font_info == NULL) { 
531                 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
532         }
533
534         if (pdf_document->fonts_iter) {
535                 poppler_fonts_iter_free (pdf_document->fonts_iter);
536         }
537
538         pdf_document->fonts_scanned_pages += n_pages;
539
540         result = poppler_font_info_scan (pdf_document->font_info, n_pages,
541                                          &pdf_document->fonts_iter);
542         if (!result) {
543                 pdf_document->fonts_scanned_pages = 0;
544                 poppler_font_info_free (pdf_document->font_info);
545                 pdf_document->font_info = NULL; 
546         }
547
548         return result;
549 }
550
551 static void
552 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
553                                GtkTreeModel    *model)
554 {
555         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
556         PopplerFontsIter *iter = pdf_document->fonts_iter;
557
558         g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
559
560         if (iter) {
561                 do {
562                         GtkTreeIter list_iter;
563                         PopplerIndexIter *child;
564                         const char *name;
565                 
566                         name = poppler_fonts_iter_get_name (iter);
567                         gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
568                         gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
569                                             EV_DOCUMENT_FONTS_COLUMN_NAME, name,
570                                             -1);
571                 } while (poppler_fonts_iter_next (iter));
572         }
573 }
574
575 static void
576 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
577 {
578         iface->fill_model = pdf_document_fonts_fill_model;
579         iface->scan = pdf_document_fonts_scan;
580         iface->get_progress = pdf_document_fonts_get_progress;
581 }
582
583 static gboolean
584 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
585 {
586         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
587         PopplerIndexIter *iter;
588
589         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
590
591         iter = poppler_index_iter_new (pdf_document->document);
592         if (iter == NULL)
593                 return FALSE;
594         poppler_index_iter_free (iter);
595
596         return TRUE;
597 }
598
599 static EvLink *
600 ev_link_from_action (PopplerAction *action)
601 {
602         EvLink *link;
603         const char *title;
604
605         title = action->any.title;
606         
607         if (action->type == POPPLER_ACTION_GOTO_DEST) {
608                 link = ev_link_new_page (title, action->goto_dest.dest->page_num - 1);
609         } else if (action->type == POPPLER_ACTION_URI) {
610                 link = ev_link_new_external (title, action->uri.uri);
611         } else {
612                 link = ev_link_new_title (title);
613         }
614
615         return link;    
616 }
617
618 static void
619 build_tree (PdfDocument      *pdf_document,
620             GtkTreeModel     *model,
621             GtkTreeIter      *parent,
622             PopplerIndexIter *iter)
623 {
624
625         do {
626                 GtkTreeIter tree_iter;
627                 PopplerIndexIter *child;
628                 PopplerAction *action;
629                 EvLink *link;
630                 
631                 action = poppler_index_iter_get_action (iter);
632                 if (action) {
633                         gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
634                         link = ev_link_from_action (action);
635                         poppler_action_free (action);
636
637                         gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
638                                             EV_DOCUMENT_LINKS_COLUMN_MARKUP, ev_link_get_title (link),
639                                             EV_DOCUMENT_LINKS_COLUMN_LINK, link,
640                                             -1);
641                         child = poppler_index_iter_get_child (iter);
642                         if (child)
643                                 build_tree (pdf_document, model, &tree_iter, child);
644                         poppler_index_iter_free (child);
645                 }
646         } while (poppler_index_iter_next (iter));
647 }
648
649
650 static GtkTreeModel *
651 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
652 {
653         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
654         GtkTreeModel *model = NULL;
655         PopplerIndexIter *iter;
656
657         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
658
659         iter = poppler_index_iter_new (pdf_document->document);
660         /* Create the model iff we have items*/
661         if (iter != NULL) {
662                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
663                                                              G_TYPE_STRING,
664                                                              G_TYPE_POINTER);
665                 build_tree (pdf_document, model, NULL, iter);
666                 poppler_index_iter_free (iter);
667         }
668         
669
670         return model;
671 }
672
673 static void
674 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
675 {
676         iface->has_document_links = pdf_document_links_has_document_links;
677         iface->get_links_model = pdf_document_links_get_links_model;
678 }
679
680 static GdkPixbuf *
681 make_thumbnail_for_size (PdfDocument *pdf_document,
682                          gint         page,
683                          gint         size,
684                          gboolean     border)
685 {
686         PopplerPage *poppler_page;
687         GdkPixbuf *pixbuf;
688         int width, height;
689         int x_offset, y_offset;
690         double scale;
691         gdouble unscaled_width, unscaled_height;
692
693         poppler_page = poppler_document_get_page (pdf_document->document, page);
694
695         g_return_val_if_fail (poppler_page != NULL, NULL);
696
697         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document), page, size, &width, &height);
698         poppler_page_get_size (poppler_page, &unscaled_width, &unscaled_height);
699         scale = width / unscaled_width;
700
701         if (border) {
702                 pixbuf = ev_document_misc_get_thumbnail_frame (width, height, NULL);
703                 x_offset = 1;
704                 y_offset = 1;
705         } else {
706                 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
707                                          width, height);
708                 gdk_pixbuf_fill (pixbuf, 0xffffffff);
709                 x_offset = 0;
710                 y_offset = 0;
711         }
712
713         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
714                                        width, height,
715                                        scale, pixbuf,
716                                        x_offset, y_offset);
717
718         return pixbuf;
719 }
720
721 static GdkPixbuf *
722 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
723                                        gint                  page,
724                                        gint                  size,
725                                        gboolean              border)
726 {
727         PdfDocument *pdf_document;
728         PopplerPage *poppler_page;
729         GdkPixbuf *pixbuf;
730
731         pdf_document = PDF_DOCUMENT (document_thumbnails);
732
733         poppler_page = poppler_document_get_page (pdf_document->document, page);
734         g_return_val_if_fail (poppler_page != NULL, NULL);
735
736         pixbuf = poppler_page_get_thumbnail (poppler_page);
737         if (pixbuf != NULL) {
738                 /* The document provides its own thumbnails. */
739                 if (border) {
740                         GdkPixbuf *real_pixbuf;
741
742                         real_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
743                         g_object_unref (pixbuf);
744                         pixbuf = real_pixbuf;
745                 }
746         } else {
747                 /* There is no provided thumbnail.  We need to make one. */
748                 pixbuf = make_thumbnail_for_size (pdf_document, page, size, border);
749         }
750         return pixbuf;
751 }
752
753 static void
754 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
755                                         gint                  page,
756                                         gint                  size,
757                                         gint                 *width,
758                                         gint                 *height)
759 {
760         PdfDocument *pdf_document;
761         PopplerPage *poppler_page;
762         gint has_thumb;
763         
764         pdf_document = PDF_DOCUMENT (document_thumbnails);
765         poppler_page = poppler_document_get_page (pdf_document->document, page);
766
767         g_return_if_fail (width != NULL);
768         g_return_if_fail (height != NULL);
769         g_return_if_fail (poppler_page != NULL);
770
771         has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
772
773         if (!has_thumb) {
774                 double page_width, page_height;
775
776                 poppler_page_get_size (poppler_page, &page_width, &page_height);
777                 if (page_width > page_height) {
778                         *width = size;
779                         *height = (int) (size * page_height / page_width);
780                 } else {
781                         *width = (int) (size * page_width / page_height);
782                         *height = size;
783                 }
784         }
785 }
786
787 static void
788 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
789 {
790         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
791         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
792 }
793
794
795 static gboolean
796 pdf_document_search_idle_callback (void *data)
797 {
798         PdfDocumentSearch *search = (PdfDocumentSearch*) data;
799         PdfDocument *pdf_document = search->document;
800         int n_pages;
801         GList *matches;
802         PopplerPage *page;
803
804         page = poppler_document_get_page (search->document->document,
805                                           search->search_page);
806
807         ev_document_doc_mutex_lock ();
808         matches = poppler_page_find_text (page, search->text);
809         ev_document_doc_mutex_unlock ();
810
811         search->pages[search->search_page] = matches;
812         ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
813                                   search->search_page);
814
815         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
816         search->search_page += 1;
817         if (search->search_page == n_pages) {
818                 /* wrap around */
819                 search->search_page = 0;
820         }
821
822         if (search->search_page != search->start_page) {
823                 return TRUE;
824         }
825
826         /* We're done. */
827         search->idle = 0; /* will return FALSE to remove */
828         return FALSE;
829 }
830
831
832 static PdfDocumentSearch *
833 pdf_document_search_new (PdfDocument *pdf_document,
834                          int          start_page,
835                          const char  *text)
836 {
837         PdfDocumentSearch *search;
838         int n_pages;
839         int i;
840
841         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
842
843         search = g_new0 (PdfDocumentSearch, 1);
844
845         search->text = g_strdup (text);
846         search->pages = g_new0 (GList *, n_pages);
847         for (i = 0; i < n_pages; i++) {
848                 search->pages[i] = NULL;
849         }
850
851         search->document = pdf_document;
852
853         /* We add at low priority so the progress bar repaints */
854         search->idle = g_idle_add_full (G_PRIORITY_LOW,
855                                         pdf_document_search_idle_callback,
856                                         search,
857                                         NULL);
858
859         search->start_page = start_page;
860         search->search_page = start_page;
861
862         return search;
863 }
864
865 static void
866 pdf_document_search_free (PdfDocumentSearch   *search)
867 {
868         PdfDocument *pdf_document = search->document;
869         int n_pages;
870         int i;
871
872         if (search->idle != 0)
873                 g_source_remove (search->idle);
874
875         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
876         for (i = 0; i < n_pages; i++) {
877                 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
878                 g_list_free (search->pages[i]);
879         }
880         
881         g_free (search->text);
882 }
883
884 static void
885 pdf_document_find_begin (EvDocumentFind   *document,
886                          int               page,
887                          const char       *search_string,
888                          gboolean          case_sensitive)
889 {
890         PdfDocument *pdf_document = PDF_DOCUMENT (document);
891
892         /* FIXME handle case_sensitive (right now XPDF
893          * code is always case insensitive for ASCII
894          * and case sensitive for all other languaages)
895          */
896
897         if (pdf_document->search &&
898             strcmp (search_string, pdf_document->search->text) == 0)
899                 return;
900
901         if (pdf_document->search)
902                 pdf_document_search_free (pdf_document->search);
903
904         pdf_document->search = pdf_document_search_new (pdf_document,
905                                                         page,
906                                                         search_string);
907 }
908
909 int
910 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
911 {
912         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
913
914         if (search) {
915                 return g_list_length (search->pages[page]);
916         } else {
917                 return 0;
918         }
919 }
920
921 gboolean
922 pdf_document_find_get_result (EvDocumentFind *document_find,
923                               int             page,
924                               int             n_result,
925                               EvRectangle    *rectangle)
926 {
927         PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
928         PdfDocumentSearch *search = pdf_document->search;
929         PopplerPage *poppler_page;
930         PopplerRectangle *r;
931         double height;
932
933         if (search == NULL)
934                 return FALSE;
935
936         r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
937                                                   n_result);
938         if (r == NULL)
939                 return FALSE;
940
941         poppler_page = poppler_document_get_page (pdf_document->document, page);
942         poppler_page_get_size (poppler_page, NULL, &height);
943         rectangle->x1 = r->x1;
944         rectangle->y1 = height - r->y2;
945         rectangle->x2 = r->x2;
946         rectangle->y2 = height - r->y1;
947         
948         return TRUE;
949 }
950
951 int
952 pdf_document_find_page_has_results (EvDocumentFind *document_find,
953                                     int             page)
954 {
955         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
956
957         g_return_val_if_fail (search != NULL, FALSE);
958
959         return search->pages[page] != NULL;
960 }
961
962 double
963 pdf_document_find_get_progress (EvDocumentFind *document_find)
964 {
965         PdfDocumentSearch *search;
966         int n_pages, pages_done;
967
968         search = PDF_DOCUMENT (document_find)->search;
969
970         if (search == NULL) {
971                 return 0;
972         }
973
974         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
975         if (search->search_page > search->start_page) {
976                 pages_done = search->search_page - search->start_page + 1;
977         } else if (search->search_page == search->start_page) {
978                 pages_done = n_pages;
979         } else {
980                 pages_done = n_pages - search->start_page + search->search_page;
981         }
982
983         return pages_done / (double) n_pages;
984 }
985
986 static void
987 pdf_document_find_cancel (EvDocumentFind *document)
988 {
989         PdfDocument *pdf_document = PDF_DOCUMENT (document);
990
991         if (pdf_document->search) {
992                 pdf_document_search_free (pdf_document->search);
993                 pdf_document->search = NULL;
994         }
995 }
996
997 static void
998 pdf_document_find_iface_init (EvDocumentFindIface *iface)
999 {
1000         iface->begin = pdf_document_find_begin;
1001         iface->get_n_results = pdf_document_find_get_n_results;
1002         iface->get_result = pdf_document_find_get_result;
1003         iface->page_has_results = pdf_document_find_page_has_results;
1004         iface->get_progress = pdf_document_find_get_progress;
1005         iface->cancel = pdf_document_find_cancel;
1006 }
1007
1008 static void
1009 pdf_document_ps_exporter_begin (EvPSExporter *exporter, const char *filename,
1010                                 int first_page, int last_page)
1011 {
1012         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1013         
1014         pdf_document->ps_file = poppler_ps_file_new (pdf_document->document, filename,
1015                                                      first_page,
1016                                                      last_page - first_page + 1);
1017 }
1018
1019 static void
1020 pdf_document_ps_exporter_do_page (EvPSExporter *exporter, int page)
1021 {
1022         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1023         PopplerPage *poppler_page;
1024
1025         g_return_if_fail (pdf_document->ps_file != NULL);
1026
1027         poppler_page = poppler_document_get_page (pdf_document->document, page);
1028         poppler_page_render_to_ps (poppler_page, pdf_document->ps_file);
1029 }
1030
1031 static void
1032 pdf_document_ps_exporter_end (EvPSExporter *exporter)
1033 {
1034         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1035
1036         poppler_ps_file_free (pdf_document->ps_file);
1037         pdf_document->ps_file = NULL;
1038 }
1039
1040 static void
1041 pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1042 {
1043         iface->begin = pdf_document_ps_exporter_begin;
1044         iface->do_page = pdf_document_ps_exporter_do_page;
1045         iface->end = pdf_document_ps_exporter_end;
1046 }
1047
1048 PdfDocument *
1049 pdf_document_new (void)
1050 {
1051         return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));
1052 }