[poppler] qt4/src qt4/tests

Pino Toscano pino at kemper.freedesktop.org
Mon Jan 17 12:40:24 PST 2011


 qt4/src/poppler-private.cc  |   30 ++++++++++++++++++++++++------
 qt4/tests/check_strings.cpp |   21 +++++++++++++++++++++
 2 files changed, 45 insertions(+), 6 deletions(-)

New commits:
commit 07a8808c22445c421f3064da7e5227dcbf40358b
Author: Pino Toscano <pino at kde.org>
Date:   Mon Jan 17 21:34:23 2011 +0100

    fix unicodeToQString() to correctly decode the Unicode sequence
    
    Use a UnicodeMap to convert the sequence to UTF-8, and convert from that to QString.
    Also, ignore the last character of the Unicode sequence if it is 0x0.
    
    Add a couple of testcases for it.

diff --git a/qt4/src/poppler-private.cc b/qt4/src/poppler-private.cc
index 3c30ff2..9599af4 100644
--- a/qt4/src/poppler-private.cc
+++ b/qt4/src/poppler-private.cc
@@ -29,6 +29,7 @@
 
 #include <Link.h>
 #include <Outline.h>
+#include <UnicodeMap.h>
 
 namespace Poppler {
 
@@ -69,12 +70,29 @@ namespace Debug {
     }
 
     QString unicodeToQString(Unicode* u, int len) {
-        QString ret;
-        ret.resize(len);
-        QChar* qch = (QChar*) ret.unicode();
-        for (;len;--len)
-          *qch++ = (QChar) *u++;
-        return ret;
+        static UnicodeMap *uMap = 0;
+        if (!uMap)
+        {
+                GooString enc("UTF-8");
+                uMap = globalParams->getUnicodeMap(&enc);
+                uMap->incRefCnt();
+        }
+
+        // ignore the last character if it is 0x0
+        if ((len > 0) && (u[len - 1] == 0))
+        {
+            --len;
+        }
+
+        GooString convertedStr;
+        for (int i = 0; i < len; ++i)
+        {
+            char buf[8];
+            const int n = uMap->mapUnicode(u[i], buf, sizeof(buf));
+            convertedStr.append(buf, n);
+        }
+
+        return QString::fromUtf8(convertedStr.getCString(), convertedStr.getLength());
     }
 
     QString UnicodeParsedString(GooString *s1) {
diff --git a/qt4/tests/check_strings.cpp b/qt4/tests/check_strings.cpp
index 7c3e3ed..1569a17 100644
--- a/qt4/tests/check_strings.cpp
+++ b/qt4/tests/check_strings.cpp
@@ -21,6 +21,8 @@
 #include <poppler-qt4.h>
 #include <poppler-private.h>
 
+#include <GlobalParams.h>
+
 Q_DECLARE_METATYPE(GooString*)
 Q_DECLARE_METATYPE(Unicode*)
 
@@ -51,11 +53,15 @@ void TestStrings::initTestCase()
 {
     qRegisterMetaType<GooString*>("GooString*");
     qRegisterMetaType<Unicode*>("Unicode*");
+
+    globalParams = new GlobalParams();
 }
 
 void TestStrings::cleanupTestCase()
 {
     qDeleteAll(m_gooStrings);
+
+    delete globalParams;
 }
 
 void TestStrings::check_unicodeToQString_data()
@@ -90,6 +96,21 @@ void TestStrings::check_unicodeToQString_data()
     u[1] = 0x0161;
     QTest::newRow("a\u0161") << u << l << QString::fromUtf8("a\u0161");
     }
+    {
+    const int l = 2;
+    Unicode *u = new Unicode[l];
+    u[0] = 0x5c01;
+    u[1] = 0x9762;
+    QTest::newRow("\xe5\xb0\x81\xe9\x9d\xa2") << u << l << QString::fromUtf8("\xe5\xb0\x81\xe9\x9d\xa2");
+    }
+    {
+    const int l = 3;
+    Unicode *u = new Unicode[l];
+    u[0] = 0x5c01;
+    u[1] = 0x9762;
+    u[2] = 0x0;
+    QTest::newRow("\xe5\xb0\x81\xe9\x9d\xa2 + 0") << u << l << QString::fromUtf8("\xe5\xb0\x81\xe9\x9d\xa2");
+    }
 }
 
 void TestStrings::check_unicodeToQString()


More information about the poppler mailing list