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