2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00

getPageLinks implementation for crengine formats

This commit is contained in:
Dobrica Pavlinusic 2012-10-11 19:25:24 +02:00
parent 5f1ce0e82d
commit 60e01c8636
3 changed files with 102 additions and 3 deletions

67
cre.cpp
View File

@ -390,6 +390,72 @@ static int cursorRight(lua_State *L) {
return 0;
}
static int getPageLinks(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
lua_newtable(L); // all links
int pos = doc->text_view->GetPos();
printf("## pos=%d\n", pos);
ldomXRangeList links;
ldomXRangeList & sel = doc->text_view->getDocument()->getSelections();
doc->text_view->getCurrentPageLinks( links );
int linkCount = links.length();
if ( linkCount ) {
sel.clear();
for ( int i=0; i<linkCount; i++ ) {
sel.add( new ldomXRange(*links[i]) );
lString16 txt = links[i]->getRangeText();
lString8 txt8 = UnicodeToLocal( txt );
lString16 link = links[i]->getHRef();
lString8 link8 = UnicodeToLocal( link );
ldomXRange currSel;
currSel = *links[i];
lvPoint start_pt ( currSel.getStart().toPoint() );
lvPoint end_pt ( currSel.getEnd().toPoint() );
printf("# link %d start %d %d end %d %d '%s' %s\n", i,
start_pt.x, start_pt.y, end_pt.x, end_pt.y,
txt8.c_str(), link8.c_str()
);
lua_newtable(L); // new link
lua_pushstring(L, "start_x");
lua_pushinteger(L, start_pt.x);
lua_settable(L, -3);
lua_pushstring(L, "start_y");
lua_pushinteger(L, start_pt.y);
lua_settable(L, -3);
lua_pushstring(L, "end_x");
lua_pushinteger(L, end_pt.x);
lua_settable(L, -3);
lua_pushstring(L, "end_y");
lua_pushinteger(L, end_pt.y);
lua_settable(L, -3);
const char * link_to = link8.c_str();
lua_pushstring(L, "section");
lua_pushstring(L, link_to);
lua_settable(L, -3);
lua_rawseti(L, -2, i + 1);
}
doc->text_view->updateSelections();
}
return 1;
}
static int drawCurrentPage(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext");
@ -536,6 +602,7 @@ static const struct luaL_Reg credocument_meth[] = {
//{"cursorRight", cursorRight},
{"drawCurrentPage", drawCurrentPage},
{"findText", findText},
{"getPageLinks", getPageLinks},
{"close", closeDocument},
{"__gc", closeDocument},
{NULL, NULL}

View File

@ -680,6 +680,12 @@ function CREReader:adjustCreReaderCommands()
)
end
function CREReader:getPageLinks()
local links = self.doc:getPageLinks()
Debug("getPageLinks", links)
return links
end
----------------------------------------------------
--- search
----------------------------------------------------

View File

@ -2993,13 +2993,24 @@ function UniReader:addAllCommands()
if links == nil or next(links) == nil then
InfoMessage:inform("No links on this page ", 2000, 1, MSG_WARN)
else
local font_size
if links[1].y0 then
Debug("using link box from mupdf for font size")
font_size = math.ceil( (links[1].y1 - links[1].y0 - 2) * unireader.globalzoom )
else
Debug("using font size from crengine for font size")
font_size = self.font_zoom
end
Debug("font_size",font_size)
Debug("shortcuts",SelectMenu.item_shortcuts)
local page_links = 0
local visible_links = {}
for i, link in ipairs(links) do
if link.page then
if link.page then -- from mupdf
local x,y,w,h = self:zoomedRectCoordTransform( link.x0,link.y0, link.x1,link.y1 )
if x > 0 and y > 0 and x < G_width and y < G_height then
-- draw top and side borders so we get a box for each link (bottom one is on page)
@ -3012,6 +3023,10 @@ function UniReader:addAllCommands()
page_links = page_links + 1
visible_links[page_links] = link
end
elseif link.section then -- from crengine
-- fb.bb:invertRect(link.x0,link.y0,link.x1 - link.x0, link.y1-link.y0)
page_links = page_links + 1
visible_links[page_links] = link
end
end
@ -3020,6 +3035,8 @@ function UniReader:addAllCommands()
return
end
Debug("visible_links", visible_links)
Screen:saveCurrentBB() -- save dimmed links
local shortcut_offset = 0
@ -3035,10 +3052,19 @@ function UniReader:addAllCommands()
local link = visible_links[ i + shortcut_offset ]
if link == nil then break end
Debug("link", i, shortcut_offset, link)
local x,y,w,h
if link.page then
local x,y,w,h = self:zoomedRectCoordTransform( link.x0,link.y0, link.x1,link.y1 )
x,y,w,h = self:zoomedRectCoordTransform( link.x0,link.y0, link.x1,link.y1 )
elseif link.section then
x,y,w,h = link.start_x, link.start_y, 10, 10
end
Debug("link coords",x,y,w,h)
if x and y and w and h then
Debug("shortcut", x,y, SelectMenu.item_shortcuts[shortcut_nr])
local face = Font:getFace("rifont", h)
renderUtf8Text(fb.bb, x, y + h - 2, face, SelectMenu.item_shortcuts[shortcut_nr])
renderUtf8Text(fb.bb, x, y + font_size - 1, face, SelectMenu.item_shortcuts[shortcut_nr])
shortcut_map[shortcut_nr] = i + shortcut_offset
shortcut_nr = shortcut_nr + 1
end