| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/text/qfontmetrics.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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 | #include "qfont.h" | - | ||||||||||||
| 41 | #include "qpaintdevice.h" | - | ||||||||||||
| 42 | #include "qfontmetrics.h" | - | ||||||||||||
| 43 | - | |||||||||||||
| 44 | #include "qfont_p.h" | - | ||||||||||||
| 45 | #include "qfontengine_p.h" | - | ||||||||||||
| 46 | - | |||||||||||||
| 47 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 48 | - | |||||||||||||
| 49 | - | |||||||||||||
| 50 | extern void qt_format_text(const QFont& font, const QRectF &_r, | - | ||||||||||||
| 51 | int tf, const QString &text, QRectF *brect, | - | ||||||||||||
| 52 | int tabStops, int *tabArray, int tabArrayLen, | - | ||||||||||||
| 53 | QPainter *painter); | - | ||||||||||||
| 54 | - | |||||||||||||
| 55 | /***************************************************************************** | - | ||||||||||||
| 56 | QFontMetrics member functions | - | ||||||||||||
| 57 | *****************************************************************************/ | - | ||||||||||||
| 58 | - | |||||||||||||
| 59 | /*! | - | ||||||||||||
| 60 | \class QFontMetrics | - | ||||||||||||
| 61 | \reentrant | - | ||||||||||||
| 62 | \inmodule QtGui | - | ||||||||||||
| 63 | - | |||||||||||||
| 64 | \brief The QFontMetrics class provides font metrics information. | - | ||||||||||||
| 65 | - | |||||||||||||
| 66 | \ingroup painting | - | ||||||||||||
| 67 | \ingroup shared | - | ||||||||||||
| 68 | - | |||||||||||||
| 69 | QFontMetrics functions calculate the size of characters and | - | ||||||||||||
| 70 | strings for a given font. There are three ways you can create a | - | ||||||||||||
| 71 | QFontMetrics object: | - | ||||||||||||
| 72 | - | |||||||||||||
| 73 | \list 1 | - | ||||||||||||
| 74 | \li Calling the QFontMetrics constructor with a QFont creates a | - | ||||||||||||
| 75 | font metrics object for a screen-compatible font, i.e. the font | - | ||||||||||||
| 76 | cannot be a printer font. If the font is changed | - | ||||||||||||
| 77 | later, the font metrics object is \e not updated. | - | ||||||||||||
| 78 | - | |||||||||||||
| 79 | (Note: If you use a printer font the values returned may be | - | ||||||||||||
| 80 | inaccurate. Printer fonts are not always accessible so the nearest | - | ||||||||||||
| 81 | screen font is used if a printer font is supplied.) | - | ||||||||||||
| 82 | - | |||||||||||||
| 83 | \li QWidget::fontMetrics() returns the font metrics for a widget's | - | ||||||||||||
| 84 | font. This is equivalent to QFontMetrics(widget->font()). If the | - | ||||||||||||
| 85 | widget's font is changed later, the font metrics object is \e not | - | ||||||||||||
| 86 | updated. | - | ||||||||||||
| 87 | - | |||||||||||||
| 88 | \li QPainter::fontMetrics() returns the font metrics for a | - | ||||||||||||
| 89 | painter's current font. If the painter's font is changed later, the | - | ||||||||||||
| 90 | font metrics object is \e not updated. | - | ||||||||||||
| 91 | \endlist | - | ||||||||||||
| 92 | - | |||||||||||||
| 93 | Once created, the object provides functions to access the | - | ||||||||||||
| 94 | individual metrics of the font, its characters, and for strings | - | ||||||||||||
| 95 | rendered in the font. | - | ||||||||||||
| 96 | - | |||||||||||||
| 97 | There are several functions that operate on the font: ascent(), | - | ||||||||||||
| 98 | descent(), height(), leading() and lineSpacing() return the basic | - | ||||||||||||
| 99 | size properties of the font. The underlinePos(), overlinePos(), | - | ||||||||||||
| 100 | strikeOutPos() and lineWidth() functions, return the properties of | - | ||||||||||||
| 101 | the line that underlines, overlines or strikes out the | - | ||||||||||||
| 102 | characters. These functions are all fast. | - | ||||||||||||
| 103 | - | |||||||||||||
| 104 | There are also some functions that operate on the set of glyphs in | - | ||||||||||||
| 105 | the font: minLeftBearing(), minRightBearing() and maxWidth(). | - | ||||||||||||
| 106 | These are by necessity slow, and we recommend avoiding them if | - | ||||||||||||
| 107 | possible. | - | ||||||||||||
| 108 | - | |||||||||||||
| 109 | For each character, you can get its width(), leftBearing() and | - | ||||||||||||
| 110 | rightBearing() and find out whether it is in the font using | - | ||||||||||||
| 111 | inFont(). You can also treat the character as a string, and use | - | ||||||||||||
| 112 | the string functions on it. | - | ||||||||||||
| 113 | - | |||||||||||||
| 114 | The string functions include width(), to return the width of a | - | ||||||||||||
| 115 | string in pixels (or points, for a printer), boundingRect(), to | - | ||||||||||||
| 116 | return a rectangle large enough to contain the rendered string, | - | ||||||||||||
| 117 | and size(), to return the size of that rectangle. | - | ||||||||||||
| 118 | - | |||||||||||||
| 119 | Example: | - | ||||||||||||
| 120 | \snippet code/src_gui_text_qfontmetrics.cpp 0 | - | ||||||||||||
| 121 | - | |||||||||||||
| 122 | \sa QFont, QFontInfo, QFontDatabase, {Character Map Example} | - | ||||||||||||
| 123 | */ | - | ||||||||||||
| 124 | - | |||||||||||||
| 125 | /*! | - | ||||||||||||
| 126 | \fn QRect QFontMetrics::boundingRect(int x, int y, int width, int height, | - | ||||||||||||
| 127 | int flags, const QString &text, int tabStops, int *tabArray) const | - | ||||||||||||
| 128 | \overload | - | ||||||||||||
| 129 | - | |||||||||||||
| 130 | Returns the bounding rectangle for the given \a text within the | - | ||||||||||||
| 131 | rectangle specified by the \a x and \a y coordinates, \a width, and | - | ||||||||||||
| 132 | \a height. | - | ||||||||||||
| 133 | - | |||||||||||||
| 134 | If Qt::TextExpandTabs is set in \a flags and \a tabArray is | - | ||||||||||||
| 135 | non-null, it specifies a 0-terminated sequence of pixel-positions | - | ||||||||||||
| 136 | for tabs; otherwise, if \a tabStops is non-zero, it is used as the | - | ||||||||||||
| 137 | tab spacing (in pixels). | - | ||||||||||||
| 138 | */ | - | ||||||||||||
| 139 | - | |||||||||||||
| 140 | /*! | - | ||||||||||||
| 141 | Constructs a font metrics object for \a font. | - | ||||||||||||
| 142 | - | |||||||||||||
| 143 | The font metrics will be compatible with the paintdevice used to | - | ||||||||||||
| 144 | create \a font. | - | ||||||||||||
| 145 | - | |||||||||||||
| 146 | The font metrics object holds the information for the font that is | - | ||||||||||||
| 147 | passed in the constructor at the time it is created, and is not | - | ||||||||||||
| 148 | updated if the font's attributes are changed later. | - | ||||||||||||
| 149 | - | |||||||||||||
| 150 | Use QFontMetrics(const QFont &, QPaintDevice *) to get the font | - | ||||||||||||
| 151 | metrics that are compatible with a certain paint device. | - | ||||||||||||
| 152 | */ | - | ||||||||||||
| 153 | QFontMetrics::QFontMetrics(const QFont &font) | - | ||||||||||||
| 154 | : d(font.d) | - | ||||||||||||
| 155 | { | - | ||||||||||||
| 156 | } never executed: end of block | 0 | ||||||||||||
| 157 | - | |||||||||||||
| 158 | /*! | - | ||||||||||||
| 159 | Constructs a font metrics object for \a font and \a paintdevice. | - | ||||||||||||
| 160 | - | |||||||||||||
| 161 | The font metrics will be compatible with the paintdevice passed. | - | ||||||||||||
| 162 | If the \a paintdevice is 0, the metrics will be screen-compatible, | - | ||||||||||||
| 163 | ie. the metrics you get if you use the font for drawing text on a | - | ||||||||||||
| 164 | \l{QWidget}{widgets} or \l{QPixmap}{pixmaps}, | - | ||||||||||||
| 165 | not on a QPicture or QPrinter. | - | ||||||||||||
| 166 | - | |||||||||||||
| 167 | The font metrics object holds the information for the font that is | - | ||||||||||||
| 168 | passed in the constructor at the time it is created, and is not | - | ||||||||||||
| 169 | updated if the font's attributes are changed later. | - | ||||||||||||
| 170 | */ | - | ||||||||||||
| 171 | QFontMetrics::QFontMetrics(const QFont &font, QPaintDevice *paintdevice) | - | ||||||||||||
| 172 | { | - | ||||||||||||
| 173 | int dpi = paintdevice ? paintdevice->logicalDpiY() : qt_defaultDpi();
| 0 | ||||||||||||
| 174 | const int screen = 0; | - | ||||||||||||
| 175 | if (font.d->dpi != dpi || font.d->screen != screen ) {
| 0 | ||||||||||||
| 176 | d = new QFontPrivate(*font.d); | - | ||||||||||||
| 177 | d->dpi = dpi; | - | ||||||||||||
| 178 | d->screen = screen; | - | ||||||||||||
| 179 | } else { never executed: end of block | 0 | ||||||||||||
| 180 | d = font.d; | - | ||||||||||||
| 181 | } never executed: end of block | 0 | ||||||||||||
| 182 | - | |||||||||||||
| 183 | } | - | ||||||||||||
| 184 | - | |||||||||||||
| 185 | /*! | - | ||||||||||||
| 186 | Constructs a copy of \a fm. | - | ||||||||||||
| 187 | */ | - | ||||||||||||
| 188 | QFontMetrics::QFontMetrics(const QFontMetrics &fm) | - | ||||||||||||
| 189 | : d(fm.d) | - | ||||||||||||
| 190 | { | - | ||||||||||||
| 191 | } never executed: end of block | 0 | ||||||||||||
| 192 | - | |||||||||||||
| 193 | /*! | - | ||||||||||||
| 194 | Destroys the font metrics object and frees all allocated | - | ||||||||||||
| 195 | resources. | - | ||||||||||||
| 196 | */ | - | ||||||||||||
| 197 | QFontMetrics::~QFontMetrics() | - | ||||||||||||
| 198 | { | - | ||||||||||||
| 199 | } | - | ||||||||||||
| 200 | - | |||||||||||||
| 201 | /*! | - | ||||||||||||
| 202 | Assigns the font metrics \a fm. | - | ||||||||||||
| 203 | */ | - | ||||||||||||
| 204 | QFontMetrics &QFontMetrics::operator=(const QFontMetrics &fm) | - | ||||||||||||
| 205 | { | - | ||||||||||||
| 206 | d = fm.d; | - | ||||||||||||
| 207 | return *this; never executed: return *this; | 0 | ||||||||||||
| 208 | } | - | ||||||||||||
| 209 | - | |||||||||||||
| 210 | /*! | - | ||||||||||||
| 211 | \fn QFontMetrics &QFontMetrics::operator=(QFontMetrics &&other) | - | ||||||||||||
| 212 | - | |||||||||||||
| 213 | Move-assigns \a other to this QFontMetrics instance. | - | ||||||||||||
| 214 | - | |||||||||||||
| 215 | \since 5.2 | - | ||||||||||||
| 216 | */ | - | ||||||||||||
| 217 | /*! | - | ||||||||||||
| 218 | \fn QFontMetricsF &QFontMetricsF::operator=(QFontMetricsF &&other) | - | ||||||||||||
| 219 | - | |||||||||||||
| 220 | Move-assigns \a other to this QFontMetricsF instance. | - | ||||||||||||
| 221 | */ | - | ||||||||||||
| 222 | - | |||||||||||||
| 223 | /*! | - | ||||||||||||
| 224 | \fn void QFontMetrics::swap(QFontMetrics &other) | - | ||||||||||||
| 225 | \since 5.0 | - | ||||||||||||
| 226 | - | |||||||||||||
| 227 | Swaps this font metrics instance with \a other. This function is | - | ||||||||||||
| 228 | very fast and never fails. | - | ||||||||||||
| 229 | */ | - | ||||||||||||
| 230 | - | |||||||||||||
| 231 | /*! | - | ||||||||||||
| 232 | Returns \c true if \a other is equal to this object; otherwise | - | ||||||||||||
| 233 | returns \c false. | - | ||||||||||||
| 234 | - | |||||||||||||
| 235 | Two font metrics are considered equal if they were constructed | - | ||||||||||||
| 236 | from the same QFont and the paint devices they were constructed | - | ||||||||||||
| 237 | for are considered compatible. | - | ||||||||||||
| 238 | - | |||||||||||||
| 239 | \sa operator!=() | - | ||||||||||||
| 240 | */ | - | ||||||||||||
| 241 | bool QFontMetrics::operator ==(const QFontMetrics &other) const | - | ||||||||||||
| 242 | { | - | ||||||||||||
| 243 | return d == other.d; never executed: return d == other.d; | 0 | ||||||||||||
| 244 | } | - | ||||||||||||
| 245 | - | |||||||||||||
| 246 | /*! | - | ||||||||||||
| 247 | \fn bool QFontMetrics::operator !=(const QFontMetrics &other) const | - | ||||||||||||
| 248 | - | |||||||||||||
| 249 | Returns \c true if \a other is not equal to this object; otherwise returns \c false. | - | ||||||||||||
| 250 | - | |||||||||||||
| 251 | Two font metrics are considered equal if they were constructed | - | ||||||||||||
| 252 | from the same QFont and the paint devices they were constructed | - | ||||||||||||
| 253 | for are considered compatible. | - | ||||||||||||
| 254 | - | |||||||||||||
| 255 | \sa operator==() | - | ||||||||||||
| 256 | */ | - | ||||||||||||
| 257 | - | |||||||||||||
| 258 | /*! | - | ||||||||||||
| 259 | Returns the ascent of the font. | - | ||||||||||||
| 260 | - | |||||||||||||
| 261 | The ascent of a font is the distance from the baseline to the | - | ||||||||||||
| 262 | highest position characters extend to. In practice, some font | - | ||||||||||||
| 263 | designers break this rule, e.g. when they put more than one accent | - | ||||||||||||
| 264 | on top of a character, or to accommodate an unusual character in | - | ||||||||||||
| 265 | an exotic language, so it is possible (though rare) that this | - | ||||||||||||
| 266 | value will be too small. | - | ||||||||||||
| 267 | - | |||||||||||||
| 268 | \sa descent() | - | ||||||||||||
| 269 | */ | - | ||||||||||||
| 270 | int QFontMetrics::ascent() const | - | ||||||||||||
| 271 | { | - | ||||||||||||
| 272 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 273 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 274 | return qRound(engine->ascent()); never executed: return qRound(engine->ascent()); | 0 | ||||||||||||
| 275 | } | - | ||||||||||||
| 276 | - | |||||||||||||
| 277 | - | |||||||||||||
| 278 | /*! | - | ||||||||||||
| 279 | Returns the descent of the font. | - | ||||||||||||
| 280 | - | |||||||||||||
| 281 | The descent is the distance from the base line to the lowest point | - | ||||||||||||
| 282 | characters extend to. In practice, some font designers break this rule, | - | ||||||||||||
| 283 | e.g. to accommodate an unusual character in an exotic language, so | - | ||||||||||||
| 284 | it is possible (though rare) that this value will be too small. | - | ||||||||||||
| 285 | - | |||||||||||||
| 286 | \sa ascent() | - | ||||||||||||
| 287 | */ | - | ||||||||||||
| 288 | int QFontMetrics::descent() const | - | ||||||||||||
| 289 | { | - | ||||||||||||
| 290 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 291 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 292 | return qRound(engine->descent()); never executed: return qRound(engine->descent()); | 0 | ||||||||||||
| 293 | } | - | ||||||||||||
| 294 | - | |||||||||||||
| 295 | /*! | - | ||||||||||||
| 296 | Returns the height of the font. | - | ||||||||||||
| 297 | - | |||||||||||||
| 298 | This is always equal to ascent()+descent(). | - | ||||||||||||
| 299 | - | |||||||||||||
| 300 | \sa leading(), lineSpacing() | - | ||||||||||||
| 301 | */ | - | ||||||||||||
| 302 | int QFontMetrics::height() const | - | ||||||||||||
| 303 | { | - | ||||||||||||
| 304 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 305 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 306 | return qRound(engine->ascent()) + qRound(engine->descent()); never executed: return qRound(engine->ascent()) + qRound(engine->descent()); | 0 | ||||||||||||
| 307 | } | - | ||||||||||||
| 308 | - | |||||||||||||
| 309 | /*! | - | ||||||||||||
| 310 | Returns the leading of the font. | - | ||||||||||||
| 311 | - | |||||||||||||
| 312 | This is the natural inter-line spacing. | - | ||||||||||||
| 313 | - | |||||||||||||
| 314 | \sa height(), lineSpacing() | - | ||||||||||||
| 315 | */ | - | ||||||||||||
| 316 | int QFontMetrics::leading() const | - | ||||||||||||
| 317 | { | - | ||||||||||||
| 318 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 319 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 320 | return qRound(engine->leading()); never executed: return qRound(engine->leading()); | 0 | ||||||||||||
| 321 | } | - | ||||||||||||
| 322 | - | |||||||||||||
| 323 | /*! | - | ||||||||||||
| 324 | Returns the distance from one base line to the next. | - | ||||||||||||
| 325 | - | |||||||||||||
| 326 | This value is always equal to leading()+height(). | - | ||||||||||||
| 327 | - | |||||||||||||
| 328 | \sa height(), leading() | - | ||||||||||||
| 329 | */ | - | ||||||||||||
| 330 | int QFontMetrics::lineSpacing() const | - | ||||||||||||
| 331 | { | - | ||||||||||||
| 332 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 333 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 334 | return qRound(engine->leading()) + qRound(engine->ascent()) + qRound(engine->descent()); never executed: return qRound(engine->leading()) + qRound(engine->ascent()) + qRound(engine->descent()); | 0 | ||||||||||||
| 335 | } | - | ||||||||||||
| 336 | - | |||||||||||||
| 337 | /*! | - | ||||||||||||
| 338 | Returns the minimum left bearing of the font. | - | ||||||||||||
| 339 | - | |||||||||||||
| 340 | This is the smallest leftBearing(char) of all characters in the | - | ||||||||||||
| 341 | font. | - | ||||||||||||
| 342 | - | |||||||||||||
| 343 | Note that this function can be very slow if the font is large. | - | ||||||||||||
| 344 | - | |||||||||||||
| 345 | \sa minRightBearing(), leftBearing() | - | ||||||||||||
| 346 | */ | - | ||||||||||||
| 347 | int QFontMetrics::minLeftBearing() const | - | ||||||||||||
| 348 | { | - | ||||||||||||
| 349 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 350 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 351 | return qRound(engine->minLeftBearing()); never executed: return qRound(engine->minLeftBearing()); | 0 | ||||||||||||
| 352 | } | - | ||||||||||||
| 353 | - | |||||||||||||
| 354 | /*! | - | ||||||||||||
| 355 | Returns the minimum right bearing of the font. | - | ||||||||||||
| 356 | - | |||||||||||||
| 357 | This is the smallest rightBearing(char) of all characters in the | - | ||||||||||||
| 358 | font. | - | ||||||||||||
| 359 | - | |||||||||||||
| 360 | Note that this function can be very slow if the font is large. | - | ||||||||||||
| 361 | - | |||||||||||||
| 362 | \sa minLeftBearing(), rightBearing() | - | ||||||||||||
| 363 | */ | - | ||||||||||||
| 364 | int QFontMetrics::minRightBearing() const | - | ||||||||||||
| 365 | { | - | ||||||||||||
| 366 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 367 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 368 | return qRound(engine->minRightBearing()); never executed: return qRound(engine->minRightBearing()); | 0 | ||||||||||||
| 369 | } | - | ||||||||||||
| 370 | - | |||||||||||||
| 371 | /*! | - | ||||||||||||
| 372 | Returns the width of the widest character in the font. | - | ||||||||||||
| 373 | */ | - | ||||||||||||
| 374 | int QFontMetrics::maxWidth() const | - | ||||||||||||
| 375 | { | - | ||||||||||||
| 376 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 377 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 378 | return qRound(engine->maxCharWidth()); never executed: return qRound(engine->maxCharWidth()); | 0 | ||||||||||||
| 379 | } | - | ||||||||||||
| 380 | - | |||||||||||||
| 381 | /*! | - | ||||||||||||
| 382 | Returns the 'x' height of the font. This is often but not always | - | ||||||||||||
| 383 | the same as the height of the character 'x'. | - | ||||||||||||
| 384 | */ | - | ||||||||||||
| 385 | int QFontMetrics::xHeight() const | - | ||||||||||||
| 386 | { | - | ||||||||||||
| 387 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 388 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 389 | if (d->capital == QFont::SmallCaps)
| 0 | ||||||||||||
| 390 | return qRound(d->smallCapsFontPrivate()->engineForScript(QChar::Script_Common)->ascent()); never executed: return qRound(d->smallCapsFontPrivate()->engineForScript(QChar::Script_Common)->ascent()); | 0 | ||||||||||||
| 391 | return qRound(engine->xHeight()); never executed: return qRound(engine->xHeight()); | 0 | ||||||||||||
| 392 | } | - | ||||||||||||
| 393 | - | |||||||||||||
| 394 | /*! | - | ||||||||||||
| 395 | \since 4.2 | - | ||||||||||||
| 396 | - | |||||||||||||
| 397 | Returns the average width of glyphs in the font. | - | ||||||||||||
| 398 | */ | - | ||||||||||||
| 399 | int QFontMetrics::averageCharWidth() const | - | ||||||||||||
| 400 | { | - | ||||||||||||
| 401 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 402 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 403 | return qRound(engine->averageCharWidth()); never executed: return qRound(engine->averageCharWidth()); | 0 | ||||||||||||
| 404 | } | - | ||||||||||||
| 405 | - | |||||||||||||
| 406 | /*! | - | ||||||||||||
| 407 | Returns \c true if character \a ch is a valid character in the font; | - | ||||||||||||
| 408 | otherwise returns \c false. | - | ||||||||||||
| 409 | */ | - | ||||||||||||
| 410 | bool QFontMetrics::inFont(QChar ch) const | - | ||||||||||||
| 411 | { | - | ||||||||||||
| 412 | return inFontUcs4(ch.unicode()); never executed: return inFontUcs4(ch.unicode()); | 0 | ||||||||||||
| 413 | } | - | ||||||||||||
| 414 | - | |||||||||||||
| 415 | /*! | - | ||||||||||||
| 416 | Returns \c true if the character \a ucs4 encoded in UCS-4/UTF-32 is a valid | - | ||||||||||||
| 417 | character in the font; otherwise returns \c false. | - | ||||||||||||
| 418 | */ | - | ||||||||||||
| 419 | bool QFontMetrics::inFontUcs4(uint ucs4) const | - | ||||||||||||
| 420 | { | - | ||||||||||||
| 421 | const int script = QChar::script(ucs4); | - | ||||||||||||
| 422 | QFontEngine *engine = d->engineForScript(script); | - | ||||||||||||
| 423 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 424 | if (engine->type() == QFontEngine::Box)
| 0 | ||||||||||||
| 425 | return false; never executed: return false; | 0 | ||||||||||||
| 426 | return engine->canRender(ucs4); never executed: return engine->canRender(ucs4); | 0 | ||||||||||||
| 427 | } | - | ||||||||||||
| 428 | - | |||||||||||||
| 429 | /*! | - | ||||||||||||
| 430 | Returns the left bearing of character \a ch in the font. | - | ||||||||||||
| 431 | - | |||||||||||||
| 432 | The left bearing is the right-ward distance of the left-most pixel | - | ||||||||||||
| 433 | of the character from the logical origin of the character. This | - | ||||||||||||
| 434 | value is negative if the pixels of the character extend to the | - | ||||||||||||
| 435 | left of the logical origin. | - | ||||||||||||
| 436 | - | |||||||||||||
| 437 | See width(QChar) for a graphical description of this metric. | - | ||||||||||||
| 438 | - | |||||||||||||
| 439 | \sa rightBearing(), minLeftBearing(), width() | - | ||||||||||||
| 440 | */ | - | ||||||||||||
| 441 | int QFontMetrics::leftBearing(QChar ch) const | - | ||||||||||||
| 442 | { | - | ||||||||||||
| 443 | const int script = ch.script(); | - | ||||||||||||
| 444 | QFontEngine *engine; | - | ||||||||||||
| 445 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
| 446 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
| 447 | else | - | ||||||||||||
| 448 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
| 449 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 450 | if (engine->type() == QFontEngine::Box)
| 0 | ||||||||||||
| 451 | return 0; never executed: return 0; | 0 | ||||||||||||
| 452 | - | |||||||||||||
| 453 | d->alterCharForCapitalization(ch); | - | ||||||||||||
| 454 | - | |||||||||||||
| 455 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
| 456 | - | |||||||||||||
| 457 | qreal lb; | - | ||||||||||||
| 458 | engine->getGlyphBearings(glyph, &lb); | - | ||||||||||||
| 459 | return qRound(lb); never executed: return qRound(lb); | 0 | ||||||||||||
| 460 | } | - | ||||||||||||
| 461 | - | |||||||||||||
| 462 | /*! | - | ||||||||||||
| 463 | Returns the right bearing of character \a ch in the font. | - | ||||||||||||
| 464 | - | |||||||||||||
| 465 | The right bearing is the left-ward distance of the right-most | - | ||||||||||||
| 466 | pixel of the character from the logical origin of a subsequent | - | ||||||||||||
| 467 | character. This value is negative if the pixels of the character | - | ||||||||||||
| 468 | extend to the right of the width() of the character. | - | ||||||||||||
| 469 | - | |||||||||||||
| 470 | See width() for a graphical description of this metric. | - | ||||||||||||
| 471 | - | |||||||||||||
| 472 | \sa leftBearing(), minRightBearing(), width() | - | ||||||||||||
| 473 | */ | - | ||||||||||||
| 474 | int QFontMetrics::rightBearing(QChar ch) const | - | ||||||||||||
| 475 | { | - | ||||||||||||
| 476 | const int script = ch.script(); | - | ||||||||||||
| 477 | QFontEngine *engine; | - | ||||||||||||
| 478 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
| 479 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
| 480 | else | - | ||||||||||||
| 481 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
| 482 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 483 | if (engine->type() == QFontEngine::Box)
| 0 | ||||||||||||
| 484 | return 0; never executed: return 0; | 0 | ||||||||||||
| 485 | - | |||||||||||||
| 486 | d->alterCharForCapitalization(ch); | - | ||||||||||||
| 487 | - | |||||||||||||
| 488 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
| 489 | - | |||||||||||||
| 490 | qreal rb; | - | ||||||||||||
| 491 | engine->getGlyphBearings(glyph, 0, &rb); | - | ||||||||||||
| 492 | return qRound(rb); never executed: return qRound(rb); | 0 | ||||||||||||
| 493 | } | - | ||||||||||||
| 494 | - | |||||||||||||
| 495 | /*! | - | ||||||||||||
| 496 | Returns the width in pixels of the first \a len characters of \a | - | ||||||||||||
| 497 | text. If \a len is negative (the default), the entire string is | - | ||||||||||||
| 498 | used. | - | ||||||||||||
| 499 | - | |||||||||||||
| 500 | Note that this value is \e not equal to boundingRect().width(); | - | ||||||||||||
| 501 | boundingRect() returns a rectangle describing the pixels this | - | ||||||||||||
| 502 | string will cover whereas width() returns the distance to where | - | ||||||||||||
| 503 | the next string should be drawn. | - | ||||||||||||
| 504 | - | |||||||||||||
| 505 | \sa boundingRect() | - | ||||||||||||
| 506 | */ | - | ||||||||||||
| 507 | int QFontMetrics::width(const QString &text, int len) const | - | ||||||||||||
| 508 | { | - | ||||||||||||
| 509 | return width(text, len, 0); never executed: return width(text, len, 0); | 0 | ||||||||||||
| 510 | } | - | ||||||||||||
| 511 | - | |||||||||||||
| 512 | /*! | - | ||||||||||||
| 513 | \internal | - | ||||||||||||
| 514 | */ | - | ||||||||||||
| 515 | int QFontMetrics::width(const QString &text, int len, int flags) const | - | ||||||||||||
| 516 | { | - | ||||||||||||
| 517 | int pos = text.indexOf(QLatin1Char('\x9c')); | - | ||||||||||||
| 518 | if (pos != -1) {
| 0 | ||||||||||||
| 519 | len = (len < 0) ? pos : qMin(pos, len);
| 0 | ||||||||||||
| 520 | } else if (len < 0) { never executed: end of block
| 0 | ||||||||||||
| 521 | len = text.length(); | - | ||||||||||||
| 522 | } never executed: end of block | 0 | ||||||||||||
| 523 | if (len == 0)
| 0 | ||||||||||||
| 524 | return 0; never executed: return 0; | 0 | ||||||||||||
| 525 | - | |||||||||||||
| 526 | if (flags & Qt::TextBypassShaping) {
| 0 | ||||||||||||
| 527 | // Skip complex shaping, only use advances | - | ||||||||||||
| 528 | int numGlyphs = len; | - | ||||||||||||
| 529 | QVarLengthGlyphLayoutArray glyphs(numGlyphs); | - | ||||||||||||
| 530 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 531 | if (!engine->stringToCMap(text.data(), len, &glyphs, &numGlyphs, 0))
| 0 | ||||||||||||
| 532 | Q_UNREACHABLE(); never executed: end of block | 0 | ||||||||||||
| 533 | - | |||||||||||||
| 534 | QFixed width; | - | ||||||||||||
| 535 | for (int i = 0; i < numGlyphs; ++i)
| 0 | ||||||||||||
| 536 | width += glyphs.advances[i]; never executed: width += glyphs.advances[i]; | 0 | ||||||||||||
| 537 | return qRound(width); never executed: return qRound(width); | 0 | ||||||||||||
| 538 | } | - | ||||||||||||
| 539 | - | |||||||||||||
| 540 | QStackTextEngine layout(text, QFont(d.data())); | - | ||||||||||||
| 541 | layout.ignoreBidi = true; | - | ||||||||||||
| 542 | return qRound(layout.width(0, len)); never executed: return qRound(layout.width(0, len)); | 0 | ||||||||||||
| 543 | } | - | ||||||||||||
| 544 | - | |||||||||||||
| 545 | /*! | - | ||||||||||||
| 546 | \overload | - | ||||||||||||
| 547 | - | |||||||||||||
| 548 | \image bearings.png Bearings | - | ||||||||||||
| 549 | - | |||||||||||||
| 550 | Returns the logical width of character \a ch in pixels. This is a | - | ||||||||||||
| 551 | distance appropriate for drawing a subsequent character after \a | - | ||||||||||||
| 552 | ch. | - | ||||||||||||
| 553 | - | |||||||||||||
| 554 | Some of the metrics are described in the image to the right. The | - | ||||||||||||
| 555 | central dark rectangles cover the logical width() of each | - | ||||||||||||
| 556 | character. The outer pale rectangles cover the leftBearing() and | - | ||||||||||||
| 557 | rightBearing() of each character. Notice that the bearings of "f" | - | ||||||||||||
| 558 | in this particular font are both negative, while the bearings of | - | ||||||||||||
| 559 | "o" are both positive. | - | ||||||||||||
| 560 | - | |||||||||||||
| 561 | \warning This function will produce incorrect results for Arabic | - | ||||||||||||
| 562 | characters or non-spacing marks in the middle of a string, as the | - | ||||||||||||
| 563 | glyph shaping and positioning of marks that happens when | - | ||||||||||||
| 564 | processing strings cannot be taken into account. When implementing | - | ||||||||||||
| 565 | an interactive text control, use QTextLayout instead. | - | ||||||||||||
| 566 | - | |||||||||||||
| 567 | \sa boundingRect() | - | ||||||||||||
| 568 | */ | - | ||||||||||||
| 569 | int QFontMetrics::width(QChar ch) const | - | ||||||||||||
| 570 | { | - | ||||||||||||
| 571 | if (QChar::category(ch.unicode()) == QChar::Mark_NonSpacing)
| 0 | ||||||||||||
| 572 | return 0; never executed: return 0; | 0 | ||||||||||||
| 573 | - | |||||||||||||
| 574 | const int script = ch.script(); | - | ||||||||||||
| 575 | QFontEngine *engine; | - | ||||||||||||
| 576 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
| 577 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
| 578 | else | - | ||||||||||||
| 579 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
| 580 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 581 | - | |||||||||||||
| 582 | d->alterCharForCapitalization(ch); | - | ||||||||||||
| 583 | - | |||||||||||||
| 584 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
| 585 | QFixed advance; | - | ||||||||||||
| 586 | - | |||||||||||||
| 587 | QGlyphLayout glyphs; | - | ||||||||||||
| 588 | glyphs.numGlyphs = 1; | - | ||||||||||||
| 589 | glyphs.glyphs = &glyph; | - | ||||||||||||
| 590 | glyphs.advances = &advance; | - | ||||||||||||
| 591 | engine->recalcAdvances(&glyphs, 0); | - | ||||||||||||
| 592 | - | |||||||||||||
| 593 | return qRound(advance); never executed: return qRound(advance); | 0 | ||||||||||||
| 594 | } | - | ||||||||||||
| 595 | - | |||||||||||||
| 596 | #if QT_VERSION < QT_VERSION_CHECK(6,0,0) | - | ||||||||||||
| 597 | /*! \obsolete | - | ||||||||||||
| 598 | - | |||||||||||||
| 599 | Returns the width of the character at position \a pos in the | - | ||||||||||||
| 600 | string \a text. | - | ||||||||||||
| 601 | - | |||||||||||||
| 602 | The whole string is needed, as the glyph drawn may change | - | ||||||||||||
| 603 | depending on the context (the letter before and after the current | - | ||||||||||||
| 604 | one) for some languages (e.g. Arabic). | - | ||||||||||||
| 605 | - | |||||||||||||
| 606 | This function also takes non spacing marks and ligatures into | - | ||||||||||||
| 607 | account. | - | ||||||||||||
| 608 | */ | - | ||||||||||||
| 609 | int QFontMetrics::charWidth(const QString &text, int pos) const | - | ||||||||||||
| 610 | { | - | ||||||||||||
| 611 | int width = 0; | - | ||||||||||||
| 612 | if (pos < 0 || pos > (int)text.length())
| 0 | ||||||||||||
| 613 | return width; never executed: return width; | 0 | ||||||||||||
| 614 | - | |||||||||||||
| 615 | QChar ch = text.at(pos); | - | ||||||||||||
| 616 | const int script = ch.script(); | - | ||||||||||||
| 617 | if (script != QChar::Script_Common) {
| 0 | ||||||||||||
| 618 | // complex script shaping. Have to do some hard work | - | ||||||||||||
| 619 | int from = qMax(0, pos - 8); | - | ||||||||||||
| 620 | int to = qMin(text.length(), pos + 8); | - | ||||||||||||
| 621 | QString cstr = QString::fromRawData(text.unicode() + from, to - from); | - | ||||||||||||
| 622 | QStackTextEngine layout(cstr, QFont(d.data())); | - | ||||||||||||
| 623 | layout.ignoreBidi = true; | - | ||||||||||||
| 624 | layout.itemize(); | - | ||||||||||||
| 625 | width = qRound(layout.width(pos-from, 1)); | - | ||||||||||||
| 626 | } else if (ch.category() != QChar::Mark_NonSpacing) { never executed: end of block
| 0 | ||||||||||||
| 627 | QFontEngine *engine; | - | ||||||||||||
| 628 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
| 629 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
| 630 | else | - | ||||||||||||
| 631 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
| 632 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 633 | - | |||||||||||||
| 634 | d->alterCharForCapitalization(ch); | - | ||||||||||||
| 635 | - | |||||||||||||
| 636 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
| 637 | QFixed advance; | - | ||||||||||||
| 638 | - | |||||||||||||
| 639 | QGlyphLayout glyphs; | - | ||||||||||||
| 640 | glyphs.numGlyphs = 1; | - | ||||||||||||
| 641 | glyphs.glyphs = &glyph; | - | ||||||||||||
| 642 | glyphs.advances = &advance; | - | ||||||||||||
| 643 | engine->recalcAdvances(&glyphs, 0); | - | ||||||||||||
| 644 | - | |||||||||||||
| 645 | width = qRound(advance); | - | ||||||||||||
| 646 | } never executed: end of block | 0 | ||||||||||||
| 647 | return width; never executed: return width; | 0 | ||||||||||||
| 648 | } | - | ||||||||||||
| 649 | #endif | - | ||||||||||||
| 650 | - | |||||||||||||
| 651 | /*! | - | ||||||||||||
| 652 | Returns the bounding rectangle of the characters in the string | - | ||||||||||||
| 653 | specified by \a text. The bounding rectangle always covers at least | - | ||||||||||||
| 654 | the set of pixels the text would cover if drawn at (0, 0). | - | ||||||||||||
| 655 | - | |||||||||||||
| 656 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
| 657 | e.g. for italicized fonts, and that the width of the returned | - | ||||||||||||
| 658 | rectangle might be different than what the width() method returns. | - | ||||||||||||
| 659 | - | |||||||||||||
| 660 | If you want to know the advance width of the string (to lay out | - | ||||||||||||
| 661 | a set of strings next to each other), use width() instead. | - | ||||||||||||
| 662 | - | |||||||||||||
| 663 | Newline characters are processed as normal characters, \e not as | - | ||||||||||||
| 664 | linebreaks. | - | ||||||||||||
| 665 | - | |||||||||||||
| 666 | The height of the bounding rectangle is at least as large as the | - | ||||||||||||
| 667 | value returned by height(). | - | ||||||||||||
| 668 | - | |||||||||||||
| 669 | \sa width(), height(), QPainter::boundingRect(), tightBoundingRect() | - | ||||||||||||
| 670 | */ | - | ||||||||||||
| 671 | QRect QFontMetrics::boundingRect(const QString &text) const | - | ||||||||||||
| 672 | { | - | ||||||||||||
| 673 | if (text.length() == 0)
| 0 | ||||||||||||
| 674 | return QRect(); never executed: return QRect(); | 0 | ||||||||||||
| 675 | - | |||||||||||||
| 676 | QStackTextEngine layout(text, QFont(d.data())); | - | ||||||||||||
| 677 | layout.ignoreBidi = true; | - | ||||||||||||
| 678 | layout.itemize(); | - | ||||||||||||
| 679 | glyph_metrics_t gm = layout.boundingBox(0, text.length()); | - | ||||||||||||
| 680 | return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height)); never executed: return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height)); | 0 | ||||||||||||
| 681 | } | - | ||||||||||||
| 682 | - | |||||||||||||
| 683 | /*! | - | ||||||||||||
| 684 | Returns the rectangle that is covered by ink if character \a ch | - | ||||||||||||
| 685 | were to be drawn at the origin of the coordinate system. | - | ||||||||||||
| 686 | - | |||||||||||||
| 687 | Note that the bounding rectangle may extend to the left of (0, 0) | - | ||||||||||||
| 688 | (e.g., for italicized fonts), and that the text output may cover \e | - | ||||||||||||
| 689 | all pixels in the bounding rectangle. For a space character the rectangle | - | ||||||||||||
| 690 | will usually be empty. | - | ||||||||||||
| 691 | - | |||||||||||||
| 692 | Note that the rectangle usually extends both above and below the | - | ||||||||||||
| 693 | base line. | - | ||||||||||||
| 694 | - | |||||||||||||
| 695 | \warning The width of the returned rectangle is not the advance width | - | ||||||||||||
| 696 | of the character. Use boundingRect(const QString &) or width() instead. | - | ||||||||||||
| 697 | - | |||||||||||||
| 698 | \sa width() | - | ||||||||||||
| 699 | */ | - | ||||||||||||
| 700 | QRect QFontMetrics::boundingRect(QChar ch) const | - | ||||||||||||
| 701 | { | - | ||||||||||||
| 702 | const int script = ch.script(); | - | ||||||||||||
| 703 | QFontEngine *engine; | - | ||||||||||||
| 704 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
| 705 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
| 706 | else | - | ||||||||||||
| 707 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
| 708 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 709 | - | |||||||||||||
| 710 | d->alterCharForCapitalization(ch); | - | ||||||||||||
| 711 | - | |||||||||||||
| 712 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
| 713 | - | |||||||||||||
| 714 | glyph_metrics_t gm = engine->boundingBox(glyph); | - | ||||||||||||
| 715 | return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height)); never executed: return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height)); | 0 | ||||||||||||
| 716 | } | - | ||||||||||||
| 717 | - | |||||||||||||
| 718 | /*! | - | ||||||||||||
| 719 | \overload | - | ||||||||||||
| 720 | - | |||||||||||||
| 721 | Returns the bounding rectangle of the characters in the string | - | ||||||||||||
| 722 | specified by \a text, which is the set of pixels the text would | - | ||||||||||||
| 723 | cover if drawn at (0, 0). The drawing, and hence the bounding | - | ||||||||||||
| 724 | rectangle, is constrained to the rectangle \a rect. | - | ||||||||||||
| 725 | - | |||||||||||||
| 726 | The \a flags argument is the bitwise OR of the following flags: | - | ||||||||||||
| 727 | \list | - | ||||||||||||
| 728 | \li Qt::AlignLeft aligns to the left border, except for | - | ||||||||||||
| 729 | Arabic and Hebrew where it aligns to the right. | - | ||||||||||||
| 730 | \li Qt::AlignRight aligns to the right border, except for | - | ||||||||||||
| 731 | Arabic and Hebrew where it aligns to the left. | - | ||||||||||||
| 732 | \li Qt::AlignJustify produces justified text. | - | ||||||||||||
| 733 | \li Qt::AlignHCenter aligns horizontally centered. | - | ||||||||||||
| 734 | \li Qt::AlignTop aligns to the top border. | - | ||||||||||||
| 735 | \li Qt::AlignBottom aligns to the bottom border. | - | ||||||||||||
| 736 | \li Qt::AlignVCenter aligns vertically centered | - | ||||||||||||
| 737 | \li Qt::AlignCenter (== \c{Qt::AlignHCenter | Qt::AlignVCenter}) | - | ||||||||||||
| 738 | \li Qt::TextSingleLine ignores newline characters in the text. | - | ||||||||||||
| 739 | \li Qt::TextExpandTabs expands tabs (see below) | - | ||||||||||||
| 740 | \li Qt::TextShowMnemonic interprets "&x" as \underline{x}; i.e., underlined. | - | ||||||||||||
| 741 | \li Qt::TextWordWrap breaks the text to fit the rectangle. | - | ||||||||||||
| 742 | \endlist | - | ||||||||||||
| 743 | - | |||||||||||||
| 744 | Qt::Horizontal alignment defaults to Qt::AlignLeft and vertical | - | ||||||||||||
| 745 | alignment defaults to Qt::AlignTop. | - | ||||||||||||
| 746 | - | |||||||||||||
| 747 | If several of the horizontal or several of the vertical alignment | - | ||||||||||||
| 748 | flags are set, the resulting alignment is undefined. | - | ||||||||||||
| 749 | - | |||||||||||||
| 750 | If Qt::TextExpandTabs is set in \a flags, then: if \a tabArray is | - | ||||||||||||
| 751 | non-null, it specifies a 0-terminated sequence of pixel-positions | - | ||||||||||||
| 752 | for tabs; otherwise if \a tabStops is non-zero, it is used as the | - | ||||||||||||
| 753 | tab spacing (in pixels). | - | ||||||||||||
| 754 | - | |||||||||||||
| 755 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
| 756 | e.g. for italicized fonts, and that the text output may cover \e | - | ||||||||||||
| 757 | all pixels in the bounding rectangle. | - | ||||||||||||
| 758 | - | |||||||||||||
| 759 | Newline characters are processed as linebreaks. | - | ||||||||||||
| 760 | - | |||||||||||||
| 761 | Despite the different actual character heights, the heights of the | - | ||||||||||||
| 762 | bounding rectangles of "Yes" and "yes" are the same. | - | ||||||||||||
| 763 | - | |||||||||||||
| 764 | The bounding rectangle returned by this function is somewhat larger | - | ||||||||||||
| 765 | than that calculated by the simpler boundingRect() function. This | - | ||||||||||||
| 766 | function uses the \l{minLeftBearing()}{maximum left} and | - | ||||||||||||
| 767 | \l{minRightBearing()}{right} font bearings as is | - | ||||||||||||
| 768 | necessary for multi-line text to align correctly. Also, | - | ||||||||||||
| 769 | fontHeight() and lineSpacing() are used to calculate the height, | - | ||||||||||||
| 770 | rather than individual character heights. | - | ||||||||||||
| 771 | - | |||||||||||||
| 772 | \sa width(), QPainter::boundingRect(), Qt::Alignment | - | ||||||||||||
| 773 | */ | - | ||||||||||||
| 774 | QRect QFontMetrics::boundingRect(const QRect &rect, int flags, const QString &text, int tabStops, | - | ||||||||||||
| 775 | int *tabArray) const | - | ||||||||||||
| 776 | { | - | ||||||||||||
| 777 | int tabArrayLen = 0; | - | ||||||||||||
| 778 | if (tabArray)
| 0 | ||||||||||||
| 779 | while (tabArray[tabArrayLen])
| 0 | ||||||||||||
| 780 | tabArrayLen++; never executed: tabArrayLen++; | 0 | ||||||||||||
| 781 | - | |||||||||||||
| 782 | QRectF rb; | - | ||||||||||||
| 783 | QRectF rr(rect); | - | ||||||||||||
| 784 | qt_format_text(QFont(d.data()), rr, flags | Qt::TextDontPrint, text, &rb, tabStops, tabArray, | - | ||||||||||||
| 785 | tabArrayLen, 0); | - | ||||||||||||
| 786 | - | |||||||||||||
| 787 | return rb.toAlignedRect(); never executed: return rb.toAlignedRect(); | 0 | ||||||||||||
| 788 | } | - | ||||||||||||
| 789 | - | |||||||||||||
| 790 | /*! | - | ||||||||||||
| 791 | Returns the size in pixels of \a text. | - | ||||||||||||
| 792 | - | |||||||||||||
| 793 | The \a flags argument is the bitwise OR of the following flags: | - | ||||||||||||
| 794 | \list | - | ||||||||||||
| 795 | \li Qt::TextSingleLine ignores newline characters. | - | ||||||||||||
| 796 | \li Qt::TextExpandTabs expands tabs (see below) | - | ||||||||||||
| 797 | \li Qt::TextShowMnemonic interprets "&x" as \underline{x}; i.e., underlined. | - | ||||||||||||
| 798 | \li Qt::TextWordWrap breaks the text to fit the rectangle. | - | ||||||||||||
| 799 | \endlist | - | ||||||||||||
| 800 | - | |||||||||||||
| 801 | If Qt::TextExpandTabs is set in \a flags, then: if \a tabArray is | - | ||||||||||||
| 802 | non-null, it specifies a 0-terminated sequence of pixel-positions | - | ||||||||||||
| 803 | for tabs; otherwise if \a tabStops is non-zero, it is used as the | - | ||||||||||||
| 804 | tab spacing (in pixels). | - | ||||||||||||
| 805 | - | |||||||||||||
| 806 | Newline characters are processed as linebreaks. | - | ||||||||||||
| 807 | - | |||||||||||||
| 808 | Despite the different actual character heights, the heights of the | - | ||||||||||||
| 809 | bounding rectangles of "Yes" and "yes" are the same. | - | ||||||||||||
| 810 | - | |||||||||||||
| 811 | \sa boundingRect() | - | ||||||||||||
| 812 | */ | - | ||||||||||||
| 813 | QSize QFontMetrics::size(int flags, const QString &text, int tabStops, int *tabArray) const | - | ||||||||||||
| 814 | { | - | ||||||||||||
| 815 | return boundingRect(QRect(0,0,0,0), flags | Qt::TextLongestVariant, text, tabStops, tabArray).size(); never executed: return boundingRect(QRect(0,0,0,0), flags | Qt::TextLongestVariant, text, tabStops, tabArray).size(); | 0 | ||||||||||||
| 816 | } | - | ||||||||||||
| 817 | - | |||||||||||||
| 818 | /*! | - | ||||||||||||
| 819 | \since 4.3 | - | ||||||||||||
| 820 | - | |||||||||||||
| 821 | Returns a tight bounding rectangle around the characters in the | - | ||||||||||||
| 822 | string specified by \a text. The bounding rectangle always covers | - | ||||||||||||
| 823 | at least the set of pixels the text would cover if drawn at (0, | - | ||||||||||||
| 824 | 0). | - | ||||||||||||
| 825 | - | |||||||||||||
| 826 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
| 827 | e.g. for italicized fonts, and that the width of the returned | - | ||||||||||||
| 828 | rectangle might be different than what the width() method returns. | - | ||||||||||||
| 829 | - | |||||||||||||
| 830 | If you want to know the advance width of the string (to lay out | - | ||||||||||||
| 831 | a set of strings next to each other), use width() instead. | - | ||||||||||||
| 832 | - | |||||||||||||
| 833 | Newline characters are processed as normal characters, \e not as | - | ||||||||||||
| 834 | linebreaks. | - | ||||||||||||
| 835 | - | |||||||||||||
| 836 | \warning Calling this method is very slow on Windows. | - | ||||||||||||
| 837 | - | |||||||||||||
| 838 | \sa width(), height(), boundingRect() | - | ||||||||||||
| 839 | */ | - | ||||||||||||
| 840 | QRect QFontMetrics::tightBoundingRect(const QString &text) const | - | ||||||||||||
| 841 | { | - | ||||||||||||
| 842 | if (text.length() == 0)
| 0 | ||||||||||||
| 843 | return QRect(); never executed: return QRect(); | 0 | ||||||||||||
| 844 | - | |||||||||||||
| 845 | QStackTextEngine layout(text, QFont(d.data())); | - | ||||||||||||
| 846 | layout.ignoreBidi = true; | - | ||||||||||||
| 847 | layout.itemize(); | - | ||||||||||||
| 848 | glyph_metrics_t gm = layout.tightBoundingBox(0, text.length()); | - | ||||||||||||
| 849 | return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height)); never executed: return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height)); | 0 | ||||||||||||
| 850 | } | - | ||||||||||||
| 851 | - | |||||||||||||
| 852 | - | |||||||||||||
| 853 | /*! | - | ||||||||||||
| 854 | \since 4.2 | - | ||||||||||||
| 855 | - | |||||||||||||
| 856 | If the string \a text is wider than \a width, returns an elided | - | ||||||||||||
| 857 | version of the string (i.e., a string with "..." in it). | - | ||||||||||||
| 858 | Otherwise, returns the original string. | - | ||||||||||||
| 859 | - | |||||||||||||
| 860 | The \a mode parameter specifies whether the text is elided on the | - | ||||||||||||
| 861 | left (e.g., "...tech"), in the middle (e.g., "Tr...ch"), or on | - | ||||||||||||
| 862 | the right (e.g., "Trol..."). | - | ||||||||||||
| 863 | - | |||||||||||||
| 864 | The \a width is specified in pixels, not characters. | - | ||||||||||||
| 865 | - | |||||||||||||
| 866 | The \a flags argument is optional and currently only supports | - | ||||||||||||
| 867 | Qt::TextShowMnemonic as value. | - | ||||||||||||
| 868 | - | |||||||||||||
| 869 | The elide mark follows the \l{Qt::LayoutDirection}{layoutdirection}. | - | ||||||||||||
| 870 | For example, it will be on the right side of the text for right-to-left | - | ||||||||||||
| 871 | layouts if the \a mode is \c{Qt::ElideLeft}, and on the left side of the | - | ||||||||||||
| 872 | text if the \a mode is \c{Qt::ElideRight}. | - | ||||||||||||
| 873 | - | |||||||||||||
| 874 | */ | - | ||||||||||||
| 875 | QString QFontMetrics::elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags) const | - | ||||||||||||
| 876 | { | - | ||||||||||||
| 877 | QString _text = text; | - | ||||||||||||
| 878 | if (!(flags & Qt::TextLongestVariant)) {
| 0 | ||||||||||||
| 879 | int posA = 0; | - | ||||||||||||
| 880 | int posB = _text.indexOf(QLatin1Char('\x9c')); | - | ||||||||||||
| 881 | while (posB >= 0) {
| 0 | ||||||||||||
| 882 | QString portion = _text.mid(posA, posB - posA); | - | ||||||||||||
| 883 | if (size(flags, portion).width() <= width)
| 0 | ||||||||||||
| 884 | return portion; never executed: return portion; | 0 | ||||||||||||
| 885 | posA = posB + 1; | - | ||||||||||||
| 886 | posB = _text.indexOf(QLatin1Char('\x9c'), posA); | - | ||||||||||||
| 887 | } never executed: end of block | 0 | ||||||||||||
| 888 | _text = _text.mid(posA); | - | ||||||||||||
| 889 | } never executed: end of block | 0 | ||||||||||||
| 890 | QStackTextEngine engine(_text, QFont(d.data())); | - | ||||||||||||
| 891 | return engine.elidedText(mode, width, flags); never executed: return engine.elidedText(mode, width, flags); | 0 | ||||||||||||
| 892 | } | - | ||||||||||||
| 893 | - | |||||||||||||
| 894 | /*! | - | ||||||||||||
| 895 | Returns the distance from the base line to where an underscore | - | ||||||||||||
| 896 | should be drawn. | - | ||||||||||||
| 897 | - | |||||||||||||
| 898 | \sa overlinePos(), strikeOutPos(), lineWidth() | - | ||||||||||||
| 899 | */ | - | ||||||||||||
| 900 | int QFontMetrics::underlinePos() const | - | ||||||||||||
| 901 | { | - | ||||||||||||
| 902 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 903 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 904 | return qRound(engine->underlinePosition()); never executed: return qRound(engine->underlinePosition()); | 0 | ||||||||||||
| 905 | } | - | ||||||||||||
| 906 | - | |||||||||||||
| 907 | /*! | - | ||||||||||||
| 908 | Returns the distance from the base line to where an overline | - | ||||||||||||
| 909 | should be drawn. | - | ||||||||||||
| 910 | - | |||||||||||||
| 911 | \sa underlinePos(), strikeOutPos(), lineWidth() | - | ||||||||||||
| 912 | */ | - | ||||||||||||
| 913 | int QFontMetrics::overlinePos() const | - | ||||||||||||
| 914 | { | - | ||||||||||||
| 915 | return ascent() + 1; never executed: return ascent() + 1; | 0 | ||||||||||||
| 916 | } | - | ||||||||||||
| 917 | - | |||||||||||||
| 918 | /*! | - | ||||||||||||
| 919 | Returns the distance from the base line to where the strikeout | - | ||||||||||||
| 920 | line should be drawn. | - | ||||||||||||
| 921 | - | |||||||||||||
| 922 | \sa underlinePos(), overlinePos(), lineWidth() | - | ||||||||||||
| 923 | */ | - | ||||||||||||
| 924 | int QFontMetrics::strikeOutPos() const | - | ||||||||||||
| 925 | { | - | ||||||||||||
| 926 | int pos = ascent() / 3; | - | ||||||||||||
| 927 | return pos > 0 ? pos : 1; never executed: return pos > 0 ? pos : 1; | 0 | ||||||||||||
| 928 | } | - | ||||||||||||
| 929 | - | |||||||||||||
| 930 | /*! | - | ||||||||||||
| 931 | Returns the width of the underline and strikeout lines, adjusted | - | ||||||||||||
| 932 | for the point size of the font. | - | ||||||||||||
| 933 | - | |||||||||||||
| 934 | \sa underlinePos(), overlinePos(), strikeOutPos() | - | ||||||||||||
| 935 | */ | - | ||||||||||||
| 936 | int QFontMetrics::lineWidth() const | - | ||||||||||||
| 937 | { | - | ||||||||||||
| 938 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 939 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 940 | return qRound(engine->lineThickness()); never executed: return qRound(engine->lineThickness()); | 0 | ||||||||||||
| 941 | } | - | ||||||||||||
| 942 | - | |||||||||||||
| 943 | - | |||||||||||||
| 944 | - | |||||||||||||
| 945 | - | |||||||||||||
| 946 | /***************************************************************************** | - | ||||||||||||
| 947 | QFontMetricsF member functions | - | ||||||||||||
| 948 | *****************************************************************************/ | - | ||||||||||||
| 949 | - | |||||||||||||
| 950 | /*! | - | ||||||||||||
| 951 | \class QFontMetricsF | - | ||||||||||||
| 952 | \reentrant | - | ||||||||||||
| 953 | \inmodule QtGui | - | ||||||||||||
| 954 | - | |||||||||||||
| 955 | \brief The QFontMetricsF class provides font metrics information. | - | ||||||||||||
| 956 | - | |||||||||||||
| 957 | \ingroup painting | - | ||||||||||||
| 958 | \ingroup shared | - | ||||||||||||
| 959 | - | |||||||||||||
| 960 | QFontMetricsF functions calculate the size of characters and | - | ||||||||||||
| 961 | strings for a given font. You can construct a QFontMetricsF object | - | ||||||||||||
| 962 | with an existing QFont to obtain metrics for that font. If the | - | ||||||||||||
| 963 | font is changed later, the font metrics object is \e not updated. | - | ||||||||||||
| 964 | - | |||||||||||||
| 965 | Once created, the object provides functions to access the | - | ||||||||||||
| 966 | individual metrics of the font, its characters, and for strings | - | ||||||||||||
| 967 | rendered in the font. | - | ||||||||||||
| 968 | - | |||||||||||||
| 969 | There are several functions that operate on the font: ascent(), | - | ||||||||||||
| 970 | descent(), height(), leading() and lineSpacing() return the basic | - | ||||||||||||
| 971 | size properties of the font. The underlinePos(), overlinePos(), | - | ||||||||||||
| 972 | strikeOutPos() and lineWidth() functions, return the properties of | - | ||||||||||||
| 973 | the line that underlines, overlines or strikes out the | - | ||||||||||||
| 974 | characters. These functions are all fast. | - | ||||||||||||
| 975 | - | |||||||||||||
| 976 | There are also some functions that operate on the set of glyphs in | - | ||||||||||||
| 977 | the font: minLeftBearing(), minRightBearing() and maxWidth(). | - | ||||||||||||
| 978 | These are by necessity slow, and we recommend avoiding them if | - | ||||||||||||
| 979 | possible. | - | ||||||||||||
| 980 | - | |||||||||||||
| 981 | For each character, you can get its width(), leftBearing() and | - | ||||||||||||
| 982 | rightBearing() and find out whether it is in the font using | - | ||||||||||||
| 983 | inFont(). You can also treat the character as a string, and use | - | ||||||||||||
| 984 | the string functions on it. | - | ||||||||||||
| 985 | - | |||||||||||||
| 986 | The string functions include width(), to return the width of a | - | ||||||||||||
| 987 | string in pixels (or points, for a printer), boundingRect(), to | - | ||||||||||||
| 988 | return a rectangle large enough to contain the rendered string, | - | ||||||||||||
| 989 | and size(), to return the size of that rectangle. | - | ||||||||||||
| 990 | - | |||||||||||||
| 991 | Example: | - | ||||||||||||
| 992 | \snippet code/src_gui_text_qfontmetrics.cpp 1 | - | ||||||||||||
| 993 | - | |||||||||||||
| 994 | \sa QFont, QFontInfo, QFontDatabase | - | ||||||||||||
| 995 | */ | - | ||||||||||||
| 996 | - | |||||||||||||
| 997 | /*! | - | ||||||||||||
| 998 | \since 4.2 | - | ||||||||||||
| 999 | - | |||||||||||||
| 1000 | Constructs a font metrics object with floating point precision | - | ||||||||||||
| 1001 | from the given \a fontMetrics object. | - | ||||||||||||
| 1002 | */ | - | ||||||||||||
| 1003 | QFontMetricsF::QFontMetricsF(const QFontMetrics &fontMetrics) | - | ||||||||||||
| 1004 | : d(fontMetrics.d) | - | ||||||||||||
| 1005 | { | - | ||||||||||||
| 1006 | } never executed: end of block | 0 | ||||||||||||
| 1007 | - | |||||||||||||
| 1008 | /*! | - | ||||||||||||
| 1009 | \since 4.2 | - | ||||||||||||
| 1010 | - | |||||||||||||
| 1011 | Assigns \a other to this object. | - | ||||||||||||
| 1012 | */ | - | ||||||||||||
| 1013 | QFontMetricsF &QFontMetricsF::operator=(const QFontMetrics &other) | - | ||||||||||||
| 1014 | { | - | ||||||||||||
| 1015 | d = other.d; | - | ||||||||||||
| 1016 | return *this; never executed: return *this; | 0 | ||||||||||||
| 1017 | } | - | ||||||||||||
| 1018 | - | |||||||||||||
| 1019 | /*! | - | ||||||||||||
| 1020 | \fn void QFontMetricsF::swap(QFontMetricsF &other) | - | ||||||||||||
| 1021 | \since 5.0 | - | ||||||||||||
| 1022 | - | |||||||||||||
| 1023 | Swaps this font metrics instance with \a other. This function is | - | ||||||||||||
| 1024 | very fast and never fails. | - | ||||||||||||
| 1025 | */ | - | ||||||||||||
| 1026 | - | |||||||||||||
| 1027 | - | |||||||||||||
| 1028 | - | |||||||||||||
| 1029 | /*! | - | ||||||||||||
| 1030 | Constructs a font metrics object for \a font. | - | ||||||||||||
| 1031 | - | |||||||||||||
| 1032 | The font metrics will be compatible with the paintdevice used to | - | ||||||||||||
| 1033 | create \a font. | - | ||||||||||||
| 1034 | - | |||||||||||||
| 1035 | The font metrics object holds the information for the font that is | - | ||||||||||||
| 1036 | passed in the constructor at the time it is created, and is not | - | ||||||||||||
| 1037 | updated if the font's attributes are changed later. | - | ||||||||||||
| 1038 | - | |||||||||||||
| 1039 | Use QFontMetricsF(const QFont &, QPaintDevice *) to get the font | - | ||||||||||||
| 1040 | metrics that are compatible with a certain paint device. | - | ||||||||||||
| 1041 | */ | - | ||||||||||||
| 1042 | QFontMetricsF::QFontMetricsF(const QFont &font) | - | ||||||||||||
| 1043 | : d(font.d) | - | ||||||||||||
| 1044 | { | - | ||||||||||||
| 1045 | } never executed: end of block | 0 | ||||||||||||
| 1046 | - | |||||||||||||
| 1047 | /*! | - | ||||||||||||
| 1048 | Constructs a font metrics object for \a font and \a paintdevice. | - | ||||||||||||
| 1049 | - | |||||||||||||
| 1050 | The font metrics will be compatible with the paintdevice passed. | - | ||||||||||||
| 1051 | If the \a paintdevice is 0, the metrics will be screen-compatible, | - | ||||||||||||
| 1052 | ie. the metrics you get if you use the font for drawing text on a | - | ||||||||||||
| 1053 | \l{QWidget}{widgets} or \l{QPixmap}{pixmaps}, | - | ||||||||||||
| 1054 | not on a QPicture or QPrinter. | - | ||||||||||||
| 1055 | - | |||||||||||||
| 1056 | The font metrics object holds the information for the font that is | - | ||||||||||||
| 1057 | passed in the constructor at the time it is created, and is not | - | ||||||||||||
| 1058 | updated if the font's attributes are changed later. | - | ||||||||||||
| 1059 | */ | - | ||||||||||||
| 1060 | QFontMetricsF::QFontMetricsF(const QFont &font, QPaintDevice *paintdevice) | - | ||||||||||||
| 1061 | { | - | ||||||||||||
| 1062 | int dpi = paintdevice ? paintdevice->logicalDpiY() : qt_defaultDpi();
| 0 | ||||||||||||
| 1063 | const int screen = 0; | - | ||||||||||||
| 1064 | if (font.d->dpi != dpi || font.d->screen != screen ) {
| 0 | ||||||||||||
| 1065 | d = new QFontPrivate(*font.d); | - | ||||||||||||
| 1066 | d->dpi = dpi; | - | ||||||||||||
| 1067 | d->screen = screen; | - | ||||||||||||
| 1068 | } else { never executed: end of block | 0 | ||||||||||||
| 1069 | d = font.d; | - | ||||||||||||
| 1070 | } never executed: end of block | 0 | ||||||||||||
| 1071 | - | |||||||||||||
| 1072 | } | - | ||||||||||||
| 1073 | - | |||||||||||||
| 1074 | /*! | - | ||||||||||||
| 1075 | Constructs a copy of \a fm. | - | ||||||||||||
| 1076 | */ | - | ||||||||||||
| 1077 | QFontMetricsF::QFontMetricsF(const QFontMetricsF &fm) | - | ||||||||||||
| 1078 | : d(fm.d) | - | ||||||||||||
| 1079 | { | - | ||||||||||||
| 1080 | } never executed: end of block | 0 | ||||||||||||
| 1081 | - | |||||||||||||
| 1082 | /*! | - | ||||||||||||
| 1083 | Destroys the font metrics object and frees all allocated | - | ||||||||||||
| 1084 | resources. | - | ||||||||||||
| 1085 | */ | - | ||||||||||||
| 1086 | QFontMetricsF::~QFontMetricsF() | - | ||||||||||||
| 1087 | { | - | ||||||||||||
| 1088 | } | - | ||||||||||||
| 1089 | - | |||||||||||||
| 1090 | /*! | - | ||||||||||||
| 1091 | Assigns the font metrics \a fm to this font metrics object. | - | ||||||||||||
| 1092 | */ | - | ||||||||||||
| 1093 | QFontMetricsF &QFontMetricsF::operator=(const QFontMetricsF &fm) | - | ||||||||||||
| 1094 | { | - | ||||||||||||
| 1095 | d = fm.d; | - | ||||||||||||
| 1096 | return *this; never executed: return *this; | 0 | ||||||||||||
| 1097 | } | - | ||||||||||||
| 1098 | - | |||||||||||||
| 1099 | /*! | - | ||||||||||||
| 1100 | Returns \c true if the font metrics are equal to the \a other font | - | ||||||||||||
| 1101 | metrics; otherwise returns \c false. | - | ||||||||||||
| 1102 | - | |||||||||||||
| 1103 | Two font metrics are considered equal if they were constructed from the | - | ||||||||||||
| 1104 | same QFont and the paint devices they were constructed for are | - | ||||||||||||
| 1105 | considered to be compatible. | - | ||||||||||||
| 1106 | */ | - | ||||||||||||
| 1107 | bool QFontMetricsF::operator ==(const QFontMetricsF &other) const | - | ||||||||||||
| 1108 | { | - | ||||||||||||
| 1109 | return d == other.d; never executed: return d == other.d; | 0 | ||||||||||||
| 1110 | } | - | ||||||||||||
| 1111 | - | |||||||||||||
| 1112 | /*! | - | ||||||||||||
| 1113 | \fn bool QFontMetricsF::operator !=(const QFontMetricsF &other) const | - | ||||||||||||
| 1114 | \overload | - | ||||||||||||
| 1115 | - | |||||||||||||
| 1116 | Returns \c true if the font metrics are not equal to the \a other font | - | ||||||||||||
| 1117 | metrics; otherwise returns \c false. | - | ||||||||||||
| 1118 | - | |||||||||||||
| 1119 | \sa operator==() | - | ||||||||||||
| 1120 | */ | - | ||||||||||||
| 1121 | - | |||||||||||||
| 1122 | /*! | - | ||||||||||||
| 1123 | Returns the ascent of the font. | - | ||||||||||||
| 1124 | - | |||||||||||||
| 1125 | The ascent of a font is the distance from the baseline to the | - | ||||||||||||
| 1126 | highest position characters extend to. In practice, some font | - | ||||||||||||
| 1127 | designers break this rule, e.g. when they put more than one accent | - | ||||||||||||
| 1128 | on top of a character, or to accommodate an unusual character in | - | ||||||||||||
| 1129 | an exotic language, so it is possible (though rare) that this | - | ||||||||||||
| 1130 | value will be too small. | - | ||||||||||||
| 1131 | - | |||||||||||||
| 1132 | \sa descent() | - | ||||||||||||
| 1133 | */ | - | ||||||||||||
| 1134 | qreal QFontMetricsF::ascent() const | - | ||||||||||||
| 1135 | { | - | ||||||||||||
| 1136 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 1137 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1138 | return engine->ascent().toReal(); never executed: return engine->ascent().toReal(); | 0 | ||||||||||||
| 1139 | } | - | ||||||||||||
| 1140 | - | |||||||||||||
| 1141 | - | |||||||||||||
| 1142 | /*! | - | ||||||||||||
| 1143 | Returns the descent of the font. | - | ||||||||||||
| 1144 | - | |||||||||||||
| 1145 | The descent is the distance from the base line to the lowest point | - | ||||||||||||
| 1146 | characters extend to. (Note that this is different from X, which | - | ||||||||||||
| 1147 | adds 1 pixel.) In practice, some font designers break this rule, | - | ||||||||||||
| 1148 | e.g. to accommodate an unusual character in an exotic language, so | - | ||||||||||||
| 1149 | it is possible (though rare) that this value will be too small. | - | ||||||||||||
| 1150 | - | |||||||||||||
| 1151 | \sa ascent() | - | ||||||||||||
| 1152 | */ | - | ||||||||||||
| 1153 | qreal QFontMetricsF::descent() const | - | ||||||||||||
| 1154 | { | - | ||||||||||||
| 1155 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 1156 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1157 | return engine->descent().toReal(); never executed: return engine->descent().toReal(); | 0 | ||||||||||||
| 1158 | } | - | ||||||||||||
| 1159 | - | |||||||||||||
| 1160 | /*! | - | ||||||||||||
| 1161 | Returns the height of the font. | - | ||||||||||||
| 1162 | - | |||||||||||||
| 1163 | This is always equal to ascent()+descent(). | - | ||||||||||||
| 1164 | - | |||||||||||||
| 1165 | \sa leading(), lineSpacing() | - | ||||||||||||
| 1166 | */ | - | ||||||||||||
| 1167 | qreal QFontMetricsF::height() const | - | ||||||||||||
| 1168 | { | - | ||||||||||||
| 1169 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 1170 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1171 | - | |||||||||||||
| 1172 | return (engine->ascent() + engine->descent()).toReal(); never executed: return (engine->ascent() + engine->descent()).toReal(); | 0 | ||||||||||||
| 1173 | } | - | ||||||||||||
| 1174 | - | |||||||||||||
| 1175 | /*! | - | ||||||||||||
| 1176 | Returns the leading of the font. | - | ||||||||||||
| 1177 | - | |||||||||||||
| 1178 | This is the natural inter-line spacing. | - | ||||||||||||
| 1179 | - | |||||||||||||
| 1180 | \sa height(), lineSpacing() | - | ||||||||||||
| 1181 | */ | - | ||||||||||||
| 1182 | qreal QFontMetricsF::leading() const | - | ||||||||||||
| 1183 | { | - | ||||||||||||
| 1184 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 1185 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1186 | return engine->leading().toReal(); never executed: return engine->leading().toReal(); | 0 | ||||||||||||
| 1187 | } | - | ||||||||||||
| 1188 | - | |||||||||||||
| 1189 | /*! | - | ||||||||||||
| 1190 | Returns the distance from one base line to the next. | - | ||||||||||||
| 1191 | - | |||||||||||||
| 1192 | This value is always equal to leading()+height(). | - | ||||||||||||
| 1193 | - | |||||||||||||
| 1194 | \sa height(), leading() | - | ||||||||||||
| 1195 | */ | - | ||||||||||||
| 1196 | qreal QFontMetricsF::lineSpacing() const | - | ||||||||||||
| 1197 | { | - | ||||||||||||
| 1198 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 1199 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1200 | return (engine->leading() + engine->ascent() + engine->descent()).toReal(); never executed: return (engine->leading() + engine->ascent() + engine->descent()).toReal(); | 0 | ||||||||||||
| 1201 | } | - | ||||||||||||
| 1202 | - | |||||||||||||
| 1203 | /*! | - | ||||||||||||
| 1204 | Returns the minimum left bearing of the font. | - | ||||||||||||
| 1205 | - | |||||||||||||
| 1206 | This is the smallest leftBearing(char) of all characters in the | - | ||||||||||||
| 1207 | font. | - | ||||||||||||
| 1208 | - | |||||||||||||
| 1209 | Note that this function can be very slow if the font is large. | - | ||||||||||||
| 1210 | - | |||||||||||||
| 1211 | \sa minRightBearing(), leftBearing() | - | ||||||||||||
| 1212 | */ | - | ||||||||||||
| 1213 | qreal QFontMetricsF::minLeftBearing() const | - | ||||||||||||
| 1214 | { | - | ||||||||||||
| 1215 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 1216 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1217 | return engine->minLeftBearing(); never executed: return engine->minLeftBearing(); | 0 | ||||||||||||
| 1218 | } | - | ||||||||||||
| 1219 | - | |||||||||||||
| 1220 | /*! | - | ||||||||||||
| 1221 | Returns the minimum right bearing of the font. | - | ||||||||||||
| 1222 | - | |||||||||||||
| 1223 | This is the smallest rightBearing(char) of all characters in the | - | ||||||||||||
| 1224 | font. | - | ||||||||||||
| 1225 | - | |||||||||||||
| 1226 | Note that this function can be very slow if the font is large. | - | ||||||||||||
| 1227 | - | |||||||||||||
| 1228 | \sa minLeftBearing(), rightBearing() | - | ||||||||||||
| 1229 | */ | - | ||||||||||||
| 1230 | qreal QFontMetricsF::minRightBearing() const | - | ||||||||||||
| 1231 | { | - | ||||||||||||
| 1232 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 1233 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1234 | return engine->minRightBearing(); never executed: return engine->minRightBearing(); | 0 | ||||||||||||
| 1235 | } | - | ||||||||||||
| 1236 | - | |||||||||||||
| 1237 | /*! | - | ||||||||||||
| 1238 | Returns the width of the widest character in the font. | - | ||||||||||||
| 1239 | */ | - | ||||||||||||
| 1240 | qreal QFontMetricsF::maxWidth() const | - | ||||||||||||
| 1241 | { | - | ||||||||||||
| 1242 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 1243 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1244 | return engine->maxCharWidth(); never executed: return engine->maxCharWidth(); | 0 | ||||||||||||
| 1245 | } | - | ||||||||||||
| 1246 | - | |||||||||||||
| 1247 | /*! | - | ||||||||||||
| 1248 | Returns the 'x' height of the font. This is often but not always | - | ||||||||||||
| 1249 | the same as the height of the character 'x'. | - | ||||||||||||
| 1250 | */ | - | ||||||||||||
| 1251 | qreal QFontMetricsF::xHeight() const | - | ||||||||||||
| 1252 | { | - | ||||||||||||
| 1253 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 1254 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1255 | if (d->capital == QFont::SmallCaps)
| 0 | ||||||||||||
| 1256 | return d->smallCapsFontPrivate()->engineForScript(QChar::Script_Common)->ascent().toReal(); never executed: return d->smallCapsFontPrivate()->engineForScript(QChar::Script_Common)->ascent().toReal(); | 0 | ||||||||||||
| 1257 | return engine->xHeight().toReal(); never executed: return engine->xHeight().toReal(); | 0 | ||||||||||||
| 1258 | } | - | ||||||||||||
| 1259 | - | |||||||||||||
| 1260 | /*! | - | ||||||||||||
| 1261 | \since 4.2 | - | ||||||||||||
| 1262 | - | |||||||||||||
| 1263 | Returns the average width of glyphs in the font. | - | ||||||||||||
| 1264 | */ | - | ||||||||||||
| 1265 | qreal QFontMetricsF::averageCharWidth() const | - | ||||||||||||
| 1266 | { | - | ||||||||||||
| 1267 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 1268 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1269 | return engine->averageCharWidth().toReal(); never executed: return engine->averageCharWidth().toReal(); | 0 | ||||||||||||
| 1270 | } | - | ||||||||||||
| 1271 | - | |||||||||||||
| 1272 | /*! | - | ||||||||||||
| 1273 | Returns \c true if character \a ch is a valid character in the font; | - | ||||||||||||
| 1274 | otherwise returns \c false. | - | ||||||||||||
| 1275 | */ | - | ||||||||||||
| 1276 | bool QFontMetricsF::inFont(QChar ch) const | - | ||||||||||||
| 1277 | { | - | ||||||||||||
| 1278 | return inFontUcs4(ch.unicode()); never executed: return inFontUcs4(ch.unicode()); | 0 | ||||||||||||
| 1279 | } | - | ||||||||||||
| 1280 | - | |||||||||||||
| 1281 | /*! | - | ||||||||||||
| 1282 | \fn bool QFontMetricsF::inFontUcs4(uint ch) const | - | ||||||||||||
| 1283 | - | |||||||||||||
| 1284 | Returns \c true if the character given by \a ch, encoded in UCS-4/UTF-32, | - | ||||||||||||
| 1285 | is a valid character in the font; otherwise returns \c false. | - | ||||||||||||
| 1286 | */ | - | ||||||||||||
| 1287 | bool QFontMetricsF::inFontUcs4(uint ucs4) const | - | ||||||||||||
| 1288 | { | - | ||||||||||||
| 1289 | const int script = QChar::script(ucs4); | - | ||||||||||||
| 1290 | QFontEngine *engine = d->engineForScript(script); | - | ||||||||||||
| 1291 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1292 | if (engine->type() == QFontEngine::Box)
| 0 | ||||||||||||
| 1293 | return false; never executed: return false; | 0 | ||||||||||||
| 1294 | return engine->canRender(ucs4); never executed: return engine->canRender(ucs4); | 0 | ||||||||||||
| 1295 | } | - | ||||||||||||
| 1296 | - | |||||||||||||
| 1297 | /*! | - | ||||||||||||
| 1298 | Returns the left bearing of character \a ch in the font. | - | ||||||||||||
| 1299 | - | |||||||||||||
| 1300 | The left bearing is the right-ward distance of the left-most pixel | - | ||||||||||||
| 1301 | of the character from the logical origin of the character. This | - | ||||||||||||
| 1302 | value is negative if the pixels of the character extend to the | - | ||||||||||||
| 1303 | left of the logical origin. | - | ||||||||||||
| 1304 | - | |||||||||||||
| 1305 | See width(QChar) for a graphical description of this metric. | - | ||||||||||||
| 1306 | - | |||||||||||||
| 1307 | \sa rightBearing(), minLeftBearing(), width() | - | ||||||||||||
| 1308 | */ | - | ||||||||||||
| 1309 | qreal QFontMetricsF::leftBearing(QChar ch) const | - | ||||||||||||
| 1310 | { | - | ||||||||||||
| 1311 | const int script = ch.script(); | - | ||||||||||||
| 1312 | QFontEngine *engine; | - | ||||||||||||
| 1313 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
| 1314 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
| 1315 | else | - | ||||||||||||
| 1316 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
| 1317 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1318 | if (engine->type() == QFontEngine::Box)
| 0 | ||||||||||||
| 1319 | return 0; never executed: return 0; | 0 | ||||||||||||
| 1320 | - | |||||||||||||
| 1321 | d->alterCharForCapitalization(ch); | - | ||||||||||||
| 1322 | - | |||||||||||||
| 1323 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
| 1324 | - | |||||||||||||
| 1325 | qreal lb; | - | ||||||||||||
| 1326 | engine->getGlyphBearings(glyph, &lb); | - | ||||||||||||
| 1327 | return lb; never executed: return lb; | 0 | ||||||||||||
| 1328 | } | - | ||||||||||||
| 1329 | - | |||||||||||||
| 1330 | /*! | - | ||||||||||||
| 1331 | Returns the right bearing of character \a ch in the font. | - | ||||||||||||
| 1332 | - | |||||||||||||
| 1333 | The right bearing is the left-ward distance of the right-most | - | ||||||||||||
| 1334 | pixel of the character from the logical origin of a subsequent | - | ||||||||||||
| 1335 | character. This value is negative if the pixels of the character | - | ||||||||||||
| 1336 | extend to the right of the width() of the character. | - | ||||||||||||
| 1337 | - | |||||||||||||
| 1338 | See width() for a graphical description of this metric. | - | ||||||||||||
| 1339 | - | |||||||||||||
| 1340 | \sa leftBearing(), minRightBearing(), width() | - | ||||||||||||
| 1341 | */ | - | ||||||||||||
| 1342 | qreal QFontMetricsF::rightBearing(QChar ch) const | - | ||||||||||||
| 1343 | { | - | ||||||||||||
| 1344 | const int script = ch.script(); | - | ||||||||||||
| 1345 | QFontEngine *engine; | - | ||||||||||||
| 1346 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
| 1347 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
| 1348 | else | - | ||||||||||||
| 1349 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
| 1350 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1351 | if (engine->type() == QFontEngine::Box)
| 0 | ||||||||||||
| 1352 | return 0; never executed: return 0; | 0 | ||||||||||||
| 1353 | - | |||||||||||||
| 1354 | d->alterCharForCapitalization(ch); | - | ||||||||||||
| 1355 | - | |||||||||||||
| 1356 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
| 1357 | - | |||||||||||||
| 1358 | qreal rb; | - | ||||||||||||
| 1359 | engine->getGlyphBearings(glyph, 0, &rb); | - | ||||||||||||
| 1360 | return rb; never executed: return rb; | 0 | ||||||||||||
| 1361 | - | |||||||||||||
| 1362 | } | - | ||||||||||||
| 1363 | - | |||||||||||||
| 1364 | /*! | - | ||||||||||||
| 1365 | Returns the width in pixels of the characters in the given \a text. | - | ||||||||||||
| 1366 | - | |||||||||||||
| 1367 | Note that this value is \e not equal to the width returned by | - | ||||||||||||
| 1368 | boundingRect().width() because boundingRect() returns a rectangle | - | ||||||||||||
| 1369 | describing the pixels this string will cover whereas width() | - | ||||||||||||
| 1370 | returns the distance to where the next string should be drawn. | - | ||||||||||||
| 1371 | - | |||||||||||||
| 1372 | \sa boundingRect() | - | ||||||||||||
| 1373 | */ | - | ||||||||||||
| 1374 | qreal QFontMetricsF::width(const QString &text) const | - | ||||||||||||
| 1375 | { | - | ||||||||||||
| 1376 | int pos = text.indexOf(QLatin1Char('\x9c')); | - | ||||||||||||
| 1377 | int len = (pos != -1) ? pos : text.length();
| 0 | ||||||||||||
| 1378 | - | |||||||||||||
| 1379 | QStackTextEngine layout(text, QFont(d.data())); | - | ||||||||||||
| 1380 | layout.ignoreBidi = true; | - | ||||||||||||
| 1381 | layout.itemize(); | - | ||||||||||||
| 1382 | return layout.width(0, len).toReal(); never executed: return layout.width(0, len).toReal(); | 0 | ||||||||||||
| 1383 | } | - | ||||||||||||
| 1384 | - | |||||||||||||
| 1385 | /*! | - | ||||||||||||
| 1386 | \overload | - | ||||||||||||
| 1387 | - | |||||||||||||
| 1388 | \image bearings.png Bearings | - | ||||||||||||
| 1389 | - | |||||||||||||
| 1390 | Returns the logical width of character \a ch in pixels. This is a | - | ||||||||||||
| 1391 | distance appropriate for drawing a subsequent character after \a | - | ||||||||||||
| 1392 | ch. | - | ||||||||||||
| 1393 | - | |||||||||||||
| 1394 | Some of the metrics are described in the image to the right. The | - | ||||||||||||
| 1395 | central dark rectangles cover the logical width() of each | - | ||||||||||||
| 1396 | character. The outer pale rectangles cover the leftBearing() and | - | ||||||||||||
| 1397 | rightBearing() of each character. Notice that the bearings of "f" | - | ||||||||||||
| 1398 | in this particular font are both negative, while the bearings of | - | ||||||||||||
| 1399 | "o" are both positive. | - | ||||||||||||
| 1400 | - | |||||||||||||
| 1401 | \warning This function will produce incorrect results for Arabic | - | ||||||||||||
| 1402 | characters or non-spacing marks in the middle of a string, as the | - | ||||||||||||
| 1403 | glyph shaping and positioning of marks that happens when | - | ||||||||||||
| 1404 | processing strings cannot be taken into account. When implementing | - | ||||||||||||
| 1405 | an interactive text control, use QTextLayout instead. | - | ||||||||||||
| 1406 | - | |||||||||||||
| 1407 | \sa boundingRect() | - | ||||||||||||
| 1408 | */ | - | ||||||||||||
| 1409 | qreal QFontMetricsF::width(QChar ch) const | - | ||||||||||||
| 1410 | { | - | ||||||||||||
| 1411 | if (ch.category() == QChar::Mark_NonSpacing)
| 0 | ||||||||||||
| 1412 | return 0.; never executed: return 0.; | 0 | ||||||||||||
| 1413 | - | |||||||||||||
| 1414 | const int script = ch.script(); | - | ||||||||||||
| 1415 | QFontEngine *engine; | - | ||||||||||||
| 1416 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
| 1417 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
| 1418 | else | - | ||||||||||||
| 1419 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
| 1420 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1421 | - | |||||||||||||
| 1422 | d->alterCharForCapitalization(ch); | - | ||||||||||||
| 1423 | - | |||||||||||||
| 1424 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
| 1425 | QFixed advance; | - | ||||||||||||
| 1426 | - | |||||||||||||
| 1427 | QGlyphLayout glyphs; | - | ||||||||||||
| 1428 | glyphs.numGlyphs = 1; | - | ||||||||||||
| 1429 | glyphs.glyphs = &glyph; | - | ||||||||||||
| 1430 | glyphs.advances = &advance; | - | ||||||||||||
| 1431 | engine->recalcAdvances(&glyphs, 0); | - | ||||||||||||
| 1432 | - | |||||||||||||
| 1433 | return advance.toReal(); never executed: return advance.toReal(); | 0 | ||||||||||||
| 1434 | } | - | ||||||||||||
| 1435 | - | |||||||||||||
| 1436 | /*! | - | ||||||||||||
| 1437 | Returns the bounding rectangle of the characters in the string | - | ||||||||||||
| 1438 | specified by \a text. The bounding rectangle always covers at least | - | ||||||||||||
| 1439 | the set of pixels the text would cover if drawn at (0, 0). | - | ||||||||||||
| 1440 | - | |||||||||||||
| 1441 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
| 1442 | e.g. for italicized fonts, and that the width of the returned | - | ||||||||||||
| 1443 | rectangle might be different than what the width() method returns. | - | ||||||||||||
| 1444 | - | |||||||||||||
| 1445 | If you want to know the advance width of the string (to lay out | - | ||||||||||||
| 1446 | a set of strings next to each other), use width() instead. | - | ||||||||||||
| 1447 | - | |||||||||||||
| 1448 | Newline characters are processed as normal characters, \e not as | - | ||||||||||||
| 1449 | linebreaks. | - | ||||||||||||
| 1450 | - | |||||||||||||
| 1451 | The height of the bounding rectangle is at least as large as the | - | ||||||||||||
| 1452 | value returned height(). | - | ||||||||||||
| 1453 | - | |||||||||||||
| 1454 | \sa width(), height(), QPainter::boundingRect() | - | ||||||||||||
| 1455 | */ | - | ||||||||||||
| 1456 | QRectF QFontMetricsF::boundingRect(const QString &text) const | - | ||||||||||||
| 1457 | { | - | ||||||||||||
| 1458 | int len = text.length(); | - | ||||||||||||
| 1459 | if (len == 0)
| 0 | ||||||||||||
| 1460 | return QRectF(); never executed: return QRectF(); | 0 | ||||||||||||
| 1461 | - | |||||||||||||
| 1462 | QStackTextEngine layout(text, QFont(d.data())); | - | ||||||||||||
| 1463 | layout.ignoreBidi = true; | - | ||||||||||||
| 1464 | layout.itemize(); | - | ||||||||||||
| 1465 | glyph_metrics_t gm = layout.boundingBox(0, len); | - | ||||||||||||
| 1466 | return QRectF(gm.x.toReal(), gm.y.toReal(), never executed: return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal()); | 0 | ||||||||||||
| 1467 | gm.width.toReal(), gm.height.toReal()); never executed: return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal()); | 0 | ||||||||||||
| 1468 | } | - | ||||||||||||
| 1469 | - | |||||||||||||
| 1470 | /*! | - | ||||||||||||
| 1471 | Returns the bounding rectangle of the character \a ch relative to | - | ||||||||||||
| 1472 | the left-most point on the base line. | - | ||||||||||||
| 1473 | - | |||||||||||||
| 1474 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
| 1475 | e.g. for italicized fonts, and that the text output may cover \e | - | ||||||||||||
| 1476 | all pixels in the bounding rectangle. | - | ||||||||||||
| 1477 | - | |||||||||||||
| 1478 | Note that the rectangle usually extends both above and below the | - | ||||||||||||
| 1479 | base line. | - | ||||||||||||
| 1480 | - | |||||||||||||
| 1481 | \sa width() | - | ||||||||||||
| 1482 | */ | - | ||||||||||||
| 1483 | QRectF QFontMetricsF::boundingRect(QChar ch) const | - | ||||||||||||
| 1484 | { | - | ||||||||||||
| 1485 | const int script = ch.script(); | - | ||||||||||||
| 1486 | QFontEngine *engine; | - | ||||||||||||
| 1487 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
| 1488 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
| 1489 | else | - | ||||||||||||
| 1490 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
| 1491 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1492 | - | |||||||||||||
| 1493 | d->alterCharForCapitalization(ch); | - | ||||||||||||
| 1494 | - | |||||||||||||
| 1495 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
| 1496 | - | |||||||||||||
| 1497 | glyph_metrics_t gm = engine->boundingBox(glyph); | - | ||||||||||||
| 1498 | return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal()); never executed: return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal()); | 0 | ||||||||||||
| 1499 | } | - | ||||||||||||
| 1500 | - | |||||||||||||
| 1501 | /*! | - | ||||||||||||
| 1502 | \overload | - | ||||||||||||
| 1503 | - | |||||||||||||
| 1504 | Returns the bounding rectangle of the characters in the given \a text. | - | ||||||||||||
| 1505 | This is the set of pixels the text would cover if drawn when constrained | - | ||||||||||||
| 1506 | to the bounding rectangle specified by \a rect. | - | ||||||||||||
| 1507 | - | |||||||||||||
| 1508 | The \a flags argument is the bitwise OR of the following flags: | - | ||||||||||||
| 1509 | \list | - | ||||||||||||
| 1510 | \li Qt::AlignLeft aligns to the left border, except for | - | ||||||||||||
| 1511 | Arabic and Hebrew where it aligns to the right. | - | ||||||||||||
| 1512 | \li Qt::AlignRight aligns to the right border, except for | - | ||||||||||||
| 1513 | Arabic and Hebrew where it aligns to the left. | - | ||||||||||||
| 1514 | \li Qt::AlignJustify produces justified text. | - | ||||||||||||
| 1515 | \li Qt::AlignHCenter aligns horizontally centered. | - | ||||||||||||
| 1516 | \li Qt::AlignTop aligns to the top border. | - | ||||||||||||
| 1517 | \li Qt::AlignBottom aligns to the bottom border. | - | ||||||||||||
| 1518 | \li Qt::AlignVCenter aligns vertically centered | - | ||||||||||||
| 1519 | \li Qt::AlignCenter (== \c{Qt::AlignHCenter | Qt::AlignVCenter}) | - | ||||||||||||
| 1520 | \li Qt::TextSingleLine ignores newline characters in the text. | - | ||||||||||||
| 1521 | \li Qt::TextExpandTabs expands tabs (see below) | - | ||||||||||||
| 1522 | \li Qt::TextShowMnemonic interprets "&x" as \underline{x}; i.e., underlined. | - | ||||||||||||
| 1523 | \li Qt::TextWordWrap breaks the text to fit the rectangle. | - | ||||||||||||
| 1524 | \endlist | - | ||||||||||||
| 1525 | - | |||||||||||||
| 1526 | Qt::Horizontal alignment defaults to Qt::AlignLeft and vertical | - | ||||||||||||
| 1527 | alignment defaults to Qt::AlignTop. | - | ||||||||||||
| 1528 | - | |||||||||||||
| 1529 | If several of the horizontal or several of the vertical alignment | - | ||||||||||||
| 1530 | flags are set, the resulting alignment is undefined. | - | ||||||||||||
| 1531 | - | |||||||||||||
| 1532 | These flags are defined in \l{Qt::AlignmentFlag}. | - | ||||||||||||
| 1533 | - | |||||||||||||
| 1534 | If Qt::TextExpandTabs is set in \a flags, the following behavior is | - | ||||||||||||
| 1535 | used to interpret tab characters in the text: | - | ||||||||||||
| 1536 | \list | - | ||||||||||||
| 1537 | \li If \a tabArray is non-null, it specifies a 0-terminated sequence of | - | ||||||||||||
| 1538 | pixel-positions for tabs in the text. | - | ||||||||||||
| 1539 | \li If \a tabStops is non-zero, it is used as the tab spacing (in pixels). | - | ||||||||||||
| 1540 | \endlist | - | ||||||||||||
| 1541 | - | |||||||||||||
| 1542 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
| 1543 | e.g. for italicized fonts. | - | ||||||||||||
| 1544 | - | |||||||||||||
| 1545 | Newline characters are processed as line breaks. | - | ||||||||||||
| 1546 | - | |||||||||||||
| 1547 | Despite the different actual character heights, the heights of the | - | ||||||||||||
| 1548 | bounding rectangles of "Yes" and "yes" are the same. | - | ||||||||||||
| 1549 | - | |||||||||||||
| 1550 | The bounding rectangle returned by this function is somewhat larger | - | ||||||||||||
| 1551 | than that calculated by the simpler boundingRect() function. This | - | ||||||||||||
| 1552 | function uses the \l{minLeftBearing()}{maximum left} and | - | ||||||||||||
| 1553 | \l{minRightBearing()}{right} font bearings as is | - | ||||||||||||
| 1554 | necessary for multi-line text to align correctly. Also, | - | ||||||||||||
| 1555 | fontHeight() and lineSpacing() are used to calculate the height, | - | ||||||||||||
| 1556 | rather than individual character heights. | - | ||||||||||||
| 1557 | - | |||||||||||||
| 1558 | \sa width(), QPainter::boundingRect(), Qt::Alignment | - | ||||||||||||
| 1559 | */ | - | ||||||||||||
| 1560 | QRectF QFontMetricsF::boundingRect(const QRectF &rect, int flags, const QString& text, | - | ||||||||||||
| 1561 | int tabStops, int *tabArray) const | - | ||||||||||||
| 1562 | { | - | ||||||||||||
| 1563 | int tabArrayLen = 0; | - | ||||||||||||
| 1564 | if (tabArray)
| 0 | ||||||||||||
| 1565 | while (tabArray[tabArrayLen])
| 0 | ||||||||||||
| 1566 | tabArrayLen++; never executed: tabArrayLen++; | 0 | ||||||||||||
| 1567 | - | |||||||||||||
| 1568 | QRectF rb; | - | ||||||||||||
| 1569 | qt_format_text(QFont(d.data()), rect, flags | Qt::TextDontPrint, text, &rb, tabStops, tabArray, | - | ||||||||||||
| 1570 | tabArrayLen, 0); | - | ||||||||||||
| 1571 | return rb; never executed: return rb; | 0 | ||||||||||||
| 1572 | } | - | ||||||||||||
| 1573 | - | |||||||||||||
| 1574 | /*! | - | ||||||||||||
| 1575 | Returns the size in pixels of the characters in the given \a text. | - | ||||||||||||
| 1576 | - | |||||||||||||
| 1577 | The \a flags argument is the bitwise OR of the following flags: | - | ||||||||||||
| 1578 | \list | - | ||||||||||||
| 1579 | \li Qt::TextSingleLine ignores newline characters. | - | ||||||||||||
| 1580 | \li Qt::TextExpandTabs expands tabs (see below) | - | ||||||||||||
| 1581 | \li Qt::TextShowMnemonic interprets "&x" as \underline{x}; i.e., underlined. | - | ||||||||||||
| 1582 | \li Qt::TextWordWrap breaks the text to fit the rectangle. | - | ||||||||||||
| 1583 | \endlist | - | ||||||||||||
| 1584 | - | |||||||||||||
| 1585 | These flags are defined in the \l{Qt::TextFlag} enum. | - | ||||||||||||
| 1586 | - | |||||||||||||
| 1587 | If Qt::TextExpandTabs is set in \a flags, the following behavior is | - | ||||||||||||
| 1588 | used to interpret tab characters in the text: | - | ||||||||||||
| 1589 | \list | - | ||||||||||||
| 1590 | \li If \a tabArray is non-null, it specifies a 0-terminated sequence of | - | ||||||||||||
| 1591 | pixel-positions for tabs in the text. | - | ||||||||||||
| 1592 | \li If \a tabStops is non-zero, it is used as the tab spacing (in pixels). | - | ||||||||||||
| 1593 | \endlist | - | ||||||||||||
| 1594 | - | |||||||||||||
| 1595 | Newline characters are processed as line breaks. | - | ||||||||||||
| 1596 | - | |||||||||||||
| 1597 | Note: Despite the different actual character heights, the heights of the | - | ||||||||||||
| 1598 | bounding rectangles of "Yes" and "yes" are the same. | - | ||||||||||||
| 1599 | - | |||||||||||||
| 1600 | \sa boundingRect() | - | ||||||||||||
| 1601 | */ | - | ||||||||||||
| 1602 | QSizeF QFontMetricsF::size(int flags, const QString &text, int tabStops, int *tabArray) const | - | ||||||||||||
| 1603 | { | - | ||||||||||||
| 1604 | return boundingRect(QRectF(), flags | Qt::TextLongestVariant, text, tabStops, tabArray).size(); never executed: return boundingRect(QRectF(), flags | Qt::TextLongestVariant, text, tabStops, tabArray).size(); | 0 | ||||||||||||
| 1605 | } | - | ||||||||||||
| 1606 | - | |||||||||||||
| 1607 | /*! | - | ||||||||||||
| 1608 | \since 4.3 | - | ||||||||||||
| 1609 | - | |||||||||||||
| 1610 | Returns a tight bounding rectangle around the characters in the | - | ||||||||||||
| 1611 | string specified by \a text. The bounding rectangle always covers | - | ||||||||||||
| 1612 | at least the set of pixels the text would cover if drawn at (0, | - | ||||||||||||
| 1613 | 0). | - | ||||||||||||
| 1614 | - | |||||||||||||
| 1615 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
| 1616 | e.g. for italicized fonts, and that the width of the returned | - | ||||||||||||
| 1617 | rectangle might be different than what the width() method returns. | - | ||||||||||||
| 1618 | - | |||||||||||||
| 1619 | If you want to know the advance width of the string (to lay out | - | ||||||||||||
| 1620 | a set of strings next to each other), use width() instead. | - | ||||||||||||
| 1621 | - | |||||||||||||
| 1622 | Newline characters are processed as normal characters, \e not as | - | ||||||||||||
| 1623 | linebreaks. | - | ||||||||||||
| 1624 | - | |||||||||||||
| 1625 | \warning Calling this method is very slow on Windows. | - | ||||||||||||
| 1626 | - | |||||||||||||
| 1627 | \sa width(), height(), boundingRect() | - | ||||||||||||
| 1628 | */ | - | ||||||||||||
| 1629 | QRectF QFontMetricsF::tightBoundingRect(const QString &text) const | - | ||||||||||||
| 1630 | { | - | ||||||||||||
| 1631 | if (text.length() == 0)
| 0 | ||||||||||||
| 1632 | return QRect(); never executed: return QRect(); | 0 | ||||||||||||
| 1633 | - | |||||||||||||
| 1634 | QStackTextEngine layout(text, QFont(d.data())); | - | ||||||||||||
| 1635 | layout.ignoreBidi = true; | - | ||||||||||||
| 1636 | layout.itemize(); | - | ||||||||||||
| 1637 | glyph_metrics_t gm = layout.tightBoundingBox(0, text.length()); | - | ||||||||||||
| 1638 | return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal()); never executed: return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal()); | 0 | ||||||||||||
| 1639 | } | - | ||||||||||||
| 1640 | - | |||||||||||||
| 1641 | /*! | - | ||||||||||||
| 1642 | \since 4.2 | - | ||||||||||||
| 1643 | - | |||||||||||||
| 1644 | If the string \a text is wider than \a width, returns an elided | - | ||||||||||||
| 1645 | version of the string (i.e., a string with "..." in it). | - | ||||||||||||
| 1646 | Otherwise, returns the original string. | - | ||||||||||||
| 1647 | - | |||||||||||||
| 1648 | The \a mode parameter specifies whether the text is elided on the | - | ||||||||||||
| 1649 | left (for example, "...tech"), in the middle (for example, "Tr...ch"), or | - | ||||||||||||
| 1650 | on the right (for example, "Trol..."). | - | ||||||||||||
| 1651 | - | |||||||||||||
| 1652 | The \a width is specified in pixels, not characters. | - | ||||||||||||
| 1653 | - | |||||||||||||
| 1654 | The \a flags argument is optional and currently only supports | - | ||||||||||||
| 1655 | Qt::TextShowMnemonic as value. | - | ||||||||||||
| 1656 | - | |||||||||||||
| 1657 | The elide mark follows the \l{Qt::LayoutDirection}{layoutdirection}. | - | ||||||||||||
| 1658 | For example, it will be on the right side of the text for right-to-left | - | ||||||||||||
| 1659 | layouts if the \a mode is \c{Qt::ElideLeft}, and on the left side of the | - | ||||||||||||
| 1660 | text if the \a mode is \c{Qt::ElideRight}. | - | ||||||||||||
| 1661 | */ | - | ||||||||||||
| 1662 | QString QFontMetricsF::elidedText(const QString &text, Qt::TextElideMode mode, qreal width, int flags) const | - | ||||||||||||
| 1663 | { | - | ||||||||||||
| 1664 | QString _text = text; | - | ||||||||||||
| 1665 | if (!(flags & Qt::TextLongestVariant)) {
| 0 | ||||||||||||
| 1666 | int posA = 0; | - | ||||||||||||
| 1667 | int posB = _text.indexOf(QLatin1Char('\x9c')); | - | ||||||||||||
| 1668 | while (posB >= 0) {
| 0 | ||||||||||||
| 1669 | QString portion = _text.mid(posA, posB - posA); | - | ||||||||||||
| 1670 | if (size(flags, portion).width() <= width)
| 0 | ||||||||||||
| 1671 | return portion; never executed: return portion; | 0 | ||||||||||||
| 1672 | posA = posB + 1; | - | ||||||||||||
| 1673 | posB = _text.indexOf(QLatin1Char('\x9c'), posA); | - | ||||||||||||
| 1674 | } never executed: end of block | 0 | ||||||||||||
| 1675 | _text = _text.mid(posA); | - | ||||||||||||
| 1676 | } never executed: end of block | 0 | ||||||||||||
| 1677 | QStackTextEngine engine(_text, QFont(d.data())); | - | ||||||||||||
| 1678 | return engine.elidedText(mode, QFixed::fromReal(width), flags); never executed: return engine.elidedText(mode, QFixed::fromReal(width), flags); | 0 | ||||||||||||
| 1679 | } | - | ||||||||||||
| 1680 | - | |||||||||||||
| 1681 | /*! | - | ||||||||||||
| 1682 | Returns the distance from the base line to where an underscore | - | ||||||||||||
| 1683 | should be drawn. | - | ||||||||||||
| 1684 | - | |||||||||||||
| 1685 | \sa overlinePos(), strikeOutPos(), lineWidth() | - | ||||||||||||
| 1686 | */ | - | ||||||||||||
| 1687 | qreal QFontMetricsF::underlinePos() const | - | ||||||||||||
| 1688 | { | - | ||||||||||||
| 1689 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 1690 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1691 | return engine->underlinePosition().toReal(); never executed: return engine->underlinePosition().toReal(); | 0 | ||||||||||||
| 1692 | } | - | ||||||||||||
| 1693 | - | |||||||||||||
| 1694 | /*! | - | ||||||||||||
| 1695 | Returns the distance from the base line to where an overline | - | ||||||||||||
| 1696 | should be drawn. | - | ||||||||||||
| 1697 | - | |||||||||||||
| 1698 | \sa underlinePos(), strikeOutPos(), lineWidth() | - | ||||||||||||
| 1699 | */ | - | ||||||||||||
| 1700 | qreal QFontMetricsF::overlinePos() const | - | ||||||||||||
| 1701 | { | - | ||||||||||||
| 1702 | return ascent() + 1; never executed: return ascent() + 1; | 0 | ||||||||||||
| 1703 | } | - | ||||||||||||
| 1704 | - | |||||||||||||
| 1705 | /*! | - | ||||||||||||
| 1706 | Returns the distance from the base line to where the strikeout | - | ||||||||||||
| 1707 | line should be drawn. | - | ||||||||||||
| 1708 | - | |||||||||||||
| 1709 | \sa underlinePos(), overlinePos(), lineWidth() | - | ||||||||||||
| 1710 | */ | - | ||||||||||||
| 1711 | qreal QFontMetricsF::strikeOutPos() const | - | ||||||||||||
| 1712 | { | - | ||||||||||||
| 1713 | return ascent() / 3.; never executed: return ascent() / 3.; | 0 | ||||||||||||
| 1714 | } | - | ||||||||||||
| 1715 | - | |||||||||||||
| 1716 | /*! | - | ||||||||||||
| 1717 | Returns the width of the underline and strikeout lines, adjusted | - | ||||||||||||
| 1718 | for the point size of the font. | - | ||||||||||||
| 1719 | - | |||||||||||||
| 1720 | \sa underlinePos(), overlinePos(), strikeOutPos() | - | ||||||||||||
| 1721 | */ | - | ||||||||||||
| 1722 | qreal QFontMetricsF::lineWidth() const | - | ||||||||||||
| 1723 | { | - | ||||||||||||
| 1724 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
| 1725 | Q_ASSERT(engine != 0); | - | ||||||||||||
| 1726 | return engine->lineThickness().toReal(); never executed: return engine->lineThickness().toReal(); | 0 | ||||||||||||
| 1727 | } | - | ||||||||||||
| 1728 | - | |||||||||||||
| 1729 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |