Skip to content

Instantly share code, notes, and snippets.

@punund
Created June 19, 2011 13:59
Show Gist options
  • Save punund/1034309 to your computer and use it in GitHub Desktop.
Save punund/1034309 to your computer and use it in GitHub Desktop.
Dynamic creation of chess diagrams
#
# responds to requests with Forsyth-Edwards notation in URL with an image:
# http://dia-x.info/fen/2S5/1bQKpR2/4s3/2bkpP2/Sr1p2r1/1P1R3B/1B2s3/8
#
class FenController < ApplicationController
include Magick
FIGDIR = 'public/images/fig/'
SUFFIX = '.gif'
###########################################################
def index
cols, rows = 8, 8
i, j = 0, 0
@dia = $board
if params[:id].present?
slashed = params[:id].split '/'
slashed.collect! do |rank|
# replace numbers with spaces
rank.gsub!(/\d+/) {' ' * $&.to_i}
rank.ljust(cols) # fill up
end
slashed.join.each_byte do |c|
unless c == 32
c = c.chr
c < 'a' ? c.downcase! : c = 'b' + c
c.sub! /n/, 's'
putFigM c, i, j
end
(j+=25; i=0) if (i+=25) > 180
end
end
send_data @dia.to_blob, type: 'image/png', disposition: 'inline'
end
###########################################################
private
###########################################################
def putFigM(c, i, j)
begin
fig = Image.read(FIGDIR + c + SUFFIX)[0]
rescue
logger.info "bad symbol #{c}"
pen = Magick::Draw.new
pen.annotate(@dia, 0,0,0,40, "bad symbol: #{c}") do
self.font_family = 'Georgia'
self.fill = '#FF5353'
self.stroke = 'transparent'
self.pointsize = 24
self.font_weight = BoldWeight
self.gravity = SouthGravity
end
else
@dia = @dia.composite(fig, i+1, j+1, Magick::OverCompositeOp)
end
end
###########################################################
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment