1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/pdf_name_tree.c Fri Mar 07 10:16:08 2003 +0000 1.3 @@ -0,0 +1,203 @@ 1.4 +/* 1.5 + * t2p: Create a PDF file from the contents of one or more TIFF 1.6 + * bilevel image files. The images in the resulting PDF file 1.7 + * will be compressed using ITU-T T.6 (G4) fax encoding. 1.8 + * 1.9 + * PDF routines 1.10 + * $Id: pdf_name_tree.c,v 1.1 2003/03/07 02:16:08 eric Exp $ 1.11 + * Copyright 2003 Eric Smith <eric@brouhaha.com> 1.12 + * 1.13 + * This program is free software; you can redistribute it and/or modify 1.14 + * it under the terms of the GNU General Public License version 2 as 1.15 + * published by the Free Software Foundation. Note that permission is 1.16 + * not granted to redistribute this program under the terms of any 1.17 + * other version of the General Public License. 1.18 + * 1.19 + * This program is distributed in the hope that it will be useful, 1.20 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 1.21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.22 + * GNU General Public License for more details. 1.23 + * 1.24 + * You should have received a copy of the GNU General Public License 1.25 + * along with this program; if not, write to the Free Software 1.26 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA 1.27 + */ 1.28 + 1.29 + 1.30 +#include <stdbool.h> 1.31 +#include <stdint.h> 1.32 +#include <stdio.h> 1.33 +#include <stdlib.h> 1.34 +#include <string.h> 1.35 + 1.36 + 1.37 +#include "bitblt.h" 1.38 +#include "pdf.h" 1.39 +#include "pdf_util.h" 1.40 +#include "pdf_prim.h" 1.41 +#include "pdf_private.h" 1.42 +#include "pdf_name_tree.h" 1.43 + 1.44 + 1.45 +#define MAX_NAME_TREE_NODE_ENTRIES 16 1.46 + 1.47 + 1.48 +struct pdf_name_tree_node 1.49 +{ 1.50 + struct pdf_obj *dict; /* indirect reference */ 1.51 + 1.52 + struct pdf_name_tree_node *parent; /* NULL for root */ 1.53 + bool leaf; 1.54 + 1.55 + int count; /* how many kids or names/numbers are 1.56 + attached to this node */ 1.57 + 1.58 + struct pdf_name_tree_node *kids [MAX_NAME_TREE_NODE_ENTRIES]; /* non-leaf only */ 1.59 + 1.60 + struct pdf_obj *min_key; 1.61 + struct pdf_obj *max_key; 1.62 + 1.63 + /* following fields valid in leaf nodes only: */ 1.64 + 1.65 + struct pdf_obj *keys [MAX_NAME_TREE_NODE_ENTRIES]; 1.66 + struct pdf_obj *values [MAX_NAME_TREE_NODE_ENTRIES]; 1.67 +}; 1.68 + 1.69 + 1.70 +struct pdf_name_tree *pdf_new_name_tree (pdf_file_handle pdf_file, 1.71 + bool number_tree) 1.72 +{ 1.73 + struct pdf_name_tree *tree; 1.74 + struct pdf_name_tree_node *root; 1.75 + 1.76 + root = pdf_calloc (1, sizeof (struct pdf_name_tree_node)); 1.77 + tree = pdf_calloc (1, sizeof (struct pdf_name_tree)); 1.78 + 1.79 + tree->pdf_file = pdf_file; 1.80 + tree->root = root; 1.81 + tree->number_tree = number_tree; 1.82 + 1.83 + root->parent = NULL; 1.84 + root->leaf = 1; 1.85 + 1.86 + return (tree); 1.87 +} 1.88 + 1.89 + 1.90 +static void pdf_split_name_tree_node (struct pdf_name_tree *tree, 1.91 + struct pdf_name_tree_node *node) 1.92 +{ 1.93 + struct pdf_name_tree_node *new_node; 1.94 + 1.95 + if (node == tree->root) 1.96 + { 1.97 + /* create new root above current root */ 1.98 + struct pdf_name_tree_node *new_root_node; 1.99 + 1.100 + new_root_node = pdf_calloc (1, sizeof (struct pdf_name_tree_node)); 1.101 + 1.102 + new_root_node->parent = NULL; 1.103 + new_root_node->leaf = 0; 1.104 + 1.105 + new_root_node->count = 1; 1.106 + new_root_node->kids [0] = node; 1.107 + 1.108 + new_root_node->min_key = node->min_key; 1.109 + new_root_node->max_key = node->max_key; 1.110 + 1.111 + node->parent = new_root_node; 1.112 + tree->root = new_root_node; 1.113 + } 1.114 + 1.115 + new_node = pdf_calloc (1, sizeof (struct pdf_name_tree_node)); 1.116 + new_node->parent = node->parent; 1.117 + new_node->leaf = node->leaf; 1.118 +} 1.119 + 1.120 + 1.121 +static void pdf_add_tree_element (struct pdf_name_tree *tree, 1.122 + struct pdf_obj *key, 1.123 + struct pdf_obj *val) 1.124 +{ 1.125 + struct pdf_name_tree_node *node; 1.126 + 1.127 + /* find node which should contain element */ 1.128 + node = tree->root; 1.129 + 1.130 + /* if node is full, split, recursing to root if necessary */ 1.131 + if (node->count == MAX_NAME_TREE_NODE_ENTRIES) 1.132 + { 1.133 + pdf_split_name_tree_node (tree, node); 1.134 + pdf_add_tree_element (tree, key, val); 1.135 + return; 1.136 + } 1.137 +} 1.138 + 1.139 + 1.140 +void pdf_add_name_tree_element (struct pdf_name_tree *tree, 1.141 + char *key, 1.142 + struct pdf_obj *val) 1.143 +{ 1.144 + struct pdf_obj *key_obj = pdf_new_string (key); 1.145 + pdf_add_tree_element (tree, key_obj, val); 1.146 +} 1.147 + 1.148 + 1.149 +void pdf_add_number_tree_element (struct pdf_name_tree *tree, 1.150 + long key, 1.151 + struct pdf_obj *val) 1.152 +{ 1.153 + struct pdf_obj *key_obj = pdf_new_integer (key); 1.154 + pdf_add_tree_element (tree, key_obj, val); 1.155 +} 1.156 + 1.157 + 1.158 +static void pdf_finalize_name_tree_node (struct pdf_name_tree *tree, 1.159 + struct pdf_name_tree_node *node) 1.160 +{ 1.161 + int i; 1.162 + 1.163 + node->dict = pdf_new_ind_ref (tree->pdf_file, pdf_new_obj (PT_DICTIONARY)); 1.164 + 1.165 + if (node->leaf) 1.166 + { 1.167 + /* write Names or Nums array */ 1.168 + struct pdf_obj *names = pdf_new_obj (PT_ARRAY); 1.169 + for (i = 0; i < node->count; i++) 1.170 + { 1.171 + pdf_add_array_elem (names, node->keys [i]); 1.172 + pdf_add_array_elem (names, node->values [i]); 1.173 + } 1.174 + pdf_set_dict_entry (node->dict, 1.175 + tree->number_tree ? "Nums" : "Names", 1.176 + names); 1.177 + } 1.178 + else 1.179 + { 1.180 + /* finalize the children first so that their dict ind ref is 1.181 + available */ 1.182 + for (i = 0; i < node->count; i++) 1.183 + pdf_finalize_name_tree_node (tree, node->kids [i]); 1.184 + 1.185 + /* write Kids array */ 1.186 + struct pdf_obj *kids = pdf_new_obj (PT_ARRAY); 1.187 + for (i = 0; i < node->count; i++) 1.188 + pdf_add_array_elem (kids, node->kids [i]->dict); 1.189 + pdf_set_dict_entry (node->dict, "Kids", kids); 1.190 + } 1.191 + 1.192 + if (! node->parent) 1.193 + { 1.194 + /* write Limits array */ 1.195 + struct pdf_obj *limits = pdf_new_obj (PT_ARRAY); 1.196 + pdf_add_array_elem (limits, node->min_key); 1.197 + pdf_add_array_elem (limits, node->max_key); 1.198 + pdf_set_dict_entry (node->dict, "Limits", limits); 1.199 + } 1.200 +} 1.201 + 1.202 + 1.203 +void pdf_finalize_name_tree (struct pdf_name_tree *tree) 1.204 +{ 1.205 + pdf_finalize_name_tree_node (tree, tree->root); 1.206 +}