Wed, 05 Mar 2003 02:26:43 +0000
fixed Count for closed bookmarks, doesn't contribute to parent's Count.
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.2 2003/03/04 18:26:43 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 if (! entry->open)
57 break;
58 entry = entry->parent;
59 }
60 }
63 /* Create a new bookmark, under the specified parent, or at the top
64 level if parent is NULL. */
65 pdf_bookmark_handle pdf_new_bookmark (pdf_bookmark_handle parent,
66 char *title,
67 bool open,
68 pdf_page_handle pdf_page)
69 {
70 pdf_file_handle pdf_file = pdf_page->pdf_file;
71 struct pdf_bookmark *root;
72 struct pdf_bookmark *entry;
74 struct pdf_obj *dest_array;
76 root = pdf_file->outline_root;
77 if (! root)
78 {
79 root = pdf_calloc (1, sizeof (struct pdf_bookmark));
80 root->dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
81 pdf_set_dict_entry (root->dict, "Count", pdf_new_integer (0));
83 pdf_file->outline_root = root;
84 pdf_set_dict_entry (pdf_file->catalog, "Outlines", root->dict);
85 }
87 entry = pdf_calloc (1, sizeof (struct pdf_bookmark));
88 entry->dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
89 entry->open = open;
91 pdf_set_dict_entry (entry->dict, "Title", pdf_new_string (title));
93 dest_array = pdf_new_obj (PT_ARRAY);
94 pdf_add_array_elem (dest_array, pdf_page->page_dict);
95 pdf_add_array_elem (dest_array, pdf_new_name ("Fit"));
96 pdf_set_dict_entry (entry->dict, "Dest", dest_array);
98 if (parent)
99 {
100 entry->parent = parent;
101 entry->prev = parent->last;
102 }
103 else
104 {
105 parent = root;
106 entry->parent = root;
107 entry->prev = root->last;
108 }
110 pdf_set_dict_entry (entry->dict, "Parent", parent->dict);
112 if (entry->prev)
113 {
114 pdf_set_dict_entry (entry->dict, "Prev", entry->prev->dict);
116 entry->prev->next = entry;
117 pdf_set_dict_entry (entry->prev->dict, "Next", entry->dict);
119 parent->last = entry;
120 pdf_set_dict_entry (parent->dict, "Last", entry->dict);
121 }
122 else
123 {
124 parent->first = entry;
125 pdf_set_dict_entry (parent->dict, "First", entry->dict);
127 parent->last = entry;
128 pdf_set_dict_entry (parent->dict, "Last", entry->dict);
129 }
131 pdf_bookmark_update_count (parent);
133 return (entry);
134 }