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