2017-07-28 20:39:54 +00:00
|
|
|
local ButtonDialog = require("ui/widget/buttondialog")
|
|
|
|
local ConfirmBox = require("ui/widget/confirmbox")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Device = require("device")
|
2013-10-22 15:11:31 +00:00
|
|
|
local Event = require("ui/event")
|
2018-03-05 15:38:04 +00:00
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
2017-07-28 20:39:54 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
2017-09-10 18:35:27 +00:00
|
|
|
local TimeVal = require("ui/timeval")
|
2013-10-22 15:11:31 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2013-10-18 20:38:07 +00:00
|
|
|
local _ = require("gettext")
|
2013-04-23 22:59:52 +00:00
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local ReaderHighlight = InputContainer:new{}
|
2013-04-23 22:59:52 +00:00
|
|
|
|
|
|
|
function ReaderHighlight:init()
|
2014-10-09 09:41:23 +00:00
|
|
|
self.ui:registerPostInitCallback(function()
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
end)
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
|
2017-01-23 14:54:14 +00:00
|
|
|
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,
|
|
|
|
},
|
2017-08-05 19:49:00 +00:00
|
|
|
overrides = { 'tap_forward', 'tap_backward', 'readermenu_tap', 'readerconfigmenu_tap', },
|
2017-01-23 14:54:14 +00:00
|
|
|
handler = function(ges) return self:onTap(nil, ges) end
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2017-01-23 14:54:14 +00:00
|
|
|
{
|
|
|
|
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
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2017-01-23 14:54:14 +00:00
|
|
|
{
|
|
|
|
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
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2017-01-23 14:54:14 +00:00
|
|
|
{
|
|
|
|
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
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2017-01-23 14:54:14 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:onReaderReady()
|
|
|
|
self:setupTouchZones()
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
|
2017-03-04 13:46:38 +00:00
|
|
|
function ReaderHighlight:addToMainMenu(menu_items)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- insert table to main reader menu
|
2017-03-04 13:46:38 +00:00
|
|
|
menu_items.highlight_options = {
|
2017-09-13 10:44:37 +00:00
|
|
|
text = _("Highlighting"),
|
2014-03-13 13:52:43 +00:00
|
|
|
sub_item_table = self:genHighlightDrawerMenu(),
|
2017-02-28 21:46:32 +00:00
|
|
|
}
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
2014-11-06 03:07:48 +00:00
|
|
|
local highlight_style = {
|
|
|
|
lighten = _("Lighten"),
|
2017-04-09 20:16:56 +00:00
|
|
|
underscore = _("Underline"),
|
2014-11-06 03:07:48 +00:00
|
|
|
invert = _("Invert"),
|
|
|
|
}
|
|
|
|
|
2014-01-17 19:05:17 +00:00
|
|
|
function ReaderHighlight:genHighlightDrawerMenu()
|
2014-11-06 03:07:48 +00:00
|
|
|
local get_highlight_style = function(style)
|
|
|
|
return {
|
|
|
|
text = highlight_style[style],
|
2014-06-08 06:02:22 +00:00
|
|
|
checked_func = function()
|
2014-11-06 03:07:48 +00:00
|
|
|
return self.view.highlight.saved_drawer == style
|
2014-05-28 11:45:01 +00:00
|
|
|
end,
|
2014-11-06 03:07:48 +00:00
|
|
|
enabled_func = function()
|
|
|
|
return not self.view.highlight.disabled
|
2014-05-28 11:45:01 +00:00
|
|
|
end,
|
2014-03-13 13:52:43 +00:00
|
|
|
callback = function()
|
2014-11-06 03:07:48 +00:00
|
|
|
self.view.highlight.saved_drawer = style
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-11-06 03:07:48 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
return {
|
2014-03-13 13:52:43 +00:00
|
|
|
{
|
2014-11-06 03:07:48 +00:00
|
|
|
text_func = function()
|
|
|
|
return self.view.highlight.disabled and _("Enable") or _("Disable")
|
2014-05-28 11:45:01 +00:00
|
|
|
end,
|
2014-03-13 13:52:43 +00:00
|
|
|
callback = function()
|
2014-11-06 03:07:48 +00:00
|
|
|
self.view.highlight.disabled = not self.view.highlight.disabled
|
2016-10-23 14:36:50 +00:00
|
|
|
end,
|
2018-09-04 21:55:58 +00:00
|
|
|
hold_callback = function(touchmenu_instance)
|
|
|
|
self:makeDefault(not self.view.highlight.disabled)
|
|
|
|
end,
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2014-11-06 03:07:48 +00:00
|
|
|
get_highlight_style("lighten"),
|
|
|
|
get_highlight_style("underscore"),
|
|
|
|
get_highlight_style("invert"),
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
2017-09-20 15:35:30 +00:00
|
|
|
-- 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
|
2014-11-05 04:28:11 +00:00
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
self.view.highlight.temp = {}
|
|
|
|
else
|
|
|
|
self.ui.document:clearSelection()
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.hold_pos then
|
|
|
|
self.hold_pos = nil
|
2014-07-02 09:46:17 +00:00
|
|
|
self.selected_text = nil
|
2017-06-18 16:08:57 +00:00
|
|
|
UIManager:setDirty(self.dialog, "ui")
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
|
|
|
end
|
2014-08-26 12:29:53 +00:00
|
|
|
end
|
|
|
|
|
2014-11-05 04:28:11 +00:00
|
|
|
function ReaderHighlight:onClearHighlight()
|
|
|
|
self:clear()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2016-03-28 00:18:25 +00:00
|
|
|
function ReaderHighlight:onTap(_, ges)
|
2014-08-26 12:29:53 +00:00
|
|
|
if not self:clear() then
|
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
return self:onTapPageSavedHighlight(ges)
|
|
|
|
else
|
|
|
|
return self:onTapXPointerSavedHighlight(ges)
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function inside_box(pos, box)
|
2014-03-13 13:52:43 +00:00
|
|
|
if pos then
|
|
|
|
local x, y = pos.x, pos.y
|
2014-05-28 11:45:01 +00:00
|
|
|
if box.x <= x and box.y <= y
|
|
|
|
and box.x + box.w >= x
|
2014-03-13 13:52:43 +00:00
|
|
|
and box.y + box.h >= y then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:onTapPageSavedHighlight(ges)
|
2014-03-13 13:52:43 +00:00
|
|
|
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]
|
2017-01-23 14:54:14 +00:00
|
|
|
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
|
2017-11-20 20:58:58 +00:00
|
|
|
logger.dbg("Tap on highlight")
|
2017-01-23 14:54:14 +00:00
|
|
|
return self:onShowHighlightDialog(page, i)
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:onTapXPointerSavedHighlight(ges)
|
2018-10-08 16:57:59 +00:00
|
|
|
-- 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).
|
2017-11-20 20:58:58 +00:00
|
|
|
local cur_page
|
|
|
|
-- In scroll mode, we'll need to check for highlights in previous or next
|
|
|
|
-- page too as some parts of them may be displayed
|
|
|
|
local neighbour_pages = self.view.view_mode ~= "page" and 1 or 0
|
2014-03-13 13:52:43 +00:00
|
|
|
local pos = self.view:screenToPageTransform(ges.pos)
|
|
|
|
for page, _ in pairs(self.view.highlight.saved) do
|
|
|
|
local items = self.view.highlight.saved[page]
|
2017-01-23 14:54:14 +00:00
|
|
|
if items then
|
|
|
|
for i = 1, #items do
|
2017-11-20 20:58:58 +00:00
|
|
|
if not cur_page then
|
|
|
|
cur_page = self.ui.document:getPageFromXPointer(self.ui.document:getXPointer())
|
|
|
|
end
|
2017-01-23 14:54:14 +00:00
|
|
|
local pos0, pos1 = items[i].pos0, items[i].pos1
|
2017-11-20 20:58:58 +00:00
|
|
|
-- document:getScreenBoxesFromPositions() is expensive, so we
|
|
|
|
-- first check this item is on current page
|
|
|
|
local page0 = self.ui.document:getPageFromXPointer(pos0)
|
|
|
|
local page1 = self.ui.document:getPageFromXPointer(pos1)
|
|
|
|
local start_page = math.min(page0, page1)
|
|
|
|
local end_page = math.max(page0, page1)
|
|
|
|
-- In scroll mode, we may be displaying cur_page and cur_page+1, so
|
|
|
|
-- we have to check the highlight start_page is <= cur_page+1.
|
|
|
|
-- Same thinking with highlight's end_page >= cur_page-1 as we may
|
|
|
|
-- be displaying a part of cur_page-1.
|
|
|
|
-- (A highlight starting on cur_page-17 and ending on cur_page+13 is
|
|
|
|
-- a highlight to consider)
|
|
|
|
if start_page <= cur_page + neighbour_pages and end_page >= cur_page - neighbour_pages then
|
|
|
|
local boxes = self.ui.document:getScreenBoxesFromPositions(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
|
2017-01-23 14:54:14 +00:00
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
|
2014-01-17 19:05:17 +00:00
|
|
|
function ReaderHighlight:onShowHighlightDialog(page, index)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.edit_highlight_dialog = ButtonDialog:new{
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Delete"),
|
|
|
|
callback = function()
|
|
|
|
self:deleteHighlight(page, index)
|
2015-07-04 17:53:43 +00:00
|
|
|
-- other part outside of the dialog may be dirty
|
|
|
|
UIManager:close(self.edit_highlight_dialog, "ui")
|
2014-03-13 13:52:43 +00:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Edit"),
|
|
|
|
callback = function()
|
2017-10-18 15:19:06 +00:00
|
|
|
self:editHighlight(page, index)
|
2014-03-13 13:52:43 +00:00
|
|
|
UIManager:close(self.edit_highlight_dialog)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
UIManager:show(self.edit_highlight_dialog)
|
|
|
|
return true
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
2017-01-15 20:51:43 +00:00
|
|
|
function ReaderHighlight:onHold(arg, ges)
|
2014-11-06 03:07:48 +00:00
|
|
|
-- disable hold gesture if highlighting is disabled
|
|
|
|
if self.view.highlight.disabled then return true end
|
2017-09-20 15:35:30 +00:00
|
|
|
self:clear() -- clear previous highlight (delayed clear may not have done it yet)
|
2018-02-26 16:25:34 +00:00
|
|
|
self.hold_ges_pos = ges.pos -- remember hold original gesture position
|
2014-03-13 13:52:43 +00:00
|
|
|
self.hold_pos = self.view:screenToPageTransform(ges.pos)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("hold position in page", self.hold_pos)
|
2014-03-13 13:52:43 +00:00
|
|
|
if not self.hold_pos then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("not inside page area")
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
|
|
|
end
|
2013-10-12 15:07:13 +00:00
|
|
|
|
2017-01-15 20:51:43 +00:00
|
|
|
-- check if we were holding on an image
|
2018-04-21 20:03:04 +00:00
|
|
|
-- 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)
|
2017-01-15 20:51:43 +00:00
|
|
|
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
|
2014-03-13 13:52:43 +00:00
|
|
|
local ok, word = pcall(self.ui.document.getWordFromPosition, self.ui.document, self.hold_pos)
|
|
|
|
if ok and word then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("selected word:", word)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.selected_word = word
|
2017-09-10 12:26:49 +00:00
|
|
|
local link = self.ui.link:getLinkFromGes(ges)
|
2017-09-09 16:30:00 +00:00
|
|
|
self.selected_link = nil
|
|
|
|
if link then
|
|
|
|
logger.dbg("link:", link)
|
|
|
|
self.selected_link = link
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
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
|
2017-06-18 16:08:57 +00:00
|
|
|
UIManager:setDirty(self.dialog, "ui")
|
2014-11-30 00:12:00 +00:00
|
|
|
-- TODO: only mark word?
|
2014-12-01 14:39:41 +00:00
|
|
|
-- Unfortunately, CREngine does not return good coordinates
|
2014-11-30 00:12:00 +00:00
|
|
|
-- UIManager:setDirty(self.dialog, "partial", self.selected_word.sbox)
|
2017-09-10 18:35:27 +00:00
|
|
|
self.hold_start_tv = TimeVal.now()
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
return true
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2016-03-28 00:18:25 +00:00
|
|
|
function ReaderHighlight:onHoldPan(_, ges)
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.hold_pos == nil then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("no previous hold position")
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
|
|
|
end
|
2014-07-02 08:38:09 +00:00
|
|
|
local page_area = self.view:getScreenPageArea(self.hold_pos.page)
|
|
|
|
if ges.pos:notIntersectWith(page_area) then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("not inside page area", ges, page_area)
|
2014-07-02 08:38:09 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
self.holdpan_pos = self.view:screenToPageTransform(ges.pos)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("holdpan position in page", self.holdpan_pos)
|
2014-11-30 00:12:00 +00:00
|
|
|
local old_text = self.selected_text and self.selected_text.text
|
2014-03-13 13:52:43 +00:00
|
|
|
self.selected_text = self.ui.document:getTextFromPositions(self.hold_pos, self.holdpan_pos)
|
2014-11-30 00:12:00 +00:00
|
|
|
if self.selected_text and old_text and old_text == self.selected_text.text then
|
|
|
|
-- no modification
|
|
|
|
return
|
|
|
|
end
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("selected text:", self.selected_text)
|
2014-03-13 13:52:43 +00:00
|
|
|
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
|
2014-12-03 06:06:43 +00:00
|
|
|
UIManager:setDirty(self.dialog, "ui")
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2018-08-01 17:29:03 +00:00
|
|
|
local info_message_ocr_text = _(
|
|
|
|
[[No OCR results or no language data.
|
2018-03-05 15:38:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-03-17 17:13:04 +00:00
|
|
|
Copy the language data files for Tesseract 3.04 (e.g., eng.traineddata for English and spa.traineddata for Spanish) into koreader/data/tessdata]])
|
2018-03-05 15:38:04 +00:00
|
|
|
|
2017-09-09 16:30:00 +00:00
|
|
|
function ReaderHighlight:lookup(selected_word, selected_link)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- if we extracted text directly
|
|
|
|
if selected_word.word then
|
|
|
|
local word_box = self.view:pageToScreenTransform(self.hold_pos.page, selected_word.sbox)
|
2017-09-09 16:30:00 +00:00
|
|
|
self.ui:handleEvent(Event:new("LookupWord", selected_word.word, word_box, self, selected_link))
|
2014-03-13 13:52:43 +00:00
|
|
|
-- 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)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("OCRed word:", word)
|
2018-03-05 15:38:04 +00:00
|
|
|
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
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2013-07-19 18:49:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:translate(selected_text)
|
2014-03-13 13:52:43 +00:00
|
|
|
if selected_text.text ~= "" then
|
|
|
|
self.ui:handleEvent(Event:new("TranslateText", self, selected_text.text))
|
|
|
|
-- or we will do OCR
|
|
|
|
else
|
|
|
|
local text = self.ui.document:getOCRText(self.hold_pos.page, selected_text)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("OCRed text:", text)
|
2018-03-05 15:38:04 +00:00
|
|
|
if text and text ~= "" then
|
|
|
|
self.ui:handleEvent(Event:new("TranslateText", self, text))
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = info_message_ocr_text,
|
|
|
|
})
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2013-07-19 18:49:03 +00:00
|
|
|
end
|
|
|
|
|
2014-08-11 13:49:42 +00:00
|
|
|
function ReaderHighlight:onHoldRelease()
|
2017-09-10 18:35:27 +00:00
|
|
|
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
|
2018-02-26 16:25:34 +00:00
|
|
|
self:onHoldPan(nil, {pos=self.hold_ges_pos})
|
2017-09-10 18:35:27 +00:00
|
|
|
end
|
|
|
|
end
|
2018-02-26 16:25:34 +00:00
|
|
|
if self.selected_text then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("show highlight dialog")
|
2014-03-13 13:52:43 +00:00
|
|
|
self.highlight_dialog = ButtonDialog:new{
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Highlight"),
|
|
|
|
callback = function()
|
|
|
|
self:saveHighlight()
|
2014-08-20 06:41:45 +00:00
|
|
|
self:onClose()
|
2014-03-13 13:52:43 +00:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Add Note"),
|
|
|
|
enabled = false,
|
|
|
|
callback = function()
|
|
|
|
self:addNote()
|
2014-08-20 06:41:45 +00:00
|
|
|
self:onClose()
|
2014-03-13 13:52:43 +00:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
2017-09-09 16:30:00 +00:00
|
|
|
{
|
|
|
|
{
|
2017-11-14 18:20:58 +00:00
|
|
|
text = "Copy",
|
|
|
|
enabled = Device:hasClipboard(),
|
|
|
|
callback = function()
|
|
|
|
Device.input.setClipboardText(self.selected_text.text)
|
|
|
|
end,
|
2017-09-09 16:30:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Translate"),
|
|
|
|
enabled = false,
|
|
|
|
callback = function()
|
|
|
|
self:translate(self.selected_text)
|
|
|
|
self:onClose()
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
2014-03-13 13:52:43 +00:00
|
|
|
{
|
2014-08-20 06:41:45 +00:00
|
|
|
{
|
|
|
|
text = _("Wikipedia"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:scheduleIn(0.1, function()
|
|
|
|
self:lookupWikipedia()
|
2017-10-06 13:58:58 +00:00
|
|
|
-- We don't call self:onClose(), we need the highlight
|
|
|
|
-- to still be there, as we may Highlight it from the
|
|
|
|
-- dict lookup widget
|
2014-08-20 06:41:45 +00:00
|
|
|
end)
|
|
|
|
end,
|
|
|
|
},
|
2014-03-13 13:52:43 +00:00
|
|
|
{
|
2017-09-09 16:30:00 +00:00
|
|
|
text = _("Dictionary"),
|
2014-03-13 13:52:43 +00:00
|
|
|
callback = function()
|
2017-09-09 16:30:00 +00:00
|
|
|
self:onHighlightDictLookup()
|
2017-10-06 13:58:58 +00:00
|
|
|
-- We don't call self:onClose(), same reason as above
|
2014-03-13 13:52:43 +00:00
|
|
|
end,
|
|
|
|
},
|
2014-08-20 06:41:45 +00:00
|
|
|
},
|
|
|
|
{
|
2014-03-13 13:52:43 +00:00
|
|
|
{
|
2017-09-09 16:30:00 +00:00
|
|
|
text = _("Follow Link"),
|
|
|
|
enabled = self.selected_link ~= nil,
|
2014-03-13 13:52:43 +00:00
|
|
|
callback = function()
|
2017-09-10 12:26:49 +00:00
|
|
|
self.ui.link:onGotoLink(self.selected_link)
|
2017-09-10 12:39:10 +00:00
|
|
|
self:onClose()
|
2014-03-13 13:52:43 +00:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
2017-09-09 16:30:00 +00:00
|
|
|
text = _("Search"),
|
2014-03-13 13:52:43 +00:00
|
|
|
callback = function()
|
2017-09-09 16:30:00 +00:00
|
|
|
self:onHighlightSearch()
|
|
|
|
UIManager:close(self.highlight_dialog)
|
2014-03-13 13:52:43 +00:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
tap_close_callback = function() self:handleEvent(Event:new("Tap")) end,
|
|
|
|
}
|
2014-12-01 14:39:41 +00:00
|
|
|
UIManager:show(self.highlight_dialog)
|
2018-02-26 16:25:34 +00:00
|
|
|
elseif self.selected_word then
|
|
|
|
self:lookup(self.selected_word, self.selected_link)
|
|
|
|
self.selected_word = nil
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
return true
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
|
2014-11-05 04:28:11 +00:00
|
|
|
function ReaderHighlight:highlightFromHoldPos()
|
2014-08-11 13:49:42 +00:00
|
|
|
if self.hold_pos then
|
|
|
|
if not self.selected_text then
|
|
|
|
self.selected_text = self.ui.document:getTextFromPositions(self.hold_pos, self.hold_pos)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("selected text:", self.selected_text)
|
2014-08-11 13:49:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-05 04:28:11 +00:00
|
|
|
function ReaderHighlight:onHighlight()
|
|
|
|
self:saveHighlight()
|
|
|
|
end
|
|
|
|
|
2017-10-18 15:19:06 +00:00
|
|
|
function ReaderHighlight:onUnhighlight(bookmark_item)
|
2017-07-28 20:39:54 +00:00
|
|
|
local page
|
|
|
|
local sel_text
|
|
|
|
local sel_pos0
|
2017-10-18 15:19:06 +00:00
|
|
|
local datetime
|
2017-07-28 20:39:54 +00:00
|
|
|
local idx
|
2017-10-18 15:19:06 +00:00
|
|
|
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
|
2017-07-28 20:39:54 +00:00
|
|
|
page = self.hold_pos.page
|
|
|
|
sel_text = self.selected_text.text
|
|
|
|
sel_pos0 = self.selected_text.pos0
|
|
|
|
end
|
2017-10-18 15:19:06 +00:00
|
|
|
if self.ui.document.info.has_pages then -- We can safely use page
|
|
|
|
for index = 1, #self.view.highlight.saved[page] do
|
|
|
|
local highlight = self.view.highlight.saved[page][index]
|
|
|
|
-- pos0 are tables and can't be compared directly, except when from
|
|
|
|
-- DictQuickLookup where these are the same object.
|
|
|
|
-- If bookmark_item provided, just check datetime
|
|
|
|
if highlight.text == sel_text and (
|
|
|
|
(datetime == nil and highlight.pos0 == sel_pos0) or
|
|
|
|
(datetime ~= nil and highlight.datetime == datetime)) then
|
|
|
|
idx = index
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else -- page is a xpointer
|
|
|
|
-- The original page could be found in bookmark_item.text, but
|
|
|
|
-- no more if it has been renamed: we need to loop through all
|
|
|
|
-- highlights on all page slots
|
|
|
|
for p, highlights in pairs(self.view.highlight.saved) do
|
|
|
|
for index = 1, #highlights do
|
|
|
|
local highlight = highlights[index]
|
|
|
|
-- pos0 are strings and can be compared directly
|
|
|
|
if highlight.text == sel_text and (
|
|
|
|
(datetime == nil and highlight.pos0 == sel_pos0) or
|
|
|
|
(datetime ~= nil and highlight.datetime == datetime)) then
|
|
|
|
page = p -- this is the original page slot
|
|
|
|
idx = index
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if idx then
|
|
|
|
break
|
|
|
|
end
|
2017-07-28 20:39:54 +00:00
|
|
|
end
|
|
|
|
end
|
2017-10-18 15:19:06 +00:00
|
|
|
if bookmark_item and not idx then
|
|
|
|
logger.warn("unhighlight: bookmark_item not found among highlights", bookmark_item)
|
|
|
|
-- Remove it from bookmarks anyway, so we're not stuck with an
|
|
|
|
-- unremovable bookmark
|
|
|
|
self.ui.bookmark:removeBookmark(bookmark_item)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
logger.dbg("found highlight to delete on page", page, idx)
|
|
|
|
self:deleteHighlight(page, idx, bookmark_item)
|
2017-07-28 20:39:54 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2015-03-12 10:50:57 +00:00
|
|
|
function ReaderHighlight:getHighlightBookmarkItem()
|
|
|
|
if self.hold_pos and not self.selected_text then
|
|
|
|
self:highlightFromHoldPos()
|
|
|
|
end
|
|
|
|
if self.selected_text and self.selected_text.pos0 and self.selected_text.pos1 then
|
|
|
|
local datetime = os.date("%Y-%m-%d %H:%M:%S")
|
|
|
|
local page = self.ui.document.info.has_pages and
|
|
|
|
self.hold_pos.page or self.selected_text.pos0
|
|
|
|
return {
|
|
|
|
page = page,
|
|
|
|
pos0 = self.selected_text.pos0,
|
|
|
|
pos1 = self.selected_text.pos1,
|
|
|
|
datetime = datetime,
|
|
|
|
notes = self.selected_text.text,
|
|
|
|
highlighted = true,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-15 15:13:19 +00:00
|
|
|
function ReaderHighlight:saveHighlight()
|
2017-07-27 17:00:37 +00:00
|
|
|
self.ui:handleEvent(Event:new("AddHighlight"))
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("save highlight")
|
2014-03-13 13:52:43 +00:00
|
|
|
local page = self.hold_pos.page
|
2015-02-01 16:09:07 +00:00
|
|
|
if self.hold_pos and self.selected_text and self.selected_text.pos0
|
|
|
|
and self.selected_text.pos1 then
|
2014-03-13 13:52:43 +00:00
|
|
|
if not self.view.highlight.saved[page] then
|
|
|
|
self.view.highlight.saved[page] = {}
|
|
|
|
end
|
2014-11-27 13:59:27 +00:00
|
|
|
local datetime = os.date("%Y-%m-%d %H:%M:%S")
|
|
|
|
local hl_item = {
|
|
|
|
datetime = datetime,
|
|
|
|
text = self.selected_text.text,
|
|
|
|
pos0 = self.selected_text.pos0,
|
|
|
|
pos1 = self.selected_text.pos1,
|
|
|
|
pboxes = self.selected_text.pboxes,
|
|
|
|
drawer = self.view.highlight.saved_drawer,
|
|
|
|
}
|
2014-03-13 13:52:43 +00:00
|
|
|
table.insert(self.view.highlight.saved[page], hl_item)
|
2015-03-12 10:50:57 +00:00
|
|
|
local bookmark_item = self:getHighlightBookmarkItem()
|
|
|
|
if bookmark_item then
|
|
|
|
self.ui.bookmark:addBookmark(bookmark_item)
|
|
|
|
end
|
2014-11-21 10:32:43 +00:00
|
|
|
--[[
|
2017-11-20 20:58:58 +00:00
|
|
|
-- disable exporting highlights to My Clippings
|
2014-11-21 10:32:43 +00:00
|
|
|
-- since it's not portable and there is a better Evernote plugin
|
|
|
|
-- to do the same thing
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.selected_text.text ~= "" then
|
2014-11-21 10:32:43 +00:00
|
|
|
self:exportToClippings(page, hl_item)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-11-21 10:32:43 +00:00
|
|
|
--]]
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.selected_text.pboxes then
|
|
|
|
self:exportToDocument(page, hl_item)
|
|
|
|
end
|
|
|
|
end
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2014-11-21 10:32:43 +00:00
|
|
|
--[[
|
2013-06-16 11:00:15 +00:00
|
|
|
function ReaderHighlight:exportToClippings(page, item)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("export highlight to clippings", item)
|
2014-03-13 13:52:43 +00:00
|
|
|
local clippings = io.open("/mnt/us/documents/My Clippings.txt", "a+")
|
|
|
|
if clippings and item.text then
|
|
|
|
local current_locale = os.setlocale()
|
|
|
|
os.setlocale("C")
|
|
|
|
clippings:write(self.document.file:gsub("(.*/)(.*)", "%2").."\n")
|
2016-03-28 01:50:23 +00:00
|
|
|
clippings:write("- KOReader Highlight Page "..page.." ")
|
2014-03-13 13:52:43 +00:00
|
|
|
clippings:write("| Added on "..os.date("%A, %b %d, %Y %I:%M:%S %p\n\n"))
|
2014-05-12 12:32:17 +00:00
|
|
|
-- My Clippings only holds one line of highlight
|
|
|
|
clippings:write(item["text"]:gsub("\n", " ").."\n")
|
2014-03-13 13:52:43 +00:00
|
|
|
clippings:write("==========\n")
|
|
|
|
clippings:close()
|
|
|
|
os.setlocale(current_locale)
|
|
|
|
end
|
2013-06-16 11:00:15 +00:00
|
|
|
end
|
2014-11-21 10:32:43 +00:00
|
|
|
--]]
|
2013-06-16 11:00:15 +00:00
|
|
|
|
2014-02-01 16:16:51 +00:00
|
|
|
function ReaderHighlight:exportToDocument(page, item)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("export highlight to document", item)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.ui.document:saveHighlight(page, item)
|
2014-02-01 16:16:51 +00:00
|
|
|
end
|
|
|
|
|
2013-06-15 15:13:19 +00:00
|
|
|
function ReaderHighlight:addNote()
|
2015-09-07 17:06:17 +00:00
|
|
|
self:handleEvent(Event:new("addNote"))
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("add Note")
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2014-08-20 06:41:45 +00:00
|
|
|
function ReaderHighlight:lookupWikipedia()
|
|
|
|
if self.selected_text then
|
|
|
|
self.ui:handleEvent(Event:new("LookupWikipedia", self.selected_text.text))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-05 04:28:11 +00:00
|
|
|
function ReaderHighlight:onHighlightSearch()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("search highlight")
|
2014-11-05 04:28:11 +00:00
|
|
|
self:highlightFromHoldPos()
|
|
|
|
if self.selected_text then
|
2015-03-12 09:41:20 +00:00
|
|
|
local text = require("util").stripePunctuations(self.selected_text.text)
|
|
|
|
self.ui:handleEvent(Event:new("ShowSearchDialog", text))
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-15 10:11:42 +00:00
|
|
|
function ReaderHighlight:onHighlightDictLookup()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("dictionary lookup highlight")
|
2015-06-15 10:11:42 +00:00
|
|
|
self:highlightFromHoldPos()
|
|
|
|
if self.selected_text then
|
2016-12-06 21:15:52 +00:00
|
|
|
self.ui:handleEvent(Event:new("LookupWord", self.selected_text.text))
|
2015-06-15 10:11:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-15 15:13:19 +00:00
|
|
|
function ReaderHighlight:shareHighlight()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.info("share highlight")
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:moreAction()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.info("more action")
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2017-10-18 15:19:06 +00:00
|
|
|
function ReaderHighlight:deleteHighlight(page, i, bookmark_item)
|
2017-07-27 17:00:37 +00:00
|
|
|
self.ui:handleEvent(Event:new("DelHighlight"))
|
2017-10-18 15:19:06 +00:00
|
|
|
logger.dbg("delete highlight", page, i)
|
2014-11-27 13:59:27 +00:00
|
|
|
local removed = table.remove(self.view.highlight.saved[page], i)
|
2017-10-18 15:19:06 +00:00
|
|
|
if bookmark_item then
|
|
|
|
self.ui.bookmark:removeBookmark(bookmark_item)
|
|
|
|
else
|
|
|
|
self.ui.bookmark:removeBookmark({
|
|
|
|
page = self.ui.document.info.has_pages and page or removed.pos0,
|
|
|
|
datetime = removed.datetime,
|
|
|
|
})
|
|
|
|
end
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2017-10-18 15:19:06 +00:00
|
|
|
function ReaderHighlight:editHighlight(page, i)
|
|
|
|
logger.info("edit highlight", page, i)
|
|
|
|
local item = self.view.highlight.saved[page][i]
|
|
|
|
self.ui.bookmark:renameBookmark({
|
|
|
|
page = self.ui.document.info.has_pages and page or item.pos0,
|
|
|
|
datetime = item.datetime,
|
|
|
|
}, true)
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
2014-01-17 19:05:17 +00:00
|
|
|
function ReaderHighlight:onReadSettings(config)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.view.highlight.saved_drawer = config:readSetting("highlight_drawer") or self.view.highlight.saved_drawer
|
2016-10-23 14:36:50 +00:00
|
|
|
local disable_highlight = config:readSetting("highlight_disabled")
|
|
|
|
if disable_highlight == nil then
|
|
|
|
disable_highlight = G_reader_settings:readSetting("highlight_disabled") or false
|
|
|
|
end
|
|
|
|
self.view.highlight.disabled = disable_highlight
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:onSaveSettings()
|
2014-03-13 13:52:43 +00:00
|
|
|
self.ui.doc_settings:saveSetting("highlight_drawer", self.view.highlight.saved_drawer)
|
2014-11-06 03:07:48 +00:00
|
|
|
self.ui.doc_settings:saveSetting("highlight_disabled", self.view.highlight.disabled)
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
2014-08-20 06:41:45 +00:00
|
|
|
function ReaderHighlight:onClose()
|
|
|
|
UIManager:close(self.highlight_dialog)
|
|
|
|
-- clear highlighted text
|
2014-08-26 12:29:53 +00:00
|
|
|
self:clear()
|
2014-08-20 06:41:45 +00:00
|
|
|
end
|
|
|
|
|
2016-10-23 14:36:50 +00:00
|
|
|
function ReaderHighlight:makeDefault(highlight_disabled)
|
|
|
|
local new_text
|
|
|
|
if highlight_disabled then
|
|
|
|
new_text = _("Disable highlight by default.")
|
|
|
|
else
|
|
|
|
new_text = _("Enable highlight by default.")
|
|
|
|
end
|
|
|
|
UIManager:show(ConfirmBox:new{
|
|
|
|
text = new_text,
|
|
|
|
ok_callback = function()
|
|
|
|
G_reader_settings:saveSetting("highlight_disabled", highlight_disabled)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return ReaderHighlight
|