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
/>.
22 #include
"lua/luatex-api.h"
24 @ write a raw PDF object
27 void pdf_write_obj
(PDF pdf
, int k
)
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
*/
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
);
44 l
= obj_obj_stream_attr
(pdf
, k
);
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);
51 pdf_out_block
(pdf
, st.s
, st.l
);
52 if
(st.s
[st.l
- 1] != '\n'
)
54 luaL_unref
(Luas
, LUA_REGISTRYINDEX
, l
);
55 obj_obj_stream_attr
(pdf
, k
) = LUA_NOREF
;
57 pdf_dict_add_streaminfo
(pdf
);
59 pdf_begin_stream
(pdf
);
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);
69 if
(obj_obj_is_file
(pdf
, k
)) {
70 boolean res
= false
; /* callback status value
*/
71 const char
*fnam
= NULL; /* callback found filename
*/
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);
81 normal_error
("pdf backend", "cannot open file for embedding");
83 byte_file f
; /* the data file's
FILE* */
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);
93 normal_error
("pdf backend", "empty file for embedding");
95 normal_error
("pdf backend", "error reading file for embedding");
98 pdf_out_block
(pdf
, (const char
*) data.s
, data.l
);
102 pdf_out_block
(pdf
, st.s
, st.l
);
104 if
(obj_obj_is_stream
(pdf
, k
)) {
107 } else
/* here we do the \n
*/
109 luaL_unref
(Luas
, LUA_REGISTRYINDEX
, l
);
110 obj_obj_data
(pdf
, k
) = LUA_NOREF
;
111 pdf-
>compress_level
= saved_compress_level
;
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
133 void scan_obj
(PDF pdf
)
137 if
(scan_keyword
("reserveobjnum")) {
139 if
(cur_cmd
!= spacer_cmd
)
142 k
= pdf_create_obj
(pdf
, obj_type_obj
, 0);
144 if
(scan_keyword
("useobjnum")) {
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");
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
);
166 lua_pushlstring
(Luas
, (char
*) st-
>s
, st-
>l
);
167 obj_obj_stream_attr
(pdf
, k
) = luaL_ref
(Luas
, LUA_REGISTRYINDEX
);
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
);
177 lua_pushlstring
(Luas
, (char
*) st-
>s
, st-
>l
);
178 obj_obj_data
(pdf
, k
) = luaL_ref
(Luas
, LUA_REGISTRYINDEX
);
186 #define tail cur_list.tail_field
188 void scan_refobj
(PDF pdf
)
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
;
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
));
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
);