コード例 #1
0
ファイル: transliteratorm17n.c プロジェクト: ueno/libtranslit
static char *
mtext_to_utf8 (MText *mt)
{
  char *buf;
  size_t cap;

  mconv_reset_converter (utf8_converter);
  cap = (mtext_len (mt) + 1) * 6;
  buf = (char *) malloc (cap);
  mconv_rebind_buffer (utf8_converter, buf, cap);
  mconv_encode (utf8_converter, mt);

  buf[utf8_converter->nbytes] = '\0';

  return buf;
}
コード例 #2
0
ファイル: nimf-m17n.c プロジェクト: cogniti/nimf
static gchar *
nimf_m17n_mtext_to_utf8 (NimfM17n *m17n, MText *mt)
{
  g_debug (G_STRLOC ": %s", G_STRFUNC);

  g_return_val_if_fail (mt != NULL, NULL);

  gint   buf_len = (mtext_len (mt) + 1) * 6;
  gchar *buf = g_malloc0 (buf_len);

  mconv_reset_converter (m17n->converter);
  mconv_rebind_buffer   (m17n->converter, (unsigned char *) buf, buf_len);
  mconv_encode (m17n->converter, mt);
  buf[m17n->converter->nbytes] = '\0';

  return buf;
}
コード例 #3
0
ファイル: engine.c プロジェクト: Nomad280279/ibus-m17n
static void
ibus_m17n_engine_update_preedit (IBusM17NEngine *m17n)
{
    IBusText *text;
    gchar *buf;
    IBusM17NEngineClass *klass = (IBusM17NEngineClass *) G_OBJECT_GET_CLASS (m17n);

    buf = ibus_m17n_mtext_to_utf8 (m17n->context->preedit);
    if (buf) {
        text = ibus_text_new_from_static_string (buf);
        if (klass->preedit_foreground != INVALID_COLOR)
            ibus_text_append_attribute (text, IBUS_ATTR_TYPE_FOREGROUND,
                                        klass->preedit_foreground, 0, -1);
        if (klass->preedit_background != INVALID_COLOR)
            ibus_text_append_attribute (text, IBUS_ATTR_TYPE_BACKGROUND,
                                        klass->preedit_background, 0, -1);
        ibus_text_append_attribute (text, IBUS_ATTR_TYPE_UNDERLINE,
                                    klass->preedit_underline, 0, -1);
        ibus_engine_update_preedit_text ((IBusEngine *) m17n,
                                         text,
                                         m17n->context->cursor_pos,
                                         mtext_len (m17n->context->preedit) > 0);
    }
}
コード例 #4
0
ファイル: nimf-m17n.c プロジェクト: cogniti/nimf
gboolean
nimf_m17n_filter_event (NimfEngine    *engine,
                        NimfServiceIC *target,
                        NimfEvent     *event)
{
  g_debug (G_STRLOC ": %s", G_STRFUNC);

  NimfM17n *m17n = NIMF_M17N (engine);

  g_return_val_if_fail (m17n->im != NULL, FALSE);

  if (event->key.type   == NIMF_EVENT_KEY_RELEASE ||
      event->key.keyval == NIMF_KEY_Shift_L       ||
      event->key.keyval == NIMF_KEY_Shift_R)
    return FALSE;

  nimf_service_im_target = target;
  guint keyval = event->key.keyval;
  gboolean move = FALSE;

  if (nimf_candidatable_is_visible (m17n->candidatable))
  {
    switch (keyval)
    {
      case NIMF_KEY_Up:
      case NIMF_KEY_KP_Up:
        keyval = NIMF_KEY_Left;
        break;
      case NIMF_KEY_Down:
      case NIMF_KEY_KP_Down:
        keyval = NIMF_KEY_Right;
        break;
      case NIMF_KEY_Left:
      case NIMF_KEY_KP_Left:
      case NIMF_KEY_Page_Up:
      case NIMF_KEY_KP_Page_Up:
        keyval = NIMF_KEY_Up;
        nimf_m17n_page_up (m17n);
        break;
      case NIMF_KEY_Right:
      case NIMF_KEY_KP_Right:
      case NIMF_KEY_Page_Down:
      case NIMF_KEY_KP_Page_Down:
        keyval = NIMF_KEY_Down;
        nimf_m17n_page_down (m17n);
        break;
      case NIMF_KEY_KP_0:
        keyval = NIMF_KEY_0;
        break;
      case NIMF_KEY_KP_1:
        keyval = NIMF_KEY_1;
        break;
      case NIMF_KEY_KP_2:
        keyval = NIMF_KEY_2;
        break;
      case NIMF_KEY_KP_3:
        keyval = NIMF_KEY_3;
        break;
      case NIMF_KEY_KP_4:
        keyval = NIMF_KEY_4;
        break;
      case NIMF_KEY_KP_5:
        keyval = NIMF_KEY_5;
        break;
      case NIMF_KEY_KP_6:
        keyval = NIMF_KEY_6;
        break;
      case NIMF_KEY_KP_7:
        keyval = NIMF_KEY_7;
        break;
      case NIMF_KEY_KP_8:
        keyval = NIMF_KEY_8;
        break;
      case NIMF_KEY_KP_9:
        keyval = NIMF_KEY_9;
        break;
      default:
        move = TRUE;
        break;
    }
  }

  const gchar *keysym_name;
  gboolean retval;

  keysym_name = nimf_keyval_to_keysym_name (keyval);
  MSymbol symbol;

  if (keysym_name)
  {
    GString *string;
    string = g_string_new ("");

    if (event->key.state & NIMF_HYPER_MASK)
      g_string_append (string, "H-");

    if (event->key.state & NIMF_SUPER_MASK)
      g_string_append (string, "s-");

    if (event->key.state & NIMF_MOD5_MASK)
      g_string_append (string, "G-");

    if (event->key.state & NIMF_MOD1_MASK)
      g_string_append (string, "A-");

    if (event->key.state & NIMF_META_MASK)
      g_string_append (string, "M-");

    if (event->key.state & NIMF_CONTROL_MASK)
      g_string_append (string, "C-");

    if (event->key.state & NIMF_SHIFT_MASK)
      g_string_append (string, "S-");

    g_string_append (string, keysym_name);
    symbol = msymbol (string->str);
    g_string_free (string, TRUE);
  }
  else
  {
    g_warning (G_STRLOC ": %s: keysym name not found", G_STRFUNC);
    symbol = Mnil;
  }

  retval = minput_filter (m17n->ic, symbol, NULL);

  if (!retval)
  {
    MText *produced;
    produced = mtext ();
    retval = !minput_lookup (m17n->ic, symbol, NULL, produced);

    if (mtext_len (produced) > 0)
    {
      gchar *buf;
      buf = nimf_m17n_mtext_to_utf8 (m17n, produced);

      if (m17n->converter->nbytes > 0)
        nimf_engine_emit_commit (engine, target, (const gchar *) buf);

      g_free (buf);
    }

    m17n_object_unref (produced);
  }

  if (m17n->ic->preedit_changed)
  {
    gchar *new_preedit = nimf_m17n_mtext_to_utf8 (m17n, m17n->ic->preedit);
    nimf_m17n_update_preedit (engine, target, new_preedit);
  }

  if (m17n->ic->status_changed)
  {
    gchar *status;
    status = nimf_m17n_mtext_to_utf8 (m17n, m17n->ic->status);

    if (status && strlen (status))
      g_debug ("Minput_status_draw: %s", status);

    g_free (status);
  }

  if (m17n->ic->candidate_list && m17n->ic->candidate_show)
  {
    nimf_m17n_update_candidate (engine, target);

    if (!nimf_candidatable_is_visible (m17n->candidatable))
      nimf_candidatable_show (m17n->candidatable, target, FALSE);
    else if (move)
      nimf_candidatable_show (m17n->candidatable, target, FALSE);
  }
  else
  {
    nimf_candidatable_hide (m17n->candidatable);
  }

  nimf_service_im_target = NULL;

  return retval;
}
コード例 #5
0
ファイル: engine.c プロジェクト: Nomad280279/ibus-m17n
static void
ibus_m17n_engine_update_lookup_table (IBusM17NEngine *m17n)
{
    ibus_lookup_table_clear (m17n->table);

    if (m17n->context->candidate_list && m17n->context->candidate_show) {
        IBusText *text;
        MPlist *group;
        group = m17n->context->candidate_list;
        gint i = 0;
        gint page = 1;
        IBusM17NEngineClass *klass = (IBusM17NEngineClass *) G_OBJECT_GET_CLASS (m17n);

        while (1) {
            gint len;
            if (mplist_key (group) == Mtext)
                len = mtext_len ((MText *) mplist_value (group));
            else
                len = mplist_length ((MPlist *) mplist_value (group));

            if (i + len > m17n->context->candidate_index)
                break;

            i += len;
            group = mplist_next (group);
            page ++;
        }

        if (mplist_key (group) == Mtext) {
            MText *mt;
            gunichar *buf;
            glong nchars, i;

            mt = (MText *) mplist_value (group);
            ibus_lookup_table_set_page_size (m17n->table, mtext_len (mt));

            buf = ibus_m17n_mtext_to_ucs4 (mt, &nchars);
            for (i = 0; i < nchars; i++) {
                ibus_lookup_table_append_candidate (m17n->table, ibus_text_new_from_unichar (buf[i]));
            }
            g_free (buf);
        }
        else {
            MPlist *p;

            p = (MPlist *) mplist_value (group);
            ibus_lookup_table_set_page_size (m17n->table, mplist_length (p));

            for (; mplist_key (p) != Mnil; p = mplist_next (p)) {
                MText *mtext;
                gchar *buf;

                mtext = (MText *) mplist_value (p);
                buf = ibus_m17n_mtext_to_utf8 (mtext);
                if (buf) {
                    ibus_lookup_table_append_candidate (m17n->table, ibus_text_new_from_string (buf));
                    g_free (buf);
                }
            }
        }

        ibus_lookup_table_set_cursor_pos (m17n->table, m17n->context->candidate_index - i);
        ibus_lookup_table_set_orientation (m17n->table, klass->lookup_table_orientation);

        text = ibus_text_new_from_printf ("( %d / %d )", page, mplist_length (m17n->context->candidate_list));

        ibus_engine_update_lookup_table ((IBusEngine *)m17n, m17n->table, TRUE);
        ibus_engine_update_auxiliary_text ((IBusEngine *)m17n, text, TRUE);
    }
    else {
        ibus_engine_hide_lookup_table ((IBusEngine *)m17n);
        ibus_engine_hide_auxiliary_text ((IBusEngine *)m17n);
    }
}
コード例 #6
0
ファイル: transliteratorm17n.c プロジェクト: ueno/libtranslit
static gchar *
transliterator_m17n_real_transliterate (TranslitTransliterator *self,
                                        const gchar            *input,
                                        guint                  *endpos,
                                        GError                **error)
{
  TransliteratorM17n *m17n = TRANSLITERATOR_M17N (self);
  const gchar *p;
  GString *string;
  gchar *output;
  gint n_filtered = 0;

  string = g_string_sized_new (strlen (input));
  minput_reset_ic (m17n->ic);
  for (p = input; ; p = g_utf8_next_char (p))
    {
      gunichar uc = g_utf8_get_char (p);
      MSymbol symbol;
      gint retval;

      if (*p == '\0')
	symbol = Mnil;
      else
	{
	  gint length;
	  gchar *utf8;

	  length = g_unichar_to_utf8 (uc, NULL);
	  utf8 = g_slice_alloc0 (length + 1);
	  g_unichar_to_utf8 (uc, utf8);
	  symbol = msymbol (utf8);
	  g_slice_free1 (length, utf8);
	}

      retval = minput_filter (m17n->ic, symbol, NULL);
      if (retval == 0)
	{
	  MText *mt = mtext ();

	  retval = minput_lookup (m17n->ic, symbol, NULL, mt);

	  if (mtext_len (mt) > 0) {
	    output = mtext_to_utf8 (mt);
	    g_string_append (string, output);
	    g_free (output);
	  }

	  if (retval)
	    g_string_append_unichar (string, uc);

	  m17n_object_unref (mt);
	  n_filtered = 0;
	}
      else
	n_filtered++;

      if (symbol == Mnil)
	break;
    }

  if (endpos)
    *endpos = g_utf8_strlen (input, -1) - n_filtered;

  return g_string_free (string, FALSE);
}