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
55QT_BEGIN_NAMESPACE
56
57
58class QTextEngine;
59class QFont;
60#ifndef QT_NO_RAWFONT
61class QRawFont;
62#endif
63class QRect;
64class QRegion;
65class QTextFormat;
66class QPalette;
67class QPainter;
68
69class Q_GUI_EXPORT QTextInlineObject
70{
71public:
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
93private:
94 friend class QTextLayout;
95 int itm;
96 QTextEngine *eng;
97};
98
99class QPaintDevice;
100class QTextFormat;
101class QTextLine;
102class QTextBlock;
103class QTextOption;
104
105class Q_GUI_EXPORT QTextLayout
106{
107public:
108 // does itemization
109 QTextLayout();
110 QTextLayout(const QString& text);
111#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
112 QTextLayout(const QString &text, const QFont &font, QPaintDevice *paintdevice = nullptr);
113#ifndef Q_QDOC
114 // the template is necessary to make QTextLayout(font,text,nullptr) and QTextLayout(font,text,NULL)
115 // not ambiguous. Implementation detail that should not be documented.
116 template<char = 0>
117#endif
118 QTextLayout(const QString &textData, const QFont &textFont, const QPaintDevice *paintdevice)
119 : QTextLayout(textData, textFont, const_cast<QPaintDevice*>(paintdevice))
120 {}
121#else
122 QTextLayout(const QString &text, const QFont &font, const QPaintDevice *paintdevice = nullptr);
123#endif
124 QTextLayout(const QTextBlock &b);
125 ~QTextLayout();
126
127 void setFont(const QFont &f);
128 QFont font() const;
129
130#ifndef QT_NO_RAWFONT
131 void setRawFont(const QRawFont &rawFont);
132#endif
133
134 void setText(const QString& string);
135 QString text() const;
136
137 void setTextOption(const QTextOption &option);
138 const QTextOption &textOption() const;
139
140 void setPreeditArea(int position, const QString &text);
141 int preeditAreaPosition() const;
142 QString preeditAreaText() const;
143
144 struct FormatRange {
145 int start;
146 int length;
147 QTextCharFormat format;
148
149 friend bool operator==(const FormatRange &lhs, const FormatRange &rhs)
150 { return lhs.start == rhs.start && lhs.length == rhs.length && lhs.format == rhs.format; }
151 friend bool operator!=(const FormatRange &lhs, const FormatRange &rhs)
152 { return !operator==(lhs, rhs); }
153 };
154 void setFormats(const QList<FormatRange> &overrides);
155 QList<FormatRange> formats() const;
156 void clearFormats();
157
158 void setCacheEnabled(bool enable);
159 bool cacheEnabled() const;
160
161 void setCursorMoveStyle(Qt::CursorMoveStyle style);
162 Qt::CursorMoveStyle cursorMoveStyle() const;
163
164 void beginLayout();
165 void endLayout();
166 void clearLayout();
167
168 QTextLine createLine();
169
170 int lineCount() const;
171 QTextLine lineAt(int i) const;
172 QTextLine lineForTextPosition(int pos) const;
173
174 enum CursorMode {
175 SkipCharacters,
176 SkipWords
177 };
178 bool isValidCursorPosition(int pos) const;
179 int nextCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const;
180 int previousCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const;
181 int leftCursorPosition(int oldPos) const;
182 int rightCursorPosition(int oldPos) const;
183
184 void draw(QPainter *p, const QPointF &pos,
185 const QList<FormatRange> &selections = QList<FormatRange>(),
186 const QRectF &clip = QRectF()) const;
187 void drawCursor(QPainter *p, const QPointF &pos, int cursorPosition) const;
188 void drawCursor(QPainter *p, const QPointF &pos, int cursorPosition, int width) const;
189
190 QPointF position() const;
191 void setPosition(const QPointF &p);
192
193 QRectF boundingRect() const;
194
195 qreal minimumWidth() const;
196 qreal maximumWidth() const;
197
198#if !defined(QT_NO_RAWFONT)
199 QList<QGlyphRun> glyphRuns(int from = -1, int length = -1) const;
200#endif
201
202 QTextEngine *engine() const { return d; }
203 void setFlags(int flags);
204private:
205 QTextLayout(QTextEngine *e) : d(e) {}
206 Q_DISABLE_COPY(QTextLayout)
207
208 friend class QPainter;
209 friend class QGraphicsSimpleTextItemPrivate;
210 friend class QGraphicsSimpleTextItem;
211 friend void qt_format_text(const QFont &font, const QRectF &_r, int tf, const QTextOption *, const QString& str,
212 QRectF *brect, int tabstops, int* tabarray, int tabarraylen,
213 QPainter *painter);
214 QTextEngine *d;
215};
216Q_DECLARE_TYPEINFO(QTextLayout::FormatRange, Q_RELOCATABLE_TYPE);
217
218
219class Q_GUI_EXPORT QTextLine
220{
221public:
222 inline QTextLine() : index(0), eng(nullptr) {}
223 inline bool isValid() const { return eng; }
224
225 QRectF rect() const;
226 qreal x() const;
227 qreal y() const;
228 qreal width() const;
229 qreal ascent() const;
230 qreal descent() const;
231 qreal height() const;
232 qreal leading() const;
233
234 void setLeadingIncluded(bool included);
235 bool leadingIncluded() const;
236
237 qreal naturalTextWidth() const;
238 qreal horizontalAdvance() const;
239 QRectF naturalTextRect() const;
240
241 enum Edge {
242 Leading,
243 Trailing
244 };
245 enum CursorPosition {
246 CursorBetweenCharacters,
247 CursorOnCharacter
248 };
249
250 /* cursorPos gets set to the valid position */
251 qreal cursorToX(int *cursorPos, Edge edge = Leading) const;
252 inline qreal cursorToX(int cursorPos, Edge edge = Leading) const { return cursorToX(&cursorPos, edge); }
253 int xToCursor(qreal x, CursorPosition = CursorBetweenCharacters) const;
254
255 void setLineWidth(qreal width);
256 void setNumColumns(int columns);
257 void setNumColumns(int columns, qreal alignmentWidth);
258
259 void setPosition(const QPointF &pos);
260 QPointF position() const;
261
262 int textStart() const;
263 int textLength() const;
264
265 int lineNumber() const { return index; }
266
267 void draw(QPainter *painter, const QPointF &position) const;
268
269#if !defined(QT_NO_RAWFONT)
270 QList<QGlyphRun> glyphRuns(int from = -1, int length = -1) const;
271#endif
272
273private:
274 QTextLine(int line, QTextEngine *e) : index(line), eng(e) {}
275 void layout_helper(int numGlyphs);
276 void draw_internal(QPainter *p, const QPointF &pos,
277 const QTextLayout::FormatRange *selection) const;
278
279 friend class QTextLayout;
280 friend class QTextFragment;
281 int index;
282 QTextEngine *eng;
283};
284
285QT_END_NAMESPACE
286
287#endif // QTEXTLAYOUT_H
288