/**************************************************************************** ** ** Copyright (C) 2015 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL21$ ** 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 http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 or version 3 as published by the Free ** Software Foundation and appearing in the file LICENSE.LGPLv21 and ** LICENSE.LGPLv3 included in the packaging of this file. Please review the ** following information to ensure the GNU Lesser General Public License ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** As a special exception, The Qt Company gives you certain additional ** rights. These rights are described in The Qt Company LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ // Most of the code here was originally written by Serika Kurusugawa, // a.k.a. Junji Takagi, and is included in Qt with the author's permission // and the grateful thanks of the Qt team. /*! \class QJisCodec \inmodule QtCore \reentrant \internal */ #include "qjiscodec_p.h" #include "qlist.h" QT_BEGIN_NAMESPACE #ifndef QT_NO_BIG_CODECS enum { Esc = 0x1b, So = 0x0e, // Shift Out Si = 0x0f, // Shift In ReverseSolidus = 0x5c, YenSign = 0x5c, Tilde = 0x7e, Overline = 0x7e }; #define IsKana(c) (((c) >= 0xa1) && ((c) <= 0xdf)) #define IsJisChar(c) (((c) >= 0x21) && ((c) <= 0x7e)) #define QValidChar(u) ((u) ? QChar((ushort)(u)) : QChar(QChar::ReplacementCharacter)) enum Iso2022State{ Ascii, MinState = Ascii, JISX0201_Latin, JISX0201_Kana, JISX0208_1978, JISX0208_1983, JISX0212, MaxState = JISX0212, UnknownState }; static const char Esc_CHARS[] = "()*+-./"; static const char Esc_Ascii[] = {Esc, '(', 'B', 0 }; static const char Esc_JISX0201_Latin[] = {Esc, '(', 'J', 0 }; static const char Esc_JISX0201_Kana[] = {Esc, '(', 'I', 0 }; static const char Esc_JISX0208_1978[] = {Esc, '$', '@', 0 }; static const char Esc_JISX0208_1983[] = {Esc, '$', 'B', 0 }; static const char Esc_JISX0212[] = {Esc, '$', '(', 'D', 0 }; static const char * const Esc_SEQ[] = { Esc_Ascii, Esc_JISX0201_Latin, Esc_JISX0201_Kana, Esc_JISX0208_1978, Esc_JISX0208_1983, Esc_JISX0212 }; /*! \internal */ QJisCodec::QJisCodec() : conv(QJpUnicodeConv::newConverter(QJpUnicodeConv::Default)) { } /*! \internal */ QJisCodec::~QJisCodec() { delete (QJpUnicodeConv*)conv; conv = 0; } QByteArray QJisCodec::convertFromUnicode(const QChar *uc, int len, ConverterState *cs) const { char replacement = '?'; if (cs) { if (cs->flags & ConvertInvalidToNull) replacement = 0; } int invalid = 0; QByteArray result; Iso2022State state = Ascii; Iso2022State prev = Ascii; for (int i = 0; i < len; i++) { QChar ch = uc[i]; uint j; if (ch.row() == 0x00 && ch.cell() < 0x80) { // Ascii if (state != JISX0201_Latin || ch.cell() == ReverseSolidus || ch.cell() == Tilde) { state = Ascii; } j = ch.cell(); } else if ((j = conv->unicodeToJisx0201(ch.row(), ch.cell())) != 0) { if (j < 0x80) { // JIS X 0201 Latin if (state != Ascii || ch.cell() == YenSign || ch.cell() == Overline) { state = JISX0201_Latin; } } else { // JIS X 0201 Kana state = JISX0201_Kana; j &= 0x7f; } } else if ((j = conv->unicodeToJisx0208(ch.row(), ch.cell())) != 0) { // JIS X 0208 state = JISX0208_1983; } else if ((j = conv->unicodeToJisx0212(ch.row(), ch.cell())) != 0) { // JIS X 0212 state = JISX0212; } else { // Invalid state = UnknownState; j = replacement; ++invalid; } if (state != prev) { if (state == UnknownState) { result += Esc_Ascii; } else { result += Esc_SEQ[state - MinState]; } prev = state; } if (j < 0x0100) { result += j & 0xff; } else { result += (j >> 8) & 0xff; result += j & 0xff; } } if (prev != Ascii) { result += Esc_Ascii; } if (cs) { cs->invalidChars += invalid; } return result; } QString QJisCodec::convertToUnicode(const char* chars, int len, ConverterState *cs) const { uchar buf[4] = {0, 0, 0, 0}; int nbuf = 0; Iso2022State state = Ascii, prev = Ascii; bool esc = false; QChar replacement = QChar::ReplacementCharacter; if (cs) { if (cs->flags & ConvertInvalidToNull) replacement = QChar::Null; nbuf = cs->remainingChars; buf[0] = (cs->state_data[0] >> 24) & 0xff; buf[1] = (cs->state_data[0] >> 16) & 0xff; buf[2] = (cs->state_data[0] >> 8) & 0xff; buf[3] = (cs->state_data[0] >> 0) & 0xff; state = (Iso2022State)((cs->state_data[1] >> 0) & 0xff); prev = (Iso2022State)((cs->state_data[1] >> 8) & 0xff); esc = cs->state_data[2]; } int invalid = 0; QString result; for (int i=0; ijisx0201ToUnicode(ch); result += QValidChar(u); break; case JISX0201_Kana: u = conv->jisx0201ToUnicode(ch | 0x80); result += QValidChar(u); break; case JISX0208_1978: case JISX0208_1983: case JISX0212: buf[nbuf++] = ch; break; default: result += QChar::ReplacementCharacter; break; } break; case 1: switch (state) { case JISX0208_1978: case JISX0208_1983: u = conv->jisx0208ToUnicode(buf[0] & 0x7f, ch & 0x7f); result += QValidChar(u); break; case JISX0212: u = conv->jisx0212ToUnicode(buf[0] & 0x7f, ch & 0x7f); result += QValidChar(u); break; default: result += replacement; ++invalid; break; } nbuf = 0; break; } } } } if (cs) { cs->remainingChars = nbuf; cs->invalidChars += invalid; cs->state_data[0] = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3]; cs->state_data[1] = (prev << 8) + state; cs->state_data[2] = esc; } return result; } /*! \internal */ int QJisCodec::_mibEnum() { return 39; } /*! \internal */ QByteArray QJisCodec::_name() { return "ISO-2022-JP"; } /*! Returns the codec's mime name. */ QList QJisCodec::_aliases() { QList list; list << "JIS7"; // Qt 3 compat return list; } #endif // QT_NO_BIG_CODECS QT_END_NAMESPACE