📄 kcharsets.cpp
字号:
/* This file is part of the KDE libraries Copyright (C) 1999 Lars Knoll (knoll@kde.org) $Id: kcharsets.cpp,v 1.2 2002/03/07 07:21:55 ymwei Exp $ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/#include "kcharsets.h"#include "kentities.c"#define _CODE_BY_YMWEI 1#if _CODE_BY_YMWEI#include "mgfont.h"#include "mgfontinfo.h"#include "mgfontdatabase.h"#else#include <qfontinfo.h>#include <qfontdatabase.h>#include <qtextcodec.h>#endif#include <kdebug.h>#include <qmap.h>#include <qcstring.h>#if _CODE_BY_YMWEItemplate class QList<MGFont::CharSet>;#define CHARSETS_COUNT 30#elsetemplate class QList<QFont::CharSet>;#if QT_VERSION > 220#define CHARSETS_COUNT 30#else#define CHARSETS_COUNT 29#endif#endif#if _CODY_BY_YMWEI#define QFont MGFont#endifstatic const char * const charsetsStr[CHARSETS_COUNT] = { "unicode", "iso-8859-1", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-9", "iso-8859-10", "iso-8859-11", "iso-8859-12", "iso-8859-13", "iso-8859-14", "iso-8859-15", "koi8r", "eucjp", "euckr", "set-th-th", "set-gbk", "set-zh", "set-zh-tw", "set-big5", "tscii", "utf-8", "utf-16", "iso-8859-11",#if QT_VERSION > 220 "koi8u",#endif "Any"};// these can contain wildcard characters. Needed for fontset matching (CJK fonts)static const char * const xNames[CHARSETS_COUNT] = { "iso10646-1", "iso8859-1", "iso8859-2", "iso8859-3", "iso8859-4", "iso8859-5", "iso8859-6", "iso8859-7", "iso8859-8", "iso8859-9", "iso8859-10", "iso8859-11", "iso8859-12", "iso8859-13", "iso8859-14", "iso8859-15", "koi8-r", "jisx0208.1983-0", "ksc5601.1987-0", "tis620.2533-1", "gb2312.1980-0", "gb2312.1980-0", "big5-0", "big5-0", "tscii-0", "utf8", "utf16", "tis620-*",#if QT_VERSION > 220 "koi8-u",#endif "" // this will always return true...};static const QFont::CharSet charsetsIds[CHARSETS_COUNT] = { QFont::Unicode, QFont::ISO_8859_1, QFont::ISO_8859_2, QFont::ISO_8859_3, QFont::ISO_8859_4, QFont::ISO_8859_5, QFont::ISO_8859_6, QFont::ISO_8859_7, QFont::ISO_8859_8, QFont::ISO_8859_9, QFont::ISO_8859_10, QFont::ISO_8859_11, QFont::ISO_8859_12, QFont::ISO_8859_13, QFont::ISO_8859_14, QFont::ISO_8859_15, QFont::KOI8R, QFont::Set_Ja, QFont::Set_Ko, QFont::Set_Th_TH, QFont::Set_GBK, QFont::Set_Zh, QFont::Set_Big5, QFont::Set_Zh_TW, QFont::TSCII, QFont::Unicode, QFont::Unicode, QFont::ISO_8859_11,#if QT_VERSION > 220 QFont::KOI8U,#endif QFont::AnyCharSet};class KCharsetsPrivate{public: KCharsetsPrivate() { db = 0; availableCharsets = 0; } ~KCharsetsPrivate() { delete db; delete availableCharsets; }#if _CODE_BY_YMWEI MGFontDatabase *db; QMap<MGFont::CharSet, QValueList<QCString> > *availableCharsets;#else QFontDatabase *db; QMap<QFont::CharSet, QValueList<QCString> > *availableCharsets;#endif void getAvailableCharsets();};static QFont::CharSet xNameToID(QCString &name){ name = name.lower(); int i = 0; while(i < CHARSETS_COUNT) { if( name == xNames[i] ) return charsetsIds[i]; i++; } return QFont::AnyCharSet;}void KCharsetsPrivate::getAvailableCharsets (){ if (availableCharsets) return; if(!db)#if _CODE_BY_YMWEI db = new MGFontDatabase;#else db = new QFontDatabase;#endif availableCharsets = new QMap<QFont::CharSet, QValueList<QCString> >; QStringList f = db->families(false); for (QStringList::Iterator it = f.begin(); it != f.end(); ++it) { QStringList chSets = db->charSets(*it, false); QCString family = (*it).latin1(); // can only be latin1 if (family.contains('-') ) // remove foundry family = family.right (family.length() - family.find('-' ) - 1); for ( QStringList::Iterator ch = chSets.begin(); ch != chSets.end(); ++ch ) { QCString cs = (*ch).latin1(); QFont::CharSet qcs = xNameToID( cs ); if ( qcs != QFont::AnyCharSet ) if( !availableCharsets->contains( qcs ) ) { QValueList<QCString> strList; strList.append( family ); availableCharsets->insert( qcs, strList ); } else ((*availableCharsets)[qcs]).append(family); } }}// --------------------------------------------------------------------------KCharsets::KCharsets(){ d = new KCharsetsPrivate;}KCharsets::~KCharsets(){ delete d;}QChar KCharsets::fromEntity(const QString &str) const{ QChar res = QChar::null; int pos = 0; if(str[pos] == '&') pos++; // Check for '�' or '�' sequence if (str[pos] == '#' && str.length()-pos > 1) { bool ok; pos++; if (str[pos] == 'x' || str[pos] == 'X') { pos++; // '�', hexadeciaml character reference QString tmp(str.unicode()+pos, str.length()-pos); res = tmp.toInt(&ok, 16); } else { // '�', deciaml character reference QString tmp(str.unicode()+pos, str.length()-pos); res = tmp.toInt(&ok, 10); } return res; } const entity *e = findEntity(str.ascii(), str.length()); if (!e) { return QChar::null; } return QChar(e->code);}QChar KCharsets::fromEntity(const QString &str, int &len) const{ // entities are never longer than 8 chars... we start from // that length and work backwards... len = 8; while(len > 0) { QString tmp = str.left(len); QChar res = fromEntity(tmp); if( res != QChar::null ) return res; len--; } return QChar::null;}QString KCharsets::toEntity(const QChar &ch) const{ QString ent; ent.sprintf("&0x%x;", ch.unicode()); return ent;}QList<QFont::CharSet> KCharsets::availableCharsets(QString family){ if(!d->db)#if _CODE_BY_YMWEI d->db = new MGFontDatabase;#else d->db = new QFontDatabase;#endif d->getAvailableCharsets(); QList<QFont::CharSet> chSets; //chSets.setAutoDelete(true); QCString f = family.latin1(); for( QMap<QFont::CharSet, QValueList<QCString> >::Iterator it = d->availableCharsets->begin(); it != d->availableCharsets->end(); ++it ) { if( !family.isNull() ) { if ( it.data().contains( f ) == 0) continue; } QFont::CharSet *i = new QFont::CharSet; *i = it.key(); chSets.append( i ); } return chSets;}QStringList KCharsets::availableCharsetNames(QString family){ QList<QFont::CharSet> list = availableCharsets( family ); list.setAutoDelete( true ); QStringList chsetNames; QFont::CharSet *chset; for ( chset = list.first(); chset != 0; chset = list.next() ) { chsetNames.append( xCharsetName( *chset ) ); } return chsetNames;}QStringList KCharsets::availableEncodingNames(){ QStringList available;#if 0 KConfig conf( "charsets", true ); QMap<QString, QString> map = conf.entryMap("charsetsForEncoding"); conf.setGroup("charsetsForEncoding"); QMap<QString, QString>::Iterator it; for( it = map.begin(); it != map.end(); ++it ) { //kdDebug(0) << "key = " << it.key() << " string =" << it.data() << endl; //kdDebug(0) << "list is: " << conf.readEntry(it.key()) << endl; QStringList charsets = conf.readListEntry(it.key()); // iterate thorugh the list and find the first charset that is available for ( QStringList::Iterator sit = charsets.begin(); sit != charsets.end(); ++sit ) { //kdDebug(0) << "checking for " << *sit << endl; if( const_cast<KCharsets *>(this)->isAvailable(*sit) ) { //kdDebug(0) << *sit << " available" << endl; available.append(it.key()); break; } } }#else available.append ("iso8859-1"); available.append ("gb2312.1980-0"); available.append ("big5-0");#endif return available;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -