1 /* pdftypes.h
2 
3    Copyright 2009-2013 Taco Hoekwater <taco@luatex.org>
4 
5    This file is part of LuaTeX.
6 
7    LuaTeX is free software; you can redistribute it and/or modify it under
8    the terms of the GNU General Public License as published by the Free
9    Software Foundation; either version 2 of the License, or (at your
10    option) any later version.
11 
12    LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15    License for more details.
16 
17    You should have received a copy of the GNU General Public License along
18    with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
19 
20 
21 #ifndef PDFTYPES_H
22 #  define PDFTYPES_H
23 
24 #ifdef HAVE_CONFIG_H
25 #include <w2c/config.h>
26 #endif
27 #  include <zlib.h>
28 #  include "lua/luatex-api.h"
29 
30 /* The prefix "PTEX" for the PDF keys is special to pdfTeX;
31    this has been registered with Adobe by Hans Hagen. */
32 
33 #  define pdfkeyprefix "PTEX"
34 
35 #  define i32round(a) (int) floor((a) + 0.5)
36 #  define i64round(a) (int64_t) floor((a) + 0.5)
37 
38 #  define MAX_OBJ_COMPRESS_LEVEL 3      /* maximum/clipping value for \pdfobjcompresslevel */
39 #  define OBJSTM_UNSET -1       /* initial value */
40 #  define OBJSTM_ALWAYS 1       /* \pdfobjcompresslevel >= OBJSTM_ALWAYS: put object into object stream */
41 #  define OBJSTM_NEVER (MAX_OBJ_COMPRESS_LEVEL + 1)
42                                         /* above maximum/clipping value for \pdfobjcompresslevel */
43 
44 typedef int internal_font_number;       /* |font| in a |char_node| */
45 
46 typedef enum {
47     NO_ZIP,                     /* no \.{ZIP} compression */
48     ZIP_WRITING,                /* \.{ZIP} compression being used */
49     ZIP_FINISH                  /* finish \.{ZIP} compression */
50 } zip_write_state_e;
51 
52 typedef enum {
53     PDFOUT_BUF,
54     OBJSTM_BUF,
55     LUASTM_BUF
56 } buffer_e;
57 
58 /* This stucture holds everything that is needed for the actual pdf generation.
59 
60 Because this structure interfaces with C++, it is not wise to use |boolean|
61 here (C++ has a boolean type built-in that is not compatible). Also, I have
62 plans to convert the backend code into a C library for use with e.g. standalone
63 lua. Together, this means that it is best only to use the standard C types and
64 the types explicitly defined in this header, and stay away from types like
65 |integer| and |eight_bits| that are used elsewhere in the \LUATEX\ sources.
66 */
67 
68 typedef struct {
69     int64_t m;                     /* mantissa (significand) */
70     int e;                      /* exponent * -1 */
71 } pdffloat;
72 
73 typedef struct {
74     pdffloat h;
75     pdffloat v;
76 } pdfpos;
77 
78 #  define scaled int
79 
80 typedef struct scaledpos_ {
81     scaled h;
82     scaled v;
83 } scaledpos;
84 
85 typedef struct scaled_whd_ {
86     scaled wd;                  /* TeX width */
87     scaled ht;                  /* TeX height */
88     scaled dp;                  /* TeX depth */
89 } scaled_whd;
90 
91 typedef struct posstructure_ {
92     scaledpos pos;              /* position on the page */
93     int dir;                    /* direction of stuff to be put onto the page */
94 } posstructure;
95 
96 typedef struct {
97     scaledpos curpos;           /* \pdflastpos position */
98     posstructure boxpos;        /* box dir and position of the box origin on the page */
99     scaled_whd boxdim;          /* box dimensions (in hlist/vlist coordinate system) */
100 } pos_info_structure;
101 
102 typedef enum { PMODE_NONE, PMODE_PAGE, PMODE_TEXT, PMODE_CHARARRAY, PMODE_CHAR
103 } pos_mode;
104 
105 typedef enum { OMODE_NONE, OMODE_DVI, OMODE_PDF, OMODE_LUA } output_mode;
106 
107 #  define MAX_OMODE 3           /* largest index in enum output_mode */
108 
109 typedef enum { ST_INITIAL, ST_OMODE_FIX, ST_FILE_OPEN, ST_HEADER_WRITTEN,
110     ST_FILE_CLOSED
111 } output_state;
112 
113 typedef struct pdf_object_list_ {
114     int info;
115     struct pdf_object_list_ *link;
116 } pdf_object_list;
117 
118 typedef enum { WMODE_H, WMODE_V } writing_mode; /* []TJ runs horizontal or vertical */
119 
120 typedef struct {
121     pdfpos pdf;                 /* pos. on page (PDF page raster) */
122     pdfpos pdf_bt_pos;          /* pos. at begin of BT-ET group (PDF page raster) */
123     pdfpos pdf_tj_pos;          /* pos. at begin of TJ array (PDF page raster) */
124     pdffloat cw;                /* pos. within [(..)..]TJ array (glyph raster);
125                                    cw.e = fractional digits in /Widths array */
126     pdffloat tj_delta;          /* rel. movement in [(..)..]TJ array (glyph raster) */
127     pdffloat fs;                /* font size in PDF units */
128     pdffloat fs_cur;            /* to check if fs.m has changed and Tf needed */
129     pdffloat tm0_cur;           /* to check if tm[0] has changed and Tm needed */
130     pdffloat cm[6];             /* cm array */
131     pdffloat tm[6];             /* Tm array */
132     double k1;                  /* conv. factor from TeX sp to PDF page raster */
133     double k2;                  /* conv. factor from PDF page raster to TJ array raster */
134     int f_pdf;                  /* /F* font number, of unexpanded base font! */
135     int f_pdf_cur;              /* to check if f_pdf has changed and Tf needed */
136     writing_mode wmode;         /* PDF writing mode WMode (horizontal/vertical) */
137     pos_mode mode;              /* current positioning mode */
138     int ishex;                  /* Whether the current char string is <> or () */
139     int need_tf;                /* flag whether Tf needs to be set */
140     int need_tm;                /* flag whether Tm needs to be set */
141     int cur_ex;                 /* the current glyph ex factor */
142 } pdfstructure;
143 
144 typedef struct obj_entry_ {
145     union {
146         int int0;
147         char *str0;
148     } u;
149     int int1;
150     off_t int2;
151     int int3;
152     union {
153         int int4;
154         char *str4;
155     } v;
156     int objtype;                /* integer int5 */
157 } obj_entry;
158 
159 typedef struct dest_name_entry_ {
160     char *objname;              /* destination name */
161     int objnum;                 /* destination object number */
162 } dest_name_entry;
163 
164 #  define pdf_max_link_level  10/* maximum depth of link nesting */
165 
166 typedef struct pdf_link_stack_record {
167     int nesting_level;
168     int link_node;              /* holds a copy of the corresponding |pdf_start_link_node| */
169     int ref_link_node;          /* points to original |pdf_start_link_node|, or a
170                                    copy of |link_node| created by |append_link| in
171                                    case of multi-line link */
172 } pdf_link_stack_record;
173 
174 /* types of objects */
175 typedef enum {
176     obj_type_font = 0,          /* index of linked list of Fonts objects */
177     obj_type_outline = 1,       /* index of linked list of outline objects */
178     obj_type_dest = 2,          /* index of linked list of destination objects */
179     obj_type_obj = 3,           /* index of linked list of raw objects */
180     obj_type_xform = 4,         /* index of linked list of XObject forms */
181     obj_type_ximage = 5,        /* index of linked list of XObject images */
182     obj_type_thread = 6,        /* index of linked list of num article threads */
183     /* |obj_type_thread| is the highest entry in |head_tab|, but there are a few
184        more linked lists that are handy: */
185     obj_type_pagestream = 7,    /* Page stream objects */
186     obj_type_page = 8,          /* /Page objects */
187     obj_type_pages = 9,         /* /Pages objects */
188     obj_type_catalog = 10,      /* /Catalog object */
189     obj_type_info = 11,         /* /Info object */
190     obj_type_link = 12,         /* link objects */
191     obj_type_annot = 13,        /* annotation objects */
192     obj_type_annots = 14,       /* /Annots objects */
193     obj_type_bead = 15,         /* thread bead objects */
194     obj_type_beads = 16,        /* /B objects (array of bead objects) */
195     obj_type_objstm = 17,       /* /ObjStm objects */
196     obj_type_others = 18        /* any other objects (also not linked in any list) */
197 } pdf_obj_type;
198 
199 #  define HEAD_TAB_MAX      6   /* obj_type_thread */
200 #  define PDF_OBJ_TYPE_MAX 18   /* obj_type_others */
201 
202 typedef struct pdf_resource_struct_ {
203     struct avl_table *resources_tree;
204     int last_resources;         /* halfword to most recently generated Resources object. */
205 } pdf_resource_struct;
206 
207 /**********************************************************************/
208 
209 typedef struct os_obj_data_ {
210     int num;
211     int off;
212 } os_obj_data;
213 
214 typedef struct strbuf_s_ {
215     unsigned char *data;        /* a PDF stream buffer */
216     unsigned char *p;           /* pointer to the next character in the PDF stream buffer */
217     size_t size;                /* currently allocated size of the PDF stream buffer, grows dynamically */
218     size_t limit;               /* maximum allowed PDF stream buffer size */
219 } strbuf_s;
220 
221 typedef struct os_struct_ {
222     os_obj_data *obj;           /* array of object stream objects */
223     strbuf_s *buf[3];
224     buffer_e curbuf;            /* select into which buffer to output */
225     luaL_Buffer b;              /* Lua buffer connected to luastm_buf */
226     unsigned int cur_objstm;    /* number of current object stream object */
227     unsigned int idx;           /* index of object within object stream [1...PDF_OS_MAX_OBJS - 1] */
228     unsigned int ostm_ctr;      /* statistics: counter for object stream objects */
229     unsigned int o_ctr;         /* statistics: counter for objects within object streams */
230     int trigger_luastm;
231 } os_struct;
232 
233 /**********************************************************************/
234 
235 #  define packet_max_recursion 100      /* see |packet_cur_s| */
236 #  define packet_stack_size 100
237 
238 typedef struct packet_stack_record_ {
239     float c0;
240     float c1;
241     float c2;
242     float c3;
243     scaledpos pos;              /* c4, c5 */
244 } packet_stack_record;
245 
246 typedef struct vf_struct_ {
247     packet_stack_record *packet_stack;  /* for "push" and "pop" */
248     int packet_stack_level;
249     int packet_stack_minlevel;  /* to check stack underflow */
250     internal_font_number lf;    /* local font number, resolved */
251     int fs_f;                   /* local font size */
252     int ex_glyph;               /* expansion value; ex_glyph = 0 means unexpanded */
253     int packet_cur_s;           /* do_vf_packet() nesting level */
254     posstructure *refpos;
255     int vflua;                  /* flag, whether vf.*() functions are allowed */
256 } vf_struct;
257 
258 /**********************************************************************/
259 
260 typedef struct pdf_output_file_ {
261     FILE *file;                 /* the PDF output file handle */
262     char *file_name;            /* the PDF output file name */
263     char *job_name;
264     output_mode o_mode;         /* output mode (DVI/PDF/...) */
265     output_state o_state;
266     /* generation parameters */
267     int gamma;
268     int image_gamma;
269     int image_hicolor;          /* boolean */
270     int image_apply_gamma;
271     int draftmode;
272     int pk_resolution;
273     int decimal_digits;
274     int gen_tounicode;
275     int inclusion_copy_font;
276     int replace_font;
277     int minor_version;          /* fixed minor part of the PDF version */
278     int compress_level;         /* level for zlib object stream compression */
279     int objcompresslevel;       /* fixed level for activating PDF object streams */
280     char *job_id_string;        /* the full job string */
281 
282     int os_enable;              /* true if object streams are globally enabled */
283     os_struct *os;              /* object stream structure pointer */
284 
285     strbuf_s *buf;              /* pointer to the current stream buffer (PDF stream, ObjStm, or Lua) */
286 
287     off_t save_offset;          /* to save |pdf_offset| */
288     off_t gone;                 /* number of bytes that were flushed to output */
289 
290     char *printf_buf;           /* a scratch buffer for |pdf_printf| */
291 
292     time_t start_time;          /* when this job started */
293     char *start_time_str;       /* minimum size for time_str is 24: "D:YYYYmmddHHMMSS+HH'MM'" */
294 
295     strbuf_s *fb;               /* pointer to auxiliary buffer for font stuff */
296 
297     char *zipbuf;
298     z_stream *c_stream;         /* compression stream pointer */
299     zip_write_state_e zip_write_state;  /* which state of compression we are in */
300     int stream_deflate;         /* true, if stream dict has /Filter/FlateDecode */
301     int stream_writing;         /* true while writing stream */
302 
303     int pk_scale_factor;        /* this is just a preprocessed value that depends on
304                                    |pk_resolution| and |decimal_digits| */
305 
306     int img_page_group_val;     /* page group information pointer from included pdf or png images */
307     char *resname_prefix;       /* global prefix of resources name */
308 
309     int mem_size;               /* allocated size of |mem| array */
310     int *mem;
311     int mem_ptr;
312 
313     pdfstructure *pstruct;      /* utity structure keeping position status in PDF page stream */
314     posstructure *posstruct;    /* structure for positioning within page */
315 
316     int obj_tab_size;           /* allocated size of |obj_tab| array */
317     obj_entry *obj_tab;
318     int head_tab[HEAD_TAB_MAX + 1];     /* heads of the object lists in |obj_tab| */
319     struct avl_table *obj_tree[PDF_OBJ_TYPE_MAX + 1];   /* this is useful for finding the objects back */
320 
321     int pages_tail;
322     int obj_ptr;                /* objects counter */
323     int last_pages;             /* pointer to most recently generated pages object */
324     int last_page;              /* pointer to most recently generated page object */
325     int last_stream;            /* pointer to most recently generated stream */
326     off_t stream_length;        /* length of most recently generated stream */
327     off_t stream_length_offset; /* file offset of the last stream length */
328     int seek_write_length;      /* flag whether to seek back and write \.{/Length} */
329     int last_byte;              /* byte most recently written to PDF file; for \.{endstream} in new line */
330 
331     /* integer last_resources;     halfword to most recently generated Resources object.
332        TH: this used to be a local in pdf_shipout, but I would like to
333        be able to split that function into a pre- and post part */
334 
335     int obj_count;
336     int xform_count;
337     int ximage_count;
338 
339     pdf_resource_struct *page_resources;
340 
341     scaledpos page_size;        /* width and height of page being shipped */
342 
343     /* the variables from pdfdest */
344     int dest_names_size;
345     int dest_names_ptr;
346     dest_name_entry *dest_names;
347     /* the (static) variables from pdfoutline */
348     int first_outline;
349     int last_outline;
350     int parent_outline;
351     /* the pdf link stack */
352     pdf_link_stack_record link_stack[(pdf_max_link_level + 1)];
353     int link_stack_ptr;
354     /* the thread data */
355     int last_thread;            /* pointer to the last thread */
356     scaled_whd thread;
357     int last_thread_id;         /* identifier of the last thread */
358     int last_thread_named_id;   /* is identifier of the last thread named */
359     int thread_level;           /* depth of nesting of box containing the last thread */
360 
361     int f_cur;                  /* TeX font number */
362     int pdflua_ref;
363     int cave;                   /* stay away from previous PDF object */
364 
365     vf_struct *vfstruct;
366 } pdf_output_file;
367 
368 typedef pdf_output_file *PDF;
369 
370 #endif                          /* PDFTYPES_H */
371