export highlighted rect in scanned page to Evernote

pull/579/head
chrox 10 years ago
parent 71eda7a0ca
commit 55734f6aaa

@ -96,6 +96,14 @@ function DjvuDocument:getUsedBBox(pageno)
return used
end
function DjvuDocument:clipPagePNGFile(pos0, pos1, pboxes, drawer, filename)
return self.koptinterface:clipPagePNGFile(self, pos0, pos1, pboxes, drawer, filename)
end
function DjvuDocument:clipPagePNGString(pos0, pos1, pboxes, drawer)
return self.koptinterface:clipPagePNGString(self, pos0, pos1, pboxes, drawer)
end
function DjvuDocument:getPageBBox(pageno)
return self.koptinterface:getPageBBox(self, pageno)
end

@ -630,6 +630,54 @@ function KoptInterface:getOCRText(doc, pageno, tboxes)
DEBUG("Not implemented yet")
end
function KoptInterface:getClipPageContext(doc, pos0, pos1, pboxes, drawer)
assert(pos0.page == pos1.page)
assert(pos0.zoom == pos1.zoom)
local rect = nil
if pboxes and #pboxes > 0 then
local box = pboxes[1]
rect = Geom:new{
x = box.x, y = box.y,
w = box.w, h = box.h,
}
for _, box in ipairs(pboxes) do
rect = rect:combine(Geom:new(box))
end
else
local zoom = pos0.zoom or 1
rect = {
x = math.min(pos0.x, pos1.x)/zoom,
y = math.min(pos0.y, pos1.y)/zoom,
w = math.abs(pos0.x - pos1.x)/zoom,
h = math.abs(pos0.y - pos1.y)/zoom
}
end
local bbox = {
x0 = rect.x, y0 = rect.y,
x1 = rect.x + rect.w,
y1 = rect.y + rect.h
}
local kc = self:createContext(doc, pos0.page, bbox)
local page = doc._document:openPage(pos0.page)
page:getPagePix(kc)
page:close()
return kc, rect
end
function KoptInterface:clipPagePNGFile(doc, pos0, pos1, pboxes, drawer, filename)
local kc = self:getClipPageContext(doc, pos0, pos1, pboxes, drawer)
kc:exportSrcPNGFile(pboxes, drawer, filename)
kc:free()
end
function KoptInterface:clipPagePNGString(doc, pos0, pos1, pboxes, drawer)
local kc = self:getClipPageContext(doc, pos0, pos1, pboxes, drawer)
local png = kc:exportSrcPNGString(pboxes, drawer)
kc:free()
return png
end
--[[
get index of nearest word box around pos
--]]

@ -193,6 +193,14 @@ function PdfDocument:getLinkFromPosition(pageno, pos)
return self.koptinterface:getLinkFromPosition(self, pageno, pos)
end
function PdfDocument:clipPagePNGFile(pos0, pos1, pboxes, drawer, filename)
return self.koptinterface:clipPagePNGFile(self, pos0, pos1, pboxes, drawer, filename)
end
function PdfDocument:clipPagePNGString(pos0, pos1, pboxes, drawer)
return self.koptinterface:clipPagePNGString(self, pos0, pos1, pboxes, drawer)
end
function PdfDocument:getPageBBox(pageno)
return self.koptinterface:getPageBBox(self, pageno)
end

@ -1 +1 @@
Subproject commit ac9eea0e045cc6e0a6021850fe804ae1c09748a3
Subproject commit e94fe284a8d1bbbf3b09de2961b086073c0e0319

@ -1,3 +1,7 @@
local DocumentRegistry = require("document/documentregistry")
local DocSettings = require("docsettings")
local DEBUG = require("dbg")
require("MD5")
-- lfs
local MyClipping = {
@ -157,7 +161,7 @@ function MyClipping:getTime(line)
for k, v in pairs(months) do
if line:find(k) then
month = v
_, _, day = line:find(" (%d%d),")
_, _, day = line:find(" (%d?%d),")
_, _, year = line:find(" (%d%d%d%d)")
break
end
@ -206,7 +210,22 @@ function MyClipping:getText(line)
return line:match("^%s*(.-)%s*$") or ""
end
-- get PNG string and md5 hash
function MyClipping:getImage(image)
--DEBUG("image", image)
local doc = DocumentRegistry:openDocument(image.file)
if doc then
local png = doc:clipPagePNGString(image.pos0, image.pos1,
image.pboxes, image.drawer)
--doc:clipPagePNGFile(image.pos0, image.pos1,
--image.pboxes, image.drawer, "/tmp/"..md5(png)..".png")
doc:close()
if png then return { png = png, hash = md5(png) } end
end
end
function MyClipping:parseHighlight(highlights, book)
--DEBUG("book", book.file)
for page, items in pairs(highlights) do
for _, item in ipairs(items) do
local clipping = {}
@ -214,8 +233,17 @@ function MyClipping:parseHighlight(highlights, book)
clipping.sort = "highlight"
clipping.time = self:getTime(item.datetime or "")
clipping.text = self:getText(item.text)
if item.pos0 and item.pos1 and item.pos0.x and item.pos0.y
and item.pos1.x and item.pos1.y then
local image = {}
image.file = book.file
image.pos0, image.pos1 = item.pos0, item.pos1
image.pboxes = item.pboxes
image.drawer = item.drawer
clipping.image = self:getImage(image)
end
-- TODO: store chapter info when exporting highlights
if clipping.text and clipping.text ~= "" then
if clipping.text and clipping.text ~= "" or clipping.image then
table.insert(book, { clipping })
end
end
@ -232,7 +260,10 @@ function MyClipping:parseHistory()
if ok and stored.highlight then
local _, _, docname = path:find("%[.*%](.*)%.lua$")
local title, author = self:getTitle(docname)
local path = DocSettings:getPathFromHistory(f)
local name = DocSettings:getNameFromHistory(f)
clippings[title] = {
file = path .. "/" .. name,
title = title,
author = author,
}
@ -250,6 +281,7 @@ function MyClipping:parseCurrentDoc(view)
local _, _, docname = path:find(".*/(.*)")
local title, author = self:getTitle(docname)
clippings[title] = {
file = view.document.file,
title = title,
author = author,
}

@ -336,10 +336,22 @@ function EvernoteExporter:exportBooknotes(client, title, booknotes)
})
--DEBUG("content", content)
local note_guid = client:findNoteByTitle(title, self.notebook_guid)
local resources = {}
for _, chapter in ipairs(booknotes) do
for _, clipping in ipairs(chapter) do
if clipping.image then
table.insert(resources, {
image = clipping.image
})
-- nullify clipping image after passing it to evernote client
clipping.image = nil
end
end
end
if not note_guid then
client:createNote(title, content, {}, self.notebook_guid)
client:createNote(title, content, resources, {}, self.notebook_guid)
else
client:updateNote(note_guid, title, content, {}, self.notebook_guid)
client:updateNote(note_guid, title, content, resources, {}, self.notebook_guid)
end
end

@ -43,6 +43,9 @@
</div>
<div style="font-size:12pt">
<span>#{= htmlescape(clipping.text) }#</span>
#{ if clipping.image then }#
<en-media type="image/png" hash="#{= clipping.image.hash }#"/>
#{ end }#
</div>
#{ if clipping.note then }#
<div style="font-size:11pt; margin-top:0.2em;">

Loading…
Cancel
Save