Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickfontmetrics.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QFont>
8
10
11/*!
12 \qmltype FontMetrics
13 \nativetype QQuickFontMetrics
14 \inqmlmodule QtQuick
15 \since 5.4
16 \ingroup qtquick-text-utility
17 \brief Provides metrics for a given font.
18
19 FontMetrics calculates the size of characters and strings for a given font.
20
21 It provides a subset of the C++ \l QFontMetricsF API, with the added
22 ability to change the font that is used for calculations via the \l font
23 property.
24
25 \code
26 FontMetrics {
27 id: fontMetrics
28 font.family: "Arial"
29 }
30
31 Rectangle {
32 width: fontMetrics.height * 4
33 height: fontMetrics.height * 2
34 }
35 \endcode
36
37 \sa QFontMetricsF, TextMetrics
38*/
39QQuickFontMetrics::QQuickFontMetrics(QObject *parent) :
40 QObject(parent),
41 m_metrics(m_font)
42{
43}
44
45/*!
46 \qmlproperty font QtQuick::FontMetrics::font
47
48 This property holds the font used for the metrics calculations.
49*/
50QFont QQuickFontMetrics::font() const
51{
52 return m_font;
53}
54
55void QQuickFontMetrics::setFont(const QFont &font)
56{
57 if (m_font != font) {
58 m_font = font;
59 m_metrics = QFontMetricsF(m_font);
60 emit fontChanged(m_font);
61 }
62}
63
64/*!
65 \qmlproperty real QtQuick::FontMetrics::ascent
66
67 This property holds the ascent of the font.
68
69 \sa {QFontMetricsF::ascent()}, descent, height
70*/
71qreal QQuickFontMetrics::ascent() const
72{
73 return m_metrics.ascent();
74}
75
76/*!
77 \qmlproperty real QtQuick::FontMetrics::descent
78
79 This property holds the descent of the font.
80
81 \sa {QFontMetricsF::descent()}, ascent, height
82*/
83qreal QQuickFontMetrics::descent() const
84{
85 return m_metrics.descent();
86}
87
88/*!
89 \qmlproperty real QtQuick::FontMetrics::height
90
91 This property holds the height of the font.
92
93 \sa {QFontMetricsF::height()}
94*/
95qreal QQuickFontMetrics::height() const
96{
97 return m_metrics.height();
98}
99
100/*!
101 \qmlproperty real QtQuick::FontMetrics::leading
102
103 This property holds the leading of the font.
104
105 \sa {QFontMetricsF::leading()}
106*/
107qreal QQuickFontMetrics::leading() const
108{
109 return m_metrics.leading();
110}
111
112/*!
113 \qmlproperty real QtQuick::FontMetrics::lineSpacing
114
115 This property holds the distance from one base line to the next.
116
117 \sa {QFontMetricsF::lineSpacing()}
118*/
119qreal QQuickFontMetrics::lineSpacing() const
120{
121 return m_metrics.lineSpacing();
122}
123
124/*!
125 \qmlproperty real QtQuick::FontMetrics::minimumLeftBearing
126
127 This property holds the minimum left bearing of the font.
128
129 \sa {QFontMetricsF::minLeftBearing()}
130*/
131qreal QQuickFontMetrics::minimumLeftBearing() const
132{
133 return m_metrics.minLeftBearing();
134}
135
136/*!
137 \qmlproperty real QtQuick::FontMetrics::minimumRightBearing
138
139 This property holds the minimum right bearing of the font.
140
141 \sa {QFontMetricsF::minRightBearing()}
142*/
143qreal QQuickFontMetrics::minimumRightBearing() const
144{
145 return m_metrics.minRightBearing();
146}
147
148/*!
149 \qmlproperty real QtQuick::FontMetrics::maximumCharacterWidth
150
151 This property holds the width of the widest character in the font.
152
153 \sa {QFontMetricsF::maxWidth()}
154*/
155qreal QQuickFontMetrics::maximumCharacterWidth() const
156{
157 return m_metrics.maxWidth();
158}
159
160/*!
161 \qmlproperty real QtQuick::FontMetrics::xHeight
162
163 This property holds the 'x' height of the font.
164
165 \sa {QFontMetricsF::xHeight()}
166*/
167qreal QQuickFontMetrics::xHeight() const
168{
169 return m_metrics.xHeight();
170}
171
172/*!
173 \qmlproperty real QtQuick::FontMetrics::averageCharacterWidth
174
175 This property holds the average width of glyphs in the font.
176
177 \sa {QFontMetricsF::averageCharWidth()}
178*/
179qreal QQuickFontMetrics::averageCharacterWidth() const
180{
181 return m_metrics.averageCharWidth();
182}
183
184/*!
185 \qmlproperty real QtQuick::FontMetrics::underlinePosition
186
187 This property holds the distance from the base line to where an underscore
188 should be drawn.
189
190 \sa {QFontMetricsF::underlinePos()}, overlinePosition, strikeOutPosition
191*/
192qreal QQuickFontMetrics::underlinePosition() const
193{
194 return m_metrics.underlinePos();
195}
196
197/*!
198 \qmlproperty real QtQuick::FontMetrics::overlinePosition
199
200 This property holds the distance from the base line to where an overline
201 should be drawn.
202
203 \sa {QFontMetricsF::overlinePos()}, underlinePosition, strikeOutPosition
204*/
205qreal QQuickFontMetrics::overlinePosition() const
206{
207 return m_metrics.overlinePos();
208}
209
210/*!
211 \qmlproperty real QtQuick::FontMetrics::strikeOutPosition
212
213 This property holds the distance from the base line to where the strikeout
214 line should be drawn.
215
216 \sa {QFontMetricsF::strikeOutPos()}, overlinePosition, underlinePosition
217*/
218qreal QQuickFontMetrics::strikeOutPosition() const
219{
220 return m_metrics.strikeOutPos();
221}
222
223/*!
224 \qmlproperty real QtQuick::FontMetrics::lineWidth
225
226 This property holds the width of the underline and strikeout lines,
227 adjusted for the point size of the font.
228
229 \sa {QFontMetricsF::lineWidth()}
230*/
231qreal QQuickFontMetrics::lineWidth() const
232{
233 return m_metrics.lineWidth();
234}
235
236/*!
237 \qmlmethod qreal QtQuick::FontMetrics::advanceWidth(string text)
238
239 This method returns the advance in pixels of the characters in \a text.
240 This is the distance from the position of the string to where the next
241 string should be drawn.
242
243 This method is offered as an imperative alternative to the
244 \l {QtQuick::TextMetrics::advanceWidth}{advanceWidth} property of
245 TextMetrics.
246
247 \sa {QFontMetricsF::horizontalAdvance()}, {QFontMetricsF::height()}
248*/
249qreal QQuickFontMetrics::advanceWidth(const QString &text) const
250{
251 return m_metrics.horizontalAdvance(text);
252}
253
254/*!
255 \qmlmethod rect QtQuick::FontMetrics::boundingRect(string text)
256
257 This method returns the bounding rectangle of the characters in the string
258 specified by \a text.
259
260 This method is offered as an imperative alternative to the
261 \l {QtQuick::TextMetrics::boundingRect}{boundingRect} property of
262 TextMetrics.
263
264 \sa {QFontMetricsF::boundingRect()}, tightBoundingRect()
265*/
266QRectF QQuickFontMetrics::boundingRect(const QString &text) const
267{
268 return m_metrics.boundingRect(text);
269}
270
271/*!
272 \qmlmethod rect QtQuick::FontMetrics::tightBoundingRect(string text)
273
274 This method returns a tight bounding rectangle around the characters in the
275 string specified by \a text.
276
277 This method is offered as an imperative alternative to the
278 \l {QtQuick::TextMetrics::tightBoundingRect}{tightBoundingRect} property of
279 TextMetrics.
280
281 \sa {QFontMetricsF::tightBoundingRect()}, boundingRect()
282*/
283QRectF QQuickFontMetrics::tightBoundingRect(const QString &text) const
284{
285 return m_metrics.tightBoundingRect(text);
286}
287
288/*!
289 \qmlmethod string QtQuick::FontMetrics::elidedText(string text, enumeration mode, real width, int flags)
290
291 This method returns an elided version of the string (i.e., a
292 string with "..." in it) if the string \a text is wider than \a width.
293 Otherwise, returns the original string.
294
295 The \a mode argument specifies the text elide mode; that is, where
296 the ellipsis should appear when displaying text that doesn't fit.
297
298 The \a flags argument is optional and currently only supports
299 \l {Qt::TextShowMnemonic}.
300
301 This method is offered as an imperative alternative to the
302 \l {QtQuick::TextMetrics::elidedText}{elidedText} property of
303 TextMetrics.
304
305 \sa Qt::TextElideMode, QFontMetricsF::elidedText()
306*/
307QString QQuickFontMetrics::elidedText(const QString &text, Qt::TextElideMode mode, qreal width, int flags) const
308{
309 return m_metrics.elidedText(text, mode, width, flags);
310}
311
312/*!
313 \qmlproperty real QtQuick::FontMetrics::capitalHeight
314
315 \since 6.9
316
317 Returns the capital height as specified by the font.
318
319 The cap-height of a font is defined as the height of a capital letter above the baseline. It
320 specifically refers to the height of capital letters that are flat - such as H or I - as opposed
321 to round letters such as O, or pointed letters like A, both of which may display overshoot.
322
323 \sa {QFontMetricsF::capHeight}, ascent, descent, height, xHeight
324*/
325qreal QQuickFontMetrics::capitalHeight() const
326{
327 return m_metrics.capHeight();
328}
329
330QT_END_NAMESPACE
331
332#include "moc_qquickfontmetrics_p.cpp"