QRawFont Class

QRawFont 提供对字体的单个物理实例访问. 更多...

头文件: #include <QRawFont>
qmake: QT += gui
开始支持版本: Qt 4.8

公有类型

enum AntialiasingType { PixelAntialiasing, SubPixelAntialiasing }
enum LayoutFlag { SeparateAdvances, KernedAdvances, UseDesignMetrics }
flags LayoutFlags

公有函数

QRawFont()
QRawFont(const QString &fileName, qreal pixelSize, QFont::HintingPreference hintingPreference = QFont::PreferDefaultHinting)
QRawFont(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference = QFont::PreferDefaultHinting)
QRawFont(const QRawFont &other)
~QRawFont()
QVector<QPointF> advancesForGlyphIndexes(const QVector<quint32> &glyphIndexes, LayoutFlags layoutFlags) const
QVector<QPointF> advancesForGlyphIndexes(const QVector<quint32> &glyphIndexes) const
bool advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs) const
bool advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs, LayoutFlags layoutFlags) const
QImage alphaMapForGlyph(quint32 glyphIndex, AntialiasingType antialiasingType = SubPixelAntialiasing, const QTransform &transform = QTransform()) const
qreal ascent() const
qreal averageCharWidth() const
QRectF boundingRect(quint32 glyphIndex) const
qreal capHeight() const
qreal descent() const
QString familyName() const
QByteArray fontTable(const char *tagName) const
bool glyphIndexesForChars(const QChar *chars, int numChars, quint32 *glyphIndexes, int *numGlyphs) const
QVector<quint32> glyphIndexesForString(const QString &text) const
QFont::HintingPreference hintingPreference() const
bool isValid() const
qreal leading() const
qreal lineThickness() const
void loadFromData(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference)
void loadFromFile(const QString &fileName, qreal pixelSize, QFont::HintingPreference hintingPreference)
qreal maxCharWidth() const
QPainterPath pathForGlyph(quint32 glyphIndex) const
qreal pixelSize() const
void setPixelSize(qreal pixelSize)
QFont::Style style() const
QString styleName() const
QList<QFontDatabase::WritingSystem> supportedWritingSystems() const
bool supportsCharacter(QChar character) const
bool supportsCharacter(uint ucs4) const
void swap(QRawFont &other)
qreal underlinePosition() const
qreal unitsPerEm() const
int weight() const
qreal xHeight() const
bool operator!=(const QRawFont &other) const
QRawFont &operator=(QRawFont &&other)
QRawFont &operator=(const QRawFont &other)
bool operator==(const QRawFont &other) const

静态公有成员

QRawFont fromFont(const QFont &font, QFontDatabase::WritingSystem writingSystem = QFontDatabase::Any)
uint qHash(const QRawFont &font, uint seed = 0)

详细描述

QRawFont 提供对字体的单个物理实例访问.

注意: QRawFont 是一个底级别类. 对于多数需求, QFont 是更适合的类.

最常见的情况, 用户界面显示文本时, 渲染字符的实际字体是未知的. 这类情况可能有几个原因: 例如, 目标系统的实际物理字体是开发人员无法预料的, 或者书写系统不支持所选文本的样式, 大小.

因此, Qt的 QFont 支持字体查询. 解释文本时, Qt 尽最大努力将文本与查询字体匹配, 但是根据支持情况, 后台可能使用不同的字体.

对于多数用例, 这是意料之中和必要的, 这样可最大限度地减少文本无法渲染的情况. 然而, 某些情况下, 对这一过程直接控制可能更加有用. QRawFont 就是针对这一情况而存在.

QRawFont 对象表示既定字体, 像素大小的单个物理实例. 即. 在典型情况下, 它表示一组TrueType或 OpenType字体表, 并使用用户指定的像素大小将度量转换为逻辑像素单位. 它可以与 QGlyphRun 结合使用, 在特定位置绘制特定的字形索引, 还可以访问一些物理字体中的相关数据.

QRawFont 仅提供对主要字体技术的支持: Windows系统的GDI和DirectWrite, Linux系统的FreeType, macOS的 CoreText. 对于其他字体后端, API将被禁用.

QRawFont 有多种构建方式:

  • 它可以调用QTextLayout::glyphs() 或 QTextFragment::glyphs()构建. 返回的QGlyphs对象包含 QRawFont对象, 这些对象表示渲染文本每部分的实际字体.
  • 它可以调用QRawFont::fromFont(), 参数是 QFont 对象. 这个函数返回一个 QRawFont 对象, 这个对象表示对QFont的查询结果, 当前系统的响应字体.
  • 它可以调用QRawFont构造函数, 参数是文件名或 QByteArray, 或者调用 loadFromFile() 或 loadFromData(). 这种情况下, 字体不会在 QFontDatabase中注册, 也不会作为常规字体选择的一部分使用.

QRawFont 在当前线程构造(不论是调用构造函数, 还是 loadFromData() 或 loadFromFile()). QRawFont 不能移动到其他线程, 必须在有问题的现场重新创建.

注意: 对于缓存静态文本的字形索引和字体选择, 避免在应用程序内部循环中重复计算字形和布局的方式是采用 QStaticText, 这个类会优化内存, 提供绘制引擎特定缓存, 提高性能.

成员类型

enum QRawFont::AntialiasingType

This enum represents the different ways a glyph can be rasterized in the function alphaMapForGlyph().

ConstantValueDescription
QRawFont::PixelAntialiasing0Will rasterize by measuring the coverage of the shape on whole pixels. The returned image contains the alpha values of each pixel based on the coverage of the glyph shape.
QRawFont::SubPixelAntialiasing1Will rasterize by measuring the coverage of each subpixel, returning a separate alpha value for each of the red, green and blue components of each pixel.

enum QRawFont::LayoutFlag
flags QRawFont::LayoutFlags

This enum tells the function advancesForGlyphIndexes() how to calculate the advances.

ConstantValueDescription
QRawFont::SeparateAdvances0Will calculate the advance for each glyph separately.
QRawFont::KernedAdvances1Will apply kerning between adjacent glyphs. Note that OpenType GPOS based kerning is currently not supported.
QRawFont::UseDesignMetrics2Use design metrics instead of hinted metrics adjusted to the resolution of the paint device. Can be OR-ed with any of the options above.

This enum was introduced or modified in Qt 5.1.

The LayoutFlags type is a typedef for QFlags<LayoutFlag>. It stores an OR combination of LayoutFlag values.

成员函数

QRawFont::QRawFont()

Constructs an invalid QRawFont.

QRawFont::QRawFont(const QString &fileName, qreal pixelSize, QFont::HintingPreference hintingPreference = QFont::PreferDefaultHinting)

Constructs a QRawFont representing the font contained in the file referenced by fileName for the size (in pixels) given by pixelSize, and using the hinting preference specified by hintingPreference.

Note: The referenced file must contain a TrueType or OpenType font.

QRawFont::QRawFont(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference = QFont::PreferDefaultHinting)

Constructs a QRawFont representing the font contained in the supplied fontData for the size (in pixels) given by pixelSize, and using the hinting preference specified by hintingPreference.

Note: The data must contain a TrueType or OpenType font.

QRawFont::QRawFont(const QRawFont &other)

Creates a QRawFont which is a copy of other.

QRawFont::~QRawFont()

Destroys the QRawFont

QVector<QPointF> QRawFont::advancesForGlyphIndexes(const QVector<quint32> &glyphIndexes, LayoutFlags layoutFlags) const

Returns the QRawFont's advances for each of the glyphIndexes in pixel units. The advances give the distance from the position of a given glyph to where the next glyph should be drawn to make it appear as if the two glyphs are unspaced. How the advances are calculated is controlled by layoutFlags.

This function was introduced in Qt 5.1.

参见 QTextLine::horizontalAdvance() and QFontMetricsF::width().

QVector<QPointF> QRawFont::advancesForGlyphIndexes(const QVector<quint32> &glyphIndexes) const

This is an overloaded function.

Returns the QRawFont's advances for each of the glyphIndexes in pixel units. The advances give the distance from the position of a given glyph to where the next glyph should be drawn to make it appear as if the two glyphs are unspaced. The advance of each glyph is calculated separately.

参见 QTextLine::horizontalAdvance() and QFontMetricsF::width().

bool QRawFont::advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs) const

This is an overloaded function.

Returns the QRawFont's advances for each of the glyphIndexes in pixel units. The advances give the distance from the position of a given glyph to where the next glyph should be drawn to make it appear as if the two glyphs are unspaced. The glyph indexes are given with the array glyphIndexes while the results are returned through advances, both of them must have numGlyphs elements. The advance of each glyph is calculated separately

参见 QTextLine::horizontalAdvance() and QFontMetricsF::width().

bool QRawFont::advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs, LayoutFlags layoutFlags) const

Returns the QRawFont's advances for each of the glyphIndexes in pixel units. The advances give the distance from the position of a given glyph to where the next glyph should be drawn to make it appear as if the two glyphs are unspaced. The glyph indexes are given with the array glyphIndexes while the results are returned through advances, both of them must have numGlyphs elements. How the advances are calculated is controlled by layoutFlags.

This function was introduced in Qt 5.1.

参见 QTextLine::horizontalAdvance() and QFontMetricsF::width().

QImage QRawFont::alphaMapForGlyph(quint32 glyphIndex, AntialiasingType antialiasingType = SubPixelAntialiasing, const QTransform &transform = QTransform()) const

This function returns a rasterized image of the glyph at the given glyphIndex in the underlying font, using the transform specified. If the QRawFont is not valid, this function will return an invalid QImage.

If the font is a color font, then the resulting image will contain the rendered glyph at the current pixel size. In this case, the antialiasingType will be ignored.

Otherwise, if antialiasingType is set to QRawFont::SubPixelAntialiasing, then the resulting image will be in QImage::Format_RGB32 and the RGB values of each pixel will represent the subpixel opacities of the pixel in the rasterization of the glyph. Otherwise, the image will be in the format of QImage::Format_Indexed8 and each pixel will contain the opacity of the pixel in the rasterization.

参见 pathForGlyph() and QPainter::drawGlyphRun().

qreal QRawFont::ascent() const

Returns the ascent of this QRawFont in pixel units.

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.

参见 QFontMetricsF::ascent().

qreal QRawFont::averageCharWidth() const

Returns the average character width of this QRawFont in pixel units.

参见 QFontMetricsF::averageCharWidth().

QRectF QRawFont::boundingRect(quint32 glyphIndex) const

Returns the smallest rectangle containing the glyph with the given glyphIndex.

This function was introduced in Qt 5.0.

qreal QRawFont::capHeight() const

Returns the cap height of this QRawFont in pixel units.

The cap height of a font is the height of a capital letter above the baseline. It specifically is the height of capital letters that are flat - such as H or I - as opposed to round letters such as O, or pointed letters like A, both of which may display overshoot.

This function was introduced in Qt 5.8.

参见 QFontMetricsF::capHeight().

qreal QRawFont::descent() const

Returns the descent of this QRawFont in pixel units.

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.

参见 QFontMetricsF::descent().

QString QRawFont::familyName() const

Returns the family name of this QRawFont.

QByteArray QRawFont::fontTable(const char *tagName) const

Retrieves the sfnt table named tagName from the underlying physical font, or an empty byte array if no such table was found. The returned font table's byte order is Big Endian, like the sfnt format specifies. The tagName must be four characters long and should be formatted in the default endianness of the current platform.

[static] QRawFont QRawFont::fromFont(const QFont &font, QFontDatabase::WritingSystem writingSystem = QFontDatabase::Any)

Fetches the physical representation based on a font query. The physical font returned is the font that will be preferred by Qt in order to display text in the selected writingSystem.

Warning: This function is potentially expensive and should not be called in performance sensitive code.

bool QRawFont::glyphIndexesForChars(const QChar *chars, int numChars, quint32 *glyphIndexes, int *numGlyphs) const

Converts a string of unicode points to glyph indexes using the CMAP table in the underlying font. The function works like glyphIndexesForString() except it take an array (chars), the results will be returned though glyphIndexes array and number of glyphs will be set in numGlyphs. The size of glyphIndexes array must be at least numChars, if that's still not enough, this function will return false, then you can resize glyphIndexes from the size returned in numGlyphs.

参见 glyphIndexesForString(), advancesForGlyphIndexes(), QGlyphRun, QTextLayout::glyphRuns(), and QTextFragment::glyphRuns().

QVector<quint32> QRawFont::glyphIndexesForString(const QString &text) const

Converts the string of unicode points given by text to glyph indexes using the CMAP table in the underlying font, and returns a vector containing the result.

Note that, in cases where there are other tables in the font that affect the shaping of the text, the returned glyph indexes will not correctly represent the rendering of the text. To get the correctly shaped text, you can use QTextLayout to lay out and shape the text, then call QTextLayout::glyphs() to get the set of glyph index list and QRawFont pairs.

参见 advancesForGlyphIndexes(), glyphIndexesForChars(), QGlyphRun, QTextLayout::glyphRuns(), and QTextFragment::glyphRuns().

QFont::HintingPreference QRawFont::hintingPreference() const

Returns the hinting preference used to construct this QRawFont.

参见 QFont::hintingPreference().

bool QRawFont::isValid() const

Returns true if the QRawFont is valid and false otherwise.

qreal QRawFont::leading() const

Returns the leading of this QRawFont in pixel units.

This is the natural inter-line spacing.

参见 QFontMetricsF::leading().

qreal QRawFont::lineThickness() const

Returns the thickness for drawing lines (underline, overline, etc.) along with text drawn in this font.

void QRawFont::loadFromData(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference)

Replaces the current QRawFont with the font contained in the supplied fontData for the size (in pixels) given by pixelSize, and using the hinting preference specified by hintingPreference.

The fontData must contain a TrueType or OpenType font.

参见 loadFromFile().

void QRawFont::loadFromFile(const QString &fileName, qreal pixelSize, QFont::HintingPreference hintingPreference)

Replaces the current QRawFont with the contents of the file referenced by fileName for the size (in pixels) given by pixelSize, and using the hinting preference specified by hintingPreference.

The file must reference a TrueType or OpenType font.

参见 loadFromData().

qreal QRawFont::maxCharWidth() const

Returns the width of the widest character in the font.

参见 QFontMetricsF::maxWidth().

QPainterPath QRawFont::pathForGlyph(quint32 glyphIndex) const

This function returns the shape of the glyph at a given glyphIndex in the underlying font if the QRawFont is valid. Otherwise, it returns an empty QPainterPath.

The returned glyph will always be unhinted.

参见 alphaMapForGlyph() and QPainterPath::addText().

qreal QRawFont::pixelSize() const

Returns the pixel size set for this QRawFont. The pixel size affects how glyphs are rasterized, the size of glyphs returned by pathForGlyph(), and is used to convert internal metrics from design units to logical pixel units.

参见 setPixelSize().

void QRawFont::setPixelSize(qreal pixelSize)

Sets the pixel size with which this font should be rendered to pixelSize.

参见 pixelSize().

QFont::Style QRawFont::style() const

Returns the style of this QRawFont.

参见 QFont::style().

QString QRawFont::styleName() const

Returns the style name of this QRawFont.

参见 QFont::styleName().

QList<QFontDatabase::WritingSystem> QRawFont::supportedWritingSystems() const

Returns a list of writing systems supported by the font according to designer supplied information in the font file. Please note that this does not guarantee support for a specific unicode point in the font. You can use the supportsCharacter() to check support for a single, specific character.

Note: The list is determined based on the unicode ranges and codepage ranges set in the font's OS/2 table and requires such a table to be present in the underlying font file.

参见 supportsCharacter().

bool QRawFont::supportsCharacter(QChar character) const

Returns true if the font has a glyph that corresponds to the given character.

参见 supportedWritingSystems().

bool QRawFont::supportsCharacter(uint ucs4) const

This is an overloaded function.

Returns true if the font has a glyph that corresponds to the UCS-4 encoded character ucs4.

参见 supportedWritingSystems().

void QRawFont::swap(QRawFont &other)

Swaps this raw font with other. This function is very fast and never fails.

This function was introduced in Qt 5.0.

qreal QRawFont::underlinePosition() const

Returns the position from baseline for drawing underlines below the text rendered with this font.

qreal QRawFont::unitsPerEm() const

Returns the number of design units define the width and height of the em square for this QRawFont. This value is used together with the pixel size when converting design metrics to pixel units, as the internal metrics are specified in design units and the pixel size gives the size of 1 em in pixels.

参见 pixelSize() and setPixelSize().

int QRawFont::weight() const

Returns the weight of this QRawFont.

参见 QFont::weight().

qreal QRawFont::xHeight() const

Returns the xHeight of this QRawFont in pixel units.

This is often but not always the same as the height of the character 'x'.

参见 QFontMetricsF::xHeight().

bool QRawFont::operator!=(const QRawFont &other) const

Returns true if this QRawFont is not equal to other. Otherwise, returns false.

QRawFont &QRawFont::operator=(QRawFont &&other)

Move-assignment operator.

QRawFont &QRawFont::operator=(const QRawFont &other)

Assigns other to this QRawFont.

bool QRawFont::operator==(const QRawFont &other) const

Returns true if this QRawFont is equal to other. Otherwise, returns false.

相关非成员

uint qHash(const QRawFont &font, uint seed = 0)

Returns the hash value for font. If specified, seed is used to initialize the hash.

This function was introduced in Qt 5.8.