Exemplo n.º 1
0
error_t	editor_set_nchar_max(t_HWidget handle, int nchars)
{
	error_t ret = EBADPARM;
	if(g_object_valid(handle)){
		t_CEditor *pme = (t_CEditor *)handle;
		ucs2_t *newbuf;
		if(nchars <= 0)
			nchars = NCHARS_MAX_DEFAULT;
		newbuf = MALLOC((nchars+1) * sizeof(ucs2_t));
		if(pme->nchars > 0 && pme->buffer){
			if(pme->nchars > nchars){
				pme->nchars = nchars;
				pme->utf8_bytes = ustring_to_utf8(pme->buffer, pme->nchars, NULL, -1);
			}
			memcpy(newbuf, pme->buffer, pme->nchars*sizeof(ucs2_t));
		}else{
			pme->nchars = 0;
		}
		FREEIF(pme->buffer);
		pme->nchars_max = nchars;
		pme->buffer = newbuf;
		pme->cursor = pme->nchars;
		pme->buffer[pme->nchars] = 0;
		ret = SUCCESS;
	}
	return ret;
}
Exemplo n.º 2
0
static gchar *
transliterator_icu_real_transliterate (TranslitTransliterator *self,
                                       const gchar            *input,
                                       guint                  *endpos,
                                       GError                **error)
{
  TransliteratorIcu *icu = TRANSLITERATOR_ICU (self);
  gchar *output;
  gint n_filtered = 0;
  UChar *ustr;
  int32_t ustrLength, ustrCapacity, limit;
  int32_t inputUstrLength;
  UErrorCode errorCode;

  ustr = ustring_from_utf8 (input, &ustrLength);
  inputUstrLength = ustrLength;
  limit = ustrLength;
  ustrCapacity = ustrLength + 1;

  do
    {
      errorCode = 0;

      /* We can't use utrans_transIncrementalUChars here, since the
       * output is sometimes unacceptable.
       *
       * For example, with the "Latin-Katakana" transliterator,
       * "kakikukeko" does not turn into Japanese characters until one
       * more vovel character follows.
       */
      utrans_transUChars (icu->trans,
			  ustr, &ustrLength, ustrCapacity,
			  0, &limit,
			  &errorCode);
      if (errorCode == U_BUFFER_OVERFLOW_ERROR)
	{
	  ustrCapacity = ustrLength + 1;
	  ustr = g_realloc (ustr, ustrCapacity);
	  ustrLength = inputUstrLength;
	  limit = inputUstrLength;
	}
    }
  while (errorCode == U_BUFFER_OVERFLOW_ERROR);

  if (errorCode != U_ZERO_ERROR)
    {
      g_free (ustr);
      g_set_error (error,
		   TRANSLIT_ERROR,
		   TRANSLIT_ERROR_FAILED,
		   "failed to transliterate: %s", u_errorName (errorCode));
      return NULL;
    }

  output = ustring_to_utf8 (ustr, ustrLength);
  g_free (ustr);

  if (endpos)
    *endpos = inputUstrLength;

  return output;
}
Exemplo n.º 3
0
static void
ibus_hangul_engine_update_hanja_list (IBusHangulEngine *hangul)
{
    gchar* hanja_key;
    gchar* preedit_utf8;
    const ucschar* hic_preedit;
    UString* preedit;
    int lookup_method;
    IBusText* ibus_text = NULL;
    guint cursor_pos = 0;
    guint anchor_pos = 0;

    if (hangul->hanja_list != NULL) {
        hanja_list_delete (hangul->hanja_list);
        hangul->hanja_list = NULL;
    }

    hic_preedit = hangul_ic_get_preedit_string (hangul->context);

    hanja_key = NULL;
    lookup_method = LOOKUP_METHOD_PREFIX;

    preedit = ustring_dup (hangul->preedit);
    ustring_append_ucs4 (preedit, hic_preedit, -1);

    if (ustring_length(preedit) > 0) {
        preedit_utf8 = ustring_to_utf8 (preedit, -1);
        if (word_commit || hangul->hanja_mode) {
            hanja_key = preedit_utf8;
            lookup_method = LOOKUP_METHOD_PREFIX;
        } else {
            gchar* substr;
            ibus_engine_get_surrounding_text ((IBusEngine *)hangul, &ibus_text,
                    &cursor_pos, &anchor_pos);

            substr = h_ibus_text_get_substring (ibus_text,
                    cursor_pos - 64, cursor_pos);

            if (substr != NULL) {
                hanja_key = g_strconcat (substr, preedit_utf8, NULL);
                g_free (preedit_utf8);
            } else {
                hanja_key = preedit_utf8;
            }
            lookup_method = LOOKUP_METHOD_SUFFIX;
        }
    } else {
        ibus_engine_get_surrounding_text ((IBusEngine *)hangul, &ibus_text,
                &cursor_pos, &anchor_pos);
        if (cursor_pos != anchor_pos) {
            // If we have selection in surrounding text, we use that.
            hanja_key = h_ibus_text_get_substring (ibus_text,
                    cursor_pos, anchor_pos);
            lookup_method = LOOKUP_METHOD_EXACT;
        } else {
            hanja_key = h_ibus_text_get_substring (ibus_text,
                    cursor_pos - 64, cursor_pos);
            lookup_method = LOOKUP_METHOD_SUFFIX;
        }
    }

    if (hanja_key != NULL) {
        hangul->hanja_list = ibus_hangul_engine_lookup_hanja_table (hanja_key,
                lookup_method);
        hangul->last_lookup_method = lookup_method;
        g_free (hanja_key);
    }

    ustring_delete (preedit);

    if (ibus_text != NULL)
        g_object_unref (ibus_text);
}