Gamedev Framework (gf)  0.11.0 A C++14 framework for 2D games
gf::Text Class Reference

Graphical text that can be drawn to a render target. More...

#include <gf/Text.h>

Inheritance diagram for gf::Text:
[legend]

## Public Member Functions

Text ()
Default constructor. More...

Text (std::string string, Font &font, unsigned characterSize=30)
Construct the text from a string, font and size. More...

void setString (std::string string)
Set the text's string. More...

const std::string & getString () const
Get the text's string. More...

void setCharacterSize (unsigned characterSize)
Set the character size. More...

unsigned getCharacterSize () const
Get the character size. More...

void setFont (Font &font)
Set the text's font. More...

const FontgetFont () const
Get the text's font. More...

void setColor (const Color4f &color)
Set the fill color of the text. More...

const Color4fgetColor () const
Get the fill color of the text. More...

void setOutlineColor (const Color4f &color)
Set the outline color of the text. More...

const Color4fgetOutlineColor () const
Get the outline color of the text. More...

void setOutlineThickness (float thickness)
Set the thickness of the text's outline. More...

float getOutlineThickness ()
Get the outline thickness of the text. More...

void setLineSpacing (float spacingFactor)
Set the line spacing factor. More...

float getLineSpacing () const
Get the size of the line spacing factor. More...

void setLetterSpacing (float spacingFactor)
Set the letter spacing factor. More...

float getLetterSpacing () const
Get the size of the letter spacing factor. More...

void setParagraphWidth (float paragraphWidth)
Set the paragraph width for aligned text. More...

float getParagraphWidth () const
Get the paragraph width. More...

void setAlignment (Alignment align)
Set the alignement of the text. More...

Alignment getAlignment () const
Get the alignment of the text. More...

RectF getLocalBounds () const
Get the local bounding rectangle of the entity. More...

void setAnchor (Anchor anchor)
Set the anchor origin of the entity. More...

VertexBuffer commitGeometry () const
Create a buffer with the current geometry. More...

VertexBuffer commitOutlineGeometry () const
Create a buffer with the current outline geometry. More...

virtual void draw (RenderTarget &target, const RenderStates &states) override
Draw the object to a render target. More...

Public Member Functions inherited from gf::Transformable
Transformable ()
Default constructor. More...

void setOrigin (Vector2f origin)
Set the local origin of the object. More...

Vector2f getOrigin () const
Get the local origin of the object. More...

void setPosition (Vector2f position)
Set the position of the object. More...

Vector2f getPosition () const
Get the position of the object. More...

void move (Vector2f offset)
Move the object by a given offset. More...

void setRotation (float angle)
Set the orientation of the object. More...

float getRotation () const
Get the orientation of the object. More...

void rotate (float angle)
Rotate the object. More...

void setScale (Vector2f factors)
Set the scale factors of the object. More...

void setScale (float factor)
Set the scale factor of the object. More...

Vector2f getScale () const
Get the current scale of the object. More...

void scale (Vector2f factors)
Scale the object. More...

void scale (float factor)
Scale the object. More...

Matrix3f getTransform () const
Get the combined transform of the object. More...

Matrix3f getInverseTransform () const
Get the inverse of the combined transform of the object. More...

Public Member Functions inherited from gf::Drawable
virtual ~Drawable ()
Virtual desctructor. More...

Protected Member Functions inherited from gf::Transformable
void setOriginFromAnchorAndBounds (Anchor anchor, const RectF &bounds)
Set the origin from an anchor and bounds. More...

## Detailed Description

Graphical text that can be drawn to a render target.

gf::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 gf::Transformable: position, rotation, scale, origin. It also adds text-specific properties such as the font to use, the character size, the global color and the text to display of course. It also provides convenience functions to calculate the graphical size of the text.

gf::Text works in combination with the gf::Font class, which loads and provides the glyphs (visual characters) of a given font.

The separation of gf::Font and gf::Text allows more flexibility and better performances: indeed a gf::Font is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, a gf::Text is a lightweight object which can combine the glyphs data and metrics of a gf::Font to display any text on a render target.

It is important to note that the gf::Text instance doesn't copy the font that it uses, it only keeps a reference to it. Thus, a gf::Font must not be destructed while it is used by a gf::Text (i.e. never write a function that uses a local gf::Font instance for creating a text).

Usage example:

// Declare and load a font
gf::Font font;
// Create a text
gf::Text text("hello", font);
text.setCharacterSize(30);
text.setColor(gf::Color::Red);
// Draw it
rendered.draw(text);
gf::Font

## ◆ Text() [1/2]

 gf::Text::Text ( )

Default constructor.

Creates an empty text.

## ◆ Text() [2/2]

 gf::Text::Text ( std::string string, Font & font, unsigned characterSize = 30 )

Construct the text from a string, font and size.

Parameters
 string Text assigned to the string in UTF-8 format font Font used to draw the string characterSize Base size of characters, in pixels

## ◆ commitGeometry()

 VertexBuffer gf::Text::commitGeometry ( ) const

Create a buffer with the current geometry.

The geometry is uploaded in the graphics memory so that it's faster to draw.

Returns
A buffer with the current geometry

## ◆ commitOutlineGeometry()

 VertexBuffer gf::Text::commitOutlineGeometry ( ) const

Create a buffer with the current outline geometry.

The geometry is uploaded in the graphics memory so that it's faster to draw.

Returns
A buffer with the current outline geometry

## ◆ draw()

 virtual void gf::Text::draw ( RenderTarget & target, const RenderStates & states )
overridevirtual

Draw the object to a render target.

This is a pure virtual function that has to be implemented by the derived class to define how the drawable should be drawn.

Parameters
 target Render target to draw to states Current render states

Implements gf::Drawable.

## ◆ getAlignment()

 Alignment gf::Text::getAlignment ( ) const
inline

Get the alignment of the text.

Returns
Current alignment of the text
setAlignment()

## ◆ getCharacterSize()

 unsigned gf::Text::getCharacterSize ( ) const
inline

Get the character size.

Returns
Size of the characters, in pixels
setCharacterSize()

## ◆ getColor()

 const Color4f& gf::Text::getColor ( ) const
inline

Get the fill color of the text.

Returns
Fill color of the text
setColor()

## ◆ getFont()

 const Font* gf::Text::getFont ( ) const
inline

Get the text's font.

If the text has no font attached, a nullptr pointer is returned. The returned pointer is const, which means that you cannot modify the font when you get it from this function.

Returns
Pointer to the text's font
setFont()

## ◆ getLetterSpacing()

 float gf::Text::getLetterSpacing ( ) const
inline

Get the size of the letter spacing factor.

Returns
The size of the letter spacing factor
setLetterSpacing()

## ◆ getLineSpacing()

 float gf::Text::getLineSpacing ( ) const
inline

Get the size of the line spacing factor.

Returns
The size of the line spacing factor
setLineSpacing

## ◆ getLocalBounds()

 RectF gf::Text::getLocalBounds ( ) const
inline

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.

Returns
Local bounding rectangle of the entity

## ◆ getOutlineColor()

 const Color4f& gf::Text::getOutlineColor ( ) const
inline

Get the outline color of the text.

Returns
Outline color of the text
setOutlineColor()

## ◆ getOutlineThickness()

 float gf::Text::getOutlineThickness ( )
inline

Get the outline thickness of the text.

Returns
Outline thickness of the text, in pixels
setOutlineThickness()

## ◆ getParagraphWidth()

 float gf::Text::getParagraphWidth ( ) const
inline

Get the paragraph width.

Returns
Paragraph width in pixels
setParagraphWidth()

## ◆ getString()

 const std::string& gf::Text::getString ( ) const
inline

Get the text's string.

The text string is in UTF-8 format.

Returns
Text's string
setString()

## ◆ setAlignment()

 void gf::Text::setAlignment ( Alignment align )

Set the alignement of the text.

By default, the text is not aligned.

Parameters
 align New alignement
getAlignment()

## ◆ setAnchor()

 void gf::Text::setAnchor ( Anchor anchor )

Set the anchor origin of the entity.

Compute the origin of the entity based on the local bounds and the specified anchor. Internally, this function calls Transformable::setOrigin().

Parameters
 anchor The anchor of the entity
getLocalBounds(), Transformable::setOrigin()

## ◆ setCharacterSize()

 void gf::Text::setCharacterSize ( unsigned characterSize )

Set the character size.

The default size is 30.

Parameters
 characterSize New character size, in pixels
getCharacterSize()

## ◆ setColor()

 void gf::Text::setColor ( const Color4f & color )

Set the fill color of the text.

By default, the text's fill color is opaque black. Setting the fill color to a transparent color with an outline will cause the outline to be displayed in the fill area of the text.

Parameters
 color New fill color of the text
getColor()

## ◆ setFont()

 void gf::Text::setFont ( 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 behavior is undefined.

Parameters
 font New font
getFont()

## ◆ setLetterSpacing()

 void gf::Text::setLetterSpacing ( float spacingFactor )

Set the letter spacing factor.

The default spacing between letters is defined by the font. This factor doesn't directly apply to the existing spacing between each character, it rather adds a fixed space between them which is calculated from the font metrics and the character size. Note that factors below 1 (including negative numbers) bring characters closer to each other. By default the letter spacing factor is 1.

Parameters
 spacingFactor New letter spacing factor
getLetterSpacing()

## ◆ setLineSpacing()

 void gf::Text::setLineSpacing ( float spacingFactor )

Set the line spacing factor.

The default spacing between lines is defined by the font. This method enables you to set a factor for the spacing between lines. By default the line spacing factor is 1.

Parameters
 spacingFactor New line spacing factor
getLineSpacing()

## ◆ setOutlineColor()

 void gf::Text::setOutlineColor ( const Color4f & color )

Set the outline color of the text.

By default, the text's outline color is opaque black.

Parameters
 color New outline color of the text
getOutlineColor()

## ◆ setOutlineThickness()

 void gf::Text::setOutlineThickness ( float thickness )

Set the thickness of the text's outline.

By default, the outline thickness is 0.

Parameters
 thickness New outline thickness, in pixels
getOutlineThickness()

## ◆ setParagraphWidth()

 void gf::Text::setParagraphWidth ( float paragraphWidth )

Set the paragraph width for aligned text.

By default, the paragraph width is 0.

Parameters
 paragraphWidth New paragraph width in pixels
getParagraphWidth()

## ◆ setString()

 void gf::Text::setString ( std::string string )

Set the text's string.

The text string is in UTF-8 format. A text's string is empty by default.

Parameters
 string New string in UTF-8 format