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

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

#include <qfontmetrics.h>

Public Functions

qreal ascent () const
 Returns the ascent of the font. More...
 
qreal averageCharWidth () const
 Returns the average width of glyphs in the font. More...
 
QRectF boundingRect (const QString &string) const
 Returns the bounding rectangle of the characters in the string specified by text. More...
 
QRectF boundingRect (QChar) const
 Returns the bounding rectangle of the character ch relative to the left-most point on the base line. More...
 
QRectF boundingRect (const QRectF &r, int flags, const QString &string, int tabstops=0, int *tabarray=0) const
 Returns the bounding rectangle of the characters in the given text. More...
 
qreal descent () const
 Returns the descent of the font. More...
 
QString elidedText (const QString &text, Qt::TextElideMode mode, qreal width, int flags=0) const
 If the string text is wider than width, returns an elided version of the string (i. More...
 
qreal 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 character given by ch, encoded in UCS-4/UTF-32, is a valid character in the font; otherwise returns false. More...
 
qreal leading () const
 Returns the leading of the font. More...
 
qreal leftBearing (QChar) const
 Returns the left bearing of character ch in the font. More...
 
qreal lineSpacing () const
 Returns the distance from one base line to the next. More...
 
qreal lineWidth () const
 Returns the width of the underline and strikeout lines, adjusted for the point size of the font. More...
 
qreal maxWidth () const
 Returns the width of the widest character in the font. More...
 
qreal minLeftBearing () const
 Returns the minimum left bearing of the font. More...
 
qreal minRightBearing () const
 Returns the minimum right bearing of the font. More...
 
bool operator!= (const QFontMetricsF &other)
 Returns true if the font metrics are not equal to the other font metrics; otherwise returns false. More...
 
bool operator!= (const QFontMetricsF &other) const
 Returns true if the font metrics are not equal to the other font metrics; otherwise returns false. More...
 
QFontMetricsFoperator= (const QFontMetricsF &)
 Assigns the font metrics fm to this font metrics object. More...
 
QFontMetricsFoperator= (const QFontMetrics &)
 Assigns other to this object. More...
 
bool operator== (const QFontMetricsF &other)
 Returns true if the font metrics are equal to the other font metrics; otherwise returns false. More...
 
bool operator== (const QFontMetricsF &other) const
 Returns true if the font metrics are equal to the other font metrics; otherwise returns false. More...
 
qreal overlinePos () const
 Returns the distance from the base line to where an overline should be drawn. More...
 
 QFontMetricsF (const QFont &)
 Constructs a font metrics object for font. More...
 
 QFontMetricsF (const QFont &, QPaintDevice *pd)
 Constructs a font metrics object for font and paintdevice. More...
 
 QFontMetricsF (const QFontMetrics &)
 Constructs a font metrics object with floating point precision from the given fontMetrics object. More...
 
 QFontMetricsF (const QFontMetricsF &)
 Constructs a copy of fm. More...
 
qreal rightBearing (QChar) const
 Returns the right bearing of character ch in the font. More...
 
QSizeF size (int flags, const QString &str, int tabstops=0, int *tabarray=0) const
 Returns the size in pixels of the characters in the given text. More...
 
qreal strikeOutPos () const
 Returns the distance from the base line to where the strikeout line should be drawn. More...
 
QRectF tightBoundingRect (const QString &text) const
 Returns a tight bounding rectangle around the characters in the string specified by text. More...
 
qreal underlinePos () const
 Returns the distance from the base line to where an underscore should be drawn. More...
 
qreal width (const QString &string) const
 Returns the width in pixels of the characters in the given text. More...
 
qreal width (QChar) const
 Returns the logical width of character ch in pixels. More...
 
qreal xHeight () const
 Returns the 'x' height of the font. More...
 
 ~QFontMetricsF ()
 Destroys the font metrics object and frees all allocated resources. More...
 

Properties

QExplicitlySharedDataPointer< QFontPrivated
 

Detailed Description

The QFontMetricsF class provides font metrics information.

Note
This class or function is reentrant.

QFontMetricsF functions calculate the size of characters and strings for a given font. You can construct a QFontMetricsF object with an existing QFont to obtain metrics for that font. If the 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);
QFontMetricsF fm(font);
qreal pixelsWide = fm.width("What's the width of this text?");
qreal pixelsHigh = fm.height();
See also
QFont QFontInfo QFontDatabase

Definition at line 145 of file qfontmetrics.h.

Constructors and Destructors

◆ QFontMetricsF() [1/4]

QFontMetricsF::QFontMetricsF ( 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 QFontMetricsF(const QFont &, QPaintDevice *) to get the font metrics that are compatible with a certain paint device.

Definition at line 1102 of file qfontmetrics.cpp.

1103  : d(font.d.data())
1104 {
1105 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfont.h:343

◆ QFontMetricsF() [2/4]

QFontMetricsF::QFontMetricsF ( 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 1120 of file qfontmetrics.cpp.

1121 {
1122  int dpi = paintdevice ? paintdevice->logicalDpiY() : qt_defaultDpi();
1123 #ifdef Q_WS_X11
1124  const QX11Info *info = qt_x11Info(paintdevice);
1125  int screen = info ? info->screen() : 0;
1126 #else
1127  const int screen = 0;
1128 #endif
1129  if (font.d->dpi != dpi || font.d->screen != screen ) {
1130  d = new QFontPrivate(*font.d);
1131  d->dpi = dpi;
1132  d->screen = screen;
1133  } else {
1134  d = font.d.data();
1135  }
1136 
1137 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
static mach_timebase_info_data_t info
int logicalDpiY() const
Definition: qpaintdevice.h:96
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)
int screen() const
Returns the number of the screen currently in use.

◆ QFontMetricsF() [3/4]

QFontMetricsF::QFontMetricsF ( const QFontMetrics fontMetrics)

Constructs a font metrics object with floating point precision from the given fontMetrics object.

Since
4.2

Definition at line 1070 of file qfontmetrics.cpp.

1071  : d(fontMetrics.d.data())
1072 {
1073 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:141

◆ QFontMetricsF() [4/4]

QFontMetricsF::QFontMetricsF ( const QFontMetricsF fm)

Constructs a copy of fm.

Definition at line 1142 of file qfontmetrics.cpp.

1143  : d(fm.d.data())
1144 {
1145 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145

◆ ~QFontMetricsF()

QFontMetricsF::~QFontMetricsF ( )

Destroys the font metrics object and frees all allocated resources.

Definition at line 1151 of file qfontmetrics.cpp.

1152 {
1153 }

Functions

◆ ascent()

qreal QFontMetricsF::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 1231 of file qfontmetrics.cpp.

Referenced by overlinePos(), QFontFamilyDelegate::paint(), and strikeOutPos().

1232 {
1234  Q_ASSERT(engine != 0);
1235  return engine->ascent().toReal();
1236 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual QFixed ascent() const =0
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
qreal toReal() const
Definition: qfixed_p.h:77

◆ averageCharWidth()

qreal QFontMetricsF::averageCharWidth ( ) const

Returns the average width of glyphs in the font.

Since
4.2

Definition at line 1366 of file qfontmetrics.cpp.

1367 {
1369  Q_ASSERT(engine != 0);
1370  return engine->averageCharWidth().toReal();
1371 }
virtual QFixed averageCharWidth() const
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
qreal toReal() const
Definition: qfixed_p.h:77

◆ boundingRect() [1/3]

QRectF QFontMetricsF::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 height().

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

Definition at line 1568 of file qfontmetrics.cpp.

Referenced by size().

1569 {
1570  int len = text.length();
1571  if (len == 0)
1572  return QRectF();
1573 
1574  QStackTextEngine layout(text, d.data());
1575  layout.ignoreBidi = true;
1576  layout.itemize();
1577  glyph_metrics_t gm = layout.boundingBox(0, len);
1578  return QRectF(gm.x.toReal(), gm.y.toReal(),
1579  gm.width.toReal(), gm.height.toReal());
1580 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
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
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
const char * layout
qreal toReal() const
Definition: qfixed_p.h:77

◆ boundingRect() [2/3]

QRectF QFontMetricsF::boundingRect ( QChar  ch) const

Returns the bounding rectangle of the character ch relative to the left-most point on the base line.

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.

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

See also
width()

Definition at line 1595 of file qfontmetrics.cpp.

1596 {
1597  const int script = QUnicodeTables::script(ch);
1598  QFontEngine *engine;
1599  if (d->capital == QFont::SmallCaps && ch.isLower())
1600  engine = d->smallCapsFontPrivate()->engineForScript(script);
1601  else
1602  engine = d->engineForScript(script);
1603  Q_ASSERT(engine != 0);
1604 
1606 
1607  QGlyphLayoutArray<10> glyphs;
1608  int nglyphs = 9;
1609  engine->stringToCMap(&ch, 1, &glyphs, &nglyphs, 0);
1610  glyph_metrics_t gm = engine->boundingBox(glyphs.glyphs[0]);
1611  return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal());
1612 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
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
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
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
qreal toReal() const
Definition: qfixed_p.h:77
void alterCharForCapitalization(QChar &c) const
Definition: qfont.cpp:309

◆ boundingRect() [3/3]

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

Returns the bounding rectangle of the characters in the given text.

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

This is the set of pixels the text would cover if drawn when constrained to the bounding rectangle specified by 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.

These flags are defined in Qt::AlignmentFlag.

If Qt::TextExpandTabs is set in flags, the following behavior is used to interpret tab characters in the text:

  • If tabArray is non-null, it specifies a 0-terminated sequence of pixel-positions for tabs in the text.
  • 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.

Newline characters are processed as line breaks.

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 1676 of file qfontmetrics.cpp.

1678 {
1679  int tabArrayLen = 0;
1680  if (tabArray)
1681  while (tabArray[tabArrayLen])
1682  tabArrayLen++;
1683 
1684  QRectF rb;
1685  qt_format_text(QFont(d.data()), rect, flags | Qt::TextDontPrint, text, &rb, tabStops, tabArray,
1686  tabArrayLen, 0);
1687  return rb;
1688 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
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

◆ descent()

qreal QFontMetricsF::descent ( ) const

Returns the descent of the font.

The descent is the distance from the base line to the lowest point characters extend to. (Note that this is different from X, which adds 1 pixel.) 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 1250 of file qfontmetrics.cpp.

1251 {
1253  Q_ASSERT(engine != 0);
1254  return engine->descent().toReal();
1255 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual QFixed descent() const =0
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
qreal toReal() const
Definition: qfixed_p.h:77

◆ elidedText()

QString QFontMetricsF::elidedText ( const QString text,
Qt::TextElideMode  mode,
qreal  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.

Definition at line 1779 of file qfontmetrics.cpp.

1780 {
1781  QString _text = text;
1782  if (!(flags & Qt::TextLongestVariant)) {
1783  int posA = 0;
1784  int posB = _text.indexOf(QLatin1Char('\x9c'));
1785  while (posB >= 0) {
1786  QString portion = _text.mid(posA, posB - posA);
1787  if (size(flags, portion).width() <= width)
1788  return portion;
1789  posA = posB + 1;
1790  posB = _text.indexOf(QLatin1Char('\x9c'), posA);
1791  }
1792  _text = _text.mid(posA);
1793  }
1794  QStackTextEngine engine(_text, QFont(d.data()));
1795  return engine.elidedText(mode, QFixed::fromReal(width), flags);
1796 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
QSizeF size(int flags, const QString &str, int tabstops=0, int *tabarray=0) const
Returns the size in pixels of the characters in the given text.
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QFixed fromReal(qreal r)
Definition: qfixed_p.h:70
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
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
qreal width(const QString &string) const
Returns the width in pixels of the characters in the given text.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
#define text
Definition: qobjectdefs.h:80

◆ height()

qreal QFontMetricsF::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 1265 of file qfontmetrics.cpp.

Referenced by qt_format_text(), and QMacStyle::subControlRect().

1266 {
1268  Q_ASSERT(engine != 0);
1269 
1270  return (engine->ascent() + engine->descent() + 1).toReal();
1271 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
#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
static qreal toReal(Register *reg, int type, bool *ok=0)

◆ inFont()

bool QFontMetricsF::inFont ( QChar  ch) const

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

Definition at line 1377 of file qfontmetrics.cpp.

1378 {
1379  const int script = QUnicodeTables::script(ch);
1380  QFontEngine *engine = d->engineForScript(script);
1381  Q_ASSERT(engine != 0);
1382  if (engine->type() == QFontEngine::Box)
1383  return false;
1384  return engine->canRender(&ch, 1);
1385 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
#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

◆ inFontUcs4()

bool QFontMetricsF::inFontUcs4 ( uint  ch) const

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

Since
4.8

Definition at line 1397 of file qfontmetrics.cpp.

1398 {
1399  const int script = QUnicodeTables::script(ucs4);
1400  QFontEngine *engine = d->engineForScript(script);
1401  Q_ASSERT(engine != 0);
1402  if (engine->type() == QFontEngine::Box)
1403  return false;
1404  QString utf16 = QString::fromUcs4(&ucs4, 1);
1405  return engine->canRender(utf16.data(), utf16.length());
1406 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
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
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()

qreal QFontMetricsF::leading ( ) const

Returns the leading of the font.

This is the natural inter-line spacing.

See also
height(), lineSpacing()

Definition at line 1280 of file qfontmetrics.cpp.

Referenced by QStaticTextPrivate::paintText(), and qt_format_text().

1281 {
1283  Q_ASSERT(engine != 0);
1284  return engine->leading().toReal();
1285 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
qreal toReal() const
Definition: qfixed_p.h:77
virtual QFixed leading() const =0

◆ leftBearing()

qreal QFontMetricsF::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 1420 of file qfontmetrics.cpp.

1421 {
1422  const int script = QUnicodeTables::script(ch);
1423  QFontEngine *engine;
1424  if (d->capital == QFont::SmallCaps && ch.isLower())
1425  engine = d->smallCapsFontPrivate()->engineForScript(script);
1426  else
1427  engine = d->engineForScript(script);
1428  Q_ASSERT(engine != 0);
1429  if (engine->type() == QFontEngine::Box)
1430  return 0;
1431 
1433 
1434  QGlyphLayoutArray<10> glyphs;
1435  int nglyphs = 9;
1436  engine->stringToCMap(&ch, 1, &glyphs, &nglyphs, 0);
1437  // ### can nglyphs != 1 happen at all? Not currently I think
1438  qreal lb;
1439  engine->getGlyphBearings(glyphs.glyphs[0], &lb);
1440  return lb;
1441 }
double qreal
Definition: qglobal.h:1193
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
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

◆ lineSpacing()

qreal QFontMetricsF::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 1294 of file qfontmetrics.cpp.

1295 {
1297  Q_ASSERT(engine != 0);
1298  return (engine->leading() + engine->ascent() + engine->descent() + 1).toReal();
1299 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
#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
virtual QFixed leading() const =0
static qreal toReal(Register *reg, int type, bool *ok=0)

◆ lineWidth()

qreal QFontMetricsF::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 1839 of file qfontmetrics.cpp.

1840 {
1842  Q_ASSERT(engine != 0);
1843  return engine->lineThickness().toReal();
1844 }
virtual QFixed lineThickness() const
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
qreal toReal() const
Definition: qfixed_p.h:77

◆ maxWidth()

qreal QFontMetricsF::maxWidth ( ) const

Returns the width of the widest character in the font.

Definition at line 1338 of file qfontmetrics.cpp.

1339 {
1341  Q_ASSERT(engine != 0);
1342  return engine->maxCharWidth();
1343 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual qreal maxCharWidth() const =0
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294

◆ minLeftBearing()

qreal QFontMetricsF::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 1311 of file qfontmetrics.cpp.

1312 {
1314  Q_ASSERT(engine != 0);
1315  return engine->minLeftBearing();
1316 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual qreal minLeftBearing() const
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294

◆ minRightBearing()

qreal QFontMetricsF::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 1328 of file qfontmetrics.cpp.

1329 {
1331  Q_ASSERT(engine != 0);
1332  return engine->minRightBearing();
1333 }
virtual qreal minRightBearing() const
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294

◆ operator!=() [1/2]

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

Returns true if the font metrics are not equal to the other font metrics; otherwise returns false.

See also
operator==()

Definition at line 197 of file qfontmetrics.h.

197 { return !operator==(other); } // 5.0 - remove me
bool operator==(const QFontMetricsF &other)
Returns true if the font metrics are equal to the other font metrics; otherwise returns false...

◆ operator!=() [2/2]

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

Returns true if the font metrics are not equal to the other font metrics; 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.

See also
operator==()

Definition at line 198 of file qfontmetrics.h.

198 { return !operator==(other); }
bool operator==(const QFontMetricsF &other)
Returns true if the font metrics are equal to the other font metrics; otherwise returns false...

◆ operator=() [1/2]

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

Assigns the font metrics fm to this font metrics object.

Definition at line 1158 of file qfontmetrics.cpp.

1159 {
1160  d = fm.d.data();
1161  return *this;
1162 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145

◆ operator=() [2/2]

QFontMetricsF & QFontMetricsF::operator= ( const QFontMetrics other)

Assigns other to this object.

Since
4.2

Definition at line 1083 of file qfontmetrics.cpp.

1084 {
1085  d = other.d.data();
1086  return *this;
1087 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
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 QFontMetricsF::operator== ( const QFontMetricsF other)

Returns true if the font metrics are equal to the other font metrics; 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 to be compatible.

Definition at line 1189 of file qfontmetrics.cpp.

1190 {
1191  return d == other.d;
1192 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201

◆ operator==() [2/2]

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

Returns true if the font metrics are equal to the other font metrics; 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 to be compatible.

Definition at line 1176 of file qfontmetrics.cpp.

1177 {
1178  return d == other.d;
1179 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201

◆ overlinePos()

qreal QFontMetricsF::overlinePos ( ) const

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

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

Definition at line 1817 of file qfontmetrics.cpp.

1818 {
1819  return ascent() + 1;
1820 }
qreal ascent() const
Returns the ascent of the font.

◆ rightBearing()

qreal QFontMetricsF::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 1455 of file qfontmetrics.cpp.

1456 {
1457  const int script = QUnicodeTables::script(ch);
1458  QFontEngine *engine;
1459  if (d->capital == QFont::SmallCaps && ch.isLower())
1460  engine = d->smallCapsFontPrivate()->engineForScript(script);
1461  else
1462  engine = d->engineForScript(script);
1463  Q_ASSERT(engine != 0);
1464  if (engine->type() == QFontEngine::Box)
1465  return 0;
1466 
1468 
1469  QGlyphLayoutArray<10> glyphs;
1470  int nglyphs = 9;
1471  engine->stringToCMap(&ch, 1, &glyphs, &nglyphs, 0);
1472  // ### can nglyphs != 1 happen at all? Not currently I think
1473  qreal rb;
1474  engine->getGlyphBearings(glyphs.glyphs[0], 0, &rb);
1475  return rb;
1476 
1477 }
double qreal
Definition: qglobal.h:1193
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
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

◆ size()

QSizeF QFontMetricsF::size ( int  flags,
const QString text,
int  tabStops = 0,
int *  tabArray = 0 
) const

Returns the size in pixels of the characters in the given text.

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

These flags are defined in Qt::TextFlags.

If Qt::TextExpandTabs is set in flags, the following behavior is used to interpret tab characters in the text:

  • If tabArray is non-null, it specifies a 0-terminated sequence of pixel-positions for tabs in the text.
  • If tabStops is non-zero, it is used as the tab spacing (in pixels).

Newline characters are processed as line breaks.

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

See also
boundingRect()

Definition at line 1718 of file qfontmetrics.cpp.

Referenced by elidedText(), and QMacStyle::subControlRect().

1719 {
1720  return boundingRect(QRectF(), flags | Qt::TextLongestVariant, text, tabStops, tabArray).size();
1721 }
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
QRectF boundingRect(const QString &string) const
Returns the bounding rectangle of the characters in the string specified by text. ...
QSizeF size() const
Returns the size of the rectangle.
Definition: qrect.h:713

◆ strikeOutPos()

qreal QFontMetricsF::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 1828 of file qfontmetrics.cpp.

1829 {
1830  return ascent() / 3.;
1831 }
qreal ascent() const
Returns the ascent of the font.

◆ tightBoundingRect()

QRectF QFontMetricsF::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 1748 of file qfontmetrics.cpp.

Referenced by QFontFamilyDelegate::paint().

1749 {
1750  if (text.length() == 0)
1751  return QRect();
1752 
1753  QStackTextEngine layout(text, d.data());
1754  layout.ignoreBidi = true;
1755  layout.itemize();
1756  glyph_metrics_t gm = layout.tightBoundingBox(0, text.length());
1757  return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal());
1758 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
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
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
const char * layout
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
qreal toReal() const
Definition: qfixed_p.h:77

◆ underlinePos()

qreal QFontMetricsF::underlinePos ( ) const

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

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

Definition at line 1804 of file qfontmetrics.cpp.

1805 {
1807  Q_ASSERT(engine != 0);
1808  return engine->underlinePosition().toReal();
1809 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
qreal toReal() const
Definition: qfixed_p.h:77
virtual QFixed underlinePosition() const

◆ width() [1/2]

qreal QFontMetricsF::width ( const QString text) const

Returns the width in pixels of the characters in the given text.

Note that this value is not equal to the width returned by boundingRect().width() because 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 1489 of file qfontmetrics.cpp.

Referenced by elidedText(), and qt_format_text().

1490 {
1491  int pos = text.indexOf(QLatin1Char('\x9c'));
1492  int len = (pos != -1) ? pos : text.length();
1493 
1494  QStackTextEngine layout(text, d.data());
1495  layout.ignoreBidi = true;
1496  layout.itemize();
1497  return layout.width(0, len).toReal();
1498 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
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
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ width() [2/2]

qreal QFontMetricsF::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 1527 of file qfontmetrics.cpp.

1528 {
1530  return 0.;
1531 
1532  const int script = QUnicodeTables::script(ch);
1533  QFontEngine *engine;
1534  if (d->capital == QFont::SmallCaps && ch.isLower())
1535  engine = d->smallCapsFontPrivate()->engineForScript(script);
1536  else
1537  engine = d->engineForScript(script);
1538  Q_ASSERT(engine != 0);
1539 
1541 
1542  QGlyphLayoutArray<8> glyphs;
1543  int nglyphs = 7;
1544  engine->stringToCMap(&ch, 1, &glyphs, &nglyphs, 0);
1545  return glyphs.advances_x[0].toReal();
1546 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
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
qreal toReal() const
Definition: qfixed_p.h:77
void alterCharForCapitalization(QChar &c) const
Definition: qfont.cpp:309

◆ xHeight()

qreal QFontMetricsF::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 1349 of file qfontmetrics.cpp.

1350 {
1352  Q_ASSERT(engine != 0);
1353  if (d->capital == QFont::SmallCaps)
1355  return engine->xHeight().toReal();
1356 }
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfontmetrics.h:201
#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
qreal toReal() const
Definition: qfixed_p.h:77

Properties

◆ d

QExplicitlySharedDataPointer<QFontPrivate> QFontMetricsF::d
private

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