/** * @brief Get the non-utf8 cursor pos for fcitx * * @return int **/ static int FcitxChewingGetRawCursorPos(char * str, int upos) { unsigned int i; int pos = 0; for (i = 0; i < upos; i++) { pos += fcitx_utf8_char_len(fcitx_utf8_get_nth_char(str, i)); } return pos; }
boolean TableCreatePhraseCode(TableDict* tableDict, char *strHZ) { unsigned char i; unsigned char i1, i2; size_t iLen; char strTemp[UTF8_MAX_LENGTH + 1] = {'\0', }; RECORD *recTemp; boolean bCanntFindCode = false; iLen = fcitx_utf8_strlen(strHZ); if (iLen >= tableDict->iCodeLength) { i2 = tableDict->iCodeLength; i1 = 1; } else { i2 = iLen; i1 = 0; } for (i = 0; i < tableDict->iCodeLength - 1; i++) { if (tableDict->rule[i].iWords == i2 && tableDict->rule[i].iFlag == i1) break; } if (i == tableDict->iCodeLength - 1) return true; int codeIdx = 0; for (i1 = 0; i1 < tableDict->iCodeLength; i1++) { int clen; char* ps; if (tableDict->rule[i].rule[i1].iFlag) { ps = fcitx_utf8_get_nth_char(strHZ, tableDict->rule[i].rule[i1].iWhich - 1); clen = fcitx_utf8_char_len(ps); strncpy(strTemp, ps, clen); } else { ps = fcitx_utf8_get_nth_char(strHZ, iLen - tableDict->rule[i].rule[i1].iWhich); clen = fcitx_utf8_char_len(ps); strncpy(strTemp, ps, clen); } int hzIndex = CalHZIndex(strTemp); if (tableDict->tableSingleHZ[hzIndex]) { if (tableDict->tableSingleHZCons[hzIndex]) recTemp = tableDict->tableSingleHZCons[hzIndex]; else recTemp = tableDict->tableSingleHZ[hzIndex]; } else { bCanntFindCode = true; break; } if (strlen(recTemp->strCode) >= tableDict->rule[i].rule[i1].iIndex) { tableDict->strNewPhraseCode[codeIdx] = recTemp->strCode[tableDict->rule[i].rule[i1].iIndex - 1]; codeIdx++; } } return bCanntFindCode; }
void FcitxLibpinyinUpdatePreedit(FcitxLibpinyin* libpinyin, char* sentence) { FcitxInstance* instance = libpinyin->owner->owner; FcitxInputState* input = FcitxInstanceGetInputState(instance); int offset = LibpinyinGetOffset(libpinyin); if (libpinyin->type == LPT_Pinyin) { int libpinyinLen = strlen(libpinyin->inst->m_raw_full_pinyin); int fcitxLen = strlen(libpinyin->buf); if (fcitxLen != libpinyinLen) { strcpy(libpinyin->buf, libpinyin->inst->m_raw_full_pinyin); libpinyin->cursor_pos += libpinyinLen - fcitxLen; } } int pyoffset = LibpinyinGetPinyinOffset(libpinyin); if (pyoffset > libpinyin->cursor_pos) libpinyin->cursor_pos = pyoffset; int hzlen = 0; if (fcitx_utf8_strlen(sentence) > offset) hzlen = fcitx_utf8_get_nth_char(sentence, offset) - sentence; else hzlen = strlen(sentence); if (hzlen > 0) { char* buf = (char*) fcitx_utils_malloc0((hzlen + 1) * sizeof(char)); strncpy(buf, sentence, hzlen); buf[hzlen] = 0; FcitxMessagesAddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_INPUT, "%s", buf); free(buf); } int charcurpos = hzlen; int lastpos = pyoffset; int curoffset = pyoffset; for (int i = offset; i < libpinyin->inst->m_pinyin_keys->len; i ++) { PinyinKey* pykey = &g_array_index(libpinyin->inst->m_pinyin_keys, PinyinKey, i); PinyinKeyPos* pykeypos = &g_array_index(libpinyin->inst->m_pinyin_key_rests, PinyinKeyPos, i); if (lastpos > 0) { FcitxMessagesMessageConcatLast (FcitxInputStateGetPreedit(input), " "); if (curoffset < libpinyin->cursor_pos) charcurpos ++; for (int j = lastpos; j < pykeypos->m_raw_begin; j ++) { char temp[2] = {'\0', '\0'}; temp[0] = libpinyin->buf[j]; FcitxMessagesMessageConcatLast (FcitxInputStateGetPreedit(input), temp); if (curoffset < libpinyin->cursor_pos) { curoffset ++; charcurpos ++; } } } lastpos = pykeypos->m_raw_end; switch (libpinyin->type) { case LPT_Pinyin: { gchar* pystring = pykey->get_pinyin_string(); FcitxMessagesAddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_CODE, "%s", pystring); size_t pylen = strlen(pystring); if (curoffset + pylen < libpinyin->cursor_pos) { curoffset += pylen; charcurpos += pylen; } else { charcurpos += libpinyin->cursor_pos - curoffset; curoffset = libpinyin->cursor_pos; } g_free(pystring); break; } case LPT_Shuangpin: { if (pykeypos->length() == 2) { const char* initial = 0; if (pykey->m_initial == CHEWING_ZERO_INITIAL) initial = "'"; else initial = get_initial_string(pykey); if (curoffset + 1 <= libpinyin->cursor_pos) { curoffset += 1; charcurpos += strlen(initial); } FcitxMessagesAddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_CODE, "%s", initial); if (curoffset + 1 <= libpinyin->cursor_pos) { curoffset += 1; charcurpos += strlen(get_middle_string(pykey)) + strlen(get_final_string(pykey)); } FcitxMessagesAddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_CODE, "%s%s", get_middle_string(pykey), get_final_string(pykey)); } else if (pykeypos->length() == 1) { gchar* pystring = pykey->get_pinyin_string(); if (curoffset + 1 <= libpinyin->cursor_pos) { curoffset += 1; charcurpos += strlen(pystring); } FcitxMessagesAddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_CODE, "%s", pystring); g_free(pystring); } break; } case LPT_Zhuyin: { gchar* pystring = pykey->get_chewing_string(); FcitxMessagesAddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_CODE, "%s", pystring); if (curoffset + pykeypos->length() <= libpinyin->cursor_pos) { curoffset += pykeypos->length(); charcurpos += strlen(pystring); } else { int diff = libpinyin->cursor_pos - curoffset; curoffset = libpinyin->cursor_pos; size_t len = fcitx_utf8_strlen(pystring); if (pykey->m_tone != CHEWING_ZERO_TONE) len --; if (diff > len) charcurpos += strlen(pystring); else { charcurpos += fcitx_utf8_get_nth_char(pystring, diff) - pystring; } } g_free(pystring); break; } } } int buflen = strlen(libpinyin->buf); if (lastpos < buflen) { FcitxMessagesMessageConcatLast (FcitxInputStateGetPreedit(input), " "); if (lastpos < libpinyin->cursor_pos) charcurpos ++; for (int i = lastpos; i < buflen; i ++) { char temp[2] = {'\0', '\0'}; temp[0] = libpinyin->buf[i]; FcitxMessagesMessageConcatLast (FcitxInputStateGetPreedit(input), temp); if (lastpos < libpinyin->cursor_pos) { charcurpos ++; lastpos++; } } } FcitxInputStateSetCursorPos(input, charcurpos); }