pdf_bookmark.c

Fri, 14 Mar 2003 08:24:37 +0000

author
eric
date
Fri, 14 Mar 2003 08:24:37 +0000
changeset 131
4b8c80d77f76
parent 125
e2ef1c2f9eca
permissions
-rw-r--r--

finished implementing page labels.

eric@74 1 /*
eric@125 2 * tumble: build a PDF file from image files
eric@74 3 *
eric@74 4 * PDF routines
eric@125 5 * $Id: pdf_bookmark.c,v 1.6 2003/03/13 00:57:05 eric Exp $
eric@78 6 * Copyright 2003 Eric Smith <eric@brouhaha.com>
eric@74 7 *
eric@74 8 * This program is free software; you can redistribute it and/or modify
eric@74 9 * it under the terms of the GNU General Public License version 2 as
eric@74 10 * published by the Free Software Foundation. Note that permission is
eric@74 11 * not granted to redistribute this program under the terms of any
eric@74 12 * other version of the General Public License.
eric@74 13 *
eric@74 14 * This program is distributed in the hope that it will be useful,
eric@74 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
eric@74 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eric@74 17 * GNU General Public License for more details.
eric@74 18 *
eric@74 19 * You should have received a copy of the GNU General Public License
eric@74 20 * along with this program; if not, write to the Free Software
eric@74 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
eric@74 22 */
eric@74 23
eric@74 24
eric@74 25 #include <stdbool.h>
eric@74 26 #include <stdint.h>
eric@74 27 #include <stdio.h>
eric@74 28 #include <stdlib.h>
eric@74 29 #include <string.h>
eric@74 30
eric@74 31
eric@74 32 #include "bitblt.h"
eric@74 33 #include "pdf.h"
eric@74 34 #include "pdf_util.h"
eric@74 35 #include "pdf_prim.h"
eric@74 36 #include "pdf_private.h"
eric@74 37
eric@74 38
eric@79 39 struct pdf_bookmark
eric@79 40 {
eric@79 41 struct pdf_obj *dict; /* indirect reference */
eric@79 42 struct pdf_obj *count;
eric@79 43 bool open;
eric@79 44
eric@79 45 struct pdf_bookmark *first;
eric@79 46 struct pdf_bookmark *last;
eric@79 47
eric@79 48 /* the following fields don't appear in the root */
eric@79 49 /* title and dest are in the dictionary but don't have
eric@79 50 explicit fields in the C structure */
eric@79 51 struct pdf_bookmark *parent;
eric@79 52 struct pdf_bookmark *prev;
eric@79 53 struct pdf_bookmark *next;
eric@79 54 };
eric@79 55
eric@79 56
eric@74 57 static void pdf_bookmark_update_count (pdf_bookmark_handle entry)
eric@74 58 {
eric@74 59 while (entry)
eric@74 60 {
eric@77 61 if (! entry->count)
eric@74 62 {
eric@77 63 entry->count = pdf_new_integer (0);
eric@77 64 pdf_set_dict_entry (entry->dict, "Count", entry->count);
eric@74 65 }
eric@77 66 pdf_set_integer (entry->count,
eric@77 67 pdf_get_integer (entry->count) +
eric@74 68 ((entry->open) ? 1 : -1));
eric@76 69 if (! entry->open)
eric@76 70 break;
eric@74 71 entry = entry->parent;
eric@74 72 }
eric@74 73 }
eric@74 74
eric@74 75
eric@74 76 /* Create a new bookmark, under the specified parent, or at the top
eric@74 77 level if parent is NULL. */
eric@74 78 pdf_bookmark_handle pdf_new_bookmark (pdf_bookmark_handle parent,
eric@74 79 char *title,
eric@74 80 bool open,
eric@74 81 pdf_page_handle pdf_page)
eric@74 82 {
eric@74 83 pdf_file_handle pdf_file = pdf_page->pdf_file;
eric@74 84 struct pdf_bookmark *root;
eric@74 85 struct pdf_bookmark *entry;
eric@74 86
eric@74 87 struct pdf_obj *dest_array;
eric@74 88
eric@74 89 root = pdf_file->outline_root;
eric@74 90 if (! root)
eric@74 91 {
eric@74 92 root = pdf_calloc (1, sizeof (struct pdf_bookmark));
eric@74 93 root->dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
eric@74 94
eric@74 95 pdf_file->outline_root = root;
eric@74 96 pdf_set_dict_entry (pdf_file->catalog, "Outlines", root->dict);
eric@74 97 }
eric@74 98
eric@74 99 entry = pdf_calloc (1, sizeof (struct pdf_bookmark));
eric@74 100 entry->dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
eric@74 101 entry->open = open;
eric@74 102
eric@74 103 pdf_set_dict_entry (entry->dict, "Title", pdf_new_string (title));
eric@74 104
eric@74 105 dest_array = pdf_new_obj (PT_ARRAY);
eric@74 106 pdf_add_array_elem (dest_array, pdf_page->page_dict);
eric@74 107 pdf_add_array_elem (dest_array, pdf_new_name ("Fit"));
eric@74 108 pdf_set_dict_entry (entry->dict, "Dest", dest_array);
eric@74 109
eric@74 110 if (parent)
eric@74 111 {
eric@74 112 entry->parent = parent;
eric@74 113 entry->prev = parent->last;
eric@74 114 }
eric@74 115 else
eric@74 116 {
eric@74 117 parent = root;
eric@74 118 entry->parent = root;
eric@74 119 entry->prev = root->last;
eric@74 120 }
eric@74 121
eric@74 122 pdf_set_dict_entry (entry->dict, "Parent", parent->dict);
eric@74 123
eric@74 124 if (entry->prev)
eric@74 125 {
eric@74 126 pdf_set_dict_entry (entry->dict, "Prev", entry->prev->dict);
eric@74 127
eric@74 128 entry->prev->next = entry;
eric@74 129 pdf_set_dict_entry (entry->prev->dict, "Next", entry->dict);
eric@74 130
eric@74 131 parent->last = entry;
eric@74 132 pdf_set_dict_entry (parent->dict, "Last", entry->dict);
eric@74 133 }
eric@74 134 else
eric@74 135 {
eric@74 136 parent->first = entry;
eric@74 137 pdf_set_dict_entry (parent->dict, "First", entry->dict);
eric@74 138
eric@74 139 parent->last = entry;
eric@74 140 pdf_set_dict_entry (parent->dict, "Last", entry->dict);
eric@74 141 }
eric@74 142
eric@74 143 pdf_bookmark_update_count (parent);
eric@74 144
eric@74 145 return (entry);
eric@74 146 }