forked from koreader/kindlepdfviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unireader.lua
2033 lines (1843 loc) · 56.9 KB
/
unireader.lua
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require "keys"
require "settings"
require "selectmenu"
require "commands"
require "helppage"
UniReader = {
-- "constants":
ZOOM_BY_VALUE = 0,
ZOOM_FIT_TO_PAGE = -1,
ZOOM_FIT_TO_PAGE_WIDTH = -2,
ZOOM_FIT_TO_PAGE_HEIGHT = -3,
ZOOM_FIT_TO_CONTENT = -4,
ZOOM_FIT_TO_CONTENT_WIDTH = -5,
ZOOM_FIT_TO_CONTENT_HEIGHT = -6,
ZOOM_FIT_TO_CONTENT_WIDTH_PAN = -7,
--ZOOM_FIT_TO_CONTENT_HEIGHT_PAN = -8,
ZOOM_FIT_TO_CONTENT_HALF_WIDTH_MARGIN = -9,
ZOOM_FIT_TO_CONTENT_HALF_WIDTH = -10,
GAMMA_NO_GAMMA = 1.0,
-- framebuffer update policy state:
rcount = 5,
rcountmax = 5,
-- zoom state:
globalzoom = 1.0,
globalzoom_orig = 1.0,
globalzoommode = -1, -- ZOOM_FIT_TO_PAGE
globalrotate = 0,
-- gamma setting:
globalgamma = 1.0, -- GAMMA_NO_GAMMA
-- cached tile size
fullwidth = 0,
fullheight = 0,
-- size of current page for current zoom level in pixels
cur_full_width = 0,
cur_full_height = 0,
offset_x = 0,
offset_y = 0,
dest_x = 0, -- real offset_x when it's smaller than screen, so it's centered
dest_y = 0,
min_offset_x = 0,
min_offset_y = 0,
content_top = 0, -- for ZOOM_FIT_TO_CONTENT_WIDTH_PAN (prevView)
-- set panning distance
shift_x = 100,
shift_y = 50,
pan_by_page = false, -- using shift_[xy] or width/height
pan_x = 0, -- top-left offset of page when pan activated
pan_y = 0,
pan_margin = 20, -- horizontal margin for two-column zoom
pan_overlap_vertical = 30,
show_overlap = 0,
-- the document:
doc = nil,
-- the document's setting store:
settings = nil,
-- list of available commands:
commands = nil,
-- we will use this one often, so keep it "static":
nulldc = DrawContext.new(),
-- tile cache configuration:
cache_max_memsize = 1024*1024*5, -- 5MB tile cache
cache_max_ttl = 20, -- time to live
-- tile cache state:
cache_current_memsize = 0,
cache = {},
-- renderer cache size
cache_document_size = 1024*1024*8, -- FIXME random, needs testing
pagehash = nil,
jump_stack = {},
highlight = {},
toc = nil,
bbox = {}, -- override getUsedBBox
}
function UniReader:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
----------------------------------------------------
-- !!!!!!!!!!!!!!!!!!!!!!!!!
--
-- For a new specific reader,
-- you must always overwrite following method:
--
-- * self:open()
--
-- overwrite other methods if needed.
----------------------------------------------------
-- open a file
function UniReader:open(filename, cache_size)
return false
end
function UniReader:init()
-- initialize commands
self:addAllCommands()
end
----------------------------------------------------
-- highlight support
----------------------------------------------------
function UniReader:screenOffset()
local x = self.dest_x
local y = self.dest_y
if self.offset_x < 0 then
x = x + self.offset_x
end
if self.offset_y < 0 then
y = y + self.offset_y
end
print("# screenOffset "..x..","..y)
return x,y
end
----------------------------------------------------
-- Given coordinates of four corners in original page
-- size and return coordinate of upper left conner in
-- zoomed page size with width and height.
----------------------------------------------------
function UniReader:zoomedRectCoordTransform(x0, y0, x1, y1)
local x,y = self:screenOffset()
return
x0 * self.globalzoom + x,
y0 * self.globalzoom + y,
(x1 - x0) * self.globalzoom,
(y1 - y0) * self.globalzoom
end
----------------------------------------------------
-- Given coordinates of four corners in original page
-- size and return rectangular area in screen. You
-- might want to call this when you want to draw stuff
-- on screen.
--
-- NOTE: this method does not check whether given area
-- is can be shown in current screen. Make sure to check
-- with _isEntireWordInScreenRange() or _isWordInScreenRange()
-- before you want to draw on the returned area.
----------------------------------------------------
function UniReader:getRectInScreen(x0, y0, x1, y1)
x, y, w, h = self:zoomedRectCoordTransform(x0, y0, x1, y1)
if x < 0 then
w = w + x
x = 0
end
if y < 0 then
h = h + y
y = 0
end
if x + w > G_width then w = G_width - x end
if y + h > G_height then h = G_height - y end
return x, y, w, h
end
-- make sure the whole word/line can be seen in screen
-- @TODO when not in FIT_TO_CONTENT_WIDTH mode,
-- self.offset_{x,y} might be negative. 12.04 2012 (houqp)
function UniReader:_isEntireLineInScreenHeightRange(l)
return (l ~= nil) and
(l.y0 * self.globalzoom) >= -self.offset_y
and (l.y1 * self.globalzoom) <= -self.offset_y + G_height
end
function UniReader:_isEntireWordInScreenRange(w)
return self:_isEntireWordInScreenHeightRange(w) and
self:_isEntireWordInScreenWidthRange(w)
end
function UniReader:_isEntireWordInScreenHeightRange(w)
return (w ~= nil) and
(w.y0 * self.globalzoom) >= -self.offset_y
and (w.y1 * self.globalzoom) <= -self.offset_y + G_height
end
function UniReader:_isEntireWordInScreenWidthRange(w)
return (w ~= nil) and
(w.x0 * self.globalzoom >= -self.offset_x) and
(w.x1 * self.globalzoom <= -self.offset_x + G_width)
end
-- make sure at least part of the word can be seen in screen
function UniReader:_isWordInScreenRange(w)
if not w then
return false
end
is_entire_word_out_of_screen_height =
(w.y1 * self.globalzoom <= -self.offset_y)
or (w.y0 * self.globalzoom >= -self.offset_y + G_height)
is_entire_word_out_of_screen_width =
(w.x0 * self.globalzoom >= -self.offset_x + G_width
or w.x1 * self.globalzoom <= -self.offset_x)
return (not is_entire_word_out_of_screen_height) and
(not is_entire_word_out_of_screen_width)
end
function UniReader:toggleTextHighLight(word_list)
for _,text_item in ipairs(word_list) do
for _,line_item in ipairs(text_item) do
-- make sure that line is in screen range
if self:_isWordInScreenRange(line_item) then
local x, y, w, h = self:getRectInScreen(
line_item.x0, line_item.y0,
line_item.x1, line_item.y1)
-- slightly enlarge the highlight height
-- for better viewing experience
x = x
y = y - h * 0.1
w = w
h = h * 1.2
self.highlight.drawer = self.highlight.drawer or "underscore"
if self.highlight.drawer == "underscore" then
self.highlight.line_width = self.highlight.line_width or 2
self.highlight.line_color = self.highlight.line_color or 5
fb.bb:paintRect(x, y+h-1, w,
self.highlight.line_width,
self.highlight.line_color)
elseif self.highlight.drawer == "marker" then
fb.bb:invertRect(x, y, w, h)
end
end -- if isEntireWordInScreenHeightRange
end -- for line_item
end -- for text_item
end
function UniReader:_wordIterFromRange(t, l0, w0, l1, w1)
local i = l0
local j = w0 - 1
return function()
if i <= l1 then
-- if in line range, loop through lines
if i == l1 then
-- in last line
if j < w1 then
j = j + 1
else
-- out of range return nil
return nil, nil
end
else
if j < #t[i] then
j = j + 1
else
-- goto next line
i = i + 1
j = 1
end
end
return i, j
end
end -- closure
end
function UniReader:_toggleWordHighLight(t, l, w)
x, y, w, h = self:getRectInScreen(t[l][w].x0, t[l].y0,
t[l][w].x1, t[l].y1)
-- slightly enlarge the highlight range for better viewing experience
x = x - w * 0.05
y = y - h * 0.05
w = w * 1.1
h = h * 1.1
fb.bb:invertRect(x, y, w, h)
end
function UniReader:_toggleTextHighLight(t, l0, w0, l1, w1)
--print("# toggle range", l0, w0, l1, w1)
-- make sure (l0, w0) is smaller than (l1, w1)
if l0 > l1 then
l0, l1 = l1, l0
w0, w1 = w1, w0
elseif l0 == l1 and w0 > w1 then
w0, w1 = w1, w0
end
for _l, _w in self:_wordIterFromRange(t, l0, w0, l1, w1) do
if self:_isWordInScreenRange(t[_l][_w]) then
-- blitbuffer module will take care of the out of screen range part.
self:_toggleWordHighLight(t, _l, _w)
end
end
end
-- remember to clear cursor before calling this
function UniReader:drawCursorAfterWord(t, l, w)
-- get height of line t[l][w] is in
local _, _, _, h = self:zoomedRectCoordTransform(0, t[l].y0, 0, t[l].y1)
-- get rect of t[l][w]
local x, y, wd, _ = self:getRectInScreen(t[l][w].x0, t[l][w].y0, t[l][w].x1, t[l][w].y1)
self.cursor:setHeight(h)
self.cursor:moveTo(x+wd, y)
self.cursor:draw()
end
function UniReader:drawCursorBeforeWord(t, l, w)
-- get height of line t[l][w] is in
local _, _, _, h = self:zoomedRectCoordTransform(0, t[l].y0, 0, t[l].y1)
-- get rect of t[l][w]
local x, y, _, _ = self:getRectInScreen(t[l][w].x0, t[l][w].y0, t[l][w].x1, t[l][w].y1)
self.cursor:setHeight(h)
self.cursor:moveTo(x, y)
self.cursor:draw()
end
function UniReader:getText(pageno)
-- define a sensible implementation when your reader supports it
return nil
end
function UniReader:startHighLightMode()
local t = self:getText(self.pageno)
if not t or #t == 0 then
return nil
end
local function _findFirstWordInView(t)
for i=1, #t, 1 do
if self:_isEntireWordInScreenRange(t[i][1]) then
return i, 1
end
end
print("## _findFirstWordInView none found in "..dump(t))
return nil
end
local function _isMovingForward(l, w)
return l.cur > l.start or (l.cur == l.start and w.cur > w.start)
end
---------------------------------------
-- some word handling help functions
---------------------------------------
local function _prevWord(t, cur_l, cur_w)
if cur_l == 1 then
if cur_w == 1 then
-- already the first word
return 1, 1
else
-- in first line, but not first word
return cur_l, cur_w -1
end
end
if cur_w <= 1 then
-- first word in current line, goto previous line
return cur_l - 1, #t[cur_l-1]
else
return cur_l, cur_w - 1
end
end
local function _nextWord(t, cur_l, cur_w)
if cur_l == #t then
if cur_w == #(t[cur_l]) then
-- already the last word
return cur_l, cur_w
else
-- in last line, but not last word
return cur_l, cur_w + 1
end
end
if cur_w < #t[cur_l] then
return cur_l, cur_w + 1
else
-- last word in current line, move to next line
return cur_l + 1, 1
end
end
local function _wordInNextLine(t, cur_l, cur_w)
if cur_l == #t then
-- already in last line, return the last word
return cur_l, #(t[cur_l])
else
return cur_l + 1, math.min(cur_w, #t[cur_l+1])
end
end
local function _wordInPrevLine(t, cur_l, cur_w)
if cur_l == 1 then
-- already in first line, return the first word
return 1, 1
else
return cur_l - 1, math.min(cur_w, #t[cur_l-1])
end
end
---------------------------------------
-- some gap handling help functions
---------------------------------------
local function _nextGap(t, cur_l, cur_w)
local is_meet_end = false
-- handle left end of line as special case.
if cur_w == 0 then
if cur_l == #t and #t[cur_l] == 1 then
is_meet_end = true
end
return cur_l, 1, is_meet_end
end
cur_l, cur_w = _nextWord(t, cur_l, cur_w)
if cur_w == 1 then
cur_w = 0
end
if cur_w ~= 0 and cur_l == #t and cur_w == #t[cur_l] then
is_meet_end = true
end
return cur_l, cur_w, is_meet_end
end
local function _prevGap(t, cur_l, cur_w)
local is_meet_start = false
-- handle left end of line as special case.
if cur_l == 1 and (cur_w == 1 or cur_w == 0) then -- in the first line
is_meet_start = true
return cur_l, 0, is_meet_start
end
if cur_w == 1 then -- not in the first line
return cur_l, 0, is_meet_start
elseif cur_w == 0 then
-- set to 1 so _prevWord() can find previous word in previous line
cur_w = 1
end
cur_l, cur_w = _prevWord(t, cur_l, cur_w)
return cur_l, cur_w, is_meet_end
end
local function _gapInNextLine(t, cur_l, cur_w)
local is_meet_end = false
if cur_l == #t then
-- already in last line
cur_w = #t[cur_l]
is_meet_end = true
else
-- handle left end of line as special case.
if cur_w == 0 then
cur_l = math.min(cur_l + 1, #t)
else
cur_l, cur_w = _wordInNextLine(t, cur_l, cur_w)
end
end
return cur_l, cur_w, is_meet_end
end
local function _gapInPrevLine(t, cur_l, cur_w)
local is_meet_start = false
if cur_l == 1 then
-- already in first line
is_meet_start = true
cur_w = 0
else
if cur_w == 0 then
-- goto left end of previous line
cur_l = math.max(cur_l - 1, 1)
else
cur_l, cur_w = _wordInPrevLine(t, cur_l, cur_w)
end
end
return cur_l, cur_w, is_meet_start
end
local l = {}
local w = {}
l.start, w.start = _findFirstWordInView(t)
if not l.start then
print("# no text in current view!")
return
end
l.cur, w.cur = l.start, w.start
l.new, w.new = l.cur, w.cur
local is_meet_start = false
local is_meet_end = false
local running = true
local cx, cy, cw, ch = self:getRectInScreen(
t[l.cur][w.cur].x0,
t[l.cur][w.cur].y0,
t[l.cur][w.cur].x1,
t[l.cur][w.cur].y1)
self.cursor = Cursor:new {
x_pos = cx+cw,
y_pos = cy,
h = ch,
line_width_factor = 4,
}
self.cursor:draw()
fb:refresh(1)
-- first use cursor to place start pos for highlight
while running do
local ev = input.saveWaitForEvent()
ev.code = adjustKeyEvents(ev)
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
if ev.code == KEY_FW_LEFT and not is_meet_start then
is_meet_end = false
l.new, w.new, is_meet_start = _prevGap(t, l.cur, w.cur)
self.cursor:clear()
if w.new ~= 0
and not self:_isEntireLineInScreenHeightRange(t[l.new])
and self:_isEntireWordInScreenWidthRange(t[l.new][w.new]) then
-- word is in previous view
local pageno = self:prevView()
self:goto(pageno)
end
-- update cursor
if w.new == 0 then
-- meet line left end, must be handled as special case
if self:_isEntireWordInScreenRange(t[l.new][1]) then
self:drawCursorBeforeWord(t, l.new, 1)
end
else
if self:_isEntireWordInScreenRange(t[l.new][w.new]) then
self:drawCursorAfterWord(t, l.new, w.new)
end
end
elseif ev.code == KEY_FW_RIGHT and not is_meet_end then
is_meet_start = false
l.new, w.new, is_meet_end = _nextGap(t, l.cur, w.cur)
self.cursor:clear()
-- we want to check whether the word is in screen range,
-- so trun gap into word
local tmp_w = w.new
if tmp_w == 0 then
tmp_w = 1
end
if not self:_isEntireLineInScreenHeightRange(t[l.new])
and self:_isEntireWordInScreenWidthRange(t[l.new][tmp_w]) then
local pageno = self:nextView()
self:goto(pageno)
end
if w.new == 0 then
-- meet line left end, must be handled as special case
if self:_isEntireWordInScreenRange(t[l.new][1]) then
self:drawCursorBeforeWord(t, l.new, 1)
end
else
if self:_isEntireWordInScreenRange(t[l.new][w.new]) then
self:drawCursorAfterWord(t, l.new, w.new)
end
end
elseif ev.code == KEY_FW_UP and not is_meet_start then
is_meet_end = false
l.new, w.new, is_meet_start = _gapInPrevLine(t, l.cur, w.cur)
self.cursor:clear()
local tmp_w = w.new
if tmp_w == 0 then
tmp_w = 1
end
if not self:_isEntireLineInScreenHeightRange(t[l.new])
and self:_isEntireWordInScreenWidthRange(t[l.new][tmp_w]) then
-- goto next view of current page
local pageno = self:prevView()
self:goto(pageno)
end
if w.new == 0 then
if self:_isEntireWordInScreenRange(t[l.new][1]) then
self:drawCursorBeforeWord(t, l.new, 1)
end
else
if self:_isEntireWordInScreenRange(t[l.new][w.new]) then
self:drawCursorAfterWord(t, l.new, w.new)
end
end
elseif ev.code == KEY_FW_DOWN and not is_meet_end then
is_meet_start = false
l.new, w.new, is_meet_end = _gapInNextLine(t, l.cur, w.cur)
self.cursor:clear()
local tmp_w = w.new
if w.cur == 0 then
tmp_w = 1
end
if not self:_isEntireLineInScreenHeightRange(t[l.new])
and self:_isEntireWordInScreenWidthRange(t[l.new][tmp_w]) then
-- goto next view of current page
local pageno = self:nextView()
self:goto(pageno)
end
if w.cur == 0 then
if self:_isEntireWordInScreenRange(t[l.new][1]) then
self:drawCursorBeforeWord(t, l.new, 1)
end
else
if self:_isEntireWordInScreenRange(t[l.new][w.new]) then
self:drawCursorAfterWord(t, l.new, w.new)
end
end
elseif ev.code == KEY_DEL then
if self.highlight[self.pageno] then
for k, text_item in ipairs(self.highlight[self.pageno]) do
for _, line_item in ipairs(text_item) do
if t[l.cur][w.cur].y0 >= line_item.y0
and t[l.cur][w.cur].y1 <= line_item.y1
and t[l.cur][w.cur].x0 >= line_item.x0
and t[l.cur][w.cur].x1 <= line_item.x1 then
self.highlight[self.pageno][k] = nil
end
end -- for line_item
end -- for text_item
end -- if not highlight table
if #self.highlight[self.pageno] == 0 then
self.highlight[self.pageno] = nil
end
return
elseif ev.code == KEY_FW_PRESS then
l.new, w.new = l.cur, w.cur
l.start, w.start = l.cur, w.cur
running = false
self.cursor:clear()
elseif ev.code == KEY_BACK then
running = false
return
end -- if check key event
l.cur, w.cur = l.new, w.new
fb:refresh(1)
end
end -- while running
--print("start", l.cur, w.cur, l.start, w.start)
-- two helper functions for highlight
local function _togglePrevWordHighLight(t, l, w)
if w.cur == 0 then
if l.cur == 1 then
-- already at the begin of first line, nothing to toggle
return l, w, true
else
w.cur = 1
end
end
l.new, w.new = _prevWord(t, l.cur, w.cur)
if l.cur == 1 and w.cur == 1 then
is_meet_start = true
-- left end of first line must be handled as special case
w.new = 0
end
if w.new ~= 0 and
not self:_isEntireLineInScreenHeightRange(t[l.new]) then
-- word out of left and right sides of current view should
-- not trigger pan by page
if self:_isEntireWordInScreenWidthRange(t[l.new][w.new]) then
-- word is in previous view
local pageno = self:prevView()
self:goto(pageno)
end
local l0 = l.start
local w0 = w.start
local l1 = l.cur
local w1 = w.cur
if _isMovingForward(l, w) then
l0, w0 = _nextWord(t, l0, w0)
l1, w1 = l.new, w.new
end
self:_toggleTextHighLight(t, l0, w0,
l1, w1)
else
self:_toggleWordHighLight(t, l.cur, w.cur)
end
l.cur, w.cur = l.new, w.new
return l, w, (is_meet_start or false)
end
local function _toggleNextWordHighLight(t, l, w)
if w.cur == 0 then
w.new = 1
else
l.new, w.new = _nextWord(t, l.cur, w.cur)
end
if l.new == #t and w.new == #t[#t] then
is_meet_end = true
end
if not self:_isEntireLineInScreenHeightRange(t[l.new]) then
if self:_isEntireWordInScreenWidthRange(t[l.new][w.new]) then
local pageno = self:nextView()
self:goto(pageno)
end
local tmp_l = l.start
local tmp_w = w.start
if _isMovingForward(l, w) then
tmp_l, tmp_w = _nextWord(t, tmp_l, tmp_w)
end
self:_toggleTextHighLight(t, tmp_l, tmp_w,
l.new, w.new)
else
self:_toggleWordHighLight(t, l.new, w.new)
end
l.cur, w.cur = l.new, w.new
return l, w, (is_meet_end or false)
end
-- go into highlight mode
running = true
while running do
local ev = input.saveWaitForEvent()
ev.code = adjustKeyEvents(ev)
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
if ev.code == KEY_FW_LEFT then
is_meet_end = false
if not is_meet_start then
l, w, is_meet_start = _togglePrevWordHighLight(t, l, w)
end
elseif ev.code == KEY_FW_RIGHT then
is_meet_start = false
if not is_meet_end then
l, w, is_meet_end = _toggleNextWordHighLight(t, l, w)
end -- if not is_meet_end
elseif ev.code == KEY_FW_UP then
is_meet_end = false
if not is_meet_start then
if l.cur == 1 then
-- handle left end of first line as special case
tmp_l = 1
tmp_w = 0
else
tmp_l, tmp_w = _wordInPrevLine(t, l.cur, w.cur)
end
while not (tmp_l == l.cur and tmp_w == w.cur) do
l, w, is_meet_start = _togglePrevWordHighLight(t, l, w)
end
end -- not is_meet_start
elseif ev.code == KEY_FW_DOWN then
is_meet_start = false
if not is_meet_end then
-- handle left end of first line as special case
if w.cur == 0 then
tmp_w = 1
else
tmp_w = w.cur
end
tmp_l, tmp_w = _wordInNextLine(t, l.cur, tmp_w)
while not (tmp_l == l.cur and tmp_w == w.cur) do
l, w, is_meet_end = _toggleNextWordHighLight(t, l, w)
end
end
elseif ev.code == KEY_FW_PRESS then
local l0, w0, l1, w1
-- find start and end of highlight text
if _isMovingForward(l, w) then
l0, w0 = _nextWord(t, l.start, w.start)
l1, w1 = l.cur, w.cur
else
l0, w0 = _nextWord(t, l.cur, w.cur)
l1, w1 = l.start, w.start
end
-- remove selection area
self:_toggleTextHighLight(t, l0, w0, l1, w1)
-- put text into highlight table of current page
local hl_item = {}
local s = ""
local prev_l = l0
local prev_w = w0
local l_item = {
x0 = t[l0][w0].x0,
y0 = t[l0].y0,
y1 = t[l0].y1,
}
for _l,_w in self:_wordIterFromRange(t, l0, w0, l1, w1) do
local word_item = t[_l][_w]
if _l > prev_l then
-- in next line, add previous line to highlight item
l_item.x1 = t[prev_l][prev_w].x1
table.insert(hl_item, l_item)
-- re initialize l_item for new line
l_item = {
x0 = word_item.x0,
y0 = t[_l].y0,
y1 = t[_l].y1,
}
end
s = s .. word_item.word .. " "
prev_l, prev_w = _l, _w
end
-- insert last line of text in line item
l_item.x1 = t[prev_l][prev_w].x1
table.insert(hl_item, l_item)
hl_item.text = s
if not self.highlight[self.pageno] then
self.highlight[self.pageno] = {}
end
table.insert(self.highlight[self.pageno], hl_item)
running = false
elseif ev.code == KEY_BACK then
running = false
end -- if key event
fb:refresh(1)
end
end -- EOF while
end
----------------------------------------------------
-- Renderer memory
----------------------------------------------------
function UniReader:getCacheSize()
return -1
end
function UniReader:cleanCache()
return
end
----------------------------------------------------
-- Setting related methods
----------------------------------------------------
-- load special settings for specific reader
function UniReader:loadSpecialSettings()
return
end
-- save special settings for specific reader
function UniReader:saveSpecialSettings()
end
--[ following are default methods ]--
function UniReader:initGlobalSettings(settings)
local pan_margin = settings:readSetting("pan_margin")
if pan_margin then
self.pan_margin = pan_margin
end
local pan_overlap_vertical = settings:readSetting("pan_overlap_vertical")
if pan_overlap_vertical then
self.pan_overlap_vertical = pan_overlap_vertical
end
local cache_max_memsize = settings:readSetting("cache_max_memsize")
if cache_max_memsize then
self.cache_max_memsize = cache_max_memsize
end
local cache_max_ttl = settings:readSetting("cache_max_ttl")
if cache_max_ttl then
self.cache_max_ttl = cache_max_ttl
end
local rcountmax = settings:readSetting("partial_refresh_count")
if rcountmax then
self.rcountmax = rcountmax
end
end
-- This is a low-level method that can be shared with all readers.
function UniReader:loadSettings(filename)
if self.doc ~= nil then
self.settings = DocSettings:open(filename,self.cache_document_size)
local gamma = self.settings:readSetting("gamma")
if gamma then
self.globalgamma = gamma
end
local jumpstack = self.settings:readSetting("jumpstack")
self.jump_stack = jumpstack or {}
local highlight = self.settings:readSetting("highlight")
self.highlight = highlight or {}
local bbox = self.settings:readSetting("bbox")
print("# bbox loaded "..dump(bbox))
self.bbox = bbox
self.globalzoom = self.settings:readSetting("globalzoom") or 1.0
self.globalzoommode = self.settings:readSetting("globalzoommode") or -1
self:loadSpecialSettings()
return true
end
return false
end
function UniReader:getLastPageOrPos()
return self.settings:readSetting("last_page") or 1
end
function UniReader:saveLastPageOrPos()
self.settings:savesetting("last_page", self.pageno)
end
-- guarantee that we have enough memory in cache
function UniReader:cacheClaim(size)
if(size > self.cache_max_memsize) then
-- we're not allowed to claim this much at all
error("too much memory claimed")
return false
end
while self.cache_current_memsize + size > self.cache_max_memsize do
-- repeat this until we have enough free memory
for k, _ in pairs(self.cache) do
if self.cache[k].ttl > 0 then
-- reduce ttl
self.cache[k].ttl = self.cache[k].ttl - 1
else
-- cache slot is at end of life, so kick it out
self.cache_current_memsize = self.cache_current_memsize - self.cache[k].size
self.cache[k].bb:free()
self.cache[k] = nil
end
end
end
self.cache_current_memsize = self.cache_current_memsize + size
return true
end
function UniReader:drawOrCache(no, preCache)
-- our general caching strategy is as follows:
-- #1 goal: we must render the needed area.
-- #2 goal: we render as much of the requested page as we can
-- #3 goal: we render the full page
-- #4 goal: we render next page, too. (TODO)
-- ideally, this should be factored out and only be called when needed (TODO)
local ok, page = pcall(self.doc.openPage, self.doc, no)
local width, height = G_width, G_height
if not ok then
-- TODO: error handling
return nil
end
local dc = self:setzoom(page, preCache)
-- offset_x_in_page & offset_y_in_page is the offset within zoomed page
-- they are always positive.
-- you can see self.offset_x_& self.offset_y as the offset within
-- draw space, which includes the page. So it can be negative and positive.
local offset_x_in_page = -self.offset_x
local offset_y_in_page = -self.offset_y
if offset_x_in_page < 0 then offset_x_in_page = 0 end
if offset_y_in_page < 0 then offset_y_in_page = 0 end
-- check if we have relevant cache contents
local pagehash = no..'_'..self.globalzoom..'_'..self.globalrotate..'_'..self.globalgamma
if self.cache[pagehash] ~= nil then
-- we have something in cache, check if it contains the requested part
if self.cache[pagehash].x <= offset_x_in_page
and self.cache[pagehash].y <= offset_y_in_page
and ( self.cache[pagehash].x + self.cache[pagehash].w >= offset_x_in_page + width
or self.cache[pagehash].w >= self.fullwidth - 1)
and ( self.cache[pagehash].y + self.cache[pagehash].h >= offset_y_in_page + height
or self.cache[pagehash].h >= self.fullheight - 1)
then