forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUTF.lua
More file actions
61 lines (53 loc) · 2.12 KB
/
UTF.lua
File metadata and controls
61 lines (53 loc) · 2.12 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
function UTF(unicode)
if unicode <= 0x7F then return string.char(unicode) end
if (unicode <= 0x7FF) then
local Byte0 = 0xC0 + math.floor(unicode / 0x40);
local Byte1 = 0x80 + math.mod(unicode, 0x40);
return string.char(Byte0, Byte1);
end;
if (unicode <= 0xFFFF) then
local Byte0 = 0xE0 + math.floor(unicode / 0x1000);
local Byte1 = 0x80 + math.mod(math.floor(unicode / 0x40), 0x40);
local Byte2 = 0x80 + math.mod(unicode, 0x40);
return string.char(Byte0, Byte1, Byte2);
end;
if (unicode <= 0x10FFFF) then
local code = unicode
local Byte3= 0x80 + math.mod(code, 0x40);
code = math.floor(code / 0x40)
local Byte2= 0x80 + math.mod(code, 0x40);
code = math.floor(code / 0x40)
local Byte1= 0x80 + math.mod(code, 0x40);
code = math.floor(code / 0x40)
local Byte0= 0xF0 + code;
return string.char(Byte0, Byte1, Byte2, Byte3);
end;
WARN('Unicode cannot be greater than U+10FFFF! (UTF('.. unicode ..'))')
return ""
end
function InsertChar(str, chr, pos)
-- if we insert char to start or end of text just glue chr and str
if pos == 0 then return chr .. str end
local strLen = STR_Utf8Len(str)
if pos == strLen then return str .. chr end
-- otherwise insert
return STR_Utf8SubString(str, 1, pos) .. chr .. STR_Utf8SubString(str, pos+1, strLen - pos) -- split str and add chr between parts
end
function AddUnicodeCharToEditText(edit, unicode)
if unicode <= 0x7F then return false end
local unicodeChar = UTF(unicode)
if unicodeChar ~= "" then
local text = edit:GetText()
local charLim = edit:GetMaxChars()
if STR_Utf8Len(text) >= charLim then
PlaySound(Sound({Cue = 'UI_Menu_Error_01', Bank = 'Interface',}))
return true
end
local pos = edit:GetCaretPosition()
text = InsertChar(text, unicodeChar, pos)
edit:SetText(text)
edit:SetCaretPosition(pos + 1)
return true
end
return false
end