-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.au3
180 lines (172 loc) · 5.54 KB
/
helper.au3
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
#include-once
#include <Array.au3>
#include <AutoItConstants.au3>
#include <File.au3>
#include <Math.au3>
Global Enum Step *2 _; Flags for keyboard states (dots, the space bar, menu keys).
$DOT_1 = 1, _
$DOT_2, _
$DOT_3, _
$DOT_4, _
$DOT_5, _
$DOT_6, _
$DOT_7, _
$DOT_8, _
$SPACE_BAR, _
$CAPSLOCK_PRESSED, _
$INSERT_PRESSED, _
$LWIN_PRESSED, _
$RWIN_PRESSED, _
$NUMPAD0_PRESSED, _
$LSHIFT_PRESSED, _
$RSHIFT_PRESSED, _
$LCONTROL_PRESSED, _
$RCONTROL_PRESSED, _
$LMENU_PRESSED, _
$RMENU_PRESSED, _
$SIX_DOTS_MASK = BitOR($DOT_1, $DOT_2, $DOT_3, $DOT_4, $DOT_5, $DOT_6), _
$EIGHT_DOTS_MASK = BitOR($SIX_DOTS_MASK, $DOT_7, $DOT_8), _
$BRL_MASK = BitOR($EIGHT_DOTS_MASK, $SPACE_BAR), _
$MODIFIER_KEY_MASK = BitOR($CAPSLOCK_PRESSED, $INSERT_PRESSED, $LWIN_PRESSED, $RWIN_PRESSED, $NUMPAD0_PRESSED, _
$LSHIFT_PRESSED, $RSHIFT_PRESSED, $LCONTROL_PRESSED, $RCONTROL_PRESSED), _
$LRMENU_MASK = BitOr($LMENU_PRESSED, $RMENU_PRESSED)
Func BRL2Bopomofo($sBRL, $bReload = False)
Static $table = Null
If $bReload Or Not IsArray($table) Then
Local $bopomofo_list
$table = ReadMapping("bopomofo.txt")
If @error Then
$table = Null
Return SetError($EOF, 0, "")
EndIf
For $i = 0 To UBound($table, $UBOUND_ROWS) - 1
$table[$i][0] = Digits2BRL($table[$i][0])
$bopomofo_list = StringSplit($table[$i][1], "-")
For $j = 1 To $bopomofo_list[0]
$bopomofo_list[$j] = Dec($bopomofo_list[$j])
Next
$table[$i][1] = StringFromASCIIArray($bopomofo_list, 1)
Next
_ArraySort($table)
EndIf
Local $pos = _ArraySearch($table, "^\Q" & $sBRL, 0, 0, 1, 3); Prefix test.
If @error Then Return SetError(1, 0, ""); Not found.
$pos = _ArraySearch($table, $sBRL, $pos, 0, 1); Exact matching.
Return SetError(0, 0, @error ? "" : $table[$pos][1]); Either prefix or a match.
EndFunc
Func BRL2Chr($iBRL); BRL to character, except the space.
Static $raw = " a1b'k2l`cif/msp""e3h9o6r~djg>ntq,*5<-u8v.%{$+x!&;:4|0z7(_?w}#y)="; It must be constant.
Local $c = StringMid($raw, BitAND($iBRL, $SIX_DOTS_MASK) + 1, 1)
If BitAND($iBRL, $DOT_7) Then
$c = Asc($c)
Switch $c
Case Asc("`") To Asc("~")
$c = Chr($c - 32)
Case Else
$c = ""
EndSwitch
EndIf
Return $c
EndFunc
Func Digits2BRL($str)
Local $cell_list = StringSplit($str, "-"), $dots
For $i = 1 To $cell_list[0]
$dots = 0x2800
If $cell_list[$i] <> "0" Then
For $d In StringSplit($cell_list[$i], "", $STR_NOCOUNT)
$dots = BitOR($dots, Eval("DOT_" & $d))
Next
EndIf
$cell_list[$i] = Asc(BRL2Chr($dots))
Next
Return StringFromASCIIArray($cell_list, 1)
EndFunc
Func IsBRLKey($iKeycode)
Switch $iKeycode
Case $VK_A
Return $DOT_7
Case $VK_D
Return $DOT_2
Case $VK_F
Return $DOT_1
Case $VK_J
Return $DOT_4
Case $VK_K
Return $DOT_5
Case $VK_L
Return $DOT_6
Case $VK_S
Return $DOT_3
Case $VK_SPACE
Return $SPACE_BAR
EndSwitch
Return 0; Not a BRL key.
EndFunc
Func ModifierKey2Flag($vkCode)
Switch $vkCode
Case $VK_CAPITAL
Return $CAPSLOCK_PRESSED
Case $VK_INSERT
Return $INSERT_PRESSED
Case $VK_LWIN
Return $LWIN_PRESSED
Case $VK_RWIN
Return $RWIN_PRESSED
Case $VK_NUMPAD0
Return $NUMPAD0_PRESSED
Case $VK_LSHIFT
Return $LSHIFT_PRESSED
Case $VK_RSHIFT
Return $RSHIFT_PRESSED
Case $VK_LCONTROL
Return $LCONTROL_PRESSED
Case $VK_RCONTROL
Return $RCONTROL_PRESSED
EndSwitch
Return 0; Null flag.
EndFunc
Func BRL2Key($sBRL, $hKL)
Static $data[][] = [[0x04040404, Null]]
Local $t = _ArraySearch($data, $hKL)
If @error Then; The specified $hKL is currently not found.
Local $row[][UBound($data, $UBOUND_COLUMNS)] = [[$hKL]], $pos = 0
$t = ReadMapping("Keyboard-Layouts\" & Hex($hKL, 8) & ".txt")
If @error Then Return SetError($EOF, 0, $sBRL)
For $i = 0 To UBound($t, $UBOUND_ROWS) - 1
$t[$i][0] = Dec($t[$i][0])
Next
_ArraySort($t)
$row[0][1] = $t
_ArrayAdd($data, $row)
Else
$t = $data[$t][1]; To read out the embedded array.
If Not IsArray($t) Then Return SetError(0, 0, StringRegExpReplace($sBRL, "([!#+\^{}])", "\{${1}\}")); The default keyboard layout.
EndIf
$sBRL = BRL2Bopomofo($sBRL)
If Not $sBRL Then Return SetError(@error, @extended, $sBRL)
Local $answer = "", $p
For $c In StringToASCIIArray($sBRL)
$p = _ArrayBinarySearch($t, $c)
$answer &= @error ? $c : $t[$p][1]
Next
Return SetError(0, 0, $answer)
EndFunc
Func ReadMapping($sFilePath)
Local $lines, $iLen = 0, $iDim2 = 0
_FileReadToArray($sFilePath, $lines, BitOR($FRTA_NOCOUNT, $FRTA_INTARRAYS), @TAB)
If @error Then Return SetError(@error, @extended, 0)
For $i = 0 To UBound($lines) - 1
If StringLeft(($lines[$i])[0], 1) <> "#" Then
$iDim2 = _Max(UBound($lines[$i]), $iDim2)
If $iLen < $i Then _ArraySwap($lines, $iLen, $i)
$iLen += 1
EndIf
Next
Local $table[$iLen][$iDim2]
For $i = 0 To $iLen - 1
For $j = 0 To UBound($lines[$i]) - 1
$table[$i][$j] = ($lines[$i])[$j]
Next
Next
Return SetError(0, 0, $table)
EndFunc