📄 qfontmetrics.cpp
字号:
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());}/*! 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);}/*! \fn int QFontMetrics::leftBearing(QChar ch) const 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);}/*! \fn int QFontMetrics::rightBearing(QChar ch) const 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 str. 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 &str, int len) const{ if (len < 0) len = str.length(); if (len == 0) return 0; QTextEngine layout(str, d); layout.ignoreBidi = true; layout.itemize(); return qRound(layout.width(0, len));}/*! \fn int QFontMetrics::width(QChar ch) const \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 (::category(ch) == 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 str. 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 &str, int pos) const{ if (pos < 0 || pos > (int)str.length()) return 0; const QChar &ch = str.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((int)str.length(), pos + 8); QString cstr = QString::fromRawData(str.unicode()+from, to-from); QTextEngine layout(cstr, d); layout.ignoreBidi = true; layout.itemize(); width = qRound(layout.width(pos-from, 1)); } else if (::category(ch) == 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 str, which is 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 text output may cover \e all pixels in the bounding rectangle. Newline characters are processed as normal characters, \e not as linebreaks. Due to the different actual character heights, the height of the bounding rectangle of e.g. "Yes" and "yes" may be different. \sa width(), QPainter::boundingRect()*/QRect QFontMetrics::boundingRect(const QString &str) const{ if (str.length() == 0) return QRect(); QTextEngine layout(str, d); layout.ignoreBidi = true; layout.itemize(); glyph_metrics_t gm = layout.boundingBox(0, str.length()); return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height));}/*! Returns the rectangle that is covered by ink if the character 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 \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 str, 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 r. The \a flgs argument is the bitwise OR of the following flags: \list \i Qt::AlignLeft aligns to the left border, except for Arabic and Hebrew where it aligns to the right. \i Qt::AlignRight aligns to the right border, except for Arabic and Hebrew where it aligns to the left. \i Qt::AlignJustify produces justified text. \i Qt::AlignHCenter aligns horizontally centered. \i Qt::AlignTop aligns to the top border. \i Qt::AlignBottom aligns to the bottom border. \i Qt::AlignVCenter aligns vertically centered \i Qt::AlignCenter (== \c{Qt::AlignHCenter | Qt::AlignVCenter}) \i Qt::TextSingleLine ignores newline characters in the text. \i Qt::TextExpandTabs expands tabs (see below) \i Qt::TextShowMnemonic interprets "&x" as \underline{x}, i.e. underlined. \i Qt::TextWordBreak 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. These flags are defined in \c qnamespace.h. If Qt::TextExpandTabs is set in \a flgs, 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 &r, int flgs, const QString& str, int tabstops, int *tabarray) const{ int tabarraylen=0; if (tabarray) while (tabarray[tabarraylen]) tabarraylen++; QRectF rb; QRectF rr(r); qt_format_text(QFont(d), rr, flgs|Qt::TextDontPrint, str, &rb, tabstops, tabarray, tabarraylen, 0); return rb.toRect();}/*! Returns the size in pixels of \a text. The \a flgs argument is the bitwise OR of the following flags: \list \i Qt::TextSingleLine ignores newline characters. \i Qt::TextExpandTabs expands tabs (see below) \i Qt::TextShowMnemonic interprets "&x" as \underline{x}, i.e. underlined. \i Qt::TextWordBreak breaks the text to fit the rectangle. \endlist These flags are defined in \c qnamespace.h. If Qt::TextExpandTabs is set in \a flgs, 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 flgs, const QString &text, int tabstops, int *tabarray) const{ return boundingRect(QRect(0,0,0,0), flgs, text, tabstops, tabarray).size();}/*! 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());}/*! Returns the distance from the base line to where an overline should be drawn.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -