/**************************************************************************** ** ** Implementation of TQTsciiCodec class ** ** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. ** ** This file is part of the tools module of the TQt GUI Toolkit. ** ** This file may be used under the terms of the GNU General ** Public License versions 2.0 or 3.0 as published by the Free ** Software Foundation and appearing in the files LICENSE.GPL2 ** and LICENSE.GPL3 included in the packaging of this file. ** Alternatively you may (at your option) use any later version ** of the GNU General Public License if such license has been ** publicly approved by Trolltech ASA (or its successors, if any) ** and the KDE Free TQt Foundation. ** ** Please review the following information to ensure GNU General ** Public Licensing retquirements will be met: ** http://trolltech.com/products/qt/licenses/licensing/opensource/. ** If you are unsure which license is appropriate for your use, please ** review the following information: ** http://trolltech.com/products/qt/licenses/licensing/licensingoverview ** or contact the sales department at sales@trolltech.com. ** ** This file may be used under the terms of the Q Public License as ** defined by Trolltech ASA and appearing in the file LICENSE.TQPL ** included in the packaging of this file. Licensees holding valid TQt ** Commercial licenses may use this file in accordance with the TQt ** Commercial License Agreement provided with the Software. ** ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted ** herein. ** **********************************************************************/ // Most of the code here was originally written by Hans Petter Bieker, // and is included in TQt with the author's permission, and the grateful // thanks of the Trolltech team. /*! \class TQTsciiCodec qtsciicodec.h \reentrant \ingroup i18n \brief The TQTsciiCodec 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. Most of the code here was written by Hans Petter Bieker and is included in TQt with the author's permission and the grateful thanks of the Trolltech team. Here is the copyright statement for the code as it was at the point of contribution. Trolltech's subsequent modifications are covered by the usual copyright for TQt. \legalese Copyright (C) 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 CONSETQUENTIAL 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 TQValidChar(u) ((u) ? TQChar((u)) : TQChar::replacement) /*! \reimp */ int TQTsciiCodec::mibEnum() const { /* There is no MIBEnum for TSCII now */ return 2028; } /*! \reimp */ TQCString TQTsciiCodec::fromUnicode(const TQString& uc, int& lenInOut) const { int l = TQMIN((int)uc.length(), lenInOut); int rlen = l+1; TQCString rstr(rlen); uchar* cursor = (uchar*)rstr.data(); for (int i = 0; i < l; i++) { TQChar 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; } lenInOut = cursor - (uchar*)rstr.data(); *cursor = 0; return rstr; } /*! \reimp */ TQString TQTsciiCodec::toUnicode(const char* chars, int len) const { TQString result; for (int i = 0; i < len; i++) { uchar ch = chars[i]; if ( ch < 0x80 ) { // ASCII result += TQChar(ch); } else if ( IsTSCIIChar(ch) ) { // TSCII uint s[3]; uint u = qt_TSCIIToUnicode(ch, s); uint *p = s; while ( u-- ) { uint c = *p++; result += TQValidChar(c); } } else { // Invalid result += TQChar::replacement; } } return result; } /*! \reimp */ const char* TQTsciiCodec::name() const { return "TSCII"; } /*! \reimp */ int TQTsciiCodec::heuristicNameMatch(const char* hint) const { const char *p = strchr(hint, '.'); if (p) p++; else p = hint; if (qstricmp(p, "TSCII") == 0) return 4; return TQTextCodec::heuristicNameMatch(hint); } /*! \reimp */ int TQTsciiCodec::heuristicContentMatch(const char* chars, int len) const { int score = 0; for (int i=0; i