forked from brouhaha/tumble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf.c
268 lines (207 loc) · 7.97 KB
/
pdf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* tumble: build a PDF file from image files
*
* PDF routines
* Copyright 2001, 2002, 2003, 2017 Eric Smith <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. Note that permission is
* not granted to redistribute this program under the terms of any
* other version of the General Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*
* 2005-05-03 [JDB] Add CreationDate and ModDate PDF headers.
* 2014-02-18 [JDB] Use PDF_PRODUCER definition.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "bitblt.h"
#include "pdf.h"
#include "pdf_util.h"
#include "pdf_prim.h"
#include "pdf_private.h"
#include "pdf_name_tree.h"
static void pdf_set_info (pdf_file_handle pdf_file, char *key, char *val)
{
if (! pdf_file->info)
pdf_file->info = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
pdf_set_dict_entry (pdf_file->info, key, pdf_new_string (val));
}
void pdf_init (void)
{
}
struct pdf_pages *pdf_new_pages (pdf_file_handle pdf_file)
{
struct pdf_pages *pages = pdf_calloc (1, sizeof (struct pdf_pages));
pages->kids = pdf_new_obj (PT_ARRAY);
/* The PDF 1.0 spec doesn't say that kids can't be an indirect object,
but Acrobat 4.0 fails to optimize files if it is. */
pages->count = pdf_new_integer (0);
pages->pages_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
pdf_set_dict_entry (pages->pages_dict, "Type", pdf_new_name ("Pages"));
pdf_set_dict_entry (pages->pages_dict, "Kids", pages->kids);
pdf_set_dict_entry (pages->pages_dict, "Count", pages->count);
return (pages);
}
pdf_file_handle pdf_create (char *filename)
{
pdf_file_handle pdf_file;
time_t current_time, adjusted_time;
struct tm *time_and_date;
int gmt_diff;
char tm_string[18], gmt_string[25];
const char tm_format[] = "D:%Y%m%d%H%M%S";
pdf_file = pdf_calloc (1, sizeof (struct pdf_file));
pdf_file->f = fopen (filename, "wb");
if (! pdf_file->f)
{
pdf_fatal ("error opening output file\n");
}
pdf_file->root = pdf_new_pages (pdf_file);
pdf_file->catalog = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
pdf_set_dict_entry (pdf_file->catalog, "Type", pdf_new_name ("Catalog"));
pdf_set_dict_entry (pdf_file->catalog, "Pages", pdf_file->root->pages_dict);
/* Outlines dictionary will be created later if needed */
pdf_set_dict_entry (pdf_file->catalog, "PageLayout", pdf_new_name ("SinglePage"));
pdf_file->info = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
pdf_set_info (pdf_file, "Producer", PDF_PRODUCER);
/* Generate CreationDate and ModDate */
current_time = time (NULL);
time_and_date = gmtime (¤t_time);
adjusted_time = mktime (time_and_date);
gmt_diff = (int) difftime (current_time, adjusted_time);
time_and_date = localtime (¤t_time);
if (strftime (tm_string, sizeof (tm_string), tm_format, time_and_date))
{
sprintf (gmt_string, "%s%+03d'%02d'", tm_string, gmt_diff / 3600, gmt_diff % 60);
pdf_set_info (pdf_file, "CreationDate", gmt_string);
pdf_set_info (pdf_file, "ModDate", gmt_string);
}
pdf_file->trailer_dict = pdf_new_obj (PT_DICTIONARY);
/* Size key will be added later */
pdf_set_dict_entry (pdf_file->trailer_dict, "Root", pdf_file->catalog);
pdf_set_dict_entry (pdf_file->trailer_dict, "Info", pdf_file->info);
/* write file header */
fprintf (pdf_file->f, "%%PDF-1.3\r\n");
/* write comment containing 8-bit chars as a hint that the file is binary */
/* PDF 1.4 spec, section 3.4.1 */
fprintf (pdf_file->f, "%%\342\343\317\323\r\n");
return (pdf_file);
}
void pdf_close (pdf_file_handle pdf_file, int page_mode)
{
char *page_mode_string;
page_mode_string = "UseNone";
switch (page_mode)
{
case PDF_PAGE_MODE_USE_NONE:
break;
case PDF_PAGE_MODE_USE_OUTLINES:
if (pdf_file->outline_root)
page_mode_string = "UseOutlines";
break;
case PDF_PAGE_MODE_USE_THUMBS:
page_mode_string = "UseThumbs";
break;
default:
pdf_fatal ("invalid page mode\n");
}
pdf_set_dict_entry (pdf_file->catalog,
"PageMode",
pdf_new_name (page_mode_string));
/* finalize trees, object numbers aren't allocated until this step */
pdf_finalize_name_trees (pdf_file);
/* add the page label number tree, if it exists, to the catalog */
if (pdf_file->page_label_tree)
pdf_set_dict_entry (pdf_file->catalog,
"PageLabels",
pdf_file->page_label_tree->root->dict);
/* write body */
pdf_write_all_ind_obj (pdf_file);
/* write cross reference table and get maximum object number */
pdf_set_dict_entry (pdf_file->trailer_dict, "Size", pdf_new_integer (pdf_write_xref (pdf_file)));
/* write trailer */
fprintf (pdf_file->f, "trailer\r\n");
pdf_write_obj (pdf_file, pdf_file->trailer_dict);
fprintf (pdf_file->f, "startxref\r\n");
fprintf (pdf_file->f, "%ld\r\n", pdf_file->xref_offset);
fprintf (pdf_file->f, "%%%%EOF\r\n");
fclose (pdf_file->f);
/* should free stuff here */
}
void pdf_set_author (pdf_file_handle pdf_file, char *author)
{
pdf_set_info (pdf_file, "Author", author);
}
void pdf_set_creator (pdf_file_handle pdf_file, char *creator)
{
pdf_set_info (pdf_file, "Creator", creator);
}
void pdf_set_producer (pdf_file_handle pdf_file, char *producer)
{
pdf_set_info (pdf_file, "Producer", producer);
}
void pdf_set_title (pdf_file_handle pdf_file, char *title)
{
pdf_set_info (pdf_file, "Title", title);
}
void pdf_set_subject (pdf_file_handle pdf_file, char *subject)
{
pdf_set_info (pdf_file, "Subject", subject);
}
void pdf_set_keywords (pdf_file_handle pdf_file, char *keywords)
{
pdf_set_info (pdf_file, "Keywords", keywords);
}
pdf_page_handle pdf_new_page (pdf_file_handle pdf_file,
double width,
double height)
{
pdf_page_handle page = pdf_calloc (1, sizeof (struct pdf_page));
page->pdf_file = pdf_file;
page->media_box = pdf_new_obj (PT_ARRAY);
pdf_add_array_elem (page->media_box, pdf_new_real (0));
pdf_add_array_elem (page->media_box, pdf_new_real (0));
pdf_add_array_elem (page->media_box, pdf_new_real (width));
pdf_add_array_elem (page->media_box, pdf_new_real (height));
page->procset = pdf_new_obj (PT_ARRAY);
pdf_add_array_elem_unique (page->procset, pdf_new_name ("PDF"));
page->resources = pdf_new_obj (PT_DICTIONARY);
pdf_set_dict_entry (page->resources, "ProcSet", page->procset);
page->page_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
pdf_set_dict_entry (page->page_dict, "Type", pdf_new_name ("Page"));
pdf_set_dict_entry (page->page_dict, "MediaBox", page->media_box);
pdf_set_dict_entry (page->page_dict, "Resources", page->resources);
if (pdf_get_integer (pdf_file->root->count) == 0)
{
pdf_obj_handle dest_array = pdf_new_obj (PT_ARRAY);
pdf_add_array_elem (dest_array, page->page_dict);
pdf_add_array_elem (dest_array, pdf_new_name ("Fit"));
pdf_set_dict_entry (pdf_file->catalog, "OpenAction", dest_array);
}
/* $$$ currently only support a single-level pages tree */
pdf_set_dict_entry (page->page_dict, "Parent", pdf_file->root->pages_dict);
pdf_add_array_elem (pdf_file->root->kids, page->page_dict);
pdf_set_integer (pdf_file->root->count,
pdf_get_integer (pdf_file->root->count) + 1);
page->last_XObject_name = '@'; /* first name will be "ImA" */
return (page);
}
void pdf_close_page (pdf_page_handle pdf_page)
{
}
void pdf_set_page_number (pdf_page_handle pdf_page, char *page_number)
{
}