mirror of
https://github.com/koreader/koreader
synced 2024-10-31 21:20:20 +00:00
coolreader search highlight
This is rough port of Android code, but seems to work
This commit is contained in:
parent
5a897fba77
commit
adf0b42e5b
69
cre.cpp
69
cre.cpp
@ -17,6 +17,7 @@
|
|||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define DEBUG_CRENGINE 1
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "blitbuffer.h"
|
#include "blitbuffer.h"
|
||||||
@ -424,6 +425,73 @@ static int registerFont(lua_State *L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ported from Android UI kpvcrlib/crengine/android/jni/docview.cpp
|
||||||
|
|
||||||
|
static int findText(lua_State *L) {
|
||||||
|
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
|
||||||
|
const char *l_pattern = luaL_checkstring(L, 2);
|
||||||
|
lString16 pattern = lString16(l_pattern);
|
||||||
|
int origin = luaL_checkint(L, 3);
|
||||||
|
bool reverse = luaL_checkint(L, 4);
|
||||||
|
bool caseInsensitive = luaL_checkint(L, 5);
|
||||||
|
const char *l_lastPatt = luaL_checkstring(L, 6);
|
||||||
|
lString16 _lastPattern = lString16(l_lastPatt);
|
||||||
|
|
||||||
|
if ( pattern.empty() )
|
||||||
|
return 0;
|
||||||
|
if ( pattern!=_lastPattern && origin==1 )
|
||||||
|
origin = 0;
|
||||||
|
_lastPattern = pattern;
|
||||||
|
LVArray<ldomWord> words;
|
||||||
|
lvRect rc;
|
||||||
|
doc->text_view->GetPos( rc );
|
||||||
|
int pageHeight = rc.height();
|
||||||
|
int start = -1;
|
||||||
|
int end = -1;
|
||||||
|
if ( reverse ) {
|
||||||
|
// reverse
|
||||||
|
if ( origin == 0 ) {
|
||||||
|
// from end current page to first page
|
||||||
|
end = rc.bottom;
|
||||||
|
} else if ( origin == -1 ) {
|
||||||
|
// from last page to end of current page
|
||||||
|
start = rc.bottom;
|
||||||
|
} else { // origin == 1
|
||||||
|
// from prev page to first page
|
||||||
|
end = rc.top;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// forward
|
||||||
|
if ( origin == 0 ) {
|
||||||
|
// from current page to last page
|
||||||
|
start = rc.top;
|
||||||
|
} else if ( origin == -1 ) {
|
||||||
|
// from first page to current page
|
||||||
|
end = rc.top;
|
||||||
|
} else { // origin == 1
|
||||||
|
// from next page to last
|
||||||
|
start = rc.bottom;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CRLog::debug("CRViewDialog::findText: Current page: %d .. %d", rc.top, rc.bottom);
|
||||||
|
CRLog::debug("CRViewDialog::findText: searching for text '%s' from %d to %d origin %d", LCSTR(pattern), start, end, origin );
|
||||||
|
if ( doc->text_view->getDocument()->findText( pattern, caseInsensitive, reverse, start, end, words, 200, pageHeight ) ) {
|
||||||
|
CRLog::debug("CRViewDialog::findText: pattern found");
|
||||||
|
doc->text_view->clearSelection();
|
||||||
|
doc->text_view->selectWords( words );
|
||||||
|
ldomMarkedRangeList * ranges = doc->text_view->getMarkedRanges();
|
||||||
|
if ( ranges ) {
|
||||||
|
if ( ranges->length()>0 ) {
|
||||||
|
int pos = ranges->get(0)->start.y;
|
||||||
|
doc->text_view->SetPos(pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
CRLog::debug("CRViewDialog::findText: pattern not found");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static const struct luaL_Reg cre_func[] = {
|
static const struct luaL_Reg cre_func[] = {
|
||||||
{"openDocument", openDocument},
|
{"openDocument", openDocument},
|
||||||
{"getFontFaces", getFontFaces},
|
{"getFontFaces", getFontFaces},
|
||||||
@ -459,6 +527,7 @@ static const struct luaL_Reg credocument_meth[] = {
|
|||||||
//{"cursorLeft", cursorLeft},
|
//{"cursorLeft", cursorLeft},
|
||||||
//{"cursorRight", cursorRight},
|
//{"cursorRight", cursorRight},
|
||||||
{"drawCurrentPage", drawCurrentPage},
|
{"drawCurrentPage", drawCurrentPage},
|
||||||
|
{"findText", findText},
|
||||||
{"close", closeDocument},
|
{"close", closeDocument},
|
||||||
{"__gc", closeDocument},
|
{"__gc", closeDocument},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
|
@ -592,3 +592,29 @@ function CREReader:adjustCreReaderCommands()
|
|||||||
end
|
end
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
----------------------------------------------------
|
||||||
|
--- search
|
||||||
|
----------------------------------------------------
|
||||||
|
function CREReader:searchHighLight(search)
|
||||||
|
Debug("FIXME CreReader::searchHighLight", search)
|
||||||
|
|
||||||
|
if self.last_search ~= nil then
|
||||||
|
self.last_search = {
|
||||||
|
search = "",
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
self.doc:findText(
|
||||||
|
search,
|
||||||
|
0, -- origin: 0=current 1=prev-first -1=backwards
|
||||||
|
0, -- reverse: boolean
|
||||||
|
1, -- caseInsensitive: boolean
|
||||||
|
self.last_search.search
|
||||||
|
)
|
||||||
|
|
||||||
|
self:redrawCurrentPage()
|
||||||
|
|
||||||
|
self.last_search.search = search
|
||||||
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user