QtBase  v6.3.1
qtextlayout.h
Go to the documentation of this file.
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 
42 #include <QtGui/qcolor.h>
43 #include <QtGui/qevent.h>
44 #include <QtGui/qglyphrun.h>
45 #include <QtGui/qtextcursor.h>
46 #include <QtGui/qtextformat.h>
47 #include <QtGui/qtguiglobal.h>
48 
49 #include <QtCore/qlist.h>
50 #include <QtCore/qnamespace.h>
51 #include <QtCore/qobject.h>
52 #include <QtCore/qrect.h>
53 #include <QtCore/qstring.h>
54 
56 
57 
58 class QTextEngine;
59 class QFont;
60 #ifndef QT_NO_RAWFONT
61 class QRawFont;
62 #endif
63 class QRect;
64 class QRegion;
65 class QTextFormat;
66 class QPalette;
67 class QPainter;
68 
69 class Q_GUI_EXPORT QTextInlineObject
70 {
71 public:
72  QTextInlineObject(int i, QTextEngine *e) : itm(i), eng(e) {}
73  inline QTextInlineObject() : itm(0), eng(nullptr) {}
74  inline bool isValid() const { return eng; }
75 
76  QRectF rect() const;
77  qreal width() const;
78  qreal ascent() const;
79  qreal descent() const;
80  qreal height() const;
81 
82  Qt::LayoutDirection textDirection() const;
83 
84  void setWidth(qreal w);
85  void setAscent(qreal a);
86  void setDescent(qreal d);
87 
88  int textPosition() const;
89 
90  int formatIndex() const;
91  QTextFormat format() const;
92 
93 private:
94  friend class QTextLayout;
95  int itm;
96  QTextEngine *eng;
97 };
98 
99 class QPaintDevice;
100 class QTextFormat;
101 class QTextLine;
102 class QTextBlock;
103 class QTextOption;
104 
105 class Q_GUI_EXPORT QTextLayout
106 {
107 public:
108  // does itemization
109  QTextLayout();
110  QTextLayout(const QString& text);
111  QTextLayout(const QString &text, const QFont &font, const QPaintDevice *paintdevice = nullptr);
112  QTextLayout(const QTextBlock &b);
113  ~QTextLayout();
114 
115  void setFont(const QFont &f);
116  QFont font() const;
117 
118 #ifndef QT_NO_RAWFONT
119  void setRawFont(const QRawFont &rawFont);
120 #endif
121 
122  void setText(const QString& string);
123  QString text() const;
124 
125  void setTextOption(const QTextOption &option);
126  const QTextOption &textOption() const;
127 
128  void setPreeditArea(int position, const QString &text);
129  int preeditAreaPosition() const;
130  QString preeditAreaText() const;
131 
132  struct FormatRange {
133  int start;
134  int length;
136 
137  friend bool operator==(const FormatRange &lhs, const FormatRange &rhs)
138  { return lhs.start == rhs.start && lhs.length == rhs.length && lhs.format == rhs.format; }
139  friend bool operator!=(const FormatRange &lhs, const FormatRange &rhs)
140  { return !operator==(lhs, rhs); }
141  };
142  void setFormats(const QList<FormatRange> &overrides);
143  QList<FormatRange> formats() const;
144  void clearFormats();
145 
146  void setCacheEnabled(bool enable);
147  bool cacheEnabled() const;
148 
149  void setCursorMoveStyle(Qt::CursorMoveStyle style);
150  Qt::CursorMoveStyle cursorMoveStyle() const;
151 
152  void beginLayout();
153  void endLayout();
154  void clearLayout();
155 
156  QTextLine createLine();
157 
158  int lineCount() const;
159  QTextLine lineAt(int i) const;
160  QTextLine lineForTextPosition(int pos) const;
161 
162  enum CursorMode {
164  SkipWords
165  };
166  bool isValidCursorPosition(int pos) const;
167  int nextCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const;
168  int previousCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const;
169  int leftCursorPosition(int oldPos) const;
170  int rightCursorPosition(int oldPos) const;
171 
172  void draw(QPainter *p, const QPointF &pos,
173  const QList<FormatRange> &selections = QList<FormatRange>(),
174  const QRectF &clip = QRectF()) const;
175  void drawCursor(QPainter *p, const QPointF &pos, int cursorPosition) const;
176  void drawCursor(QPainter *p, const QPointF &pos, int cursorPosition, int width) const;
177 
178  QPointF position() const;
179  void setPosition(const QPointF &p);
180 
181  QRectF boundingRect() const;
182 
183  qreal minimumWidth() const;
184  qreal maximumWidth() const;
185 
186 #if !defined(QT_NO_RAWFONT)
187  QList<QGlyphRun> glyphRuns(int from = -1, int length = -1) const;
188 #endif
189 
190  QTextEngine *engine() const { return d; }
191  void setFlags(int flags);
192 private:
193  QTextLayout(QTextEngine *e) : d(e) {}
195 
196  friend class QPainter;
199  friend void qt_format_text(const QFont &font, const QRectF &_r, int tf, const QTextOption *, const QString& str,
200  QRectF *brect, int tabstops, int* tabarray, int tabarraylen,
201  QPainter *painter);
202  QTextEngine *d;
203 };
205 
206 
207 class Q_GUI_EXPORT QTextLine
208 {
209 public:
210  inline QTextLine() : index(0), eng(nullptr) {}
211  inline bool isValid() const { return eng; }
212 
213  QRectF rect() const;
214  qreal x() const;
215  qreal y() const;
216  qreal width() const;
217  qreal ascent() const;
218  qreal descent() const;
219  qreal height() const;
220  qreal leading() const;
221 
222  void setLeadingIncluded(bool included);
223  bool leadingIncluded() const;
224 
225  qreal naturalTextWidth() const;
226  qreal horizontalAdvance() const;
227  QRectF naturalTextRect() const;
228 
229  enum Edge {
231  Trailing
232  };
235  CursorOnCharacter
236  };
237 
238  /* cursorPos gets set to the valid position */
239  qreal cursorToX(int *cursorPos, Edge edge = Leading) const;
240  inline qreal cursorToX(int cursorPos, Edge edge = Leading) const { return cursorToX(&cursorPos, edge); }
241  int xToCursor(qreal x, CursorPosition = CursorBetweenCharacters) const;
242 
243  void setLineWidth(qreal width);
244  void setNumColumns(int columns);
245  void setNumColumns(int columns, qreal alignmentWidth);
246 
247  void setPosition(const QPointF &pos);
248  QPointF position() const;
249 
250  int textStart() const;
251  int textLength() const;
252 
253  int lineNumber() const { return index; }
254 
255  void draw(QPainter *painter, const QPointF &position) const;
256 
257 #if !defined(QT_NO_RAWFONT)
258  QList<QGlyphRun> glyphRuns(int from = -1, int length = -1) const;
259 #endif
260 
261 private:
262  QTextLine(int line, QTextEngine *e) : index(line), eng(e) {}
263  void layout_helper(int numGlyphs);
264  void draw_internal(QPainter *p, const QPointF &pos,
265  const QTextLayout::FormatRange *selection) const;
266 
267  friend class QTextLayout;
268  friend class QTextFragment;
269  int index;
270  QTextEngine *eng;
271 };
272 
274 
275 #endif // QTEXTLAYOUT_H
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
Arabic default style
Definition: afstyles.h:94
[0]
Definition: edge.h:60
The QFont class specifies a query for a font used for drawing text.
Definition: qfont.h:56
The QGraphicsSimpleTextItem class provides a simple text path item that you can add to a QGraphicsSce...
Definition: qlist.h:108
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
The QPalette class contains color groups for each widget state.
Definition: qpalette.h:55
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:242
The QRawFont class provides access to a single physical instance of a font.
Definition: qrawfont.h:60
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:59
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:63
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QTextBlock class provides a container for text fragments in a QTextDocument. \inmodule QtGui.
Definition: qtextobject.h:193
The QTextCharFormat class provides formatting information for characters in a QTextDocument....
Definition: qtextformat.h:416
The QTextFormat class provides formatting information for a QTextDocument. \inmodule QtGui.
Definition: qtextformat.h:126
The QTextFragment class holds a piece of text in a QTextDocument with a single QTextCharFormat....
Definition: qtextobject.h:292
The QTextInlineObject class represents an inline object in a QAbstractTextDocumentLayout and its impl...
Definition: qtextlayout.h:70
QTextInlineObject(int i, QTextEngine *e)
Definition: qtextlayout.h:72
bool isValid() const
Definition: qtextlayout.h:74
The QTextLayout class is used to lay out and render text. \inmodule QtGui.
Definition: qtextlayout.h:106
QTextEngine * engine() const
Definition: qtextlayout.h:190
The QTextLine class represents a line of text inside a QTextLayout. \inmodule QtGui.
Definition: qtextlayout.h:208
qreal cursorToX(int cursorPos, Edge edge=Leading) const
Definition: qtextlayout.h:240
@ CursorBetweenCharacters
Definition: qtextlayout.h:234
bool isValid() const
Definition: qtextlayout.h:211
int lineNumber() const
Definition: qtextlayout.h:253
The QTextOption class provides a description of general rich text properties. \inmodule QtGui.
Definition: qtextoption.h:54
QString str
[2]
double e
rect
[4]
auto it unsigned count const
Definition: hb-iter.hh:848
QHighDpiScaling::Point position(T, QHighDpiScaling::Point::Kind)
LayoutDirection
Definition: qnamespace.h:1462
CursorMoveStyle
Definition: qnamespace.h:1684
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
#define Q_DISABLE_COPY(Class)
Definition: qglobal.h:515
@ text
bool operator==(const QMakeBaseKey &one, const QMakeBaseKey &two)
GLenum GLuint GLenum GLsizei length
Definition: qopengl.h:270
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLenum mode
GLfloat GLfloat GLfloat w
[0]
GLint GLsizei GLsizei height
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint index
[2]
GLenum const void GLbitfield GLsizei numGlyphs
GLfloat GLfloat f
GLint GLsizei width
GLbitfield flags
GLboolean enable
GLint GLsizei GLsizei GLenum format
GLint y
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
GLuint GLenum option
Definition: qopenglext.h:5929
QT_BEGIN_NAMESPACE void qt_format_text(const QFont &fnt, const QRectF &_r, int tf, const QTextOption *opt, const QString &str, QRectF *brect, int tabstops, int *, int tabarraylen, QPainter *painter)
Definition: qpainter.cpp:7100
Q_DECLARE_TYPEINFO(QTextLayout::FormatRange, Q_RELOCATABLE_TYPE)
@ Q_RELOCATABLE_TYPE
Definition: qtypeinfo.h:156
QObject::connect nullptr
MyClass setText
myFilter draw(painter, QPoint(0, 0), originalPixmap)
QItemSelection * selection
[0]
QPainter painter(this)
[7]
setFont(font)
label setLineWidth(2)
The QTextLayout::FormatRange structure is used to apply extra formatting information for a specified ...
Definition: qtextlayout.h:132
QTextCharFormat format
Definition: qtextlayout.h:135
friend bool operator==(const FormatRange &lhs, const FormatRange &rhs)
Definition: qtextlayout.h:137
friend bool operator!=(const FormatRange &lhs, const FormatRange &rhs)
Definition: qtextlayout.h:139
#define rhs