Wed, 12 Mar 2003 08:38:04 +0000
updated copyright notice.
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.5 2003/03/05 12:56:25 eric Exp $
8 * Copyright 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 struct pdf_bookmark
42 {
43 struct pdf_obj *dict; /* indirect reference */
44 struct pdf_obj *count;
45 bool open;
47 struct pdf_bookmark *first;
48 struct pdf_bookmark *last;
50 /* the following fields don't appear in the root */
51 /* title and dest are in the dictionary but don't have
52 explicit fields in the C structure */
53 struct pdf_bookmark *parent;
54 struct pdf_bookmark *prev;
55 struct pdf_bookmark *next;
56 };
59 static void pdf_bookmark_update_count (pdf_bookmark_handle entry)
60 {
61 while (entry)
62 {
63 if (! entry->count)
64 {
65 entry->count = pdf_new_integer (0);
66 pdf_set_dict_entry (entry->dict, "Count", entry->count);
67 }
68 pdf_set_integer (entry->count,
69 pdf_get_integer (entry->count) +
70 ((entry->open) ? 1 : -1));
71 if (! entry->open)
72 break;
73 entry = entry->parent;
74 }
75 }
78 /* Create a new bookmark, under the specified parent, or at the top
79 level if parent is NULL. */
80 pdf_bookmark_handle pdf_new_bookmark (pdf_bookmark_handle parent,
81 char *title,
82 bool open,
83 pdf_page_handle pdf_page)
84 {
85 pdf_file_handle pdf_file = pdf_page->pdf_file;
86 struct pdf_bookmark *root;
87 struct pdf_bookmark *entry;
89 struct pdf_obj *dest_array;
91 root = pdf_file->outline_root;
92 if (! root)
93 {
94 root = pdf_calloc (1, sizeof (struct pdf_bookmark));
95 root->dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
97 pdf_file->outline_root = root;
98 pdf_set_dict_entry (pdf_file->catalog, "Outlines", root->dict);
99 }
101 entry = pdf_calloc (1, sizeof (struct pdf_bookmark));
102 entry->dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
103 entry->open = open;
105 pdf_set_dict_entry (entry->dict, "Title", pdf_new_string (title));
107 dest_array = pdf_new_obj (PT_ARRAY);
108 pdf_add_array_elem (dest_array, pdf_page->page_dict);
109 pdf_add_array_elem (dest_array, pdf_new_name ("Fit"));
110 pdf_set_dict_entry (entry->dict, "Dest", dest_array);
112 if (parent)
113 {
114 entry->parent = parent;
115 entry->prev = parent->last;
116 }
117 else
118 {
119 parent = root;
120 entry->parent = root;
121 entry->prev = root->last;
122 }
124 pdf_set_dict_entry (entry->dict, "Parent", parent->dict);
126 if (entry->prev)
127 {
128 pdf_set_dict_entry (entry->dict, "Prev", entry->prev->dict);
130 entry->prev->next = entry;
131 pdf_set_dict_entry (entry->prev->dict, "Next", entry->dict);
133 parent->last = entry;
134 pdf_set_dict_entry (parent->dict, "Last", entry->dict);
135 }
136 else
137 {
138 parent->first = entry;
139 pdf_set_dict_entry (parent->dict, "First", entry->dict);
141 parent->last = entry;
142 pdf_set_dict_entry (parent->dict, "Last", entry->dict);
143 }
145 pdf_bookmark_update_count (parent);
147 return (entry);
148 }