3
% Copyright 2009-2010 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/>.
21
static const char _svn_version[] =
22
"$Id: pdfobj.w 3571 2010-04-02 13:50:45Z taco $"
23
"$URL: http://foundry.supelec.fr/svn/luatex/tags/beta-0.60.1/source/texk/web2c/luatexdir/pdf/pdfobj.w $";
28
#include "lua/luatex-api.h"
32
@ write a raw PDF object
35
void pdf_write_obj(PDF pdf, int k)
39
size_t i, li; /* index into |data.s| */
40
int saved_compress_level = pdf->compress_level;
41
int os_level = 1; /* gives compressed objects for \.{\\pdfobjcompresslevel} $>$ 0 */
42
int l = 0; /* possibly a lua registry reference */
45
if (obj_obj_pdfcompresslevel(pdf, k) > -1) /* -1 = "unset" */
46
pdf->compress_level = obj_obj_pdfcompresslevel(pdf, k);
47
if (obj_obj_pdfoslevel(pdf, k) > -1) /* -1 = "unset" */
48
os_level = obj_obj_pdfoslevel(pdf, k);
49
if (obj_obj_is_stream(pdf, k)) {
50
pdf_begin_dict(pdf, k, 0);
51
l = obj_obj_stream_attr(pdf, k);
53
lua_rawgeti(Luas, LUA_REGISTRYINDEX, l);
54
assert(lua_isstring(Luas, -1));
55
st.s = lua_tolstring(Luas, -1, &li);
57
for (i = 0; i < st.l; i++)
58
pdf_out(pdf, st.s[i]);
59
if (st.s[st.l - 1] != '\n')
61
luaL_unref(Luas, LUA_REGISTRYINDEX, l);
62
obj_obj_stream_attr(pdf, k) = LUA_NOREF;
64
pdf_begin_stream(pdf);
66
pdf_begin_obj(pdf, k, os_level);
67
l = obj_obj_data(pdf, k);
68
lua_rawgeti(Luas, LUA_REGISTRYINDEX, l);
69
assert(lua_isstring(Luas, -1));
70
st.s = lua_tolstring(Luas, -1, &li);
72
if (obj_obj_is_file(pdf, k)) {
73
boolean res = false; /* callback status value */
74
const char *fnam = NULL; /* callback found filename */
76
/* st.s is also |\0|-terminated, even as lstring */
77
fnam = luatex_find_file(st.s, find_data_file_callback);
78
callback_id = callback_defined(read_data_file_callback);
79
if (fnam && callback_id > 0) {
80
boolean file_opened = false;
81
res = run_callback(callback_id, "S->bSd", fnam,
82
&file_opened, &data.s, &ll);
85
pdf_error("ext5", "cannot open file for embedding");
87
byte_file f; /* the data file's FILE* */
90
if (!luatex_open_input
91
(&f, fnam, kpse_tex_format, FOPEN_RBIN_MODE, true))
92
pdf_error("ext5", "cannot open file for embedding");
93
res = read_data_file(f, &data.s, &ll);
98
pdf_error("ext5", "empty file for embedding");
100
pdf_error("ext5", "error reading file for embedding");
103
for (i = 0; i < data.l; i++)
104
pdf_out(pdf, data.s[i]);
105
if (!obj_obj_is_stream(pdf, k) && data.s[data.l - 1] != '\n')
110
for (i = 0; i < st.l; i++)
111
pdf_out(pdf, st.s[i]);
112
if (!obj_obj_is_stream(pdf, k) && st.s[st.l - 1] != '\n')
115
if (obj_obj_is_stream(pdf, k))
119
luaL_unref(Luas, LUA_REGISTRYINDEX, l);
120
obj_obj_data(pdf, k) = LUA_NOREF;
121
pdf->compress_level = saved_compress_level;
125
void init_obj_obj(PDF pdf, int k)
127
obj_obj_stream_attr(pdf, k) = LUA_NOREF;
128
obj_obj_data(pdf, k) = LUA_NOREF;
129
unset_obj_obj_is_stream(pdf, k);
130
unset_obj_obj_is_file(pdf, k);
131
obj_obj_pdfcompresslevel(pdf, k) = -1; /* unset */
132
obj_obj_pdfoslevel(pdf, k) = -1; /* unset */
135
@ The \.{\\pdfobj} primitive is used to create a ``raw'' object in the PDF
136
output file. The object contents will be hold in memory and will be written
137
out only when the object is referenced by \.{\\pdfrefobj}. When \.{\\pdfobj}
138
is used with \.{\\immediate}, the object contents will be written out
139
immediately. Objects referenced in the current page are appended into
143
void scan_obj(PDF pdf)
147
if (scan_keyword("reserveobjnum")) {
148
/* Scan an optional space */
150
if (cur_cmd != spacer_cmd)
152
incr(pdf->obj_count);
153
pdf_create_obj(pdf, obj_type_obj, pdf->sys_obj_ptr + 1);
154
k = pdf->sys_obj_ptr;
156
if (scan_keyword("useobjnum")) {
159
check_obj_exists(pdf, obj_type_obj, k);
160
if (is_obj_scheduled(pdf, k) || obj_data_ptr(pdf, k) != 0)
161
luaL_error(Luas, "object in use");
163
incr(pdf->obj_count);
164
pdf_create_obj(pdf, obj_type_obj, pdf->sys_obj_ptr + 1);
165
k = pdf->sys_obj_ptr;
167
obj_data_ptr(pdf, k) = pdf_get_mem(pdf, pdfmem_obj_size);
168
init_obj_obj(pdf, k);
169
if (scan_keyword("uncompressed")) {
170
obj_obj_pdfcompresslevel(pdf, k) = 0; /* \pdfcompresslevel = 0 */
171
obj_obj_pdfoslevel(pdf, k) = 0;
173
if (scan_keyword("stream")) {
174
set_obj_obj_is_stream(pdf, k);
175
if (scan_keyword("attr")) {
177
st = tokenlist_to_lstring(def_ref, true);
179
lua_pushlstring(Luas, (char *) st->s, st->l);
180
obj_obj_stream_attr(pdf, k) = luaL_ref(Luas, LUA_REGISTRYINDEX);
185
if (scan_keyword("file"))
186
set_obj_obj_is_file(pdf, k);
188
st = tokenlist_to_lstring(def_ref, true);
190
lua_pushlstring(Luas, (char *) st->s, st->l);
191
obj_obj_data(pdf, k) = luaL_ref(Luas, LUA_REGISTRYINDEX);
199
#define tail cur_list.tail_field
201
void scan_refobj(PDF pdf)
204
check_obj_exists(pdf, obj_type_obj, cur_val);
205
new_whatsit(pdf_refobj_node);
206
pdf_obj_objnum(tail) = cur_val;
210
void pdf_ref_obj(PDF pdf, halfword p)
212
if (!is_obj_scheduled(pdf, pdf_obj_objnum(p)))
213
addto_page_resources(pdf, obj_type_obj, pdf_obj_objnum(p));