3 Copyright 2009-2013 Taco Hoekwater <taco@luatex.org>
5 This file is part of LuaTeX.
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.
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.
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/>.
26 #include <w2c/config.h>
29 # include "lua/luatex-api.h"
32 The prefix "PTEX" for the PDF keys is special to pdfTeX and has been registered with
36 # define pdfkeyprefix "PTEX"
38 # define i32round(a) (int) floor((a) + 0.5)
39 /*# define i64round(a) (int64_t) floor((a) + 0.5)*/
40 /*# define i64round(a) (int64_t) ( (double)(a+0.5) - ((int64_t)(a+0.5))%1 ) */
42 # define i64round(a) (int64_t) win32_floor((a) + 0.5)
44 # define i64round(a) (int64_t) lround((a))
48 # define MAX_OBJ_COMPRESS_LEVEL 3 /* maximum/clipping value for \pdfobjcompresslevel */
49 # define OBJSTM_UNSET -1 /* initial value */
50 # define OBJSTM_ALWAYS 1 /* \pdfobjcompresslevel >= OBJSTM_ALWAYS: put object into object stream */
51 # define OBJSTM_NEVER (MAX_OBJ_COMPRESS_LEVEL + 1) /* above maximum/clipping value for \pdfobjcompresslevel */
53 typedef int internal_font_number
; /* |font| in a |char_node| */
56 NO_ZIP
, /* no \.{ZIP} compression */
57 ZIP_WRITING
, /* \.{ZIP} compression being used */
58 ZIP_FINISH
/* finish \.{ZIP} compression */
67 This stucture holds everything that is needed for the actual pdf generation.
69 Because this structure interfaces with C++, it is not wise to use |boolean|
70 here (C++ has a boolean type built-in that is not compatible). Also, I have
71 plans to convert the backend code into a C library for use with e.g. standalone
72 lua. Together, this means that it is best only to use the standard C types and
73 the types explicitly defined in this header, and stay away from types like
74 |integer| and |eight_bits| that are used elsewhere in the \LUATEX\ sources.
79 int64_t m
; /* mantissa (significand) */
80 int e
; /* exponent * -1 */
90 typedef struct scaledpos_
{
98 typedef struct scaled_whd_
{
99 scaled wd
; /* TeX width */
100 scaled ht
; /* TeX height */
101 scaled dp
; /* TeX depth */
104 typedef struct posstructure_
{
105 scaledpos pos
; /* position on the page */
106 int dir
; /* direction of stuff to be put onto the page */
110 scaledpos curpos
; /* \pdflastpos position */
111 posstructure boxpos
; /* box dir and position of the box origin on the page */
112 scaled_whd boxdim
; /* box dimensions (in hlist/vlist coordinate system) */
113 } pos_info_structure
;
131 typedef struct pdf_object_list_
{
133 struct pdf_object_list_
*link
;
136 typedef enum { /* []TJ runs horizontal or vertical */
142 pdfpos pdf
; /* pos. on page (PDF page raster) */
143 pdfpos pdf_bt_pos
; /* pos. at begin of BT-ET group (PDF page raster) */
144 pdfpos pdf_tj_pos
; /* pos. at begin of TJ array (PDF page raster) */
145 pdffloat cw
; /* pos. within [(..)..]TJ array (glyph raster); cw.e = fractional digits in /Widths array */
146 pdffloat tj_delta
; /* rel. movement in [(..)..]TJ array (glyph raster) */
147 pdffloat fs
; /* font size in PDF units */
148 pdffloat fs_cur
; /* to check if fs.m has changed and Tf needed */
149 pdffloat tm0_cur
; /* to check if tm[0] has changed and Tm needed */
150 pdffloat cm
[6]; /* cm array */
151 pdffloat tm
[6]; /* Tm array */
152 double k1
; /* conv. factor from TeX sp to PDF page raster */
153 double k2
; /* conv. factor from PDF page raster to TJ array raster */
154 int f_pdf
; /* /F* font number, of unexpanded base font! */
155 int f_pdf_cur
; /* to check if f_pdf has changed and Tf needed */
156 writing_mode wmode
; /* PDF writing mode WMode (horizontal/vertical) */
157 pos_mode mode
; /* current positioning mode */
158 int ishex
; /* Whether the current char string is <> or () */
159 int need_tf
; /* flag whether Tf needs to be set */
160 int need_tm
; /* flag whether Tm needs to be set */
161 int cur_ex
; /* the current glyph ex factor */
164 typedef struct obj_entry_
{
176 int objtype
; /* integer int5 */
179 typedef struct dest_name_entry_
{
180 char *objname
; /* destination name */
181 int objnum
; /* destination object number */
184 # define pdf_max_link_level 10 /* maximum depth of link nesting */
186 typedef struct pdf_link_stack_record
{
188 int link_node
; /* holds a copy of the corresponding |pdf_start_link_node| */
189 int ref_link_node
; /* points to original |pdf_start_link_node|, or a
190 copy of |link_node| created by |append_link| in
191 case of multi-line link */
192 } pdf_link_stack_record
;
194 /* types of objects */
197 NB: |obj_type_thread| is the highest entry in |head_tab|, but there are a few
198 more linked lists that are handy.
202 obj_type_font
= 0, /* index of linked list of Fonts objects */
203 obj_type_outline
= 1, /* index of linked list of outline objects */
204 obj_type_dest
= 2, /* index of linked list of destination objects */
205 obj_type_obj
= 3, /* index of linked list of raw objects */
206 obj_type_xform
= 4, /* index of linked list of XObject forms */
207 obj_type_ximage
= 5, /* index of linked list of XObject images */
208 obj_type_thread
= 6, /* index of linked list of num article threads */
209 obj_type_pagestream
= 7, /* Page stream objects */
210 obj_type_page
= 8, /* /Page objects */
211 obj_type_pages
= 9, /* /Pages objects */
212 obj_type_catalog
= 10, /* /Catalog object */
213 obj_type_info
= 11, /* /Info object */
214 obj_type_link
= 12, /* link objects */
215 obj_type_annot
= 13, /* annotation objects */
216 obj_type_annots
= 14, /* /Annots objects */
217 obj_type_bead
= 15, /* thread bead objects */
218 obj_type_beads
= 16, /* /B objects (array of bead objects) */
219 obj_type_objstm
= 17, /* /ObjStm objects */
220 obj_type_others
= 18 /* any other objects (also not linked in any list) */
223 # define HEAD_TAB_MAX 6 /* obj_type_thread */
224 # define PDF_OBJ_TYPE_MAX 18 /* obj_type_others */
226 typedef struct pdf_resource_struct_
{
227 struct avl_table
*resources_tree
;
228 int last_resources
; /* halfword to most recently generated Resources object. */
229 } pdf_resource_struct
;
232 typedef struct os_obj_data_
{
237 typedef struct strbuf_s_
{
238 unsigned char *data
; /* a PDF stream buffer */
239 unsigned char *p
; /* pointer to the next character in the PDF stream buffer */
240 size_t size
; /* currently allocated size of the PDF stream buffer, grows dynamically */
241 size_t limit
; /* maximum allowed PDF stream buffer size */
244 typedef struct os_struct_
{
245 os_obj_data
*obj
; /* array of object stream objects */
247 buffer_e curbuf
; /* select into which buffer to output */
248 unsigned int cur_objstm
; /* number of current object stream object */
249 unsigned int idx
; /* index of object within object stream [1...PDF_OS_MAX_OBJS - 1] */
250 unsigned int ostm_ctr
; /* statistics: counter for object stream objects */
251 unsigned int o_ctr
; /* statistics: counter for objects within object streams */
255 # define packet_max_recursion 100 /* see |packet_cur_s| */
256 # define packet_stack_size 100
258 typedef struct packet_stack_record_
{
263 scaledpos pos
; /* c4, c5 */
264 } packet_stack_record
;
266 typedef struct vf_struct_
{
267 packet_stack_record
*packet_stack
; /* for "push" and "pop" */
268 int packet_stack_level
;
269 int packet_stack_minlevel
; /* to check stack underflow */
270 internal_font_number lf
; /* local font number, resolved */
271 int fs_f
; /* local font size */
272 int ex_glyph
; /* expansion value; ex_glyph = 0 means unexpanded */
273 int packet_cur_s
; /* do_vf_packet() nesting level */
274 posstructure
*refpos
;
275 int vflua
; /* flag, whether vf.*() functions are allowed */
278 typedef struct pdf_output_file_
{
279 FILE *file
; /* the PDF output file handle */
280 char *file_name
; /* the PDF output file name */
282 output_mode o_mode
; /* output mode (DVI/PDF/...) */
283 output_state o_state
;
284 /* generation parameters */
287 int image_hicolor
; /* boolean */
288 int image_apply_gamma
;
294 int inclusion_copy_font
;
295 int minor_version
; /* fixed minor part of the PDF version */
296 int compress_level
; /* level for zlib object stream compression */
297 int objcompresslevel
; /* fixed level for activating PDF object streams */
298 char *job_id_string
; /* the full job string */
300 int os_enable
; /* true if object streams are globally enabled */
301 os_struct
*os
; /* object stream structure pointer */
303 strbuf_s
*buf
; /* pointer to the current stream buffer (PDF stream, ObjStm, or Lua) */
305 off_t save_offset
; /* to save |pdf_offset| */
306 off_t gone
; /* number of bytes that were flushed to output */
308 char *printf_buf
; /* a scratch buffer for |pdf_printf| */
310 time_t start_time
; /* when this job started */
311 char *start_time_str
; /* minimum size for time_str is 24: "D:YYYYmmddHHMMSS+HH'MM'" */
313 strbuf_s
*fb
; /* pointer to auxiliary buffer for font stuff */
316 z_stream
*c_stream
; /* compression stream pointer */
317 zip_write_state_e zip_write_state
; /* which state of compression we are in */
318 int stream_deflate
; /* true, if stream dict has /Filter/FlateDecode */
319 int stream_writing
; /* true while writing stream */
321 int pk_scale_factor
; /* this is just a preprocessed value that depends on |pk_resolution| and |decimal_digits| */
323 int img_page_group_val
; /* page group information pointer from included pdf or png images */
324 char *resname_prefix
; /* global prefix of resources name */
326 int mem_size
; /* allocated size of |mem| array */
330 pdfstructure
*pstruct
; /* utity structure keeping position status in PDF page stream */
331 posstructure
*posstruct
; /* structure for positioning within page */
333 int obj_tab_size
; /* allocated size of |obj_tab| array */
335 int head_tab
[HEAD_TAB_MAX
+ 1]; /* heads of the object lists in |obj_tab| */
336 struct avl_table
*obj_tree
[PDF_OBJ_TYPE_MAX
+ 1]; /* this is useful for finding the objects back */
339 int obj_ptr
; /* objects counter */
340 int last_pages
; /* pointer to most recently generated pages object */
341 int last_page
; /* pointer to most recently generated page object */
342 int last_stream
; /* pointer to most recently generated stream */
343 off_t stream_length
; /* length of most recently generated stream */
344 off_t stream_length_offset
; /* file offset of the last stream length */
345 int seek_write_length
; /* flag whether to seek back and write \.{/Length} */
346 int last_byte
; /* byte most recently written to PDF file; for \.{endstream} in new line */
352 pdf_resource_struct
*page_resources
;
354 scaledpos page_size
; /* width and height of page being shipped */
356 /* the variables from pdfdest */
359 dest_name_entry
*dest_names
;
360 /* the (static) variables from pdfoutline */
364 /* the pdf link stack */
365 pdf_link_stack_record link_stack
[(pdf_max_link_level
+ 1)];
367 /* the thread data */
368 int last_thread
; /* pointer to the last thread */
370 int last_thread_id
; /* identifier of the last thread */
371 int last_thread_named_id
; /* is identifier of the last thread named */
372 int thread_level
; /* depth of nesting of box containing the last thread */
374 int f_cur
; /* TeX font number */
375 int cave
; /* stay away from previous PDF object */
380 typedef pdf_output_file
*PDF
;