use the new QTextBoundaryFinder class in Qt.
authorlars <lars@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 24 Oct 2007 12:32:09 +0000 (12:32 +0000)
committerlars <lars@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 24 Oct 2007 12:32:09 +0000 (12:32 +0000)
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@26963 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/platform/qt/TextBoundaries.cpp
WebCore/platform/qt/TextBreakIteratorQt.cpp

index 942eecc7b1bd009aa0bf6168ac13513ef2264b75..0159f0120ddecc1e6eb406e85c4ab7a314f53061 100644 (file)
@@ -1,3 +1,26 @@
+2007-10-24  Lars Knoll  <lars@trolltech.com>
+
+        Reviewed by Simon.
+
+        use the new QTextBoundaryFinder class in Qt.
+
+        WARNING: NO TEST CASES ADDED OR CHANGED
+
+        * platform/qt/TextBoundaries.cpp:
+        (WebCore::findNextWordFromIndex):
+        (WebCore::findWordBoundary):
+        * platform/qt/TextBreakIteratorQt.cpp:
+        (WebCore::wordBreakIterator):
+        (WebCore::characterBreakIterator):
+        (WebCore::lineBreakIterator):
+        (WebCore::sentenceBreakIterator):
+        (WebCore::textBreakFirst):
+        (WebCore::textBreakNext):
+        (WebCore::textBreakPreceding):
+        (WebCore::textBreakFollowing):
+        (WebCore::textBreakCurrent):
+        (WebCore::isTextBreak):
+
 2007-10-24  Darin Adler  <darin@apple.com>
 
         Reviewed by Maciej.
index fb7ee8052ba216986500158ddf6734e05591b122..bdc851b9304fe4453a477df88d8120c4cb77a43f 100644 (file)
 #include <QDebug>
 #include <stdio.h>
 
+#if QT_VERSION >= 0x040400
+#include <qtextboundaryfinder.h>
 
-// This is very primitive. When I'll have time I'll do the "proper" implementation based on
-// http://www.unicode.org/reports/tr29/tr29-4.html
 namespace WebCore
 {
 
+int findNextWordFromIndex(UChar const* buffer, int len, int position, bool forward)
+{
+    QString str(reinterpret_cast<QChar const*>(buffer), len);
+    QTextBoundaryFinder iterator(QTextBoundaryFinder::Word, str);
+    iterator.setPosition(position >= len ? len - 1 : position);
+    if (forward) {
+        int pos = iterator.toNextBoundary();
+        while (pos > 0) {
+            if (QChar(buffer[pos-1]).isLetterOrNumber())
+                return pos;
+            pos = iterator.toNextBoundary();
+        }
+        return len;
+    } else {
+        int pos = iterator.toPreviousBoundary();
+        while (pos > 0) {
+            if (QChar(buffer[pos]).isLetterOrNumber())
+                return pos;
+            pos = iterator.toPreviousBoundary();
+        }
+        return 0;
+    }
+}
+
+void findWordBoundary(UChar const* buffer, int len, int position, int* start, int* end)
+{
+    QString str(reinterpret_cast<QChar const*>(buffer), len);
+    QTextBoundaryFinder iterator(QTextBoundaryFinder::Word, str);
+    iterator.setPosition(position);
+    *start = position > 0 ? iterator.toPreviousBoundary() : 0;
+    *end = position == len ? len : iterator.toNextBoundary();
+}
+
+}
+
+#else
+namespace WebCore
+{
+    
 int findNextWordFromIndex(UChar const* buffer, int len, int position, bool forward)
 {
     QString str(reinterpret_cast<QChar const*>(buffer), len);
@@ -82,5 +121,5 @@ void findWordBoundary(UChar const* buffer, int len, int position, int* start, in
     *end = currentPosition;
 }
 
-
 }
+#endif
index f8e8f4f4c7d333022eca14fd6b3bbac7c6271bb5..1852e7009f2a662972e49f63502092979ae2e099 100644 (file)
  */
 
 #include "TextBreakIterator.h"
+
+#if QT_VERSION >= 0x040400
+#include <QtCore/qtextboundaryfinder.h>
+#include <qdebug.h>
+
+#ifdef DEBUG_TEXT_ITERATORS
+#define DEBUG qDebug
+#else
+#define DEBUG if (1) {} else qDebug
+#endif
+
+namespace WebCore {
+
+    class TextBreakIterator : public QTextBoundaryFinder
+    {
+    };
+
+
+    TextBreakIterator* wordBreakIterator(const UChar* string, int length)
+    {
+        static QTextBoundaryFinder* iterator = 0;
+        static const UChar* cachedString = 0;
+        static int cachedLength = 0;
+        if (string != cachedString || length != cachedLength) {
+            if (!iterator)
+                iterator = new QTextBoundaryFinder;
+
+            QString s((const QChar*)string, length);
+            DEBUG() << "wordBreakIterator" << length << s;
+            *iterator = QTextBoundaryFinder(QTextBoundaryFinder::Word, s);
+            cachedString = string;
+            cachedLength = length;
+        } else {
+            iterator->setPosition(0);
+        }
+        return static_cast<TextBreakIterator*>(iterator);
+    }
+
+    TextBreakIterator* characterBreakIterator(const UChar* string, int length)
+    {
+        static QTextBoundaryFinder* iterator = 0;
+        static const UChar* cachedString = 0;
+        static int cachedLength = 0;
+        if (string != cachedString || length != cachedLength) {
+            if (!iterator)
+                iterator = new QTextBoundaryFinder;
+
+            QString s((const QChar*)string, length);
+            DEBUG() << "characterBreakIterator" << length << s;
+            *iterator = QTextBoundaryFinder(QTextBoundaryFinder::Grapheme, s);
+            cachedString = string;
+            cachedLength = length;
+        } else {
+            iterator->setPosition(0);
+        }
+        return static_cast<TextBreakIterator*>(iterator);
+    }
+
+    TextBreakIterator* lineBreakIterator(const UChar* string, int length)
+    {
+        static QTextBoundaryFinder *iterator = 0;
+        static const UChar *cachedString = 0;
+        static int cachedLength = 0;
+        if (string != cachedString || length != cachedLength) {
+            if (!iterator)
+                iterator = new QTextBoundaryFinder;
+
+            QString s((const QChar *)string, length);
+            DEBUG() << "lineBreakIterator" << length << s;
+            *iterator = QTextBoundaryFinder(QTextBoundaryFinder::Line, s);
+            cachedString = string;
+            cachedLength = length;
+        } else {
+            iterator->setPosition(0);
+        }
+        return static_cast<TextBreakIterator*>(iterator);
+    }
+
+    TextBreakIterator* sentenceBreakIterator(const UChar* string, int length)
+    {
+        static QTextBoundaryFinder* iterator = 0;
+        static const UChar* cachedString = 0;
+        static int cachedLength = 0;
+        if (string != cachedString || length != cachedLength) {
+            if (!iterator)
+                iterator = new QTextBoundaryFinder;
+
+            QString s((const QChar*)string, length);
+            DEBUG() << "sentenceBreakIterator" << length << s;
+            *iterator = QTextBoundaryFinder(QTextBoundaryFinder::Sentence, s);
+            cachedString = string;
+            cachedLength = length;
+        } else {
+            iterator->setPosition(0);
+        }
+        return static_cast<TextBreakIterator*>(iterator);
+    }
+
+    int textBreakFirst(TextBreakIterator* bi)
+    {
+        bi->toStart();
+        DEBUG() << "textBreakFirst" << bi->position();
+        return bi->position();
+    }
+
+    int textBreakNext(TextBreakIterator* bi)
+    {
+        int pos = bi->toNextBoundary();
+        DEBUG() << "textBreakNext" << pos;
+        return pos;
+    }
+
+    int textBreakPreceding(TextBreakIterator* bi, int pos)
+    {
+        bi->setPosition(pos);
+        int newpos = bi->toPreviousBoundary();
+        DEBUG() << "textBreakPreceding" << pos << newpos;
+        return newpos;
+    }
+
+    int textBreakFollowing(TextBreakIterator* bi, int pos)
+    {
+        bi->setPosition(pos);
+        int newpos = bi->toNextBoundary();
+        DEBUG() << "textBreakFollowing" << pos << newpos;
+        return newpos;
+    }
+
+    int textBreakCurrent(TextBreakIterator* bi)
+    {
+        return bi->position();
+    }
+
+    bool isTextBreak(TextBreakIterator*, int)
+    {
+        return true;
+    }
+
+}
+#else
 #include <qtextlayout.h>
 
 namespace WebCore {
@@ -187,3 +327,4 @@ bool isTextBreak(TextBreakIterator*, int)
 
 }
 
+#endif