QFontMetrics Class Reference

#include <qfontmetrics.h>

Collaboration diagram for QFontMetrics:

Collaboration graph
[legend]
List of all members.

Detailed Description

The QFontMetrics class provides font metrics information.

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.)

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.

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 44 of file qfontmetrics.h.

Public Member Functions

 QFontMetrics (const QFont &)
 QFontMetrics (const QFont &, QPaintDevice *pd)
 QFontMetrics (const QFontMetrics &)
 ~QFontMetrics ()
QFontMetricsoperator= (const QFontMetrics &)
int ascent () const
int descent () const
int height () const
int leading () const
int lineSpacing () const
int minLeftBearing () const
int minRightBearing () const
int maxWidth () const
int xHeight () const
int averageCharWidth () const
bool inFont (QChar) const
int leftBearing (QChar) const
int rightBearing (QChar) const
int width (const QString &, int len=-1) const
int width (QChar) const
int charWidth (const QString &str, int pos) const
QRect boundingRect (QChar) const
QRect boundingRect (const QString &text) const
QRect boundingRect (const QRect &r, int flags, const QString &text, int tabstops=0, int *tabarray=0) const
QRect boundingRect (int x, int y, int w, int h, int flags, const QString &text, int tabstops=0, int *tabarray=0) const
QSize size (int flags, const QString &str, int tabstops=0, int *tabarray=0) const
QString elidedText (const QString &text, Qt::TextElideMode mode, int width, int flags=0) const
int underlinePos () const
int overlinePos () const
int strikeOutPos () const
int lineWidth () const
bool operator== (const QFontMetrics &other)
bool operator== (const QFontMetrics &other) const
bool operator!= (const QFontMetrics &other)
bool operator!= (const QFontMetrics &other) const

Private Attributes

QFontPrivated

Friends

class QFontMetricsF
class QStackTextEngine


Constructor & Destructor Documentation

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

References d, QBasicAtomic::ref(), and QFontPrivate::ref.

00145     : d(font.d)
00146 {
00147     d->ref.ref();
00148 }

Here is the call graph for this function:

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

References QFont::d, d, QFontPrivate::dpi, info, QPaintDevice::logicalDpiY(), qt_defaultDpi(), qt_x11Info(), QBasicAtomic::ref(), QFontPrivate::ref, and QFontPrivate::screen.

00164 {
00165     int dpi = paintdevice ? paintdevice->logicalDpiY() : qt_defaultDpi();
00166 #ifdef Q_WS_X11
00167     const QX11Info *info = qt_x11Info(paintdevice);
00168     int screen = info ? info->screen() : 0;
00169 #else
00170     const int screen = 0;
00171 #endif
00172     if (font.d->dpi != dpi || font.d->screen != screen ) {
00173         d = new QFontPrivate(*font.d);
00174         d->dpi = dpi;
00175         d->screen = screen;
00176     } else {
00177         d = font.d;
00178         d->ref.ref();
00179     }
00180 
00181 }

Here is the call graph for this function:

QFontMetrics::QFontMetrics ( const QFontMetrics fm  ) 

Constructs a copy of fm.

Definition at line 186 of file qfontmetrics.cpp.

References d, QBasicAtomic::ref(), and QFontPrivate::ref.

00187     : d(fm.d)
00188 { d->ref.ref(); }

Here is the call graph for this function:

QFontMetrics::~QFontMetrics (  ) 

Destroys the font metrics object and frees all allocated resources.

Definition at line 194 of file qfontmetrics.cpp.

References d, QBasicAtomic::deref(), and QFontPrivate::ref.

00195 {
00196     if (!d->ref.deref())
00197         delete d;
00198 }

Here is the call graph for this function:


Member Function Documentation

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

Assigns the font metrics fm.

Definition at line 203 of file qfontmetrics.cpp.

References d, and qAtomicAssign().

00204 {
00205     qAtomicAssign(d, fm.d);
00206     return *this;
00207 }

Here is the call graph for this function:

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

References QFontEngine::ascent(), QUnicodeTables::Common, d, QFontPrivate::engineForScript(), and qRound().

Referenced by Q3TextFormat::applyFont(), BackTabTextEdit::loadResource(), overlinePos(), Q3ListBoxPixmap::paint(), Q3ListBoxText::paint(), QLineEdit::paintEvent(), CharacterWidget::paintEvent(), Q3ComboBox::paintEvent(), Q3TextFormat::Q3TextFormat(), QTipLabel::QTipLabel(), strikeOutPos(), Q3TextFormat::update(), and qdesigner_internal::Connection::updatePixmap().

00277 {
00278     QFontEngine *engine = d->engineForScript(QUnicodeTables::Common);
00279     Q_ASSERT(engine != 0);
00280     return qRound(engine->ascent());
00281 }

Here is the call graph for this function:

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

References QUnicodeTables::Common, d, QFontEngine::descent(), QFontPrivate::engineForScript(), and qRound().

Referenced by Q3TextFormat::descent(), QCleanlooksStyle::drawControl(), Q3FileDialogPrivate::MCItem::paint(), Q3TextFormat::Q3TextFormat(), QTipLabel::QTipLabel(), and Q3TextFormat::update().

00296 {
00297     QFontEngine *engine = d->engineForScript(QUnicodeTables::Common);
00298     Q_ASSERT(engine != 0);
00299     return qRound(engine->descent());
00300 }

Here is the call graph for this function:

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

References QFontEngine::ascent(), QUnicodeTables::Common, d, QFontEngine::descent(), QFontPrivate::engineForScript(), and qRound().

Referenced by Q3CheckListItem::activate(), QMenuBarPrivate::calcActionRects(), QMenuPrivate::calcActionRects(), Q3IconViewItem::calcRect(), QTextDocumentLayoutPrivate::drawListItem(), Q3TextParagraph::drawString(), QStatusBar::event(), Q3FileDialogPrivate::MCItem::height(), Q3ListView::init(), QCss::ValueExtractor::lengthValue(), BackTabTextEdit::loadResource(), QGroupBox::minimumSizeHint(), QLineEdit::minimumSizeHint(), Q3ScrollView::minimumSizeHint(), Q3ListBoxPixmap::paint(), Q3FileDialogPrivate::MCItem::paint(), Q3ListBoxText::paint(), QLineEdit::paintEvent(), Q3ListView::reconfigureItems(), QStatusBar::reformat(), Q3Header::sectionSizeHint(), QProgressBar::sizeHint(), Q3TableItem::sizeHint(), Q3TextEdit::sizeHint(), QScrollArea::sizeHint(), Q3ScrollView::sizeHint(), Q3ProgressBar::sizeHint(), QCommonStyle::subControlRect(), QCleanlooksStyle::subControlRect(), and QTabBar::tabSizeHint().

00311 {
00312     QFontEngine *engine = d->engineForScript(QUnicodeTables::Common);
00313     Q_ASSERT(engine != 0);
00314     return qRound(engine->ascent() + engine->descent()) + 1;
00315 }

Here is the call graph for this function:

int QFontMetrics::leading (  )  const

Returns the leading of the font.

This is the natural inter-line spacing.

See also:
height(), lineSpacing()

Definition at line 324 of file qfontmetrics.cpp.

References QUnicodeTables::Common, d, QFontPrivate::engineForScript(), QFontEngine::leading(), and qRound().

Referenced by Q3TextFormat::applyFont(), Q3TextFormat::leading(), QLineEdit::minimumSizeHint(), Q3TextFormat::Q3TextFormat(), and Q3TextFormat::update().

00325 {
00326     QFontEngine *engine = d->engineForScript(QUnicodeTables::Common);
00327     Q_ASSERT(engine != 0);
00328     return qRound(engine->leading());
00329 }

Here is the call graph for this function:

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

References QFontEngine::ascent(), QUnicodeTables::Common, d, QFontEngine::descent(), QFontPrivate::engineForScript(), QFontEngine::leading(), and qRound().

Referenced by Q3TextFormat::applyFont(), Q3TextEdit::changeEvent(), QItemDelegate::doLayout(), QTextDocumentLayoutPrivate::drawListItem(), Q3ListBoxPixmap::height(), Q3ListBoxText::height(), Q3FileDialog::init(), QHeaderView::minimumSectionSize(), Q3TextEdit::optimAppend(), Q3TextEdit::optimDoAutoScroll(), Q3TextEdit::optimDrawContents(), Q3TextEdit::optimInsert(), Q3TextEdit::optimMousePressEvent(), Q3TextEdit::optimMouseReleaseEvent(), Q3TextEdit::optimSetText(), QView3DWidget::paintGL(), Q3TextEdit::paragraphAt(), QCommonStyle::pixelMetric(), Q3TextFormat::Q3TextFormat(), QComboBoxPrivate::recomputeSizeHint(), QDockWidgetPrivate::relayout(), Q3Header::sectionSizeHint(), Q3ListViewItem::setup(), QLabelPrivate::sizeForWidth(), QFontFamilyDelegate::sizeHint(), Q3Header::sizeHint(), Q3TimeEdit::sizeHint(), Q3DateEdit::sizeHint(), Q3ComboBox::sizeHint(), QLineEdit::sizeHint(), Q3TextEdit::sync(), and Q3TextFormat::update().

00339 {
00340     QFontEngine *engine = d->engineForScript(QUnicodeTables::Common);
00341     Q_ASSERT(engine != 0);
00342     return qRound(engine->leading() + engine->ascent() + engine->descent()) + 1;
00343 }

Here is the call graph for this function:

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

References QUnicodeTables::Common, d, QFontPrivate::engineForScript(), QFontEngine::minLeftBearing(), and qRound().

Referenced by Q3IconView::changeEvent(), Q3ListView::init(), Q3TextFormat::minLeftBearing(), QLineEdit::paintEvent(), Q3IconView::Q3IconView(), Q3TextFormat::Q3TextFormat(), Q3ListView::reconfigureItems(), Q3TextFormat::update(), and Q3FileDialogPrivate::MCItem::width().

00356 {
00357     QFontEngine *engine = d->engineForScript(QUnicodeTables::Common);
00358     Q_ASSERT(engine != 0);
00359     return qRound(engine->minLeftBearing());
00360 }

Here is the call graph for this function:

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

References QUnicodeTables::Common, d, QFontPrivate::engineForScript(), QFontEngine::minRightBearing(), and qRound().

Referenced by Q3IconView::changeEvent(), Q3ListView::init(), Q3TextFormat::minRightBearing(), QLineEdit::paintEvent(), Q3IconView::Q3IconView(), Q3TextFormat::Q3TextFormat(), Q3ListView::reconfigureItems(), Q3TextFormat::update(), and Q3FileDialogPrivate::MCItem::width().

00373 {
00374     QFontEngine *engine = d->engineForScript(QUnicodeTables::Common);
00375     Q_ASSERT(engine != 0);
00376     return qRound(engine->minRightBearing());
00377 }

Here is the call graph for this function:

int QFontMetrics::maxWidth (  )  const

Returns the width of the widest character in the font.

Definition at line 382 of file qfontmetrics.cpp.

References QUnicodeTables::Common, d, QFontPrivate::engineForScript(), QFontEngine::maxCharWidth(), and qRound().

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

00383 {
00384     QFontEngine *engine = d->engineForScript(QUnicodeTables::Common);
00385     Q_ASSERT(engine != 0);
00386     return qRound(engine->maxCharWidth());
00387 }

Here is the call graph for this function:

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

References QUnicodeTables::Common, d, QFontPrivate::engineForScript(), qRound(), and QFontEngine::xHeight().

Referenced by QCss::ValueExtractor::lengthValue().

00394 {
00395     QFontEngine *engine = d->engineForScript(QUnicodeTables::Common);
00396     Q_ASSERT(engine != 0);
00397     return qRound(engine->xHeight());
00398 }

Here is the call graph for this function:

int QFontMetrics::averageCharWidth (  )  const

Since:
4.2
Returns the average width of glyphs in the font.

Definition at line 405 of file qfontmetrics.cpp.

References QFontEngine::averageCharWidth(), QUnicodeTables::Common, d, QFontPrivate::engineForScript(), and qRound().

Referenced by QLabelPrivate::sizeForWidth().

00406 {
00407     QFontEngine *engine = d->engineForScript(QUnicodeTables::Common);
00408     Q_ASSERT(engine != 0);
00409     return qRound(engine->averageCharWidth());
00410 }

Here is the call graph for this function:

bool QFontMetrics::inFont ( QChar  ch  )  const

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

Definition at line 416 of file qfontmetrics.cpp.

References QFontEngine::Box, QFontEngine::canRender(), d, QFontPrivate::engineForScript(), QUnicodeTables::script(), and QFontEngine::type().

Referenced by QWindowsXPStyle::styleHint().

00417 {
00418     const int script = QUnicodeTables::script(ch);
00419     QFontEngine *engine = d->engineForScript(script);
00420     Q_ASSERT(engine != 0);
00421     if (engine->type() == QFontEngine::Box)
00422         return false;
00423     return engine->canRender(&ch, 1);
00424 }

Here is the call graph for this function:

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

References QFontEngine::boundingBox(), QFontEngine::Box, d, QFontPrivate::engineForScript(), qRound(), QUnicodeTables::script(), QFontEngine::stringToCMap(), QFontEngine::type(), and glyph_metrics_t::x.

Referenced by qdesigner_internal::Connection::updatePixmap().

00439 {
00440     const int script = QUnicodeTables::script(ch);
00441     QFontEngine *engine = d->engineForScript(script);
00442     Q_ASSERT(engine != 0);
00443     if (engine->type() == QFontEngine::Box)
00444         return 0;
00445 
00446     QGlyphLayout glyphs[10];
00447     int nglyphs = 9;
00448     engine->stringToCMap(&ch, 1, glyphs, &nglyphs, 0);
00449     // ### can nglyphs != 1 happen at all? Not currently I think
00450     glyph_metrics_t gi = engine->boundingBox(glyphs[0].glyph);
00451     return qRound(gi.x);
00452 }

Here is the call graph for this function:

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

References QFontEngine::boundingBox(), QFontEngine::Box, d, QFontPrivate::engineForScript(), qRound(), QUnicodeTables::script(), QFontEngine::stringToCMap(), QFontEngine::type(), glyph_metrics_t::width, glyph_metrics_t::x, and glyph_metrics_t::xoff.

Referenced by Q3TextFormatter::bidiReorderLine(), and Q3TextFormatterBreakWords::format().

00467 {
00468     const int script = QUnicodeTables::script(ch);
00469     QFontEngine *engine = d->engineForScript(script);
00470     Q_ASSERT(engine != 0);
00471     if (engine->type() == QFontEngine::Box)
00472         return 0;
00473 
00474     QGlyphLayout glyphs[10];
00475     int nglyphs = 9;
00476     engine->stringToCMap(&ch, 1, glyphs, &nglyphs, 0);
00477     // ### can nglyphs != 1 happen at all? Not currently I think
00478     glyph_metrics_t gi = engine->boundingBox(glyphs[0].glyph);
00479     return qRound(gi.xoff - gi.x - gi.width);
00480 }

Here is the call graph for this function:

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

References d, layout, QString::length(), and qRound().

Referenced by Q3DataTable::adjustColumn(), QTextDocument::adjustSize(), Q3SimpleRichTextData::adjustSize(), QMenuBarPrivate::calcActionRects(), QMenuPrivate::calcActionRects(), Q3IconViewItem::calcRect(), Q3IconViewItem::calcTmpText(), Q3TextEdit::changeEvent(), charWidth(), QComboBoxPrivate::computeWidthHint(), Q3TitleBar::cutText(), QTextDocumentLayoutPrivate::drawListItem(), Q3TextParagraph::drawString(), elliditide(), EditorPage::fontChange(), Q3ListView::init(), QGroupBox::minimumSizeHint(), QAbstractSpinBox::minimumSizeHint(), Q3TextEdit::optimCharIndex(), Q3TextEdit::optimDoAutoScroll(), Q3TextEdit::optimInsert(), Q3TextEdit::optimSetText(), QFontFamilyDelegate::paint(), CharacterWidget::paintEvent(), Q3SVGPaintEnginePrivate::play(), printPage(), qEllipsisText(), qStrWidth(), qt_drawFontLining(), QComboBoxPrivate::recomputeSizeHint(), Q3ListView::reconfigureItems(), QExpandingLineEdit::resizeToContents(), Q3Header::sectionSizeHint(), MessagesTreeView::setModel(), Q3Table::setNumRows(), PathDeformRenderer::setText(), QWindowsStyle::sizeFromContents(), Q3TableItem::sizeHint(), QProgressBar::sizeHint(), QFontFamilyDelegate::sizeHint(), Q3Header::sizeHint(), Q3TimeEdit::sizeHint(), Q3DateEdit::sizeHint(), QAbstractSpinBox::sizeHint(), Q3ComboBox::sizeHint(), QDateTimeEdit::sizeHint(), QLineEdit::sizeHint(), Q3ProgressBar::sizeHint(), QFontComboBox::sizeHint(), QToolButton::sizeHint(), MainWindow::sizeHint(), Q3ListViewItem::width(), Q3FileDialogPrivate::MCItem::width(), Q3TextFormat::width(), Q3ListBoxPixmap::width(), and Q3ListBoxText::width().

00495 {
00496     if (len < 0)
00497         len = text.length();
00498     if (len == 0)
00499         return 0;
00500 
00501     QTextEngine layout(text, d);
00502     layout.ignoreBidi = true;
00503     layout.itemize();
00504     return qRound(layout.width(0, len));
00505 }

Here is the call graph for this function:

int QFontMetrics::width ( QChar  ch  )  const

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

Returns the logical width of character ch in pixels. 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. Use charWidth() instead if you aren't looking for the width of isolated characters.
See also:
boundingRect(), charWidth()

Definition at line 532 of file qfontmetrics.cpp.

References QUnicodeTables::category(), d, QFontPrivate::engineForScript(), QChar::Mark_NonSpacing, qRound(), QUnicodeTables::script(), and QFontEngine::stringToCMap().

00533 {
00534     if (QUnicodeTables::category(ch) == QChar::Mark_NonSpacing)
00535         return 0;
00536 
00537     const int script = QUnicodeTables::script(ch);
00538     QFontEngine *engine = d->engineForScript(script);
00539     Q_ASSERT(engine != 0);
00540 
00541     QGlyphLayout glyphs[8];
00542     int nglyphs = 7;
00543     engine->stringToCMap(&ch, 1, glyphs, &nglyphs, 0);
00544     return qRound(glyphs[0].advance.x);
00545 }

Here is the call graph for this function:

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

References QUnicodeTables::category(), QUnicodeTables::Common, d, QFontPrivate::engineForScript(), QString::fromRawData(), layout, QString::length(), QChar::Mark_NonSpacing, qMax(), qMin(), qRound(), QUnicodeTables::script(), QFontEngine::stringToCMap(), QString::unicode(), and width().

Referenced by Q3TextParagraph::drawString(), and Q3TextFormat::width().

00559 {
00560     if (pos < 0 || pos > (int)text.length())
00561         return 0;
00562 
00563     const QChar &ch = text.unicode()[pos];
00564     const int script = QUnicodeTables::script(ch);
00565     int width;
00566 
00567     if (script != QUnicodeTables::Common) {
00568         // complex script shaping. Have to do some hard work
00569         int from = qMax(0, pos - 8);
00570         int to = qMin(text.length(), pos + 8);
00571         QString cstr = QString::fromRawData(text.unicode() + from, to - from);
00572         QTextEngine layout(cstr, d);
00573         layout.ignoreBidi = true;
00574         layout.itemize();
00575         width = qRound(layout.width(pos-from, 1));
00576     } else if (QUnicodeTables::category(ch) == QChar::Mark_NonSpacing) {
00577         width = 0;
00578     } else {
00579         QFontEngine *engine = d->engineForScript(script);
00580         Q_ASSERT(engine != 0);
00581 
00582         QGlyphLayout glyphs[8];
00583         int nglyphs = 7;
00584         engine->stringToCMap(&ch, 1, glyphs, &nglyphs, 0);
00585         width = qRound(glyphs[0].advance.x);
00586     }
00587     return width;
00588 }

Here is the call graph for this function:

QRect QFontMetrics::boundingRect ( QChar  ch  )  const

Returns the rectangle that is covered by ink if character ch where 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 639 of file qfontmetrics.cpp.

References QFontEngine::boundingBox(), d, QFontPrivate::engineForScript(), glyph_metrics_t::height, qRound(), QUnicodeTables::script(), QFontEngine::stringToCMap(), glyph_metrics_t::width, glyph_metrics_t::x, and glyph_metrics_t::y.

Referenced by Q3IconViewItem::calcRect(), Q3CanvasText::draw(), XFormView::drawTextType(), BackTabTextEdit::loadResource(), QCalendarWidget::minimumSizeHint(), ColorDock::paintEvent(), QWhatsThat::QWhatsThat(), size(), Q3TableItem::sizeHint(), QCleanlooksStyle::subControlRect(), and QCalendarWidgetPrivate::updateHeader().

00640 {
00641     const int script = QUnicodeTables::script(ch);
00642     QFontEngine *engine = d->engineForScript(script);
00643     Q_ASSERT(engine != 0);
00644 
00645     QGlyphLayout glyphs[10];
00646     int nglyphs = 9;
00647     engine->stringToCMap(&ch, 1, glyphs, &nglyphs, 0);
00648     glyph_metrics_t gm = engine->boundingBox(glyphs[0].glyph);
00649     return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height));
00650 }

Here is the call graph for this function:

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()

Definition at line 610 of file qfontmetrics.cpp.

References d, glyph_metrics_t::height, layout, QString::length(), qRound(), glyph_metrics_t::width, glyph_metrics_t::x, and glyph_metrics_t::y.

00611 {
00612     if (text.length() == 0)
00613         return QRect();
00614 
00615     QTextEngine layout(text, d);
00616     layout.ignoreBidi = true;
00617     layout.itemize();
00618     glyph_metrics_t gm = layout.boundingBox(0, text.length());
00619     return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height));
00620 }

Here is the call graph for this function:

QRect QFontMetrics::boundingRect ( const QRect rect,
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 of the characters in the string specified by text, which is the set of pixels the text would cover if drawn at (0, 0). 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::AlignLeft aligns to the left border, except for Arabic and Hebrew where it aligns to the right. Qt::AlignRight aligns to the right border, except for Arabic and Hebrew where it aligns to the left. Qt::AlignJustify produces justified text. Qt::AlignHCenter aligns horizontally centered. Qt::AlignTop aligns to the top border. Qt::AlignBottom aligns to the bottom border. Qt::AlignVCenter aligns vertically centered Qt::AlignCenter (== {Qt::AlignHCenter | Qt::AlignVCenter}) Qt::TextSingleLine ignores newline characters in the text. Qt::TextExpandTabs expands tabs (see below) Qt::TextShowMnemonic interprets "&amp;x" as {x}, i.e. underlined. Qt::TextWordBreak breaks the text to fit the rectangle.

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

References d, qt_format_text(), Qt::TextDontPrint, and QRectF::toRect().

00710 {
00711     int tabArrayLen = 0;
00712     if (tabArray)
00713         while (tabArray[tabArrayLen])
00714             tabArrayLen++;
00715 
00716     QRectF rb;
00717     QRectF rr(rect);
00718     qt_format_text(QFont(d), rr, flags | Qt::TextDontPrint, text, &rb, tabStops, tabArray,
00719                    tabArrayLen, 0);
00720     return rb.toRect();
00721 }

Here is the call graph for this function:

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 79 of file qfontmetrics.h.

00081         { return boundingRect(QRect(x, y, w, h), flags, text, tabstops, tabarray); }

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

Returns the size in pixels of text.

The flags argument is the bitwise OR of the following flags: Qt::TextSingleLine ignores newline characters. Qt::TextExpandTabs expands tabs (see below) Qt::TextShowMnemonic interprets "&amp;x" as {x}, i.e. underlined. Qt::TextWordBreak breaks the text to fit the rectangle.

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()

Definition at line 746 of file qfontmetrics.cpp.

References boundingRect(), and QRect::size().

Referenced by qdesigner_internal::TabOrderEditor::indicatorRect(), QPushButton::sizeHint(), QToolBoxButton::sizeHint(), QToolButton::sizeHint(), QCommonStyle::subControlRect(), QTabBar::tabSizeHint(), qdesigner_internal::Connection::updatePixmap(), and Q3ListViewItem::width().

00747 {
00748     return boundingRect(QRect(0,0,0,0), flags, text, tabStops, tabArray).size();
00749 }

Here is the call graph for this function:

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

Since:
4.2
If the string text is wider than width, returns an elided version of the string (i.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 767 of file qfontmetrics.cpp.

References d, and QTextEngine::elidedText().

Referenced by QItemDelegate::drawDisplay(), QAbstractItemDelegate::elidedText(), QToolBoxButton::paintEvent(), QWorkspaceTitleBar::paintEvent(), and QHeaderView::paintSection().

00768 {
00769     QStackTextEngine engine(text, QFont(d));
00770     return engine.elidedText(mode, width, flags);
00771 }

Here is the call graph for this function:

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

References QUnicodeTables::Common, d, QFontPrivate::engineForScript(), qRound(), and QFontEngine::underlinePosition().

Referenced by qt_drawFontLining().

00780 {
00781     QFontEngine *engine = d->engineForScript(QUnicodeTables::Common);
00782     Q_ASSERT(engine != 0);
00783     return qRound(engine->underlinePosition());
00784 }

Here is the call graph for this function:

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

References ascent().

Referenced by qt_drawFontLining().

00793 {
00794     return ascent() + 1;
00795 }

Here is the call graph for this function:

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

References ascent().

Referenced by qt_drawFontLining().

00804 {
00805     int pos = ascent() / 3;
00806     return pos > 0 ? pos : 1;
00807 }

Here is the call graph for this function:

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

References QUnicodeTables::Common, d, QFontPrivate::engineForScript(), QFontEngine::lineThickness(), and qRound().

Referenced by qt_drawFontLining().

00816 {
00817     QFontEngine *engine = d->engineForScript(QUnicodeTables::Common);
00818     Q_ASSERT(engine != 0);
00819     return qRound(engine->lineThickness());
00820 }

Here is the call graph for this function:

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

References d.

00236 {
00237     return d == other.d;
00238 }

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 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 220 of file qfontmetrics.cpp.

References d.

00221 {
00222     return d == other.d;
00223 }

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 93 of file qfontmetrics.h.

References operator==().

00093 { return !operator==(other); } // 5.0 - remove me

Here is the call graph for this function:

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 94 of file qfontmetrics.h.

References operator==().

00094 { return !operator==(other); }

Here is the call graph for this function:


Friends And Related Function Documentation

friend class QFontMetricsF [friend]

Definition at line 109 of file qfontmetrics.h.

friend class QStackTextEngine [friend]

Definition at line 110 of file qfontmetrics.h.


Member Data Documentation

QFontPrivate* QFontMetrics::d [private]

Definition at line 112 of file qfontmetrics.h.

Referenced by ascent(), averageCharWidth(), boundingRect(), charWidth(), descent(), elidedText(), height(), inFont(), leading(), leftBearing(), lineSpacing(), lineWidth(), maxWidth(), minLeftBearing(), minRightBearing(), QFontMetricsF::operator=(), operator=(), operator==(), QFontMetrics(), rightBearing(), underlinePos(), width(), xHeight(), and ~QFontMetrics().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 17:39:36 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1