aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cerence/xt9/xt9common/xt9cpime.cpp
blob: 17ae0526f33987110fc647f7f5f10d88cf81d226 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Virtual Keyboard module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "xt9cpime.h"
#include "xt9languagemap.h"
#include "xt9dbfile.h"
#include "xt9callbacks.h"
#include <QStandardPaths>
#include <QLoggingCategory>

QT_BEGIN_NAMESPACE
namespace QtVirtualKeyboard {

static const ET9SYMB CANGJIE_MAP[] = {
    0x65E5, 0x6708, 0x91D1, 0x6728, 0x6C34, 0x706B, 0x571F, 0x7AF9, 0x6208,
    0x5341, 0x5927, 0x4E2D, 0x4E00, 0x5F13, 0x4EBA, 0x5FC3, 0x624B, 0x53E3,
    0x5C38, 0x5EFF, 0x5C71, 0x5973, 0x7530, 0x96E3, 0x535C };
static const size_t CANGJIE_MAP_SIZE = sizeof(CANGJIE_MAP) / sizeof(ET9SYMB);
static const ET9SYMB CANGJIE_WILDCARD_SYMB = 0x91CD;

static ET9SYMB GetCangjieMappingSymb(ET9SYMB symb)
{
    if (symb == CANGJIE_WILDCARD_SYMB)
        return ET9CPCANGJIEWILDCARD;

    for (size_t i = 0; i < CANGJIE_MAP_SIZE; i++) {
        if (CANGJIE_MAP[i] == symb)
            return static_cast<ET9SYMB>(i + 'a');
    }

    return symb;
}

static ET9SYMB GetCangjieSymb(ET9SYMB symb)
{
    if (symb == ET9CPCANGJIEWILDCARD)
        return CANGJIE_WILDCARD_SYMB;

    if (ET9CPIsCangJieLowerSymbol(symb))
        return CANGJIE_MAP[symb - 'a'];

    if (ET9CPIsCangJieUpperSymbol(symb))
        return CANGJIE_MAP[symb - 'A'];

    return symb;
}

class CangjieConverter : public Xt9KeyboardGenerator::CodeConverter {
public:
    QString convertTo(const QString &codes) const override
    {
        QVector<ushort> cangjieBuf(codes.size());
        for (int i = 0; i < codes.size(); ++i) {
            cangjieBuf[i] = GetCangjieSymb(codes.at(i).unicode());
        }
        return QString::fromUtf16(reinterpret_cast<const char16_t *>(cangjieBuf.constData()), cangjieBuf.length());
    }

    QString convertFrom(const QString &codes) const override
    {
        QVector<ushort> cangjieBuf(codes.size());
        for (int i = 0; i < codes.size(); ++i) {
            cangjieBuf[i] = GetCangjieMappingSymb(codes.at(i).unicode());
        }
        return QString::fromUtf16(reinterpret_cast<const char16_t *>(cangjieBuf.constData()), cangjieBuf.length());
    }
};

Q_GLOBAL_STATIC(CangjieConverter, cangjieConverter)

Xt9CpIme::Xt9CpIme(Xt9RequestCallback *requestCallback) :
    Xt9Ime(requestCallback)
{
}

Xt9KeyboardGenerator::CodeConverter *Xt9CpIme::getCangjieConverter()
{
    return cangjieConverter;
}

void Xt9CpIme::sysInit()
{
    Xt9Ime::sysInit();
    memset(&sLingInfo, 0, sizeof(sLingInfo));
    XT9_API(ET9CPSysInit, &sLingInfo, &sWordSymbInfo, this);
}

bool Xt9CpIme::ldbInit(ET9U32 dwFirstLdbNum, ET9U32 dwSecondLdbNum, ET9U32 eInputMode)
{
    ET9STATUS eStatus;
    Q_UNUSED(dwSecondLdbNum)

    eStatus = XT9_API(ET9CPLdbInit, &sLingInfo, dwFirstLdbNum, &ET9CPLdbReadData);
    if (!eStatus) {
        XT9_API(ET9CPSetInputMode, &sLingInfo, static_cast<ET9CPMode>(eInputMode));
        XT9_API(ET9CPClearComponent, &sLingInfo);
        XT9_API(ET9CPSetBilingual, &sLingInfo);
    }

    return !eStatus;
}

qint64 Xt9CpIme::dlmPreferredSize() const
{
    return ET9CPDLM_SIZE_NORMAL;
}

bool Xt9CpIme::dlmInit(void *data, qint64 size)
{
    ET9STATUS eStatus;

    eStatus = XT9_API(ET9CPDLMInit, &sLingInfo, static_cast<ET9CPDLM_info *>(data), static_cast<ET9U32>(size), nullptr);

    return !eStatus;
}

QString Xt9CpIme::exactWord(int *wordCompLen)
{
    QString exactWord = Xt9Ime::exactWord(wordCompLen);

    ET9CPPhrase phrase;
    ET9CPSpell spell;
    ET9U8 selSymbCount = 0;
    if (!ET9CPGetSelection(&sLingInfo, &phrase, &spell, &selSymbCount) && selSymbCount)
        exactWord.remove(0, static_cast<int>(selSymbCount));

    replaceSpecialSymbol(exactWord);

    return exactWord;
}

void Xt9CpIme::replaceSpecialSymbol(QString &exactWord) const
{
    for (int i = 0; i < exactWord.length(); ++i) {
        ET9SYMB symb = exactWord.at(i).unicode();
        if (ET9CPSymToCPTone(symb)) {
            exactWord.replace(i, 1, QChar(symb - ET9CPTONE1 + '1'));
        } else if (ET9CPIsStrokeSymbol(symb)) {
            int strokeIndex = symb - ET9CPSTROKE1;
            static const ushort STROKE_TABLE[ET9CPSTROKEWILDCARD - ET9CPSTROKE1 + 1] = {
                0x4E00,
                0x4E28,
                0x4E3F,
                0x4E36,
                0x4E5B,
                '*'
            };
            exactWord.replace(i, 1, QChar(STROKE_TABLE[strokeIndex]));
        }
    }
}

QString Xt9CpIme::spell()
{
    ET9STATUS eStatus;
    ET9CPSpell spell;

    eStatus = XT9_API(ET9CPGetSpell, &sLingInfo, &spell);
    if (eStatus == ET9STATUS_NEED_SELLIST_BUILD) {
        ET9U16 gestureValue;
        eStatus = XT9_API(ET9CPBuildSelectionList, &sLingInfo, &gestureValue);
        if (eStatus)
            return QString();

        eStatus = XT9_API(ET9CPGetSpell, &sLingInfo, &spell);
        if (eStatus)
            return QString();
    } else if (eStatus) {
        return QString();
    }

    QString result = QString::fromUtf16(reinterpret_cast<const char16_t *>(spell.pSymbs), spell.bLen);

    replaceSpecialSymbol(result);

    return result;
}

QStringList Xt9CpIme::buildSelectionList(int *defaultListIndex, ET9U16 *gestureValue)
{
    ET9STATUS eStatus;
    ET9U16 totalWords;

    eStatus = XT9_API(ET9CPBuildSelectionList, &sLingInfo, gestureValue);

    if (defaultListIndex)
        *defaultListIndex = -1;

    if (eStatus)
        return QStringList();

    eStatus = XT9_API(ET9CPGetPhraseCount, &sLingInfo, &totalWords);

    if (defaultListIndex && totalWords > 0 && !exactWord().isEmpty())
        *defaultListIndex = 0;

    QStringList list;
    for (ET9U16 i = 0; i < totalWords; ++i) {
        ET9CPPhrase phrase;
        ET9CPSpell spell;
        ET9CPPhraseSource phraseSource;
        eStatus = ET9CPGetPhrase(&sLingInfo, i, &phrase, &spell, &phraseSource);
        if (eStatus)
            return QStringList();

        const QString word = QString::fromUtf16(reinterpret_cast<const char16_t *>(phrase.pSymbs), phrase.bLen);

        list.append(word);
    }

    return list;
}

ET9STATUS Xt9CpIme::selectWord(int index)
{
    ET9CPPhrase phrase;
    ET9CPSpell spell;
    ET9CPPhraseSource phraseSource;
    ET9STATUS eStatus;

    qCDebug(lcXT9) << "selectWord" << index;

    eStatus = XT9_API(ET9CPGetPhrase, &sLingInfo, static_cast<ET9U16>(index), &phrase, &spell, &phraseSource);
    if (!eStatus) {
        eStatus = XT9_API(ET9CPSelectPhrase, &sLingInfo, static_cast<ET9U16>(index), &spell);
    }

    return eStatus;
}

void Xt9CpIme::cursorMoved()
{
    const ET9U32 maxBufLen = 456;
    ET9SYMB buffer[maxBufLen];
    ET9_Request request;

    request.eType = ET9_REQ_BufferContext;
    request.data.sBufferContextInfo.psBuf = buffer;
    request.data.sBufferContextInfo.dwMaxBufLen = maxBufLen;
    request.data.sBufferContextInfo.dwBufLen = static_cast<ET9U32>(-1);

    _requestCallback->request(&request);

    if (request.data.sBufferContextInfo.dwBufLen != static_cast<ET9U32>(-1))
        ET9CPSetContext(&sLingInfo, request.data.sBufferContextInfo.psBuf, request.data.sBufferContextInfo.dwBufLen);
    else
        ET9CPSetContext(&sLingInfo, request.data.sBufferContextInfo.psBuf, 0);
}

void Xt9CpIme::commitSelection()
{
    XT9_API(ET9CPCommitSelection, &sLingInfo);
}

ET9SYMB Xt9CpIme::lastSymb()
{
    const QString word = Xt9Ime::exactWord();
    const int wordLength = word.length();
    return wordLength > 0 ? word.at(wordLength - 1).unicode() : 0;
}

bool Xt9CpIme::addTone(ET9CPSYMB symb)
{
    ET9STATUS eStatus;

    eStatus = XT9_API(ET9AddExplicitSymb, sLingInfo.Base.pWordSymbInfo, symb, 0, ET9NOSHIFT, ET9_NO_ACTIVE_INDEX);
    if (eStatus)
        return false;

    eStatus = XT9_API(ET9CPBuildSelectionList, &sLingInfo, nullptr);
    if (eStatus) {
        XT9_API(ET9ClearOneSymb, sLingInfo.Base.pWordSymbInfo);
        return false;
    }

    return true;
}

ET9STATUS Xt9CpIme::ET9CPLdbReadData(ET9CPLingInfo *pLingInfo, ET9U8 *ET9FARDATA *ppbSrc, ET9U32 *pdwSizeInBytes)
{
    return reinterpret_cast<Xt9CpIme *>(pLingInfo->pPublicExtension)->ldbReadData(pLingInfo->dwLdbNum, ppbSrc, pdwSizeInBytes);
}

} // namespace QtVirtualKeyboard
QT_END_NAMESPACE