- class Text: dsfml.graphics.drawable.Drawable, dsfml.graphics.transformable.Transformable;
- Graphical text that can be drawn to a render target.
Text is a drawable class that allows to easily display some text with custom style and color on a render target.
It inherits all the functions from Transformable: position, rotation, scale, origin. It also adds text-specific properties such as the font to use, the character size, the font style (bold, italic, underlined), the global color and the text to display of course. It also provides convenience functions to calculate the graphical size of the text, or to get the global position of a given character.
Text works in combination with the Font class, which loads and provides the glyphs (visual characters) of a given font.
The separation of Font and Text allows more flexibility and better performances: indeed a Font is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, a Text is a lightweight object which can combine the glyphs data and metrics of a Font to display any text on a render target.
It is important to note that the Text instance doesn't copy the font that it uses, it only keeps a reference to it. Thus, a Font must not be destructed while it is used by a Text (i.e. never write a function that uses a local Font instance for creating a text).
Laurent Gomila, Jeremy DeHaan
- enum Style: int;
- Enumeration of the string drawing styles.
- const uint getCharacterSize();
- Get the character size.
Size of the characters, in pixels.
- const Color getColor();
- Get the global color of the text.
Global color of the text.
- const const(Font) getFont();
- Get thet text's font.
If the text has no font attached, a NULL pointer is returned. The returned reference is const, which means that you cannot modify the font when you get it from this function.
- FloatRect getGlobalBounds();
- Get the global bounding rectangle of the entity.
The returned rectangle is in global coordinates, which means that it takes in account the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the sprite in the global 2D world's coordinate system.
Global bounding rectangle of the entity.
- const FloatRect getLocalBounds();
- Get the local bounding rectangle of the entity.
The returned rectangle is in local coordinates, which means that it ignores the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the entity in the entity's coordinate system.
Local bounding rectangle of the entity.
- const immutable(T) getString(T = char)() if (is(T == dchar) || is(T == wchar) || is(T == char));
- Get the text's string.
The returned string is a dstring, a unicode type.
- const Style getStyle();
- Get the text's style.
- void setCharacterSize(uint size);
- Set the character size.
The default size is 30.
uint size New character size, in pixels.
- void setColor(Color color);
- Set the global color of the text.
By default, the text's color is opaque white.
Color color New color of the text.
- void setFont(const(Font) font);
- Set the text's font.
The font argument refers to a font that must exist as long as the text uses it. Indeed, the text doesn't store its own copy of the font, but rather keeps a pointer to the one that you passed to this function. If the font is destroyed and the text tries to use it, the behaviour is undefined.
const(Font) font New font
- void setString(T)(immutable(T) text) if (is(T == dchar) || is(T == wchar) || is(T == char));
- Set the text's string.
A text's string is empty by default.
immutable(T) text New string
- void setStyle(Style style);
- Set the text's style.
You can pass a combination of one or more styles, for example Style.Bold | Text.Italic.
- void draw(RenderTarget renderTarget, RenderStates renderStates);
- Draw the object to a render target.
RenderTarget renderTarget Render target to draw to RenderStates renderStates Current render states
- Vector2f findCharacterPos(size_t index);
- Return the position of the index-th character.
This function computes the visual position of a character from its index in the string. The returned position is in global coordinates (translation, rotation, scale and origin are applied). If index is out of range, the position of the end of the string is returned.
size_t index Index of the character
Position of the character.