pyglet.text.document
Formatted and unformatted document interfaces used by text layout.
Abstract representation
Styled text in pyglet is represented by one of the AbstractDocument
classes,
which manage the state representation of text and style independently of how
it is loaded or rendered.
A document consists of the document text (a Unicode string) and a set of named style ranges. For example, consider the following (artificial) example:
0 5 10 15 20
The cat sat on the mat.
+++++++ +++++++ "weight"
++++++ "italic"
If this example were to be rendered, “The cat” and “the mat” would have a weight set (“bold”, “thin”, etc.), and “on the” would be in italics. Note that the second “the” is both weighted and italic.
The document styles recorded for this example would be a specific "weight"
over
ranges (0-7) & (15-22) and "italic"
over range (12-18). Overlapping styles are
permitted; unlike HTML and other structured markup, the ranges need not be nested.
The document has no knowledge of the semantics of "weight"
names or
"italic"
, it stores only the style names. The pyglet layout classes give
meaning to these style names in the way they are rendered; but you are also free
to invent your own style names (which will be ignored by the layout classes).
This can be useful to tag areas of interest in a document, or maintain
references back to the source material.
As well as text, the document can contain arbitrary elements represented by
InlineElement
. An inline element behaves
like a single character in the document, but can be rendered by the application.
Paragraph breaks
Paragraph breaks are marked with a “newline” character (U+0010). The Unicode paragraph break (U+2029) can also be used.
Line breaks (U+2028) can be used to force a line break within a paragraph.
See Unicode recommendation UTR #13 for more information: https://www.unicode.org/standard/reports/tr13/tr13-5.html.
Document classes
Any class implementing AbstractDocument
provides an interface to a
document model as described above. In theory a structured document such as
HTML or XML could export this model, though the classes provided by pyglet
implement only unstructured documents.
The UnformattedDocument
class assumes any styles set are set over the entire
document. So, regardless of the range specified when setting a "weight"
style attribute, for example, the entire document will receive that style.
The FormattedDocument
class implements the document model directly, using
the RunList class to represent style runs efficiently.
Style attributes
The following character style attribute names are recognised by pyglet:
font_name
Font family name, as given to
pyglet.font.load()
.font_size
Font size, in points.
weight
String.
italic
Boolean.
underline
4-tuple of ints in range (0, 255) giving RGBA underline color, or None (default) for no underline.
kerning
Additional space to insert between glyphs, in points. Defaults to 0.
baseline
Offset of glyph baseline from line baseline, in points. Positive values give a superscript, negative values give a subscript. Defaults to 0.
color
4-tuple of ints in range (0, 255) giving RGBA text color
background_color
4-tuple of ints in range (0, 255) giving RGBA text background color; or
None
for no background fill.
The following paragraph style attribute names are recognised by pyglet. Note that paragraph styles are handled no differently from character styles by the document: it is the application’s responsibility to set the style over an entire paragraph, otherwise results are undefined.
align
left
(default),center
orright
.indent
Additional horizontal space to insert before the first
leading
Additional space to insert between consecutive lines within a paragraph, in points. Defaults to 0.
line_spacing
Distance between consecutive baselines in a paragraph, in points. Defaults to
None
, which automatically calculates the tightest line spacing for each line based on the font ascent and descent.margin_left
Left paragraph margin, in pixels.
margin_right
Right paragraph margin, in pixels.
margin_top
Margin above paragraph, in pixels.
margin_bottom
Margin below paragraph, in pixels. Adjacent margins do not collapse.
tab_stops
List of horizontal tab stops, in pixels, measured from the left edge of the text layout. Defaults to the empty list. When the tab stops are exhausted, they implicitly continue at 50 pixel intervals.
wrap
Boolean. If True (the default), text wraps within the width of the layout.
Other attributes can be used to store additional style information within the document; it will be ignored by the built-in text classes.
All style attributes (including those not present in a document) default to
None
(including the so-called “boolean” styles listed above). The meaning
of a None
style is style- and application-dependent.
- class AbstractDocument
Abstract document interface used by all
pyglet.text
classes.This class can be overridden to interface pyglet with a third-party document format. It may be easier to implement the document format in terms of one of the supplied concrete classes
FormattedDocument
orUnformattedDocument
.- __init__(text: str = '') None
Initialize a document with text.
- Parameters:
text (
str
) – Initial text string.
- delete_text(start: int, end: int) None
Delete text from the document.
Dispatches an
on_delete_text()
event.
- get_element(position: int) InlineElement
Get the element at a specified position.
- Parameters:
position (
int
) – Position in the document of the element.- Return type:
- abstract get_font( ) Font
Get the font instance used at the given position.
- abstract get_font_runs( ) AbstractRunIterator
Get a style iterator over the pyglet.font.Font instances used in the document.
The font instances are created on-demand by inspection of the
font_name
,font_size
,weight
anditalic
style attributes.
- get_paragraph_end(pos: int) int
Get the end position of a paragraph from the character position.
- Return type:
- get_paragraph_start(pos: int) int
Get the starting position of a paragraph from the character position.
- Return type:
- abstract get_style(attribute: str, position: int = 0) Any
Get an attribute style at the given position.
- get_style_range(attribute: str, start: int, end: int) Any
Get an attribute style over the given range.
If the style varies over the range, STYLE_INDETERMINATE is returned.
- abstract get_style_runs(
- attribute: str,
Get a style iterator over the given style attribute.
- Parameters:
attribute (
str
) – Name of style attribute to query.- Return type:
AbstractRunIterator
- insert_element( ) None
Insert a element into the document.
See the
InlineElement
class documentation for details of usage.
- insert_text( ) None
Insert text into the document.
Dispatches an
on_insert_text()
event.
- set_paragraph_style( ) None
Set the style for a range of paragraphs.
This is a convenience method for set_style that aligns the character range to the enclosing paragraph(s).
Dispatches an
on_style_text()
event.
- set_style( ) None
Set text style of a range between start and end of the document.
Dispatches an
on_style_text()
event.
- property text: str
Document text.
For efficient incremental updates, use the
insert_text()
anddelete_text()
methods instead of replacing this property.
- class FormattedDocument
Simple implementation of a document that maintains text formatting.
Changes to text style are applied according to the description in
AbstractDocument
. All styles default toNone
.- __init__(text: str = '') None
Initialize a document with text.
- Parameters:
text (
str
) – Initial text string.
- get_element_runs() _ElementIterator
- Return type:
_ElementIterator
- get_font( ) Font
Get the font instance used at the given position.
- get_font_runs( ) _FontStyleRunsRangeIterator
Get a style iterator over the pyglet.font.Font instances used in the document.
The font instances are created on-demand by inspection of the
font_name
,font_size
,weight
anditalic
style attributes.
- class InlineElement
Arbitrary inline element positioned within a formatted document.
Elements behave like a single glyph in the document. They are measured by their horizontal advance, ascent above the baseline, and descent below the baseline.
The pyglet layout classes reserve space in the layout for elements and call the element’s methods to ensure they are rendered at the appropriate position.
If the size of an element (any of the
advance
,ascent
, ordescent
variables) is modified it is the application’s responsibility to trigger a reflow of the appropriate area in the affected layouts. This can be done by forcing a style change over the element’s position.- abstract place(
- layout: TextLayout,
- x: float,
- y: float,
- z: float,
- line_x: float,
- line_y: float,
- rotation: float,
- visible: bool,
- anchor_x: float,
- anchor_y: float,
- Return type:
- abstract remove(layout: TextLayout) None
- Return type:
- class UnformattedDocument
A document having uniform style over all text.
Changes to the style of text within the document affects the entire document. For convenience, the
position
parameters of the style methods may therefore be omitted.- get_element_runs() ConstRunIterator
- Return type:
ConstRunIterator
- get_font( ) Font
Get the font instance used at the given position.
- get_font_runs( ) ConstRunIterator
Get a style iterator over the pyglet.font.Font instances used in the document.
The font instances are created on-demand by inspection of the
font_name
,font_size
,weight
anditalic
style attributes.
- get_style_runs(
- attribute: str,
Get a style iterator over the given style attribute.
- Parameters:
attribute (
str
) – Name of style attribute to query.- Return type:
ConstRunIterator
- set_paragraph_style( ) None
Set the style for a range of paragraphs.
This is a convenience method for set_style that aligns the character range to the enclosing paragraph(s).
Dispatches an
on_style_text()
event.
- set_style( ) None
Set text style of a range between start and end of the document.
Dispatches an
on_style_text()
event.
- STYLE_INDETERMINATE = 'indeterminate'
The style attribute takes on multiple values in the document.