]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdf-document.cc
poor man's dropshadow.
[evince.git] / pdf / xpdf / pdf-document.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 <glib/gi18n.h>
21
22 #include "gpdf-g-switch.h"
23 #include "pdf-document.h"
24 #include "ev-ps-exporter.h"
25 #include "ev-document-find.h"
26 #include "gpdf-g-switch.h"
27 #include "ev-document-bookmarks.h"
28 #include "ev-document-thumbnails.h"
29
30 #include "GlobalParams.h"
31 #include "GDKSplashOutputDev.h"
32 #include "PDFDoc.h"
33 #include "Outline.h"
34 #include "UnicodeMap.h"
35 #include "GlobalParams.h"
36 #include "GfxState.h"
37 #include "Thumb.h"
38 #include "goo/GList.h"
39 #include "PSOutputDev.h"
40
41 enum {
42         PROP_0,
43         PROP_TITLE
44 };
45
46 typedef struct
47 {
48         PdfDocument *document;
49         gunichar *ucs4;
50         glong ucs4_len;
51         guint idle;
52         /* full results are only possible for the rendered current page */
53         int current_page;
54         GArray *current_page_results;
55         guchar *other_page_flags; /* length n_pages + 1, first element ignored */
56         int start_page;   /* skip this one as we iterate, since we did it first */
57         int search_page;  /* the page we're searching now */
58         TextOutputDev *output_dev;
59 } PdfDocumentSearch;
60
61 typedef struct _PdfDocumentClass PdfDocumentClass;
62
63 #define PDF_DOCUMENT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), PDF_TYPE_DOCUMENT, PdfDocumentClass))
64 #define PDF_IS_DOCUMENT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), PDF_TYPE_DOCUMENT))
65 #define PDF_DOCUMENT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), PDF_TYPE_DOCUMENT, PdfDocumentClass))
66
67 struct _PdfDocumentClass
68 {
69         GObjectClass parent_class;
70 };
71
72 struct _PdfDocument
73 {
74         GObject parent_instance;
75
76         int page;
77         int page_x_offset;
78         int page_y_offset;
79         double scale;
80         GdkDrawable *target;
81
82         GDKSplashOutputDev *out;
83         PSOutputDev *ps_out;
84         PDFDoc *doc;
85         UnicodeMap *umap;
86
87         gboolean page_valid;
88
89         PdfDocumentSearch *search;
90 };
91
92 static void pdf_document_document_bookmarks_iface_init  (EvDocumentBookmarksIface  *iface);
93 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
94 static void pdf_document_document_iface_init            (EvDocumentIface           *iface);
95 static void pdf_document_ps_exporter_iface_init (EvPSExporterIface   *iface);
96 static void pdf_document_find_iface_init        (EvDocumentFindIface *iface);
97 static void pdf_document_search_free            (PdfDocumentSearch   *search);
98 static void pdf_document_search_page_changed    (PdfDocumentSearch   *search);
99
100 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
101                          {
102                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
103                                                         pdf_document_document_iface_init);
104                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_BOOKMARKS,
105                                                         pdf_document_document_bookmarks_iface_init);
106                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
107                                                         pdf_document_document_thumbnails_iface_init);
108                                  G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
109                                                         pdf_document_ps_exporter_iface_init);
110                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
111                                                         pdf_document_find_iface_init);
112                          });
113
114 static gboolean
115 document_validate_page (PdfDocument *pdf_document)
116 {
117         if (!pdf_document->page_valid) {
118                 pdf_document->doc->displayPage (pdf_document->out, pdf_document->page,
119                                                 72 * pdf_document->scale,
120                                                 72 * pdf_document->scale,
121                                                 0, gTrue, gTrue);
122
123                 pdf_document->page_valid = TRUE;
124
125                 /* Update the search results available to the app since
126                  * we only provide full results on the current page
127                  */
128                 if (pdf_document->search)
129                         pdf_document_search_page_changed (pdf_document->search);
130         }
131
132         return pdf_document->page_valid;
133 }
134
135 static gboolean
136 pdf_document_load (EvDocument  *document,
137                    const char  *uri,
138                    GError     **error)
139 {
140         PdfDocument *pdf_document = PDF_DOCUMENT (document);
141         PDFDoc *newDoc;
142         int err;
143         char *filename;
144         GString *filename_g;
145         GString *enc;
146
147         if (!globalParams) {
148                 globalParams = new GlobalParams("/etc/xpdfrc");
149                 globalParams->setupBaseFontsFc(NULL);
150         }
151
152         if (! pdf_document->umap) {
153                 enc = new GString("UTF-8");
154                 pdf_document->umap = globalParams->getUnicodeMap(enc);
155                 pdf_document->umap->incRefCnt ();
156                 delete enc;
157         }
158
159         filename = g_filename_from_uri (uri, NULL, error);
160         if (!filename)
161                 return FALSE;
162
163         filename_g = new GString (filename);
164         g_free (filename);
165
166         // open the PDF file, assumes ownership of filename_g
167         newDoc = new PDFDoc(filename_g, 0, 0);
168
169         if (!newDoc->isOk()) {
170                 err = newDoc->getErrorCode();
171                 delete newDoc;
172
173                 /* FIXME: Add a real error enum to EvDocument */
174                 g_set_error (error, G_FILE_ERROR,
175                              G_FILE_ERROR_FAILED,
176                              "Failed to load document (error %d) '%s'\n",
177                              err,
178                              uri);
179
180                 return FALSE;
181         }
182
183         if (pdf_document->doc)
184                 delete pdf_document->doc;
185         pdf_document->doc = newDoc;
186
187         pdf_document->page = 1;
188
189         if (pdf_document->out)
190                 pdf_document->out->startDoc(pdf_document->doc->getXRef());
191
192         pdf_document->page_valid = FALSE;
193
194         g_object_notify (G_OBJECT (pdf_document), "title");
195
196         return TRUE;
197 }
198
199 static int
200 pdf_document_get_n_pages (EvDocument  *document)
201 {
202         PdfDocument *pdf_document = PDF_DOCUMENT (document);
203
204         if (pdf_document->doc)
205                 return pdf_document->doc->getNumPages();
206         else
207                 return 1;
208 }
209
210 static void
211 pdf_document_set_page (EvDocument  *document,
212                        int          page)
213 {
214         PdfDocument *pdf_document = PDF_DOCUMENT (document);
215
216         page = CLAMP (page, 1, ev_document_get_n_pages (document));
217
218         if (page != pdf_document->page) {
219                 pdf_document->page = page;
220                 pdf_document->page_valid = FALSE;
221         }
222
223         ev_document_changed (document);
224 }
225
226 static int
227 pdf_document_get_page (EvDocument  *document)
228 {
229         PdfDocument *pdf_document = PDF_DOCUMENT (document);
230
231         return pdf_document->page;
232 }
233
234 static void
235 redraw_callback (void *data)
236 {
237         /* Need to hook up through a EvDocument callback? */
238 }
239
240 static void
241 pdf_document_set_target (EvDocument  *document,
242                          GdkDrawable *target)
243 {
244         PdfDocument *pdf_document = PDF_DOCUMENT (document);
245
246         if (pdf_document->target != target) {
247                 if (pdf_document->target)
248                         g_object_unref (pdf_document->target);
249
250                 pdf_document->target = target;
251
252                 if (pdf_document->target)
253                         g_object_ref (pdf_document->target);
254
255                 if (pdf_document->out) {
256                         delete pdf_document->out;
257                         pdf_document->out = NULL;
258                 }
259
260                 if (pdf_document->target) {
261                         pdf_document->out = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
262                                                          redraw_callback, (void*) document);
263
264                         if (pdf_document->doc)
265                                 pdf_document->out->startDoc(pdf_document->doc->getXRef());
266
267                 }
268
269                 pdf_document->page_valid = FALSE;
270         }
271 }
272
273 static void
274 pdf_document_set_scale (EvDocument  *document,
275                         double       scale)
276 {
277         PdfDocument *pdf_document = PDF_DOCUMENT (document);
278
279         if (pdf_document->scale != scale) {
280                 pdf_document->scale = scale;
281                 pdf_document->page_valid = FALSE;
282         }
283 }
284
285 static void
286 pdf_document_set_page_offset (EvDocument  *document,
287                               int          x,
288                               int          y)
289 {
290         PdfDocument *pdf_document = PDF_DOCUMENT (document);
291
292         pdf_document->page_x_offset = x;
293         pdf_document->page_y_offset = y;
294 }
295
296 static void
297 pdf_document_get_page_size (EvDocument   *document,
298                             int          *width,
299                             int          *height)
300 {
301         PdfDocument *pdf_document = PDF_DOCUMENT (document);
302
303         if (document_validate_page (pdf_document)) {
304                 if (width)
305                         *width = pdf_document->out->getBitmapWidth();
306                 if (height)
307                         *height = pdf_document->out->getBitmapHeight();
308         } else {
309                 if (width)
310                         *width = 1;
311                 if (height)
312                         *height = 1;
313         }
314 }
315
316 static void
317 pdf_document_render (EvDocument  *document,
318                      int          clip_x,
319                      int          clip_y,
320                      int          clip_width,
321                      int          clip_height)
322 {
323         PdfDocument *pdf_document = PDF_DOCUMENT (document);
324         GdkRectangle page;
325         GdkRectangle draw;
326
327         if (!document_validate_page (pdf_document) || !pdf_document->target)
328                 return;
329
330         page.x = pdf_document->page_x_offset;
331         page.y = pdf_document->page_y_offset;
332         page.width = pdf_document->out->getBitmapWidth();
333         page.height = pdf_document->out->getBitmapHeight();
334
335         draw.x = clip_x;
336         draw.y = clip_y;
337         draw.width = clip_width;
338         draw.height = clip_height;
339
340         if (gdk_rectangle_intersect (&page, &draw, &draw))
341                 pdf_document->out->redraw (draw.x - page.x, draw.y - page.y,
342                                            pdf_document->target,
343                                            draw.x, draw.y,
344                                            draw.width, draw.height);
345 }
346
347 static void
348 pdf_document_search_emit_found (PdfDocumentSearch *search)
349 {
350         PdfDocument *pdf_document = search->document;
351         int n_pages;
352         int pages_done;
353         GArray *tmp_results;
354         int i;
355
356         n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
357         if (search->search_page > search->start_page) {
358                 pages_done = search->search_page - search->start_page;
359         } else if (search->search_page == search->start_page) {
360                 pages_done = n_pages;
361         } else {
362                 pages_done = n_pages - search->start_page + search->search_page;
363         }
364
365         tmp_results = g_array_new (FALSE, FALSE, sizeof (EvFindResult));
366         g_array_append_vals (tmp_results,
367                              search->current_page_results->data,
368                              search->current_page_results->len);
369
370         /* Now append a bogus element for each page that has a result in it,
371          * that is not the current page
372          */
373         i = 1;
374         while (i <= n_pages) {
375                 if (i != pdf_document->page &&
376                     search->other_page_flags[i]) {
377                         EvFindResult result;
378
379                         result.page_num = i;
380
381                         /* Use bogus coordinates, again we can't get coordinates
382                          * until this is the current page because TextOutputDev
383                          * isn't good enough
384                          */
385                         result.highlight_area.x = -1;
386                         result.highlight_area.y = -1;
387                         result.highlight_area.width = 1;
388                         result.highlight_area.height = 1;
389
390                         g_array_append_val (tmp_results, result);
391                 }
392
393                 ++i;
394         }
395
396         ev_document_find_found (EV_DOCUMENT_FIND (pdf_document),
397                                 (EvFindResult*) tmp_results->data,
398                                 tmp_results->len,
399                                 pages_done / (double) n_pages);
400
401         g_array_free (tmp_results, TRUE);
402 }
403
404 static void
405 pdf_document_search_page_changed (PdfDocumentSearch   *search)
406 {
407         PdfDocument *pdf_document = search->document;
408         int current_page;
409         EvFindResult result;
410         int xMin, yMin, xMax, yMax;
411
412         current_page = pdf_document->page;
413
414         if (!pdf_document->page_valid) {
415                 /* we can't do anything until displayPage() */
416                 search->current_page = -1;
417                 return;
418         }
419
420         if (search->current_page == current_page)
421                 return;
422
423         /* We need to create current_page_results for the new current page */
424         g_array_set_size (search->current_page_results, 0);
425
426         if (pdf_document->out->findText (search->ucs4, search->ucs4_len,
427                                          gTrue, gTrue, // startAtTop, stopAtBottom
428                                          gFalse, gFalse, // startAtLast, stopAtLast
429                                          &xMin, &yMin, &xMax, &yMax)) {
430                 result.page_num = pdf_document->page;
431
432                 result.highlight_area.x = xMin;
433                 result.highlight_area.y = yMin;
434                 result.highlight_area.width = xMax - xMin;
435                 result.highlight_area.height = yMax - yMin;
436
437                 g_array_append_val (search->current_page_results, result);
438                 /* Now find further results */
439
440                 while (pdf_document->out->findText (search->ucs4, search->ucs4_len,
441                                                     gFalse, gTrue,
442                                                     gTrue, gFalse,
443                                                     &xMin, &yMin, &xMax, &yMax)) {
444
445                         result.page_num = pdf_document->page;
446
447                         result.highlight_area.x = xMin;
448                         result.highlight_area.y = yMin;
449                         result.highlight_area.width = xMax - xMin;
450                         result.highlight_area.height = yMax - yMin;
451
452                         g_array_append_val (search->current_page_results, result);
453                 }
454         }
455
456         /* needed for the initial current page since we don't search
457          * it in the idle
458          */
459         search->other_page_flags[current_page] =
460                 search->current_page_results->len > 0;
461
462         pdf_document_search_emit_found (search);
463 }
464
465 static gboolean
466 pdf_document_search_idle_callback (void *data)
467 {
468         PdfDocumentSearch *search = (PdfDocumentSearch*) data;
469         PdfDocument *pdf_document = search->document;
470         int n_pages;
471         double xMin, yMin, xMax, yMax;
472
473         /* Note that PDF page count is 1 through n_pages INCLUSIVE
474          * like a real book. We are looking to add one result for each
475          * page with a match, because the coordinates are meaningless
476          * with TextOutputDev, so we just want to flag matching pages
477          * and then when the user switches to the current page, we
478          * will emit "found" again with the real results.
479          */
480         n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
481
482         if (search->search_page == search->start_page) {
483                 goto end_search;
484         }
485
486         if (search->output_dev == 0) {
487                 /* First time through here... */
488                 search->output_dev = new TextOutputDev (NULL, gTrue, gFalse, gFalse);
489                 if (!search->output_dev->isOk()) {
490                         goto end_search;
491                 }
492         }
493
494         pdf_document->doc->displayPage (search->output_dev,
495                                         search->search_page,
496                                         72, 72, 0, gTrue, gFalse);
497
498         if (search->output_dev->findText (search->ucs4,
499                                           search->ucs4_len,
500                                           gTrue, gTrue, // startAtTop, stopAtBottom
501                                           gFalse, gFalse, // startAtLast, stopAtLast
502                                           &xMin, &yMin, &xMax, &yMax)) {
503                 /* This page has results */
504                 search->other_page_flags[search->search_page] = TRUE;
505         }
506
507         search->search_page += 1;
508         if (search->search_page > n_pages) {
509                 /* wrap around */
510                 search->search_page = 1;
511         }
512
513         /* We do this even if nothing was found, to update the percent complete */
514         pdf_document_search_emit_found (search);
515
516         return TRUE;
517
518  end_search:
519         /* We're done. */
520         search->idle = 0; /* will return FALSE to remove */
521         return FALSE;
522 }
523
524 static void
525 pdf_document_find_begin (EvDocumentFind   *document,
526                          const char       *search_string,
527                          gboolean          case_sensitive)
528 {
529         PdfDocument *pdf_document = PDF_DOCUMENT (document);
530         PdfDocumentSearch *search;
531         int n_pages;
532         gunichar *ucs4;
533         glong ucs4_len;
534
535         /* FIXME handle case_sensitive (right now XPDF
536          * code is always case insensitive for ASCII
537          * and case sensitive for all other languaages)
538          */
539
540         g_assert (sizeof (gunichar) == sizeof (Unicode));
541         ucs4 = g_utf8_to_ucs4_fast (search_string, -1,
542                                     &ucs4_len);
543
544         if (pdf_document->search &&
545             pdf_document->search->ucs4_len == ucs4_len &&
546             memcmp (pdf_document->search->ucs4,
547                     ucs4,
548                     sizeof (gunichar) * ucs4_len) == 0) {
549                 /* Search is unchanged */
550                 g_free (ucs4);
551                 return;
552         }
553
554         if (pdf_document->search) {
555                 pdf_document_search_free (pdf_document->search);
556                 pdf_document->search = NULL;
557         }
558
559         search = g_new0 (PdfDocumentSearch, 1);
560
561         search->ucs4 = ucs4;
562         search->ucs4_len = ucs4_len;
563
564         search->current_page_results = g_array_new (FALSE,
565                                                     FALSE,
566                                                     sizeof (EvFindResult));
567         n_pages = ev_document_get_n_pages (EV_DOCUMENT (document));
568
569         /* This is an array of bool; with the first value ignored
570          * so we can index by the based-at-1 page numbers
571          */
572         search->other_page_flags = g_new0 (guchar, n_pages + 1);
573
574         search->document = pdf_document;
575
576         /* We add at low priority so the progress bar repaints */
577         search->idle = g_idle_add_full (G_PRIORITY_LOW,
578                                         pdf_document_search_idle_callback,
579                                         search,
580                                         NULL);
581
582         search->output_dev = 0;
583
584         search->start_page = pdf_document->page;
585         search->search_page = search->start_page + 1;
586         if (search->search_page > n_pages)
587                 search->search_page = 1;
588
589         search->current_page = -1;
590
591         pdf_document->search = search;
592
593         /* Update for the current page right away */
594         pdf_document_search_page_changed (search);
595 }
596
597 static void
598 pdf_document_find_cancel (EvDocumentFind   *document)
599 {
600         PdfDocument *pdf_document = PDF_DOCUMENT (document);
601
602         if (pdf_document->search) {
603                 pdf_document_search_free (pdf_document->search);
604                 pdf_document->search = NULL;
605         }
606 }
607
608 static void
609 pdf_document_search_free (PdfDocumentSearch   *search)
610 {
611         if (search->idle != 0)
612                 g_source_remove (search->idle);
613
614         if (search->output_dev)
615                 delete search->output_dev;
616
617         g_array_free (search->current_page_results, TRUE);
618         g_free (search->other_page_flags);
619
620         g_free (search->ucs4);
621         g_free (search);
622 }
623
624 static void
625 pdf_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
626 {
627         PdfDocument *document = PDF_DOCUMENT (exporter);
628
629         if (document->ps_out)
630                 delete document->ps_out;
631
632         document->ps_out = new PSOutputDev ((char *)filename, document->doc->getXRef(),
633                                             document->doc->getCatalog(), 1,
634                                             ev_document_get_n_pages (EV_DOCUMENT (document)),
635                                             psModePS);
636 }
637
638 static void
639 pdf_document_ps_export_do_page (EvPSExporter *exporter, int page)
640 {
641         PdfDocument *document = PDF_DOCUMENT (exporter);
642
643         document->doc->displayPage (document->ps_out, page,
644                                     72.0, 72.0, 0, gTrue, gFalse);
645 }
646
647 static void
648 pdf_document_ps_export_end (EvPSExporter *exporter)
649 {
650         PdfDocument *document = PDF_DOCUMENT (exporter);
651
652         delete document->ps_out;
653         document->ps_out = NULL;
654 }
655
656
657 /* EvDocumentBookmarks Implementation */
658 typedef struct
659 {
660         /* goo GList, not glib */
661         GList *items;
662         int index;
663         int level;
664 } BookmarksIter;
665
666 static gchar *
667 unicode_to_char (OutlineItem *outline_item,
668                  UnicodeMap *uMap)
669 {
670         GString gstr;
671         gchar buf[8]; /* 8 is enough for mapping an unicode char to a string */
672         int i, n;
673
674         for (i = 0; i < outline_item->getTitleLength(); ++i) {
675                 n = uMap->mapUnicode(outline_item->getTitle()[i], buf, sizeof(buf));
676                 gstr.append(buf, n);
677         }
678
679         return g_strdup (gstr.getCString ());
680 }
681
682
683 static gboolean
684 pdf_document_bookmarks_has_document_bookmarks (EvDocumentBookmarks *document_bookmarks)
685 {
686         PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
687         Outline *outline;
688
689         g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
690
691         outline = pdf_document->doc->getOutline();
692         if (outline->getItems() != NULL &&
693             outline->getItems()->getLength() > 0)
694                 return TRUE;
695
696         return FALSE;
697 }
698
699 static EvDocumentBookmarksIter *
700 pdf_document_bookmarks_begin_read (EvDocumentBookmarks *document_bookmarks)
701 {
702         PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
703         Outline *outline;
704         BookmarksIter *iter;
705         GList *items;
706
707         g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), NULL);
708
709         outline = pdf_document->doc->getOutline();
710         items = outline->getItems();
711         if (! items)
712                 return NULL;
713
714         iter = g_new0 (BookmarksIter, 1);
715         iter->items = items;
716         iter->index = 0;
717         iter->level = 0;
718
719         return (EvDocumentBookmarksIter *) iter;
720 }
721
722 static gboolean
723 pdf_document_bookmarks_get_values (EvDocumentBookmarks      *document_bookmarks,
724                                    EvDocumentBookmarksIter  *bookmarks_iter,
725                                    char                    **title,
726                                    EvDocumentBookmarksType  *type,
727                                    gint                     *page)
728 {
729         PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
730         BookmarksIter *iter = (BookmarksIter *)bookmarks_iter;
731         OutlineItem *anItem;
732         LinkAction *link_action;
733         LinkDest *link_dest = NULL;
734         LinkURI *link_uri = NULL;
735         LinkGoTo *link_goto = NULL;
736         GString *named_dest;
737         Unicode *link_title;
738         Ref page_ref;
739         gint page_num = -1;
740
741         g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
742         g_return_val_if_fail (iter != NULL, FALSE);
743         g_return_val_if_fail (title != NULL, FALSE);
744         g_return_val_if_fail (type != NULL, FALSE);
745         g_return_val_if_fail (page != NULL, FALSE);
746
747         anItem = (OutlineItem *)iter->items->get(iter->index);
748         link_action = anItem->getAction ();
749         link_title = anItem->getTitle ();
750
751         if (link_action) {
752                 switch (link_action->getKind ()) {
753
754                 case actionGoTo:
755                         link_goto = dynamic_cast <LinkGoTo *> (link_action);
756                         link_dest = link_goto->getDest ();
757                         named_dest = link_goto->getNamedDest ();
758
759                         /* Wow!  This seems excessively slow on large
760                          * documents. I need to investigate more... -jrb */
761                         if (link_dest != NULL) {
762                                 link_dest = link_dest->copy ();
763                         } else if (named_dest != NULL) {
764                                 named_dest = named_dest->copy ();
765                                 link_dest = pdf_document->doc->findDest (named_dest);
766                                 delete named_dest;
767                         }
768                         if (link_dest != NULL) {
769                                 if (link_dest->isPageRef ()) {
770                                         page_ref = link_dest->getPageRef ();
771                                         page_num = pdf_document->doc->findPage (page_ref.num, page_ref.gen);
772                                 } else {
773                                         page_num = link_dest->getPageNum ();
774                                 }
775
776                                 delete link_dest;
777                         }
778
779                         break;
780                 case actionURI:
781                         link_uri = dynamic_cast <LinkURI *> (link_action);
782                         break;
783
784                 case actionNamed:
785                         /*Skip, for now */
786                 default:
787                         g_warning ("Unknown link action type: %d", link_action->getKind ());
788                 }
789
790                 *title = g_strdup (unicode_to_char (anItem, pdf_document->umap));
791         } else if (link_title) {
792                 *title = g_strdup (unicode_to_char (anItem, pdf_document->umap));
793         }
794
795         *type = EV_DOCUMENT_BOOKMARKS_TYPE_LINK;
796         *page = page_num;
797
798         return TRUE;
799 }
800
801 static EvDocumentBookmarksIter *
802 pdf_document_bookmarks_get_child (EvDocumentBookmarks     *document_bookmarks,
803                                   EvDocumentBookmarksIter *bookmarks_iter)
804 {
805         BookmarksIter *iter = (BookmarksIter *)bookmarks_iter;
806         BookmarksIter *child_iter;
807         OutlineItem *anItem;
808
809         g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
810
811         anItem = (OutlineItem *)iter->items->get(iter->index);
812         anItem->open ();
813         if (! (anItem->hasKids() && anItem->getKids()) )
814                 return NULL;
815
816         child_iter = g_new0 (BookmarksIter, 1);
817         child_iter->index = 0;
818         child_iter->level = iter->level + 1;
819         child_iter->items = anItem->getKids ();
820         g_assert (child_iter->items);
821
822         return (EvDocumentBookmarksIter *) child_iter;
823 }
824
825 static gboolean
826 pdf_document_bookmarks_next (EvDocumentBookmarks     *document_bookmarks,
827                              EvDocumentBookmarksIter *bookmarks_iter)
828 {
829         BookmarksIter *iter = (BookmarksIter *) bookmarks_iter;
830
831         g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
832
833         iter->index++;
834         if (iter->index >= iter->items->getLength())
835                 return FALSE;
836
837         return TRUE;
838 }
839
840 static void
841 pdf_document_bookmarks_free_iter (EvDocumentBookmarks     *document_bookmarks,
842                                   EvDocumentBookmarksIter *iter)
843 {
844         g_return_if_fail (PDF_IS_DOCUMENT (document_bookmarks));
845         g_return_if_fail (iter != NULL);
846
847         /* FIXME: Should I close all the nodes?? Free them? */
848         g_free (iter);
849 }
850
851 static void
852 pdf_document_finalize (GObject *object)
853 {
854         PdfDocument *pdf_document = PDF_DOCUMENT (object);
855
856         if (pdf_document->umap) {
857                 pdf_document->umap->decRefCnt ();
858                 pdf_document->umap = NULL;
859         }
860
861         if (pdf_document->search)
862                 pdf_document_search_free (pdf_document->search);
863
864         if (pdf_document->target)
865                 g_object_unref (pdf_document->target);
866
867         if (pdf_document->out)
868                 delete pdf_document->out;
869         if (pdf_document->ps_out)
870                 delete pdf_document->ps_out;
871         if (pdf_document->doc)
872                 delete pdf_document->doc;
873
874 }
875
876 static void
877 pdf_document_set_property (GObject *object,
878                            guint prop_id,
879                            const GValue *value,
880                            GParamSpec *pspec)
881 {
882         switch (prop_id)
883
884         {
885                 case PROP_TITLE:
886                         /* read only */
887                         break;
888         }
889 }
890
891 static gboolean
892 has_unicode_marker (GString *string)
893 {
894         return ((string->getChar (0) & 0xff) == 0xfe &&
895                 (string->getChar (1) & 0xff) == 0xff);
896 }
897
898 static gchar *
899 pdf_info_dict_get_string (Dict *info_dict, const gchar *key) {
900         Object obj;
901         GString *value;
902         gchar *result;
903
904         g_return_val_if_fail (info_dict != NULL, NULL);
905         g_return_val_if_fail (key != NULL, NULL);
906
907         if (!info_dict->lookup ((gchar *)key, &obj)->isString ()) {
908                 obj.free ();
909                 return g_strdup (_("Unknown"));
910         }
911
912         value = obj.getString ();
913
914         if (has_unicode_marker (value)) {
915                 result = g_convert (value->getCString () + 2,
916                                     value->getLength () - 2,
917                                     "UTF-8", "UTF-16BE", NULL, NULL, NULL);
918         } else {
919                 result = g_strndup (value->getCString (), value->getLength ());
920         }
921
922         obj.free ();
923
924         return result;
925 }
926
927 static char *
928 pdf_document_get_title (PdfDocument *pdf_document)
929 {
930         char *title = NULL;
931         Object info;
932
933         pdf_document->doc->getDocInfo (&info);
934
935         if (info.isDict ()) {
936                 title = pdf_info_dict_get_string (info.getDict(), "Title");
937         }
938
939         return title;
940 }
941
942 static void
943 pdf_document_get_property (GObject *object,
944                            guint prop_id,
945                            GValue *value,
946                            GParamSpec *pspec)
947 {
948         PdfDocument *pdf_document = PDF_DOCUMENT (object);
949         char *title;
950
951         switch (prop_id)
952         {
953                 case PROP_TITLE:
954                         title = pdf_document_get_title (pdf_document);
955                         g_value_set_string (value, title);
956                         g_free (title);
957                         break;
958         }
959 }
960
961 static void
962 pdf_document_class_init (PdfDocumentClass *klass)
963 {
964         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
965
966         gobject_class->finalize = pdf_document_finalize;
967         gobject_class->get_property = pdf_document_get_property;
968         gobject_class->set_property = pdf_document_set_property;
969
970         g_object_class_override_property (gobject_class, PROP_TITLE, "title");
971 }
972
973 static void
974 pdf_document_document_iface_init (EvDocumentIface *iface)
975 {
976         iface->load = pdf_document_load;
977         iface->get_n_pages = pdf_document_get_n_pages;
978         iface->set_page = pdf_document_set_page;
979         iface->get_page = pdf_document_get_page;
980         iface->set_scale = pdf_document_set_scale;
981         iface->set_target = pdf_document_set_target;
982         iface->set_page_offset = pdf_document_set_page_offset;
983         iface->get_page_size = pdf_document_get_page_size;
984         iface->render = pdf_document_render;
985 }
986
987 static void
988 pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
989 {
990         iface->begin = pdf_document_ps_export_begin;
991         iface->do_page = pdf_document_ps_export_do_page;
992         iface->end = pdf_document_ps_export_end;
993 }
994
995
996 static void
997 pdf_document_find_iface_init (EvDocumentFindIface *iface)
998 {
999         iface->begin = pdf_document_find_begin;
1000         iface->cancel = pdf_document_find_cancel;
1001 }
1002
1003 static void
1004 pdf_document_document_bookmarks_iface_init (EvDocumentBookmarksIface *iface)
1005 {
1006         iface->has_document_bookmarks = pdf_document_bookmarks_has_document_bookmarks;
1007         iface->begin_read = pdf_document_bookmarks_begin_read;
1008         iface->get_values = pdf_document_bookmarks_get_values;
1009         iface->get_child = pdf_document_bookmarks_get_child;
1010         iface->next = pdf_document_bookmarks_next;
1011         iface->free_iter = pdf_document_bookmarks_free_iter;
1012 }
1013
1014 /* Thumbnails */
1015 static GdkPixbuf *
1016 pdf_document_thumbnails_get_page_pixbuf (PdfDocument *pdf_document,
1017                                          gdouble      scale_factor,
1018                                          gint         page_num,
1019                                          gint         width,
1020                                          gint         height)
1021 {
1022         GdkPixmap *pixmap;
1023         GDKSplashOutputDev *output;
1024         GdkPixbuf *pixbuf;
1025         GdkPixbuf *shadow;
1026         gint rowstride;
1027         guchar *data;
1028
1029         pixmap = gdk_pixmap_new (pdf_document->target,
1030                                  width, height, -1);
1031
1032         output = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
1033                                          NULL, NULL);
1034         output->startDoc (pdf_document->doc->getXRef());
1035         pdf_document->doc->displayPage (output,
1036                                         page_num + 1,
1037                                         72*scale_factor,
1038                                         72*scale_factor,
1039                                         0, gTrue, gFalse);
1040         output->redraw (0, 0,
1041                         pixmap,
1042                         0, 0,
1043                         width, height);
1044         pixbuf = gdk_pixbuf_get_from_drawable (NULL,
1045                                                pixmap,
1046                                                NULL,
1047                                                0, 0,
1048                                                0, 0,
1049                                                width, height);
1050         gdk_drawable_unref (pixmap);
1051         delete output;
1052
1053         shadow = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1054                                  TRUE, 8,
1055                                  width + 4,
1056                                  height + 4);
1057         gdk_pixbuf_fill (shadow, 0x000000ff);
1058         gdk_pixbuf_copy_area (pixbuf, 0, 0,
1059                               width,
1060                               height,
1061                               shadow,
1062                               1, 1);
1063         g_object_unref (pixbuf);
1064
1065         /* Add the corner */
1066         data = gdk_pixbuf_get_pixels (shadow);
1067         rowstride = gdk_pixbuf_get_rowstride (shadow);
1068         data [(width + 2) * 4 + 3] = 0;
1069         data [(width + 3) * 4 + 3] = 0;
1070         data [(width + 2) * 4 + (rowstride * 1) + 3] = 0;
1071         data [(width + 3) * 4 + (rowstride * 1) + 3] = 0;
1072
1073         data [(height + 2) * rowstride + 3] = 0;
1074         data [(height + 3) * rowstride + 3] = 0;
1075         data [(height + 2) * rowstride + 4 + 3] = 0;
1076         data [(height + 3) * rowstride + 4 + 3] = 0;
1077         
1078         return shadow;
1079 }
1080
1081 static GdkPixbuf *
1082 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1083                                        gint                  page,
1084                                        gint                  width)
1085 {
1086         PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1087         GdkPixbuf *thumbnail;
1088         Page *the_page;
1089         Object the_thumb;
1090         Thumb *thumb = NULL;
1091         gboolean have_ethumbs = FALSE;
1092
1093         /* getPage seems to want page + 1 for some reason; */
1094         the_page = pdf_document->doc->getCatalog ()->getPage (page + 1);
1095         the_page->getThumb(&the_thumb);
1096
1097         if (!(the_thumb.isNull () || the_thumb.isNone())) {
1098                 /* Build the thumbnail object */
1099                 thumb = new Thumb(pdf_document->doc->getXRef (),
1100                                   &the_thumb);
1101
1102                 have_ethumbs = thumb->ok();
1103         }
1104
1105         if (have_ethumbs) {
1106                 guchar *data;
1107                 GdkPixbuf *tmp_pixbuf;
1108
1109                 data = thumb->getPixbufData();
1110                 /* FISME: scale the image if it's not an appropriate size */
1111                 tmp_pixbuf = gdk_pixbuf_new_from_data (data,
1112                                                        GDK_COLORSPACE_RGB,
1113                                                        FALSE,
1114                                                        8,
1115                                                        thumb->getWidth (),
1116                                                        thumb->getHeight (),
1117                                                        thumb->getWidth () * 3,
1118                                                        NULL, NULL);
1119
1120                 thumbnail = tmp_pixbuf;
1121         } else {
1122                 gdouble page_ratio;
1123                 gdouble scale_factor;
1124                 gint dest_height;
1125
1126                 page_ratio = the_page->getHeight () / the_page->getWidth ();
1127                 scale_factor = (gdouble)width / the_page->getWidth ();
1128                 dest_height = (gint) (width * page_ratio);
1129
1130                 thumbnail = pdf_document_thumbnails_get_page_pixbuf (pdf_document,
1131                                                                      scale_factor,
1132                                                                      page,
1133                                                                      width,
1134                                                                      dest_height);
1135
1136                 /* FIXME: Actually get the image... */
1137         }
1138
1139         return thumbnail;
1140 }
1141 static void
1142 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1143 {
1144         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1145 }
1146
1147
1148 static void
1149 pdf_document_init (PdfDocument *pdf_document)
1150 {
1151         pdf_document->page = 1;
1152         pdf_document->page_x_offset = 0;
1153         pdf_document->page_y_offset = 0;
1154         pdf_document->scale = 1.;
1155
1156         pdf_document->page_valid = FALSE;
1157 }
1158