Update PDF annotations when changing bookmark text (#7411)

pull/7443/head
Toromtomtom 3 years ago committed by GitHub
parent dbb4bbbee4
commit 3706196bfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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()

@ -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

@ -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
--]]

@ -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)

Loading…
Cancel
Save