Bopomofo
authorBYVoid <byvoid1@gmail.com>
Thu, 13 May 2010 09:41:57 +0000 (17:41 +0800)
committerBYVoid <byvoid1@gmail.com>
Thu, 13 May 2010 09:41:57 +0000 (17:41 +0800)
src/BopomofoEditor.cc
src/BopomofoEditor.h
src/PinyinEditor.cc
src/PinyinEngine.cc
src/PinyinParser.cc
src/PinyinParser.h

index af5757e..c5432aa 100644 (file)
@@ -26,11 +26,11 @@ BopomofoEditor::insert (gint ch)
     if (G_UNLIKELY (m_text.length () >= MAX_PINYIN_LEN))
         return TRUE;
 
-    gint key = get_bopomofo_keyboard_map(ch);
+    gint key = keyvalToBopomofo(ch);
     if (key >= BOPOMOFO_TONE_2 && key <= BOPOMOFO_TONE_5) {
         if (m_cursor == 0)
             return TRUE;  /* invalid format: tone should not be the first character */
-        key = get_bopomofo_keyboard_map(m_text.c_str()[m_cursor - 1]);
+        key = keyvalToBopomofo(m_text.c_str()[m_cursor - 1]);
         if (key >= BOPOMOFO_TONE_2 && key <= BOPOMOFO_TONE_5)
             return TRUE;  /* invalid format: two tone character should not be together  */
     }
@@ -260,10 +260,10 @@ BopomofoEditor::updatePinyin (void)
     else {
         bopomofo.clear();
         for(String::iterator i = m_text.begin();i != m_text.end(); ++i) {
-            bopomofo += bopomofo_char[get_bopomofo_keyboard_map(*i)];
+            bopomofo += bopomofo_char[keyvalToBopomofo(*i)];
         }
 
-        m_pinyin_len = PinyinParser::parse_bopomofo(bopomofo,            // bopomofo
+        m_pinyin_len = PinyinParser::parseBopomofo(bopomofo,            // bopomofo
                                                     m_cursor,            // text length
                                                     Config::option (),   // option
                                                     m_pinyin,            // result
@@ -290,7 +290,7 @@ BopomofoEditor::updateAuxiliaryText (void)
     for (String::iterator i = m_text.begin();i!=m_text.end();i++) {
         if (m_cursor == i - m_text.begin())
             m_buffer << '|';
-        m_buffer.appendUnichar(bopomofo_char[get_bopomofo_keyboard_map(*i)]);
+        m_buffer.appendUnichar(bopomofo_char[keyvalToBopomofo(*i)]);
     }
     if (m_cursor == m_text.length())
         m_buffer << '|';
@@ -322,7 +322,7 @@ BopomofoEditor::commit (void)
     }
 
     while (*p != '\0') {
-        m_buffer.appendUnichar ((gunichar)bopomofo_char[get_bopomofo_keyboard_map(*p++)]);
+        m_buffer.appendUnichar ((gunichar)bopomofo_char[keyvalToBopomofo(*p++)]);
     }
 
     m_phrase_editor.commit ();
@@ -333,8 +333,6 @@ BopomofoEditor::commit (void)
 void
 BopomofoEditor::updatePreeditText (void)
 {
-    PinyinEditor::updatePreeditText();
-#if 0
     /* preedit text = selected phrases + highlight candidate + rest text */
     if (G_UNLIKELY (m_phrase_editor.empty () && m_text.empty ())) {
         hidePreeditText ();
@@ -379,22 +377,26 @@ BopomofoEditor::updatePreeditText (void)
                     edit_end = m_buffer.utf8Length ();
 
                     /* append rest text */
-                    m_buffer << textAfterPinyin (edit_end);
+                    if (m_cursor >= MAX_PHRASE_LEN) {
+                        for (const gchar *p=m_text.c_str() + MAX_PHRASE_LEN; *p ;++p) {
+                            m_buffer.appendUnichar(bopomofo_char[keyvalToBopomofo(*p)]);
+                        }
+                    }
                 }
                 else {
-                    guint candidate_end = edit_begin + candidate.len;
-                    m_buffer << m_pinyin[edit_begin]->sheng << m_pinyin[edit_begin]->yun;
-
-                    for (guint i = edit_begin + 1; i < candidate_end; i++) {
-                        m_buffer << ' ' << m_pinyin[i]->sheng << m_pinyin[i]->yun;
+                    for (const gchar *p=m_text.c_str(); *p ;++p) {
+                        if (p - m_text.c_str() == m_cursor)
+                            m_buffer << ' ';
+                        m_buffer.appendUnichar(bopomofo_char[keyvalToBopomofo(*p)]);
                     }
-                    m_buffer << ' ' << textAfterPinyin (candidate_end);
                     edit_end = m_buffer.utf8Length ();
                 }
             }
         }
         else {
-            m_buffer << textAfterPinyin ();
+            for (const gchar *p=m_text.c_str() + m_pinyin_len; *p ;++p) {
+                m_buffer.appendUnichar(bopomofo_char[keyvalToBopomofo(*p)]);
+            }
         }
     }
 
@@ -410,7 +412,6 @@ BopomofoEditor::updatePreeditText (void)
                                         edit_begin, edit_end);
     }
     Editor::updatePreeditText (preedit_text, edit_begin, TRUE);
-#endif
 }
 
 };
index 9516233..2ff9838 100644 (file)
@@ -42,7 +42,7 @@ protected:
 
     gboolean processBopomofo (guint keyval, guint keycode, guint modifiers);
 
-    gint get_bopomofo_keyboard_map(gint ch) {
+    gint keyvalToBopomofo(gint ch) {
         switch(ch){
         case IBUS_1: return BOPOMOFO_B;
         case IBUS_q: return BOPOMOFO_P;
index 07fd16d..bef4112 100644 (file)
@@ -400,7 +400,7 @@ PinyinEditor::updatePreeditText (void)
                     for (guint i = edit_begin + 1; i < candidate_end; i++) {
                         m_buffer << ' ' << m_pinyin[i]->sheng << m_pinyin[i]->yun;
                     }
-                    m_buffer << ' ' << textAfterPinyin (candidate_end);
+                    m_buffer << '|' << textAfterPinyin (candidate_end);
                     edit_end = m_buffer.utf8Length ();
                 }
             }
index d279e66..ec84bb5 100644 (file)
@@ -10,7 +10,6 @@
 #include "BopomofoEditor.h"
 #include "PinyinEngine.h"
 #include "HalfFullConverter.h"
-#include "SimpTradConverter.h"
 #include "Config.h"
 #include "Text.h"
 #include "Util.h"
index fe8e31b..e2c4f97 100644 (file)
@@ -280,7 +280,7 @@ PinyinParser::isBopomofoToneChar (const wchar_t ch)
 }
 
 guint
-PinyinParser::parse_bopomofo (const std::wstring   &bopomofo,
+PinyinParser::parseBopomofo (const std::wstring   &bopomofo,
                              gint            len,
                              guint           option,
                              PinyinArray    &result,
@@ -320,6 +320,8 @@ PinyinParser::parse_bopomofo (const std::wstring   &bopomofo,
             if (G_UNLIKELY (bs_res != NULL))
                 break;
         }
+        if (G_UNLIKELY (bs_res == NULL))
+            break;
         result.append(*bs_res,bpmf - bopomofo.begin() ,(*bs_res)->len);
         bpmf += i;
     }
index ebf0815..4379741 100644 (file)
@@ -16,7 +16,7 @@ public:
                         PinyinArray  &result,      // store pinyin in result
                         guint         max);        // max length of the result
     static const Pinyin * isPinyin (gint sheng, gint yun, guint option);
-    static guint parse_bopomofo (const std::wstring   &bopomofo,
+    static guint parseBopomofo (const std::wstring   &bopomofo,
                                  gint            len,
                                  guint           option,
                                  PinyinArray    &result,