-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
readerlink.lua
1173 lines (1119 loc) · 51.2 KB
/
readerlink.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
--[[--
ReaderLink is an abstraction for document-specific link interfaces.
]]
local BD = require("ui/bidi")
local ButtonDialogTitle = require("ui/widget/buttondialogtitle")
local ConfirmBox = require("ui/widget/confirmbox")
local Device = require("device")
local Event = require("ui/event")
local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer")
local LinkBox = require("ui/widget/linkbox")
local Notification = require("ui/widget/notification")
local UIManager = require("ui/uimanager")
local ffiutil = require("ffi/util")
local logger = require("logger")
local util = require("util")
local _ = require("gettext")
local Screen = Device.screen
local T = ffiutil.template
local ReaderLink = InputContainer:new{
location_stack = {}
}
function ReaderLink:init()
if Device:isTouchDevice() then
self.ui:registerTouchZones({
{
id = "tap_link",
ges = "tap",
screen_zone = {
ratio_x = 0, ratio_y = 0,
ratio_w = 1, ratio_h = 1,
},
overrides = {
-- Tap on links have priority over everything (it can
-- be disabled with "Tap to follow links" menu item)
"tap_forward",
"tap_backward",
"readermenu_tap",
"readerconfigmenu_tap",
"readerhighlight_tap",
"readerfooter_tap",
"tap_top_left_corner",
"tap_top_right_corner",
"tap_left_bottom_corner",
"tap_right_bottom_corner",
},
handler = function(ges) return self:onTap(_, ges) end,
},
{
id = "swipe_link",
ges = "swipe",
screen_zone = {
ratio_x = 0, ratio_y = 0,
ratio_w = 1, ratio_h = 1,
},
overrides = {
"paging_swipe",
"rolling_swipe"
},
handler = function(ges) return self:onSwipe(_, ges) end,
},
})
end
self.ui:registerPostInitCallback(function()
self.ui.menu:registerToMainMenu(self)
end)
-- For relative local file links
local directory, filename = util.splitFilePathName(self.ui.document.file) -- luacheck: no unused
self.document_dir = directory
-- Migrate these old settings to the new common one
if G_reader_settings:isTrue("tap_link_footnote_popup")
or G_reader_settings:isTrue("swipe_link_footnote_popup") then
G_reader_settings:saveSetting("tap_link_footnote_popup", nil)
G_reader_settings:saveSetting("swipe_link_footnote_popup", nil)
G_reader_settings:saveSetting("footnote_link_in_popup", true)
end
end
function ReaderLink:onReadSettings(config)
-- called when loading new document
self.location_stack = {}
end
local function isTapToFollowLinksOn()
return G_reader_settings:nilOrTrue("tap_to_follow_links")
end
local function isLargerTapAreaToFollowLinksEnabled()
return G_reader_settings:isTrue("larger_tap_area_to_follow_links")
end
local function isTapIgnoreExternalLinksEnabled()
return G_reader_settings:isTrue("tap_ignore_external_links")
end
local function isFootnoteLinkInPopupEnabled()
return G_reader_settings:isTrue("footnote_link_in_popup")
end
local function isPreferFootnoteEnabled()
return G_reader_settings:isTrue("link_prefer_footnote")
end
local function isSwipeToGoBackEnabled()
return G_reader_settings:isTrue("swipe_to_go_back")
end
local function isSwipeToFollowNearestLinkEnabled()
return G_reader_settings:isTrue("swipe_to_follow_nearest_link")
end
local function isSwipeIgnoreExternalLinksEnabled()
return G_reader_settings:nilOrTrue("swipe_ignore_external_links")
end
local function isSwipeToJumpToLatestBookmarkEnabled()
return G_reader_settings:isTrue("swipe_to_jump_to_latest_bookmark")
end
function ReaderLink:addToMainMenu(menu_items)
-- insert table to main reader menu
menu_items.follow_links = {
text = _("Links"),
sub_item_table = {
{
text = _("Tap to follow links"),
checked_func = isTapToFollowLinksOn,
callback = function()
G_reader_settings:saveSetting("tap_to_follow_links",
not isTapToFollowLinksOn())
end,
help_text = _([[Tap on links to follow them.]]),
},
{
text = _("Ignore external links on tap"),
enabled_func = isTapToFollowLinksOn,
checked_func = isTapIgnoreExternalLinksEnabled,
callback = function()
G_reader_settings:saveSetting("tap_ignore_external_links",
not isTapIgnoreExternalLinksEnabled())
end,
help_text = _([[
Ignore taps on external links. Useful with Wikipedia EPUBs to make page turning easier.
You can still follow them from the dictionary window or the selection menu after holding on them.]]),
separator = true,
},
{
text = _("Swipe to go back"),
checked_func = isSwipeToGoBackEnabled,
callback = function()
G_reader_settings:saveSetting("swipe_to_go_back",
not isSwipeToGoBackEnabled())
end,
help_text = _([[Swipe to the right to go back to the previous location after you have followed a link. When the location stack is empty, swiping to the right takes you to the previous page.]]),
},
{
text = _("Swipe to follow nearest link"),
checked_func = isSwipeToFollowNearestLinkEnabled,
callback = function()
G_reader_settings:saveSetting("swipe_to_follow_nearest_link",
not isSwipeToFollowNearestLinkEnabled())
end,
help_text = _([[Swipe to the left to follow the link nearest to where you started the swipe. This is useful when a small font is used and tapping on small links is tedious.]]),
},
{
text = _("Ignore external links on swipe"),
enabled_func = isSwipeToFollowNearestLinkEnabled,
checked_func = isSwipeIgnoreExternalLinksEnabled,
callback = function()
G_reader_settings:saveSetting("swipe_ignore_external_links",
not isSwipeIgnoreExternalLinksEnabled())
end,
help_text = _([[
Ignore external links near swipe. Useful with Wikipedia EPUBs to follow only footnotes with swipe.
You can still follow external links from the dictionary window or the selection menu after holding on them.]]),
separator = true,
},
{
text = _("Swipe to jump to latest bookmark"),
checked_func = isSwipeToJumpToLatestBookmarkEnabled,
callback = function()
G_reader_settings:saveSetting("swipe_to_jump_to_latest_bookmark",
not isSwipeToJumpToLatestBookmarkEnabled())
end,
help_text = _([[
Swipe to the left to go the most recently bookmarked page.
This can be useful to quickly swipe back and forth between what you are reading and some reference page (for example notes, a map or a characters list).
If any of the other Swipe to follow link options is enabled, this will work only when the current page contains no link.]]),
},
}
}
-- Insert other items that are (for now) only supported with CreDocuments
-- (They could be supported nearly as-is, but given that there is a lot
-- less visual feedback on PDF document of what is a link, or that we just
-- followed a link, than on EPUB, it's safer to not use them on PDF documents
-- even if the user enabled these features for EPUB documents).
if not self.ui.document.info.has_pages then
-- Tap section
table.insert(menu_items.follow_links.sub_item_table, 2, {
text = _("Allow larger tap area around links"),
enabled_func = isTapToFollowLinksOn,
checked_func = isLargerTapAreaToFollowLinksEnabled,
callback = function()
G_reader_settings:saveSetting("larger_tap_area_to_follow_links",
not isLargerTapAreaToFollowLinksEnabled())
end,
help_text = _([[Extends the tap area around internal links. Useful with a small font where tapping on small footnote links may be tedious.]]),
})
table.insert(menu_items.follow_links.sub_item_table, 4, {
text = _("Show footnotes in popup"),
enabled_func = function()
return isTapToFollowLinksOn() or isSwipeToFollowNearestLinkEnabled()
end,
checked_func = isFootnoteLinkInPopupEnabled,
callback = function()
G_reader_settings:saveSetting("footnote_link_in_popup",
not isFootnoteLinkInPopupEnabled())
end,
help_text = _([[
Show internal link target content in a footnote popup when it looks like it might be a footnote, instead of following the link.
Note that depending on the book quality, footnote detection may not always work correctly.
The footnote content may be empty, truncated, or include other footnotes.
From the footnote popup, you can jump to the footnote location in the book by swiping to the left.]]),
})
table.insert(menu_items.follow_links.sub_item_table, 5, {
text = _("Show more links as footnotes"),
enabled_func = function()
return isFootnoteLinkInPopupEnabled() and
(isTapToFollowLinksOn() or isSwipeToFollowNearestLinkEnabled())
end,
checked_func = isPreferFootnoteEnabled,
callback = function()
G_reader_settings:saveSetting("link_prefer_footnote",
not isPreferFootnoteEnabled())
end,
help_text = _([[Loosen footnote detection rules to show more links as footnotes.]]),
})
table.insert(menu_items.follow_links.sub_item_table, 6, {
text = _("Set footnote popup font size"),
enabled_func = function()
return isFootnoteLinkInPopupEnabled() and
(isTapToFollowLinksOn() or isSwipeToFollowNearestLinkEnabled())
end,
keep_menu_open = true,
callback = function()
local SpinWidget = require("ui/widget/spinwidget")
UIManager:show(SpinWidget:new{
width = Screen:getWidth() * 0.75,
value = G_reader_settings:readSetting("footnote_popup_relative_font_size") or -2,
value_min = -10,
value_max = 5,
precision = "%+d",
ok_text = _("Set font size"),
title_text = _("Set footnote popup font size"),
text = _([[
The footnote popup font adjusts to the font size you've set for the document.
You can specify here how much smaller or larger it should be relative to the document font size.
A negative value will make it smaller, while a positive one will make it larger.
The recommended value is -2.]]),
callback = function(spin)
G_reader_settings:saveSetting("footnote_popup_relative_font_size", spin.value)
end,
})
end,
help_text = _([[
The footnote popup font adjusts to the font size you've set for the document.
This allows you to specify how much smaller or larger it should be relative to the document font size.]]),
separator = true,
})
end
menu_items.go_to_previous_location = {
text = _("Go back to previous location"),
enabled_func = function() return #self.location_stack > 0 end,
callback = function() self:onGoBackLink() end,
hold_callback = function(touchmenu_instance)
UIManager:show(ConfirmBox:new{
text = _("Clear location history?"),
ok_text = _("Clear"),
ok_callback = function()
self:onClearLocationStack()
touchmenu_instance:closeMenu()
end,
})
end,
}
end
--- Check if a xpointer to <a> node really points to itself
function ReaderLink:isXpointerCoherent(a_xpointer)
-- Get screen coordinates of xpointer
local screen_y, screen_x = self.ui.document:getScreenPositionFromXPointer(a_xpointer)
-- Get again link and a_xpointer from this position
local re_link_xpointer, re_a_xpointer = self.ui.document:getLinkFromPosition({x = screen_x, y = screen_y}) -- luacheck: no unused
-- We should get the same a_xpointer. If not, crengine has messed up
-- and we should not trust this xpointer to get back to this link.
if re_a_xpointer ~= a_xpointer then
-- Try it again with screen_x+1 (in the rare cases where screen_x
-- fails, screen_x+1 usually works - probably something in crengine,
-- but easier to workaround here that way)
re_link_xpointer, re_a_xpointer = self.ui.document:getLinkFromPosition({x = screen_x+1, y = screen_y}) -- luacheck: no unused
if re_a_xpointer ~= a_xpointer then
logger.info("not coherent a_xpointer:", a_xpointer)
return false
end
end
return true
end
--- Gets link from gesture.
-- `Document:getLinkFromPosition()` behaves differently depending on
-- document type, so this function provides a wrapper.
function ReaderLink:getLinkFromGes(ges)
if self.ui.document.info.has_pages then
local pos = self.view:screenToPageTransform(ges.pos)
if pos then
-- link box in native page
local link, lbox = self.ui.document:getLinkFromPosition(pos.page, pos)
if link and lbox then
return {
link = link,
lbox = lbox,
pos = pos,
}
end
end
else
local link_xpointer, a_xpointer = self.ui.document:getLinkFromPosition(ges.pos)
logger.dbg("getLinkFromPosition link_xpointer:", link_xpointer)
logger.dbg("getLinkFromPosition a_xpointer:", a_xpointer)
-- On some documents, crengine may sometimes give a wrong a_xpointer
-- (in some Wikipedia saved as EPUB, it would point to some other <A>
-- element in the same paragraph). If followed then back, we could get
-- to a different page. So, we check here how valid it is, and if not,
-- we just discard it so that addCurrentLocationToStack() is used.
local from_xpointer = nil
if a_xpointer and self:isXpointerCoherent(a_xpointer) then
from_xpointer = a_xpointer
end
if link_xpointer ~= "" then
-- This link's source xpointer is more precise than a classic
-- xpointer to top of a page: we can show a marker at its
-- y-position in target page
-- (keep a_xpointer even if incoherent, might be needed for
-- footnote detection (better than nothing if incoherent)
return {
xpointer = link_xpointer,
marker_xpointer = link_xpointer,
from_xpointer = from_xpointer,
a_xpointer = a_xpointer,
-- tap y-position should be a good approximation of link y
-- (needed to keep its highlight a bit more time if it was
-- hidden by the footnote popup)
link_y = ges.pos.y
}
end
end
end
--- Highlights a linkbox if available and goes to it.
function ReaderLink:showLinkBox(link, allow_footnote_popup)
if link and link.lbox then -- pdfdocument
-- screen box that holds the link
local sbox = self.view:pageToScreenTransform(link.pos.page,
self.ui.document:nativeToPageRectTransform(link.pos.page, link.lbox))
if sbox then
UIManager:show(LinkBox:new{
box = sbox,
timeout = FOLLOW_LINK_TIMEOUT,
callback = function()
self:onGotoLink(link.link, false, allow_footnote_popup)
end
})
return true
end
elseif link and link.xpointer ~= "" then -- credocument
return self:onGotoLink(link, false, allow_footnote_popup)
end
end
function ReaderLink:onTap(_, ges)
if not isTapToFollowLinksOn() then return end
if self.ui.document.info.has_pages then
-- (footnote popup and larger tap area are for not
-- not supported with non-CreDocuments)
local link = self:getLinkFromGes(ges)
if link then
if link.link and link.link.uri and isTapIgnoreExternalLinksEnabled() then
return
end
return self:showLinkBox(link)
end
return
end
-- For CreDocuments only from now on
local allow_footnote_popup = isFootnoteLinkInPopupEnabled()
-- If tap_ignore_external_links, skip precise tap detection to really
-- ignore a tap on an external link, and allow using onGoToPageLink()
-- to find the nearest internal link
if not isTapIgnoreExternalLinksEnabled() then
local link = self:getLinkFromGes(ges)
if link then
return self:showLinkBox(link, allow_footnote_popup)
end
end
if isLargerTapAreaToFollowLinksEnabled() or isTapIgnoreExternalLinksEnabled() then
local max_distance = 0 -- used when only isTapIgnoreExternalLinksEnabled()
if isLargerTapAreaToFollowLinksEnabled() then
-- If no link found exactly at the tap position,
-- try to find any link in page around that tap position.
-- With "Ignore external links", onGoToPageLink() will grab
-- only internal links, which is nice as url links are usually
-- longer - so this give more chance to catch a small link to
-- footnote stuck to a longer Wikipedia article name link.
--
-- 30px on a reference 167 dpi screen makes 0.45cm, which
-- seems fine (on a 300dpi device, this will be scaled
-- to 54px (which makes 1/20th of screen witdh on a GloHD)
-- Trust Screen.dpi (which may not be the real device
-- screen DPI if the user has set another one).
max_distance = Screen:scaleByDPI(30)
end
return self:onGoToPageLink(ges, isTapIgnoreExternalLinksEnabled(), allow_footnote_popup, max_distance)
end
end
--- Remember current location so we can go back to it
function ReaderLink:addCurrentLocationToStack()
if self.ui.document.info.has_pages then
table.insert(self.location_stack, self.ui.paging:getBookLocation())
else
table.insert(self.location_stack, {
xpointer = self.ui.rolling:getBookLocation(),
})
end
end
function ReaderLink:onClearLocationStack(show_notification)
self.location_stack = {}
if show_notification then
UIManager:show(Notification:new{
text = _("Location history cleared."),
timeout = 2,
})
end
return true
end
--- Goes to link.
-- (This is called by other modules (highlight, search) to jump to a xpointer,
-- they should not provide allow_footnote_popup=true)
function ReaderLink:onGotoLink(link, neglect_current_location, allow_footnote_popup)
logger.dbg("onGotoLink:", link)
local link_url
if self.ui.document.info.has_pages then
-- internal pdf links have a "page" attribute, while external ones have an "uri" attribute
if link.page then -- Internal link
logger.dbg("Internal link:", link)
if not neglect_current_location then
self:addCurrentLocationToStack()
end
self.ui:handleEvent(Event:new("GotoPage", link.page + 1))
return true
end
link_url = link.uri -- external link
else
-- For crengine, internal links may look like :
-- #_doc_fragment_0_Organisation (link from anchor)
-- /body/DocFragment/body/ul[2]/li[5]/text()[3].16 (xpointer from full-text search)
-- If the XPointer does not exist (or is a full url), we will jump to page 1
-- Best to check that this link exists in document with the following,
-- which accepts both of the above legitimate xpointer as input.
if self.ui.document:isXPointerInDocument(link.xpointer) then
logger.dbg("Internal link:", link)
if allow_footnote_popup then
if self:showAsFootnotePopup(link, neglect_current_location) then
return true
end
-- if it fails for any reason, fallback to following link
end
if not neglect_current_location then
if link.from_xpointer then
-- We have a more precise xpointer than the xpointer to top of
-- current page that addCurrentLocationToStack() would give, and
-- we may be able to show a marker when back
local saved_location
if self.view.view_mode == "scroll" then
-- In scroll mode, we still use the top of page as the
-- xpointer to go back to, so we get back to the same view.
-- We can still show the marker at the link position
saved_location = {
xpointer = self.ui.rolling:getBookLocation(),
marker_xpointer = link.from_xpointer,
}
else
-- In page mode, we use the same for go to and for marker,
-- as 'page mode' ensures we get back to the same view.
saved_location = {
xpointer = link.from_xpointer,
marker_xpointer = link.from_xpointer,
}
end
table.insert(self.location_stack, saved_location)
else
self:addCurrentLocationToStack()
end
end
self.ui:handleEvent(Event:new("GotoXPointer", link.xpointer, link.marker_xpointer))
return true
end
link_url = link.xpointer -- external link
end
logger.dbg("External link:", link_url)
local is_http_link = link_url:find("^https?://") ~= nil
if is_http_link and self:onGoToExternalLink(link_url) then
return true
end
-- Check if it is a link to a local file
local linked_filename = link_url:gsub("^file:", "") -- remove local file protocol if any
local anchor
if linked_filename:find("?") then -- remove any query string (including any following anchor)
linked_filename, anchor = linked_filename:match("^(.-)(%?.*)$")
elseif linked_filename:find("#") then -- remove any anchor
linked_filename, anchor = linked_filename:match("^(.-)(#.*)$")
end
linked_filename = ffiutil.joinPath(self.document_dir, linked_filename) -- get full path
linked_filename = ffiutil.realpath(linked_filename) -- clean full path from ./ or ../
if linked_filename and lfs.attributes(linked_filename, "mode") == "file" then
local DocumentRegistry = require("document/documentregistry")
local provider = DocumentRegistry:getProvider(linked_filename)
if provider then
-- Display filename with anchor or query string, so the user gets
-- this information and can manually go to the appropriate place
local display_filename = linked_filename
if anchor then
display_filename = display_filename .. anchor
end
UIManager:show(ConfirmBox:new{
text = T(_("Would you like to read this local document?\n\n%1\n"), BD.filepath(display_filename)),
ok_callback = function()
UIManager:scheduleIn(0.1, function()
self.ui:switchDocument(linked_filename)
end)
end
})
else
UIManager:show(InfoMessage:new{
text = T(_("Link to unsupported local file:\n%1"), BD.url(link_url)),
})
end
return true
end
-- Not supported
UIManager:show(InfoMessage:new{
text = T(_("Invalid or external link:\n%1"), BD.url(link_url)),
-- no timeout to allow user to type that link in his web browser
})
-- don't propagate, user will notice and tap elsewhere if he wants to change page
return true
end
function ReaderLink:onGoToExternalLink(link_url)
local text
local dialog
local buttons = {
{
{
text = _("Cancel"),
callback = function()
UIManager:close(dialog)
end,
},
},
}
local add_button = function(button)
if #buttons[#buttons] >= 2 then
-- add new row if last contains already 2
table.insert(buttons, {})
end
-- append button to last row
table.insert(buttons[#buttons], button)
end
-- Set up buttons for alternative external link handling methods
local alt_handlers_buttons = {}
if self.ui.wallabag then
table.insert(alt_handlers_buttons, {
text = _("Add to Wallabag"),
callback = function()
UIManager:close(dialog)
self.ui:handleEvent(Event:new("AddWallabagArticle", link_url))
end,
})
end
if Device:canOpenLink() then
table.insert(alt_handlers_buttons, {
text = _("Open in browser"),
callback = function()
UIManager:close(dialog)
Device:openLink(link_url)
end,
})
end
-- Check if it is a wikipedia link
local wiki_lang, wiki_page = link_url:match([[https?://([^%.]+).wikipedia.org/wiki/([^/]+)]])
if wiki_lang and wiki_page then
logger.dbg("Wikipedia link:", wiki_lang, wiki_page)
-- Ask for user confirmation before launching lookup (on a
-- wikipedia page saved as epub, full of wikipedia links, it's
-- too easy to click on links when wanting to change page...)
-- But first check if this wikipedia article has been saved as EPUB
local epub_filename = util.getSafeFilename(wiki_page:gsub("_", " ")) .. "."..string.upper(wiki_lang)..".epub"
local epub_fullpath
-- either in current book directory
local last_file = G_reader_settings:readSetting("lastfile")
if last_file then
local current_book_dir = last_file:match("(.*)/")
local epub_path = current_book_dir .. "/" .. epub_filename
if util.pathExists(epub_path) then
epub_fullpath = epub_path
end
end
-- or in wikipedia save directory
if not epub_fullpath then
local dir = G_reader_settings:readSetting("wikipedia_save_dir")
if not dir then dir = G_reader_settings:readSetting("home_dir") end
if not dir then dir = require("apps/filemanager/filemanagerutil").getDefaultDir() end
if dir then
local epub_path = dir .. "/" .. epub_filename
if util.pathExists(epub_path) then
epub_fullpath = epub_path
end
end
end
text = T(_("Would you like to read this Wikipedia %1 article?\n\n%2\n"), wiki_lang:upper(), wiki_page:gsub("_", " "))
add_button({
text = _("Read online"),
callback = function()
UIManager:nextTick(function()
UIManager:close(dialog)
self.ui:handleEvent(Event:new("LookupWikipedia", wiki_page, false, true, wiki_lang))
end)
end,
})
if epub_fullpath then
text = T("%1\n%2", text, _("This article has previously been saved as EPUB. You may wish to read the saved EPUB instead."))
add_button({
text = _("Read EPUB"),
callback = function()
UIManager:scheduleIn(0.1, function()
UIManager:close(dialog)
self.ui:switchDocument(epub_fullpath)
end)
end,
})
end
else
if #alt_handlers_buttons == 0 then
-- No external link handler
return false
end
text = T(_("External link:\n\n%1"), BD.url(link_url))
end
-- Add all alternative handlers buttons
for __, button in ipairs(alt_handlers_buttons) do
add_button(button)
end
dialog = ButtonDialogTitle:new{
title = text,
use_info_style = true,
buttons = buttons,
}
UIManager:show(dialog)
return true
end
--- Goes back to previous location.
function ReaderLink:onGoBackLink(show_notification_if_empty)
local saved_location = table.remove(self.location_stack)
if saved_location then
logger.dbg("GoBack: restoring:", saved_location)
self.ui:handleEvent(Event:new('RestoreBookLocation', saved_location))
return true
elseif show_notification_if_empty then
UIManager:show(Notification:new{
text = _("Location history is empty."),
timeout = 2,
})
end
end
function ReaderLink:onSwipe(arg, ges)
local direction = BD.flipDirectionIfMirroredUILayout(ges.direction)
if direction == "east" then
if isSwipeToGoBackEnabled() then
if #self.location_stack > 0 then
-- Remember if location stack is going to be empty, so we
-- can stop the propagation of next swipe back: so the user
-- knows it is empty and that next swipe back will get him
-- to previous page (and not to previous location)
self.swipe_back_resist = #self.location_stack == 1
return self:onGoBackLink()
elseif self.swipe_back_resist then
self.swipe_back_resist = false
-- Make that gesture don't do anything, and show a Notification
-- so the user knows why
UIManager:show(Notification:new{
text = _("Location history is empty."),
timeout = 2,
})
return true
end
end
elseif direction == "west" then
local ret = false
if isSwipeToFollowNearestLinkEnabled() then
ret = self:onGoToPageLink(ges, isSwipeIgnoreExternalLinksEnabled(), isFootnoteLinkInPopupEnabled())
end
-- If no link found, or no follow link option enabled,
-- jump to latest bookmark (if enabled)
if not ret and isSwipeToJumpToLatestBookmarkEnabled() then
ret = self:onGoToLatestBookmark(ges)
end
return ret
end
end
--- Goes to link nearest to the gesture (or first link in page)
function ReaderLink:onGoToPageLink(ges, internal_links_only, allow_footnote_popup, max_distance)
local selected_link = nil
local selected_distance2 = nil
-- We use squares of distances all along the calculations, no need
-- to math.sqrt() them when comparing
if self.ui.document.info.has_pages then
local pos = self.view:screenToPageTransform(ges.pos)
if not pos then
return
end
local links = self.ui.document:getPageLinks(pos.page)
if not links or #links == 0 then
return
end
-- DEBUG("PDF Page links : ", links)
-- We may get multiple links: internal ones (with "page" key)
-- that we're interested in, but also external links (no "page", but
-- a "uri" key) that we don't care about.
-- [2] = {
-- ["y1"] = 107.88977050781,
-- ["x1"] = 176.60360717773,
-- ["y0"] = 97.944396972656,
-- ["x0"] = 97,
-- ["page"] = 347
-- },
local pos_x, pos_y = pos.x, pos.y
local shortest_dist = nil
for _, link in ipairs(links) do
if not internal_links_only or link.page then
local start_dist = math.pow(link.x0 - pos_x, 2) + math.pow(link.y0 - pos_y, 2)
local end_dist = math.pow(link.x1 - pos_x, 2) + math.pow(link.y1 - pos_y, 2)
local min_dist = math.min(start_dist, end_dist)
if shortest_dist == nil or min_dist < shortest_dist then
-- onGotoLink()'s GotoPage event needs the link
-- itself, and will use its "page" value
selected_link = link
shortest_dist = min_dist
end
end
end
if shortest_dist then
selected_distance2 = shortest_dist
end
else
-- Getting segments on a page with many internal links is
-- a bit expensive. With larger_tap_area_to_follow_links=true,
-- this is done for each tap on screen (changing pages, showing
-- menu...). We might want to cache these links per page (and
-- clear that cache when page layout change).
-- If we care only about internal links, request them only
-- (and avoid that expensive segments work on external links)
local links = self.ui.document:getPageLinks(internal_links_only)
if not links or #links == 0 then
return
end
-- DEBUG("CRE Page links : ", links)
-- We may get multiple links: internal ones (they have a "section" key)
-- that we're interested in, but also external links (no "section", but
-- a "uri" key) that we don't care about.
-- [1] = {
-- ["end_x"] = 825,
-- ["uri"] = "",
-- ["end_y"] = 333511,
-- ["start_x"] = 90,
-- ["start_y"] = 333511
-- },
-- [2] = {
-- ["end_x"] = 366,
-- ["section"] = "#_doc_fragment_19_ftn_fn6",
-- ["end_y"] = 1201,
-- ["start_x"] = 352,
-- ["start_y"] = 1201
-- ["a_xpointer"] = "/body/DocFragment/body/div/p[12]/sup[3]/a[3].0",
-- },
-- and when segments requested (example for a multi-lines link):
-- [3] = {
-- ["section"] = "#_doc_fragment_0_ Man_of_letters",
-- ["a_xpointer"] = "/body/DocFragment/body/div/div[4]/ul/li[3]/ul/li[2]/ul/li[1]/ul/li[3]/a.0",
-- ["start_x"] = 101,
-- ["start_y"] = 457,
-- ["end_x"] = 176,
-- ["end_y"] = 482,,
-- ["segments"] = {
-- [1] = {
-- ["x0"] = 101,
-- ["y0"] = 457,
-- ["x1"] = 590,
-- ["y1"] = 482,
-- },
-- [2] = {
-- ["x0"] = 101,
-- ["y0"] = 482,
-- ["x1"] = 177,
-- ["y1"] = 507,
-- }
-- },
-- },
-- Note: with some documents and some links, crengine may give wrong
-- coordinates, and our code below may miss or give the wrong first
-- or nearest link...
local pos_x, pos_y = ges.pos.x, ges.pos.y
local shortest_dist = nil
for _, link in ipairs(links) do
-- link.uri may be an empty string with some invalid links: ignore them
if link.section or (link.uri and link.uri ~= "") then
if link.segments then
-- With segments, each is a horizontal segment, with start_x < end_x,
-- and we should compute the distance from gesture position to
-- each segment.
local segments_max_y = -1
local link_is_shortest = false
local segments = link.segments
for i=1, #segments do
local segment = segments[i]
local segment_dist
-- Distance here is kept squared (d^2 = diff_x^2 + diff_y^2),
-- and we compute each part individually
-- First, vertical distance (squared)
if pos_y < segment.y0 then -- above the segment height
segment_dist = math.pow(segment.y0 - pos_y, 2)
elseif pos_y > segment.y1 then -- below the segment height
segment_dist = math.pow(pos_y - segment.y1, 2)
else -- gesture pos is on the segment height, no vertical distance
segment_dist = 0
end
-- Next, horizontal distance (squared)
if pos_x < segment.x0 then -- on the left of segment: calc dist to x0
segment_dist = segment_dist + math.pow(segment.x0 - pos_x, 2)
elseif pos_x > segment.x1 then -- on the right of segment : calc dist to x1
segment_dist = segment_dist + math.pow(pos_x - segment.x1, 2)
-- else -- gesture pos is in the segment width, no horizontal distance
end
if shortest_dist == nil or segment_dist < shortest_dist then
selected_link = link
shortest_dist = segment_dist
link_is_shortest = true
end
if segment.y1 > segments_max_y then
segments_max_y = segment.y1
end
end
if link_is_shortest then
-- update the selected_link we just set with its lower segment y
selected_link.link_y = segments_max_y
end
else
-- Before "segments" were available, we did this:
-- We'd only get a horizontal segment if the link is on a single line.
-- When it is multi-lines, we can't do much calculation...
-- We used to just check distance from start_x and end_x, and
-- we could miss a tap in the middle of a long link.
-- (also start_y = end_y = the top of the rect for a link on a single line)
local start_dist = math.pow(link.start_x - pos_x, 2) + math.pow(link.start_y - pos_y, 2)
local end_dist = math.pow(link.end_x - pos_x, 2) + math.pow(link.end_y - pos_y, 2)
local min_dist = math.min(start_dist, end_dist)
if shortest_dist == nil or min_dist < shortest_dist then
selected_link = link
selected_link.link_y = link.end_y
shortest_dist = min_dist
end
end
end
end
if shortest_dist then
selected_distance2 = shortest_dist
end
if selected_link then
logger.dbg("nearest selected_link", selected_link)
-- Check a_xpointer is coherent, use it as from_xpointer only if it is
local from_xpointer = nil
if selected_link.a_xpointer and self:isXpointerCoherent(selected_link.a_xpointer) then
from_xpointer = selected_link.a_xpointer
end
-- Make it a link as expected by onGotoLink
selected_link = {
xpointer = selected_link.section or selected_link.uri,
marker_xpointer = selected_link.section,
from_xpointer = from_xpointer,
-- (keep a_xpointer even if incoherent, might be needed for
-- footnote detection (better than nothing if incoherent)
a_xpointer = selected_link.a_xpointer,
-- keep the link y position, so we can keep its highlight shown
-- a bit more time if it was hidden by the footnote popup
link_y = selected_link.link_y,
}
end
end
if selected_link then
if max_distance and selected_distance2 and selected_distance2 > math.pow(max_distance, 2) then
logger.dbg("nearest link is further than max distance, ignoring it")
else
return self:onGotoLink(selected_link, false, allow_footnote_popup)
end
end
end
function ReaderLink:onGoToLatestBookmark(ges)
local latest_bookmark = self.ui.bookmark:getLatestBookmark()
if latest_bookmark then
if self.ui.document.info.has_pages then
-- self:onGotoLink() needs something with a page attribute.
-- we need to substract 1 to bookmark page, as links start from 0
-- and onGotoLink will add 1 - we need a fake_link (with a single
-- page attribute) so we don't touch the bookmark itself
local fake_link = {}
fake_link.page = latest_bookmark.page - 1
return self:onGotoLink(fake_link)
else
-- Make it a link as expected by onGotoLink
local link
if latest_bookmark.pos0 then -- text highlighted, precise xpointer
link = {
xpointer = latest_bookmark.pos0,
marker_xpointer = latest_bookmark.pos0,
}
else -- page bookmarked, 'page' is a xpointer to top of page
link = {
xpointer = latest_bookmark.page,
}
end
return self:onGotoLink(link)
end
end
end
function ReaderLink:showAsFootnotePopup(link, neglect_current_location)
if self.ui.document.info.has_pages then
return false -- not supported
end
local source_xpointer = link.from_xpointer or link.a_xpointer
local target_xpointer = link.xpointer
if not source_xpointer or not target_xpointer then
return false
end
local trust_source_xpointer = link.from_xpointer ~= nil
-- For reference, Kobo information and conditions for showing a link as popup:
-- https://github.com/kobolabs/epub-spec#footnotesendnotes-are-fully-supported-across-kobo-platforms
-- Calibre has its own heuristics to decide if a link is to a footnote or not,
-- and what to gather around the footnote target as the footnote content to display:
-- Nearly same logic, implemented in python and in coffeescript:
-- https://github.com/kovidgoyal/calibre/blob/master/src/pyj/read_book/footnotes.pyj
-- https://github.com/kovidgoyal/calibre/blob/master/src/calibre/ebooks/oeb/display/extract.coffee
-- We do many tests, including most of those done by Kobo and Calibre, to
-- detect if a link is to a footnote.
-- The detection is done in cre.cpp, because it makes use of DOM navigation and
-- inspection that can't be done from Lua (unless we add many proxy functions)
-- Detection flags, to allow tweaking a bit cre.cpp code if needed
local flags = 0
-- If no detection decided, fallback to false (not a footnote, so, follow link)
if isPreferFootnoteEnabled() then
flags = flags + 0x0001 -- if set, fallback to true
end
if trust_source_xpointer then
-- trust source_xpointer: allow checking attribute and styles
-- if not trusted, checks marked (*) don't apply