From ed59adecea7cff68c1e4a53d23eebf7e9af3d502 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sun, 26 Aug 2012 14:28:03 +0200 Subject: [PATCH 1/6] naive implementation of search highlight issue #75 --- kpvcrlib/crengine | 2 +- unireader.lua | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/kpvcrlib/crengine b/kpvcrlib/crengine index b62d054cc..7f0e8a096 160000 --- a/kpvcrlib/crengine +++ b/kpvcrlib/crengine @@ -1 +1 @@ -Subproject commit b62d054cca57366bc1ba2fa249c5bbf5c5234346 +Subproject commit 7f0e8a096e80f366b20ba2a301f44bfc27568b98 diff --git a/unireader.lua b/unireader.lua index b041336cd..d77ad239f 100644 --- a/unireader.lua +++ b/unireader.lua @@ -2462,6 +2462,56 @@ function UniReader:addAllCommands() unireader:goto(unireader.pageno) end ) + self.commands:add(KEY_DOT, nil, ".", + "search and highlight text", + function(unireader) + local search = InputBox:input(G_height - 100, 100, + "Search:") + + if search == nil or string.len( search ) < 1 then + unireader:goto(unireader.pageno) + return nil + end + + local t = self:getText(self.pageno) + if not t or #t == 0 then + showInfoMsgWithDelay("No text available for search", 2000, 1); + return nil + end + + Debug("self:getText", self.pageno,t) + + for i = 1, #t, 1 do + for j = 1, #t[i], 1 do + local e = t[i][j] + if e.word ~= nil then + if string.match( e.word, search ) then + + if not self.highlight[self.pageno] then + self.highlight[self.pageno] = {} + end + + local hl_item = { + text = e.word, + [1] = { + x0 = e.x0, + y0 = e.y0, + x1 = e.x1, + y1 = e.y1, + } + } + + table.insert(self.highlight[self.pageno], hl_item) + end + end + end + end + + Debug("self.highlight", self.highlight); + + unireader:goto(unireader.pageno) + end + ) self.commands:add(KEY_P, MOD_SHIFT, "P", "make screenshot", function(unireader) From e8ab2381c39e90a29d2882d370f6bbff84359678 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sun, 26 Aug 2012 14:48:51 +0200 Subject: [PATCH 2/6] extract code into new function UniReader:searchHighLight(search) --- unireader.lua | 83 ++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/unireader.lua b/unireader.lua index d77ad239f..090a9c4d1 100644 --- a/unireader.lua +++ b/unireader.lua @@ -1790,6 +1790,47 @@ function UniReader:showHighLight() end end + +function UniReader:searchHighLight(search) + local t = self:getText(self.pageno) + if not t or #t == 0 then + showInfoMsgWithDelay("No text available for search", 2000, 1); + return nil + end + + Debug("self:getText", self.pageno,t) + + for i = 1, #t, 1 do + for j = 1, #t[i], 1 do + local e = t[i][j] + if e.word ~= nil then + if string.match( e.word, search ) then + + if not self.highlight[self.pageno] then + self.highlight[self.pageno] = {} + end + + local hl_item = { + text = e.word, + [1] = { + x0 = e.x0, + y0 = e.y0, + x1 = e.x1, + y1 = e.y1, + } + } + + table.insert(self.highlight[self.pageno], hl_item) + end + end + end + end + + Debug("self.highlight", self.highlight); + +end + + -- used in UniReader:showMenu() function UniReader:_drawReadingInfo() local width, height = G_width, G_height @@ -2468,47 +2509,9 @@ function UniReader:addAllCommands() local search = InputBox:input(G_height - 100, 100, "Search:") - if search == nil or string.len( search ) < 1 then - unireader:goto(unireader.pageno) - return nil - end - - local t = self:getText(self.pageno) - if not t or #t == 0 then - showInfoMsgWithDelay("No text available for search", 2000, 1); - return nil - end - - Debug("self:getText", self.pageno,t) - - for i = 1, #t, 1 do - for j = 1, #t[i], 1 do - local e = t[i][j] - if e.word ~= nil then - if string.match( e.word, search ) then - - if not self.highlight[self.pageno] then - self.highlight[self.pageno] = {} - end - - local hl_item = { - text = e.word, - [1] = { - x0 = e.x0, - y0 = e.y0, - x1 = e.x1, - y1 = e.y1, - } - } - - table.insert(self.highlight[self.pageno], hl_item) - end - end - end + if search ~= nil and string.len( search ) > 0 then + unireader:searchHighLight(search) end - - Debug("self.highlight", self.highlight); - unireader:goto(unireader.pageno) end ) From eb38dfedd4c2097dc62847e3fffeb8f2b8545237 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sun, 26 Aug 2012 15:16:08 +0200 Subject: [PATCH 3/6] show search highlight until next page refresh --- unireader.lua | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/unireader.lua b/unireader.lua index 090a9c4d1..c02fb9ed7 100644 --- a/unireader.lua +++ b/unireader.lua @@ -1800,11 +1800,17 @@ function UniReader:searchHighLight(search) Debug("self:getText", self.pageno,t) + local found = 0 + local old_highlight = self.highlight + self.highlight = {} -- FIXME show only search results? + + search = string.lower(search) + for i = 1, #t, 1 do for j = 1, #t[i], 1 do local e = t[i][j] if e.word ~= nil then - if string.match( e.word, search ) then + if string.match( string.lower(e.word), search ) then if not self.highlight[self.pageno] then self.highlight[self.pageno] = {} @@ -1821,12 +1827,21 @@ function UniReader:searchHighLight(search) } table.insert(self.highlight[self.pageno], hl_item) + found = found + 1 end end end end - Debug("self.highlight", self.highlight); + if found > 0 then + Debug("self.highlight", self.highlight); + else + found = 'NO' + end + self:goto(self.pageno) -- show highlights, remove input + showInfoMsgWithDelay( found.." hits for "..search, 5000, 1) + + self.highlight = old_highlight -- will not remove search highlights until page refresh end @@ -2512,7 +2527,6 @@ function UniReader:addAllCommands() if search ~= nil and string.len( search ) > 0 then unireader:searchHighLight(search) end - unireader:goto(unireader.pageno) end ) self.commands:add(KEY_P, MOD_SHIFT, "P", From d8273016390023ce7338c412563b37239d3201b5 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sun, 26 Aug 2012 16:03:55 +0200 Subject: [PATCH 4/6] search through whole document --- unireader.lua | 82 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/unireader.lua b/unireader.lua index c02fb9ed7..c12109ed4 100644 --- a/unireader.lua +++ b/unireader.lua @@ -1792,54 +1792,74 @@ end function UniReader:searchHighLight(search) - local t = self:getText(self.pageno) - if not t or #t == 0 then - showInfoMsgWithDelay("No text available for search", 2000, 1); - return nil - end - Debug("self:getText", self.pageno,t) + search = string.lower(search) -- case in-sensitive - local found = 0 local old_highlight = self.highlight self.highlight = {} -- FIXME show only search results? - search = string.lower(search) + local pageno = self.pageno -- start search at current page + local max_pageno = self.doc:getPages() + local found = 0 - for i = 1, #t, 1 do - for j = 1, #t[i], 1 do - local e = t[i][j] - if e.word ~= nil then - if string.match( string.lower(e.word), search ) then + while found == 0 do - if not self.highlight[self.pageno] then - self.highlight[self.pageno] = {} - end + local t = self:getText(pageno) - local hl_item = { - text = e.word, - [1] = { - x0 = e.x0, - y0 = e.y0, - x1 = e.x1, - y1 = e.y1, - } - } + if t ~= nil and #t > 0 then + + Debug("self:getText", pageno, #t) + + for i = 1, #t, 1 do + for j = 1, #t[i], 1 do + local e = t[i][j] + if e.word ~= nil then + if string.match( string.lower(e.word), search ) then - table.insert(self.highlight[self.pageno], hl_item) - found = found + 1 + if not self.highlight[pageno] then + self.highlight[pageno] = {} + end + + local hl_item = { + text = e.word, + [1] = { + x0 = e.x0, + y0 = e.y0, + x1 = e.x1, + y1 = e.y1, + } + } + + table.insert(self.highlight[pageno], hl_item) + found = found + 1 + end + end end end + + else + Debug("self:getText", pageno, 'empty') + end + + if found > 0 then + Debug("self.highlight", self.highlight); + self.pageno = pageno + else + pageno = math.mod( pageno + 1, max_pageno + 1 ) + Debug("next page", pageno, max_pageno) + if pageno == self.pageno then -- wrap around, stop + found = -1 + end end + end + self:goto(self.pageno) -- show highlights, remove input if found > 0 then - Debug("self.highlight", self.highlight); + showInfoMsgWithDelay( found.." hits '"..search.."' page "..self.pageno, 2000, 1) else - found = 'NO' + showInfoMsgWithDelay( "'"..search.."' not found in document", 2000, 1) end - self:goto(self.pageno) -- show highlights, remove input - showInfoMsgWithDelay( found.." hits for "..search, 5000, 1) self.highlight = old_highlight -- will not remove search highlights until page refresh From 2dfee0a2fb6dfa50ab94cd227842b6e5832979fa Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sun, 26 Aug 2012 16:19:51 +0200 Subject: [PATCH 5/6] remember last search so it's incremental now --- unireader.lua | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/unireader.lua b/unireader.lua index c12109ed4..7652a41fb 100644 --- a/unireader.lua +++ b/unireader.lua @@ -88,6 +88,8 @@ UniReader = { toc = nil, bbox = {}, -- override getUsedBBox + + last_search = {} } function UniReader:new(o) @@ -1802,6 +1804,16 @@ function UniReader:searchHighLight(search) local max_pageno = self.doc:getPages() local found = 0 + if self.last_search then + Debug("self.last_search",self.last_search) + if self.last_search.pageno == self.pageno + and self.last_search.search == search + then + pageno = pageno + 1 + Debug("continue search for ", search) + end + end + while found == 0 do local t = self:getText(pageno) @@ -1857,6 +1869,11 @@ function UniReader:searchHighLight(search) self:goto(self.pageno) -- show highlights, remove input if found > 0 then showInfoMsgWithDelay( found.." hits '"..search.."' page "..self.pageno, 2000, 1) + self.last_search = { + pageno = self.pageno, + search = search, + hits = found, + } else showInfoMsgWithDelay( "'"..search.."' not found in document", 2000, 1) end @@ -2542,7 +2559,7 @@ function UniReader:addAllCommands() "search and highlight text", function(unireader) local search = InputBox:input(G_height - 100, 100, - "Search:") + "Search:", self.last_search.search ) if search ~= nil and string.len( search ) > 0 then unireader:searchHighLight(search) From 1c6421dc3f4ebb18f089456c59c0ea563e25514f Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sun, 26 Aug 2012 16:50:16 +0200 Subject: [PATCH 6/6] correctly show default value in inputbox --- inputbox.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/inputbox.lua b/inputbox.lua index f3f16c77d..923194854 100644 --- a/inputbox.lua +++ b/inputbox.lua @@ -149,6 +149,14 @@ function InputBox:input(ypos, height, title, d_text, is_hint) renderUtf8Text(fb.bb, self.input_start_x+5, self.input_start_y, self.face, d_text, 0) fb.bb:dimRect(self.input_start_x-5, self.input_start_y-19, self.input_slot_w, self.fheight, self.input_bg) else + self.input_string = d_text + string.gsub( d_text, "(.)", function(char) + table.insert(self.charlist, self.charpos, char) + self.charpos = self.charpos + 1 + return "" + end) + Debug("charlist", self.charlist) + self.input_cur_x = self.input_cur_x + (self.fwidth * #self.charlist) self.cursor.x_pos = self.cursor.x_pos + (self.fwidth * #self.charlist) self:refreshText()