1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtCore module of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
42
// Most of the code here was originally written by Serika Kurusugawa,
43
// a.k.a. Junji Takagi, and is included in Qt with the author's permission
44
// and the grateful thanks of the Qt team.
52
#include "qjiscodec_p.h"
57
#ifndef QT_NO_BIG_CODECS
60
So = 0x0e, // Shift Out
61
Si = 0x0f, // Shift In
63
ReverseSolidus = 0x5c,
69
#define IsKana(c) (((c) >= 0xa1) && ((c) <= 0xdf))
70
#define IsJisChar(c) (((c) >= 0x21) && ((c) <= 0x7e))
72
#define QValidChar(u) ((u) ? QChar((ushort)(u)) : QChar(QChar::ReplacementCharacter))
74
enum Iso2022State{ Ascii, MinState = Ascii,
75
JISX0201_Latin, JISX0201_Kana,
76
JISX0208_1978, JISX0208_1983,
77
JISX0212, MaxState = JISX0212,
80
static const char Esc_CHARS[] = "()*+-./";
82
static const char Esc_Ascii[] = {Esc, '(', 'B', 0 };
83
static const char Esc_JISX0201_Latin[] = {Esc, '(', 'J', 0 };
84
static const char Esc_JISX0201_Kana[] = {Esc, '(', 'I', 0 };
85
static const char Esc_JISX0208_1978[] = {Esc, '$', '@', 0 };
86
static const char Esc_JISX0208_1983[] = {Esc, '$', 'B', 0 };
87
static const char Esc_JISX0212[] = {Esc, '$', '(', 'D', 0 };
88
static const char * const Esc_SEQ[] = { Esc_Ascii,
98
QJisCodec::QJisCodec() : conv(QJpUnicodeConv::newConverter(QJpUnicodeConv::Default))
106
QJisCodec::~QJisCodec()
108
delete (QJpUnicodeConv*)conv;
112
QByteArray QJisCodec::convertFromUnicode(const QChar *uc, int len, ConverterState *cs) const
114
char replacement = '?';
116
if (cs->flags & ConvertInvalidToNull)
122
Iso2022State state = Ascii;
123
Iso2022State prev = Ascii;
124
for (int i = 0; i < len; i++) {
127
if (ch.row() == 0x00 && ch.cell() < 0x80) {
129
if (state != JISX0201_Latin ||
130
ch.cell() == ReverseSolidus || ch.cell() == Tilde) {
134
} else if ((j = conv->unicodeToJisx0201(ch.row(), ch.cell())) != 0) {
137
if (state != Ascii ||
138
ch.cell() == YenSign || ch.cell() == Overline) {
139
state = JISX0201_Latin;
143
state = JISX0201_Kana;
146
} else if ((j = conv->unicodeToJisx0208(ch.row(), ch.cell())) != 0) {
148
state = JISX0208_1983;
149
} else if ((j = conv->unicodeToJisx0212(ch.row(), ch.cell())) != 0) {
154
state = UnknownState;
159
if (state == UnknownState) {
162
result += Esc_SEQ[state - MinState];
169
result += (j >> 8) & 0xff;
178
cs->invalidChars += invalid;
183
QString QJisCodec::convertToUnicode(const char* chars, int len, ConverterState *cs) const
185
uchar buf[4] = {0, 0, 0, 0};
187
Iso2022State state = Ascii, prev = Ascii;
189
QChar replacement = QChar::ReplacementCharacter;
191
if (cs->flags & ConvertInvalidToNull)
192
replacement = QChar::Null;
193
nbuf = cs->remainingChars;
194
buf[0] = (cs->state_data[0] >> 24) & 0xff;
195
buf[1] = (cs->state_data[0] >> 16) & 0xff;
196
buf[2] = (cs->state_data[0] >> 8) & 0xff;
197
buf[3] = (cs->state_data[0] >> 0) & 0xff;
198
state = (Iso2022State)((cs->state_data[1] >> 0) & 0xff);
199
prev = (Iso2022State)((cs->state_data[1] >> 8) & 0xff);
200
esc = cs->state_data[2];
205
for (int i=0; i<len; i++) {
209
state = UnknownState;
212
if (ch == '$' || strchr(Esc_CHARS, ch)) {
221
if (strchr(Esc_CHARS, ch)) {
226
state = JISX0208_1978; // Esc $ @
229
state = JISX0208_1983; // Esc $ B
239
state = Ascii; // Esc (B
242
state = JISX0201_Kana; // Esc (I
245
state = JISX0201_Latin; // Esc (J
257
state = JISX0212; // Esc $ (D
270
} else if (ch == So) {
273
state = JISX0201_Kana;
275
} else if (ch == Si) {
277
if (prev == Ascii || prev == JISX0201_Latin) {
290
result += QLatin1Char(ch);
295
u = conv->jisx0201ToUnicode(ch);
296
result += QValidChar(u);
299
u = conv->jisx0201ToUnicode(ch | 0x80);
300
result += QValidChar(u);
308
result += QChar::ReplacementCharacter;
316
u = conv->jisx0208ToUnicode(buf[0] & 0x7f, ch & 0x7f);
317
result += QValidChar(u);
320
u = conv->jisx0212ToUnicode(buf[0] & 0x7f, ch & 0x7f);
321
result += QValidChar(u);
324
result += replacement;
336
cs->remainingChars = nbuf;
337
cs->invalidChars += invalid;
338
cs->state_data[0] = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
339
cs->state_data[1] = (prev << 8) + state;
340
cs->state_data[2] = esc;
351
int QJisCodec::_mibEnum()
359
QByteArray QJisCodec::_name()
361
return "ISO-2022-JP";
365
Returns the codec's mime name.
367
QList<QByteArray> QJisCodec::_aliases()
369
QList<QByteArray> list;
370
list << "JIS7"; // Qt 3 compat
374
#endif // QT_NO_BIG_CODECS