summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/lua/lpdflib.c
blob: ab2761ea13d6c29f8904a7c727168c3c6a1aa626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* lpdflib.c
   
   Copyright 2006-2008 Taco Hoekwater <taco@luatex.org>

   This file is part of LuaTeX.

   LuaTeX is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 2 of the License, or (at your
   option) any later version.

   LuaTeX is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
   License for more details.

   You should have received a copy of the GNU General Public License along
   with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */

#include "luatex-api.h"
#include <ptexlib.h>

static const char _svn_version[] =
    "$Id: lpdflib.c 2329 2009-04-18 14:25:30Z hhenkel $ "
    "$URL: http://scm.foundry.supelec.fr/svn/luatex/trunk/source/texk/web2c/luatexdir/lua/lpdflib.c $";

static int findcurv(lua_State * L)
{
    int j;
    j = get_cur_v();
    lua_pushnumber(L, j);
    return 1;
}

static int findcurh(lua_State * L)
{
    int j;
    j = get_cur_h();
    lua_pushnumber(L, j);
    return 1;
}

int luapdfprint(lua_State * L)
{
    int n;
    unsigned i;
    size_t len;
    const char *outputstr, *st;
    ctm_transform_modes literal_mode;
    n = lua_gettop(L);
    if (!lua_isstring(L, -1)) {
        lua_pushstring(L, "no string to print");
        lua_error(L);
    }
    literal_mode = set_origin;
    if (n == 2) {
        if (!lua_isstring(L, -2)) {
            lua_pushstring(L, "invalid argument for print literal mode");
            lua_error(L);
        } else {
            outputstr = (char *) lua_tostring(L, -2);
            if (strcmp(outputstr, "direct") == 0)
                literal_mode = direct_always;
            else if (strcmp(outputstr, "page") == 0)
                literal_mode = direct_page;
            else {
                lua_pushstring(L, "invalid argument for print literal mode");
                lua_error(L);
            }
        }
    } else {
        if (n != 1) {
            lua_pushstring(L, "invalid number of arguments");
            lua_error(L);
        }
    }
    check_pdfminorversion();
    switch (literal_mode) {
    case (set_origin):
        pdf_goto_pagemode();
        pos = synch_p_with_c(cur);
        pdf_set_pos(pos.h, pos.v);
        break;
    case (direct_page):
        pdf_goto_pagemode();
        break;
    case (direct_always):
        pdf_end_string_nl();
        break;
    default:
        assert(0);
    }
    st = lua_tolstring(L, n, &len);
    for (i = 0; i < len; i++) {
        if (i % 16 == 0)
            pdfroom(16);
        pdf_buf[pdf_ptr++] = st[i];
    }
    return 0;
}

static int l_immediateobj(lua_State * L)
{
    unsigned i;
    size_t len;
    const char *st;
    if (!lua_isstring(L, -1))
        luaL_error(L, "pdf.immediateobj needs string value");
    pdf_create_obj(obj_type_others, 0);
    pdf_begin_obj(obj_ptr, 1);
    st = lua_tolstring(L, -1, &len);
    for (i = 0; i < len; i++) {
        if (i % 16 == 0)
            pdfroom(16);
        pdf_buf[pdf_ptr++] = st[i];
    }
    pdf_puts("\n");
    pdf_end_obj();
    lua_pop(L, 1);
    lua_pushinteger(L, obj_ptr);
    return 1;
}

static int getpdf(lua_State * L)
{
    char *st;
    if (lua_isstring(L, 2)) {
        st = (char *) lua_tostring(L, 2);
        if (st && *st) {
            if (*st == 'h')
                return findcurh(L);
            else if (*st == 'v')
                return findcurv(L);
        }
    }
    lua_pushnil(L);
    return 1;
}

static int setpdf(lua_State * L)
{
    return (L == NULL ? 0 : 0); /* for -Wall */
}

static const struct luaL_reg pdflib[] = {
    {"print", luapdfprint},
    {"immediateobj", l_immediateobj},
    {NULL, NULL}                /* sentinel */
};


int luaopen_pdf(lua_State * L)
{
    luaL_register(L, "pdf", pdflib);
    /* build meta table */
    luaL_newmetatable(L, "pdf_meta");
    lua_pushstring(L, "__index");
    lua_pushcfunction(L, getpdf);
    /* do these later, NYI */
    if (0) {
        lua_settable(L, -3);
        lua_pushstring(L, "__newindex");
        lua_pushcfunction(L, setpdf);
    }
    lua_settable(L, -3);
    lua_setmetatable(L, -2);    /* meta to itself */
    return 1;
}