2019-02-24 05:34:15 +00:00
|
|
|
--[[
|
|
|
|
CanvasContext is introduced to abstract out screen hardware code from document
|
|
|
|
render module. This abstraction makes it possible to use core document module
|
|
|
|
in headless mode.
|
|
|
|
|
|
|
|
You can think of canvas as a virtual screen. It provides render related
|
|
|
|
settings like canvas dimension and DPI. User of document module need to
|
|
|
|
initialize CanvasContext with settings from the actual hardware screen before
|
|
|
|
calling renderPage/drawPage.
|
|
|
|
|
|
|
|
Note: CanvasContext is a singleton and it is not thread safe.
|
|
|
|
]]--
|
|
|
|
|
2019-02-18 16:01:00 +00:00
|
|
|
local Mupdf = require("ffi/mupdf")
|
|
|
|
|
|
|
|
local CanvasContext = {
|
|
|
|
is_color_rendering_enabled = false,
|
|
|
|
is_bgr = false,
|
|
|
|
}
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Initialize CanvasContext with settings from device.
|
|
|
|
|
|
|
|
The following key is required for a device object:
|
|
|
|
|
|
|
|
* hasBGRFrameBuffer: function() -> boolean
|
|
|
|
* screen: object with following methods:
|
|
|
|
* getWidth() -> int
|
|
|
|
* getHeight() -> int
|
|
|
|
* getDPI() -> int
|
|
|
|
* getSize() -> Rect
|
|
|
|
* scaleBySize(int) -> int
|
2019-04-23 07:21:23 +00:00
|
|
|
* isColorEnabled() -> boolean
|
2019-02-18 16:01:00 +00:00
|
|
|
]]--
|
|
|
|
function CanvasContext:init(device)
|
2022-01-16 17:36:11 +00:00
|
|
|
self.device = device
|
2019-02-18 16:01:00 +00:00
|
|
|
self.screen = device.screen
|
2022-09-14 01:49:50 +00:00
|
|
|
-- NOTE: These work because they don't actually require accessing the Device object itself,
|
|
|
|
-- as opposed to more dynamic methods like the Screen ones we handle properly later...
|
|
|
|
-- By which I mean when one naively calls CanvasContext:isKindle(), it calls
|
|
|
|
-- device.isKindle(CanvasContext), whereas when one calls Device:isKindle(), it calls
|
|
|
|
-- Device.isKindle(Device).
|
|
|
|
-- In the latter case, self is sane, but *NOT* in the former.
|
|
|
|
-- TL;DR: The methods assigned below must *never* access self.
|
|
|
|
-- (Or programmers would have to be careful to call them through CanvasContext as functions,
|
|
|
|
-- and not methods, which is clunky, error-prone, and unexpected).
|
2019-02-18 16:01:00 +00:00
|
|
|
self.isAndroid = device.isAndroid
|
2019-08-20 16:38:02 +00:00
|
|
|
self.isDesktop = device.isDesktop
|
2020-04-11 15:45:37 +00:00
|
|
|
self.isEmulator = device.isEmulator
|
2019-02-18 16:01:00 +00:00
|
|
|
self.isKindle = device.isKindle
|
2020-08-25 21:13:39 +00:00
|
|
|
self.isPocketBook = device.isPocketBook
|
2021-04-19 07:04:31 +00:00
|
|
|
self.hasSystemFonts = device.hasSystemFonts
|
2022-09-14 01:49:50 +00:00
|
|
|
self:setColorRenderingEnabled(device.screen:isColorEnabled())
|
2019-02-18 16:01:00 +00:00
|
|
|
|
2022-01-16 17:36:11 +00:00
|
|
|
-- NOTE: At 32bpp, Kobo's fb is BGR, not RGB. Handle the conversion in MuPDF if needed.
|
2019-02-18 16:01:00 +00:00
|
|
|
if device:hasBGRFrameBuffer() then
|
|
|
|
self.is_bgr = true
|
|
|
|
Mupdf.bgr = true
|
|
|
|
end
|
2022-02-05 18:49:13 +00:00
|
|
|
|
2022-08-08 09:42:56 +00:00
|
|
|
-- This one may be called by a subprocess, and would crash on Android when
|
|
|
|
-- calling android.isEink() which is only allowed from the main thread.
|
2022-09-14 01:49:50 +00:00
|
|
|
local hasEinkScreen = device:hasEinkScreen()
|
2022-08-08 09:42:56 +00:00
|
|
|
self.hasEinkScreen = function() return hasEinkScreen end
|
|
|
|
|
2022-02-05 18:49:13 +00:00
|
|
|
self.canHWDither = device.canHWDither
|
|
|
|
self.fb_bpp = device.screen.fb_bpp
|
2019-02-18 16:01:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function CanvasContext:setColorRenderingEnabled(val)
|
|
|
|
self.is_color_rendering_enabled = val
|
|
|
|
end
|
|
|
|
|
|
|
|
function CanvasContext:getWidth()
|
|
|
|
return self.screen:getWidth()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CanvasContext:getHeight()
|
|
|
|
return self.screen:getHeight()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CanvasContext:getDPI()
|
|
|
|
return self.screen:getDPI()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CanvasContext:getSize()
|
|
|
|
return self.screen:getSize()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CanvasContext:scaleBySize(px)
|
|
|
|
return self.screen:scaleBySize(px)
|
|
|
|
end
|
|
|
|
|
2022-01-16 17:36:11 +00:00
|
|
|
function CanvasContext:enableCPUCores(amount)
|
|
|
|
return self.device:enableCPUCores(amount)
|
|
|
|
end
|
|
|
|
|
2019-02-18 16:01:00 +00:00
|
|
|
return CanvasContext
|