From c4320d24cd2d29c2e77958b4a8fd44f2bd587ca7 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Mon, 4 Dec 2006 13:50:46 +0000 Subject: [PATCH] Make string conversion work with non-ucs2-characters if using qt 4.2 * src/frontends/qt4/qt_helpers.[Ch] (toqstr): Use QString::fromUcs4 if the qt version is at least 4.2. (qstring_to_ucs4): Use QString::toUcs4 if the qt version is at least 4.2 (ucs4_to_qstring): Delete to avoid confusion, since it was only used in one place * src/frontends/qt4/panelstack.C (PanelStack::addCategory): Use toqstr instead of ucs4_to_qstring * src/support/unicode.[Ch] (ucs2_to_ucs4): Replace with utf16_to_ucs4 (ucs4_to_ucs2): Replace with ucs4_to_utf16 git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@16169 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/qt4/panelstack.C | 3 +- src/frontends/qt4/qt_helpers.C | 19 ++++++----- src/frontends/qt4/qt_helpers.h | 11 ++++++- src/support/unicode.C | 59 ++++++++++------------------------ src/support/unicode.h | 20 +++--------- 5 files changed, 43 insertions(+), 69 deletions(-) diff --git a/src/frontends/qt4/panelstack.C b/src/frontends/qt4/panelstack.C index fe9d8d0d20..f765407e35 100644 --- a/src/frontends/qt4/panelstack.C +++ b/src/frontends/qt4/panelstack.C @@ -60,8 +60,7 @@ void PanelStack::addCategory(docstring const & n, docstring const & parent) { QTreeWidgetItem * item; - QString name; - lyx::ucs4_to_qstring(n, name); + QString const name(toqstr(n)); lyxerr[Debug::GUI] << "addCategory n= " << lyx::to_utf8(n) << " parent= " << endl; diff --git a/src/frontends/qt4/qt_helpers.C b/src/frontends/qt4/qt_helpers.C index 587f1630c0..c19f1ef5ec 100644 --- a/src/frontends/qt4/qt_helpers.C +++ b/src/frontends/qt4/qt_helpers.C @@ -111,30 +111,33 @@ void lengthToWidgets(QLineEdit * input, LengthCombo * combo, } -void ucs4_to_qstring(lyx::docstring const & str, QString & s) +#if QT_VERSION < 0x040200 +// We use QString::fromUcs4 in Qt 4.2 and higher +QString const toqstr(docstring const & str) { + QString s; int i = static_cast(str.size()); s.resize(i); for (; --i >= 0;) s[i] = ucs4_to_qchar(str[i]); -} - - -QString const toqstr(docstring const & ucs4) -{ - QString s; - ucs4_to_qstring(ucs4, s); return s; } +#endif docstring const qstring_to_ucs4(QString const & qstr) { +#if QT_VERSION >= 0x040200 + QVector const ucs4 = qstr.toUcs4(); + return docstring(ucs4.begin(), ucs4.end()); +#else + // This does not properly convert surrogate pairs int const ls = qstr.size(); docstring ucs4; for (int i = 0; i < ls; ++i) ucs4 += static_cast(qstr[i].unicode()); return ucs4; +#endif } diff --git a/src/frontends/qt4/qt_helpers.h b/src/frontends/qt4/qt_helpers.h index 3bbe1afac0..ff2eaacfd8 100644 --- a/src/frontends/qt4/qt_helpers.h +++ b/src/frontends/qt4/qt_helpers.h @@ -87,9 +87,18 @@ inline QChar const ucs4_to_qchar(char_type const ucs4) { return QChar(static_cast(ucs4)); } + +#if QT_VERSION >= 0x040200 +inline QString const toqstr(docstring const & ucs4) +{ + // If possible we let qt do the work, since this version does not + // need to be superfast. + return QString::fromUcs4(reinterpret_cast(ucs4.data()), ucs4.length()); +} +#else QString const toqstr(docstring const & ucs4); +#endif -void ucs4_to_qstring(docstring const & str, QString & s); inline void ucs4_to_qstring(char_type const * str, size_t ls, QString & s) { diff --git a/src/support/unicode.C b/src/support/unicode.C index fcb47ca56d..6e814a7b9f 100644 --- a/src/support/unicode.C +++ b/src/support/unicode.C @@ -24,14 +24,23 @@ using std::endl; +namespace { + +#ifdef WORDS_BIGENDIAN + char const * utf16_codeset = "UTF16-BE"; +#else + char const * utf16_codeset = "UTF16-LE"; +#endif + +} + + namespace lyx { #ifdef WORDS_BIGENDIAN char const * ucs4_codeset = "UCS-4BE"; - char const * ucs2_codeset = "UCS-2BE"; #else char const * ucs4_codeset = "UCS-4LE"; - char const * ucs2_codeset = "UCS-2LE"; #endif static const iconv_t invalid_cd = (iconv_t)(-1); @@ -219,52 +228,18 @@ utf8_to_ucs4(char const * utf8str, size_t ls) } -lyx::char_type -ucs2_to_ucs4(unsigned short c) -{ - return ucs2_to_ucs4(&c, 1)[0]; -} - - -std::vector -ucs2_to_ucs4(std::vector const & ucs2str) -{ - if (ucs2str.empty()) - return std::vector(); - - return ucs2_to_ucs4(&ucs2str[0], ucs2str.size()); -} - - -std::vector -ucs2_to_ucs4(unsigned short const * ucs2str, size_t ls) -{ - static IconvProcessor processor(ucs4_codeset, ucs2_codeset); - return iconv_convert(processor, ucs2str, ls); -} - - -unsigned short -ucs4_to_ucs2(lyx::char_type c) -{ - return ucs4_to_ucs2(&c, 1)[0]; -} - - -std::vector -ucs4_to_ucs2(std::vector const & ucs4str) +std::vector +utf16_to_ucs4(unsigned short const * s, size_t ls) { - if (ucs4str.empty()) - return std::vector(); - - return ucs4_to_ucs2(&ucs4str[0], ucs4str.size()); + static IconvProcessor processor(ucs4_codeset, utf16_codeset); + return iconv_convert(processor, s, ls); } std::vector -ucs4_to_ucs2(lyx::char_type const * s, size_t ls) +ucs4_to_utf16(char_type const * s, size_t ls) { - static IconvProcessor processor(ucs2_codeset, ucs4_codeset); + static IconvProcessor processor(utf16_codeset, ucs4_codeset); return iconv_convert(processor, s, ls); } diff --git a/src/support/unicode.h b/src/support/unicode.h index e04f3b6ebb..16c4e00a03 100644 --- a/src/support/unicode.h +++ b/src/support/unicode.h @@ -67,24 +67,13 @@ std::vector utf8_to_ucs4(std::vector const & utf8str); std::vector utf8_to_ucs4(char const * utf8str, size_t ls); -// ucs2_to_ucs4 +// utf16_to_ucs4 -lyx::char_type ucs2_to_ucs4(unsigned short c); +std::vector utf16_to_ucs4(unsigned short const * s, size_t ls); -std::vector -ucs2_to_ucs4(std::vector const & ucs2str); - -std::vector -ucs2_to_ucs4(unsigned short const * ucs2str, size_t ls); - -// ucs4_to_ucs2 - -unsigned short ucs4_to_ucs2(lyx::char_type c); - -std::vector -ucs4_to_ucs2(std::vector const & ucs4str); +// ucs4_to_utf16 -std::vector ucs4_to_ucs2(lyx::char_type const * s, size_t ls); +std::vector ucs4_to_utf16(char_type const * s, size_t ls); // ucs4_to_utf8 @@ -105,7 +94,6 @@ std::vector ucs4_to_eightbit(lyx::char_type const * ucs4str, size_t ls, std::string const & encoding); extern char const * ucs4_codeset; -extern char const * ucs2_codeset; } // namespace lyx -- 2.39.5