From f3341d9dc017fd712ee735045596f2b7a2cf90d0 Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Mon, 29 Mar 2021 05:11:02 +0200 Subject: [PATCH] PdfDocument: Unbreak highlights (#7457) Regression since #7411 Fix #7456 --- frontend/document/documentregistry.lua | 1 - frontend/document/pdfdocument.lua | 96 +++++++++++++------------- 2 files changed, 48 insertions(+), 49 deletions(-) diff --git a/frontend/document/documentregistry.lua b/frontend/document/documentregistry.lua index ebfb66272..e66c6b81c 100644 --- a/frontend/document/documentregistry.lua +++ b/frontend/document/documentregistry.lua @@ -234,7 +234,6 @@ function DocumentRegistry:closeDocument(file) end -- load implementations: - require("document/credocument"):register(DocumentRegistry) require("document/pdfdocument"):register(DocumentRegistry) require("document/djvudocument"):register(DocumentRegistry) diff --git a/frontend/document/pdfdocument.lua b/frontend/document/pdfdocument.lua index 481b38ba3..d13746f4b 100644 --- a/frontend/document/pdfdocument.lua +++ b/frontend/document/pdfdocument.lua @@ -175,53 +175,6 @@ function PdfDocument:getPageLinks(pageno) return links end -function PdfDocument:saveHighlight(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_type = C.PDF_ANNOT_HIGHLIGHT - if item.drawer == "lighten" then - annot_type = C.PDF_ANNOT_HIGHLIGHT - elseif item.drawer == "underscore" then - annot_type = C.PDF_ANNOT_UNDERLINE - elseif item.drawer == "strikeout" then - annot_type = C.PDF_ANNOT_STRIKEOUT - end - page:addMarkupAnnotation(quadpoints, n, annot_type) - 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) @@ -234,7 +187,7 @@ function PdfDocument:_checkIfWritable() return self.is_writable end -function PdfDocument:_quadpointsFromPboxes(pboxes) +local function _quadpointsFromPboxes(pboxes) -- will also need mupdf_h.lua to be evaluated once -- but this is guaranteed at this point local n = #pboxes @@ -254,6 +207,53 @@ function PdfDocument:_quadpointsFromPboxes(pboxes) return quadpoints, n end +function PdfDocument:saveHighlight(pageno, item) + local can_write = self:_checkIfWritable() + if can_write ~= true then return can_write end + + self.is_edited = true + local quadpoints, n = _quadpointsFromPboxes(item.pboxes) + local page = self._document:openPage(pageno) + local annot_type = C.PDF_ANNOT_HIGHLIGHT + if item.drawer == "lighten" then + annot_type = C.PDF_ANNOT_HIGHLIGHT + elseif item.drawer == "underscore" then + annot_type = C.PDF_ANNOT_UNDERLINE + elseif item.drawer == "strikeout" then + annot_type = C.PDF_ANNOT_STRIKEOUT + end + page:addMarkupAnnotation(quadpoints, n, annot_type) + page:close() +end + +function PdfDocument:deleteHighlight(pageno, item) + local can_write = self:_checkIfWritable() + if can_write ~= true then return can_write end + + self.is_edited = true + local quadpoints, n = _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 = _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 + function PdfDocument:writeDocument() logger.info("writing document to", self.file) self._document:writeDocument(self.file)