QTextBoundaryFinder: Consider soft hyphen as line breaking opportunity
authorKonstantin Ritt <ritt.ks@gmail.com>
Fri, 25 May 2012 02:00:28 +0000 (05:00 +0300)
committerQt by Nokia <qt-info@nokia.com>
Thu, 7 Jun 2012 19:18:36 +0000 (21:18 +0200)
SoftHyphen enum value was added to specify such a boundary reason

Change-Id: I4248909eed6ab8cbca419de4dcf9fe917620a158
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
src/corelib/tools/qtextboundaryfinder.cpp
src/corelib/tools/qtextboundaryfinder.h
tests/auto/corelib/tools/qtextboundaryfinder/tst_qtextboundaryfinder.cpp

index b3b3d83..1e12d6e 100644 (file)
@@ -160,6 +160,8 @@ static void init(QTextBoundaryFinder::BoundaryType type, const QChar *chars, int
   \value NotAtBoundary  The boundary finder is not at a boundary position.
   \value StartWord  The boundary finder is at the start of a word.
   \value EndWord  The boundary finder is at the end of a word.
+  \value SoftHyphen  The boundary finder is at the soft hyphen
+                     (can occur for a Line boundary type only).
 */
 
 /*!
@@ -373,7 +375,7 @@ int QTextBoundaryFinder::toNextBoundary()
         break;
     case Line:
         Q_ASSERT(pos);
-        while (pos < length && d->attributes[pos-1].lineBreakType < HB_Break)
+        while (pos < length && d->attributes[pos-1].lineBreakType == HB_NoBreak)
             ++pos;
         break;
     }
@@ -415,7 +417,7 @@ int QTextBoundaryFinder::toPreviousBoundary()
             --pos;
         break;
     case Line:
-        while (pos > 0 && d->attributes[pos-1].lineBreakType < HB_Break)
+        while (pos > 0 && d->attributes[pos-1].lineBreakType == HB_NoBreak)
             --pos;
         break;
     }
@@ -440,7 +442,7 @@ bool QTextBoundaryFinder::isAtBoundary() const
     case Word:
         return d->attributes[pos].wordBoundary;
     case Line:
-        return (pos > 0) ? d->attributes[pos-1].lineBreakType >= HB_Break : true;
+        return (pos > 0) ? d->attributes[pos-1].lineBreakType != HB_NoBreak : true;
     case Sentence:
         return d->attributes[pos].sentenceBoundary;
     }
@@ -456,6 +458,8 @@ QTextBoundaryFinder::BoundaryReasons QTextBoundaryFinder::boundaryReasons() cons
         return NotAtBoundary;
     if (! isAtBoundary())
         return NotAtBoundary;
+    if (t == Line && pos < length && d->attributes[pos-1].lineBreakType == HB_SoftHyphen)
+        return SoftHyphen;
     if (pos == 0) {
         if (d->attributes[pos].whiteSpace)
             return NotAtBoundary;
index dc26939..61af89b 100644 (file)
@@ -70,8 +70,8 @@ public:
     enum BoundaryReason {
         NotAtBoundary = 0,
         StartWord = 1,
-        EndWord = 2
-        //Hyphen
+        EndWord = 2,
+        SoftHyphen = 4
     };
     Q_DECLARE_FLAGS( BoundaryReasons, BoundaryReason )
 
@@ -105,6 +105,8 @@ private:
     QTextBoundaryFinderPrivate *d;
 };
 
+Q_DECLARE_OPERATORS_FOR_FLAGS(QTextBoundaryFinder::BoundaryReasons)
+
 QT_END_NAMESPACE
 
 QT_END_HEADER
index d8dd0f6..e5a5f99 100644 (file)
@@ -70,6 +70,8 @@ private slots:
     void lineBoundaries_manual();
 
     void fastConstructor();
+    void isAtSoftHyphen_data();
+    void isAtSoftHyphen();
     void thaiLineBreak();
 };
 
@@ -485,7 +487,7 @@ void tst_QTextBoundaryFinder::lineBoundaries_manual_data()
         QChar s[] = { 0x0061, 0x00AD, 0x0062, 0x0009, 0x0063, 0x0064 };
         QString testString(s, sizeof(s)/sizeof(s[0]));
         QList<int> expectedBreakPositions;
-        expectedBreakPositions << 0 << 4 << 6;
+        expectedBreakPositions << 0 << 2 << 4 << 6;
 
         QTest::newRow("x(AL)x(BA)+(AL)x(BA)+(AL)x(AL)+") << testString << expectedBreakPositions;
     }
@@ -522,6 +524,34 @@ void tst_QTextBoundaryFinder::fastConstructor()
     QCOMPARE(finder.boundaryReasons(), QTextBoundaryFinder::NotAtBoundary);
 }
 
+void tst_QTextBoundaryFinder::isAtSoftHyphen_data()
+{
+    QTest::addColumn<QString>("testString");
+    QTest::addColumn<QList<int> >("expectedBreakPositions");
+
+    QString testString = QString::fromUtf8("I a-m break-able");
+    testString.replace(QLatin1Char('-'), QChar(0x00AD));
+    QList<int> expectedBreakPositions;
+    expectedBreakPositions << 0 << 2 << 4 << 6 << 12 << 16;
+    QTest::newRow("Soft Hyphen") << testString << expectedBreakPositions;
+}
+
+void tst_QTextBoundaryFinder::isAtSoftHyphen()
+{
+    QFETCH(QString, testString);
+    QFETCH(QList<int>, expectedBreakPositions);
+
+    doTestData(testString, expectedBreakPositions, QTextBoundaryFinder::Line);
+
+    QTextBoundaryFinder boundaryFinder(QTextBoundaryFinder::Line, testString);
+    for (int i = 0; (i = testString.indexOf(QChar(0x00AD), i)) != -1; ++i) {
+        QVERIFY(expectedBreakPositions.contains(i + 1));
+        boundaryFinder.setPosition(i + 1);
+        QVERIFY(boundaryFinder.isAtBoundary());
+        QVERIFY(boundaryFinder.boundaryReasons() == QTextBoundaryFinder::SoftHyphen);
+    }
+}
+
 #include <qlibrary.h>
 
 #define LIBTHAI_MAJOR   0