Skip to content

Rich Text

keyed_extras.rich_text

Rich text rendering with Pango.

FontWeight

Bases: IntEnum

Font weight constants.

THIN class-attribute instance-attribute

THIN = THIN

Thin weight (= 100).

ULTRALIGHT class-attribute instance-attribute

ULTRALIGHT = ULTRALIGHT

Ultralight weight (= 200).

LIGHT class-attribute instance-attribute

LIGHT = LIGHT

Light weight (= 300).

BOOK class-attribute instance-attribute

BOOK = BOOK

Book weight (= 380).

NORMAL class-attribute instance-attribute

NORMAL = NORMAL

Default weight (= 400).

MEDIUM class-attribute instance-attribute

MEDIUM = MEDIUM

Medium weight (= 600).

SEMIBOLD class-attribute instance-attribute

SEMIBOLD = SEMIBOLD

Semibold weight (= 500).

BOLD class-attribute instance-attribute

BOLD = BOLD

Bold weight (= 700)

ULTRABOLD class-attribute instance-attribute

ULTRABOLD = ULTRABOLD

Ultrabold weight (= 800).

HEAVY class-attribute instance-attribute

HEAVY = HEAVY

Heavy weight (= 900).

ULTRAHEAVY class-attribute instance-attribute

ULTRAHEAVY = ULTRAHEAVY

Ultraheavy weight (= 1000).

FontStyle

Bases: IntEnum

Font style constants.

NORMAL class-attribute instance-attribute

NORMAL = NORMAL

The font is upright.

ITALIC class-attribute instance-attribute

ITALIC = ITALIC

The font is slanted in an italic style.

OBLIQUE class-attribute instance-attribute

OBLIQUE = OBLIQUE

The font is slanted, but in a roman style.

Alignment

Bases: IntEnum

Text alignment constants.

LEFT class-attribute instance-attribute

LEFT = LEFT

Put all available space on the right.

RIGHT class-attribute instance-attribute

RIGHT = RIGHT

Put all available space on the left.

CENTER class-attribute instance-attribute

CENTER = CENTER

Center the line within the available space.

WrapMode

Bases: IntEnum

Text wrapping mode constants.

NONE class-attribute instance-attribute

NONE = 3

Do not wrap.

WORD class-attribute instance-attribute

WORD = WORD

Wrap lines at word boundaries.

CHAR class-attribute instance-attribute

CHAR = CHAR

Wrap lines at character boundaries.

WORD_CHAR class-attribute instance-attribute

WORD_CHAR = WORD_CHAR

Wrap lines at word boundaries, but fall back to character boundaries if there is not enough space for a full word.

Markup

Create PangoMarkup strings.

Use this in conjuction with RichText(use_markup=True, ...).

Example
text = (
    Markup()
    .span("Hello ", color="#FF0000", weight=FontWeight.BOLD)
    .span("World", style=FontStyle.ITALIC)
    .text
)

span

span(text, color=None, font=None, size=None, weight=None, style=None, underline=None, strikethrough=None, background=None, stretch=None, letter_spacing=None)

Add a formatted span of text.

Parameters:

Name Type Description Default
text str

The text content

required
color str | tuple[float, float, float] | None

Text color as hex string (#RRGGBB) or RGB tuple (0-1)

None
font str | None

Font family name

None
size int | None

Font size in points (multiplied by Pango.SCALE)

None
weight int | None

Font weight from FontWeight constants

None
style int | None

Font style from FontStyle constants

None
underline bool | None

Whether to underline the text

None
strikethrough bool | None

Whether to strike through the text

None
background str | tuple[float, float, float] | None

Background color as hex string or RGB tuple

None
stretch int | None

Font stretch (condensed, expanded)

None
letter_spacing int | None

Spacing between letters in Pango units

None

Returns:

Type Description
Self

self

newline

newline()

Add a line break.

Returns:

Type Description
Self

self

RichText

Bases: Base

Rich text that can be drawn with complex styling using Pango.

Parameters:

Name Type Description Default
scene Scene

The scene to which the text belongs

required
text HasValue[str]

Text or markup string to display

required
width HasValue[float]

Maximum width for wrapping text (0 for no wrapping)

0
x HasValue[float] | None

X position (left side of text box when no path is used)

None
y HasValue[float] | None

Y position (top side of text box when no path is used)

None
font str

Default font family name

'Sans'
size HasValue[float]

Default font size in points

24
color tuple[float, float, float] | HasValue[Color]

Default RGB color tuple

(1, 1, 1)
alpha HasValue[float]

Opacity from 0-1

1.0
alignment int

Text alignment (not used when on path)

LEFT
wrap_mode int

Text wrapping mode (not used when on path)

WORD
operator Operator

Cairo operator for blending

OPERATOR_OVER
use_markup bool

Whether to interpret the text as Pango markup

False
line_spacing HasValue[float]

Additional spacing between lines (not used when on path)

1.0
indent HasValue[float]

First line indent in pixels (not used when on path)

0.0
path HasValue[LineString] | list[Tuple[float, float]] | None

Optional path for text to follow.

Warning! This currently trashes the text's original kerning and looks pretty bad.

None
path_offset HasValue[float]

Distance along the path to start text (in 0-1) (used when path is provided)

0.0
path_spacing HasValue[float]

Spacing factor between characters (used when path is provided)

1.0
align_perpendicular bool

Whether to align glyphs perpendicular to the path (used when path is provided)

True