| 167 | } |
| 168 | |
| 169 | void Client::updateComposition(Json::Value& msg, Ime::EditSession* session, bool& endComposition) { |
| 170 | const auto& compositionStringVal = msg["compositionString"]; |
| 171 | bool emptyComposition = false; |
| 172 | bool hasCompositionString = false; |
| 173 | std::wstring compositionString; |
| 174 | if (compositionStringVal.isString()) { |
| 175 | // composition buffer |
| 176 | compositionString = utf8ToUtf16(compositionStringVal.asCString()); |
| 177 | hasCompositionString = true; |
| 178 | if (compositionString.empty()) { |
| 179 | emptyComposition = true; |
| 180 | if (textService_->isComposing() && !textService_->showingCandidates()) { |
| 181 | // when the composition buffer is empty and we are not showing the candidate list, end composition. |
| 182 | textService_->setCompositionString(session, L"", 0); |
| 183 | endComposition = true; |
| 184 | } |
| 185 | } |
| 186 | else { |
| 187 | if (!textService_->isComposing()) { |
| 188 | textService_->startComposition(session->context()); |
| 189 | } |
| 190 | textService_->setCompositionString(session, compositionString.c_str(), compositionString.length()); |
| 191 | } |
| 192 | // FIXME: update the position of candidate and message window when the composition string is changed. |
| 193 | if (textService_->candidateWindow_ != nullptr) { |
| 194 | textService_->updateCandidatesWindow(session); |
| 195 | } |
| 196 | if (textService_->messageWindow_ != nullptr) { |
| 197 | textService_->updateMessageWindow(session); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | const auto& compositionCursorVal = msg["compositionCursor"]; |
| 202 | if (compositionCursorVal.isInt()) { |
| 203 | // composition cursor |
| 204 | if (!emptyComposition) { |
| 205 | int compositionCursor = compositionCursorVal.asInt(); |
| 206 | if (!textService_->isComposing()) { |
| 207 | textService_->startComposition(session->context()); |
| 208 | } |
| 209 | // NOTE: |
| 210 | // This fixes PIME bug #166: incorrect handling of UTF-16 surrogate pairs. |
| 211 | // The TSF API unfortunately treat a UTF-16 surrogate pair as two characters while |
| 212 | // they actually represent one unicode character only. To workaround this TSF bug, |
| 213 | // we get the composition string, and try to move the cursor twice when a UTF-16 |
| 214 | // surrogate pair is found. |
| 215 | if (!hasCompositionString) |
| 216 | compositionString = textService_->compositionString(session); |
| 217 | int fixedCursorPos = 0; |
| 218 | for (int i = 0; i < compositionCursor; ++i) { |
| 219 | ++fixedCursorPos; |
| 220 | if (IS_HIGH_SURROGATE(compositionString[i])) // this is the first part of a UTF16 surrogate pair (Windows uses UTF16-LE) |
| 221 | ++fixedCursorPos; |
| 222 | } |
| 223 | textService_->setCompositionCursor(session, fixedCursorPos); |
| 224 | } |
| 225 | } |
| 226 | if (endComposition) { |
nothing calls this directly
no test coverage detected