qtextlayout.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtGui module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QTEXTLAYOUT_H
  40. #define QTEXTLAYOUT_H
  41. #include <QtCore/qstring.h>
  42. #include <QtCore/qnamespace.h>
  43. #include <QtCore/qrect.h>
  44. #include <QtCore/qvector.h>
  45. #include <QtGui/qcolor.h>
  46. #include <QtCore/qobject.h>
  47. #include <QtGui/qevent.h>
  48. #include <QtGui/qtextformat.h>
  49. #include <QtGui/qglyphrun.h>
  50. #include <QtGui/qtextcursor.h>
  51. QT_BEGIN_NAMESPACE
  52. class QTextEngine;
  53. class QFont;
  54. #ifndef QT_NO_RAWFONT
  55. class QRawFont;
  56. #endif
  57. class QRect;
  58. class QRegion;
  59. class QTextFormat;
  60. class QPalette;
  61. class QPainter;
  62. class Q_GUI_EXPORT QTextInlineObject
  63. {
  64. public:
  65. QTextInlineObject(int i, QTextEngine *e) : itm(i), eng(e) {}
  66. inline QTextInlineObject() : itm(0), eng(Q_NULLPTR) {}
  67. inline bool isValid() const { return eng; }
  68. QRectF rect() const;
  69. qreal width() const;
  70. qreal ascent() const;
  71. qreal descent() const;
  72. qreal height() const;
  73. Qt::LayoutDirection textDirection() const;
  74. void setWidth(qreal w);
  75. void setAscent(qreal a);
  76. void setDescent(qreal d);
  77. int textPosition() const;
  78. int formatIndex() const;
  79. QTextFormat format() const;
  80. private:
  81. friend class QTextLayout;
  82. int itm;
  83. QTextEngine *eng;
  84. };
  85. class QPaintDevice;
  86. class QTextFormat;
  87. class QTextLine;
  88. class QTextBlock;
  89. class QTextOption;
  90. class Q_GUI_EXPORT QTextLayout
  91. {
  92. public:
  93. // does itemization
  94. QTextLayout();
  95. QTextLayout(const QString& text);
  96. QTextLayout(const QString& text, const QFont &font, QPaintDevice *paintdevice = Q_NULLPTR);
  97. QTextLayout(const QTextBlock &b);
  98. ~QTextLayout();
  99. void setFont(const QFont &f);
  100. QFont font() const;
  101. #ifndef QT_NO_RAWFONT
  102. void setRawFont(const QRawFont &rawFont);
  103. #endif
  104. void setText(const QString& string);
  105. QString text() const;
  106. void setTextOption(const QTextOption &option);
  107. const QTextOption &textOption() const;
  108. void setPreeditArea(int position, const QString &text);
  109. int preeditAreaPosition() const;
  110. QString preeditAreaText() const;
  111. struct FormatRange {
  112. int start;
  113. int length;
  114. QTextCharFormat format;
  115. friend bool operator==(const FormatRange &lhs, const FormatRange &rhs)
  116. { return lhs.start == rhs.start && lhs.length == rhs.length && lhs.format == rhs.format; }
  117. friend bool operator!=(const FormatRange &lhs, const FormatRange &rhs)
  118. { return !operator==(lhs, rhs); }
  119. };
  120. #if QT_DEPRECATED_SINCE(5, 6)
  121. QT_DEPRECATED_X("Use setFormats()") void setAdditionalFormats(const QList<FormatRange> &overrides);
  122. QT_DEPRECATED_X("Use formats()") QList<FormatRange> additionalFormats() const;
  123. QT_DEPRECATED_X("Use clearFormats()") void clearAdditionalFormats();
  124. #endif
  125. void setFormats(const QVector<FormatRange> &overrides);
  126. QVector<FormatRange> formats() const;
  127. void clearFormats();
  128. void setCacheEnabled(bool enable);
  129. bool cacheEnabled() const;
  130. void setCursorMoveStyle(Qt::CursorMoveStyle style);
  131. Qt::CursorMoveStyle cursorMoveStyle() const;
  132. void beginLayout();
  133. void endLayout();
  134. void clearLayout();
  135. QTextLine createLine();
  136. int lineCount() const;
  137. QTextLine lineAt(int i) const;
  138. QTextLine lineForTextPosition(int pos) const;
  139. enum CursorMode {
  140. SkipCharacters,
  141. SkipWords
  142. };
  143. bool isValidCursorPosition(int pos) const;
  144. int nextCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const;
  145. int previousCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const;
  146. int leftCursorPosition(int oldPos) const;
  147. int rightCursorPosition(int oldPos) const;
  148. void draw(QPainter *p, const QPointF &pos, const QVector<FormatRange> &selections = QVector<FormatRange>(),
  149. const QRectF &clip = QRectF()) const;
  150. void drawCursor(QPainter *p, const QPointF &pos, int cursorPosition) const;
  151. void drawCursor(QPainter *p, const QPointF &pos, int cursorPosition, int width) const;
  152. QPointF position() const;
  153. void setPosition(const QPointF &p);
  154. QRectF boundingRect() const;
  155. qreal minimumWidth() const;
  156. qreal maximumWidth() const;
  157. #if !defined(QT_NO_RAWFONT)
  158. QList<QGlyphRun> glyphRuns(int from = -1, int length = -1) const;
  159. #endif
  160. QTextEngine *engine() const { return d; }
  161. void setFlags(int flags);
  162. private:
  163. QTextLayout(QTextEngine *e) : d(e) {}
  164. Q_DISABLE_COPY(QTextLayout)
  165. friend class QPainter;
  166. friend class QGraphicsSimpleTextItemPrivate;
  167. friend class QGraphicsSimpleTextItem;
  168. friend void qt_format_text(const QFont &font, const QRectF &_r, int tf, const QTextOption *, const QString& str,
  169. QRectF *brect, int tabstops, int* tabarray, int tabarraylen,
  170. QPainter *painter);
  171. QTextEngine *d;
  172. };
  173. Q_DECLARE_TYPEINFO(QTextLayout::FormatRange, Q_RELOCATABLE_TYPE);
  174. class Q_GUI_EXPORT QTextLine
  175. {
  176. public:
  177. inline QTextLine() : index(0), eng(Q_NULLPTR) {}
  178. inline bool isValid() const { return eng; }
  179. QRectF rect() const;
  180. qreal x() const;
  181. qreal y() const;
  182. qreal width() const;
  183. qreal ascent() const;
  184. qreal descent() const;
  185. qreal height() const;
  186. qreal leading() const;
  187. void setLeadingIncluded(bool included);
  188. bool leadingIncluded() const;
  189. qreal naturalTextWidth() const;
  190. qreal horizontalAdvance() const;
  191. QRectF naturalTextRect() const;
  192. enum Edge {
  193. Leading,
  194. Trailing
  195. };
  196. enum CursorPosition {
  197. CursorBetweenCharacters,
  198. CursorOnCharacter
  199. };
  200. /* cursorPos gets set to the valid position */
  201. qreal cursorToX(int *cursorPos, Edge edge = Leading) const;
  202. inline qreal cursorToX(int cursorPos, Edge edge = Leading) const { return cursorToX(&cursorPos, edge); }
  203. int xToCursor(qreal x, CursorPosition = CursorBetweenCharacters) const;
  204. void setLineWidth(qreal width);
  205. void setNumColumns(int columns);
  206. void setNumColumns(int columns, qreal alignmentWidth);
  207. void setPosition(const QPointF &pos);
  208. QPointF position() const;
  209. int textStart() const;
  210. int textLength() const;
  211. int lineNumber() const { return index; }
  212. void draw(QPainter *p, const QPointF &point, const QTextLayout::FormatRange *selection = Q_NULLPTR) const;
  213. #if !defined(QT_NO_RAWFONT)
  214. QList<QGlyphRun> glyphRuns(int from = -1, int length = -1) const;
  215. #endif
  216. private:
  217. QTextLine(int line, QTextEngine *e) : index(line), eng(e) {}
  218. void layout_helper(int numGlyphs);
  219. friend class QTextLayout;
  220. friend class QTextFragment;
  221. int index;
  222. QTextEngine *eng;
  223. };
  224. QT_END_NAMESPACE
  225. #endif // QTEXTLAYOUT_H