1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #ifndef QTEXTLAYOUT_H
42 #define QTEXTLAYOUT_H
43 
44 #include <QtCore/qstring.h>
45 #include <QtCore/qnamespace.h>
46 #include <QtCore/qrect.h>
47 #include <QtCore/qvector.h>
48 #include <QtGui/qcolor.h>
49 #include <QtCore/qobject.h>
50 #include <QtGui/qevent.h>
51 #include <QtGui/qtextformat.h>
52 #include <QtGui/qglyphrun.h>
53 #include <QtGui/qtextcursor.h>
54 
55 QT_BEGIN_HEADER
56 
57 QT_BEGIN_NAMESPACE
58 
59 QT_MODULE(Gui)
60 
61 class QTextEngine;
62 class QFont;
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:
QTextInlineObject(int i,QTextEngine * e)72     QTextInlineObject(int i, QTextEngine *e) : itm(i), eng(e) {}
QTextInlineObject()73     inline QTextInlineObject() : itm(0), eng(0) {}
isValid()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, QPaintDevice *paintdevice = 0);
112     QTextLayout(const QTextBlock &b);
113     ~QTextLayout();
114 
115     void setFont(const QFont &f);
116     QFont font() const;
117 
118     void setText(const QString& string);
119     QString text() const;
120 
121     void setTextOption(const QTextOption &option);
122     QTextOption textOption() const;
123 
124     void setPreeditArea(int position, const QString &text);
125     int preeditAreaPosition() const;
126     QString preeditAreaText() const;
127 
128     struct FormatRange {
129         int start;
130         int length;
131         QTextCharFormat format;
132     };
133     void setAdditionalFormats(const QList<FormatRange> &overrides);
134     QList<FormatRange> additionalFormats() const;
135     void clearAdditionalFormats();
136 
137     void setCacheEnabled(bool enable);
138     bool cacheEnabled() const;
139 
140     void setCursorMoveStyle(Qt::CursorMoveStyle style);
141     Qt::CursorMoveStyle cursorMoveStyle() const;
142 
143     void beginLayout();
144     void endLayout();
145     void clearLayout();
146 
147     QTextLine createLine();
148 
149     int lineCount() const;
150     QTextLine lineAt(int i) const;
151     QTextLine lineForTextPosition(int pos) const;
152 
153     enum CursorMode {
154         SkipCharacters,
155         SkipWords
156     };
157     bool isValidCursorPosition(int pos) const;
158     int nextCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const;
159     int previousCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const;
160     int leftCursorPosition(int oldPos) const;
161     int rightCursorPosition(int oldPos) const;
162 
163     void draw(QPainter *p, const QPointF &pos, const QVector<FormatRange> &selections = QVector<FormatRange>(),
164               const QRectF &clip = QRectF()) const;
165     void drawCursor(QPainter *p, const QPointF &pos, int cursorPosition) const;
166     void drawCursor(QPainter *p, const QPointF &pos, int cursorPosition, int width) const;
167 
168     QPointF position() const;
169     void setPosition(const QPointF &p);
170 
171     QRectF boundingRect() const;
172 
173     qreal minimumWidth() const;
174     qreal maximumWidth() const;
175 
176 #if !defined(QT_NO_RAWFONT)
177     QList<QGlyphRun> glyphRuns() const;
178 #endif
179 
engine()180     QTextEngine *engine() const { return d; }
181     void setFlags(int flags);
182 private:
QTextLayout(QTextEngine * e)183     QTextLayout(QTextEngine *e) : d(e) {}
184     Q_DISABLE_COPY(QTextLayout)
185 
186     friend class QPainter;
187     friend class QPSPrinter;
188     friend class QGraphicsSimpleTextItemPrivate;
189     friend class QGraphicsSimpleTextItem;
190     friend void qt_format_text(const QFont &font, const QRectF &_r, int tf, const QTextOption *, const QString& str,
191                                QRectF *brect, int tabstops, int* tabarray, int tabarraylen,
192                                QPainter *painter);
193     QTextEngine *d;
194 };
195 
196 
197 class Q_GUI_EXPORT QTextLine
198 {
199 public:
QTextLine()200     inline QTextLine() : i(0), eng(0) {}
isValid()201     inline bool isValid() const { return eng; }
202 
203     QRectF rect() const;
204     qreal x() const;
205     qreal y() const;
206     qreal width() const;
207     qreal ascent() const;
208     qreal descent() const;
209     qreal height() const;
210     qreal leading() const;
211 
212     void setLeadingIncluded(bool included);
213     bool leadingIncluded() const;
214 
215     qreal naturalTextWidth() const;
216     qreal horizontalAdvance() const;
217     QRectF naturalTextRect() const;
218 
219     enum Edge {
220         Leading,
221         Trailing
222     };
223     enum CursorPosition {
224         CursorBetweenCharacters,
225         CursorOnCharacter
226     };
227 
228     /* cursorPos gets set to the valid position */
229     qreal cursorToX(int *cursorPos, Edge edge = Leading) const;
230     inline qreal cursorToX(int cursorPos, Edge edge = Leading) const { return cursorToX(&cursorPos, edge); }
231     int xToCursor(qreal x, CursorPosition = CursorBetweenCharacters) const;
232 
233     void setLineWidth(qreal width);
234     void setNumColumns(int columns);
235     void setNumColumns(int columns, qreal alignmentWidth);
236 
237     void setPosition(const QPointF &pos);
238     QPointF position() const;
239 
240     int textStart() const;
241     int textLength() const;
242 
lineNumber()243     int lineNumber() const { return i; }
244 
245     void draw(QPainter *p, const QPointF &point, const QTextLayout::FormatRange *selection = 0) const;
246 
247 private:
QTextLine(int line,QTextEngine * e)248     QTextLine(int line, QTextEngine *e) : i(line), eng(e) {}
249     void layout_helper(int numGlyphs);
250 
251 #if !defined(QT_NO_RAWFONT)
252     QList<QGlyphRun> glyphs(int from, int length) const;
253 #endif
254 
255     friend class QTextLayout;
256     friend class QTextFragment;
257     int i;
258     QTextEngine *eng;
259 };
260 
261 QT_END_NAMESPACE
262 
263 QT_END_HEADER
264 
265 #endif // QTEXTLAYOUT_H
266