Render simple text and formatted documents efficiently.
Three layout classes are provided:
TextLayout
The entire document is laid out before it is rendered. The layout will be grouped with other layouts in the same batch (allowing for efficient rendering of multiple layouts).
Any change to the layout or document, and even querying some properties, will cause the entire document to be laid out again.
ScrollableTextLayout
Based on TextLayout()
.
A separate group is used for layout which crops the contents of the
layout to the layout rectangle. Additionally, the contents of the
layout can be “scrolled” within that rectangle with the view_x
and
view_y
properties.
IncrementalTextLayout
Based on ScrollableTextLayout
.
When the layout or document are modified, only the affected regions are laid out again. This permits efficient interactive editing and styling of text.
Only the visible portion of the layout is actually rendered; as the viewport is scrolled additional sections are rendered and discarded as required. This permits efficient viewing and editing of large documents.
Additionally, this class provides methods for locating the position of a caret in the document, and for displaying interactive text selections.
All three layout classes can be used with either UnformattedDocument
or
FormattedDocument
, and can be either single-line or multiline
. The
combinations of these options effectively provides 12 different text display
possibilities.
The following character style attribute names are recognised by the layout classes. Data types and units are as specified.
Where an attribute is marked “as a distance” the value is assumed to be
in pixels if given as an int or float, otherwise a string of the form
"0u"
is required, where 0
is the distance and u
is the unit; one
of "px"
(pixels), "pt"
(points), "pc"
(picas), "cm"
(centimeters), "mm"
(millimeters) or "in"
(inches). For example,
"14pt"
is the distance covering 14 points, which at the default DPI of 96
is 18 pixels.
font_name
pyglet.font.load()
.font_size
bold
italic
underline
kerning
baseline
color
background_color
None
for no background fill.The following paragraph style attribute names are recognised. 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
or right
.indent
leading
line_spacing
None
, which automatically calculates the tightest line
spacing for each line based on the font ascent and descent.margin_left
margin_right
margin_top
margin_bottom
tab_stops
wrap
char
, word
, True (default) or False. The boundaries at which to
wrap text to prevent it overflowing a line. With char
, the line
wraps anywhere in the text; with word
or True, the line wraps at
appropriate boundaries between words; with False the line does not wrap,
and may overflow the layout width. char
and word
styles are
since pyglet 1.2.Other attributes can be used to store additional style information within the document; they will be ignored by the built-in text classes.
New in version 1.1.
IncrementalTextLayout
(document, width, height, multiline=False, dpi=None, batch=None, group=None, wrap_lines=True)¶Displayed text suitable for interactive editing and/or scrolling large documents.
Unlike TextLayout()
and ScrollableTextLayout
, this class generates
vertex lists only for lines of text that are visible. As the document is
scrolled, vertex lists are deleted and created as appropriate to keep
video memory usage to a minimum and improve rendering speed.
Changes to the document are quickly reflected in this layout, as only the affected line(s) are reflowed. Use begin_update and end_update to further reduce the amount of processing required.
The layout can also display a text selection (text with a different
background color). The Caret
class implements a visible text cursor and
provides event handlers for scrolling, selecting and editing text in an
incremental text layout.
delete
()¶ensure_line_visible
(line)¶Adjust view_y so that the line with the given index is visible.
Parameters: | line (int) – Line index. |
---|
ensure_x_visible
(x)¶Adjust view_x so that the given X coordinate is visible.
The X coordinate is given relative to the current view_x.
Parameters: | x (int) – X coordinate |
---|
get_line_count
()¶Get the number of lines in the text layout.
Return type: | int |
---|
get_line_from_point
(x, y)¶Get the closest line index to a point.
Parameters: |
|
---|---|
Return type: | int |
get_line_from_position
(position)¶Get the line index of a character position in the document.
Parameters: | position (int) – Document position. |
---|---|
Return type: | int |
get_point_from_line
(line)¶Get the X, Y coordinates of a line index.
Parameters: | line (int) – Line index. |
---|---|
Return type: | (int, int) |
Returns: | (x, y) |
get_point_from_position
(position, line=None)¶Get the X, Y coordinates of a position in the document.
The position that ends a line has an ambiguous point: it can be either the end of the line, or the beginning of the next line. You may optionally specify a line index to disambiguate the case.
The resulting Y coordinate gives the baseline of the line.
Parameters: |
|
---|---|
Return type: | (int, int) |
Returns: | (x, y) |
get_position_from_line
(line)¶Get the first document character position of a given line index.
Parameters: | line (int) – Line index. |
---|---|
Return type: | int |
get_position_from_point
(x, y)¶Get the closest document position to a point.
Parameters: |
|
---|
get_position_on_line
(line, x)¶Get the closest document position for a given line index and X coordinate.
Parameters: |
|
---|---|
Return type: | int |
on_delete_text
(start, end)¶on_insert_text
(start, text)¶on_layout_update
()¶Some or all of the layout text was reflowed.
Text reflow is caused by document edits or changes to the layout’s size. Changes to the layout’s position or active selection, and certain document edits such as text color, do not cause a reflow.
Handle this event to update the position of a graphical element that depends on the laid out position of a glyph or line.
on_style_text
(start, end, attributes)¶set_selection
(start, end)¶Set the text selection range.
If start
equals end
no selection will be visible.
Parameters: |
|
---|
event_types
= [‘on_layout_update’]¶height
¶multiline
¶selection_background_color
¶Background color of active selection.
The color is an RGBA tuple with components in range [0, 255].
Type: | (int, int, int, int) |
---|
selection_color
¶Text color of active selection.
The color is an RGBA tuple with components in range [0, 255].
Type: | (int, int, int, int) |
---|
selection_end
¶End position of the active selection (exclusive).
See: | set_selection |
---|---|
Type: | int |
selection_start
¶Starting position of the active selection.
See: | set_selection |
---|---|
Type: | int |
view_y
¶width
¶ScrollableTextLayout
(document, width, height, multiline=False, dpi=None, batch=None, group=None, wrap_lines=True)¶Display text in a scrollable viewport.
This class does not display a scrollbar or handle scroll events; it merely
clips the text that would be drawn in TextLayout()
to the bounds of the
layout given by x, y, width and height; and offsets the text by a
scroll offset.
Use view_x and view_y to scroll the text within the viewport.
anchor_x
¶anchor_y
¶height
¶view_x
¶Horizontal scroll offset.
The initial value is 0, and the left edge of the text will touch the left
side of the layout bounds. A positive value causes the text to “scroll”
to the right. Values are automatically clipped into the range
[0, content_width - width]
Type: | int |
---|
view_y
¶Vertical scroll offset.
The initial value is 0, and the top of the text will touch the top of the layout bounds (unless the content height is less than the layout height, in which case content_valign is used).
A negative value causes the text to “scroll” upwards. Values outside of
the range [height - content_height, 0]
are automatically clipped in
range.
Type: | int |
---|
width
¶x
¶y
¶ScrollableTextLayoutGroup
(parent=None)¶Top-level rendering group for ScrollableTextLayout
.
The group maintains internal state for setting the clipping planes and view transform for scrolling. Because the group has internal state specific to the text layout, the group is never shared.
set_state
()¶unset_state
()¶height
¶Height of the text layout.
Type: | int |
---|
left
¶Left edge of the text layout.
Type: | int |
---|
top
¶Top edge of the text layout (measured from the bottom of the graphics viewport).
Type: | int |
---|
translate_x
= 0¶translate_y
= 0¶view_x
¶Horizontal scroll offset.
Type: | int |
---|
view_y
¶Vertical scroll offset.
Type: | int |
---|
width
¶Width of the text layout.
Type: | int |
---|
TextLayout
(document, width=None, height=None, multiline=False, dpi=None, batch=None, group=None, wrap_lines=True)¶Lay out and display documents.
This class is intended for displaying documents that do not change regularly – any change will cost some time to lay out the complete document again and regenerate all vertex lists.
The benefit of this class is that texture state is shared between
all layouts of this class. The time to draw one TextLayout()
may be
roughly the same as the time to draw one IncrementalTextLayout
; but
drawing ten TextLayout()
objects in one batch is much faster than drawing
ten incremental or scrollable text layouts.
Label()
and HTMLLabel()
provide a convenient interface to this class.
Variables: |
|
---|
begin_update
()¶Indicate that a number of changes to the layout or document are about to occur.
Changes to the layout or document between calls to begin_update and end_update do not trigger any costly relayout of text. Relayout of all changes is performed when end_update is called.
Note that between the begin_update and end_update calls, values such as content_width and content_height are undefined (i.e., they may or may not be updated to reflect the latest changes).
delete
()¶Remove this layout from its batch.
draw
()¶Draw this text layout.
Note that this method performs very badly if a batch was supplied to the constructor. If you add this layout to a batch, you should ideally use only the batch’s draw method.
end_update
()¶Perform pending layout changes since begin_update.
See begin_update.
on_delete_text
(start, end)¶Event handler for AbstractDocument.on_delete_text.
The event handler is bound by the text layout; there is no need for applications to interact with this method.
on_insert_text
(start, text)¶Event handler for AbstractDocument.on_insert_text.
The event handler is bound by the text layout; there is no need for applications to interact with this method.
on_style_text
(start, end, attributes)¶Event handler for AbstractDocument.on_style_text.
The event handler is bound by the text layout; there is no need for applications to interact with this method.
anchor_x
¶Horizontal anchor alignment.
This property determines the meaning of the x coordinate. It is one of the enumerants:
"left"
(default)"center"
"right"
For the purposes of calculating the position resulting from this alignment, the width of the layout is taken to be width if multiline is True and wrap_lines is True, otherwise content_width.
Type: | str |
---|
anchor_y
¶Vertical anchor alignment.
This property determines the meaning of the y coordinate. It is one of the enumerants:
"top"
"center"
"baseline"
"bottom"
(default)For the purposes of calculating the position resulting from this alignment, the height of the layout is taken to be the smaller of height and content_height.
See also content_valign.
Type: | str |
---|
background_group
= OrderedGroup(0)¶batch
¶The Batch that this Layout is assigned to.
If no Batch is assigned, an internal Batch is created and used.
Type: | Batch |
---|
content_valign
¶Vertical alignment of content within larger layout box.
This property determines how content is positioned within the layout
box when content_height
is less than height
. It is one
of the enumerants:
top
(default)center
bottom
This property has no effect when content_height
is greater
than height
(in which case the content is aligned to the top) or when
height
is None
(in which case there is no vertical layout box
dimension).
Type: | str |
---|
document
¶Document to display.
For IncrementalTextLayout
it is far more efficient to modify a document
in-place than to replace the document instance on the layout.
Type: | AbstractDocument |
---|
dpi
¶Get DPI used by this layout.
Read-only.
Type: | float |
---|
foreground_decoration_group
= TextLayoutForegroundDecorationGroup(2)¶foreground_group
= TextLayoutForegroundGroup(1)¶height
¶Height of the layout.
Type: | int |
---|
multiline
¶Set if multiline layout is enabled.
If multiline is False, newline and paragraph characters are ignored and text is not word-wrapped. If True, the text is word-wrapped only if the wrap_lines is True.
Type: | bool |
---|
top_group
= <pyglet.text.layout.TextLayoutGroup object>¶width
¶Width of the layout.
This property has no effect if multiline is False or wrap_lines is False.
Type: | int |
---|
y
¶Y coordinate of the layout.
See also anchor_y.
Type: | int |
---|
TextLayoutForegroundDecorationGroup
(order, parent=None)¶Rendering group for decorative elements (e.g., glyph underlines) in all text layouts.
The group disables GL_TEXTURE_2D
.
set_state
()¶TextLayoutForegroundGroup
(order, parent=None)¶Rendering group for foreground elements (glyphs) in all text layouts.
The group enables GL_TEXTURE_2D
.
set_state
()¶TextLayoutGroup
(parent=None)¶Top-level rendering group for TextLayout()
.
The blend function is set for glyph rendering (GL_SRC_ALPHA
/
GL_ONE_MINUS_SRC_ALPHA
). The group is shared by all TextLayout()
instances as it has no internal state.
set_state
()¶unset_state
()¶