HtmlBoxWidget: fix incorrect selection (#13276)

In same cases MuPDF returns a visually single line of text as multiple lines. Merge such lines to ensure that getSelectedText works properly.

See: https://github.com/koreader/koreader/pull/13232#issuecomment-2658171531
This commit is contained in:
TnS-hun
2025-02-17 13:13:52 +01:00
committed by GitHub
parent b8a31ef182
commit dbc76eb410

View File

@@ -469,6 +469,29 @@ function HtmlBoxWidget:updateHighlight()
if self.page_boxes == nil then
local page = self.document:openPage(self.page_number)
self.page_boxes = page:getPageText()
-- In same cases MuPDF returns a visually single line of text as multiple lines.
-- Merge such lines to ensure that getSelectedText works properly.
local line_index = 2
while line_index <= #self.page_boxes do
local prev_line = self.page_boxes[line_index - 1]
local line = self.page_boxes[line_index]
if line.y0 == prev_line.y0 and line.y1 == prev_line.y1 then
if line.x0 < prev_line.x0 then
prev_line.x0 = line.x0
end
if line.x1 > prev_line.x1 then
prev_line.x1 = line.x1
end
for _, word in ipairs(line) do
table.insert(prev_line, word)
end
table.remove(self.page_boxes, line_index)
else
line_index = line_index + 1
end
end
page:close()
end