📄 qfontmetrics.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qfont.h"#include "qpaintdevice.h"#include "qfontmetrics.h"#include "qfont_p.h"#include "qfontengine_p.h"#include <private/qunicodetables_p.h>#include <math.h>#ifdef Q_WS_X11#include "qx11info_x11.h"extern const QX11Info *qt_x11Info(const QPaintDevice *pd);#endifextern 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);extern int qt_defaultDpi();/***************************************************************************** QFontMetrics member functions *****************************************************************************//*! \class QFontMetrics \brief The QFontMetrics class provides font metrics information. \ingroup multimedia \ingroup shared \ingroup text QFontMetrics functions calculate the size of characters and strings for a given font. There are three ways you can create a QFontMetrics object: \list 1 \o 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 \e 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.) \o 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 \e not updated. \o 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 \e not updated. \endlist 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: \code QFont font("times", 24); QFontMetrics fm(font); int pixelsWide = fm.width("What's the width of this text?"); int pixelsHigh = fm.height(); \endcode \sa QFont, QFontInfo, QFontDatabase, QFontComboBox, {Character Map Example}*//*! \fn QRect QFontMetrics::boundingRect(int x, int y, int width, int height, int flags, const QString &text, int tabStops, int *tabArray) const \overload Returns the bounding rectangle for the given \a text within the rectangle specified by the \a x and \a y coordinates, \a width, and \a height. If Qt::TextExpandTabs is set in \a flags and \a tabArray is non-null, it specifies a 0-terminated sequence of pixel-positions for tabs; otherwise, if \a tabStops is non-zero, it is used as the tab spacing (in pixels).*//*! Constructs a font metrics object for \a font. The font metrics will be compatible with the paintdevice used to create \a 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.*/QFontMetrics::QFontMetrics(const QFont &font) : d(font.d){ d->ref.ref();}/*! Constructs a font metrics object for \a font and \a paintdevice. The font metrics will be compatible with the paintdevice passed. If the \a paintdevice is 0, the metrics will be screen-compatible, ie. the metrics you get if you use the font for drawing text on a \link QWidget widgets\endlink or \link QPixmap pixmaps\endlink, 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.*/QFontMetrics::QFontMetrics(const QFont &font, QPaintDevice *paintdevice){ int dpi = paintdevice ? paintdevice->logicalDpiY() : qt_defaultDpi();#ifdef Q_WS_X11 const QX11Info *info = qt_x11Info(paintdevice); int screen = info ? info->screen() : 0;#else const int screen = 0;#endif if (font.d->dpi != dpi || font.d->screen != screen ) { d = new QFontPrivate(*font.d); d->dpi = dpi; d->screen = screen; } else { d = font.d; d->ref.ref(); }}/*! Constructs a copy of \a fm.*/QFontMetrics::QFontMetrics(const QFontMetrics &fm) : d(fm.d){ d->ref.ref(); }/*! Destroys the font metrics object and frees all allocated resources.*/QFontMetrics::~QFontMetrics(){ if (!d->ref.deref()) delete d;}/*! Assigns the font metrics \a fm.*/QFontMetrics &QFontMetrics::operator=(const QFontMetrics &fm){ qAtomicAssign(d, fm.d); return *this;}/*! \overload Returns true if \a 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. \sa operator!=()*/bool QFontMetrics::operator ==(const QFontMetrics &other) const{ return d == other.d;}/*! Returns true if \a 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. \sa operator!=()*/bool QFontMetrics::operator ==(const QFontMetrics &other){ return d == other.d;}/*! \fn bool QFontMetrics::operator!=(const QFontMetrics &other) Returns true if \a 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. \sa operator==()*//*! \fn bool QFontMetrics::operator !=(const QFontMetrics &other) const Returns true if \a 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. \sa operator==()*//*! 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. \sa descent()*/int QFontMetrics::ascent() const{ QFontEngine *engine = d->engineForScript(QUnicodeTables::Common); Q_ASSERT(engine != 0); return qRound(engine->ascent());}/*! 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. \sa ascent()*/int QFontMetrics::descent() const{ QFontEngine *engine = d->engineForScript(QUnicodeTables::Common); Q_ASSERT(engine != 0); return qRound(engine->descent());}/*! Returns the height of the font. This is always equal to ascent()+descent()+1 (the 1 is for the base line). \sa leading(), lineSpacing()*/int QFontMetrics::height() const{ QFontEngine *engine = d->engineForScript(QUnicodeTables::Common); Q_ASSERT(engine != 0); return qRound(engine->ascent() + engine->descent()) + 1;}/*! Returns the leading of the font. This is the natural inter-line spacing. \sa height(), lineSpacing()*/int QFontMetrics::leading() const{ QFontEngine *engine = d->engineForScript(QUnicodeTables::Common); Q_ASSERT(engine != 0); return qRound(engine->leading());}/*! Returns the distance from one base line to the next. This value is always equal to leading()+height(). \sa height(), leading()*/int QFontMetrics::lineSpacing() const{ QFontEngine *engine = d->engineForScript(QUnicodeTables::Common); Q_ASSERT(engine != 0); return qRound(engine->leading() + engine->ascent() + engine->descent()) + 1;}/*! 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. \sa minRightBearing(), leftBearing()*/int QFontMetrics::minLeftBearing() const{ QFontEngine *engine = d->engineForScript(QUnicodeTables::Common); Q_ASSERT(engine != 0); return qRound(engine->minLeftBearing());}/*! 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. \sa minLeftBearing(), rightBearing()*/int QFontMetrics::minRightBearing() const{ QFontEngine *engine = d->engineForScript(QUnicodeTables::Common); Q_ASSERT(engine != 0); return qRound(engine->minRightBearing());}/*! Returns the width of the widest character in the font.*/int QFontMetrics::maxWidth() const{ QFontEngine *engine = d->engineForScript(QUnicodeTables::Common); Q_ASSERT(engine != 0); return qRound(engine->maxCharWidth());}/*! Returns the 'x' height of the font. This is often but not always the same as the height of the character 'x'.*/int QFontMetrics::xHeight() const{ QFontEngine *engine = d->engineForScript(QUnicodeTables::Common); Q_ASSERT(engine != 0); return qRound(engine->xHeight());}/*! \since 4.2 Returns the average width of glyphs in the font.*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -