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

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

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

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

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