Fri, 07 Mar 2003 11:35:36 +0000
more work on pdf_add_tree_element().
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.5 2003/03/04 18:09:49 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"
40 static void pdf_set_info (pdf_file_handle pdf_file, char *key, char *val)
41 {
42 if (! pdf_file->info)
43 pdf_file->info = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
45 pdf_set_dict_entry (pdf_file->info, key, pdf_new_string (val));
46 }
49 void pdf_init (void)
50 {
51 }
54 struct pdf_pages *pdf_new_pages (pdf_file_handle pdf_file)
55 {
56 struct pdf_pages *pages = pdf_calloc (1, sizeof (struct pdf_pages));
57 pages->kids = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_ARRAY));
58 pages->count = pdf_new_integer (0);
59 pages->pages_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
60 pdf_set_dict_entry (pages->pages_dict, "Type", pdf_new_name ("Pages"));
61 pdf_set_dict_entry (pages->pages_dict, "Kids", pages->kids);
62 pdf_set_dict_entry (pages->pages_dict, "Count", pages->count);
63 return (pages);
64 }
67 pdf_file_handle pdf_create (char *filename, int page_mode)
68 {
69 pdf_file_handle pdf_file;
70 char *page_mode_string;
72 switch (page_mode)
73 {
74 case PDF_PAGE_MODE_USE_NONE: page_mode_string = "UseNone"; break;
75 case PDF_PAGE_MODE_USE_OUTLINES: page_mode_string = "UseOutlines"; break;
76 case PDF_PAGE_MODE_USE_THUMBS: page_mode_string = "UseThumbs"; break;
77 default:
78 pdf_fatal ("invalid page mode\n");
79 }
81 pdf_file = pdf_calloc (1, sizeof (struct pdf_file));
83 pdf_file->f = fopen (filename, "wb");
84 if (! pdf_file->f)
85 {
86 pdf_fatal ("error opening output file\n");
87 }
89 pdf_file->root = pdf_new_pages (pdf_file);
91 pdf_file->catalog = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
92 pdf_set_dict_entry (pdf_file->catalog, "Type", pdf_new_name ("Catalog"));
93 pdf_set_dict_entry (pdf_file->catalog, "Pages", pdf_file->root->pages_dict);
94 /* Outlines dictionary will be created later if needed */
95 pdf_set_dict_entry (pdf_file->catalog,
96 "PageMode",
97 pdf_new_name (page_mode_string));
99 pdf_file->info = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
100 pdf_set_info (pdf_file, "Producer", "t2p, Copyright 2003 Eric Smith <eric@brouhaha.com>");
102 pdf_file->trailer_dict = pdf_new_obj (PT_DICTIONARY);
103 /* Size key will be added later */
104 pdf_set_dict_entry (pdf_file->trailer_dict, "Root", pdf_file->catalog);
105 pdf_set_dict_entry (pdf_file->trailer_dict, "Info", pdf_file->info);
107 /* write file header */
108 fprintf (pdf_file->f, "%%PDF-1.2\r\n");
110 return (pdf_file);
111 }
114 void pdf_close (pdf_file_handle pdf_file)
115 {
116 /* write body */
117 pdf_write_all_ind_obj (pdf_file);
119 /* write cross reference table and get maximum object number */
120 pdf_set_dict_entry (pdf_file->trailer_dict, "Size", pdf_new_integer (pdf_write_xref (pdf_file)));
122 /* write trailer */
123 fprintf (pdf_file->f, "trailer\r\n");
124 pdf_write_obj (pdf_file, pdf_file->trailer_dict);
125 fprintf (pdf_file->f, "startxref\r\n");
126 fprintf (pdf_file->f, "%ld\r\n", pdf_file->xref_offset);
127 fprintf (pdf_file->f, "%%%%EOF\r\n");
129 fclose (pdf_file->f);
130 /* should free stuff here */
131 }
134 void pdf_set_author (pdf_file_handle pdf_file, char *author)
135 {
136 pdf_set_info (pdf_file, "Author", author);
137 }
139 void pdf_set_creator (pdf_file_handle pdf_file, char *creator)
140 {
141 pdf_set_info (pdf_file, "Creator", creator);
142 }
144 void pdf_set_producer (pdf_file_handle pdf_file, char *producer)
145 {
146 pdf_set_info (pdf_file, "Producer", producer);
147 }
149 void pdf_set_title (pdf_file_handle pdf_file, char *title)
150 {
151 pdf_set_info (pdf_file, "Title", title);
152 }
154 void pdf_set_subject (pdf_file_handle pdf_file, char *subject)
155 {
156 pdf_set_info (pdf_file, "Subject", subject);
157 }
159 void pdf_set_keywords (pdf_file_handle pdf_file, char *keywords)
160 {
161 pdf_set_info (pdf_file, "Keywords", keywords);
162 }
165 pdf_page_handle pdf_new_page (pdf_file_handle pdf_file,
166 double width,
167 double height)
168 {
169 pdf_page_handle page = pdf_calloc (1, sizeof (struct pdf_page));
171 page->pdf_file = pdf_file;
173 page->media_box = pdf_new_obj (PT_ARRAY);
174 pdf_add_array_elem (page->media_box, pdf_new_real (0));
175 pdf_add_array_elem (page->media_box, pdf_new_real (0));
176 pdf_add_array_elem (page->media_box, pdf_new_real (width));
177 pdf_add_array_elem (page->media_box, pdf_new_real (height));
179 page->procset = pdf_new_obj (PT_ARRAY);
180 pdf_add_array_elem (page->procset, pdf_new_name ("PDF"));
182 page->resources = pdf_new_obj (PT_DICTIONARY);
183 pdf_set_dict_entry (page->resources, "ProcSet", page->procset);
185 page->page_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
186 pdf_set_dict_entry (page->page_dict, "Type", pdf_new_name ("Page"));
187 pdf_set_dict_entry (page->page_dict, "MediaBox", page->media_box);
188 pdf_set_dict_entry (page->page_dict, "Resources", page->resources);
190 /* $$$ currently only support a single-level pages tree */
191 pdf_set_dict_entry (page->page_dict, "Parent", pdf_file->root->pages_dict);
192 pdf_add_array_elem (pdf_file->root->kids, page->page_dict);
193 pdf_set_integer (pdf_file->root->count,
194 pdf_get_integer (pdf_file->root->count) + 1);
196 page->last_XObject_name = '@'; /* first name will be "ImA" */
198 return (page);
199 }
201 void pdf_close_page (pdf_page_handle pdf_page)
202 {
203 }
206 void pdf_set_page_number (pdf_page_handle pdf_page, char *page_number)
207 {
208 }
210 void pdf_bookmark (pdf_page_handle pdf_page, int level, char *name)
211 {
212 }