static gunichar lx_tn_engine_get_prev_surrounding_char (LxTNEngine *lx_tn_engine, IBusEngine *ibus_engine) { if (is_client_support_surrounding (ibus_engine)) { IBusText *surrounding; guint cursor_pos; guint anchor_pos; gunichar *u_surrounding; gunichar ret = 0; ibus_engine_get_surrounding_text (ibus_engine, &surrounding, &cursor_pos, &anchor_pos); u_surrounding = g_utf8_to_ucs4 (ibus_text_get_text (surrounding), -1, NULL, NULL, NULL); if (u_surrounding) { ret = (cursor_pos > 0) ? u_surrounding[cursor_pos - 1] : 0; g_free (u_surrounding); } return ret; } return 0; }
static gboolean lx_tn_engine_convert_seq (LxTNEngine *lx_tn_engine, IBusEngine *ibus_engine, gunichar input_char) { if (is_client_support_surrounding (ibus_engine)) { IBusText *surrounding; guint cursor_pos; guint anchor_pos; LxTNConv conv; ibus_engine_get_surrounding_text (ibus_engine, &surrounding, &cursor_pos, &anchor_pos); if (lx_tn_im_conversion (ibus_text_get_text (surrounding), cursor_pos, anchor_pos, input_char, &conv)) { IBusText *text; ibus_engine_delete_surrounding_text (ibus_engine, conv.del_offset, -conv.del_offset); text = ibus_text_new_from_static_string (conv.commit_text); ibus_engine_commit_text (ibus_engine, text); return TRUE; } } return FALSE; }
static gchar* h_ibus_text_get_substring (IBusText* ibus_text, glong p1, glong p2) { const gchar* text; const gchar* begin; const gchar* end; gchar* substring; glong limit; glong pos; glong n; text = ibus_text_get_text (ibus_text); limit = ibus_text_get_length (ibus_text) + 1; if (text == NULL || limit == 0) return NULL; p1 = MAX(0, p1); p2 = MAX(0, p2); pos = MIN(p1, p2); n = ABS(p2 - p1); if (pos + n > limit) n = limit - pos; begin = g_utf8_offset_to_pointer (text, pos); end = g_utf8_offset_to_pointer (begin, n); substring = g_strndup (begin, end - begin); return substring; }
static void FcitxThaiGetPrevCell(FcitxThai* thai, struct thcell_t* res) { th_init_cell(res); if (is_client_support_surrounding(IBUS_ENGINE(libthai_engine))) { IBusText* surrounding; guint cursor_pos; guint anchor_pos; const gchar* s; gchar* tis_text = NULL; ibus_engine_get_surrounding_text(IBUS_ENGINE(libthai_engine), &surrounding, &cursor_pos, &anchor_pos); s = ibus_text_get_text(surrounding); cursor_pos = g_utf8_offset_to_pointer(s, cursor_pos) - s; while (*s) { const gchar* t; tis_text = g_convert(s, cursor_pos, "TIS-620", "UTF-8", NULL, NULL, NULL); if (tis_text) break; t = g_utf8_next_char(s); cursor_pos -= (t - s); s = t; } if (tis_text) { gint char_index; char_index = g_utf8_pointer_to_offset(s, s + cursor_pos); th_prev_cell((thchar_t*) tis_text, char_index, res, TRUE); g_free(tis_text); } } else { /* retrieve from the fallback buffer */ th_prev_cell(libthai_engine->char_buff, libthai_engine->buff_tail, res, TRUE); } }