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