Add keyboard mappings for Bopomofo
authorBYVoid <byvoid1@gmail.com>
Thu, 20 May 2010 08:20:08 +0000 (16:20 +0800)
committerPeng Huang <shawn.p.huang@gmail.com>
Sun, 23 May 2010 00:04:40 +0000 (08:04 +0800)
scripts/genbopomofokeyboard.py [new file with mode: 0644]
src/BopomofoEditor.cc
src/BopomofoEditor.h
src/BopomofoKeyboard.h [new file with mode: 0644]

diff --git a/scripts/genbopomofokeyboard.py b/scripts/genbopomofokeyboard.py
new file mode 100644 (file)
index 0000000..14ba9d2
--- /dev/null
@@ -0,0 +1,100 @@
+# vim:set et sts=4:
+# -*- coding: utf-8 -*-
+
+bopomofo = [
+"BOPOMOFO_ZERO",
+"BOPOMOFO_B",
+"BOPOMOFO_P",
+"BOPOMOFO_M",
+"BOPOMOFO_F",
+"BOPOMOFO_D",
+"BOPOMOFO_T",
+"BOPOMOFO_N",
+"BOPOMOFO_L",
+"BOPOMOFO_G",
+"BOPOMOFO_K",
+"BOPOMOFO_H",
+"BOPOMOFO_J",
+"BOPOMOFO_Q",
+"BOPOMOFO_X",
+"BOPOMOFO_ZH",
+"BOPOMOFO_CH",
+"BOPOMOFO_SH",
+"BOPOMOFO_R",
+"BOPOMOFO_Z",
+"BOPOMOFO_C",
+"BOPOMOFO_S",
+"BOPOMOFO_I",
+"BOPOMOFO_U",
+"BOPOMOFO_V",
+"BOPOMOFO_A",
+"BOPOMOFO_O",
+"BOPOMOFO_E",
+"BOPOMOFO_E2",
+"BOPOMOFO_AI",
+"BOPOMOFO_EI",
+"BOPOMOFO_AO",
+"BOPOMOFO_OU",
+"BOPOMOFO_AN",
+"BOPOMOFO_EN",
+"BOPOMOFO_ANG",
+"BOPOMOFO_ENG",
+"BOPOMOFO_ER",
+"BOPOMOFO_TONE_2",
+"BOPOMOFO_TONE_3",
+"BOPOMOFO_TONE_4",
+"BOPOMOFO_TONE_5",
+]
+
+bopomofo_keyboard = (
+    #標準注音鍵盤
+    (
+    "1","q","a","z","2","w","s","x","e","d","c","r","f","v","5","t","g","b","y","h","n",
+    "u","j","m","8","i","k",",","9","o","l",".","0","p",";","/","-",
+    "6","3","4","7",
+    ),
+    #精業注音鍵盤
+    (
+    "2","w","s","x","3","e","d","c","r","f","v","t","g","b","6","y","h","n","u","j","m",
+    "-","[","'","8","i","k",",","9","o","l",".","0","p",";","/","=",
+    "q","a","z","1",
+    ),
+    #倚天注音鍵盤
+    (
+    "b","p","m","f","d","t","n","l","v","k","h","g","7","c",",",".","/","j",";","'","s",
+    "e","x","u","a","o","r","w","i","q","z","y","8","9","0","-","=",
+    "2","3","4","1",
+    ),
+    #IBM注音鍵盤
+    (
+    "1","2","3","4","5","6","7","8","9","0","-","q","w","e","r","t","y","u","i","o","p",
+    "a","s","d","f","g","h","j","k","l",";","z","x","c","v","b","n",
+    "m",",",".","/",
+    ),
+)
+
+def tochar(ch):
+    if ch == "'" or ch == "\\":
+        ch = "\\" + ch;
+    return "'" + ch + "'"
+
+def gen_table():
+    i = 0
+    print 'static const gint'
+    print 'bopomofo_keyboard[][41][2] = {'
+    for keyboard in bopomofo_keyboard:
+        print '    {'
+        items = []
+        i=1
+        for v in keyboard:
+            items.append ((v,bopomofo[i]));
+            i += 1
+        items.sort()
+        for k,v in items:
+            print '        {%4s, %15s },' % (tochar(k),v)
+        print '    },'
+    print '};'
+    print
+
+if __name__ == "__main__":
+    gen_table()
index b7615b2..ef292fc 100644 (file)
@@ -35,6 +35,9 @@
 
 namespace PY {
 
+#include "Bopomofo.h"
+#include "BopomofoKeyboard.h"
+
 BopomofoEditor::BopomofoEditor (PinyinProperties & props)
     : PinyinEditor (props),
       m_select_mode (FALSE)
@@ -582,4 +585,27 @@ BopomofoEditor::updatePreeditText (void)
     Editor::updatePreeditText (preedit_text, edit_begin, TRUE);
 }
 
+static int
+keyboard_cmp (const void *p1, const void *p2)
+{
+    const gint s1 = (const gint) p1;
+    const gint *s2 = (const gint *) p2;
+    return s1 - s2[0];
+}
+
+gint
+BopomofoEditor::keyvalToBopomofo(gint ch)
+{
+    const gint keyboard = 0; /* TODO: setup */
+    const gint *brs;
+    brs = (const gint *) std::bsearch ((void *) ch,
+                                       bopomofo_keyboard[keyboard],
+                                       G_N_ELEMENTS (bopomofo_keyboard[keyboard]),
+                                       sizeof(bopomofo_keyboard[keyboard][0]),
+                                       keyboard_cmp);
+    if (G_UNLIKELY (brs == NULL))
+        return BOPOMOFO_ZERO;
+    return brs[1];
+}
+
 };
index 870f9e0..ce87322 100644 (file)
@@ -26,8 +26,6 @@
 
 namespace PY {
 
-#include "Bopomofo.h"
-
 class BopomofoEditor : public PinyinEditor {
 
 public:
@@ -66,57 +64,7 @@ protected:
     gboolean processNumber (guint keyval, guint keycode, guint modifiers);
     gboolean processNumberWithShift (guint keyval, guint keycode, guint modifiers);
     gboolean processBopomofo (guint keyval, guint keycode, guint modifiers);
-
-    gint keyvalToBopomofo(gint ch) {
-        switch(ch){
-        case IBUS_1: return BOPOMOFO_B;
-        case IBUS_q: return BOPOMOFO_P;
-        case IBUS_a: return BOPOMOFO_M;
-        case IBUS_z: return BOPOMOFO_F;
-        case IBUS_2: return BOPOMOFO_D;
-        case IBUS_w: return BOPOMOFO_T;
-        case IBUS_s: return BOPOMOFO_N;
-        case IBUS_x: return BOPOMOFO_L;
-        case IBUS_e: return BOPOMOFO_G;
-        case IBUS_d: return BOPOMOFO_K;
-        case IBUS_c: return BOPOMOFO_H;
-        case IBUS_r: return BOPOMOFO_J;
-        case IBUS_f: return BOPOMOFO_Q;
-        case IBUS_v: return BOPOMOFO_X;
-        case IBUS_5: return BOPOMOFO_ZH;
-        case IBUS_t: return BOPOMOFO_CH;
-        case IBUS_g: return BOPOMOFO_SH;
-        case IBUS_b: return BOPOMOFO_R;
-        case IBUS_y: return BOPOMOFO_Z;
-        case IBUS_h: return BOPOMOFO_C;
-        case IBUS_n: return BOPOMOFO_S;
-
-        case IBUS_u: return BOPOMOFO_I;
-        case IBUS_j: return BOPOMOFO_U;
-        case IBUS_m: return BOPOMOFO_V;
-        case IBUS_8: return BOPOMOFO_A;
-        case IBUS_i: return BOPOMOFO_O;
-        case IBUS_k: return BOPOMOFO_E;
-        case IBUS_comma: return BOPOMOFO_E2;
-        case IBUS_9: return BOPOMOFO_AI;
-        case IBUS_o: return BOPOMOFO_EI;
-        case IBUS_l: return BOPOMOFO_AO;
-        case IBUS_period: return BOPOMOFO_OU;
-        case IBUS_0: return BOPOMOFO_AN;
-        case IBUS_p: return BOPOMOFO_EN;
-        case IBUS_semicolon: return BOPOMOFO_ANG;
-        case IBUS_slash: return BOPOMOFO_ENG;
-        case IBUS_minus: return BOPOMOFO_ER;
-
-        case IBUS_3: return BOPOMOFO_TONE_2;
-        case IBUS_4: return BOPOMOFO_TONE_3;
-        case IBUS_6: return BOPOMOFO_TONE_4;
-        case IBUS_7: return BOPOMOFO_TONE_5;
-
-        default:
-            return 0;
-        }
-    }
+    gint keyvalToBopomofo(gint ch);
 
 
 };
diff --git a/src/BopomofoKeyboard.h b/src/BopomofoKeyboard.h
new file mode 100644 (file)
index 0000000..dce5379
--- /dev/null
@@ -0,0 +1,204 @@
+/* vim:set et ts=4 sts=4:
+ *
+ * ibus-pinyin - The Chinese PinYin engine for IBus
+ *
+ * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
+ * Copyright (c) 2010 BYVoid <byvoid1@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#ifndef __PY_BOPOMOFO_KEYBOARD_H_
+#define __PY_BOPOMOFO_KEYBOARD_H_
+
+#include "Bopomofo.h"
+
+static const gint
+bopomofo_keyboard[][41][2] = {
+    {
+        { ',',     BOPOMOFO_E2 },
+        { '-',     BOPOMOFO_ER },
+        { '.',     BOPOMOFO_OU },
+        { '/',    BOPOMOFO_ENG },
+        { '0',     BOPOMOFO_AN },
+        { '1',      BOPOMOFO_B },
+        { '2',      BOPOMOFO_D },
+        { '3', BOPOMOFO_TONE_3 },
+        { '4', BOPOMOFO_TONE_4 },
+        { '5',     BOPOMOFO_ZH },
+        { '6', BOPOMOFO_TONE_2 },
+        { '7', BOPOMOFO_TONE_5 },
+        { '8',      BOPOMOFO_A },
+        { '9',     BOPOMOFO_AI },
+        { ';',    BOPOMOFO_ANG },
+        { 'a',      BOPOMOFO_M },
+        { 'b',      BOPOMOFO_R },
+        { 'c',      BOPOMOFO_H },
+        { 'd',      BOPOMOFO_K },
+        { 'e',      BOPOMOFO_G },
+        { 'f',      BOPOMOFO_Q },
+        { 'g',     BOPOMOFO_SH },
+        { 'h',      BOPOMOFO_C },
+        { 'i',      BOPOMOFO_O },
+        { 'j',      BOPOMOFO_U },
+        { 'k',      BOPOMOFO_E },
+        { 'l',     BOPOMOFO_AO },
+        { 'm',      BOPOMOFO_V },
+        { 'n',      BOPOMOFO_S },
+        { 'o',     BOPOMOFO_EI },
+        { 'p',     BOPOMOFO_EN },
+        { 'q',      BOPOMOFO_P },
+        { 'r',      BOPOMOFO_J },
+        { 's',      BOPOMOFO_N },
+        { 't',     BOPOMOFO_CH },
+        { 'u',      BOPOMOFO_I },
+        { 'v',      BOPOMOFO_X },
+        { 'w',      BOPOMOFO_T },
+        { 'x',      BOPOMOFO_L },
+        { 'y',      BOPOMOFO_Z },
+        { 'z',      BOPOMOFO_F },
+    },
+    {
+        {'\'',      BOPOMOFO_V },
+        { ',',     BOPOMOFO_E2 },
+        { '-',      BOPOMOFO_I },
+        { '.',     BOPOMOFO_OU },
+        { '/',    BOPOMOFO_ENG },
+        { '0',     BOPOMOFO_AN },
+        { '1', BOPOMOFO_TONE_5 },
+        { '2',      BOPOMOFO_B },
+        { '3',      BOPOMOFO_D },
+        { '6',     BOPOMOFO_ZH },
+        { '8',      BOPOMOFO_A },
+        { '9',     BOPOMOFO_AI },
+        { ';',    BOPOMOFO_ANG },
+        { '=',     BOPOMOFO_ER },
+        { '[',      BOPOMOFO_U },
+        { 'a', BOPOMOFO_TONE_3 },
+        { 'b',      BOPOMOFO_X },
+        { 'c',      BOPOMOFO_L },
+        { 'd',      BOPOMOFO_N },
+        { 'e',      BOPOMOFO_T },
+        { 'f',      BOPOMOFO_K },
+        { 'g',      BOPOMOFO_Q },
+        { 'h',     BOPOMOFO_SH },
+        { 'i',      BOPOMOFO_O },
+        { 'j',      BOPOMOFO_C },
+        { 'k',      BOPOMOFO_E },
+        { 'l',     BOPOMOFO_AO },
+        { 'm',      BOPOMOFO_S },
+        { 'n',      BOPOMOFO_R },
+        { 'o',     BOPOMOFO_EI },
+        { 'p',     BOPOMOFO_EN },
+        { 'q', BOPOMOFO_TONE_2 },
+        { 'r',      BOPOMOFO_G },
+        { 's',      BOPOMOFO_M },
+        { 't',      BOPOMOFO_J },
+        { 'u',      BOPOMOFO_Z },
+        { 'v',      BOPOMOFO_H },
+        { 'w',      BOPOMOFO_P },
+        { 'x',      BOPOMOFO_F },
+        { 'y',     BOPOMOFO_CH },
+        { 'z', BOPOMOFO_TONE_4 },
+    },
+    {
+        {'\'',      BOPOMOFO_C },
+        { ',',     BOPOMOFO_ZH },
+        { '-',    BOPOMOFO_ENG },
+        { '.',     BOPOMOFO_CH },
+        { '/',     BOPOMOFO_SH },
+        { '0',    BOPOMOFO_ANG },
+        { '1', BOPOMOFO_TONE_5 },
+        { '2', BOPOMOFO_TONE_2 },
+        { '3', BOPOMOFO_TONE_3 },
+        { '4', BOPOMOFO_TONE_4 },
+        { '7',      BOPOMOFO_Q },
+        { '8',     BOPOMOFO_AN },
+        { '9',     BOPOMOFO_EN },
+        { ';',      BOPOMOFO_Z },
+        { '=',     BOPOMOFO_ER },
+        { 'a',      BOPOMOFO_A },
+        { 'b',      BOPOMOFO_B },
+        { 'c',      BOPOMOFO_X },
+        { 'd',      BOPOMOFO_D },
+        { 'e',      BOPOMOFO_I },
+        { 'f',      BOPOMOFO_F },
+        { 'g',      BOPOMOFO_J },
+        { 'h',      BOPOMOFO_H },
+        { 'i',     BOPOMOFO_AI },
+        { 'j',      BOPOMOFO_R },
+        { 'k',      BOPOMOFO_K },
+        { 'l',      BOPOMOFO_L },
+        { 'm',      BOPOMOFO_M },
+        { 'n',      BOPOMOFO_N },
+        { 'o',      BOPOMOFO_O },
+        { 'p',      BOPOMOFO_P },
+        { 'q',     BOPOMOFO_EI },
+        { 'r',      BOPOMOFO_E },
+        { 's',      BOPOMOFO_S },
+        { 't',      BOPOMOFO_T },
+        { 'u',      BOPOMOFO_V },
+        { 'v',      BOPOMOFO_G },
+        { 'w',     BOPOMOFO_E2 },
+        { 'x',      BOPOMOFO_U },
+        { 'y',     BOPOMOFO_OU },
+        { 'z',     BOPOMOFO_AO },
+    },
+    {
+        { ',', BOPOMOFO_TONE_3 },
+        { '-',      BOPOMOFO_H },
+        { '.', BOPOMOFO_TONE_4 },
+        { '/', BOPOMOFO_TONE_5 },
+        { '0',      BOPOMOFO_K },
+        { '1',      BOPOMOFO_B },
+        { '2',      BOPOMOFO_P },
+        { '3',      BOPOMOFO_M },
+        { '4',      BOPOMOFO_F },
+        { '5',      BOPOMOFO_D },
+        { '6',      BOPOMOFO_T },
+        { '7',      BOPOMOFO_N },
+        { '8',      BOPOMOFO_L },
+        { '9',      BOPOMOFO_G },
+        { ';',     BOPOMOFO_AO },
+        { 'a',      BOPOMOFO_I },
+        { 'b',    BOPOMOFO_ENG },
+        { 'c',     BOPOMOFO_EN },
+        { 'd',      BOPOMOFO_V },
+        { 'e',      BOPOMOFO_X },
+        { 'f',      BOPOMOFO_A },
+        { 'g',      BOPOMOFO_O },
+        { 'h',      BOPOMOFO_E },
+        { 'i',      BOPOMOFO_Z },
+        { 'j',     BOPOMOFO_E2 },
+        { 'k',     BOPOMOFO_AI },
+        { 'l',     BOPOMOFO_EI },
+        { 'm', BOPOMOFO_TONE_2 },
+        { 'n',     BOPOMOFO_ER },
+        { 'o',      BOPOMOFO_C },
+        { 'p',      BOPOMOFO_S },
+        { 'q',      BOPOMOFO_J },
+        { 'r',     BOPOMOFO_ZH },
+        { 's',      BOPOMOFO_U },
+        { 't',     BOPOMOFO_CH },
+        { 'u',      BOPOMOFO_R },
+        { 'v',    BOPOMOFO_ANG },
+        { 'w',      BOPOMOFO_Q },
+        { 'x',     BOPOMOFO_AN },
+        { 'y',     BOPOMOFO_SH },
+        { 'z',     BOPOMOFO_OU },
+    },
+};
+
+
+#endif /* __PY_BOPOMOFO_KEYBOARD_H_ */