pdf_name_tree.c

Fri, 07 Mar 2003 11:35:36 +0000

author
eric
date
Fri, 07 Mar 2003 11:35:36 +0000
changeset 84
a54eb7b9fc15
parent 83
bbc7dd27aa5b
child 85
dcfd1d4b5c24
permissions
-rw-r--r--

more work on pdf_add_tree_element().

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