Thu, 13 Mar 2003 07:59:10 +0000
don't use page mode USE_OUTLINES if there are no outline entries.
eric@62 | 1 | /* |
eric@62 | 2 | * t2p: Create a PDF file from the contents of one or more TIFF |
eric@62 | 3 | * bilevel image files. The images in the resulting PDF file |
eric@62 | 4 | * will be compressed using ITU-T T.6 (G4) fax encoding. |
eric@62 | 5 | * |
eric@62 | 6 | * PDF routines |
eric@118 | 7 | * $Id: pdf_prim.c,v 1.10 2003/03/12 22:56:57 eric Exp $ |
eric@62 | 8 | * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> |
eric@62 | 9 | * |
eric@62 | 10 | * This program is free software; you can redistribute it and/or modify |
eric@62 | 11 | * it under the terms of the GNU General Public License version 2 as |
eric@62 | 12 | * published by the Free Software Foundation. Note that permission is |
eric@62 | 13 | * not granted to redistribute this program under the terms of any |
eric@62 | 14 | * other version of the General Public License. |
eric@62 | 15 | * |
eric@62 | 16 | * This program is distributed in the hope that it will be useful, |
eric@62 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
eric@62 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
eric@62 | 19 | * GNU General Public License for more details. |
eric@62 | 20 | * |
eric@62 | 21 | * You should have received a copy of the GNU General Public License |
eric@62 | 22 | * along with this program; if not, write to the Free Software |
eric@62 | 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA |
eric@62 | 24 | */ |
eric@62 | 25 | |
eric@62 | 26 | |
eric@66 | 27 | #include <stdarg.h> |
eric@62 | 28 | #include <stdbool.h> |
eric@62 | 29 | #include <stdint.h> |
eric@59 | 30 | #include <stdio.h> |
eric@59 | 31 | #include <stdlib.h> |
eric@59 | 32 | #include <string.h> |
eric@59 | 33 | |
eric@62 | 34 | #include "bitblt.h" |
eric@60 | 35 | #include "pdf.h" |
eric@60 | 36 | #include "pdf_util.h" |
eric@60 | 37 | #include "pdf_prim.h" |
eric@60 | 38 | #include "pdf_private.h" |
eric@59 | 39 | |
eric@59 | 40 | |
eric@59 | 41 | struct pdf_array_elem |
eric@59 | 42 | { |
eric@59 | 43 | struct pdf_array_elem *next; |
eric@59 | 44 | struct pdf_obj *val; |
eric@59 | 45 | }; |
eric@59 | 46 | |
eric@59 | 47 | |
eric@59 | 48 | struct pdf_array |
eric@59 | 49 | { |
eric@59 | 50 | struct pdf_array_elem *first; |
eric@59 | 51 | struct pdf_array_elem *last; |
eric@59 | 52 | }; |
eric@59 | 53 | |
eric@59 | 54 | |
eric@59 | 55 | struct pdf_dict_entry |
eric@59 | 56 | { |
eric@59 | 57 | struct pdf_dict_entry *next; |
eric@59 | 58 | char *key; |
eric@59 | 59 | struct pdf_obj *val; |
eric@59 | 60 | }; |
eric@59 | 61 | |
eric@59 | 62 | |
eric@59 | 63 | struct pdf_dict |
eric@59 | 64 | { |
eric@59 | 65 | struct pdf_dict_entry *first; |
eric@59 | 66 | }; |
eric@59 | 67 | |
eric@59 | 68 | |
eric@59 | 69 | struct pdf_stream |
eric@59 | 70 | { |
eric@59 | 71 | struct pdf_obj *stream_dict; |
eric@59 | 72 | struct pdf_obj *length; |
eric@59 | 73 | pdf_stream_write_callback callback; |
eric@59 | 74 | void *app_data; /* arg to pass to callback */ |
eric@59 | 75 | struct pdf_obj *filters; /* name or array of names */ |
eric@59 | 76 | struct pdf_obj *decode_parms; |
eric@59 | 77 | }; |
eric@59 | 78 | |
eric@59 | 79 | |
eric@59 | 80 | struct pdf_obj |
eric@59 | 81 | { |
eric@59 | 82 | /* these fields only apply to indirectly referenced objects */ |
eric@59 | 83 | struct pdf_obj *prev; |
eric@59 | 84 | struct pdf_obj *next; |
eric@59 | 85 | unsigned long obj_num; |
eric@59 | 86 | unsigned long obj_gen; |
eric@59 | 87 | long int file_offset; |
eric@59 | 88 | |
eric@59 | 89 | /* these fields apply to all objects */ |
eric@59 | 90 | unsigned long ref_count; |
eric@59 | 91 | pdf_obj_type type; |
eric@59 | 92 | union { |
eric@62 | 93 | bool boolean; |
eric@59 | 94 | char *name; |
eric@59 | 95 | char *string; |
eric@74 | 96 | long integer; |
eric@59 | 97 | double real; |
eric@59 | 98 | struct pdf_obj *ind_ref; |
eric@59 | 99 | struct pdf_dict dict; |
eric@59 | 100 | struct pdf_array array; |
eric@59 | 101 | struct pdf_stream stream; |
eric@59 | 102 | } val; |
eric@59 | 103 | }; |
eric@59 | 104 | |
eric@59 | 105 | |
eric@59 | 106 | struct pdf_obj *ref (struct pdf_obj *obj) |
eric@59 | 107 | { |
eric@59 | 108 | obj->ref_count++; |
eric@59 | 109 | return (obj); |
eric@59 | 110 | } |
eric@59 | 111 | |
eric@59 | 112 | |
eric@59 | 113 | void unref (struct pdf_obj *obj) |
eric@59 | 114 | { |
eric@59 | 115 | if ((--obj->ref_count) == 0) |
eric@59 | 116 | { |
eric@59 | 117 | /* $$$ free the object */ |
eric@59 | 118 | } |
eric@59 | 119 | } |
eric@59 | 120 | |
eric@59 | 121 | |
eric@59 | 122 | struct pdf_obj *pdf_deref_ind_obj (struct pdf_obj *ind_obj) |
eric@59 | 123 | { |
eric@59 | 124 | pdf_assert (ind_obj->type == PT_IND_REF); |
eric@59 | 125 | return (ind_obj->val.ind_ref); |
eric@59 | 126 | } |
eric@59 | 127 | |
eric@59 | 128 | |
eric@59 | 129 | void pdf_set_dict_entry (struct pdf_obj *dict_obj, char *key, struct pdf_obj *val) |
eric@59 | 130 | { |
eric@59 | 131 | struct pdf_dict_entry *entry; |
eric@59 | 132 | |
eric@59 | 133 | if (dict_obj->type == PT_IND_REF) |
eric@59 | 134 | dict_obj = pdf_deref_ind_obj (dict_obj); |
eric@59 | 135 | |
eric@59 | 136 | pdf_assert (dict_obj->type == PT_DICTIONARY); |
eric@59 | 137 | |
eric@59 | 138 | /* replacing existing entry? */ |
eric@59 | 139 | for (entry = dict_obj->val.dict.first; entry; entry = entry->next) |
eric@59 | 140 | if (strcmp (entry->key, key) == 0) |
eric@59 | 141 | { |
eric@59 | 142 | unref (entry->val); |
eric@59 | 143 | entry->val = ref (val); |
eric@59 | 144 | return; |
eric@59 | 145 | } |
eric@59 | 146 | |
eric@59 | 147 | /* new entry */ |
eric@67 | 148 | entry = pdf_calloc (1, sizeof (struct pdf_dict_entry)); |
eric@59 | 149 | |
eric@59 | 150 | entry->next = dict_obj->val.dict.first; |
eric@59 | 151 | dict_obj->val.dict.first = entry; |
eric@59 | 152 | |
eric@59 | 153 | entry->key = pdf_strdup (key); |
eric@59 | 154 | entry->val = ref (val); |
eric@59 | 155 | } |
eric@59 | 156 | |
eric@59 | 157 | |
eric@59 | 158 | struct pdf_obj *pdf_get_dict_entry (struct pdf_obj *dict_obj, char *key) |
eric@59 | 159 | { |
eric@59 | 160 | struct pdf_dict_entry *entry; |
eric@59 | 161 | |
eric@59 | 162 | if (dict_obj->type == PT_IND_REF) |
eric@59 | 163 | dict_obj = pdf_deref_ind_obj (dict_obj); |
eric@59 | 164 | |
eric@59 | 165 | pdf_assert (dict_obj->type == PT_DICTIONARY); |
eric@59 | 166 | |
eric@59 | 167 | for (entry = dict_obj->val.dict.first; entry; entry = entry->next) |
eric@59 | 168 | if (strcmp (entry->key, key) == 0) |
eric@59 | 169 | return (entry->val); |
eric@59 | 170 | |
eric@59 | 171 | return (NULL); |
eric@59 | 172 | } |
eric@59 | 173 | |
eric@59 | 174 | |
eric@59 | 175 | void pdf_add_array_elem (struct pdf_obj *array_obj, struct pdf_obj *val) |
eric@59 | 176 | { |
eric@67 | 177 | struct pdf_array_elem *elem = pdf_calloc (1, sizeof (struct pdf_array_elem)); |
eric@59 | 178 | |
eric@59 | 179 | if (array_obj->type == PT_IND_REF) |
eric@59 | 180 | array_obj = pdf_deref_ind_obj (array_obj); |
eric@59 | 181 | |
eric@59 | 182 | pdf_assert (array_obj->type == PT_ARRAY); |
eric@59 | 183 | |
eric@59 | 184 | elem->val = ref (val); |
eric@59 | 185 | |
eric@59 | 186 | if (! array_obj->val.array.first) |
eric@59 | 187 | array_obj->val.array.first = elem; |
eric@59 | 188 | else |
eric@59 | 189 | array_obj->val.array.last->next = elem; |
eric@59 | 190 | |
eric@59 | 191 | array_obj->val.array.last = elem; |
eric@59 | 192 | } |
eric@59 | 193 | |
eric@59 | 194 | |
eric@118 | 195 | void pdf_add_array_elem_unique (struct pdf_obj *array_obj, struct pdf_obj *val) |
eric@118 | 196 | { |
eric@118 | 197 | struct pdf_array_elem *elem; |
eric@118 | 198 | |
eric@118 | 199 | if (array_obj->type == PT_IND_REF) |
eric@118 | 200 | array_obj = pdf_deref_ind_obj (array_obj); |
eric@118 | 201 | |
eric@118 | 202 | pdf_assert (array_obj->type == PT_ARRAY); |
eric@118 | 203 | |
eric@118 | 204 | for (elem = array_obj->val.array.first; elem; elem = elem->next) |
eric@118 | 205 | if (pdf_compare_obj (val, elem->val) == 0) |
eric@118 | 206 | return; |
eric@118 | 207 | |
eric@118 | 208 | elem = pdf_calloc (1, sizeof (struct pdf_array_elem)); |
eric@118 | 209 | |
eric@118 | 210 | elem->val = ref (val); |
eric@118 | 211 | |
eric@118 | 212 | if (! array_obj->val.array.first) |
eric@118 | 213 | array_obj->val.array.first = elem; |
eric@118 | 214 | else |
eric@118 | 215 | array_obj->val.array.last->next = elem; |
eric@118 | 216 | |
eric@118 | 217 | array_obj->val.array.last = elem; |
eric@118 | 218 | } |
eric@118 | 219 | |
eric@118 | 220 | |
eric@59 | 221 | struct pdf_obj *pdf_new_obj (pdf_obj_type type) |
eric@59 | 222 | { |
eric@67 | 223 | struct pdf_obj *obj = pdf_calloc (1, sizeof (struct pdf_obj)); |
eric@59 | 224 | obj->type = type; |
eric@59 | 225 | return (obj); |
eric@59 | 226 | } |
eric@59 | 227 | |
eric@59 | 228 | |
eric@62 | 229 | struct pdf_obj *pdf_new_bool (bool val) |
eric@59 | 230 | { |
eric@59 | 231 | struct pdf_obj *obj = pdf_new_obj (PT_BOOL); |
eric@62 | 232 | obj->val.boolean = val; |
eric@59 | 233 | return (obj); |
eric@59 | 234 | } |
eric@59 | 235 | |
eric@59 | 236 | |
eric@59 | 237 | struct pdf_obj *pdf_new_name (char *name) |
eric@59 | 238 | { |
eric@59 | 239 | struct pdf_obj *obj = pdf_new_obj (PT_NAME); |
eric@59 | 240 | obj->val.name = pdf_strdup (name); |
eric@59 | 241 | return (obj); |
eric@59 | 242 | } |
eric@59 | 243 | |
eric@59 | 244 | |
eric@59 | 245 | struct pdf_obj *pdf_new_string (char *str) |
eric@59 | 246 | { |
eric@59 | 247 | struct pdf_obj *obj = pdf_new_obj (PT_STRING); |
eric@59 | 248 | obj->val.string = pdf_strdup (str); |
eric@59 | 249 | return (obj); |
eric@59 | 250 | } |
eric@59 | 251 | |
eric@59 | 252 | |
eric@74 | 253 | struct pdf_obj *pdf_new_integer (long val) |
eric@59 | 254 | { |
eric@59 | 255 | struct pdf_obj *obj = pdf_new_obj (PT_INTEGER); |
eric@59 | 256 | obj->val.integer = val; |
eric@59 | 257 | return (obj); |
eric@59 | 258 | } |
eric@59 | 259 | |
eric@59 | 260 | |
eric@59 | 261 | struct pdf_obj *pdf_new_real (double val) |
eric@59 | 262 | { |
eric@59 | 263 | struct pdf_obj *obj = pdf_new_obj (PT_REAL); |
eric@59 | 264 | obj->val.real = val; |
eric@59 | 265 | return (obj); |
eric@59 | 266 | } |
eric@59 | 267 | |
eric@59 | 268 | |
eric@59 | 269 | struct pdf_obj *pdf_new_stream (pdf_file_handle pdf_file, |
eric@59 | 270 | struct pdf_obj *stream_dict, |
eric@59 | 271 | pdf_stream_write_callback callback, |
eric@59 | 272 | void *app_data) |
eric@59 | 273 | { |
eric@59 | 274 | struct pdf_obj *obj = pdf_new_obj (PT_STREAM); |
eric@59 | 275 | |
eric@59 | 276 | obj->val.stream.stream_dict = stream_dict; |
eric@59 | 277 | obj->val.stream.length = pdf_new_ind_ref (pdf_file, pdf_new_integer (0)); |
eric@59 | 278 | pdf_set_dict_entry (obj->val.stream.stream_dict, "Length", obj->val.stream.length); |
eric@59 | 279 | |
eric@59 | 280 | obj->val.stream.callback = callback; |
eric@59 | 281 | obj->val.stream.app_data = app_data; |
eric@59 | 282 | return (obj); |
eric@59 | 283 | } |
eric@59 | 284 | |
eric@59 | 285 | |
eric@59 | 286 | /* $$$ currently limited to one filter per stream */ |
eric@59 | 287 | void pdf_stream_add_filter (struct pdf_obj *stream, |
eric@59 | 288 | char *filter_name, |
eric@59 | 289 | struct pdf_obj *decode_parms) |
eric@59 | 290 | { |
eric@59 | 291 | if (stream->type == PT_IND_REF) |
eric@59 | 292 | stream = pdf_deref_ind_obj (stream); |
eric@59 | 293 | |
eric@59 | 294 | pdf_assert (stream->type == PT_STREAM); |
eric@59 | 295 | |
eric@59 | 296 | pdf_set_dict_entry (stream->val.stream.stream_dict, "Filter", pdf_new_name (filter_name)); |
eric@59 | 297 | if (decode_parms) |
eric@59 | 298 | pdf_set_dict_entry (stream->val.stream.stream_dict, "DecodeParms", decode_parms); |
eric@59 | 299 | } |
eric@59 | 300 | |
eric@59 | 301 | |
eric@59 | 302 | struct pdf_obj *pdf_new_ind_ref (pdf_file_handle pdf_file, struct pdf_obj *obj) |
eric@59 | 303 | { |
eric@59 | 304 | struct pdf_obj *ind_obj; |
eric@59 | 305 | |
eric@59 | 306 | pdf_assert (obj->type != PT_IND_REF); |
eric@59 | 307 | |
eric@59 | 308 | ind_obj = pdf_new_obj (PT_IND_REF); |
eric@59 | 309 | |
eric@59 | 310 | ind_obj->type = PT_IND_REF; |
eric@59 | 311 | ind_obj->val.ind_ref = obj; |
eric@59 | 312 | |
eric@59 | 313 | /* is there already an indirect reference to this object? */ |
eric@59 | 314 | if (! obj->obj_num) |
eric@59 | 315 | { |
eric@59 | 316 | /* no, assign object number/generation and add to linked list */ |
eric@59 | 317 | if (! pdf_file->first_ind_obj) |
eric@59 | 318 | { |
eric@59 | 319 | obj->obj_num = 1; |
eric@59 | 320 | pdf_file->first_ind_obj = pdf_file->last_ind_obj = obj; |
eric@59 | 321 | } |
eric@59 | 322 | else |
eric@59 | 323 | { |
eric@59 | 324 | obj->obj_num = pdf_file->last_ind_obj->obj_num + 1; |
eric@59 | 325 | pdf_file->last_ind_obj->next = obj; |
eric@59 | 326 | obj->prev = pdf_file->last_ind_obj; |
eric@59 | 327 | pdf_file->last_ind_obj = obj; |
eric@59 | 328 | } |
eric@59 | 329 | } |
eric@59 | 330 | |
eric@59 | 331 | return (ind_obj); |
eric@59 | 332 | } |
eric@59 | 333 | |
eric@59 | 334 | |
eric@74 | 335 | long pdf_get_integer (struct pdf_obj *obj) |
eric@59 | 336 | { |
eric@59 | 337 | if (obj->type == PT_IND_REF) |
eric@59 | 338 | obj = pdf_deref_ind_obj (obj); |
eric@59 | 339 | |
eric@59 | 340 | pdf_assert (obj->type == PT_INTEGER); |
eric@59 | 341 | |
eric@59 | 342 | return (obj->val.integer); |
eric@59 | 343 | } |
eric@59 | 344 | |
eric@74 | 345 | void pdf_set_integer (struct pdf_obj *obj, long val) |
eric@59 | 346 | { |
eric@59 | 347 | if (obj->type == PT_IND_REF) |
eric@59 | 348 | obj = pdf_deref_ind_obj (obj); |
eric@59 | 349 | |
eric@59 | 350 | pdf_assert (obj->type == PT_INTEGER); |
eric@59 | 351 | |
eric@59 | 352 | obj->val.integer = val; |
eric@59 | 353 | } |
eric@59 | 354 | |
eric@59 | 355 | |
eric@59 | 356 | double pdf_get_real (struct pdf_obj *obj) |
eric@59 | 357 | { |
eric@59 | 358 | if (obj->type == PT_IND_REF) |
eric@59 | 359 | obj = pdf_deref_ind_obj (obj); |
eric@59 | 360 | |
eric@59 | 361 | pdf_assert (obj->type == PT_REAL); |
eric@59 | 362 | |
eric@59 | 363 | return (obj->val.real); |
eric@59 | 364 | } |
eric@59 | 365 | |
eric@59 | 366 | void pdf_set_real (struct pdf_obj *obj, double val) |
eric@59 | 367 | { |
eric@59 | 368 | if (obj->type == PT_IND_REF) |
eric@59 | 369 | obj = pdf_deref_ind_obj (obj); |
eric@59 | 370 | |
eric@59 | 371 | pdf_assert (obj->type == PT_REAL); |
eric@59 | 372 | |
eric@59 | 373 | obj->val.real = val; |
eric@59 | 374 | } |
eric@59 | 375 | |
eric@59 | 376 | |
eric@82 | 377 | int pdf_compare_obj (struct pdf_obj *o1, struct pdf_obj *o2) |
eric@82 | 378 | { |
eric@82 | 379 | if (o1->type == PT_IND_REF) |
eric@82 | 380 | o1 = pdf_deref_ind_obj (o1); |
eric@82 | 381 | |
eric@82 | 382 | if (o2->type == PT_IND_REF) |
eric@82 | 383 | o2 = pdf_deref_ind_obj (o2); |
eric@82 | 384 | |
eric@82 | 385 | pdf_assert (o1->type == o2->type); |
eric@82 | 386 | |
eric@82 | 387 | switch (o1->type) |
eric@82 | 388 | { |
eric@82 | 389 | case PT_INTEGER: |
eric@82 | 390 | if (o1->val.integer < o2->val.integer) |
eric@82 | 391 | return (-1); |
eric@82 | 392 | if (o1->val.integer > o2->val.integer) |
eric@82 | 393 | return (1); |
eric@82 | 394 | return (0); |
eric@82 | 395 | case PT_REAL: |
eric@82 | 396 | if (o1->val.real < o2->val.real) |
eric@82 | 397 | return (-1); |
eric@82 | 398 | if (o1->val.real > o2->val.real) |
eric@82 | 399 | return (1); |
eric@82 | 400 | return (0); |
eric@82 | 401 | case PT_STRING: |
eric@82 | 402 | return (strcmp (o1->val.string, o2->val.string)); |
eric@118 | 403 | case PT_NAME: |
eric@118 | 404 | return (strcmp (o1->val.name, o2->val.name)); |
eric@82 | 405 | default: |
eric@82 | 406 | pdf_fatal ("invalid object type for comparison\n"); |
eric@82 | 407 | } |
eric@82 | 408 | } |
eric@82 | 409 | |
eric@82 | 410 | |
eric@59 | 411 | static int name_char_needs_quoting (char c) |
eric@59 | 412 | { |
eric@59 | 413 | return ((c < '!') || (c > '~') || (c == '/') || (c == '\\') || |
eric@59 | 414 | (c == '(') || (c == ')') || (c == '<') || (c == '>') || |
eric@59 | 415 | (c == '[') || (c == ']') || (c == '{') || (c == '}') || |
eric@59 | 416 | (c == '%')); |
eric@59 | 417 | } |
eric@59 | 418 | |
eric@59 | 419 | |
eric@59 | 420 | void pdf_write_name (pdf_file_handle pdf_file, char *s) |
eric@59 | 421 | { |
eric@59 | 422 | fprintf (pdf_file->f, "/"); |
eric@59 | 423 | while (*s) |
eric@59 | 424 | if (name_char_needs_quoting (*s)) |
eric@59 | 425 | fprintf (pdf_file->f, "#%02x", 0xff & *(s++)); |
eric@59 | 426 | else |
eric@59 | 427 | fprintf (pdf_file->f, "%c", *(s++)); |
eric@59 | 428 | fprintf (pdf_file->f, " "); |
eric@59 | 429 | } |
eric@59 | 430 | |
eric@59 | 431 | |
eric@59 | 432 | static int string_char_needs_quoting (char c) |
eric@59 | 433 | { |
eric@59 | 434 | return ((c < ' ') || (c > '~') || (c == '\\') || |
eric@59 | 435 | (c == '(') || (c == ')')); |
eric@59 | 436 | } |
eric@59 | 437 | |
eric@59 | 438 | |
eric@59 | 439 | void pdf_write_string (pdf_file_handle pdf_file, char *s) |
eric@59 | 440 | { |
eric@59 | 441 | fprintf (pdf_file->f, "("); |
eric@59 | 442 | while (*s) |
eric@59 | 443 | if (string_char_needs_quoting (*s)) |
eric@59 | 444 | fprintf (pdf_file->f, "\\%03o", 0xff & *(s++)); |
eric@59 | 445 | else |
eric@59 | 446 | fprintf (pdf_file->f, "%c", *(s++)); |
eric@59 | 447 | fprintf (pdf_file->f, ") "); |
eric@59 | 448 | } |
eric@59 | 449 | |
eric@59 | 450 | |
eric@59 | 451 | void pdf_write_real (pdf_file_handle pdf_file, double num) |
eric@59 | 452 | { |
eric@59 | 453 | /* $$$ not actually good enough, precision needs to be variable, |
eric@59 | 454 | and no exponent is allowed */ |
eric@59 | 455 | fprintf (pdf_file->f, "%0f ", num); |
eric@59 | 456 | } |
eric@59 | 457 | |
eric@59 | 458 | |
eric@59 | 459 | void pdf_write_ind_ref (pdf_file_handle pdf_file, struct pdf_obj *ind_obj) |
eric@59 | 460 | { |
eric@59 | 461 | struct pdf_obj *obj = pdf_deref_ind_obj (ind_obj); |
eric@59 | 462 | fprintf (pdf_file->f, "%ld %ld R ", obj->obj_num, obj->obj_gen); |
eric@59 | 463 | } |
eric@59 | 464 | |
eric@59 | 465 | |
eric@59 | 466 | void pdf_write_array (pdf_file_handle pdf_file, struct pdf_obj *array_obj) |
eric@59 | 467 | { |
eric@59 | 468 | struct pdf_array_elem *elem; |
eric@59 | 469 | |
eric@59 | 470 | pdf_assert (array_obj->type == PT_ARRAY); |
eric@59 | 471 | |
eric@59 | 472 | fprintf (pdf_file->f, "[ "); |
eric@59 | 473 | for (elem = array_obj->val.array.first; elem; elem = elem->next) |
eric@59 | 474 | { |
eric@59 | 475 | pdf_write_obj (pdf_file, elem->val); |
eric@59 | 476 | fprintf (pdf_file->f, " "); |
eric@59 | 477 | } |
eric@59 | 478 | fprintf (pdf_file->f, "] "); |
eric@59 | 479 | } |
eric@59 | 480 | |
eric@59 | 481 | |
eric@59 | 482 | void pdf_write_dict (pdf_file_handle pdf_file, struct pdf_obj *dict_obj) |
eric@59 | 483 | { |
eric@59 | 484 | struct pdf_dict_entry *entry; |
eric@59 | 485 | |
eric@59 | 486 | pdf_assert (dict_obj->type == PT_DICTIONARY); |
eric@59 | 487 | |
eric@59 | 488 | fprintf (pdf_file->f, "<<\r\n"); |
eric@59 | 489 | for (entry = dict_obj->val.dict.first; entry; entry = entry->next) |
eric@59 | 490 | { |
eric@59 | 491 | pdf_write_name (pdf_file, entry->key); |
eric@59 | 492 | fprintf (pdf_file->f, " "); |
eric@59 | 493 | pdf_write_obj (pdf_file, entry->val); |
eric@59 | 494 | fprintf (pdf_file->f, "\r\n"); |
eric@59 | 495 | } |
eric@59 | 496 | fprintf (pdf_file->f, ">>\r\n"); |
eric@59 | 497 | } |
eric@59 | 498 | |
eric@59 | 499 | |
eric@59 | 500 | void pdf_stream_write_data (pdf_file_handle pdf_file, |
eric@59 | 501 | struct pdf_obj *stream, |
eric@59 | 502 | char *data, |
eric@59 | 503 | unsigned long len) |
eric@59 | 504 | { |
eric@59 | 505 | while (len) |
eric@59 | 506 | { |
eric@59 | 507 | unsigned long l2 = fwrite (data, 1, len, pdf_file->f); |
eric@59 | 508 | data += l2; |
eric@59 | 509 | len -= l2; |
eric@59 | 510 | if (ferror (pdf_file->f)) |
eric@59 | 511 | pdf_fatal ("error writing stream data\n"); |
eric@59 | 512 | } |
eric@59 | 513 | } |
eric@59 | 514 | |
eric@59 | 515 | |
eric@66 | 516 | void pdf_stream_printf (pdf_file_handle pdf_file, |
eric@66 | 517 | struct pdf_obj *stream, |
eric@66 | 518 | char *fmt, ...) |
eric@66 | 519 | { |
eric@66 | 520 | va_list ap; |
eric@66 | 521 | |
eric@66 | 522 | va_start (ap, fmt); |
eric@66 | 523 | vfprintf (pdf_file->f, fmt, ap); |
eric@66 | 524 | va_end (ap); |
eric@66 | 525 | } |
eric@66 | 526 | |
eric@66 | 527 | |
eric@59 | 528 | void pdf_write_stream (pdf_file_handle pdf_file, struct pdf_obj *stream) |
eric@59 | 529 | { |
eric@59 | 530 | unsigned long begin_pos, end_pos; |
eric@59 | 531 | |
eric@59 | 532 | pdf_assert (stream->type == PT_STREAM); |
eric@59 | 533 | |
eric@59 | 534 | pdf_write_dict (pdf_file, stream->val.stream.stream_dict); |
eric@59 | 535 | fprintf (pdf_file->f, "stream\r\n"); |
eric@59 | 536 | begin_pos = ftell (pdf_file->f); |
eric@59 | 537 | stream->val.stream.callback (pdf_file, |
eric@59 | 538 | stream, |
eric@59 | 539 | stream->val.stream.app_data); |
eric@59 | 540 | end_pos = ftell (pdf_file->f); |
eric@59 | 541 | fprintf (pdf_file->f, "\r\nendstream\r\n"); |
eric@59 | 542 | |
eric@59 | 543 | pdf_set_integer (stream->val.stream.length, end_pos - begin_pos); |
eric@59 | 544 | } |
eric@59 | 545 | |
eric@59 | 546 | |
eric@59 | 547 | void pdf_write_obj (pdf_file_handle pdf_file, struct pdf_obj *obj) |
eric@59 | 548 | { |
eric@59 | 549 | switch (obj->type) |
eric@59 | 550 | { |
eric@59 | 551 | case PT_NULL: |
eric@59 | 552 | fprintf (pdf_file->f, "null "); |
eric@59 | 553 | break; |
eric@59 | 554 | case PT_BOOL: |
eric@62 | 555 | if (obj->val.boolean) |
eric@59 | 556 | fprintf (pdf_file->f, "true "); |
eric@59 | 557 | else |
eric@59 | 558 | fprintf (pdf_file->f, "false "); |
eric@59 | 559 | break; |
eric@59 | 560 | case PT_NAME: |
eric@59 | 561 | pdf_write_name (pdf_file, obj->val.name); |
eric@59 | 562 | break; |
eric@59 | 563 | case PT_STRING: |
eric@59 | 564 | pdf_write_string (pdf_file, obj->val.string); |
eric@59 | 565 | break; |
eric@59 | 566 | case PT_INTEGER: |
eric@59 | 567 | fprintf (pdf_file->f, "%ld ", obj->val.integer); |
eric@59 | 568 | break; |
eric@59 | 569 | case PT_REAL: |
eric@59 | 570 | pdf_write_real (pdf_file, obj->val.real); |
eric@59 | 571 | break; |
eric@59 | 572 | case PT_IND_REF: |
eric@59 | 573 | pdf_write_ind_ref (pdf_file, obj); |
eric@59 | 574 | break; |
eric@59 | 575 | case PT_DICTIONARY: |
eric@59 | 576 | pdf_write_dict (pdf_file, obj); |
eric@59 | 577 | break; |
eric@59 | 578 | case PT_ARRAY: |
eric@59 | 579 | pdf_write_array (pdf_file, obj); |
eric@59 | 580 | break; |
eric@59 | 581 | case PT_STREAM: |
eric@59 | 582 | pdf_write_stream (pdf_file, obj); |
eric@59 | 583 | break; |
eric@59 | 584 | default: |
eric@59 | 585 | pdf_fatal ("bad object type\n"); |
eric@59 | 586 | } |
eric@59 | 587 | } |
eric@59 | 588 | |
eric@59 | 589 | |
eric@59 | 590 | void pdf_write_ind_obj (pdf_file_handle pdf_file, struct pdf_obj *ind_obj) |
eric@59 | 591 | { |
eric@59 | 592 | struct pdf_obj *obj; |
eric@59 | 593 | |
eric@59 | 594 | if (ind_obj->type == PT_IND_REF) |
eric@59 | 595 | obj = pdf_deref_ind_obj (ind_obj); |
eric@59 | 596 | else |
eric@59 | 597 | obj = ind_obj; |
eric@59 | 598 | |
eric@59 | 599 | obj->file_offset = ftell (pdf_file->f); |
eric@59 | 600 | fprintf (pdf_file->f, "%ld %ld obj\r\n", obj->obj_num, obj->obj_gen); |
eric@59 | 601 | pdf_write_obj (pdf_file, obj); |
eric@59 | 602 | fprintf (pdf_file->f, "endobj\r\n"); |
eric@59 | 603 | } |
eric@59 | 604 | |
eric@59 | 605 | |
eric@59 | 606 | void pdf_write_all_ind_obj (pdf_file_handle pdf_file) |
eric@59 | 607 | { |
eric@59 | 608 | struct pdf_obj *ind_obj; |
eric@59 | 609 | for (ind_obj = pdf_file->first_ind_obj; ind_obj; ind_obj = ind_obj->next) |
eric@59 | 610 | if (! ind_obj->file_offset) |
eric@59 | 611 | pdf_write_ind_obj (pdf_file, ind_obj); |
eric@59 | 612 | } |
eric@59 | 613 | |
eric@59 | 614 | |
eric@59 | 615 | unsigned long pdf_write_xref (pdf_file_handle pdf_file) |
eric@59 | 616 | { |
eric@59 | 617 | struct pdf_obj *ind_obj; |
eric@59 | 618 | pdf_file->xref_offset = ftell (pdf_file->f); |
eric@59 | 619 | fprintf (pdf_file->f, "xref\r\n"); |
eric@59 | 620 | fprintf (pdf_file->f, "0 %ld\r\n", pdf_file->last_ind_obj->obj_num + 1); |
eric@59 | 621 | fprintf (pdf_file->f, "0000000000 65535 f\r\n"); |
eric@59 | 622 | for (ind_obj = pdf_file->first_ind_obj; ind_obj; ind_obj = ind_obj->next) |
eric@59 | 623 | fprintf (pdf_file->f, "%010ld 00000 n\r\n", ind_obj->file_offset); |
eric@59 | 624 | return (pdf_file->last_ind_obj->obj_num + 1); |
eric@59 | 625 | } |
eric@59 | 626 | |
eric@59 | 627 | |
eric@101 | 628 | /* this isn't really a PDF primitive data type */ |
eric@101 | 629 | char pdf_new_XObject (pdf_page_handle pdf_page, struct pdf_obj *ind_ref) |
eric@101 | 630 | { |
eric@101 | 631 | char XObject_name [4] = "Im "; |
eric@101 | 632 | |
eric@101 | 633 | XObject_name [2] = ++pdf_page->last_XObject_name; |
eric@101 | 634 | |
eric@101 | 635 | if (! pdf_page->XObject_dict) |
eric@101 | 636 | { |
eric@101 | 637 | pdf_page->XObject_dict = pdf_new_obj (PT_DICTIONARY); |
eric@101 | 638 | pdf_set_dict_entry (pdf_page->resources, "XObject", pdf_page->XObject_dict); |
eric@101 | 639 | } |
eric@101 | 640 | |
eric@101 | 641 | pdf_set_dict_entry (pdf_page->XObject_dict, & XObject_name [0], ind_ref); |
eric@101 | 642 | |
eric@101 | 643 | return (pdf_page->last_XObject_name); |
eric@101 | 644 | } |
eric@101 | 645 | |
eric@101 | 646 |