Qt 4.8
Public Functions | Properties | Friends | List of all members
QFontMetrics Class Reference

The QFontMetrics class provides font metrics information. More...

#include <qfontmetrics.h>

Public Functions

int ascent () const
 Returns the ascent of the font. More...
 
int averageCharWidth () const
 Returns the average width of glyphs in the font. More...
 
QRect boundingRect (QChar) const
 Returns the rectangle that is covered by ink if character ch were to be drawn at the origin of the coordinate system. More...
 
QRect boundingRect (const QString &text) const
 Returns the bounding rectangle of the characters in the string specified by text. More...
 
QRect boundingRect (const QRect &r, int flags, const QString &text, int tabstops=0, int *tabarray=0) const
 Returns the bounding rectangle of the characters in the string specified by text, which is the set of pixels the text would cover if drawn at (0, 0). More...
 
QRect boundingRect (int x, int y, int w, int h, int flags, const QString &text, int tabstops=0, int *tabarray=0) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns the bounding rectangle for the given text within the rectangle specified by the x and y coordinates, width, and height. More...
 
int charWidth (const QString &str, int pos) const
 Returns the width of the character at position pos in the string text. More...
 
int descent () const
 Returns the descent of the font. More...
 
QString elidedText (const QString &text, Qt::TextElideMode mode, int width, int flags=0) const
 If the string text is wider than width, returns an elided version of the string (i. More...
 
int height () const
 Returns the height of the font. More...
 
bool inFont (QChar) const
 Returns true if character ch is a valid character in the font; otherwise returns false. More...
 
bool inFontUcs4 (uint ucs4) const
 Returns true if the given character encoded in UCS-4/UTF-32 is a valid character in the font; otherwise returns false. More...
 
int leading () const
 Returns the leading of the font. More...
 
int leftBearing (QChar) const
 Returns the left bearing of character ch in the font. More...
 
int lineSpacing () const
 Returns the distance from one base line to the next. More...
 
int lineWidth () const
 Returns the width of the underline and strikeout lines, adjusted for the point size of the font. More...
 
int maxWidth () const
 Returns the width of the widest character in the font. More...
 
int minLeftBearing () const
 Returns the minimum left bearing of the font. More...
 
int minRightBearing () const
 Returns the minimum right bearing of the font. More...
 
bool operator!= (const QFontMetrics &other)
 Returns true if other is not equal to this object; otherwise returns false. More...
 
bool operator!= (const QFontMetrics &other) const
 Returns true if other is not equal to this object; otherwise returns false. More...
 
QFontMetricsoperator= (const QFontMetrics &)
 Assigns the font metrics fm. More...
 
bool operator== (const QFontMetrics &other)
 Returns true if other is equal to this object; otherwise returns false. More...
 
bool operator== (const QFontMetrics &other) const
 Returns true if other is equal to this object; otherwise returns false. More...
 
int overlinePos () const
 Returns the distance from the base line to where an overline should be drawn. More...
 
 QFontMetrics (const QFont &)
 Constructs a font metrics object for font. More...
 
 QFontMetrics (const QFont &, QPaintDevice *pd)
 Constructs a font metrics object for font and paintdevice. More...
 
 QFontMetrics (const QFontMetrics &)
 Constructs a copy of fm. More...
 
int rightBearing (QChar) const
 Returns the right bearing of character ch in the font. More...
 
QSize size (int flags, const QString &str, int tabstops=0, int *tabarray=0) const
 Returns the size in pixels of text. More...
 
int strikeOutPos () const
 Returns the distance from the base line to where the strikeout line should be drawn. More...
 
QRect tightBoundingRect (const QString &text) const
 Returns a tight bounding rectangle around the characters in the string specified by text. More...
 
int underlinePos () const
 Returns the distance from the base line to where an underscore should be drawn. More...
 
int width (const QString &, int len=-1) const
 Returns the width in pixels of the first len characters of text. More...
 
int width (const QString &, int len, int flags) const
 
int width (QChar) const
 Returns the logical width of character ch in pixels. More...
 
int xHeight () const
 Returns the 'x' height of the font. More...
 
 ~QFontMetrics ()
 Destroys the font metrics object and frees all allocated resources. More...
 

Properties

QExplicitlySharedDataPointer< QFontPrivated
 

Friends

class QFontMetricsF
 
class QFontPrivate
 
class QStackTextEngine
 

Detailed Description

The QFontMetrics class provides font metrics information.

Note
This class or function is reentrant.

QFontMetrics functions calculate the size of characters and strings for a given font. There are three ways you can create a QFontMetrics object:

  1. Calling the QFontMetrics constructor with a QFont creates a font metrics object for a screen-compatible font, i.e. the font cannot be a printer font. If the font is changed later, the font metrics object is not updated.

    (Note: If you use a printer font the values returned may be inaccurate. Printer fonts are not always accessible so the nearest screen font is used if a printer font is supplied.)

  2. QWidget::fontMetrics() returns the font metrics for a widget's font. This is equivalent to QFontMetrics(widget->font()). If the widget's font is changed later, the font metrics object is not updated.

  3. QPainter::fontMetrics() returns the font metrics for a painter's current font. If the painter's font is changed later, the font metrics object is not updated.

Once created, the object provides functions to access the individual metrics of the font, its characters, and for strings rendered in the font.

There are several functions that operate on the font: ascent(), descent(), height(), leading() and lineSpacing() return the basic size properties of the font. The underlinePos(), overlinePos(), strikeOutPos() and lineWidth() functions, return the properties of the line that underlines, overlines or strikes out the characters. These functions are all fast.

There are also some functions that operate on the set of glyphs in the font: minLeftBearing(), minRightBearing() and maxWidth(). These are by necessity slow, and we recommend avoiding them if possible.

For each character, you can get its width(), leftBearing() and rightBearing() and find out whether it is in the font using inFont(). You can also treat the character as a string, and use the string functions on it.

The string functions include width(), to return the width of a string in pixels (or points, for a printer), boundingRect(), to return a rectangle large enough to contain the rendered string, and size(), to return the size of that rectangle.

Example:

QFont font("times", 24);
QFontMetrics fm(font);
int pixelsWide = fm.width("What's the width of this text?");
int pixelsHigh = fm.height();
See also
QFont, QFontInfo, QFontDatabase, QFontComboBox, {Character Map Example}

Definition at line 65 of file qfontmetrics.h.

Constructors and Destructors

◆ QFontMetrics() [1/3]

QFontMetrics::QFontMetrics ( const QFont font)

Constructs a font metrics object for font.

The font metrics will be compatible with the paintdevice used to create font.

The font metrics object holds the information for the font that is passed in the constructor at the time it is created, and is not updated if the font's attributes are changed later.

Use QFontMetrics(const QFont &, QPaintDevice *) to get the font metrics that are compatible with a certain paint device.

Definition at line 167 of file qfontmetrics.cpp.

168  : d(font.d.data())
169 {
170 }
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfont.h:343
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ QFontMetrics() [2/3]

QFontMetrics::QFontMetrics ( const QFont font,
QPaintDevice paintdevice 
)

Constructs a font metrics object for font and paintdevice.

The font metrics will be compatible with the paintdevice passed. If the paintdevice is 0, the metrics will be screen-compatible, ie. the metrics you get if you use the font for drawing text on a widgets or pixmaps, not on a QPicture or QPrinter.

The font metrics object holds the information for the font that is passed in the constructor at the time it is created, and is not updated if the font's attributes are changed later.

Definition at line 185 of file qfontmetrics.cpp.

186 {
187  int dpi = paintdevice ? paintdevice->logicalDpiY() : qt_defaultDpi();
188 #ifdef Q_WS_X11
189  const QX11Info *info = qt_x11Info(paintdevice);
190  int screen = info ? info->screen() : 0;
191 #else
192  const int screen = 0;
193 #endif
194  if (font.d->dpi != dpi || font.d->screen != screen ) {
195  d = new QFontPrivate(*font.d);
196  d->dpi = dpi;
197  d->screen = screen;
198  } else {
199  d = font.d.data();
200  }
201 
202 }
static mach_timebase_info_data_t info
int logicalDpiY() const
Definition: qpaintdevice.h:96
friend class QFontPrivate
Definition: qfontmetrics.h:136
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
The QX11Info class provides information about the X display configuration.
Definition: qx11info_x11.h:63
Q_GUI_EXPORT int qt_defaultDpi()
Definition: qfont.cpp:240
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfont.h:343
int screen
Definition: qfont_p.h:181
const QX11Info * qt_x11Info(const QPaintDevice *pd)
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141
int screen() const
Returns the number of the screen currently in use.

◆ QFontMetrics() [3/3]

QFontMetrics::QFontMetrics ( const QFontMetrics fm)

Constructs a copy of fm.

Definition at line 207 of file qfontmetrics.cpp.

208  : d(fm.d.data())
209 {
210 }
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ ~QFontMetrics()

QFontMetrics::~QFontMetrics ( )

Destroys the font metrics object and frees all allocated resources.

Definition at line 216 of file qfontmetrics.cpp.

217 {
218 }

Functions

◆ ascent()

int QFontMetrics::ascent ( ) const

Returns the ascent of the font.

The ascent of a font is the distance from the baseline to the highest position characters extend to. In practice, some font designers break this rule, e.g. when they put more than one accent on top of a character, or to accommodate an unusual character in an exotic language, so it is possible (though rare) that this value will be too small.

See also
descent()

Definition at line 305 of file qfontmetrics.cpp.

Referenced by QDragManager::drag(), QDeclarativeTextInput::drawContents(), QGtkStyle::drawControl(), QMacStyle::drawControl(), QPicture::exec(), overlinePos(), QLineEdit::paintEvent(), QPainterReplayer::process(), QTipLabel::reuseTip(), strikeOutPos(), QDeclarativeTextPrivate::updateSize(), and QDeclarativeTextEdit::updateSize().

306 {
308  Q_ASSERT(engine != 0);
309  return qRound(engine->ascent());
310 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual QFixed ascent() const =0
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ averageCharWidth()

int QFontMetrics::averageCharWidth ( ) const

Returns the average width of glyphs in the font.

Since
4.2

Definition at line 438 of file qfontmetrics.cpp.

Referenced by QAccessibleTextWidget::characterRect(), and QLabelPrivate::sizeForWidth().

439 {
441  Q_ASSERT(engine != 0);
442  return qRound(engine->averageCharWidth());
443 }
virtual QFixed averageCharWidth() const
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ boundingRect() [1/4]

QRect QFontMetrics::boundingRect ( QChar  ch) const

Returns the rectangle that is covered by ink if character ch were to be drawn at the origin of the coordinate system.

Note that the bounding rectangle may extend to the left of (0, 0) (e.g., for italicized fonts), and that the text output may cover all pixels in the bounding rectangle. For a space character the rectangle will usually be empty.

Note that the rectangle usually extends both above and below the base line.

Warning
The width of the returned rectangle is not the advance width of the character. Use boundingRect(const QString &) or width() instead.
See also
width()

Definition at line 753 of file qfontmetrics.cpp.

Referenced by QVistaHelper::drawTitleBar(), QStyle::itemTextRect(), QCalendarWidget::minimumSizeHint(), QMacStylePrivate::pushButtonSizeFromContents(), QWhatsThat::QWhatsThat(), QComboBoxPrivate::recomputeSizeHint(), size(), QLabelPrivate::sizeForWidth(), QCleanlooksStyle::subControlRect(), QGtkStyle::subControlRect(), and QMenuPrivate::updateActionRects().

754 {
755  const int script = QUnicodeTables::script(ch);
756  QFontEngine *engine;
757  if (d->capital == QFont::SmallCaps && ch.isLower())
758  engine = d->smallCapsFontPrivate()->engineForScript(script);
759  else
760  engine = d->engineForScript(script);
761  Q_ASSERT(engine != 0);
762 
764 
765  QGlyphLayoutArray<10> glyphs;
766  int nglyphs = 9;
767  engine->stringToCMap(&ch, 1, &glyphs, &nglyphs, 0);
768  glyph_metrics_t gm = engine->boundingBox(glyphs.glyphs[0]);
769  return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height));
770 }
HB_Glyph * glyphs
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const =0
QFontPrivate * smallCapsFontPrivate() const
Definition: qfont.cpp:323
bool isLower() const
Returns true if the character is a lowercase letter, i.
Definition: qchar.h:272
uint capital
Definition: qfont_p.h:192
virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs)=0
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
void alterCharForCapitalization(QChar &c) const
Definition: qfont.cpp:309
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ boundingRect() [2/4]

QRect QFontMetrics::boundingRect ( const QString text) const

Returns the bounding rectangle of the characters in the string specified by text.

The bounding rectangle always covers at least the set of pixels the text would cover if drawn at (0, 0).

Note that the bounding rectangle may extend to the left of (0, 0), e.g. for italicized fonts, and that the width of the returned rectangle might be different than what the width() method returns.

If you want to know the advance width of the string (to layout a set of strings next to each other), use width() instead.

Newline characters are processed as normal characters, not as linebreaks.

The height of the bounding rectangle is at least as large as the value returned by height().

See also
width(), height(), QPainter::boundingRect(), tightBoundingRect()

Definition at line 724 of file qfontmetrics.cpp.

725 {
726  if (text.length() == 0)
727  return QRect();
728 
729  QStackTextEngine layout(text, d.data());
730  layout.ignoreBidi = true;
731  layout.itemize();
732  glyph_metrics_t gm = layout.boundingBox(0, text.length());
733  return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height));
734 }
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
const char * layout
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ boundingRect() [3/4]

QRect QFontMetrics::boundingRect ( const QRect rect,
int  flags,
const QString text,
int  tabStops = 0,
int *  tabArray = 0 
) const

Returns the bounding rectangle of the characters in the string specified by text, which is the set of pixels the text would cover if drawn at (0, 0).

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

The drawing, and hence the bounding rectangle, is constrained to the rectangle rect.

The flags argument is the bitwise OR of the following flags:

Qt::Horizontal alignment defaults to Qt::AlignLeft and vertical alignment defaults to Qt::AlignTop.

If several of the horizontal or several of the vertical alignment flags are set, the resulting alignment is undefined.

If Qt::TextExpandTabs is set in flags, then: if tabArray is non-null, it specifies a 0-terminated sequence of pixel-positions for tabs; otherwise if tabStops is non-zero, it is used as the tab spacing (in pixels).

Note that the bounding rectangle may extend to the left of (0, 0), e.g. for italicized fonts, and that the text output may cover all pixels in the bounding rectangle.

Newline characters are processed as linebreaks.

Despite the different actual character heights, the heights of the bounding rectangles of "Yes" and "yes" are the same.

The bounding rectangle returned by this function is somewhat larger than that calculated by the simpler boundingRect() function. This function uses the maximum left and right font bearings as is necessary for multi-line text to align correctly. Also, fontHeight() and lineSpacing() are used to calculate the height, rather than individual character heights.

See also
width(), QPainter::boundingRect(), Qt::Alignment

Definition at line 831 of file qfontmetrics.cpp.

833 {
834  int tabArrayLen = 0;
835  if (tabArray)
836  while (tabArray[tabArrayLen])
837  tabArrayLen++;
838 
839  QRectF rb;
840  QRectF rr(rect);
841  qt_format_text(QFont(d.data()), rr, flags | Qt::TextDontPrint, text, &rb, tabStops, tabArray,
842  tabArrayLen, 0);
843 
844  return rb.toAlignedRect();
845 }
QRect toAlignedRect() const
Returns a QRect based on the values of this rectangle that is the smallest possible integer rectangle...
Definition: qrect.cpp:2817
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
void qt_format_text(const QFont &font, const QRectF &_r, int tf, const QString &text, QRectF *brect, int tabStops, int *tabArray, int tabArrayLen, QPainter *painter)
Definition: qpainter.cpp:8448
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ boundingRect() [4/4]

QRect QFontMetrics::boundingRect ( int  x,
int  y,
int  width,
int  height,
int  flags,
const QString text,
int  tabStops = 0,
int *  tabArray = 0 
) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns the bounding rectangle for the given text within the rectangle specified by the x and y coordinates, width, and height.

If Qt::TextExpandTabs is set in flags and tabArray is non-null, it specifies a 0-terminated sequence of pixel-positions for tabs; otherwise, if tabStops is non-zero, it is used as the tab spacing (in pixels).

Definition at line 106 of file qfontmetrics.h.

108  { return boundingRect(QRect(x, y, w, h), flags, text, tabstops, tabarray); }
QRect boundingRect(QChar) const
Returns the rectangle that is covered by ink if character ch were to be drawn at the origin of the co...
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58

◆ charWidth()

int QFontMetrics::charWidth ( const QString text,
int  pos 
) const

Returns the width of the character at position pos in the string text.

The whole string is needed, as the glyph drawn may change depending on the context (the letter before and after the current one) for some languages (e.g. Arabic).

This function also takes non spacing marks and ligatures into account.

Definition at line 666 of file qfontmetrics.cpp.

667 {
668  if (pos < 0 || pos > (int)text.length())
669  return 0;
670 
671  QChar ch = text.unicode()[pos];
672  const int script = QUnicodeTables::script(ch);
673  int width;
674 
675  if (script != QUnicodeTables::Common) {
676  // complex script shaping. Have to do some hard work
677  int from = qMax(0, pos - 8);
678  int to = qMin(text.length(), pos + 8);
679  QString cstr = QString::fromRawData(text.unicode() + from, to - from);
680  QStackTextEngine layout(cstr, d.data());
681  layout.ignoreBidi = true;
682  layout.itemize();
683  width = qRound(layout.width(pos-from, 1));
684  } else if (QChar::category(ch.unicode()) == QChar::Mark_NonSpacing) {
685  width = 0;
686  } else {
687  QFontEngine *engine;
688  if (d->capital == QFont::SmallCaps && ch.isLower())
689  engine = d->smallCapsFontPrivate()->engineForScript(script);
690  else
691  engine = d->engineForScript(script);
692  Q_ASSERT(engine != 0);
693 
695 
696  QGlyphLayoutArray<8> glyphs;
697  int nglyphs = 7;
698  engine->stringToCMap(&ch, 1, &glyphs, &nglyphs, 0);
699  width = qRound(glyphs.advances_x[0]);
700  }
701  return width;
702 }
int width(const QString &, int len=-1) const
Returns the width in pixels of the first len characters of text.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
QFixed * advances_x
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const =0
QFontPrivate * smallCapsFontPrivate() const
Definition: qfont.cpp:323
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
static QString fromRawData(const QChar *, int size)
Constructs a QString that uses the first size Unicode characters in the array unicode.
Definition: qstring.cpp:7673
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
Category category() const
Returns the character&#39;s category.
Definition: qchar.cpp:853
bool isLower() const
Returns true if the character is a lowercase letter, i.
Definition: qchar.h:272
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
const QChar * unicode() const
Returns a &#39;\0&#39;-terminated Unicode representation of the string.
Definition: qstring.h:706
uint capital
Definition: qfont_p.h:192
const char * layout
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
void alterCharForCapitalization(QChar &c) const
Definition: qfont.cpp:309
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ descent()

int QFontMetrics::descent ( ) const

Returns the descent of the font.

The descent is the distance from the base line to the lowest point characters extend to. In practice, some font designers break this rule, e.g. to accommodate an unusual character in an exotic language, so it is possible (though rare) that this value will be too small.

See also
ascent()

Definition at line 323 of file qfontmetrics.cpp.

Referenced by QCommonStyle::drawControl(), QWindowsVistaStyle::drawControl(), QGtkStyle::drawControl(), and QTipLabel::reuseTip().

324 {
326  Q_ASSERT(engine != 0);
327  return qRound(engine->descent());
328 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual QFixed descent() const =0
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ elidedText()

QString QFontMetrics::elidedText ( const QString text,
Qt::TextElideMode  mode,
int  width,
int  flags = 0 
) const

If the string text is wider than width, returns an elided version of the string (i.

Since
4.2

e., a string with "..." in it). Otherwise, returns the original string.

The mode parameter specifies whether the text is elided on the left (e.g., "...tech"), in the middle (e.g., "Tr...ch"), or on the right (e.g., "Trol...").

The width is specified in pixels, not characters.

The flags argument is optional and currently only supports Qt::TextShowMnemonic as value.

The elide mark will follow the layout direction; it will be on the right side of the text for right-to-left layouts, and on the left side for right-to-left layouts. Note that this behavior is independent of the text language.

Definition at line 937 of file qfontmetrics.cpp.

Referenced by QCleanlooksStyle::drawComplexControl(), QWindowsVistaStyle::drawControl(), QCleanlooksStyle::drawControl(), QWindowsXPStyle::drawControl(), QGtkStyle::drawControl(), QMacStyle::drawControl(), QItemDelegate::drawDisplay(), QAbstractItemDelegate::elidedText(), QGraphicsWidgetPrivate::initStyleOptionTitleBar(), QLineEdit::paintEvent(), QWorkspaceTitleBar::paintEvent(), QFileDialogComboBox::paintEvent(), QHeaderView::paintSection(), QMdiSubWindowPrivate::titleBarOptions(), and QDeclarativeTextPrivate::updateLayout().

938 {
939  QString _text = text;
940  if (!(flags & Qt::TextLongestVariant)) {
941  int posA = 0;
942  int posB = _text.indexOf(QLatin1Char('\x9c'));
943  while (posB >= 0) {
944  QString portion = _text.mid(posA, posB - posA);
945  if (size(flags, portion).width() <= width)
946  return portion;
947  posA = posB + 1;
948  posB = _text.indexOf(QLatin1Char('\x9c'), posA);
949  }
950  _text = _text.mid(posA);
951  }
952  QStackTextEngine engine(_text, QFont(d.data()));
953  return engine.elidedText(mode, width, flags);
954 }
int width(const QString &, int len=-1) const
Returns the width in pixels of the first len characters of text.
The QString class provides a Unicode character string.
Definition: qstring.h:83
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
QSize size(int flags, const QString &str, int tabstops=0, int *tabarray=0) const
Returns the size in pixels of text.
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141
#define text
Definition: qobjectdefs.h:80

◆ height()

int QFontMetrics::height ( ) const

Returns the height of the font.

This is always equal to ascent()+descent()+1 (the 1 is for the base line).

See also
leading(), lineSpacing()

Definition at line 338 of file qfontmetrics.cpp.

Referenced by QAccessibleLineEdit::characterRect(), QCommandLinkButtonPrivate::descriptionOffset(), QItemDelegate::doLayout(), QDragManager::drag(), QWindowsStyle::drawComplexControl(), QMotifStyle::drawComplexControl(), QMacStyle::drawComplexControl(), QMacStyle::drawControl(), QTextDocumentLayoutPrivate::drawListItem(), QStatusBar::event(), QPlainTextEdit::event(), QScriptEdit::extraAreaPaintEvent(), QDeclarativeTextEdit::loadCursorDelegate(), QGroupBox::minimumSizeHint(), QLineEdit::minimumSizeHint(), QLineEdit::paintEvent(), QCommonStyle::pixelMetric(), QPlastiqueStyle::pixelMetric(), QStyleSheetStyle::pixelMetric(), qt_aqua_get_known_size(), QMdi::SimpleCascader::rearrange(), QStatusBar::reformat(), QDecorationWindows::region(), QHeaderView::setDefaultSectionSize(), QDeclarativeTextInput::setFont(), QDeclarativeTextEdit::setFont(), QScrollArea::sizeHint(), QProgressBar::sizeHint(), QFontFamilyDelegate::sizeHint(), QLineEdit::sizeHint(), QCommonStyle::subControlRect(), QWindowsMobileStyle::subControlRect(), QWindowsCEStyle::subControlRect(), QCleanlooksStyle::subControlRect(), QStyleSheetStyle::subControlRect(), QTabBar::tabSizeHint(), QStyleSheetStyle::titleBarLayout(), QDockWidgetLayout::titleHeight(), QCommandLinkButtonPrivate::titleRect(), QMenuPrivate::updateActionRects(), QDeclarativeTextPrivate::updateSize(), QDeclarativeTextEdit::updateSize(), and QCommonStylePrivate::viewItemLayout().

339 {
341  Q_ASSERT(engine != 0);
342  return qRound(engine->ascent()) + qRound(engine->descent()) + 1;
343 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual QFixed ascent() const =0
virtual QFixed descent() const =0
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ inFont()

bool QFontMetrics::inFont ( QChar  ch) const

Returns true if character ch is a valid character in the font; otherwise returns false.

Definition at line 449 of file qfontmetrics.cpp.

Referenced by QCommonStyle::styleHint(), and QWindowsStyle::styleHint().

450 {
451  const int script = QUnicodeTables::script(ch);
452  QFontEngine *engine = d->engineForScript(script);
453  Q_ASSERT(engine != 0);
454  if (engine->type() == QFontEngine::Box)
455  return false;
456  return engine->canRender(&ch, 1);
457 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual Type type() const =0
virtual bool canRender(const QChar *string, int len)=0
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ inFontUcs4()

bool QFontMetrics::inFontUcs4 ( uint  character) const

Returns true if the given character encoded in UCS-4/UTF-32 is a valid character in the font; otherwise returns false.

Since
4.8

Definition at line 469 of file qfontmetrics.cpp.

470 {
471  const int script = QUnicodeTables::script(ucs4);
472  QFontEngine *engine = d->engineForScript(script);
473  Q_ASSERT(engine != 0);
474  if (engine->type() == QFontEngine::Box)
475  return false;
476  QString utf16 = QString::fromUcs4(&ucs4, 1);
477  return engine->canRender(utf16.data(), utf16.length());
478 }
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual Type type() const =0
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
virtual bool canRender(const QChar *string, int len)=0
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141
static QString fromUcs4(const uint *, int size=-1)
Returns a QString initialized with the first size characters of the Unicode string unicode (ISO-10646...
Definition: qstring.cpp:4356

◆ leading()

int QFontMetrics::leading ( ) const

Returns the leading of the font.

This is the natural inter-line spacing.

See also
height(), lineSpacing()

Definition at line 352 of file qfontmetrics.cpp.

Referenced by QLineEdit::minimumSizeHint().

353 {
355  Q_ASSERT(engine != 0);
356  return qRound(engine->leading());
357 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141
virtual QFixed leading() const =0

◆ leftBearing()

int QFontMetrics::leftBearing ( QChar  ch) const

Returns the left bearing of character ch in the font.

The left bearing is the right-ward distance of the left-most pixel of the character from the logical origin of the character. This value is negative if the pixels of the character extend to the left of the logical origin.

See width(QChar) for a graphical description of this metric.

See also
rightBearing(), minLeftBearing(), width()

Definition at line 492 of file qfontmetrics.cpp.

493 {
494  const int script = QUnicodeTables::script(ch);
495  QFontEngine *engine;
496  if (d->capital == QFont::SmallCaps && ch.isLower())
497  engine = d->smallCapsFontPrivate()->engineForScript(script);
498  else
499  engine = d->engineForScript(script);
500  Q_ASSERT(engine != 0);
501  if (engine->type() == QFontEngine::Box)
502  return 0;
503 
505 
506  QGlyphLayoutArray<10> glyphs;
507  int nglyphs = 9;
508  engine->stringToCMap(&ch, 1, &glyphs, &nglyphs, 0);
509  // ### can nglyphs != 1 happen at all? Not currently I think
510  qreal lb;
511  engine->getGlyphBearings(glyphs.glyphs[0], &lb);
512  return qRound(lb);
513 }
double qreal
Definition: qglobal.h:1193
HB_Glyph * glyphs
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const =0
QFontPrivate * smallCapsFontPrivate() const
Definition: qfont.cpp:323
virtual Type type() const =0
bool isLower() const
Returns true if the character is a lowercase letter, i.
Definition: qchar.h:272
virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing=0, qreal *rightBearing=0)
uint capital
Definition: qfont_p.h:192
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
void alterCharForCapitalization(QChar &c) const
Definition: qfont.cpp:309
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ lineSpacing()

int QFontMetrics::lineSpacing ( ) const

Returns the distance from one base line to the next.

This value is always equal to leading()+height().

See also
height(), leading()

Definition at line 366 of file qfontmetrics.cpp.

Referenced by QTextDocumentLayoutPrivate::drawListItem(), QScriptEdit::extraAreaMouseEvent(), QScriptEdit::extraAreaPaintEvent(), QScriptEdit::extraAreaWidth(), and QLabelPrivate::sizeForWidth().

367 {
369  Q_ASSERT(engine != 0);
370  return qRound(engine->leading()) + qRound(engine->ascent()) + qRound(engine->descent()) + 1;
371 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual QFixed ascent() const =0
virtual QFixed descent() const =0
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141
virtual QFixed leading() const =0

◆ lineWidth()

int QFontMetrics::lineWidth ( ) const

Returns the width of the underline and strikeout lines, adjusted for the point size of the font.

See also
underlinePos(), overlinePos(), strikeOutPos()

Definition at line 998 of file qfontmetrics.cpp.

999 {
1001  Q_ASSERT(engine != 0);
1002  return qRound(engine->lineThickness());
1003 }
virtual QFixed lineThickness() const
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ maxWidth()

int QFontMetrics::maxWidth ( ) const

Returns the width of the widest character in the font.

Definition at line 410 of file qfontmetrics.cpp.

Referenced by QLineEdit::minimumSizeHint(), and QHeaderView::setDefaultSectionSize().

411 {
413  Q_ASSERT(engine != 0);
414  return qRound(engine->maxCharWidth());
415 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual qreal maxCharWidth() const =0
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ minLeftBearing()

int QFontMetrics::minLeftBearing ( ) const

Returns the minimum left bearing of the font.

This is the smallest leftBearing(char) of all characters in the font.

Note that this function can be very slow if the font is large.

See also
minRightBearing(), leftBearing()

Definition at line 383 of file qfontmetrics.cpp.

Referenced by QLineEdit::paintEvent().

384 {
386  Q_ASSERT(engine != 0);
387  return qRound(engine->minLeftBearing());
388 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual qreal minLeftBearing() const
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ minRightBearing()

int QFontMetrics::minRightBearing ( ) const

Returns the minimum right bearing of the font.

This is the smallest rightBearing(char) of all characters in the font.

Note that this function can be very slow if the font is large.

See also
minLeftBearing(), rightBearing()

Definition at line 400 of file qfontmetrics.cpp.

Referenced by QLineEdit::paintEvent().

401 {
403  Q_ASSERT(engine != 0);
404  return qRound(engine->minRightBearing());
405 }
virtual qreal minRightBearing() const
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ operator!=() [1/2]

bool QFontMetrics::operator!= ( const QFontMetrics other)
inline

Returns true if other is not equal to this object; otherwise returns false.

Two font metrics are considered equal if they were constructed from the same QFont and the paint devices they were constructed for are considered compatible.

See also
operator==()

Definition at line 122 of file qfontmetrics.h.

122 { return !operator==(other); } // 5.0 - remove me
bool operator==(const QFontMetrics &other)
Returns true if other is equal to this object; otherwise returns false.

◆ operator!=() [2/2]

bool QFontMetrics::operator!= ( const QFontMetrics other) const
inline

Returns true if other is not equal to this object; otherwise returns false.

Two font metrics are considered equal if they were constructed from the same QFont and the paint devices they were constructed for are considered compatible.

See also
operator==()

Definition at line 123 of file qfontmetrics.h.

123 { return !operator==(other); }
bool operator==(const QFontMetrics &other)
Returns true if other is equal to this object; otherwise returns false.

◆ operator=()

QFontMetrics & QFontMetrics::operator= ( const QFontMetrics fm)

Assigns the font metrics fm.

Definition at line 223 of file qfontmetrics.cpp.

224 {
225  d = fm.d.data();
226  return *this;
227 }
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ operator==() [1/2]

bool QFontMetrics::operator== ( const QFontMetrics other)

Returns true if other is equal to this object; otherwise returns false.

Two font metrics are considered equal if they were constructed from the same QFont and the paint devices they were constructed for are considered compatible.

See also
operator!=()

Definition at line 258 of file qfontmetrics.cpp.

259 {
260  return d == other.d;
261 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ operator==() [2/2]

bool QFontMetrics::operator== ( const QFontMetrics other) const

Returns true if other is equal to this object; otherwise returns false.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Two font metrics are considered equal if they were constructed from the same QFont and the paint devices they were constructed for are considered compatible.

See also
operator!=()

Definition at line 243 of file qfontmetrics.cpp.

244 {
245  return d == other.d;
246 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ overlinePos()

int QFontMetrics::overlinePos ( ) const

Returns the distance from the base line to where an overline should be drawn.

See also
underlinePos(), strikeOutPos(), lineWidth()

Definition at line 975 of file qfontmetrics.cpp.

976 {
977  return ascent() + 1;
978 }
int ascent() const
Returns the ascent of the font.

◆ rightBearing()

int QFontMetrics::rightBearing ( QChar  ch) const

Returns the right bearing of character ch in the font.

The right bearing is the left-ward distance of the right-most pixel of the character from the logical origin of a subsequent character. This value is negative if the pixels of the character extend to the right of the width() of the character.

See width() for a graphical description of this metric.

See also
leftBearing(), minRightBearing(), width()

Definition at line 527 of file qfontmetrics.cpp.

528 {
529  const int script = QUnicodeTables::script(ch);
530  QFontEngine *engine;
531  if (d->capital == QFont::SmallCaps && ch.isLower())
532  engine = d->smallCapsFontPrivate()->engineForScript(script);
533  else
534  engine = d->engineForScript(script);
535  Q_ASSERT(engine != 0);
536  if (engine->type() == QFontEngine::Box)
537  return 0;
538 
540 
541  QGlyphLayoutArray<10> glyphs;
542  int nglyphs = 9;
543  engine->stringToCMap(&ch, 1, &glyphs, &nglyphs, 0);
544  // ### can nglyphs != 1 happen at all? Not currently I think
545  qreal rb;
546  engine->getGlyphBearings(glyphs.glyphs[0], 0, &rb);
547  return qRound(rb);
548 }
double qreal
Definition: qglobal.h:1193
HB_Glyph * glyphs
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const =0
QFontPrivate * smallCapsFontPrivate() const
Definition: qfont.cpp:323
virtual Type type() const =0
bool isLower() const
Returns true if the character is a lowercase letter, i.
Definition: qchar.h:272
virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing=0, qreal *rightBearing=0)
uint capital
Definition: qfont_p.h:192
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
void alterCharForCapitalization(QChar &c) const
Definition: qfont.cpp:309
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ size()

QSize QFontMetrics::size ( int  flags,
const QString text,
int  tabStops = 0,
int *  tabArray = 0 
) const

Returns the size in pixels of text.

Use the size() function in combination with QString::left() instead.

The flags argument is the bitwise OR of the following flags:

If Qt::TextExpandTabs is set in flags, then: if tabArray is non-null, it specifies a 0-terminated sequence of pixel-positions for tabs; otherwise if tabStops is non-zero, it is used as the tab spacing (in pixels).

Newline characters are processed as linebreaks.

Despite the different actual character heights, the heights of the bounding rectangles of "Yes" and "yes" are the same.

See also
boundingRect()

QSize size = size(flags, str, len, tabstops, tabarray); QSize size = size(flags, str.left(len), tabstops, tabarray);

Definition at line 870 of file qfontmetrics.cpp.

Referenced by QMenuBarPrivate::calcActionRects(), elidedText(), QMacStyle::sizeFromContents(), QPushButton::sizeHint(), QToolBoxButton::sizeHint(), QToolButton::sizeHint(), DetailButton::sizeHint(), QCommonStyle::subControlRect(), QWindowsMobileStyle::subControlRect(), QWindowsCEStyle::subControlRect(), and QTabBar::tabSizeHint().

871 {
872  return boundingRect(QRect(0,0,0,0), flags | Qt::TextLongestVariant, text, tabStops, tabArray).size();
873 }
QRect boundingRect(QChar) const
Returns the rectangle that is covered by ink if character ch were to be drawn at the origin of the co...
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58

◆ strikeOutPos()

int QFontMetrics::strikeOutPos ( ) const

Returns the distance from the base line to where the strikeout line should be drawn.

See also
underlinePos(), overlinePos(), lineWidth()

Definition at line 986 of file qfontmetrics.cpp.

987 {
988  int pos = ascent() / 3;
989  return pos > 0 ? pos : 1;
990 }
int ascent() const
Returns the ascent of the font.

◆ tightBoundingRect()

QRect QFontMetrics::tightBoundingRect ( const QString text) const

Returns a tight bounding rectangle around the characters in the string specified by text.

Since
4.3

The bounding rectangle always covers at least the set of pixels the text would cover if drawn at (0, 0).

Note that the bounding rectangle may extend to the left of (0, 0), e.g. for italicized fonts, and that the width of the returned rectangle might be different than what the width() method returns.

If you want to know the advance width of the string (to layout a set of strings next to each other), use width() instead.

Newline characters are processed as normal characters, not as linebreaks.

Warning
Calling this method is very slow on Windows.
See also
width(), height(), boundingRect()

Definition at line 900 of file qfontmetrics.cpp.

901 {
902  if (text.length() == 0)
903  return QRect();
904 
905  QStackTextEngine layout(text, d.data());
906  layout.ignoreBidi = true;
907  layout.itemize();
908  glyph_metrics_t gm = layout.tightBoundingBox(0, text.length());
909  return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height));
910 }
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
const char * layout
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ underlinePos()

int QFontMetrics::underlinePos ( ) const

Returns the distance from the base line to where an underscore should be drawn.

See also
overlinePos(), strikeOutPos(), lineWidth()

Definition at line 962 of file qfontmetrics.cpp.

963 {
965  Q_ASSERT(engine != 0);
966  return qRound(engine->underlinePosition());
967 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
virtual QFixed underlinePosition() const
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ width() [1/3]

int QFontMetrics::width ( const QString text,
int  len = -1 
) const

Returns the width in pixels of the first len characters of text.

If len is negative (the default), the entire string is used.

Note that this value is not equal to boundingRect().width(); boundingRect() returns a rectangle describing the pixels this string will cover whereas width() returns the distance to where the next string should be drawn.

See also
boundingRect()

Definition at line 562 of file qfontmetrics.cpp.

Referenced by QTextDocument::adjustSize(), QAccessibleLineEdit::characterRect(), charWidth(), QComboBoxPrivate::computeWidthHint(), QDragManager::drag(), QTextLine::draw(), QPlastiqueStyle::drawComplexControl(), QMacStyle::drawComplexControl(), QPlastiqueStyle::drawControl(), QTextDocumentLayoutPrivate::drawListItem(), elidedText(), elliditide(), QScriptEdit::extraAreaWidth(), QPlainTextDocumentLayout::layoutBlock(), QGroupBox::minimumSizeHint(), QAbstractSpinBox::minimumSizeHint(), QFontFamilyDelegate::paint(), printPage(), qt_aqua_get_known_size(), qt_mac_get_fixed_pitch(), QComboBoxPrivate::recomputeSizeHint(), QExpandingLineEdit::resizeToContents(), QLabelPrivate::sizeForWidth(), QCleanlooksStyle::sizeFromContents(), QWindowsStyle::sizeFromContents(), QToolButton::sizeHint(), QFontComboBox::sizeHint(), QProgressBar::sizeHint(), QFontFamilyDelegate::sizeHint(), QAbstractSpinBox::sizeHint(), QLineEdit::sizeHint(), QDateTimeEdit::sizeHint(), QStyleSheetStyle::subControlRect(), QStyleSheetStyle::titleBarLayout(), QMessageBoxPrivate::updateSize(), and width().

563 {
564  return width(text, len, 0);
565 }
int width(const QString &, int len=-1) const
Returns the width in pixels of the first len characters of text.

◆ width() [2/3]

int QFontMetrics::width ( const QString text,
int  len,
int  flags 
) const
Warning
This function is not part of the public interface.

Definition at line 570 of file qfontmetrics.cpp.

571 {
572  int pos = text.indexOf(QLatin1Char('\x9c'));
573  if (pos != -1) {
574  len = (len < 0) ? pos : qMin(pos, len);
575  } else if (len < 0) {
576  len = text.length();
577  }
578  if (len == 0)
579  return 0;
580 
581  if (flags & Qt::TextBypassShaping) {
582  // Skip harfbuzz complex shaping, only use advances
583  int numGlyphs = len;
584  QVarLengthGlyphLayoutArray glyphs(numGlyphs);
586  if (!engine->stringToCMap(text.data(), len, &glyphs, &numGlyphs, 0)) {
587  glyphs.resize(numGlyphs);
588  if (!engine->stringToCMap(text.data(), len, &glyphs, &numGlyphs, 0))
589  Q_ASSERT_X(false, Q_FUNC_INFO, "stringToCMap shouldn't fail twice");
590  }
591 
592  QFixed width;
593  for (int i = 0; i < numGlyphs; ++i)
594  width += glyphs.advances_x[i];
595  return qRound(width);
596  }
597 
598  QStackTextEngine layout(text, d.data());
599  layout.ignoreBidi = true;
600  return qRound(layout.width(0, len));
601 }
int width(const QString &, int len=-1) const
Returns the width in pixels of the first len characters of text.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const =0
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
const char * layout
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ width() [3/3]

int QFontMetrics::width ( QChar  ch) const

Returns the logical width of character ch in pixels.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

bearings.png
Bearings

This is a distance appropriate for drawing a subsequent character after ch.

Some of the metrics are described in the image to the right. The central dark rectangles cover the logical width() of each character. The outer pale rectangles cover the leftBearing() and rightBearing() of each character. Notice that the bearings of "f" in this particular font are both negative, while the bearings of "o" are both positive.

Warning
This function will produce incorrect results for Arabic characters or non-spacing marks in the middle of a string, as the glyph shaping and positioning of marks that happens when processing strings cannot be taken into account. When implementing an interactive text control, use QTextLayout instead.
See also
boundingRect()

Definition at line 630 of file qfontmetrics.cpp.

631 {
633  return 0;
634 
635  const int script = QUnicodeTables::script(ch);
636  QFontEngine *engine;
637  if (d->capital == QFont::SmallCaps && ch.isLower())
638  engine = d->smallCapsFontPrivate()->engineForScript(script);
639  else
640  engine = d->engineForScript(script);
641  Q_ASSERT(engine != 0);
642 
644 
645  QGlyphLayoutArray<8> glyphs;
646  int nglyphs = 7;
647  engine->stringToCMap(&ch, 1, &glyphs, &nglyphs, 0);
648  return qRound(glyphs.advances_x[0]);
649 }
QFixed * advances_x
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const =0
QFontPrivate * smallCapsFontPrivate() const
Definition: qfont.cpp:323
Category category() const
Returns the character&#39;s category.
Definition: qchar.cpp:853
bool isLower() const
Returns true if the character is a lowercase letter, i.
Definition: qchar.h:272
uint capital
Definition: qfont_p.h:192
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
void alterCharForCapitalization(QChar &c) const
Definition: qfont.cpp:309
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ xHeight()

int QFontMetrics::xHeight ( ) const

Returns the 'x' height of the font.

This is often but not always the same as the height of the character 'x'.

Definition at line 421 of file qfontmetrics.cpp.

422 {
424  Q_ASSERT(engine != 0);
425  if (d->capital == QFont::SmallCaps)
427  return qRound(engine->xHeight());
428 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QFontPrivate * smallCapsFontPrivate() const
Definition: qfont.cpp:323
uint capital
Definition: qfont_p.h:192
virtual QFixed ascent() const =0
virtual QFixed xHeight() const
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

Friends and Related Functions

◆ QFontMetricsF

friend class QFontMetricsF
friend

Definition at line 138 of file qfontmetrics.h.

◆ QFontPrivate

friend class QFontPrivate
friend

Definition at line 136 of file qfontmetrics.h.

Referenced by QFontMetrics().

◆ QStackTextEngine

friend class QStackTextEngine
friend

Definition at line 139 of file qfontmetrics.h.

Properties

◆ d

QExplicitlySharedDataPointer<QFontPrivate> QFontMetrics::d
private

The documentation for this class was generated from the following files: