Debian Codegraph @

texk/web2c/luatexdir/pdf/pdfoutline.c

raw
1 /*
2
3 Copyright 2009-2011 Taco Hoekwater <taco@@luatex.org>
4
5 This file is part of LuaTeX.
6
7 LuaTeX is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
19
20 */
21
22 #include "ptexlib.h"
23
24 /*tex
25
26 Data structure of outlines; it's not able to write out outline entries before
27 all outline entries are defined, so memory allocated for outline entries
28 can't not be deallocated and will stay in memory. For this reason we will
29 store data of outline entries in |pdf->mem| instead of |mem|.
30
31 */
32
33 /*tex The size of memory in |pdf->mem| which |obj_outline_ptr| points to: */
34
35 #define pdfmem_outline_size 8
36
37 /*tex The count of all opened children: */
38
39 #define obj_outline_count obj_info
40
41 /*tex The pointer to |pdf->mem|: */
42
43 #define obj_outline_ptr obj_aux
44
45 #define obj_outline_title(pdf,A) pdf->mem[obj_outline_ptr(pdf,A)]
46 #define obj_outline_parent(pdf,A) pdf->mem[obj_outline_ptr(pdf,A) + 1]
47 #define obj_outline_prev(pdf,A) pdf->mem[obj_outline_ptr(pdf,A) + 2]
48 #define obj_outline_next(pdf,A) pdf->mem[obj_outline_ptr(pdf,A) + 3]
49 #define obj_outline_first(pdf,A) pdf->mem[obj_outline_ptr(pdf,A) + 4]
50 #define obj_outline_last(pdf,A) pdf->mem[obj_outline_ptr(pdf,A) + 5]
51 #define obj_outline_action_objnum(pdf,A) pdf->mem[obj_outline_ptr(pdf,A) + 6]
52 #define obj_outline_attr(pdf,A) pdf->mem[obj_outline_ptr(pdf,A) + 7]
53
54 #define set_obj_outline_count(pdf,A,B) obj_outline_count(pdf,A)=B
55 #define set_obj_outline_ptr(pdf,A,B) obj_outline_ptr(pdf,A)=B
56 #define set_obj_outline_action_objnum(pdf,A,B) obj_outline_action_objnum(pdf,A)=B
57 #define set_obj_outline_title(pdf,A,B) obj_outline_title(pdf,A)=B
58 #define set_obj_outline_prev(pdf,A,B) obj_outline_prev(pdf,A)=B
59 #define set_obj_outline_next(pdf,A,B) obj_outline_next(pdf,A)=B
60 #define set_obj_outline_first(pdf,A,B) obj_outline_first(pdf,A)=B
61 #define set_obj_outline_last(pdf,A,B) obj_outline_last(pdf,A)=B
62 #define set_obj_outline_parent(pdf,A,B) obj_outline_parent(pdf,A)=B
63 #define set_obj_outline_attr(pdf,A,B) obj_outline_attr(pdf,A)=B
64
65 static int open_subentries(PDF pdf, halfword p)
66 {
67 int c, l, r;
68 int k = 0;
69 if (obj_outline_first(pdf, p) != 0) {
70 l = obj_outline_first(pdf, p);
71 do {
72 k++;
73 c = open_subentries(pdf, l);
74 if (obj_outline_count(pdf, l) > 0)
75 k = k + c;
76 obj_outline_parent(pdf, l) = p;
77 r = obj_outline_next(pdf, l);
78 if (r == 0)
79 obj_outline_last(pdf, p) = l;
80 l = r;
81 } while (l != 0);
82 }
83 if (obj_outline_count(pdf, p) > 0)
84 obj_outline_count(pdf, p) = k;
85 else
86 obj_outline_count(pdf, p) = -k;
87 return k;
88 }
89
90 /*tex Return the number of outline entries in the same level with |p|: */
91
92 static int outline_list_count(PDF pdf, pointer p)
93 {
94 int k = 1;
95 while (obj_outline_prev(pdf, p) != 0) {
96 k++;
97 p = obj_outline_prev(pdf, p);
98 }
99 return k;
100 }
101
102 void scan_pdfoutline(PDF pdf)
103 {
104 halfword q, r;
105 int i, k, l;
106 int j = 0;
107 halfword p = null;
108 if (scan_keyword("attr")) {
109 scan_toks(false, true);
110 r = def_ref;
111 } else {
112 r = 0;
113 }
114 if (scan_keyword("useobjnum")) {
115 scan_int();
116 j = cur_val;
117 } else {
118 p = scan_action(pdf);
119 }
120 if (scan_keyword("count")) {
121 scan_int();
122 i = cur_val;
123 } else {
124 i = 0;
125 }
126 scan_toks(false, true);
127 q = def_ref;
128 if (j == 0) {
129 j = pdf_create_obj(pdf, obj_type_others, 0);
130 pdf_begin_obj(pdf, j, OBJSTM_ALWAYS);
131 write_action(pdf, p);
132 pdf_end_obj(pdf);
133 delete_action_ref(p);
134 }
135 k = pdf_create_obj(pdf, obj_type_outline, 0);
136 set_obj_outline_ptr(pdf, k, pdf_get_mem(pdf, pdfmem_outline_size));
137 set_obj_outline_action_objnum(pdf, k, j);
138 set_obj_outline_count(pdf, k, i);
139 l = pdf_create_obj(pdf, obj_type_others, 0);
140 pdf_begin_obj(pdf, l, OBJSTM_ALWAYS);
141 {
142 char *s = tokenlist_to_cstring(q, true, NULL);
143 pdf_print_str_ln(pdf, s);
144 xfree(s);
145 }
146 delete_token_ref(q);
147 pdf_end_obj(pdf);
148 set_obj_outline_title(pdf, k, l);
149 set_obj_outline_prev(pdf, k, 0);
150 set_obj_outline_next(pdf, k, 0);
151 set_obj_outline_first(pdf, k, 0);
152 set_obj_outline_last(pdf, k, 0);
153 set_obj_outline_parent(pdf, k, pdf->parent_outline);
154 set_obj_outline_attr(pdf, k, r);
155 if (pdf->first_outline == 0)
156 pdf->first_outline = k;
157 if (pdf->last_outline == 0) {
158 if (pdf->parent_outline != 0)
159 set_obj_outline_first(pdf, pdf->parent_outline, k);
160 } else {
161 set_obj_outline_next(pdf, pdf->last_outline, k);
162 set_obj_outline_prev(pdf, k, pdf->last_outline);
163 }
164 pdf->last_outline = k;
165 if (obj_outline_count(pdf, k) != 0) {
166 pdf->parent_outline = k;
167 pdf->last_outline = 0;
168 } else if ((pdf->parent_outline != 0) &&
169 (outline_list_count(pdf, k) ==
170 abs(obj_outline_count(pdf, pdf->parent_outline)))) {
171 j = pdf->last_outline;
172 do {
173 set_obj_outline_last(pdf, pdf->parent_outline, j);
174 j = pdf->parent_outline;
175 pdf->parent_outline = obj_outline_parent(pdf, pdf->parent_outline);
176 } while (!((pdf->parent_outline == 0) ||
177 (outline_list_count(pdf, j) <
178 abs(obj_outline_count(pdf, pdf->parent_outline)))));
179 if (pdf->parent_outline == 0)
180 pdf->last_outline = pdf->first_outline;
181 else
182 pdf->last_outline = obj_outline_first(pdf, pdf->parent_outline);
183 while (obj_outline_next(pdf, pdf->last_outline) != 0)
184 pdf->last_outline = obj_outline_next(pdf, pdf->last_outline);
185 }
186 }
187
188 /*tex
189
190 In the end we must flush \PDF\F objects that cannot be written out
191 immediately after shipping out pages.
192
193 */
194
195 int print_outlines(PDF pdf)
196 {
197 int k, l, a;
198 int outlines;
199 if (pdf->first_outline != 0) {
200 outlines = pdf_create_obj(pdf, obj_type_others, 0);
201 l = pdf->first_outline;
202 k = 0;
203 do {
204 k++;
205 a = open_subentries(pdf, l);
206 if (obj_outline_count(pdf, l) > 0)
207 k = k + a;
208 set_obj_outline_parent(pdf, l, pdf->obj_ptr);
209 l = obj_outline_next(pdf, l);
210 } while (l != 0);
211 pdf_begin_obj(pdf, outlines, OBJSTM_ALWAYS);
212 pdf_begin_dict(pdf);
213 pdf_dict_add_name(pdf, "Type", "Outlines");
214 pdf_dict_add_ref(pdf, "First", pdf->first_outline);
215 pdf_dict_add_ref(pdf, "Last", pdf->last_outline);
216 pdf_dict_add_int(pdf, "Count", k);
217 pdf_end_dict(pdf);
218 pdf_end_obj(pdf);
219 /*tex Output \PDF\ outline entries. */
220 k = pdf->head_tab[obj_type_outline];
221 while (k != 0) {
222 if (obj_outline_parent(pdf, k) == pdf->parent_outline) {
223 if (obj_outline_prev(pdf, k) == 0)
224 pdf->first_outline = k;
225 if (obj_outline_next(pdf, k) == 0)
226 pdf->last_outline = k;
227 }
228 pdf_begin_obj(pdf, k, OBJSTM_ALWAYS);
229 pdf_begin_dict(pdf);
230 pdf_dict_add_ref(pdf, "Title", obj_outline_title(pdf, k));
231 pdf_dict_add_ref(pdf, "A", obj_outline_action_objnum(pdf, k));
232 if (obj_outline_parent(pdf, k) != 0)
233 pdf_dict_add_ref(pdf, "Parent", obj_outline_parent(pdf, k));
234 if (obj_outline_prev(pdf, k) != 0)
235 pdf_dict_add_ref(pdf, "Prev", obj_outline_prev(pdf, k));
236 if (obj_outline_next(pdf, k) != 0)
237 pdf_dict_add_ref(pdf, "Next", obj_outline_next(pdf, k));
238 if (obj_outline_first(pdf, k) != 0)
239 pdf_dict_add_ref(pdf, "First", obj_outline_first(pdf, k));
240 if (obj_outline_last(pdf, k) != 0)
241 pdf_dict_add_ref(pdf, "Last", obj_outline_last(pdf, k));
242 if (obj_outline_count(pdf, k) != 0)
243 pdf_dict_add_int(pdf, "Count", obj_outline_count(pdf, k));
244 if (obj_outline_attr(pdf, k) != 0) {
245 pdf_out(pdf, '\n');
246 pdf_print_toks(pdf, obj_outline_attr(pdf, k));
247 pdf_out(pdf, '\n');
248 delete_token_ref(obj_outline_attr(pdf, k));
249 set_obj_outline_attr(pdf, k, null);
250 }
251 pdf_end_dict(pdf);
252 pdf_end_obj(pdf);
253 k = obj_link(pdf, k);
254 }
255 } else {
256 outlines = 0;
257 }
258 return outlines;
259 }