リビジョン | a2cd2b640e4a16710e53798556a03281640b0f7d (tree) |
---|---|
日時 | 2020-10-03 19:11:44 |
作者 | dyknon <dyknon@user...> |
コミッター | dyknon |
カーソルが見える。
@@ -28,12 +28,31 @@ const IBusManager = imports.misc.ibusManager; | ||
28 | 28 | |
29 | 29 | const {IBus, Pango, St} = imports.gi; |
30 | 30 | |
31 | +var _cursor_letter = "|"; //"\u20ed"; | |
32 | +var _cursor_combining = false; | |
33 | +var _cursor_utf8_cnt; | |
34 | + | |
31 | 35 | var _style_pango_attributes; |
32 | 36 | var _preedit_handle_id = void 0; |
33 | 37 | var _style_handle_id = void 0; |
34 | 38 | const _disablers = []; |
35 | 39 | |
40 | +function utf8_len(c){ | |
41 | + if(c < 0x80){ | |
42 | + return 1; | |
43 | + }else if(c < 0x800){ | |
44 | + return 2; | |
45 | + }else if(c < 0x10000){ | |
46 | + return 3; | |
47 | + }else{ | |
48 | + return 4; | |
49 | + } | |
50 | +} | |
51 | + | |
36 | 52 | function init(){ |
53 | + if(_cursor_letter !== void 0){ | |
54 | + _cursor_utf8_cnt = utf8_len(_cursor_letter.codePointAt(0)) | |
55 | + } | |
37 | 56 | } |
38 | 57 | |
39 | 58 | function enable(){ |
@@ -119,45 +138,48 @@ function connect_signals(candidate_popup){ | ||
119 | 138 | // text.get_text(): Javascript string object. |
120 | 139 | // Its indexes are based on UTF-16 code units. |
121 | 140 | const text_string = text.get_text(); |
141 | + let newstring = text_string; | |
122 | 142 | const attrs = text.get_attributes(); |
123 | - const chars = []; | |
124 | - const codes = []; | |
125 | - { // split chars | |
143 | + const utf8i = [0]; | |
144 | + { | |
145 | + let unicode_count = 0; | |
126 | 146 | let utf16_count = 0; |
147 | + let utf8_count = 0; | |
127 | 148 | while(utf16_count < text_string.length){ |
128 | - const code = text_string.charCodeAt(utf16_count); | |
129 | - if(0xd800 <= code && code <= 0xdbff){ | |
130 | - const lo = text_string.charCodeAt(utf16_count+1); | |
131 | - if(0xdc00 <= lo && lo <= 0xdfff){ | |
132 | - utf16_count += 2; | |
133 | - chars.push(String.fromCharCode(code, lo)); | |
134 | - codes.push(((code&0x3ff) << 10 | lo&0x3ff) + 0x10000); | |
135 | - continue; | |
149 | + if(_cursor_letter !== void 0 && cursorPosition == unicode_count){ | |
150 | + if(!_cursor_combining || unicode_count){ | |
151 | + newstring = ""; | |
152 | + newstring += text_string.substring(0, utf16_count); | |
153 | + newstring += _cursor_letter; | |
154 | + newstring += text_string.substring(utf16_count); | |
155 | + utf8_count += _cursor_utf8_cnt; | |
136 | 156 | } |
137 | 157 | } |
138 | - utf16_count += 1; | |
139 | - chars.push(String.fromCharCode(code)); | |
140 | - codes.push(code); | |
141 | - } | |
142 | - } | |
143 | - const utf8i = [0]; | |
144 | - { // generate byte index list of utf-8 representation | |
145 | - let i = 0; | |
146 | - for(const c of codes){ | |
147 | - if(c < 0x80){ | |
148 | - i += 1; | |
149 | - }else if(c < 0x800){ | |
150 | - i += 2; | |
151 | - }else if(c < 0x10000){ | |
152 | - i += 3; | |
158 | + | |
159 | + const code = text_string.codePointAt(utf16_count); | |
160 | + if(code > 0xffff){ | |
161 | + utf16_count += 2; | |
153 | 162 | }else{ |
154 | - i += 4; | |
163 | + utf16_count += 1; | |
164 | + } | |
165 | + | |
166 | + utf8_count += utf8_len(code); | |
167 | + unicode_count++; | |
168 | + | |
169 | + utf8i.push(utf8_count); | |
170 | + } | |
171 | + if(_cursor_letter !== void 0 && cursorPosition == unicode_count){ | |
172 | + if(!_cursor_combining || unicode_count){ | |
173 | + newstring = ""; | |
174 | + newstring += text_string.substring(0, utf16_count); | |
175 | + newstring += _cursor_letter; | |
176 | + newstring += text_string.substring(utf16_count); | |
177 | + utf8_count += _cursor_utf8_cnt; | |
155 | 178 | } |
156 | - utf8i.push(i); | |
157 | 179 | } |
158 | 180 | } |
159 | 181 | |
160 | - candidate_popup._preeditText.text = text_string; | |
182 | + candidate_popup._preeditText.text = newstring; | |
161 | 183 | const pango_attrs = _style_pango_attributes.copy(); |
162 | 184 | { // Translate iBus attributes to Pango one. |
163 | 185 | let attr; |