Difficult-Rocket/libs/pyglet/text/__init__.py

526 lines
17 KiB
Python
Raw Normal View History

2021-04-16 23:21:06 +08:00
"""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.
.. versionadded:: 1.1
"""
2022-07-16 20:20:23 +08:00
from os.path import dirname as _dirname
from os.path import splitext as _splitext
2021-04-16 23:21:06 +08:00
import pyglet
2022-07-16 20:20:23 +08:00
2021-04-16 23:21:06 +08:00
from pyglet.text import layout, document, caret
class DocumentDecodeException(Exception):
"""An error occurred decoding document text."""
pass
class DocumentDecoder:
"""Abstract document decoder.
"""
def decode(self, text, location=None):
"""Decode document text.
:Parameters:
`text` : str
Text to decode
`location` : `Location`
Location to use as base path for additional resources
referenced within the document (for example, HTML images).
:rtype: `AbstractDocument`
"""
raise NotImplementedError('abstract')
def get_decoder(filename, mimetype=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`
`DocumentDecodeException` is raised if another MIME type is given.
:Parameters:
`filename` : str
Filename to guess the MIME type from. If a MIME type is given,
the filename is ignored.
`mimetype` : str
MIME type to lookup, or ``None`` to guess the type from the
filename.
:rtype: `DocumentDecoder`
"""
if mimetype is None:
2022-07-16 20:20:23 +08:00
_, ext = _splitext(filename)
2021-04-16 23:21:06 +08:00
if ext.lower() in ('.htm', '.html', '.xhtml'):
mimetype = 'text/html'
else:
mimetype = 'text/plain'
if mimetype == 'text/plain':
from pyglet.text.formats import plaintext
return plaintext.PlainTextDecoder()
elif mimetype == 'text/html':
from pyglet.text.formats import html
return html.HTMLDecoder()
elif mimetype == 'text/vnd.pyglet-attributed':
from pyglet.text.formats import attributed
return attributed.AttributedTextDecoder()
else:
2023-01-25 20:38:17 +08:00
raise DocumentDecodeException(f'Unknown format "{mimetype}"')
2021-04-16 23:21:06 +08:00
def load(filename, file=None, mimetype=None):
"""Load a document from a file.
:Parameters:
`filename` : str
Filename of document to load.
`file` : file-like object
File object containing encoded data. If omitted, `filename` is
loaded from disk.
`mimetype` : str
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.
:rtype: `AbstractDocument`
"""
decoder = get_decoder(filename, mimetype)
if not file:
with open(filename) as f:
file_contents = f.read()
else:
file_contents = file.read()
file.close()
if hasattr(file_contents, "decode"):
file_contents = file_contents.decode()
2022-07-16 20:20:23 +08:00
location = pyglet.resource.FileLocation(_dirname(filename))
2021-04-16 23:21:06 +08:00
return decoder.decode(file_contents, location)
def decode_html(text, location=None):
"""Create a document directly from some HTML formatted text.
:Parameters:
`text` : str
HTML data to decode.
`location` : str
Location giving the base path for additional resources
referenced from the document (e.g., images).
:rtype: `FormattedDocument`
"""
decoder = get_decoder(None, 'text/html')
return decoder.decode(text, location)
def decode_attributed(text):
"""Create a document directly from some attributed text.
See `pyglet.text.formats.attributed` for a description of attributed text.
:Parameters:
`text` : str
Attributed text to decode.
:rtype: `FormattedDocument`
"""
decoder = get_decoder(None, 'text/vnd.pyglet-attributed')
return decoder.decode(text)
def decode_text(text):
"""Create a document directly from some plain text.
:Parameters:
`text` : str
Plain text to initialise the document with.
:rtype: `UnformattedDocument`
"""
decoder = get_decoder(None, 'text/plain')
return decoder.decode(text)
class DocumentLabel(layout.TextLayout):
"""Base label class.
A label is a layout that exposes convenience methods for manipulating the
associated document.
"""
def __init__(self, document=None,
2022-10-23 19:56:14 +08:00
x=0, y=0, z=0, width=None, height=None,
2021-04-16 23:21:06 +08:00
anchor_x='left', anchor_y='baseline',
2022-08-29 13:21:35 +08:00
multiline=False, dpi=None, batch=None, group=None, rotation=0):
2021-04-16 23:21:06 +08:00
"""Create a label for a given document.
:Parameters:
`document` : `AbstractDocument`
Document to attach to the layout.
`x` : int
X coordinate of the label.
`y` : int
Y coordinate of the label.
2022-10-23 19:56:14 +08:00
`z` : int
Z coordinate of the label.
2021-04-16 23:21:06 +08:00
`width` : int
Width of the label in pixels, or None
`height` : int
Height of the label in pixels, or None
`anchor_x` : str
Anchor point of the X coordinate: one of ``"left"``,
``"center"`` or ``"right"``.
`anchor_y` : str
Anchor point of the Y coordinate: one of ``"bottom"``,
``"baseline"``, ``"center"`` or ``"top"``.
`multiline` : bool
If True, the label will be word-wrapped and accept newline
characters. You must also set the width of the label.
`dpi` : float
Resolution of the fonts in this layout. Defaults to 96.
`batch` : `~pyglet.graphics.Batch`
Optional graphics batch to add the label to.
`group` : `~pyglet.graphics.Group`
Optional graphics group to use.
2022-08-29 13:21:35 +08:00
`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.
2021-04-16 23:21:06 +08:00
"""
2022-07-16 20:20:23 +08:00
super().__init__(document, width, height, multiline, dpi, batch, group)
2021-04-16 23:21:06 +08:00
self._x = x
self._y = y
2022-10-23 19:56:14 +08:00
self._z = z
2022-08-29 13:21:35 +08:00
self._rotation = rotation
2021-04-16 23:21:06 +08:00
self._anchor_x = anchor_x
self._anchor_y = anchor_y
self._update()
@property
def text(self):
"""The text of the label.
:type: str
"""
return self.document.text
@text.setter
def text(self, text):
self.document.text = text
@property
def color(self):
"""Text color.
Color is a 4-tuple of RGBA components, each in range [0, 255].
:type: (int, int, int, int)
"""
return self.document.get_style('color')
@color.setter
def color(self, color):
r, g, b, *a = color
color = r, g, b, a[0] if a else 255
self.document.set_style(0, len(self.document.text), {'color': color})
@property
def opacity(self):
"""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
2021-12-26 23:06:03 +08:00
make the label appear semi-translucent.
:type: int
"""
2021-10-28 06:43:35 +08:00
return self.color[3]
@opacity.setter
def opacity(self, alpha):
2021-10-28 06:43:35 +08:00
if alpha != self.color[3]:
self.color = list(map(int, (*self.color[:3], alpha)))
2021-04-16 23:21:06 +08:00
@property
def font_name(self):
"""Font family name.
The font name, as passed to :py:func:`pyglet.font.load`. A list of names can
optionally be given: the first matching font will be used.
:type: str or list
"""
return self.document.get_style('font_name')
@font_name.setter
def font_name(self, font_name):
self.document.set_style(0, len(self.document.text), {'font_name': font_name})
2021-04-16 23:21:06 +08:00
@property
def font_size(self):
"""Font size, in points.
:type: float
"""
return self.document.get_style('font_size')
@font_size.setter
def font_size(self, font_size):
2022-07-16 20:20:23 +08:00
self.document.set_style(0, len(self.document.text), {'font_size': font_size})
2021-04-16 23:21:06 +08:00
@property
def bold(self):
"""Bold font style.
:type: bool
"""
return self.document.get_style('bold')
@bold.setter
def bold(self, bold):
2022-07-16 20:20:23 +08:00
self.document.set_style(0, len(self.document.text), {'bold': bold})
2021-04-16 23:21:06 +08:00
@property
def italic(self):
"""Italic font style.
:type: bool
"""
return self.document.get_style('italic')
@italic.setter
def italic(self, italic):
2022-07-16 20:20:23 +08:00
self.document.set_style(0, len(self.document.text), {'italic': italic})
2021-04-16 23:21:06 +08:00
def get_style(self, name):
"""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.
:Parameters:
`name` : str
Style name to query. See documentation for
`pyglet.text.layout` for known style names.
:rtype: object
"""
return self.document.get_style_range(name, 0, len(self.document.text))
def set_style(self, name, value):
"""Set a document style value by name over the whole document.
:Parameters:
`name` : str
Name of the style to set. See documentation for
`pyglet.text.layout` for known style names.
`value` : object
Value of the style.
"""
self.document.set_style(0, len(self.document.text), {name: value})
2022-07-16 20:20:23 +08:00
def __del__(self):
self.delete()
2021-04-16 23:21:06 +08:00
class Label(DocumentLabel):
"""Plain text label.
"""
def __init__(self, text='',
2021-04-17 01:14:38 +08:00
font_name=None, font_size=None, bold=False, italic=False, stretch=False,
2021-04-16 23:21:06 +08:00
color=(255, 255, 255, 255),
2022-10-23 19:56:14 +08:00
x=0, y=0, z=0, width=None, height=None,
2021-04-16 23:21:06 +08:00
anchor_x='left', anchor_y='baseline',
align='left',
2022-08-29 13:21:35 +08:00
multiline=False, dpi=None, batch=None, group=None, rotation=0):
2021-04-16 23:21:06 +08:00
"""Create a plain text label.
:Parameters:
`text` : str
Text to display.
`font_name` : str or list
Font family name(s). If more than one name is given, the
first matching name is used.
`font_size` : float
Font size, in points.
2021-04-17 01:14:38 +08:00
`bold` : bool/str
2021-04-16 23:21:06 +08:00
Bold font style.
2021-04-17 01:14:38 +08:00
`italic` : bool/str
2021-04-16 23:21:06 +08:00
Italic font style.
2021-04-17 01:14:38 +08:00
`stretch` : bool/str
Stretch font style.
2021-04-16 23:21:06 +08:00
`color` : (int, int, int, int)
Font colour, as RGBA components in range [0, 255].
`x` : int
X coordinate of the label.
`y` : int
Y coordinate of the label.
2022-10-23 19:56:14 +08:00
`z` : int
Z coordinate of the label.
2021-04-16 23:21:06 +08:00
`width` : int
Width of the label in pixels, or None
`height` : int
Height of the label in pixels, or None
`anchor_x` : str
Anchor point of the X coordinate: one of ``"left"``,
``"center"`` or ``"right"``.
`anchor_y` : str
Anchor point of the Y coordinate: one of ``"bottom"``,
``"baseline"``, ``"center"`` or ``"top"``.
`align` : str
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` : float
Resolution of the fonts in this layout. Defaults to 96.
`batch` : `~pyglet.graphics.Batch`
Optional graphics batch to add the label to.
`group` : `~pyglet.graphics.Group`
Optional graphics group to use.
2022-08-29 13:21:35 +08:00
`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.
2021-04-16 23:21:06 +08:00
"""
2022-07-16 20:20:23 +08:00
doc = decode_text(text)
2022-10-23 19:56:14 +08:00
super().__init__(doc, x, y, z, width, height, anchor_x, anchor_y, multiline, dpi, batch, group, rotation)
2021-04-16 23:21:06 +08:00
self.document.set_style(0, len(self.document.text), {
'font_name': font_name,
'font_size': font_size,
'bold': bold,
'italic': italic,
2021-04-17 01:14:38 +08:00
'stretch': stretch,
2021-04-16 23:21:06 +08:00
'color': color,
'align': align,
})
class HTMLLabel(DocumentLabel):
"""HTML formatted text label.
A subset of HTML 4.01 is supported. See `pyglet.text.formats.html` for
details.
"""
def __init__(self, text='', location=None,
2022-10-23 19:56:14 +08:00
x=0, y=0, z=0, width=None, height=None,
2021-04-16 23:21:06 +08:00
anchor_x='left', anchor_y='baseline',
2022-08-29 13:21:35 +08:00
multiline=False, dpi=None, batch=None, group=None, rotation=0):
2021-04-16 23:21:06 +08:00
"""Create a label with an HTML string.
:Parameters:
`text` : str
HTML formatted text to display.
`location` : `Location`
Location object for loading images referred to in the
document. By default, the working directory is used.
`x` : int
X coordinate of the label.
`y` : int
Y coordinate of the label.
2022-10-23 19:56:14 +08:00
`z` : int
Z coordinate of the label.
2021-04-16 23:21:06 +08:00
`width` : int
Width of the label in pixels, or None
`height` : int
Height of the label in pixels, or None
`anchor_x` : str
Anchor point of the X coordinate: one of ``"left"``,
``"center"`` or ``"right"``.
`anchor_y` : str
Anchor point of the Y coordinate: one of ``"bottom"``,
``"baseline"``, ``"center"`` or ``"top"``.
`multiline` : bool
If True, the label will be word-wrapped and render paragraph
and line breaks. You must also set the width of the label.
`dpi` : float
Resolution of the fonts in this layout. Defaults to 96.
`batch` : `~pyglet.graphics.Batch`
Optional graphics batch to add the label to.
`group` : `~pyglet.graphics.Group`
Optional graphics group to use.
2022-08-29 13:21:35 +08:00
`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.
2021-04-16 23:21:06 +08:00
"""
self._text = text
self._location = location
2022-07-16 20:20:23 +08:00
doc = decode_html(text, location)
2022-10-23 19:56:14 +08:00
super().__init__(doc, x, y, z, width, height, anchor_x, anchor_y, multiline, dpi, batch, group, rotation)
2021-04-16 23:21:06 +08:00
@property
def text(self):
"""HTML formatted text of the label.
:type: str
"""
return self._text
@text.setter
def text(self, text):
self._text = text
self.document = decode_html(text, self._location)