mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
adapt widgets and text rendering to new font rendering
no background for text rendering anymore
This commit is contained in:
@@ -2,6 +2,7 @@ local Font = require("ui/font")
|
||||
local Screen = require("ui/screen")
|
||||
local Cache = require("cache")
|
||||
local CacheItem = require("cacheitem")
|
||||
local BlitBuffer = require("ffi/blitbuffer")
|
||||
local DEBUG = require("dbg")
|
||||
|
||||
--[[
|
||||
@@ -61,22 +62,20 @@ local function utf8Chars(input)
|
||||
return read_next_glyph, input, 1
|
||||
end
|
||||
|
||||
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.."|"..(bold and 1 or 0).."|"..bgcolor.."|"..fgcolor
|
||||
function RenderText:getGlyph(face, charcode, bold)
|
||||
local hash = "glyph|"..face.hash.."|"..charcode.."|"..(bold and 1 or 0)
|
||||
local glyph = GlyphCache:check(hash)
|
||||
if glyph then
|
||||
-- cache hit
|
||||
return glyph[1]
|
||||
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
|
||||
for index, font in pairs(Font.fallbacks) do
|
||||
-- use original size before scaling by screen DPI
|
||||
local fb_face = Font:getFace(font, face.orig_size)
|
||||
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)
|
||||
break
|
||||
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}
|
||||
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
|
||||
DEBUG("renderUtf8Text called without text");
|
||||
return 0
|
||||
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,
|
||||
-- see: http://freetype.org/freetype2/docs/glyphs/glyphs-4.html
|
||||
local pen_x = 0
|
||||
@@ -159,15 +164,16 @@ function RenderText:renderUtf8Text(buffer, x, y, face, text, kerning, bold, bgco
|
||||
end
|
||||
for _, charcode, uchar in utf8Chars(text) do
|
||||
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
|
||||
pen_x = pen_x + face.ftface:getKerning(prevcharcode, charcode)
|
||||
end
|
||||
buffer:blitFrom(
|
||||
buffer:colorblitFrom(
|
||||
glyph.bb,
|
||||
x + pen_x + glyph.l, y - glyph.t,
|
||||
0, 0,
|
||||
glyph.bb:getWidth(), glyph.bb:getHeight())
|
||||
glyph.bb:getWidth(), glyph.bb:getHeight(),
|
||||
fgcolor)
|
||||
pen_x = pen_x + glyph.ax
|
||||
prevcharcode = charcode
|
||||
end -- if pen_x < text_width
|
||||
|
||||
@@ -34,7 +34,6 @@ function Button:init()
|
||||
if self.text then
|
||||
self.label_widget = TextWidget:new{
|
||||
text = self.text,
|
||||
bgcolor = 0.0,
|
||||
fgcolor = self.enabled and 1.0 or 0.5,
|
||||
bold = true,
|
||||
face = Font:getFace(self.text_font_face, self.text_font_size)
|
||||
|
||||
@@ -23,7 +23,7 @@ end
|
||||
|
||||
function FixedTextWidget:paintTo(bb, x, y)
|
||||
RenderText:renderUtf8Text(bb, x, y+self._height, self.face, self.text, true, self.bold,
|
||||
self.bgcolor, self.fgcolor)
|
||||
self.fgcolor)
|
||||
end
|
||||
|
||||
return FixedTextWidget
|
||||
|
||||
@@ -51,7 +51,7 @@ end
|
||||
function InputText:initTextBox(text)
|
||||
self.text = 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 show_text = self.text
|
||||
@@ -65,7 +65,6 @@ function InputText:initTextBox(text)
|
||||
text_widget = ScrollTextWidget:new{
|
||||
text = show_text,
|
||||
face = self.face,
|
||||
bgcolor = bgcolor,
|
||||
fgcolor = fgcolor,
|
||||
width = self.width,
|
||||
height = self.height,
|
||||
@@ -74,7 +73,6 @@ function InputText:initTextBox(text)
|
||||
text_widget = TextBoxWidget:new{
|
||||
text = show_text,
|
||||
face = self.face,
|
||||
bgcolor = bgcolor,
|
||||
fgcolor = fgcolor,
|
||||
width = self.width,
|
||||
height = self.height,
|
||||
|
||||
@@ -70,7 +70,6 @@ function ItemShortCutIcon:init()
|
||||
TextWidget:new{
|
||||
text = self.key,
|
||||
face = sc_face,
|
||||
bgcolor = background/15,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ Text widget with vertical scroll bar
|
||||
local ScrollTextWidget = InputContainer:new{
|
||||
text = nil,
|
||||
face = nil,
|
||||
bgcolor = 0.0, -- [0.0, 1.0]
|
||||
fgcolor = 1.0, -- [0.0, 1.0]
|
||||
width = 400,
|
||||
height = 20,
|
||||
@@ -28,7 +27,6 @@ function ScrollTextWidget:init()
|
||||
self.text_widget = TextBoxWidget:new{
|
||||
text = self.text,
|
||||
face = self.face,
|
||||
bgcolor = self.bgcolor,
|
||||
fgcolor = self.fgcolor,
|
||||
width = self.width - self.scroll_bar_width - self.text_scroll_span,
|
||||
height = self.height
|
||||
|
||||
@@ -13,7 +13,6 @@ 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
|
||||
height = nil,
|
||||
@@ -217,7 +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.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
|
||||
end
|
||||
y = y + line_height_px + font_height
|
||||
|
||||
@@ -10,7 +10,6 @@ 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,
|
||||
_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())
|
||||
--@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,
|
||||
self.bgcolor, self.fgcolor, self.width)
|
||||
self.fgcolor, self.width)
|
||||
end
|
||||
|
||||
function TextWidget:free()
|
||||
|
||||
@@ -20,13 +20,12 @@ local ToggleLabel = TextWidget:new{
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
local ToggleSwitch = InputContainer:new{
|
||||
width = Screen:scaleByDPI(216),
|
||||
height = Screen:scaleByDPI(30),
|
||||
bgcolor = 0, -- unfoused item color
|
||||
fgcolor = 7, -- focused item color
|
||||
}
|
||||
|
||||
@@ -91,12 +90,10 @@ function ToggleSwitch:update()
|
||||
if pos == i then
|
||||
self.toggle_content[i].color = 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
|
||||
else
|
||||
self.toggle_content[i].color = 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
|
||||
end
|
||||
end
|
||||
|
||||
@@ -80,7 +80,6 @@ function TouchMenuItem:init()
|
||||
},
|
||||
TextWidget:new{
|
||||
text = self.item.text or self.item.text_func(),
|
||||
bgcolor = 0.0,
|
||||
fgcolor = item_enabled ~= false and 1.0 or 0.5,
|
||||
face = self.face,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user