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