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 * 2007-05-07 [JDB] Add declarations for pdf_new_string_n() and 23 * pdf_write_name(). 24 */ 25 26 27 typedef enum 28 { 29 PT_BAD, 30 31 /* scalar */ 32 PT_NULL, 33 PT_BOOL, 34 PT_NAME, 35 PT_STRING, 36 PT_INTEGER, 37 PT_REAL, 38 PT_IND_REF, 39 40 /* composite */ 41 PT_DICTIONARY, 42 PT_ARRAY, 43 PT_STREAM 44 } pdf_obj_type; 45 46 47 struct pdf_obj; 48 49 50 typedef void (*pdf_stream_write_callback)(pdf_file_handle pdf_file, 51 struct pdf_obj *stream, 52 void *app_data); 53 54 55 /* returns -1 if o1 < 02, 0 if o1 == o2, 1 if o1 > o2 */ 56 /* only works for integer, real, string, and name objects */ 57 int pdf_compare_obj (struct pdf_obj *o1, struct pdf_obj *o2); 58 59 60 void pdf_set_dict_entry (struct pdf_obj *dict_obj, char *key, struct pdf_obj *val); 61 struct pdf_obj *pdf_get_dict_entry (struct pdf_obj *dict_obj, char *key); 62 63 64 void pdf_add_array_elem (struct pdf_obj *array_obj, struct pdf_obj *val); 65 66 67 /* Following is intended for things like ProcSet in which an array object 68 is used to represent a set. Only works if all objects in array, and 69 the element to be added are of scalar types (types that are supported 70 by pdf_compare_obj. Not efficient for large arrays as it does a 71 comaprison to every element. */ 72 void pdf_add_array_elem_unique (struct pdf_obj *array_obj, struct pdf_obj *val); 73 74 75 /* Create a new object that will NOT be used indirectly */ 76 struct pdf_obj *pdf_new_obj (pdf_obj_type type); 77 78 struct pdf_obj *pdf_new_bool (bool val); 79 80 struct pdf_obj *pdf_new_name (char *name); 81 82 struct pdf_obj *pdf_new_string (char *str); 83 84 struct pdf_obj *pdf_new_string_n (char *str, int n); 85 86 struct pdf_obj *pdf_new_integer (long val); 87 88 struct pdf_obj *pdf_new_real (double val); 89 90 91 /* Create a new indirect object */ 92 struct pdf_obj *pdf_new_ind_ref (pdf_file_handle pdf_file, struct pdf_obj *obj); 93 94 /* get the object referenced by an indirect reference */ 95 struct pdf_obj *pdf_deref_ind_obj (struct pdf_obj *ind_obj); 96 97 98 long pdf_get_integer (struct pdf_obj *obj); 99 void pdf_set_integer (struct pdf_obj *obj, long val); 100 101 102 double pdf_get_real (struct pdf_obj *obj); 103 void pdf_set_real (struct pdf_obj *obj, double val); 104 105 106 /* The callback will be called when the stream data is to be written to the 107 file. app_data will be passed as an argument to the callback. */ 108 struct pdf_obj *pdf_new_stream (pdf_file_handle pdf_file, 109 struct pdf_obj *stream_dict, 110 pdf_stream_write_callback callback, 111 void *app_data); 112 113 /* The callback should call pdf_stream_write_data() or pdf_stream_printf() 114 to write the actual stream data. */ 115 116 void pdf_stream_flush_bits (pdf_file_handle pdf_file, 117 struct pdf_obj *stream); 118 119 void pdf_stream_write_data (pdf_file_handle pdf_file, 120 struct pdf_obj *stream, 121 char *data, 122 unsigned long len); 123 124 void pdf_stream_printf (pdf_file_handle pdf_file, 125 struct pdf_obj *stream, 126 char *fmt, ...); 127 128 129 void pdf_stream_add_filter (struct pdf_obj *stream, 130 char *filter_name, 131 struct pdf_obj *decode_parms); 132 133 134 /* Write the object to the file */ 135 void pdf_write_obj (pdf_file_handle pdf_file, struct pdf_obj *obj); 136 137 138 /* Write the indirect object to the file. For most objects this should 139 be done by pdf_write_all_ind_obj() when the file is being closed, but for 140 large objects such as streams, it's probably better to do it as soon as the 141 object is complete. */ 142 void pdf_write_ind_obj (pdf_file_handle pdf_file, struct pdf_obj *ind_obj); 143 144 145 /* Write all indirect objects that haven't already been written to the file. */ 146 void pdf_write_all_ind_obj (pdf_file_handle pdf_file); 147 148 149 /* Write the cross reference table, and return the maximum object number */ 150 unsigned long pdf_write_xref (pdf_file_handle pdf_file); 151 152 153 /* Write a name, escaping reserved characters */ 154 void pdf_write_name (pdf_file_handle pdf_file, char *s); 155 156 157 /* this isn't really a PDF primitive data type */ 158 char pdf_new_XObject (pdf_page_handle pdf_page, struct pdf_obj *ind_ref); 159