縁取り完成。

たかが縁取り、されど縁取り。
検索しても出てこないので試行錯誤してしまった。

これにて完成を宣言しよう。
白黒専用だが。
ついでにシステムにインストールされてないフォントを使う機能も追加。
そして使えないサンプルを置いておく。

# DXRuby1.0.6サンプル
require 'dxruby'

class Image
  def drawFramingFont(x, y, str, font)
    image = Image.new(self.width, self.height).drawFont(x, y, str, font)
    # まず半透明の部分を黒にする
    for y in 0..self.height-1
      for x in 0..self.width-1
        temp = image[x, y][0]
        image[x, y] = [255, temp, temp, temp] if temp != 255 and temp != 0
      end
    end
    image2 = image.dup
    # 透明の部分に対して、横の影響を与える
    for y in 1..self.height-2
      for x in 1..self.width-2
        if image.compare(x, y, [0,0,0,0])
          temp_color = 0
          count = 0
          temp1 = image[x-1, y]
          temp2 = image[x+1, y]
          temp3 = image[x, y-1]
          temp4 = image[x, y+1]
          if temp1[0] != 0
            temp_color += temp1[1]
            count+=1
          end
          if temp2[0] != 0
            temp_color += temp2[1]
            count+=1
          end
          if temp3[0] != 0
            temp_color += temp3[1]
            count+=1
          end
          if temp4[0] != 0
            temp_color += temp4[1]
            count+=1
          end
          image2[x,y] = [temp_color / count,0,0,0] if count > 0
        end
      end
    end
    return self.draw(0, 0, image2)
  end
end

Font.install "HuiFont29.ttf"
font = Font.new(32, "ふい字")

image1 = Image.new(96, 48, [255,0,0]).drawFramingFont(0,8,"あ葱乙",font)
image2 = Image.new(96, 48, [255,255,255]).drawFramingFont(0,8,"あ葱乙",font)

Window.width = 96
Window.height = 96

Window.loop do
  Window.draw(0,0,image1)
  Window.draw(0,48,image2)
end

DXRubyに組み込まれるわけでもなく、静かに忘れられていく縁取り機能なのでした。

おまけ。
縦横にずらして黒い文字を描画して、真ん中に白い文字を描画するのが普通のやりかた。

require 'dxruby'

class Image
  def drawFramingFont(x, y, str, font)
    self.drawFont(x-1, y, str, font, [0,0,0])
    self.drawFont(x+1, y, str, font, [0,0,0])
    self.drawFont(x, y-1, str, font, [0,0,0])
    self.drawFont(x, y+1, str, font, [0,0,0])
    self.drawFont(x, y, str, font)
  end
end

Window.width = 500
Window.height = 100

font = Font.new(32)
image = Image.new(500, 100,[255,0,0]).drawFramingFont(0,0, "がおーがおーがおーわおー", font)

Window.loop do
  Window.draw(0,0, image)
end