rasem 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -6,6 +6,9 @@ class Rasem::SVGImage
6
6
  else
7
7
  @output = create_output(nil)
8
8
  end
9
+
10
+ # Initialize a stack of default styles
11
+ @default_styles = []
9
12
 
10
13
  write_header(*args)
11
14
  if block
@@ -76,6 +79,17 @@ class Rasem::SVGImage
76
79
  @closed
77
80
  end
78
81
 
82
+ def with_style(style={}, &proc)
83
+ # Merge passed style with current default style
84
+ updated_style = default_style.update(style)
85
+ # Push updated style to the stack
86
+ @default_styles.push(updated_style)
87
+ # Call the block
88
+ self.instance_exec(&proc)
89
+ # Pop style again to revert changes
90
+ @default_styles.pop
91
+ end
92
+
79
93
  private
80
94
  # Creates an object for ouput out of an argument
81
95
  def create_output(arg)
@@ -121,12 +135,25 @@ private
121
135
  @output << '"/>'
122
136
  end
123
137
 
138
+ # Return current deafult style
139
+ def default_style
140
+ @default_styles.last || {}
141
+ end
142
+
124
143
  # Writes styles to current output
144
+ # Avaialable styles are:
145
+ # fill: Fill color
146
+ # stroke-width: stroke width
147
+ # stroke: stroke color
148
+ # fill-opacity: fill opacity. ranges from 0 to 1
149
+ # stroke-opacity: stroke opacity. ranges from 0 to 1
150
+ # opacity: Opacity for the whole element
125
151
  def write_style(style)
126
- return if style.nil? || style.empty?
152
+ style_ = default_style.merge(style)
153
+ return if style_.empty?
127
154
  @output << ' style="'
128
- style.each_pair do |style, value|
129
- @output << "#{style}:#{value};"
155
+ style_.each_pair do |style, value|
156
+ @output << "#{style.to_s.gsub('_','-')}:#{value};"
130
157
  end
131
158
  @output << '"'
132
159
  end
@@ -189,4 +189,75 @@ describe Rasem::SVGImage do
189
189
  str.should =~ %r{points="0,0 1,2 3,4"}
190
190
  end
191
191
 
192
+ it "should fix style names" do
193
+ img = Rasem::SVGImage.new("", 100, 100) do
194
+ circle(0, 0, 10, :stroke_width=>3)
195
+ end
196
+ str = img.output
197
+ str.should =~ %r{style=}
198
+ str.should =~ %r{stroke-width:3}
199
+ end
200
+
201
+ it "should group styles" do
202
+ img = Rasem::SVGImage.new("", 100, 100) do
203
+ with_style :stroke_width=>3 do
204
+ circle(0, 0, 10)
205
+ end
206
+ end
207
+ str = img.output
208
+ str.should =~ %r{style=}
209
+ str.should =~ %r{stroke-width:3}
210
+ end
211
+
212
+ it "should group styles nesting" do
213
+ img = Rasem::SVGImage.new("", 100, 100) do
214
+ with_style :stroke_width=>3 do
215
+ with_style :fill=>"black" do
216
+ circle(0, 0, 10)
217
+ end
218
+ end
219
+ end
220
+ str = img.output
221
+ str.should =~ %r{style=}
222
+ str.should =~ %r{stroke-width:3}
223
+ str.should =~ %r{fill:black}
224
+ end
225
+
226
+ it "should group styles override nesting" do
227
+ img = Rasem::SVGImage.new("", 100, 100) do
228
+ with_style :stroke_width=>3 do
229
+ with_style :stroke_width=>5 do
230
+ circle(0, 0, 10)
231
+ end
232
+ end
233
+ end
234
+ str = img.output
235
+ str.should =~ %r{style=}
236
+ str.should =~ %r{stroke-width:5}
237
+ end
238
+
239
+ it "should group styles limited effect" do
240
+ img = Rasem::SVGImage.new("", 100, 100) do
241
+ with_style :stroke_width=>3 do
242
+ with_style :stroke_width=>5 do
243
+ end
244
+ end
245
+ circle(0, 0, 10)
246
+ end
247
+ str = img.output
248
+ str.should_not =~ %r{style=}
249
+ str.should_not =~ %r{stroke-width:}
250
+ end
251
+
252
+ it "should create a group" do
253
+ img = Rasem::SVGImage.new("", 100, 100) do
254
+ group :stroke_width=>3 do
255
+ circle(0, 0, 10)
256
+ circle(20, 20, 10)
257
+ end
258
+ end
259
+ str = img.output
260
+ str.should =~ %r{<group.*circle.*circle.*</group>}
261
+ end
262
+
192
263
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ahmed Eldawy
@@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
110
  requirements:
111
111
  - - ">="
112
112
  - !ruby/object:Gem::Version
113
- hash: 3982115988403201538
113
+ hash: -2737296889092417028
114
114
  segments:
115
115
  - 0
116
116
  version: "0"