QFontMetrics/QRawFont: Optimize SMP code points handling a bit
authorKonstantin Ritt <ritt.ks@gmail.com>
Sun, 10 Jun 2012 15:30:10 +0000 (18:30 +0300)
committerQt by Nokia <qt-info@nokia.com>
Thu, 14 Jun 2012 03:22:11 +0000 (05:22 +0200)
Calling QString::fromUcs4() for the single UCS-4 -encoded character is quite suboptimal
since the BOM detections and the resulting QString aren't really used;
all we need is to split the UCS-4 code point into the UCS-2 surrogate pair.

Change-Id: Ia5b68312909bf551cf2493d9e2752a7d7d837fb9
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
src/gui/text/qfontengine_p.h
src/gui/text/qfontmetrics.cpp
src/gui/text/qrawfont.cpp
src/gui/text/qrawfont.h

index 4741de3..bcf763d 100644 (file)
@@ -225,6 +225,18 @@ public:
     virtual const char *name() const = 0;
 
     virtual bool canRender(const QChar *string, int len) = 0;
+    inline bool canRender(uint ucs4) {
+        QChar utf16[2];
+        int utf16len = 1;
+        if (QChar::requiresSurrogates(ucs4)) {
+            utf16[0] = QChar::highSurrogate(ucs4);
+            utf16[1] = QChar::lowSurrogate(ucs4);
+            ++utf16len;
+        } else {
+            utf16[0] = QChar(ucs4);
+        }
+        return canRender(utf16, utf16len);
+    }
 
     virtual Type type() const = 0;
 
index 83d51e2..e5ddcb8 100644 (file)
@@ -413,8 +413,7 @@ bool QFontMetrics::inFontUcs4(uint ucs4) const
     Q_ASSERT(engine != 0);
     if (engine->type() == QFontEngine::Box)
         return false;
-    QString utf16 = QString::fromUcs4(&ucs4, 1);
-    return engine->canRender(utf16.data(), utf16.length());
+    return engine->canRender(ucs4);
 }
 
 /*!
@@ -1274,8 +1273,7 @@ bool QFontMetricsF::inFontUcs4(uint ucs4) const
     Q_ASSERT(engine != 0);
     if (engine->type() == QFontEngine::Box)
         return false;
-    QString utf16 = QString::fromUcs4(&ucs4, 1);
-    return engine->canRender(utf16.data(), utf16.length());
+    return engine->canRender(ucs4);
 }
 
 /*!
index 3bd4d88..ae14f1c 100644 (file)
@@ -620,17 +620,15 @@ bool QRawFont::supportsCharacter(QChar character) const
 }
 
 /*!
+    \overload
+
    Returns true if the font has a glyph that corresponds to the UCS-4 encoded character \a ucs4.
 
    \sa supportedWritingSystems()
 */
-bool QRawFont::supportsCharacter(quint32 ucs4) const
+bool QRawFont::supportsCharacter(uint ucs4) const
 {
-    if (!d->isValid())
-        return false;
-
-    QString str = QString::fromUcs4(&ucs4, 1);
-    return d->fontEngine->canRender(str.constData(), str.size());
+    return d->isValid() && d->fontEngine->canRender(ucs4);
 }
 
 // qfontdatabase.cpp
index bc5f662..870e714 100644 (file)
@@ -126,7 +126,7 @@ public:
                       qreal pixelSize,
                       QFont::HintingPreference hintingPreference);
 
-    bool supportsCharacter(quint32 ucs4) const;
+    bool supportsCharacter(uint ucs4) const;
     bool supportsCharacter(QChar character) const;
     QList<QFontDatabase::WritingSystem> supportedWritingSystems() const;