Mon, 14 Dec 2009 16:18:21 +0000
remove erroneous 0.33-philpem1 tag
1 /*
2 * tumble: build a PDF file from image files
3 *
4 * PDF routines
5 * $Id: pdf_bookmark.c,v 1.6 2003/03/13 00:57:05 eric Exp $
6 * Copyright 2003 Eric Smith <eric@brouhaha.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation. Note that permission is
11 * not granted to redistribute this program under the terms of any
12 * other version of the General Public License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
22 */
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
32 #include "bitblt.h"
33 #include "pdf.h"
34 #include "pdf_util.h"
35 #include "pdf_prim.h"
36 #include "pdf_private.h"
39 struct pdf_bookmark
40 {
41 struct pdf_obj *dict; /* indirect reference */
42 struct pdf_obj *count;
43 bool open;
45 struct pdf_bookmark *first;
46 struct pdf_bookmark *last;
48 /* the following fields don't appear in the root */
49 /* title and dest are in the dictionary but don't have
50 explicit fields in the C structure */
51 struct pdf_bookmark *parent;
52 struct pdf_bookmark *prev;
53 struct pdf_bookmark *next;
54 };
57 static void pdf_bookmark_update_count (pdf_bookmark_handle entry)
58 {
59 while (entry)
60 {
61 if (! entry->count)
62 {
63 entry->count = pdf_new_integer (0);
64 pdf_set_dict_entry (entry->dict, "Count", entry->count);
65 }
66 pdf_set_integer (entry->count,
67 pdf_get_integer (entry->count) +
68 ((entry->open) ? 1 : -1));
69 if (! entry->open)
70 break;
71 entry = entry->parent;
72 }
73 }
76 /* Create a new bookmark, under the specified parent, or at the top
77 level if parent is NULL. */
78 pdf_bookmark_handle pdf_new_bookmark (pdf_bookmark_handle parent,
79 char *title,
80 bool open,
81 pdf_page_handle pdf_page)
82 {
83 pdf_file_handle pdf_file = pdf_page->pdf_file;
84 struct pdf_bookmark *root;
85 struct pdf_bookmark *entry;
87 struct pdf_obj *dest_array;
89 root = pdf_file->outline_root;
90 if (! root)
91 {
92 root = pdf_calloc (1, sizeof (struct pdf_bookmark));
93 root->dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
95 pdf_file->outline_root = root;
96 pdf_set_dict_entry (pdf_file->catalog, "Outlines", root->dict);
97 }
99 entry = pdf_calloc (1, sizeof (struct pdf_bookmark));
100 entry->dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
101 entry->open = open;
103 pdf_set_dict_entry (entry->dict, "Title", pdf_new_string (title));
105 dest_array = pdf_new_obj (PT_ARRAY);
106 pdf_add_array_elem (dest_array, pdf_page->page_dict);
107 pdf_add_array_elem (dest_array, pdf_new_name ("Fit"));
108 pdf_set_dict_entry (entry->dict, "Dest", dest_array);
110 if (parent)
111 {
112 entry->parent = parent;
113 entry->prev = parent->last;
114 }
115 else
116 {
117 parent = root;
118 entry->parent = root;
119 entry->prev = root->last;
120 }
122 pdf_set_dict_entry (entry->dict, "Parent", parent->dict);
124 if (entry->prev)
125 {
126 pdf_set_dict_entry (entry->dict, "Prev", entry->prev->dict);
128 entry->prev->next = entry;
129 pdf_set_dict_entry (entry->prev->dict, "Next", entry->dict);
131 parent->last = entry;
132 pdf_set_dict_entry (parent->dict, "Last", entry->dict);
133 }
134 else
135 {
136 parent->first = entry;
137 pdf_set_dict_entry (parent->dict, "First", entry->dict);
139 parent->last = entry;
140 pdf_set_dict_entry (parent->dict, "Last", entry->dict);
141 }
143 pdf_bookmark_update_count (parent);
145 return (entry);
146 }