ImageMagick/RMagickで将棋盤を描くpart1

ブラウザ将棋盤の機能拡張として盤面図を出力させようと思い、ImageMagickというかRMagickをいじってます。


大きさが固定されている、なぜか太字にならない等々の問題はありますが、とりあえずそれっぽいものは出力できました。

!# /usr/bin/ruby

require 'RMagick'

class ShogiBoard
  FU = ""
  KYO = ""
  KEI = ""
  GIN = ""
  KIN = ""
  KAKU = ""
  HI = ""
  GYOKU = ""
  TO = ""
  NARI_KYO = ""
  NARI_KEI = ""
  NARI_GIN = ""
  UMA = ""
  RYU = ""

  def initialize
    @status_onboard = [[[KYO, true], [KEI, true], [GIN, true], [KIN, true], [GYOKU, true], [KIN, true], [GIN, true], [KEI, true], [KYO, true]],
                       [nil, [HI, true], nil, nil, nil, nil, nil, [KAKU, true], nil],
                       [[FU, true], [FU, true], [FU, true], [FU, true], [FU, true], [FU, true], [FU, true], [FU, true], [FU, true]],
                       [nil, nil, nil, nil, nil, nil, nil, nil, nil],
                       [nil, nil, nil, nil, nil, nil, nil, nil, nil],
                       [nil, nil, nil, nil, nil, nil, nil, nil, nil],
                       [[FU, false], [FU, false], [FU, false], [FU, false], [FU, false], [FU, false], [FU, false], [FU, false], [FU, false]],
                       [nil, [KAKU, false], nil, nil, nil, nil, nil, [HI, false], nil],
                       [[KYO, false], [KEI, false], [GIN, false], [KIN, false], [GYOKU, false], [KIN, false], [GIN, false], [KEI, false], [KYO, false]]]
  end
  
  def draw
    canvas = Magick::ImageList.new
    canvas.new_image(270, 270, Magick::HatchFill.new("white", "black", 30))
    text = Magick::Draw.new
    text.pointsize = 29
    text.font = "/usr/share/fonts/truetype/ttf-japanese-gothic.ttf"
    @status_onboard.each_with_index do |row_pieces, y_index|
      row_pieces.each_with_index do |piece, x_index|
        if piece
          x = -120 + x_index * 30
          y = -120 + y_index * 30
          text.annotate(canvas, 0, 0, x, y, piece[0]) {
            self.gravity = Magick::CenterGravity
            if piece[1]
              self.rotation = 180
              self.font_weight = Magick::BoldWeight
            end
          }
        end
      end
    end
    canvas.display
  end
end


できあがりはこんな感じです。



あとは、レイヤーか何かを使って持ち駒の部分をくっつけ・・れればいいのですが。できなかったら、線の引っ張り方から調べ直しかなあ。


■参考
http://www.imagemagick.org/RMagick/doc/