Class: QgsFontUtils

class qgis.core.QgsFontUtils

Bases: sip.wrapper

QgsFontUtils(QgsFontUtils)

Methods

addRecentFontFamily

Adds a font family to the list of recently used font families.

asCSS

Returns a CSS string representing the specified font as closely as possible.

fontFamilyHasStyle

Check whether font family on system has specific style

fontFamilyMatchOnSystem

Check whether font family is on system

fontFamilyOnSystem

Check whether font family is on system in a quick manner, which does not compare [foundry]

fontMatchOnSystem

Check whether exact font is on system

fromMimeData

Attempts to parse the provided mime data as a QFont.

getStandardTestFont

Gets standard test font with specific style

loadStandardTestFonts

Loads standard test fonts from filesystem or qrc resource

recentFontFamilies

Returns a list of recently used font families.

setFromXmlChildNode

Sets the properties of a font to match the properties stored in an XML child node.

setFromXmlElement

Sets the properties of a font to match the properties stored in an XML element.

standardTestFontFamily

Gets standard test font family

toMimeData

Returns new mime data representing the specified font settings.

toXmlElement

Returns a DOM element containing the properties of the font.

translateNamedStyle

Returns the localized named style of a font, if such a translation is available.

untranslateNamedStyle

Returns the english named style of a font, if possible.

updateFontViaStyle

Updates font with named style and retain all font properties

addRecentFontFamily(family: str)

Adds a font family to the list of recently used font families.

New in version 3.0.

Parameters

family (str) –

asCSS(font: QFont, pointToPixelMultiplier: float = 1)str

Returns a CSS string representing the specified font as closely as possible.

Parameters
  • font (QFont) – QFont to convert

  • pointToPixelMultiplier (float = 1) – scaling factor to apply to convert point sizes to pixel font sizes. The CSS returned by this function will always use pixels for font sizes, so this parameter should be set to a suitable value to convert point sizes to pixels (e.g., taking into account destination DPI)

Return type

str

Returns

partial CSS string, e.g., “font-family: Comic Sans; font-size: 12px;”

New in version 2.16.

fontFamilyHasStyle(family: str, style: str)bool

Check whether font family on system has specific style

Parameters
  • family (str) – The family to test

  • style (str) – The style to test for

Return type

bool

Returns

Whether family has style

New in version 2.1.

fontFamilyMatchOnSystem(family: str, chosen: str = '')Tuple[bool, bool]

Check whether font family is on system

Parameters
  • family (str) – The family to test

  • chosen (str = '') – The actual family (possibly from different foundry) returned by system

  • match – Whether the family [foundry] returned by system is a match

Return type

Tuple[bool, bool]

Returns

Whether family was found on system

fontFamilyOnSystem(family: str)bool

Check whether font family is on system in a quick manner, which does not compare [foundry]

Parameters

family (str) – The family to test

Return type

bool

Returns

Whether family was found on system

Note

This is good for use in loops of large lists, e.g. registering many features for labeling

fontMatchOnSystem(f: QFont)bool

Check whether exact font is on system

Parameters

f (QFont) – The font to test for match

Return type

bool

fromMimeData(data: QMimeData)Tuple[QFont, bool]

Attempts to parse the provided mime data as a QFont. If data can be parsed as a QFont, ok will be set to True.

See also

toMimeData()

New in version 3.0.

Parameters

data (QMimeData) –

Return type

Tuple[QFont, bool]

getStandardTestFont(style: str = '', pointsize: int = 12)QFont

Gets standard test font with specific style

Parameters
  • style (str = '') – Style to load, e.g. Roman, Oblique, Bold, Bold Oblique

  • pointsize (int = 12) – Font point size to set

Return type

QFont

Returns

QFont

New in version 2.1.

loadStandardTestFonts(loadstyles: Iterable[str])bool

Loads standard test fonts from filesystem or qrc resource

Parameters

loadstyles (Iterable[str]) – List of styles to load, e.g. All, Roman, Oblique, Bold, Bold Oblique

Return type

bool

Returns

Whether any font was loaded

Note

Done by default on debug app/server startup to ensure fonts available for unit tests (Roman and Bold)

New in version 2.1.

recentFontFamilies()List[str]

Returns a list of recently used font families.

New in version 3.0.

Return type

List[str]

setFromXmlChildNode(font: QFont, element: QDomElement, childNode: str)bool

Sets the properties of a font to match the properties stored in an XML child node. Calling this will overwrite the current properties of the font.

Parameters
  • font (QFont) – font to update

  • element (QDomElement) – DOM element

  • childNode (str) – name of child node

Return type

bool

Returns

True if child node exists and properties were successfully read from node

See also

toXmlElement()

New in version 2.10.

setFromXmlElement(font: QFont, element: QDomElement)bool

Sets the properties of a font to match the properties stored in an XML element. Calling this will overwrite the current properties of the font.

Parameters
  • font (QFont) – font to update

  • element (QDomElement) – DOM element

Return type

bool

Returns

True if properties were successfully read from element

See also

toXmlElement()

New in version 2.10.

standardTestFontFamily()str

Gets standard test font family

New in version 2.1.

Return type

str

toMimeData(font: QFont)QMimeData

Returns new mime data representing the specified font settings. Caller takes responsibility for deleting the returned object.

See also

fromMimeData()

New in version 3.0.

Parameters

font (QFont) –

Return type

QMimeData

toXmlElement(font: QFont, document: QDomDocument, elementName: str)QDomElement

Returns a DOM element containing the properties of the font.

Parameters
  • font (QFont) – font

  • document (QDomDocument) – DOM document

  • elementName (str) – name for DOM element

Return type

QDomElement

Returns

DOM element containing font settings

New in version 2.10.

translateNamedStyle(namedStyle: str)str

Returns the localized named style of a font, if such a translation is available.

Parameters

namedStyle (str) – a named style, i.e. “Bold”, “Italic”, etc

Return type

str

Returns

The localized named style

New in version 2.12.

untranslateNamedStyle(namedStyle: str)str

Returns the english named style of a font, if possible.

Parameters

namedStyle (str) – a localized named style, i.e. “Fett”, “Kursiv”, etc

Return type

str

Returns

The english named style

New in version 2.12.

updateFontViaStyle(f: QFont, fontstyle: str, fallback: bool = False)bool

Updates font with named style and retain all font properties

Parameters
  • f (QFont) – The font to update

  • fontstyle (str) – The style to try and switch the font to

  • fallback (bool = False) – If no matching fontstyle found for font, assign most similar or first style found to font

Return type

bool

Returns

Whether the font was updated (also returns True if the requested style matches font’s current style)

Note

This is a more featured replacement for a Qt 4.8+ function: void QFont.setStyleName ( const QString & styleName )