pyglet.font.group

class FontGroup

A collection of fonts that can be used like a single font.

Each font can be assigned a range of Unicode values that it is set to handle.

Added in version 3.0.

__init__(name: str) None

Create a new font group.

Parameters:

name (str) – A unique name describing the grouping; must be unique enough to not collide with an existing font.

add(
family: str,
start: int | str,
end: int | str,
) FontGroup

Add a font family responsible for a range of characters.

The first match will be used by layouts.

Parameters:
  • family (str) – The name of the font family for this range.

  • start (int | str) – The start of the range. This may be a single-character string or an integer corresponding to a Unicode code point.

  • end (int | str) – The end of the range. This may be a single-character string or an integer corresponding to a Unicode code point.

Return type:

FontGroup

Returns:

This existing font group instance.

get_font(
size: float | None,
weight: str | None = 'normal',
style: str | None = 'normal',
stretch: str | None = 'normal',
dpi: int | None = None,
) FontGroupInstance
Return type:

FontGroupInstance

class FontGroupInstance

A font instance based off the FontGroup.

__init__(
group: FontGroup,
size: float,
weight: str | Weight,
style: str | Style,
stretch: str | Stretch,
dpi: int | None,
) None

Initialize a font that can be used with Pyglet.

Parameters:
  • name – Font family, for example, “Times New Roman”. If a list of names is provided, the first one matching a known font is used. If no font can be matched to the name(s), a default font is used. The default font will be platform dependent.

  • size (float) – Size of the font, in points. The returned font may be an exact match or the closest available.

  • weight (str | Weight) – If set, a specific weight variant is returned if one exists for the given font family and size. The weight is provided as a string. For example: “bold” or “light”.

  • style (str | Style) – If True, an italic variant is returned, if one exists for the given family and size. For some Font renderers, italics may have an “oblique” variation which can be specified as a string.

  • stretch (str | Stretch) – If True, a stretch variant is returned, if one exists for the given family and size. Currently only supported by Windows through the DirectWrite font renderer. For example, “condensed” or “expanded”.

  • dpi (int | None) – int The assumed resolution of the display device, for the purposes of determining the pixel size of the font. Defaults to 96.

get_glyphs(
text: str,
shaping: bool = False,
) tuple[list[Glyph], list[GlyphPosition]]

Create and return a list of Glyphs for text.

If any characters do not have a known glyph representation in this font, a substitution will be made.

Parameters:
  • text (str) – Text to render.

  • shaping (bool) – If the text will be shaped using the global option. If False, no text shaping will occur and positioning will instead be based on glyph dimensions.

Return type:

tuple[list[Glyph], list[GlyphPosition]]

get_text_size(text: str) tuple[int, int]

Return’s an estimated width and height of text using glyph metrics without rendering..

This does not take into account any shaping.

Return type:

tuple[int, int]