Created
March 11, 2012 07:20
-
-
Save kozy4324/2015410 to your computer and use it in GitHub Desktop.
草野球のスコア画像を生成するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require 'rubygems' | |
require 'cairo' | |
file_in = ARGV[0] # "a.png" | |
name_in = ARGV[1] # "をとの,18,18|hoge bar,18,12" | |
score_in = ARGV[2] # "1,2,3,4,5,6,7|0,1,2,3,4,5,x" | |
m = 5 | |
w = 350 | |
h = 80 | |
b = h/5 | |
r = 7 | |
x = w-b*2*(r+2) | |
x_pos = (1..(r+1)).map {|i| x = x + b*2} | |
y_pos = [b, b*3] | |
header_chars = x_pos.each_with_index.map {|x, i| if i < r then (i+1).to_s else '計' end} | |
surface = Cairo::ImageSurface.new Cairo::FORMAT_ARGB32, w+2*m, h+2*m | |
c = Cairo::Context.new surface | |
# 背景 | |
c.fill do | |
c.set_source_rgb 0x00/255.0, 0x33/255.0, 0x00/255.0 | |
c.rectangle 0, 0, w+2*m, h+2*m | |
end | |
# 線 | |
c.translate m, m | |
c.stroke do | |
c.set_source_rgb 1, 1, 1 | |
c.line_join = Cairo::LineJoin::ROUND | |
c.line_width = 2 | |
c.move_to w/2, 0 | |
[[w,0],[w,h],[0,h],[0,0],[w/2,0]].each {|xy| c.line_to *xy} | |
y_pos.each do |y| | |
c.move_to 0, y | |
c.line_to w, y | |
end | |
x_pos.each do |x| | |
c.move_to x, 0 | |
c.line_to x, h | |
end | |
end | |
# スコア | |
def put_char1(c, char, size, x, y) | |
c.font_size = size | |
c.move_to x+size/10*(6-char.size), y+size/2-1 | |
c.show_text char | |
end | |
header_chars.each_with_index do |char, i| | |
put_char1 c, char, 11, x_pos[i]+b/2, b/2 | |
end | |
def put_char2(c, char, size, x, y) | |
c.font_size = size | |
if char.size == 1 | |
c.move_to x+size/10-1, y+size/2-3 | |
else | |
c.move_to x+size/10-1-7, y+size/2-3 | |
end | |
c.show_text char | |
end | |
all_scores = score_in.split("|").map {|scores| scores.split ","} | |
all_scores.each_with_index do |scores, y_i| | |
sum = scores.inject 0 do |s,i| | |
s+i.to_i | |
end | |
scores.push sum | |
scores.each_with_index do |score, i| | |
put_char2 c, score.to_s, 24, x_pos[i]+b/2, b*2+y_i*b*2 | |
end | |
end | |
def put_str(c, str, size, x, y) | |
c.font_size = size | |
c.move_to x+size/10-1, y+size/2-3 | |
c.show_text str | |
end | |
names = name_in.split("|").map {|name| name.split ","} | |
names.each_with_index do |name_info, i| | |
name, size, offset = name_info | |
put_str c, name, size.to_i, offset.to_i, b*2+b*2*i | |
end | |
surface.write_to_png file_in | |
puts "create #{file_in}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby bb_score_img.rb "a.png" "をとの,18,18|hoge bar,18,12" "1,2,3,4,5,6,7|0,1,2,3,4,4,x" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment