forked from Hanjp-IM/libhanjp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputcontext.cpp
More file actions
128 lines (103 loc) · 2.38 KB
/
inputcontext.cpp
File metadata and controls
128 lines (103 loc) · 2.38 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
#include "hanjp.h"
#include <hangul.h>
#include "keyboard.h"
#include "automata.h"
using namespace Hanjp;
using namespace std;
static inline bool is_hiragana(char32_t ch) {
return ch >= 0x3040 && ch <= 0x309F;
}
static inline bool is_katakana(char32_t ch) {
return ch >= 0x30A0 && ch <= 0x30FF;
}
#define KANA_GAP 0x60
static inline void convert(u32string& str, OutputType type) {
for(auto& ch : str) {
switch(type) {
case HIRAGANA:
if(is_katakana(ch)) {
ch -= KANA_GAP;
}
break;
case KATAKANA:
if(is_hiragana(ch)) {
ch += KANA_GAP;
}
break;
case HALF_KATAKANA:
default:
break;
}
}
}
int Hanjp::init() {
return hangul_init();
}
int Hanjp::fini() {
return hangul_fini();
}
InputContext::InputContext() : output_type(HIRAGANA) {
am = new AutomataDefault;
keyboard = new Keyboard;
}
InputContext::~InputContext() {
delete am;
delete keyboard;
}
void InputContext::flush_internal() {
committed = preedit;
preedit.clear();
}
void InputContext::reset() {
am->flush();
hangul.clear();
preedit.clear();
committed.clear();
output_type = HIRAGANA;
}
u32string InputContext::flush() {
am->flush();
flush_internal();
return committed;
}
AMSIG InputContext::process(int ascii) {
char32_t ch;
AMSIG signal;
u32string popped;
committed.clear();
ch = keyboard->get_mapping(0, ascii);
signal = am->push(ch, popped, hangul); //push to automata and signal and results
convert(popped, output_type); //Convert popped string to output type
preedit += popped;
switch(signal) {
case FLUSH:
flush_internal();
break;
case EAT:
case POP:
default:
break;
}
return signal;
}
bool InputContext::backspace() {
if(!am->backspace()) {
if(preedit.empty()) {
return false;
}
preedit.pop_back();
}
return true;
}
void InputContext::set_output_type(OutputType type) {
output_type = type;
}
const u32string& InputContext::get_preedit_string() const {
return preedit;
}
const u32string& InputContext::get_commit_string() const {
return committed;
}
OutputType InputContext::get_output_type() const {
return output_type;
}