Class: ShogiKoma::Painter

Inherits:
Object
  • Object
show all
Defined in:
lib/shogi_koma/painter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePainter

Returns a new instance of Painter.



6
7
8
9
10
# File 'lib/shogi_koma/painter.rb', line 6

def initialize
  @width = 200
  @height = 200
  @font = "IPAMincho"
end

Instance Attribute Details

#fontObject

Returns the value of attribute font.



5
6
7
# File 'lib/shogi_koma/painter.rb', line 5

def font
  @font
end

#heightObject

Returns the value of attribute height.



5
6
7
# File 'lib/shogi_koma/painter.rb', line 5

def height
  @height
end

#widthObject

Returns the value of attribute width.



5
6
7
# File 'lib/shogi_koma/painter.rb', line 5

def width
  @width
end

Instance Method Details

#divide(text) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/shogi_koma/painter.rb', line 42

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

#draw(context, text) ⇒ Object



22
23
24
25
26
# File 'lib/shogi_koma/painter.rb', line 22

def draw(context, text)
  draw_body(context)
  text = divide(text)
  send("draw_text#{text.length}", context, text)
end

#draw_body(context) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/shogi_koma/painter.rb', line 28

def draw_body(context)
  context.set_line_width(0.01)
  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_rgb(1, 0.8, 0.2)
  context.fill_preserve
  context.set_source_color(:black)
  context.stroke
end

#draw_text1(context, text) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/shogi_koma/painter.rb', line 61

def draw_text1(context, text)
  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) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/shogi_koma/painter.rb', line 69

def draw_text2(context, text)
  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

#write_to_png(text, output_path) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/shogi_koma/painter.rb', line 12

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