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