draft commit, test implementation

This commit is contained in:
traycold
2012-03-19 19:41:09 +01:00
parent 496ab3720e
commit c11ecce55a
3 changed files with 68 additions and 7 deletions

View File

@@ -43,6 +43,35 @@ function clearglyphcache()
glyphcache = {}
end
function sizeUtf8Text(face, facehash, text, kerning)
if text == nil then
print("# sizeUtf8Text called without text");
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
local prevcharcode = 0
for uchar in string.gfind(text, "([%z\1-\127\194-\244][\128-\191]*)") do
if pen_x < buffer:getWidth() then
local charcode = util.utf8charcode(uchar)
local glyph = getglyph(face, facehash, charcode)
if kerning and prevcharcode then
local kern = face:getKerning(prevcharcode, charcode)
pen_x = pen_x + kern
print("prev:"..string.char(prevcharcode+10).." curr:"..string.char(charcode).." kern:"..kern)
buffer:addblitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
else
print("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
pen_x = pen_x + glyph.ax
prevcharcode = charcode
end
end
return pen_x
end
function renderUtf8Text(buffer, x, y, face, facehash, text, kerning)
if text == nil then
print("# renderUtf8Text called without text");
@@ -59,8 +88,10 @@ function renderUtf8Text(buffer, x, y, face, facehash, text, kerning)
if kerning and prevcharcode then
local kern = face:getKerning(prevcharcode, charcode)
pen_x = pen_x + kern
--print("prev:"..prevcharcode.." curr:"..charcode.." kern:"..kern)
buffer:addblitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
else
--print(" curr:"..charcode)
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
@@ -69,4 +100,3 @@ function renderUtf8Text(buffer, x, y, face, facehash, text, kerning)
end
return pen_x
end