### Home Classes Methods

### In Files

- lib/rubygame/ttf.rb

### Parent

Object

### Methods

- ::new
- ::quit
- ::setup
- #_render
- #ascent
- #bold
- #bold=
- #bold?
- #descent
- #height
- #italic
- #italic=
- #italic?
- #line_skip
- #render
- #render_unicode
- #render_utf8
- #size_text
- #size_unicode
- #size_utf8
- #underline
- #underline=
- #underline?

### Files

- CREDITS
- LICENSE
- NEWS
- README
- ROADMAP
- custom_sdl_load_paths.rdoc
- getting_started.rdoc
- keyboard_symbols.rdoc
- macosx_install.rdoc
- managing_framerate.rdoc
- surface_palettes.rdoc
- windows_install.rdoc

### Class/Module Index

Quicksearch

- Rubygame
- Rubygame::ActiveEvent
- Rubygame::Clock
- Rubygame::Color
- Rubygame::Color::ColorBase
- Rubygame::Color::ColorHSL
- Rubygame::Color::ColorHSV
- Rubygame::Color::ColorRGB
- Rubygame::Color::Palette
- Rubygame::Event
- Rubygame::EventActions
- Rubygame::EventActions::BlockAction
- Rubygame::EventActions::MethodAction
- Rubygame::EventActions::MultiAction
- Rubygame::EventHandler
- Rubygame::EventHandler::HasEventHandler
- Rubygame::EventHook
- Rubygame::EventQueue
- Rubygame::EventTriggers
- Rubygame::EventTriggers::AndTrigger
- Rubygame::EventTriggers::AttrTrigger
- Rubygame::EventTriggers::BlockTrigger
- Rubygame::EventTriggers::InstanceOfTrigger
- Rubygame::EventTriggers::KeyPressTrigger
- Rubygame::EventTriggers::KeyReleaseTrigger
- Rubygame::EventTriggers::KindOfTrigger
- Rubygame::EventTriggers::MouseMoveTrigger
- Rubygame::EventTriggers::MousePressTrigger
- Rubygame::EventTriggers::MouseReleaseTrigger
- Rubygame::EventTriggers::OrTrigger
- Rubygame::EventTriggers::TickTrigger
- Rubygame::EventTriggers::YesTrigger
- Rubygame::Events
- Rubygame::Events::ClockTicked
- Rubygame::Events::InputFocusGained
- Rubygame::Events::InputFocusLost
- Rubygame::Events::JoystickAxisMoved
- Rubygame::Events::JoystickBallMoved
- Rubygame::Events::JoystickButtonEvent
- Rubygame::Events::JoystickButtonPressed
- Rubygame::Events::JoystickButtonReleased
- Rubygame::Events::JoystickHatMoved
- Rubygame::Events::KeyPressed
- Rubygame::Events::KeyReleased
- Rubygame::Events::KeyboardEvent
- Rubygame::Events::MouseButtonEvent
- Rubygame::Events::MouseFocusGained
- Rubygame::Events::MouseFocusLost
- Rubygame::Events::MouseMoved
- Rubygame::Events::MousePressed
- Rubygame::Events::MouseReleased
- Rubygame::Events::QuitRequested
- Rubygame::Events::WindowExposed
- Rubygame::Events::WindowMinimized
- Rubygame::Events::WindowResized
- Rubygame::Events::WindowUnminimized
- Rubygame::ExposeEvent
- Rubygame::Ftor
- Rubygame::GL
- Rubygame::Hotspot
- Rubygame::JoyAxisEvent
- Rubygame::JoyBallEvent
- Rubygame::JoyDownEvent
- Rubygame::JoyHatEvent
- Rubygame::JoyUpEvent
- Rubygame::Joystick
- Rubygame::Key
- Rubygame::KeyDownEvent
- Rubygame::KeyUpEvent
- Rubygame::MailQueue
- Rubygame::MediaBag
- Rubygame::Mixer
- Rubygame::Mixer::Music
- Rubygame::Mixer::Sample
- Rubygame::Mouse
- Rubygame::MouseDownEvent
- Rubygame::MouseMotionEvent
- Rubygame::MouseUpEvent
- Rubygame::Music
- Rubygame::NamedResource
- Rubygame::NamedResource::NamedResourceClassMethods
- Rubygame::QuitEvent
- Rubygame::Rect
- Rubygame::ResizeEvent
- Rubygame::SDLError
- Rubygame::SFont
- Rubygame::Screen
- Rubygame::Sound
- Rubygame::Sprites
- Rubygame::Sprites::DepthSortGroup
- Rubygame::Sprites::Group
- Rubygame::Sprites::LimitGroup
- Rubygame::Sprites::Sprite
- Rubygame::Sprites::UpdateGroup
- Rubygame::Surface
- Rubygame::TTF

No matching classes.

# Rubygame::TTF

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.

### Public Class Methods

new( file, size ) click to toggle source

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

quit() click to toggle source

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.

    # File lib/rubygame/ttf.rb, line 56
    def self.quit
      if( SDL::TTF.WasInit() != 0 )
        SDL::TTF.Quit()
      end
    end

setup() click to toggle source

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

### Public Instance Methods

_render( text, smooth, color, back, shaded, blended, solid ) click to toggle source

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

ascent() click to toggle source

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

bold() click to toggle source
Alias for: bold?
bold=( enabled ) click to toggle source

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

bold?() click to toggle source

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

Also aliased as: bold
descent() click to toggle source

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

height() click to toggle source

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

italic() click to toggle source
Alias for: italic?
italic=( enabled ) click to toggle source

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

italic?() click to toggle source

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

Also aliased as: italic
line_skip() click to toggle source

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

render( text, smooth, color, back=nil ) click to toggle source

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

render_unicode( text, smooth, color, back=nil ) click to toggle source

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

render_utf8( text, smooth, color, back=nil ) click to toggle source

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

size_text( text ) click to toggle source

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

size_unicode( text ) click to toggle source

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

size_utf8( text ) click to toggle source

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

underline() click to toggle source
Alias for: underline?
underline=( enabled ) click to toggle source

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

underline?() click to toggle source

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

Also aliased as: underline

[Validate]

Generated with the Darkfish Rdoc Generator 2.