-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext2texture.lua
52 lines (49 loc) · 1.37 KB
/
text2texture.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
local t2t = {}
local config = {
mapping = "`1234567890-=~!@#$%^&*()_+qwertyuiop[]QWERTYUIOP{}asdfghjkl;'ASDFGHJKL:\"zxcvbnm,./ZXCVBNM<>?\\|",
path = "grid.fontmap"
}
local fontmap = textures[config.path]
local characters = {}
local fontmap_size = fontmap:getDimensions()
local width = 0
local charID = 1
local char_font = {}
for scanX = 0, fontmap_size.x-1, 1 do
width = width + 1
local column = {}
local gap = true
for scanY = 0, fontmap_size.y-1, 1 do
local p = fontmap:getPixel(scanX,scanY)
if p.x > 0.5 then
gap = false
table.insert(char_font,vec(width,scanY))
end
end
if gap then
characters[config.mapping:sub(charID,charID)] = {data=char_font,width = width}
width = 0
charID = charID + 1
char_font = {}
end
characters[config.mapping:sub(charID,charID)] = {}
end
characters["WHITESPACE"] = {data={},width=3}
characters["LINEBREAK"] = {data={},width=0,linebreak=true}
---@param text string
function t2t:text2pixels(text)
local compound = {}
for i = 1, text:len(), 1 do
local char = text:sub(i,i)
local c = characters[char]
if c then
table.insert(compound,c)
elseif char == " " then
table.insert(compound,characters.WHITESPACE)
elseif char == "\n" then
table.insert(compound,characters.LINEBREAK)
end
end
return compound
end
return t2t