Object
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.
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.) |
# 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
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.
# 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
Does the heavy lifting for the render methods.
# 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
Return the biggest ascent (baseline to top; in pixels) of all glyphs in the font.
# File lib/rubygame/ttf.rb, line 173 def ascent SDL::TTF.FontAnscent( @struct ) end
Enable or disable bold mode for this font. Returns the old value.
# File lib/rubygame/ttf.rb, line 123 def bold=( enabled ) _set_style( enabled, SDL::TTF::STYLE_BOLD ) end
True if bold mode is enabled for this font.
# File lib/rubygame/ttf.rb, line 115 def bold? _get_style( SDL::TTF::STYLE_BOLD ) end
Return the biggest descent (baseline to bottom; in pixels) of all glyphs in the font.
# File lib/rubygame/ttf.rb, line 181 def descent SDL::TTF.FontDescent( @struct ) end
Return the biggest height (bottom to top; in pixels) of all glyphs in the font.
# File lib/rubygame/ttf.rb, line 165 def height SDL::TTF.FontHeight( @struct ) end
Enable or disable italic mode for this font. Returns the old value.
# File lib/rubygame/ttf.rb, line 140 def italic=( enabled ) _set_style( enabled, SDL::TTF::STYLE_ITALIC ) end
True if italic mode is enabled for this font.
# File lib/rubygame/ttf.rb, line 131 def italic? _get_style( SDL::TTF::STYLE_ITALIC ) end
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.
# File lib/rubygame/ttf.rb, line 189 def line_skip SDL::TTF.FontLineSkip( @struct ) end
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. |
# 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
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. |
# 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
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. |
# 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
The width and height the text would be if it were rendered, without the overhead of actually rendering it.
# File lib/rubygame/ttf.rb, line 198 def size_text( text ) SDL::TTF.SizeText(@struct, text) end
The width and height the Unicode text would be if it were rendered, without the overhead of actually rendering it.
# File lib/rubygame/ttf.rb, line 214 def size_unicode( text ) SDL::TTF.SizeUNICODE(@struct, text) end
The width and height the UTF-8 encoded text would be if it were rendered, without the overhead of actually rendering it.
# File lib/rubygame/ttf.rb, line 206 def size_utf8( text ) SDL::TTF.SizeUTF8(@struct, text) end
Enable or disable underline mode for this font. Returns the old value.
# File lib/rubygame/ttf.rb, line 157 def underline=( enabled ) _set_style( enabled, SDL::TTF::STYLE_UNDERLINE ) end
True if underline mode is enabled for this font.
# File lib/rubygame/ttf.rb, line 148 def underline? _get_style( SDL::TTF::STYLE_UNDERLINE ) end
Generated with the Darkfish Rdoc Generator 2.