From a47ecfcdc215438083246bf6bbcfffeaa2d1ba03 Mon Sep 17 00:00:00 2001 From: Euphoriyy <96319862+Euphoriyy@users.noreply.github.com> Date: Sun, 8 Mar 2026 07:31:57 -0400 Subject: [PATCH] kopt: option to invert document in night mode (#14954) --- frontend/dispatcher.lua | 2 ++ frontend/document/document.lua | 9 +++++++ frontend/document/koptinterface.lua | 38 +++++++++++++++++++++++------ frontend/ui/data/koptoptions.lua | 10 ++++++++ 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/frontend/dispatcher.lua b/frontend/dispatcher.lua index 6d963ee25..507f397fa 100644 --- a/frontend/dispatcher.lua +++ b/frontend/dispatcher.lua @@ -299,6 +299,7 @@ local settingsList = { kopt_forced_ocr = {category="configurable", paging=true}, kopt_writing_direction = {category="configurable", paging=true}, kopt_defect_size = {category="string", paging=true}, -- not shown in the bottom menu + kopt_nightmode_document = {category="configurable", paging=true}, kopt_max_columns = {category="configurable", paging=true}, kopt_auto_straighten = {category="absolutenumber", paging=true}, @@ -551,6 +552,7 @@ local dispatcher_menu_order = { "kopt_forced_ocr", "kopt_writing_direction", "kopt_defect_size", + "kopt_nightmode_document", "kopt_max_columns", "kopt_auto_straighten", } diff --git a/frontend/document/document.lua b/frontend/document/document.lua index 30f4ede2c..e97b40b34 100644 --- a/frontend/document/document.lua +++ b/frontend/document/document.lua @@ -547,6 +547,15 @@ function Document:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma) end end +function Document:drawPageInverted(target, x, y, rect, pageno, zoom, rotation, gamma) + local tile = self:renderPage(pageno, rect, zoom, rotation, gamma) + target:invertblitFrom(tile.bb, + x, y, + rect.x - tile.excerpt.x, + rect.y - tile.excerpt.y, + rect.w, rect.h) +end + function Document:getDrawnImagesStatistics() -- For now, only set by CreDocument in CreDocument:drawCurrentView() -- Returns 0, 0 (as per Document:init) otherwise. diff --git a/frontend/document/koptinterface.lua b/frontend/document/koptinterface.lua index 7308ca7bf..bd68d7fd5 100644 --- a/frontend/document/koptinterface.lua +++ b/frontend/document/koptinterface.lua @@ -12,6 +12,7 @@ local FFIUtil = require("ffi/util") local Geom = require("ui/geometry") local KOPTContext = require("ffi/koptcontext") local Persist = require("persist") +local Screen = require("device").screen local TileCacheItem = require("document/tilecacheitem") local Utf8Proc = require("ffi/utf8proc") local logger = require("logger") @@ -577,10 +578,13 @@ function KoptInterface:hintReflowedPage(doc, pageno, zoom, rotation, gamma, hint end function KoptInterface:drawPage(doc, target, x, y, rect, pageno, zoom, rotation, gamma) + local nightmode_invert = doc.configurable.nightmode_document == 1 and Screen.night_mode if doc.configurable.text_wrap == 1 then - self:drawContextPage(doc, target, x, y, rect, pageno, zoom, rotation) + self:drawContextPage(doc, target, x, y, rect, pageno, zoom, rotation, nightmode_invert) elseif doc.configurable.page_opt == 1 or doc.configurable.auto_straighten > 0 then - self:drawContextPage(doc, target, x, y, rect, pageno, zoom, rotation) + self:drawContextPage(doc, target, x, y, rect, pageno, zoom, rotation, nightmode_invert) + elseif nightmode_invert then + Document.drawPageInverted(doc, target, x, y, rect, pageno, zoom, rotation, gamma) else Document.drawPage(doc, target, x, y, rect, pageno, zoom, rotation, gamma) end @@ -591,13 +595,31 @@ Draw cached tile pixels into target blitbuffer. Inherited from common document interface. --]] -function KoptInterface:drawContextPage(doc, target, x, y, rect, pageno, zoom, rotation) +function KoptInterface:drawContextPage(doc, target, x, y, rect, pageno, zoom, rotation, nightmode_invert) local tile = self:renderPage(doc, pageno, rect, zoom, rotation, 1.0) - target:blitFrom(tile.bb, - x, y, - rect.x - tile.excerpt.x, - rect.y - tile.excerpt.y, - rect.w, rect.h) + local offs_x = rect.x - tile.excerpt.x + local offs_y = rect.y - tile.excerpt.y + if nightmode_invert then + if doc.configurable.page_opt == 1 then + -- Dewatermark enabled: draw tile, then invert the drawn area + target:blitFrom(tile.bb, + x, y, + offs_x, offs_y, + rect.w, rect.h) + target:invertRect(x, y, rect.w, rect.h) + else + -- Dewatermark disabled: invert tile directly + target:invertblitFrom(tile.bb, + x, y, + offs_x, offs_y, + rect.w, rect.h) + end + else + target:blitFrom(tile.bb, + x, y, + offs_x, offs_y, + rect.w, rect.h) + end end --[[ diff --git a/frontend/ui/data/koptoptions.lua b/frontend/ui/data/koptoptions.lua index 2b5e9b93b..783aa1088 100644 --- a/frontend/ui/data/koptoptions.lua +++ b/frontend/ui/data/koptoptions.lua @@ -630,6 +630,16 @@ This can also be used to remove some gray background or to convert a grayscale o end, name_text_hold_callback = optionsutil.showValues, }, + { + name = "nightmode_document", + name_text = _("Invert Document"), + toggle = {_("off"), _("on")}, + values = {0, 1}, + default_value = 0, + show_func = function() return Device.screen.night_mode end, + name_text_hold_callback = optionsutil.showValues, + help_text = _([[Invert document in night mode. Useful for image-heavy documents such as comics or manga.]]), + }, { name = "max_columns", name_text = _("Document Columns"),