From 2e5d2b99301b36ae3be7bfa100370f91dbc5c26c Mon Sep 17 00:00:00 2001 From: chrox Date: Sat, 13 Jul 2013 11:18:49 +0800 Subject: [PATCH] add semi-auto bbox option in page crop dialog --- frontend/document/djvudocument.lua | 2 +- frontend/document/koptinterface.lua | 29 +++++++++++++++++++++++++---- frontend/document/pdfdocument.lua | 5 +++++ frontend/ui/data/koptoptions.lua | 6 +++--- frontend/ui/data/strings.lua | 1 + 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/frontend/document/djvudocument.lua b/frontend/document/djvudocument.lua index c6b723e51..0dbd9b4a8 100644 --- a/frontend/document/djvudocument.lua +++ b/frontend/document/djvudocument.lua @@ -87,7 +87,7 @@ function DjvuDocument:invertTextYAxel(pageno, text_table) end function DjvuDocument:getPageBBox(pageno) - if self.configurable.text_wrap ~= 1 and self.configurable.trim_page == 1 then + if self.configurable.text_wrap ~= 1 and self.configurable.trim_page > 0 then return self.koptinterface:getAutoBBox(self, pageno) else return Document.getPageBBox(self, pageno) diff --git a/frontend/document/koptinterface.lua b/frontend/document/koptinterface.lua index 11397bd62..21ed28622 100644 --- a/frontend/document/koptinterface.lua +++ b/frontend/document/koptinterface.lua @@ -75,10 +75,31 @@ function KoptInterface:getContextHash(doc, pageno, bbox) end function KoptInterface:getAutoBBox(doc, pageno) + local bbox = { + x0 = 0, y0 = 0, + x1 = 0, y1 = 0, + } + local context_hash = self:getContextHash(doc, pageno, bbox) + local hash = "autobbox|"..context_hash + local cached = Cache:check(hash) + if not cached then + local page = doc._document:openPage(pageno) + local kc = self:createContext(doc, pageno, bbox) + bbox.x0, bbox.y0, bbox.x1, bbox.y1 = page:getAutoBBox(kc) + DEBUG("Auto detected bbox", bbox) + page:close() + Cache:insert(hash, CacheItem:new{ autobbox = bbox }) + return bbox + else + return cached.autobbox + end +end + +function KoptInterface:getSemiAutoBBox(doc, pageno) -- use manual bbox local bbox = Document.getPageBBox(doc, pageno) local context_hash = self:getContextHash(doc, pageno, bbox) - local hash = "autobbox|"..context_hash + local hash = "semiautobbox|"..context_hash local cached = Cache:check(hash) if not cached then local page = doc._document:openPage(pageno) @@ -89,12 +110,12 @@ function KoptInterface:getAutoBBox(doc, pageno) auto_bbox.y0 = auto_bbox.y0 + bbox.y0 auto_bbox.x1 = auto_bbox.x1 + bbox.x0 auto_bbox.y1 = auto_bbox.y1 + bbox.y0 - DEBUG("Auto detected bbox", auto_bbox) + DEBUG("Semi-auto detected bbox", auto_bbox) page:close() - Cache:insert(hash, CacheItem:new{ autobbox = auto_bbox }) + Cache:insert(hash, CacheItem:new{ semiautobbox = auto_bbox }) return auto_bbox else - return cached.autobbox + return cached.semiautobbox end end diff --git a/frontend/document/pdfdocument.lua b/frontend/document/pdfdocument.lua index d49949f98..dafa4309c 100644 --- a/frontend/document/pdfdocument.lua +++ b/frontend/document/pdfdocument.lua @@ -92,8 +92,13 @@ end function PdfDocument:getPageBBox(pageno) if self.configurable.text_wrap ~= 1 and self.configurable.trim_page == 1 then + -- auto bbox finding return self.koptinterface:getAutoBBox(self, pageno) + elseif self.configurable.text_wrap ~= 1 and self.configurable.trim_page == 2 then + -- semi-auto bbox finding + return self.koptinterface:getSemiAutoBBox(self, pageno) else + -- get saved manual bbox return Document.getPageBBox(self, pageno) end end diff --git a/frontend/ui/data/koptoptions.lua b/frontend/ui/data/koptoptions.lua index 7e723000c..8b8fe65e0 100644 --- a/frontend/ui/data/koptoptions.lua +++ b/frontend/ui/data/koptoptions.lua @@ -23,12 +23,12 @@ KoptOptions = { { name = "trim_page", name_text = PAGE_CROP_STR, - toggle = {AUTO_STR, MANUAL_STR}, + toggle = {MANUAL_STR, AUTO_STR, SEMIAUTO_STR}, alternate = false, - values = {1, 0}, + values = {0, 1, 2}, default_value = DKOPTREADER_CONFIG_TRIM_PAGE, event = "PageCrop", - args = {"auto", "manual"}, + args = {"manual", "auto", "semi-auto"}, } } }, diff --git a/frontend/ui/data/strings.lua b/frontend/ui/data/strings.lua index b8bbee1cd..9036ed419 100644 --- a/frontend/ui/data/strings.lua +++ b/frontend/ui/data/strings.lua @@ -25,6 +25,7 @@ ON_STR = _("on") OFF_STR = _("off") AUTO_STR = _("auto") MANUAL_STR = _("manual") +SEMIAUTO_STR = _("semi-auto") SMALL_STR = _("small") MEDIUM_STR = _("medium") LARGE_STR = _("large")