pdf_bookmark.c

changeset 74
12bc5088172e
child 76
2129cfb36fcc
     1.1 diff -r 6e306105c128 -r 12bc5088172e pdf_bookmark.c
     1.2 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 +++ b/pdf_bookmark.c	Wed Mar 05 01:58:36 2003 +0000
     1.4 @@ -0,0 +1,132 @@
     1.5 +/*
     1.6 + * t2p: Create a PDF file from the contents of one or more TIFF
     1.7 + *      bilevel image files.  The images in the resulting PDF file
     1.8 + *      will be compressed using ITU-T T.6 (G4) fax encoding.
     1.9 + *
    1.10 + * PDF routines
    1.11 + * $Id: pdf_bookmark.c,v 1.1 2003/03/04 17:58:36 eric Exp $
    1.12 + * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
    1.13 + *
    1.14 + * This program is free software; you can redistribute it and/or modify
    1.15 + * it under the terms of the GNU General Public License version 2 as
    1.16 + * published by the Free Software Foundation.  Note that permission is
    1.17 + * not granted to redistribute this program under the terms of any
    1.18 + * other version of the General Public License.
    1.19 + *
    1.20 + * This program is distributed in the hope that it will be useful,
    1.21 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.22 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.23 + * GNU General Public License for more details.
    1.24 + *
    1.25 + * You should have received a copy of the GNU General Public License
    1.26 + * along with this program; if not, write to the Free Software
    1.27 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    1.28 + */
    1.29 +
    1.30 +
    1.31 +#include <stdbool.h>
    1.32 +#include <stdint.h>
    1.33 +#include <stdio.h>
    1.34 +#include <stdlib.h>
    1.35 +#include <string.h>
    1.36 +
    1.37 +
    1.38 +#include "bitblt.h"
    1.39 +#include "pdf.h"
    1.40 +#include "pdf_util.h"
    1.41 +#include "pdf_prim.h"
    1.42 +#include "pdf_private.h"
    1.43 +
    1.44 +
    1.45 +static void pdf_bookmark_update_count (pdf_bookmark_handle entry)
    1.46 +{
    1.47 +  struct pdf_obj *count_obj;
    1.48 +
    1.49 +  while (entry)
    1.50 +    {
    1.51 +      count_obj = pdf_get_dict_entry (entry->dict, "Count");
    1.52 +      if (! count_obj)
    1.53 +	{
    1.54 +	  count_obj = pdf_new_integer (0);
    1.55 +	  pdf_set_dict_entry (entry->dict, "Count", count_obj);
    1.56 +	}
    1.57 +      pdf_set_integer (count_obj,
    1.58 +		       pdf_get_integer (count_obj) +
    1.59 +		       ((entry->open) ? 1 : -1));
    1.60 +      entry = entry->parent;
    1.61 +    }
    1.62 +}
    1.63 +
    1.64 +
    1.65 +/* Create a new bookmark, under the specified parent, or at the top
    1.66 +   level if parent is NULL. */
    1.67 +pdf_bookmark_handle pdf_new_bookmark (pdf_bookmark_handle parent,
    1.68 +				      char *title,
    1.69 +				      bool open,
    1.70 +				      pdf_page_handle pdf_page)
    1.71 +{
    1.72 +  pdf_file_handle pdf_file = pdf_page->pdf_file;
    1.73 +  struct pdf_bookmark *root;
    1.74 +  struct pdf_bookmark *entry;
    1.75 +
    1.76 +  struct pdf_obj *dest_array;
    1.77 +
    1.78 +  root = pdf_file->outline_root;
    1.79 +  if (! root)
    1.80 +    {
    1.81 +      root = pdf_calloc (1, sizeof (struct pdf_bookmark));
    1.82 +      root->dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    1.83 +      pdf_set_dict_entry (root->dict, "Count", pdf_new_integer (0));
    1.84 +
    1.85 +      pdf_file->outline_root = root;
    1.86 +      pdf_set_dict_entry (pdf_file->catalog, "Outlines", root->dict);
    1.87 +    }
    1.88 +
    1.89 +  entry = pdf_calloc (1, sizeof (struct pdf_bookmark));
    1.90 +  entry->dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    1.91 +  entry->open = open;
    1.92 +
    1.93 +  pdf_set_dict_entry (entry->dict, "Title", pdf_new_string (title));
    1.94 +
    1.95 +  dest_array = pdf_new_obj (PT_ARRAY);
    1.96 +  pdf_add_array_elem (dest_array, pdf_page->page_dict);
    1.97 +  pdf_add_array_elem (dest_array, pdf_new_name ("Fit"));
    1.98 +  pdf_set_dict_entry (entry->dict, "Dest", dest_array);
    1.99 +
   1.100 +  if (parent)
   1.101 +    {
   1.102 +      entry->parent = parent;
   1.103 +      entry->prev = parent->last;
   1.104 +    }
   1.105 +  else
   1.106 +    {
   1.107 +      parent = root;
   1.108 +      entry->parent = root;
   1.109 +      entry->prev = root->last;
   1.110 +    }
   1.111 +
   1.112 +  pdf_set_dict_entry (entry->dict, "Parent", parent->dict);
   1.113 +
   1.114 +  if (entry->prev)
   1.115 +    {
   1.116 +      pdf_set_dict_entry (entry->dict, "Prev", entry->prev->dict);
   1.117 +
   1.118 +      entry->prev->next = entry;
   1.119 +      pdf_set_dict_entry (entry->prev->dict, "Next", entry->dict);
   1.120 +
   1.121 +      parent->last = entry;
   1.122 +      pdf_set_dict_entry (parent->dict, "Last", entry->dict);
   1.123 +    }
   1.124 +  else
   1.125 +    {
   1.126 +      parent->first = entry;
   1.127 +      pdf_set_dict_entry (parent->dict, "First", entry->dict);
   1.128 +
   1.129 +      parent->last = entry;
   1.130 +      pdf_set_dict_entry (parent->dict, "Last", entry->dict);
   1.131 +    }
   1.132 +
   1.133 +  pdf_bookmark_update_count (parent);
   1.134 +
   1.135 +  return (entry);
   1.136 +}