Debian Codegraph @

texk/web2c/luatexdir/pdf/pdfpagetree.c

raw
1 /*
2
3 Copyright 2006-2011 Taco Hoekwater <[email protected]>
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 #define PAGES_TREE_KIDSMAX 10
25
26 static struct avl_table *divert_list_tree = NULL;
27
28 typedef struct pages_entry_ {
29 /*tex The object number of this |/Pages| object. */
30 int objnum;
31 /*tex The total number of all pages below. */
32 int number_of_pages;
33 /*tex The number of direct kid objects. */
34 int number_of_kids;
35 /*tex The array of kid object numbers. */
36 int kids[PAGES_TREE_KIDSMAX];
37 struct pages_entry_ *next;
38 } pages_entry;
39
40 typedef struct divert_list_entry_ {
41 int divnum;
42 pages_entry *first;
43 pages_entry *last;
44 } divert_list_entry;
45
46 static int comp_divert_list_entry(const void *pa, const void *pb, void *p)
47 {
48 (void) p;
49 if (((const divert_list_entry *) pa)->divnum > ((const divert_list_entry *) pb)->divnum)
50 return 1;
51 if (((const divert_list_entry *) pa)->divnum < ((const divert_list_entry *) pb)->divnum)
52 return -1;
53 return 0;
54 }
55
56 static pages_entry *new_pages_entry(PDF pdf)
57 {
58 int i;
59 pages_entry *p = xtalloc(1, pages_entry);
60 p->number_of_pages = p->number_of_kids = 0;
61 for (i = 0; i < PAGES_TREE_KIDSMAX; i++)
62 p->kids[i] = 0;
63 p->next = NULL;
64 p->objnum = pdf_create_obj(pdf, obj_type_pages, 0);
65 return p;
66 }
67
68 static divert_list_entry *new_divert_list_entry(void)
69 {
70 divert_list_entry *d;
71 d = xtalloc(1, divert_list_entry);
72 d->first = d->last = NULL;
73 return d;
74 }
75
76 static void ensure_list_tree(void)
77 {
78 if (divert_list_tree == NULL) {
79 divert_list_tree = avl_create(comp_divert_list_entry, NULL, &avl_xallocator);
80 }
81 }
82
83 static divert_list_entry *get_divert_list(int divnum)
84 {
85 divert_list_entry *d, tmp;
86 void **aa;
87 tmp.divnum = divnum;
88 d = (divert_list_entry *) avl_find(divert_list_tree, &tmp);
89 if (d == NULL) {
90 d = new_divert_list_entry();
91 d->divnum = divnum;
92 /*tex The next bit of code can actually be removed. */
93 aa = avl_probe(divert_list_tree, d);
94 if (aa==NULL) {
95 normal_error("pdf backend","page list lookup error");
96 }
97 }
98 return d;
99 }
100
101 /*tex |pdf_do_page_divert| returns the current |/Parent| object number. */
102
103 int pdf_do_page_divert(PDF pdf, int objnum, int divnum)
104 {
105 divert_list_entry *d;
106 pages_entry *p;
107 /*tex Initialize the tree. */
108 ensure_list_tree();
109 /*tex Make sure we have a list for this diversion. */
110 d = get_divert_list(divnum);
111 if (d->first == NULL || d->last->number_of_kids == PAGES_TREE_KIDSMAX) {
112 /*tex Append a new |pages_entry|. */
113 p = new_pages_entry(pdf);
114 if (d->first == NULL)
115 d->first = p;
116 else
117 d->last->next = p;
118 d->last = p;
119 }
120 p = d->last;
121 p->kids[p->number_of_kids++] = objnum;
122 p->number_of_pages++;
123 return p->objnum;
124 }
125
126 static void movelist(divert_list_entry * d, divert_list_entry * dto)
127 {
128 if (d != NULL && d->first != NULL && d->divnum != dto->divnum) {
129 /*tex No undivert of empty list or into self. */
130 if (dto->first == NULL)
131 dto->first = d->first;
132 else
133 dto->last->next = d->first;
134 dto->last = d->last;
135 /*tex One could as well remove this |divert_list_entry|. */
136 d->first = d->last = NULL;
137 }
138 }
139
140 /*tex Undivert from diversion |divnum| into diversion |curdivnum|. */
141
142 void pdf_do_page_undivert(int divnum, int curdivnum)
143 {
144 divert_list_entry *d, *dto, tmp;
145 struct avl_traverser t;
146 /*tex Initialize the tree. */
147 ensure_list_tree();
148 /*tex Find the diversion |curdivnum| list where diversion |divnum| should go. */
149 dto = get_divert_list(curdivnum);
150 if (divnum == 0) {
151 /*tex Zero is a special case: undivert {\em all} lists. */
152 avl_t_init(&t, divert_list_tree);
153 for (d = avl_t_first(&t, divert_list_tree); d != NULL;
154 d = avl_t_next(&t))
155 movelist(d, dto);
156 } else {
157 tmp.divnum = divnum;
158 d = (divert_list_entry *) avl_find(divert_list_tree, &tmp);
159 movelist(d, dto);
160 }
161 }
162
163 /*tex Write a |/Pages| object. */
164
165 static void write_pages(PDF pdf, pages_entry * p, int parent)
166 {
167 int i;
168 int pages_attributes ;
169 pdf_begin_obj(pdf, p->objnum, OBJSTM_ALWAYS);
170 pdf_begin_dict(pdf);
171 pdf_dict_add_name(pdf, "Type", "Pages");
172 if (parent == 0) {
173 /*tex It's root. Lookup the attributes once. */
174 pages_attributes = pdf_pages_attr;
175 if (pages_attributes != null) {
176 pdf_print_toks(pdf, pages_attributes);
177 pdf_out(pdf, ' ');
178 }
179 print_pdf_table_string(pdf, "pagesattributes");
180 pdf_out(pdf, ' ');
181 } else {
182 pdf_dict_add_ref(pdf, "Parent", parent);
183 }
184 pdf_dict_add_int(pdf, "Count", (int) p->number_of_pages);
185 pdf_add_name(pdf, "Kids");
186 pdf_begin_array(pdf);
187 for (i = 0; i < p->number_of_kids; i++) {
188 pdf_add_ref(pdf, (int) p->kids[i]);
189 }
190 pdf_end_array(pdf);
191 pdf_end_dict(pdf);
192 pdf_end_obj(pdf);
193 }
194
195 /*tex
196
197 Loop over all |/Pages| objects, output them, create their parents, recursing
198 bottom up, return the |/Pages| root object number.
199
200 */
201
202 static int output_pages_list(PDF pdf, pages_entry * pe)
203 {
204 pages_entry *p, *q, *r;
205 if (pe->next == NULL) {
206 /*tex Everything fits into one |pages_entry|. */
207 write_pages(pdf, pe, 0);
208 return pe->objnum;
209 }
210 /*tex One level higher needed. */
211 q = r = new_pages_entry(pdf);
212 for (p = pe; p != NULL; p = p->next) {
213 if (q->number_of_kids == PAGES_TREE_KIDSMAX) {
214 q->next = new_pages_entry(pdf);
215 q = q->next;
216 }
217 q->kids[q->number_of_kids++] = p->objnum;
218 q->number_of_pages += p->number_of_pages;
219 write_pages(pdf, p, q->objnum);
220 }
221 /*tex Recurse through next higher level. */
222 return output_pages_list(pdf, r);
223 }
224
225 int output_pages_tree(PDF pdf)
226 {
227 divert_list_entry *d;
228 /*tex Concatenate all diversions into diversion 0. */
229 pdf_do_page_undivert(0, 0);
230 /*tex Get diversion 0. */
231 d = get_divert_list(0);
232 return output_pages_list(pdf, d->first);
233 }