rubypost 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/objects.rb CHANGED
@@ -1,132 +1,166 @@
1
1
  module RubyPost
2
2
 
3
- #base class for rubypost
4
- class Object
5
- #returns the string of metapost commmands
6
- def compile
7
- '%compile not implemented for ' + self.class.to_s
3
+ #base class for rubypost
4
+ class Object
5
+ #returns the string of metapost commmands
6
+ def compile
7
+ '%compile not implemented for ' + self.class.to_s
8
+ end
8
9
  end
9
- end
10
10
 
11
- #stores the macros that particular drawbles need.
12
- #This should really be a private class.
13
- class Macros < Object
11
+ #stores the macros that particular drawbles need.
12
+ #This should really be a private class.
13
+ class Macros < Object
14
14
 
15
- def initialize
16
- @inputs = Hash.new
17
- end
15
+ def initialize
16
+ @inputs = Hash.new
17
+ end
18
18
 
19
- #uses hash to make sure we never input same thing twice
20
- def add_input(s)
21
- @inputs[s] = nil
22
- end
19
+ #uses hash to make sure we never input same thing twice
20
+ def add_input(s)
21
+ @inputs[s] = nil
22
+ end
23
23
 
24
- def compile
25
- str = String.new
26
- @inputs.each_key do
27
- |k| str = str + "input " + k + ";\n"
24
+ def compile
25
+ str = String.new
26
+ @inputs.each_key do
27
+ |k| str = str + "input " + k + ";\n"
28
+ end
29
+ str
28
30
  end
29
- str
31
+
30
32
  end
33
+
34
+ #stores the macros that particular drawbles need.
35
+ #This should really be a private class.
36
+ class PicturePrecompiler < Object
31
37
 
32
- end
38
+ def initialize
39
+ @pictures = Array.new
40
+ end
41
+
42
+ def add_picture(s)
43
+ @pictures.push(s)
44
+ end
45
+
46
+ def compile
47
+ str = "picture " + @@org_picture + ";\n" + @@org_picture + " := currentpicture;\n"
48
+ @pictures.each do
49
+ |p| str = str + p.precompile + "\n"
50
+ end
51
+ str
52
+ end
53
+
54
+ end
55
+
56
+ #module variable to be altered by drawables that
57
+ #need a particular metapost macro input.
58
+ @@picture_precompiler = PicturePrecompiler.new
59
+
60
+ #module variable to be altered by drawables that
61
+ #need a particular metapost macro input.
62
+ @@Inputs = Macros.new
33
63
 
34
- #global module variable to be altered by drawables that
35
- #need a particular metapost macro input.
36
- $Inputs = Macros.new
64
+ #this is the origninal metapost picture that is
65
+ #what actually gets displayed. You shouldn't use
66
+ #this name for any other picture!
67
+ @@org_picture = "ORIGINAL_PICTURE"
37
68
 
38
- #metapost file
39
- #A metapost file can contain many figures.
40
- #Notes: Filenames cannot contain underscores for view to work! <br>
41
- #compile_to_str has a dodgy backspace handler.
42
- class File < Object
69
+ #metapost file
70
+ #A metapost file can contain many figures.
71
+ #Notes: Filenames cannot contain underscores for view to work! <br>
72
+ #compile_to_str has a dodgy backspace handler.
73
+ class File < Object
43
74
 
44
- attr_writer :fname
75
+ attr_writer :fname
45
76
 
46
- @@start_of_file = "prologues := 2;\n"
77
+ @@start_of_file = "prologues := 2;\n"
47
78
 
48
- #input 'sarith' so that metapost can read exponential notation
49
- def initialize(fname = nil)
50
- @figures = Array.new
51
- @fname = fname
52
- end
79
+ #input 'sarith' so that metapost can read exponential notation
80
+ def initialize(fname = nil)
81
+ @figures = Array.new
82
+ @fname = fname
83
+ end
53
84
 
54
- #add a new figure to this mpost file
55
- def add_figure(f)
56
- @figures.push(f)
57
- end
85
+ #add a new figure to this mpost file
86
+ def add_figure(f)
87
+ @figures.push(f)
88
+ end
58
89
 
59
- #returns the mp file as a str
60
- def compile_to_string
61
- str = @@start_of_file + $Inputs.compile
62
- @figures.each_index do
63
- |i| str = str + 'beginfig(' + (i+1).to_s + ");\n" + @figures[i].compile + "\n"
90
+ #returns the mp file as a str
91
+ def compile_to_string
92
+ str = @@start_of_file + @@Inputs.compile
93
+ #save the original metapost picture
94
+ str = str + @@picture_precompiler.compile
95
+ @figures.each_index do
96
+ |i| str = str + 'beginfig(' + (i+1).to_s + ");\n" + @figures[i].compile + "\n"
97
+ end
98
+ str = str + "end;\n"
99
+ #remove the backspaces
100
+ strback = str.gsub(/.[\b]/, '')
101
+ if (strback==nil)
102
+ return str
103
+ else
104
+ return strback
105
+ end
64
106
  end
65
- str = str + "end;\n"
66
- #remove the backspaces
67
- strback = str.gsub(/.[\b]/, '')
68
- if (strback==nil)
69
- return str
70
- else
71
- return strback
107
+
108
+ #writes the string of metapost commands to a file named 'fname.mp'
109
+ def compile_to_file(fname=@fname)
110
+ @fname = fname
111
+ IO::File.open(fname + '.mp','w') { |f| f.puts self.compile_to_string }
72
112
  end
73
- end
74
113
 
75
- #writes the string of metapost commands to a file named 'fname.mp'
76
- def compile_to_file(fname=@fname)
77
- @fname = fname
78
- IO::File.open(fname + '.mp','w') { |f| f.puts self.compile_to_string }
79
- end
114
+ #calls compile_to_file and writes the
115
+ #and copmiles the metapost commands if mpost is in the path
116
+ def compile(fname=@fname)
117
+ compile_to_file(fname)
118
+ system('mpost -quiet ' + fname + '.mp')
119
+ end
80
120
 
81
- #calls compile_to_file and writes the
82
- #and copmiles the metapost commands if mpost is in the path
83
- def compile(fname=@fname)
84
- compile_to_file(fname)
85
- system('mpost -quiet ' + fname + '.mp')
86
- end
121
+ #default view command is view_dvi
122
+ def view
123
+ view_dvi
124
+ end
87
125
 
88
- #default view command is view_dvi
89
- def view
90
- view_dvi
91
- end
126
+ #assumes that the file has already been compiled by metapost. ie compile_to_ps
127
+ #has already been called. This assumes that the yap viewer and tex is in your path. Install
128
+ #miktex to get these by default. <p>
129
+ #Notes: Filenames cannot contain underscores for view_dvi to work. The "tex mproof" will
130
+ #not work with underscores
131
+ def view_dvi
132
+ str = 'tex mproof'
133
+ @figures.each_index { |i| str = str + ' ' + @fname + '.' + (i+1).to_s }
134
+ system(str)
135
+ system('yap mproof.dvi')
136
+ end
92
137
 
93
- #assumes that the file has already been compiled by metapost. ie compile_to_ps
94
- #has already been called. This assumes that the yap viewer and tex is in your path. Install
95
- #miktex to get these by default. <p>
96
- #Notes: Filenames cannot contain underscores for view_dvi to work. The "tex mproof" will
97
- #not work with underscores
98
- def view_dvi
99
- str = 'tex mproof'
100
- @figures.each_index { |i| str = str + ' ' + @fname + '.' + (i+1).to_s }
101
- system(str)
102
- system('yap mproof.dvi')
103
138
  end
104
-
105
- end
106
139
 
107
- #wrapper for the metapost figure
108
- #Figures actually become the figures that will to be viewed
109
- class Figure < Object
140
+ #wrapper for the metapost figure
141
+ #Figures actually become the figures that will to be viewed
142
+ class Figure < Object
110
143
 
111
- def initialize
112
- @drawables = Array.new
113
- end
144
+ def initialize
145
+ @draw_commands = Array.new
146
+ end
114
147
 
115
- def add_drawable(d)
116
- @drawables.push(d)
117
- end
148
+ def add_drawable(d)
149
+ @draw_commands.push(d)
150
+ end
118
151
 
119
- def compile
120
- str = String.new
121
- @drawables.each do |d|
122
- str = str + d.draw_type + ' ' + d.compile + "\n"
152
+ def compile
153
+ str = String.new
154
+ @draw_commands.each do |d|
155
+ str = str + d.compile + "\n"
156
+ end
157
+ str = str + "endfig;\n"
158
+ return str
123
159
  end
124
- str = str + "endfig;\n"
125
- return str
126
- end
127
160
 
128
- end
161
+ end
129
162
 
163
+ #end module RubyPost
130
164
  end
131
165
 
132
166
  #Add the compile functionality to Ruby Numeric. Calling
data/lib/options.rb CHANGED
@@ -1,177 +1,177 @@
1
1
 
2
2
  module RubyPost
3
3
 
4
- #return the string s wrapped in btex etex
5
- def latex(s)
6
- 'btex ' + s + ' etex'
7
- end
8
-
9
- #options for rubypost drawables. These wrap metapose commands
10
- #such as 'withpen', 'scaled' and etc
11
- class Option < Object
12
- end
13
-
14
- #Can insert pure metapost option commands here.
15
- class CustomOption < Option
16
- def initialize
17
- @command = String.new
4
+ #return the string s wrapped in btex etex
5
+ def latex(s)
6
+ 'btex ' + s + ' etex'
18
7
  end
19
-
20
- def initialize(c=Sting.new)
21
- @command = c
8
+
9
+ #options for rubypost drawables. These wrap metapose commands
10
+ #such as 'withpen', 'scaled' and etc
11
+ class Option < Object
22
12
  end
13
+
14
+ #Can insert pure metapost option commands here.
15
+ class CustomOption < Option
16
+ def initialize
17
+ @command = String.new
18
+ end
23
19
 
24
- def compile
25
- @command.compile
26
- end
20
+ def initialize(c=Sting.new)
21
+ @command = c
22
+ end
23
+
24
+ def compile
25
+ @command.compile
26
+ end
27
27
 
28
- end
28
+ end
29
29
 
30
- #wrapper for the metapost withcolor command
31
- #Except colour is now spelt correctly.
32
- class Colour < Option
30
+ #wrapper for the metapost withcolor command
31
+ #Except colour is now spelt correctly.
32
+ class Colour < Option
33
33
 
34
- attr :r
35
- attr :g
36
- attr :b
34
+ attr :r
35
+ attr :g
36
+ attr :b
37
37
 
38
- def initialize(r=0,g=0,b=0)
39
- @r = r
40
- @g = g
41
- @b = b
42
- end
38
+ def initialize(r=0,g=0,b=0)
39
+ @r = r
40
+ @g = g
41
+ @b = b
42
+ end
43
43
 
44
- def compile
45
- 'withcolor' + '(' + @r.to_s + ',' + @g.to_s + ',' + @b.to_s + ')'
46
- end
44
+ def compile
45
+ 'withcolor' + '(' + @r.to_s + ',' + @g.to_s + ',' + @b.to_s + ')'
46
+ end
47
47
 
48
- end
48
+ end
49
49
 
50
- #incorrectly spelt Colour alias
51
- class Color < Colour
52
- end
50
+ #incorrectly spelt Colour alias
51
+ class Color < Colour
52
+ end
53
53
 
54
54
 
55
- #wraps the metapost withpen command
56
- class Pen < Option
55
+ #wraps the metapost withpen command
56
+ class Pen < Option
57
57
 
58
- attr_writer :pen_type, :scale
58
+ attr_writer :pen_type, :scale
59
59
 
60
- def initialize(pt='pencircle', scale = 1)
61
- @pen_type = pt
62
- @scale = scale
63
- end
60
+ def initialize(pt='pencircle', scale = 1)
61
+ @pen_type = pt
62
+ @scale = scale
63
+ end
64
64
 
65
- def compile
66
- 'withpen ' + @pen_type + ' scaled ' + @scale.compile
67
- end
65
+ def compile
66
+ 'withpen ' + @pen_type + ' scaled ' + @scale.compile
67
+ end
68
68
 
69
- end
69
+ end
70
70
 
71
- #base class for options related to paths
72
- class PathOption < Option
73
- end
71
+ #base class for options related to paths
72
+ class PathOption < Option
73
+ end
74
74
 
75
- #dased path
76
- #there are a plethora of dashing options. Only implemented
77
- #evenly at present.
78
- class Dashed < PathOption
75
+ #dased path
76
+ #there are a plethora of dashing options. Only implemented
77
+ #evenly at present.
78
+ class Dashed < PathOption
79
79
 
80
- def initialize(t='evenly')
80
+ def initialize(t='evenly')
81
81
  @type = t
82
- end
82
+ end
83
83
 
84
- #evenly dashed line
85
- def evenly
86
- @type = 'evenly'
87
- self
88
- end
84
+ #evenly dashed line
85
+ def evenly
86
+ @type = 'evenly'
87
+ self
88
+ end
89
89
 
90
- #dahed line with dots inbetween dashes
91
- def withdots
92
- @type = 'withdots'
93
- self
94
- end
90
+ #dahed line with dots inbetween dashes
91
+ def withdots
92
+ @type = 'withdots'
93
+ self
94
+ end
95
95
 
96
- #set the type with the metapost command s.
97
- def type(s)
98
- @type = s
99
- self
100
- end
96
+ #set the type with the metapost command s.
97
+ def type(s)
98
+ @type = s
99
+ self
100
+ end
101
101
 
102
- def compile
103
- 'dashed ' + @type
104
- end
102
+ def compile
103
+ 'dashed ' + @type
104
+ end
105
105
 
106
- end
106
+ end
107
107
 
108
- #Wrapped the scaled metapost command.
109
- #Resizes the drawable.
110
- class Scale < Option
108
+ #Wrapped the scaled metapost command.
109
+ #Resizes the drawable.
110
+ class Scale < Option
111
111
 
112
- attr_writer :scale
112
+ attr_writer :scale
113
113
 
114
- def initialize(scale=1)
115
- @scale = scale
116
- end
114
+ def initialize(scale=1)
115
+ @scale = scale
116
+ end
117
117
 
118
- def compile
119
- 'scaled ' + @scale.compile
120
- end
118
+ def compile
119
+ 'scaled ' + @scale.compile
120
+ end
121
121
 
122
- end
122
+ end
123
123
 
124
- #Scale alias
125
- class Scaled < Scale
126
- end
124
+ #Scale alias
125
+ class Scaled < Scale
126
+ end
127
127
 
128
- #Wraps the rotated metapost command
129
- class Rotate < Option
128
+ #Wraps the rotated metapost command
129
+ class Rotate < Option
130
130
 
131
- attr_writer :degrees
131
+ attr_writer :degrees
132
132
 
133
- def initialize(degrees=0)
134
- @degrees = degrees
135
- end
133
+ def initialize(degrees=0)
134
+ @degrees = degrees
135
+ end
136
136
 
137
- #set the angle in radiians
138
- def radians=(rads)
139
- @degrees = 180.0*rads/Math::PI
140
- self
141
- end
137
+ #set the angle in radiians
138
+ def radians=(rads)
139
+ @degrees = 180.0*rads/Math::PI
140
+ self
141
+ end
142
142
 
143
- def compile
144
- 'rotated ' + @degrees.to_s
145
- end
143
+ def compile
144
+ 'rotated ' + @degrees.to_s
145
+ end
146
146
 
147
- end
147
+ end
148
148
 
149
- #Rotate alias
150
- class Rotated < Rotate
151
- end
149
+ #Rotate alias
150
+ class Rotated < Rotate
151
+ end
152
152
 
153
- #Wraps the metapose shifted command. <br>
154
- #The translation @t is specifed by a Pair
155
- class Translate < Option
153
+ #Wraps the metapose shifted command. <br>
154
+ #The translation @t is specifed by a Pair
155
+ class Translate < Option
156
156
 
157
- attr_writer :t
157
+ attr_writer :t
158
158
 
159
- def initialize(p=Pair.new)
160
- @t = p
161
- end
159
+ def initialize(p=Pair.new)
160
+ @t = p
161
+ end
162
162
 
163
- def compile
164
- 'shifted ' + @t.compile
165
- end
163
+ def compile
164
+ 'shifted ' + @t.compile
165
+ end
166
166
 
167
- end
167
+ end
168
168
 
169
- #Translate alias
170
- class Shifted < Translate
171
- end
169
+ #Translate alias
170
+ class Shifted < Translate
171
+ end
172
172
 
173
- #Translate alias
174
- class Shift < Translate
175
- end
173
+ #Translate alias
174
+ class Shift < Translate
175
+ end
176
176
 
177
177
  end
@@ -1,6 +1,6 @@
1
- #puts Float#to_s back to it's original state
2
- class Float
3
- def to_s
4
- orig_to_s
5
- end
6
- end
1
+ #puts Float#to_s back to it's original state
2
+ class Float
3
+ def to_s
4
+ orig_to_s
5
+ end
6
+ end
data/lib/rubypost.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  #load the rest of rubypost
2
2
  require 'objects'
3
3
  require 'drawable'
4
+ require 'draw'
4
5
  require 'options'
5
6
  require 'graph'
@@ -1,10 +1,9 @@
1
- #require 'rubygems'
2
- #gem 'rubypost'
1
+ #code to generate a plot of the data in metapost format
3
2
  #require 'rubypost'
4
3
 
5
- #load the rest of rubypost
6
4
  require '../lib/objects'
7
5
  require '../lib/drawable'
6
+ require '../lib/draw'
8
7
  require '../lib/options'
9
8
 
10
9
  include RubyPost
@@ -16,8 +15,8 @@ file = RubyPost::File.new('testcircle')
16
15
  #draw a circle
17
16
  fig1 = Figure.new
18
17
  file.add_figure(fig1)
19
- circle1 = RubyPost::Circle.new.scale(1.cm)
20
- fig1.add_drawable(circle1)
18
+ circle1 = RubyPost::Circle.new
19
+ fig1.add_drawable(Draw.new(circle1).scale(1.cm))
21
20
 
22
21
  puts file.compile_to_string
23
22
  file.compile
data/tests/test_graph.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  #code to generate a plot of the data in metapost format
2
- require 'rubypost'
3
- require 'drawable'
4
- require 'options'
5
- require 'graph'
2
+ #require 'rubypost'
3
+
4
+ require '../lib/objects'
5
+ require '../lib/drawable'
6
+ require '../lib/draw'
7
+ require '../lib/options'
8
+ require '../lib/graph'
6
9
 
7
10
  include RubyPost
8
11
 
@@ -11,11 +14,11 @@ fig = Figure.new
11
14
  file.add_figure(fig)
12
15
 
13
16
  graph = Graph.new
14
- graph.add_option(XLabel.new(latex('x label')))
15
- graph.add_option(GraphTitle.new(latex('A graph title')))
16
- graph.add_option(YLabel.new(latex('the y label')))
17
- graph.add_option(YLabelRight.new(latex('the y label on the right')))
18
- fig.add_drawable(graph)
17
+ graph.add_graph_option(XLabel.new(latex('x label')))
18
+ graph.add_graph_option(GraphTitle.new(latex('A graph title')))
19
+ graph.add_graph_option(YLabel.new(latex('the y label')))
20
+ graph.add_graph_option(YLabelRight.new(latex('the y label on the right')))
21
+ fig.add_drawable(Draw.new(graph))
19
22
 
20
23
  #plot y = x^2
21
24
  x = (1..20).to_a
@@ -25,6 +28,6 @@ gd = GraphData.new(x, y)
25
28
  gd.add_option(RubyPost::Colour.new(0.0,1.0,0.0))
26
29
  graph.add_data(gd)
27
30
 
28
- file.compile('figures')
31
+ file.compile('testgraph')
29
32
 
30
33
  file.view
data/tests/test_pair.rb CHANGED
@@ -1,13 +1,11 @@
1
1
  require 'test/unit'
2
2
 
3
- #require 'rubygems'
4
- #gem 'rubypost'
5
3
  #require 'rubypost'
6
4
 
7
- #load the rest of rubypost
8
5
  require '../lib/objects'
9
6
  require '../lib/drawable'
10
- require '../lib/options'
7
+
8
+
11
9
 
12
10
  include RubyPost
13
11