beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / pdf / pdfobj.w
blob7dd510615950fc46ed9d2bcb965eeaba82048b75
1 % pdfobj.w
3 % Copyright 2009-2011 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/>.
20 @ @c
21 #include "ptexlib.h"
22 #include "lua/luatex-api.h"
24 @ write a raw PDF object
27 void pdf_write_obj(PDF pdf, int k)
29 lstring data;
30 const_lstring st;
31 size_t li; /* index into |data.s| */
32 int saved_compress_level = pdf->compress_level;
33 int os_threshold = OBJSTM_ALWAYS; /* gives compressed objects for \.{\\pdfvariable objcompresslevel} >= |OBJSTM_ALWAYS| */
34 int l = 0; /* possibly a lua registry reference */
35 int ll = 0;
36 data.s = NULL;
37 if (obj_obj_pdfcompresslevel(pdf, k) > -1) /* -1 = "unset" */
38 pdf->compress_level = obj_obj_pdfcompresslevel(pdf, k);
39 if (obj_obj_objstm_threshold(pdf, k) != OBJSTM_UNSET)
40 os_threshold = obj_obj_objstm_threshold(pdf, k);
41 if (obj_obj_is_stream(pdf, k)) {
42 pdf_begin_obj(pdf, k, OBJSTM_NEVER);
43 pdf_begin_dict(pdf);
44 l = obj_obj_stream_attr(pdf, k);
45 if (l != LUA_NOREF) {
46 lua_rawgeti(Luas, LUA_REGISTRYINDEX, l);
47 if (lua_type(Luas,-1) != LUA_TSTRING)
48 normal_error("pdf backend","invalid object");
49 st.s = lua_tolstring(Luas, -1, &li);
50 st.l = li;
51 pdf_out_block(pdf, st.s, st.l);
52 if (st.s[st.l - 1] != '\n')
53 pdf_out(pdf, '\n');
54 luaL_unref(Luas, LUA_REGISTRYINDEX, l);
55 obj_obj_stream_attr(pdf, k) = LUA_NOREF;
57 pdf_dict_add_streaminfo(pdf);
58 pdf_end_dict(pdf);
59 pdf_begin_stream(pdf);
60 } else
61 pdf_begin_obj(pdf, k, os_threshold);
62 l = obj_obj_data(pdf, k);
63 lua_rawgeti(Luas, LUA_REGISTRYINDEX, l);
64 if (lua_type(Luas,-1) != LUA_TSTRING)
65 normal_error("pdf backend","invalid object");
66 st.s = lua_tolstring(Luas, -1, &li);
67 st.l = li;
68 lua_pop(Luas, 1);
69 if (obj_obj_is_file(pdf, k)) {
70 boolean res = false; /* callback status value */
71 const char *fnam = NULL; /* callback found filename */
72 int callback_id;
73 /* st.s is also |\0|-terminated, even as lstring */
74 fnam = luatex_find_file(st.s, find_data_file_callback);
75 callback_id = callback_defined(read_data_file_callback);
76 if (fnam && callback_id > 0) {
77 boolean file_opened = false;
78 res = run_callback(callback_id, "S->bSd", fnam, &file_opened, &data.s, &ll);
79 data.l = (size_t) ll;
80 if (!file_opened)
81 normal_error("pdf backend", "cannot open file for embedding");
82 } else {
83 byte_file f; /* the data file's FILE* */
84 if (!fnam)
85 fnam = st.s;
86 if (!luatex_open_input(&f, fnam, kpse_tex_format, FOPEN_RBIN_MODE, true))
87 normal_error("pdf backend", "cannot open file for embedding");
88 res = read_data_file(f, &data.s, &ll);
89 data.l = (size_t) ll;
90 close_file(f);
92 if (data.l == 0L)
93 normal_error("pdf backend", "empty file for embedding");
94 if (!res)
95 normal_error("pdf backend", "error reading file for embedding");
96 tprint("<<");
97 tprint(st.s);
98 pdf_out_block(pdf, (const char *) data.s, data.l);
99 xfree(data.s);
100 tprint(">>");
101 } else {
102 pdf_out_block(pdf, st.s, st.l);
104 if (obj_obj_is_stream(pdf, k)) {
105 pdf_end_stream(pdf);
106 pdf_end_obj(pdf);
107 } else /* here we do the \n */
108 pdf_end_obj(pdf);
109 luaL_unref(Luas, LUA_REGISTRYINDEX, l);
110 obj_obj_data(pdf, k) = LUA_NOREF;
111 pdf->compress_level = saved_compress_level;
114 @ @c
115 void init_obj_obj(PDF pdf, int k)
117 obj_obj_stream_attr(pdf, k) = LUA_NOREF;
118 obj_obj_data(pdf, k) = LUA_NOREF;
119 unset_obj_obj_is_stream(pdf, k);
120 unset_obj_obj_is_file(pdf, k);
121 obj_obj_pdfcompresslevel(pdf, k) = -1; /* unset */
122 obj_obj_objstm_threshold(pdf, k) = OBJSTM_UNSET; /* unset */
125 @ The \.{\\pdfextension obj} primitive is used to create a ``raw'' object in the
126 PDF output file. The object contents will be hold in memory and will be written
127 out only when the object is referenced by \.{\\pdfextension refobj}. When
128 \.{\\pdfextension obj} is used with \.{\\immediate}, the object contents will be
129 written out immediately. Objects referenced in the current page are appended into
130 |pdf_obj_list|.
133 void scan_obj(PDF pdf)
135 int k;
136 lstring *st = NULL;
137 if (scan_keyword("reserveobjnum")) {
138 get_x_token();
139 if (cur_cmd != spacer_cmd)
140 back_input();
141 pdf->obj_count++;
142 k = pdf_create_obj(pdf, obj_type_obj, 0);
143 } else {
144 if (scan_keyword("useobjnum")) {
145 scan_int();
146 k = cur_val;
147 check_obj_type(pdf, obj_type_obj, k);
148 if (is_obj_scheduled(pdf, k) || obj_data_ptr(pdf, k) != 0)
149 luaL_error(Luas, "object in use");
150 } else {
151 pdf->obj_count++;
152 k = pdf_create_obj(pdf, obj_type_obj, 0);
154 obj_data_ptr(pdf, k) = pdf_get_mem(pdf, pdfmem_obj_size);
155 init_obj_obj(pdf, k);
156 if (scan_keyword("uncompressed")) {
157 obj_obj_pdfcompresslevel(pdf, k) = 0;
158 obj_obj_objstm_threshold(pdf, k) = OBJSTM_NEVER;
160 if (scan_keyword("stream")) {
161 set_obj_obj_is_stream(pdf, k);
162 if (scan_keyword("attr")) {
163 scan_toks(false, true);
164 st = tokenlist_to_lstring(def_ref, true);
165 flush_list(def_ref);
166 lua_pushlstring(Luas, (char *) st->s, st->l);
167 obj_obj_stream_attr(pdf, k) = luaL_ref(Luas, LUA_REGISTRYINDEX);
168 free_lstring(st);
169 st = NULL;
172 if (scan_keyword("file"))
173 set_obj_obj_is_file(pdf, k);
174 scan_toks(false, true);
175 st = tokenlist_to_lstring(def_ref, true);
176 flush_list(def_ref);
177 lua_pushlstring(Luas, (char *) st->s, st->l);
178 obj_obj_data(pdf, k) = luaL_ref(Luas, LUA_REGISTRYINDEX);
179 free_lstring(st);
180 st = NULL;
182 pdf_last_obj = k;
185 @ @c
186 #define tail cur_list.tail_field
188 void scan_refobj(PDF pdf)
190 scan_int();
191 check_obj_type(pdf, obj_type_obj, cur_val);
192 new_whatsit(pdf_refobj_node);
193 pdf_obj_objnum(tail) = cur_val;
196 void scan_refobj_lua(PDF pdf, int k)
198 check_obj_type(pdf, obj_type_obj, k);
199 new_whatsit(pdf_refobj_node);
200 pdf_obj_objnum(tail) = k;
203 @ @c
204 void pdf_ref_obj(PDF pdf, halfword p)
206 if (!is_obj_scheduled(pdf, pdf_obj_objnum(p)))
207 addto_page_resources(pdf, obj_type_obj, pdf_obj_objnum(p));
210 @ @c
211 void pdf_ref_obj_lua(PDF pdf, int k)
213 if (!is_obj_scheduled(pdf, k))
214 addto_page_resources(pdf, obj_type_obj, k);