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