]> git.lyx.org Git - features.git/commitdiff
* qt_helpers.[Ch]: new conversion from/to ucs4 strings and chars to/from QChar and...
authorAbdelrazak Younes <younes@lyx.org>
Wed, 30 Aug 2006 14:59:07 +0000 (14:59 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Wed, 30 Aug 2006 14:59:07 +0000 (14:59 +0000)
* 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

src/frontends/qt4/QLPainter.C
src/frontends/qt4/QLyXKeySym.C
src/frontends/qt4/qfont_metrics.C
src/frontends/qt4/qt_helpers.C
src/frontends/qt4/qt_helpers.h

index 3a388e389a783279a7bb108143f3df6dcc7297fb..4fdbd7f28bea087c1f8ec55f858c4987841c354f 100644 (file)
@@ -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<boost::uint32_t> in(s, s + ls);
-       //std::vector<unsigned short> ucs2 = ucs4_to_ucs2(in);
-       std::vector<unsigned short> 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
index 2149cd9e5597810220bff0dbe506c23510e12797..8260a6aff2c48b8bf2c3266be6edd486537f6ee9 100644 (file)
@@ -200,18 +200,11 @@ string QLyXKeySym::getSymbolName() const
 
 size_t QLyXKeySym::getUCSEncoded() const
 {
-    unsigned short const * ptr = text_.utf16();
-    std::vector<unsigned short> 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]);
 }
 
 
index 255f5a25576ccbcc24aebc048bc1c1eec7c02153..f886ed79a064efaea5a014dbd24f9147f9d7dcca 100644 (file)
@@ -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<unsigned short> 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;
 }
index e92bd379550baa1eeef0f7ec6742cdce67e520ad..348b0cf191fc6b8c651216f583c7e9078d48e4d0 100644 (file)
@@ -20,6 +20,8 @@
 #include "support/lstrings.h"
 #include "support/convert.h"
 
+#include "debug.h"
+
 #include <QComboBox>
 #include <qlineedit.h>
 #include <qtextcodec.h>
 
 
 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<char_type> 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<char_type> qstring_to_ucs4(QString const & qstr)
+{
+       size_t ls = qstr.size();
+       vector<char_type> ucs4;
+       for (size_t i = 0; i < ls; ++i)
+               ucs4.push_back(static_cast<boost::uint32_t>(qstr[i].unicode()));
+
+       return ucs4;
+}
+
+
+void qstring_to_ucs4(QString const & qstr, vector<char_type> & ucs4)
+{
+       size_t ls = qstr.size();
+       ucs4.clear();
+       for (size_t i = 0; i < ls; ++i)
+               ucs4.push_back(static_cast<boost::uint32_t>(qstr[i].unicode()));
+}
+
+
+char_type const qchar_to_ucs4(QChar const & qchar)
+{
+       return static_cast<boost::uint32_t>(qchar.unicode());
+}
+
+
+QChar const ucs4_to_qchar(char_type const & ucs4)
+{
+       return QChar(static_cast<unsigned short>(ucs4));
+}
+
+
 QString const qt_(char const * str)
 {
        return toqstr(_(str));
index 5b36ae8b31824b99b2a307dffa37411116806630..bab194d43a2b4e2ca40f10ac535e243f81750e49 100644 (file)
 #include "lyxlength.h"
 //#include "lengthcombo.h"
 
+#include "support/types.h"
+
+#include <vector>
+
 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<lyx::char_type> const & ucs4);
+
+std::vector<lyx::char_type> qstring_to_ucs4(QString const & qstr);
+
+void qstring_to_ucs4(QString const & qstr, std::vector<lyx::char_type> & 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
  *