| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2010 by Dominik Seichter * |
| 3 |
* [email protected] * |
| 4 |
* * |
| 5 |
* This program is free software; you can redistribute it and/or modify * |
| 6 |
* it under the terms of the GNU General Public License as published by * |
| 7 |
* the Free Software Foundation; either version 2 of the License, or * |
| 8 |
* (at your option) any later version. * |
| 9 |
* * |
| 10 |
* This program is distributed in the hope that it will be useful, * |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 |
* GNU General Public License for more details. * |
| 14 |
* * |
| 15 |
* You should have received a copy of the GNU General Public License * |
| 16 |
* along with this program; if not, write to the * |
| 17 |
* Free Software Foundation, Inc., * |
| 18 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 |
***************************************************************************/ |
| 20 |
|
| 21 |
#include "luaconverter.h" |
| 22 |
|
| 23 |
// Note: this is *not* lua.hpp shipped with lua 5.1, it's a wrapper |
| 24 |
// header we use to handle version differences between lua 5.1 and lua |
| 25 |
// 5.0 . |
| 26 |
#include "lua_compat.h" |
| 27 |
|
| 28 |
#include <stdexcept> |
| 29 |
#include <iostream> |
| 30 |
|
| 31 |
using namespace PoDoFo; |
| 32 |
|
| 33 |
#define FUNCTION_START_PAGE "start_page" |
| 34 |
#define FUNCTION_END_PAGE "end_page" |
| 35 |
#define FUNCTION_START_XOBJECT "start_xobject" |
| 36 |
#define FUNCTION_END_XOBJECT "end_xobject" |
| 37 |
#define FUNCTION_SET_STROKING_GRAY "set_stroking_color_gray" |
| 38 |
#define FUNCTION_SET_STROKING_RGB "set_stroking_color_rgb" |
| 39 |
#define FUNCTION_SET_STROKING_CMYK "set_stroking_color_cmyk" |
| 40 |
#define FUNCTION_SET_NON_STROKING_GRAY "set_non_stroking_color_gray" |
| 41 |
#define FUNCTION_SET_NON_STROKING_RGB "set_non_stroking_color_rgb" |
| 42 |
#define FUNCTION_SET_NON_STROKING_CMYK "set_non_stroking_color_cmyk" |
| 43 |
|
| 44 |
LuaMachina::LuaMachina() |
| 45 |
{ |
| 46 |
/* Init the Lua interpreter */ |
| 47 |
L = imp_lua_open(); |
| 48 |
if (!L) |
| 49 |
{ |
| 50 |
throw std::runtime_error("Whoops! Failed to open lua!"); |
| 51 |
} |
| 52 |
|
| 53 |
/* Init the Lua libraries we want users to have access to. |
| 54 |
* Note that the `os' and `io' libraries MUST NOT be included, |
| 55 |
* as providing access to those libraries to the user would |
| 56 |
* make running plan files unsafe. */ |
| 57 |
luaopen_base(L); |
| 58 |
luaopen_table(L); |
| 59 |
luaopen_string(L); |
| 60 |
luaopen_math(L); |
| 61 |
} |
| 62 |
|
| 63 |
LuaMachina::~LuaMachina() |
| 64 |
{ |
| 65 |
lua_close(L); |
| 66 |
} |
| 67 |
|
| 68 |
LuaConverter::LuaConverter( const std::string & sLuaScript ) |
| 69 |
: IConverter() |
| 70 |
{ |
| 71 |
if( imp_lua_dofile(m_machina.State(), sLuaScript.c_str()) ) |
| 72 |
{ |
| 73 |
std::cerr << "Unable to process Lua script:\"" |
| 74 |
<< lua_tostring(m_machina.State(), -1)<<"\"" << std::endl; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
LuaConverter::~LuaConverter() |
| 79 |
{ |
| 80 |
} |
| 81 |
|
| 82 |
void LuaConverter::StartPage( PdfPage*, int nPageNumber ) |
| 83 |
{ |
| 84 |
lua_getglobal( m_machina.State(), FUNCTION_START_PAGE ); |
| 85 |
lua_pushnumber( m_machina.State(), static_cast<double>(nPageNumber) ); |
| 86 |
// Lua 5.1 only |
| 87 |
//lua_pushinteger( m_machina.State(), nPageNumber ); |
| 88 |
lua_call( m_machina.State(), 1, 0 ); |
| 89 |
} |
| 90 |
|
| 91 |
void LuaConverter::EndPage( PdfPage*, int nPageNumber ) |
| 92 |
{ |
| 93 |
lua_getglobal( m_machina.State(), FUNCTION_END_PAGE ); |
| 94 |
lua_pushnumber( m_machina.State(), static_cast<double>(nPageNumber) ); |
| 95 |
// Lua 5.1 only |
| 96 |
//lua_pushinteger( m_machina.State(), nPageNumber ); |
| 97 |
lua_call( m_machina.State(), 1, 0 ); |
| 98 |
} |
| 99 |
|
| 100 |
void LuaConverter::StartXObject( PdfXObject* ) |
| 101 |
{ |
| 102 |
lua_getglobal( m_machina.State(), FUNCTION_START_XOBJECT ); |
| 103 |
lua_call( m_machina.State(), 0, 0 ); |
| 104 |
} |
| 105 |
|
| 106 |
void LuaConverter::EndXObject( PdfXObject* ) |
| 107 |
{ |
| 108 |
lua_getglobal( m_machina.State(), FUNCTION_END_XOBJECT ); |
| 109 |
lua_call( m_machina.State(), 0, 0 ); |
| 110 |
} |
| 111 |
|
| 112 |
PdfColor LuaConverter::GetColorFromReturnValue(const char* pszFunctionName) |
| 113 |
{ |
| 114 |
int top; |
| 115 |
double value; |
| 116 |
std::vector<double> colors; |
| 117 |
size_t len; |
| 118 |
|
| 119 |
luaL_checktype(m_machina.State(), 1, LUA_TTABLE); |
| 120 |
len = imp_lua_getn( m_machina.State(), -1 ); |
| 121 |
// Lua 5.1 only |
| 122 |
//len = lua_objlen( m_machina.State(), -1 ); |
| 123 |
|
| 124 |
for( int i = 1; i <= static_cast<int>(len); i++ ) |
| 125 |
{ |
| 126 |
lua_rawgeti( m_machina.State(), -1, i ); |
| 127 |
top = lua_gettop( m_machina.State() ); |
| 128 |
value = static_cast<double>(lua_tonumber(m_machina.State(), top)); |
| 129 |
lua_pop(m_machina.State(), 1); |
| 130 |
|
| 131 |
colors.push_back( value ); |
| 132 |
} |
| 133 |
|
| 134 |
switch( len ) |
| 135 |
{ |
| 136 |
case 1: |
| 137 |
return PdfColor( colors[0] ); |
| 138 |
case 3: |
| 139 |
return PdfColor( colors[0], |
| 140 |
colors[1], |
| 141 |
colors[2] ); |
| 142 |
case 4: |
| 143 |
return PdfColor( colors[0], |
| 144 |
colors[1], |
| 145 |
colors[2], |
| 146 |
colors[3] ); |
| 147 |
default: |
| 148 |
{ |
| 149 |
PdfError::LogMessage( eLogSeverity_Error, "Array length is %i returned by %s.\n", len, pszFunctionName ); |
| 150 |
PODOFO_RAISE_ERROR_INFO( ePdfError_CannotConvertColor, |
| 151 |
"Arrays returned from Lua must be { g }, { r,g,b } or { c,m,y,k }!" ); |
| 152 |
break; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return PdfColor(); |
| 157 |
} |
| 158 |
|
| 159 |
PdfColor LuaConverter::SetStrokingColorGray( const PdfColor & rColor ) |
| 160 |
{ |
| 161 |
lua_getglobal( m_machina.State(), FUNCTION_SET_STROKING_GRAY ); |
| 162 |
lua_pushnumber( m_machina.State(), rColor.GetGrayScale() ); |
| 163 |
lua_call( m_machina.State(), 1, 1 ); |
| 164 |
|
| 165 |
return this->GetColorFromReturnValue( FUNCTION_SET_STROKING_GRAY ); |
| 166 |
} |
| 167 |
|
| 168 |
PdfColor LuaConverter::SetStrokingColorRGB( const PdfColor & rColor ) |
| 169 |
{ |
| 170 |
lua_getglobal( m_machina.State(), FUNCTION_SET_STROKING_RGB ); |
| 171 |
lua_pushnumber( m_machina.State(), rColor.GetRed() ); |
| 172 |
lua_pushnumber( m_machina.State(), rColor.GetGreen() ); |
| 173 |
lua_pushnumber( m_machina.State(), rColor.GetBlue() ); |
| 174 |
lua_call( m_machina.State(), 3, 1 ); |
| 175 |
|
| 176 |
return this->GetColorFromReturnValue( FUNCTION_SET_STROKING_RGB ); |
| 177 |
} |
| 178 |
|
| 179 |
PdfColor LuaConverter::SetStrokingColorCMYK( const PdfColor & rColor ) |
| 180 |
{ |
| 181 |
lua_getglobal( m_machina.State(), FUNCTION_SET_STROKING_CMYK ); |
| 182 |
lua_pushnumber( m_machina.State(), rColor.GetCyan() ); |
| 183 |
lua_pushnumber( m_machina.State(), rColor.GetMagenta() ); |
| 184 |
lua_pushnumber( m_machina.State(), rColor.GetYellow() ); |
| 185 |
lua_pushnumber( m_machina.State(), rColor.GetBlack() ); |
| 186 |
lua_call( m_machina.State(), 4, 1 ); |
| 187 |
|
| 188 |
return this->GetColorFromReturnValue( FUNCTION_SET_STROKING_CMYK ); |
| 189 |
} |
| 190 |
|
| 191 |
PdfColor LuaConverter::SetNonStrokingColorGray( const PdfColor & rColor ) |
| 192 |
{ |
| 193 |
lua_getglobal( m_machina.State(), FUNCTION_SET_NON_STROKING_GRAY ); |
| 194 |
lua_pushnumber( m_machina.State(), rColor.GetGrayScale() ); |
| 195 |
lua_call( m_machina.State(), 1, 1 ); |
| 196 |
|
| 197 |
return this->GetColorFromReturnValue( FUNCTION_SET_NON_STROKING_GRAY ); |
| 198 |
} |
| 199 |
|
| 200 |
PdfColor LuaConverter::SetNonStrokingColorRGB( const PdfColor & rColor ) |
| 201 |
{ |
| 202 |
lua_getglobal( m_machina.State(), FUNCTION_SET_NON_STROKING_RGB ); |
| 203 |
lua_pushnumber( m_machina.State(), rColor.GetRed() ); |
| 204 |
lua_pushnumber( m_machina.State(), rColor.GetGreen() ); |
| 205 |
lua_pushnumber( m_machina.State(), rColor.GetBlue() ); |
| 206 |
lua_call( m_machina.State(), 3, 1 ); |
| 207 |
|
| 208 |
return this->GetColorFromReturnValue( FUNCTION_SET_NON_STROKING_RGB ); |
| 209 |
} |
| 210 |
|
| 211 |
PdfColor LuaConverter::SetNonStrokingColorCMYK( const PdfColor & rColor ) |
| 212 |
{ |
| 213 |
lua_getglobal( m_machina.State(), FUNCTION_SET_NON_STROKING_CMYK ); |
| 214 |
lua_pushnumber( m_machina.State(), rColor.GetCyan() ); |
| 215 |
lua_pushnumber( m_machina.State(), rColor.GetMagenta() ); |
| 216 |
lua_pushnumber( m_machina.State(), rColor.GetYellow() ); |
| 217 |
lua_pushnumber( m_machina.State(), rColor.GetBlack() ); |
| 218 |
lua_call( m_machina.State(), 4, 1 ); |
| 219 |
|
| 220 |
return this->GetColorFromReturnValue( FUNCTION_SET_NON_STROKING_CMYK ); |
| 221 |
} |
| 222 |
|