📄 qfontmetrics.cpp
字号:
int QFontMetrics::averageCharWidth() const{ QFontEngine *engine = d->engineForScript(QUnicodeTables::Common); Q_ASSERT(engine != 0); return qRound(engine->averageCharWidth());}/*! Returns true if character \a ch is a valid character in the font; otherwise returns false.*/bool QFontMetrics::inFont(QChar ch) const{ const int script = QUnicodeTables::script(ch); QFontEngine *engine = d->engineForScript(script); Q_ASSERT(engine != 0); if (engine->type() == QFontEngine::Box) return false; return engine->canRender(&ch, 1);}/*! Returns the left bearing of character \a 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. \sa rightBearing(), minLeftBearing(), width()*/int QFontMetrics::leftBearing(QChar ch) const{ const int script = QUnicodeTables::script(ch); QFontEngine *engine = d->engineForScript(script); Q_ASSERT(engine != 0); if (engine->type() == QFontEngine::Box) return 0; QGlyphLayout glyphs[10]; int nglyphs = 9; engine->stringToCMap(&ch, 1, glyphs, &nglyphs, 0); // ### can nglyphs != 1 happen at all? Not currently I think glyph_metrics_t gi = engine->boundingBox(glyphs[0].glyph); return qRound(gi.x);}/*! Returns the right bearing of character \a 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. \sa leftBearing(), minRightBearing(), width()*/int QFontMetrics::rightBearing(QChar ch) const{ const int script = QUnicodeTables::script(ch); QFontEngine *engine = d->engineForScript(script); Q_ASSERT(engine != 0); if (engine->type() == QFontEngine::Box) return 0; QGlyphLayout glyphs[10]; int nglyphs = 9; engine->stringToCMap(&ch, 1, glyphs, &nglyphs, 0); // ### can nglyphs != 1 happen at all? Not currently I think glyph_metrics_t gi = engine->boundingBox(glyphs[0].glyph); return qRound(gi.xoff - gi.x - gi.width);}/*! Returns the width in pixels of the first \a len characters of \a text. If \a len is negative (the default), the entire string is used. Note that this value is \e 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. \sa boundingRect()*/int QFontMetrics::width(const QString &text, int len) const{ if (len < 0) len = text.length(); if (len == 0) return 0; QTextEngine layout(text, d); layout.ignoreBidi = true; layout.itemize(); return qRound(layout.width(0, len));}/*! \overload \img bearings.png Bearings Returns the logical width of character \a ch in pixels. This is a distance appropriate for drawing a subsequent character after \a 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. \sa boundingRect(), charWidth()*/int QFontMetrics::width(QChar ch) const{ if (QChar::category(ch.unicode()) == QChar::Mark_NonSpacing) return 0; const int script = QUnicodeTables::script(ch); QFontEngine *engine = d->engineForScript(script); Q_ASSERT(engine != 0); QGlyphLayout glyphs[8]; int nglyphs = 7; engine->stringToCMap(&ch, 1, glyphs, &nglyphs, 0); return qRound(glyphs[0].advance.x);}/*! Returns the width of the character at position \a pos in the string \a 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.*/int QFontMetrics::charWidth(const QString &text, int pos) const{ if (pos < 0 || pos > (int)text.length()) return 0; const QChar &ch = text.unicode()[pos]; const int script = QUnicodeTables::script(ch); int width; if (script != QUnicodeTables::Common) { // complex script shaping. Have to do some hard work int from = qMax(0, pos - 8); int to = qMin(text.length(), pos + 8); QString cstr = QString::fromRawData(text.unicode() + from, to - from); QTextEngine layout(cstr, d); layout.ignoreBidi = true; layout.itemize(); width = qRound(layout.width(pos-from, 1)); } else if (QChar::category(ch.unicode()) == QChar::Mark_NonSpacing) { width = 0; } else { QFontEngine *engine = d->engineForScript(script); Q_ASSERT(engine != 0); QGlyphLayout glyphs[8]; int nglyphs = 7; engine->stringToCMap(&ch, 1, glyphs, &nglyphs, 0); width = qRound(glyphs[0].advance.x); } return width;}/*! Returns the bounding rectangle of the characters in the string specified by \a 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, \e not as linebreaks. The height of the bounding rectangle is at least as large as the value returned by height(). \sa width(), height(), QPainter::boundingRect(), tightBoundingRect()*/QRect QFontMetrics::boundingRect(const QString &text) const{ if (text.length() == 0) return QRect(); QTextEngine layout(text, d); layout.ignoreBidi = true; layout.itemize(); glyph_metrics_t gm = layout.boundingBox(0, text.length()); return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height));}/*! Returns the rectangle that is covered by ink if character \a ch were to be drawn at the origin of the coordinate system. Note that the bounding rectangle may extend to the left of (0, 0), e.g. for italicized fonts, and that the text output may cover \e 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. \sa width()*/QRect QFontMetrics::boundingRect(QChar ch) const{ const int script = QUnicodeTables::script(ch); QFontEngine *engine = d->engineForScript(script); Q_ASSERT(engine != 0); QGlyphLayout glyphs[10]; int nglyphs = 9; engine->stringToCMap(&ch, 1, glyphs, &nglyphs, 0); glyph_metrics_t gm = engine->boundingBox(glyphs[0].glyph); return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height));}/*! \overload Returns the bounding rectangle of the characters in the string specified by \a 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 \a rect. The \a flags argument is the bitwise OR of the following flags: \list \o Qt::AlignLeft aligns to the left border, except for Arabic and Hebrew where it aligns to the right. \o Qt::AlignRight aligns to the right border, except for Arabic and Hebrew where it aligns to the left. \o Qt::AlignJustify produces justified text. \o Qt::AlignHCenter aligns horizontally centered. \o Qt::AlignTop aligns to the top border. \o Qt::AlignBottom aligns to the bottom border. \o Qt::AlignVCenter aligns vertically centered \o Qt::AlignCenter (== \c{Qt::AlignHCenter | Qt::AlignVCenter}) \o Qt::TextSingleLine ignores newline characters in the text. \o Qt::TextExpandTabs expands tabs (see below) \o Qt::TextShowMnemonic interprets "&x" as \underline{x}, i.e. underlined. \o Qt::TextWordWrap breaks the text to fit the rectangle. \endlist 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 \a flags, then: if \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). 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 \e 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 \link minLeftBearing() maximum left \endlink and \link minRightBearing() right \endlink 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. \sa width(), QPainter::boundingRect(), Qt::Alignment*/QRect QFontMetrics::boundingRect(const QRect &rect, int flags, const QString &text, int tabStops, int *tabArray) const{ int tabArrayLen = 0; if (tabArray) while (tabArray[tabArrayLen]) tabArrayLen++; QRectF rb; QRectF rr(rect); qt_format_text(QFont(d), rr, flags | Qt::TextDontPrint, text, &rb, tabStops, tabArray, tabArrayLen, 0); return rb.toAlignedRect();}/*! Returns the size in pixels of \a text. The \a flags argument is the bitwise OR of the following flags: \list \o Qt::TextSingleLine ignores newline characters. \o Qt::TextExpandTabs expands tabs (see below) \o Qt::TextShowMnemonic interprets "&x" as \underline{x}, i.e. underlined. \o Qt::TextWordBreak breaks the text to fit the rectangle. \endlist If Qt::TextExpandTabs is set in \a flags, then: if \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). 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. \sa boundingRect()*/QSize QFontMetrics::size(int flags, const QString &text, int tabStops, int *tabArray) const{ return boundingRect(QRect(0,0,0,0), flags, text, tabStops, tabArray).size();}/*! \since 4.3 Returns a tight bounding rectangle around the characters in the string specified by \a 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, \e not as linebreaks. \warning Calling this method is very slow on Windows. \sa width(), height(), boundingRect()*/QRect QFontMetrics::tightBoundingRect(const QString &text) const{ if (text.length() == 0) return QRect(); QTextEngine layout(text, d); layout.ignoreBidi = true; layout.itemize(); glyph_metrics_t gm = layout.tightBoundingBox(0, text.length()); return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height));}/*! \since 4.2 If the string \a text is wider than \a width, returns an elided version of the string (i.e., a string with "..." in it). Otherwise, returns the original string. The \a 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 \a width is specified in pixels, not characters. The \a flags argument is optional and currently only supports Qt::TextShowMnemonic as value.*/QString QFontMetrics::elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags) const{ QStackTextEngine engine(text, QFont(d)); return engine.elidedText(mode, width, flags);}/*! Returns the distance from the base line to where an underscore should be drawn. \sa overlinePos(), strikeOutPos(), lineWidth()*/int QFontMetrics::underlinePos() const{ QFontEngine *engine = d->engineForScript(QUnicodeTables::Common); Q_ASSERT(engine != 0); return qRound(engine->underlinePosition());}/*!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -