Debian Codegraph @

texk/dvipdfm-x/pdfnames.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
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-1307 USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <ctype.h>
28 #include <math.h>
29 #include <string.h>
30
31 #include "system.h"
32 #include "mem.h"
33 #include "error.h"
34 #include "numbers.h"
35
36 /* Hash */
37 #include "dpxutil.h"
38
39 #include "pdfobj.h"
40
41 #include "pdfnames.h"
42
43 #include "dvipdfmx.h"
44
45 struct obj_data
46 {
47 pdf_obj *reference;
48 pdf_obj *object;
49 int closed; /* 1 if object is closed */
50 };
51
52 static char *
53 printable_key (const char *key, int keylen)
54 {
55 #define MAX_KEY 32
56 static char pkey[MAX_KEY+4];
57 int i, len;
58 unsigned char hi, lo;
59
60 for (i = 0, len = 0;
61 i < keylen && len < MAX_KEY; i++) {
62 if (isprint((unsigned char)key[i])) {
63 pkey[len++] = key[i];
64 } else {
65 hi = (key[i] >> 4) & 0xff;
66 lo = key[i] & 0xff;
67 pkey[len++] = '#';
68 pkey[len++] = (hi < 10) ? hi + '0' : (hi - 10) + 'A';
69 pkey[len++] = (lo < 10) ? lo + '0' : (lo - 10) + 'A';
70 }
71 }
72 pkey[len] = '\0';
73
74 return (char *) pkey;
75 }
76
77 static inline void
78 hval_free (void *hval)
79 {
80 struct obj_data *value;
81
82 value = (struct obj_data *) hval;
83
84 if (value->reference) {
85 pdf_release_obj(value->reference);
86 value->reference = NULL;
87 }
88 if (value->object) {
89 pdf_release_obj(value->object);
90 value->object = NULL;
91 }
92
93 RELEASE(value);
94
95 return;
96 }
97
98 struct ht_table *
99 pdf_new_name_tree (void)
100 {
101 struct ht_table *names;
102
103 names = NEW(1, struct ht_table);
104 ht_init_table(names, hval_free);
105
106 return names;
107 }
108
109 static void
110 check_objects_defined (struct ht_table *ht_tab)
111 {
112 struct ht_iter iter;
113
114 if (ht_set_iter(ht_tab, &iter) >= 0) {
115 do {
116 char *key;
117 int keylen;
118 struct obj_data *value;
119
120 key = ht_iter_getkey(&iter, &keylen);
121 value = ht_iter_getval(&iter);
122 if (PDF_OBJ_UNDEFINED(value->object)) {
123 pdf_names_add_object(ht_tab, key, keylen, pdf_new_null());
124 WARN("Object @%s used, but not defined. Replaced by null.",
125 printable_key(key, keylen));
126 }
127 } while (ht_iter_next(&iter) >= 0);
128 ht_clear_iter(&iter);
129 }
130 }
131
132 void
133 pdf_delete_name_tree (struct ht_table **names)
134 {
135 ASSERT(names && *names);
136
137 check_objects_defined (*names);
138
139 ht_clear_table(*names);
140 RELEASE(*names);
141 *names = NULL;
142 }
143
144 int
145 pdf_names_add_object (struct ht_table *names, const void *key, int keylen, pdf_obj *object)
146 {
147 struct obj_data *value;
148
149 ASSERT(names && object);
150
151 if (!key || keylen < 1) {
152 WARN("Null string used for name tree key.");
153 return -1;
154 }
155
156 value = ht_lookup_table(names, key, keylen);
157 if (!value) {
158 value = NEW(1, struct obj_data);
159 value->object = object;
160 value->reference = NULL;
161 value->closed = 0;
162 ht_append_table(names, key, keylen, value);
163 } else {
164 if (value->object && PDF_OBJ_UNDEFINED(value->object)) {
165 pdf_transfer_label(object, value->object);
166 pdf_release_obj(value->object);
167 value->object = object;
168 } else {
169 WARN("Object @%s already defined.", printable_key(key, keylen));
170 pdf_release_obj(object);
171 return -1;
172 }
173 }
174
175 return 0;
176 }
177
178 pdf_obj *
179 pdf_names_reserve (struct ht_table *names, const void *key, int keylen)
180 {
181 pdf_obj *obj_ref = NULL;
182 struct obj_data *value;
183
184 ASSERT(names);
185
186 if (!key || keylen < 1) {
187 WARN("Null string used for name tree key.");
188 return NULL;
189 }
190
191 value = ht_lookup_table(names, key, keylen);
192 if (!value) {
193 value = NEW(1, struct obj_data);
194 value->object = pdf_new_undefined();
195 value->reference = NULL;
196 value->closed = 0;
197 ht_append_table(names, key, keylen, value);
198 obj_ref = pdf_ref_obj(value->object);
199 } else {
200 if (value->object && PDF_OBJ_UNDEFINED(value->object)) {
201 if (!value->reference)
202 value->reference = pdf_ref_obj(value->object);
203 obj_ref = pdf_link_obj(value->reference);
204 } else {
205 WARN("Object @%s already defined.", printable_key(key, keylen));
206 obj_ref = NULL;
207 }
208 }
209
210 return obj_ref;
211 }
212
213 /*
214 * The following routine returns copies, not the original object.
215 */
216 pdf_obj *
217 pdf_names_lookup_reference (struct ht_table *names, const void *key, int keylen)
218 {
219 struct obj_data *value;
220 pdf_obj *obj_ref;
221
222 ASSERT(names);
223
224 value = ht_lookup_table(names, key, keylen);
225
226 if (value) {
227 if (!value->reference) {
228 if (value->object) {
229 value->reference = pdf_ref_obj(value->object);
230 } else {
231 WARN("Can't create object ref for already released object: %s", printable_key(key, keylen));
232 }
233 }
234 obj_ref = pdf_link_obj(value->reference);
235 } else {
236 /* A null object as dummy would create problems because as value
237 * of a dictionary entry, a null object is be equivalent to no entry
238 * at all. This matters for optimization of PDF destinations.
239 */
240 obj_ref = pdf_names_reserve(names, key, keylen);
241 }
242
243 return obj_ref;
244 }
245
246 pdf_obj *
247 pdf_names_lookup_object (struct ht_table *names, const void *key, int keylen)
248 {
249 struct obj_data *value;
250
251 ASSERT(names);
252
253 value = ht_lookup_table(names, key, keylen);
254 if (!value || PDF_OBJ_UNDEFINED(value->object))
255 return NULL;
256 if (value->closed)
257 WARN("Object \"%s\" already closed.", printable_key(key, keylen));
258
259 return value->object;
260 }
261
262 int
263 pdf_names_close_object (struct ht_table *names, const void *key, int keylen)
264 {
265 struct obj_data *value;
266
267 ASSERT(names);
268
269 value = ht_lookup_table(names, key, keylen);
270 if (!value ||PDF_OBJ_UNDEFINED(value->object) ) {
271 WARN("Cannot close undefined object @%s.", printable_key(key, keylen));
272 return -1;
273 }
274 ASSERT(value->object);
275
276 if (value->closed) {
277 WARN("Object @%s already closed.", printable_key(key, keylen));
278 return -1;
279 }
280
281 if (value->reference) {
282 pdf_release_obj(value->object);
283 value->object = NULL;
284 }
285 value->closed = 1;
286
287 return 0;
288 }
289
290 struct named_object
291 {
292 char *key;
293 int keylen;
294 pdf_obj *value;
295 };
296
297 static inline int
298 cmp_key (const void *d1, const void *d2)
299 {
300 const struct named_object *sd1, *sd2;
301 int keylen, cmp;
302
303 sd1 = (const struct named_object *) d1;
304 sd2 = (const struct named_object *) d2;
305
306 if (!sd1->key)
307 cmp = -1;
308 else if (!sd2->key)
309 cmp = 1;
310 else {
311 keylen = MIN(sd1->keylen, sd2->keylen);
312 cmp = memcmp(sd1->key, sd2->key, keylen);
313 if (!cmp) {
314 cmp = sd1->keylen - sd2->keylen;
315 }
316 }
317
318 return cmp;
319 }
320
321 #define NAME_CLUSTER 4
322 static pdf_obj *
323 build_name_tree (struct named_object *first, int num_leaves, int is_root)
324 {
325 pdf_obj *result;
326 int i;
327
328 result = pdf_new_dict();
329 /*
330 * According to PDF Refrence, Third Edition (p.101-102), a name tree
331 * always has exactly one root node, which contains a SINGLE entry:
332 * either Kids or Names but not both. If the root node has a Names
333 * entry, it is the only node in the tree. If it has a Kids entry,
334 * then each of the remaining nodes is either an intermediate node,
335 * containing a Limits entry and a Kids entry, or a leaf node,
336 * containing a Limits entry and a Names entry.
337 */
338 if (!is_root) {
339 struct named_object *last;
340 pdf_obj *limits;
341
342 limits = pdf_new_array();
343 last = &first[num_leaves - 1];
344 pdf_add_array(limits, pdf_new_string(first->key, first->keylen));
345 pdf_add_array(limits, pdf_new_string(last->key , last->keylen ));
346 pdf_add_dict (result, pdf_new_name("Limits"), limits);
347 }
348
349 if (num_leaves > 0 &&
350 num_leaves <= 2 * NAME_CLUSTER) {
351 pdf_obj *names;
352
353 /* Create leaf nodes. */
354 names = pdf_new_array();
355 for (i = 0; i < num_leaves; i++) {
356 struct named_object *cur;
357
358 cur = &first[i];
359 pdf_add_array(names, pdf_new_string(cur->key, cur->keylen));
360 switch (PDF_OBJ_TYPEOF(cur->value)) {
361 case PDF_ARRAY:
362 case PDF_DICT:
363 case PDF_STREAM:
364 case PDF_STRING:
365 pdf_add_array(names, pdf_ref_obj(cur->value));
366 break;
367 case PDF_OBJ_INVALID:
368 ERROR("Invalid object...: %s", printable_key(cur->key, cur->keylen));
369 default:
370 pdf_add_array(names, pdf_link_obj(cur->value));
371 break;
372 }
373 pdf_release_obj(cur->value);
374 cur->value = NULL;
375 }
376 pdf_add_dict(result, pdf_new_name("Names"), names);
377 } else if (num_leaves > 0) {
378 pdf_obj *kids;
379
380 /* Intermediate node */
381 kids = pdf_new_array();
382 for (i = 0; i < NAME_CLUSTER; i++) {
383 pdf_obj *subtree;
384 int start, end;
385
386 start = (i*num_leaves) / NAME_CLUSTER;
387 end = ((i+1)*num_leaves) / NAME_CLUSTER;
388 subtree = build_name_tree(&first[start], (end - start), 0);
389 pdf_add_array (kids, pdf_ref_obj(subtree));
390 pdf_release_obj(subtree);
391 }
392 pdf_add_dict(result, pdf_new_name("Kids"), kids);
393 }
394
395 return result;
396 }
397
398 static struct named_object *
399 flat_table (struct ht_table *ht_tab, int *num_entries,
400 struct ht_table *filter)
401 {
402 struct named_object *objects;
403 struct ht_iter iter;
404 int count;
405
406 ASSERT(ht_tab);
407
408 objects = NEW(ht_tab->count, struct named_object);
409 count = 0;
410 if (ht_set_iter(ht_tab, &iter) >= 0) {
411 do {
412 char *key;
413 int keylen;
414 struct obj_data *value;
415
416 key = ht_iter_getkey(&iter, &keylen);
417
418 if (filter) {
419 pdf_obj *new_obj = ht_lookup_table(filter, key, keylen);
420
421 if (!new_obj)
422 continue;
423
424 key = pdf_string_value(new_obj);
425 keylen = pdf_string_length(new_obj);
426 }
427
428 value = ht_iter_getval(&iter);
429 ASSERT(value->object);
430 if (PDF_OBJ_UNDEFINED(value->object)) {
431 WARN("Object @%s\" not defined. Replaced by null.",
432 printable_key(key, keylen));
433 objects[count].key = (char *) key;
434 objects[count].keylen = keylen;
435 objects[count].value = pdf_new_null();
436 } else if (value->object) {
437 objects[count].key = (char *) key;
438 objects[count].keylen = keylen;
439 objects[count].value = pdf_link_obj(value->object);
440 }
441 count++;
442 } while (ht_iter_next(&iter) >= 0);
443 ht_clear_iter(&iter);
444 }
445
446 *num_entries = count;
447 objects = RENEW(objects, count, struct named_object);
448
449 return objects;
450 }
451
452 pdf_obj *
453 pdf_names_create_tree (struct ht_table *names, int *count,
454 struct ht_table *filter)
455 {
456 pdf_obj *name_tree;
457 struct named_object *flat;
458
459 flat = flat_table(names, count, filter);
460 if (!flat)
461 name_tree = NULL;
462 else {
463 qsort(flat, *count, sizeof(struct named_object), cmp_key);
464 name_tree = build_name_tree(flat, *count, 1);
465 RELEASE(flat);
466 }
467
468 return name_tree;
469 }