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