From: Abdelrazak Younes Date: Wed, 30 Aug 2006 14:59:07 +0000 (+0000) Subject: * qt_helpers.[Ch]: new conversion from/to ucs4 strings and chars to/from QChar and... X-Git-Tag: 1.6.10~12681 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=4d5767fd9c2c5a61c8dd9ba8cdeca1d5706d2794;p=features.git * qt_helpers.[Ch]: new conversion from/to ucs4 strings and chars to/from QChar and QString. * qfont_metrics.C: make use of above methods instead of those in unicode.[Ch] * QLPainter.C: ditto * QLyXKeySym.C: ditto + simplification and a BOOST_ASSERT that verifies that the event is one char only. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@14852 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/frontends/qt4/QLPainter.C b/src/frontends/qt4/QLPainter.C index 3a388e389a..4fdbd7f28b 100644 --- a/src/frontends/qt4/QLPainter.C +++ b/src/frontends/qt4/QLPainter.C @@ -19,6 +19,7 @@ #include "FontLoader.h" #include "Application.h" +#include "qt_helpers.h" #include "debug.h" #include "language.h" @@ -239,11 +240,7 @@ void QLPainter::text(int x, int y, char_type const * s, size_t ls, for (unsigned int i = 0; i < ls; ++i) str[i] = QChar(encoding->ucs(s[i])); #else - //std::vector in(s, s + ls); - //std::vector ucs2 = ucs4_to_ucs2(in); - std::vector ucs2 = ucs4_to_ucs2(s, ls); - ucs2.push_back(0); - QString str = QString::fromUtf16(&ucs2[0]); + QString str = ucs4_to_qstring(s, ls); #endif #if 0 diff --git a/src/frontends/qt4/QLyXKeySym.C b/src/frontends/qt4/QLyXKeySym.C index 2149cd9e55..8260a6aff2 100644 --- a/src/frontends/qt4/QLyXKeySym.C +++ b/src/frontends/qt4/QLyXKeySym.C @@ -200,18 +200,11 @@ string QLyXKeySym::getSymbolName() const size_t QLyXKeySym::getUCSEncoded() const { - unsigned short const * ptr = text_.utf16(); - std::vector tmp(ptr, ptr + text_.length()); - - //lyxerr << "Data is " << tmp << endl; - lyxerr << "Length is " << text_.length() << endl; - - if (text_.isEmpty()) - return 0; + if (text_.isEmpty()) + return 0; - //size_t res = utf8_to_ucs4(tmp, tmp.length()); - //lyxerr << "Res is " << res << endl; - return ucs2_to_ucs4(tmp)[0]; + BOOST_ASSERT(text_.size() == 1); + return qchar_to_ucs4(text_[0]); } diff --git a/src/frontends/qt4/qfont_metrics.C b/src/frontends/qt4/qfont_metrics.C index 255f5a2557..f886ed79a0 100644 --- a/src/frontends/qt4/qfont_metrics.C +++ b/src/frontends/qt4/qfont_metrics.C @@ -16,6 +16,7 @@ #include "Application.h" #include "FontLoader.h" +#include "qt_helpers.h" #include "language.h" @@ -29,7 +30,7 @@ using std::string; namespace { -int smallcapswidth(unsigned short const * s, size_t ls, LyXFont const & f) +int smallcapswidth(QString const & s, LyXFont const & f) { if (!lyx_gui::use_gui) return 1; @@ -43,8 +44,10 @@ int smallcapswidth(unsigned short const * s, size_t ls, LyXFont const & f) int w = 0; + size_t const ls = s.size(); + for (size_t i = 0; i < ls; ++i) { - QChar const c = s[i]; + QChar const & c = s[i]; QChar const uc = c.toUpper(); if (c != uc) w += qsmallm.width(uc); @@ -80,7 +83,7 @@ int font_metrics::ascent(char_type c, LyXFont const & f) { if (!lyx_gui::use_gui) return 1; - QRect const & r = theApp->fontLoader().metrics(f).boundingRect(ucs4_to_ucs2(c)); + QRect const & r = theApp->fontLoader().metrics(f).boundingRect(ucs4_to_qchar(c)); // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y // value by the height: (x, -y-height, width, height). // Other versions return: (x, -y, width, height) @@ -96,7 +99,7 @@ int font_metrics::descent(char_type c, LyXFont const & f) { if (!lyx_gui::use_gui) return 1; - QRect const & r = theApp->fontLoader().metrics(f).boundingRect(ucs4_to_ucs2(c)); + QRect const & r = theApp->fontLoader().metrics(f).boundingRect(ucs4_to_qchar(c)); // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y // value by the height: (x, -y-height, width, height). // Other versions return: (x, -y, width, height) @@ -112,7 +115,7 @@ int font_metrics::lbearing(char_type c, LyXFont const & f) { if (!lyx_gui::use_gui) return 1; - return theApp->fontLoader().metrics(f).leftBearing(ucs4_to_ucs2(c)); + return theApp->fontLoader().metrics(f).leftBearing(ucs4_to_qchar(c)); } @@ -123,7 +126,7 @@ int font_metrics::rbearing(char_type c, LyXFont const & f) QFontMetrics const & m = theApp->fontLoader().metrics(f); // Qt rbearing is from the right edge of the char's width(). - unsigned short sc = ucs4_to_ucs2(c); + QChar sc = ucs4_to_qchar(c); return m.width(sc) - m.rightBearing(sc); } @@ -133,20 +136,19 @@ int font_metrics::width(char_type const * s, size_t ls, LyXFont const & f) if (!lyx_gui::use_gui) return ls; - std::vector ucs2 = ucs4_to_ucs2(s, ls); - ucs2.push_back(0); + QString ucs2 = ucs4_to_qstring(s, ls); if (f.realShape() == LyXFont::SMALLCAPS_SHAPE) - return smallcapswidth(&ucs2[0], ls, f); + return smallcapswidth(ucs2, f); QLFontInfo & fi = theApp->fontLoader().fontinfo(f); if (ls == 1) - return fi.width(ucs2[0]); + return fi.width(ucs2[0].unicode()); int w = 0; for (size_t i = 0; i < ls; ++i) - w += fi.width(ucs2[i]); + w += fi.width(ucs2[i].unicode()); return w; } diff --git a/src/frontends/qt4/qt_helpers.C b/src/frontends/qt4/qt_helpers.C index e92bd37955..348b0cf191 100644 --- a/src/frontends/qt4/qt_helpers.C +++ b/src/frontends/qt4/qt_helpers.C @@ -20,6 +20,8 @@ #include "support/lstrings.h" #include "support/convert.h" +#include "debug.h" + #include #include #include @@ -28,10 +30,13 @@ using lyx::support::isStrDbl; +using lyx::char_type; +using std::vector; using std::make_pair; using std::string; using std::pair; +using std::endl; string makeFontName(string const & family, string const & foundry) @@ -114,6 +119,61 @@ QString const toqstr(string const & str) } +QString const ucs4_to_qstring(char_type const * str, size_t ls) +{ + QString s; + + for (size_t i = 0; i < ls; ++i) + s.append(ucs4_to_qchar(str[i])); + + return s; +} + + +QString const ucs4_to_qstring(vector const & ucs4) +{ + QString s; + size_t const ls = ucs4.size(); + + for (size_t i = 0; i < ls; ++i) + s.append(ucs4_to_qchar(ucs4[i])); + + return s; +} + + +vector qstring_to_ucs4(QString const & qstr) +{ + size_t ls = qstr.size(); + vector ucs4; + for (size_t i = 0; i < ls; ++i) + ucs4.push_back(static_cast(qstr[i].unicode())); + + return ucs4; +} + + +void qstring_to_ucs4(QString const & qstr, vector & ucs4) +{ + size_t ls = qstr.size(); + ucs4.clear(); + for (size_t i = 0; i < ls; ++i) + ucs4.push_back(static_cast(qstr[i].unicode())); +} + + +char_type const qchar_to_ucs4(QChar const & qchar) +{ + return static_cast(qchar.unicode()); +} + + +QChar const ucs4_to_qchar(char_type const & ucs4) +{ + return QChar(static_cast(ucs4)); +} + + QString const qt_(char const * str) { return toqstr(_(str)); diff --git a/src/frontends/qt4/qt_helpers.h b/src/frontends/qt4/qt_helpers.h index 5b36ae8b31..bab194d43a 100644 --- a/src/frontends/qt4/qt_helpers.h +++ b/src/frontends/qt4/qt_helpers.h @@ -17,10 +17,15 @@ #include "lyxlength.h" //#include "lengthcombo.h" +#include "support/types.h" + +#include + class LengthCombo; class QComboBox; class QLineEdit; class QString; +class QChar; std::string makeFontName(std::string const & family, std::string const & foundry); @@ -56,6 +61,24 @@ QString const toqstr(char const * str); QString const toqstr(std::string const & str); +/** + * toqstr - convert ucs4 into QString + * + * QString uses ucs2 (a.k.a utf16) internally. + */ +QString const ucs4_to_qstring(lyx::char_type const * str, size_t ls); + +QString const ucs4_to_qstring(std::vector const & ucs4); + +std::vector qstring_to_ucs4(QString const & qstr); + +void qstring_to_ucs4(QString const & qstr, std::vector & ucs4); + +lyx::char_type const qchar_to_ucs4(QChar const & qchar); + +QChar const ucs4_to_qchar(lyx::char_type const & ucs4); + + /** * qt_ - i18nize string and convert to unicode *