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