check position nullity in getTextFromBoxes

pull/351/head
chrox 11 years ago
parent 2f2d3553ff
commit 5bf56cbf4c

@ -532,29 +532,29 @@ end
--[[ --[[
get index of nearest word box around pos get index of nearest word box around pos
--]] --]]
local function getWordBoxIndices(boxes, pos) local function inside_box(box, pos)
local function inside_box(box) local x, y = pos.x, pos.y
local x, y = pos.x, pos.y if box.x0 <= x and box.y0 <= y and box.x1 >= x and box.y1 >= y then
if box.x0 <= x and box.y0 <= y and box.x1 >= x and box.y1 >= y then return true
return true
end
return false
end end
local function box_distance(i, j) return false
local wb = boxes[i][j] end
if inside_box(wb) then
return 0 local function box_distance(box, pos)
else if inside_box(box, pos) then
local x0, y0 = pos.x, pos.y return 0
local x1, y1 = (wb.x0 + wb.x1) / 2, (wb.y0 + wb.y1) / 2 else
return (x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1) local x0, y0 = pos.x, pos.y
end local x1, y1 = (box.x0 + box.x1) / 2, (box.y0 + box.y1) / 2
return (x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1)
end end
end
local function getWordBoxIndices(boxes, pos)
local m, n = 1, 1 local m, n = 1, 1
for i = 1, #boxes do for i = 1, #boxes do
for j = 1, #boxes[i] do for j = 1, #boxes[i] do
if box_distance(i, j) < box_distance(m, n) then if box_distance(boxes[i][j], pos) < box_distance(boxes[m][n], pos) then
m, n = i, j m, n = i, j
end end
end end
@ -586,6 +586,7 @@ end
get text and text boxes between pos0 and pos1 get text and text boxes between pos0 and pos1
--]] --]]
function KoptInterface:getTextFromBoxes(boxes, pos0, pos1) function KoptInterface:getTextFromBoxes(boxes, pos0, pos1)
if not pos0 or not pos1 then return {} end
local line_text = "" local line_text = ""
local line_boxes = {} local line_boxes = {}
local i_start, j_start = getWordBoxIndices(boxes, pos0) local i_start, j_start = getWordBoxIndices(boxes, pos0)
@ -706,12 +707,10 @@ function KoptInterface:nativeToReflowPosTransform(doc, pageno, pos)
local kctx_hash = "kctx|"..context_hash local kctx_hash = "kctx|"..context_hash
local cached = Cache:check(kctx_hash) local cached = Cache:check(kctx_hash)
local kc = self:waitForContext(cached.kctx) local kc = self:waitForContext(cached.kctx)
--kc:setDebug() local x, y = kc:nativeToReflowPosTransform(pos.x, pos.y)
--DEBUG("transform native pos", pos) if x ~= nil and y ~= nil then
local rpos = {} return {x=x, y=y}
rpos.x, rpos.y = kc:nativeToReflowPosTransform(pos.x, pos.y) end
--DEBUG("transformed reflowed pos", rpos)
return rpos
end end
--[[ --[[

Loading…
Cancel
Save