From cb46108e4c4b14a9ce40c136be707f3d14220d4b Mon Sep 17 00:00:00 2001 From: kerivin Date: Mon, 18 May 2026 20:15:02 +0300 Subject: [PATCH] Isolate PDF SMask (#15401) Closes #7992. --- frontend/document/document.lua | 11 +++++++---- frontend/document/koptinterface.lua | 11 ++++++----- frontend/ui/data/koptoptions.lua | 14 ++++++++++++++ spec/unit/cache_spec.lua | 2 +- spec/unit/koptinterface_spec.lua | 2 +- spec/unit/pdf_bench.lua | 2 +- 6 files changed, 30 insertions(+), 12 deletions(-) diff --git a/frontend/document/document.lua b/frontend/document/document.lua index e97b40b34..57c8aa39f 100644 --- a/frontend/document/document.lua +++ b/frontend/document/document.lua @@ -392,15 +392,16 @@ end function Document:getFullPageHash(pageno, zoom, rotation, gamma) return "renderpg|"..self.file.."|"..self.mod_time.."|"..pageno.."|" - ..zoom.."|" - ..rotation.."|"..gamma.."|"..self.render_mode..(self.render_color and "|color" or "|bw") + ..zoom.."|"..rotation.."|"..gamma.."|"..self.configurable.background_cleanup.."|" + ..self.render_mode..(self.render_color and "|color" or "|bw") ..(self.reflowable_font_size and "|"..self.reflowable_font_size or "") end function Document:getPagePartHash(pageno, zoom, rotation, gamma, rect) return "renderpgpart|"..self.file.."|"..self.mod_time.."|"..pageno.."|" - ..tostring(rect).."|"..zoom.."|"..tostring(rect.scaled_rect).."|" - ..rotation.."|"..gamma.."|"..self.render_mode..(self.render_color and "|color" or "|bw") + ..tostring(rect).."|"..zoom.."|"..tostring(rect.scaled_rect) + .."|"..rotation.."|"..gamma.."|"..self.configurable.background_cleanup.."|" + ..self.render_mode..(self.render_color and "|color" or "|bw") ..(self.reflowable_font_size and "|"..self.reflowable_font_size or "") end @@ -502,6 +503,8 @@ function Document:renderPage(pageno, rect, zoom, rotation, gamma, hinting) dc:setGamma(gamma) end + dc:setIsolateSMask(self.configurable.background_cleanup) + -- And finally, render the page in our BB local page = self._document:openPage(pageno) page:draw(dc, tile.bb, size.x, size.y, self.render_mode) diff --git a/frontend/document/koptinterface.lua b/frontend/document/koptinterface.lua index 69beef781..443854a7e 100644 --- a/frontend/document/koptinterface.lua +++ b/frontend/document/koptinterface.lua @@ -103,6 +103,7 @@ function KoptInterface:setDefaultConfigurable(configurable) configurable.defect_size = G_defaults:readSetting("DKOPTREADER_CONFIG_DEFECT_SIZE") configurable.line_spacing = G_defaults:readSetting("DKOPTREADER_CONFIG_LINE_SPACING") configurable.word_spacing = G_defaults:readSetting("DKOPTREADER_CONFIG_DEFAULT_WORD_SPACING") + configurable.background_cleanup = 0 end function KoptInterface:waitForContext(kc) @@ -385,7 +386,7 @@ function KoptInterface:getCoverPageImage(doc) local native_size = Document.getNativePageDimensions(doc, 1) local canvas_size = CanvasContext:getSize() local zoom = math.min(canvas_size.w / native_size.w, canvas_size.h / native_size.h) - local tile = Document.renderPage(doc, 1, nil, zoom, 0, 1.0) + local tile = Document.renderPage(doc, 1, nil, zoom, 0, 1.0, false) if tile then return tile.bb:copy() end @@ -540,9 +541,9 @@ function KoptInterface:hintPage(doc, pageno, zoom, rotation, gamma) DocCache:memoryPressureCheck() if doc.configurable.text_wrap == 1 then - self:hintReflowedPage(doc, pageno, zoom, rotation, gamma, true) + self:hintReflowedPage(doc, pageno, zoom, rotation, true) elseif doc.configurable.page_opt == 1 or doc.configurable.auto_straighten > 0 then - self:renderOptimizedPage(doc, pageno, nil, zoom, rotation, gamma, true) + self:renderOptimizedPage(doc, pageno, nil, zoom, rotation, true) else Document.hintPage(doc, pageno, zoom, rotation, gamma) end @@ -557,7 +558,7 @@ off by calling self:waitForContext(kctx) Inherited from common document interface. --]] -function KoptInterface:hintReflowedPage(doc, pageno, zoom, rotation, gamma, hinting) +function KoptInterface:hintReflowedPage(doc, pageno, zoom, rotation, hinting) local bbox = doc:getPageBBox(pageno) local hash_list = { "kctx" } self:getContextHash(doc, pageno, bbox, hash_list) @@ -596,7 +597,7 @@ Draw cached tile pixels into target blitbuffer. Inherited from common document interface. --]] function KoptInterface:drawContextPage(doc, target, x, y, rect, pageno, zoom, rotation, nightmode_invert) - local tile = self:renderPage(doc, pageno, rect, zoom, rotation, 1.0) + local tile = self:renderPage(doc, pageno, rect, zoom, rotation, 1.0, false) target:blitFrom(tile.bb, x, y, rect.x - tile.excerpt.x, diff --git a/frontend/ui/data/koptoptions.lua b/frontend/ui/data/koptoptions.lua index 783aa1088..87dbc85dd 100644 --- a/frontend/ui/data/koptoptions.lua +++ b/frontend/ui/data/koptoptions.lua @@ -515,6 +515,20 @@ Some of the other settings are only available when reflow mode is enabled.]]), name_text_hold_callback = optionsutil.showValues, help_text = _([[Remove watermarks from the rendered document. This can also be used to remove some gray background or to convert a grayscale or color document to black & white and get more contrast for easier reading.]]), + }, + { + name = "background_cleanup", + name_text = _("Background Cleanup"), + toggle = {C_("Background Cleanup", "off"), C_("Background Cleanup", "on")}, + values = {0, 1}, + default_value = 0, + args = {0, 1}, + show_func = function(configurable, document) + return document.is_pdf == true + end, + name_text_hold_callback = optionsutil.showValues, + help_text = _([[Render the essentials in black and white, ignoring extra data for better contrast and faster rendering. +Useful for Internet Archive PDF documents.]]), }, { name = "hw_dithering", diff --git a/spec/unit/cache_spec.lua b/spec/unit/cache_spec.lua index 2a7ca50b0..2021cd2bf 100644 --- a/spec/unit/cache_spec.lua +++ b/spec/unit/cache_spec.lua @@ -28,7 +28,7 @@ describe("Cache module", function() it("should deserialize blitbuffer", function() for pageno = 1, math.min(max_page, doc.info.number_of_pages) do - doc:hintPage(pageno, 1, 0, 1.0, 0) + doc:hintPage(pageno, 1, 0, 1.0) end DocCache:clear() end) diff --git a/spec/unit/koptinterface_spec.lua b/spec/unit/koptinterface_spec.lua index f9c5b853f..03c753087 100644 --- a/spec/unit/koptinterface_spec.lua +++ b/spec/unit/koptinterface_spec.lua @@ -60,7 +60,7 @@ describe("Koptinterface module", function() it("should hint reflowed page in background", function() doc.configurable.text_wrap = 1 - Koptinterface:hintReflowedPage(doc, 1, 1.0, 0, 1.0, 0) + Koptinterface:hintReflowedPage(doc, 1, 1.0, 0, false) -- and wait for reflowing to complete local kc = Koptinterface:getCachedContext(doc, 1) assert.truthy(kc) diff --git a/spec/unit/pdf_bench.lua b/spec/unit/pdf_bench.lua index a7356e4fa..91a09c84e 100644 --- a/spec/unit/pdf_bench.lua +++ b/spec/unit/pdf_bench.lua @@ -23,7 +23,7 @@ describe("PDF benchmark:", function() end for pageno = 1, math.min(9, doc.info.number_of_pages) do local secs, usecs = util.gettime() - assert.truthy(doc:renderPage(pageno, nil, 1, 0, 1.0)) + assert.truthy(doc:renderPage(pageno, nil, 1, 0, 1.0, false)) local nsecs, nusecs = util.gettime() local dur = nsecs - secs + (nusecs - usecs) / 1000000 logDuration(logfile, pageno, dur)