rubypost 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,4 +1,4 @@
1
- #RubyPost 0.0.4 README
1
+ #RubyPost 0.0.5 README
2
2
 
3
3
  You can download the latest version of rubypost from rubyforge. Type:
4
4
 
data/lib/draw.rb CHANGED
@@ -106,45 +106,45 @@ module RubyPost
106
106
 
107
107
  #alias DoubleArrow, implements the metapost drawdblarrow command
108
108
  class DblArrow < DoubleArrow
109
- end
110
-
111
-
112
- #This wraps the metapost clipping command. This needs
113
- #to be used with a little care. It will clip everything that
114
- #has been previously added to the picture when you add it.
115
- #Anything that gets drawn after clipping, wont be clipped.
116
- #This is in keeping with the way that metapost works,
117
- #however most other objects in rubypost the order inwhich
118
- #they are added has no effect (other than overlay).
119
- # <\br>
120
- #Clip only kind of fits as a draw command. It's doesn't need colours
121
- #and stuff. This is the best place to put it at the moment, but it
122
- #probably should inheret something else.
123
- class Clip < BaseDrawCommand
124
-
125
- attr_writer :path, :picture
126
-
127
- def initialize(path=nil, pic='currentpicture')
128
- super()
129
- @picture = pic
130
- @path = path
131
- end
132
-
133
- #set the clipping path
134
- def set_path(p)
135
- @path = p
136
- end
137
-
138
- #set the picture to clip. Defaults to 'currentpicture' see a metapost guide
139
- def set_picture(p='currentpicture')
140
- @picture = p
141
- end
142
-
143
- def compile
144
- "clip " + @picture.compile + " to " + @path.compile + ' ' + compile_options + ";\n"
145
- end
146
-
147
- end
109
+ end
110
+
111
+
112
+ #This wraps the metapost clipping command. This needs
113
+ #to be used with a little care. It will clip everything that
114
+ #has been previously added to the picture when you add it.
115
+ #Anything that gets drawn after clipping, wont be clipped.
116
+ #This is in keeping with the way that metapost works,
117
+ #however most other objects in rubypost the order inwhich
118
+ #they are added has no effect (other than overlay).
119
+ # <\br>
120
+ #Clip only kind of fits as a draw command. It's doesn't need colours
121
+ #and stuff. This is the best place to put it at the moment, but it
122
+ #probably should inheret something else.
123
+ class Clip < BaseDrawCommand
124
+
125
+ attr_writer :path, :picture
126
+
127
+ def initialize(path=nil, pic='currentpicture')
128
+ super()
129
+ @picture = pic
130
+ @path = path
131
+ end
132
+
133
+ #set the clipping path
134
+ def set_path(p)
135
+ @path = p
136
+ end
137
+
138
+ #set the picture to clip. Defaults to 'currentpicture' see a metapost guide
139
+ def set_picture(p='currentpicture')
140
+ @picture = p
141
+ end
142
+
143
+ def compile
144
+ "clip " + @picture.compile + " to " + @path.compile + ' ' + compile_options + ";\n"
145
+ end
146
+
147
+ end
148
148
 
149
149
 
150
150
  end
data/lib/drawable.rb CHANGED
@@ -1,209 +1,199 @@
1
- require 'matrix'
2
- #require 'vector'
3
-
4
-
5
- module RubyPost
6
-
7
- #rubypost drawable
8
- #base class for anything that actually gets drawn on a figure
9
- class Drawable < Object
10
- end
11
-
12
- #convert a number string to letter string
13
- #this just avoids some silly metapost problems with
14
- #numbers in names
15
- def numbers_to_letters(n)
16
- s = n.to_s
17
- str = ''
18
- s.bytes.to_a.each{ |b| str = str + (b + 20).chr }
19
- return str
20
- end
21
-
22
- #wrapper for the metapost picture
23
- #pictures are collection of drawables
24
- class Picture < Drawable
25
-
26
- attr_writer :name
27
-
28
- #store a unique picture name incase it was not specified.
29
- @@default_name = 0
30
-
31
- #intialise a picture with it's name, use a string!
32
- def initialize(name="pic" + numbers_to_letters(@@default_name) + "blerg")
33
- super()
34
- @name = name
35
- @draw_commands = Array.new
36
- @@default_name = @@default_name + 1
37
- @@picture_precompiler.add_picture(self)
38
- end
39
-
40
- def add_drawable(d)
41
- @draw_commands.push(d)
42
- end
43
-
44
- #creates the definition of the picture that goes
45
- #at the start of the file
46
- def precompile
47
- str = "picture " + @name + ";\n"
48
- @draw_commands.each do |d|
49
- str = str + d.compile + "\n"
50
- end
51
- str = str + @name + " := currentpicture; currentpicture := " + @@org_picture + ";\n"
52
- end
53
-
54
- def compile
55
- @name.compile
56
- end
57
-
58
- end
59
-
60
- #A custom drawable. Send it a metapost string
61
- class CustomDrawable < Drawable
62
-
63
- attr_writer :command
64
-
65
- def initialize(c=String.new)
66
- super()
67
- @command = c
68
- end
69
-
70
- def compile
71
- @command.compile
72
- end
73
-
74
- end
75
-
76
- #Cartesion point.
77
- class Pair < Drawable
78
-
79
- attr :x
80
- attr :y
81
-
82
- #Can set the value and individual scales of the x and y point
83
- def initialize(x=0,y=0)
84
- super()
85
- @x = x
86
- @y = y
87
- end
88
-
89
- def compile
90
- '(' + @x.compile + ', ' + @y.compile + ')'
91
- end
92
-
93
- #returns the addition of the pairs
94
- def +(p)
95
- Pair.new(@x + p.x, @y + p.y)
96
- end
97
-
98
- #returns the subtraction of the pairs
99
- def -(p)
100
- Pair.new(@x - p.x, @y - p.y)
101
- end
102
-
103
- #returns the pair multiplied by a scalar
104
- def *(n)
105
- Pair.new(@x*n, @y*n)
106
- end
107
-
108
- #returns the pair divided by a scalar
109
- def /(n)
110
- Pair.new(@x/n, @y/n)
111
- end
112
-
113
- #returns true if the pairs are equal
114
- def ==(p)
115
- @x == p.x && @y == p.y
116
- end
117
-
118
- end
119
-
120
- #sequence of pairs connected as a metapost path
121
- class Path < Drawable
122
-
123
- attr_writer :line_type
124
-
125
- def initialize
126
- super()
127
- @p = Array.new
128
- straight
129
- end
130
-
131
- def add_pair(p)
132
- @p.push(p)
133
- self
134
- end
135
-
136
- #returns a pair that is the centroid of the pairs
137
- #of this path
138
- def center(p)
139
- ret = Pair.new
140
- @p.each { |p| ret = ret + p }
141
- return ret/p.length
142
- end
143
-
144
- #reverse the path. <br>
145
- #Note, this reverses the pairs that have so far
146
- #been added. Anything added after calling reverse
147
- #will be appended to the end of the array as usual
148
- def reverse
149
- @p = @p.reverse
150
- self
151
- end
152
-
153
- #set the path to have straight line segments
154
- def straight
155
- @line_type = '--'
156
- self
157
- end
158
-
159
- #set the path to have curved line segments
160
- def curved
161
- @line_type = '..'
162
- self
163
- end
164
-
165
- def compile
166
- str = '('
167
- (@p.length-1).times do |i|
168
- str = str + @p[i].compile + @line_type
169
- end
170
- str + @p[-1].compile + ')'
171
- end
172
-
173
- def clone
174
- c = Path.new
175
- @p.each{ |i| c.add_pair(i) }
176
- c.line_type = @line_type
177
- c
178
- end
179
-
180
- end
181
-
182
- #Wraps teh metapost unitsquare command
183
- class Square < Path
184
-
185
- def initialize
186
- super()
187
- @p.push(Pair.new(-0.5,-0.5))
188
- @p.push(Pair.new(-0.5,0.5))
189
- @p.push(Pair.new(0.5,0.5))
190
- @p.push(Pair.new(0.5,-0.5))
191
- @p.push('cycle')
192
- end
193
-
194
- end
195
-
196
- #Wraps the metapost fullcircle command
197
- class Circle < Drawable
198
-
199
- def initialize
200
- super()
201
- end
202
-
203
- def compile
204
- 'fullcircle'
205
- end
206
-
207
- end
208
-
1
+ require 'matrix'
2
+ #require 'vector'
3
+
4
+
5
+ module RubyPost
6
+
7
+ #rubypost drawable
8
+ #base class for anything that actually gets drawn on a figure
9
+ class Drawable < Object
10
+ end
11
+
12
+ #wrapper for the metapost picture
13
+ #pictures are collection of drawables
14
+ class Picture < Drawable
15
+
16
+ attr_writer :name
17
+
18
+ #store a unique picture name unless incase it was not specified.
19
+ @@default_name = 0
20
+
21
+ #intialise a picture with it's name, use a string!
22
+ def initialize(name="picture_number" + @@default_name.to_s)
23
+ super()
24
+ @name = name
25
+ @draw_commands = Array.new
26
+ @@default_name = @@default_name + 1
27
+ @@picture_precompiler.add_picture(self)
28
+ end
29
+
30
+ def add_drawable(d)
31
+ @draw_commands.push(d)
32
+ end
33
+
34
+ #creates the definition of the picture that goes
35
+ #at the start of the file
36
+ def precompile
37
+ str = "picture " + @name + ";\n"
38
+ @draw_commands.each do |d|
39
+ str = str + d.compile + "\n"
40
+ end
41
+ str = str + @name + " := currentpicture; currentpicture := " + @@org_picture + ";\n"
42
+ end
43
+
44
+ def compile
45
+ @name.compile
46
+ end
47
+
48
+ end
49
+
50
+ #A custom drawable. Send it a metapost string
51
+ class CustomDrawable < Drawable
52
+
53
+ attr_writer :command
54
+
55
+ def initialize(c=String.new)
56
+ super()
57
+ @command = c
58
+ end
59
+
60
+ def compile
61
+ @command.compile
62
+ end
63
+
64
+ end
65
+
66
+ #Cartesion point.
67
+ class Pair < Drawable
68
+
69
+ attr :x
70
+ attr :y
71
+
72
+ #Can set the value and individual scales of the x and y point
73
+ def initialize(x=0,y=0)
74
+ super()
75
+ @x = x
76
+ @y = y
77
+ end
78
+
79
+ def compile
80
+ '(' + @x.compile + ', ' + @y.compile + ')'
81
+ end
82
+
83
+ #returns the addition of the pairs
84
+ def +(p)
85
+ Pair.new(@x + p.x, @y + p.y)
86
+ end
87
+
88
+ #returns the subtraction of the pairs
89
+ def -(p)
90
+ Pair.new(@x - p.x, @y - p.y)
91
+ end
92
+
93
+ #returns the pair multiplied by a scalar
94
+ def *(n)
95
+ Pair.new(@x*n, @y*n)
96
+ end
97
+
98
+ #returns the pair divided by a scalar
99
+ def /(n)
100
+ Pair.new(@x/n, @y/n)
101
+ end
102
+
103
+ #returns true if the pairs are equal
104
+ def ==(p)
105
+ @x == p.x && @y == p.y
106
+ end
107
+
108
+ end
109
+
110
+ #sequence of pairs connected as a metapost path
111
+ class Path < Drawable
112
+
113
+ attr_writer :line_type
114
+
115
+ def initialize
116
+ super()
117
+ @p = Array.new
118
+ straight
119
+ end
120
+
121
+ def add_pair(p)
122
+ @p.push(p)
123
+ self
124
+ end
125
+
126
+ #returns a pair that is the centroid of the pairs
127
+ #of this path
128
+ def center(p)
129
+ ret = Pair.new
130
+ @p.each { |p| ret = ret + p }
131
+ return ret/p.length
132
+ end
133
+
134
+ #reverse the path. <br>
135
+ #Note, this reverses the pairs that have so far
136
+ #been added. Anything added after calling reverse
137
+ #will be appended to the end of the array as usual
138
+ def reverse
139
+ @p = @p.reverse
140
+ self
141
+ end
142
+
143
+ #set the path to have straight line segments
144
+ def straight
145
+ @line_type = '--'
146
+ self
147
+ end
148
+
149
+ #set the path to have curved line segments
150
+ def curved
151
+ @line_type = '..'
152
+ self
153
+ end
154
+
155
+ def compile
156
+ str = '('
157
+ (@p.length-1).times do |i|
158
+ str = str + @p[i].compile + @line_type
159
+ end
160
+ str + @p[-1].compile + ')'
161
+ end
162
+
163
+ def clone
164
+ c = Path.new
165
+ @p.each{ |i| c.add_pair(i) }
166
+ c.line_type = @line_type
167
+ c
168
+ end
169
+
170
+ end
171
+
172
+ #Wraps teh metapost unitsquare command
173
+ class Square < Path
174
+
175
+ def initialize
176
+ super()
177
+ @p.push(Pair.new(-0.5,-0.5))
178
+ @p.push(Pair.new(-0.5,0.5))
179
+ @p.push(Pair.new(0.5,0.5))
180
+ @p.push(Pair.new(0.5,-0.5))
181
+ @p.push('cycle')
182
+ end
183
+
184
+ end
185
+
186
+ #Wraps the metapost fullcircle command
187
+ class Circle < Drawable
188
+
189
+ def initialize
190
+ super()
191
+ end
192
+
193
+ def compile
194
+ 'fullcircle'
195
+ end
196
+
197
+ end
198
+
209
199
  end