Wed, 05 Mar 2003 01:58:36 +0000
added bookmark support.
1 /*
2 * t2p: Create a PDF file from the contents of one or more TIFF
3 * bilevel image files. The images in the resulting PDF file
4 * will be compressed using ITU-T T.6 (G4) fax encoding.
5 *
6 * PDF routines
7 * $Id: pdf_bookmark.c,v 1.1 2003/03/04 17:58:36 eric Exp $
8 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation. Note that permission is
13 * not granted to redistribute this program under the terms of any
14 * other version of the General Public License.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
24 */
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
34 #include "bitblt.h"
35 #include "pdf.h"
36 #include "pdf_util.h"
37 #include "pdf_prim.h"
38 #include "pdf_private.h"
41 static void pdf_bookmark_update_count (pdf_bookmark_handle entry)
42 {
43 struct pdf_obj *count_obj;
45 while (entry)
46 {
47 count_obj = pdf_get_dict_entry (entry->dict, "Count");
48 if (! count_obj)
49 {
50 count_obj = pdf_new_integer (0);
51 pdf_set_dict_entry (entry->dict, "Count", count_obj);
52 }
53 pdf_set_integer (count_obj,
54 pdf_get_integer (count_obj) +
55 ((entry->open) ? 1 : -1));
56 entry = entry->parent;
57 }
58 }
61 /* Create a new bookmark, under the specified parent, or at the top
62 level if parent is NULL. */
63 pdf_bookmark_handle pdf_new_bookmark (pdf_bookmark_handle parent,
64 char *title,
65 bool open,
66 pdf_page_handle pdf_page)
67 {
68 pdf_file_handle pdf_file = pdf_page->pdf_file;
69 struct pdf_bookmark *root;
70 struct pdf_bookmark *entry;
72 struct pdf_obj *dest_array;
74 root = pdf_file->outline_root;
75 if (! root)
76 {
77 root = pdf_calloc (1, sizeof (struct pdf_bookmark));
78 root->dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
79 pdf_set_dict_entry (root->dict, "Count", pdf_new_integer (0));
81 pdf_file->outline_root = root;
82 pdf_set_dict_entry (pdf_file->catalog, "Outlines", root->dict);
83 }
85 entry = pdf_calloc (1, sizeof (struct pdf_bookmark));
86 entry->dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
87 entry->open = open;
89 pdf_set_dict_entry (entry->dict, "Title", pdf_new_string (title));
91 dest_array = pdf_new_obj (PT_ARRAY);
92 pdf_add_array_elem (dest_array, pdf_page->page_dict);
93 pdf_add_array_elem (dest_array, pdf_new_name ("Fit"));
94 pdf_set_dict_entry (entry->dict, "Dest", dest_array);
96 if (parent)
97 {
98 entry->parent = parent;
99 entry->prev = parent->last;
100 }
101 else
102 {
103 parent = root;
104 entry->parent = root;
105 entry->prev = root->last;
106 }
108 pdf_set_dict_entry (entry->dict, "Parent", parent->dict);
110 if (entry->prev)
111 {
112 pdf_set_dict_entry (entry->dict, "Prev", entry->prev->dict);
114 entry->prev->next = entry;
115 pdf_set_dict_entry (entry->prev->dict, "Next", entry->dict);
117 parent->last = entry;
118 pdf_set_dict_entry (parent->dict, "Last", entry->dict);
119 }
120 else
121 {
122 parent->first = entry;
123 pdf_set_dict_entry (parent->dict, "First", entry->dict);
125 parent->last = entry;
126 pdf_set_dict_entry (parent->dict, "Last", entry->dict);
127 }
129 pdf_bookmark_update_count (parent);
131 return (entry);
132 }