| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2009-2011 Taco Hoekwater <[email protected]> |
| 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 |
|
| 22 |
#include "ptexlib.h" |
| 23 |
#include "lua/luatex-api.h" |
| 24 |
|
| 25 |
/*tex Write a raw \PDF\ object: */ |
| 26 |
|
| 27 |
void pdf_write_obj(PDF pdf, int k) |
| 28 |
{ |
| 29 |
lstring data; |
| 30 |
const_lstring st; |
| 31 |
/*tex The index into |data.s|: */ |
| 32 |
size_t li; |
| 33 |
int saved_compress_level ; |
| 34 |
int saved_objcompresslevel ; |
| 35 |
// int saved_stream_deflate ; |
| 36 |
/*tex Gives compressed objects for |\pdfvariable objcompresslevel| >= |OBJSTM_ALWAYS|: */ |
| 37 |
int os_threshold = OBJSTM_ALWAYS; |
| 38 |
/*tex Possibly a \LUA\ registry reference: */ |
| 39 |
int l = 0; |
| 40 |
int ll = 0; |
| 41 |
data.s = NULL; |
| 42 |
/*tex We can have an immediate object before we are initialized. */ |
| 43 |
ensure_output_state(pdf, ST_HEADER_WRITTEN); |
| 44 |
saved_compress_level = pdf->compress_level; |
| 45 |
saved_objcompresslevel = pdf->objcompresslevel; |
| 46 |
// saved_stream_deflate = pdf->stream_deflate; |
| 47 |
/*tex End of a ugly hack. */ |
| 48 |
if (obj_obj_pdfcompresslevel(pdf, k) > -1) { |
| 49 |
/*tex A value of |-1| means ``unset''. */ |
| 50 |
pdf->compress_level = obj_obj_pdfcompresslevel(pdf, k); |
| 51 |
if (pdf->compress_level == 0) { |
| 52 |
pdf->objcompresslevel = 0; |
| 53 |
} |
| 54 |
} |
| 55 |
if (obj_obj_objstm_threshold(pdf, k) != OBJSTM_UNSET) |
| 56 |
os_threshold = obj_obj_objstm_threshold(pdf, k); |
| 57 |
if (obj_obj_is_stream(pdf, k)) { |
| 58 |
pdf_begin_obj(pdf, k, OBJSTM_NEVER); |
| 59 |
pdf_begin_dict(pdf); |
| 60 |
l = obj_obj_stream_attr(pdf, k); |
| 61 |
if (l != LUA_NOREF) { |
| 62 |
lua_rawgeti(Luas, LUA_REGISTRYINDEX, l); |
| 63 |
if (lua_type(Luas,-1) != LUA_TSTRING) |
| 64 |
normal_error("pdf backend","invalid object"); |
| 65 |
st.s = lua_tolstring(Luas, -1, &li); |
| 66 |
st.l = li; |
| 67 |
lua_pop(Luas,1); |
| 68 |
pdf_check_space(pdf); |
| 69 |
pdf_out_block(pdf, st.s, st.l); |
| 70 |
pdf_set_space(pdf); |
| 71 |
luaL_unref(Luas, LUA_REGISTRYINDEX, l); |
| 72 |
obj_obj_stream_attr(pdf, k) = LUA_NOREF; |
| 73 |
} |
| 74 |
pdf_dict_add_streaminfo(pdf); |
| 75 |
pdf_end_dict(pdf); |
| 76 |
pdf_begin_stream(pdf); |
| 77 |
} else |
| 78 |
pdf_begin_obj(pdf, k, os_threshold); |
| 79 |
l = obj_obj_data(pdf, k); |
| 80 |
lua_rawgeti(Luas, LUA_REGISTRYINDEX, l); |
| 81 |
if (lua_type(Luas,-1) != LUA_TSTRING) |
| 82 |
normal_error("pdf backend","invalid object"); |
| 83 |
st.s = lua_tolstring(Luas, -1, &li); |
| 84 |
st.l = li; |
| 85 |
lua_pop(Luas, 1); |
| 86 |
if (obj_obj_is_file(pdf, k)) { |
| 87 |
/*tex The callback status value: */ |
| 88 |
boolean res = false; |
| 89 |
/*tex The callback found filename: */ |
| 90 |
const char *fnam = NULL; |
| 91 |
int callback_id; |
| 92 |
/*tex |st.s| is also |\0|-terminated, even as |lstring| */ |
| 93 |
fnam = luatex_find_file(st.s, find_data_file_callback); |
| 94 |
callback_id = callback_defined(read_data_file_callback); |
| 95 |
if (fnam && callback_id > 0) { |
| 96 |
boolean file_opened = false; |
| 97 |
res = run_callback(callback_id, "S->bSd", fnam, &file_opened, &data.s, &ll); |
| 98 |
data.l = (size_t) ll; |
| 99 |
if (!file_opened) |
| 100 |
normal_error("pdf backend", "cannot open file for embedding"); |
| 101 |
} else { |
| 102 |
/*tex The data file's |FILE*|: */ |
| 103 |
byte_file f; |
| 104 |
if (!fnam) |
| 105 |
fnam = st.s; |
| 106 |
if (!luatex_open_input(&f, fnam, kpse_tex_format, FOPEN_RBIN_MODE, true)) |
| 107 |
normal_error("pdf backend", "cannot open file for embedding"); |
| 108 |
res = read_data_file(f, &data.s, &ll); |
| 109 |
data.l = (size_t) ll; |
| 110 |
close_file(f); |
| 111 |
} |
| 112 |
if (data.l == 0L) |
| 113 |
normal_error("pdf backend", "empty file for embedding"); |
| 114 |
if (!res) |
| 115 |
normal_error("pdf backend", "error reading file for embedding"); |
| 116 |
tprint("<<"); |
| 117 |
tprint(st.s); |
| 118 |
pdf_out_block(pdf, (const char *) data.s, data.l); |
| 119 |
xfree(data.s); |
| 120 |
tprint(">>"); |
| 121 |
} else { |
| 122 |
pdf_out_block(pdf, st.s, st.l); |
| 123 |
} |
| 124 |
if (obj_obj_is_stream(pdf, k)) { |
| 125 |
pdf_end_stream(pdf); |
| 126 |
pdf_end_obj(pdf); |
| 127 |
} else { |
| 128 |
/*tex Here we do the |\n|. */ |
| 129 |
pdf_end_obj(pdf); |
| 130 |
} |
| 131 |
luaL_unref(Luas, LUA_REGISTRYINDEX, l); |
| 132 |
obj_obj_data(pdf, k) = LUA_NOREF; |
| 133 |
pdf->compress_level = saved_compress_level; |
| 134 |
pdf->objcompresslevel = saved_objcompresslevel; |
| 135 |
// pdf->stream_deflate = saved_stream_deflate; |
| 136 |
} |
| 137 |
|
| 138 |
void init_obj_obj(PDF pdf, int k) |
| 139 |
{ |
| 140 |
obj_obj_stream_attr(pdf, k) = LUA_NOREF; |
| 141 |
obj_obj_data(pdf, k) = LUA_NOREF; |
| 142 |
unset_obj_obj_is_stream(pdf, k); |
| 143 |
unset_obj_obj_is_file(pdf, k); |
| 144 |
unset_obj_obj_no_length(pdf, k); |
| 145 |
obj_obj_pdfcompresslevel(pdf, k) = -1; /* unset */ |
| 146 |
obj_obj_objstm_threshold(pdf, k) = OBJSTM_UNSET; /* unset */ |
| 147 |
} |
| 148 |
|
| 149 |
/*tex |
| 150 |
|
| 151 |
The |\pdfextension obj| primitive is used to create a ``raw'' object in |
| 152 |
the PDF output file. The object contents will be hold in memory and will be |
| 153 |
written out only when the object is referenced by |\pdfextension refobj|. |
| 154 |
When |\pdfextension obj| is used with |\immediate|, the object contents |
| 155 |
will be written out immediately. Objects referenced in the current page are |
| 156 |
appended into |pdf_obj_list|. |
| 157 |
|
| 158 |
*/ |
| 159 |
|
| 160 |
void scan_obj(PDF pdf) |
| 161 |
{ |
| 162 |
int k; |
| 163 |
lstring *st = NULL; |
| 164 |
if (scan_keyword("reserveobjnum")) { |
| 165 |
get_x_token(); |
| 166 |
if (cur_cmd != spacer_cmd) |
| 167 |
back_input(); |
| 168 |
pdf->obj_count++; |
| 169 |
k = pdf_create_obj(pdf, obj_type_obj, 0); |
| 170 |
} else { |
| 171 |
if (scan_keyword("useobjnum")) { |
| 172 |
scan_int(); |
| 173 |
k = cur_val; |
| 174 |
check_obj_type(pdf, obj_type_obj, k); |
| 175 |
if (is_obj_scheduled(pdf, k) || obj_data_ptr(pdf, k) != 0) |
| 176 |
normal_error("pdf backend", "scheduled object is already used"); |
| 177 |
} else { |
| 178 |
pdf->obj_count++; |
| 179 |
k = pdf_create_obj(pdf, obj_type_obj, 0); |
| 180 |
} |
| 181 |
obj_data_ptr(pdf, k) = pdf_get_mem(pdf, pdfmem_obj_size); |
| 182 |
init_obj_obj(pdf, k); |
| 183 |
if (scan_keyword("uncompressed")) { |
| 184 |
obj_obj_pdfcompresslevel(pdf, k) = 0; |
| 185 |
obj_obj_objstm_threshold(pdf, k) = OBJSTM_NEVER; |
| 186 |
} |
| 187 |
if (scan_keyword("stream")) { |
| 188 |
set_obj_obj_is_stream(pdf, k); |
| 189 |
if (scan_keyword("attr")) { |
| 190 |
scan_toks(false, true); |
| 191 |
st = tokenlist_to_lstring(def_ref, true); |
| 192 |
flush_list(def_ref); |
| 193 |
lua_pushlstring(Luas, (char *) st->s, st->l); |
| 194 |
obj_obj_stream_attr(pdf, k) = luaL_ref(Luas, LUA_REGISTRYINDEX); |
| 195 |
free_lstring(st); |
| 196 |
st = NULL; |
| 197 |
} |
| 198 |
} |
| 199 |
if (scan_keyword("file")) |
| 200 |
set_obj_obj_is_file(pdf, k); |
| 201 |
scan_toks(false, true); |
| 202 |
st = tokenlist_to_lstring(def_ref, true); |
| 203 |
flush_list(def_ref); |
| 204 |
lua_pushlstring(Luas, (char *) st->s, st->l); |
| 205 |
obj_obj_data(pdf, k) = luaL_ref(Luas, LUA_REGISTRYINDEX); |
| 206 |
free_lstring(st); |
| 207 |
st = NULL; |
| 208 |
} |
| 209 |
pdf_last_obj = k; |
| 210 |
} |
| 211 |
|
| 212 |
void scan_refobj(PDF pdf) |
| 213 |
{ |
| 214 |
scan_int(); |
| 215 |
check_obj_type(pdf, obj_type_obj, cur_val); |
| 216 |
new_whatsit(pdf_refobj_node); |
| 217 |
pdf_obj_objnum(tail_par) = cur_val; |
| 218 |
} |
| 219 |
|
| 220 |
void scan_refobj_lua(PDF pdf, int k) |
| 221 |
{ |
| 222 |
check_obj_type(pdf, obj_type_obj, k); |
| 223 |
new_whatsit(pdf_refobj_node); |
| 224 |
pdf_obj_objnum(tail_par) = k; |
| 225 |
} |
| 226 |
|
| 227 |
void pdf_ref_obj(PDF pdf, halfword p) |
| 228 |
{ |
| 229 |
if (!is_obj_scheduled(pdf, pdf_obj_objnum(p))) |
| 230 |
addto_page_resources(pdf, obj_type_obj, pdf_obj_objnum(p)); |
| 231 |
} |
| 232 |
|
| 233 |
void pdf_ref_obj_lua(PDF pdf, int k) |
| 234 |
{ |
| 235 |
if (!is_obj_scheduled(pdf, k)) |
| 236 |
addto_page_resources(pdf, obj_type_obj, k); |
| 237 |
} |