|
|
|
@ -175,12 +175,44 @@ function PdfDocument:getPageLinks(pageno)
|
|
|
|
|
return links
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
local function _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: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 quadpoints, n = _quadpointsFromPboxes(item.pboxes)
|
|
|
|
|
local page = self._document:openPage(pageno)
|
|
|
|
|
local annot_type = C.PDF_ANNOT_HIGHLIGHT
|
|
|
|
|
if item.drawer == "lighten" then
|
|
|
|
@ -194,12 +226,12 @@ function PdfDocument:saveHighlight(pageno, item)
|
|
|
|
|
page:close()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Document:deleteHighlight(pageno, item)
|
|
|
|
|
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 = self:_quadpointsFromPboxes(item.pboxes)
|
|
|
|
|
local quadpoints, n = _quadpointsFromPboxes(item.pboxes)
|
|
|
|
|
local page = self._document:openPage(pageno)
|
|
|
|
|
local annot = page:getMarkupAnnotation(quadpoints, n)
|
|
|
|
|
if annot ~= nil then
|
|
|
|
@ -213,7 +245,7 @@ function PdfDocument:updateHighlightContents(pageno, item, contents)
|
|
|
|
|
if can_write ~= true then return can_write end
|
|
|
|
|
|
|
|
|
|
self.is_edited = true
|
|
|
|
|
local quadpoints, n = self:_quadpointsFromPboxes(item.pboxes)
|
|
|
|
|
local quadpoints, n = _quadpointsFromPboxes(item.pboxes)
|
|
|
|
|
local page = self._document:openPage(pageno)
|
|
|
|
|
local annot = page:getMarkupAnnotation(quadpoints, n)
|
|
|
|
|
if annot ~= nil then
|
|
|
|
@ -222,38 +254,6 @@ function PdfDocument:updateHighlightContents(pageno, item, contents)
|
|
|
|
|
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)
|
|
|
|
|