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
bool glyphIndexesForChars(const QChar *chars, int numChars, quint32 *glyphIndexes, int *numGlyphs) const
QList<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=(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 对象,这些对象代表用于渲染文本各部分的实际字体。
  • 它可以通过向QRawFont::fromFont() 传递QFont 对象来构建。该函数将返回一个 QRawFont 对象,该对象代表响应QFont 查询时所选择的字体以及所选择的书写系统。
  • 可以直接向 QRawFont 构造函数传递文件名或QByteArray ,或调用loadFromFile() 或loadFromData() 来构造字体。在这种情况下,字体不会在QFontDatabase 中注册,也不会作为常规字体选择的一部分。

QRawFont被认为是构建它的线程的本地字体(可以使用构造函数,也可以调用loadFromData() 或loadFromFile() )。QRawFont不能移动到其他线程,而必须在相关线程中重新创建。

注: 如果需要缓存静态文本的字形索引和字体选择,以避免在应用程序的内循环中进行重塑和中继,那么QStaticText 类是更好的选择,因为它优化了缓存的内存成本,而且还提供了特定于绘画引擎的缓存以提高速度。

成员类型文档

enum QRawFont::AntialiasingType

该枚举表示在函数alphaMapForGlyph() 中光栅化字形的不同方式。

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

枚举 QRawFont::LayoutFlag
flags QRawFont::LayoutFlags

该枚举用于告诉函数advancesForGlyphIndexes() 如何计算前进方向。

常量说明
QRawFont::SeparateAdvances0将分别计算每个字形的进角。
QRawFont::KernedAdvances1将在相邻字形之间应用字距均衡。请注意,目前还不支持基于 OpenType GPOS 的字形校正。
QRawFont::UseDesignMetrics2使用设计度量,而不是根据喷绘设备分辨率调整的提示度量。可与上述任何选项进行 OR 编辑。

LayoutFlags 类型是QFlags<LayoutFlag> 的类型定义。它存储 LayoutFlag 值的 OR 组合。

成员函数文档

QRawFont::QRawFont()

构造无效的 QRawFont。

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

根据pixelSize 给出的尺寸(像素),并使用hintingPreference 指定的提示首选项,构造一个 QRawFont,代表所提供的fontData 中包含的字体。

注: 数据必须包含 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 时,如果字体中存在 TrueType 表KERN ,该函数将应用 TrueType 表中的字距规则。在许多现代字体中,字尖规则是通过 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 时,如果字体中存在 TrueType 表KERN ,该函数将应用 TrueType 表中的字尖规则。在许多现代字体中,字尖规则是通过 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 表,如果没有找到该表,则读取空字节数组。返回的字体表的字节序为大端(Big Endian),与 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 中的文本而首选的字体。

警告: 此函数可能比较昂贵,不应在对性能敏感的代码中调用。

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() 。

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)

使用所提供的fontData 中包含的字体替换当前的QRawFont ,字体大小(像素)由pixelSize 给定,并使用由hintingPreference 指定的提示首选项。

fontData 必须包含 TrueType 或 OpenType 字体。

另请参阅 loadFromFile() 。

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

使用fileName 引用的文件内容替换当前的QRawFont ,文件大小(像素)由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 范围和 codepage 范围确定的,并要求在底层字体文件中存在该表。

另请参阅 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 作为计算的种子。

© 2025 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.