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
40 #ifndef QFONTMETRICS_H
41 #define QFONTMETRICS_H
42
43 #include <QtGui/qtguiglobal.h>
44 #include <QtGui/qfont.h>
45 #include <QtCore/qsharedpointer.h>
46 #ifndef QT_INCLUDE_COMPAT
47 #include <QtCore/qrect.h>
48 #endif
49
50 QT_BEGIN_NAMESPACE
51
52
53
54 class QTextCodec;
55 class QRect;
56
57
58 class Q_GUI_EXPORT QFontMetrics
59 {
60 public:
61 explicit QFontMetrics(const QFont &);
62 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
63 QFontMetrics(const QFont &font, QPaintDevice *pd);
64 #ifndef Q_QDOC
65 // the template is necessary to make QFontMetrics(font,nullptr) and QFontMetrics(font,NULL)
66 // not ambiguous. Implementation detail that should not be documented.
67 template<char = 0>
68 #endif
QFontMetrics(const QFont & font,const QPaintDevice * pd)69 QFontMetrics(const QFont &font, const QPaintDevice *pd)
70 : QFontMetrics(font, const_cast<QPaintDevice*>(pd))
71 {}
72 #else
73 QFontMetrics(const QFont &font, const QPaintDevice *pd);
74 #endif
75 QFontMetrics(const QFontMetrics &);
76 ~QFontMetrics();
77
78 QFontMetrics &operator=(const QFontMetrics &);
79 inline QFontMetrics &operator=(QFontMetrics &&other) noexcept
80 { qSwap(d, other.d); return *this; }
81
swap(QFontMetrics & other)82 void swap(QFontMetrics &other) noexcept
83 { qSwap(d, other.d); }
84
85 int ascent() const;
86 int capHeight() const;
87 int descent() const;
88 int height() const;
89 int leading() const;
90 int lineSpacing() const;
91 int minLeftBearing() const;
92 int minRightBearing() const;
93 int maxWidth() const;
94
95 int xHeight() const;
96 int averageCharWidth() const;
97
98 bool inFont(QChar) const;
99 bool inFontUcs4(uint ucs4) const;
100
101 int leftBearing(QChar) const;
102 int rightBearing(QChar) const;
103
104 #if QT_DEPRECATED_SINCE(5, 11)
105 QT_DEPRECATED_X("Use QFontMetrics::horizontalAdvance")
106 int width(const QString &, int len = -1) const;
107 QT_DEPRECATED_X("Use QFontMetrics::horizontalAdvance")
108 int width(const QString &, int len, int flags) const;
109 QT_DEPRECATED_X("Use QFontMetrics::horizontalAdvance")
110 int width(QChar) const;
111 #endif
112
113 int horizontalAdvance(const QString &, int len = -1) const;
114 int horizontalAdvance(QChar) const;
115
116 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
117 QT_DEPRECATED int charWidth(const QString &str, int pos) const;
118 #endif
119
120 QRect boundingRect(QChar) const;
121
122 QRect boundingRect(const QString &text) const;
123 QRect boundingRect(const QRect &r, int flags, const QString &text, int tabstops = 0, int *tabarray = nullptr) const;
124 inline QRect boundingRect(int x, int y, int w, int h, int flags, const QString &text,
125 int tabstops = 0, int *tabarray = nullptr) const
126 { return boundingRect(QRect(x, y, w, h), flags, text, tabstops, tabarray); }
127 QSize size(int flags, const QString& str, int tabstops = 0, int *tabarray = nullptr) const;
128
129 QRect tightBoundingRect(const QString &text) const;
130
131 QString elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags = 0) const;
132
133 int underlinePos() const;
134 int overlinePos() const;
135 int strikeOutPos() const;
136 int lineWidth() const;
137
138 qreal fontDpi() const;
139
140 bool operator==(const QFontMetrics &other) const;
141 inline bool operator !=(const QFontMetrics &other) const { return !operator==(other); }
142
143 private:
144 friend class QFontMetricsF;
145 friend class QStackTextEngine;
146
147 QExplicitlySharedDataPointer<QFontPrivate> d;
148 };
149
Q_DECLARE_SHARED(QFontMetrics)150 Q_DECLARE_SHARED(QFontMetrics)
151
152 class Q_GUI_EXPORT QFontMetricsF
153 {
154 public:
155 explicit QFontMetricsF(const QFont &font);
156 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
157 QFontMetricsF(const QFont &font, QPaintDevice *pd);
158 #ifndef Q_QDOC
159 // the template is necessary to make QFontMetrics(font,nullptr) and QFontMetrics(font,NULL)
160 // not ambiguous. Implementation detail that should not be documented.
161 template<char = 0>
162 #endif
163 QFontMetricsF(const QFont &font, const QPaintDevice *pd)
164 : QFontMetricsF(font, const_cast<QPaintDevice*>(pd))
165 {}
166 #else
167 QFontMetricsF(const QFont &font, const QPaintDevice *pd);
168 #endif
169 QFontMetricsF(const QFontMetrics &);
170 QFontMetricsF(const QFontMetricsF &);
171 ~QFontMetricsF();
172
173 QFontMetricsF &operator=(const QFontMetricsF &);
174 QFontMetricsF &operator=(const QFontMetrics &);
175 inline QFontMetricsF &operator=(QFontMetricsF &&other) noexcept
176 { qSwap(d, other.d); return *this; }
177
178 void swap(QFontMetricsF &other) noexcept { qSwap(d, other.d); }
179
180 qreal ascent() const;
181 qreal capHeight() const;
182 qreal descent() const;
183 qreal height() const;
184 qreal leading() const;
185 qreal lineSpacing() const;
186 qreal minLeftBearing() const;
187 qreal minRightBearing() const;
188 qreal maxWidth() const;
189
190 qreal xHeight() const;
191 qreal averageCharWidth() const;
192
193 bool inFont(QChar) const;
194 bool inFontUcs4(uint ucs4) const;
195
196 qreal leftBearing(QChar) const;
197 qreal rightBearing(QChar) const;
198
199 #if QT_DEPRECATED_SINCE(5, 11)
200 qreal width(const QString &string) const;
201 qreal width(QChar) const;
202 #endif
203
204 qreal horizontalAdvance(const QString &string, int length = -1) const;
205 qreal horizontalAdvance(QChar) const;
206
207 QRectF boundingRect(const QString &string) const;
208 QRectF boundingRect(QChar) const;
209 QRectF boundingRect(const QRectF &r, int flags, const QString& string, int tabstops = 0, int *tabarray = nullptr) const;
210 QSizeF size(int flags, const QString& str, int tabstops = 0, int *tabarray = nullptr) const;
211
212 QRectF tightBoundingRect(const QString &text) const;
213
214 QString elidedText(const QString &text, Qt::TextElideMode mode, qreal width, int flags = 0) const;
215
216 qreal underlinePos() const;
217 qreal overlinePos() const;
218 qreal strikeOutPos() const;
219 qreal lineWidth() const;
220
221 qreal fontDpi() const;
222
223 bool operator==(const QFontMetricsF &other) const;
224 inline bool operator !=(const QFontMetricsF &other) const { return !operator==(other); }
225
226 private:
227 QExplicitlySharedDataPointer<QFontPrivate> d;
228 };
229
230 Q_DECLARE_SHARED(QFontMetricsF)
231
232 QT_END_NAMESPACE
233
234 #endif // QFONTMETRICS_H
235