PDF: import embedded highlights (#15040)

This commit is contained in:
hius07 authored and GitHub committed 2026-03-01 20:51:11 +02:00
1 parent 7fdba6a997
commit 3f2cd054d8
3 files changed
+81 -1

No files matched your search

@@ -9,6 +9,7 @@ local DocSettings = require("docsettings")
local Event = require("ui/event")
local Geom = require("ui/geometry")
local GestureRange = require("ui/gesturerange")
local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer")
local InputDialog = require("ui/widget/inputdialog")
local LineWidget = require("ui/widget/linewidget")
@@ -995,6 +996,19 @@ function ReaderBookmark:onShowBookmark()
},
})
table.insert(buttons, {}) -- separator
if bookmark.document.is_pdf then
table.insert(buttons, {
{
text = _("Import embedded highlights"),
enabled = bookmark.document.configurable.text_wrap == 0,
callback = function()
UIManager:close(bm_dialog)
bookmark:importEmbeddedHighlights()
end,
},
})
end
table.insert(buttons, {}) -- separator
table.insert(buttons, {
{
text = _("Current page"),
@@ -1626,6 +1640,18 @@ function ReaderBookmark:filterByHighlightColor()
self.ui.highlight:showHighlightColorDialog(filter_by_color_callback)
end
function ReaderBookmark:importEmbeddedHighlights()
local boxes = self.document:getEmbeddedAnnotationsBoxes()
if boxes then
local count = self.ui.highlight:saveHighlightsFromBoxes(boxes)
self.bookmark_menu[1].close_callback()
self:onShowBookmark()
UIManager:show(InfoMessage:new{ text = T(N_("1 highlight added", "%1 highlights added", count), count) })
else
UIManager:show(InfoMessage:new{ text = _("No embedded highlights found") })
end
end
function ReaderBookmark:doesBookmarkMatchTable(item)
if self.match_table[item.type] then
if self.match_table.search_str then
@@ -1365,7 +1365,7 @@ function ReaderHighlight:showHighlightNoteOrDialog(index)
annotation.note = nil
self.ui:handleEvent(Event:new("AnnotationsModified",
{ annotation, nb_highlights_added = 1, nb_notes_added = -1 }))
self:writePdfAnnotation("content", annotation, nil)
self:writePdfAnnotation("content", annotation, "")
if self.view.highlight.note_mark then -- refresh note marker
UIManager:setDirty(self.dialog, "ui")
end
@@ -2628,6 +2628,50 @@ function ReaderHighlight:getSavedExtendedHighlightPage(highlight, page, index)
return item
end
-- Saves PDF embedded highlights as KOReader annotations
-- (For pdf documents only, reflow mode must be off)
function ReaderHighlight:saveHighlightsFromBoxes(boxes)
local highlight_write_into_pdf = self.highlight_write_into_pdf
self.highlight_write_into_pdf = nil -- prevent writing into PDF once more
local rotation = Screen:getRotationMode()
local zoom = self.ui.zooming:getZoom(1)
local note = _("embedded highlight")
local count = 0
for page, page_pboxes in pairs(boxes) do
for _, hl_pboxes in ipairs(page_pboxes) do
local first_box = hl_pboxes[1]
local pos0 = {
page = page,
rotation = rotation,
zoom = zoom,
x = first_box.x + 1, -- inside the box
y = first_box.y + 1, -- ditto
}
local last_box = hl_pboxes[#hl_pboxes]
local pos1 = {
page = page,
rotation = rotation,
zoom = zoom,
x = last_box.x + last_box.w - 2, -- ditto
y = last_box.y + last_box.h - 2, -- ditto
}
local text = self.document:getTextFromPositions(pos0, pos1)
self.selected_text = {
pos0 = pos0,
pos1 = pos1,
pboxes = hl_pboxes,
text = text and text.text or "",
note = note,
}
self:saveHighlight()
count = count + 1
end
end
self.highlight_write_into_pdf = highlight_write_into_pdf
self.selected_text = nil
return count
end
function ReaderHighlight:onReadSettings(config)
self.view.highlight.saved_drawer = config:readSetting("highlight_drawer")
or G_reader_settings:readSetting("highlight_drawing_style") or self.view.highlight.saved_drawer
+10
View File
@@ -293,6 +293,16 @@ function PdfDocument:updateHighlightContents(pageno, item, contents)
page:close()
end
function PdfDocument:getEmbeddedAnnotationsBoxes()
local boxes = {}
for pageno = 1, self.info.number_of_pages do
local page = self._document:openPage(pageno)
boxes[pageno] = page:getMarkupAnnotationBoxesFromPage()
page:close()
end
return next(boxes) and boxes
end
function PdfDocument:writeDocument()
logger.info("writing document to", self.file)
self._document:writeDocument(self.file)