Class: Rubygame::TTF

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygame/ttf.rb

Overview

IMPORTANT: this class only exists if SDL_ttf is available! Your code should check “defined?(Rubygame::TTF) != nil” to see if you can use this class, or be prepared to rescue from NameError.

TTF provides an interface to SDL_ttf, allowing TrueType Font files to be loaded and used to render text to Surfaces.

The TTF class must be initialized with the #setup method before any TTF objects can be created or used.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, size) ⇒ TTF

Create a new TTF object, which can render text to a Surface with a particular font style and size.

file

filename of the TrueType font to use. Should be a TTF or FON file.

size

point size (based on 72DPI). (That means the height in pixels from the bottom of the descent to the top of the ascent.)



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubygame/ttf.rb', line 72

def initialize( file, size )
  if( SDL::TTF.WasInit() == 0 )
    raise( Rubygame::SDLError,
           "You must call TTF.setup before opening a font." )
  end

  @struct = SDL::TTF.OpenFont( file, size )

  if( @struct.pointer.null? )
    raise Rubygame::SDLError, "Could not open font: #{SDL.GetError()}"
  end
end

Class Method Details

.quitObject

Clean up and quit SDL_ttf, making the TTF class unusable as a result (until it is setup again). This does not need to be called before Rubygame exits, as it will be done automatically.



56
57
58
59
60
# File 'lib/rubygame/ttf.rb', line 56

def self.quit
  if( SDL::TTF.WasInit() != 0 )
    SDL::TTF.Quit()
  end
end

.setupObject

Attempt to setup the TTF class for use by initializing SDL_ttf. This must be called before the TTF class can be used. Raises SDLError if there is a problem initializing SDL_ttf.



44
45
46
47
48
49
# File 'lib/rubygame/ttf.rb', line 44

def self.setup
  if( SDL::TTF.WasInit() == 0 and SDL::TTF.Init() != 0 )
    raise( Rubygame::SDLError,
           "Could not setup TTF class: #{SDL.GetError()}" )
  end
end

Instance Method Details

#_get_style(style) ⇒ Object

:nodoc:



90
91
92
# File 'lib/rubygame/ttf.rb', line 90

def _get_style( style )       # :nodoc:
  return (SDL::TTF.GetFontStyle(@struct) & style == style)
end

#_render(text, smooth, color, back, shaded, blended, solid) ⇒ Object

Does the heavy lifting for the render methods.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rubygame/ttf.rb', line 222

def _render( text, smooth, color, back, shaded, blended, solid ) # :nodoc;

  color = SDL::Color.new( Rubygame::Color.make_sdl_rgba(color) )

  if back
    back = SDL::Color.new( Rubygame::Color.make_sdl_rgba(back) )
  end

  surf =
    if smooth
      if back
        shaded.call( @struct, text, color, back )
      else
        blended.call( @struct, text, color )
      end
    else
      if back
        s = solid.call( @struct, text, color )
        SDL::SetColors( s, back.pointer, 0, 1 )
        SDL::SetColorKey( s, 0, 0 );
        s
      else
        solid.call( @struct, text, color )
      end
    end

  if surf.pointer.null?
    raise Rubygame::SDLError, "Could not render text: #{SDL.GetError()}"
  end

  return Rubygame::Surface.new( surf )

end

#_set_style(enable, style) ⇒ Object

Sets the style and returns the old value.



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rubygame/ttf.rb', line 97

def _set_style( enable, style ) # :nodoc:
  old = SDL::TTF.GetFontStyle(@struct)
  if( !enable and (old & style == style ) )
  SDL::TTF.SetFontStyle( @struct, old ^ style )
    return true
  elsif( enable )
    SDL::TTF.SetFontStyle( @struct, old | style )
    return false
  else
    # No change
    return enable;
  end
end

#ascentObject

Return the biggest ascent (baseline to top; in pixels) of all glyphs in the font.



173
174
175
# File 'lib/rubygame/ttf.rb', line 173

def ascent
  SDL::TTF.FontAnscent( @struct )
end

#bold=(enabled) ⇒ Object

Enable or disable bold mode for this font. Returns the old value.



123
124
125
# File 'lib/rubygame/ttf.rb', line 123

def bold=( enabled )
  _set_style( enabled, SDL::TTF::STYLE_BOLD )
end

#bold?Boolean Also known as: bold

True if bold mode is enabled for this font.

Returns:

  • (Boolean)


115
116
117
# File 'lib/rubygame/ttf.rb', line 115

def bold?
  _get_style( SDL::TTF::STYLE_BOLD )
end

#descentObject

Return the biggest descent (baseline to bottom; in pixels) of all glyphs in the font.



181
182
183
# File 'lib/rubygame/ttf.rb', line 181

def descent
  SDL::TTF.FontDescent( @struct )
end

#heightObject

Return the biggest height (bottom to top; in pixels) of all glyphs in the font.



165
166
167
# File 'lib/rubygame/ttf.rb', line 165

def height
  SDL::TTF.FontHeight( @struct )
end

#italic=(enabled) ⇒ Object

Enable or disable italic mode for this font. Returns the old value.



140
141
142
# File 'lib/rubygame/ttf.rb', line 140

def italic=( enabled )
  _set_style( enabled, SDL::TTF::STYLE_ITALIC )
end

#italic?Boolean Also known as: italic

True if italic mode is enabled for this font.

Returns:

  • (Boolean)


131
132
133
# File 'lib/rubygame/ttf.rb', line 131

def italic?
  _get_style( SDL::TTF::STYLE_ITALIC )
end

#line_skipObject

Return the recommended distance (in pixels) from a point on a line of text to the same point on the line of text below it.



189
190
191
# File 'lib/rubygame/ttf.rb', line 189

def line_skip
  SDL::TTF.FontLineSkip( @struct )
end

#render(text, smooth, color, back = nil) ⇒ Object

Renders a string to a Surface with the font’s style and the given color(s).

text

the text string to render

smooth

Use anti-aliasing if true. Enabling this makes the text look much nicer (smooth curves), but is much slower.

color

the color to render the text, in the form [r,g,b]

back

the color to use as a background for the text. This option can be omitted to have a transparent background.



267
268
269
270
271
272
# File 'lib/rubygame/ttf.rb', line 267

def render( text, smooth, color, back=nil )
  _render( text, smooth, color, back,
           SDL::TTF.method(:RenderText_Shaded),
           SDL::TTF.method(:RenderText_Blended),
           SDL::TTF.method(:RenderText_Solid) )
end

#render_unicode(text, smooth, color, back = nil) ⇒ Object

Renders a Unicode string to a Surface with the font’s style and the given color(s).

text

the text string to render

smooth

Use anti-aliasing if true. Enabling this makes the text look much nicer (smooth curves), but is much slower.

color

the color to render the text, in the form [r,g,b]

back

the color to use as a background for the text. This option can be omitted to have a transparent background.



303
304
305
306
307
308
# File 'lib/rubygame/ttf.rb', line 303

def render_unicode( text, smooth, color, back=nil )
  _render( text, smooth, color, back,
           SDL::TTF.method(:RenderUNICODE_Shaded),
           SDL::TTF.method(:RenderUNICODE_Blended),
           SDL::TTF.method(:RenderUNICODE_Solid) )
end

#render_utf8(text, smooth, color, back = nil) ⇒ Object

Renders a UTF-8 string to a Surface with the font’s style and the given color(s).

text

the text string to render

smooth

Use anti-aliasing if true. Enabling this makes the text look much nicer (smooth curves), but is much slower.

color

the color to render the text, in the form [r,g,b]

back

the color to use as a background for the text. This option can be omitted to have a transparent background.



285
286
287
288
289
290
# File 'lib/rubygame/ttf.rb', line 285

def render_utf8( text, smooth, color, back=nil )
  _render( text, smooth, color, back,
           SDL::TTF.method(:RenderUTF8_Shaded),
           SDL::TTF.method(:RenderUTF8_Blended),
           SDL::TTF.method(:RenderUTF8_Solid) )
end

#size_text(text) ⇒ Object

The width and height the text would be if it were rendered, without the overhead of actually rendering it.



198
199
200
# File 'lib/rubygame/ttf.rb', line 198

def size_text( text )
  SDL::TTF.SizeText(@struct, text)
end

#size_unicode(text) ⇒ Object

The width and height the Unicode text would be if it were rendered, without the overhead of actually rendering it.



214
215
216
# File 'lib/rubygame/ttf.rb', line 214

def size_unicode( text )
  SDL::TTF.SizeUNICODE(@struct, text)
end

#size_utf8(text) ⇒ Object

The width and height the UTF-8 encoded text would be if it were rendered, without the overhead of actually rendering it.



206
207
208
# File 'lib/rubygame/ttf.rb', line 206

def size_utf8( text )
  SDL::TTF.SizeUTF8(@struct, text)
end

#underline=(enabled) ⇒ Object

Enable or disable underline mode for this font. Returns the old value.



157
158
159
# File 'lib/rubygame/ttf.rb', line 157

def underline=( enabled )
  _set_style( enabled, SDL::TTF::STYLE_UNDERLINE )
end

#underline?Boolean Also known as: underline

True if underline mode is enabled for this font.

Returns:

  • (Boolean)


148
149
150
# File 'lib/rubygame/ttf.rb', line 148

def underline?
  _get_style( SDL::TTF::STYLE_UNDERLINE )
end