-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
readerhighlight.lua
1227 lines (1163 loc) · 48.2 KB
/
readerhighlight.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
local BD = require("ui/bidi")
local ButtonDialog = require("ui/widget/buttondialog")
local Device = require("device")
local Event = require("ui/event")
local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer")
local MultiConfirmBox = require("ui/widget/multiconfirmbox")
local Notification = require("ui/widget/notification")
local TimeVal = require("ui/timeval")
local Translator = require("ui/translator")
local UIManager = require("ui/uimanager")
local logger = require("logger")
local _ = require("gettext")
local C_ = _.pgettext
local T = require("ffi/util").template
local Screen = Device.screen
local ReaderHighlight = InputContainer:new{}
function ReaderHighlight:init()
self.ui:registerPostInitCallback(function()
self.ui.menu:registerToMainMenu(self)
end)
end
function ReaderHighlight:setupTouchZones()
-- deligate gesture listener to readerui
self.ges_events = {}
self.onGesture = nil
if not Device:isTouchDevice() then return end
self.ui:registerTouchZones({
{
id = "readerhighlight_tap",
ges = "tap",
screen_zone = {
ratio_x = 0, ratio_y = 0, ratio_w = 1, ratio_h = 1,
},
overrides = {
"tap_forward",
"tap_backward",
"readermenu_tap",
"readerconfigmenu_tap",
},
handler = function(ges) return self:onTap(nil, ges) end
},
{
id = "readerhighlight_hold",
ges = "hold",
screen_zone = {
ratio_x = 0, ratio_y = 0, ratio_w = 1, ratio_h = 1,
},
handler = function(ges) return self:onHold(nil, ges) end
},
{
id = "readerhighlight_hold_release",
ges = "hold_release",
screen_zone = {
ratio_x = 0, ratio_y = 0, ratio_w = 1, ratio_h = 1,
},
handler = function() return self:onHoldRelease() end
},
{
id = "readerhighlight_hold_pan",
ges = "hold_pan",
rate = 2.0,
screen_zone = {
ratio_x = 0, ratio_y = 0, ratio_w = 1, ratio_h = 1,
},
handler = function(ges) return self:onHoldPan(nil, ges) end
},
})
end
function ReaderHighlight:onReaderReady()
self:setupTouchZones()
end
function ReaderHighlight:addToMainMenu(menu_items)
-- insert table to main reader menu
menu_items.highlight_options = {
text = _("Highlighting"),
sub_item_table = self:genHighlightDrawerMenu(),
}
menu_items.translation_settings = Translator:genSettingsMenu()
end
local highlight_style = {
lighten = _("Lighten"),
underscore = _("Underline"),
invert = _("Invert"),
}
function ReaderHighlight:genHighlightDrawerMenu()
local get_highlight_style = function(style)
return {
text = highlight_style[style],
checked_func = function()
return self.view.highlight.saved_drawer == style
end,
enabled_func = function()
return not self.view.highlight.disabled
end,
callback = function()
self.view.highlight.saved_drawer = style
end
}
end
return {
{
text = _("Allow highlighting"),
checked_func = function()
return not self.view.highlight.disabled
end,
callback = function()
self.view.highlight.disabled = not self.view.highlight.disabled
end,
hold_callback = function(touchmenu_instance)
self:toggleDefault()
end,
separator = true,
},
get_highlight_style("lighten"),
get_highlight_style("underscore"),
get_highlight_style("invert"),
}
end
-- Returns a unique id, that can be provided on delayed call to :clear(id)
-- to ensure current highlight has not already been cleared, and that we
-- are not going to clear a new highlight
function ReaderHighlight:getClearId()
self.clear_id = TimeVal.now() -- can act as a unique id
return self.clear_id
end
function ReaderHighlight:clear(clear_id)
if clear_id then -- should be provided by delayed call to clear()
if clear_id ~= self.clear_id then
-- if clear_id is no more valid, highlight has already been
-- cleared since this clear_id was given
return
end
end
self.clear_id = nil -- invalidate id
if self.ui.document.info.has_pages then
self.view.highlight.temp = {}
else
self.ui.document:clearSelection()
end
if self.restore_page_mode_func then
self.restore_page_mode_func()
self.restore_page_mode_func = nil
end
self.selected_text_start_xpointer = nil
if self.hold_pos then
self.hold_pos = nil
self.selected_text = nil
UIManager:setDirty(self.dialog, "ui")
return true
end
end
function ReaderHighlight:onClearHighlight()
self:clear()
return true
end
function ReaderHighlight:onTap(_, ges)
if not self:clear() and ges then
if self.ui.document.info.has_pages then
return self:onTapPageSavedHighlight(ges)
else
return self:onTapXPointerSavedHighlight(ges)
end
end
end
local function inside_box(pos, box)
if pos then
local x, y = pos.x, pos.y
if box.x <= x and box.y <= y
and box.x + box.w >= x
and box.y + box.h >= y then
return true
end
end
end
function ReaderHighlight:onTapPageSavedHighlight(ges)
local pages = self.view:getCurrentPageList()
local pos = self.view:screenToPageTransform(ges.pos)
for key, page in pairs(pages) do
local items = self.view.highlight.saved[page]
if items then
for i = 1, #items do
local pos0, pos1 = items[i].pos0, items[i].pos1
local boxes = self.ui.document:getPageBoxesFromPositions(page, pos0, pos1)
if boxes then
for index, box in pairs(boxes) do
if inside_box(pos, box) then
logger.dbg("Tap on highlight")
return self:onShowHighlightDialog(page, i)
end
end
end
end
end
end
end
function ReaderHighlight:onTapXPointerSavedHighlight(ges)
-- Getting screen boxes is done for each tap on screen (changing pages,
-- showing menu...). We might want to cache these boxes per page (and
-- clear that cache when page layout change or highlights are added
-- or removed).
local cur_view_top, cur_view_bottom
local pos = self.view:screenToPageTransform(ges.pos)
for page, _ in pairs(self.view.highlight.saved) do
local items = self.view.highlight.saved[page]
if items then
for i = 1, #items do
local pos0, pos1 = items[i].pos0, items[i].pos1
-- document:getScreenBoxesFromPositions() is expensive, so we
-- first check this item is on current page
if not cur_view_top then
-- Even in page mode, it's safer to use pos and ui.dimen.h
-- than pages' xpointers pos, even if ui.dimen.h is a bit
-- larger than pages' heights
cur_view_top = self.ui.document:getCurrentPos()
if self.view.view_mode == "page" and self.ui.document:getVisiblePageCount() > 1 then
cur_view_bottom = cur_view_top + 2 * self.ui.dimen.h
else
cur_view_bottom = cur_view_top + self.ui.dimen.h
end
end
local spos0 = self.ui.document:getPosFromXPointer(pos0)
local spos1 = self.ui.document:getPosFromXPointer(pos1)
local start_pos = math.min(spos0, spos1)
local end_pos = math.max(spos0, spos1)
if start_pos <= cur_view_bottom and end_pos >= cur_view_top then
local boxes = self.ui.document:getScreenBoxesFromPositions(pos0, pos1, true) -- get_segments=true
if boxes then
for index, box in pairs(boxes) do
if inside_box(pos, box) then
logger.dbg("Tap on highlight")
return self:onShowHighlightDialog(page, i)
end
end
end
end
end
end
end
end
function ReaderHighlight:updateHighlight(page, index, side, direction, move_by_char)
if self.ui.document.info.has_pages then -- we do this only if it's epub file
return
end
local highlight = self.view.highlight.saved[page][index]
local highlight_time = highlight.datetime
local highlight_beginning = highlight.pos0
local highlight_end = highlight.pos1
if side == 0 then -- we move pos0
local updated_highlight_beginning
if direction == 1 then -- move highlight to the right
if move_by_char then
updated_highlight_beginning = self.ui.document:getNextVisibleChar(highlight_beginning)
else
updated_highlight_beginning = self.ui.document:getNextVisibleWordStart(highlight_beginning)
end
else -- move highlight to the left
if move_by_char then
updated_highlight_beginning = self.ui.document:getPrevVisibleChar(highlight_beginning)
else
updated_highlight_beginning = self.ui.document:getPrevVisibleWordStart(highlight_beginning)
end
end
if updated_highlight_beginning then
local order = self.ui.document:compareXPointers(updated_highlight_beginning, highlight_end)
if order and order > 0 then -- only if beginning did not go past end
self.view.highlight.saved[page][index].pos0 = updated_highlight_beginning
end
end
else -- we move pos1
local updated_highlight_end
if direction == 1 then -- move highlight to the right
if move_by_char then
updated_highlight_end = self.ui.document:getNextVisibleChar(highlight_end)
else
updated_highlight_end = self.ui.document:getNextVisibleWordEnd(highlight_end)
end
else -- move highlight to the left
if move_by_char then
updated_highlight_end = self.ui.document:getPrevVisibleChar(highlight_end)
else
updated_highlight_end = self.ui.document:getPrevVisibleWordEnd(highlight_end)
end
end
if updated_highlight_end then
local order = self.ui.document:compareXPointers(highlight_beginning, updated_highlight_end)
if order and order > 0 then -- only if end did not go back past beginning
self.view.highlight.saved[page][index].pos1 = updated_highlight_end
end
end
end
local new_beginning = self.view.highlight.saved[page][index].pos0
local new_end = self.view.highlight.saved[page][index].pos1
local new_text = self.ui.document:getTextFromXPointers(new_beginning, new_end)
self.view.highlight.saved[page][index].text = new_text
local new_highlight = self.view.highlight.saved[page][index]
self.ui.bookmark:updateBookmark({
page = highlight_beginning,
datetime = highlight_time,
updated_highlight = new_highlight
}, true)
UIManager:setDirty(self.dialog, "ui")
end
function ReaderHighlight:onShowHighlightDialog(page, index)
local buttons = {
{
{
text = _("Delete"),
callback = function()
self:deleteHighlight(page, index)
-- other part outside of the dialog may be dirty
UIManager:close(self.edit_highlight_dialog, "ui")
end,
},
{
text = _("Edit"),
callback = function()
self:editHighlight(page, index)
UIManager:close(self.edit_highlight_dialog)
end,
},
{
text = _("…"),
callback = function()
self.selected_text = self.view.highlight.saved[page][index]
self:onShowHighlightMenu()
UIManager:close(self.edit_highlight_dialog)
end,
},
}
}
if not self.ui.document.info.has_pages then
local start_prev = "◁⇱"
local start_next = "⇱▷"
local end_prev = "◁⇲"
local end_next = "⇲▷"
if BD.mirroredUILayout() then
-- Sadly, there's only north west & south east arrow to corner,
-- north east and south west do not exist in Unicode.
start_prev, start_next = BD.ltr(start_next), BD.ltr(start_prev)
end_prev, end_next = BD.ltr(end_next), BD.ltr(end_prev)
end
table.insert(buttons, {
{
text = start_prev,
callback = function()
self:updateHighlight(page, index, 0, -1, false)
end,
hold_callback = function()
self:updateHighlight(page, index, 0, -1, true)
return true
end
},
{
text = start_next,
callback = function()
self:updateHighlight(page, index, 0, 1, false)
end,
hold_callback = function()
self:updateHighlight(page, index, 0, 1, true)
return true
end
},
{
text = end_prev,
callback = function()
self:updateHighlight(page, index, 1, -1, false)
end,
hold_callback = function()
self:updateHighlight(page, index, 1, -1, true)
end
},
{
text = end_next,
callback = function()
self:updateHighlight(page, index, 1, 1, false)
end,
hold_callback = function()
self:updateHighlight(page, index, 1, 1, true)
end
}
})
end
self.edit_highlight_dialog = ButtonDialog:new{
buttons = buttons
}
UIManager:show(self.edit_highlight_dialog)
return true
end
function ReaderHighlight:onShowHighlightMenu()
local highlight_buttons = {
{
{
text = _("Highlight"),
callback = function()
self:saveHighlight()
self:onClose()
end,
enabled = self.hold_pos ~= nil,
},
{
text = _("Add Note"),
callback = function()
self:addNote()
self:onClose()
end,
enabled = self.hold_pos ~= nil,
},
},
{
{
text = C_("Text", "Copy"),
enabled = Device:hasClipboard(),
callback = function()
Device.input.setClipboardText(self.selected_text.text)
end,
},
{
text = _("View HTML"),
enabled = not self.ui.document.info.has_pages,
callback = function()
self:viewSelectionHTML()
end,
},
},
{
{
text = _("Wikipedia"),
callback = function()
UIManager:scheduleIn(0.1, function()
self:lookupWikipedia()
-- We don't call self:onClose(), we need the highlight
-- to still be there, as we may Highlight it from the
-- dict lookup widget
end)
end,
},
{
text = _("Dictionary"),
callback = function()
self:onHighlightDictLookup()
-- We don't call self:onClose(), same reason as above
end,
},
},
{
{
text = _("Translate"),
callback = function()
self:translate(self.selected_text)
-- We don't call self:onClose(), so one can still see
-- the highlighted text when moving the translated
-- text window, and also if NetworkMgr:promptWifiOn()
-- is needed, so the user can just tap again on this
-- button and does not need to select the text again.
end,
},
{
text = _("Search"),
callback = function()
self:onHighlightSearch()
UIManager:close(self.highlight_dialog)
end,
},
},
}
if self.selected_link ~= nil then
table.insert(highlight_buttons, { -- for now, a single button in an added row
{
text = _("Follow Link"),
callback = function()
local link = self.selected_link.link or self.selected_link
self.ui.link:onGotoLink(link)
self:onClose()
end,
},
})
end
self.highlight_dialog = ButtonDialog:new{
buttons = highlight_buttons,
tap_close_callback = function() self:handleEvent(Event:new("Tap")) end,
}
UIManager:show(self.highlight_dialog)
end
function ReaderHighlight:onHold(arg, ges)
-- disable hold gesture if highlighting is disabled
if self.view.highlight.disabled then return true end
self:clear() -- clear previous highlight (delayed clear may not have done it yet)
self.hold_ges_pos = ges.pos -- remember hold original gesture position
self.hold_pos = self.view:screenToPageTransform(ges.pos)
logger.dbg("hold position in page", self.hold_pos)
if not self.hold_pos then
logger.dbg("not inside page area")
return true
end
-- check if we were holding on an image
-- we provide want_frames=true, so we get a list of images for
-- animated GIFs (supported by ImageViewer)
local image = self.ui.document:getImageFromPosition(self.hold_pos, true)
if image then
logger.dbg("hold on image")
local ImageViewer = require("ui/widget/imageviewer")
local imgviewer = ImageViewer:new{
image = image,
-- title_text = _("Document embedded image"),
-- No title, more room for image
with_title_bar = false,
fullscreen = true,
}
UIManager:show(imgviewer)
return true
end
-- otherwise, we must be holding on text
local ok, word = pcall(self.ui.document.getWordFromPosition, self.ui.document, self.hold_pos)
if ok and word then
logger.dbg("selected word:", word)
self.selected_word = word
local link = self.ui.link:getLinkFromGes(ges)
self.selected_link = nil
if link then
logger.dbg("link:", link)
self.selected_link = link
end
if self.ui.document.info.has_pages then
local boxes = {}
table.insert(boxes, self.selected_word.sbox)
self.view.highlight.temp[self.hold_pos.page] = boxes
end
UIManager:setDirty(self.dialog, "ui")
--- @todo only mark word?
-- Unfortunately, CREngine does not return good coordinates
-- UIManager:setDirty(self.dialog, "partial", self.selected_word.sbox)
self.hold_start_tv = TimeVal.now()
if word.pos0 then
-- Remember original highlight start position, so we can show
-- a marker when back from across-pages text selection, which
-- is handled in onHoldPan()
self.selected_text_start_xpointer = word.pos0
end
end
return true
end
function ReaderHighlight:onHoldPan(_, ges)
if self.hold_pos == nil then
logger.dbg("no previous hold position")
return true
end
local page_area = self.view:getScreenPageArea(self.hold_pos.page)
if ges.pos:notIntersectWith(page_area) then
logger.dbg("not inside page area", ges, page_area)
return true
end
self.holdpan_pos = self.view:screenToPageTransform(ges.pos)
logger.dbg("holdpan position in page", self.holdpan_pos)
if not self.ui.document.info.has_pages and self.selected_text_start_xpointer then
-- With CreDocuments, allow text selection across multiple pages
-- by (temporarily) switching to scroll mode when panning to the
-- top left or bottom right corners.
local is_in_top_left_corner = self.holdpan_pos.y < 1/8*Screen:getHeight()
and self.holdpan_pos.x < 1/8*Screen:getWidth()
local is_in_bottom_right_corner = self.holdpan_pos.y > 7/8*Screen:getHeight()
and self.holdpan_pos.x > 7/8*Screen:getWidth()
if BD.mirroredUILayout() then
-- Note: this might not be really usable, as crengine native selection
-- is not adapted to RTL text
is_in_top_left_corner = self.holdpan_pos.y < 1/8*Screen:getHeight()
and self.holdpan_pos.x > 7/8*Screen:getWidth()
is_in_bottom_right_corner = self.holdpan_pos.y > 7/8*Screen:getHeight()
and self.holdpan_pos.x < 1/8*Screen:getWidth()
end
if is_in_top_left_corner or is_in_bottom_right_corner then
if self.was_in_some_corner then
-- Do nothing, wait for the user to move his finger out of that corner
return true
end
self.was_in_some_corner = true
if self.ui.document:getVisiblePageCount() == 1 then -- single page mode
-- We'll adjust hold_pos.y after the mode switch and the scroll
-- so it's accurate in the new screen coordinates
local orig_y = self.ui.document:getScreenPositionFromXPointer(self.selected_text_start_xpointer)
if self.view.view_mode ~= "scroll" then
-- Switch from page mode to scroll mode
local restore_page_mode_xpointer = self.ui.document:getXPointer() -- top of current page
self.restore_page_mode_func = function()
self.ui:handleEvent(Event:new("SetViewMode", "page"))
self.ui.rolling:onGotoXPointer(restore_page_mode_xpointer, self.selected_text_start_xpointer)
end
self.ui:handleEvent(Event:new("SetViewMode", "scroll"))
end
-- (using rolling:onGotoViewRel(1/3) has some strange side effects)
local scroll_distance = math.floor(Screen:getHeight() * 1/3)
local move_y = is_in_bottom_right_corner and scroll_distance or -scroll_distance
self.ui.rolling:_gotoPos(self.ui.document:getCurrentPos() + move_y)
local new_y = self.ui.document:getScreenPositionFromXPointer(self.selected_text_start_xpointer)
self.hold_pos.y = self.hold_pos.y - orig_y + new_y
UIManager:setDirty(self.dialog, "ui")
return true
else -- two pages mode
-- We don't switch to scroll mode: we just turn 1 page to
-- allow continuing the selection.
-- Unlike in 1-page mode, we have a limitation here: we can't adjust
-- the selection to further than current page and prev/next one.
-- So don't handle another corner if we already handled one:
if self.restore_page_mode_func then
return true
end
-- Also, we are not able to move hold_pos.x out of screen,
-- so if we started on the right page, ignore top left corner,
-- and if we started on the left page, ignore bottom right corner.
local screen_half_width = math.floor(Screen:getWidth() * 1/2)
if self.hold_pos.x >= screen_half_width and is_in_top_left_corner then
return true
elseif self.hold_pos.x <= screen_half_width and is_in_bottom_right_corner then
return true
end
local cur_page = self.ui.document:getCurrentPage()
local restore_page_mode_xpointer = self.ui.document:getXPointer() -- top of current page
self.restore_page_mode_func = function()
self.ui.rolling:onGotoXPointer(restore_page_mode_xpointer, self.selected_text_start_xpointer)
end
if is_in_bottom_right_corner then
self.ui.rolling:_gotoPage(cur_page + 1, true) -- no odd left page enforcement
self.hold_pos.x = self.hold_pos.x - screen_half_width
else
self.ui.rolling:_gotoPage(cur_page - 1, true) -- no odd left page enforcement
self.hold_pos.x = self.hold_pos.x + screen_half_width
end
UIManager:setDirty(self.dialog, "ui")
return true
end
else
self.was_in_some_corner = nil
end
end
local old_text = self.selected_text and self.selected_text.text
self.selected_text = self.ui.document:getTextFromPositions(self.hold_pos, self.holdpan_pos)
if self.selected_text and self.selected_text.pos0 then
if not self.selected_text_start_xpointer then
-- This should have been set in onHold(), where we would get
-- a precise pos0 on the first word selected.
-- Do it here too in case onHold() missed it, but it could be
-- less precise (getTextFromPositions() does order pos0 and pos1,
-- so it's not certain pos0 is where we started from; we get
-- the ones from the first pan, and if it is not small enough
-- and spans quite some height, the marker could point away
-- from the start position)
self.selected_text_start_xpointer = self.selected_text.pos0
end
end
if self.selected_text and old_text and old_text == self.selected_text.text then
-- no modification
return
end
logger.dbg("selected text:", self.selected_text)
if self.selected_text then
self.view.highlight.temp[self.hold_pos.page] = self.selected_text.sboxes
-- remove selected word if hold moves out of word box
if not self.selected_text.sboxes or #self.selected_text.sboxes == 0 then
self.selected_word = nil
elseif self.selected_word and not self.selected_word.sbox:contains(self.selected_text.sboxes[1]) or
#self.selected_text.sboxes > 1 then
self.selected_word = nil
end
end
UIManager:setDirty(self.dialog, "ui")
end
local info_message_ocr_text = _([[
No OCR results or no language data.
KOReader has a build-in OCR engine for recognizing words in scanned PDF and DjVu documents. In order to use OCR in scanned pages, you need to install tesseract trained data for your document language.
You can download language data files for version 3.04 from https://github.com/tesseract-ocr/tesseract/wiki/Data-Files
Copy the language data files for Tesseract 3.04 (e.g., eng.traineddata for English and spa.traineddata for Spanish) into koreader/data/tessdata]])
function ReaderHighlight:lookup(selected_word, selected_link)
-- if we extracted text directly
if selected_word.word then
local word_box = self.view:pageToScreenTransform(self.hold_pos.page, selected_word.sbox)
self.ui:handleEvent(Event:new("LookupWord", selected_word.word, word_box, self, selected_link))
-- or we will do OCR
elseif selected_word.sbox and self.hold_pos then
local word = self.ui.document:getOCRWord(self.hold_pos.page, selected_word)
logger.dbg("OCRed word:", word)
if word and word ~= "" then
local word_box = self.view:pageToScreenTransform(self.hold_pos.page, selected_word.sbox)
self.ui:handleEvent(Event:new("LookupWord", word, word_box, self, selected_link))
else
UIManager:show(InfoMessage:new{
text = info_message_ocr_text,
})
end
end
end
local function prettifyCss(css_text)
-- This is not perfect, but enough to make some ugly CSS readable.
-- Get rid of \t so we can use it as a replacement/hiding char
css_text = css_text:gsub("\t", " ")
-- Wrap and indent declarations
css_text = css_text:gsub("%s*{%s*", " {\n ")
css_text = css_text:gsub(";%s*}%s*", ";\n}\n")
css_text = css_text:gsub(";%s*([^}])", ";\n %1")
css_text = css_text:gsub("%s*}%s*", "\n}\n")
-- Cleanup declarations
css_text = css_text:gsub("{[^}]*}", function(s)
s = s:gsub("%s*:%s*", ": ")
-- Temporarily hide/replace ',' in declaration so they
-- are not matched and made multi-lines by followup gsub
s = s:gsub("%s*,%s*", "\t")
return s
end)
-- Have each selector (separated by ',') on a new line
css_text = css_text:gsub("%s*,%s*", " ,\n")
-- Restore hidden ',' in declarations
css_text = css_text:gsub("\t", ", ")
return css_text
end
function ReaderHighlight:viewSelectionHTML(debug_view)
if self.ui.document.info.has_pages then
return
end
if self.selected_text and self.selected_text.pos0 and self.selected_text.pos1 then
-- For available flags, see the "#define WRITENODEEX_*" in crengine/src/lvtinydom.cpp
local html_flags = 0x3030 -- valid and classic displayed HTML, with only block nodes indented
if not debug_view then
debug_view = 0
end
if debug_view == 1 then
-- Each node on a line, with markers and numbers of skipped chars and siblings shown,
-- with possibly invalid HTML (text nodes not escaped)
html_flags = 0x3353
elseif debug_view == 2 then
-- Additionally see rendering methods and unicode codepoint of each char
html_flags = 0x3757
end
local html, css_files = self.ui.document:getHTMLFromXPointers(self.selected_text.pos0,
self.selected_text.pos1, html_flags, true)
if html then
-- Make some invisible chars visible
if debug_view >= 1 then
html = html:gsub("\xC2\xA0", "␣") -- no break space: open box
html = html:gsub("\xC2\xAD", "⋅") -- soft hyphen: dot operator (smaller than middle dot ·)
-- Prettify inlined CSS (from <HEAD>, put in an internal
-- <body><stylesheet> element by crengine (the opening tag may
-- include some href=, or end with " ~X>" with some html_flags)
-- (We do that in debug_view mode only: as this may increase
-- the height of this section, we don't want to have to scroll
-- many pages to get to the HTML content on the initial view.)
html = html:gsub("(<stylesheet[^>]*>)%s*(.-)%s*(</stylesheet>)", function(pre, css_text, post)
return pre .. "\n" .. prettifyCss(css_text) .. post
end)
end
local TextViewer = require("ui/widget/textviewer")
local Font = require("ui/font")
local textviewer
local buttons_table = {}
if css_files then
for i=1, #css_files do
local button = {
text = T(_("View %1"), css_files[i]),
callback = function()
local css_text = self.ui.document:getDocumentFileContent(css_files[i])
local cssviewer
cssviewer = TextViewer:new{
title = css_files[i],
text = css_text or _("Failed getting CSS content"),
text_face = Font:getFace("smallinfont"),
justified = false,
para_direction_rtl = false,
auto_para_direction = false,
buttons_table = {
{{
text = _("Prettify"),
enabled = css_text and true or false,
callback = function()
UIManager:close(cssviewer)
UIManager:show(TextViewer:new{
title = css_files[i],
text = prettifyCss(css_text),
text_face = Font:getFace("smallinfont"),
justified = false,
para_direction_rtl = false,
auto_para_direction = false,
})
end,
}},
{{
text = _("Close"),
callback = function()
UIManager:close(cssviewer)
end,
}},
}
}
UIManager:show(cssviewer)
end,
}
-- One button per row, too make room for the possibly long css filename
table.insert(buttons_table, {button})
end
end
local next_debug_text
local next_debug_view = debug_view + 1
if next_debug_view == 1 then
next_debug_text = _("Switch to debug view")
elseif next_debug_view == 2 then
next_debug_text = _("Switch to extended debug view")
else
next_debug_view = 0
next_debug_text = _("Switch to standard view")
end
table.insert(buttons_table, {{
text = next_debug_text,
callback = function()
UIManager:close(textviewer)
self:viewSelectionHTML(next_debug_view)
end,
}})
table.insert(buttons_table, {{
text = _("Close"),
callback = function()
UIManager:close(textviewer)
end,
}})
textviewer = TextViewer:new{
title = _("Selection HTML"),
text = html,
text_face = Font:getFace("smallinfont"),
justified = false,
para_direction_rtl = false,
auto_para_direction = false,
buttons_table = buttons_table,
}
UIManager:show(textviewer)
else
UIManager:show(InfoMessage:new{
text = _("Failed getting HTML for selection"),
})
end
end
end
function ReaderHighlight:translate(selected_text)
if selected_text.text ~= "" then
self:onTranslateText(selected_text.text)
-- or we will do OCR
else
local text = self.ui.document:getOCRText(self.hold_pos.page, selected_text)
logger.dbg("OCRed text:", text)
if text and text ~= "" then
self:onTranslateText(text)
else
UIManager:show(InfoMessage:new{
text = info_message_ocr_text,
})
end
end
end
function ReaderHighlight:onTranslateText(text)
Translator:showTranslation(text)
end
function ReaderHighlight:onHoldRelease()
if self.hold_start_tv then
local hold_duration = TimeVal.now() - self.hold_start_tv
hold_duration = hold_duration.sec + hold_duration.usec/1000000
self.hold_start_tv = nil
if hold_duration > 3.0 and self.selected_word then
-- if we were holding for more than 3 seconds on a word, make
-- it behave like we panned and selected more words, so we can
-- directly access the highlight menu and avoid a dict lookup
self:onHoldPan(nil, {pos=self.hold_ges_pos})
end
end
if self.selected_text then
local default_highlight_action = G_reader_settings:readSetting("default_highlight_action")
if not default_highlight_action then
self:onShowHighlightMenu()
elseif default_highlight_action == "highlight" then
self:saveHighlight()
self:onClose()
elseif default_highlight_action == "translate" then
self:translate(self.selected_text)
self:onClose()
elseif default_highlight_action == "wikipedia" then
self:lookupWikipedia()
self:onClose()
end
elseif self.selected_word then
self:lookup(self.selected_word, self.selected_link)
self.selected_word = nil
end
return true
end
function ReaderHighlight:onCycleHighlightAction()
local next_actions = {
highlight = "translate",
translate = "wikipedia",
wikipedia = nil
}
local current_action = G_reader_settings:readSetting("default_highlight_action")
if not current_action then
G_reader_settings:saveSetting("default_highlight_action", "highlight")
UIManager:show(Notification:new{
text = _("Default highlight action changed to 'highlight'."),
timeout = 1,
})
else
local next_action = next_actions[current_action]
G_reader_settings:saveSetting("default_highlight_action", next_action)
UIManager:show(Notification:new{
text = T(_("Default highlight action changed to '%1'."), (next_action or "default")),
timeout = 1,
})
end
return true
end
function ReaderHighlight:onCycleHighlightStyle()
local next_actions = {
lighten = "underscore",
underscore = "invert",
invert = "lighten"
}
self.view.highlight.saved_drawer = next_actions[self.view.highlight.saved_drawer]
self.ui.doc_settings:saveSetting("highlight_drawer", self.view.highlight.saved_drawer)
UIManager:show(Notification:new{
text = T(_("Default highlight style changed to '%1'."), self.view.highlight.saved_drawer),
timeout = 1,
})
return true
end
function ReaderHighlight:highlightFromHoldPos()
if self.hold_pos then
if not self.selected_text then
self.selected_text = self.ui.document:getTextFromPositions(self.hold_pos, self.hold_pos)
logger.dbg("selected text:", self.selected_text)
end
end
end
function ReaderHighlight:onHighlight()
self:saveHighlight()
end
function ReaderHighlight:onUnhighlight(bookmark_item)
local page
local sel_text
local sel_pos0
local datetime
local idx
if bookmark_item then -- called from Bookmarks menu onHold
page = bookmark_item.page
sel_text = bookmark_item.notes
sel_pos0 = bookmark_item.pos0
datetime = bookmark_item.datetime
else -- called from DictQuickLookup Unhighlight button
page = self.hold_pos.page
sel_text = self.selected_text.text
sel_pos0 = self.selected_text.pos0
end
if self.ui.document.info.has_pages then -- We can safely use page