pdf.c

Thu, 13 Mar 2003 08:03:11 +0000

author
eric
date
Thu, 13 Mar 2003 08:03:11 +0000
changeset 124
ba64dfca82e9
parent 118
c22e1c0a64fd
child 125
e2ef1c2f9eca
permissions
-rw-r--r--

Acrobat 4.0 fails to optimize PDF files in which the Kids array in the Pages object is an indirect reference.

eric@62 1 /*
eric@62 2 * t2p: Create a PDF file from the contents of one or more TIFF
eric@62 3 * bilevel image files. The images in the resulting PDF file
eric@62 4 * will be compressed using ITU-T T.6 (G4) fax encoding.
eric@62 5 *
eric@62 6 * PDF routines
eric@124 7 * $Id: pdf.c,v 1.9 2003/03/13 00:03:11 eric Exp $
eric@62 8 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
eric@62 9 *
eric@62 10 * This program is free software; you can redistribute it and/or modify
eric@62 11 * it under the terms of the GNU General Public License version 2 as
eric@62 12 * published by the Free Software Foundation. Note that permission is
eric@62 13 * not granted to redistribute this program under the terms of any
eric@62 14 * other version of the General Public License.
eric@62 15 *
eric@62 16 * This program is distributed in the hope that it will be useful,
eric@62 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
eric@62 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eric@62 19 * GNU General Public License for more details.
eric@62 20 *
eric@62 21 * You should have received a copy of the GNU General Public License
eric@62 22 * along with this program; if not, write to the Free Software
eric@62 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
eric@62 24 */
eric@62 25
eric@62 26
eric@62 27 #include <stdbool.h>
eric@62 28 #include <stdint.h>
eric@59 29 #include <stdio.h>
eric@59 30 #include <stdlib.h>
eric@59 31
eric@59 32
eric@62 33 #include "bitblt.h"
eric@60 34 #include "pdf.h"
eric@60 35 #include "pdf_util.h"
eric@60 36 #include "pdf_prim.h"
eric@60 37 #include "pdf_private.h"
eric@85 38 #include "pdf_name_tree.h"
eric@59 39
eric@59 40
eric@59 41 static void pdf_set_info (pdf_file_handle pdf_file, char *key, char *val)
eric@59 42 {
eric@59 43 if (! pdf_file->info)
eric@59 44 pdf_file->info = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
eric@59 45
eric@59 46 pdf_set_dict_entry (pdf_file->info, key, pdf_new_string (val));
eric@59 47 }
eric@59 48
eric@59 49
eric@59 50 void pdf_init (void)
eric@59 51 {
eric@59 52 }
eric@59 53
eric@59 54
eric@59 55 struct pdf_pages *pdf_new_pages (pdf_file_handle pdf_file)
eric@59 56 {
eric@67 57 struct pdf_pages *pages = pdf_calloc (1, sizeof (struct pdf_pages));
eric@124 58
eric@124 59 pages->kids = pdf_new_obj (PT_ARRAY);
eric@124 60 /* The PDF 1.0 spec doesn't say that kids can't be an indirect object,
eric@124 61 but Acrobat 4.0 fails to optimize files if it is. */
eric@124 62
eric@59 63 pages->count = pdf_new_integer (0);
eric@59 64 pages->pages_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
eric@59 65 pdf_set_dict_entry (pages->pages_dict, "Type", pdf_new_name ("Pages"));
eric@59 66 pdf_set_dict_entry (pages->pages_dict, "Kids", pages->kids);
eric@59 67 pdf_set_dict_entry (pages->pages_dict, "Count", pages->count);
eric@59 68 return (pages);
eric@59 69 }
eric@59 70
eric@59 71
eric@75 72 pdf_file_handle pdf_create (char *filename, int page_mode)
eric@59 73 {
eric@59 74 pdf_file_handle pdf_file;
eric@75 75 char *page_mode_string;
eric@75 76
eric@75 77 switch (page_mode)
eric@75 78 {
eric@75 79 case PDF_PAGE_MODE_USE_NONE: page_mode_string = "UseNone"; break;
eric@75 80 case PDF_PAGE_MODE_USE_OUTLINES: page_mode_string = "UseOutlines"; break;
eric@75 81 case PDF_PAGE_MODE_USE_THUMBS: page_mode_string = "UseThumbs"; break;
eric@75 82 default:
eric@75 83 pdf_fatal ("invalid page mode\n");
eric@75 84 }
eric@59 85
eric@67 86 pdf_file = pdf_calloc (1, sizeof (struct pdf_file));
eric@59 87
eric@59 88 pdf_file->f = fopen (filename, "wb");
eric@59 89 if (! pdf_file->f)
eric@59 90 {
eric@59 91 pdf_fatal ("error opening output file\n");
eric@59 92 }
eric@59 93
eric@59 94 pdf_file->root = pdf_new_pages (pdf_file);
eric@59 95
eric@59 96 pdf_file->catalog = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
eric@59 97 pdf_set_dict_entry (pdf_file->catalog, "Type", pdf_new_name ("Catalog"));
eric@59 98 pdf_set_dict_entry (pdf_file->catalog, "Pages", pdf_file->root->pages_dict);
eric@75 99 /* Outlines dictionary will be created later if needed */
eric@75 100 pdf_set_dict_entry (pdf_file->catalog,
eric@75 101 "PageMode",
eric@75 102 pdf_new_name (page_mode_string));
eric@59 103
eric@59 104 pdf_file->info = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
eric@124 105 pdf_set_info (pdf_file, "Producer", "tumble by Eric Smith <eric@brouhaha.com>");
eric@59 106
eric@59 107 pdf_file->trailer_dict = pdf_new_obj (PT_DICTIONARY);
eric@59 108 /* Size key will be added later */
eric@59 109 pdf_set_dict_entry (pdf_file->trailer_dict, "Root", pdf_file->catalog);
eric@59 110 pdf_set_dict_entry (pdf_file->trailer_dict, "Info", pdf_file->info);
eric@59 111
eric@59 112 /* write file header */
eric@59 113 fprintf (pdf_file->f, "%%PDF-1.2\r\n");
eric@59 114
eric@59 115 return (pdf_file);
eric@59 116 }
eric@59 117
eric@59 118
eric@59 119 void pdf_close (pdf_file_handle pdf_file)
eric@59 120 {
eric@85 121 /* finalize all data structures */
eric@85 122 pdf_finalize_name_trees (pdf_file);
eric@85 123
eric@59 124 /* write body */
eric@59 125 pdf_write_all_ind_obj (pdf_file);
eric@59 126
eric@59 127 /* write cross reference table and get maximum object number */
eric@59 128 pdf_set_dict_entry (pdf_file->trailer_dict, "Size", pdf_new_integer (pdf_write_xref (pdf_file)));
eric@59 129
eric@59 130 /* write trailer */
eric@59 131 fprintf (pdf_file->f, "trailer\r\n");
eric@59 132 pdf_write_obj (pdf_file, pdf_file->trailer_dict);
eric@59 133 fprintf (pdf_file->f, "startxref\r\n");
eric@59 134 fprintf (pdf_file->f, "%ld\r\n", pdf_file->xref_offset);
eric@59 135 fprintf (pdf_file->f, "%%%%EOF\r\n");
eric@59 136
eric@59 137 fclose (pdf_file->f);
eric@59 138 /* should free stuff here */
eric@59 139 }
eric@59 140
eric@59 141
eric@59 142 void pdf_set_author (pdf_file_handle pdf_file, char *author)
eric@59 143 {
eric@59 144 pdf_set_info (pdf_file, "Author", author);
eric@59 145 }
eric@59 146
eric@59 147 void pdf_set_creator (pdf_file_handle pdf_file, char *creator)
eric@59 148 {
eric@59 149 pdf_set_info (pdf_file, "Creator", creator);
eric@59 150 }
eric@59 151
eric@59 152 void pdf_set_producer (pdf_file_handle pdf_file, char *producer)
eric@59 153 {
eric@59 154 pdf_set_info (pdf_file, "Producer", producer);
eric@59 155 }
eric@59 156
eric@59 157 void pdf_set_title (pdf_file_handle pdf_file, char *title)
eric@59 158 {
eric@59 159 pdf_set_info (pdf_file, "Title", title);
eric@59 160 }
eric@59 161
eric@59 162 void pdf_set_subject (pdf_file_handle pdf_file, char *subject)
eric@59 163 {
eric@59 164 pdf_set_info (pdf_file, "Subject", subject);
eric@59 165 }
eric@59 166
eric@59 167 void pdf_set_keywords (pdf_file_handle pdf_file, char *keywords)
eric@59 168 {
eric@59 169 pdf_set_info (pdf_file, "Keywords", keywords);
eric@59 170 }
eric@59 171
eric@59 172
eric@59 173 pdf_page_handle pdf_new_page (pdf_file_handle pdf_file,
eric@59 174 double width,
eric@59 175 double height)
eric@59 176 {
eric@67 177 pdf_page_handle page = pdf_calloc (1, sizeof (struct pdf_page));
eric@59 178
eric@59 179 page->pdf_file = pdf_file;
eric@59 180
eric@59 181 page->media_box = pdf_new_obj (PT_ARRAY);
eric@59 182 pdf_add_array_elem (page->media_box, pdf_new_real (0));
eric@59 183 pdf_add_array_elem (page->media_box, pdf_new_real (0));
eric@59 184 pdf_add_array_elem (page->media_box, pdf_new_real (width));
eric@59 185 pdf_add_array_elem (page->media_box, pdf_new_real (height));
eric@59 186
eric@59 187 page->procset = pdf_new_obj (PT_ARRAY);
eric@118 188 pdf_add_array_elem_unique (page->procset, pdf_new_name ("PDF"));
eric@59 189
eric@59 190 page->resources = pdf_new_obj (PT_DICTIONARY);
eric@59 191 pdf_set_dict_entry (page->resources, "ProcSet", page->procset);
eric@59 192
eric@59 193 page->page_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
eric@59 194 pdf_set_dict_entry (page->page_dict, "Type", pdf_new_name ("Page"));
eric@59 195 pdf_set_dict_entry (page->page_dict, "MediaBox", page->media_box);
eric@59 196 pdf_set_dict_entry (page->page_dict, "Resources", page->resources);
eric@59 197
eric@59 198 /* $$$ currently only support a single-level pages tree */
eric@59 199 pdf_set_dict_entry (page->page_dict, "Parent", pdf_file->root->pages_dict);
eric@59 200 pdf_add_array_elem (pdf_file->root->kids, page->page_dict);
eric@59 201 pdf_set_integer (pdf_file->root->count,
eric@59 202 pdf_get_integer (pdf_file->root->count) + 1);
eric@59 203
eric@59 204 page->last_XObject_name = '@'; /* first name will be "ImA" */
eric@59 205
eric@59 206 return (page);
eric@59 207 }
eric@59 208
eric@59 209 void pdf_close_page (pdf_page_handle pdf_page)
eric@59 210 {
eric@59 211 }
eric@59 212
eric@59 213
eric@59 214 void pdf_set_page_number (pdf_page_handle pdf_page, char *page_number)
eric@59 215 {
eric@59 216 }
eric@59 217
eric@59 218 void pdf_bookmark (pdf_page_handle pdf_page, int level, char *name)
eric@59 219 {
eric@59 220 }