Load fonts and render text.
This is a fairly-low level interface to text rendering. Obtain a font using load()
:
from pyglet import font
arial = font.load('Arial', 14, bold=True, italic=False)
Manually loading fonts is only required in the following situations:
pyglet.font.text
.You are encouraged to use pyglet.text
for actual text rendering. Classes in this module will
handle font loading for you, so manual loading is not required.
pyglet will automatically load any system-installed fonts. You can add additional fonts
(for example, from your program resources) using add_file()
or
add_directory()
. These fonts are then available in the same way as system-installed fonts:
from pyglet import font
font.add_file('action_man.ttf')
action_man = font.load('Action Man', 16)
See the pyglet.font.base
module for documentation on the base classes used
by this package.
Text
(font, text=”, x=0, y=0, z=0, color=(1, 1, 1, 1), width=None, halign=’left’, valign=’baseline’)¶Simple displayable text.
This is a convenience class for rendering strings of text. It takes care of caching the vertices so the text can be rendered every frame with little performance penalty.
Text can be word-wrapped by specifying a width to wrap into. If the width is not specified, it gives the width of the text as laid out.
Warning
Deprecated. Use pyglet.text.Label
.
draw
()¶BASELINE
= ‘baseline’¶BOTTOM
= ‘bottom’¶CENTER
= ‘center’¶LEFT
= ‘left’¶RIGHT
= ‘right’¶TOP
= ‘top’¶color
¶font
¶halign
¶Horizontal alignment of the text.
The text is positioned relative to x and width according to this property, which must be one of the alignment constants LEFT, CENTER or RIGHT.
Type: | str |
---|
height
¶Height of the text.
This property is the ascent minus the descent of the font, unless there is more than one line of word-wrapped text, in which case the height takes into account the line leading. Read-only.
Type: | float |
---|
leading
¶Vertical space between adjacent lines, in pixels.
Type: | int |
---|
line_height
¶Vertical distance between adjacent baselines, in pixels.
Type: | int |
---|
text
¶Text to render.
The glyph vertices are only recalculated as needed, so multiple changes to the text can be performed with no performance penalty.
Type: | str |
---|
valign
¶Vertical alignment of the text.
The text is positioned relative to y according to this property, which must be one of the alignment constants BOTTOM, BASELINE, CENTER or TOP.
Type: | str |
---|
width
¶Width of the text.
When set, this enables word-wrapping to the specified width. Otherwise, the width of the text as it will be rendered can be determined.
Type: | float |
---|
x
¶X coordinate of the text
y
¶Y coordinate of the text
z
¶add_file
(font)¶Add a font to pyglet’s search path.
In order to load a font that is not installed on the system, you must call this method to tell pyglet that it exists. You can supply either a filename or any file-like object.
The font format is platform-dependent, but is typically a TrueType font
file containing a single font face. Note that to use a font added with this method,
you should pass the face name (not the file name) to :meth:pyglet.font.load()
or any
other place where you normally specify a font.
Parameters: | font (str or file-like object) – Filename or file-like object to load fonts from. |
---|
add_directory
(dir)¶Add a directory of fonts to pyglet’s search path.
This function simply calls pyglet.font.add_file()
for each file with a .ttf
extension in the given directory. Subdirectories are not searched.
Parameters: | dir (str) – Directory that contains font files. |
---|
load
(name=None, size=None, bold=False, italic=False, dpi=None)¶Load a font for rendering.
Parameters: |
|
---|---|
Return type: | Font |
have_font
(name)¶Check if specified system font name is available.