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
50QT_BEGIN_NAMESPACE
51
52
53
54class QTextCodec;
55class QRect;
56
57
58class Q_GUI_EXPORT QFontMetrics
59{
60public:
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
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
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 bool operator==(const QFontMetrics &other) const;
139 inline bool operator !=(const QFontMetrics &other) const { return !operator==(other); }
140
141private:
142 friend class QFontMetricsF;
143 friend class QStackTextEngine;
144
145 QExplicitlySharedDataPointer<QFontPrivate> d;
146};
147
148Q_DECLARE_SHARED(QFontMetrics)
149
150class Q_GUI_EXPORT QFontMetricsF
151{
152public:
153 explicit QFontMetricsF(const QFont &font);
154#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
155 QFontMetricsF(const QFont &font, QPaintDevice *pd);
156#ifndef Q_QDOC
157 // the template is necessary to make QFontMetrics(font,nullptr) and QFontMetrics(font,NULL)
158 // not ambiguous. Implementation detail that should not be documented.
159 template<char = 0>
160#endif
161 QFontMetricsF(const QFont &font, const QPaintDevice *pd)
162 : QFontMetricsF(font, const_cast<QPaintDevice*>(pd))
163 {}
164#else
165 QFontMetricsF(const QFont &font, const QPaintDevice *pd);
166#endif
167 QFontMetricsF(const QFontMetrics &);
168 QFontMetricsF(const QFontMetricsF &);
169 ~QFontMetricsF();
170
171 QFontMetricsF &operator=(const QFontMetricsF &);
172 QFontMetricsF &operator=(const QFontMetrics &);
173 inline QFontMetricsF &operator=(QFontMetricsF &&other) noexcept
174 { qSwap(d, other.d); return *this; }
175
176 void swap(QFontMetricsF &other) noexcept { qSwap(d, other.d); }
177
178 qreal ascent() const;
179 qreal capHeight() const;
180 qreal descent() const;
181 qreal height() const;
182 qreal leading() const;
183 qreal lineSpacing() const;
184 qreal minLeftBearing() const;
185 qreal minRightBearing() const;
186 qreal maxWidth() const;
187
188 qreal xHeight() const;
189 qreal averageCharWidth() const;
190
191 bool inFont(QChar) const;
192 bool inFontUcs4(uint ucs4) const;
193
194 qreal leftBearing(QChar) const;
195 qreal rightBearing(QChar) const;
196
197#if QT_DEPRECATED_SINCE(5, 11)
198 qreal width(const QString &string) const;
199 qreal width(QChar) const;
200#endif
201
202 qreal horizontalAdvance(const QString &string, int length = -1) const;
203 qreal horizontalAdvance(QChar) const;
204
205 QRectF boundingRect(const QString &string) const;
206 QRectF boundingRect(QChar) const;
207 QRectF boundingRect(const QRectF &r, int flags, const QString& string, int tabstops = 0, int *tabarray = nullptr) const;
208 QSizeF size(int flags, const QString& str, int tabstops = 0, int *tabarray = nullptr) const;
209
210 QRectF tightBoundingRect(const QString &text) const;
211
212 QString elidedText(const QString &text, Qt::TextElideMode mode, qreal width, int flags = 0) const;
213
214 qreal underlinePos() const;
215 qreal overlinePos() const;
216 qreal strikeOutPos() const;
217 qreal lineWidth() const;
218
219 bool operator==(const QFontMetricsF &other) const;
220 inline bool operator !=(const QFontMetricsF &other) const { return !operator==(other); }
221
222private:
223 QExplicitlySharedDataPointer<QFontPrivate> d;
224};
225
226Q_DECLARE_SHARED(QFontMetricsF)
227
228QT_END_NAMESPACE
229
230#endif // QFONTMETRICS_H
231