mirror of
https://github.com/tumic0/GPXSee.git
synced 2025-01-19 12:12:08 +01:00
Use QTextCodec for conversions to UTF on Qt5
This way we can support East Asian encodings at least on Qt5 in some way (the Qt codecs are not 100% compatible with the CPx encodings, but the result should be much better than a fallback to cp1250...).
This commit is contained in:
parent
f810117cbe
commit
6b70f4a958
@ -1,6 +1,70 @@
|
|||||||
#include <QVector>
|
|
||||||
#include "textcodec.h"
|
#include "textcodec.h"
|
||||||
|
|
||||||
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
|
||||||
|
#include <QTextCodec>
|
||||||
|
|
||||||
|
TextCodec::TextCodec()
|
||||||
|
{
|
||||||
|
_codec = QTextCodec::codecForName("Windows-1252");
|
||||||
|
}
|
||||||
|
|
||||||
|
TextCodec::TextCodec(int codepage)
|
||||||
|
{
|
||||||
|
switch (codepage) {
|
||||||
|
case 65001:
|
||||||
|
_codec = 0;
|
||||||
|
break;
|
||||||
|
case 932:
|
||||||
|
_codec = QTextCodec::codecForName("Shift-JIS");
|
||||||
|
break;
|
||||||
|
case 936:
|
||||||
|
_codec = QTextCodec::codecForName("GB18030");
|
||||||
|
break;
|
||||||
|
case 949:
|
||||||
|
_codec = QTextCodec::codecForName("EUC-KR");
|
||||||
|
break;
|
||||||
|
case 950:
|
||||||
|
_codec = QTextCodec::codecForName("Big5");
|
||||||
|
break;
|
||||||
|
case 1250:
|
||||||
|
_codec = QTextCodec::codecForName("Windows-1250");
|
||||||
|
break;
|
||||||
|
case 1251:
|
||||||
|
_codec = QTextCodec::codecForName("Windows-1251");
|
||||||
|
break;
|
||||||
|
case 1253:
|
||||||
|
_codec = QTextCodec::codecForName("Windows-1253");
|
||||||
|
break;
|
||||||
|
case 1254:
|
||||||
|
_codec = QTextCodec::codecForName("Windows-1254");
|
||||||
|
break;
|
||||||
|
case 1255:
|
||||||
|
_codec = QTextCodec::codecForName("Windows-1255");
|
||||||
|
break;
|
||||||
|
case 1256:
|
||||||
|
_codec = QTextCodec::codecForName("Windows-1256");
|
||||||
|
break;
|
||||||
|
case 1257:
|
||||||
|
_codec = QTextCodec::codecForName("Windows-1257");
|
||||||
|
break;
|
||||||
|
case 1258:
|
||||||
|
_codec = QTextCodec::codecForName("Windows-1258");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
_codec = QTextCodec::codecForName("Windows-1252");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QString TextCodec::toString(const QByteArray &ba) const
|
||||||
|
{
|
||||||
|
return _codec ? _codec->toUnicode(ba) : QString::fromUtf8(ba);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else // QT6
|
||||||
|
|
||||||
|
#include <QVector>
|
||||||
|
|
||||||
static const char32_t cp1250[] = {
|
static const char32_t cp1250[] = {
|
||||||
0x20AC, 0x0000, 0x201A, 0x0000, 0x201E, 0x2026, 0x2020, 0x2021,
|
0x20AC, 0x0000, 0x201A, 0x0000, 0x201E, 0x2026, 0x2020, 0x2021,
|
||||||
0x0000, 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179,
|
0x0000, 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179,
|
||||||
@ -192,10 +256,7 @@ TextCodec::TextCodec(int codepage)
|
|||||||
|
|
||||||
QString TextCodec::toString(const QByteArray &ba) const
|
QString TextCodec::toString(const QByteArray &ba) const
|
||||||
{
|
{
|
||||||
if (_table)
|
return _table ? from8bCp(ba) : QString::fromUtf8(ba);
|
||||||
return from8bCp(ba);
|
|
||||||
else
|
|
||||||
return QString::fromUtf8(ba);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString TextCodec::from8bCp(const QByteArray &ba) const
|
QString TextCodec::from8bCp(const QByteArray &ba) const
|
||||||
@ -212,3 +273,4 @@ QString TextCodec::from8bCp(const QByteArray &ba) const
|
|||||||
|
|
||||||
return QString::fromUcs4(ucs4.constData(), ucs4.size());
|
return QString::fromUcs4(ucs4.constData(), ucs4.size());
|
||||||
}
|
}
|
||||||
|
#endif // QT6
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
|
class QTextCodec;
|
||||||
|
|
||||||
class TextCodec
|
class TextCodec
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -12,9 +14,12 @@ public:
|
|||||||
QString toString(const QByteArray &ba) const;
|
QString toString(const QByteArray &ba) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
QTextCodec *_codec;
|
||||||
|
#else // QT6
|
||||||
QString from8bCp(const QByteArray &ba) const;
|
QString from8bCp(const QByteArray &ba) const;
|
||||||
|
|
||||||
const char32_t *_table;
|
const char32_t *_table;
|
||||||
|
#endif // QT6
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // TEXTCODEC_H
|
#endif // TEXTCODEC_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user