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