Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pdf/xpdf/pdf-document.cc
blob: 24e0b36298e3fd258bff34cced8224d732ffeade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
/* pdfdocument.h: Implementation of EvDocument for PDF
 * Copyright (C) 2004, Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "gpdf-g-switch.h"
#include "pdf-document.h"
#include "ev-ps-exporter.h"
#include "ev-document-find.h"
#include "gpdf-g-switch.h"

#include "GlobalParams.h"
#include "GDKSplashOutputDev.h"
#include "PDFDoc.h"
#include "Outline.h"
#include "UnicodeMap.h"
#include "GlobalParams.h"
#include "goo/GList.h"
#include "PSOutputDev.h"

typedef struct
{
	PdfDocument *document;
	gunichar *ucs4;
	glong ucs4_len;
	guint idle;
        /* full results are only possible for the rendered current page */
        int current_page;
        GArray *current_page_results;
        guchar *other_page_flags; /* length n_pages + 1, first element ignored */
        int start_page;   /* skip this one as we iterate, since we did it first */
        int search_page;  /* the page we're searching now */
        TextOutputDev *output_dev;
} PdfDocumentSearch;

typedef struct _PdfDocumentClass PdfDocumentClass;

#define PDF_DOCUMENT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), PDF_TYPE_DOCUMENT, PdfDocumentClass))
#define PDF_IS_DOCUMENT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), PDF_TYPE_DOCUMENT))
#define PDF_DOCUMENT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), PDF_TYPE_DOCUMENT, PdfDocumentClass))

struct _PdfDocumentClass
{
	GObjectClass parent_class;
};

struct _PdfDocument
{
	GObject parent_instance;

	int page;
	int page_x_offset;
	int page_y_offset;
	double scale;
	GdkDrawable *target;

	GDKSplashOutputDev *out;
	PSOutputDev *ps_out;
	PDFDoc *doc;
	UnicodeMap *umap;

	gboolean page_valid;

	PdfDocumentSearch *search;
};

static void pdf_document_document_bookmarks_iface_init (EvDocumentBookmarksIface *iface);
static void pdf_document_document_iface_init           (EvDocumentIface          *iface);
static void pdf_document_ps_exporter_iface_init (EvPSExporterIface   *iface);
static void pdf_document_find_iface_init        (EvDocumentFindIface *iface);
static void pdf_document_search_free            (PdfDocumentSearch   *search);
static void pdf_document_search_page_changed    (PdfDocumentSearch   *search);

G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
                         {
				 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
							pdf_document_document_iface_init);
				 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_BOOKMARKS,
							pdf_document_document_bookmarks_iface_init);
				 G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
							pdf_document_ps_exporter_iface_init);
				 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
							pdf_document_find_iface_init);
			 });

static gboolean
document_validate_page (PdfDocument *pdf_document)
{
	if (!pdf_document->page_valid) {
		pdf_document->doc->displayPage (pdf_document->out, pdf_document->page,
						72 * pdf_document->scale,
						72 * pdf_document->scale,
						0, gTrue, gTrue);

		pdf_document->page_valid = TRUE;

                /* Update the search results available to the app since
                 * we only provide full results on the current page
                 */
                if (pdf_document->search)
                        pdf_document_search_page_changed (pdf_document->search);
	}
	
	return pdf_document->page_valid;
}

static gboolean
pdf_document_load (EvDocument  *document,
		   const char  *uri,
		   GError     **error)
{
	PdfDocument *pdf_document = PDF_DOCUMENT (document);
	PDFDoc *newDoc;
	int err;
	char *filename;
	GString *filename_g;
	GString *enc; 

	if (!globalParams) {
		globalParams = new GlobalParams("/etc/xpdfrc");
		globalParams->setupBaseFontsFc(NULL);
	}

	if (! pdf_document->umap) {
		enc = new GString("UTF-8");
		pdf_document->umap = globalParams->getUnicodeMap(enc);
		pdf_document->umap->incRefCnt (); 
		delete enc;
	}

	filename = g_filename_from_uri (uri, NULL, error);
	if (!filename)
		return FALSE;

	filename_g = new GString (filename);
	g_free (filename);

	// open the PDF file, assumes ownership of filename_g
	newDoc = new PDFDoc(filename_g, 0, 0);

	if (!newDoc->isOk()) {
		err = newDoc->getErrorCode();
		delete newDoc;

		/* FIXME: Add a real error enum to EvDocument */
		g_set_error (error, G_FILE_ERROR,
			     G_FILE_ERROR_FAILED,
			     "Failed to load document (error %d) '%s'\n",
			     err,
			     uri);

		return FALSE;
	}

	if (pdf_document->doc)
		delete pdf_document->doc;
	pdf_document->doc = newDoc;

	pdf_document->page = 1;

	if (pdf_document->out)
		pdf_document->out->startDoc(pdf_document->doc->getXRef());

	pdf_document->page_valid = FALSE;

	return TRUE;
}

static int
pdf_document_get_n_pages (EvDocument  *document)
{
	PdfDocument *pdf_document = PDF_DOCUMENT (document);

	if (pdf_document->doc)
		return pdf_document->doc->getNumPages();
	else
		return 1;
}

static void
pdf_document_set_page (EvDocument  *document,
		       int          page)
{
	PdfDocument *pdf_document = PDF_DOCUMENT (document);

	page = CLAMP (page, 1, ev_document_get_n_pages (document));

	if (page != pdf_document->page) {
		pdf_document->page = page;
		pdf_document->page_valid = FALSE;
	}

	ev_document_changed (document);
}

static int
pdf_document_get_page (EvDocument  *document)
{
	PdfDocument *pdf_document = PDF_DOCUMENT (document);

	return pdf_document->page;
}

static void
redraw_callback (void *data)
{
	/* Need to hook up through a EvDocument callback? */
}

static void
pdf_document_set_target (EvDocument  *document,
			 GdkDrawable *target)
{
	PdfDocument *pdf_document = PDF_DOCUMENT (document);

	if (pdf_document->target != target) {
		if (pdf_document->target)
			g_object_unref (pdf_document->target);

		pdf_document->target = target;

		if (pdf_document->target)
			g_object_ref (pdf_document->target);

		if (pdf_document->out) {
			delete pdf_document->out;
			pdf_document->out = NULL;
		}

		if (pdf_document->target) {
			pdf_document->out = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
							 redraw_callback, (void*) document);

			if (pdf_document->doc)
				pdf_document->out->startDoc(pdf_document->doc->getXRef());

		}

		pdf_document->page_valid = FALSE;
	}
}

static void
pdf_document_set_scale (EvDocument  *document,
			double       scale)
{
	PdfDocument *pdf_document = PDF_DOCUMENT (document);

	if (pdf_document->scale != scale) {
		pdf_document->scale = scale;
		pdf_document->page_valid = FALSE;
	}
}

static void
pdf_document_set_page_offset (EvDocument  *document,
			      int          x,
			      int          y)
{
	PdfDocument *pdf_document = PDF_DOCUMENT (document);

	pdf_document->page_x_offset = x;
	pdf_document->page_y_offset = y;
}

static void
pdf_document_get_page_size (EvDocument   *document,
			    int          *width,
			    int          *height)
{
	PdfDocument *pdf_document = PDF_DOCUMENT (document);

	if (document_validate_page (pdf_document)) {
		if (width)
			*width = pdf_document->out->getBitmapWidth();
		if (height)
			*height = pdf_document->out->getBitmapHeight();
	} else {
		if (width)
			*width = 1;
		if (height)
			*height = 1;
	}
}

static void
pdf_document_render (EvDocument  *document,
		     int          clip_x,
		     int          clip_y,
		     int          clip_width,
		     int          clip_height)
{
	PdfDocument *pdf_document = PDF_DOCUMENT (document);
	GdkRectangle page;
	GdkRectangle draw;

	if (!document_validate_page (pdf_document) || !pdf_document->target)
		return;

	page.x = pdf_document->page_x_offset;
	page.y = pdf_document->page_y_offset;
	page.width = pdf_document->out->getBitmapWidth();
	page.height = pdf_document->out->getBitmapHeight();

	draw.x = clip_x;
	draw.y = clip_y;
	draw.width = clip_width;
	draw.height = clip_height;

	if (gdk_rectangle_intersect (&page, &draw, &draw))
		pdf_document->out->redraw (draw.x - page.x, draw.y - page.y,
					   pdf_document->target,
					   draw.x, draw.y,
					   draw.width, draw.height);
}

static void
pdf_document_search_emit_found (PdfDocumentSearch *search)
{
        PdfDocument *pdf_document = search->document;
        int n_pages;
        int pages_done;
        GArray *tmp_results;
        int i;

        n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
        if (search->search_page > search->start_page) {
                pages_done = search->search_page - search->start_page;
        } else if (search->search_page == search->start_page) {
                pages_done = n_pages;
        } else {
                pages_done = n_pages - search->start_page + search->search_page;
        }

        tmp_results = g_array_new (FALSE, FALSE, sizeof (EvFindResult));
        g_array_append_vals (tmp_results,
                             search->current_page_results->data,
                             search->current_page_results->len);

        /* Now append a bogus element for each page that has a result in it,
         * that is not the current page
         */
        i = 1;
        while (i <= n_pages) {
                if (i != pdf_document->page &&
                    search->other_page_flags[i]) {
                        EvFindResult result;
                        
                        result.page_num = i;
                        
                        /* Use bogus coordinates, again we can't get coordinates
                         * until this is the current page because TextOutputDev
                         * isn't good enough
                         */
                        result.highlight_area.x = -1;
                        result.highlight_area.y = -1;
                        result.highlight_area.width = 1;
                        result.highlight_area.height = 1;
                        
                        g_array_append_val (tmp_results, result);
                }

                ++i;
        }
        
        ev_document_find_found (EV_DOCUMENT_FIND (pdf_document),
                                (EvFindResult*) tmp_results->data,
                                tmp_results->len,
                                pages_done / (double) n_pages);
        
        g_array_free (tmp_results, TRUE);
}

static void
pdf_document_search_page_changed (PdfDocumentSearch   *search)
{
        PdfDocument *pdf_document = search->document;
        int current_page;
        EvFindResult result;
        int xMin, yMin, xMax, yMax;

        current_page = pdf_document->page;

        if (!pdf_document->page_valid) {
                /* we can't do anything until displayPage() */
                search->current_page = -1;
                return;
        }
        
        if (search->current_page == current_page)
                return;
        
        /* We need to create current_page_results for the new current page */
        g_array_set_size (search->current_page_results, 0);
        
        if (pdf_document->out->findText (search->ucs4, search->ucs4_len,
                                         gTrue, gTrue, // startAtTop, stopAtBottom
                                         gFalse, gFalse, // startAtLast, stopAtLast
                                         &xMin, &yMin, &xMax, &yMax)) {
                result.page_num = pdf_document->page;

                result.highlight_area.x = xMin;
                result.highlight_area.y = yMin;
                result.highlight_area.width = xMax - xMin;
                result.highlight_area.height = yMax - yMin;

                g_array_append_val (search->current_page_results, result);
                /* Now find further results */

                while (pdf_document->out->findText (search->ucs4, search->ucs4_len,
                                                    gFalse, gTrue,
                                                    gTrue, gFalse,
                                                    &xMin, &yMin, &xMax, &yMax)) {

                        result.page_num = pdf_document->page;

                        result.highlight_area.x = xMin;
                        result.highlight_area.y = yMin;
                        result.highlight_area.width = xMax - xMin;
                        result.highlight_area.height = yMax - yMin;
                        
                        g_array_append_val (search->current_page_results, result);
                }
        }

        /* needed for the initial current page since we don't search
         * it in the idle
         */
        search->other_page_flags[current_page] =
                search->current_page_results->len > 0;
        
        pdf_document_search_emit_found (search);
}

static gboolean
pdf_document_search_idle_callback (void *data)
{
        PdfDocumentSearch *search = (PdfDocumentSearch*) data;
        PdfDocument *pdf_document = search->document;
        int n_pages;
        double xMin, yMin, xMax, yMax;

        /* Note that PDF page count is 1 through n_pages INCLUSIVE
         * like a real book. We are looking to add one result for each
         * page with a match, because the coordinates are meaningless
         * with TextOutputDev, so we just want to flag matching pages
         * and then when the user switches to the current page, we
         * will emit "found" again with the real results.
         */
        n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));

        if (search->search_page == search->start_page) {
                goto end_search;
        }

        if (search->output_dev == 0) {
                /* First time through here... */
                search->output_dev = new TextOutputDev (NULL, gTrue, gFalse, gFalse);
                if (!search->output_dev->isOk()) {
                        goto end_search;
                }
        }
                                                  
        pdf_document->doc->displayPage (search->output_dev,
                                        search->search_page,
                                        72, 72, 0, gTrue, gFalse);

        if (search->output_dev->findText (search->ucs4,
                                          search->ucs4_len,
                                          gTrue, gTrue, // startAtTop, stopAtBottom
                                          gFalse, gFalse, // startAtLast, stopAtLast
                                          &xMin, &yMin, &xMax, &yMax)) {
                /* This page has results */
                search->other_page_flags[search->search_page] = TRUE;
        }
        
        search->search_page += 1;
        if (search->search_page > n_pages) {
                /* wrap around */
                search->search_page = 1;
        }

        /* We do this even if nothing was found, to update the percent complete */
        pdf_document_search_emit_found (search);
        
        return TRUE;

 end_search:
        /* We're done. */
        search->idle = 0; /* will return FALSE to remove */
        return FALSE;
}

static void
pdf_document_find_begin (EvDocumentFind   *document,
                         const char       *search_string,
                         gboolean          case_sensitive)
{
        PdfDocument *pdf_document = PDF_DOCUMENT (document);
        PdfDocumentSearch *search;
        int n_pages;
        gunichar *ucs4;
        glong ucs4_len;

        /* FIXME handle case_sensitive (right now XPDF
         * code is always case insensitive for ASCII
         * and case sensitive for all other languaages)
         */
        
        g_assert (sizeof (gunichar) == sizeof (Unicode));
        ucs4 = g_utf8_to_ucs4_fast (search_string, -1,
                                    &ucs4_len);

        if (pdf_document->search &&
            pdf_document->search->ucs4_len == ucs4_len &&
            memcmp (pdf_document->search->ucs4,
                    ucs4,
                    sizeof (gunichar) * ucs4_len) == 0) {
                /* Search is unchanged */
                g_free (ucs4);
                return;
        }

        if (pdf_document->search) {
                pdf_document_search_free (pdf_document->search);
                pdf_document->search = NULL;
        }
        
        search = g_new0 (PdfDocumentSearch, 1);

        search->ucs4 = ucs4;
        search->ucs4_len = ucs4_len;
        
        search->current_page_results = g_array_new (FALSE,
                                                    FALSE,
                                                    sizeof (EvFindResult));
        n_pages = ev_document_get_n_pages (EV_DOCUMENT (document)); 

        /* This is an array of bool; with the first value ignored
         * so we can index by the based-at-1 page numbers
         */
        search->other_page_flags = g_new0 (guchar, n_pages + 1);
        
        search->document = pdf_document;

        /* We add at low priority so the progress bar repaints */
        search->idle = g_idle_add_full (G_PRIORITY_LOW,
                                        pdf_document_search_idle_callback,
                                        search,
                                        NULL);

        search->output_dev = 0;

        search->start_page = pdf_document->page;
        search->search_page = search->start_page + 1;
        if (search->search_page > n_pages)
                search->search_page = 1;

        search->current_page = -1;

        pdf_document->search = search;
        
        /* Update for the current page right away */
        pdf_document_search_page_changed (search);
}

static void
pdf_document_find_cancel (EvDocumentFind   *document)
{
        PdfDocument *pdf_document = PDF_DOCUMENT (document);

	if (pdf_document->search) {
		pdf_document_search_free (pdf_document->search);
		pdf_document->search = NULL;
	}
}

static void
pdf_document_search_free (PdfDocumentSearch   *search)
{
        if (search->idle != 0)
                g_source_remove (search->idle);

	if (search->output_dev)
		delete search->output_dev;
	
        g_array_free (search->current_page_results, TRUE);
        g_free (search->other_page_flags);
        
        g_free (search->ucs4);
	g_free (search);
}

static void
pdf_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
{
	PdfDocument *document = PDF_DOCUMENT (exporter);

	if (document->ps_out)
		delete document->ps_out;

	document->ps_out = new PSOutputDev ((char *)filename, document->doc->getXRef(),
					    document->doc->getCatalog(), 1,
					    ev_document_get_n_pages (EV_DOCUMENT (document)),
					    psModePS);	
}

static void
pdf_document_ps_export_do_page (EvPSExporter *exporter, int page)
{
	PdfDocument *document = PDF_DOCUMENT (exporter);

	document->doc->displayPage (document->ps_out, page,
				    72.0, 72.0, 0, gTrue, gFalse);
}

static void
pdf_document_ps_export_end (EvPSExporter *exporter)
{
	PdfDocument *document = PDF_DOCUMENT (exporter);

	delete document->ps_out;
	document->ps_out = NULL;
}


/* EvDocumentBookmarks Implementation */
typedef struct
{
	/* goo GList, not glib */
	GList *items;
	int index;
	int level;
} BookmarksIter;

static gchar *
unicode_to_char (OutlineItem *outline_item, 
		 UnicodeMap *uMap)
{
	GString gstr;
	gchar buf[8]; /* 8 is enough for mapping an unicode char to a string */
	int i, n;
	
	for (i = 0; i < outline_item->getTitleLength(); ++i) {
		n = uMap->mapUnicode(outline_item->getTitle()[i], buf, sizeof(buf));
		gstr.append(buf, n);
	}

	return g_strdup (gstr.getCString ());
}


static gboolean
pdf_document_bookmarks_has_document_bookmarks (EvDocumentBookmarks *document_bookmarks)
{
	PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
	Outline *outline;

	g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);

	outline = pdf_document->doc->getOutline();
	if (outline->getItems() != NULL &&
	    outline->getItems()->getLength() > 0)
		return TRUE;

	return FALSE;
}

static EvDocumentBookmarksIter *
pdf_document_bookmarks_begin_read (EvDocumentBookmarks *document_bookmarks)
{
	PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
	Outline *outline;
	BookmarksIter *iter;
	GList *items;

	g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), NULL);

	outline = pdf_document->doc->getOutline();
	items = outline->getItems();
	if (! items)
		return NULL;

	iter = g_new0 (BookmarksIter, 1);
	iter->items = items;
	iter->index = 0;
	iter->level = 0;

	return (EvDocumentBookmarksIter *) iter;
}

static gboolean
pdf_document_bookmarks_get_values (EvDocumentBookmarks      *document_bookmarks,
				   EvDocumentBookmarksIter  *bookmarks_iter,
				   char                    **title,
				   EvDocumentBookmarksType  *type,
				   gint                     *page)
{
	PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
	BookmarksIter *iter = (BookmarksIter *)bookmarks_iter;
	OutlineItem *anItem;
	LinkAction *link_action;
	LinkDest *link_dest = NULL;
	LinkURI *link_uri = NULL;
	LinkGoTo *link_goto = NULL;
	GString *named_dest; 
	Unicode *link_title;
	Ref page_ref;
	gint page_num = -1;

	g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
	g_return_val_if_fail (iter != NULL, FALSE);
	g_return_val_if_fail (title != NULL, FALSE);
	g_return_val_if_fail (type != NULL, FALSE);
	g_return_val_if_fail (page != NULL, FALSE);

	anItem = (OutlineItem *)iter->items->get(iter->index);
	link_action = anItem->getAction ();
	link_title = anItem->getTitle (); 

	if (link_action) {
		switch (link_action->getKind ()) {

		case actionGoTo:
			link_goto = dynamic_cast <LinkGoTo *> (link_action);
			link_dest = link_goto->getDest ();
			named_dest = link_goto->getNamedDest ();

			/* Wow!  This seems excessively slow on large
			 * documents. I need to investigate more... -jrb */
			if (link_dest != NULL) {
				link_dest = link_dest->copy ();
			} else if (named_dest != NULL) {
				named_dest = named_dest->copy ();
				link_dest = pdf_document->doc->findDest (named_dest);
				delete named_dest;
			}
			if (link_dest != NULL) {
				if (link_dest->isPageRef ()) {
					page_ref = link_dest->getPageRef ();
					page_num = pdf_document->doc->findPage (page_ref.num, page_ref.gen);
				} else {
					page_num = link_dest->getPageNum ();
				}
				
				delete link_dest;
			}
				
			break;
		case actionURI:
			link_uri = dynamic_cast <LinkURI *> (link_action);
			break;
			
		case actionNamed:
			/*Skip, for now */
		default:
			g_warning ("Unknown link action type: %d", link_action->getKind ());
		}

		*title = g_strdup (unicode_to_char (anItem, pdf_document->umap));
	} else if (link_title) {
		*title = g_strdup (unicode_to_char (anItem, pdf_document->umap));
	}

	*type = EV_DOCUMENT_BOOKMARKS_TYPE_LINK;
	*page = page_num;

	return TRUE;
}

static EvDocumentBookmarksIter *
pdf_document_bookmarks_get_child (EvDocumentBookmarks     *document_bookmarks,
				  EvDocumentBookmarksIter *bookmarks_iter)
{
	BookmarksIter *iter = (BookmarksIter *)bookmarks_iter;
	BookmarksIter *child_iter;
	OutlineItem *anItem;

	g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);

	anItem = (OutlineItem *)iter->items->get(iter->index);
	anItem->open ();
	if (! (anItem->hasKids() && anItem->getKids()) )
		return NULL;

	child_iter = g_new0 (BookmarksIter, 1);
	child_iter->index = 0;
	child_iter->level = iter->level + 1;
	child_iter->items = anItem->getKids ();
	g_assert (child_iter->items);

	return (EvDocumentBookmarksIter *) child_iter;
}

static gboolean
pdf_document_bookmarks_next (EvDocumentBookmarks     *document_bookmarks,
			     EvDocumentBookmarksIter *bookmarks_iter)
{
	BookmarksIter *iter = (BookmarksIter *) bookmarks_iter;

	g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);

	iter->index++;
	if (iter->index >= iter->items->getLength())
		return FALSE;

	return TRUE;
}

static void
pdf_document_bookmarks_free_iter (EvDocumentBookmarks     *document_bookmarks,
				  EvDocumentBookmarksIter *iter)
{
	g_return_if_fail (PDF_IS_DOCUMENT (document_bookmarks));
	g_return_if_fail (iter != NULL);

	/* FIXME: Should I close all the nodes?? Free them? */
	g_free (iter);
}

static void
pdf_document_finalize (GObject *object)
{
	PdfDocument *pdf_document = PDF_DOCUMENT (object);

	if (pdf_document->umap) {
		pdf_document->umap->decRefCnt ();
		pdf_document->umap = NULL;
	}

	if (pdf_document->search)
		pdf_document_search_free (pdf_document->search);
	
	if (pdf_document->target)
		g_object_unref (pdf_document->target);

	if (pdf_document->out)
		delete pdf_document->out;
	if (pdf_document->ps_out)
		delete pdf_document->ps_out;
	if (pdf_document->doc)
		delete pdf_document->doc;

}

static void
pdf_document_class_init (PdfDocumentClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

	gobject_class->finalize = pdf_document_finalize;
}

static void
pdf_document_document_iface_init (EvDocumentIface *iface)
{
	iface->load = pdf_document_load;
	iface->get_n_pages = pdf_document_get_n_pages;
	iface->set_page = pdf_document_set_page;
	iface->get_page = pdf_document_get_page;
	iface->set_scale = pdf_document_set_scale;
	iface->set_target = pdf_document_set_target;
	iface->set_page_offset = pdf_document_set_page_offset;
	iface->get_page_size = pdf_document_get_page_size;
	iface->render = pdf_document_render;
}

static void
pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
{
	iface->begin = pdf_document_ps_export_begin;
	iface->do_page = pdf_document_ps_export_do_page;
	iface->end = pdf_document_ps_export_end;
}


static void
pdf_document_find_iface_init (EvDocumentFindIface *iface)
{
        iface->begin = pdf_document_find_begin;
        iface->cancel = pdf_document_find_cancel;
}

static void
pdf_document_document_bookmarks_iface_init (EvDocumentBookmarksIface *iface)
{
	iface->has_document_bookmarks = pdf_document_bookmarks_has_document_bookmarks;
	iface->begin_read = pdf_document_bookmarks_begin_read;
	iface->get_values = pdf_document_bookmarks_get_values;
	iface->get_child = pdf_document_bookmarks_get_child;
	iface->next = pdf_document_bookmarks_next;
	iface->free_iter = pdf_document_bookmarks_free_iter;
}


static void
pdf_document_init (PdfDocument *pdf_document)
{
	pdf_document->page = 1;
	pdf_document->page_x_offset = 0;
	pdf_document->page_y_offset = 0;
	pdf_document->scale = 1.;

	pdf_document->page_valid = FALSE;
}