Class: SFML::Text

Inherits:
Object
  • Object
show all
Includes:
Graphics::Transformable
Defined in:
lib/sfml/graphics/text.rb

Overview

Drawable rendered text. Like Sprite, requires a Font at construction time in SFML 3, and we hold a Ruby reference to it to keep the GPU resource alive while the Text object is in use.

font = SFML::Font.find("DejaVuSans")
score = SFML::Text.new(font, "0 : 0",
  character_size: 48,
  fill_color: SFML::Color.white,
  position: [400, 30],
  style: %i[bold])
window.draw(score)
score.string = "1 : 0"  # mutate freely after construction

Constant Summary collapse

CSFML_PREFIX =
:sfText
STYLES =
{
  regular:        0,
  bold:           1 << 0,
  italic:         1 << 1,
  underlined:     1 << 2,
  strike_through: 1 << 3,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Graphics::Transformable

#move, #origin, #origin=, #position, #position=, #rotate, #rotation, #rotation=, #scale, #scale=, #scale_by

Constructor Details

#initialize(font, string = "", **opts) ⇒ Text

Returns a new instance of Text.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sfml/graphics/text.rb', line 26

def initialize(font, string = "", **opts)
  raise ArgumentError, "Text requires a SFML::Font" unless font.is_a?(Font)

  ptr = C::Graphics.sfText_create(font.handle)
  raise Error, "sfText_create returned NULL" if ptr.null?
  @handle = FFI::AutoPointer.new(ptr, C::Graphics.method(:sfText_destroy))
  @font   = font # keep alive for GC

  self.string             = string
  self.character_size     = opts[:character_size]    if opts.key?(:character_size)
  self.fill_color         = opts[:fill_color]        if opts.key?(:fill_color)
  self.outline_color      = opts[:outline_color]     if opts.key?(:outline_color)
  self.outline_thickness  = opts[:outline_thickness] if opts.key?(:outline_thickness)
  self.style              = opts[:style]             if opts.key?(:style)
  self.position           = opts[:position]          if opts.key?(:position)
  self.origin             = opts[:origin]            if opts.key?(:origin)
  self.rotation           = opts[:rotation]          if opts.key?(:rotation)
  self.scale              = opts[:scale]             if opts.key?(:scale)
end

Instance Attribute Details

#fontObject

Returns the value of attribute font.



46
47
48
# File 'lib/sfml/graphics/text.rb', line 46

def font
  @font
end

#handleObject (readonly)

:nodoc:



142
143
144
# File 'lib/sfml/graphics/text.rb', line 142

def handle
  @handle
end

Instance Method Details

#character_sizeObject



80
# File 'lib/sfml/graphics/text.rb', line 80

def character_size = C::Graphics.sfText_getCharacterSize(@handle)

#character_size=(value) ⇒ Object



82
83
84
# File 'lib/sfml/graphics/text.rb', line 82

def character_size=(value)
  C::Graphics.sfText_setCharacterSize(@handle, Integer(value))
end

#draw_on(target, states_ptr = nil) ⇒ Object

:nodoc:



138
139
140
# File 'lib/sfml/graphics/text.rb', line 138

def draw_on(target, states_ptr = nil) # :nodoc:
  target._draw_native(:Text, @handle, states_ptr)
end

#fill_colorObject



86
# File 'lib/sfml/graphics/text.rb', line 86

def fill_color = Color.from_native(C::Graphics.sfText_getFillColor(@handle))

#fill_color=(c) ⇒ Object



88
89
90
# File 'lib/sfml/graphics/text.rb', line 88

def fill_color=(c)
  C::Graphics.sfText_setFillColor(@handle, c.to_native)
end

#global_boundsObject

Bounding box after applying the Text’s transform (position/scale/rotation).



134
135
136
# File 'lib/sfml/graphics/text.rb', line 134

def global_bounds
  Rect.from_native(C::Graphics.sfText_getGlobalBounds(@handle))
end

#local_boundsObject

Bounding box of the text in its own (untransformed) coordinate system. Use this to centre or align glyphs precisely:

text.origin = [text.local_bounds.width / 2, 0]


129
130
131
# File 'lib/sfml/graphics/text.rb', line 129

def local_bounds
  Rect.from_native(C::Graphics.sfText_getLocalBounds(@handle))
end

#outline_colorObject



92
# File 'lib/sfml/graphics/text.rb', line 92

def outline_color = Color.from_native(C::Graphics.sfText_getOutlineColor(@handle))

#outline_color=(c) ⇒ Object



94
95
96
# File 'lib/sfml/graphics/text.rb', line 94

def outline_color=(c)
  C::Graphics.sfText_setOutlineColor(@handle, c.to_native)
end

#outline_thicknessObject



98
# File 'lib/sfml/graphics/text.rb', line 98

def outline_thickness = C::Graphics.sfText_getOutlineThickness(@handle)

#outline_thickness=(t) ⇒ Object



100
101
102
# File 'lib/sfml/graphics/text.rb', line 100

def outline_thickness=(t)
  C::Graphics.sfText_setOutlineThickness(@handle, t.to_f)
end

#stringObject

Read the current string back as Ruby UTF-8. CSFML returns a pointer to a null-terminated sfChar32 (UTF-32) array; we walk it until the zero terminator and pack the codepoints back into a UTF-8 String.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sfml/graphics/text.rb', line 57

def string
  ptr = C::Graphics.sfText_getUnicodeString(@handle)
  return "" if ptr.null?

  codepoints = []
  offset = 0
  loop do
    cp = ptr.get_uint32(offset)
    break if cp.zero?
    codepoints << cp
    offset += 4
  end
  codepoints.pack("U*")
end

#string=(value) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/sfml/graphics/text.rb', line 72

def string=(value)
  str = value.to_s.encode("UTF-8")
  cps = str.unpack("U*")  # array of integer codepoints
  buf = FFI::MemoryPointer.new(:uint32, cps.length + 1)
  buf.write_array_of_uint32(cps + [0])
  C::Graphics.sfText_setUnicodeString(@handle, buf)
end

#styleObject

Returns an Array of style symbols, e.g. [:bold, :italic].



105
106
107
108
109
110
111
# File 'lib/sfml/graphics/text.rb', line 105

def style
  bits = C::Graphics.sfText_getStyle(@handle)
  return [:regular] if bits.zero?
  STYLES.each_with_object([]) do |(name, value), acc|
    acc << name if value != 0 && (bits & value) != 0
  end
end

#style=(value) ⇒ Object

Accepts a single Symbol, an Array of Symbols, or a raw integer bitmask.



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sfml/graphics/text.rb', line 114

def style=(value)
  bits = case value
         when Integer then value
         when Symbol  then STYLES.fetch(value)
         when Array
           value.reduce(0) { |acc, sym| acc | STYLES.fetch(sym) }
         else
           raise ArgumentError, "Text#style= expects Symbol, Array, or Integer; got #{value.class}"
         end
  C::Graphics.sfText_setStyle(@handle, bits)
end