diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2010-06-05 20:05:44 (GMT) |
---|---|---|
committer | Lars Knoll <lars.knoll@nokia.com> | 2010-06-09 22:07:52 (GMT) |
commit | fee7f7d67f780b798a63994a1125f9ca3ade1bd3 (patch) | |
tree | 534a237bac875be4e9c7cf471b86576d2234f779 /src/gui | |
parent | 7358af18674f6dbd9abf67f6e02809f43e2cfc3e (diff) | |
download | Qt-fee7f7d67f780b798a63994a1125f9ca3ade1bd3.zip Qt-fee7f7d67f780b798a63994a1125f9ca3ade1bd3.tar.gz Qt-fee7f7d67f780b798a63994a1125f9ca3ade1bd3.tar.bz2 |
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')
-rw-r--r-- | src/gui/text/qtextobject.cpp | 43 | ||||
-rw-r--r-- | src/gui/text/qtextobject.h | 2 |
2 files changed, 45 insertions, 0 deletions
diff --git a/src/gui/text/qtextobject.cpp b/src/gui/text/qtextobject.cpp index 088eb98..f386871 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() diff --git a/src/gui/text/qtextobject.h b/src/gui/text/qtextobject.h index 67f67d8..a573a26 100644 --- a/src/gui/text/qtextobject.h +++ b/src/gui/text/qtextobject.h @@ -221,6 +221,8 @@ public: QTextCharFormat charFormat() const; int charFormatIndex() const; + Qt::LayoutDirection textDirection() const; + QString text() const; const QTextDocument *document() const; |