/**************************************************************************** ** $Id$ ** ** Implementation of QTsciiCodec class ** ** Copyright (C) 2000 Hans Petter Bieker . All rights reserved. ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. ** ** This file is part of the tools module of the Qt GUI Toolkit. ** ** Contributed by Hans Petter Bieker ** See the documentation for their license statement for the code as ** it was at the time of contribution. ** **********************************************************************/ /*! \class QTsciiCodec qtsciicodec.h \ingroup i18n \brief The QTsciiCodec class provides conversion to and from the Tamil TSCII encoding. TSCII, formally the Tamil Standard Code Information Interchange specification, is a commonly used charset for Tamils. The official page for the standard is at \link http://www.tamil.net/tscii/ http://www.tamil.net/tscii/\endlink This codec uses the mapping table found at \link http://www.geocities.com/Athens/5180/tsciiset.html http://www.geocities.com/Athens/5180/tsciiset.html\endlink. Tamil uses composed Unicode which might cause some problems if you are using Unicode fonts instead of TSCII fonts. The TSCII codec was contributed to Qt by Hans Petter Bieker \. The copyright notice for his code follows: \legalese Copyright 2000 Hans Petter Bieker . All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: \list 1 \i Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. \i Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. \endlist THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "qtsciicodec.h" #ifndef QT_NO_CODECS static unsigned char qt_UnicodeToTSCII(ushort u1, ushort u2, ushort u3); static unsigned int qt_TSCIIToUnicode(unsigned int code, uint *s); #define IsTSCIIChar(c) (((c) >= 0x80) && ((c) <= 0xfd)) #define QValidChar(u) ((u) ? QChar((u)) : QChar::replacement) /*! \reimp */ int QTsciiCodec::mibEnum() const { /* There is no MIBEnum for TSCII now */ return 2028; } /*! \reimp */ QCString QTsciiCodec::fromUnicode(const QString& uc, int& len_in_out) const { int l = QMIN((int)uc.length(), len_in_out); int rlen = l+1; QCString rstr(rlen); uchar* cursor = (uchar*)rstr.data(); for (int i = 0; i < l; i++) { QChar ch = uc[i]; uchar j; if ( ch.row() == 0x00 && ch.cell() < 0x80 ) { // ASCII j = ch.cell(); } else if ((j = qt_UnicodeToTSCII(uc[i].unicode(), uc[i + 1].unicode(), uc[i + 2].unicode()))) { // We have to check the combined chars first! i += 2; } else if ((j = qt_UnicodeToTSCII(uc[i].unicode(), uc[i + 1].unicode(), 0))) { i++; } else if ((j = qt_UnicodeToTSCII(uc[i].unicode(), 0, 0))) { } else { // Error j = '?'; // unknown char } *cursor++ = j; } len_in_out = cursor - (uchar*)rstr.data(); *cursor = 0; return rstr; } /*! \reimp */ QString QTsciiCodec::toUnicode(const char* chars, int len) const { QString result; for (int i = 0; i < len; i++) { uchar ch = chars[i]; if ( ch < 0x80 ) { // ASCII result += QChar(ch); } else if ( IsTSCIIChar(ch) ) { // TSCII uint s[3]; uint u = qt_TSCIIToUnicode(ch, s); uint *p = s; while ( u-- ) { uint c = *p++; result += QValidChar(c); } } else { // Invalid result += QChar::replacement; } } return result; } /*! \reimp */ const char* QTsciiCodec::name() const { return "TSCII"; } /*! \reimp */ int QTsciiCodec::heuristicNameMatch(const char* hint) const { const char *p = strchr(hint, '.'); if (p) p++; else p = hint; if (qstricmp(p, "TSCII") == 0) return 4; return QTextCodec::heuristicNameMatch(hint); } /*! \reimp */ int QTsciiCodec::heuristicContentMatch(const char* chars, int len) const { int score = 0; for (int i=0; i