1 | // Copyright (C) 2021 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 | |
4 | #include "qquicktextmetrics_p.h" |
5 | |
6 | #include <QFont> |
7 | #include <QTextOption> |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | /*! |
12 | \qmltype TextMetrics |
13 | \instantiates QQuickTextMetrics |
14 | \inqmlmodule QtQuick |
15 | \since 5.4 |
16 | \ingroup qtquick-text-utility |
17 | \brief Provides metrics for a given font and text. |
18 | |
19 | TextMetrics calculates various properties of a given string of text for a |
20 | particular font. |
21 | |
22 | It provides a declarative API for the functions in \l QFontMetricsF which |
23 | take arguments. |
24 | |
25 | \code |
26 | TextMetrics { |
27 | id: textMetrics |
28 | font.family: "Arial" |
29 | elide: Text.ElideMiddle |
30 | elideWidth: 100 |
31 | text: "Hello World" |
32 | } |
33 | |
34 | MyItem { |
35 | text: textMetrics.elidedText |
36 | } |
37 | \endcode |
38 | |
39 | \sa QFontMetricsF, FontMetrics |
40 | */ |
41 | QQuickTextMetrics::QQuickTextMetrics(QObject *parent) : |
42 | QObject(parent), |
43 | m_metrics(m_font), |
44 | m_elide(Qt::ElideNone), |
45 | m_elideWidth(0), |
46 | m_renderType(QQuickText::QtRendering) |
47 | { |
48 | } |
49 | |
50 | /*! |
51 | \qmlproperty font QtQuick::TextMetrics::font |
52 | |
53 | This property holds the font used for the metrics calculations. |
54 | */ |
55 | QFont QQuickTextMetrics::font() const |
56 | { |
57 | return m_font; |
58 | } |
59 | |
60 | void QQuickTextMetrics::setFont(const QFont &font) |
61 | { |
62 | if (m_font != font) { |
63 | m_font = font; |
64 | m_metrics = QFontMetricsF(m_font); |
65 | emit fontChanged(); |
66 | emit metricsChanged(); |
67 | } |
68 | } |
69 | |
70 | /*! |
71 | \qmlproperty string QtQuick::TextMetrics::text |
72 | |
73 | This property holds the text used for the metrics calculations. |
74 | */ |
75 | QString QQuickTextMetrics::text() const |
76 | { |
77 | return m_text; |
78 | } |
79 | |
80 | void QQuickTextMetrics::setText(const QString &text) |
81 | { |
82 | if (m_text != text) { |
83 | m_text = text; |
84 | emit textChanged(); |
85 | emit metricsChanged(); |
86 | } |
87 | } |
88 | |
89 | /*! |
90 | \qmlproperty enumeration QtQuick::TextMetrics::elide |
91 | |
92 | This property holds the elide mode of the text. This determines the |
93 | position in which the string is elided. The possible values are: |
94 | |
95 | \value Qt::ElideNone No eliding; this is the default value. |
96 | \value Qt::ElideLeft For example: "...World" |
97 | \value Qt::ElideMiddle For example: "He...ld" |
98 | \value Qt::ElideRight For example: "Hello..." |
99 | |
100 | \sa elideWidth, QFontMetrics::elidedText |
101 | */ |
102 | Qt::TextElideMode QQuickTextMetrics::elide() const |
103 | { |
104 | return m_elide; |
105 | } |
106 | |
107 | void QQuickTextMetrics::setElide(Qt::TextElideMode elide) |
108 | { |
109 | if (m_elide != elide) { |
110 | m_elide = elide; |
111 | emit elideChanged(); |
112 | emit metricsChanged(); |
113 | } |
114 | } |
115 | |
116 | /*! |
117 | \qmlproperty real QtQuick::TextMetrics::elideWidth |
118 | |
119 | This property holds the largest width the text can have (in pixels) before |
120 | eliding will occur. |
121 | |
122 | \sa elide, QFontMetrics::elidedText |
123 | */ |
124 | qreal QQuickTextMetrics::elideWidth() const |
125 | { |
126 | return m_elideWidth; |
127 | } |
128 | |
129 | void QQuickTextMetrics::setElideWidth(qreal elideWidth) |
130 | { |
131 | if (m_elideWidth != elideWidth) { |
132 | m_elideWidth = elideWidth; |
133 | emit elideWidthChanged(); |
134 | emit metricsChanged(); |
135 | } |
136 | } |
137 | |
138 | /*! |
139 | \qmlproperty real QtQuick::TextMetrics::advanceWidth |
140 | |
141 | This property holds the advance in pixels of the characters in \l text. |
142 | This is the distance from the position of the string to where the next |
143 | string should be drawn. |
144 | |
145 | \sa {QFontMetricsF::horizontalAdvance()} |
146 | */ |
147 | qreal QQuickTextMetrics::advanceWidth() const |
148 | { |
149 | QTextOption option; |
150 | option.setUseDesignMetrics(m_renderType == QQuickText::QtRendering); |
151 | return m_metrics.horizontalAdvance(string: m_text, textOption: option); |
152 | } |
153 | |
154 | /*! |
155 | \qmlproperty rect QtQuick::TextMetrics::boundingRect |
156 | |
157 | This property holds the bounding rectangle of the characters in the string |
158 | specified by \l text. |
159 | |
160 | \sa {QFontMetricsF::boundingRect()}, tightBoundingRect |
161 | */ |
162 | QRectF QQuickTextMetrics::boundingRect() const |
163 | { |
164 | QTextOption option; |
165 | option.setUseDesignMetrics(m_renderType == QQuickText::QtRendering); |
166 | return m_metrics.boundingRect(text: m_text, textOption: option); |
167 | } |
168 | |
169 | /*! |
170 | \qmlproperty real QtQuick::TextMetrics::width |
171 | |
172 | This property holds the width of the bounding rectangle of the characters |
173 | in the string specified by \l text. It is equivalent to: |
174 | |
175 | \code |
176 | textMetrics.boundingRect.width |
177 | \endcode |
178 | |
179 | \sa boundingRect |
180 | */ |
181 | qreal QQuickTextMetrics::width() const |
182 | { |
183 | return boundingRect().width(); |
184 | } |
185 | |
186 | /*! |
187 | \qmlproperty real QtQuick::TextMetrics::height |
188 | |
189 | This property holds the height of the bounding rectangle of the characters |
190 | in the string specified by \l text. It is equivalent to: |
191 | |
192 | \code |
193 | textMetrics.boundingRect.height |
194 | \endcode |
195 | |
196 | \sa boundingRect |
197 | */ |
198 | qreal QQuickTextMetrics::height() const |
199 | { |
200 | return boundingRect().height(); |
201 | } |
202 | |
203 | /*! |
204 | \qmlproperty rect QtQuick::TextMetrics::tightBoundingRect |
205 | |
206 | This property holds a tight bounding rectangle around the characters in the |
207 | string specified by \l text. |
208 | |
209 | \sa {QFontMetricsF::tightBoundingRect()}, boundingRect |
210 | */ |
211 | QRectF QQuickTextMetrics::tightBoundingRect() const |
212 | { |
213 | QTextOption option; |
214 | option.setUseDesignMetrics(m_renderType == QQuickText::QtRendering); |
215 | return m_metrics.tightBoundingRect(text: m_text, textOption: option); |
216 | } |
217 | |
218 | /*! |
219 | \qmlproperty string QtQuick::TextMetrics::elidedText |
220 | |
221 | This property holds an elided version of the string (i.e., a string with |
222 | "..." in it) if the string \l text is wider than \l elideWidth. If the |
223 | text is not wider than \l elideWidth, or \l elide is set to |
224 | \c Qt::ElideNone, this property will be equal to the original string. |
225 | |
226 | \sa {QFontMetricsF::elidedText()} |
227 | */ |
228 | QString QQuickTextMetrics::elidedText() const |
229 | { |
230 | return m_metrics.elidedText(text: m_text, mode: m_elide, width: m_elideWidth); |
231 | } |
232 | |
233 | /*! |
234 | \qmlproperty enumeration QtQuick::TextMetrics::renderType |
235 | |
236 | Override the default rendering type for this component. |
237 | |
238 | Supported render types are: |
239 | |
240 | \value TextEdit.QtRendering Text is rendered using a scalable distance field for each glyph. |
241 | \value TextEdit.NativeRendering Text is rendered using a platform-specific technique. |
242 | |
243 | This should match the intended \c renderType where you draw the text. |
244 | |
245 | \since 6.3 |
246 | \sa {Text::renderType}{Text.renderType} |
247 | */ |
248 | QQuickText::RenderType QQuickTextMetrics::renderType() const |
249 | { |
250 | return m_renderType; |
251 | } |
252 | |
253 | void QQuickTextMetrics::setRenderType(QQuickText::RenderType renderType) |
254 | { |
255 | if (m_renderType == renderType) |
256 | return; |
257 | |
258 | m_renderType = renderType; |
259 | emit renderTypeChanged(); |
260 | } |
261 | |
262 | QT_END_NAMESPACE |
263 | |
264 | #include "moc_qquicktextmetrics_p.cpp" |
265 | |