-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
359 lines (324 loc) · 10 KB
/
Copy pathmain.lua
File metadata and controls
359 lines (324 loc) · 10 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
358
359
require('mylib')
require('config')
function processOption(option)
option=string.gsub(option,"%) ", ")\n");
option=string.gsub(option," %(", " \n(");
return option;
end
function unprocessOption(option)
option=string.gsub(option, "%)\n", ") ");
option=string.gsub(option, " \n%(", " (");
return option
end
function loadPreset(fn)
local fno,msg=loadfile(fn)
if not fno then print(msg) return end
fno()
local layout=panel:findLayout("Automatic segmentation");
layout:findWidget("MIN gap"):sliderValue(MIN_gap);
layout:findWidget("Margin"):sliderValue(margin);
layout:findWidget("N columns"):sliderValue(N_columns);
layout:findWidget("white point"):sliderValue(white_point);
layout:findWidget("Crop T"):sliderValue(crop_T);
layout:findWidget("Crop B"):sliderValue(crop_B);
layout:findWidget("Crop L"):sliderValue(crop_L);
layout:findWidget("Crop R"):sliderValue(crop_R);
panel:findWidget("Option_Input"):inputValue(processOption(option));
local options=panel:findWidget("options")
local argMenu=table.find(goptions, option)-1
--print("argMenu", argMenu)
options:menuValue(argMenu)
panel:redraw();
end
function processPresetName(fn)
return string.sub(os.processFileName(fn), 1,-5)
end
function processOptionName(fn)
return string.sub(os.processFileName(fn), 1,-5)
end
function setPresetMenu()
local presetMenu=panel:findWidget('presets')
-- load preset
presets=os.glob("presets/*.lua")
local function comp(a,b)
local fn=processPresetName(a)
if fn=="default preset" then return true end
return a<b
end
table.sort(presets, comp)
if device.supported_presets then
device.supported_presets['default preset']=true
presets=array.filter(function(f) return device.supported_presets[processPresetName(f)] end,
presets)
end
presetMenu:menuSize(#presets)
for i=1,#presets do
local shortc
if i-1<10 then
shortc="FL_ALT+"..tostring(i-1)
end
local preset=processPresetName(presets[i])
presetMenu:menuItem(i-1,preset ,shortc)
end
presetMenu:menuValue(0)
end
function setOptionMenu()
local optionMenu=panel:findWidget('options')
-- load option
options=os.glob("script/*.lua")
--printTable(options)
local function comp(a,b)
return a<b
end
table.sort(options, comp)
if device.supported_options then
options=array.filter(function(f) return device.supported_options[processOptionName(f)] end,
options)
end
for i=1, #options do
options[i]=processOptionName(options[i])
end
optionMenu:menuSize(#options)
for i=1,#options do
optionMenu:menuItem(i-1,options[i])
end
optionMenu:menuValue(0)
goptions=options
end
function ctor()
panel:create("Button", "Load a PDF file", "Load a PDF file");
panel:widget(0):buttonShortcut("FL_ALT+l");
panel:widget(0):buttonTooltip("Alt+L");
if os.isUnix() then
panel:create("Button", "Load a PDF file (native)", "Load a PDF file (native)");
panel:widget(0):buttonShortcut("FL_ALT+n");
panel:widget(0):buttonTooltip("Alt+N");
end
panel:create("Choice", "presets")
--panel:create("Button", "Preset", "default preset");
setPresetMenu();
-- load devices
panel:create("Choice", "devices")
local ndevices=0
local idevice=0
gdevices={}
for k,v in pairs(devices) do
ndevices=ndevices+1
gdevices[ndevices]={k,v}
if device==v then
idevice=ndevices
end
end
panel:widget(0):menuSize(ndevices+1)
panel:widget(0):menuItem(0, 'choose a device')
for i,v in ipairs(gdevices) do
panel:widget(0):menuItem(i, v[1])
end
panel:widget(0):menuValue(idevice)
panel:create("Check_Button", "Use automatic segmentation", "Use automatic segmentation");
panel:widget(0):checkButtonValue(1);
panel:create("Layout", "Automatic segmentation", "segmentation parameters");
panel:layout(0):create("Value_Slider", "MIN gap","MIN gap", 1);
panel:layout(0):widget(0):sliderRange(0.0, 10.0);
panel:layout(0):create("Value_Slider", "MIN text-gap","MIN text-gap");
panel:layout(0):widget(0):sliderRange(1.0, 10.0);
panel:layout(0):widget(0):sliderValue(2.0);
panel:layout(0):create("Value_Slider", "Margin","Margin");
panel:layout(0):widget(0):sliderRange(0.5, 10.0);
panel:layout(0):create("Value_Slider", "N columns","N columns");
panel:layout(0):widget(0):sliderRange(0.7, 10);
panel:layout(0):widget(0):sliderStep(0.1);
panel:layout(0):create("Value_Slider", "white point","white point");
panel:layout(0):widget(0):sliderRange(230, 255);
panel:layout(0):widget(0):sliderStep(1);
panel:layout(0):create("Value_Slider", "Crop T","Crop Top");
panel:layout(0):widget(0):sliderRange(0, 20);
panel:layout(0):widget(0):sliderStep(0.1);
panel:layout(0):create("Value_Slider", "Crop B","Crop Bttm");
panel:layout(0):widget(0):sliderRange(0, 20);
panel:layout(0):widget(0):sliderStep(0.1);
panel:layout(0):create("Value_Slider", "Crop L","Crop Left");
panel:layout(0):widget(0):sliderRange(0, 20);
panel:layout(0):widget(0):sliderStep(0.1);
panel:layout(0):create("Value_Slider", "Crop R","Crop right");
panel:layout(0):widget(0):sliderRange(0, 20);
panel:layout(0):widget(0):sliderStep(0.1);
panel:layout(0):create("Button", "update","update");
panel:layout(0):widget(0):buttonShortcut("FL_ALT+u")
panel:layout(0):widget(0):buttonTooltip("ALT+u")
panel:layout(0):updateLayout();
panel:setWidgetHeight(60);
panel:setUniformGuidelines(5);
panel:create("Input", "Option_Input", "Option",1);
panel:widget(0):inputType("FL_MULTILINE_OUTPUT");
panel:resetToDefault()
-- panel:create("Button", "Option", "Change option", 1);
panel:create("Choice", "options","",0)
setOptionMenu()
panel:create("Button", "Process current page", "Process current page",0);
panel:widget(0):deactivate();
if output_to_pdf then
if device and device.output_format==".cbz" then
panel:create("Button", "Convert processed pages to PDF", "Convert processed pages to CBZ")
else
panel:create("Button", "Convert processed pages to PDF", "Convert processed pages to PDF")
end
panel:widget(0):deactivate();
end
panel:create("Button", "Process all pages", "Process all pages",0);
panel:widget(0):deactivate();
panel:widget(0):buttonShortcut("FL_ALT+p");
panel:create("Button", "Batch process", "Batch process",0);
panel:create("Box", "Status");
panel:updateLayout();
loadPreset("presets/default preset.lua")
if filename then
print(filename)
win:load(filename)
filename=nil
end
if auto then
print("Automatic processing:")
g_automatic_mode=true
onCallback(panel:findWidget("Process all pages"), nil)
panel('exit',0)
end
end
function dtor()
print('')
end
function Fltk.ChooseFile(title, path, mask, write)
if write==nil then write=false end
local fn=Fltk.chooseFile(title, path, mask,write)
if fn=="" then return nil end
return fn
end
function setDefaultUI()
setPresetMenu()
setDefault()
setOptionMenu()
end
function onCallback(w, userData)
if w:id()=="Load a PDF file (native)" then
local fn=os.capture("python filechooser.py")
if fn~='Closed, no files selected' then
print(fn..':')
win:load(fn)
end
elseif w:id()=="devices" then
local dev=gdevices[w:menuValue()]
device=dev[2] or device
print(dev[1]..' selected', dev[2])
setDefaultUI()
loadPreset("presets/default preset.lua")
win:redraw()
panel:redraw()
elseif w:id()=="Convert processed pages to PDF" then
local outdir=string.sub(win.filename, 1, -5)
local files=os.glob(outdir.."/*"..output_format)
printTable(files)
table.sort(files)
local outpdf, fn
local function open(fn)
if os.isUnix() then
print('gnome-open "'..fn..'"')
os.execute('gnome-open "'..fn..'"')
else
os.execute('start "'..fn..'"')
end
end
if device and device.output_format==".cbz" then
fn=outdir.."_1.cbz"
outpdf=CBZwriter:new(fn)
elseif device and device.output_format==".xml" then
fn=outdir.."_1.xml"
outpdf=XMLwriter:new(fn,book_pages, outdir)
outpdf:init()
outpdf:addPage()
outpdf:save(fn)
open(string.sub(fn,1,-4).."pdf")
return
else
outpdf=PDFWriter()
fn=outdir.."_1.pdf"
end
outpdf:init()
if not os.isUnix() then
for i=1,#files do
files[i]=string.gsub(files[i],"/","\\")
end
end
for i=1,#files do
print(files[i])
local image=CImage()
image:Load(files[i])
if color_depth>8 then
outpdf:addPageColor(image)
else
outpdf:addPage(image, color_depth)
end
collectgarbage();
end
outpdf:save(fn)
open(fn)
collectgarbage();
elseif w:id()=="presets" then
local fn=presets[w:menuValue()+1]
local filename=os.processFileName(fn)
--w:buttonSetLabel(string.sub(filename,1, -5))
loadPreset(fn)
win:pageChanged();
win:redraw();
panel:redraw();
return true
elseif (w:id()=="Option") then
local fn=Fltk.ChooseFile("Choose option", "script", "*.lua");
if fn then
local fn2=os.filename(fn)
if device.supported_options then
if not device.supported_options[fn2] then
util.msgBox('Option "'..string.sub(fn2,1,-5)..'" is not supported for '..(device.device_name or "the device").."!")
return true
end
end
panel:findWidget("Option_Input"):inputValue(processOption(string.sub(fn, 1,-5)))
end
--print('hihi')
return true
elseif (w:id()=="options") then
local menuText=w:menuText()
panel:findWidget("Option_Input"):inputValue(processOption(menuText))
--print('hihi')
return true
elseif(w:id()=="Process current page" or w:id()=="Process all pages") then
panel:findWidget("Process current page"):deactivate();
panel:findWidget("Process all pages"):deactivate();
local bAll=w:id()=="Process all pages";
local fn="script/"..unprocessOption(panel:findWidget("Option_Input"):inputValue())..".lua";
local fno,msg=loadfile(fn)
if not fno then
print(fn)
print(msg)
return false
end
fno()
local outdir=string.sub(win.filename, 1, -5)
if bAll then
if not output_to_pdf then
CreateDirectory(outdir);
end
processAllPages(outdir)
else
CreateDirectory(outdir);
processPage(outdir, win.currPage, win:getNumRects())
if output_to_pdf then
panel:findWidget("Convert processed pages to PDF"):activate();
end
end
win:setStatus("processing ended")
panel:findWidget("Process current page"):activate();
panel:findWidget("Process all pages"):activate();
return true
end
return false
end