added name trees and number trees.

Fri, 07 Mar 2003 10:16:08 +0000

author
eric
date
Fri, 07 Mar 2003 10:16:08 +0000
changeset 80
96c64a7566df
parent 79
4cd9d23d924a
child 81
0fc65859d436

added name trees and number trees.

Makefile file | annotate | diff | revisions
TODO file | annotate | diff | revisions
pdf_name_tree.c file | annotate | diff | revisions
pdf_name_tree.h file | annotate | diff | revisions
     1.1 --- a/Makefile	Wed Mar 05 20:56:25 2003 +0000
     1.2 +++ b/Makefile	Fri Mar 07 10:16:08 2003 +0000
     1.3 @@ -1,7 +1,7 @@
     1.4  # t2p: build a PDF file out of one or more TIFF Class F Group 4 files
     1.5  # Makefile
     1.6 -# $Id: Makefile,v 1.17 2003/03/04 17:58:36 eric Exp $
     1.7 -# Copyright 2001 Eric Smith <eric@brouhaha.com>
     1.8 +# $Id: Makefile,v 1.18 2003/03/07 02:16:08 eric Exp $
     1.9 +# Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
    1.10  #
    1.11  # This program is free software; you can redistribute it and/or modify
    1.12  # it under the terms of the GNU General Public License version 2 as
    1.13 @@ -42,11 +42,13 @@
    1.14  
    1.15  TARGETS = t2p bitblt_test
    1.16  
    1.17 -CSRCS = t2p.c semantics.c bitblt.c bitblt_test.c bitblt_table_gen.c \
    1.18 -	pdf.c pdf_util.c pdf_prim.c pdf_bookmark.c pdf_g4.c pdf_g4_table_gen.c
    1.19 +CSRCS = t2p.c semantics.c \
    1.20 +	bitblt.c bitblt_test.c bitblt_table_gen.c \
    1.21 +	pdf_g4.c pdf_g4_table_gen.c \
    1.22 +	pdf.c pdf_util.c pdf_prim.c pdf_bookmark.c pdf_name_tree.c
    1.23  OSRCS = scanner.l parser.y
    1.24  HDRS = t2p.h semantics.h bitblt.h \
    1.25 -	pdf.h pdf_private.h pdf_util.h pdf_prim.h
    1.26 +	pdf.h pdf_private.h pdf_util.h pdf_prim.h pdf_name_tree.h
    1.27  MISC = COPYING Makefile
    1.28  
    1.29  DISTFILES = $(MISC) $(HDRS) $(CSRCS) $(OSRCS)
    1.30 @@ -62,7 +64,8 @@
    1.31  
    1.32  
    1.33  t2p: t2p.o scanner.o semantics.o parser.tab.o bitblt.o \
    1.34 -	pdf.o pdf_util.o pdf_prim.o pdf_bookmark.o pdf_g4.o
    1.35 +	pdf_g4.o \
    1.36 +	pdf.o pdf_util.o pdf_prim.o pdf_bookmark.o pdf_name_tree.o
    1.37  
    1.38  bitblt_tables.h: bitblt_table_gen
    1.39  	./bitblt_table_gen >bitblt_tables.h
     2.1 --- a/TODO	Wed Mar 05 20:56:25 2003 +0000
     2.2 +++ b/TODO	Fri Mar 07 10:16:08 2003 +0000
     2.3 @@ -1,5 +1,5 @@
     2.4  t2p TODO list
     2.5 -$Id: TODO,v 1.10 2003/03/04 17:58:36 eric Exp $
     2.6 +$Id: TODO,v 1.11 2003/03/07 02:16:08 eric Exp $
     2.7  
     2.8  No particular order.
     2.9  
    2.10 @@ -73,6 +73,7 @@
    2.11      * alternate destination specs (only is /Fit currently supported)
    2.12  
    2.13  * name trees, number trees
    2.14 +    * when finalize is called, set flag and allow no further changes
    2.15  
    2.16  * page labels
    2.17  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/pdf_name_tree.c	Fri Mar 07 10:16:08 2003 +0000
     3.3 @@ -0,0 +1,203 @@
     3.4 +/*
     3.5 + * t2p: Create a PDF file from the contents of one or more TIFF
     3.6 + *      bilevel image files.  The images in the resulting PDF file
     3.7 + *      will be compressed using ITU-T T.6 (G4) fax encoding.
     3.8 + *
     3.9 + * PDF routines
    3.10 + * $Id: pdf_name_tree.c,v 1.1 2003/03/07 02:16:08 eric Exp $
    3.11 + * Copyright 2003 Eric Smith <eric@brouhaha.com>
    3.12 + *
    3.13 + * This program is free software; you can redistribute it and/or modify
    3.14 + * it under the terms of the GNU General Public License version 2 as
    3.15 + * published by the Free Software Foundation.  Note that permission is
    3.16 + * not granted to redistribute this program under the terms of any
    3.17 + * other version of the General Public License.
    3.18 + *
    3.19 + * This program is distributed in the hope that it will be useful,
    3.20 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.22 + * GNU General Public License for more details.
    3.23 + *
    3.24 + * You should have received a copy of the GNU General Public License
    3.25 + * along with this program; if not, write to the Free Software
    3.26 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    3.27 + */
    3.28 +
    3.29 +
    3.30 +#include <stdbool.h>
    3.31 +#include <stdint.h>
    3.32 +#include <stdio.h>
    3.33 +#include <stdlib.h>
    3.34 +#include <string.h>
    3.35 +
    3.36 +
    3.37 +#include "bitblt.h"
    3.38 +#include "pdf.h"
    3.39 +#include "pdf_util.h"
    3.40 +#include "pdf_prim.h"
    3.41 +#include "pdf_private.h"
    3.42 +#include "pdf_name_tree.h"
    3.43 +
    3.44 +
    3.45 +#define MAX_NAME_TREE_NODE_ENTRIES 16
    3.46 +
    3.47 +
    3.48 +struct pdf_name_tree_node
    3.49 +{
    3.50 +  struct pdf_obj *dict;    /* indirect reference */
    3.51 +
    3.52 +  struct pdf_name_tree_node *parent;  /* NULL for root */
    3.53 +  bool leaf;
    3.54 +
    3.55 +  int count;               /* how many kids or names/numbers are
    3.56 +			      attached to this node */
    3.57 +
    3.58 +  struct pdf_name_tree_node *kids [MAX_NAME_TREE_NODE_ENTRIES];  /* non-leaf only */
    3.59 +
    3.60 +  struct pdf_obj *min_key;
    3.61 +  struct pdf_obj *max_key;
    3.62 +
    3.63 +  /* following fields valid in leaf nodes only: */
    3.64 +
    3.65 +  struct pdf_obj *keys [MAX_NAME_TREE_NODE_ENTRIES];
    3.66 +  struct pdf_obj *values [MAX_NAME_TREE_NODE_ENTRIES];
    3.67 +};
    3.68 +
    3.69 +
    3.70 +struct pdf_name_tree *pdf_new_name_tree (pdf_file_handle pdf_file,
    3.71 +					 bool number_tree)
    3.72 +{
    3.73 +  struct pdf_name_tree *tree;
    3.74 +  struct pdf_name_tree_node *root;
    3.75 +
    3.76 +  root = pdf_calloc (1, sizeof (struct pdf_name_tree_node));
    3.77 +  tree = pdf_calloc (1, sizeof (struct pdf_name_tree));
    3.78 +
    3.79 +  tree->pdf_file = pdf_file;
    3.80 +  tree->root = root;
    3.81 +  tree->number_tree = number_tree;
    3.82 +
    3.83 +  root->parent = NULL;
    3.84 +  root->leaf = 1;
    3.85 +
    3.86 +  return (tree);
    3.87 +}
    3.88 +
    3.89 +
    3.90 +static void pdf_split_name_tree_node (struct pdf_name_tree *tree,
    3.91 +				      struct pdf_name_tree_node *node)
    3.92 +{
    3.93 +  struct pdf_name_tree_node *new_node;
    3.94 +
    3.95 +  if (node == tree->root)
    3.96 +    {
    3.97 +      /* create new root above current root */
    3.98 +      struct pdf_name_tree_node *new_root_node;
    3.99 +
   3.100 +      new_root_node = pdf_calloc (1, sizeof (struct pdf_name_tree_node));
   3.101 +
   3.102 +      new_root_node->parent = NULL;
   3.103 +      new_root_node->leaf = 0;
   3.104 +
   3.105 +      new_root_node->count = 1;
   3.106 +      new_root_node->kids [0] = node;
   3.107 +
   3.108 +      new_root_node->min_key = node->min_key;
   3.109 +      new_root_node->max_key = node->max_key;
   3.110 +
   3.111 +      node->parent = new_root_node;
   3.112 +      tree->root = new_root_node;
   3.113 +    }
   3.114 +
   3.115 +  new_node = pdf_calloc (1, sizeof (struct pdf_name_tree_node));
   3.116 +  new_node->parent = node->parent;
   3.117 +  new_node->leaf = node->leaf;
   3.118 +}
   3.119 +
   3.120 +
   3.121 +static void pdf_add_tree_element (struct pdf_name_tree *tree,
   3.122 +				  struct pdf_obj *key,
   3.123 +				  struct pdf_obj *val)
   3.124 +{
   3.125 +  struct pdf_name_tree_node *node;
   3.126 +
   3.127 +  /* find node which should contain element */
   3.128 +  node = tree->root;
   3.129 +
   3.130 +  /* if node is full, split, recursing to root if necessary */
   3.131 +  if (node->count == MAX_NAME_TREE_NODE_ENTRIES)
   3.132 +    {
   3.133 +      pdf_split_name_tree_node (tree, node);
   3.134 +      pdf_add_tree_element (tree, key, val);
   3.135 +      return;
   3.136 +    }
   3.137 +}
   3.138 +
   3.139 +
   3.140 +void pdf_add_name_tree_element (struct pdf_name_tree *tree,
   3.141 +				char *key,
   3.142 +				struct pdf_obj *val)
   3.143 +{
   3.144 +  struct pdf_obj *key_obj = pdf_new_string (key);
   3.145 +  pdf_add_tree_element (tree, key_obj, val);
   3.146 +}
   3.147 +
   3.148 +
   3.149 +void pdf_add_number_tree_element (struct pdf_name_tree *tree,
   3.150 +				  long key,
   3.151 +				  struct pdf_obj *val)
   3.152 +{
   3.153 +  struct pdf_obj *key_obj = pdf_new_integer (key);
   3.154 +  pdf_add_tree_element (tree, key_obj, val);
   3.155 +}
   3.156 +
   3.157 +
   3.158 +static void pdf_finalize_name_tree_node (struct pdf_name_tree *tree,
   3.159 +					 struct pdf_name_tree_node *node)
   3.160 +{
   3.161 +  int i;
   3.162 +
   3.163 +  node->dict = pdf_new_ind_ref (tree->pdf_file, pdf_new_obj (PT_DICTIONARY));
   3.164 +
   3.165 +  if (node->leaf)
   3.166 +    {
   3.167 +      /* write Names or Nums array */
   3.168 +      struct pdf_obj *names = pdf_new_obj (PT_ARRAY);
   3.169 +      for (i = 0; i < node->count; i++)
   3.170 +	{
   3.171 +	  pdf_add_array_elem (names, node->keys [i]);
   3.172 +	  pdf_add_array_elem (names, node->values [i]);
   3.173 +	}
   3.174 +      pdf_set_dict_entry (node->dict,
   3.175 +			  tree->number_tree ? "Nums" : "Names",
   3.176 +			  names);
   3.177 +    }
   3.178 +  else
   3.179 +    {
   3.180 +      /* finalize the children first so that their dict ind ref is
   3.181 +	 available */
   3.182 +      for (i = 0; i < node->count; i++)
   3.183 +	pdf_finalize_name_tree_node (tree, node->kids [i]);
   3.184 +
   3.185 +      /* write Kids array */
   3.186 +      struct pdf_obj *kids = pdf_new_obj (PT_ARRAY);
   3.187 +      for (i = 0; i < node->count; i++)
   3.188 +	pdf_add_array_elem (kids, node->kids [i]->dict);
   3.189 +      pdf_set_dict_entry (node->dict, "Kids", kids);
   3.190 +    }
   3.191 +
   3.192 +  if (! node->parent)
   3.193 +    {
   3.194 +      /* write Limits array */
   3.195 +      struct pdf_obj *limits = pdf_new_obj (PT_ARRAY);
   3.196 +      pdf_add_array_elem (limits, node->min_key);
   3.197 +      pdf_add_array_elem (limits, node->max_key);
   3.198 +      pdf_set_dict_entry (node->dict, "Limits", limits);
   3.199 +    }
   3.200 +}
   3.201 +
   3.202 +
   3.203 +void pdf_finalize_name_tree (struct pdf_name_tree *tree)
   3.204 +{
   3.205 +  pdf_finalize_name_tree_node (tree, tree->root);
   3.206 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/pdf_name_tree.h	Fri Mar 07 10:16:08 2003 +0000
     4.3 @@ -0,0 +1,50 @@
     4.4 +/*
     4.5 + * t2p: Create a PDF file from the contents of one or more TIFF
     4.6 + *      bilevel image files.  The images in the resulting PDF file
     4.7 + *      will be compressed using ITU-T T.6 (G4) fax encoding.
     4.8 + *
     4.9 + * PDF routines
    4.10 + * $Id: pdf_name_tree.h,v 1.1 2003/03/07 02:16:08 eric Exp $
    4.11 + * Copyright 2003 Eric Smith <eric@brouhaha.com>
    4.12 + *
    4.13 + * This program is free software; you can redistribute it and/or modify
    4.14 + * it under the terms of the GNU General Public License version 2 as
    4.15 + * published by the Free Software Foundation.  Note that permission is
    4.16 + * not granted to redistribute this program under the terms of any
    4.17 + * other version of the General Public License.
    4.18 + *
    4.19 + * This program is distributed in the hope that it will be useful,
    4.20 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.22 + * GNU General Public License for more details.
    4.23 + *
    4.24 + * You should have received a copy of the GNU General Public License
    4.25 + * along with this program; if not, write to the Free Software
    4.26 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    4.27 + */
    4.28 +
    4.29 +
    4.30 +struct pdf_name_tree
    4.31 +{
    4.32 +  pdf_file_handle   pdf_file;
    4.33 +  bool              number_tree;   /* false for name tree,
    4.34 +				      true for number tree */
    4.35 +  struct pdf_name_tree_node *root;
    4.36 +};
    4.37 +
    4.38 +
    4.39 +struct pdf_name_tree *pdf_new_name_tree (pdf_file_handle pdf_file,
    4.40 +					 bool number_tree);
    4.41 +
    4.42 +
    4.43 +void pdf_add_name_tree_element (struct pdf_name_tree *tree,
    4.44 +				char *key,
    4.45 +				struct pdf_obj *val);
    4.46 +
    4.47 +
    4.48 +void pdf_add_number_tree_element (struct pdf_name_tree *tree,
    4.49 +				  long key,
    4.50 +				  struct pdf_obj *val);
    4.51 +
    4.52 +
    4.53 +void pdf_finalize_name_tree (struct pdf_name_tree *tree);