-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRightPanel.cpp
More file actions
357 lines (333 loc) · 8.99 KB
/
Copy pathRightPanel.cpp
File metadata and controls
357 lines (333 loc) · 8.99 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
// RightPanel.cpp
//
// Copyright 2004 by Taesoo Kwon.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
#include "stdafx.h"
#include "RightPanel.h"
#include "PDFwin.h"
#include "utility/operatorString.h"
#ifdef USE_LUABIND
#include "WrapperLua/BaselibLUA.h"
#include "WrapperLua/MainlibLUA.h"
#include <luabind/luabind.hpp>
#include <luabind/function.hpp>
//#include <luabind/policy.hpp>
#include <luabind/operator.hpp>
using namespace luabind;
#else
#include "WrapperLua/LUAwrapper.h"
#include "WrapperLua/OR_LUA_Stack.h"
#include "luna_baselib.h"
#include "luna_mainlib.h"
void Register_mainlib(lua_State* L);
void Register_baselib(lua_State* L);
#endif
#include "PDFWriter.h"
#include "utility/FltkAddon.h"
#include <iostream>
void reflow(CImage& inout, int desired_width, int min_gap, int max_gap, int thr_white, double right_max_margin, int figure_min_height);
void trimVertSpaces(CImage& inout, int min_gap, int max_gap, int thr_white) ;
TString processOption(const char* option)
{
TString opt=option;
opt.replace(") ", ")\n");
opt.replace(" (", " \n(");
return opt;
}
TString unprocessOption(const char* option)
{
TString opt=option;
opt.replace(")\n", ") ");
opt.replace(" \n(", " (");
return opt;
}
static int add_file_and_line(lua_State* L)
{
luaL_dostring(L, "dbg.traceBack()");
luaL_dostring(L, "dbg.console()");
return 1;
}
bool CreateDirectory(const char *PathToCreate);
bool DeleteAllFiles(const char* path, const char* mask,bool bConfirm);
#ifdef USE_LUABIND
static LUAwrapper* L=NULL;
#else
static lua_State* L=NULL;
#endif
static void _initLuaEnvironment(RightPanel* win)
{
#ifdef USE_LUABIND
delete(L);
L=new LUAwrapper();
luabind::set_pcall_callback(&add_file_and_line);
addMainlibModule(L->L);
// export member functions of PDFwin for use in LUA script.
// to understand the following codes, please refer to "luabind" manual.
module(L->L)[
def("reflow", &reflow),
def("trimVertSpaces", &trimVertSpaces),
def("CreateDirectory", (bool (*)(const char*))&CreateDirectory),
def("DeleteAllFiles", &DeleteAllFiles),
class_<PDFwin>("PDFwin")
.def_readonly("filename", &PDFwin::_filename)
.def_readonly("currPage", &PDFwin::mCurrPage)
.def("getRectImage_width", &PDFwin::getRectImage_width)
.def("load", &PDFwin::load)
.def("getDPI_width", &PDFwin::getDPI_width)
.def("getDPI_height", &PDFwin::getDPI_height)
.def("getRectImage_height", &PDFwin::getRectImage_height)
.def("setStatus", &PDFwin::setStatus)
.def("getNumPages", &PDFwin::getNumPages)
.def("getNumRects", &PDFwin::getNumRects)
.def("setCurPage", &PDFwin::setCurPage)
.def("pageChanged", &PDFwin::pageChanged)
.def("redraw", &PDFwin::redraw)
.def("deleteAllFiles", &PDFwin::deleteAllFiles)
.def("deleteAllFilesWithoutConfirm", &PDFwin::deleteAllFilesWithoutConfirm),
class_<PDFWriter>("PDFWriter")
.def(constructor<>())
.def("init", &PDFWriter::init)
.def("addPage", &PDFWriter::addPage)
.def("addPageColor", &PDFWriter::addPageColor)
.def("save", &PDFWriter::save)
.def("isValid",&PDFWriter::isValid)
];
L->setRef<PDFwin>("win", *(win->mPDFwin));
L->setRef<FlLayout>("panel", *win);
#else
if(L) lua_close(L);
L=lua_open();
luaopen_base(L);
luaL_openlibs(L);
Register_baselib(L);
Register_mainlib(L);
lunaStack ls(L);
ls.set<PDFwin>("win", (win->mPDFwin));
ls.set<FlLayout>("panel", win);
#endif
}
#ifdef USE_LUABIND
#define CATCH_LUABIND_ERROR(x, y) catch(luabind::error& e)\
{\
std::cout <<"lua error"<<x<<","<<e.what()<<"\n";\
int n=lua_gettop(e.state());\
Msg::msgBox("lua error %s", lua_tostring(e.state(), n));\
ASSERT(0);\
luaL_dostring(L->L, "dbg.traceBack()");\
luaL_dostring(L->L, "dbg.console()");\
_releaseScript(L, y);\
L=NULL;\
}
static void _releaseScript(LUAwrapper* L, FlLayout& l)
{
if(L)
{
luabind::call_function<void>(L->L, "dtor");
delete L;
}
}
#else
static void _releaseScript(lua_State* L, FlLayout& l)
{
if(L)
{
lunaStack l(L);
l.getglobal("dtor");
l.call(0,0);
lua_close(L);
L=NULL;
}
}
static void handleLUAerror(lua_State* L)
{
printf("handleLUAerror:\n");
luna_printStack(L);
luaL_dostring(L, "dbg.traceBack()");
luaL_dostring(L, "dbg.console()");
}
#endif
extern TString g_arg;
static void _loadScript(RightPanel* win, const char* script)
{
_initLuaEnvironment(win);
#ifdef USE_LUABIND
if(L)
{
try
{
if (g_arg.length()>0)
L->dostring(g_arg.ptr());
L->dofile(script);
}
CATCH_LUABIND_ERROR("_loadScript", *win)
}
if (L)
{
try
{
luabind::call_function<void>(L->L, "ctor");
}
CATCH_LUABIND_ERROR("ctor", *win)
}
#else
if(L)
{
if (g_arg.length()>0)
{
if (luaL_dostring(L, g_arg.ptr())==1) handleLUAerror(L);
}
if(luaL_dofile(L, script)==1)
handleLUAerror(L);
lunaStack l(L);
l.getglobal("ctor");
l.call(0,0);
}
#endif
}
RightPanel::RightPanel(int x, int y, int w, int h, PDFwin* pdfwin)
:FlLayout(x,y, w, h)
{
mPDFwin=pdfwin;
mPDFwin->mLayout=this;
_loadScript(this, "main.lua");
}
RightPanel::~RightPanel(void)
{
_releaseScript(L, *this);
}
#ifndef NO_SHOW_WIN
#include <FL/Fl_Native_File_Chooser.H>
#endif
void RightPanel::onCallback(FlLayout::Widget const& w, Fl_Widget * pWidget, int userData)
{
#ifdef USE_LUABIND
try
{
if(L)
{
bool res=luabind::call_function<bool>(L->L, "onCallback", w, userData);
if (res) return;
}
}
CATCH_LUABIND_ERROR("onCallBack", *this)
#else
if(L)
{
lunaStack l(L);
l.getglobal("onCallback");
l.push<FlLayout::Widget>(&w);
l<<userData;
l.call(2,1);
bool res;
l>>res;
if(res) return;
}
#endif
if(w.mId=="update")
{
mPDFwin->pageChanged();
mPDFwin->redraw();
}
else if(w.mId=="Use automatic segmentation")
{
if(w.checkButton()->value())
{
findLayout("Automatic segmentation")->activate();
redraw();
}
else
{
findLayout("Automatic segmentation")->deactivate();
redraw();
}
}
else if(w.mId=="Option")
{
TString fn=FlChooseFile("Choose option", "script","*.lua");
if(fn.length())
{
TString ff, dir;
ff=sz1::filename(fn, dir);
find<Fl_Input>("Option_Input")->value(processOption(ff.left(-4)));
redraw();
}
}
else if(w.mId=="Load a PDF file")
{
#ifndef NO_SHOW_WIN
Fl_Native_File_Chooser *chooser = new Fl_Native_File_Chooser();
chooser->type(Fl_Native_File_Chooser::BROWSE_FILE); // let user browse a single file
chooser->title("Open a file"); // optional title
//chooser->preset_file("/var/tmp/somefile.txt"); // optional filename preset
chooser->filter("PDF Files\t*.pdf"); // optional filter
TString fn;
switch ( chooser->show() ) {
case -1: // ERROR
fprintf(stderr, "*** ERROR show() failed:%s\n", chooser->errmsg());
break;
case 1: // CANCEL
fprintf(stderr, "*** CANCEL\n");
break;
default: // USER PICKED A FILE
fn=chooser->filename();
break;
}
//TString fn=fl_file_chooser("Choose a PDF file", "*.pdf", NULL);
if(fn.length())
{
mPDFwin->load(fn);
}
#endif
}
else if(w.mId=="Batch process")
{
#ifndef NO_SHOW_WIN
Fl_Native_File_Chooser *chooser = new Fl_Native_File_Chooser();
chooser->type(Fl_Native_File_Chooser::BROWSE_MULTI_FILE); // let user browse a single file
chooser->title("Open files"); // optional title
//chooser->preset_file("/var/tmp/somefile.txt"); // optional filename preset
chooser->filter("PDF Files\t*.pdf"); // optional filter
TStrings fn;
switch ( chooser->show() ) {
case -1: // ERROR
fprintf(stderr, "*** ERROR show() failed:%s\n", chooser->errmsg());
break;
case 1: // CANCEL
fprintf(stderr, "*** CANCEL\n");
break;
default: // USER PICKED A FILE
{
fn.resize(chooser->count());
for (int n = 0; n < chooser->count(); n++ )
fn[n]=chooser->filename(n);
}
break;
}
//TString fn=fl_file_chooser("Choose a PDF file", "*.pdf", NULL);
if(fn.size())
{
for(int i=0; i<fn.size(); i++)
{
mPDFwin->load(fn[i]);
onCallback(findWidget("Process all pages"), w.widgetRaw(), 0);
}
}
#endif
}
}