Debian Codegraph @

podofo/tools/podofoimpose/planreader_lua.cpp

raw
1 //
2 // C++ Implementation: planreader_lua
3 //
4 // Description:
5 //
6 //
7 // Author: Pierre Marchand <[email protected]>, (C) 2008
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include "planreader_lua.h"
14
15 #include <stdexcept>
16 #include <iostream>
17
18 // Note: this is *not* lua.hpp shipped with lua 5.1, it's a wrapper
19 // header we use to handle version differences between lua 5.1 and lua
20 // 5.0 .
21 #include "lua_compat.h"
22
23 LuaMachina::LuaMachina()
24 {
25 /* Init the Lua interpreter */
26 L = imp_lua_open();
27 if (!L)
28 {
29 throw std::runtime_error("Whoops! Failed to open lua!");
30 }
31
32 /* Init the Lua libraries we want users to have access to.
33 * Note that the `os' and `io' libraries MUST NOT be included,
34 * as providing access to those libraries to the user would
35 * make running plan files unsafe. */
36 luaopen_base(L);
37 luaopen_table(L);
38 luaopen_string(L);
39 luaopen_math(L);
40 }
41
42 LuaMachina::~LuaMachina()
43 {
44 lua_close(L);
45 }
46
47 PlanReader_Lua::PlanReader_Lua(const std::string & planfile, PoDoFo::Impose::ImpositionPlan * ip)
48 {
49 // std::cerr<<"PlanReader_Lua::PlanReader_Lua "<< planfile <<std::endl;
50 plan = ip;
51
52 lua_pushcfunction(L.State(), &PlanReader_Lua::PushRecord);
53 lua_setglobal(L.State(), "PushRecord");
54
55 lua_pushlightuserdata(L.State(), static_cast<void*>(this));
56 lua_setglobal(L.State(), "This");
57
58 setNumber("PageCount", plan->sourceVars.PageCount);
59 setNumber("SourceWidth", plan->sourceVars.PageWidth );
60 setNumber("SourceHeight", plan->sourceVars.PageHeight);
61
62 // imp_lua_dofile is a wrapper around luaL_dofile for Lua 5.0/5.1 compat.
63 if(imp_lua_dofile(L.State(), planfile.c_str()))
64 {
65 std::cerr<<"Unable to process Lua script:\"" <<lua_tostring(L.State(), -1)<<"\""<<std::endl ;
66 }
67 else // if not reached, the plan remains invalid
68 {
69 if(hasGlobal("PageWidth"))
70 plan->setDestWidth(getNumber("PageWidth"));
71 if(hasGlobal("PageHeight"))
72 plan->setDestHeight(getNumber("PageHeight"));
73 if(hasGlobal("Scale"))
74 plan->setScale(getNumber("Scale"));
75 if(hasGlobal("BoundingBox"))
76 plan->setBoundingBox(getString("BoundingBox"));
77 }
78
79 }
80
81 PlanReader_Lua::~ PlanReader_Lua()
82 { }
83
84 int PlanReader_Lua::PushRecord ( lua_State * L )
85 {
86 /* TODO: check stack for space!
87 I would be glad to do that, but I don’t know how - pm
88 */
89 if ( ! ( lua_isnumber ( L, 1 ) &&
90 lua_isnumber ( L, 2 ) &&
91 lua_isnumber ( L, 3 ) &&
92 lua_isnumber ( L, 4 ) &&
93 lua_isnumber ( L, 5 ) ) )
94 {
95 throw std::runtime_error ( "One or more arguments to PushRecord were not numbers" );
96 }
97
98 /* Get the instance of the reader which runs the script */
99 lua_getglobal ( L , "This" );
100 if ( ! lua_islightuserdata ( L, -1 ) )
101 throw std::runtime_error ( "\"This\" is not valid" ); // ;-)
102 PlanReader_Lua *that = static_cast<PlanReader_Lua*> ( lua_touserdata ( L, -1 ) );
103 lua_pop ( L, 1 );
104
105 // std::cerr<<"PlanReader_Lua::PushRecord "<<lua_tonumber ( L, 1 )<<" "<<lua_tonumber ( L, 2 )<<" "<<lua_tonumber ( L, 3 )<<" "<<lua_tonumber ( L, 4 )<<" "<<lua_tonumber ( L, 5 )<<std::endl;
106
107 /* and add a new record to it */
108 PoDoFo::Impose::PageRecord P(
109 lua_tonumber ( L, 1 ),
110 lua_tonumber ( L, 2 ),
111 lua_tonumber ( L, 3 ),
112 lua_tonumber ( L, 4 ),
113 lua_tonumber ( L, 5 ));
114
115 if (lua_isnumber ( L, 6 ))
116 P.scaleX = lua_tonumber ( L, 6 );
117 if (lua_isnumber ( L, 7 ))
118 P.scaleY = lua_tonumber ( L, 7 );
119
120 if(P.isValid())
121 that->plan->push_back ( P );
122
123 lua_pop ( L, 5 );
124
125 return 0;
126 }
127
128 double PlanReader_Lua::getNumber(const std::string & name)
129 {
130 lua_getglobal(L.State(), name.c_str());
131 if (!lua_isnumber(L.State(), -1))
132 {
133 std::string errString = name + " is non-number";
134 throw std::runtime_error(errString.c_str());
135 }
136 double d = lua_tonumber(L.State(), -1);
137 lua_pop(L.State(), 1);
138 return d;
139 }
140
141 void PlanReader_Lua::setNumber(const std::string & name, double value)
142 {
143 lua_pushnumber(L.State(), value);
144 lua_setglobal(L.State(), name.c_str()); /* pops stack */
145 }
146
147 bool PlanReader_Lua::hasGlobal(const std::string & name)
148 {
149 bool ret(true);
150 lua_getglobal(L.State(), name.c_str());
151 if(lua_isnil(L.State(), -1) > 0)
152 {
153 ret = false;
154 }
155 lua_pop(L.State(), 1);
156 return ret;
157 }
158
159 std::string PlanReader_Lua::getString(const std::string& name)
160 {
161 lua_getglobal(L.State(), name.c_str());
162 if (!lua_isstring(L.State(), -1))
163 {
164 std::string errString = name + " is non-string";
165 throw std::runtime_error(errString.c_str());
166 }
167 std::string s( lua_tostring(L.State(), -1) );
168 lua_pop(L.State(), 1);
169 return s;
170 }
171