forked from gideros/BMFont
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmfont.lua
81 lines (71 loc) · 1.94 KB
/
bmfont.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
-- check if string1 starts with string2
local function startsWith(string1, string2)
return string1:sub(1, #string2) == string2
end
-- create table from a bmfont line
local function lineToTable(line)
local result = {}
for pair in line:gmatch("%a+=[-%d]+") do
local key = pair:match("%a+")
local value = pair:match("[-%d]+")
result[key] = tonumber(value)
end
return result
end
-- this is our BMFont class
BMFont = {}
BMFont.__index = BMFont
-- and its new function
function BMFont.new(...)
local self = setmetatable({}, BMFont)
if self.init ~= nil and type(self.init) == "function" then
self:init(...)
end
return self
end
function BMFont:init(fontfile, imagefile, filtering)
-- load font texture
self.texture = Texture.new(imagefile, filtering)
-- read glyphs from font.txt and store them in chars table
self.chars = {}
file = io.open(fontfile, "rt")
for line in file:lines() do
if startsWith(line, "char ") then
local char = lineToTable(line)
self.chars[char.id] = char
end
end
io.close(file)
end
-- this is our BMTextField class
BMTextField = gideros.class(Sprite)
function BMTextField:init(font, str)
self.font = font
self.str = str
self:createCharacters()
end
function BMTextField:setText(str)
if self.str ~= str then
self.str = str
self:createCharacters()
end
end
function BMTextField:createCharacters()
-- remove all children
for i=self:getNumChildren(),1,-1 do
self:removeChildAt(i)
end
-- create a TextureRegion from each character and add them as a Bitmap
local x = 0
local y = 0
for i=1,#self.str do
local char = self.font.chars[self.str:byte(i)]
if char ~= nil then
local region = TextureRegion.new(self.font.texture, char.x, char.y, char.width, char.height)
local bitmap = Bitmap.new(region)
bitmap:setPosition(x + char.xoffset, y + char.yoffset)
self:addChild(bitmap)
x = x + char.xadvance
end
end
end