src/gui/text/qtextobject.cpp
changeset 33 3e2da88830cd
parent 18 2f34d5167611
child 37 758a864f9613
--- a/src/gui/text/qtextobject.cpp	Tue Jul 06 15:10:48 2010 +0300
+++ b/src/gui/text/qtextobject.cpp	Wed Aug 18 10:37:55 2010 +0300
@@ -1140,6 +1140,49 @@
 }
 
 /*!
+  \since 4.7
+
+  Returns the resolved text direction.
+
+  If the block has no explicit direction set, it will resolve the
+  direction from the blocks content. Returns either Qt::LeftToRight
+  or Qt::RightToLeft.
+
+  \sa QTextFormat::layoutDirection(), QString::isRightToLeft(), Qt::LayoutDirection
+*/
+Qt::LayoutDirection QTextBlock::textDirection() const
+{
+    Qt::LayoutDirection dir = blockFormat().layoutDirection();
+    if (dir != Qt::LayoutDirectionAuto)
+        return dir;
+
+    const QString buffer = p->buffer();
+
+    const int pos = position();
+    QTextDocumentPrivate::FragmentIterator it = p->find(pos);
+    QTextDocumentPrivate::FragmentIterator end = p->find(pos + length() - 1); // -1 to omit the block separator char
+    for (; it != end; ++it) {
+        const QTextFragmentData * const frag = it.value();
+        const QChar *p = buffer.constData() + frag->stringPosition;
+        const QChar * const end = p + frag->size_array[0];
+        while (p < end) {
+            switch(QChar::direction(p->unicode()))
+            {
+            case QChar::DirL:
+                return Qt::LeftToRight;
+            case QChar::DirR:
+            case QChar::DirAL:
+                return Qt::RightToLeft;
+            default:
+                break;
+            }
+            ++p;
+        }
+    }
+    return Qt::LeftToRight;
+}
+
+/*!
     Returns the block's contents as plain text.
 
     \sa length() charFormat() blockFormat()