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 ->
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).
(* Declare and load a font *)
let font = new font (`File "arial.ttf") in
(* Create a text *)
let text = new text ~string:"hello"
(* Draw it *)
method destroy :
method find_character_pos :
int -> float * float
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 :
method get_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 :
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 :
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
Returns Local bounding rectangle of the entity
method get_string :
method get_style :
method set_character_size :
int -> unit
The default size is 30.
method set_color :
Color.t -> unit
By default, the text's color is opaque white.
method set_font :
'a. (#const_font as 'a) -> unit
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
method set_style :
TextStyle.t list -> unit
You can pass a combination of one or more styles, for example
TextStyle.Bold ; TextStyle.Italic . The default style is .