rasem 0.6.1 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,103 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Rasem::SVGTagWithParent do
4
+
5
+ it "should get a reference to the main image object" do
6
+ img = Rasem::SVGImage.new(:width => 100, :height => 100)
7
+ l = img.line(0, 0, 100, 100)
8
+
9
+ (l.img == img).should == true
10
+ end
11
+
12
+
13
+ it "should propagate the reference to the main image object" do
14
+ img = Rasem::SVGImage.new(:width => 100, :height => 100)
15
+ l = img.group.group.line(0, 0, 100, 100)
16
+
17
+ (l.img == img).should == true
18
+ end
19
+
20
+
21
+ end
22
+
23
+
24
+ describe Rasem::SVGImage do
25
+
26
+ it "should create a defs section" do
27
+ img = Rasem::SVGImage.new(:width => 100, :height => 100) do
28
+ defs do
29
+ group(:id => "group1") do
30
+ circle(0, 0, 20)
31
+ end
32
+ end
33
+ end
34
+
35
+ img.to_s.should =~ %r{.*<svg[^>]*>.*<defs>.*<g[^>]*id="group1">.*<circle[^>]*/></g>.*</defs>.*</svg>}
36
+ end
37
+
38
+
39
+ it "should also create a defs section" do
40
+ img = Rasem::SVGImage.new(:width => 100, :height => 100) do
41
+ def_group("group1") do
42
+ circle(0, 0, 20)
43
+ end
44
+ end
45
+
46
+ img.to_s.should =~ %r{.*<svg[^>]*>.*<defs>.*<g[^>]*id="group1">.*<circle[^>]*/></g>.*</defs>.*</svg>}
47
+ end
48
+
49
+
50
+ it "should update the existing definition" do
51
+ img = Rasem::SVGImage.new(:width => 100, :height => 100) do
52
+ def_group("group1") do
53
+ circle(0, 0, 20)
54
+ end
55
+
56
+ def_group("group1", :update) do
57
+ circle(0, 0, 40)
58
+ end
59
+
60
+ end
61
+
62
+ img.to_s.should =~ %r{.*<svg[^>]*>.*<defs>.*<g[^>]*id="group1">.*<circle cx="0" cy="0" r="40"[^>]*/></g>.*</defs>.*</svg>}
63
+ end
64
+
65
+
66
+ it "should skip the new definition" do
67
+ img = Rasem::SVGImage.new(:width => 100, :height => 100) do
68
+ def_group("group1") do
69
+ circle(0, 0, 20)
70
+ end
71
+
72
+ def_group("group1") do
73
+ circle(0, 0, 40)
74
+ fail "should skip the block execution"
75
+ end
76
+
77
+ def_group("group1", :skip) do
78
+ circle(0, 0, 40)
79
+ fail "should skip the block execution"
80
+ end
81
+
82
+ end
83
+
84
+ img.to_s.should =~ %r{.*<svg[^>]*>.*<defs>.*<g[^>]*id="group1">.*<circle cx="0" cy="0" r="20"[^>]*/></g>.*</defs>.*</svg>}
85
+ end
86
+
87
+
88
+ it "should fail with the new definition" do
89
+ img = Rasem::SVGImage.new(:width => 100, :height => 100)
90
+ img.def_group("group1") do
91
+ circle(0, 0, 20)
92
+ end
93
+
94
+ expect{img.def_group("group1", :fail) do
95
+ circle(0, 0, 40)
96
+ end}.to raise_error
97
+
98
+ img.to_s.should =~ %r{.*<svg[^>]*>.*<defs>.*<g[^>]*id="group1">.*<circle cx="0" cy="0" r="20"[^>]*/></g>.*</defs>.*</svg>}
99
+ end
100
+
101
+
102
+ end
103
+
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Rasem::SVGLinearGradient do
4
+
5
+ it "should create a linear gradient in the defs section" do
6
+ img = Rasem::SVGImage.new(:width => 100, :height => 100) do
7
+ linearGradient("lgrad1") {}
8
+ end
9
+
10
+ img.to_s.should =~ %r{.*<svg[^>]*>.*<defs>.*<linearGradient id="lgrad1"/>.*</defs></svg}
11
+ end
12
+
13
+
14
+ it "should support adding stops" do
15
+ img = Rasem::SVGImage.new(:width => 100, :height => 100) do
16
+ linearGradient("lgrad1") do
17
+ stop("0%", "green", 1)
18
+ stop("100%", "blue", 1)
19
+ end
20
+ end
21
+
22
+ img.to_s.should =~ %r{.*<svg[^>]*>.*<defs>.*<linearGradient id="lgrad1">.*<stop offset="0%" stop-color="green" stop-opacity="1"/>.*<stop offset="100%" stop-color="blue" stop-opacity="1"/>.*</linearGradient>.*</defs></svg}
23
+ end
24
+
25
+ end
26
+
27
+
28
+ describe Rasem::SVGRadialGradient do
29
+
30
+ it "should create a radial gradient in the defs section" do
31
+ img = Rasem::SVGImage.new(:width => 100, :height => 100) do
32
+ radialGradient("rgrad1") {}
33
+ end
34
+
35
+ img.to_s.should =~ %r{.*<svg[^>]*>.*<defs>.*<radialGradient id="rgrad1"/>.*</defs></svg}
36
+ end
37
+
38
+
39
+ it "should support adding stops" do
40
+ img = Rasem::SVGImage.new(:width => 100, :height => 100) do
41
+ radialGradient("rgrad1") do
42
+ stop("0%", "green", 1)
43
+ stop("100%", "blue", 1)
44
+ end
45
+ end
46
+
47
+ img.to_s.should =~ %r{.*<svg[^>]*>.*<defs>.*<radialGradient id="rgrad1">.*<stop offset="0%" stop-color="green" stop-opacity="1"/>.*<stop offset="100%" stop-color="blue" stop-opacity="1"/>.*</radialGradient.*</defs></svg}
48
+ end
49
+
50
+
51
+
52
+ end
53
+
@@ -0,0 +1,227 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Rasem::SVGPath do
4
+
5
+ it "should be an instance of SVGTag" do
6
+ tag = Rasem::SVGPath.new
7
+ (class_hierarchy(tag).include? Rasem::SVGTag).should == true
8
+ end
9
+
10
+
11
+ it "should create an empty path" do
12
+ tag = Rasem::SVGPath.new
13
+ tag.to_s.should =~ %r{<path d=\"\"\/>}
14
+ end
15
+
16
+
17
+ it "should add a move command" do
18
+ tag = Rasem::SVGPath.new do
19
+ moveTo(0, 0)
20
+ end
21
+
22
+ tag.to_s.should =~ %r{d=\"\s*m\s*0,0\"\/>}
23
+ end
24
+
25
+
26
+ it "should add an absolut move command" do
27
+ tag = Rasem::SVGPath.new do
28
+ moveToA(0, 0)
29
+ end
30
+
31
+ tag.to_s.should =~ %r{d=\"\s*M\s*0,0\"\/>}
32
+ end
33
+
34
+
35
+ it "should add a line command" do
36
+ tag = Rasem::SVGPath.new do
37
+ lineTo(0, 0)
38
+ end
39
+
40
+ tag.to_s.should =~ %r{d=\"\s*l\s*0,0\"\/>}
41
+ end
42
+
43
+
44
+ it "should add an absolut line command" do
45
+ tag = Rasem::SVGPath.new do
46
+ lineToA(0, 0)
47
+ end
48
+
49
+ tag.to_s.should =~ %r{d=\"\s*L\s*0,0\"\/>}
50
+ end
51
+
52
+
53
+ it "should add an horizontal line command" do
54
+ tag = Rasem::SVGPath.new do
55
+ hlineTo(0)
56
+ end
57
+
58
+ tag.to_s.should =~ %r{d=\"\s*h\s*0\"\/>}
59
+ end
60
+
61
+
62
+ it "should add an absolut horizontal line command" do
63
+ tag = Rasem::SVGPath.new do
64
+ hlineToA(0)
65
+ end
66
+
67
+ tag.to_s.should =~ %r{d=\"\s*H\s*0\"\/>}
68
+ end
69
+
70
+
71
+ it "should add an vertical line command" do
72
+ tag = Rasem::SVGPath.new do
73
+ vlineTo(0)
74
+ end
75
+
76
+ tag.to_s.should =~ %r{d=\"\s*v\s*0\"\/>}
77
+ end
78
+
79
+
80
+ it "should add an absolut vertical line command" do
81
+ tag = Rasem::SVGPath.new do
82
+ vlineToA(0)
83
+ end
84
+
85
+ tag.to_s.should =~ %r{d=\"\s*V\s*0\"\/>}
86
+ end
87
+
88
+
89
+ it "should add a curve command" do
90
+ tag = Rasem::SVGPath.new do
91
+ curveTo(10, 10, 0, 0, 5, 5)
92
+ end
93
+
94
+ tag.to_s.should =~ %r{d=\"\s*c\s*0,0\s*5,5\s*10,10\"\/>}
95
+ end
96
+
97
+
98
+ it "should add an absolut curve command" do
99
+ tag = Rasem::SVGPath.new do
100
+ curveToA(10, 10, 0, 0, 5, 5)
101
+ end
102
+
103
+ tag.to_s.should =~ %r{d=\"\s*C\s*0,0\s*5,5\s*10,10\"\/>}
104
+ end
105
+
106
+
107
+ it "should add a smooth curve command" do
108
+ tag = Rasem::SVGPath.new do
109
+ scurveTo(10, 10, 5, 5)
110
+ end
111
+
112
+ tag.to_s.should =~ %r{d=\"\s*s\s*5,5\s*10,10\"\/>}
113
+ end
114
+
115
+
116
+ it "should add an absolut smooth curve command" do
117
+ tag = Rasem::SVGPath.new do
118
+ scurveToA(10, 10, 5, 5)
119
+ end
120
+
121
+ tag.to_s.should =~ %r{d=\"\s*S\s*5,5\s*10,10\"\/>}
122
+ end
123
+
124
+
125
+ it "should add a quadratic curve command" do
126
+ tag = Rasem::SVGPath.new do
127
+ qcurveTo(10, 10, 5, 5)
128
+ end
129
+
130
+ tag.to_s.should =~ %r{d=\"\s*q\s*5,5\s*10,10\"\/>}
131
+ end
132
+
133
+
134
+ it "should add an absolut quadratic curve command" do
135
+ tag = Rasem::SVGPath.new do
136
+ qcurveToA(10, 10, 5, 5)
137
+ end
138
+
139
+ tag.to_s.should =~ %r{d=\"\s*Q\s*5,5\s*10,10\"\/>}
140
+ end
141
+
142
+
143
+ it "should add a smooth quadratic curve command" do
144
+ tag = Rasem::SVGPath.new do
145
+ sqcurveTo(10, 10)
146
+ end
147
+
148
+ tag.to_s.should =~ %r{d=\"\s*t\s*10,10\"\/>}
149
+ end
150
+
151
+
152
+ it "should add an absolut smooth quadratic curve command" do
153
+ tag = Rasem::SVGPath.new do
154
+ sqcurveToA(10, 10)
155
+ end
156
+
157
+ tag.to_s.should =~ %r{d=\"\s*T\s*10,10\"\/>}
158
+ end
159
+
160
+
161
+ it "should add an arc command" do
162
+ tag = Rasem::SVGPath.new do
163
+ arcTo(10, 10, 1, 2, 3, 4, 5)
164
+ end
165
+
166
+ tag.to_s.should =~ %r{d=\"\s*a\s*1,2\s*3\s*4,5\s*10,10\"\/>}
167
+ end
168
+
169
+
170
+ it "should add an absolut arc command" do
171
+ tag = Rasem::SVGPath.new do
172
+ arcToA(10, 10, 1, 2, 3, 4, 5)
173
+ end
174
+
175
+ tag.to_s.should =~ %r{d=\"\s*A\s*1,2\s*3\s*4,5\s*10,10\"\/>}
176
+ end
177
+
178
+
179
+ it "should add a closepath command" do
180
+ tag = Rasem::SVGPath.new do
181
+ close
182
+ end
183
+
184
+ tag.to_s.should =~ %r{d=\"\s*Z\"\/>}
185
+ end
186
+
187
+
188
+ it "should allow to combine commands" do
189
+ tag = Rasem::SVGPath.new do
190
+ moveTo(0, 0)
191
+ lineTo(0, 0)
192
+ hlineTo(0)
193
+ vlineTo(0)
194
+ curveTo(10, 10, 0, 0, 5, 5)
195
+ scurveTo(10, 10, 5, 5)
196
+ qcurveTo(10, 10, 5, 5)
197
+ sqcurveTo(10, 10)
198
+ arcTo(10, 10, 1, 2, 3, 4, 5)
199
+ close
200
+ end
201
+
202
+ tag.to_s.should =~ %r{d=\"\s*m\s*0,0\s*l\s*0,0\s*h\s*0\s*v\s*0\s*c\s*0,0\s*5,5\s*10,10\s*s\s*5,5\s*10,10\s*q\s*5,5\s*10,10\s*t\s*10,10\s*a\s*1,2\s*3\s*4,5\s*10,10\s*Z\s*\"}
203
+
204
+
205
+ tag = Rasem::SVGPath.new do
206
+ moveToA(0, 0)
207
+ lineToA(0, 0)
208
+ hlineToA(0)
209
+ vlineToA(0)
210
+ curveToA(10, 10, 0, 0, 5, 5)
211
+ scurveToA(10, 10, 5, 5)
212
+ qcurveToA(10, 10, 5, 5)
213
+ sqcurveToA(10, 10)
214
+ arcToA(10, 10, 1, 2, 3, 4, 5)
215
+ close
216
+ end
217
+
218
+ tag.to_s.should =~ %r{d=\"\s*M\s*0,0\s*L\s*0,0\s*H\s*0\s*V\s*0\s*C\s*0,0\s*5,5\s*10,10\s*S\s*5,5\s*10,10\s*Q\s*5,5\s*10,10\s*T\s*10,10\s*A\s*1,2\s*3\s*4,5\s*10,10\s*Z\s*\"}
219
+
220
+ end
221
+
222
+
223
+
224
+
225
+
226
+ end
227
+
@@ -1,50 +1,206 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
- require 'tempfile'
3
2
 
4
- describe Rasem::SVGImage do
5
- it "should initialize an empty image" do
6
- img = Rasem::SVGImage.new(100, 100)
7
- str = img.output
8
- str.should =~ %r{width="100"}
9
- str.should =~ %r{height="100"}
3
+ describe Rasem::SVGTag do
4
+ it "should create an empty tag" do
5
+ tag = Rasem::SVGTag.new("svg")
6
+ str = ""
7
+ tag.write(str)
8
+ str.should =~ %r{<svg/>}
10
9
  end
11
10
 
12
- it "should initialize XML correctly" do
13
- img = Rasem::SVGImage.new(100, 100)
14
- str = img.output
15
- str.should =~ /^<\?xml/
11
+ it "should create a tag with parameters" do
12
+ tag = Rasem::SVGTag.new("svg", :width=>"100%", :height=>"100%")
13
+ str = ""
14
+ tag.write(str)
15
+ str.should =~ %r{<svg width="100%" height="100%"/>}
16
+ end
17
+
18
+ it "should allow editing a parameter in existing tag" do
19
+ tag = Rasem::SVGTag.new("svg", :width=>"100%", :height=>"100%")
20
+ tag.height?.should == "100%"
21
+ tag.height = "15%"
22
+ tag.height?.should == "15%"
23
+ end
24
+
25
+ it "should raise errors if trying to reach invalid parameter" do
26
+ tag = Rasem::SVGTag.new("svg", :width=>"100%", :height=>"100%")
27
+ expect { tag.hight? }.to raise_error
28
+ expect { tag.higgg = "10" }.to raise_error
29
+ end
30
+
31
+ it "should call spawn_child if possible to add that child" do
32
+ tag = Rasem::SVGTag.new("svg", :width=>"100%", :height=>"100%")
33
+ tag.should_receive('spawn_child').with(:g)
34
+ tag.g
35
+ end
36
+
37
+ it "should not call spawn_child if not possible to add that child" do
38
+ tag = Rasem::SVGTag.new("svg", :width=>"100%", :height=>"100%")
39
+ tag.should_not_receive('spawn_child')
40
+ expect { tag.ggr }.to raise_error
41
+ end
42
+
43
+ it "should create a child" do
44
+ tag = Rasem::SVGTag.new("svg")
45
+ tag.g
46
+ str = ""
47
+ tag.write(str)
48
+ str.should =~ %r{<svg><g/></svg>}
49
+ end
50
+
51
+ it "should use alias for some children" do
52
+ tag = Rasem::SVGTag.new("svg")
53
+ tag.group
54
+ str = ""
55
+ tag.write(str)
56
+ str.should =~ %r{<svg><g/></svg>}
57
+ end
58
+
59
+ it "should pass parameters to child" do
60
+ tag = Rasem::SVGTag.new("svg")
61
+ tag.group :id=>"G1", :class=>"C1"
62
+ str = ""
63
+ tag.write(str)
64
+ str.should =~ %r{<svg><g id="G1" class="C1"/></svg>}
65
+ end
66
+
67
+ it "should raise an error when passing wrong parameter" do
68
+ tag = Rasem::SVGTag.new("svg")
69
+ expect { tag.group :height=>"100%" }.to raise_error
70
+ end
71
+
72
+ it "should allow passing non named parameters" do
73
+ tag = Rasem::SVGTag.new("svg")
74
+ tag.line 0, 0, 100, 100
75
+ str = ""
76
+ tag.write(str)
77
+ str.should =~ %r{<svg><line x1="0" y1="0" x2="100" y2="100".*/></svg>}
78
+ end
79
+
80
+ it "should raise error when passing not enough non named parameters" do
81
+ tag = Rasem::SVGTag.new("svg")
82
+ expect { tag.line 0, 0, 100 }.to raise_error
83
+ end
84
+
85
+ it "should raise error when passing ill formed non named parameters (even if count matches)" do
86
+ tag = Rasem::SVGTag.new("svg")
87
+ expect { tag.line 0, 0, 100, :x2=>100 }.to raise_error
88
+ end
89
+
90
+ it "should raise error when passing too much non named parameters" do
91
+ tag = Rasem::SVGTag.new("svg")
92
+ expect { tag.line 0, 0, 100, 10, 10 }.to raise_error
93
+ end
94
+
95
+ it "should allow following non named parameters with named ones" do
96
+ tag = Rasem::SVGTag.new("svg")
97
+ tag.line 0, 0, 100, 100, :id=>"Line1"
98
+ str = ""
99
+ tag.write(str)
100
+ str.should =~ %r{line}
101
+ str.should =~ %r{x1="0"}
102
+ str.should =~ %r{y1="0"}
103
+ str.should =~ %r{x2="100"}
104
+ str.should =~ %r{y2="100"}
105
+ str.should =~ %r{id="Line1"}
106
+ end
107
+
108
+ it "should use named parameters when there is a conflict" do
109
+ tag = Rasem::SVGTag.new("svg")
110
+ tag.line 0, 0, 100, 100, :x1=>"10"
111
+ str = ""
112
+ tag.write(str)
113
+ str.should =~ %r{line}
114
+ str.should =~ %r{x1="10"}
115
+ str.should_not =~ %r{x1="0"}
116
+ end
117
+
118
+ it "should insert a child from block within" do
119
+ tag = Rasem::SVGTag.new("svg",:height=>"100%") do
120
+ g :id=>"G1" do
121
+ end
122
+ end
123
+ str = ""
124
+ tag.write(str)
125
+ str.should =~ %r{<svg height="100%"><g id="G1"/></svg>}
16
126
  end
17
127
 
18
- it "should close an image" do
19
- img = Rasem::SVGImage.new(100, 100)
20
- img.close
21
- str = img.output
22
- str.should =~ %r{</svg>}
128
+ it "should nest easily" do
129
+ tag = Rasem::SVGTag.new("svg") do
130
+ g do
131
+ g do
132
+ g do
133
+ g do
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
139
+ str = ""
140
+ tag.write(str)
141
+ str.should =~ %r{<svg><g><g><g><g/></g></g></g></svg>}
142
+ end
143
+
144
+ it "should add style element if parameters contain style tag" do
145
+ tag = Rasem::SVGTag.new("svg") do
146
+ line 0, 0, 10, 10, :stroke=>"violet"
147
+ end
148
+ str = ""
149
+ tag.write(str)
150
+ str.should =~ %r{style="stroke:violet;"}
23
151
  end
24
152
 
25
- it "should auto close an image with block" do
26
- img = Rasem::SVGImage.new(100, 100) do
153
+ it "should add transform element if parameters contain style tag" do
154
+ tag = Rasem::SVGTag.new("svg") do
155
+ line 0, 0, 10, 10, :scale=>2
27
156
  end
28
- img.should be_closed
157
+ str = ""
158
+ tag.write(str)
159
+ str.should =~ /transform=".*scale\(2\).*"/
29
160
  end
30
-
161
+ end
162
+
163
+ describe Rasem::SVGImage do
164
+ it "should initialize an empty image" do
165
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100")
166
+ str = ""
167
+ img.write(str)
168
+ str.should =~ /<svg.*width="100".*height="100"/
169
+ end
170
+
171
+ it "should initialize an empty image in given output" do
172
+ str = ""
173
+ img = Rasem::SVGImage.new({:width=>"100", :height=>"100"}, str) do
174
+ end
175
+ str.should =~ /<svg.*width="100".*height="100"/
176
+ end
177
+
178
+ it "should initialize XML correctly" do
179
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100")
180
+ str = ""
181
+ img.write(str)
182
+ str.should =~ /^<\?xml/
183
+ end
184
+
31
185
  it "should draw line using method" do
32
- img = Rasem::SVGImage.new(100, 100)
186
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100")
33
187
  img.line(0, 0, 100, 100)
34
- img.close
35
- str = img.output
188
+ str = ""
189
+ img.write(str)
36
190
  str.should =~ %r{<line}
37
191
  str.should =~ %r{x1="0"}
38
192
  str.should =~ %r{y1="0"}
39
193
  str.should =~ %r{x2="100"}
40
194
  str.should =~ %r{y2="100"}
41
195
  end
42
-
196
+
43
197
  it "should draw line using a block" do
44
- img = Rasem::SVGImage.new(100, 100) do
198
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
45
199
  line(0, 0, 100, 100)
46
200
  end
47
- str = img.output
201
+ str = ""
202
+ img.write(str)
203
+
48
204
  str.should =~ %r{<line}
49
205
  str.should =~ %r{x1="0"}
50
206
  str.should =~ %r{y1="0"}
@@ -53,19 +209,23 @@ describe Rasem::SVGImage do
53
209
  end
54
210
 
55
211
  it "should draw a line with style" do
56
- img = Rasem::SVGImage.new(100, 100) do
212
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
57
213
  line(0, 0, 10, 10, :fill=>"white")
58
214
  end
59
- str = img.output
215
+ str = ""
216
+ img.write(str)
217
+
60
218
  str.should =~ %r{style=}
61
219
  str.should =~ %r{fill:white}
62
220
  end
63
221
 
64
222
  it "should draw a circle" do
65
- img = Rasem::SVGImage.new(100, 100) do
223
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
66
224
  circle(0, 0, 10)
67
225
  end
68
- str = img.output
226
+ str = ""
227
+ img.write(str)
228
+
69
229
  str.should =~ %r{<circle}
70
230
  str.should =~ %r{cx="0"}
71
231
  str.should =~ %r{cy="0"}
@@ -73,38 +233,46 @@ describe Rasem::SVGImage do
73
233
  end
74
234
 
75
235
  it "should draw a circle with style" do
76
- img = Rasem::SVGImage.new(100, 100) do
236
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
77
237
  circle(0, 0, 10, :fill=>"white")
78
238
  end
79
- str = img.output
239
+ str = ""
240
+ img.write(str)
241
+
80
242
  str.should =~ %r{style=}
81
243
  str.should =~ %r{fill:white}
82
244
  end
83
245
 
84
246
  it "should draw a rectangle" do
85
- img = Rasem::SVGImage.new(100, 100) do
247
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
86
248
  rectangle(0, 0, 100, 300)
87
249
  end
88
- str = img.output
250
+ str = ""
251
+ img.write(str)
252
+
89
253
  str.should =~ %r{<rect}
90
254
  str.should =~ %r{width="100"}
91
255
  str.should =~ %r{height="300"}
92
256
  end
93
257
 
94
258
  it "should draw a rectangle with style" do
95
- img = Rasem::SVGImage.new(100, 100) do
259
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
96
260
  rectangle(0, 0, 10, 10, :fill=>"white")
97
261
  end
98
- str = img.output
262
+ str = ""
263
+ img.write(str)
264
+
99
265
  str.should =~ %r{style=}
100
266
  str.should =~ %r{fill:white}
101
267
  end
102
268
 
103
269
  it "should draw a symmetric round-rectangle" do
104
- img = Rasem::SVGImage.new(100, 100) do
270
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
105
271
  rectangle(0, 0, 100, 300, 20)
106
272
  end
107
- str = img.output
273
+ str = ""
274
+ img.write(str)
275
+
108
276
  str.should =~ %r{<rect}
109
277
  str.should =~ %r{width="100"}
110
278
  str.should =~ %r{height="300"}
@@ -113,19 +281,23 @@ describe Rasem::SVGImage do
113
281
  end
114
282
 
115
283
  it "should draw a symmetric rounded-rectangle with style" do
116
- img = Rasem::SVGImage.new(100, 100) do
284
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
117
285
  rectangle(0, 0, 10, 10, 2, :fill=>"white")
118
286
  end
119
- str = img.output
287
+ str = ""
288
+ img.write(str)
289
+
120
290
  str.should =~ %r{style=}
121
291
  str.should =~ %r{fill:white}
122
292
  end
123
293
 
124
294
  it "should draw a non-symmetric round-rectangle" do
125
- img = Rasem::SVGImage.new(100, 100) do
295
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
126
296
  rectangle(0, 0, 100, 300, 20, 5)
127
297
  end
128
- str = img.output
298
+ str = ""
299
+ img.write(str)
300
+
129
301
  str.should =~ %r{<rect}
130
302
  str.should =~ %r{width="100"}
131
303
  str.should =~ %r{height="300"}
@@ -134,19 +306,23 @@ describe Rasem::SVGImage do
134
306
  end
135
307
 
136
308
  it "should draw a non-symmetric rounded-rectangle with style" do
137
- img = Rasem::SVGImage.new(100, 100) do
309
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
138
310
  rectangle(0, 0, 10, 10, 2, 4, :fill=>"white")
139
311
  end
140
- str = img.output
312
+ str = ""
313
+ img.write(str)
314
+
141
315
  str.should =~ %r{style=}
142
316
  str.should =~ %r{fill:white}
143
317
  end
144
-
318
+
145
319
  it "should draw an ellipse" do
146
- img = Rasem::SVGImage.new(100, 100) do
320
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
147
321
  ellipse(0, 0, 100, 300)
148
322
  end
149
- str = img.output
323
+ str = ""
324
+ img.write(str)
325
+
150
326
  str.should =~ %r{<ellipse}
151
327
  str.should =~ %r{cx="0"}
152
328
  str.should =~ %r{cy="0"}
@@ -155,178 +331,210 @@ describe Rasem::SVGImage do
155
331
  end
156
332
 
157
333
  it "should draw an ellipse with style" do
158
- img = Rasem::SVGImage.new(100, 100) do
334
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
159
335
  ellipse(0, 0, 3, 10, :fill=>"white")
160
336
  end
161
- str = img.output
337
+ str = ""
338
+ img.write(str)
339
+
162
340
  str.should =~ %r{style=}
163
341
  str.should =~ %r{fill:white}
164
342
  end
165
343
 
166
344
  it "should draw a polygon given an array of points" do
167
- img = Rasem::SVGImage.new(100, 100) do
345
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
168
346
  polygon([[0,0], [1,2], [3,4]])
169
347
  end
170
- str = img.output
348
+ str = ""
349
+ img.write(str)
350
+
171
351
  str.should =~ %r{<polygon}
172
352
  str.should =~ %r{points="0,0 1,2 3,4"}
173
353
  end
174
354
 
175
355
  it "should draw a polygon with style" do
176
- img = Rasem::SVGImage.new(100, 100) do
356
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
177
357
  polygon([[0,0], [1,2], [3,4]], :fill=>"white")
178
358
  end
179
- str = img.output
359
+ str = ""
360
+ img.write(str)
361
+
180
362
  str.should =~ %r{style=}
181
363
  str.should =~ %r{fill:white}
182
364
  end
183
365
 
184
366
  it "should draw a polyline given an array of points" do
185
- img = Rasem::SVGImage.new(100, 100) do
367
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
186
368
  polyline([[0,0], [1,2], [3,4]])
187
369
  end
188
- str = img.output
370
+ str = ""
371
+ img.write(str)
372
+
189
373
  str.should =~ %r{<polyline}
190
374
  str.should =~ %r{points="0,0 1,2 3,4"}
191
375
  end
192
376
 
193
377
  it "should fix style names" do
194
- img = Rasem::SVGImage.new(100, 100) do
378
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
195
379
  circle(0, 0, 10, :stroke_width=>3)
196
380
  end
197
- str = img.output
381
+ str = ""
382
+ img.write(str)
383
+
198
384
  str.should =~ %r{style=}
199
385
  str.should =~ %r{stroke-width:3}
200
386
  end
201
-
387
+
202
388
  it "should group styles" do
203
- img = Rasem::SVGImage.new(100, 100) do
389
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
204
390
  with_style :stroke_width=>3 do
205
391
  circle(0, 0, 10)
206
392
  end
207
393
  end
208
- str = img.output
394
+ str = ""
395
+ img.write(str)
396
+
209
397
  str.should =~ %r{style=}
210
398
  str.should =~ %r{stroke-width:3}
211
399
  end
212
400
 
213
401
  it "should group styles nesting" do
214
- img = Rasem::SVGImage.new(100, 100) do
402
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
215
403
  with_style :stroke_width=>3 do
216
404
  with_style :fill=>"black" do
217
405
  circle(0, 0, 10)
218
406
  end
219
407
  end
220
408
  end
221
- str = img.output
409
+ str = ""
410
+ img.write(str)
411
+
222
412
  str.should =~ %r{style=}
223
413
  str.should =~ %r{stroke-width:3}
224
414
  str.should =~ %r{fill:black}
225
415
  end
226
416
 
227
417
  it "should group styles override nesting" do
228
- img = Rasem::SVGImage.new(100, 100) do
418
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
229
419
  with_style :stroke_width=>3 do
230
420
  with_style :stroke_width=>5 do
231
421
  circle(0, 0, 10)
232
422
  end
233
423
  end
234
424
  end
235
- str = img.output
425
+ str = ""
426
+ img.write(str)
427
+
236
428
  str.should =~ %r{style=}
237
429
  str.should =~ %r{stroke-width:5}
238
430
  end
239
431
 
240
432
  it "should group styles limited effect" do
241
- img = Rasem::SVGImage.new(100, 100) do
433
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
242
434
  with_style :stroke_width=>3 do
243
435
  with_style :stroke_width=>5 do
244
436
  end
245
437
  end
246
438
  circle(0, 0, 10)
247
439
  end
248
- str = img.output
440
+ str = ""
441
+ img.write(str)
442
+
249
443
  str.should_not =~ %r{stroke-width:3}
250
444
  str.should_not =~ %r{stroke-width:5}
251
445
  end
252
-
446
+
253
447
  it "should create a group" do
254
- img = Rasem::SVGImage.new(100, 100) do
448
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
255
449
  group :stroke_width=>3 do
256
450
  circle(0, 0, 10)
257
451
  circle(20, 20, 10)
258
452
  end
259
453
  end
260
- str = img.output
454
+ str = ""
455
+ img.write(str)
456
+
261
457
  str.should =~ %r{<g .*circle.*circle.*</g>}
262
458
  end
263
-
459
+
460
+ it "should apply transforms to a group" do
461
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
462
+ group(:scale => 5, :translate => [15,20]) do
463
+ circle(0, 0, 10)
464
+ end
465
+ end
466
+ str = ""
467
+ img.write(str)
468
+
469
+ str.should =~ %r{scale\(5\)}
470
+ str.should =~ %r{translate\(15,20\)}
471
+ end
472
+
264
473
  it "should update width and height after init" do
265
- img = Rasem::SVGImage.new(100, 100) do
266
- set_width 200
267
- set_height 300
474
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
475
+ self.width = 200
476
+ self.height = 300
268
477
  end
269
- str = img.output
478
+ str = ""
479
+ img.write(str)
480
+
270
481
  str.should =~ %r{width="200"}
271
482
  str.should =~ %r{height="300"}
272
483
  end
273
484
 
274
485
  it "should draw text" do
275
- img = Rasem::SVGImage.new(100, 100) do
276
- text 10, 20, "Hello world!"
486
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
487
+ text 10, 20 do
488
+ raw "Hello world!"
489
+ end
277
490
  end
278
- str = img.output
491
+ str = ""
492
+ img.write(str)
493
+
279
494
  str.should =~ %r{<text}
280
495
  str.should =~ %r{x="10"}
281
496
  str.should =~ %r{y="20"}
282
497
  str.should =~ %r{Hello world!}
283
498
  end
284
499
 
285
- it "should draw multiline text" do
286
- img = Rasem::SVGImage.new(100, 100) do
287
- text 10, 20, "Hello\nworld!"
288
- end
289
- str = img.output
290
- str.should =~ %r{<text.*tspan.*tspan.*</text}
291
- end
292
-
500
+ # it "should draw multiline text" do
501
+ # img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
502
+ # text 10, 20 do
503
+ # raw "Hello\nworld!"
504
+ # end
505
+ # end
506
+ # str = ""
507
+ # img.write(str)
508
+ #
509
+ # str.should =~ %r{<text.*tspan.*tspan.*</text}
510
+ # end
511
+
293
512
  it "should draw text with font" do
294
- img = Rasem::SVGImage.new(100, 100) do
295
- text 10, 20, "Hello\nworld!", :font_family=>"Times", "font-size"=>24
513
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
514
+ text 10, 20, "font-family"=>"Times", "font-size"=>24 do
515
+ raw "Hello World"
516
+ end
296
517
  end
297
- str = img.output
518
+ str = ""
519
+ img.write(str)
520
+
298
521
  str.should =~ %r{font-family="Times"}
299
522
  str.should =~ %r{font-size="24"}
300
523
  end
301
524
 
302
- it "should generate an image out of a .rasem file" do
303
- rasem_file = Tempfile.new("temp.rasem")
304
- rasem_file.puts "circle 50, 50, 50"
305
- rasem_file.close
306
-
307
- File.should_not be_exists(rasem_file.path+".svg")
308
- Rasem::Application.run!(rasem_file.path)
309
-
310
- File.should be_exists(rasem_file.path+".svg")
311
- end
312
-
313
- it "should raise an exception for a malformed .rasem file" do
314
- rasem_file = Tempfile.new("temp.rasem")
315
- rasem_file.puts "@x.asdf"
316
- rasem_file.close
317
-
318
- lambda { Rasem::Application.run!(rasem_file.path) }.should raise_error
319
- end
320
-
321
- it "should generate only the portion of backtrace in .rasem file" do
322
- rasem_file = Tempfile.new("temp.rasem")
323
- rasem_file.puts "@x.asdf"
324
- rasem_file.close
325
-
326
- begin
327
- Rasem::Application.run!(rasem_file.path)
328
- rescue Exception => e
329
- e.backtrace.should have(1).lines
525
+ it "should include an image" do
526
+ img = Rasem::SVGImage.new(:width=>"100", :height=>"100") do
527
+ image 10, 20, 30, 40, 'image.png'
330
528
  end
529
+ str = ""
530
+ img.write(str)
531
+
532
+ str.should =~ %r{<image}
533
+ str.should =~ %r{x="10"}
534
+ str.should =~ %r{y="20"}
535
+ str.should =~ %r{width="30"}
536
+ str.should =~ %r{height="40"}
537
+ str.should =~ %r{xlink:href="image.png"}
331
538
  end
539
+
332
540
  end