pyglet.text
Submodules
Details
Text formatting, layout and display.
This module provides classes for loading styled documents from text files, HTML files and a pyglet-specific markup format. Documents can be styled with multiple fonts, colours, styles, text sizes, margins, paragraph alignments, and so on.
Using the layout classes, documents can be laid out on a single line or word-wrapped to fit a rectangle. A layout can then be efficiently drawn in a window or updated incrementally (for example, to support interactive text editing).
The label classes provide a simple interface for the common case where an application simply needs to display some text in a window.
A plain text label can be created with:
label = pyglet.text.Label('Hello, world',
font_name='Times New Roman',
font_size=36,
x=10, y=10)
Alternatively, a styled text label using HTML can be created with:
label = pyglet.text.HTMLLabel('<b>Hello</b>, <i>world</i>',
x=10, y=10)
Either label can then be drawn at any time with:
label.draw()
For details on the subset of HTML supported, see pyglet.text.formats.html.
Refer to the Programming Guide for advanced usage of the document and layout classes, including interactive editing, embedding objects within documents and creating scrollable layouts.
- exception DocumentDecodeException
An error occurred decoding document text.
- class DocumentDecoder
Abstract document decoder.
- abstract decode( ) AbstractDocument
Decode document text.
- Parameters:
- Return type:
- class DocumentLabel
Base label class.
A label is a layout that exposes convenience methods for manipulating the associated document.
- __init__(
- document: AbstractDocument,
- x: float = 0.0,
- y: float = 0.0,
- z: float = 0.0,
- width: int | None = None,
- height: int | None = None,
- anchor_x: Literal['left', 'center', 'right'] = 'left',
- anchor_y: Literal['top', 'bottom', 'center', 'baseline'] = 'baseline',
- rotation: float = 0.0,
- multiline: bool = False,
- dpi: int | None = None,
- batch: Batch | None = None,
- group: Group | None = None,
- program: ShaderProgram | None = None,
- init_document: bool = True,
Create a label for a given document.
- Parameters:
document (
AbstractDocument
) – Document to attach to the layout.x (
float
) – X coordinate of the label.y (
float
) – Y coordinate of the label.z (
float
) – Z coordinate of the label.height (
int
|None
) – Height of the label in pixels, orNone
anchor_x (
Literal
['left'
,'center'
,'right'
]) – Anchor point of the X coordinate: one of"left"
, “center”` or"right"
.anchor_y (
Literal
['top'
,'bottom'
,'center'
,'baseline'
]) – Anchor point of the Y coordinate: one of"bottom"
,"baseline"
,"center"
or"top"
.rotation (
float
) – The amount to rotate the label in degrees. A positive amount will be a clockwise rotation, negative values will result in counter-clockwise rotation.multiline (
bool
) – IfTrue
, the label will be word-wrapped and accept newline characters. You must also set the width of the label.dpi (
int
|None
) – Resolution of the fonts in this layout. Defaults to 96.batch (
Batch
|None
) – Optional graphics batch to add the label to.program (
ShaderProgram
|None
) – Optional graphics shader to use. Will affect all glyphs.init_document (
bool
) – IfTrue
, the document will be initialized. If you are passing an already-initialized document, then you can avoid duplicating work by setting this toFalse
.
- get_style(name: str) Any
Get a document style value by name.
If the document has more than one value of the named style, pyglet.text.document.STYLE_INDETERMINATE is returned.
- property color: tuple[int, int, int, int]
Text color.
Color is a 4-tuple of RGBA components, each in range [0, 255].
- property font_name: str | list[str]
Font family name.
The font name, as passed to
pyglet.font.load()
. A list of names can optionally be given: the first matching font will be used.
- property opacity: int
Blend opacity.
This property sets the alpha component of the colour of the label’s vertices. With the default blend mode, this allows the layout to be drawn with fractional opacity, blending with the background.
An opacity of 255 (the default) has no effect. An opacity of 128 will make the label appear semi-translucent.
- class HTMLLabel
HTML formatted text label.
A subset of HTML 4.01 is supported. See pyglet.text.formats.html for details.
- __init__(
- text: str = '',
- location: Location | None = None,
- x: float = 0.0,
- y: float = 0.0,
- z: float = 0.0,
- width: int | None = None,
- height: int | None = None,
- anchor_x: Literal['left', 'center', 'right'] = 'left',
- anchor_y: Literal['top', 'bottom', 'center', 'baseline'] = 'baseline',
- rotation: float = 0.0,
- multiline: bool = False,
- dpi: int | None = None,
- batch: Batch | None = None,
- group: Group | None = None,
- program: ShaderProgram | None = None,
Create a label with an HTML string.
- Parameters:
text (
str
) – Text to display.location (
Location
|None
) – Location object for loading images referred to in the document. By default, the working directory is used.x (
float
) – X coordinate of the label.y (
float
) – Y coordinate of the label.z (
float
) – Z coordinate of the label.height (
int
|None
) – Height of the label in pixels, or Noneanchor_x (
Literal
['left'
,'center'
,'right'
]) – Anchor point of the X coordinate: one of"left"
,"center"
or"right"
.anchor_y (
Literal
['top'
,'bottom'
,'center'
,'baseline'
]) – Anchor point of the Y coordinate: one of"bottom"
,"baseline"
,"center"
or"top"
.rotation (
float
) – The amount to rotate the label in degrees. A positive amount will be a clockwise rotation, negative values will result in counter-clockwise rotation.multiline (
bool
) – If True, the label will be word-wrapped and accept newline characters. You must also set the width of the label.dpi (
int
|None
) – Resolution of the fonts in this layout. Defaults to 96.batch (
Batch
|None
) – Optional graphics batch to add the label to.program (
ShaderProgram
|None
) – Optional graphics shader to use. Will affect all glyphs.
- class Label
Plain text label.
- __init__(
- text: str = '',
- x: float = 0.0,
- y: float = 0.0,
- z: float = 0.0,
- width: int | None = None,
- height: int | None = None,
- anchor_x: Literal['left', 'center', 'right'] = 'left',
- anchor_y: Literal['top', 'bottom', 'center', 'baseline'] = 'baseline',
- rotation: float = 0.0,
- multiline: bool = False,
- dpi: int | None = None,
- font_name: str | None = None,
- font_size: float | None = None,
- bold: bool | str = False,
- italic: bool | str = False,
- stretch: bool | str = False,
- color: tuple[int, int, int, int] | tuple[int, int, int] = (255, 255, 255, 255),
- align: Literal['bottom', 'center', 'top'] = 'left',
- batch: Batch | None = None,
- group: Group | None = None,
- program: ShaderProgram | None = None,
Create a plain text label.
- Parameters:
text (
str
) – Text to display.font_name (
str
|None
) – Font family name(s). If more than one name is given, the first matching name is used.color (
tuple
[int
,int
,int
,int
] |tuple
[int
,int
,int
]) – Font color as RGBA or RGB components, each within0 <= component <= 255
.x (
float
) – X coordinate of the label.y (
float
) – Y coordinate of the label.z (
float
) – Z coordinate of the label.height (
int
|None
) – Height of the label in pixels, or Noneanchor_x (
Literal
['left'
,'center'
,'right'
]) – Anchor point of the X coordinate: one of"left"
,"center"
or"right"
.anchor_y (
Literal
['top'
,'bottom'
,'center'
,'baseline'
]) – Anchor point of the Y coordinate: one of"bottom"
,"baseline"
,"center"
or"top"
.rotation (
float
) – The amount to rotate the label in degrees. A positive amount will be a clockwise rotation, negative values will result in counter-clockwise rotation.align (
Literal
['bottom'
,'center'
,'top'
]) – Horizontal alignment of text on a line, only applies if a width is supplied. One of"left"
,"center"
or"right"
.multiline (
bool
) – If True, the label will be word-wrapped and accept newline characters. You must also set the width of the label.dpi (
int
|None
) – Resolution of the fonts in this layout. Defaults to 96.batch (
Batch
|None
) – Optional graphics batch to add the label to.program (
ShaderProgram
|None
) – Optional graphics shader to use. Will affect all glyphs.
- decode_attributed(text: str) FormattedDocument
Create a document directly from some attributed text.
See pyglet.text.formats.attributed for a description of attributed text.
- Return type:
- decode_html( ) FormattedDocument
Create a document directly from some HTML formatted text.
- Parameters:
- Return type:
- decode_text(text: str) UnformattedDocument
Create a document directly from some plain text.
- Return type:
- get_decoder(
- filename: str | None,
- mimetype: Literal['text/plain', 'text/html', 'text/vnd.pyglet-attributed'] | None = None,
Get a document decoder for the given filename and MIME type.
If
mimetype
is omitted it is guessed from the filename extension.The following MIME types are supported:
text/plain
Plain text
text/html
HTML 4 Transitional
text/vnd.pyglet-attributed
Attributed text; see pyglet.text.formats.attributed
- Parameters:
- Raises:
DocumentDecodeException – If MIME type is not from the supported types.
- Return type:
- load(
- filename: str,
- file: BinaryIO | None = None,
- mimetype: Literal['text/plain', 'text/html', 'text/vnd.pyglet-attributed'] | None = None,
Load a document from a file.
- Parameters:
filename (
str
) – Filename of document to load.file (
BinaryIO
|None
) – File object containing encoded data. If omitted,filename
is loaded from disk.mimetype (
Literal
['text/plain'
,'text/html'
,'text/vnd.pyglet-attributed'
] |None
) – MIME type of the document. If omitted, the filename extension is used to guess a MIME type. See get_decoder for a list of supported MIME types.
- Return type: