2013-10-18 20:38:07 +00:00
|
|
|
local Widget = require("ui/widget/widget")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Screen = require("device").screen
|
2013-10-18 20:38:07 +00:00
|
|
|
local RenderText = require("ui/rendertext")
|
|
|
|
local Geom = require("ui/geometry")
|
2014-10-22 13:34:11 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
--[[
|
|
|
|
A TextWidget puts a string on a single line
|
|
|
|
--]]
|
|
|
|
local TextWidget = Widget:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
text = nil,
|
|
|
|
face = nil,
|
|
|
|
bold = nil,
|
2014-10-22 13:34:11 +00:00
|
|
|
fgcolor = Blitbuffer.COLOR_BLACK,
|
2014-03-13 13:52:43 +00:00
|
|
|
_bb = nil,
|
|
|
|
_length = 0,
|
|
|
|
_height = 0,
|
|
|
|
_maxlength = 1200,
|
2013-10-18 20:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
--function TextWidget:_render()
|
2014-03-13 13:52:43 +00:00
|
|
|
--local h = self.face.size * 1.3
|
|
|
|
--self._bb = Blitbuffer.new(self._maxlength, h)
|
2014-10-22 14:40:24 +00:00
|
|
|
--self._bb:fill(Blitbuffer.COLOR_WHITE)
|
2014-03-13 13:52:43 +00:00
|
|
|
--self._length = RenderText:renderUtf8Text(self._bb, 0, h*0.8, self.face, self.text, true, self.bold)
|
2013-10-18 20:38:07 +00:00
|
|
|
--end
|
|
|
|
|
|
|
|
function TextWidget:getSize()
|
2014-03-13 13:52:43 +00:00
|
|
|
--if not self._bb then
|
|
|
|
--self:_render()
|
|
|
|
--end
|
|
|
|
--return { w = self._length, h = self._bb:getHeight() }
|
|
|
|
local tsize = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, self.text, true, self.bold)
|
|
|
|
if not tsize then
|
|
|
|
return Geom:new{}
|
|
|
|
end
|
|
|
|
self._length = tsize.x
|
|
|
|
self._height = self.face.size * 1.5
|
|
|
|
return Geom:new{
|
|
|
|
w = self._length,
|
|
|
|
h = self._height,
|
|
|
|
}
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function TextWidget:paintTo(bb, x, y)
|
2014-03-13 13:52:43 +00:00
|
|
|
--if not self._bb then
|
|
|
|
--self:_render()
|
|
|
|
--end
|
|
|
|
--bb:blitFrom(self._bb, x, y, 0, 0, self._length, self._bb:getHeight())
|
|
|
|
--@TODO Don't use kerning for monospaced fonts. (houqp)
|
|
|
|
RenderText:renderUtf8Text(bb, x, y+self._height*0.7, self.face, self.text, true, self.bold,
|
2014-10-04 13:53:30 +00:00
|
|
|
self.fgcolor, self.width)
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function TextWidget:free()
|
2014-03-13 13:52:43 +00:00
|
|
|
if self._bb then
|
|
|
|
self._bb:free()
|
|
|
|
self._bb = nil
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return TextWidget
|