From 0deae50039d414c5fbd179aec7096fd798b4ff37 Mon Sep 17 00:00:00 2001 From: Adam Buntain-Cooper Date: Fri, 9 Oct 2026 07:38:20 +0100 Subject: [PATCH] CreDocuments: save EPubCFI alongside xpointers (last_pos, annotations) EPubCFI is the per-EPUB-specs equivalent of our xpointers. Save the EPubCFI version of our xpointers in metadata.epub.lua so they can be used by plugins or external tools. They are not used internally. --- frontend/apps/filemanager/filemanagerutil.lua | 1 + .../apps/reader/modules/readerannotation.lua | 1 + .../apps/reader/modules/readerhighlight.lua | 6 +- .../apps/reader/modules/readerrolling.lua | 2 + frontend/document/credocument.lua | 17 ++++++ spec/unit/epubcfi_generation_spec.lua | 61 +++++++++++++++++++ spec/unit/readerhighlight_spec.lua | 19 ++++++ 7 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 spec/unit/epubcfi_generation_spec.lua diff --git a/frontend/apps/filemanager/filemanagerutil.lua b/frontend/apps/filemanager/filemanagerutil.lua index a4e2c7ff6..c71774fb2 100644 --- a/frontend/apps/filemanager/filemanagerutil.lua +++ b/frontend/apps/filemanager/filemanagerutil.lua @@ -88,6 +88,7 @@ function filemanagerutil.resetDocumentSettings(file) highlights_imported = true, last_page = true, last_xpointer = true, + last_epubcfi = true, } local file_abs_path = ffiUtil.realpath(file) if file_abs_path then diff --git a/frontend/apps/reader/modules/readerannotation.lua b/frontend/apps/reader/modules/readerannotation.lua index 06e538275..a53fe627b 100644 --- a/frontend/apps/reader/modules/readerannotation.lua +++ b/frontend/apps/reader/modules/readerannotation.lua @@ -78,6 +78,7 @@ function ReaderAnnotation:buildAnnotation(bm, highlights, init) page = bm.page, -- highlight location, xPointer or number (pdf) pos0 = bm.pos0, -- highlight start position, xPointer (== page) or table (pdf) pos1 = bm.pos1, -- highlight end position, xPointer or table (pdf) + epubcfi = bm.epubcfi, -- epubcfi, based on pos0/pos1 xPointers, not used by pdf pboxes = hl.pboxes, -- pdf pboxes, used only and changeable by addMarkupAnnotation ext = hl.ext, -- pdf multi-page highlight } diff --git a/frontend/apps/reader/modules/readerhighlight.lua b/frontend/apps/reader/modules/readerhighlight.lua index 6ecd125f1..882663e32 100644 --- a/frontend/apps/reader/modules/readerhighlight.lua +++ b/frontend/apps/reader/modules/readerhighlight.lua @@ -1115,6 +1115,7 @@ function ReaderHighlight:updateHighlightRolling(highlight, side, direction, move local new_beginning = highlight.pos0 local new_end = highlight.pos1 + highlight.epubcfi = self.document:getEPubCFIRangeFromXPointers(new_beginning, new_end) highlight.text = self.ui.document:getTextFromXPointers(new_beginning, new_end) if side == 0 then -- Ensure we show the page with the new beginning of highlight @@ -2298,7 +2299,6 @@ function ReaderHighlight:saveHighlight(extend_to_sentence) pg_or_xp = self.selected_text.pos0.page end local item = { - page = self.ui.paging and self.selected_text.pos0.page or self.selected_text.pos0, pos0 = self.selected_text.pos0, pos1 = self.selected_text.pos1, text = util.cleanupSelectedText(self.selected_text.text), @@ -2309,9 +2309,13 @@ function ReaderHighlight:saveHighlight(extend_to_sentence) chapter = self.ui.toc:getTocTitleByPage(pg_or_xp), } if self.ui.paging then + item.page = self.selected_text.pos0.page item.pboxes = self.selected_text.pboxes item.ext = self.selected_text.ext self:writePdfAnnotation("save", item) + else -- rolling + item.page = self.selected_text.pos0 + item.epubcfi = self.document:getEPubCFIRangeFromXPointers(item.pos0, item.pos1) end local index = self.ui.annotation:addItem(item) self.view.footer:maybeUpdateFooter() diff --git a/frontend/apps/reader/modules/readerrolling.lua b/frontend/apps/reader/modules/readerrolling.lua index 0fb68ff66..7805df7fa 100644 --- a/frontend/apps/reader/modules/readerrolling.lua +++ b/frontend/apps/reader/modules/readerrolling.lua @@ -287,6 +287,8 @@ function ReaderRolling:onCloseDocument() self.current_header_height = nil -- show unload progress bar at top + self.ui.doc_settings:saveSetting("last_epubcfi", self.ui.document:getEPubCFIFromXPointer(self.xpointer)) + local cache_file_path = self.ui.document:getCacheFilePath() -- nil if no cache file self.ui.doc_settings:saveSetting("cache_file_path", cache_file_path) if self.ui.document:hasCacheFile() then diff --git a/frontend/document/credocument.lua b/frontend/document/credocument.lua index e534b67a4..4348361bf 100644 --- a/frontend/document/credocument.lua +++ b/frontend/document/credocument.lua @@ -889,6 +889,22 @@ function CreDocument:getPageXPointer(page) return self._document:getPageXPointer(page) end +function CreDocument:getEPubCFI() + return self._document:getEPubCFI() +end + +function CreDocument:getPageEPubCFI(page) + return self._document:getPageEPubCFI(page) +end + +function CreDocument:getEPubCFIFromXPointer(xp) + return self._document:getEPubCFIFromXPointer(xp) +end + +function CreDocument:getEPubCFIRangeFromXPointers(start_xp, end_xp) + return self._document:getEPubCFIRangeFromXPointers(start_xp, end_xp) +end + function CreDocument:isXPointerInDocument(xp) return self._document:isXPointerInDocument(xp) end @@ -1932,6 +1948,7 @@ function CreDocument:setupCallCache() elseif name == "getScreenPositionFromXPointer" then cache_by_tag = true elseif name == "getNearestWordFromPosition" then cache_by_tag = true elseif name == "getXPointer" then cache_by_tag = true + elseif name == "getEPubCFI" then cache_by_tag = true elseif name == "isXPointerInCurrentPage" then cache_by_tag = true elseif name == "getPageMapCurrentPageLabel" then cache_by_tag = true elseif name == "getPageMapVisiblePageLabels" then cache_by_tag = true diff --git a/spec/unit/epubcfi_generation_spec.lua b/spec/unit/epubcfi_generation_spec.lua new file mode 100644 index 000000000..0fa67e7ba --- /dev/null +++ b/spec/unit/epubcfi_generation_spec.lua @@ -0,0 +1,61 @@ +describe("EPUB CFI generation", function() + local DocumentRegistry, UIManager, ReaderUI, Screen + local readerui, rolling + + setup(function() + require("commonrequire") + disable_plugins() + UIManager = require("ui/uimanager") + stub(UIManager, "getNthTopWidget") + UIManager.getNthTopWidget.returns({}) + DocumentRegistry = require("document/documentregistry") + ReaderUI = require("apps/reader/readerui") + Screen = require("device").screen + + local sample_epub = "spec/front/unit/data/juliet.epub" + readerui = ReaderUI:new{ + dimen = Screen:getSize(), + document = DocumentRegistry:openDocument(sample_epub), + } + rolling = readerui.rolling + end) + + teardown(function() + readerui:onClose() + end) + + it("should return a CFI that changes as the position changes", function() + local seen = {} + for _, page in ipairs({1, 10, 20, 40}) do + rolling:onGotoPage(page) + local cfi = readerui.document:getEPubCFI() + assert.is_string(cfi) + assert.is_truthy(cfi:match("^epubcfi%(.+%)$")) + assert.is_nil(seen[cfi], "CFI was stale (repeated) at page " .. page .. ": " .. cfi) + seen[cfi] = page + end + end) + + it("should agree between getEPubCFI and getEPubCFIFromXPointer", function() + for _, page in ipairs({1, 10, 20, 40}) do + rolling:onGotoPage(page) + local xp = readerui.document:getXPointer() + assert.are.equal(readerui.document:getEPubCFI(), + readerui.document:getEPubCFIFromXPointer(xp)) + end + end) + + it("should return nil for an xpointer absent from the document", function() + assert.is_nil(readerui.document:getEPubCFIFromXPointer("/body[1]/DocFragment[999]/body[1]")) + end) + + -- This must run last: onCloseDocument tears down scheduled tasks that + -- earlier tests rely on. + it("should save last_epubcfi consistent with xpointer on document close", function() + rolling:onGotoPage(20) + rolling:onCloseDocument() + assert.are.equal( + readerui.document:getEPubCFIFromXPointer(rolling.xpointer), + readerui.doc_settings:readSetting("last_epubcfi")) + end) +end) diff --git a/spec/unit/readerhighlight_spec.lua b/spec/unit/readerhighlight_spec.lua index c7e6fcff9..c6115d182 100644 --- a/spec/unit/readerhighlight_spec.lua +++ b/spec/unit/readerhighlight_spec.lua @@ -99,6 +99,12 @@ describe("Readerhighlight module", function() assert.spy(selection_spy).was_called() assert.Equals(1, #readerui.annotation.annotations) assert.Equals('thy', readerui.annotation.annotations[1].text) + assert.Equals("epubcfi(/6/10!/4/2/2[section_77306]/4/74/2,/3:28,/3:31)", + readerui.annotation.annotations[1].epubcfi) + assert.Equals("/body[1]/DocFragment[5]/body[1]/div[1]/div[1]/div[1]/blockquote[19]/p[1]/text()[2].28", + readerui.annotation.annotations[1].pos0) + assert.Equals("/body[1]/DocFragment[5]/body[1]/div[1]/div[1]/div[1]/blockquote[19]/p[1]/text()[2].31", + readerui.annotation.annotations[1].pos1) end) it("should highlight text", function() highlight_text("readerhighlight_epub_text.png", @@ -107,6 +113,12 @@ describe("Readerhighlight module", function() assert.spy(selection_spy).was_called() assert.Equals(1, #readerui.annotation.annotations) assert.Equals('Montagues.\nSAMPSON', readerui.annotation.annotations[1].text) + assert.Equals("epubcfi(/6/10!/4/2/2[section_77306]/4,/74/2/5:25,/76/2/1:7)", + readerui.annotation.annotations[1].epubcfi) + assert.Equals("/body[1]/DocFragment[5]/body[1]/div[1]/div[1]/div[1]/blockquote[19]/p[1]/text()[3].25", + readerui.annotation.annotations[1].pos0) + assert.Equals("/body[1]/DocFragment[5]/body[1]/div[1]/div[1]/div[1]/p[19]/strong[1]/text()[1].7", + readerui.annotation.annotations[1].pos1) end) it("should response on tap gesture", function() tap_highlight_text("readerhighlight_epub_tap.png", @@ -114,7 +126,13 @@ describe("Readerhighlight module", function() Geom:new{ x = 370, y = 314 }, Geom:new{ x = 190, y = 305 }) assert.spy(selection_spy).was_called() + assert.Equals("/body[1]/DocFragment[5]/body[1]/div[1]/div[1]/div[1]/p[20]/strong[1]/text()[1].0", + readerui.annotation.annotations[1].pos0) + assert.Equals("/body[1]/DocFragment[5]/body[1]/div[1]/div[1]/div[1]/blockquote[21]/p[1]/text()[1].27", + readerui.annotation.annotations[1].pos1) assert.Equals('GREGORY\nHow! turn thy back and run?', readerui.annotation.annotations[1].text) + assert.Equals("epubcfi(/6/10!/4/2/2[section_77306]/4,/80/2/1:0,/82/2/1:27)", + readerui.annotation.annotations[1].epubcfi) end) end) @@ -148,6 +166,7 @@ describe("Readerhighlight module", function() Geom:new{ x = 260, y = 70 }) assert.Equals(1, #readerui.annotation.annotations) assert.Equals('Penn', readerui.annotation.annotations[1].text) + assert.is_nil(readerui.annotation.annotations[1].epubcfi) end) it("should highlight text", function() highlight_text("readerhighlight_pdf_layer_text.png",