~ubuntu-branches/ubuntu/wily/luatex/wily

« back to all changes in this revision

Viewing changes to source/texk/web2c/luatexdir/pdf/pdfobj.w

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2010-04-29 00:47:19 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20100429004719-o42etkqe90n97b9e
Tags: 0.60.1-1
* new upstream release, adapt build-script patch
* disable patch: upstream-epstopdf_cc_no_xpdf_patching, included upstream
* disable patch: libpoppler-0.12, not needed anymore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
% pdfobj.w
 
2
 
 
3
% Copyright 2009-2010 Taco Hoekwater <taco@@luatex.org>
 
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
@ @c
 
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 $";
 
24
 
 
25
#include "ptexlib.h"
 
26
 
 
27
@ @c
 
28
#include "lua/luatex-api.h"
 
29
 
 
30
int pdf_last_obj;
 
31
 
 
32
@ write a raw PDF object 
 
33
 
 
34
@c
 
35
void pdf_write_obj(PDF pdf, int k)
 
36
{
 
37
    lstring data;
 
38
    const_lstring st;
 
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 */
 
43
    int ll = 0;
 
44
    data.s = NULL;
 
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);
 
52
        if (l != LUA_NOREF) {
 
53
            lua_rawgeti(Luas, LUA_REGISTRYINDEX, l);
 
54
            assert(lua_isstring(Luas, -1));
 
55
            st.s = lua_tolstring(Luas, -1, &li);
 
56
            st.l = li;
 
57
            for (i = 0; i < st.l; i++)
 
58
                pdf_out(pdf, st.s[i]);
 
59
            if (st.s[st.l - 1] != '\n')
 
60
                pdf_out(pdf, '\n');
 
61
            luaL_unref(Luas, LUA_REGISTRYINDEX, l);
 
62
            obj_obj_stream_attr(pdf, k) = LUA_NOREF;
 
63
        }
 
64
        pdf_begin_stream(pdf);
 
65
    } else
 
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);
 
71
    st.l = li;
 
72
    if (obj_obj_is_file(pdf, k)) {
 
73
        boolean res = false;    /* callback status value */
 
74
        const char *fnam = NULL;        /* callback found filename */
 
75
        int callback_id;
 
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);
 
83
            data.l = (size_t) ll;
 
84
            if (!file_opened)
 
85
                pdf_error("ext5", "cannot open file for embedding");
 
86
        } else {
 
87
            byte_file f;        /* the data file's FILE* */
 
88
            if (!fnam)
 
89
                fnam = st.s;
 
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);
 
94
            data.l = (size_t) ll;
 
95
            close_file(f);
 
96
        }
 
97
        if (data.l == 0L)
 
98
            pdf_error("ext5", "empty file for embedding");
 
99
        if (!res)
 
100
            pdf_error("ext5", "error reading file for embedding");
 
101
        tprint("<<");
 
102
        tprint(st.s);
 
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')
 
106
            pdf_out(pdf, '\n');
 
107
        xfree(data.s);
 
108
        tprint(">>");
 
109
    } else {
 
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')
 
113
            pdf_out(pdf, '\n');
 
114
    }
 
115
    if (obj_obj_is_stream(pdf, k))
 
116
        pdf_end_stream(pdf);
 
117
    else
 
118
        pdf_end_obj(pdf);
 
119
    luaL_unref(Luas, LUA_REGISTRYINDEX, l);
 
120
    obj_obj_data(pdf, k) = LUA_NOREF;
 
121
    pdf->compress_level = saved_compress_level;
 
122
}
 
123
 
 
124
@ @c
 
125
void init_obj_obj(PDF pdf, int k)
 
126
{
 
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 */
 
133
}
 
134
 
 
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
 
140
   |pdf_obj_list|. 
 
141
 
 
142
@c
 
143
void scan_obj(PDF pdf)
 
144
{
 
145
    int k;
 
146
    lstring *st = NULL;
 
147
    if (scan_keyword("reserveobjnum")) {
 
148
        /* Scan an optional space */
 
149
        get_x_token();
 
150
        if (cur_cmd != spacer_cmd)
 
151
            back_input();
 
152
        incr(pdf->obj_count);
 
153
        pdf_create_obj(pdf, obj_type_obj, pdf->sys_obj_ptr + 1);
 
154
        k = pdf->sys_obj_ptr;
 
155
    } else {
 
156
        if (scan_keyword("useobjnum")) {
 
157
            scan_int();
 
158
            k = cur_val;
 
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");
 
162
        } else {
 
163
            incr(pdf->obj_count);
 
164
            pdf_create_obj(pdf, obj_type_obj, pdf->sys_obj_ptr + 1);
 
165
            k = pdf->sys_obj_ptr;
 
166
        }
 
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;
 
172
        }
 
173
        if (scan_keyword("stream")) {
 
174
            set_obj_obj_is_stream(pdf, k);
 
175
            if (scan_keyword("attr")) {
 
176
                scan_pdf_ext_toks();
 
177
                st = tokenlist_to_lstring(def_ref, true);
 
178
                flush_list(def_ref);
 
179
                lua_pushlstring(Luas, (char *) st->s, st->l);
 
180
                obj_obj_stream_attr(pdf, k) = luaL_ref(Luas, LUA_REGISTRYINDEX);
 
181
                free_lstring(st);
 
182
                st = NULL;
 
183
            }
 
184
        }
 
185
        if (scan_keyword("file"))
 
186
            set_obj_obj_is_file(pdf, k);
 
187
        scan_pdf_ext_toks();
 
188
        st = tokenlist_to_lstring(def_ref, true);
 
189
        flush_list(def_ref);
 
190
        lua_pushlstring(Luas, (char *) st->s, st->l);
 
191
        obj_obj_data(pdf, k) = luaL_ref(Luas, LUA_REGISTRYINDEX);
 
192
        free_lstring(st);
 
193
        st = NULL;
 
194
    }
 
195
    pdf_last_obj = k;
 
196
}
 
197
 
 
198
@ @c
 
199
#define tail          cur_list.tail_field
 
200
 
 
201
void scan_refobj(PDF pdf)
 
202
{
 
203
    scan_int();
 
204
    check_obj_exists(pdf, obj_type_obj, cur_val);
 
205
    new_whatsit(pdf_refobj_node);
 
206
    pdf_obj_objnum(tail) = cur_val;
 
207
}
 
208
 
 
209
@ @c
 
210
void pdf_ref_obj(PDF pdf, halfword p)
 
211
{
 
212
    if (!is_obj_scheduled(pdf, pdf_obj_objnum(p)))
 
213
        addto_page_resources(pdf, obj_type_obj, pdf_obj_objnum(p));
 
214
}