From 4633ca08328f9dc21661e86bebfb067a45982099 Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Sat, 31 Mar 2012 20:22:22 +0800 Subject: [PATCH] mod: use percent as absolute location in DOCs in CREReader The height of documents will be changed after zoom in or zoom out, so we cannot use pos to mark positions inside documents. --- cre.cpp | 21 ++++++++++++++++++++- crereader.lua | 26 ++++++++++++++++---------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/cre.cpp b/cre.cpp index 96c2aad0a..efd989863 100644 --- a/cre.cpp +++ b/cre.cpp @@ -84,6 +84,14 @@ static int getPos(lua_State *L) { return 1; } +static int getPosPercent(lua_State *L) { + CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument"); + + lua_pushinteger(L, doc->text_view->getPosPercent()); + + return 1; +} + static int getFullHeight(lua_State *L) { CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument"); @@ -161,6 +169,15 @@ static int gotoPage(lua_State *L) { return 0; } +static int gotoPercent(lua_State *L) { + CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument"); + int percent = luaL_checkint(L, 2); + + doc->text_view->SetPos(percent * doc->text_view->GetFullHeight() / 10000); + + return 0; +} + static int gotoPos(lua_State *L) { CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument"); int pos = luaL_checkint(L, 2); @@ -223,9 +240,11 @@ static const struct luaL_Reg credocument_meth[] = { {"getPages", getNumberOfPages}, {"getCurrentPage", getCurrentPage}, {"getPos", getPos}, - {"GetFullHeight", getFullHeight}, + {"getPosPercent", getPosPercent}, + {"getFullHeight", getFullHeight}, {"getToc", getTableOfContent}, {"gotoPage", gotoPage}, + {"gotoPercent", gotoPercent}, {"gotoPos", gotoPos}, {"zoomFont", zoomFont}, {"drawCurrentPage", drawCurrentPage}, diff --git a/crereader.lua b/crereader.lua index 679df6d4a..c730eb846 100644 --- a/crereader.lua +++ b/crereader.lua @@ -3,6 +3,7 @@ require "inputbox" CREReader = UniReader:new{ pos = 0, + percent = 0, pan_overlap_vertical = 0, } @@ -26,11 +27,16 @@ function CREReader:open(filename) end function CREReader:getLastPageOrPos() - return self.settings:readSetting("last_pos") or 0 + local last_percent = self.settings:readSetting("last_percent") + if last_percent then + return (last_percent * self.doc:getFullHeight()) / 10000 + else + return 0 + end end function CREReader:saveLastPageOrPos() - self.settings:savesetting("last_pos", self.pos) + self.settings:savesetting("last_percent", self.percent) end function CREReader:setzoom(page, preCache) @@ -41,12 +47,12 @@ function CREReader:addJump(pos, notes) end function CREReader:goto(pos) - local pos = math.min(pos, self.doc:GetFullHeight()) + local pos = math.min(pos, self.doc:getFullHeight()) pos = math.max(pos, 0) -- add to jump_stack, distinguish jump from normal page turn if self.pos and math.abs(self.pos - pos) > height then - self:addJump(self.pos) + self:addJump(self.percent) end self.doc:gotoPos(pos) @@ -64,6 +70,7 @@ function CREReader:goto(pos) self.pos = pos self.pageno = self.doc:getCurrentPage() + self.percent = self.doc:getPosPercent() end function CREReader:redrawCurrentPage() @@ -73,7 +80,7 @@ end -- used in CREReader:showMenu() function CREReader:_drawReadingInfo() local ypos = height - 50 - local load_percent = (self.pos / self.doc:GetFullHeight()) + local load_percent = self.percent/100 fb.bb:paintRect(0, ypos, width, 50, 0) @@ -84,8 +91,7 @@ function CREReader:_drawReadingInfo() cur_section = "Section: "..cur_section end renderUtf8Text(fb.bb, 10, ypos+6, face, fhash, - "Position: "..math.floor((load_percent*100)).."%".. - " "..cur_section, true) + "Position: "..load_percent.."%".." "..cur_section, true) ypos = ypos + 15 blitbuffer.progressBar(fb.bb, 10, ypos, width-20, 15, @@ -144,9 +150,9 @@ function CREReader:adjustCreReaderCommands() "jump to *10% of document", function(cr, keydef) print('jump to position: '.. - math.floor(cr.doc:GetFullHeight()*(keydef.keycode-KEY_1)/9).. - '/'..cr.doc:GetFullHeight()) - cr:goto(math.floor(cr.doc:GetFullHeight()*(keydef.keycode-KEY_1)/9)) + math.floor(cr.doc:getFullHeight()*(keydef.keycode-KEY_1)/9).. + '/'..cr.doc:getFullHeight()) + cr:goto(math.floor(cr.doc:getFullHeight()*(keydef.keycode-KEY_1)/9)) end ) end