Class: Rubygame::SFont

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

Overview

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/

Constant Summary collapse

@@default_glyphs =
[\
"!",'"',"#","$","%","&","'","(",")","*","+",",","-",".","/","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","{","|","}","~"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, glyphs = nil, spacew = nil) ⇒ SFont

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 (“).



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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

Instance Attribute Details

#heightObject (readonly)

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).



132
133
134
# File 'lib/rubygame/sfont.rb', line 132

def height
  @height
end

Class Method Details

.default_glyphsObject

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 { | } ~


63
64
65
# File 'lib/rubygame/sfont.rb', line 63

def SFont.default_glyphs
  @@default_glyphs
end

Instance Method Details

#render(string) ⇒ Object

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

This method takes this argument:

string

the text string to render.



211
212
213
214
215
216
217
218
219
220
# 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) ⇒ Object

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.



201
202
203
204
205
# 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