QRawFont 类

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 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 ()
QList<QPointF> advancesForGlyphIndexes (const QList<quint32> & glyphIndexes , QRawFont::LayoutFlags layoutFlags ) const
QList<QPointF> advancesForGlyphIndexes (const QList<quint32> & glyphIndexes ) const
bool advancesForGlyphIndexes (const quint32 * glyphIndexes , QPointF * advances , int numGlyphs ) const
bool advancesForGlyphIndexes (const quint32 * glyphIndexes , QPointF * advances , int numGlyphs , QRawFont::LayoutFlags layoutFlags ) 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
(从 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 & font , size_t seed = 0)

详细描述

注意: QrawFont 是低级类。对于大多数目的 QFont 是更合适的类。

Most commonly, when presenting text in a user interface, the exact fonts used to render the characters is to some extent unknown. This can be the case for several reasons: For instance, the actual, physical fonts present on the target system could be unexpected to the developers, or the text could contain user selected styles, sizes or writing systems that are not supported by font chosen in the code.

Therefore, Qt's QFont class really represents a query for fonts. When text is interpreted, Qt will do its best to match the text to the query, but depending on the support, different fonts can be used behind the scenes.

For most use cases, this is both expected and necessary, as it minimizes the possibility of text in the user interface being undisplayable. In some cases, however, more direct control over the process might be useful. It is for these use cases the QRawFont class exists.

A QRawFont object represents a single, physical instance of a given font in a given pixel size. I.e. in the typical case it represents a set of TrueType or OpenType font tables and uses a user specified pixel size to convert metrics into logical pixel units. It can be used in combination with the QGlyphRun class to draw specific glyph indexes at specific positions, and also have accessors to some relevant data in the physical font.

QRawFont only provides support for the main font technologies: GDI and DirectWrite on Windows platforms, FreeType on Linux platforms and CoreText on macOS. For other font back-ends, the APIs will be disabled.

QRawFont can be constructed in a number of ways:

  • It can be constructed by calling QTextLayout::glyphs() or QTextFragment::glyphs(). The returned QGlyphs objects will contain QRawFont objects which represent the actual fonts used to render each portion of the text.
  • It can be constructed by passing a QFont 对象到 QRawFont::fromFont (). The function will return a QRawFont object representing the font that will be selected as response to the QFont query and the selected writing system.
  • It can be constructed by passing a file name or QByteArray directly to the QRawFont constructor, or by calling loadFromFile () 或 loadFromData (). In this case, the font will not be registered in QFontDatabase , and it will not be available as part of regular font selection.

QRawFont is considered local to the thread in which it is constructed (either using a constructor, or by calling loadFromData () 或 loadFromFile ()). The QRawFont cannot be moved to a different thread, but will have to be recreated in the thread in question.

注意: For the requirement of caching glyph indexes and font selections for static text to avoid reshaping and relayouting in the inner loop of an application, a better choice is the QStaticText class, since it optimizes the memory cost of the cache and also provides the possibility of paint engine specific caches for an additional speed-up.

成员类型文档编制

enum QRawFont:: AntialiasingType

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

常量 描述
QRawFont::PixelAntialiasing 0 Will 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::SubPixelAntialiasing 1 Will 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

此枚举告诉函数 advancesForGlyphIndexes () how to calculate the advances.

常量 描述
QRawFont::SeparateAdvances 0 Will calculate the advance for each glyph separately.
QRawFont::KernedAdvances 1 Will apply kerning between adjacent glyphs. Note that OpenType GPOS based kerning is currently not supported.
QRawFont::UseDesignMetrics 2 Use design metrics instead of hinted metrics adjusted to the resolution of the paint device. Can be OR-ed with any of the options above.

LayoutFlags 类型是 typedef 对于 QFlags <LayoutFlag>。它存储 LayoutFlag 值的 OR 组合。

成员函数文档编制

QRawFont:: QRawFont ()

构造无效 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 .

注意: 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 .

注意: The data must contain a TrueType or OpenType font.

QRawFont:: QRawFont (const QRawFont & other )

Creates a QRawFont which is a copy of other .

[noexcept] QRawFont:: ~QRawFont ()

销毁 QRawFont

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

返回 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 .

注意: KernedAdvances is requested, this function will apply kerning rules from the TrueType table KERN , if this is available in the font. In many modern fonts, kerning is handled through OpenType rules or AAT rules, which requires a full shaping step to be applied. To get the results of fully shaping the text, use QTextLayout .

另请参阅 QTextLine::horizontalAdvance (), QFontMetricsF::horizontalAdvance (),和 QTextLayout::glyphRuns ().

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

这是重载函数。

返回 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 () 和 QFontMetricsF::horizontalAdvance ().

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

这是重载函数。

返回 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 () 和 QFontMetricsF::horizontalAdvance ().

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

返回 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 .

注意: KernedAdvances is requested, this function will apply kerning rules from the TrueType table KERN , if this is available in the font. In many modern fonts, kerning is handled through OpenType rules or AAT rules, which requires a full shaping step to be applied. To get the results of fully shaping the text, use QTextLayout .

另请参阅 QTextLine::horizontalAdvance (), QFontMetricsF::horizontalAdvance (),和 QTextLayout::glyphRuns ().

QImage QRawFont:: alphaMapForGlyph ( quint32 glyphIndex , QRawFont::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 将被忽略。

否则,若 antialiasingType 被设为 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 () 和 QPainter::drawGlyphRun ().

qreal QRawFont:: ascent () const

返回上升为此 QRawFont 以像素为单位。

字体的上升是从基线到字符最高位置的延伸距离。实践中,一些字体设计者打破了此规则,如:当在字符顶部放置多个重音时 (或为容纳外来语言中的不寻常字符),所以此值很小是可能的 (尽管罕见)。

另请参阅 QFontMetricsF::ascent ().

qreal QRawFont:: averageCharWidth () const

Returns the average character width of this QRawFont 以像素为单位。

另请参阅 QFontMetricsF::averageCharWidth ().

QRectF QRawFont:: boundingRect ( quint32 glyphIndex ) const

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

qreal QRawFont:: capHeight () const

Returns the cap height of this QRawFont 以像素为单位。

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.

另请参阅 QFontMetricsF::capHeight ().

qreal QRawFont:: descent () const

返回下降为此 QRawFont 以像素为单位。

下降是从基线到字符最低点的延伸距离。实践中,一些字体设计者打破了此规则,如:为容纳外来语言中的不寻常字符,所以此值很小是可能的 (尽管罕见)。

另请参阅 QFontMetricsF::descent ().

QString QRawFont:: familyName () const

返回系列名称为此 QRawFont .

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

Retrieves the sfnt table specified by tag 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.

该函数在 Qt 6.7 引入。

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

此函数重载 fontTable (QFont::Tag).

The name must be a four-character string.

[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 .

警告: 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 (),和 QTextFragment::glyphRuns ().

QList < 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 list 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 (),和 QTextFragment::glyphRuns ().

QFont::HintingPreference QRawFont:: hintingPreference () const

Returns the hinting preference used to construct this QRawFont .

另请参阅 QFont::hintingPreference ().

bool QRawFont:: isValid () const

返回 true QRawFont is valid and false otherwise.

qreal QRawFont:: leading () const

返回行间距为此 QRawFont 以像素为单位。

这是自然行间的间距。

另请参阅 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

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

另请参阅 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 () 和 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

返回样式为此 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.

注意: 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

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

另请参阅 supportedWritingSystems ().

bool QRawFont:: supportsCharacter ( uint ucs4 ) const

这是重载函数。

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

另请参阅 supportedWritingSystems ().

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

交换此原生字体与 other 。此函数非常快且从不失败。

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 () 和 setPixelSize ().

int QRawFont:: weight () const

返回粗细为此 QRawFont .

另请参阅 QFont::weight ().

qreal QRawFont:: xHeight () const

返回 xHeight 为此 QRawFont 以像素为单位。

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

另请参阅 QFontMetricsF::xHeight ().

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

返回 true 若此 QRawFont 不等于 other 。否则,返回 false .

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

赋值 other 到此 QRawFont .

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

返回 true 若此 QRawFont 等于 other 。否则,返回 false .

相关非成员

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

返回哈希值为 font 。若指定, seed 用于初始化哈希。