summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/include_lua/create_kbd_layout.lua
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/include_lua/create_kbd_layout.lua')
-rw-r--r--apps/plugins/lua/include_lua/create_kbd_layout.lua119
1 files changed, 64 insertions, 55 deletions
diff --git a/apps/plugins/lua/include_lua/create_kbd_layout.lua b/apps/plugins/lua/include_lua/create_kbd_layout.lua
index bb3ab297ca..3d8f343cc9 100644
--- a/apps/plugins/lua/include_lua/create_kbd_layout.lua
+++ b/apps/plugins/lua/include_lua/create_kbd_layout.lua
@@ -1,74 +1,82 @@
1--create keyboard layout 1--create keyboard layout
2--BILGUS 4/2021 2--BILGUS 4/2021
3local function encode_short(n) 3-- kbdlayout = require("create_kbd_layout")
4 return string.char(bit.band(0x00FF, n), bit.rshift(bit.band(0xFF00, n), 8)) 4-- local layout = kbdlayout.create_keyboard_layout("abcd")
5end 5local _kbdlayout = {} do
6 6
7function utf8decode(str) 7 local function encode_short(n)
8 local INVALID = 0xfffd 8 return string.char(bit.band(0x00FF, n), bit.rshift(bit.band(0xFF00, n), 8))
9 local t = {} 9 end
10 local function check_char(c)
11 local tail = false
12 local code
13 c = string.byte(c)
14 if (c <= 0x7f) or (c >= 0xc2) then
15 -- Start of new character
16 if (c < 0x80) then -- U-00000000 - U-0000007F, 1 string.byte
17 code = c;
18 elseif (c < 0xe0) then -- U-00000080 - U-000007FF, 2 string.bytes
19 tail = 1;
20 code = bit.band(c, 0x1f)
21 elseif (c < 0xf0) then -- U-00000800 - U-0000FFFF, 3 string.bytes
22 tail = 2;
23 code = bit.band(c, 0x0f)
24 elseif (c < 0xf5) then -- U-00010000 - U-001FFFFF, 4 string.bytes
25 tail = 3;
26 code = bit.band(c, 0x07)
27 else
28 -- Invalid size
29 code = 0xfffd;
30 end
31 10
32 while tail and c ~= 0 do 11 local function utf8decode(str)
33 tail = tail - 1 12 local INVALID = 0xfffd
34 if bit.band(c, 0xc0) == 0x80 then 13 local t = {}
35 -- Valid continuation character 14 local function check_char(c)
36 code = bit.bor(bit.lshift(code, 6),bit.band(c, 0x3f)) 15 local tail = false
16 local code
17 c = string.byte(c)
18 if (c <= 0x7f) or (c >= 0xc2) then
19 -- Start of new character
20 if (c < 0x80) then -- U-00000000 - U-0000007F, 1 string.byte
21 code = c;
22 elseif (c < 0xe0) then -- U-00000080 - U-000007FF, 2 string.bytes
23 tail = 1;
24 code = bit.band(c, 0x1f)
25 elseif (c < 0xf0) then -- U-00000800 - U-0000FFFF, 3 string.bytes
26 tail = 2;
27 code = bit.band(c, 0x0f)
28 elseif (c < 0xf5) then -- U-00010000 - U-001FFFFF, 4 string.bytes
29 tail = 3;
30 code = bit.band(c, 0x07)
37 else 31 else
38 -- Invalid continuation char 32 -- Invalid size
39 code = INVALID; 33 code = 0xfffd;
40 break; 34 end
35
36 while tail and c ~= 0 do
37 tail = tail - 1
38 if bit.band(c, 0xc0) == 0x80 then
39 -- Valid continuation character
40 code = bit.bor(bit.lshift(code, 6),bit.band(c, 0x3f))
41 else
42 -- Invalid continuation char
43 code = INVALID;
44 break;
45 end
41 end 46 end
47 else
48 -- Invalid UTF-8 char
49 code = INVALID;
42 end 50 end
43 else 51 -- currently we don't support chars above U-FFFF
44 -- Invalid UTF-8 char 52 t[#t + 1 ] = encode_short((code < 0x10000) and code or 0xfffd)
45 code = INVALID;
46 end 53 end
47 -- currently we don't support chars above U-FFFF 54 str:gsub(".", check_char) -- run check function for every char
48 t[#t + 1 ] = encode_short((code < 0x10000) and code or 0xfffd) 55 return table.concat(t)
49 end 56 end
50 str:gsub(".", check_char) -- run check function for every char
51 return table.concat(t)
52end
53 57
54function create_keyboard_layout(s_layout) 58 local function create_keyboard_layout(s_layout)
55 local insert = table.insert 59 local insert = table.insert
56 lines = {} 60 lines = {}
57 61
58 local t={} 62 local t={}
59 for str in string.gmatch(s_layout, "([^\n]+)") do 63 for str in string.gmatch(s_layout, "([^\n]+)") do
60 local len = string.len(str) 64 local len = string.len(str)
61 lines[#lines + 1] = 65 lines[#lines + 1] =
62 table.concat({encode_short(len), utf8decode(str)}) 66 table.concat({encode_short(len), utf8decode(str)})
63 end 67 end
64 lines[#lines + 1] = encode_short(0xFEFF) 68 lines[#lines + 1] = encode_short(0xFEFF)
65 69
66 return table.concat(lines) 70 return table.concat(lines)
71 end
72 _kbdlayout.create_keyboard_layout = create_keyboard_layout
73 _kbdlayout.utf8decode = utf8decode
67end 74end
68 75
76
69--[[ 77--[[
70local name = "Test_KBD_LAYOUT_" .. tostring(1) 78local name = "Test_KBD_LAYOUT_" .. tostring(1)
71local test = create_keyboard_layout("ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n0123456789") 79local test = _kbdlayout.create_keyboard_layout("ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n0123456789")
72local file = io.open('/' .. name, "w+") -- overwrite, rb ignores the 'b' flag 80local file = io.open('/' .. name, "w+") -- overwrite, rb ignores the 'b' flag
73file:write(test)-- write the layout to the file now 81file:write(test)-- write the layout to the file now
74file:close() 82file:close()
@@ -79,3 +87,4 @@ if not file then
79end 87end
80rb.kbd_input(name, test) 88rb.kbd_input(name, test)
81]] 89]]
90return _kbdlayout