SFontぽいもの

そんな話題を見かけたので作ってみた。
画像のデータはこちらからお借りした。
http://user.cs.tu-berlin.de/~karlb/sfont/fonts.html

SFontそのものを使ったことが無いからこういう感じでいいのかどうかよくわからない。
なんかそれっぽく描画できてるからいいんじゃね?みたいな。
参考までに。
テキトーに作っているのでバグってたら直して使ってください。絶対あるはず。

# SFont対応みたいなもの
require 'dxruby'

class SFont
  SFontData = Struct.new(:image, :size)

  def initialize(filename)
    image = Image.load(filename)
    @sfont_array = []

    x = 0

    # 紫の線を飛ばす
    while image.compare(x, 0, [255,255,0,255]) do
      x += 1
    end

    while x < image.width
      # 文字部分をsliceする
      char_start_x = x
      while !image.compare(x, 0, [255,255,0,255]) do
        x += 1
      end
      @sfont_array << SFontData.new(image.slice(char_start_x, 0, x - char_start_x, image.height), x - char_start_x)

      # 紫の線を飛ばす
      while image.compare(x, 0, [255,255,0,255]) && x < image.width do
        x += 1
      end
    end
  end

  def [](index)
    @sfont_array[index - 0x21]
  end
end

module Window
  def self.draw_sfont(x, y, str, sfont, z = 0)
    str.each_byte do |code|
      Window.draw(x, y, sfont[code].image, z)
      x += sfont[code].size
    end
  end
end

class Image
  def draw_sfont(x, y, str, sfont)
    str.each_byte do |code|
      self.draw(x, y, sfont[code].image)
      x += sfont[code].size
    end
  end
end

str1 = "!\"\#$%&'()*+,-./0123456789:;<=>?@"
str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"
str3 = "abcdefghijklmnopqrstuvwxyz{|}~"

sfont = SFont.new('24P_Arial_NeonYellow.png')
image = Image.new(640, 108)
image.draw_sfont(0, 0, str1, sfont)
image.draw_sfont(0, 36, str2, sfont)
image.draw_sfont(0, 72, str3, sfont)

Window.loop do
  Window.draw_sfont(0, 10, str1, sfont)
  Window.draw_sfont(0, 46, str2, sfont)
  Window.draw_sfont(0, 82, str3, sfont)

  Window.draw(0, 200, image)
end

このアルゴリズムで紫の線が描画されないのが微妙に納得行かない。
変だなー

追記。
each_byteなら1.8/1.9.1両対応できると教えてもらったので変更した。
あ、紫の線って飛ばしたところだから描画されなくて当たり前やん。はっずかしー