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 QtQuick 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#include "qquickfontmetrics_p.h"
41
42#include <QFont>
43
44QT_BEGIN_NAMESPACE
45
46/*!
47 \qmltype FontMetrics
48 \instantiates QQuickFontMetrics
49 \inqmlmodule QtQuick
50 \since 5.4
51 \ingroup qtquick-text-utility
52 \brief Provides metrics for a given font.
53
54 FontMetrics calculates the size of characters and strings for a given font.
55
56 It provides a subset of the C++ \l QFontMetricsF API, with the added
57 ability to change the font that is used for calculations via the \l font
58 property.
59
60 \code
61 FontMetrics {
62 id: fontMetrics
63 font.family: "Arial"
64 }
65
66 Rectangle {
67 width: fontMetrics.height * 4
68 height: fontMetrics.height * 2
69 }
70 \endcode
71
72 \sa QFontMetricsF, TextMetrics
73*/
74QQuickFontMetrics::QQuickFontMetrics(QObject *parent) :
75 QObject(parent),
76 m_metrics(m_font)
77{
78}
79
80QQuickFontMetrics::~QQuickFontMetrics()
81{
82}
83
84/*!
85 \qmlproperty font QtQuick::FontMetrics::font
86
87 This property holds the font used for the metrics calculations.
88*/
89QFont QQuickFontMetrics::font() const
90{
91 return m_font;
92}
93
94void QQuickFontMetrics::setFont(const QFont &font)
95{
96 if (m_font != font) {
97 m_font = font;
98 m_metrics = QFontMetricsF(m_font);
99 emit fontChanged(m_font);
100 }
101}
102
103/*!
104 \qmlproperty real QtQuick::FontMetrics::ascent
105
106 This property holds the ascent of the font.
107
108 \sa {QFontMetricsF::ascent()}, descent, height
109*/
110qreal QQuickFontMetrics::ascent() const
111{
112 return m_metrics.ascent();
113}
114
115/*!
116 \qmlproperty real QtQuick::FontMetrics::descent
117
118 This property holds the descent of the font.
119
120 \sa {QFontMetricsF::descent()}, ascent, height
121*/
122qreal QQuickFontMetrics::descent() const
123{
124 return m_metrics.descent();
125}
126
127/*!
128 \qmlproperty real QtQuick::FontMetrics::height
129
130 This property holds the height of the font.
131
132 \sa {QFontMetricsF::height()}
133*/
134qreal QQuickFontMetrics::height() const
135{
136 return m_metrics.height();
137}
138
139/*!
140 \qmlproperty real QtQuick::FontMetrics::leading
141
142 This property holds the leading of the font.
143
144 \sa {QFontMetricsF::leading()}
145*/
146qreal QQuickFontMetrics::leading() const
147{
148 return m_metrics.leading();
149}
150
151/*!
152 \qmlproperty real QtQuick::FontMetrics::lineSpacing
153
154 This property holds the distance from one base line to the next.
155
156 \sa {QFontMetricsF::lineSpacing()}
157*/
158qreal QQuickFontMetrics::lineSpacing() const
159{
160 return m_metrics.lineSpacing();
161}
162
163/*!
164 \qmlproperty real QtQuick::FontMetrics::minimumLeftBearing
165
166 This property holds the minimum left bearing of the font.
167
168 \sa {QFontMetricsF::minLeftBearing()}
169*/
170qreal QQuickFontMetrics::minimumLeftBearing() const
171{
172 return m_metrics.minLeftBearing();
173}
174
175/*!
176 \qmlproperty real QtQuick::FontMetrics::minimumRightBearing
177
178 This property holds the minimum right bearing of the font.
179
180 \sa {QFontMetricsF::minRightBearing()}
181*/
182qreal QQuickFontMetrics::minimumRightBearing() const
183{
184 return m_metrics.minRightBearing();
185}
186
187/*!
188 \qmlproperty real QtQuick::FontMetrics::maximumCharacterWidth
189
190 This property holds the width of the widest character in the font.
191
192 \sa {QFontMetricsF::maxWidth()}
193*/
194qreal QQuickFontMetrics::maximumCharacterWidth() const
195{
196 return m_metrics.maxWidth();
197}
198
199/*!
200 \qmlproperty real QtQuick::FontMetrics::xHeight
201
202 This property holds the 'x' height of the font.
203
204 \sa {QFontMetricsF::xHeight()}
205*/
206qreal QQuickFontMetrics::xHeight() const
207{
208 return m_metrics.xHeight();
209}
210
211/*!
212 \qmlproperty real QtQuick::FontMetrics::averageCharacterWidth
213
214 This property holds the average width of glyphs in the font.
215
216 \sa {QFontMetricsF::averageCharWidth()}
217*/
218qreal QQuickFontMetrics::averageCharacterWidth() const
219{
220 return m_metrics.averageCharWidth();
221}
222
223/*!
224 \qmlproperty real QtQuick::FontMetrics::underlinePosition
225
226 This property holds the distance from the base line to where an underscore
227 should be drawn.
228
229 \sa {QFontMetricsF::underlinePos()}, overlinePosition, strikeOutPosition
230*/
231qreal QQuickFontMetrics::underlinePosition() const
232{
233 return m_metrics.underlinePos();
234}
235
236/*!
237 \qmlproperty real QtQuick::FontMetrics::overlinePosition
238
239 This property holds the distance from the base line to where an overline
240 should be drawn.
241
242 \sa {QFontMetricsF::overlinePos()}, underlinePosition, strikeOutPosition
243*/
244qreal QQuickFontMetrics::overlinePosition() const
245{
246 return m_metrics.overlinePos();
247}
248
249/*!
250 \qmlproperty real QtQuick::FontMetrics::strikeOutPosition
251
252 This property holds the distance from the base line to where the strikeout
253 line should be drawn.
254
255 \sa {QFontMetricsF::strikeOutPos()}, overlinePosition, underlinePosition
256*/
257qreal QQuickFontMetrics::strikeOutPosition() const
258{
259 return m_metrics.strikeOutPos();
260}
261
262/*!
263 \qmlproperty real QtQuick::FontMetrics::lineWidth
264
265 This property holds the width of the underline and strikeout lines,
266 adjusted for the point size of the font.
267
268 \sa {QFontMetricsF::lineWidth()}
269*/
270qreal QQuickFontMetrics::lineWidth() const
271{
272 return m_metrics.lineWidth();
273}
274
275/*!
276 \qmlmethod qreal QtQuick::FontMetrics::advanceWidth(string text)
277
278 This method returns the advance in pixels of the characters in \a text.
279 This is the distance from the position of the string to where the next
280 string should be drawn.
281
282 This method is offered as an imperative alternative to the
283 \l {QQuickTextMetrics::advanceWidth}{advanceWidth} property of
284 \l {QQuickTextMetrics::advanceWidth}{TextMetrics}.
285
286 \sa {QFontMetricsF::width()}, {QFontMetricsF::height()}
287*/
288qreal QQuickFontMetrics::advanceWidth(const QString &text) const
289{
290 return m_metrics.horizontalAdvance(text);
291}
292
293/*!
294 \qmlmethod rect QtQuick::FontMetrics::boundingRect(string text)
295
296 This method returns the bounding rectangle of the characters in the string
297 specified by \a text.
298
299 This method is offered as an imperative alternative to the
300 \l {QQuickTextMetrics::boundingRect}{boundingRect} property of
301 \l {QQuickTextMetrics::boundingRect}{TextMetrics}.
302
303 \sa {QFontMetricsF::boundingRect()}, tightBoundingRect()
304*/
305QRectF QQuickFontMetrics::boundingRect(const QString &text) const
306{
307 return m_metrics.boundingRect(text);
308}
309
310/*!
311 \qmlmethod rect QtQuick::FontMetrics::tightBoundingRect(string text)
312
313 This method returns a tight bounding rectangle around the characters in the
314 string specified by \a text.
315
316 This method is offered as an imperative alternative to the
317 \l {QQuickTextMetrics::tightBoundingRect}{tightBoundingRect} property of
318 \l {QQuickTextMetrics::tightBoundingRect}{TextMetrics}.
319
320 \sa {QFontMetricsF::tightBoundingRect()}, boundingRect()
321*/
322QRectF QQuickFontMetrics::tightBoundingRect(const QString &text) const
323{
324 return m_metrics.tightBoundingRect(text);
325}
326
327/*!
328 \qmlmethod string QtQuick::FontMetrics::elidedText(string text, enumeration mode, real width, int flags)
329
330 This method returns a returns an elided version of the string (i.e., a
331 string with "..." in it) if the string \a text is wider than \a width.
332 Otherwise, returns the original string.
333
334 The \a flags argument is optional and currently only supports
335 \l {Qt::TextShowMnemonic}.
336
337 This method is offered as an imperative alternative to the
338 \l {QQuickTextMetrics::elidedText}{elidedText} property of
339 \l {QQuickTextMetrics::elidedText}{TextMetrics}.
340
341 \sa {QFontMetricsF::elidedText()}
342*/
343QString QQuickFontMetrics::elidedText(const QString &text, Qt::TextElideMode mode, qreal width, int flags) const
344{
345 return m_metrics.elidedText(text, mode, width, flags);
346}
347
348QT_END_NAMESPACE
349
350#include "moc_qquickfontmetrics_p.cpp"
351