summaryrefslogtreecommitdiffstats
path: root/src/gui/text/qtextobject.cpp
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@nokia.com>2010-06-05 22:05:44 +0200
committerLars Knoll <lars.knoll@nokia.com>2010-06-10 00:07:52 +0200
commitfee7f7d67f780b798a63994a1125f9ca3ade1bd3 (patch)
tree534a237bac875be4e9c7cf471b86576d2234f779 /src/gui/text/qtextobject.cpp
parent7358af18674f6dbd9abf67f6e02809f43e2cfc3e (diff)
Add QTextBlock::textDirection()
The method returns the resolved text direction for the block. It implements P1-P3 of the Unicode bidi algorithm. Task-number: Part of Qt-3292 Reviewed-by: Simon Hausmann
Diffstat (limited to 'src/gui/text/qtextobject.cpp')
-rw-r--r--src/gui/text/qtextobject.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/gui/text/qtextobject.cpp b/src/gui/text/qtextobject.cpp
index 088eb9891d..f386871d5e 100644
--- a/src/gui/text/qtextobject.cpp
+++ b/src/gui/text/qtextobject.cpp
@@ -1140,6 +1140,49 @@ int QTextBlock::charFormatIndex() const
}
/*!
+ \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 QTextBlock::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()