kcharsets.cpp

来自「将konqueror浏览器移植到ARM9 2410中」· C++ 代码 · 共 920 行 · 第 1/2 页

CPP
920
字号
}QStringList KCharsets::availableFamilies( QFont::CharSet ch ){    QStringList families;    QStrList chFamilies = (*d->availableCharsets)[ch];    // list Unicode as available if nothing else found    if ( ( (int)ch != QFont::Unicode ) && chFamilies.isEmpty() )	chFamilies = (*d->availableCharsets)[QFont::Unicode];    for ( unsigned i = 0; i < chFamilies.count(); ++i )	families.append( QString::fromLatin1( chFamilies.at( i ) ) );    return families;}void KCharsets::setQFont(QFont &f, QString charset) const{    QFont::CharSet cs = nameToID(charset);    if ( cs == QFont::AnyCharSet ) {	cs = charsetForEncoding(charset);    }    setQFont( f, cs );}void KCharsets::setQFont(QFont &f, QFont::CharSet charset) const{    if(QFontInfo(f).charSet() == charset)        return;    if(charset == QFont::AnyCharSet)    {        f.setCharSet(QFont::AnyCharSet);        return;    }    if(!d->db)        d->db = new QFontDatabase;    d->getAvailableCharsets();    QCString family = f.family().latin1();    //kdDebug() << "KCharsets::setQFont family=" << family << endl;    QStrList chFamilies = (*d->availableCharsets)[charset];    if(chFamilies.contains(family)) {	//kdDebug() << "KCharsets::setQFont: charsetAvailable in family" << endl;        f.setCharSet(charset);        return;    }    // ok... we don't have the charset in the specified family, let's    // try to find a replacement.    if(chFamilies.count() != 0) {	//kdDebug() << "KCharsets::setQFont: using family " << chFamilies.first() << " in native charset " << charset << endl;        f.setFamily(chFamilies.first());        f.setCharSet(charset);        return;    }    QStrList ucFamilies = (*d->availableCharsets)[QFont::Unicode];    if(ucFamilies.contains(family)) {	//kdDebug() << "KCharsets::setQFont: using unicode" << endl;	// just setting the charset to unicode should work        f.setCharSet(QFont::Unicode);        return;    }    // Unicode and any family    if(ucFamilies.count() != 0) {	//kdDebug() << "KCharsets::setQFont: using family " << chFamilies.first() << " in unicode" << endl;        f.setFamily(ucFamilies.first());        f.setCharSet(QFont::Unicode);        return;    }    // give up -- just use something    f.setCharSet(QFont::AnyCharSet);    return;}bool KCharsets::isAvailable(const QString &charset){    QFont::CharSet cs = nameToID(charset);    return cs == QFont::AnyCharSet ? false : isAvailable(cs);}bool KCharsets::isAvailable(QFont::CharSet charset){    d->getAvailableCharsets();    return d->availableCharsets->contains(charset);}QFont::CharSet KCharsets::charsetForLocale(){    return nameToID(KGlobal::locale()->charset());}bool KCharsets::hasUnicode(QString family) const{    d->getAvailableCharsets();    if(!d->availableCharsets->contains(QFont::Unicode))        return false;    QStrList lst = (*d->availableCharsets)[QFont::Unicode];    if(lst.contains(family.latin1()))        return true;    return false;}bool KCharsets::hasUnicode(QFont &font) const{    return hasUnicode(font.family());}QString KCharsets::xCharsetName(QFont::CharSet charSet) const{    switch( charSet )    {    case QFont::Unicode:        return "iso10646-1";    case QFont::ISO_8859_1:        return "iso8859-1";    case QFont::ISO_8859_2:        return "iso8859-2";    case QFont::ISO_8859_3:        return "iso8859-3";    case QFont::ISO_8859_4:        return "iso8859-4";    case QFont::ISO_8859_5:        return "iso8859-5";    case QFont::ISO_8859_6:        return "iso8859-6";    case QFont::ISO_8859_7:        return "iso8859-7";    case QFont::ISO_8859_8:        return "iso8859-8";    case QFont::ISO_8859_9:        return "iso8859-9";    case QFont::ISO_8859_10:        return "iso8859-10";    case QFont::ISO_8859_11: // most of them are actually named as tis620        return "tis620-0";    case QFont::ISO_8859_12:        return "iso8859-12";    case QFont::ISO_8859_13:        return "iso8859-13";    case QFont::ISO_8859_14:        return "iso8859-14";    case QFont::ISO_8859_15:        return "iso8859-15";    case QFont::KOI8R:        return "koi8-r";    case QFont::KOI8U:        return "koi8-u";    case QFont::Set_Ko:        return "ksc5601.1987-0";    case QFont::Set_Ja:        return "jisx0208.1983-0";    case QFont::TSCII:        return "tscii-0";    case QFont::Set_Th_TH:	return "unknown";	case QFont::Set_GBK:        return "gbk-0";	case QFont::Set_Zh:	 return "gb2312.1980-0";    case QFont::Set_Zh_TW:        return "cns11643.1986-*";    case QFont::Set_Big5:	return "big5-0";#if QT_VERSION >= 224    case QFont::CP1251:	return "microsoft-cp1251";    case QFont::PT154:	return "paratype-cp154";#endif    case QFont::AnyCharSet:    default:        break;    }    return "*-*";}QFont::CharSet KCharsets::nameToID(QString name) const{    name = name.lower();    if(d->nameToIDMap.contains(name))        return d->nameToIDMap[name]; // cache hit    int i = 0;    while(i < CHARSETS_COUNT)    {        if( name == charsetsStr[i] )        {            d->nameToIDMap.replace(name, charsetsIds[i]);            return charsetsIds[i];        }        i++;    }    i = CHARSETS_COUNT-1;    while( i-- ) {        if( name.find( xNames[i] ) == 0 ) {            d->nameToIDMap.replace(name, charsetsIds[i]);            return charsetsIds[i];	}    }    d->nameToIDMap.replace(name, QFont::AnyCharSet);    return QFont::AnyCharSet;}QString KCharsets::name(const QFont &f){    QFont::CharSet c = f.charSet();    return name(c);}QString KCharsets::name(QFont::CharSet c){    int i = 0;    while(i < CHARSETS_COUNT)    {        if( c == charsetsIds[i] )            return charsetsStr[i];        i++;    }    return "any";}QFont::CharSet KCharsets::xNameToID(QString name) const{    name = name.lower();    // fix this stone age problem    if ( name == "iso10646" )	name = xNames[0];    // try longest names first, then shorter ones    // to avoid that iso-8859-10 matches iso-8859-1    int i = CHARSETS_COUNT-1; // avoid the "" entry    while( i-- ) {        if( name.find( xNames[i] ) == 0 ) {	    return charsetsIds[i];	}    }    return QFont::AnyCharSet;}QTextCodec *KCharsets::codecForName(const QString &n) const{    bool b;    return codecForName( n, b );}QTextCodec *KCharsets::codecForName(const QString &n, bool &ok) const{    ok = true;    QTextCodec* codec = 0;    // dict lookup is case insensitive anyway    if((codec = d->codecForNameDict[n.isEmpty() ? "->locale<-" : n.latin1()]))        return codec; // cache hit, return    QCString name = n.lower().latin1();    if (n.isEmpty()) {        QString lc = KGlobal::locale()->charset();        if (lc.isEmpty())            codec = QTextCodec::codecForName("iso8859-1");        else            codec = codecForName(lc);        d->codecForNameDict.replace("->locale<-", codec);        return codec;    }    codec = QTextCodec::codecForName(name);    if(codec) {        d->codecForNameDict.replace(name, codec);        return codec;    }    // these codecs are built into Qt, but the name given for the codec is different,    // so QTextCodec did not recognise it.    d->conf()->setGroup("builtin");    QString cname = d->conf()->readEntry(name.data());    if(!cname.isEmpty() && !cname.isNull())        codec = QTextCodec::codecForName(cname.latin1());    if(codec)    {        d->codecForNameDict.replace(name, codec);        return codec;    }    d->conf()->setGroup("general");    QString dir = d->conf()->readEntry("i18ndir", QString::fromLatin1("/usr/share/i18n/charmaps"));    dir += "/";    // these are codecs not included in Qt. They can be build up if the corresponding charmap    // is available in the charmap directory.    d->conf()->setGroup("aliases");    cname = d->conf()->readEntry(name.data());    if(cname.isNull() || cname.isEmpty())        cname = name;    cname = cname.upper();    codec = QTextCodec::loadCharmapFile(dir + cname);    if(codec) {        d->codecForNameDict.replace(name, codec);        return codec;    }    // this also failed, the last resort is now to take some compatibility charmap    d->conf()->setGroup("conversionHints");    cname = cname.lower();    cname = d->conf()->readEntry(cname);    if(!cname.isEmpty() && !cname.isNull())        codec = QTextCodec::codecForName(cname.latin1());    if(codec) {        d->codecForNameDict.replace(name, codec);        return codec;    }    // could not assign a codec, let's return Latin1    ok = false;    return QTextCodec::codecForName("iso8859-1");}QFont::CharSet KCharsets::charsetForEncoding(const QString &e) const{    return charsetForEncoding( e, false );}QFont::CharSet KCharsets::charsetForEncoding(const QString &e, bool noUnicode) const{    QCString encoding = e.lower().latin1();    if(!noUnicode && d->charsetForEncodingMap.contains(encoding))        return d->charsetForEncodingMap[encoding]; // cache hit    d->conf()->setGroup("charsetsForEncoding");    //kdDebug(0) << "list for " << encoding << " is: " << d->conf->readEntry(encoding) << endl;    QString enc = d->conf()->readEntry(encoding.data());    if(enc.isEmpty()) {	d->conf()->setGroup("builtin");	enc = d->conf()->readEntry(encoding.data());	encoding = enc.lower().latin1();	d->conf()->setGroup("charsetsForEncoding");	//kdDebug(0) << "list for " << encoding << " is: " << d->conf->readEntry(encoding) << endl <<endl;    }    QStringList charsets;    charsets = d->conf()->readListEntry(encoding.data());    // iterate thorugh the list and find the first charset that is available    for ( QStringList::Iterator it = charsets.begin(); it != charsets.end(); ++it ) {        QFont::CharSet cs = nameToID(*it);	if ( noUnicode ) {	    if ( cs != QFont::Unicode )		return cs;	} else {	    if( const_cast<KCharsets *>(this)->isAvailable(cs) ) {		//kdDebug(0) << *it << " available" << endl;		d->charsetForEncodingMap.replace(QCString(e.latin1()), cs);		return cs;	    }	}        //kdDebug(0) << *it << " is not available" << endl;    }    // let's hope the system has a unicode font...    d->charsetForEncodingMap.replace(QCString(e.latin1()), QFont::Unicode);    return QFont::Unicode;}bool KCharsets::supportsScript( const QFont &f, QFont::CharSet charset ){    QChar ch;    QFont::CharSet fcs = f.charSet();    switch( charset ) {	case QFont::ISO_8859_1:	    ch = 0xc0; break; //Latin A circumflex	case QFont::ISO_8859_2:	    ch = 0x013d; break;	case QFont::ISO_8859_3:	    ch = 0x0126; break;	case QFont::ISO_8859_10:	case QFont::ISO_8859_4:	    ch = 0x014b; break;	case QFont::ISO_8859_6:	    if ( fcs != QFont::Unicode ) return false;	    ch = 0xfef5; break;	case QFont::ISO_8859_7:	    ch = 0x3aa; break;	case QFont::ISO_8859_8:	    ch  = 0x05D3; break;	case QFont::ISO_8859_9:	    ch = 0x0131; break;	case QFont::ISO_8859_11:	case QFont::Set_Th_TH:	    ch = 0x0E23; break;	case QFont::ISO_8859_13:	    ch  = 0x0179; break;	case QFont::ISO_8859_14:	    ch  = 0x0177; break;	case QFont::ISO_8859_15:	    ch  = 0x0153; break;	case QFont::ISO_8859_5:	case QFont::KOI8U:#if QT_VERSION >= 224	case QFont::CP1251:	case QFont::PT154:#endif	    ch = 0x0454; break;	case QFont::KOI8R:	    ch = 0x0431; break;	case QFont::Set_Ja:	    ch  = 0x3041; break;	case QFont::Set_Ko:	    ch = 0xac00; break;	case QFont::Set_Zh:	case QFont::Set_GBK:	case QFont::Set_Big5:	case QFont::Set_Zh_TW:	    ch = 0x4e00; break;	case QFont::Unicode:	    return (fcs == charset);	case QFont::TSCII:	    ch = 0x0b90;	case QFont::ISO_8859_12:	case QFont::AnyCharSet:	default:	    ch = 0x0; break;    }    if ( charset == fcs || ch == QChar::null )	return true;    return QFontMetrics(f).inFont( ch );}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?