~ubuntu-branches/ubuntu/maverick/luatex/maverick

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2009-12-25 09:47:05 UTC
  • mfrom: (1.1.9 upstream) (4.2.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091225094705-y33rpflo8t4u9nag
Tags: 0.50.0-1
* new upstream release
* disable fix-hurd-ftbfs patch, included upstream
* disable upstram-fixes, included upstream
* disable ubuntu_libpoppler-0.11, not needed anymore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* pdfobj.c
 
2
   
 
3
   Copyright 2009 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
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 $";
 
23
 
 
24
#include "ptexlib.h"
 
25
#include "lua/luatex-api.h"
 
26
 
 
27
int pdf_last_obj;
 
28
 
 
29
/* write a raw PDF object */
 
30
 
 
31
void pdf_write_obj(PDF pdf, int k)
 
32
{
 
33
    lstring data, st;
 
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 */
 
38
    int ll = 0;
 
39
    data.s = st.s = NULL;
 
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);
 
47
        if (l != LUA_NOREF) {
 
48
            lua_rawgeti(Luas, LUA_REGISTRYINDEX, l);
 
49
            assert(lua_isstring(Luas, -1));
 
50
            st.s = (unsigned char *) lua_tolstring(Luas, -1, &li);
 
51
            st.l = (unsigned)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')
 
55
                pdf_out(pdf, '\n');
 
56
            luaL_unref(Luas, LUA_REGISTRYINDEX, l);
 
57
            obj_obj_stream_attr(pdf, k) = LUA_NOREF;
 
58
        }
 
59
        pdf_begin_stream(pdf);
 
60
    } else
 
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);
 
66
    st.l = (unsigned)li;
 
67
    if (obj_obj_is_file(pdf, k)) {
 
68
        boolean res = false;    /* callback status value */
 
69
        char *fnam = NULL;      /* callback found filename */
 
70
        int callback_id;
 
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;
 
79
            if (!file_opened)
 
80
                pdf_error("ext5", "cannot open file for embedding");
 
81
        } else {
 
82
            byte_file f;        /* the data file's FILE* */
 
83
            if (!fnam)
 
84
                fnam = (char *) st.s;
 
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;
 
90
            close_file(f);
 
91
        }
 
92
        if ((int) data.l == 0)
 
93
            pdf_error("ext5", "empty file for embedding");
 
94
        if (!res)
 
95
            pdf_error("ext5", "error reading file for embedding");
 
96
        tprint("<<");
 
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')
 
101
            pdf_out(pdf, '\n');
 
102
        if (data.s != NULL)
 
103
            xfree(data.s);
 
104
        tprint(">>");
 
105
    } else {
 
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')
 
109
            pdf_out(pdf, '\n');
 
110
    }
 
111
    if (obj_obj_is_stream(pdf, k))
 
112
        pdf_end_stream(pdf);
 
113
    else
 
114
        pdf_end_obj(pdf);
 
115
    luaL_unref(Luas, LUA_REGISTRYINDEX, l);
 
116
    obj_obj_data(pdf, k) = LUA_NOREF;
 
117
    pdf->compress_level = saved_compress_level;
 
118
}
 
119
 
 
120
void init_obj_obj(PDF pdf, int k)
 
121
{
 
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 */
 
128
}
 
129
 
 
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
 
135
   |pdf_obj_list|. */
 
136
 
 
137
void scan_obj(PDF pdf)
 
138
{
 
139
    int k;
 
140
    lstring *st = NULL;
 
141
    if (scan_keyword("reserveobjnum")) {
 
142
        /* Scan an optional space */
 
143
        get_x_token();
 
144
        if (cur_cmd != spacer_cmd)
 
145
            back_input();
 
146
        incr(pdf->obj_count);
 
147
        pdf_create_obj(pdf, obj_type_obj, pdf->sys_obj_ptr + 1);
 
148
        k = pdf->sys_obj_ptr;
 
149
    } else {
 
150
        if (scan_keyword("useobjnum")) {
 
151
            scan_int();
 
152
            k = cur_val;
 
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");
 
156
        } else {
 
157
            incr(pdf->obj_count);
 
158
            pdf_create_obj(pdf, obj_type_obj, pdf->sys_obj_ptr + 1);
 
159
            k = pdf->sys_obj_ptr;
 
160
        }
 
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;
 
166
        }
 
167
        if (scan_keyword("stream")) {
 
168
            set_obj_obj_is_stream(pdf, k);
 
169
            if (scan_keyword("attr")) {
 
170
                scan_pdf_ext_toks();
 
171
                st = tokenlist_to_lstring(def_ref, true);
 
172
                flush_list(def_ref);
 
173
                lua_pushlstring(Luas, (char *) st->s, st->l);
 
174
                obj_obj_stream_attr(pdf, k) = luaL_ref(Luas, LUA_REGISTRYINDEX);
 
175
                free_lstring(st);
 
176
                st = NULL;
 
177
            }
 
178
        }
 
179
        if (scan_keyword("file"))
 
180
            set_obj_obj_is_file(pdf, k);
 
181
        scan_pdf_ext_toks();
 
182
        st = tokenlist_to_lstring(def_ref, true);
 
183
        flush_list(def_ref);
 
184
        lua_pushlstring(Luas, (char *) st->s, st->l);
 
185
        obj_obj_data(pdf, k) = luaL_ref(Luas, LUA_REGISTRYINDEX);
 
186
        free_lstring(st);
 
187
        st = NULL;
 
188
    }
 
189
    pdf_last_obj = k;
 
190
}
 
191
 
 
192
#define tail          cur_list.tail_field
 
193
 
 
194
void scan_refobj(PDF pdf)
 
195
{
 
196
    scan_int();
 
197
    check_obj_exists(pdf, obj_type_obj, cur_val);
 
198
    new_whatsit(pdf_refobj_node);
 
199
    pdf_obj_objnum(tail) = cur_val;
 
200
}
 
201
 
 
202
void pdf_ref_obj(PDF pdf, halfword p)
 
203
{
 
204
    if (!is_obj_scheduled(pdf, pdf_obj_objnum(p)))
 
205
        append_object_list(pdf, obj_type_obj, pdf_obj_objnum(p));
 
206
}