sf::Text Class Reference
[Graphics module]

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

#include <Text.hpp>

Inheritance diagram for sf::Text:
sf::Drawable

List of all members.

Public Types

enum  Style {
  Regular = 0,
  Bold = 1 << 0,
  Italic = 1 << 1,
  Underlined = 1 << 2
}
 

Enumeration of the string drawing styles.

More...

Public Member Functions

 Text ()
 Default constructor.
 Text (const String &string, const Font &font=Font::GetDefaultFont(), unsigned int characterSize=30)
 Construct the string from a string, font and size.
void SetString (const String &string)
 Set the text's string.
void SetFont (const Font &font)
 Set the text's font.
void SetCharacterSize (unsigned int size)
 Set the character size.
void SetStyle (unsigned long style)
 Set the text's style.
const StringGetString () const
 Get the text's string.
const FontGetFont () const
 Get the text's font.
unsigned int GetCharacterSize () const
 Get the character size.
unsigned long GetStyle () const
 Get the text's style.
Vector2f GetCharacterPos (std::size_t index) const
 Return the position of the index-th character.
FloatRect GetRect () const
 Get the bounding rectangle of the text.
void SetPosition (float x, float y)
 Set the position of the object.
void SetPosition (const Vector2f &position)
 Set the position of the object.
void SetX (float x)
 Set the X position of the object.
void SetY (float y)
 Set the Y position of the object.
void SetScale (float factorX, float factorY)
 Set the scale factors of the object.
void SetScale (const Vector2f &factors)
 Set the scale factors of the object.
void SetScaleX (float factor)
 Set the X scale factor of the object.
void SetScaleY (float factor)
 Set the Y scale factor of the object.
void SetOrigin (float x, float y)
 Set the local origin of the object.
void SetOrigin (const Vector2f &origin)
 Set the local origin of the object.
void SetRotation (float angle)
 Set the orientation of the object.
void SetColor (const Color &color)
 Set the global color of the object.
void SetBlendMode (Blend::Mode mode)
 Set the blending mode of the object.
const Vector2fGetPosition () const
 Get the position of the object.
const Vector2fGetScale () const
 Get the current scale of the object.
const Vector2fGetOrigin () const
 Get the local origin of the object.
float GetRotation () const
 Get the orientation of the object.
const ColorGetColor () const
 Get the color of the object.
Blend::Mode GetBlendMode () const
 Get the blend mode of the object.
void Move (float offsetX, float offsetY)
 Move the object by a given offset.
void Move (const Vector2f &offset)
 Move the object by a given offset.
void Scale (float factorX, float factorY)
 Scale the object.
void Scale (const Vector2f &factor)
 Scale the object.
void Rotate (float angle)
 Rotate the object.
Vector2f TransformToLocal (const Vector2f &point) const
 Transform a point in object local coordinates.
Vector2f TransformToGlobal (const Vector2f &point) const
 Transform a local point in global coordinates.

Protected Member Functions

virtual void Render (RenderTarget &target, Renderer &renderer) const
 Draw the object to a render target.
const Matrix3GetMatrix () const
 Get the transform matrix of the object.
const Matrix3GetInverseMatrix () const
 Get the inverse transform matrix of the object.

Detailed Description

Graphical text that can be drawn to a render target.

sf::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 sf::Drawable: position, rotation, scale, origin, global color and blend mode. It also adds text-specific properties such as the font to use, the character size, the font style (bold, italic, underlined), and the text to display of course. It also provides convenience functions to calculate the graphical size of the text, or to get the visual position of a given character.

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

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

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

Usage example:

 // Declare and load a font
 sf::Font font;
 font.LoadFromFile("arial.ttf");
 
 // Create a text
 sf::Text text("hello");
 text.SetFont(font);
 text.SetCharacterSize(30);
 text.SetStyle(sf::Text::Regular);

 // Display it
 window.Draw(text); // window is a sf::RenderWindow
See also:
sf::Font

Definition at line 45 of file Text.hpp.


Member Enumeration Documentation

Enumeration of the string drawing styles.

Enumerator:
Regular 

Regular characters, no style.

Bold 

Bold characters.

Italic 

Italic characters.

Underlined 

Underlined characters.

Definition at line 53 of file Text.hpp.


Constructor & Destructor Documentation

sf::Text::Text (  ) 

Default constructor.

Creates an empty text.

sf::Text::Text ( const String string,
const Font font = Font::GetDefaultFont(),
unsigned int  characterSize = 30 
) [explicit]

Construct the string from a string, font and size.

Parameters:
string Text assigned to the string
font Font used to draw the string
characterSize Base size of characters, in pixels

Member Function Documentation

Blend::Mode sf::Drawable::GetBlendMode (  )  const [inherited]

Get the blend mode of the object.

Returns:
Current blend mode
See also:
SetBlendMode
Vector2f sf::Text::GetCharacterPos ( std::size_t  index  )  const

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 local coordinates (translation, rotation, scale and origin are not applied). You can easily get the corresponding global position with the TransformToGlobal function. If index is out of range, the position of the end of the string is returned.

Parameters:
index Index of the character
Returns:
Position of the character
unsigned int sf::Text::GetCharacterSize (  )  const

Get the character size.

Returns:
Size of the characters, in pixels
See also:
SetCharacterSize
const Color& sf::Drawable::GetColor (  )  const [inherited]

Get the color of the object.

Returns:
Current color
See also:
SetColor
const Font& sf::Text::GetFont (  )  const

Get the text's font.

The returned reference is const, which means that you cannot modify the font when you get it from this function.

Returns:
Text's font
See also:
SetFont
const Matrix3& sf::Drawable::GetInverseMatrix (  )  const [protected, inherited]

Get the inverse transform matrix of the object.

Returns:
Inverse transform matrix
See also:
GetMatrix
const Matrix3& sf::Drawable::GetMatrix (  )  const [protected, inherited]

Get the transform matrix of the object.

Returns:
Transform matrix
See also:
GetInverseMatrix
const Vector2f& sf::Drawable::GetOrigin (  )  const [inherited]

Get the local origin of the object.

Returns:
Current origin
See also:
SetOrigin
const Vector2f& sf::Drawable::GetPosition (  )  const [inherited]

Get the position of the object.

Returns:
Current position
See also:
SetPosition
FloatRect sf::Text::GetRect (  )  const

Get the bounding rectangle of the text.

The returned rectangle is in global coordinates.

Returns:
Bounding rectangle of the text
float sf::Drawable::GetRotation (  )  const [inherited]

Get the orientation of the object.

The rotation is always in the range [0, 360].

Returns:
Current rotation, in degrees
See also:
SetRotation
const Vector2f& sf::Drawable::GetScale (  )  const [inherited]

Get the current scale of the object.

Returns:
Current scale factors
See also:
SetScale
const String& sf::Text::GetString (  )  const

Get the text's string.

The returned string is a sf::String, which can automatically be converted to standard string types. So, the following lines of code are all valid:

 sf::String   s1 = text.GetString();
 std::string  s2 = text.GetString();
 std::wstring s3 = text.GetString();
Returns:
Text's string
See also:
GetString
unsigned long sf::Text::GetStyle (  )  const

Get the text's style.

Returns:
Text's style
See also:
SetStyle
void sf::Drawable::Move ( const Vector2f offset  )  [inherited]

Move the object by a given offset.

This function adds to the current position of the object, unlike SetPosition which overwrites it. Thus, it is equivalent to the following code:

 object.SetPosition(object.GetPosition() + offset);
Parameters:
offset Offset
See also:
SetPosition
void sf::Drawable::Move ( float  offsetX,
float  offsetY 
) [inherited]

Move the object by a given offset.

This function adds to the current position of the object, unlike SetPosition which overwrites it. Thus, it is equivalent to the following code:

 sf::Vector2f pos = object.GetPosition();
 object.SetPosition(pos.x + offsetX, pos.y + offsetY);
Parameters:
offsetX X offset
offsetY Y offset
See also:
SetPosition
virtual void sf::Text::Render ( RenderTarget target,
Renderer renderer 
) const [protected, virtual]

Draw the object to a render target.

Parameters:
target Render target
renderer Renderer providing low-level rendering commands

Implements sf::Drawable.

void sf::Drawable::Rotate ( float  angle  )  [inherited]

Rotate the object.

This function ads to the current rotation of the object, unlike SetRotation which overwrites it. Thus, it is equivalent to the following code:

 object.SetRotation(object.GetRotation() + angle);
Parameters:
angle Angle of rotation, in degrees
void sf::Drawable::Scale ( const Vector2f factor  )  [inherited]

Scale the object.

This function multiplies the current scale of the object, unlike SetScale which overwrites it. Thus, it is equivalent to the following code:

 sf::Vector2f scale = object.GetScale();
 object.SetScale(scale.x * factor.x, scale.y * factor.y);
Parameters:
factor Scale factors
See also:
SetScale
void sf::Drawable::Scale ( float  factorX,
float  factorY 
) [inherited]

Scale the object.

This function multiplies the current scale of the object, unlike SetScale which overwrites it. Thus, it is equivalent to the following code:

 sf::Vector2f scale = object.GetScale();
 object.SetScale(scale.x * factorX, scale.y * factorY);
Parameters:
factorX Horizontal scale factor
factorY Vertical scale factor
See also:
SetScale
void sf::Drawable::SetBlendMode ( Blend::Mode  mode  )  [inherited]

Set the blending mode of the object.

This property defines how the pixels of an object are blended with the pixels of the render target to which it is drawn. To know more about the blending modes available, see the sf::Blend::Mode enum. The default blend mode is Blend::Alpha.

Parameters:
mode New blending mode
See also:
GetBlendMode
void sf::Text::SetCharacterSize ( unsigned int  size  ) 

Set the character size.

The default size is 30.

Parameters:
size New character size, in pixels
See also:
GetCharacterSize
void sf::Drawable::SetColor ( const Color color  )  [inherited]

Set the global color of the object.

This global color affects the entire object, and modulates (multiplies) its original pixels. The default color is white.

Parameters:
color New color
See also:
GetColor
void sf::Text::SetFont ( const Font font  ) 

Set the text's font.

Texts have a valid font by default, which the built-in Font::GetDefaultFont().

Parameters:
font New font
See also:
GetFont
void sf::Drawable::SetOrigin ( const Vector2f origin  )  [inherited]

Set the local origin of the object.

The origin of an object defines the center point for all transformations (position, scale, rotation). The coordinates of this point must be relative to the top-left corner of the object, and ignore all transformations (position, scale, rotation). The default origin of a drawable object is (0, 0).

Parameters:
origin New origin
See also:
GetOrigin
void sf::Drawable::SetOrigin ( float  x,
float  y 
) [inherited]

Set the local origin of the object.

The origin of an object defines the center point for all transformations (position, scale, rotation). The coordinates of this point must be relative to the top-left corner of the object, and ignore all transformations (position, scale, rotation). The default origin of a drawable object is (0, 0).

Parameters:
x X coordinate of the new origin
y Y coordinate of the new origin
See also:
GetOrigin
void sf::Drawable::SetPosition ( const Vector2f position  )  [inherited]

Set the position of the object.

This function completely overwrites the previous position. See Move to apply an offset based on the previous position instead. The default position of a drawable object is (0, 0).

Parameters:
position New position
See also:
Move, SetX, SetY, GetPosition
void sf::Drawable::SetPosition ( float  x,
float  y 
) [inherited]

Set the position of the object.

This function completely overwrites the previous position. See Move to apply an offset based on the previous position instead. The default position of a drawable object is (0, 0).

Parameters:
x X coordinate of the new position
y Y coordinate of the new position
See also:
Move, SetX, SetY, GetPosition
void sf::Drawable::SetRotation ( float  angle  )  [inherited]

Set the orientation of the object.

This function completely overwrites the previous rotation. See Rotate to add an angle based on the previous rotation instead. The default rotation of a drawable object is 0.

Parameters:
angle New rotation, in degrees
See also:
Rotate, GetRotation
void sf::Drawable::SetScale ( const Vector2f factors  )  [inherited]

Set the scale factors of the object.

scale.x and scale.y must be strictly positive, otherwise they are ignored. This function completely overwrites the previous scale. See Scale to add a factor based on the previous scale instead. The default scale of a drawable object is (1, 1).

Parameters:
factors New scale factors
See also:
Scale, SetScaleX, SetScaleY, GetScale
void sf::Drawable::SetScale ( float  factorX,
float  factorY 
) [inherited]

Set the scale factors of the object.

factorX and factorY must be strictly positive, otherwise they are ignored. This function completely overwrites the previous scale. See Scale to add a factor based on the previous scale instead. The default scale of a drawable object is (1, 1).

Parameters:
factorX New horizontal scale factor
factorY New vertical scale factor
See also:
Scale, SetScaleX, SetScaleY, GetScale
void sf::Drawable::SetScaleX ( float  factor  )  [inherited]

Set the X scale factor of the object.

factor must be strictly positive, otherwise it is ignored.

Parameters:
factor New horizontal scale factor
See also:
SetScaleY, SetScale, GetScale
void sf::Drawable::SetScaleY ( float  factor  )  [inherited]

Set the Y scale factor of the object.

factor must be strictly positive, otherwise it is ignored.

Parameters:
factor New vertical scale factor
See also:
SetScaleX, SetScale, GetScale
void sf::Text::SetString ( const String string  ) 

Set the text's string.

The string argument is a sf::String, which can automatically be constructed from standard string types. So, the following calls are all valid:

 text.SetString("hello");
 text.SetString(L"hello");
 text.SetString(std::string("hello"));
 text.SetString(std::wstring(L"hello"));

A text's string is empty by default.

Parameters:
string New string
See also:
GetString
void sf::Text::SetStyle ( unsigned long  style  ) 

Set the text's style.

You can pass a combination of one or more styles, for example sf::Text::Bold | sf::Text::Italic. The default style is sf::Text::Regular.

Parameters:
style New style
See also:
GetStyle
void sf::Drawable::SetX ( float  x  )  [inherited]

Set the X position of the object.

Parameters:
x New X coordinate
See also:
SetY, SetPosition, GetPosition
void sf::Drawable::SetY ( float  y  )  [inherited]

Set the Y position of the object.

Parameters:
y New Y coordinate
See also:
SetX, SetPosition, GetPosition
Vector2f sf::Drawable::TransformToGlobal ( const Vector2f point  )  const [inherited]

Transform a local point in global coordinates.

This function takes a point in local coordinates, and transforms it in global coordinates. In other words, it applies the same transformations that are applied to the object (origin, translation, rotation and scale).

Parameters:
point Point to transform
Returns:
The transformed point
See also:
TransformToLocal
Vector2f sf::Drawable::TransformToLocal ( const Vector2f point  )  const [inherited]

Transform a point in object local coordinates.

This function takes a point in global coordinates, and transforms it in coordinates local to the object. In other words, it applies the inverse of all the transformations applied to the object (origin, translation, rotation and scale).

Parameters:
point Point to transform
Returns:
The transformed point
See also:
TransformToGlobal

The documentation for this class was generated from the following file: