adapt widgets and text rendering to new font rendering

no background for text rendering anymore
pull/949/head
Hans-Werner Hilse 10 years ago
parent a713936d8c
commit 7b1777e095

@ -2,6 +2,7 @@ local Font = require("ui/font")
local Screen = require("ui/screen") local Screen = require("ui/screen")
local Cache = require("cache") local Cache = require("cache")
local CacheItem = require("cacheitem") local CacheItem = require("cacheitem")
local BlitBuffer = require("ffi/blitbuffer")
local DEBUG = require("dbg") local DEBUG = require("dbg")
--[[ --[[
@ -61,22 +62,20 @@ local function utf8Chars(input)
return read_next_glyph, input, 1 return read_next_glyph, input, 1
end end
function RenderText:getGlyph(face, charcode, bold, bgcolor, fgcolor) function RenderText:getGlyph(face, charcode, bold)
if bgcolor == nil then bgcolor = 0.0 end local hash = "glyph|"..face.hash.."|"..charcode.."|"..(bold and 1 or 0)
if fgcolor == nil then fgcolor = 1.0 end
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, bold) local rendered_glyph = face.ftface:renderGlyph(charcode, 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
-- use original size before scaling by screen DPI -- use original size before scaling by screen DPI
local fb_face = Font:getFace(font, face.orig_size) local fb_face = Font:getFace(font, face.orig_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, bold) rendered_glyph = fb_face.ftface:renderGlyph(charcode, bold)
--DEBUG("fallback to font", font) --DEBUG("fallback to font", font)
break break
end end
@ -143,12 +142,18 @@ function RenderText:sizeUtf8Text(x, width, face, text, kerning, bold)
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, bold, bgcolor, fgcolor, width) function RenderText:renderUtf8Text(buffer, x, y, face, text, kerning, bold, fgcolor, width)
if not text then if not text then
DEBUG("renderUtf8Text called without text"); DEBUG("renderUtf8Text called without text");
return 0 return 0
end end
if not fgcolor then
fgcolor = BlitBuffer.Color8(0xFF)
elseif type(fgcolor) == "number" then
fgcolor = BlitBuffer.Color8(fgcolor*0xFF)
end
-- may still need more adaptive pen placement when kerning, -- may still need more adaptive pen placement when kerning,
-- see: http://freetype.org/freetype2/docs/glyphs/glyphs-4.html -- see: http://freetype.org/freetype2/docs/glyphs/glyphs-4.html
local pen_x = 0 local pen_x = 0
@ -159,15 +164,16 @@ function RenderText:renderUtf8Text(buffer, x, y, face, text, kerning, bold, bgco
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, bold, bgcolor, fgcolor) 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
buffer:blitFrom( buffer:colorblitFrom(
glyph.bb, glyph.bb,
x + pen_x + glyph.l, y - glyph.t, x + pen_x + glyph.l, y - glyph.t,
0, 0, 0, 0,
glyph.bb:getWidth(), glyph.bb:getHeight()) glyph.bb:getWidth(), glyph.bb:getHeight(),
fgcolor)
pen_x = pen_x + glyph.ax pen_x = pen_x + glyph.ax
prevcharcode = charcode prevcharcode = charcode
end -- if pen_x < text_width end -- if pen_x < text_width

@ -34,7 +34,6 @@ function Button:init()
if self.text then if self.text then
self.label_widget = TextWidget:new{ self.label_widget = TextWidget:new{
text = self.text, text = self.text,
bgcolor = 0.0,
fgcolor = self.enabled and 1.0 or 0.5, fgcolor = self.enabled and 1.0 or 0.5,
bold = true, bold = true,
face = Font:getFace(self.text_font_face, self.text_font_size) face = Font:getFace(self.text_font_face, self.text_font_size)

@ -23,7 +23,7 @@ end
function FixedTextWidget:paintTo(bb, x, y) function FixedTextWidget:paintTo(bb, x, y)
RenderText:renderUtf8Text(bb, x, y+self._height, self.face, self.text, true, self.bold, RenderText:renderUtf8Text(bb, x, y+self._height, self.face, self.text, true, self.bold,
self.bgcolor, self.fgcolor) self.fgcolor)
end end
return FixedTextWidget return FixedTextWidget

@ -51,7 +51,7 @@ end
function InputText:initTextBox(text) function InputText:initTextBox(text)
self.text = text self.text = text
self:initCharlist(text) self:initCharlist(text)
local bgcolor, fgcolor = 0.0, self.text == "" and 0.5 or 1.0 local fgcolor = 0.0, self.text == "" and 0.5 or 1.0
local text_widget = nil local text_widget = nil
local show_text = self.text local show_text = self.text
@ -65,7 +65,6 @@ function InputText:initTextBox(text)
text_widget = ScrollTextWidget:new{ text_widget = ScrollTextWidget:new{
text = show_text, text = show_text,
face = self.face, face = self.face,
bgcolor = bgcolor,
fgcolor = fgcolor, fgcolor = fgcolor,
width = self.width, width = self.width,
height = self.height, height = self.height,
@ -74,7 +73,6 @@ function InputText:initTextBox(text)
text_widget = TextBoxWidget:new{ text_widget = TextBoxWidget:new{
text = show_text, text = show_text,
face = self.face, face = self.face,
bgcolor = bgcolor,
fgcolor = fgcolor, fgcolor = fgcolor,
width = self.width, width = self.width,
height = self.height, height = self.height,

@ -70,7 +70,6 @@ function ItemShortCutIcon:init()
TextWidget:new{ TextWidget:new{
text = self.key, text = self.key,
face = sc_face, face = sc_face,
bgcolor = background/15,
}, },
}, },
} }

@ -15,7 +15,6 @@ Text widget with vertical scroll bar
local ScrollTextWidget = InputContainer:new{ local ScrollTextWidget = InputContainer:new{
text = nil, text = nil,
face = nil, face = nil,
bgcolor = 0.0, -- [0.0, 1.0]
fgcolor = 1.0, -- [0.0, 1.0] fgcolor = 1.0, -- [0.0, 1.0]
width = 400, width = 400,
height = 20, height = 20,
@ -28,7 +27,6 @@ function ScrollTextWidget:init()
self.text_widget = TextBoxWidget:new{ self.text_widget = TextBoxWidget:new{
text = self.text, text = self.text,
face = self.face, face = self.face,
bgcolor = self.bgcolor,
fgcolor = self.fgcolor, fgcolor = self.fgcolor,
width = self.width - self.scroll_bar_width - self.text_scroll_span, width = self.width - self.scroll_bar_width - self.text_scroll_span,
height = self.height height = self.height

@ -13,7 +13,6 @@ local TextBoxWidget = Widget:new{
text = nil, text = nil,
face = nil, face = nil,
bold = nil, bold = nil,
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
height = nil, height = nil,
@ -217,7 +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, self.bold, self.bgcolor, self.fgcolor) RenderText:renderUtf8Text(self._bb, pen_x, y, self.face, w.word, true, self.bold, 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

@ -10,7 +10,6 @@ local TextWidget = Widget:new{
text = nil, text = nil,
face = nil, face = nil,
bold = nil, bold = nil,
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,
_length = 0, _length = 0,
@ -48,7 +47,7 @@ function TextWidget:paintTo(bb, x, y)
--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, true, self.bold, RenderText:renderUtf8Text(bb, x, y+self._height*0.7, self.face, self.text, true, self.bold,
self.bgcolor, self.fgcolor, self.width) self.fgcolor, self.width)
end end
function TextWidget:free() function TextWidget:free()

@ -20,13 +20,12 @@ local ToggleLabel = TextWidget:new{
} }
function ToggleLabel:paintTo(bb, x, y) function ToggleLabel:paintTo(bb, x, y)
RenderText:renderUtf8Text(bb, x, y+self._height*0.75, self.face, self.text, true, self.bold, self.bgcolor, self.fgcolor) RenderText:renderUtf8Text(bb, x, y+self._height*0.75, self.face, self.text, true, self.bold, self.fgcolor)
end end
local ToggleSwitch = InputContainer:new{ local ToggleSwitch = InputContainer:new{
width = Screen:scaleByDPI(216), width = Screen:scaleByDPI(216),
height = Screen:scaleByDPI(30), height = Screen:scaleByDPI(30),
bgcolor = 0, -- unfoused item color
fgcolor = 7, -- focused item color fgcolor = 7, -- focused item color
} }
@ -91,12 +90,10 @@ function ToggleSwitch:update()
if pos == i then if pos == i then
self.toggle_content[i].color = self.fgcolor self.toggle_content[i].color = self.fgcolor
self.toggle_content[i].background = self.fgcolor self.toggle_content[i].background = self.fgcolor
self.toggle_content[i][1][1].bgcolor = self.fgcolor/15
self.toggle_content[i][1][1].fgcolor = 0.0 self.toggle_content[i][1][1].fgcolor = 0.0
else else
self.toggle_content[i].color = self.bgcolor self.toggle_content[i].color = self.bgcolor
self.toggle_content[i].background = self.bgcolor self.toggle_content[i].background = self.bgcolor
self.toggle_content[i][1][1].bgcolor = 0.0
self.toggle_content[i][1][1].fgcolor = 1.0 self.toggle_content[i][1][1].fgcolor = 1.0
end end
end end

@ -80,7 +80,6 @@ function TouchMenuItem:init()
}, },
TextWidget:new{ TextWidget:new{
text = self.item.text or self.item.text_func(), text = self.item.text or self.item.text_func(),
bgcolor = 0.0,
fgcolor = item_enabled ~= false and 1.0 or 0.5, fgcolor = item_enabled ~= false and 1.0 or 0.5,
face = self.face, face = self.face,
}, },

Loading…
Cancel
Save