rasem 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.5.2
@@ -18,10 +18,11 @@ class Rasem::Application
18
18
  else
19
19
  svg_file = source_file + ".svg"
20
20
  end
21
+ img = Rasem::SVGImage.new(nil, "100%", "100%") do
22
+ eval(File.read(source_file), binding)
23
+ end
21
24
  File.open(svg_file, "w") do |f|
22
- Rasem::SVGImage.new(f, 100, 100) do
23
- eval(File.read(source_file), binding)
24
- end
25
+ f << img.output
25
26
  end
26
27
  end
27
28
 
@@ -1,21 +1,34 @@
1
1
  class Rasem::SVGImage
2
+ DefaultStyle = {:stroke=>"black", :fill=>"black"}
2
3
 
3
- def initialize(*args, &block)
4
- if args.length == 3
5
- @output = create_output(args.shift)
6
- else
7
- @output = create_output(nil)
8
- end
4
+ def initialize(width, height, output=nil, &block)
5
+ @output = create_output(output)
9
6
 
10
7
  # Initialize a stack of default styles
11
- @default_styles = []
8
+ @default_styles = [DefaultStyle]
12
9
 
13
- write_header(*args)
10
+ write_header(width, height)
14
11
  if block
15
12
  self.instance_exec(&block)
16
13
  self.close
17
14
  end
18
15
  end
16
+
17
+ def set_width(new_width)
18
+ if @output.respond_to?(:sub!)
19
+ @output.sub!(/<svg width="[^"]+"/, %Q{<svg width="#{new_width}"})
20
+ else
21
+ raise "Cannot change width after initialization for this output"
22
+ end
23
+ end
24
+
25
+ def set_height(new_height)
26
+ if @output.respond_to?(:sub!)
27
+ @output.sub!(/<svg width="([^"]+)" height="[^"]+"/, %Q{<svg width="\\1" height="#{new_height}"})
28
+ else
29
+ raise "Cannot change width after initialization for this output"
30
+ end
31
+ end
19
32
 
20
33
  # Draw a straight line between the two end points
21
34
  def line(x1, y1, x2, y2, style={})
@@ -81,7 +94,7 @@ class Rasem::SVGImage
81
94
 
82
95
  def with_style(style={}, &proc)
83
96
  # Merge passed style with current default style
84
- updated_style = default_style.update(style)
97
+ updated_style = default_style.merge(style)
85
98
  # Push updated style to the stack
86
99
  @default_styles.push(updated_style)
87
100
  # Call the block
@@ -142,8 +155,8 @@ private
142
155
  @output << "#{x},#{y}"
143
156
  @output << " " unless coords.empty?
144
157
  end
145
- write_style(style)
146
158
  @output << '"/>'
159
+ write_style(style)
147
160
  end
148
161
 
149
162
  # Return current deafult style
data/spec/rasem_spec.rb CHANGED
@@ -2,33 +2,33 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe Rasem::SVGImage do
4
4
  it "should initialize an empty image" do
5
- img = Rasem::SVGImage.new("", 100, 100)
5
+ img = Rasem::SVGImage.new(100, 100)
6
6
  str = img.output
7
7
  str.should =~ %r{width="100"}
8
8
  str.should =~ %r{height="100"}
9
9
  end
10
10
 
11
11
  it "should initialize XML correctly" do
12
- img = Rasem::SVGImage.new("", 100, 100)
12
+ img = Rasem::SVGImage.new(100, 100)
13
13
  str = img.output
14
14
  str.should =~ /^<\?xml/
15
15
  end
16
16
 
17
17
  it "should close an image" do
18
- img = Rasem::SVGImage.new("", 100, 100)
18
+ img = Rasem::SVGImage.new(100, 100)
19
19
  img.close
20
20
  str = img.output
21
21
  str.should =~ %r{</svg>}
22
22
  end
23
23
 
24
24
  it "should auto close an image with block" do
25
- img = Rasem::SVGImage.new("", 100, 100) do
25
+ img = Rasem::SVGImage.new(100, 100) do
26
26
  end
27
27
  img.should be_closed
28
28
  end
29
29
 
30
30
  it "should draw line using method" do
31
- img = Rasem::SVGImage.new("", 100, 100)
31
+ img = Rasem::SVGImage.new(100, 100)
32
32
  img.line(0, 0, 100, 100)
33
33
  img.close
34
34
  str = img.output
@@ -40,7 +40,7 @@ describe Rasem::SVGImage do
40
40
  end
41
41
 
42
42
  it "should draw line using a block" do
43
- img = Rasem::SVGImage.new("", 100, 100) do
43
+ img = Rasem::SVGImage.new(100, 100) do
44
44
  line(0, 0, 100, 100)
45
45
  end
46
46
  str = img.output
@@ -52,7 +52,7 @@ describe Rasem::SVGImage do
52
52
  end
53
53
 
54
54
  it "should draw a line with style" do
55
- img = Rasem::SVGImage.new("", 100, 100) do
55
+ img = Rasem::SVGImage.new(100, 100) do
56
56
  line(0, 0, 10, 10, :fill=>"white")
57
57
  end
58
58
  str = img.output
@@ -61,7 +61,7 @@ describe Rasem::SVGImage do
61
61
  end
62
62
 
63
63
  it "should draw a circle" do
64
- img = Rasem::SVGImage.new("", 100, 100) do
64
+ img = Rasem::SVGImage.new(100, 100) do
65
65
  circle(0, 0, 10)
66
66
  end
67
67
  str = img.output
@@ -72,7 +72,7 @@ describe Rasem::SVGImage do
72
72
  end
73
73
 
74
74
  it "should draw a circle with style" do
75
- img = Rasem::SVGImage.new("", 100, 100) do
75
+ img = Rasem::SVGImage.new(100, 100) do
76
76
  circle(0, 0, 10, :fill=>"white")
77
77
  end
78
78
  str = img.output
@@ -81,7 +81,7 @@ describe Rasem::SVGImage do
81
81
  end
82
82
 
83
83
  it "should draw a rectangle" do
84
- img = Rasem::SVGImage.new("", 100, 100) do
84
+ img = Rasem::SVGImage.new(100, 100) do
85
85
  rectangle(0, 0, 100, 300)
86
86
  end
87
87
  str = img.output
@@ -91,7 +91,7 @@ describe Rasem::SVGImage do
91
91
  end
92
92
 
93
93
  it "should draw a rectangle with style" do
94
- img = Rasem::SVGImage.new("", 100, 100) do
94
+ img = Rasem::SVGImage.new(100, 100) do
95
95
  rectangle(0, 0, 10, 10, :fill=>"white")
96
96
  end
97
97
  str = img.output
@@ -100,7 +100,7 @@ describe Rasem::SVGImage do
100
100
  end
101
101
 
102
102
  it "should draw a symmetric round-rectangle" do
103
- img = Rasem::SVGImage.new("", 100, 100) do
103
+ img = Rasem::SVGImage.new(100, 100) do
104
104
  rectangle(0, 0, 100, 300, 20)
105
105
  end
106
106
  str = img.output
@@ -112,7 +112,7 @@ describe Rasem::SVGImage do
112
112
  end
113
113
 
114
114
  it "should draw a symmetric rounded-rectangle with style" do
115
- img = Rasem::SVGImage.new("", 100, 100) do
115
+ img = Rasem::SVGImage.new(100, 100) do
116
116
  rectangle(0, 0, 10, 10, 2, :fill=>"white")
117
117
  end
118
118
  str = img.output
@@ -121,7 +121,7 @@ describe Rasem::SVGImage do
121
121
  end
122
122
 
123
123
  it "should draw a non-symmetric round-rectangle" do
124
- img = Rasem::SVGImage.new("", 100, 100) do
124
+ img = Rasem::SVGImage.new(100, 100) do
125
125
  rectangle(0, 0, 100, 300, 20, 5)
126
126
  end
127
127
  str = img.output
@@ -133,7 +133,7 @@ describe Rasem::SVGImage do
133
133
  end
134
134
 
135
135
  it "should draw a non-symmetric rounded-rectangle with style" do
136
- img = Rasem::SVGImage.new("", 100, 100) do
136
+ img = Rasem::SVGImage.new(100, 100) do
137
137
  rectangle(0, 0, 10, 10, 2, 4, :fill=>"white")
138
138
  end
139
139
  str = img.output
@@ -142,7 +142,7 @@ describe Rasem::SVGImage do
142
142
  end
143
143
 
144
144
  it "should draw an ellipse" do
145
- img = Rasem::SVGImage.new("", 100, 100) do
145
+ img = Rasem::SVGImage.new(100, 100) do
146
146
  ellipse(0, 0, 100, 300)
147
147
  end
148
148
  str = img.output
@@ -154,7 +154,7 @@ describe Rasem::SVGImage do
154
154
  end
155
155
 
156
156
  it "should draw an ellipse with style" do
157
- img = Rasem::SVGImage.new("", 100, 100) do
157
+ img = Rasem::SVGImage.new(100, 100) do
158
158
  ellipse(0, 0, 3, 10, :fill=>"white")
159
159
  end
160
160
  str = img.output
@@ -163,7 +163,7 @@ describe Rasem::SVGImage do
163
163
  end
164
164
 
165
165
  it "should draw a polygon given an array of points" do
166
- img = Rasem::SVGImage.new("", 100, 100) do
166
+ img = Rasem::SVGImage.new(100, 100) do
167
167
  polygon([[0,0], [1,2], [3,4]])
168
168
  end
169
169
  str = img.output
@@ -172,7 +172,7 @@ describe Rasem::SVGImage do
172
172
  end
173
173
 
174
174
  it "should draw a polygon with style" do
175
- img = Rasem::SVGImage.new("", 100, 100) do
175
+ img = Rasem::SVGImage.new(100, 100) do
176
176
  polygon([[0,0], [1,2], [3,4]], :fill=>"white")
177
177
  end
178
178
  str = img.output
@@ -181,7 +181,7 @@ describe Rasem::SVGImage do
181
181
  end
182
182
 
183
183
  it "should draw a polyline given an array of points" do
184
- img = Rasem::SVGImage.new("", 100, 100) do
184
+ img = Rasem::SVGImage.new(100, 100) do
185
185
  polyline([[0,0], [1,2], [3,4]])
186
186
  end
187
187
  str = img.output
@@ -190,7 +190,7 @@ describe Rasem::SVGImage do
190
190
  end
191
191
 
192
192
  it "should fix style names" do
193
- img = Rasem::SVGImage.new("", 100, 100) do
193
+ img = Rasem::SVGImage.new(100, 100) do
194
194
  circle(0, 0, 10, :stroke_width=>3)
195
195
  end
196
196
  str = img.output
@@ -199,7 +199,7 @@ describe Rasem::SVGImage do
199
199
  end
200
200
 
201
201
  it "should group styles" do
202
- img = Rasem::SVGImage.new("", 100, 100) do
202
+ img = Rasem::SVGImage.new(100, 100) do
203
203
  with_style :stroke_width=>3 do
204
204
  circle(0, 0, 10)
205
205
  end
@@ -210,7 +210,7 @@ describe Rasem::SVGImage do
210
210
  end
211
211
 
212
212
  it "should group styles nesting" do
213
- img = Rasem::SVGImage.new("", 100, 100) do
213
+ img = Rasem::SVGImage.new(100, 100) do
214
214
  with_style :stroke_width=>3 do
215
215
  with_style :fill=>"black" do
216
216
  circle(0, 0, 10)
@@ -224,7 +224,7 @@ describe Rasem::SVGImage do
224
224
  end
225
225
 
226
226
  it "should group styles override nesting" do
227
- img = Rasem::SVGImage.new("", 100, 100) do
227
+ img = Rasem::SVGImage.new(100, 100) do
228
228
  with_style :stroke_width=>3 do
229
229
  with_style :stroke_width=>5 do
230
230
  circle(0, 0, 10)
@@ -237,7 +237,7 @@ describe Rasem::SVGImage do
237
237
  end
238
238
 
239
239
  it "should group styles limited effect" do
240
- img = Rasem::SVGImage.new("", 100, 100) do
240
+ img = Rasem::SVGImage.new(100, 100) do
241
241
  with_style :stroke_width=>3 do
242
242
  with_style :stroke_width=>5 do
243
243
  end
@@ -245,12 +245,12 @@ describe Rasem::SVGImage do
245
245
  circle(0, 0, 10)
246
246
  end
247
247
  str = img.output
248
- str.should_not =~ %r{style=}
249
- str.should_not =~ %r{stroke-width:}
248
+ str.should_not =~ %r{stroke-width:3}
249
+ str.should_not =~ %r{stroke-width:5}
250
250
  end
251
251
 
252
252
  it "should create a group" do
253
- img = Rasem::SVGImage.new("", 100, 100) do
253
+ img = Rasem::SVGImage.new(100, 100) do
254
254
  group :stroke_width=>3 do
255
255
  circle(0, 0, 10)
256
256
  circle(20, 20, 10)
@@ -259,5 +259,15 @@ describe Rasem::SVGImage do
259
259
  str = img.output
260
260
  str.should =~ %r{<g .*circle.*circle.*</g>}
261
261
  end
262
+
263
+ it "should update width and height after init" do
264
+ img = Rasem::SVGImage.new(100, 100) do
265
+ set_width 200
266
+ set_height 300
267
+ end
268
+ str = img.output
269
+ str.should =~ %r{width="200"}
270
+ str.should =~ %r{height="300"}
271
+ end
262
272
 
263
273
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 5
8
- - 1
9
- version: 0.5.1
8
+ - 2
9
+ version: 0.5.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ahmed Eldawy
@@ -112,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
112
  requirements:
113
113
  - - ">="
114
114
  - !ruby/object:Gem::Version
115
- hash: -2955251890705000744
115
+ hash: -1380337936267073737
116
116
  segments:
117
117
  - 0
118
118
  version: "0"