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#ifdef Q_COMPILER_RVALUE_REFS
80 inline QFontMetrics &operator=(QFontMetrics &&other) Q_DECL_NOEXCEPT
81 { qSwap(d, other.d); return *this; }
82#endif
83
84 void swap(QFontMetrics &other) Q_DECL_NOEXCEPT
85 { qSwap(d, other.d); }
86
87 int ascent() const;
88 int capHeight() const;
89 int descent() const;
90 int height() const;
91 int leading() const;
92 int lineSpacing() const;
93 int minLeftBearing() const;
94 int minRightBearing() const;
95 int maxWidth() const;
96
97 int xHeight() const;
98 int averageCharWidth() const;
99
100 bool inFont(QChar) const;
101 bool inFontUcs4(uint ucs4) const;
102
103 int leftBearing(QChar) const;
104 int rightBearing(QChar) const;
105
106#if QT_DEPRECATED_SINCE(5, 11)
107 QT_DEPRECATED_X("Use QFontMetrics::horizontalAdvance")
108 int width(const QString &, int len = -1) const;
109 QT_DEPRECATED_X("Use QFontMetrics::horizontalAdvance")
110 int width(const QString &, int len, int flags) const;
111 QT_DEPRECATED_X("Use QFontMetrics::horizontalAdvance")
112 int width(QChar) const;
113#endif
114
115 int horizontalAdvance(const QString &, int len = -1) const;
116 int horizontalAdvance(QChar) const;
117
118#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
119 QT_DEPRECATED int charWidth(const QString &str, int pos) const;
120#endif
121
122 QRect boundingRect(QChar) const;
123
124 QRect boundingRect(const QString &text) const;
125 QRect boundingRect(const QRect &r, int flags, const QString &text, int tabstops = 0, int *tabarray = nullptr) const;
126 inline QRect boundingRect(int x, int y, int w, int h, int flags, const QString &text,
127 int tabstops = 0, int *tabarray = nullptr) const
128 { return boundingRect(QRect(x, y, w, h), flags, text, tabstops, tabarray); }
129 QSize size(int flags, const QString& str, int tabstops = 0, int *tabarray = nullptr) const;
130
131 QRect tightBoundingRect(const QString &text) const;
132
133 QString elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags = 0) const;
134
135 int underlinePos() const;
136 int overlinePos() const;
137 int strikeOutPos() const;
138 int lineWidth() const;
139
140 bool operator==(const QFontMetrics &other) const;
141 inline bool operator !=(const QFontMetrics &other) const { return !operator==(other); }
142
143private:
144 friend class QFontMetricsF;
145 friend class QStackTextEngine;
146
147 QExplicitlySharedDataPointer<QFontPrivate> d;
148};
149
150Q_DECLARE_SHARED(QFontMetrics)
151
152class Q_GUI_EXPORT QFontMetricsF
153{
154public:
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#ifdef Q_COMPILER_RVALUE_REFS
176 inline QFontMetricsF &operator=(QFontMetricsF &&other)
177 { qSwap(d, other.d); return *this; }
178#endif
179
180 void swap(QFontMetricsF &other) { qSwap(d, other.d); }
181
182 qreal ascent() const;
183 qreal capHeight() const;
184 qreal descent() const;
185 qreal height() const;
186 qreal leading() const;
187 qreal lineSpacing() const;
188 qreal minLeftBearing() const;
189 qreal minRightBearing() const;
190 qreal maxWidth() const;
191
192 qreal xHeight() const;
193 qreal averageCharWidth() const;
194
195 bool inFont(QChar) const;
196 bool inFontUcs4(uint ucs4) const;
197
198 qreal leftBearing(QChar) const;
199 qreal rightBearing(QChar) const;
200
201#if QT_DEPRECATED_SINCE(5, 11)
202 qreal width(const QString &string) const;
203 qreal width(QChar) const;
204#endif
205
206 qreal horizontalAdvance(const QString &string, int length = -1) const;
207 qreal horizontalAdvance(QChar) const;
208
209 QRectF boundingRect(const QString &string) const;
210 QRectF boundingRect(QChar) const;
211 QRectF boundingRect(const QRectF &r, int flags, const QString& string, int tabstops = 0, int *tabarray = nullptr) const;
212 QSizeF size(int flags, const QString& str, int tabstops = 0, int *tabarray = nullptr) const;
213
214 QRectF tightBoundingRect(const QString &text) const;
215
216 QString elidedText(const QString &text, Qt::TextElideMode mode, qreal width, int flags = 0) const;
217
218 qreal underlinePos() const;
219 qreal overlinePos() const;
220 qreal strikeOutPos() const;
221 qreal lineWidth() const;
222
223 bool operator==(const QFontMetricsF &other) const;
224 inline bool operator !=(const QFontMetricsF &other) const { return !operator==(other); }
225
226private:
227 QExplicitlySharedDataPointer<QFontPrivate> d;
228};
229
230Q_DECLARE_SHARED(QFontMetricsF)
231
232QT_END_NAMESPACE
233
234#endif // QFONTMETRICS_H
235