diff --git a/frontend/apps/reader/modules/readerbookmark.lua b/frontend/apps/reader/modules/readerbookmark.lua index 981e0ecc6..cce9a3ad8 100644 --- a/frontend/apps/reader/modules/readerbookmark.lua +++ b/frontend/apps/reader/modules/readerbookmark.lua @@ -597,9 +597,11 @@ end function ReaderBookmark:renameBookmark(item, from_highlight) if from_highlight then -- Called by ReaderHighlight:editHighlight, we need to find the bookmark + local pboxes = item.pboxes for i=1, #self.bookmarks do if item.datetime == self.bookmarks[i].datetime and item.page == self.bookmarks[i].page then item = self.bookmarks[i] + item.pboxes = pboxes if item.text == nil or item.text == "" then -- Make up bookmark text as done in onShowBookmark local page = item.page @@ -647,6 +649,10 @@ function ReaderBookmark:renameBookmark(item, from_highlight) if item.text == self.bookmarks[i].text and item.pos0 == self.bookmarks[i].pos0 and item.pos1 == self.bookmarks[i].pos1 and item.page == self.bookmarks[i].page then self.bookmarks[i].text = value + local setting = G_reader_settings:readSetting("save_document") + if setting ~= "disable" then + self.ui.document:updateHighlightContents(item.page, item, value) + end UIManager:close(self.input) if not from_highlight then self.refresh() diff --git a/frontend/apps/reader/modules/readerhighlight.lua b/frontend/apps/reader/modules/readerhighlight.lua index 4e95a4a36..c26b75b07 100644 --- a/frontend/apps/reader/modules/readerhighlight.lua +++ b/frontend/apps/reader/modules/readerhighlight.lua @@ -1442,6 +1442,11 @@ function ReaderHighlight:deleteHighlight(page, i, bookmark_item) datetime = removed.datetime, }) end + local setting = G_reader_settings:readSetting("save_document") + if setting ~= "disable" then + logger.dbg("delete highlight from document", removed) + self.ui.document:deleteHighlight(page, removed) + end end function ReaderHighlight:editHighlight(page, i) @@ -1449,6 +1454,7 @@ function ReaderHighlight:editHighlight(page, i) self.ui.bookmark:renameBookmark({ page = self.ui.document.info.has_pages and page or item.pos0, datetime = item.datetime, + pboxes = item.pboxes }, true) end diff --git a/frontend/document/document.lua b/frontend/document/document.lua index 371bf12f3..2008f96a7 100644 --- a/frontend/document/document.lua +++ b/frontend/document/document.lua @@ -483,6 +483,14 @@ function Document:saveHighlight(pageno, item) return nil end +function Document:deleteHighlight(pageno, item) + return nil +end + +function Document:updateHighlightContents(pageno, item, contents) + return nil +end + --[[ helper functions --]] diff --git a/frontend/document/pdfdocument.lua b/frontend/document/pdfdocument.lua index 1087bbeb4..481b38ba3 100644 --- a/frontend/document/pdfdocument.lua +++ b/frontend/document/pdfdocument.lua @@ -176,34 +176,11 @@ function PdfDocument:getPageLinks(pageno) end function PdfDocument:saveHighlight(pageno, item) - local suffix = util.getFileNameSuffix(self.file) - if string.lower(suffix) ~= "pdf" then return end + local can_write = self:_checkIfWritable() + if can_write ~= true then return can_write end - if self.is_writable == nil then - local handle = io.open(self.file, 'r+b') - self.is_writable = handle ~= nil - if handle then handle:close() end - end - if self.is_writable == false then - return false - end self.is_edited = true - -- will also need mupdf_h.lua to be evaluated once - -- but this is guaranteed at this point - local n = #item.pboxes - local quadpoints = ffi.new("float[?]", 8*n) - for i=1, n do - -- The order must be left bottom, right bottom, left top, right top. - -- https://bugs.ghostscript.com/show_bug.cgi?id=695130 - quadpoints[8*i-8] = item.pboxes[i].x - quadpoints[8*i-7] = item.pboxes[i].y + item.pboxes[i].h - quadpoints[8*i-6] = item.pboxes[i].x + item.pboxes[i].w - quadpoints[8*i-5] = item.pboxes[i].y + item.pboxes[i].h - quadpoints[8*i-4] = item.pboxes[i].x - quadpoints[8*i-3] = item.pboxes[i].y - quadpoints[8*i-2] = item.pboxes[i].x + item.pboxes[i].w - quadpoints[8*i-1] = item.pboxes[i].y - end + local quadpoints, n = self:_quadpointsFromPboxes(item.pboxes) local page = self._document:openPage(pageno) local annot_type = C.PDF_ANNOT_HIGHLIGHT if item.drawer == "lighten" then @@ -217,6 +194,66 @@ function PdfDocument:saveHighlight(pageno, item) page:close() end +function Document:deleteHighlight(pageno, item) + local can_write = self:_checkIfWritable() + if can_write ~= true then return can_write end + + self.is_edited = true + local quadpoints, n = self:_quadpointsFromPboxes(item.pboxes) + local page = self._document:openPage(pageno) + local annot = page:getMarkupAnnotation(quadpoints, n) + if annot ~= nil then + page:deleteMarkupAnnotation(annot) + end + page:close() +end + +function PdfDocument:updateHighlightContents(pageno, item, contents) + local can_write = self:_checkIfWritable() + if can_write ~= true then return can_write end + + self.is_edited = true + local quadpoints, n = self:_quadpointsFromPboxes(item.pboxes) + local page = self._document:openPage(pageno) + local annot = page:getMarkupAnnotation(quadpoints, n) + if annot ~= nil then + page:updateMarkupAnnotation(annot, contents) + end + page:close() +end + +-- returns nil if file is not a pdf, true if document is a writable pdf, false else +function PdfDocument:_checkIfWritable() + local suffix = util.getFileNameSuffix(self.file) + if string.lower(suffix) ~= "pdf" then return nil end + if self.is_writable == nil then + local handle = io.open(self.file, 'r+b') + self.is_writable = handle ~= nil + if handle then handle:close() end + end + return self.is_writable +end + +function PdfDocument:_quadpointsFromPboxes(pboxes) + -- will also need mupdf_h.lua to be evaluated once + -- but this is guaranteed at this point + local n = #pboxes + local quadpoints = ffi.new("float[?]", 8*n) + for i=1, n do + -- The order must be left bottom, right bottom, left top, right top. + -- https://bugs.ghostscript.com/show_bug.cgi?id=695130 + quadpoints[8*i-8] = pboxes[i].x + quadpoints[8*i-7] = pboxes[i].y + pboxes[i].h + quadpoints[8*i-6] = pboxes[i].x + pboxes[i].w + quadpoints[8*i-5] = pboxes[i].y + pboxes[i].h + quadpoints[8*i-4] = pboxes[i].x + quadpoints[8*i-3] = pboxes[i].y + quadpoints[8*i-2] = pboxes[i].x + pboxes[i].w + quadpoints[8*i-1] = pboxes[i].y + end + return quadpoints, n +end + function PdfDocument:writeDocument() logger.info("writing document to", self.file) self._document:writeDocument(self.file)