2011-12-01 00:35:53 +00:00
|
|
|
glyphcache_max_memsize = 256*1024 -- 256kB glyphcache
|
|
|
|
glyphcache_current_memsize = 0
|
|
|
|
glyphcache = {}
|
|
|
|
glyphcache_max_age = 4096
|
2012-03-20 19:15:24 +00:00
|
|
|
function glyphCacheClaim(size)
|
2011-12-01 00:35:53 +00:00
|
|
|
if(size > glyphcache_max_memsize) then
|
|
|
|
error("too much memory claimed")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
while glyphcache_current_memsize + size > glyphcache_max_memsize do
|
|
|
|
for k, _ in pairs(glyphcache) do
|
|
|
|
if glyphcache[k].age > 0 then
|
|
|
|
glyphcache[k].age = glyphcache[k].age - 1
|
|
|
|
else
|
|
|
|
glyphcache_current_memsize = glyphcache_current_memsize - glyphcache[k].size
|
2012-04-09 17:04:26 +00:00
|
|
|
glyphcache[k].glyph.bb:free()
|
2011-12-01 00:35:53 +00:00
|
|
|
glyphcache[k] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
glyphcache_current_memsize = glyphcache_current_memsize + size
|
|
|
|
return true
|
|
|
|
end
|
2012-04-09 17:04:26 +00:00
|
|
|
function getGlyph(face, charcode)
|
|
|
|
local hash = glyphCacheHash(face.hash, charcode)
|
2011-12-01 00:35:53 +00:00
|
|
|
if glyphcache[hash] == nil then
|
2012-04-09 17:04:26 +00:00
|
|
|
local glyph = face.ftface:renderGlyph(charcode)
|
2011-12-01 00:35:53 +00:00
|
|
|
local size = glyph.bb:getWidth() * glyph.bb:getHeight() / 2 + 32
|
2012-03-20 19:15:24 +00:00
|
|
|
glyphCacheClaim(size);
|
2011-12-01 00:35:53 +00:00
|
|
|
glyphcache[hash] = {
|
|
|
|
age = glyphcache_max_age,
|
|
|
|
size = size,
|
|
|
|
g = glyph
|
|
|
|
}
|
2011-12-01 13:28:10 +00:00
|
|
|
else
|
|
|
|
glyphcache[hash].age = glyphcache_max_age
|
2011-12-01 00:35:53 +00:00
|
|
|
end
|
|
|
|
return glyphcache[hash].g
|
|
|
|
end
|
2012-03-20 19:15:24 +00:00
|
|
|
function glyphCacheHash(face, charcode)
|
2011-12-01 00:35:53 +00:00
|
|
|
return face..'_'..charcode;
|
|
|
|
end
|
2012-03-20 19:15:24 +00:00
|
|
|
function clearGlyphCache()
|
2011-12-01 00:35:53 +00:00
|
|
|
glyphcache = {}
|
|
|
|
end
|
|
|
|
|
2012-04-15 22:10:41 +00:00
|
|
|
function sizeUtf8Text(x, width, face, text, kerning)
|
2012-03-19 18:41:09 +00:00
|
|
|
if text == nil then
|
2012-04-20 10:29:08 +00:00
|
|
|
debug("sizeUtf8Text called without text");
|
2012-03-19 18:41:09 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
-- may still need more adaptive pen placement when kerning,
|
|
|
|
-- see: http://freetype.org/freetype2/docs/glyphs/glyphs-4.html
|
|
|
|
local pen_x = 0
|
2012-04-15 14:07:39 +00:00
|
|
|
local pen_y_top = 0
|
|
|
|
local pen_y_bottom = 0
|
2012-03-19 18:41:09 +00:00
|
|
|
local prevcharcode = 0
|
|
|
|
for uchar in string.gfind(text, "([%z\1-\127\194-\244][\128-\191]*)") do
|
2012-04-15 14:07:39 +00:00
|
|
|
if pen_x < (width - x) then
|
2012-03-19 18:41:09 +00:00
|
|
|
local charcode = util.utf8charcode(uchar)
|
2012-04-15 22:10:41 +00:00
|
|
|
local glyph = getGlyph(face, charcode)
|
2012-03-19 18:41:09 +00:00
|
|
|
if kerning and prevcharcode then
|
2012-04-15 22:10:41 +00:00
|
|
|
local kern = face.ftface:getKerning(prevcharcode, charcode)
|
2012-03-19 18:41:09 +00:00
|
|
|
pen_x = pen_x + kern
|
2012-04-18 16:16:49 +00:00
|
|
|
--debug("prev:"..string.char(prevcharcode).." curr:"..string.char(charcode).." kern:"..kern)
|
2012-03-19 18:41:09 +00:00
|
|
|
else
|
2012-04-18 16:16:49 +00:00
|
|
|
--debug("curr:"..string.char(charcode))
|
2012-03-19 18:41:09 +00:00
|
|
|
end
|
|
|
|
pen_x = pen_x + glyph.ax
|
2012-04-15 14:07:39 +00:00
|
|
|
pen_y_top = math.max(pen_y_top, glyph.t)
|
|
|
|
pen_y_bottom = math.max(pen_y_bottom, glyph.bb:getHeight() - glyph.t)
|
2012-04-18 16:16:49 +00:00
|
|
|
--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)
|
2012-03-19 18:41:09 +00:00
|
|
|
prevcharcode = charcode
|
|
|
|
end
|
|
|
|
end
|
2012-04-15 14:07:39 +00:00
|
|
|
return { x = pen_x, y_top = pen_y_top, y_bottom = pen_y_bottom}
|
2012-03-19 18:41:09 +00:00
|
|
|
end
|
|
|
|
|
2012-04-15 22:10:41 +00:00
|
|
|
function renderUtf8Text(buffer, x, y, face, text, kerning)
|
2012-03-06 11:31:51 +00:00
|
|
|
if text == nil then
|
2012-04-20 10:29:08 +00:00
|
|
|
debug("renderUtf8Text called without text");
|
2012-04-15 22:10:41 +00:00
|
|
|
return 0
|
2012-03-06 11:31:51 +00:00
|
|
|
end
|
2011-12-01 15:11:43 +00:00
|
|
|
-- may still need more adaptive pen placement when kerning,
|
|
|
|
-- see: http://freetype.org/freetype2/docs/glyphs/glyphs-4.html
|
2011-12-01 00:35:53 +00:00
|
|
|
local pen_x = 0
|
2011-12-01 15:11:43 +00:00
|
|
|
local prevcharcode = 0
|
2011-12-01 00:35:53 +00:00
|
|
|
for uchar in string.gfind(text, "([%z\1-\127\194-\244][\128-\191]*)") do
|
2011-12-07 23:45:39 +00:00
|
|
|
if pen_x < buffer:getWidth() then
|
|
|
|
local charcode = util.utf8charcode(uchar)
|
2012-04-09 17:04:26 +00:00
|
|
|
local glyph = getGlyph(face, charcode)
|
2011-12-07 23:45:39 +00:00
|
|
|
if kerning and prevcharcode then
|
2012-04-09 17:04:26 +00:00
|
|
|
local kern = face.ftface:getKerning(prevcharcode, charcode)
|
2011-12-07 23:45:39 +00:00
|
|
|
pen_x = pen_x + kern
|
2012-04-18 16:16:49 +00:00
|
|
|
--debug("prev:"..string.char(prevcharcode).." curr:"..string.char(charcode).." pen_x:"..pen_x.." kern:"..kern)
|
2011-12-07 23:45:39 +00:00
|
|
|
buffer:addblitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
|
|
|
|
else
|
2012-04-18 16:16:49 +00:00
|
|
|
--debug(" curr:"..string.char(charcode))
|
2011-12-07 23:45:39 +00:00
|
|
|
buffer:blitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
|
|
|
|
end
|
|
|
|
pen_x = pen_x + glyph.ax
|
|
|
|
prevcharcode = charcode
|
2011-12-01 15:11:43 +00:00
|
|
|
end
|
2011-12-01 00:35:53 +00:00
|
|
|
end
|
2012-03-13 21:51:25 +00:00
|
|
|
return pen_x
|
2011-12-01 00:35:53 +00:00
|
|
|
end
|