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