Wed, 12 Mar 2003 06:57:46 +0000
changed g4_table_gen to generate both a header and a C source file (g4_tables.[ch]).
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_prim.c,v 1.8 2003/03/10 01:49:50 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 <stdarg.h>
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "bitblt.h"
35 #include "pdf.h"
36 #include "pdf_util.h"
37 #include "pdf_prim.h"
38 #include "pdf_private.h"
41 struct pdf_array_elem
42 {
43 struct pdf_array_elem *next;
44 struct pdf_obj *val;
45 };
48 struct pdf_array
49 {
50 struct pdf_array_elem *first;
51 struct pdf_array_elem *last;
52 };
55 struct pdf_dict_entry
56 {
57 struct pdf_dict_entry *next;
58 char *key;
59 struct pdf_obj *val;
60 };
63 struct pdf_dict
64 {
65 struct pdf_dict_entry *first;
66 };
69 struct pdf_stream
70 {
71 struct pdf_obj *stream_dict;
72 struct pdf_obj *length;
73 pdf_stream_write_callback callback;
74 void *app_data; /* arg to pass to callback */
75 struct pdf_obj *filters; /* name or array of names */
76 struct pdf_obj *decode_parms;
77 };
80 struct pdf_obj
81 {
82 /* these fields only apply to indirectly referenced objects */
83 struct pdf_obj *prev;
84 struct pdf_obj *next;
85 unsigned long obj_num;
86 unsigned long obj_gen;
87 long int file_offset;
89 /* these fields apply to all objects */
90 unsigned long ref_count;
91 pdf_obj_type type;
92 union {
93 bool boolean;
94 char *name;
95 char *string;
96 long integer;
97 double real;
98 struct pdf_obj *ind_ref;
99 struct pdf_dict dict;
100 struct pdf_array array;
101 struct pdf_stream stream;
102 } val;
103 };
106 struct pdf_obj *ref (struct pdf_obj *obj)
107 {
108 obj->ref_count++;
109 return (obj);
110 }
113 void unref (struct pdf_obj *obj)
114 {
115 if ((--obj->ref_count) == 0)
116 {
117 /* $$$ free the object */
118 }
119 }
122 struct pdf_obj *pdf_deref_ind_obj (struct pdf_obj *ind_obj)
123 {
124 pdf_assert (ind_obj->type == PT_IND_REF);
125 return (ind_obj->val.ind_ref);
126 }
129 void pdf_set_dict_entry (struct pdf_obj *dict_obj, char *key, struct pdf_obj *val)
130 {
131 struct pdf_dict_entry *entry;
133 if (dict_obj->type == PT_IND_REF)
134 dict_obj = pdf_deref_ind_obj (dict_obj);
136 pdf_assert (dict_obj->type == PT_DICTIONARY);
138 /* replacing existing entry? */
139 for (entry = dict_obj->val.dict.first; entry; entry = entry->next)
140 if (strcmp (entry->key, key) == 0)
141 {
142 unref (entry->val);
143 entry->val = ref (val);
144 return;
145 }
147 /* new entry */
148 entry = pdf_calloc (1, sizeof (struct pdf_dict_entry));
150 entry->next = dict_obj->val.dict.first;
151 dict_obj->val.dict.first = entry;
153 entry->key = pdf_strdup (key);
154 entry->val = ref (val);
155 }
158 struct pdf_obj *pdf_get_dict_entry (struct pdf_obj *dict_obj, char *key)
159 {
160 struct pdf_dict_entry *entry;
162 if (dict_obj->type == PT_IND_REF)
163 dict_obj = pdf_deref_ind_obj (dict_obj);
165 pdf_assert (dict_obj->type == PT_DICTIONARY);
167 for (entry = dict_obj->val.dict.first; entry; entry = entry->next)
168 if (strcmp (entry->key, key) == 0)
169 return (entry->val);
171 return (NULL);
172 }
175 void pdf_add_array_elem (struct pdf_obj *array_obj, struct pdf_obj *val)
176 {
177 struct pdf_array_elem *elem = pdf_calloc (1, sizeof (struct pdf_array_elem));
179 if (array_obj->type == PT_IND_REF)
180 array_obj = pdf_deref_ind_obj (array_obj);
182 pdf_assert (array_obj->type == PT_ARRAY);
184 elem->val = ref (val);
186 if (! array_obj->val.array.first)
187 array_obj->val.array.first = elem;
188 else
189 array_obj->val.array.last->next = elem;
191 array_obj->val.array.last = elem;
192 }
195 struct pdf_obj *pdf_new_obj (pdf_obj_type type)
196 {
197 struct pdf_obj *obj = pdf_calloc (1, sizeof (struct pdf_obj));
198 obj->type = type;
199 return (obj);
200 }
203 struct pdf_obj *pdf_new_bool (bool val)
204 {
205 struct pdf_obj *obj = pdf_new_obj (PT_BOOL);
206 obj->val.boolean = val;
207 return (obj);
208 }
211 struct pdf_obj *pdf_new_name (char *name)
212 {
213 struct pdf_obj *obj = pdf_new_obj (PT_NAME);
214 obj->val.name = pdf_strdup (name);
215 return (obj);
216 }
219 struct pdf_obj *pdf_new_string (char *str)
220 {
221 struct pdf_obj *obj = pdf_new_obj (PT_STRING);
222 obj->val.string = pdf_strdup (str);
223 return (obj);
224 }
227 struct pdf_obj *pdf_new_integer (long val)
228 {
229 struct pdf_obj *obj = pdf_new_obj (PT_INTEGER);
230 obj->val.integer = val;
231 return (obj);
232 }
235 struct pdf_obj *pdf_new_real (double val)
236 {
237 struct pdf_obj *obj = pdf_new_obj (PT_REAL);
238 obj->val.real = val;
239 return (obj);
240 }
243 struct pdf_obj *pdf_new_stream (pdf_file_handle pdf_file,
244 struct pdf_obj *stream_dict,
245 pdf_stream_write_callback callback,
246 void *app_data)
247 {
248 struct pdf_obj *obj = pdf_new_obj (PT_STREAM);
250 obj->val.stream.stream_dict = stream_dict;
251 obj->val.stream.length = pdf_new_ind_ref (pdf_file, pdf_new_integer (0));
252 pdf_set_dict_entry (obj->val.stream.stream_dict, "Length", obj->val.stream.length);
254 obj->val.stream.callback = callback;
255 obj->val.stream.app_data = app_data;
256 return (obj);
257 }
260 /* $$$ currently limited to one filter per stream */
261 void pdf_stream_add_filter (struct pdf_obj *stream,
262 char *filter_name,
263 struct pdf_obj *decode_parms)
264 {
265 if (stream->type == PT_IND_REF)
266 stream = pdf_deref_ind_obj (stream);
268 pdf_assert (stream->type == PT_STREAM);
270 pdf_set_dict_entry (stream->val.stream.stream_dict, "Filter", pdf_new_name (filter_name));
271 if (decode_parms)
272 pdf_set_dict_entry (stream->val.stream.stream_dict, "DecodeParms", decode_parms);
273 }
276 struct pdf_obj *pdf_new_ind_ref (pdf_file_handle pdf_file, struct pdf_obj *obj)
277 {
278 struct pdf_obj *ind_obj;
280 pdf_assert (obj->type != PT_IND_REF);
282 ind_obj = pdf_new_obj (PT_IND_REF);
284 ind_obj->type = PT_IND_REF;
285 ind_obj->val.ind_ref = obj;
287 /* is there already an indirect reference to this object? */
288 if (! obj->obj_num)
289 {
290 /* no, assign object number/generation and add to linked list */
291 if (! pdf_file->first_ind_obj)
292 {
293 obj->obj_num = 1;
294 pdf_file->first_ind_obj = pdf_file->last_ind_obj = obj;
295 }
296 else
297 {
298 obj->obj_num = pdf_file->last_ind_obj->obj_num + 1;
299 pdf_file->last_ind_obj->next = obj;
300 obj->prev = pdf_file->last_ind_obj;
301 pdf_file->last_ind_obj = obj;
302 }
303 }
305 return (ind_obj);
306 }
309 long pdf_get_integer (struct pdf_obj *obj)
310 {
311 if (obj->type == PT_IND_REF)
312 obj = pdf_deref_ind_obj (obj);
314 pdf_assert (obj->type == PT_INTEGER);
316 return (obj->val.integer);
317 }
319 void pdf_set_integer (struct pdf_obj *obj, long val)
320 {
321 if (obj->type == PT_IND_REF)
322 obj = pdf_deref_ind_obj (obj);
324 pdf_assert (obj->type == PT_INTEGER);
326 obj->val.integer = val;
327 }
330 double pdf_get_real (struct pdf_obj *obj)
331 {
332 if (obj->type == PT_IND_REF)
333 obj = pdf_deref_ind_obj (obj);
335 pdf_assert (obj->type == PT_REAL);
337 return (obj->val.real);
338 }
340 void pdf_set_real (struct pdf_obj *obj, double val)
341 {
342 if (obj->type == PT_IND_REF)
343 obj = pdf_deref_ind_obj (obj);
345 pdf_assert (obj->type == PT_REAL);
347 obj->val.real = val;
348 }
351 int pdf_compare_obj (struct pdf_obj *o1, struct pdf_obj *o2)
352 {
353 if (o1->type == PT_IND_REF)
354 o1 = pdf_deref_ind_obj (o1);
356 if (o2->type == PT_IND_REF)
357 o2 = pdf_deref_ind_obj (o2);
359 pdf_assert (o1->type == o2->type);
361 switch (o1->type)
362 {
363 case PT_INTEGER:
364 if (o1->val.integer < o2->val.integer)
365 return (-1);
366 if (o1->val.integer > o2->val.integer)
367 return (1);
368 return (0);
369 case PT_REAL:
370 if (o1->val.real < o2->val.real)
371 return (-1);
372 if (o1->val.real > o2->val.real)
373 return (1);
374 return (0);
375 case PT_STRING:
376 return (strcmp (o1->val.string, o2->val.string));
377 default:
378 pdf_fatal ("invalid object type for comparison\n");
379 }
380 }
383 static int name_char_needs_quoting (char c)
384 {
385 return ((c < '!') || (c > '~') || (c == '/') || (c == '\\') ||
386 (c == '(') || (c == ')') || (c == '<') || (c == '>') ||
387 (c == '[') || (c == ']') || (c == '{') || (c == '}') ||
388 (c == '%'));
389 }
392 void pdf_write_name (pdf_file_handle pdf_file, char *s)
393 {
394 fprintf (pdf_file->f, "/");
395 while (*s)
396 if (name_char_needs_quoting (*s))
397 fprintf (pdf_file->f, "#%02x", 0xff & *(s++));
398 else
399 fprintf (pdf_file->f, "%c", *(s++));
400 fprintf (pdf_file->f, " ");
401 }
404 static int string_char_needs_quoting (char c)
405 {
406 return ((c < ' ') || (c > '~') || (c == '\\') ||
407 (c == '(') || (c == ')'));
408 }
411 void pdf_write_string (pdf_file_handle pdf_file, char *s)
412 {
413 fprintf (pdf_file->f, "(");
414 while (*s)
415 if (string_char_needs_quoting (*s))
416 fprintf (pdf_file->f, "\\%03o", 0xff & *(s++));
417 else
418 fprintf (pdf_file->f, "%c", *(s++));
419 fprintf (pdf_file->f, ") ");
420 }
423 void pdf_write_real (pdf_file_handle pdf_file, double num)
424 {
425 /* $$$ not actually good enough, precision needs to be variable,
426 and no exponent is allowed */
427 fprintf (pdf_file->f, "%0f ", num);
428 }
431 void pdf_write_ind_ref (pdf_file_handle pdf_file, struct pdf_obj *ind_obj)
432 {
433 struct pdf_obj *obj = pdf_deref_ind_obj (ind_obj);
434 fprintf (pdf_file->f, "%ld %ld R ", obj->obj_num, obj->obj_gen);
435 }
438 void pdf_write_array (pdf_file_handle pdf_file, struct pdf_obj *array_obj)
439 {
440 struct pdf_array_elem *elem;
442 pdf_assert (array_obj->type == PT_ARRAY);
444 fprintf (pdf_file->f, "[ ");
445 for (elem = array_obj->val.array.first; elem; elem = elem->next)
446 {
447 pdf_write_obj (pdf_file, elem->val);
448 fprintf (pdf_file->f, " ");
449 }
450 fprintf (pdf_file->f, "] ");
451 }
454 void pdf_write_dict (pdf_file_handle pdf_file, struct pdf_obj *dict_obj)
455 {
456 struct pdf_dict_entry *entry;
458 pdf_assert (dict_obj->type == PT_DICTIONARY);
460 fprintf (pdf_file->f, "<<\r\n");
461 for (entry = dict_obj->val.dict.first; entry; entry = entry->next)
462 {
463 pdf_write_name (pdf_file, entry->key);
464 fprintf (pdf_file->f, " ");
465 pdf_write_obj (pdf_file, entry->val);
466 fprintf (pdf_file->f, "\r\n");
467 }
468 fprintf (pdf_file->f, ">>\r\n");
469 }
472 void pdf_stream_write_data (pdf_file_handle pdf_file,
473 struct pdf_obj *stream,
474 char *data,
475 unsigned long len)
476 {
477 while (len)
478 {
479 unsigned long l2 = fwrite (data, 1, len, pdf_file->f);
480 data += l2;
481 len -= l2;
482 if (ferror (pdf_file->f))
483 pdf_fatal ("error writing stream data\n");
484 }
485 }
488 void pdf_stream_printf (pdf_file_handle pdf_file,
489 struct pdf_obj *stream,
490 char *fmt, ...)
491 {
492 va_list ap;
494 va_start (ap, fmt);
495 vfprintf (pdf_file->f, fmt, ap);
496 va_end (ap);
497 }
500 void pdf_write_stream (pdf_file_handle pdf_file, struct pdf_obj *stream)
501 {
502 unsigned long begin_pos, end_pos;
504 pdf_assert (stream->type == PT_STREAM);
506 pdf_write_dict (pdf_file, stream->val.stream.stream_dict);
507 fprintf (pdf_file->f, "stream\r\n");
508 begin_pos = ftell (pdf_file->f);
509 stream->val.stream.callback (pdf_file,
510 stream,
511 stream->val.stream.app_data);
512 end_pos = ftell (pdf_file->f);
513 fprintf (pdf_file->f, "\r\nendstream\r\n");
515 pdf_set_integer (stream->val.stream.length, end_pos - begin_pos);
516 }
519 void pdf_write_obj (pdf_file_handle pdf_file, struct pdf_obj *obj)
520 {
521 switch (obj->type)
522 {
523 case PT_NULL:
524 fprintf (pdf_file->f, "null ");
525 break;
526 case PT_BOOL:
527 if (obj->val.boolean)
528 fprintf (pdf_file->f, "true ");
529 else
530 fprintf (pdf_file->f, "false ");
531 break;
532 case PT_NAME:
533 pdf_write_name (pdf_file, obj->val.name);
534 break;
535 case PT_STRING:
536 pdf_write_string (pdf_file, obj->val.string);
537 break;
538 case PT_INTEGER:
539 fprintf (pdf_file->f, "%ld ", obj->val.integer);
540 break;
541 case PT_REAL:
542 pdf_write_real (pdf_file, obj->val.real);
543 break;
544 case PT_IND_REF:
545 pdf_write_ind_ref (pdf_file, obj);
546 break;
547 case PT_DICTIONARY:
548 pdf_write_dict (pdf_file, obj);
549 break;
550 case PT_ARRAY:
551 pdf_write_array (pdf_file, obj);
552 break;
553 case PT_STREAM:
554 pdf_write_stream (pdf_file, obj);
555 break;
556 default:
557 pdf_fatal ("bad object type\n");
558 }
559 }
562 void pdf_write_ind_obj (pdf_file_handle pdf_file, struct pdf_obj *ind_obj)
563 {
564 struct pdf_obj *obj;
566 if (ind_obj->type == PT_IND_REF)
567 obj = pdf_deref_ind_obj (ind_obj);
568 else
569 obj = ind_obj;
571 obj->file_offset = ftell (pdf_file->f);
572 fprintf (pdf_file->f, "%ld %ld obj\r\n", obj->obj_num, obj->obj_gen);
573 pdf_write_obj (pdf_file, obj);
574 fprintf (pdf_file->f, "endobj\r\n");
575 }
578 void pdf_write_all_ind_obj (pdf_file_handle pdf_file)
579 {
580 struct pdf_obj *ind_obj;
581 for (ind_obj = pdf_file->first_ind_obj; ind_obj; ind_obj = ind_obj->next)
582 if (! ind_obj->file_offset)
583 pdf_write_ind_obj (pdf_file, ind_obj);
584 }
587 unsigned long pdf_write_xref (pdf_file_handle pdf_file)
588 {
589 struct pdf_obj *ind_obj;
590 pdf_file->xref_offset = ftell (pdf_file->f);
591 fprintf (pdf_file->f, "xref\r\n");
592 fprintf (pdf_file->f, "0 %ld\r\n", pdf_file->last_ind_obj->obj_num + 1);
593 fprintf (pdf_file->f, "0000000000 65535 f\r\n");
594 for (ind_obj = pdf_file->first_ind_obj; ind_obj; ind_obj = ind_obj->next)
595 fprintf (pdf_file->f, "%010ld 00000 n\r\n", ind_obj->file_offset);
596 return (pdf_file->last_ind_obj->obj_num + 1);
597 }