Class OcsfmlGraphics.text

class text : ?string:string -> ?position:float * float -> ?scale:float * float -> ?rotation:float -> ?origin:float * float -> ?color:Color.t -> ?font:font -> ?character_size:int -> ?style:TextStyle.t list -> unit -> object .. end
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::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.

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 *)
    let font = new font (`File "arial.ttf"in
    
    (* Create a text *)
    let text = new text ~string:"hello" 
                        ~font
                        ~character_size:30.
                        ~style:[Bold]
                        ~color:Color.red () 
    in
    
    (* Draw it *)
    window#draw text
    

Inherits
method destroy : unit
method find_character_pos : int -> float * float
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.
Returns Position of the character

method get_character_size : int
Get the character size.
Returns Size of the characters, in pixels.

Get the global color of the text.

method get_font : const_font reference
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.

method get_global_bounds : FloatRect.t
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.
Returns Global bounding rectangle of the entity

method get_local_bounds : FloatRect.t
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

method get_string : string
Get the text's string.
Returns Text's string
method get_style : TextStyle.t list
Get the text's style.
Returns Text's style.
method set_character_size : int -> unit
Set the character size.

The default size is 30.

method set_color : Color.t -> unit
Set the global color of the text.

By default, the text's color is opaque white.

method set_font : 'a. (#const_font as 'a) -> unit
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. Texts have a valid font by default, which is the built-in get_default_font().

method set_string : string -> unit
Set the text's string.
method set_style : TextStyle.t list -> unit
Set the text's style.

You can pass a combination of one or more styles, for example TextStyle.Bold ; TextStyle.Italic . The default style is [].