@ -14,7 +14,9 @@ Example:
local Blitbuffer = require ( " ffi/blitbuffer " )
local Geom = require ( " ui/geometry " )
local Math = require ( " optmath " )
local RenderText = require ( " ui/rendertext " )
local Size = require ( " ui/size " )
local Widget = require ( " ui/widget/widget " )
local Screen = require ( " device " ) . screen
@ -23,10 +25,12 @@ local TextWidget = Widget:new{
face = nil ,
bold = nil ,
fgcolor = Blitbuffer.COLOR_BLACK ,
padding = Size.padding . small , -- should padding be function of face.size ?
max_width = nil ,
_bb = nil ,
_length = 0 ,
_height = 0 ,
_baseline_h = 0 ,
_maxlength = 1200 ,
}
@ -44,7 +48,19 @@ function TextWidget:updateSize()
else
self._length = math.ceil ( tsize.x )
end
self._height = math.ceil ( self.face . size * 1.5 )
-- Used to be:
-- self._height = math.ceil(self.face.size * 1.5)
-- self._baseline_h = self._height*0.7
-- But better compute baseline alignment from freetype font metrics
-- to get better vertical centering of text in box
-- (Freetype doc on this at https://www.freetype.org/freetype2/docs/tutorial/step2.html)
local face_height , face_ascender = self.face . ftface : getHeightAndAscender ( )
self._height = math.ceil ( face_height ) + 2 * self.padding
self._baseline_h = Math.round ( face_ascender ) + self.padding
-- With our UI fonts, this usually gives 0.72 to 0.74, so text is aligned
-- a bit lower than before with the hardcoded 0.7
-- require("logger").warn("1.5*face.size:", self.face.size * 1.5, "face_height:", face_height, "self._height:", self._height)
-- require("logger").warn("self._height ratio:", 1.0*self._baseline_h/self._height)
end
function TextWidget : getSize ( )
@ -70,7 +86,7 @@ function TextWidget:paintTo(bb, x, y)
--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 ,
RenderText : renderUtf8Text ( bb , x , y + self._ baseline_h , self.face , self.text , true , self.bold ,
self.fgcolor , self.max_width and self.max_width or self.width )
end