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