pdf.c

Mon, 14 Dec 2009 16:18:21 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 14 Dec 2009 16:18:21 +0000
changeset 172
2fae6df568f6
parent 133
76c197fe2eeb
permissions
-rw-r--r--

remove erroneous 0.33-philpem1 tag

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