kopt: color support

Keep colors when reflowing documents.
This commit is contained in:
Benoit Pierre
2024-07-14 22:00:31 +02:00
committed by Frans de Jonge
parent cb002f3d1f
commit 0c17941ffb
5 changed files with 14 additions and 36 deletions

View File

@@ -25,7 +25,6 @@ local function validDjvuFile(filename)
end
function DjvuDocument:init()
self:updateColorRendering()
local djvu = require("libs/libkoreader-djvu")
self.koptinterface = require("document/koptinterface")
self.koptinterface:setDefaultConfigurable(self.configurable)
@@ -38,6 +37,7 @@ function DjvuDocument:init()
if not ok then
error(self._document) -- will contain error message
end
self:updateColorRendering()
self.is_open = true
self.info.has_pages = true
self.info.configurable = true

View File

@@ -387,14 +387,6 @@ function Document:updateColorRendering()
end
end
function Document:preRenderPage()
return nil
end
function Document:postRenderPage()
return nil
end
function Document:getTileCacheValidity()
return self.tile_cache_validity_ts
end
@@ -407,15 +399,15 @@ function Document:resetTileCacheValidity()
self.tile_cache_validity_ts = os.time()
end
function Document:getFullPageHash(pageno, zoom, rotation, gamma, render_mode, color)
function Document:getFullPageHash(pageno, zoom, rotation, gamma, render_mode)
return "renderpg|"..self.file.."|"..self.mod_time.."|"..pageno.."|"
..zoom.."|"..rotation.."|"..gamma.."|"..render_mode..(color and "|color" or "")
..zoom.."|"..rotation.."|"..gamma.."|"..render_mode..(self.render_color and "|color" or "|bw")
..(self.reflowable_font_size and "|"..self.reflowable_font_size or "")
end
function Document:renderPage(pageno, rect, zoom, rotation, gamma, render_mode, hinting)
local hash_excerpt
local hash = self:getFullPageHash(pageno, zoom, rotation, gamma, render_mode, self.render_color)
local hash = self:getFullPageHash(pageno, zoom, rotation, gamma, render_mode)
local tile = DocCache:check(hash, TileCacheItem)
if not tile then
hash_excerpt = hash.."|"..tostring(rect)
@@ -435,7 +427,6 @@ function Document:renderPage(pageno, rect, zoom, rotation, gamma, render_mode, h
if hinting then
CanvasContext:enableCPUCores(2)
end
self:preRenderPage()
local page_size = self:getPageDimensions(pageno, zoom, rotation)
-- this will be the size we actually render
@@ -493,7 +484,6 @@ function Document:renderPage(pageno, rect, zoom, rotation, gamma, render_mode, h
page:close()
DocCache:insert(hash, tile)
self:postRenderPage()
if hinting then
CanvasContext:enableCPUCores(1)
end

View File

@@ -169,6 +169,7 @@ function KoptInterface:getContextHash(doc, pageno, bbox, hash_list)
local canvas_size = CanvasContext:getSize()
table.insert(hash_list, doc.file)
table.insert(hash_list, doc.mod_time)
table.insert(hash_list, doc.render_color and 'color' or 'bw')
table.insert(hash_list, pageno)
doc.configurable:hash(hash_list)
table.insert(hash_list, bbox.x0)

View File

@@ -21,12 +21,6 @@ local PdfDocument = Document:extend{
function PdfDocument:init()
if not pdf then pdf = require("ffi/mupdf") end
-- mupdf.color has to stay false for kopt to work correctly
-- and be accurate (including its job about showing highlight
-- boxes). We will turn it on and off in PdfDocument:preRenderPage()
-- and :postRenderPage() when mupdf is called without kopt involved.
pdf.color = false
self:updateColorRendering()
self.koptinterface = require("document/koptinterface")
self.koptinterface:setDefaultConfigurable(self.configurable)
local ok
@@ -34,6 +28,7 @@ function PdfDocument:init()
if not ok then
error(self._document) -- will contain error message
end
self:updateColorRendering()
self.is_reflowable = self._document:isDocumentReflowable()
self.reflowable_font_size = self:convertKoptToReflowableFontSize()
-- no-op on PDF
@@ -48,6 +43,13 @@ function PdfDocument:init()
end
end
function PdfDocument:updateColorRendering()
Document.updateColorRendering(self) -- will set self.render_color
if self._document then
self._document:setColorRendering(self.render_color)
end
end
function PdfDocument:layoutDocument(font_size)
if font_size then
self.reflowable_font_size = font_size
@@ -81,14 +83,6 @@ function PdfDocument:convertKoptToReflowableFontSize(font_size)
end
end
function PdfDocument:preRenderPage()
pdf.color = self.render_color
end
function PdfDocument:postRenderPage()
pdf.color = false
end
function PdfDocument:unlock(password)
if not self._document:authenticatePassword(password) then
return false

View File

@@ -84,18 +84,11 @@ function HtmlBoxWidget:_render()
if self.bb then
return
end
-- In pdfdocument.lua, color is activated only at the moment of
-- rendering and then immediately disabled, for safety with kopt.
-- We do the same here.
Mupdf.color = Screen:isColorEnabled()
local page = self.document:openPage(self.page_number)
self.document:setColorRendering(Screen:isColorEnabled())
local dc = DrawContext.new()
self.bb = page:draw_new(dc, self.dimen.w, self.dimen.h, 0, 0)
page:close()
Mupdf.color = false
end
function HtmlBoxWidget:getSize()