From 72d4c924a10e73d0e3da4d36afb599b028a5fefc Mon Sep 17 00:00:00 2001 From: anaxonda <15372684+anaxonda@users.noreply.github.com> Date: Wed, 8 Jul 2026 12:15:16 -0500 Subject: [PATCH] DjVu: preserve text-layer reading order in selection (#15298) Fixes #15286. --- frontend/document/djvudocument.lua | 43 +++++++--------- frontend/document/koptinterface.lua | 33 ++++++------ spec/unit/djvudocument_spec.lua | 78 +++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 40 deletions(-) create mode 100644 spec/unit/djvudocument_spec.lua diff --git a/frontend/document/djvudocument.lua b/frontend/document/djvudocument.lua index ccecf3db5..7dc853991 100644 --- a/frontend/document/djvudocument.lua +++ b/frontend/document/djvudocument.lua @@ -57,7 +57,6 @@ function DjvuDocument:comparePositions(pos1, pos2) end -- Performance is better pre-allocated than as table.sort(tbl, function() … end). -local function compareByX(a, b) return a.x0 < b.x0 end local function compareByYThenX(a, b) if a.y0 - b.y0 == 0 then return a.x0 < b.x0 end return a.y0 < b.y0 @@ -75,21 +74,6 @@ local function collectWords(node, words) return words end ---- X-only sort that tries to avoid sorting when already ordered. -local function sortWordsByX(words) - local n = #words - if n < 2 then return end - local prev = words[1].x0 - for i = 2, n do - local x = words[i].x0 - if prev > x then - table.sort(words, compareByX) - return - end - prev = x - end -end - --- Collect only direct word children (no recursion). local function collectDirectWords(node, words) for i = 1, #node do @@ -108,15 +92,18 @@ local function hasDirectWordChildren(node) return false end -local function groupWordsIntoLines(words) - if #words == 0 then return {} end - -- Sort by y (top to bottom), then x (left to right) - table.sort(words, compareByYThenX) - -- Estimate a dynamic threshold based on word heights +local function getLineGroupingThreshold(words) local sum_h = 0 - for i = 1, #words do sum_h = sum_h + math.floor((words[i].y1 - words[i].y0) + 0.5) end + for i = 1, #words do + sum_h = sum_h + math.floor((words[i].y1 - words[i].y0) + 0.5) + end local avg_h = sum_h / #words - local threshold = math.max(2, math.floor(avg_h * 0.5 + 0.5)) + return math.max(2, math.floor(avg_h * 0.5 + 0.5)) +end + +local function groupWordsIntoLinesInOrder(words) + if #words == 0 then return {} end + local threshold = getLineGroupingThreshold(words) local lines = {} local current = { words[1] } local current_y = (words[1].y0 + words[1].y1) / 2 @@ -137,6 +124,11 @@ local function groupWordsIntoLines(words) return lines end +local function groupWordsIntoLinesByGeometry(words) + table.sort(words, compareByYThenX) + return groupWordsIntoLinesInOrder(words) +end + local function computeBboxFromWords(words) local w0 = words[1] local minx, miny, maxx, maxy = w0.x0, w0.y0, w0.x1, w0.y1 @@ -167,7 +159,6 @@ function DjvuDocument:getPageTextBoxes(pageno) if node.line then local words = collectWords(node, {}) if #words > 0 then - sortWordsByX(words) setLineBbox(words) lines[#lines + 1] = words end @@ -177,7 +168,7 @@ function DjvuDocument:getPageTextBoxes(pageno) -- Only handle direct words here to avoid double-processing nested structures. local words = collectDirectWords(node, {}) if #words > 0 then - local groups = groupWordsIntoLines(words) + local groups = groupWordsIntoLinesInOrder(words) for i = 1, #groups do setLineBbox(groups[i]) lines[#lines + 1] = groups[i] @@ -204,7 +195,7 @@ function DjvuDocument:getPageTextBoxes(pageno) end -- No explicit line nodes: group all words heuristically by y. local all_words = collectWords(page_text, {}) - local grouped = groupWordsIntoLines(all_words) + local grouped = groupWordsIntoLinesByGeometry(all_words) for i = 1, #grouped do setLineBbox(grouped[i]) end return grouped end diff --git a/frontend/document/koptinterface.lua b/frontend/document/koptinterface.lua index cdae10252..d7f8ce9a4 100644 --- a/frontend/document/koptinterface.lua +++ b/frontend/document/koptinterface.lua @@ -1005,6 +1005,16 @@ local function getWordBoxIndices(boxes, pos) return m, n end +local function compareWordBoxIndices(i1, j1, i2, j2) + if i1 == i2 then + if j1 == j2 then + return 0 + end + return j1 < j2 and 1 or -1 + end + return i1 < i2 and 1 or -1 +end + --[[-- Get word and word box around `pos`. --]] @@ -1035,7 +1045,7 @@ function KoptInterface:getTextFromBoxes(boxes, pos0, pos1) local line_boxes = {} local i_start, j_start = getWordBoxIndices(boxes, pos0) local i_stop, j_stop = getWordBoxIndices(boxes, pos1) - if i_start == i_stop and j_start > j_stop or i_start > i_stop then + if compareWordBoxIndices(i_start, j_start, i_stop, j_stop) == -1 then i_start, i_stop = i_stop, i_start j_start, j_stop = j_stop, j_start end @@ -1078,12 +1088,12 @@ function KoptInterface:getTextFromBoxes(boxes, pos0, pos1) if prev_word:sub(-1, -1) == " " or word:sub(1, 1) == " " then -- Already a space between these words add_space = false - elseif dist_from_prev_word < box_height * 0.03 then + elseif dist_from_prev_word >= 0 and dist_from_prev_word < box_height * 0.03 then -- If the space between previous word box and this word box -- is smaller than 5% of box height, assume these boxes -- should be stuck add_space = false - elseif dist_from_prev_word < box_height * 0.8 then + elseif dist_from_prev_word >= 0 and dist_from_prev_word < box_height * 0.8 then local prev_word_end = prev_word:match(util.UTF8_CHAR_PATTERN.."$") local word_start = word:match(util.UTF8_CHAR_PATTERN) if util.isCJKChar(prev_word_end) and util.isCJKChar(word_start) then @@ -1460,18 +1470,13 @@ function KoptInterface:comparePositions(doc, ppos1, ppos2) elseif ppos1.page > ppos2.page then return -1 end - local box1 = self:getWordFromPosition(doc, ppos1).pbox - local box2 = self:getWordFromPosition(doc, ppos2).pbox - if box1.y == box2.y then - if box1.x == box2.x then - return 0 - elseif box1.x > box2.x then - return -1 - end - elseif box1.y > box2.y then - return -1 + local text_boxes = self:getTextBoxes(doc, ppos1.page) + if not text_boxes then + return 0 end - return 1 + local i1, j1 = getWordBoxIndices(text_boxes, ppos1) + local i2, j2 = getWordBoxIndices(text_boxes, ppos2) + return compareWordBoxIndices(i1, j1, i2, j2) end --[[-- diff --git a/spec/unit/djvudocument_spec.lua b/spec/unit/djvudocument_spec.lua new file mode 100644 index 000000000..b68d0f0b9 --- /dev/null +++ b/spec/unit/djvudocument_spec.lua @@ -0,0 +1,78 @@ +describe("DjVu document module", function() + local DjvuDocument + + setup(function() + require("commonrequire") + DjvuDocument = require("document/djvudocument") + end) + + local function get_words(lines) + local words = {} + for i = 1, #lines do + words[i] = {} + for j = 1, #lines[i] do + words[i][j] = lines[i][j].word + end + end + return words + end + + local function make_doc(page_text) + return { + _document = { + getPageText = function(_, pageno) + assert.is_equal(1, pageno) + return page_text + end, + }, + } + end + + it("should flatten nested line nodes", function() + local page_text = { + { + { + line = true, + { word = "Strategie", x0 = 50, y0 = 10, x1 = 90, y1 = 20 }, + { word = "del", x0 = 10, y0 = 10, x1 = 30, y1 = 20 }, + { word = "terrore", x0 = 100, y0 = 10, x1 = 150, y1 = 20 }, + }, + }, + } + + local lines = DjvuDocument.getPageTextBoxes(make_doc(page_text), 1) + + assert.are_same({ { "Strategie", "del", "terrore" } }, get_words(lines)) + end) + + it("should preserve source order for direct word children", function() + local page_text = { + { + word = false, + { word = "draw", x0 = 45, y0 = 10, x1 = 70, y1 = 20 }, + { word = "attention,", x0 = 10, y0 = 10, x1 = 40, y1 = 20 }, + { word = "in", x0 = 75, y0 = 10, x1 = 85, y1 = 20 }, + }, + } + + local lines = DjvuDocument.getPageTextBoxes(make_doc(page_text), 1) + + assert.are_same({ { "draw", "attention,", "in" } }, get_words(lines)) + end) + + it("should fall back to geometric grouping when only loose words exist", function() + local page_text = { + { word = "first", x0 = 40, y0 = 30, x1 = 60, y1 = 40 }, + { word = "line", x0 = 70, y0 = 30, x1 = 90, y1 = 40 }, + { word = "second", x0 = 10, y0 = 60, x1 = 40, y1 = 70 }, + { word = "line", x0 = 45, y0 = 60, x1 = 65, y1 = 70 }, + } + + local lines = DjvuDocument.getPageTextBoxes(make_doc(page_text), 1) + + assert.are_same({ + { "first", "line" }, + { "second", "line" }, + }, get_words(lines)) + end) +end)