Skip to content

Commit

Permalink
simplify framebuffer dpi handling (koreader#843)
Browse files Browse the repository at this point in the history
  • Loading branch information
houqp authored and Frenzie committed Apr 22, 2019
1 parent 55ed4be commit 4c2ed56
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 9 deletions.
4 changes: 0 additions & 4 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ std = "luajit"
-- ignore implicit self
self = false

globals = {
"G_reader_settings",
}

read_globals = {
"DLANDSCAPE_CLOCKWISE_ROTATION",
"lfs",
Expand Down
9 changes: 4 additions & 5 deletions ffi/framebuffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,12 @@ function fb:getScreenHeight()
return self.screen_size.h
end

local screen_dpi_override

function fb:getDPI()
if self.dpi ~= nil then return self.dpi end
if self.dpi ~= nil then
return self.dpi
end

self.dpi = EMULATE_READER_DPI or screen_dpi_override
self.dpi = EMULATE_READER_DPI

if self.dpi == nil and self.device then
self.dpi = self.device.display_dpi
Expand All @@ -273,7 +273,6 @@ function fb:getDPI()
end

function fb:setDPI(dpi)
screen_dpi_override = dpi
self.dpi = dpi
end

Expand Down
63 changes: 63 additions & 0 deletions ffi/framebuffer_dummy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
local BB = require("ffi/blitbuffer")

local framebuffer = {}

function framebuffer:init()
self.bb = BB.new(600, 800)
self.bb:fill(BB.COLOR_WHITE)

framebuffer.parent.init(self)
end

function framebuffer:resize(w, h)
end

function framebuffer:_newBB(w, h)
local rotation
local inverse

if self.bb then
rotation = self.bb:getRotation()
inverse = self.bb:getInverse() == 1
self.bb:free()
end
if self.invert_bb then self.invert_bb:free() end

-- we present this buffer to the outside
local bb = BB.new(w, h, BB.TYPE_BBRGB32)
local flash = os.getenv("EMULATE_READER_FLASH")
if flash then
-- in refresh emulation mode, we use a shadow blitbuffer
-- and blit refresh areas from it.
self.bb = BB.new(w, h, BB.TYPE_BBRGB32)
else
self.bb = bb
end
self.invert_bb = BB.new(w, h, BB.TYPE_BBRGB32)

if rotation then
self.bb:setRotation(rotation)
end

-- reinit inverse mode on resize
if inverse then
self.bb:invert()
end
end

function framebuffer:_render(bb, x, y, w, h)
end

function framebuffer:refreshFullImp(x, y, w, h)
end

function framebuffer:setWindowTitle(new_title)
end

function framebuffer:setWindowIcon(icon)
end

function framebuffer:close()
end

return require("ffi/framebuffer"):extend(framebuffer)
30 changes: 30 additions & 0 deletions spec/unit/framebuffer_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
describe("Framebuffer unit tests", function()
local fb

setup(function()
fb = require("ffi/framebuffer_dummy"):new{
dummy = true,
device = {
device_dpi = 167,
}
}
end)

it("should set & update DPI", function()
assert.are.equals(160, fb:getDPI())

fb:setDPI(120)
assert.are.equals(120, fb:getDPI())

fb:setDPI(60)
assert.are.equals(60, fb:getDPI())
end)

it("should scale by DPI", function()
fb:setDPI(167)
assert.are.equals(30, fb:scaleBySize(30))

fb:setDPI(167 * 3)
assert.are.equals(60, fb:scaleBySize(30))
end)
end)

0 comments on commit 4c2ed56

Please sign in to comment.