Skip to content

Commit 96f9a33

Browse files
Merge pull request tuyenvm#297 from nhutuananh/master
2 parents b0055ea + 3878e6c commit 96f9a33

File tree

1 file changed

+70
-84
lines changed

1 file changed

+70
-84
lines changed

Sources/OpenKey/engine/ConvertTool.cpp

Lines changed: 70 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ int convertToolHotKey = 0;
2525

2626
static vector<Uint8> _breakCode = {'.', '?', '!'};
2727

28-
static bool findKeyCode(const Uint32& charCode, const Uint8& code, int& j, int& k) {
28+
static bool findKeyCode(const Uint32& charCode, const Uint8& code, int& codeTableRow, int& codeTableCol) {
2929
//find character which has tone/mark
3030
for (map<Uint32, vector<Uint16>>::iterator it = _codeTable[code].begin(); it != _codeTable[code].end(); ++it) {
3131
for (int z = 0; z < it->second.size(); z++) {
3232
if (charCode == it->second[z]) {
33-
j = it->first;
34-
k = z;
33+
codeTableRow = it->first;
34+
codeTableCol = z;
3535
return true;
3636
}//end if
3737
}
@@ -50,8 +50,8 @@ static Uint16 getUnicodeCompoundMarkIndex(const Uint16& mark) {
5050

5151
string convertUtil(const string& sourceString) {
5252
wstring data = utf8ToWideString(sourceString);
53-
Uint16 t = 0, target;
54-
int j, k, p;
53+
Uint16 sourceCharCode = 0, target;
54+
int codeTableRow, codeTableCol;
5555
vector<wchar_t> _temp;
5656
bool hasBreak = false;
5757
bool shouldUpperCase = false;
@@ -61,116 +61,102 @@ string convertUtil(const string& sourceString) {
6161
shouldUpperCase = false;
6262

6363
for (int i = 0; i < data.size(); i++) {
64-
p = 0;
64+
Uint16 sourceCharCode = 0;
65+
int extraCharsConsumed = 0;
6566
//find char with tone/mark
6667
if (i < data.size() - 1) {
6768
switch (convertToolFromCode) {
6869
case 2: //VNI
6970
case 4: //1258
70-
t = (Uint16)data[i] | (data[i+1] << 8);
71-
p = 1;
71+
sourceCharCode = (Uint16)data[i] | (data[i+1] << 8);
72+
extraCharsConsumed = 1;
7273
break;
7374
case 3:{ //Unicode Compound
7475
target = getUnicodeCompoundMarkIndex(data[i+1]);
7576
if (target > 0){
76-
t = (Uint16)data[i] | target;
77-
p = 1;
77+
sourceCharCode = (Uint16)data[i] | target;
78+
extraCharsConsumed = 1;
7879
} else {
79-
t = (Uint16)data[i];
80+
sourceCharCode = (Uint16)data[i];
8081
}
8182
break;
8283
}
8384
default:
84-
t = (Uint16)data[i];
85+
sourceCharCode = (Uint16)data[i];
8586
break;
8687
}
87-
88-
if (findKeyCode(t, convertToolFromCode, j, k)) {
89-
i += p;
90-
target = _codeTable[convertToolToCode][j][k];
91-
if ((convertToolToAllCaps || shouldUpperCase) && k % 2 != 0) {
92-
target = _codeTable[convertToolToCode][j][k-1];
93-
} else if ((convertToolToAllNonCaps || !shouldUpperCase) && k % 2 == 0) {
94-
target = _codeTable[convertToolToCode][j][k+1];
95-
}
96-
97-
//remove mark/tone
98-
if (convertToolRemoveMark) {
99-
target = keyCodeToCharacter((Uint8)j);
100-
if (convertToolToAllCaps) {
101-
target = towupper(target);
102-
} else if (convertToolToAllNonCaps) {
103-
target = towlower(target);
104-
}
105-
}
106-
107-
if (convertToolToCode == 0 || convertToolToCode == 1) { //Unicode
108-
_temp.push_back(target);
109-
} else if (convertToolToCode == 2 || convertToolToCode == 4) { //VNI, VN Locale 1258
110-
if (HIBYTE(target) > 32) {
111-
_temp.push_back((Uint8)target);
112-
_temp.push_back(target>>8);
113-
} else {
114-
_temp.push_back((Uint8)target);
115-
}
116-
} else if (convertToolToCode == 3) { //Unicode Compound
117-
if ((target >> 13) > 0) {
118-
_temp.push_back(target & 0x1FFF);
119-
_temp.push_back(_unicodeCompoundMark[(target>>13) - 1]);
120-
} else {
121-
_temp.push_back(target);
122-
}
123-
}
124-
shouldUpperCase = false;
125-
hasBreak = false;
126-
continue;
127-
}
12888
}
129-
130-
//find primary keycode first
131-
t = (Uint16)data[i];
132-
if (findKeyCode(t, convertToolFromCode, j, k)) {
133-
target = _codeTable[convertToolToCode][j][k];
134-
if ((convertToolToAllCaps || shouldUpperCase) && k % 2 != 0) {
135-
target = _codeTable[convertToolToCode][j][k-1];
136-
} else if ((convertToolToAllNonCaps || !shouldUpperCase) && k % 2 == 0) {
137-
target = _codeTable[convertToolToCode][j][k+1];
89+
else // i = data.size() - 1 -- last character
90+
{
91+
sourceCharCode = (Uint16)data[i];
92+
}
93+
94+
if (findKeyCode(sourceCharCode, convertToolFromCode, codeTableRow, codeTableCol)) {
95+
i += extraCharsConsumed;
96+
target = _codeTable[convertToolToCode][codeTableRow][codeTableCol];
97+
if ((convertToolToAllCaps || shouldUpperCase) && codeTableCol % 2 != 0) {
98+
target = _codeTable[convertToolToCode][codeTableRow][codeTableCol-1];
99+
} else if ((convertToolToAllNonCaps) && codeTableCol % 2 == 0) { // || !shouldUpperCase
100+
target = _codeTable[convertToolToCode][codeTableRow][codeTableCol+1];
138101
}
139102

140103
//remove mark/tone
141104
if (convertToolRemoveMark) {
142-
target = keyCodeToCharacter((Uint8)j);
105+
target = keyCodeToCharacter((Uint8)codeTableRow);
143106
if (convertToolToAllCaps) {
144107
target = towupper(target);
145-
} else if (convertToolToAllNonCaps){
108+
} else if (convertToolToAllNonCaps) {
146109
target = towlower(target);
110+
} else {
111+
if (codeTableCol % 2 == 0) {
112+
target = towupper(target);
113+
} else {
114+
target = towlower(target);
115+
}
147116
}
148117
}
149118

150-
_temp.push_back(target);
119+
if (convertToolToCode == 0 || convertToolToCode == 1) { //Unicode
120+
_temp.push_back(target);
121+
} else if (convertToolToCode == 2 || convertToolToCode == 4) { //VNI, VN Locale 1258
122+
if (HIBYTE(target) > 32) {
123+
_temp.push_back((Uint8)target);
124+
_temp.push_back(target>>8);
125+
} else {
126+
_temp.push_back((Uint8)target);
127+
}
128+
} else if (convertToolToCode == 3) { //Unicode Compound
129+
if ((target >> 13) > 0) {
130+
_temp.push_back(target & 0x1FFF);
131+
_temp.push_back(_unicodeCompoundMark[(target>>13) - 1]);
132+
} else {
133+
_temp.push_back(target);
134+
}
135+
}
151136
shouldUpperCase = false;
152137
hasBreak = false;
153-
continue;
154-
}
155-
156-
//if dont find => normal char
157-
if (convertToolToAllCaps || shouldUpperCase)
158-
_temp.push_back(towupper(data[i]));
159-
else if (convertToolToAllNonCaps || !shouldUpperCase)
160-
_temp.push_back(towlower(data[i]));
138+
} // end if (findKeyCode(sourceCharCode
161139
else
162-
_temp.push_back(data[i]);
163-
164-
if (t == '\n' || (hasBreak && t == ' ')) {
165-
if (convertToolToCapsFirstLetter || convertToolToCapsEachWord)
140+
{
141+
//if dont find => normal char
142+
if (convertToolToAllCaps || shouldUpperCase)
143+
_temp.push_back(towupper(data[i]));
144+
else if (convertToolToAllNonCaps) // || !shouldUpperCase
145+
_temp.push_back(towlower(data[i]));
146+
else
147+
_temp.push_back(data[i]);
148+
149+
if (sourceCharCode == '\n' || (hasBreak && sourceCharCode == ' ')) {
150+
if (convertToolToCapsFirstLetter || convertToolToCapsEachWord)
151+
shouldUpperCase = true;
152+
} else if (sourceCharCode == ' ' && convertToolToCapsEachWord) {
166153
shouldUpperCase = true;
167-
} else if (t == ' ' && convertToolToCapsEachWord) {
168-
shouldUpperCase = true;
169-
} else if (std::find(_breakCode.begin(), _breakCode.end(), t) != _breakCode.end()) {
170-
hasBreak = true;
171-
} else {
172-
shouldUpperCase = false;
173-
hasBreak = false;
154+
} else if (std::find(_breakCode.begin(), _breakCode.end(), sourceCharCode) != _breakCode.end()) {
155+
hasBreak = true;
156+
} else {
157+
shouldUpperCase = false;
158+
hasBreak = false;
159+
}
174160
}
175161
}
176162
_temp.push_back(0);

0 commit comments

Comments
 (0)