add map_character
[chise/ruby.git] / src / stroke.rb
1 #!/usr/bin/env ruby
2 # StrokeFont library by eto 2003-0311
3
4 require 'sgl'
5 require 'kage'
6 require 'csf'
7
8 module StrokeFont
9   class StrokesRenderer #======================================================================
10     def initialize
11       @start_time = nil
12       @strokes = nil
13       @hsv = [0, 0, 100]
14       init
15     end
16     attr_accessor :hsv
17     def init() @start_time = Time.now; end
18     def set_strokes(strokes)
19       @strokes = strokes
20       init
21     end
22     def draw
23       return if @strokes.nil?
24       @strokes.strokes.each_with_index {|stroke, index|
25         #draw_delay(stroke, index)
26         draw_alpha(stroke, 100)
27       }
28     end
29     def draw_alpha(stroke, time)
30       px, py = 0, 0
31       span = 0.1
32       time += span*2
33       stroke.points.each {|x, y|
34         a = time / span
35         colorHSV(@hsv[0], @hsv[1], @hsv[2], a*100.0)
36         line(px, py, x, y) if (px != 0 || py != 0) #最初の点ではない
37         px, py = x, y
38         time -= span
39       }
40     end
41     def draw_delay(stroke, index)
42       now = Time.now
43       @start_time = Time.now if @start_time == nil
44       diff = now - @start_time #開始からの秒数がはいる
45       draw_alpha(stroke, diff - index*0.3)
46     end
47   end
48
49   class Stroke #====================================================================== 一本の線
50     def initialize
51       @points = []
52       @length = nil
53     end
54     attr_reader :points
55     def add_point(x, y)
56       @points << [x, y]
57     end
58     def length #未チェック
59       return @length if @length
60       len = 0.0
61       px, py = -1, -1
62       @points.each {|x, y|
63         if px != -1
64           len += Math.sqrt((x-px)*(x-px)+(y-py)*(y-py))
65         end
66         px, py = x, y
67       }
68       @length = len
69       return @length
70     end
71   end
72
73   class Strokes #====================================================================== 複数の線
74     def initialize
75       @strokes = []
76       @px1, @py1, @px2, @py2 = 0, 0, 0, 0
77       @x1, @y1, @x2, @y2 = 0, 0, 0, 0
78       @px, @py = -1, -1
79     end
80     attr_reader :strokes
81     def add_line(x1, y1, x2, y2)
82       if (@px != x1 || @py != y1) #以前の点とつながっていなかったら、
83         @strokes << Stroke.new
84         @strokes.last.add_point(x1, y1)
85       end
86       @strokes.last.add_point(x2, y2)
87       @px, @py = x2, y2
88     end
89   end
90 end
91
92 #----------------------------------------------------------------------end.