From 7bbc5b5ed205e54eb962cc8fe81bf23185d1b5a1 Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Mon, 19 Mar 2012 21:22:45 +0800 Subject: [PATCH] highlight with cursor demo --- djvureader.lua | 124 ++++++++++++++++++++++++++++++++++++++++++++++--- reader.lua | 1 + 2 files changed, 119 insertions(+), 6 deletions(-) diff --git a/djvureader.lua b/djvureader.lua index fed95555d..755727d6a 100644 --- a/djvureader.lua +++ b/djvureader.lua @@ -25,6 +25,16 @@ function DJVUReader:_isWordInScreenRange(w) -self.offset_y) end +------------------------------------------------ +-- @text text object returned from doc:getPageText() +-- @l0 start line +-- @w0 start word +-- @l1 end line +-- @w1 end word +-- +-- get words from the w0th word in l0th line +-- to w1th word in l1th line (not included). +------------------------------------------------ function DJVUReader:_genTextIter(text, l0, w0, l1, w1) local word_items = {} local count = 0 @@ -32,12 +42,21 @@ function DJVUReader:_genTextIter(text, l0, w0, l1, w1) local w = w0 local tmp_w1 = 0 + print(l0, w0, l1, w1) + + if l0 < 1 or w0 < 1 or l0 > l1 then + return function() return nil end + end + -- build item table while l <= l1 do local words = text[l].words if l == l1 then - tmp_w1 = w1 + tmp_w1 = w1 - 1 + if tmp_w1 == 0 then + break + end else tmp_w1 = #words end @@ -58,7 +77,7 @@ end function DJVUReader:_drawTextHighLight(text_iter) for i in text_iter do - fb.bb:paintRect( + fb.bb:invertRect( i.x0*self.globalzoom, self.offset_y+self.cur_full_height-(i.y1*self.globalzoom), (i.x1-i.x0)*self.globalzoom, @@ -69,9 +88,102 @@ end function DJVUReader:startHighLightMode() local t = self.doc:getPageText(self.pageno) - --self:_drawTextHighLight(self:_genTextIter(t, 1, 1, #t, #(t[#t].words))) - -- highlight the first line - self:_drawTextHighLight(self:_genTextIter(t, 1, 1, 1, #(t[1].words))) - fb:refresh(0) + local function _nextWord(t, cur_l, cur_w) + local new_l = cur_l + local new_w = cur_w + + if new_w >= #(t[new_l].words) then + -- already the last word, goto next line + new_l = new_l + 1 + if new_l > #t or #(t[new_l].words) == 0 then + return cur_l, cur_w + end + new_w = 1 + else + -- simply move to next word in the same line + new_w = new_w + 1 + end + + return new_l, new_w + end + + local function _prevWord(t, cur_l, cur_w) + local new_l = cur_l + local new_w = cur_w + + if new_w == 1 then + -- already the first word, goto previous line + new_l = new_l - 1 + if new_l == 0 or #(t[new_l].words) == 0 then + return cur_l, cur_w + end + new_w = #(t[new_l].words) + 1 + else + -- simply move to previous word in the same line + new_w = new_w - 1 + end + + return new_l, new_w + end + + local function _nextLine(t, cur_l, cur_w) + local new_l = cur_l + local new_w = cur_w + + if new_l >= #t then + return cur_l, cur_w + end + + new_l = new_l + 1 + new_w = math.min(new_w, #t[new_l].words+1) + + return new_l, new_w + end + + local function _prevLine(t, cur_l, cur_w) + local new_l = cur_l + local new_w = cur_w + + if new_l == 1 then + return cur_l, cur_w + end + + new_l = new_l - 1 + new_w = math.min(new_w, #t[new_l].words+1) + + return new_l, new_w + end + + + -- next to be marked word position + local cur_l = 1 + --local cur_w = #(t[1].words) + local cur_w = 1 + local new_l = 1 + local new_w = 1 + local iter + + while true do + local ev = input.waitForEvent() + ev.code = adjustKeyEvents(ev) + if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then + if ev.code == KEY_FW_RIGHT then + new_l, new_w = _nextWord(t, cur_l, cur_w) + iter = self:_genTextIter(t, cur_l, cur_w, new_l, new_w) + elseif ev.code == KEY_FW_LEFT then + new_l, new_w = _prevWord(t, cur_l, cur_w) + iter = self:_genTextIter(t, new_l, new_w, cur_l, cur_w) + elseif ev.code == KEY_FW_DOWN then + new_l, new_w = _nextLine(t, cur_l, cur_w) + iter = self:_genTextIter(t, cur_l, cur_w, new_l, new_w) + elseif ev.code == KEY_FW_UP then + new_l, new_w = _prevLine(t, cur_l, cur_w) + iter = self:_genTextIter(t, new_l, new_w, cur_l, cur_w) + end + self:_drawTextHighLight(iter) + fb:refresh(0) + cur_l, cur_w = new_l, new_w + end + end end diff --git a/reader.lua b/reader.lua index 9c18d3cee..4b582d9f9 100755 --- a/reader.lua +++ b/reader.lua @@ -140,6 +140,7 @@ if ARGV[optind] and lfs.attributes(ARGV[optind], "mode") == "directory" then else if file ~= nil then running = openFile(file) + print(file) else running = false end