3
Copyright 2009 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
static const char __svn_version[] =
21
"$Id: pdfobj.c 3298 2009-12-24 15:54:33Z taco $"
22
"$URL: http://foundry.supelec.fr/svn/luatex/tags/beta-0.50.0/source/texk/web2c/luatexdir/pdf/pdfobj.c $";
25
#include "lua/luatex-api.h"
29
/* write a raw PDF object */
31
void pdf_write_obj(PDF pdf, int k)
34
size_t i, li; /* index into |data.s| */
35
int saved_compress_level = pdf->compress_level;
36
int os_level = 1; /* gives compressed objects for \pdfobjcompresslevel > 0 */
37
int l = 0; /* possibly a lua registry reference */
40
if (obj_obj_pdfcompresslevel(pdf, k) > -1) /* -1 = "unset" */
41
pdf->compress_level = obj_obj_pdfcompresslevel(pdf, k);
42
if (obj_obj_pdfoslevel(pdf, k) > -1) /* -1 = "unset" */
43
os_level = obj_obj_pdfoslevel(pdf, k);
44
if (obj_obj_is_stream(pdf, k)) {
45
pdf_begin_dict(pdf, k, 0);
46
l = obj_obj_stream_attr(pdf, k);
48
lua_rawgeti(Luas, LUA_REGISTRYINDEX, l);
49
assert(lua_isstring(Luas, -1));
50
st.s = (unsigned char *) lua_tolstring(Luas, -1, &li);
52
for (i = 0; i < st.l; i++)
53
pdf_out(pdf, st.s[i]);
54
if (st.s[(int) st.l - 1] != '\n')
56
luaL_unref(Luas, LUA_REGISTRYINDEX, l);
57
obj_obj_stream_attr(pdf, k) = LUA_NOREF;
59
pdf_begin_stream(pdf);
61
pdf_begin_obj(pdf, k, os_level);
62
l = obj_obj_data(pdf, k);
63
lua_rawgeti(Luas, LUA_REGISTRYINDEX, l);
64
assert(lua_isstring(Luas, -1));
65
st.s = (unsigned char *) lua_tolstring(Luas, -1, &li);
67
if (obj_obj_is_file(pdf, k)) {
68
boolean res = false; /* callback status value */
69
char *fnam = NULL; /* callback found filename */
71
/* st.s is also '\0'-terminated, even as lstring */
72
fnam = luatex_find_file((char *) st.s, find_data_file_callback);
73
callback_id = callback_defined(read_data_file_callback);
74
if (fnam && callback_id > 0) {
75
boolean file_opened = false;
76
res = run_callback(callback_id, "S->bSd", fnam,
77
&file_opened, &data.s, &ll);
78
data.l = (unsigned)ll;
80
pdf_error("ext5", "cannot open file for embedding");
82
byte_file f; /* the data file's FILE* */
85
if (!luatex_open_input
86
(&f, fnam, kpse_tex_format, FOPEN_RBIN_MODE, true))
87
pdf_error("ext5", "cannot open file for embedding");
88
res = read_data_file(f, &data.s, &ll);
89
data.l = (unsigned)ll;
92
if ((int) data.l == 0)
93
pdf_error("ext5", "empty file for embedding");
95
pdf_error("ext5", "error reading file for embedding");
97
tprint((char *) st.s);
98
for (i = 0; i < data.l; i++)
99
pdf_out(pdf, data.s[i]);
100
if (!obj_obj_is_stream(pdf, k) && data.s[(int) data.l - 1] != '\n')
106
for (i = 0; i < st.l; i++)
107
pdf_out(pdf, st.s[i]);
108
if (!obj_obj_is_stream(pdf, k) && st.s[(int) st.l - 1] != '\n')
111
if (obj_obj_is_stream(pdf, k))
115
luaL_unref(Luas, LUA_REGISTRYINDEX, l);
116
obj_obj_data(pdf, k) = LUA_NOREF;
117
pdf->compress_level = saved_compress_level;
120
void init_obj_obj(PDF pdf, int k)
122
obj_obj_stream_attr(pdf, k) = LUA_NOREF;
123
obj_obj_data(pdf, k) = LUA_NOREF;
124
unset_obj_obj_is_stream(pdf, k);
125
unset_obj_obj_is_file(pdf, k);
126
obj_obj_pdfcompresslevel(pdf, k) = -1; /* unset */
127
obj_obj_pdfoslevel(pdf, k) = -1; /* unset */
130
/* The \.{\\pdfobj} primitive is used to create a ``raw'' object in the PDF
131
output file. The object contents will be hold in memory and will be written
132
out only when the object is referenced by \.{\\pdfrefobj}. When \.{\\pdfobj}
133
is used with \.{\\immediate}, the object contents will be written out
134
immediately. Objects referenced in the current page are appended into
137
void scan_obj(PDF pdf)
141
if (scan_keyword("reserveobjnum")) {
142
/* Scan an optional space */
144
if (cur_cmd != spacer_cmd)
146
incr(pdf->obj_count);
147
pdf_create_obj(pdf, obj_type_obj, pdf->sys_obj_ptr + 1);
148
k = pdf->sys_obj_ptr;
150
if (scan_keyword("useobjnum")) {
153
check_obj_exists(pdf, obj_type_obj, k);
154
if (is_obj_scheduled(pdf, k) || obj_data_ptr(pdf, k) != 0)
155
luaL_error(Luas, "object in use");
157
incr(pdf->obj_count);
158
pdf_create_obj(pdf, obj_type_obj, pdf->sys_obj_ptr + 1);
159
k = pdf->sys_obj_ptr;
161
obj_data_ptr(pdf, k) = pdf_get_mem(pdf, pdfmem_obj_size);
162
init_obj_obj(pdf, k);
163
if (scan_keyword("uncompressed")) {
164
obj_obj_pdfcompresslevel(pdf, k) = 0; /* \pdfcompresslevel = 0 */
165
obj_obj_pdfoslevel(pdf, k) = 0;
167
if (scan_keyword("stream")) {
168
set_obj_obj_is_stream(pdf, k);
169
if (scan_keyword("attr")) {
171
st = tokenlist_to_lstring(def_ref, true);
173
lua_pushlstring(Luas, (char *) st->s, st->l);
174
obj_obj_stream_attr(pdf, k) = luaL_ref(Luas, LUA_REGISTRYINDEX);
179
if (scan_keyword("file"))
180
set_obj_obj_is_file(pdf, k);
182
st = tokenlist_to_lstring(def_ref, true);
184
lua_pushlstring(Luas, (char *) st->s, st->l);
185
obj_obj_data(pdf, k) = luaL_ref(Luas, LUA_REGISTRYINDEX);
192
#define tail cur_list.tail_field
194
void scan_refobj(PDF pdf)
197
check_obj_exists(pdf, obj_type_obj, cur_val);
198
new_whatsit(pdf_refobj_node);
199
pdf_obj_objnum(tail) = cur_val;
202
void pdf_ref_obj(PDF pdf, halfword p)
204
if (!is_obj_scheduled(pdf, pdf_obj_objnum(p)))
205
append_object_list(pdf, obj_type_obj, pdf_obj_objnum(p));