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