pdf_name_tree.c

Fri, 14 Mar 2003 07:08:52 +0000

author
eric
date
Fri, 14 Mar 2003 07:08:52 +0000
changeset 130
d47b66e1722f
parent 125
e2ef1c2f9eca
child 131
4b8c80d77f76
permissions
-rw-r--r--

bookmarks should only be created for first page in a group.

eric@80 1 /*
eric@125 2 * tumble: build a PDF file from image files
eric@80 3 *
eric@80 4 * PDF routines
eric@125 5 * $Id: pdf_name_tree.c,v 1.9 2003/03/13 00:57:05 eric Exp $
eric@80 6 * Copyright 2003 Eric Smith <eric@brouhaha.com>
eric@80 7 *
eric@80 8 * This program is free software; you can redistribute it and/or modify
eric@80 9 * it under the terms of the GNU General Public License version 2 as
eric@80 10 * published by the Free Software Foundation. Note that permission is
eric@80 11 * not granted to redistribute this program under the terms of any
eric@80 12 * other version of the General Public License.
eric@80 13 *
eric@80 14 * This program is distributed in the hope that it will be useful,
eric@80 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
eric@80 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eric@80 17 * GNU General Public License for more details.
eric@80 18 *
eric@80 19 * You should have received a copy of the GNU General Public License
eric@80 20 * along with this program; if not, write to the Free Software
eric@80 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
eric@80 22 */
eric@80 23
eric@80 24
eric@80 25 #include <stdbool.h>
eric@80 26 #include <stdint.h>
eric@80 27 #include <stdio.h>
eric@80 28 #include <stdlib.h>
eric@80 29 #include <string.h>
eric@80 30
eric@80 31
eric@80 32 #include "bitblt.h"
eric@80 33 #include "pdf.h"
eric@80 34 #include "pdf_util.h"
eric@80 35 #include "pdf_prim.h"
eric@80 36 #include "pdf_private.h"
eric@80 37 #include "pdf_name_tree.h"
eric@80 38
eric@80 39
eric@80 40 #define MAX_NAME_TREE_NODE_ENTRIES 16
eric@80 41
eric@80 42
eric@80 43 struct pdf_name_tree_node
eric@80 44 {
eric@80 45 struct pdf_obj *dict; /* indirect reference */
eric@80 46
eric@80 47 struct pdf_name_tree_node *parent; /* NULL for root */
eric@80 48 bool leaf;
eric@80 49
eric@80 50 int count; /* how many kids or names/numbers are
eric@80 51 attached to this node */
eric@80 52
eric@80 53 struct pdf_name_tree_node *kids [MAX_NAME_TREE_NODE_ENTRIES]; /* non-leaf only */
eric@80 54
eric@80 55 struct pdf_obj *min_key;
eric@80 56 struct pdf_obj *max_key;
eric@80 57
eric@80 58 /* following fields valid in leaf nodes only: */
eric@80 59
eric@80 60 struct pdf_obj *keys [MAX_NAME_TREE_NODE_ENTRIES];
eric@80 61 struct pdf_obj *values [MAX_NAME_TREE_NODE_ENTRIES];
eric@80 62 };
eric@80 63
eric@80 64
eric@80 65 struct pdf_name_tree *pdf_new_name_tree (pdf_file_handle pdf_file,
eric@80 66 bool number_tree)
eric@80 67 {
eric@80 68 struct pdf_name_tree *tree;
eric@80 69 struct pdf_name_tree_node *root;
eric@80 70
eric@80 71 root = pdf_calloc (1, sizeof (struct pdf_name_tree_node));
eric@80 72 tree = pdf_calloc (1, sizeof (struct pdf_name_tree));
eric@80 73
eric@80 74 tree->pdf_file = pdf_file;
eric@80 75 tree->root = root;
eric@80 76 tree->number_tree = number_tree;
eric@80 77
eric@80 78 root->parent = NULL;
eric@80 79 root->leaf = 1;
eric@80 80
eric@85 81 tree->next = pdf_file->name_tree_list;
eric@85 82 pdf_file->name_tree_list = tree;
eric@85 83
eric@80 84 return (tree);
eric@80 85 }
eric@80 86
eric@80 87
eric@80 88 static void pdf_split_name_tree_node (struct pdf_name_tree *tree,
eric@80 89 struct pdf_name_tree_node *node)
eric@80 90 {
eric@87 91 struct pdf_name_tree_node *parent;
eric@80 92 struct pdf_name_tree_node *new_node;
eric@87 93 int i, j;
eric@80 94
eric@87 95 parent = node->parent;
eric@87 96 if (! parent)
eric@80 97 {
eric@80 98 /* create new root above current root */
eric@80 99 struct pdf_name_tree_node *new_root_node;
eric@80 100
eric@80 101 new_root_node = pdf_calloc (1, sizeof (struct pdf_name_tree_node));
eric@80 102
eric@80 103 new_root_node->parent = NULL;
eric@80 104 new_root_node->leaf = 0;
eric@80 105
eric@80 106 new_root_node->count = 1;
eric@80 107 new_root_node->kids [0] = node;
eric@80 108
eric@80 109 new_root_node->min_key = node->min_key;
eric@80 110 new_root_node->max_key = node->max_key;
eric@80 111
eric@87 112 parent = new_root_node;
eric@80 113 node->parent = new_root_node;
eric@80 114 tree->root = new_root_node;
eric@80 115 }
eric@80 116
eric@89 117 if (parent->count == MAX_NAME_TREE_NODE_ENTRIES)
eric@89 118 {
eric@89 119 pdf_split_name_tree_node (tree, parent);
eric@89 120 parent = node->parent;
eric@89 121 }
eric@89 122
eric@80 123 new_node = pdf_calloc (1, sizeof (struct pdf_name_tree_node));
eric@87 124 new_node->parent = parent;
eric@80 125 new_node->leaf = node->leaf;
eric@84 126
eric@87 127 /* move half the node's entries into the new node */
eric@87 128 i = node->count / 2;
eric@87 129 j = node->count - i;
eric@87 130
eric@87 131 memcpy (& new_node->kids [0],
eric@87 132 & node->kids [i],
eric@87 133 j * sizeof (struct pdf_name_tree_node *));
eric@87 134 memcpy (& new_node->keys [0],
eric@87 135 & node->keys [i],
eric@87 136 j * sizeof (struct pdf_obj *));
eric@87 137 memcpy (& new_node->values [0],
eric@87 138 & node->values [i],
eric@87 139 j * sizeof (struct pdf_obj *));
eric@89 140
eric@87 141 node->count = i;
eric@87 142 new_node->count = j;
eric@87 143
eric@89 144 if (! new_node->leaf)
eric@89 145 for (i = 0; i < j; i++)
eric@89 146 new_node->kids [i]->parent = new_node;
eric@89 147
eric@87 148 /* set max_key of the old node */
eric@87 149 if (node->leaf)
eric@87 150 node->max_key = node->keys [node->count - 1];
eric@87 151 else
eric@87 152 node->max_key = node->kids [node->count - 1]->max_key;
eric@87 153
eric@87 154 /* set min_key and max_key in the new node */
eric@87 155 if (new_node->leaf)
eric@87 156 {
eric@87 157 new_node->min_key = new_node->keys [0];
eric@87 158 new_node->max_key = new_node->keys [new_node->count - 1];
eric@87 159 }
eric@87 160 else
eric@87 161 {
eric@87 162 new_node->min_key = new_node->kids [0]->min_key;
eric@87 163 new_node->max_key = new_node->kids [new_node->count - 1]->max_key;
eric@87 164 }
eric@87 165
eric@87 166 /* insert new node in parent's kids array */
eric@87 167 /* find entry of old node */
eric@87 168 for (i = 0; i < parent->count; i++)
eric@87 169 if (parent->kids [i] == node)
eric@87 170 break;
eric@87 171
eric@87 172 /* it had better have been there! */
eric@87 173 pdf_assert (i < parent->count);
eric@87 174
eric@87 175 /* the new node goes in the slot to the right of the old node */
eric@87 176 i++;
eric@87 177
eric@87 178 /* move other entries right one position */
eric@88 179 if (i != parent->count)
eric@87 180 {
eric@87 181 memmove (& parent->kids [i+1],
eric@87 182 & parent->kids [i],
eric@87 183 (parent->count - i) * sizeof (struct pdf_name_tree_node *));
eric@87 184 }
eric@87 185
eric@87 186 parent->kids [i] = new_node;
eric@87 187 parent->count++;
eric@80 188 }
eric@80 189
eric@80 190
eric@80 191 static void pdf_add_tree_element (struct pdf_name_tree *tree,
eric@80 192 struct pdf_obj *key,
eric@80 193 struct pdf_obj *val)
eric@80 194 {
eric@80 195 struct pdf_name_tree_node *node;
eric@83 196 int i;
eric@80 197
eric@80 198 /* find node which should contain element */
eric@80 199 node = tree->root;
eric@83 200 while (! node->leaf)
eric@83 201 {
eric@83 202 for (i = 0; i < (node->count - 1); i++)
eric@83 203 if (pdf_compare_obj (key, node->kids [i + 1]->min_key) < 0)
eric@83 204 break;
eric@83 205 node = node->kids [i];
eric@83 206 }
eric@80 207
eric@80 208 /* if node is full, split, recursing to root if necessary */
eric@80 209 if (node->count == MAX_NAME_TREE_NODE_ENTRIES)
eric@80 210 {
eric@80 211 pdf_split_name_tree_node (tree, node);
eric@80 212 pdf_add_tree_element (tree, key, val);
eric@80 213 return;
eric@80 214 }
eric@83 215
eric@84 216 /* figure out in which slot to insert it */
eric@84 217 for (i = 0; i < node->count; i++)
eric@88 218 if (pdf_compare_obj (key, node->keys [i]) < 0)
eric@84 219 break;
eric@84 220
eric@84 221 /* move other entries right one position */
eric@84 222 if (i != node->count)
eric@84 223 {
eric@84 224 memmove (& node->keys [i+1],
eric@84 225 & node->keys [i],
eric@84 226 (node->count - i) * sizeof (struct pdf_obj *));
eric@84 227 memmove (& node->values [i+1],
eric@84 228 & node->values [i],
eric@84 229 (node->count - i) * sizeof (struct pdf_obj *));
eric@84 230 }
eric@84 231
eric@84 232 node->keys [i] = key;
eric@84 233 node->values [i] = val;
eric@84 234
eric@84 235 node->count++;
eric@83 236
eric@83 237 /* update limits, recursing upwards if necessary */
eric@83 238 if (i == 0)
eric@83 239 {
eric@83 240 node->min_key = key;
eric@83 241 while (node->parent && (node->parent->kids [0] == node))
eric@83 242 {
eric@83 243 node = node->parent;
eric@83 244 node->min_key = key;
eric@83 245 }
eric@83 246 }
eric@88 247 if (i == (node->count - 1))
eric@83 248 {
eric@83 249 node->max_key = key;
eric@83 250 while (node->parent && (node->parent->kids [node->parent->count - 1] == node))
eric@83 251 {
eric@83 252 node = node->parent;
eric@83 253 node->max_key = key;
eric@83 254 }
eric@83 255 }
eric@80 256 }
eric@80 257
eric@80 258
eric@80 259 void pdf_add_name_tree_element (struct pdf_name_tree *tree,
eric@80 260 char *key,
eric@80 261 struct pdf_obj *val)
eric@80 262 {
eric@80 263 struct pdf_obj *key_obj = pdf_new_string (key);
eric@80 264 pdf_add_tree_element (tree, key_obj, val);
eric@80 265 }
eric@80 266
eric@80 267
eric@80 268 void pdf_add_number_tree_element (struct pdf_name_tree *tree,
eric@80 269 long key,
eric@80 270 struct pdf_obj *val)
eric@80 271 {
eric@80 272 struct pdf_obj *key_obj = pdf_new_integer (key);
eric@80 273 pdf_add_tree_element (tree, key_obj, val);
eric@80 274 }
eric@80 275
eric@80 276
eric@80 277 static void pdf_finalize_name_tree_node (struct pdf_name_tree *tree,
eric@80 278 struct pdf_name_tree_node *node)
eric@80 279 {
eric@80 280 int i;
eric@80 281
eric@80 282 node->dict = pdf_new_ind_ref (tree->pdf_file, pdf_new_obj (PT_DICTIONARY));
eric@80 283
eric@80 284 if (node->leaf)
eric@80 285 {
eric@80 286 /* write Names or Nums array */
eric@80 287 struct pdf_obj *names = pdf_new_obj (PT_ARRAY);
eric@80 288 for (i = 0; i < node->count; i++)
eric@80 289 {
eric@80 290 pdf_add_array_elem (names, node->keys [i]);
eric@80 291 pdf_add_array_elem (names, node->values [i]);
eric@80 292 }
eric@80 293 pdf_set_dict_entry (node->dict,
eric@80 294 tree->number_tree ? "Nums" : "Names",
eric@80 295 names);
eric@80 296 }
eric@80 297 else
eric@80 298 {
eric@80 299 /* finalize the children first so that their dict ind ref is
eric@80 300 available */
eric@113 301 struct pdf_obj *kids;
eric@113 302
eric@80 303 for (i = 0; i < node->count; i++)
eric@80 304 pdf_finalize_name_tree_node (tree, node->kids [i]);
eric@80 305
eric@80 306 /* write Kids array */
eric@113 307 kids = pdf_new_obj (PT_ARRAY);
eric@80 308 for (i = 0; i < node->count; i++)
eric@80 309 pdf_add_array_elem (kids, node->kids [i]->dict);
eric@80 310 pdf_set_dict_entry (node->dict, "Kids", kids);
eric@80 311 }
eric@80 312
eric@88 313 if (node->parent)
eric@80 314 {
eric@80 315 /* write Limits array */
eric@80 316 struct pdf_obj *limits = pdf_new_obj (PT_ARRAY);
eric@80 317 pdf_add_array_elem (limits, node->min_key);
eric@80 318 pdf_add_array_elem (limits, node->max_key);
eric@80 319 pdf_set_dict_entry (node->dict, "Limits", limits);
eric@80 320 }
eric@80 321 }
eric@80 322
eric@80 323
eric@85 324 void pdf_finalize_name_trees (pdf_file_handle pdf_file)
eric@80 325 {
eric@85 326 struct pdf_name_tree *tree;
eric@85 327
eric@85 328 for (tree = pdf_file->name_tree_list; tree; tree = tree->next)
eric@85 329 pdf_finalize_name_tree_node (tree, tree->root);
eric@80 330 }