Fix the kerning logic in text size/rendering.

1. Fix the functions sizeUtf8Text(), renderUtf8Text() and
renderUtf8TextWidth() to have a meaningful test whether to kern or not
(the current code is meaningless as it contains the `else` clause that is never
executed).
2. Whitespace changes to filechooser.lua
Tigran Aivazian 12 years ago
parent 379cba7b68
commit c7b4cf71f8

@ -61,11 +61,14 @@ end
function DrawTitle(text, lmargin, y, height, color, font_face) function DrawTitle(text, lmargin, y, height, color, font_face)
local r = 6 -- radius for round corners local r = 6 -- radius for round corners
color = 3 -- redefine to ignore the input for background color color = 3 -- redefine to ignore the input for background color
fb.bb:paintRect(1, 1, G_width-2, height - r, color) fb.bb:paintRect(1, 1, G_width-2, height - r, color)
blitbuffer.paintBorder(fb.bb, 1, height/2, G_width-2, height/2, height/2, color, r) blitbuffer.paintBorder(fb.bb, 1, height/2, G_width-2, height/2, height/2, color, r)
local t = BatteryLevel() .. os.date(" %H:%M") local t = BatteryLevel() .. os.date(" %H:%M")
r = sizeUtf8Text(0, G_width, font_face, t, true).x r = sizeUtf8Text(0, G_width, font_face, t, true).x
renderUtf8Text(fb.bb, G_width-r-lmargin, height-10, font_face, t, true) renderUtf8Text(fb.bb, G_width-r-lmargin, height-10, font_face, t, true)
r = G_width - r - 2 * lmargin - 10 -- let's leave small gap r = G_width - r - 2 * lmargin - 10 -- let's leave small gap
if sizeUtf8Text(0, G_width, font_face, text, true).x <= r then if sizeUtf8Text(0, G_width, font_face, text, true).x <= r then
renderUtf8Text(fb.bb, lmargin, height-10, font_face, text, true) renderUtf8Text(fb.bb, lmargin, height-10, font_face, text, true)

@ -2,11 +2,13 @@ glyphcache_max_memsize = 256*1024 -- 256kB glyphcache
glyphcache_current_memsize = 0 glyphcache_current_memsize = 0
glyphcache = {} glyphcache = {}
glyphcache_max_age = 4 glyphcache_max_age = 4
function glyphCacheClaim(size) function glyphCacheClaim(size)
if(size > glyphcache_max_memsize) then if(size > glyphcache_max_memsize) then
error("too much memory claimed") error("too much memory claimed")
return false return false
end end
while glyphcache_current_memsize + size > glyphcache_max_memsize do while glyphcache_current_memsize + size > glyphcache_max_memsize do
for k, _ in pairs(glyphcache) do for k, _ in pairs(glyphcache) do
if glyphcache[k].age > 0 then if glyphcache[k].age > 0 then
@ -19,11 +21,14 @@ function glyphCacheClaim(size)
end end
end end
end end
glyphcache_current_memsize = glyphcache_current_memsize + size glyphcache_current_memsize = glyphcache_current_memsize + size
return true return true
end end
function getGlyph(face, charcode) function getGlyph(face, charcode)
local hash = glyphCacheHash(face.hash, charcode) local hash = glyphCacheHash(face.hash, charcode)
if glyphcache[hash] == nil then if glyphcache[hash] == nil then
local glyph = face.ftface:renderGlyph(charcode) local glyph = face.ftface:renderGlyph(charcode)
local size = glyph.bb:getWidth() * glyph.bb:getHeight() / 2 + 32 local size = glyph.bb:getWidth() * glyph.bb:getHeight() / 2 + 32
@ -36,21 +41,25 @@ function getGlyph(face, charcode)
else else
glyphcache[hash].age = glyphcache_max_age glyphcache[hash].age = glyphcache_max_age
end end
return glyphcache[hash].glyph return glyphcache[hash].glyph
end end
function glyphCacheHash(face, charcode) function glyphCacheHash(face, charcode)
return face..'_'..charcode; return face..'_'..charcode;
end end
function clearGlyphCache() function clearGlyphCache()
glyphcache = {} glyphcache = {}
glyphcache_current_memsize = 0 glyphcache_current_memsize = 0
end end
function sizeUtf8Text(x, width, face, text, kerning) function sizeUtf8Text(x, width, face, text, kerning)
if text == nil then if not text then
Debug("sizeUtf8Text called without text"); Debug("sizeUtf8Text called without text");
return return
end 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
@ -61,72 +70,63 @@ function sizeUtf8Text(x, width, face, text, kerning)
if pen_x < (width - x) then if pen_x < (width - x) then
local charcode = util.utf8charcode(uchar) local charcode = util.utf8charcode(uchar)
local glyph = getGlyph(face, charcode) local glyph = getGlyph(face, charcode)
if kerning and prevcharcode then if kerning and (prevcharcode ~= 0) then
local kern = face.ftface:getKerning(prevcharcode, charcode) pen_x = pen_x + face.ftface:getKerning(prevcharcode, charcode)
pen_x = pen_x + kern
--Debug("prev:"..string.char(prevcharcode).." curr:"..string.char(charcode).." kern:"..kern)
else
--Debug("curr:"..string.char(charcode))
end end
pen_x = pen_x + glyph.ax pen_x = pen_x + glyph.ax
pen_y_top = math.max(pen_y_top, glyph.t) pen_y_top = math.max(pen_y_top, glyph.t)
pen_y_bottom = math.max(pen_y_bottom, glyph.bb:getHeight() - glyph.t) pen_y_bottom = math.max(pen_y_bottom, glyph.bb:getHeight() - glyph.t)
--Debug("ax:"..glyph.ax.." t:"..glyph.t.." r:"..glyph.r.." h:"..glyph.bb:getHeight().." w:"..glyph.bb:getWidth().." yt:"..pen_y_top.." yb:"..pen_y_bottom) --Debug("ax:"..glyph.ax.." t:"..glyph.t.." r:"..glyph.r.." h:"..glyph.bb:getHeight().." w:"..glyph.bb:getWidth().." yt:"..pen_y_top.." yb:"..pen_y_bottom)
prevcharcode = charcode prevcharcode = charcode
end end -- if pen_x < (width -x)
end end -- for uchar
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 renderUtf8Text(buffer, x, y, face, text, kerning) function renderUtf8Text(buffer, x, y, face, text, kerning)
if text == nil then if not text then
Debug("renderUtf8Text called without text"); Debug("renderUtf8Text called without text");
return 0 return 0
end 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
local prevcharcode = 0 local prevcharcode = 0
buffer_width = buffer:getWidth()
for uchar in string.gfind(text, "([%z\1-\127\194-\244][\128-\191]*)") do for uchar in string.gfind(text, "([%z\1-\127\194-\244][\128-\191]*)") do
if pen_x < buffer:getWidth() then if pen_x < buffer_width then
local charcode = util.utf8charcode(uchar) local charcode = util.utf8charcode(uchar)
local glyph = getGlyph(face, charcode) local glyph = getGlyph(face, charcode)
if kerning and prevcharcode then if kerning and (prevcharcode ~= 0) then
local kern = face.ftface:getKerning(prevcharcode, charcode) pen_x = pen_x + face.ftface:getKerning(prevcharcode, charcode)
pen_x = pen_x + kern
--Debug("prev:"..string.char(prevcharcode).." curr:"..string.char(charcode).." pen_x:"..pen_x.." kern:"..kern)
buffer:addblitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
else
--Debug(" curr:"..string.char(charcode))
buffer:blitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
end end
buffer:addblitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
pen_x = pen_x + glyph.ax pen_x = pen_x + glyph.ax
prevcharcode = charcode prevcharcode = charcode
end end -- if pen_x < buffer_width
end end -- for uchar
return pen_x return pen_x
end end
-- render UTF8 text restricted by width 'w' -- render UTF8 text restricted by width 'w'
function renderUtf8TextWidth(buffer, x, y, face, text, kerning, w) function renderUtf8TextWidth(buffer, x, y, face, text, kerning, w)
if text == nil then if not text then
Debug("renderUtf8Text called without text"); Debug("renderUtf8Text called without text");
return nil return nil
end end
local prevcharcode, pen_x, rest = 0, 0, "" local prevcharcode, pen_x, rest = 0, 0, ""
for uchar in string.gfind(text, "([%z\1-\127\194-\244][\128-\191]*)") do for uchar in string.gfind(text, "([%z\1-\127\194-\244][\128-\191]*)") do
if pen_x < w then if pen_x < w then
local charcode = util.utf8charcode(uchar) local charcode = util.utf8charcode(uchar)
local glyph = getGlyph(face, charcode) local glyph = getGlyph(face, charcode)
if kerning and prevcharcode then if kerning and prevcharcode then
local kern = face.ftface:getKerning(prevcharcode, charcode) pen_x = pen_x + face.ftface:getKerning(prevcharcode, charcode)
pen_x = pen_x + kern
--Debug("prev:"..string.char(prevcharcode).." curr:"..string.char(charcode).." pen_x:"..pen_x.." kern:"..kern)
buffer:addblitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
else
--Debug(" curr:"..string.char(charcode))
buffer:blitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
end end
buffer:addblitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
pen_x = pen_x + glyph.ax pen_x = pen_x + glyph.ax
prevcharcode = charcode prevcharcode = charcode
else else
@ -134,7 +134,7 @@ function renderUtf8TextWidth(buffer, x, y, face, text, kerning, w)
rest = rest .. uchar rest = rest .. uchar
end end
end end
return { leaved = rest, x = pen_x, y = y } return { left = rest, x = pen_x, y = y }
end end
function SplitString(text) function SplitString(text)
@ -176,13 +176,12 @@ function renderUtf8Multiline(buffer, x, y, face, text, kerning, w, line_spacing)
end end
lx = x -- move lx to the line start and draw next word until the last char lx = x -- move lx to the line start and draw next word until the last char
render = renderUtf8TextWidth(buffer, lx, y, face, words[i], kerning, w-gl.ax) render = renderUtf8TextWidth(buffer, lx, y, face, words[i], kerning, w-gl.ax)
while render.leaved ~= "" do while render.left ~= "" do
y = y + line_spacing y = y + line_spacing
render = renderUtf8TextWidth(buffer, lx, y, face, render.leaved, kerning, w-gl.ax) render = renderUtf8TextWidth(buffer, lx, y, face, render.left, kerning, w-gl.ax)
end end
lx = lx + render.x lx = lx + render.x
end -- if end -- if
end --for end --for
return { x = x, y = y } return { x = x, y = y }
end end

Loading…
Cancel
Save