Repository navigation
Expand file tree
/
Copy pathtext.lua
More file actions
271 lines (243 loc) · 7.8 KB
/
Copy pathtext.lua
File metadata and controls
271 lines (243 loc) · 7.8 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
--[[
License for code: LGPL 3.0
see at: https://www.gnu.org/licenses/lgpl-3.0.txt
This code was adapted from signs_lib from VanessaE
the original lib can be found at: https://content.minetest.net/packages/mt-mods/signs_lib/
]]--
-- CONSTANTS
-- Path to the textures.
local TP = signs_lib.path .. "/textures"
-- Font file formatter
local CHAR_FILE = "%s_%02x.png"
-- Fonts path
local CHAR_PATH = TP .. "/" .. CHAR_FILE
local max_lenght = 20
-- Initialize character texture cache
local ctexcache = {}
local function fill_line(x, y, w, c, font_size, colorbgw)
c = c or "0"
local tex = { }
for xx = 0, math.max(0, w), colorbgw do
table.insert(tex, (":%d,%d=signs_lib_color_"..font_size.."px_%s.png"):format(x + xx, y, c))
end
return table.concat(tex)
end
local function clamp_characters(text, max_lenght)
text = text or ""
max_lenght = max_lenght or 20
local control_chars = {"##","#0","#1","#2","#3","#4","#5","#6","#7","#8","#9","#a","#b","#c","#d","#e","#f"}
local control_order = {}
local new_string = text
--first creates a memory of each control code
for i = 1, #new_string do
local c = new_string:sub(i,i+1)
for i, item in pairs(control_chars) do
if c == item then
table.insert(control_order, item)
break
end
end
end
--create control spaces (the order was saved in "control_order"
local control_char = "\001"
for i, item in pairs(control_chars) do
new_string = string.gsub(new_string, item, control_char)
end
--now make another string counting it outside and breaking when reachs 20
local count = 0
local curr_index = 0
local c = ""
for i = 1, #new_string, 1 do
c = string.sub(new_string,i,i)
if not (c == control_char) then
count = count + 1
end
curr_index = i
if count == max_lenght then break end
end
local cutstring = string.sub(new_string,1,curr_index)
--now reconstruct the string
local outputstring = ""
local control_order_curr_intex = 0
for i = 1, #cutstring, 1 do
c = string.sub(cutstring,i,i)
if not (c == control_char) then
outputstring = outputstring .. (c or "")
else
control_order_curr_intex = control_order_curr_intex + 1
outputstring = outputstring .. (control_order[control_order_curr_intex] or "")
end
end
return outputstring, count
end
-- check if a file does exist
-- to avoid reopening file after checking again
-- pass TRUE as second argument
local function file_exists(name, return_handle, mode)
mode = mode or "r";
local f = io.open(name, mode)
if f ~= nil then
if (return_handle) then
return f
end
io.close(f)
return true
else
return false
end
end
-- make char texture file name
-- if texture file does not exist use fallback texture instead
local function char_tex(font_name, ch)
if ctexcache[font_name..ch] then
return ctexcache[font_name..ch], true
else
local c = ch:byte()
local exists, tex = file_exists(CHAR_PATH:format(font_name, c))
if exists and c ~= 14 then
tex = CHAR_FILE:format(font_name, c)
else
tex = CHAR_FILE:format(font_name, 0x0)
end
ctexcache[font_name..ch] = tex
return tex, exists
end
end
local function make_text_texture(text, default_color, line_width, line_height, cwidth_tab, font_size, colorbgw)
local split = signs_lib.split_lines_and_words
local width = 0
local maxw = 0
local font_name = "signs_lib_font_"..font_size.."px"
local text_ansi = signs_lib.Utf8ToAnsi(text)
local text_splited = split(text_ansi)[1]
local words = { }
default_color = default_color or 0
local cur_color = tonumber(default_color, 16)
-- We check which chars are available here.
for word_i, word in ipairs(text_splited) do
local chars = { }
local ch_offs = 0
word = string.gsub(word, "%^[12345678abcdefgh]", {
["^1"] = string.char(0x81),
["^2"] = string.char(0x82),
["^3"] = string.char(0x83),
["^4"] = string.char(0x84),
["^5"] = string.char(0x85),
["^6"] = string.char(0x86),
["^7"] = string.char(0x87),
["^8"] = string.char(0x88),
["^a"] = string.char(0x8a),
["^b"] = string.char(0x8b),
["^c"] = string.char(0x8c),
["^d"] = string.char(0x8d),
["^e"] = string.char(0x8e),
["^f"] = string.char(0x8f),
["^g"] = string.char(0x90),
["^h"] = string.char(0x91)
})
local word_l = #word
local i = 1
while i <= word_l do
local c = word:sub(i, i)
if c == "#" then
local cc = tonumber(word:sub(i+1, i+1), 16)
if cc then
i = i + 1
cur_color = cc
end
else
local w = cwidth_tab[c]
if w then
width = width + w + 1
if width >= (line_width - cwidth_tab[" "]) then
width = 0
else
maxw = math.max(width, maxw)
end
local max_input_chars = max_lenght
if #chars < max_input_chars then
table.insert(chars, {
off = ch_offs,
tex = char_tex(font_name, c),
col = ("%X"):format(cur_color),
})
end
ch_offs = ch_offs + w
end
end
i = i + 1
end
width = width + cwidth_tab[" "] + 1
maxw = math.max(width, maxw)
table.insert(words, { chars=chars, w=ch_offs })
end
-- Okay, we actually build the "line texture" here.
local texture = { }
local start_xpos = math.floor((line_width - maxw) / 2)
local xpos = start_xpos
local ypos = line_height
if line_height == signs_lib.lineheight32 then ypos = line_height/4 end
cur_color = nil
for word_i, word in ipairs(words) do
local xoffs = (xpos - start_xpos)
if (xoffs > 0) and ((xoffs + word.w) > maxw) then
table.insert(texture, fill_line(xpos, ypos, maxw, "n", font_size, colorbgw))
xpos = start_xpos
ypos = ypos + line_height
table.insert(texture, fill_line(xpos, ypos, maxw, cur_color, font_size, colorbgw))
end
for ch_i, ch in ipairs(word.chars) do
if ch.col ~= cur_color then
cur_color = ch.col
table.insert(texture, fill_line(xpos + ch.off, ypos, maxw, cur_color, font_size, colorbgw))
end
table.insert(texture, (":%d,%d=%s"):format(xpos + ch.off, ypos, ch.tex))
end
table.insert(
texture,
(":%d,%d="):format(xpos + word.w, ypos) .. char_tex(font_name, " ")
)
xpos = xpos + word.w + cwidth_tab[" "]
if xpos >= (line_width + cwidth_tab[" "]) then break end
end
table.insert(texture, fill_line(xpos, ypos, maxw, "n", font_size, colorbgw))
table.insert(texture, fill_line(start_xpos, ypos + line_height, maxw, "n", font_size, colorbgw))
return table.concat(texture)
end
function airutils.convert_text_to_texture(text, default_color, horizontal_aligment)
text = text or ""
default_color = default_color or 0
horizontal_aligment = horizontal_aligment or 3
local font_size
local line_width
local line_height
local char_width
local colorbgw
local chars_per_line
local widemult
local count = 0
--text = string.sub(text,1,max_lenght)
text, count = clamp_characters(text, max_lenght)
if count <= 10 then
widemult = 0.75
font_size = 32
chars_per_line = 10
line_width = math.floor(signs_lib.avgwidth32 * chars_per_line) * (horizontal_aligment * widemult)
line_height = signs_lib.lineheight32
char_width = signs_lib.charwidth32
colorbgw = signs_lib.colorbgw32
else
widemult = 0.5
font_size = 16
chars_per_line = 21
line_width = math.floor(signs_lib.avgwidth16 * chars_per_line) * (horizontal_aligment * widemult)
line_height = signs_lib.lineheight16
char_width = signs_lib.charwidth16
colorbgw = signs_lib.colorbgw16
end
local texture = { ("[combine:%dx%d"):format(line_width, line_height) }
local linetex = make_text_texture(text, default_color, line_width, line_height, char_width, font_size, colorbgw)
table.insert(texture, linetex)
table.insert(texture, "^[makealpha:0,0,0")
return table.concat(texture, "")
end