2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/frontend/ui/widget/imagewidget.lua

57 lines
1.1 KiB
Lua
Raw Normal View History

2013-10-18 20:38:07 +00:00
local Widget = require("ui/widget/widget")
local Geom = require("ui/geometry")
-- Image
2013-03-12 17:18:53 +00:00
--[[
ImageWidget shows an image from a file
--]]
2013-10-18 20:38:07 +00:00
local ImageWidget = Widget:new{
2013-03-12 17:18:53 +00:00
file = nil,
invert = nil,
dim = nil,
hide = nil,
2013-03-12 17:18:53 +00:00
_bb = nil
}
function ImageWidget:_render()
local itype = string.lower(string.match(self.file, ".+%.([^.]+)") or "")
if itype == "jpeg" or itype == "jpg" then
self._bb = Image:fromJPEG(self.file)
2013-03-12 17:18:53 +00:00
elseif itype == "png" then
self._bb = Image:fromPNG(self.file)
2013-03-12 17:18:53 +00:00
end
end
function ImageWidget:getSize()
if not self._bb then
self:_render()
end
return Geom:new{ w = self._bb:getWidth(), h = self._bb:getHeight() }
end
function ImageWidget:paintTo(bb, x, y)
local size = self:getSize()
2013-07-23 08:47:39 +00:00
self.dimen = Geom:new{
x = x, y = y,
w = size.w,
h = size.h
}
if self.hide then return end
2013-03-12 17:18:53 +00:00
bb:blitFrom(self._bb, x, y, 0, 0, size.w, size.h)
if self.invert then
bb:invertRect(x, y, size.w, size.h)
end
if self.dim then
bb:dimRect(x, y, size.w, size.h)
end
2013-03-12 17:18:53 +00:00
end
function ImageWidget:free()
if self._bb then
self._bb:free()
self._bb = nil
end
end
2013-10-18 20:38:07 +00:00
return ImageWidget