1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 14:53:21 +02: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:
Martin Tůma 2021-10-25 10:30:09 +02:00
parent f810117cbe
commit 6b70f4a958
2 changed files with 73 additions and 6 deletions

View File

@ -1,6 +1,70 @@
#include <QVector>
#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[] = {
0x20AC, 0x0000, 0x201A, 0x0000, 0x201E, 0x2026, 0x2020, 0x2021,
0x0000, 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179,
@ -192,10 +256,7 @@ TextCodec::TextCodec(int codepage)
QString TextCodec::toString(const QByteArray &ba) const
{
if (_table)
return from8bCp(ba);
else
return QString::fromUtf8(ba);
return _table ? from8bCp(ba) : QString::fromUtf8(ba);
}
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());
}
#endif // QT6

View File

@ -3,6 +3,8 @@
#include <QString>
class QTextCodec;
class TextCodec
{
public:
@ -12,9 +14,12 @@ public:
QString toString(const QByteArray &ba) const;
private:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QTextCodec *_codec;
#else // QT6
QString from8bCp(const QByteArray &ba) const;
const char32_t *_table;
#endif // QT6
};
#endif // TEXTCODEC_H