+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.
#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);
*end = currentPosition;
}
-
}
+#endif
*/
#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 {
}
+#endif