sfml3.rb

Class: SFML::Text

Inherits:
Object
  • Object
show all
Defined in:
ext/graphics/text.c,
ext/graphics/text.c

Overview

A drawable string of characters, rendered using a Font, positioned, rotated, scaled and tinted like any other Transformable object.

Includes Drawable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#new(font) ⇒ Text #new(font, string) ⇒ Text #new(font, string, character_size) ⇒ Text

CSFML requires a font at creation time -- its C API unconditionally dereferences a NULL font argument rather than tolerating one, so unlike most other constructors here, the font is not optional.

Overloads:

  • #new(font) ⇒ Text
  • #new(font, string) ⇒ Text
  • #new(font, string, character_size) ⇒ Text

Raises:

  • (ArgumentError) —

    if font is not a Font



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'ext/graphics/text.c', line 97

static VALUE Text_initialize(int argc, VALUE* argv, VALUE self) {
    VALUE rb_font, rb_string, rb_size;
    Text* ptr;

    rb_scan_args(argc, argv, "12", &rb_font, &rb_string, &rb_size);

    if (!rb_obj_is_kind_of(rb_font, Get_Klass_Font())) {
        raise_invalid_argument_class(Get_Klass_Font());
    }

    TypedData_Get_Struct(self, Text, &Text_data_type, ptr);

    ptr->text = sfText_create(Get_Font_Struct(rb_font));
    ptr->rb_font = rb_font;

    if (ptr->text == NULL) {
        rb_raise(rb_eRuntimeError, "failed to create text");
    }

    if (!NIL_P(rb_string)) {
        sfText_setString(ptr->text, StringValueCStr(rb_string));
    }

    if (!NIL_P(rb_size)) {
        sfText_setCharacterSize(ptr->text, (unsigned int)NUM2UINT(rb_size));
    }

    return self;
}

Instance Attribute Details

#character_size ⇒ Integer

Returns the character size in pixels.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    in pixels



565
566
567
# File 'ext/graphics/text.c', line 565

static VALUE Text_get_character_size(VALUE self) {
    return UINT2NUM(sfText_getCharacterSize(Get_Text_Struct(self)));
}

#fill_color ⇒ Color

Returns the shape's fill color.

Returns:

Returns:



565
566
567
# File 'ext/graphics/text.c', line 565

static VALUE Text_get_fill_color(VALUE self) {
    return color_to_rb(sfText_getFillColor(Get_Text_Struct(self)));
}

#font ⇒ Font?

Returns the text's font.

Returns:

Returns:



565
566
567
# File 'ext/graphics/text.c', line 565

static VALUE Text_get_font(VALUE self) {
    return Get_Text(self)->rb_font;
}

#letter_spacing ⇒ Float

Returns the spacing between letters.

Returns:

  • (Float)

Returns:

  • (Float) —

    a multiplier of the font's default spacing (1.0 = default)



565
566
567
# File 'ext/graphics/text.c', line 565

static VALUE Text_get_letter_spacing(VALUE self) {
    return DBL2NUM(sfText_getLetterSpacing(Get_Text_Struct(self)));
}

#line_spacing ⇒ Float

Returns the line spacing multiplier.

Returns:

  • (Float)

Returns:

  • (Float) —

    a multiplier of the font's default line spacing



565
566
567
# File 'ext/graphics/text.c', line 565

static VALUE Text_get_line_spacing(VALUE self) {
    return DBL2NUM(sfText_getLineSpacing(Get_Text_Struct(self)));
}

#origin ⇒ Vector2

Returns the object's origin.

Returns:

Returns:



565
566
567
# File 'ext/graphics/text.c', line 565

static VALUE Text_get_origin(VALUE self) {
    return vec2f_to_rb(sfText_getOrigin(Get_Text_Struct(self)));
}

#outline_color ⇒ Color

Returns the shape's outline color.

Returns:

Returns:



565
566
567
# File 'ext/graphics/text.c', line 565

static VALUE Text_get_outline_color(VALUE self) {
    return color_to_rb(sfText_getOutlineColor(Get_Text_Struct(self)));
}

#outline_thickness ⇒ Float

Returns the shape's outline thickness.

Returns:

  • (Float)

Returns:

  • (Float)


565
566
567
# File 'ext/graphics/text.c', line 565

static VALUE Text_get_outline_thickness(VALUE self) {
    return DBL2NUM(sfText_getOutlineThickness(Get_Text_Struct(self)));
}

#position ⇒ Vector2

Returns the object's position.

Returns:

Returns:



565
566
567
# File 'ext/graphics/text.c', line 565

static VALUE Text_get_position(VALUE self) {
    return vec2f_to_rb(sfText_getPosition(Get_Text_Struct(self)));
}

#rotation ⇒ Float

Returns the object's rotation, in degrees.

Returns:

  • (Float)

Returns:

  • (Float) —

    degrees



565
566
567
# File 'ext/graphics/text.c', line 565

static VALUE Text_get_rotation(VALUE self) {
    return DBL2NUM(sfText_getRotation(Get_Text_Struct(self)));
}

#scale ⇒ Vector2

Returns the object's scale factors.

Returns:

Returns:



565
566
567
# File 'ext/graphics/text.c', line 565

static VALUE Text_get_scale(VALUE self) {
    return vec2f_to_rb(sfText_getScale(Get_Text_Struct(self)));
}

#string ⇒ String

Returns the displayed string.

Returns:

  • (String)

Returns:

  • (String)


565
566
567
# File 'ext/graphics/text.c', line 565

static VALUE Text_get_string(VALUE self) {
    return utf32_to_rb(sfText_getUnicodeString(Get_Text_Struct(self)));
}

#style ⇒ Integer

Returns the text style flags.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    a bitmask of the active style flags



565
566
567
# File 'ext/graphics/text.c', line 565

static VALUE Text_get_style(VALUE self) {
    return text_style_to_rb(sfText_getStyle(Get_Text_Struct(self)));
}

Instance Method Details

#copy ⇒ Text

Returns a deep copy of the object.

Returns:

Returns:

  • (Text) —

    an independent copy



133
134
135
136
137
138
139
140
141
# File 'ext/graphics/text.c', line 133

static VALUE Text_copy(VALUE self) {
    Text* ptr = Get_Text(self);
    VALUE copy = Text_wrap(Get_Klass_Text(), sfText_copy(ptr->text));
    Text* copy_ptr = Get_Text(copy);

    copy_ptr->rb_font = ptr->rb_font;

    return copy;
}

#draw(target, state) ⇒ nil

Part of the Drawable interface; call Target#draw instead of this directly.

Returns:

  • (nil)

Returns:

  • (nil)

Raises:

  • (TypeError) —

    if target is not a Target or state is not a RenderState



552
553
554
555
556
557
558
559
560
561
562
563
# File 'ext/graphics/text.c', line 552

static VALUE Text_draw(VALUE self, VALUE rb_target, VALUE rb_state) {
    TargetView view = Get_RenderTarget_View(rb_target);

    if (!rb_obj_is_kind_of(rb_state, Get_Klass_RenderState())) {
        raise_invalid_argument_class(Get_Klass_RenderState());
    }

    TARGET_DRAW(view, sfRenderWindow_drawText, sfRenderTexture_drawText, Get_Text_Struct(self),
                Get_RenderState_Struct(rb_state));

    return Qnil;
}

#find_character_pos(index) ⇒ Vector2

Returns the index of the character at the given position.

Returns:

Returns:

  • (Vector2) —

    the position of the +index+-th character, in global (parent) coordinates



516
517
518
# File 'ext/graphics/text.c', line 516

static VALUE Text_find_character_pos(VALUE self, VALUE rb_index) {
    return vec2f_to_rb(sfText_findCharacterPos(Get_Text_Struct(self), (size_t)NUM2SIZET(rb_index)));
}

#global_bounds ⇒ Rect

Returns the bounding box after the transform is applied.

Returns:

Returns:

  • (Rect) —

    the bounding box in the parent's coordinate system, after the current transform is applied



538
539
540
# File 'ext/graphics/text.c', line 538

static VALUE Text_get_global_bounds(VALUE self) {
    return rect_to_rb(sfText_getGlobalBounds(Get_Text_Struct(self)));
}

#inverse_transform ⇒ Array<Float>

Returns the 3x3 row-major inverse of the object's transform matrix.

Returns:

  • (Array<Float>)

Returns:

  • (Array<Float>) —

    the inverse of #transform



504
505
506
# File 'ext/graphics/text.c', line 504

static VALUE Text_get_inverse_transform(VALUE self) {
    return Transform_MatrixToArray(sfText_getInverseTransform(Get_Text_Struct(self)).matrix);
}

#local_bounds ⇒ Rect

Returns the bounding box in local (untransformed) coordinates.

Returns:

Returns:

  • (Rect) —

    the bounding box in local coordinates, before any transform is applied



527
528
529
# File 'ext/graphics/text.c', line 527

static VALUE Text_get_local_bounds(VALUE self) {
    return rect_to_rb(sfText_getLocalBounds(Get_Text_Struct(self)));
}

#transform ⇒ Array<Float>

Returns the object's 3x3 row-major transform matrix.

Returns:

  • (Array<Float>)

Returns:

  • (Array<Float>) —

    the 9-element matrix (also available as #matrix)



494
495
496
# File 'ext/graphics/text.c', line 494

static VALUE Text_get_transform(VALUE self) {
    return Transform_MatrixToArray(sfText_getTransform(Get_Text_Struct(self)).matrix);
}

#move(offset) ⇒ self

Moves the object by offset.

Returns:

  • (self)

Returns:

  • (self)


459
460
461
462
# File 'ext/graphics/text.c', line 459

static VALUE Text_move(VALUE self, VALUE rb_offset) {
    sfText_move(Get_Text_Struct(self), vec2f_from_rb(rb_offset));
    return self;
}

#rotate(angle) ⇒ self

Rotates the object by angle degrees.

Returns:

  • (self)

Returns:

  • (self)


471
472
473
474
# File 'ext/graphics/text.c', line 471

static VALUE Text_rotate(VALUE self, VALUE rb_angle) {
    sfText_rotate(Get_Text_Struct(self), NUM2DBL(rb_angle));
    return self;
}

#scale!(factors) ⇒ self

Scales the object by factors relative to its current scale.

Returns:

  • (self)

Returns:

  • (self)


483
484
485
486
# File 'ext/graphics/text.c', line 483

static VALUE Text_scale(VALUE self, VALUE rb_factors) {
    sfText_scale(Get_Text_Struct(self), vec2f_from_rb(rb_factors));
    return self;
}

#transform ⇒ Array<Float>

Returns the object's 3x3 row-major transform matrix.

Returns:

  • (Array<Float>)

Returns:

  • (Array<Float>) —

    the 9-element matrix (also available as #matrix)



494
495
496
# File 'ext/graphics/text.c', line 494

static VALUE Text_get_transform(VALUE self) {
    return Transform_MatrixToArray(sfText_getTransform(Get_Text_Struct(self)).matrix);
}