class ShogiKoma::Painter

Attributes

body_color[R]
border_scale[RW]
font[RW]
frame_color[R]
height[RW]
text_color[R]
width[RW]

Public Class Methods

new() click to toggle source
# File lib/shogi_koma/painter.rb, line 8
def initialize
  @width = 200
  @height = 200
  @font = "IPAMincho"
  @border_scale = 1.0
  set_body_rgb(1, 0.8, 0.2)
  set_frame_color(:black)
  set_text_color(:black)
end

Public Instance Methods

draw(context, text) click to toggle source
# File lib/shogi_koma/painter.rb, line 59
def draw(context, text)
  draw_body(context)
  text = divide(text)
  __send__("draw_text#{text.length}", context, text)
end
draw_body(context) click to toggle source
# File lib/shogi_koma/painter.rb, line 65
def draw_body(context)
  border_width = 0.01 * @border_scale
  context.set_line_width(border_width)
  context.move_to(0.2, 0.2)
  context.line_to(0.5, 0.1)
  context.line_to(0.8, 0.2)
  context.line_to(0.9, 0.9)
  context.line_to(0.1, 0.9)
  context.close_path
  context.set_source_color(@body_color)
  context.fill_preserve
  context.set_source_color(@frame_color)
  context.stroke
end
draw_text1(context, text) click to toggle source
# File lib/shogi_koma/painter.rb, line 80
def draw_text1(context, text)
  context.set_source_color(@text_color)
  text = text[0] if text.is_a?(Array)
  context.select_font_face(@font)
  context.font_size = 0.6
  context.move_to(0.2, 0.75)
  context.show_text(text)
end
draw_text2(context, text) click to toggle source
# File lib/shogi_koma/painter.rb, line 89
def draw_text2(context, text)
  context.set_source_color(@text_color)
  context.select_font_face(@font)
  context.font_size = 0.4
  context.move_to(0.3, 0.49)
  context.show_text(text[0])
  context.move_to(0.3, 0.85)
  context.show_text(text[1])
end
set_body_color(color) click to toggle source
# File lib/shogi_koma/painter.rb, line 22
def set_body_color(color)
  @body_color = Cairo::Color.parse(color)
end
set_body_rgb(r, g, b, a=1.0)
Alias for: set_body_rgba
set_body_rgba(r, g, b, a=1.0) click to toggle source
# File lib/shogi_koma/painter.rb, line 26
def set_body_rgba(r, g, b, a=1.0)
  @body_color = Cairo::Color.parse([:rgba, r, g, b, a])
end
Also aliased as: set_body_rgb
set_font(part_of_font_name) click to toggle source
# File lib/shogi_koma/painter.rb, line 18
def set_font(part_of_font_name)
  @font = Fontdock::Local.find(part_of_font_name)
end
set_frame_color(color) click to toggle source
# File lib/shogi_koma/painter.rb, line 31
def set_frame_color(color)
  @frame_color = Cairo::Color.parse(color)
end
set_frame_rgb(r, g, b, a=1.0)
Alias for: set_frame_rgba
set_frame_rgba(r, g, b, a=1.0) click to toggle source
# File lib/shogi_koma/painter.rb, line 35
def set_frame_rgba(r, g, b, a=1.0)
  @frame_color = Cairo::Color.parse([:rgba, r, g, b, a])
end
Also aliased as: set_frame_rgb
set_text_color(color) click to toggle source
# File lib/shogi_koma/painter.rb, line 40
def set_text_color(color)
  @text_color = Cairo::Color.parse(color)
end
set_text_rgb(r, g, b, a=1.0)
Alias for: set_text_rgba
set_text_rgba(r, g, b, a=1.0) click to toggle source
# File lib/shogi_koma/painter.rb, line 44
def set_text_rgba(r, g, b, a=1.0)
  @text_color = Cairo::Color.parse([:rgba, r, g, b, a])
end
Also aliased as: set_text_rgb
write_to_png(text, output_path) click to toggle source
# File lib/shogi_koma/painter.rb, line 49
def write_to_png(text, output_path)
  Cairo::ImageSurface.new(:argb32, @width, @height) do |surface|
    Cairo::Context.new(surface) do |context|
      context.scale(@width, @height)
      draw(context, text)
    end
    surface.write_to_png(output_path)
  end
end

Private Instance Methods

divide(text) click to toggle source
# File lib/shogi_koma/painter.rb, line 100
def divide(text)
  case text.length
  when 1
    text
  when 2
    if text.bytes.to_a.length == 2
      [text]
    else
      text
    end
  else
    if text[0].bytes.to_a.length == 1 && text[1].bytes.to_a.length == 1
      [text[0..1], text[2..-1]]
    else
      [text[0], text[1..-1]]
    end
  end
end