From b42b4ed2044ff4623867e84df1853c1ac9266d46 Mon Sep 17 00:00:00 2001 From: chrox Date: Sun, 2 Feb 2014 00:16:51 +0800 Subject: [PATCH] save highlight to pdf document --- frontend/document/document.lua | 4 ++ frontend/document/pdfdocument.lua | 73 ++++++++++++++++++++++++++ frontend/ui/reader/readerhighlight.lua | 11 +++- frontend/ui/reader/readerview.lua | 4 +- 4 files changed, 89 insertions(+), 3 deletions(-) diff --git a/frontend/document/document.lua b/frontend/document/document.lua index f5f4b27b9..e74ad5bc9 100644 --- a/frontend/document/document.lua +++ b/frontend/document/document.lua @@ -290,6 +290,10 @@ function Document:getPageText(pageno) return text end +function Document:saveHighlight(pageno, item) + return nil +end + --[[ helper functions --]] diff --git a/frontend/document/pdfdocument.lua b/frontend/document/pdfdocument.lua index 3ccc5d813..e7e9fa8df 100644 --- a/frontend/document/pdfdocument.lua +++ b/frontend/document/pdfdocument.lua @@ -4,6 +4,40 @@ local KoptOptions = require("ui/data/koptoptions") local Document = require("document/document") local Configurable = require("ui/reader/configurable") local DrawContext = require("ffi/drawcontext") +local ffi = require("ffi") +ffi.cdef[[ +typedef struct fz_point_s fz_point; +struct fz_point_s { + float x, y; +}; +typedef enum { + FZ_ANNOT_TEXT, + FZ_ANNOT_LINK, + FZ_ANNOT_FREETEXT, + FZ_ANNOT_LINE, + FZ_ANNOT_SQUARE, + FZ_ANNOT_CIRCLE, + FZ_ANNOT_POLYGON, + FZ_ANNOT_POLYLINE, + FZ_ANNOT_HIGHLIGHT, + FZ_ANNOT_UNDERLINE, + FZ_ANNOT_SQUIGGLY, + FZ_ANNOT_STRIKEOUT, + FZ_ANNOT_STAMP, + FZ_ANNOT_CARET, + FZ_ANNOT_INK, + FZ_ANNOT_POPUP, + FZ_ANNOT_FILEATTACHMENT, + FZ_ANNOT_SOUND, + FZ_ANNOT_MOVIE, + FZ_ANNOT_WIDGET, + FZ_ANNOT_SCREEN, + FZ_ANNOT_PRINTERMARK, + FZ_ANNOT_TRAPNET, + FZ_ANNOT_WATERMARK, + FZ_ANNOT_3D +} fz_annot_type; +]] local PdfDocument = Document:new{ _document = false, @@ -12,6 +46,7 @@ local PdfDocument = Document:new{ dc_null = DrawContext.new(), options = KoptOptions, koptinterface = nil, + annot_revision = 0, } function PdfDocument:init() @@ -112,6 +147,44 @@ function PdfDocument:getPageLinks(pageno) return links end +function PdfDocument:saveHighlight(pageno, item) + self.annot_revision = self.annot_revision + 1 + local n = #item.pboxes + local quadpoints = ffi.new("fz_point[?]", 4*n) + for i=1, n do + quadpoints[4*i-4].x = item.pboxes[i].x + item.pboxes[i].w + quadpoints[4*i-4].y = item.pboxes[i].y + item.pboxes[i].h + quadpoints[4*i-3].x = item.pboxes[i].x + quadpoints[4*i-3].y = item.pboxes[i].y + item.pboxes[i].h + quadpoints[4*i-2].x = item.pboxes[i].x + quadpoints[4*i-2].y = item.pboxes[i].y + quadpoints[4*i-1].x = item.pboxes[i].x + item.pboxes[i].w + quadpoints[4*i-1].y = item.pboxes[i].y + end + local page = self._document:openPage(pageno) + local annot_type = ffi.C.FZ_ANNOT_HIGHLIGHT + if item.drawer == "lighten" then + annot_type = ffi.C.FZ_ANNOT_HIGHLIGHT + elseif item.drawer == "underscore" then + annot_type = ffi.C.FZ_ANNOT_UNDERLINE + elseif item.drawer == "strikeout" then + annot_type = ffi.C.FZ_ANNOT_STRIKEOUT + end + page:addMarkupAnnotation(quadpoints, 4*n, annot_type) + page:close() +end + +function PdfDocument:writeDocument() + self._document:writeDocument(self.file) +end + +function PdfDocument:close() + if self.annot_revision ~= 0 then + self:writeDocument() + end + Document.close(self) +end + function PdfDocument:getLinkFromPosition(pageno, pos) return self.koptinterface:getLinkFromPosition(self, pageno, pos) end diff --git a/frontend/ui/reader/readerhighlight.lua b/frontend/ui/reader/readerhighlight.lua index f7b4dc505..d3ebc261d 100644 --- a/frontend/ui/reader/readerhighlight.lua +++ b/frontend/ui/reader/readerhighlight.lua @@ -349,18 +349,22 @@ function ReaderHighlight:saveHighlight() hl_item["text"] = self.selected_text.text hl_item["pos0"] = self.selected_text.pos0 hl_item["pos1"] = self.selected_text.pos1 + hl_item["pboxes"] = self.selected_text.pboxes hl_item["datetime"] = os.date("%Y-%m-%d %H:%M:%S") hl_item["drawer"] = self.view.highlight.saved_drawer table.insert(self.view.highlight.saved[page], hl_item) if self.selected_text.text ~= "" then self:exportToClippings(page, hl_item) end + if self.selected_text.pboxes then + self:exportToDocument(page, hl_item) + end end --DEBUG("saved hightlights", self.view.highlight.saved[page]) end function ReaderHighlight:exportToClippings(page, item) - DEBUG("export highlight", item) + DEBUG("export highlight to clippings", item) local clippings = io.open("/mnt/us/documents/My Clippings.txt", "a+") if clippings and item.text then local current_locale = os.setlocale() @@ -375,6 +379,11 @@ function ReaderHighlight:exportToClippings(page, item) end end +function ReaderHighlight:exportToDocument(page, item) + DEBUG("export highlight to document", item) + self.ui.document:saveHighlight(page, item) +end + function ReaderHighlight:addNote() DEBUG("add Note") end diff --git a/frontend/ui/reader/readerview.lua b/frontend/ui/reader/readerview.lua index 23865f2b6..6dc0eb9b9 100644 --- a/frontend/ui/reader/readerview.lua +++ b/frontend/ui/reader/readerview.lua @@ -369,7 +369,7 @@ function ReaderView:drawPageSavedHighlight(bb, x, y) end end -- end for each box end -- end if boxes - end -- end for each hightlight + end -- end for each highlight end -- end for each page end @@ -389,7 +389,7 @@ function ReaderView:drawXPointerSavedHighlight(bb, x, y) end end -- end for each box end -- end if boxes - end -- end for each hightlight + end -- end for each highlight end -- end for all saved highlight end