From 8ccb3ebd8b516025484b3b4cd09baf52d1b52f2f Mon Sep 17 00:00:00 2001 From: chrox Date: Tue, 30 Apr 2013 13:44:37 +0800 Subject: [PATCH 01/11] update gitignore file to ignore files in test directory --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index fd5871263..1073f850b 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ lua-* git-rev *.o tags +test/* emu From 4e1572d7d02bd5fbf1a1f76d750b49e96b13ea74 Mon Sep 17 00:00:00 2001 From: chrox Date: Tue, 30 Apr 2013 13:45:14 +0800 Subject: [PATCH 02/11] change cache size to 10 MB --- frontend/cache.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/cache.lua b/frontend/cache.lua index 02a7d4f77..2425b9102 100644 --- a/frontend/cache.lua +++ b/frontend/cache.lua @@ -20,7 +20,7 @@ A global LRU cache ]]-- Cache = { -- cache configuration: - max_memsize = 1024*1024*15, -- 15MB cache size + max_memsize = 1024*1024*10, -- 10MB cache size -- cache state: current_memsize = 0, -- associative cache From 5b3aedbea5297b473ea9b5a243c9c6e412480c43 Mon Sep 17 00:00:00 2001 From: chrox Date: Tue, 30 Apr 2013 13:47:30 +0800 Subject: [PATCH 03/11] save model information for later retrieval --- frontend/ui/device.lua | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/frontend/ui/device.lua b/frontend/ui/device.lua index 4d77e520f..8653f62f9 100644 --- a/frontend/ui/device.lua +++ b/frontend/ui/device.lua @@ -7,14 +7,15 @@ Device = { } function Device:getModel() + if self.model then return self.model end local std_out = io.popen("grep 'MX' /proc/cpuinfo | cut -d':' -f2 | awk {'print $2'}", "r") local cpu_mod = std_out:read() if not cpu_mod then local ret = os.execute("grep 'Hardware : Mario Platform' /proc/cpuinfo", "r") if ret ~= 0 then - return nil + self.model = nil else - return "KindleDXG" + self.model = "KindleDXG" end end if cpu_mod == "MX50" then @@ -25,20 +26,21 @@ function Device:getModel() -- another special file for KT is Neonode zForce touchscreen: -- /sys/devices/platform/zforce.0/ if pw_test_fd then - return "KindlePaperWhite" + self.model = "KindlePaperWhite" elseif kt_test_fd then - return "KindleTouch" + self.model = "KindleTouch" else - return "Kindle4" + self.model = "Kindle4" end elseif cpu_mod == "MX35" then -- check if we are running on Kindle 3 (additional volume input) - return "Kindle3" + self.model = "Kindle3" elseif cpu_mod == "MX3" then - return "Kindle2" + self.model = "Kindle2" else - return nil + self.model = nil end + return self.model end function Device:isKindle4() From ecb797a097d4a3399b31ebb920020a1fd169976d Mon Sep 17 00:00:00 2001 From: chrox Date: Tue, 30 Apr 2013 13:48:50 +0800 Subject: [PATCH 04/11] enlarge hightlight box in readerhightlight.lus --- frontend/ui/reader/readerview.lua | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/frontend/ui/reader/readerview.lua b/frontend/ui/reader/readerview.lua index d92d6c350..e98498c72 100644 --- a/frontend/ui/reader/readerview.lua +++ b/frontend/ui/reader/readerview.lua @@ -289,12 +289,7 @@ function ReaderView:drawScrollView(bb, x, y) end function ReaderView:drawHightlight(bb, x, y, rect) - -- slightly enlarge the highlight box - -- for better viewing experience - local x = rect.x - rect.h * 0.01 - local y = rect.y - rect.h * 0.01 - local w = rect.w + rect.h * 0.02 - local h = rect.h + rect.h * 0.02 + local x, y, w, h = rect.x, rect.y, rect.w, rect.h self.highlight.drawer = self.highlight.drawer or "underscore" if self.highlight.drawer == "underscore" then From 5b6e9a8e03bfab2fcad72c613b544d7f6ef53b55 Mon Sep 17 00:00:00 2001 From: chrox Date: Tue, 30 Apr 2013 13:51:10 +0800 Subject: [PATCH 05/11] OCR is almost stable, so enable it now --- frontend/ui/reader/readerhighlight.lua | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/frontend/ui/reader/readerhighlight.lua b/frontend/ui/reader/readerhighlight.lua index 9a068c025..b108b68a6 100644 --- a/frontend/ui/reader/readerhighlight.lua +++ b/frontend/ui/reader/readerhighlight.lua @@ -68,6 +68,10 @@ function ReaderHighlight:onHold(arg, ges) local screen_rect = self.view:pageToScreenTransform(self.pos.page, self.word_info.box) DEBUG("highlight word rect", screen_rect) if screen_rect then + screen_rect.x = screen_rect.x - screen_rect.h * 0.2 + screen_rect.y = screen_rect.y - screen_rect.h * 0.2 + screen_rect.w = screen_rect.w + screen_rect.h * 0.4 + screen_rect.h = screen_rect.h + screen_rect.h * 0.4 self.view.highlight.rect = screen_rect UIManager:setDirty(self.dialog, "partial") -- if we extracted text directly @@ -77,11 +81,11 @@ function ReaderHighlight:onHold(arg, ges) else UIManager:scheduleIn(0.1, function() local word_box = self.word_info.box - word_box.x = word_box.x - math.floor(word_box.h * 0.1) - word_box.y = word_box.y - math.floor(word_box.h * 0.1) - word_box.w = word_box.w + math.floor(word_box.h * 0.2) - word_box.h = word_box.h + math.floor(word_box.h * 0.2) - --local word = self.ui.document:getOCRWord(self.pos.page, word_box) + word_box.x = word_box.x - math.floor(word_box.h * 0.2) + word_box.y = word_box.y - math.floor(word_box.h * 0.4) + word_box.w = word_box.w + math.floor(word_box.h * 0.4) + word_box.h = word_box.h + math.floor(word_box.h * 0.6) + local word = self.ui.document:getOCRWord(self.pos.page, word_box) DEBUG("OCRed word:", word) self.ui:handleEvent(Event:new("LookupWord", word)) end) From cb873ee528c9f4c92ed1254c15bb18c23af658f5 Mon Sep 17 00:00:00 2001 From: chrox Date: Tue, 30 Apr 2013 13:52:10 +0800 Subject: [PATCH 06/11] init/free OCR engine when necessary --- frontend/document/koptinterface.lua | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/frontend/document/koptinterface.lua b/frontend/document/koptinterface.lua index 2b5cbaa45..6628bc476 100644 --- a/frontend/document/koptinterface.lua +++ b/frontend/document/koptinterface.lua @@ -7,7 +7,7 @@ require "ui/reader/readerconfig" KoptInterface = { tessocr_data = "data", ocr_lang = "eng", - ocr_type = 0, -- default, for more accuracy use 3 + ocr_type = 3, -- default 0, for more accuracy use 3 } ContextCacheItem = CacheItem:new{} @@ -19,6 +19,15 @@ function ContextCacheItem:onFree() end end +OCREngine = CacheItem:new{} + +function OCREngine:onFree() + if self.ocrengine.freeOCR then + DEBUG("free OCREngine", self.ocrengine) + self.ocrengine:freeOCR() + end +end + function KoptInterface:waitForContext(kc) -- if koptcontext is being processed in background thread -- the isPreCache will return 1. @@ -107,6 +116,11 @@ function KoptInterface:getPageText(doc, pageno) end function KoptInterface:getOCRWord(doc, pageno, rect) + local ocrengine = "ocrengine" + if not Cache:check(ocrengine) then + local dummy = KOPTContext.new() + Cache:insert(ocrengine, OCREngine:new{ ocrengine = dummy }) + end local bbox = doc:getPageBBox(pageno) local context_hash = self:getContextHash(doc, pageno, bbox) local hash = "ocrword|"..context_hash..rect.x..rect.y..rect.w..rect.h @@ -117,14 +131,10 @@ function KoptInterface:getOCRWord(doc, pageno, rect) if cached then local kc = self:waitForContext(cached.kctx) local fullwidth, fullheight = kc:getPageDim() - --os.execute("echo 3 > /proc/sys/vm/drop_caches") local ok, word = pcall( - kc.getOCRWord, kc, - self.tessocr_data, - self.ocr_lang, - self.ocr_type, - rect.x, rect.y, - rect.w, rect.h) + kc.getTOCRWord, kc, + rect.x, rect.y, rect.w, rect.h, + self.tessocr_data, self.ocr_lang, self.ocr_type, 0, 1) Cache:insert(hash, CacheItem:new{ ocrword = word }) return word end From a5a7cebd735a7127faa0b0bacb91a278f270709d Mon Sep 17 00:00:00 2001 From: chrox Date: Tue, 30 Apr 2013 18:43:39 +0800 Subject: [PATCH 07/11] dict windlow fit to landscape screen width --- frontend/ui/widget/dict.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/frontend/ui/widget/dict.lua b/frontend/ui/widget/dict.lua index 5716bdeda..1a8922ea5 100644 --- a/frontend/ui/widget/dict.lua +++ b/frontend/ui/widget/dict.lua @@ -11,7 +11,6 @@ DictQuickLookup = InputContainer:new{ title_face = Font:getFace("tfont", 20), content_face = Font:getFace("cfont", 18), - width = Screen:getWidth() - 100 } function DictQuickLookup:init() @@ -44,13 +43,13 @@ function DictQuickLookup:init() TextBoxWidget:new{ text = self.dict, face = self.title_face, - width = self.width, + width = Screen:getWidth() - 100, }, VerticalSpan:new{ width = 20 }, TextBoxWidget:new{ text = self.definition, face = self.content_face, - width = self.width, + width = Screen:getWidth() - 100, } } } From e560c20c6d6ee5680c83e72997d15eaf64ae9fba Mon Sep 17 00:00:00 2001 From: chrox Date: Tue, 30 Apr 2013 18:45:12 +0800 Subject: [PATCH 08/11] add word lookup with stardict --- frontend/ui/reader/readerdictionary.lua | 35 +++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/frontend/ui/reader/readerdictionary.lua b/frontend/ui/reader/readerdictionary.lua index 1470f044a..81292417c 100644 --- a/frontend/ui/reader/readerdictionary.lua +++ b/frontend/ui/reader/readerdictionary.lua @@ -7,33 +7,52 @@ function ReaderDictionary:init() local dev_mod = Device:getModel() if dev_mod == "KindlePaperWhite" or dev_mod == "KindleTouch" then require("liblipclua") - JSON = require("JSON") DEBUG("init lipc handler com.github.koreader.dictionary") self.lipc_handle = lipc.init("com.github.koreader.dictionary") end + JSON = require("JSON") end function ReaderDictionary:onLookupWord(word) + self:stardictLookup(word) +end + +function ReaderDictionary:nativeDictLookup(word) DEBUG("lookup word:", word) --self:quickLookup() - if self.lipc_handle and JSON and word then + if self.lipc_handle and word then self.lipc_handle:set_string_property( "com.github.koreader.kpvbooklet.dict", "lookup", word) local results_str = self.lipc_handle:get_string_property( "com.github.koreader.kpvbooklet.word", word) if results_str then - --DEBUG("def str:", word, definitions) + --DEBUG("result str:", word, results_str) + local ok, results_tab = pcall(JSON.decode, JSON, results_str) + --DEBUG("lookup result table:", word, results_tab) + if results_tab and results_tab[1] then + self:showDict(results_tab[1]) + end + end + end +end + +function ReaderDictionary:stardictLookup(word) + DEBUG("lookup word:", word) + if word then + local std_out = io.popen("./sdcv -nj "..'\"'..word..'\"', "r") + local results_str = std_out:read("*all") + if results_str then + --DEBUG("result str:", word, results_str) local ok, results_tab = pcall(JSON.decode, JSON, results_str) - DEBUG("lookup result table:", word, results_tab) - if results_tab[1] then - self:quickLookup(results_tab[1]) + --DEBUG("lookup result table:", word, results_tab) + if results_tab and results_tab[1] then + self:showDict(results_tab[1]) end end end - return true end -function ReaderDictionary:quickLookup(result) +function ReaderDictionary:showDict(result) -- UIManager:show(DictQuickLookup:new{ -- dict = "Oxford Dictionary of English", -- definition = "coordination n. [mass noun] 1 the organization of the different elements of a \ From e71aa93a971c19d8a0d893c5278d999484215b23 Mon Sep 17 00:00:00 2001 From: chrox Date: Tue, 30 Apr 2013 18:46:04 +0800 Subject: [PATCH 09/11] add sdcv in customupdate --- Makefile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index b87f4ff62..d19eb3589 100644 --- a/Makefile +++ b/Makefile @@ -25,10 +25,10 @@ XGETTEXT_BIN=$(KOREADER_MISC_TOOL)/gettext/lua_xgettext.py MO_DIR=i18n -all: $(KOR_BASE)/koreader-base $(KOR_BASE)/extr mo +all: $(KOR_BASE)/koreader-base $(KOR_BASE)/extr $(KOR_BASE)/sdcv mo -$(KOR_BASE)/koreader-base $(KOR_BASE)/extr: - make -C $(KOR_BASE) koreader-base extr +$(KOR_BASE)/koreader-base $(KOR_BASE)/extr $(KOR_BASE)/sdcv: + make -C $(KOR_BASE) koreader-base extr sdcv fetchthirdparty: git submodule init @@ -50,6 +50,7 @@ bootstrapemu: test -d $(EMU_DIR)/resources || (cd $(EMU_DIR) && ln -s ../resources ./) test -e $(EMU_DIR)/koreader-base || (cd $(EMU_DIR) && ln -s ../$(KOR_BASE)/koreader-base ./) test -e $(EMU_DIR)/extr || (cd $(EMU_DIR) && ln -s ../$(KOR_BASE)/extr ./) + test -e $(EMU_DIR)/sdcv || (cd $(EMU_DIR) && ln -s ../$(KOR_BASE)/sdcv ./) test -e $(EMU_DIR)/reader.lua || (cd $(EMU_DIR) && ln -s ../reader.lua ./) test -e $(EMU_DIR)/history || (mkdir $(EMU_DIR)/history) test -e $(EMU_DIR)/$(MO_DIR) || (cd $(EMU_DIR) && ln -s ../$(MO_DIR) ./) @@ -64,7 +65,7 @@ customupdate: all # create new dir for package mkdir -p $(INSTALL_DIR)/{history,screenshots,clipboard,libs} cp -p README.md COPYING $(KOR_BASE)/{koreader-base,extr} koreader.sh $(LUA_FILES) $(INSTALL_DIR) - $(STRIP) --strip-unneeded $(INSTALL_DIR)/koreader-base $(INSTALL_DIR)/extr + $(STRIP) --strip-unneeded $(INSTALL_DIR)/koreader-base $(INSTALL_DIR)/extr $(INSTALL_DIR)/sdcv mkdir $(INSTALL_DIR)/data cp -L koreader-base/$(DJVULIB) $(KOR_BASE)/$(CRELIB) \ $(KOR_BASE)/$(LUALIB) $(KOR_BASE)/$(K2PDFOPTLIB) \ From 594950c73dd8013451543102c1da41595b405e17 Mon Sep 17 00:00:00 2001 From: chrox Date: Tue, 30 Apr 2013 18:47:30 +0800 Subject: [PATCH 10/11] add env variable for stardict dictionary dir --- koreader.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/koreader.sh b/koreader.sh index faae48a71..8525fc67a 100755 --- a/koreader.sh +++ b/koreader.sh @@ -12,6 +12,9 @@ cd /mnt/us/koreader/ # export trained OCR data directory export TESSDATA_PREFIX="data" +# export dict directory +export STARDICT_DATA_DIR="data/dict" + # bind-mount system fonts if ! grep /mnt/us/koreader/fonts/host /proc/mounts; then mount -o bind /usr/java/lib/fonts /mnt/us/koreader/fonts/host From db56b52bed598eb86482bbee9743e9ecec9826d3 Mon Sep 17 00:00:00 2001 From: chrox Date: Tue, 30 Apr 2013 20:06:01 +0800 Subject: [PATCH 11/11] add missing sdcv --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d19eb3589..7dbde999c 100644 --- a/Makefile +++ b/Makefile @@ -64,7 +64,7 @@ customupdate: all rm -rf $(INSTALL_DIR) # create new dir for package mkdir -p $(INSTALL_DIR)/{history,screenshots,clipboard,libs} - cp -p README.md COPYING $(KOR_BASE)/{koreader-base,extr} koreader.sh $(LUA_FILES) $(INSTALL_DIR) + cp -p README.md COPYING $(KOR_BASE)/{koreader-base,extr,sdcv} koreader.sh $(LUA_FILES) $(INSTALL_DIR) $(STRIP) --strip-unneeded $(INSTALL_DIR)/koreader-base $(INSTALL_DIR)/extr $(INSTALL_DIR)/sdcv mkdir $(INSTALL_DIR)/data cp -L koreader-base/$(DJVULIB) $(KOR_BASE)/$(CRELIB) \