PySide6.QtGui.QRawFont¶
- class QRawFont¶
The
QRawFontclass provides access to a single physical instance of a font.Details
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
QFontclass 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
QRawFontclass exists.A
QRawFontobject 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 theQGlyphRunclass to draw specific glyph indexes at specific positions, and also have accessors to some relevant data in the physical font.QRawFontonly 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.QRawFontcan be constructed in a number of ways:It can be constructed by calling QTextLayout::glyphs() or QTextFragment::glyphs(). The returned QGlyphs objects will contain
QRawFontobjects which represent the actual fonts used to render each portion of the text.It can be constructed by passing a
QFontobject tofromFont(). The function will return aQRawFontobject representing the font that will be selected as response to theQFontquery and the selected writing system.It can be constructed by passing a file name or QByteArray directly to the
QRawFontconstructor, or by callingloadFromFile()orloadFromData(). In this case, the font will not be registered inQFontDatabase, and it will not be available as part of regular font selection.
QRawFontis considered local to the thread in which it is constructed (either using a constructor, or by callingloadFromData()orloadFromFile()). TheQRawFontcannot be moved to a different thread, but will have to be recreated in the thread in question.Note
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
QStaticTextclass, since it optimizes the memory cost of the cache and also provides the possibility of paint engine specific caches for an additional speed-up.Synopsis¶
Methods¶
def
__init__()def
ascent()def
boundingRect()def
capHeight()def
descent()def
familyName()def
fontTable()def
isValid()def
leading()def
lineThickness()def
loadFromData()def
loadFromFile()def
maxCharWidth()def
__ne__()def
__eq__()def
pathForGlyph()def
pixelSize()def
setPixelSize()def
style()def
styleName()def
swap()def
unitsPerEm()def
weight()def
xHeight()
Static functions¶
def
fromFont()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
- class AntialiasingType¶
This enum represents the different ways a glyph can be rasterized in the function
alphaMapForGlyph().Constant
Description
QRawFont.AntialiasingType.PixelAntialiasing
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.AntialiasingType.SubPixelAntialiasing
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.
- class LayoutFlag¶
(inherits
enum.Flag) This enum tells the functionadvancesForGlyphIndexes()how to calculate the advances.Constant
Description
QRawFont.LayoutFlag.SeparateAdvances
Will calculate the advance for each glyph separately.
QRawFont.LayoutFlag.KernedAdvances
Will apply kerning between adjacent glyphs. Note that OpenType GPOS based kerning is currently not supported.
QRawFont.LayoutFlag.UseDesignMetrics
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.
- __init__()¶
Constructs an invalid
QRawFont.- __init__(other)
- Parameters:
other –
QRawFont
Creates a
QRawFontwhich is a copy ofother.- __init__(fontData, pixelSize[, hintingPreference=QFont.PreferDefaultHinting])
- Parameters:
fontData –
QByteArraypixelSize – float
hintingPreference –
HintingPreference
Constructs a
QRawFontrepresenting the font contained in the suppliedfontDatafor the size (in pixels) given bypixelSize, and using the hinting preference specified byhintingPreference.Note
The data must contain a TrueType or OpenType font.
- __init__(fileName, pixelSize[, hintingPreference=QFont.PreferDefaultHinting])
- Parameters:
fileName – str
pixelSize – float
hintingPreference –
HintingPreference
Constructs a
QRawFontrepresenting the font contained in the file referenced byfileNamefor the size (in pixels) given bypixelSize, and using the hinting preference specified byhintingPreference.Note
The referenced file must contain a TrueType or OpenType font.
- advancesForGlyphIndexes(glyphIndexes)¶
- Parameters:
glyphIndexes – .list of quint32
- Return type:
.list of QPointF
Returns the
QRawFont‘s advances for each of theglyphIndexesin 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.See also
- advancesForGlyphIndexes(glyphIndexes, layoutFlags)
- Parameters:
glyphIndexes – .list of quint32
layoutFlags – Combination of
LayoutFlag
- Return type:
.list of QPointF
Returns the
QRawFont‘s advances for each of theglyphIndexesin 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 bylayoutFlags.Note
When
KernedAdvancesis requested, this function will apply kerning rules from the TrueType tableKERN, 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, useQTextLayout.- alphaMapForGlyph(glyphIndex[, antialiasingType=QRawFont.AntialiasingType.SubPixelAntialiasing[, transform=QTransform()]])¶
- Parameters:
glyphIndex – int
antialiasingType –
AntialiasingTypetransform –
QTransform
- Return type:
This function returns a rasterized image of the glyph at the given
glyphIndexin the underlying font, using thetransformspecified. If theQRawFontis not valid, this function will return an invalidQImage.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
antialiasingTypewill be ignored.Otherwise, if
antialiasingTypeis set toSubPixelAntialiasing, then the resulting image will be inFormat_RGB32and 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 ofFormat_Indexed8and each pixel will contain the opacity of the pixel in the rasterization.See also
- ascent()¶
- Return type:
float
Returns the ascent of this
QRawFontin 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.
See also
- averageCharWidth()¶
- Return type:
float
Returns the average character width of this
QRawFontin pixel units.See also
Returns the smallest rectangle containing the glyph with the given
glyphIndex.- capHeight()¶
- Return type:
float
Returns the cap height of this
QRawFontin 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.
See also
- descent()¶
- Return type:
float
Returns the descent of this
QRawFontin 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.
See also
- familyName()¶
- Return type:
str
Returns the family name of this
QRawFont.Retrieves the sfnt table specified by
tagfrom 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.- fontTable(tagName)
- Parameters:
tagName – str
- Return type:
The name must be a four-character string.
- static fromFont(font[, writingSystem=QFontDatabase.Any])¶
- Parameters:
font –
QFontwritingSystem –
WritingSystem
- Return type:
Fetches the physical representation based on a
fontquery. The physical font returned is the font that will be preferred by Qt in order to display text in the selectedwritingSystem.Warning
This function is potentially expensive and should not be called in performance sensitive code.
- glyphIndexesForString(text)¶
- Parameters:
text – str
- Return type:
.list of quint32
Converts the string of unicode points given by
textto 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
QTextLayoutto lay out and shape the text, then call QTextLayout::glyphs() to get the set of glyph index list andQRawFontpairs.See also
advancesForGlyphIndexes()glyphIndexesForChars()QGlyphRunglyphRuns()glyphRuns()- hintingPreference()¶
- Return type:
Returns the hinting preference used to construct this
QRawFont.See also
- isValid()¶
- Return type:
bool
Returns
trueif theQRawFontis valid and false otherwise.- leading()¶
- Return type:
float
Returns the leading of this
QRawFontin pixel units.This is the natural inter-line spacing.
See also
- lineThickness()¶
- Return type:
float
Returns the thickness for drawing lines (underline, overline, etc.) along with text drawn in this font.
- loadFromData(fontData, pixelSize, hintingPreference)¶
- Parameters:
fontData –
QByteArraypixelSize – float
hintingPreference –
HintingPreference
Replaces the current
QRawFontwith the font contained in the suppliedfontDatafor the size (in pixels) given bypixelSize, and using the hinting preference specified byhintingPreference.The
fontDatamust contain a TrueType or OpenType font.See also
- loadFromFile(fileName, pixelSize, hintingPreference)¶
- Parameters:
fileName – str
pixelSize – float
hintingPreference –
HintingPreference
Replaces the current
QRawFontwith the contents of the file referenced byfileNamefor the size (in pixels) given bypixelSize, and using the hinting preference specified byhintingPreference.The file must reference a TrueType or OpenType font.
See also
- maxCharWidth()¶
- Return type:
float
Returns the width of the widest character in the font.
See also
Returns
trueif thisQRawFontis not equal toother. Otherwise, returnsfalse.Returns
trueif thisQRawFontis equal toother. Otherwise, returnsfalse.- pathForGlyph(glyphIndex)¶
- Parameters:
glyphIndex – int
- Return type:
This function returns the shape of the glyph at a given
glyphIndexin the underlying font if theQRawFontis valid. Otherwise, it returns an emptyQPainterPath.The returned glyph will always be unhinted.
See also
- pixelSize()¶
- Return type:
float
Returns the pixel size set for this
QRawFont. The pixel size affects how glyphs are rasterized, the size of glyphs returned bypathForGlyph(), and is used to convert internal metrics from design units to logical pixel units.See also
- setPixelSize(pixelSize)¶
- Parameters:
pixelSize – float
Sets the pixel size with which this font should be rendered to
pixelSize.See also
Returns the style of this
QRawFont.See also
- styleName()¶
- Return type:
str
Returns the style name of this
QRawFont.See also
- supportedWritingSystems()¶
- Return type:
.list of QFontDatabase.WritingSystem
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.
See also
- supportsCharacter(character)¶
- Parameters:
character –
QChar- Return type:
bool
Returns
trueif the font has a glyph that corresponds to the givencharacter.See also
- supportsCharacter(ucs4)
- Parameters:
ucs4 – int
- Return type:
bool
Returns
trueif the font has a glyph that corresponds to the UCS-4 encoded characterucs4.See also
Swaps this raw font with
other. This operation is very fast and never fails.- underlinePosition()¶
- Return type:
float
Returns the position from baseline for drawing underlines below the text rendered with this font.
- unitsPerEm()¶
- Return type:
float
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.See also
- weight()¶
- Return type:
int
Returns the weight of this
QRawFont.See also
- xHeight()¶
- Return type:
float
Returns the xHeight of this
QRawFontin pixel units.This is often but not always the same as the height of the character ‘x’.
See also