2
0
mirror of https://github.com/koreader/koreader synced 2024-11-18 03:25:46 +00:00
koreader/frontend/document/canvascontext.lua
NiLuJe fadee1f5dc
Clarify our OOP semantics across the codebase (#9586)
Basically:

* Use `extend` for class definitions
* Use `new` for object instantiations

That includes some minor code cleanups along the way:

* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
  * ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 02:14:48 +02:00

100 lines
3.2 KiB
Lua

--[[
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.
]]--
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
* isColorEnabled() -> boolean
]]--
function CanvasContext:init(device)
self.device = device
self.screen = device.screen
-- 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).
self.isAndroid = device.isAndroid
self.isDesktop = device.isDesktop
self.isEmulator = device.isEmulator
self.isKindle = device.isKindle
self.isPocketBook = device.isPocketBook
self.hasSystemFonts = device.hasSystemFonts
self:setColorRenderingEnabled(device.screen:isColorEnabled())
-- NOTE: At 32bpp, Kobo's fb is BGR, not RGB. Handle the conversion in MuPDF if needed.
if device:hasBGRFrameBuffer() then
self.is_bgr = true
Mupdf.bgr = true
end
-- 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.
local hasEinkScreen = device:hasEinkScreen()
self.hasEinkScreen = function() return hasEinkScreen end
self.canHWDither = device.canHWDither
self.fb_bpp = device.screen.fb_bpp
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
function CanvasContext:enableCPUCores(amount)
return self.device:enableCPUCores(amount)
end
return CanvasContext