From 3032ba0f8eae871db2f36443f26a7977b5261740 Mon Sep 17 00:00:00 2001 From: Jiang Jiang Date: Tue, 10 May 2011 13:30:33 +0200 Subject: Add some QRawFont related low level functions to avoid extra copying Added functions: - QRawFont::glyphIndexesForChars(const QChar *chars, int numChars, quint32 *glyphIndexes, int *numGlyphs) const - QRawFont::advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs) const Reviewed-by: Eskil (cherry picked from commit 965af9eb2932efae5d736df54c3859460017b6a5) --- src/gui/text/qrawfont.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++---- src/gui/text/qrawfont.h | 2 ++ 2 files changed, 55 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/gui/text/qrawfont.cpp b/src/gui/text/qrawfont.cpp index 29394e9b7a..44ddfd2d75 100644 --- a/src/gui/text/qrawfont.cpp +++ b/src/gui/text/qrawfont.cpp @@ -428,7 +428,7 @@ int QRawFont::weight() const of the text. To get the correctly shaped text, you can use QTextLayout to lay out and shape the text, and then call QTextLayout::glyphRuns() to get the set of glyph index list and QRawFont pairs. - \sa advancesForGlyphIndexes(), QGlyphRun, QTextLayout::glyphRuns(), QTextFragment::glyphRuns() + \sa advancesForGlyphIndexes(), glyphIndexesForChars(), QGlyphRun, QTextLayout::glyphRuns(), QTextFragment::glyphRuns() */ QVector QRawFont::glyphIndexesForString(const QString &text) const { @@ -437,11 +437,9 @@ QVector QRawFont::glyphIndexesForString(const QString &text) const int nglyphs = text.size(); QVarLengthGlyphLayoutArray glyphs(nglyphs); - if (!d->fontEngine->stringToCMap(text.data(), text.size(), &glyphs, &nglyphs, - QTextEngine::GlyphIndicesOnly)) { + if (!glyphIndexesForChars(text.data(), text.size(), glyphs.glyphs, &nglyphs)) { glyphs.resize(nglyphs); - if (!d->fontEngine->stringToCMap(text.data(), text.size(), &glyphs, &nglyphs, - QTextEngine::GlyphIndicesOnly)) { + if (!glyphIndexesForChars(text.data(), text.size(), glyphs.glyphs, &nglyphs)) { Q_ASSERT_X(false, Q_FUNC_INFO, "stringToCMap shouldn't fail twice"); return QVector(); } @@ -454,6 +452,26 @@ QVector QRawFont::glyphIndexesForString(const QString &text) const return glyphIndexes; } +/*! + Converts a string of unicode points to glyph indexes using the CMAP table in the + underlying font. The function works like glyphIndexesForString() except it take + an array (\a chars), the results will be returned though \a glyphIndexes array + and number of glyphs will be set in \a numGlyphs. The size of \a glyphIndexes array + must be at least \a numChars, if that's still not enough, this function will return + false, then you can resize \a glyphIndexes from the size returned in \a numGlyphs. + + \sa glyphIndexesForString(), advancesForGlyphIndexes(), QGlyphs, QTextLayout::glyphs(), QTextFragment::glyphs() +*/ +bool QRawFont::glyphIndexesForChars(const QChar *chars, int numChars, quint32 *glyphIndexes, int *numGlyphs) const +{ + if (!isValid()) + return false; + + QGlyphLayout glyphs; + glyphs.glyphs = glyphIndexes; + return d->fontEngine->stringToCMap(chars, numChars, &glyphs, numGlyphs, QTextEngine::GlyphIndicesOnly); +} + /*! Returns the QRawFont's advances for each of the \a glyphIndexes in pixel units. The advances give the distance from the position of a given glyph to where the next glyph should be drawn @@ -479,6 +497,36 @@ QVector QRawFont::advancesForGlyphIndexes(const QVector &glyph return advances; } +/*! + Returns the QRawFont's advances for each of the \a glyphIndexes in pixel units. The advances + give the distance from the position of a given glyph to where the next glyph should be drawn + to make it appear as if the two glyphs are unspaced. The glyph indexes are given with the + array \a glyphIndexes while the results are returned through \a advances, both of them must + have \a numGlyphs elements. + + \sa QTextLine::horizontalAdvance(), QFontMetricsF::width() +*/ +bool QRawFont::advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs) const +{ + if (!isValid()) + return false; + + QGlyphLayout glyphs; + glyphs.glyphs = const_cast(glyphIndexes); + glyphs.numGlyphs = numGlyphs; + QVarLengthArray advances_x(numGlyphs); + QVarLengthArray advances_y(numGlyphs); + glyphs.advances_x = advances_x.data(); + glyphs.advances_y = advances_y.data(); + + d->fontEngine->recalcAdvances(&glyphs, 0); + + for (int i=0; i glyphIndexesForString(const QString &text) const; QVector advancesForGlyphIndexes(const QVector &glyphIndexes) const; + bool glyphIndexesForChars(const QChar *chars, int numChars, quint32 *glyphIndexes, int *numGlyphs) const; + bool advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs) const; QImage alphaMapForGlyph(quint32 glyphIndex, AntialiasingType antialiasingType = SubPixelAntialiasing, -- cgit v1.2.3 From 16628b76c0d24927cb6160447a1439d40a4f0fea Mon Sep 17 00:00:00 2001 From: Jiang Jiang Date: Wed, 11 May 2011 12:13:01 +0200 Subject: Move QTextCursor::MoveStyle to Qt namespace We cannot use QTextCursor::MoveStyle enums in QTextLine because QTextCursor is not a QObject, while referring to that enum in Q_PROPERTY requires it to be. That's why we need to move the enums in Qt namespace. Reviewed-by: David Boddie (cherry picked from commit 5eba82b752e85a5d6cb3a893214ed2646d75f362) --- src/corelib/global/qnamespace.h | 6 ++++++ src/gui/text/qtextcursor.cpp | 18 +++++++++--------- src/gui/text/qtextcursor.h | 4 ---- src/gui/text/qtextdocument.cpp | 6 +++--- src/gui/text/qtextdocument.h | 6 +++--- src/gui/text/qtextdocument_p.cpp | 2 +- src/gui/text/qtextdocument_p.h | 2 +- src/gui/text/qtextengine_p.h | 2 +- src/gui/text/qtextlayout.cpp | 14 +++++++------- src/gui/text/qtextlayout.h | 4 ++-- src/gui/widgets/qlinecontrol.cpp | 2 +- src/gui/widgets/qlinecontrol_p.h | 8 ++++---- src/gui/widgets/qlineedit.cpp | 8 ++++---- src/gui/widgets/qlineedit.h | 5 +++-- 14 files changed, 45 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/corelib/global/qnamespace.h b/src/corelib/global/qnamespace.h index 75ce68afbf..f086513e33 100644 --- a/src/corelib/global/qnamespace.h +++ b/src/corelib/global/qnamespace.h @@ -94,6 +94,7 @@ Qt { Q_ENUMS(GestureState) Q_ENUMS(GestureType) #endif + Q_ENUMS(CursorMoveStyle) #endif // (defined(Q_MOC_RUN) || defined(QT_JAMBI_RUN)) #if defined(Q_MOC_RUN) @@ -1784,6 +1785,11 @@ public: NavigationModeCursorAuto, NavigationModeCursorForceVisible }; + + enum CursorMoveStyle { + LogicalMoveStyle, + VisualMoveStyle + }; } #ifdef Q_MOC_RUN ; diff --git a/src/gui/text/qtextcursor.cpp b/src/gui/text/qtextcursor.cpp index 4f6857a201..17bcc6c7aa 100644 --- a/src/gui/text/qtextcursor.cpp +++ b/src/gui/text/qtextcursor.cpp @@ -362,7 +362,7 @@ bool QTextCursorPrivate::movePosition(QTextCursor::MoveOperation op, QTextCursor currentCharFormat = -1; bool adjustX = true; QTextBlock blockIt = block(); - bool visualMovement = priv->defaultCursorMoveStyle == QTextCursor::Visual; + bool visualMovement = priv->defaultCursorMoveStyle == Qt::VisualMoveStyle; if (!blockIt.isValid()) return false; @@ -2568,18 +2568,18 @@ QTextDocument *QTextCursor::document() const } /*! - \enum QTextCursor::MoveStyle + \enum Qt::CursorMoveStyle - This enum describes the movement style available to QTextCursor. The options + This enum describes the movement style available to text cursors. The options are: - \value Logical Within a left-to-right text block, increase cursor position - when pressing left arrow key, decrease cursor position when pressing the - right arrow key. If the text block is right-to-left, the opposite behavior + \value LogicalMoveStyle Within a left-to-right text block, decrease cursor + position when pressing left arrow key, increase cursor position when pressing + the right arrow key. If the text block is right-to-left, the opposite behavior applies. - \value Visual Pressing the left arrow key will always cause the cursor to move - left, regardless of the text's writing direction. The same behavior applies to - right arrow key. + \value VisualMoveStyle Pressing the left arrow key will always cause the cursor + to move left, regardless of the text's writing direction. Pressing the right + arrow key will always cause the cursor to move right. */ QT_END_NAMESPACE diff --git a/src/gui/text/qtextcursor.h b/src/gui/text/qtextcursor.h index 9e4c0b82df..4eaeb41ee9 100644 --- a/src/gui/text/qtextcursor.h +++ b/src/gui/text/qtextcursor.h @@ -86,10 +86,6 @@ public: MoveAnchor, KeepAnchor }; - enum MoveStyle { - Logical, - Visual - }; void setPosition(int pos, MoveMode mode = MoveAnchor); int position() const; diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index 9169955617..0d33912420 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -589,9 +589,9 @@ void QTextDocument::setDefaultTextOption(const QTextOption &option) \since 4.8 The default cursor movement style is used by all QTextCursor objects - created from the document. The default is QTextCursor::Logical. + created from the document. The default is Qt::LogicalMoveStyle. */ -QTextCursor::MoveStyle QTextDocument::defaultCursorMoveStyle() const +Qt::CursorMoveStyle QTextDocument::defaultCursorMoveStyle() const { Q_D(const QTextDocument); return d->defaultCursorMoveStyle; @@ -602,7 +602,7 @@ QTextCursor::MoveStyle QTextDocument::defaultCursorMoveStyle() const Set the default cursor movement style. */ -void QTextDocument::setDefaultCursorMoveStyle(QTextCursor::MoveStyle style) +void QTextDocument::setDefaultCursorMoveStyle(Qt::CursorMoveStyle style) { Q_D(QTextDocument); d->defaultCursorMoveStyle = style; diff --git a/src/gui/text/qtextdocument.h b/src/gui/text/qtextdocument.h index e515b36cc0..b826dc1777 100644 --- a/src/gui/text/qtextdocument.h +++ b/src/gui/text/qtextdocument.h @@ -46,7 +46,6 @@ #include #include #include -#include QT_BEGIN_HEADER @@ -70,6 +69,7 @@ class QUrl; class QVariant; class QRectF; class QTextOption; +class QTextCursor; template class QVector; @@ -269,8 +269,8 @@ public: QTextOption defaultTextOption() const; void setDefaultTextOption(const QTextOption &option); - QTextCursor::MoveStyle defaultCursorMoveStyle() const; - void setDefaultCursorMoveStyle(QTextCursor::MoveStyle style); + Qt::CursorMoveStyle defaultCursorMoveStyle() const; + void setDefaultCursorMoveStyle(Qt::CursorMoveStyle style); Q_SIGNALS: void contentsChange(int from, int charsRemoves, int charsAdded); diff --git a/src/gui/text/qtextdocument_p.cpp b/src/gui/text/qtextdocument_p.cpp index 779b1fff35..640f7aa4a3 100644 --- a/src/gui/text/qtextdocument_p.cpp +++ b/src/gui/text/qtextdocument_p.cpp @@ -209,7 +209,7 @@ QTextDocumentPrivate::QTextDocumentPrivate() defaultTextOption.setTabStop(80); // same as in qtextengine.cpp defaultTextOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); - defaultCursorMoveStyle = QTextCursor::Logical; + defaultCursorMoveStyle = Qt::LogicalMoveStyle; indentWidth = 40; documentMargin = 4; diff --git a/src/gui/text/qtextdocument_p.h b/src/gui/text/qtextdocument_p.h index 6563920c95..e72d676aa5 100644 --- a/src/gui/text/qtextdocument_p.h +++ b/src/gui/text/qtextdocument_p.h @@ -342,7 +342,7 @@ private: public: QTextOption defaultTextOption; - QTextCursor::MoveStyle defaultCursorMoveStyle; + Qt::CursorMoveStyle defaultCursorMoveStyle; #ifndef QT_NO_CSSPARSER QCss::StyleSheet parsedDefaultStyleSheet; #endif diff --git a/src/gui/text/qtextengine_p.h b/src/gui/text/qtextengine_p.h index c476485c1d..e06ca1cfbb 100644 --- a/src/gui/text/qtextengine_p.h +++ b/src/gui/text/qtextengine_p.h @@ -598,7 +598,7 @@ public: inline bool visualCursorMovement() const { return (visualMovement || - (block.docHandle() ? block.docHandle()->defaultCursorMoveStyle == QTextCursor::Visual : false)); + (block.docHandle() ? block.docHandle()->defaultCursorMoveStyle == Qt::VisualMoveStyle : false)); } struct SpecialData { diff --git a/src/gui/text/qtextlayout.cpp b/src/gui/text/qtextlayout.cpp index 1280b4650b..07aeb72a49 100644 --- a/src/gui/text/qtextlayout.cpp +++ b/src/gui/text/qtextlayout.cpp @@ -579,27 +579,27 @@ bool QTextLayout::cacheEnabled() const } /*! - Set the visual cursor movement style. If the QTextLayout is backed by + Set the cursor movement style. If the QTextLayout is backed by a document, you can ignore this and use the option in QTextDocument, this option is for widgets like QLineEdit or custom widgets without - a QTextDocument. Default value is QTextCursor::Logical. + a QTextDocument. Default value is Qt::LogicalMoveStyle. \sa setCursorMoveStyle() */ -void QTextLayout::setCursorMoveStyle(QTextCursor::MoveStyle style) +void QTextLayout::setCursorMoveStyle(Qt::CursorMoveStyle style) { - d->visualMovement = style == QTextCursor::Visual ? true : false; + d->visualMovement = style == Qt::VisualMoveStyle ? true : false; } /*! The cursor movement style of this QTextLayout. The default is - QTextCursor::Logical. + Qt::LogicalMoveStyle. \sa setCursorMoveStyle() */ -QTextCursor::MoveStyle QTextLayout::cursorMoveStyle() const +Qt::CursorMoveStyle QTextLayout::cursorMoveStyle() const { - return d->visualMovement ? QTextCursor::Visual : QTextCursor::Logical; + return d->visualMovement ? Qt::VisualMoveStyle : Qt::LogicalMoveStyle; } /*! diff --git a/src/gui/text/qtextlayout.h b/src/gui/text/qtextlayout.h index 8fe488a95a..89fbfb2072 100644 --- a/src/gui/text/qtextlayout.h +++ b/src/gui/text/qtextlayout.h @@ -137,8 +137,8 @@ public: void setCacheEnabled(bool enable); bool cacheEnabled() const; - void setCursorMoveStyle(QTextCursor::MoveStyle style); - QTextCursor::MoveStyle cursorMoveStyle() const; + void setCursorMoveStyle(Qt::CursorMoveStyle style); + Qt::CursorMoveStyle cursorMoveStyle() const; void beginLayout(); void endLayout(); diff --git a/src/gui/widgets/qlinecontrol.cpp b/src/gui/widgets/qlinecontrol.cpp index eb4e142328..40ebb65d0a 100644 --- a/src/gui/widgets/qlinecontrol.cpp +++ b/src/gui/widgets/qlinecontrol.cpp @@ -1585,7 +1585,7 @@ void QLineControl::processKeyEvent(QKeyEvent* event) } bool unknown = false; - bool visual = cursorMoveStyle() == QTextCursor::Visual; + bool visual = cursorMoveStyle() == Qt::VisualMoveStyle; if (false) { } diff --git a/src/gui/widgets/qlinecontrol_p.h b/src/gui/widgets/qlinecontrol_p.h index 0042f17261..6918b83b90 100644 --- a/src/gui/widgets/qlinecontrol_p.h +++ b/src/gui/widgets/qlinecontrol_p.h @@ -160,8 +160,8 @@ public: int cursorWidth() const { return m_cursorWidth; } void setCursorWidth(int value) { m_cursorWidth = value; } - QTextCursor::MoveStyle cursorMoveStyle() const { return m_textLayout.cursorMoveStyle(); } - void setCursorMoveStyle(QTextCursor::MoveStyle style) { m_textLayout.setCursorMoveStyle(style); } + Qt::CursorMoveStyle cursorMoveStyle() const { return m_textLayout.cursorMoveStyle(); } + void setCursorMoveStyle(Qt::CursorMoveStyle style) { m_textLayout.setCursorMoveStyle(style); } void moveCursor(int pos, bool mark = false); void cursorForward(bool mark, int steps) @@ -169,11 +169,11 @@ public: int c = m_cursor; if (steps > 0) { while (steps--) - c = cursorMoveStyle() == QTextCursor::Visual ? m_textLayout.rightCursorPosition(c) + c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.rightCursorPosition(c) : m_textLayout.nextCursorPosition(c); } else if (steps < 0) { while (steps++) - c = cursorMoveStyle() == QTextCursor::Visual ? m_textLayout.leftCursorPosition(c) + c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.leftCursorPosition(c) : m_textLayout.previousCursorPosition(c); } moveCursor(c, mark); diff --git a/src/gui/widgets/qlineedit.cpp b/src/gui/widgets/qlineedit.cpp index 43c3f52d2b..3c9874ea4e 100644 --- a/src/gui/widgets/qlineedit.cpp +++ b/src/gui/widgets/qlineedit.cpp @@ -1116,24 +1116,24 @@ void QLineEdit::setDragEnabled(bool b) \brief the movement style of cursor in this line edit \since 4.8 - When this property is set to QTextCursor::Visual, the line edit will use visual + When this property is set to Qt::VisualMoveStyle, the line edit will use visual movement style. Pressing the left arrow key will always cause the cursor to move left, regardless of the text's writing direction. The same behavior applies to right arrow key. - When the property is QTextCursor::Logical (the default), within a LTR text block, + When the property is Qt::LogicalMoveStyle (the default), within a LTR text block, increase cursor position when pressing left arrow key, decrease cursor position when pressing the right arrow key. If the text block is right to left, the opposite behavior applies. */ -QTextCursor::MoveStyle QLineEdit::cursorMoveStyle() const +Qt::CursorMoveStyle QLineEdit::cursorMoveStyle() const { Q_D(const QLineEdit); return d->control->cursorMoveStyle(); } -void QLineEdit::setCursorMoveStyle(QTextCursor::MoveStyle style) +void QLineEdit::setCursorMoveStyle(Qt::CursorMoveStyle style) { Q_D(QLineEdit); d->control->setCursorMoveStyle(style); diff --git a/src/gui/widgets/qlineedit.h b/src/gui/widgets/qlineedit.h index 73a736c690..e88273fa7e 100644 --- a/src/gui/widgets/qlineedit.h +++ b/src/gui/widgets/qlineedit.h @@ -85,6 +85,7 @@ class Q_GUI_EXPORT QLineEdit : public QWidget Q_PROPERTY(bool redoAvailable READ isRedoAvailable) Q_PROPERTY(bool acceptableInput READ hasAcceptableInput) Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText) + Q_PROPERTY(Qt::CursorMoveStyle cursorMoveStyle READ cursorMoveStyle WRITE setCursorMoveStyle) public: explicit QLineEdit(QWidget* parent=0); @@ -159,8 +160,8 @@ public: void setDragEnabled(bool b); bool dragEnabled() const; - void setCursorMoveStyle(QTextCursor::MoveStyle style); - QTextCursor::MoveStyle cursorMoveStyle() const; + void setCursorMoveStyle(Qt::CursorMoveStyle style); + Qt::CursorMoveStyle cursorMoveStyle() const; QString inputMask() const; void setInputMask(const QString &inputMask); -- cgit v1.2.3