### Home Classes Methods

### In Files

- lib/rubygame/sfont.rb

### Parent

Object

### Methods

- ::default_glyphs
- ::new
- #blit_glyph
- #load_glyph
- #render
- #string_width

### 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::SFont

NOTE: you must require 'rubygame/sfont' manually to gain access to Rubygame::SFont. It is not imported with Rubygame by default!

SFont is a type of bitmapped font, which is loaded from an image file with a meaningful top row of pixels, and the font itself below that. The top row provides information about what parts of of the lower area contains font data, and which parts are empty.

The image file should contain all of the glyphs on one row, with the colorkey color at the bottom-left pixel and the "skip" color at the top-left pixel.

The colorkey color is applied to the image file when it is loaded, so that all pixels of that color appear transparent. Alternatively, if the alpha value of pixel [0,0] is 0 (that is, if it is fully transparent), the image file is assumed to have a proper alpha channel, and no colorkey is applied. The skip color is used in the top row of pixels to indicate that all the pixels below it contain empty space, and that the next pixel that is not the skip color marks the beginning of the next glyph.

The official SFont homepage, with information on SFont and sample font files, can be found at www.linux-games.com/sfont/. More information on SFont, and a useful utility for automatically generating the top row for a font, can be found at: www.nostatic.org/sfont/

### Attributes

height[R]

Return the height of the font, in pixels. This is the same as the height of the image file (all glyphs are the same height).

### Public Class Methods

default_glyphs() click to toggle source

Returns an array of strings, each string a single ASCII character from ! (33) to ~ (126). This is the default set of characters in a SFont. The full set is as follows:

    ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ `
    a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~

    # File lib/rubygame/sfont.rb, line 63
    def SFont.default_glyphs
      @@default_glyphs
    end

new(filename,glyphs=nil,spacew=nil) click to toggle source

Create a now SFont instance.

This function takes these arguments:

filename | the name of the image file from which the font should be loaded. Or, a Surface with a font image on it. The Surface will be copied onto separate Surfaces for each glyph, so the original Surface can be recycled.
glyphs | array of strings, one for each glyph, in the order they are found in the image file. If glyphs is not provided, or is nil, it is assumed to be the normal SFont order; that is, ASCII characters ! (33) to ~ (126). See SFont.default_glyphs for a full list.

spacew

represents the width of a space character ( ). You can either specify the width in pixels, or specify a character whose width, as found in the image, should be used. Alternatively, you could add a space character to the list of glyphs, and to the image file. If `spacew` is not given or is nil, and the space character is not in the list of glyphs, it will have the same width as the double-quote character (").

    # File lib/rubygame/sfont.rb, line 87
        def initialize(filename,glyphs=nil,spacew=nil)
          # load the surface containing all the glyphs
          surface = nil
          if filename.is_a? String
            surface = Surface.load_image(filename)
          elsif filename.is_a? Surface
            surface = filename
          end
          @height = surface.height
          colorkey = surface.get_at(0,@height-1)

          # set colorkey if "transparent" color is not actually transparent
          if colorkey[3] != 0
            surface.set_colorkey(colorkey[0..2])
          end

          @glyphs = {}
          @skip = surface.get_at(0,0)[0..2]

          # split the glyphs into separate surfaces
          glyphs = (glyphs or @@default_glyphs)
          start_x = 2
          glyphs.each{ |glyph| start_x = load_glyph(surface,glyph,start_x) }

          if not glyphs.include?(" ")
            if spacew == nil
              spacew = @glyphs['"'].width
            elsif spacew.kind_of? Numeric
              spacew = spacew.to_i
            elsif spacew.kind_of? String
              if glyphs.include? spacew
                spacew = @glyphs[spacew].width
              else
                spacew = @glyphs['"'].width
              end
            else
              raise(ArgumentError,"spacew must be Numeric, String, \
    or nil (got %s)"%[spacew.class])
            end
            @glyphs[" "] = Surface.new([spacew,@height])
          end
        end

### Public Instance Methods

render(string) click to toggle source

Renders the given string to a Surface, and returns that surface.

This method takes this argument:

string | the text string to render.

    # File lib/rubygame/sfont.rb, line 211
    def render(string)
      size = [self.string_width(string),self.height]
      render = Surface.new(size)
      x = 0
      string.each_byte { |glyph|
        blit_glyph("%c"%[glyph],render,[x,0])
        x += @glyphs["%c"%[glyph]].width
      }
      return render
    end

string_width(string) click to toggle source

Pretends to render the given string, and returns the width in pixels of the surface that would be created if it were rendered using SFont#render. If you want the height too, you can get it with SFont#height (the height is contant).

This method takes this argument:

string | the string to pretend to render.

    # File lib/rubygame/sfont.rb, line 201
    def string_width(string)
      w = 0
      string.each_byte { |glyph| w += @glyphs["%c"%[glyph]].width }
      return w
    end

### Private Instance Methods

blit_glyph(glyph,surface,pos) click to toggle source

This is a private function which is used to render a string.

Blit a single glyph to a Surface at the given position.

This private method takes these arguments:

glyph | a string containing the glyph to blit.
surface | the target surface to blit onto.
pos | an Array of the x and y values to blit the glyph to.

    # File lib/rubygame/sfont.rb, line 189
    def blit_glyph(glyph,surface,pos) # :doc:
      @glyphs[glyph].blit(surface,pos)
    end

load_glyph(surface,glyph,start_x) click to toggle source

This is a private function which is used to parse the font image.

Create a Surface for a single glyph, and store it as a value in the +@glyphs+ hash, indexed by the glyph (string) it represents.

Starting at a pixel in the "skip" region to the left of the glyph. Scans to the right along the top row until it finds a non-skip pixel (this is where the glyph starts) and then scans until it finds a skip pixel (this is where the glyph ends.

Returns the x value it stops at, plus 1. This should be fed back in for the `start_x` of the next glyph.

This private method takes these arguments:

surface | the Surface containing all glyph image data.
glyph | a string containing the current glyph
start_x | the x position to start scanning at.

    # File lib/rubygame/sfont.rb, line 151
    def load_glyph(surface,glyph,start_x) # :doc:
      # find where this glyph starts
      begin
        while(surface.get_at(start_x,0)[0..2] == @skip)
          start_x += 1
        end
      rescue IndexError
        return -1
      end

      end_x = start_x

      # find how wide this glyph is
      begin
        while(surface.get_at(end_x,0)[0..2] != @skip)
          end_x += 1
        end
      rescue IndexError
        return -1
      end

      # make a new surface for the glyph and blit the image onto it
      rect = Rect.new(start_x, 0, end_x-start_x, surface.h)
      @glyphs[glyph] = Surface.new(rect.size)
      surface.blit(@glyphs[glyph],[0,0],rect)

      return end_x+1
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.