本页内容

QRawFont Class

QRawFont 类用于访问字体的单个物理实例。更多内容...

标题: #include <QRawFont>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui

公共类型

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

公共函数

QRawFont()
QRawFont(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference = QFont::PreferDefaultHinting)
QRawFont(const QString &fileName, qreal pixelSize, QFont::HintingPreference hintingPreference = QFont::PreferDefaultHinting)
QRawFont(const QRawFont &other)
~QRawFont()
QList<QPointF> advancesForGlyphIndexes(const QList<quint32> &glyphIndexes, QRawFont::LayoutFlags layoutFlags) const
bool advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs, QRawFont::LayoutFlags layoutFlags) const
QList<QPointF> advancesForGlyphIndexes(const QList<quint32> &glyphIndexes) const
bool advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs) const
QImage alphaMapForGlyph(quint32 glyphIndex, QRawFont::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
(since 6.7) QByteArray fontTable(QFont::Tag tag) const
QByteArray fontTable(const char *tag) const
(since 6.11) quint32 glyphCount() const
bool glyphIndexesForChars(const QChar *chars, int numChars, quint32 *glyphIndexes, int *numGlyphs) const
QList<quint32> glyphIndexesForString(const QString &text) const
(since 6.11) QString glyphName(quint32 glyphIndex) 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=(const QRawFont &other)
bool operator==(const QRawFont &other) const

静态公共成员

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

详细说明

注意: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 对象,这些对象代表用于渲染文本各部分的实际字体。
  • 可以通过将QFont 对象传递给QRawFont::fromFont()来创建。该函数将返回一个QRawFont对象,该对象代表将作为对QFont 查询的响应以及根据所选书写系统而选定的字体。
  • 可以通过将文件名或QByteArray 直接传递给QRawFont构造函数,或通过调用loadFromFile()或loadFromData()来创建该对象。在这种情况下,该字体不会注册到QFontDatabase 中,也不会作为常规字体选择的一部分提供。

QRawFont 被视为其构造线程(无论是通过构造函数,还是通过调用loadFromData() 或loadFromFile())的本地对象。该 QRawFont 无法移动到其他线程,而必须在该线程中重新创建。

注意:若需 为静态文本缓存字形索引和字体选择,以避免在应用程序的内部循环中重新调整形状和布局,则QStaticText 类是更佳的选择,因为它优化了缓存的内存开销,并支持针对绘制引擎的专用缓存以进一步提升速度。

成员类型文档

enum QRawFont::AntialiasingType

该枚举表示在函数alphaMapForGlyph()中,字形可以被栅格化的不同方式。

常量值描述
QRawFont::PixelAntialiasing0将通过测量形状在整个像素上的覆盖范围来进行光栅化。返回的图像包含基于字形形状覆盖范围的每个像素的 alpha 值。
QRawFont::SubPixelAntialiasing1将通过测量每个子像素的覆盖范围来进行光栅化,并为每个像素的红、绿、蓝三原色分别返回一个独立的透明度值。

enum QRawFont::LayoutFlag
flags QRawFont::LayoutFlags

此枚举用于告知函数advancesForGlyphIndexes() 如何计算步进量。

常量值描述
QRawFont::SeparateAdvances0将分别计算每个字形的间距。
QRawFont::KernedAdvances1将在相邻字形之间应用字距调整。请注意,目前不支持基于 OpenType GPOS 的字距调整。
QRawFont::UseDesignMetrics2使用设计度量值,而不是根据绘制设备的分辨率调整后的提示度量值。可与上述任何选项进行“或”运算。

LayoutFlags 类型是QFlags<LayoutFlag> 的 typedef。它存储 LayoutFlag 值的按“或”运算组合。

成员函数文档

QRawFont::QRawFont()

构建了一个无效的 QRawFont。

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

构建一个 QRawFont 对象,该对象表示所提供的fontData 中包含的字体,其大小(以像素为单位)由pixelSize 指定,并使用由hintingPreference 指定的提示首选项。

注意:数据中 必须包含 TrueType 或 OpenType 字体。

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

创建一个 QRawFont 对象,该对象表示由fileName 引用的文件中所包含的字体,其大小(以像素为单位)由pixelSize 指定,并使用由hintingPreference 指定的提示首选项。

注意:所 引用的文件 必须包含 TrueType 或 OpenType 字体。

QRawFont::QRawFont(const QRawFont &other)

创建一个 QRawFont 对象,该对象是other 的副本。

[noexcept] QRawFont::~QRawFont()

摧毁了QRawFont

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

返回QRawFont 中每个glyphIndexes 的间距值(单位为像素)。这些间距值表示从给定字形的位置到下一个字形应绘制位置的距离,以使两个字形看起来没有间隙。间距值的计算方式由layoutFlags 控制。

注意:当 请求KernedAdvances 时 ,如果字体中包含KERN 表,本函数将应用该表中的字距调整规则。在许多现代字体中,字距调整通过OpenType规则或AAT规则处理,这需要应用完整的塑形步骤。要获得对文本进行完整塑形后的结果,请使用QTextLayout 。

另请参阅 QTextLine::horizontalAdvance()、QFontMetricsF::horizontalAdvance() 和QTextLayout::glyphRuns()。

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

返回QRawFont 中每个glyphIndexes 的间距(单位为像素)。这些间距表示从给定字形的位置到下一个字形应绘制位置的距离,以确保两个字形看起来没有间隔。 字形索引通过数组glyphIndexes 提供,而结果则通过advances 返回,两者都必须包含numGlyphs 个元素。间距的计算方式由layoutFlags 控制。

注意:当 请求KernedAdvances 时, 如果字体中包含KERN 表,本函数将应用其中的字距调整规则。在许多现代字体中,字距调整是通过OpenType规则或AAT规则处理的,这需要应用完整的塑形步骤。要获取文本完全塑形后的结果,请使用QTextLayout 。

另请参阅 QTextLine::horizontalAdvance()、QFontMetricsF::horizontalAdvance() 和QTextLayout::glyphRuns()。

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

返回QRawFont 中每个glyphIndexes 的间距(单位为像素)。这些间距表示从给定字形的位置到下一个字形应绘制位置的距离,以确保这两个字形看起来没有间隙。每个字形的间距均单独计算。

这是一个重载函数。

另请参阅 QTextLine::horizontalAdvance() 和QFontMetricsF::horizontalAdvance()。

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

返回QRawFont 中每个glyphIndexes 的间距值,单位为像素。这些间距值表示从给定字形的位置到下一个字形应绘制位置的距离,以使这两个字形看起来没有间距。 字形索引通过数组glyphIndexes 提供,而结果通过advances 返回,两者都必须包含numGlyphs 个元素。每个字形的间距均单独计算

这是一个重载函数。

另请参阅 QTextLine::horizontalAdvance() 和QFontMetricsF::horizontalAdvance()。

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

该函数使用指定的transform ,返回底层字体中位于给定glyphIndex 处的字形栅格化图像。如果QRawFont 无效,该函数将返回一个无效的QImage 。

如果该字体是彩色字体,则生成的图像将包含以当前像素大小渲染的字形。在这种情况下,antialiasingType 将被忽略。

否则,如果antialiasingType 设置为QRawFont::SubPixelAntialiasing ,则生成的图像将采用QImage::Format_RGB32 格式,且每个像素的 RGB 值将代表字形光栅化过程中该像素的亚像素不透明度。否则,图像将采用QImage::Format_Indexed8 格式,且每个像素将包含光栅化过程中该像素的不透明度。

另请参阅 pathForGlyph() 和QPainter::drawGlyphRun()。

qreal QRawFont::ascent() const

返回该QRawFont 的升高值(以像素为单位)。

字体的升高是指从基线到字符延伸到的最高位置之间的距离。实际上,有些字体设计师会打破这一规则,例如在字符上方添加多个重音符号,或者为了适应某种特殊语言中的非常规字符,因此该值可能(尽管很少见)会过小。

另请参阅 QFontMetricsF::ascent()。

qreal QRawFont::averageCharWidth() const

返回该QRawFont 的平均字符宽度(单位为像素)。

另请参阅 QFontMetricsF::averageCharWidth()。

QRectF QRawFont::boundingRect(quint32 glyphIndex) const

返回包含具有给定glyphIndex 的字形的最小的矩形。

qreal QRawFont::capHeight() const

返回此QRawFont 的字母高度,单位为像素。

字体的“大写字母高度”是指大写字母高于基线的高度。具体而言,它指的是平顶大写字母(如 H 或 I)的高度,而非圆顶字母(如 O)或尖顶字母(如 A)的高度——后两者在显示时可能会出现超出基线的情况。

另请参阅 QFontMetricsF::capHeight()。

qreal QRawFont::descent() const

返回该QRawFont 的下伸高度(单位为像素)。

下伸高度是指从基线到字符延伸到的最低点的距离。实际上,有些字体设计师会打破这一规则,例如为了适应某种罕见语言中的特殊字符,因此该值可能(尽管很少见)过小。

另请参阅 QFontMetricsF::descent()。

QString QRawFont::familyName() const

返回此QRawFont 的家族名称。

[since 6.7] QByteArray QRawFont::fontTable(QFont::Tag tag) const

从底层物理字体中检索由tag 指定的sfnt表;若未找到该表,则返回一个空字节数组。返回的字体表的字节序为大端序,这与sfnt格式的规定一致。

该函数于 Qt 6.7 中引入。

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

名称必须是一个由四个字符组成的字符串。

该函数重载了fontTable (QFont::Tag)。

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

根据font 查询获取字体的物理表示形式。返回的物理字体是Qt在所选writingSystem 中显示文本时优先使用的字体。

警告:此 函数可能消耗大量资源,不应在对性能要求较高的代码中调用。

[since 6.11] quint32 QRawFont::glyphCount() const

返回该QRawFont 中的字形数量。

该函数于 Qt 6.11 中引入。

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

使用底层字体中的 CMAP 表,将一串 Unicode 点转换为字形索引。 该函数的工作原理与glyphIndexesForString()类似,不同之处在于它接受一个数组(chars ),结果将通过glyphIndexes 数组返回,且字形数量将设置在numGlyphs 中。glyphIndexes 数组的大小必须至少为numChars ,如果仍不足,该函数将返回false,此时您可以根据numGlyphs 中返回的大小调整glyphIndexes 的大小。

另请参阅 glyphIndexesForString(),advancesForGlyphIndexes(),QGlyphRun,QTextLayout::glyphRuns() 以及QTextFragment::glyphRuns()。

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

使用底层字体中的 CMAP 表,将text 返回的 Unicode 点字符串转换为字形索引,并返回一个包含结果的列表。

请注意,如果字体中存在其他会影响文本造型的表格,则返回的字形索引将无法正确反映文本的渲染效果。 要获得形状正确的文本,您可以使用QTextLayout 来布局和调整文本形状,然后调用 QTextLayout::glyphs() 来获取字形索引列表和QRawFont 对的集合。

另请参阅 advancesForGlyphIndexes()、glyphIndexesForChars()、QGlyphRun 、QTextLayout::glyphRuns() 以及QTextFragment::glyphRuns()。

[since 6.11] QString QRawFont::glyphName(quint32 glyphIndex) const

返回给定glyphIndex 的名称。

如果该字形在字体中没有显式名称,则会根据其字形索引合成一个名称。

该函数于 Qt 6.11 中引入。

QFont::HintingPreference QRawFont::hintingPreference() const

返回用于构建此QRawFont 的提示偏好设置。

另请参阅 QFont::hintingPreference()。

bool QRawFont::isValid() const

如果QRawFont 有效,则返回true ;否则返回false。

qreal QRawFont::leading() const

返回此QRawFont 的行首间距,单位为像素。

这是自然的行间距。

另请参阅 QFontMetricsF::leading()。

qreal QRawFont::lineThickness() const

返回使用此字体绘制文本时,其附属线条(下划线、上划线等)的粗细。

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

将当前的QRawFont 替换为fontData 中包含的字体,字号(以像素为单位)由pixelSize 指定,并使用由hintingPreference 指定的提示偏好设置。

fontData 必须包含 TrueType 或 OpenType 字体。

另请参阅 loadFromFile()。

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

将当前的QRawFont 替换为fileName 所引用的文件内容,尺寸(以像素为单位)由pixelSize 指定,并使用hintingPreference 指定的提示首选项。

该文件必须指向一种 TrueType 或 OpenType 字体。

另请参阅 loadFromData()。

qreal QRawFont::maxCharWidth() const

返回字体中宽度最大的字符的宽度。

另请参阅 QFontMetricsF::maxWidth()。

QPainterPath QRawFont::pathForGlyph(quint32 glyphIndex) const

如果QRawFont 有效,则该函数返回底层字体中给定glyphIndex 处字形的形状;否则,它将返回一个空的QPainterPath 。

返回的字形始终未经过提示处理。

另请参阅 alphaMapForGlyph() 和QPainterPath::addText()。

qreal QRawFont::pixelSize() const

返回为该QRawFont 设置的像素大小。像素大小会影响字形如何被栅格化,以及pathForGlyph()返回的字形大小,并用于将内部度量单位从设计单位转换为逻辑像素单位。

另请参阅 setPixelSize()。

void QRawFont::setPixelSize(qreal pixelSize)

将此字体的渲染像素大小设置为pixelSize 。

另请参阅 pixelSize()。

QFont::Style QRawFont::style() const

返回此QRawFont 的样式。

另请参阅 QFont::style()。

QString QRawFont::styleName() const

返回此QRawFont 的样式名称。

另请参阅 QFont::styleName()。

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

根据字体文件中设计者提供的信息,返回该字体支持的书写系统列表。请注意,这并不保证字体支持某个特定的Unicode点。您可以使用supportsCharacter() 来检查对单个特定字符的支持情况。

注意:该 列表是根据字体中 OS/2 表中设置的 Unicode 范围和码页范围确定的,并且要求底层字体文件中存在此类表。

另请参阅 supportsCharacter()。

bool QRawFont::supportsCharacter(QChar character) const

如果该字体中存在与给定的character 相对应的字形,则返回true 。

另请参阅 supportedWritingSystems()。

bool QRawFont::supportsCharacter(uint ucs4) const

如果该字体包含与 UCS-4 编码字符ucs4 对应的字形,则返回true 。

这是一个重载函数。

另请参阅 supportedWritingSystems()。

[noexcept] void QRawFont::swap(QRawFont &other)

将此原始字体替换为other 。此操作速度极快,且绝不会失败。

qreal QRawFont::underlinePosition() const

返回以该字体渲染的文本下方绘制下划线时,相对于基线的起始位置。

qreal QRawFont::unitsPerEm() const

返回定义此QRawFont 中 em 方块宽度和高度的设计单位数量。在将设计度量单位转换为像素单位时,该值将与像素大小结合使用,因为内部度量单位以设计单位指定,而像素大小则表示 1 em 在像素中的尺寸。

另请参阅 pixelSize() 和setPixelSize()。

int QRawFont::weight() const

返回此QRawFont 的权重。

另请参阅 QFont::weight()。

qreal QRawFont::xHeight() const

返回此QRawFont 的xHeight,单位为像素。

这通常(但并非总是)与字符“x”的高度相同。

另请参阅 QFontMetricsF::xHeight()。

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

如果该QRawFont 不等于other ,则返回true 。否则,返回false 。

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

将other 分配给此QRawFont 。

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

如果该QRawFont 等于other ,则返回true 。否则,返回false 。

相关非成员

[noexcept] size_t qHash(const QRawFont &key, size_t seed = 0)

返回key 的哈希值,并使用seed 作为计算的种子。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.