aboutsummaryrefslogtreecommitdiffstats
path: root/src/virtualkeyboard/3rdparty
diff options
context:
space:
mode:
authorJarkko Koivikko <jarkko.koivikko@code-q.fi>2015-11-17 09:33:53 +0200
committerJarkko Koivikko <jarkko.koivikko@code-q.fi>2015-11-27 11:02:45 +0000
commit8177619e003eaf6097a27bc9e3abd7e8fcdd7d1d (patch)
tree67cf3cb0fdf794e64447012956d61f2b151b225e /src/virtualkeyboard/3rdparty
parent52cd240a4d11e297f67e620f3516064105cc0de1 (diff)
Add Zhuyin input method for Traditional Chinese
Zhuyin can be enabled with CONFIG+=tcime or CONFIG+=zhuyin qmake flags. Note that CONFIG+=tcime enables both Cangjie and Zhuyin input methods. You can also enable the Cangjie input method with CONFIG+=cangjie, in which case the Zhuyin input method will not be activated, unless the config contains CONFIG+=zhuyin also. Change-Id: Iddea01f3e3d7f1dafff80e17da5b7cf89d4cfc55 Task-number: QTRD-3726 Reviewed-by: Rainer Keller <rainer.keller@theqtcompany.com> Reviewed-by: Liang Qi <liang.qi@theqtcompany.com>
Diffstat (limited to 'src/virtualkeyboard/3rdparty')
-rw-r--r--src/virtualkeyboard/3rdparty/tcime/data/java/dict_zhuyin.datbin0 -> 37323 bytes
-rw-r--r--src/virtualkeyboard/3rdparty/tcime/data/qt/dict_zhuyin.datbin0 -> 36044 bytes
-rw-r--r--src/virtualkeyboard/3rdparty/tcime/tcime.pro8
-rw-r--r--src/virtualkeyboard/3rdparty/tcime/zhuyindictionary.cpp61
-rw-r--r--src/virtualkeyboard/3rdparty/tcime/zhuyindictionary.h42
-rw-r--r--src/virtualkeyboard/3rdparty/tcime/zhuyintable.cpp161
-rw-r--r--src/virtualkeyboard/3rdparty/tcime/zhuyintable.h74
7 files changed, 344 insertions, 2 deletions
diff --git a/src/virtualkeyboard/3rdparty/tcime/data/java/dict_zhuyin.dat b/src/virtualkeyboard/3rdparty/tcime/data/java/dict_zhuyin.dat
new file mode 100644
index 00000000..3587635e
--- /dev/null
+++ b/src/virtualkeyboard/3rdparty/tcime/data/java/dict_zhuyin.dat
Binary files differ
diff --git a/src/virtualkeyboard/3rdparty/tcime/data/qt/dict_zhuyin.dat b/src/virtualkeyboard/3rdparty/tcime/data/qt/dict_zhuyin.dat
new file mode 100644
index 00000000..6aee7de5
--- /dev/null
+++ b/src/virtualkeyboard/3rdparty/tcime/data/qt/dict_zhuyin.dat
Binary files differ
diff --git a/src/virtualkeyboard/3rdparty/tcime/tcime.pro b/src/virtualkeyboard/3rdparty/tcime/tcime.pro
index 65261f46..218563a6 100644
--- a/src/virtualkeyboard/3rdparty/tcime/tcime.pro
+++ b/src/virtualkeyboard/3rdparty/tcime/tcime.pro
@@ -16,13 +16,17 @@ SOURCES += \
cangjiedictionary.cpp \
cangjietable.cpp \
phrasedictionary.cpp \
- worddictionary.cpp
+ worddictionary.cpp \
+ zhuyindictionary.cpp \
+ zhuyintable.cpp
HEADERS += \
cangjiedictionary.h \
cangjietable.h \
phrasedictionary.h \
- worddictionary.h
+ worddictionary.h \
+ zhuyindictionary.h \
+ zhuyintable.h
OTHER_FILES += \
data/dict_cangjie.dat \
diff --git a/src/virtualkeyboard/3rdparty/tcime/zhuyindictionary.cpp b/src/virtualkeyboard/3rdparty/tcime/zhuyindictionary.cpp
new file mode 100644
index 00000000..1cf303c1
--- /dev/null
+++ b/src/virtualkeyboard/3rdparty/tcime/zhuyindictionary.cpp
@@ -0,0 +1,61 @@
+/*
+ * Qt implementation of TCIME library
+ * This file is part of the Qt Virtual Keyboard module.
+ * Contact: http://www.qt.io/licensing/
+ *
+ * Copyright (C) 2015 The Qt Company
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "zhuyindictionary.h"
+#include "zhuyintable.h"
+
+using namespace tcime;
+
+ZhuyinDictionary::ZhuyinDictionary() :
+ WordDictionary()
+{
+}
+
+QStringList ZhuyinDictionary::getWords(const QString &input) const
+{
+ // Look up the syllables index; return empty string for invalid syllables.
+ QStringList pair = ZhuyinTable::stripTones(input);
+ int syllablesIndex = !pair.isEmpty() ? ZhuyinTable::getSyllablesIndex(pair[0]) : -1;
+ if (syllablesIndex < 0 || syllablesIndex >= dictionary().size())
+ return QStringList();
+
+ // [22-initials * 39-finals] syllables array; each syllables entry points to
+ // a char[] containing words for that syllables.
+ const DictionaryEntry &data = dictionary()[syllablesIndex];
+ if (data.isEmpty())
+ return QStringList();
+
+ // Counts of words for each tone are stored in the array beginning.
+ int tone = ZhuyinTable::getTones(pair[1].at(0));
+ int length = (int) data[tone].unicode();
+ if (length == 0)
+ return QStringList();
+
+ int start = ZhuyinTable::getTonesCount();
+ for (int i = 0; i < tone; ++i)
+ start += (int) data[i].unicode();
+
+ QStringList words;
+ for (int i = 0; i < length; ++i)
+ words.append(data[start + i]);
+
+ return words;
+}
diff --git a/src/virtualkeyboard/3rdparty/tcime/zhuyindictionary.h b/src/virtualkeyboard/3rdparty/tcime/zhuyindictionary.h
new file mode 100644
index 00000000..c9469b72
--- /dev/null
+++ b/src/virtualkeyboard/3rdparty/tcime/zhuyindictionary.h
@@ -0,0 +1,42 @@
+/*
+ * Qt implementation of TCIME library
+ * This file is part of the Qt Virtual Keyboard module.
+ * Contact: http://www.qt.io/licensing/
+ *
+ * Copyright (C) 2015 The Qt Company
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ZHUYINDICTIONARY_H
+#define ZHUYINDICTIONARY_H
+
+#include "worddictionary.h"
+
+namespace tcime {
+
+/**
+ * Extends WordDictionary to provide zhuyin word-suggestions.
+ */
+class ZhuyinDictionary : public WordDictionary
+{
+public:
+ ZhuyinDictionary();
+
+ QStringList getWords(const QString &input) const;
+};
+
+}
+
+#endif // ZHUYINDICTIONARY_H
diff --git a/src/virtualkeyboard/3rdparty/tcime/zhuyintable.cpp b/src/virtualkeyboard/3rdparty/tcime/zhuyintable.cpp
new file mode 100644
index 00000000..c405b1e3
--- /dev/null
+++ b/src/virtualkeyboard/3rdparty/tcime/zhuyintable.cpp
@@ -0,0 +1,161 @@
+/*
+ * Qt implementation of TCIME library
+ * This file is part of the Qt Virtual Keyboard module.
+ * Contact: http://www.qt.io/licensing/
+ *
+ * Copyright (C) 2015 The Qt Company
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "zhuyintable.h"
+#include <QStringList>
+
+using namespace tcime;
+
+const int ZhuyinTable::INITIALS_SIZE = 22;
+const QList<QChar> ZhuyinTable::yiEndingFinals = QList<QChar>()
+ << 0x311a << 0x311b << 0x311d << 0x311e << 0x3120 << 0x3121 << 0x3122
+ << 0x3123 << 0x3124 << 0x3125;
+const QList<QChar> ZhuyinTable::wuEndingFinals = QList<QChar>()
+ << 0x311a << 0x311b << 0x311e << 0x311f << 0x3122 << 0x3123 << 0x3124
+ << 0x3125;
+const QList<QChar> ZhuyinTable::yuEndingFinals = QList<QChar>()
+ << 0x311d << 0x3122 << 0x3123 << 0x3125;
+const int ZhuyinTable::YI_FINALS_INDEX = 14;
+const int ZhuyinTable::WU_FINALS_INDEX = 25;
+const int ZhuyinTable::YU_FINALS_INDEX = 34;
+const QChar ZhuyinTable::YI_FINALS = 0x3127;
+const QChar ZhuyinTable::WU_FINALS = 0x3128;
+const QChar ZhuyinTable::YU_FINALS = 0x3129;
+const QList<QChar> ZhuyinTable::tones = QList<QChar>()
+ << ZhuyinTable::DEFAULT_TONE << 0x02d9 << 0x02ca << 0x02c7 << 0x02cb;
+const QChar ZhuyinTable::DEFAULT_TONE = QChar(' ');
+
+int ZhuyinTable::getInitials(const QChar &initials)
+{
+ // Calculate the index by its distance to the first initials 'ㄅ' (b).
+ int index = initials.unicode() - 0x3105 + 1;
+ if (index >= ZhuyinTable::INITIALS_SIZE)
+ // Syllables starting with finals can still be valid.
+ return 0;
+
+ return (index >= 0) ? index : -1;
+}
+
+int ZhuyinTable::getFinals(const QString &finals)
+{
+ if (finals.length() == 0)
+ // Syllables ending with no finals can still be valid.
+ return 0;
+
+ if (finals.length() > 2)
+ return -1;
+
+ // Compute the index instead of direct lookup the whole array to save
+ // traversing time. First calculate the distance to the first finals
+ // 'ㄚ' (a).
+ const QChar firstFinal = finals.at(0);
+ int index = firstFinal.unicode() - 0x311a + 1;
+ if (index < YI_FINALS_INDEX)
+ return index;
+
+ // Check 'ㄧ' (yi), 'ㄨ' (wu) , and 'ㄩ' (yu) group finals.
+ QList<QChar> endingFinals;
+ if (firstFinal == YI_FINALS) {
+ index = YI_FINALS_INDEX;
+ endingFinals = yiEndingFinals;
+ } else if (firstFinal == WU_FINALS) {
+ index = WU_FINALS_INDEX;
+ endingFinals = wuEndingFinals;
+ } else if (firstFinal == YU_FINALS) {
+ index = YU_FINALS_INDEX;
+ endingFinals = yuEndingFinals;
+ } else {
+ return -1;
+ }
+
+ if (finals.length() == 1)
+ return index;
+
+ for (int i = 0; i < endingFinals.size(); ++i) {
+ if (finals.at(1) == endingFinals[i])
+ return index + i + 1;
+ }
+ return -1;
+}
+
+int ZhuyinTable::getSyllablesIndex(const QString &syllables)
+{
+ if (syllables.isEmpty())
+ return -1;
+
+ int initials = getInitials(syllables.at(0));
+ if (initials < 0)
+ return -1;
+
+ // Strip out initials before getting finals column-index.
+ int finals = getFinals((initials != 0) ? syllables.mid(1) : syllables);
+ if (finals < 0)
+ return -1;
+
+ return (finals * INITIALS_SIZE + initials);
+}
+
+int ZhuyinTable::getTones(const QChar &c)
+{
+ for (int i = 0; i < tones.size(); ++i) {
+ if (tones[i] == c)
+ return i;
+ }
+ // Treat all other characters as the default tone with the index 0.
+ return 0;
+}
+
+int ZhuyinTable::getTonesCount()
+{
+ return tones.size();
+}
+
+bool ZhuyinTable::isTone(const QChar &c)
+{
+ for (int i = 0; i < tones.size(); ++i) {
+ if (tones[i] == c)
+ return true;
+ }
+ return false;
+}
+
+bool ZhuyinTable::isYiWuYuFinals(const QChar &c)
+{
+ ushort unicode = c.unicode();
+ return unicode == YI_FINALS || unicode == WU_FINALS || unicode == YU_FINALS;
+}
+
+QStringList ZhuyinTable::stripTones(const QString &input)
+{
+ const int last = input.length() - 1;
+ if (last < 0)
+ return QStringList();
+
+ QChar tone = input.at(last);
+ if (isTone(tone)) {
+ QString syllables = input.left(last);
+ if (syllables.length() <= 0)
+ return QStringList();
+ return QStringList() << syllables << QString(tone);
+ }
+ // Treat the tone-less input as the default tone (tone-0).
+ return QStringList() << input << QString(DEFAULT_TONE);
+}
diff --git a/src/virtualkeyboard/3rdparty/tcime/zhuyintable.h b/src/virtualkeyboard/3rdparty/tcime/zhuyintable.h
new file mode 100644
index 00000000..8512574e
--- /dev/null
+++ b/src/virtualkeyboard/3rdparty/tcime/zhuyintable.h
@@ -0,0 +1,74 @@
+/*
+ * Qt implementation of TCIME library
+ * This file is part of the Qt Virtual Keyboard module.
+ * Contact: http://www.qt.io/licensing/
+ *
+ * Copyright (C) 2015 The Qt Company
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ZHUYINTABLE_H
+#define ZHUYINTABLE_H
+
+#include <QMap>
+#include <QChar>
+#include <QString>
+
+namespace tcime {
+
+class ZhuyinTable
+{
+ Q_DISABLE_COPY(ZhuyinTable)
+ ZhuyinTable() {}
+
+ // All Chinese characters are mapped into a zhuyin table as described in
+ // http://en.wikipedia.org/wiki/Zhuyin_table.
+ static const int INITIALS_SIZE;
+
+ // Finals that can be appended after 'ㄧ' (yi), 'ㄨ' (wu), or 'ㄩ' (yu).
+ static const QList<QChar> yiEndingFinals;
+ static const QList<QChar> wuEndingFinals;
+ static const QList<QChar> yuEndingFinals;
+
+ // 'ㄧ' (yi) finals start from position 14 and are followed by 'ㄨ' (wu)
+ // finals, and 'ㄩ' (yu) finals follow after 'ㄨ' (wu) finals.
+ static const int YI_FINALS_INDEX;
+ static const int WU_FINALS_INDEX;
+ static const int YU_FINALS_INDEX;
+
+ // 'ㄧ' (yi), 'ㄨ' (wu) , and 'ㄩ' (yu) finals.
+ static const QChar YI_FINALS;
+ static const QChar WU_FINALS;
+ static const QChar YU_FINALS;
+
+ // Default tone and four tone symbols: '˙', 'ˊ', 'ˇ', and 'ˋ'.
+ static const QList<QChar> tones;
+
+public:
+ static const QChar DEFAULT_TONE;
+
+ static int getInitials(const QChar &initials);
+ static int getFinals(const QString &finals);
+ static int getSyllablesIndex(const QString &syllables);
+ static int getTones(const QChar &c);
+ static int getTonesCount();
+ static bool isTone(const QChar &c);
+ static bool isYiWuYuFinals(const QChar &c);
+ static QStringList stripTones(const QString &input);
+};
+
+}
+
+#endif // ZHUYINTABLE_H