QGlyphRun Class
QGlyphRun 类可直接访问字体中的内部字形。更多内容...
| 标题: | #include <QGlyphRun> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
| qmake: | QT += gui |
- 所有成员的列表,包括继承的成员
- QGlyphRun 属于“隐式共享类”。
公共类型
| enum | GlyphRunFlag { Overline, Underline, StrikeOut, RightToLeft, SplitLigature } |
| flags | GlyphRunFlags |
公共函数
| QGlyphRun() | |
| QGlyphRun(const QGlyphRun &other) | |
| ~QGlyphRun() | |
| QRectF | boundingRect() const |
| void | clear() |
| QGlyphRun::GlyphRunFlags | flags() const |
| QList<quint32> | glyphIndexes() const |
| bool | isEmpty() const |
| bool | isRightToLeft() const |
| bool | overline() const |
| QList<QPointF> | positions() const |
| QRawFont | rawFont() const |
| void | setBoundingRect(const QRectF &boundingRect) |
| void | setFlag(QGlyphRun::GlyphRunFlag flag, bool enabled = true) |
| void | setFlags(QGlyphRun::GlyphRunFlags flags) |
| void | setGlyphIndexes(const QList<quint32> &glyphIndexes) |
| void | setOverline(bool overline) |
| void | setPositions(const QList<QPointF> &positions) |
| void | setRawData(const quint32 *glyphIndexArray, const QPointF *glyphPositionArray, int size) |
| void | setRawFont(const QRawFont &rawFont) |
| void | setRightToLeft(bool rightToLeft) |
(since 6.5) void | setSourceString(const QString &sourceString) |
| void | setStrikeOut(bool strikeOut) |
(since 6.5) void | setStringIndexes(const QList<qsizetype> &stringIndexes) |
| void | setUnderline(bool underline) |
(since 6.5) QString | sourceString() const |
| bool | strikeOut() const |
(since 6.5) QList<qsizetype> | stringIndexes() const |
| void | swap(QGlyphRun &other) |
| bool | underline() const |
| bool | operator!=(const QGlyphRun &other) const |
| QGlyphRun & | operator=(const QGlyphRun &other) |
| bool | operator==(const QGlyphRun &other) const |
详细说明
当 Qt 显示用 Unicode 编码的文本字符串时,它会首先根据一种或多种字体,将 Unicode 点转换为一个字形索引列表和一个位置列表。 在这种情况下,文本的 Unicode 表示形式和 `QFont ` 对象将作为一种方便的抽象,隐藏了在屏幕上显示文本时实际发生的情况的细节。 例如,当文本实际显示在屏幕上时,除了用户指定的字体之外,它可能还由一组字体表示,例如,如果最初选择的字体不支持文本中包含的所有书写系统。
在某些情况下,作为应用程序开发人员,能够更低层次地控制特定字体中的哪些字形被绘制到屏幕上,这会非常有用。 例如,在使用外部字体引擎和文本整形器与 Qt 配合的应用程序中,就可能出现这种情况。QGlyphRun 提供了一个接口,用于访问将文本显示在屏幕上所需的原始数据。它包含一个字形索引列表、每个字形的位置以及一个字体。
用户有责任确保所选字体确实包含提供的字形索引。
QTextLayout::glyphRuns() 或QTextFragment::glyphRuns() 可用于将 Unicode 编码文本转换为 QGlyphRun 对象列表,而QPainter::drawGlyphRun() 可用于绘制字形。
成员类型文档
enum QGlyphRun::GlyphRunFlag
flags QGlyphRun::GlyphRunFlags
此枚举描述了用于改变字形序列在视觉布局中呈现方式或行为的标志。生成字形序列的布局可根据相关内部数据设置这些标志,以保留呈现文本时所需的信息,从而按照布局使用者的意图显示文本。
| 常量 | 值 | 描述 |
|---|---|---|
QGlyphRun::Overline | 0x01 | 表示字形应与上划线一起显示。 |
QGlyphRun::Underline | 0x02 | 表示字形应与上划线一起显示。 |
QGlyphRun::StrikeOut | 0x04 | 表示字形应显示为划线状态。 |
QGlyphRun::RightToLeft | 0x08 | 表示字形按从右到左的顺序排列。这可能会影响相对于字形序列的其他屏幕元素(例如内联文本对象)的位置。 |
QGlyphRun::SplitLigature | 0x10 | 表示字形序列拆分了一个连字字形。这意味着连字字形包含在该序列中,但它所代表的字符仅对应该连字的一部分。 在此情况下,可使用字形序列的boundingRect()函数来获取与该字形序列所表示的字符对应的字形所覆盖的区域。 在可视化字形时,需要小心裁剪到该边界矩形内,以确保仅绘制连字的相应部分。特别是,当从QTextLayout 中为特定字符范围检索字形序列时,例如检索QTextLayout 的选定区域时,可能会出现这种情况。 |
GlyphRunFlags 类型是QFlags<GlyphRunFlag> 的 typedef 定义。它存储 GlyphRunFlag 值的按“或”运算组合。
成员函数文档
QGlyphRun::QGlyphRun()
创建一个空的 QGlyphRun 对象。
QGlyphRun::QGlyphRun(const QGlyphRun &other)
创建一个 QGlyphRun 对象,该对象是other 的副本。
[noexcept] QGlyphRun::~QGlyphRun()
销毁QGlyphRun 。
QRectF QGlyphRun::boundingRect() const
返回包含该QGlyphRun 中所有字形的最小的矩形。如果已通过setBoundingRect()设置了边界矩形,则返回该边界矩形。否则,将根据字形序列中各字形的字体度量值计算边界矩形。
另请参阅 setBoundingRect()。
void QGlyphRun::clear()
清除QGlyphRun 对象中的所有数据。
QGlyphRun::GlyphRunFlags QGlyphRun::flags() const
返回为该QGlyphRun 设置的标志。
QList<quint32> QGlyphRun::glyphIndexes() const
返回此QGlyphRun 对象的字形索引。
另请参阅 setGlyphIndexes() 和setPositions()。
bool QGlyphRun::isEmpty() const
如果QGlyphRun 不包含任何字形,则返回true 。
bool QGlyphRun::isRightToLeft() const
如果该QGlyphRun 包含从右向左绘制的字形,则返回true 。
另请参阅 setRightToLeft() 和flags()。
bool QGlyphRun::overline() const
如果该QGlyphRun 应绘制带上划线的装饰,则返回true 。
另请参阅 setOverline() 和flags()。
QList<QPointF> QGlyphRun::positions() const
返回该字形索引集中的每个字形的基线边缘位置。
另请参阅 setPositions()。
QRawFont QGlyphRun::rawFont() const
返回为此QGlyphRun 对象选定的字体。
另请参阅 setRawFont()。
void QGlyphRun::setBoundingRect(const QRectF &boundingRect)
将此QGlyphRun 中字形的边界矩形设置为boundingRect 。boundingRect() 函数将返回该矩形,除非该参数为空;若为空,则返回字形序列中字形的边界矩形。
注意:除非 您正在实现文本塑形,否则 无需使用此函数。该函数专门用于当QGlyphRun 应表示一个比其所包含字形区域更小的区域时。 例如,当通过调用 `QTextLayout::glyphRuns()` 获取字形序列,且指定的范围仅包含连字(两个或多个字符组合成单个字形)的一部分时,可能会发生这种情况。 在这种情况下,边界矩形应仅包含连字字形中的相应部分,具体范围基于连字中各字符平均宽度的计算结果确定。
为了支持此类情况(例如,应使用与正文颜色不同的颜色绘制选区),有必要将绘制机制裁剪为 `boundingRect()` 返回的矩形,以避免绘制整个连字字形。
另请参阅 boundingRect()。
void QGlyphRun::setFlag(QGlyphRun::GlyphRunFlag flag, bool enabled = true)
如果enabled 为 true,则flag 处于启用状态;否则,处于禁用状态。
void QGlyphRun::setFlags(QGlyphRun::GlyphRunFlags flags)
将此QGlyphRun 的标志设置为flags 。
void QGlyphRun::setGlyphIndexes(const QList<quint32> &glyphIndexes)
将此QGlyphRun 对象的字形索引设置为glyphIndexes 。字形索引必须对所选字体有效。
另请参阅 glyphIndexes()。
void QGlyphRun::setOverline(bool overline)
表示如果overline 为真,则应为该QGlyphRun 绘制上划线装饰;否则,应为QGlyphRun 绘制无上划线装饰。
另请参阅 overline()、setFlag() 和setFlags()。
void QGlyphRun::setPositions(const QList<QPointF> &positions)
将此字形索引集中的每个字形的基线边缘位置设置为positions 。
另请参阅 positions()。
void QGlyphRun::setRawData(const quint32 *glyphIndexArray, const QPointF *glyphPositionArray, int size)
设置此QGlyphRun 的字形索引和位置,以使用数组glyphIndexArray 和glyphPositionArray 中的前size 个元素。数据不会被复制。调用方必须保证,只要此QGlyphRun 及其任何副本存在,这些数组就不会被删除。
另请参阅 setGlyphIndexes() 和setPositions()。
void QGlyphRun::setRawFont(const QRawFont &rawFont)
将用于查找字形索引的字体设置为指定的rawFont 。
另请参阅 rawFont() 和setGlyphIndexes()。
void QGlyphRun::setRightToLeft(bool rightToLeft)
表示该QGlyphRun 包含的字形,如果rightToLeft 为真,则应按从右到左的顺序排列。否则,字形的顺序默认为从左到右。
另请参阅 isRightToLeft()、setFlag() 和setFlags()。
[since 6.5] void QGlyphRun::setSourceString(const QString &sourceString)
将与字形序列对应的字符串设置为sourceString 。如果进行了此设置,则stringIndexes() 返回的索引应为该字符串中的索引。
该函数在 Qt 6.5 中引入。
另请参阅 sourceString() 和stringIndexes()。
void QGlyphRun::setStrikeOut(bool strikeOut)
表示如果strikeOut 为真,则应为该QGlyphRun 添加删除线装饰。否则,应为该QGlyphRun 绘制无删除线装饰。
另请参阅 strikeOut()、setFlag() 和setFlags()。
[since 6.5] void QGlyphRun::setStringIndexes(const QList<qsizetype> &stringIndexes)
将与字形索引对应的字符串索引列表设置为stringIndexes
有关此列表约定规则的更多详细信息,请参阅stringIndexes()。
该函数于 Qt 6.5 中引入。
另请参阅 stringIndexes() 和sourceString()。
void QGlyphRun::setUnderline(bool underline)
表示如果underline 为真,则应为该QGlyphRun 绘制带下划线的样式。否则,应为该QGlyphRun 绘制不带下划线的样式。
另请参阅 underline()、setFlag() 和setFlags()。
[since 6.5] QString QGlyphRun::sourceString() const
如果字形序列是从字符串创建的,且该字符串是从布局中请求的,则返回与该字形序列对应的字符串。
该函数自 Qt 6.5 起引入。
另请参阅 setSourceString()、stringIndexes() 和QTextLayout::glyphRuns()。
bool QGlyphRun::strikeOut() const
如果该QGlyphRun 应以划线样式进行绘制,则返回true 。
另请参见 setStrikeOut() 和flags()。
[since 6.5] QList<qsizetype> QGlyphRun::stringIndexes() const
如果字形序列是从字符串构建的,且已向布局请求了字符串索引,则返回与每个字形索引对应的字符串索引。在这种情况下,返回向量的长度将与glyphIndexes()的返回值长度一致。在其他情况下,该向量将为空。
由于单个字形可能对应源字符串中的多个字符,因此字符串索引列表中可能会存在间隙。例如,如果字符串“first”由包含字符对“fi”连字的字体进行处理,那么这个由五个字符组成的字符串将生成一个仅包含四个字形的字形序列。 此时,字形索引可能是 (1, 2, 3, 4)(四个任意字形索引),而字符串索引则是 (0, 2, 3, 4)。 字形遵循字符串的逻辑顺序,因此可以推断,在此情况下,第一个字形涵盖了字符 0 和 1。
反之,一个字符也可能生成多个字形,在这种情况下,字符串索引列表中将出现重复条目。
字符串索引与字符串相对应,可通过sourceString() 获取(可选)。
该函数在 Qt 6.5 中引入。
另请参阅 setStringIndexes()、sourceString() 和QTextLayout::glyphRuns()。
[noexcept] void QGlyphRun::swap(QGlyphRun &other)
将当前符文运行实例与other 互换。此操作速度极快,且绝不会失败。
bool QGlyphRun::underline() const
如果该QGlyphRun 应采用下划线样式进行绘制,则返回true 。
另请参阅 setUnderline() 和flags()。
bool QGlyphRun::operator!=(const QGlyphRun &other) const
将other 与该QGlyphRun 对象进行比较。如果字形索引列表、位置列表或字体中的任何一项不同,则返回true ;否则返回false 。
QGlyphRun &QGlyphRun::operator=(const QGlyphRun &other)
将other 分配给此QGlyphRun 对象。
bool QGlyphRun::operator==(const QGlyphRun &other) const
将other 与该QGlyphRun 对象进行比较。如果字形索引列表、位置列表和字体均相等,则返回true ;否则返回false 。
© 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.