kopt: add fallbacks for cases where kctx is not in cache

There were a handful of cases where if there was no cached kctx there
was no fallback and several KoptInterface methods would return nil,
causing issues in various parts of KOReader (this happened with the
migration to selected_text everywhere but it's unclear how that change
caused this regression).

In any case, from a correctness perspective it makes sense to have the
corresponding fallback paths to create a new kctx if we couldn't find a
cached one.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
pull/8364/head
Aleksa Sarai 3 years ago committed by Frans de Jonge
parent b21029f1ac
commit 3ffb4c1692

@ -538,19 +538,22 @@ function KoptInterface:getReflowedTextBoxes(doc, pageno)
local hash = table.concat(hash_list, "|") local hash = table.concat(hash_list, "|")
local cached = DocCache:check(hash) local cached = DocCache:check(hash)
if not cached then if not cached then
local kc
local kctx_hash = hash:gsub("^rfpgboxes|", "kctx|") local kctx_hash = hash:gsub("^rfpgboxes|", "kctx|")
cached = DocCache:check(kctx_hash) cached = DocCache:check(kctx_hash)
if cached then if not cached then
local kc = self:waitForContext(cached.kctx) kc = self:getCachedContext(doc, pageno)
--kc:setDebug() else
local fullwidth, fullheight = kc:getPageDim() kc = self:waitForContext(cached.kctx)
local boxes, nr_word = kc:getReflowedWordBoxes("dst", 0, 0, fullwidth, fullheight) end
if not boxes then --kc:setDebug()
return nil local fullwidth, fullheight = kc:getPageDim()
end local boxes, nr_word = kc:getReflowedWordBoxes("dst", 0, 0, fullwidth, fullheight)
DocCache:insert(hash, CacheItem:new{ rfpgboxes = boxes, size = 192 * nr_word }) -- estimation if not boxes then
return boxes return
end end
DocCache:insert(hash, CacheItem:new{ rfpgboxes = boxes, size = 192 * nr_word }) -- estimation
return boxes
else else
return cached.rfpgboxes return cached.rfpgboxes
end end
@ -566,19 +569,27 @@ function KoptInterface:getNativeTextBoxes(doc, pageno)
local hash = table.concat(hash_list, "|") local hash = table.concat(hash_list, "|")
local cached = DocCache:check(hash) local cached = DocCache:check(hash)
if not cached then if not cached then
local kc
local kctx_hash = hash:gsub("^nativepgboxes|", "kctx|") local kctx_hash = hash:gsub("^nativepgboxes|", "kctx|")
cached = DocCache:check(kctx_hash) cached = DocCache:check(kctx_hash)
if cached then if not cached then
local kc = self:waitForContext(cached.kctx) kc = self:createContext(doc, pageno)
--kc:setDebug() DocCache:insert(kctx_hash, ContextCacheItem:new{
local fullwidth, fullheight = kc:getPageDim() persistent = true,
local boxes, nr_word = kc:getNativeWordBoxes("dst", 0, 0, fullwidth, fullheight) size = self.last_context_size or self.default_context_size,
if not boxes then kctx = kc,
return nil })
end else
DocCache:insert(hash, CacheItem:new{ nativepgboxes = boxes, size = 192 * nr_word }) -- estimation kc = self:waitForContext(cached.kctx)
return boxes end
--kc:setDebug()
local fullwidth, fullheight = kc:getPageDim()
local boxes, nr_word = kc:getNativeWordBoxes("dst", 0, 0, fullwidth, fullheight)
if not boxes then
return
end end
DocCache:insert(hash, CacheItem:new{ nativepgboxes = boxes, size = 192 * nr_word }) -- estimation
return boxes
else else
return cached.nativepgboxes return cached.nativepgboxes
end end
@ -596,21 +607,24 @@ function KoptInterface:getReflowedTextBoxesFromScratch(doc, pageno)
local hash = table.concat(hash_list, "|") local hash = table.concat(hash_list, "|")
local cached = DocCache:check(hash) local cached = DocCache:check(hash)
if not cached then if not cached then
local reflowed_kc
local kctx_hash = hash:gsub("^scratchrfpgboxes|", "kctx|") local kctx_hash = hash:gsub("^scratchrfpgboxes|", "kctx|")
cached = DocCache:check(kctx_hash) cached = DocCache:check(kctx_hash)
if cached then if not cached then
local reflowed_kc = self:waitForContext(cached.kctx) reflowed_kc = self:getCachedContext(doc, pageno)
local fullwidth, fullheight = reflowed_kc:getPageDim() else
local kc = self:createContext(doc, pageno) reflowed_kc = self:waitForContext(cached.kctx)
kc:copyDestBMP(reflowed_kc) end
local boxes, nr_word = kc:getNativeWordBoxes("dst", 0, 0, fullwidth, fullheight) local fullwidth, fullheight = reflowed_kc:getPageDim()
kc:free() local kc = self:createContext(doc, pageno)
if not boxes then kc:copyDestBMP(reflowed_kc)
return nil local boxes, nr_word = kc:getNativeWordBoxes("dst", 0, 0, fullwidth, fullheight)
end kc:free()
DocCache:insert(hash, CacheItem:new{ scratchrfpgboxes = boxes, size = 192 * nr_word }) -- estimation if not boxes then
return boxes return
end end
DocCache:insert(hash, CacheItem:new{ scratchrfpgboxes = boxes, size = 192 * nr_word }) -- estimation
return boxes
else else
return cached.scratchrfpgboxes return cached.scratchrfpgboxes
end end
@ -723,17 +737,20 @@ function KoptInterface:getReflewOCRWord(doc, pageno, rect)
local hash = table.concat(hash_list, "|") local hash = table.concat(hash_list, "|")
local cached = DocCache:check(hash) local cached = DocCache:check(hash)
if not cached then if not cached then
local kc
local kctx_hash = hash:gsub("^rfocrword|", "kctx|") local kctx_hash = hash:gsub("^rfocrword|", "kctx|")
cached = DocCache:check(kctx_hash) cached = DocCache:check(kctx_hash)
if cached then if not cached then
local kc = self:waitForContext(cached.kctx) kc = self:getCachedContext(doc, pageno)
local _, word = pcall( else
kc.getTOCRWord, kc, "dst", kc = self:waitForContext(cached.kctx)
rect.x, rect.y, rect.w, rect.h,
self.tessocr_data, self.ocr_lang, self.ocr_type, 0, 1)
DocCache:insert(hash, CacheItem:new{ rfocrword = word, size = #word + 64 }) -- estimation
return word
end end
local _, word = pcall(
kc.getTOCRWord, kc, "dst",
rect.x, rect.y, rect.w, rect.h,
self.tessocr_data, self.ocr_lang, self.ocr_type, 0, 1)
DocCache:insert(hash, CacheItem:new{ rfocrword = word, size = #word + 64 }) -- estimation
return word
else else
return cached.rfocrword return cached.rfocrword
end end

Loading…
Cancel
Save