add bold property for text rendering

pull/486/head
chrox 10 years ago
parent bfff863861
commit 24ed02bbee

@ -61,22 +61,22 @@ local function utf8Chars(input)
return read_next_glyph, input, 1
end
function RenderText:getGlyph(face, charcode, bgcolor, fgcolor)
function RenderText:getGlyph(face, charcode, bold, bgcolor, fgcolor)
if bgcolor == nil then bgcolor = 0.0 end
if fgcolor == nil then fgcolor = 1.0 end
local hash = "glyph|"..face.hash.."|"..charcode.."|"..bgcolor.."|"..fgcolor
local hash = "glyph|"..face.hash.."|"..charcode.."|"..(bold and 1 or 0).."|"..bgcolor.."|"..fgcolor
local glyph = GlyphCache:check(hash)
if glyph then
-- cache hit
return glyph[1]
end
local rendered_glyph = face.ftface:renderGlyph(charcode, bgcolor, fgcolor)
local rendered_glyph = face.ftface:renderGlyph(charcode, bgcolor, fgcolor, bold)
if face.ftface:checkGlyph(charcode) == 0 then
for index, font in pairs(Font.fallbacks) do
-- rescale face size by DPI since it will be scaled in getFace again
local fb_face = Font:getFace(font, Screen:rescaleByDPI(face.size))
if fb_face.ftface:checkGlyph(charcode) ~= 0 then
rendered_glyph = fb_face.ftface:renderGlyph(charcode, bgcolor, fgcolor)
rendered_glyph = fb_face.ftface:renderGlyph(charcode, bgcolor, fgcolor, bold)
--DEBUG("fallback to font", font)
break
end
@ -92,13 +92,13 @@ function RenderText:getGlyph(face, charcode, bgcolor, fgcolor)
return rendered_glyph
end
function RenderText:getSubTextByWidth(text, face, width, kerning)
function RenderText:getSubTextByWidth(text, face, width, kerning, bold)
local pen_x = 0
local prevcharcode = 0
local char_list = {}
for _, charcode, uchar in utf8Chars(text) do
if pen_x < width then
local glyph = self:getGlyph(face, charcode)
local glyph = self:getGlyph(face, charcode, bold)
if kerning and prevcharcode then
local kern = face.ftface:getKerning(prevcharcode, charcode)
pen_x = pen_x + kern
@ -115,7 +115,7 @@ function RenderText:getSubTextByWidth(text, face, width, kerning)
return table.concat(char_list)
end
function RenderText:sizeUtf8Text(x, width, face, text, kerning)
function RenderText:sizeUtf8Text(x, width, face, text, kerning, bold)
if not text then
DEBUG("sizeUtf8Text called without text");
return
@ -129,7 +129,7 @@ function RenderText:sizeUtf8Text(x, width, face, text, kerning)
local prevcharcode = 0
for _, charcode, uchar in utf8Chars(text) do
if pen_x < (width - x) then
local glyph = self:getGlyph(face, charcode)
local glyph = self:getGlyph(face, charcode, bold)
if kerning and (prevcharcode ~= 0) then
pen_x = pen_x + (face.ftface):getKerning(prevcharcode, charcode)
end
@ -143,7 +143,7 @@ function RenderText:sizeUtf8Text(x, width, face, text, kerning)
return { x = pen_x, y_top = pen_y_top, y_bottom = pen_y_bottom}
end
function RenderText:renderUtf8Text(buffer, x, y, face, text, kerning, bgcolor, fgcolor, width)
function RenderText:renderUtf8Text(buffer, x, y, face, text, kerning, bold, bgcolor, fgcolor, width)
if not text then
DEBUG("renderUtf8Text called without text");
return 0
@ -159,7 +159,7 @@ function RenderText:renderUtf8Text(buffer, x, y, face, text, kerning, bgcolor, f
end
for _, charcode, uchar in utf8Chars(text) do
if pen_x < text_width then
local glyph = self:getGlyph(face, charcode, bgcolor, fgcolor)
local glyph = self:getGlyph(face, charcode, bold, bgcolor, fgcolor)
if kerning and (prevcharcode ~= 0) then
pen_x = pen_x + face.ftface:getKerning(prevcharcode, charcode)
end

@ -9,7 +9,7 @@ FixedTextWidget
local FixedTextWidget = TextWidget:new{}
function FixedTextWidget:getSize()
local tsize = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, self.text, true)
local tsize = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, self.text, true, self.bold)
if not tsize then
return Geom:new{}
end
@ -22,8 +22,8 @@ function FixedTextWidget:getSize()
end
function FixedTextWidget:paintTo(bb, x, y)
RenderText:renderUtf8Text(bb, x, y+self._height, self.face, self.text,
true, self.bgcolor, self.fgcolor)
RenderText:renderUtf8Text(bb, x, y+self._height, self.face, self.text, true, self.bold,
self.bgcolor, self.fgcolor)
end
return FixedTextWidget

@ -11,6 +11,7 @@ A TextWidget that handles long text wrapping
local TextBoxWidget = Widget:new{
text = nil,
face = nil,
bold = nil,
bgcolor = 0.0, -- [0.0, 1.0]
fgcolor = 1.0, -- [0.0, 1.0]
width = 400, -- in pixels
@ -115,7 +116,7 @@ function TextBoxWidget:_getVerticalList(alg)
for w in word:gsplit("%p+", true) do
local word_box = {}
word_box.word = w
word_box.width = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, w, true).x
word_box.width = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, w, true, self.bold).x
table.insert(h_list, word_box)
end
end
@ -215,8 +216,7 @@ function TextBoxWidget:_render(v_list)
for _,w in ipairs(l) do
--@TODO Don't use kerning for monospaced fonts. (houqp)
-- refert to cb25029dddc42693cc7aaefbe47e9bd3b7e1a750 in master tree
RenderText:renderUtf8Text(self._bb, pen_x, y, self.face, w.word, true,
self.bgcolor, self.fgcolor)
RenderText:renderUtf8Text(self._bb, pen_x, y, self.face, w.word, true, self.bold, self.bgcolor, self.fgcolor)
pen_x = pen_x + w.width
end
y = y + line_height_px + font_height

@ -9,6 +9,7 @@ A TextWidget puts a string on a single line
local TextWidget = Widget:new{
text = nil,
face = nil,
bold = nil,
bgcolor = 0.0, -- [0.0, 1.0]
fgcolor = 1.0, -- [0.0, 1.0]
_bb = nil,
@ -20,7 +21,7 @@ local TextWidget = Widget:new{
--function TextWidget:_render()
--local h = self.face.size * 1.3
--self._bb = Blitbuffer.new(self._maxlength, h)
--self._length = RenderText:renderUtf8Text(self._bb, 0, h*0.8, self.face, self.text, self.color)
--self._length = RenderText:renderUtf8Text(self._bb, 0, h*0.8, self.face, self.text, true, self.bold)
--end
function TextWidget:getSize()
@ -28,7 +29,7 @@ function TextWidget:getSize()
--self:_render()
--end
--return { w = self._length, h = self._bb:getHeight() }
local tsize = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, self.text, true)
local tsize = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, self.text, true, self.bold)
if not tsize then
return Geom:new{}
end
@ -46,8 +47,8 @@ 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.bgcolor, self.fgcolor, self.width)
RenderText:renderUtf8Text(bb, x, y+self._height*0.7, self.face, self.text, true, self.bold,
self.bgcolor, self.fgcolor, self.width)
end
function TextWidget:free()

@ -1 +1 @@
Subproject commit 04a517ad2734fb7357924f082f479c63ae4c4f49
Subproject commit f35cbdff906b265712afffc8831bafe5fbb21f3b
Loading…
Cancel
Save