1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#ifndef QTEXTLAYOUT_H
4#define QTEXTLAYOUT_H
5
6#include <QtGui/qcolor.h>
7#include <QtGui/qevent.h>
8#include <QtGui/qglyphrun.h>
9#include <QtGui/qtextcursor.h>
10#include <QtGui/qtextformat.h>
11#include <QtGui/qtguiglobal.h>
12
13#include <QtCore/qlist.h>
14#include <QtCore/qnamespace.h>
15#include <QtCore/qobject.h>
16#include <QtCore/qrect.h>
17#include <QtCore/qstring.h>
18
19QT_BEGIN_NAMESPACE
20
21
22class QTextEngine;
23class QFont;
24#ifndef QT_NO_RAWFONT
25class QRawFont;
26#endif
27class QRect;
28class QRegion;
29class QTextFormat;
30class QPalette;
31class QPainter;
32
33class Q_GUI_EXPORT QTextInlineObject
34{
35public:
36 QTextInlineObject(int i, QTextEngine *e) : itm(i), eng(e) {}
37 inline QTextInlineObject() : itm(0), eng(nullptr) {}
38 inline bool isValid() const { return eng; }
39
40 QRectF rect() const;
41 qreal width() const;
42 qreal ascent() const;
43 qreal descent() const;
44 qreal height() const;
45
46 Qt::LayoutDirection textDirection() const;
47
48 void setWidth(qreal w);
49 void setAscent(qreal a);
50 void setDescent(qreal d);
51
52 int textPosition() const;
53
54 int formatIndex() const;
55 QTextFormat format() const;
56
57private:
58 friend class QTextLayout;
59 int itm;
60 QTextEngine *eng;
61};
62
63class QPaintDevice;
64class QTextFormat;
65class QTextLine;
66class QTextBlock;
67class QTextOption;
68
69class Q_GUI_EXPORT QTextLayout
70{
71public:
72 enum GlyphRunRetrievalFlag : quint16 {
73 RetrieveGlyphIndexes = 0x1,
74 RetrieveGlyphPositions = 0x2,
75 RetrieveStringIndexes = 0x4,
76 RetrieveString = 0x8,
77
78 DefaultRetrievalFlags = RetrieveGlyphIndexes | RetrieveGlyphPositions,
79 RetrieveAll = 0xffff
80 };
81 Q_DECLARE_FLAGS(GlyphRunRetrievalFlags, GlyphRunRetrievalFlag)
82
83 // does itemization
84 QTextLayout();
85 QTextLayout(const QString& text);
86 QTextLayout(const QString &text, const QFont &font, const QPaintDevice *paintdevice = nullptr);
87 QTextLayout(const QTextBlock &b);
88 ~QTextLayout();
89
90 void setFont(const QFont &f);
91 QFont font() const;
92
93#ifndef QT_NO_RAWFONT
94 void setRawFont(const QRawFont &rawFont);
95#endif
96
97 void setText(const QString& string);
98 QString text() const;
99
100 void setTextOption(const QTextOption &option);
101 const QTextOption &textOption() const;
102
103 void setPreeditArea(int position, const QString &text);
104 int preeditAreaPosition() const;
105 QString preeditAreaText() const;
106
107 struct FormatRange {
108 int start;
109 int length;
110 QTextCharFormat format;
111
112 friend bool operator==(const FormatRange &lhs, const FormatRange &rhs)
113 { return lhs.start == rhs.start && lhs.length == rhs.length && lhs.format == rhs.format; }
114 friend bool operator!=(const FormatRange &lhs, const FormatRange &rhs)
115 { return !operator==(lhs, rhs); }
116 };
117 void setFormats(const QList<FormatRange> &overrides);
118 QList<FormatRange> formats() const;
119 void clearFormats();
120
121 void setCacheEnabled(bool enable);
122 bool cacheEnabled() const;
123
124 void setCursorMoveStyle(Qt::CursorMoveStyle style);
125 Qt::CursorMoveStyle cursorMoveStyle() const;
126
127 void beginLayout();
128 void endLayout();
129 void clearLayout();
130
131 QTextLine createLine();
132
133 int lineCount() const;
134 QTextLine lineAt(int i) const;
135 QTextLine lineForTextPosition(int pos) const;
136
137 enum CursorMode {
138 SkipCharacters,
139 SkipWords
140 };
141 bool isValidCursorPosition(int pos) const;
142 int nextCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const;
143 int previousCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const;
144 int leftCursorPosition(int oldPos) const;
145 int rightCursorPosition(int oldPos) const;
146
147 void draw(QPainter *p, const QPointF &pos,
148 const QList<FormatRange> &selections = QList<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
153 QPointF position() const;
154 void setPosition(const QPointF &p);
155
156 QRectF boundingRect() const;
157
158 qreal minimumWidth() const;
159 qreal maximumWidth() const;
160
161#if !defined(QT_NO_RAWFONT)
162
163# if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
164 QList<QGlyphRun> glyphRuns(int from, int length, GlyphRunRetrievalFlags flags) const;
165 QList<QGlyphRun> glyphRuns(int from = -1, int length = -1) const;
166# else
167 QList<QGlyphRun> glyphRuns(int from = -1,
168 int length = -1,
169 GlyphRunRetrievalFlags flags = DefaultRetrievalFlags) const;
170# endif
171#endif
172
173 QTextEngine *engine() const { return d; }
174 void setFlags(int flags);
175private:
176 QTextLayout(QTextEngine *e) : d(e) {}
177 Q_DISABLE_COPY(QTextLayout)
178
179 friend class QPainter;
180 friend class QGraphicsSimpleTextItemPrivate;
181 friend class QGraphicsSimpleTextItem;
182 friend void qt_format_text(const QFont &font, const QRectF &_r, int tf, const QTextOption *, const QString& str,
183 QRectF *brect, int tabstops, int* tabarray, int tabarraylen,
184 QPainter *painter);
185 QTextEngine *d;
186};
187Q_DECLARE_TYPEINFO(QTextLayout::FormatRange, Q_RELOCATABLE_TYPE);
188Q_DECLARE_OPERATORS_FOR_FLAGS(QTextLayout::GlyphRunRetrievalFlags)
189
190class Q_GUI_EXPORT QTextLine
191{
192public:
193 inline QTextLine() : index(0), eng(nullptr) {}
194 inline bool isValid() const { return eng; }
195
196 QRectF rect() const;
197 qreal x() const;
198 qreal y() const;
199 qreal width() const;
200 qreal ascent() const;
201 qreal descent() const;
202 qreal height() const;
203 qreal leading() const;
204
205 void setLeadingIncluded(bool included);
206 bool leadingIncluded() const;
207
208 qreal naturalTextWidth() const;
209 qreal horizontalAdvance() const;
210 QRectF naturalTextRect() const;
211
212 enum Edge {
213 Leading,
214 Trailing
215 };
216 enum CursorPosition {
217 CursorBetweenCharacters,
218 CursorOnCharacter
219 };
220
221 /* cursorPos gets set to the valid position */
222 qreal cursorToX(int *cursorPos, Edge edge = Leading) const;
223 inline qreal cursorToX(int cursorPos, Edge edge = Leading) const { return cursorToX(cursorPos: &cursorPos, edge); }
224 int xToCursor(qreal x, CursorPosition = CursorBetweenCharacters) const;
225
226 void setLineWidth(qreal width);
227 void setNumColumns(int columns);
228 void setNumColumns(int columns, qreal alignmentWidth);
229
230 void setPosition(const QPointF &pos);
231 QPointF position() const;
232
233 int textStart() const;
234 int textLength() const;
235
236 int lineNumber() const { return index; }
237
238 void draw(QPainter *painter, const QPointF &position) const;
239
240#if !defined(QT_NO_RAWFONT)
241# if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
242 QList<QGlyphRun> glyphRuns(int from, int length, QTextLayout::GlyphRunRetrievalFlags flags) const;
243 QList<QGlyphRun> glyphRuns(int from = -1, int length = -1) const;
244# else
245 QList<QGlyphRun> glyphRuns(int from = -1,
246 int length = -1,
247 QTextLayout::GlyphRunRetrievalFlags flags = QTextLayout::DefaultRetrievalFlags) const;
248# endif
249#endif
250
251private:
252 QTextLine(int line, QTextEngine *e) : index(line), eng(e) {}
253 void layout_helper(int numGlyphs);
254 void draw_internal(QPainter *p, const QPointF &pos,
255 const QTextLayout::FormatRange *selection) const;
256
257 friend class QTextLayout;
258 friend class QTextFragment;
259 int index;
260 QTextEngine *eng;
261};
262
263QT_END_NAMESPACE
264
265#endif // QTEXTLAYOUT_H
266

source code of qtbase/src/gui/text/qtextlayout.h