rasem 0.1.1 → 0.2.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.1.1
1
+ 0.2.0
@@ -15,25 +15,43 @@ class Rasem::SVGImage
15
15
  end
16
16
 
17
17
  # Draw a straight line between the two end points
18
- def line(x1, y1, x2, y2)
19
- @output << %Q{<line x1="#{x1}" y1="#{y1}" x2="#{x2}" y2="#{y2}"/>}
18
+ def line(x1, y1, x2, y2, style={})
19
+ @output << %Q{<line x1="#{x1}" y1="#{y1}" x2="#{x2}" y2="#{y2}"}
20
+ write_style(style)
21
+ @output << %Q{/>}
20
22
  end
21
23
 
22
24
  # Draw a circle given a center and a radius
23
- def circle(cx, cy, r)
24
- @output << %Q{<circle cx="#{cx}" cy="#{cy}" r="#{r}"/>}
25
+ def circle(cx, cy, r, style={})
26
+ @output << %Q{<circle cx="#{cx}" cy="#{cy}" r="#{r}"}
27
+ write_style(style)
28
+ @output << %Q{/>}
25
29
  end
26
30
 
27
31
  # Draw a rectangle or rounded rectangle
28
- def rectangle(x, y, width, height, rx=nil, ry=rx)
32
+ def rectangle(x, y, width, height, *args)
33
+ style = (!args.empty? && args.last.is_a?(Hash)) ? args.pop : {}
34
+ if args.length == 0
35
+ rx = ry = 0
36
+ elsif args.length == 1
37
+ rx = ry = args.pop
38
+ elsif args.length == 2
39
+ rx, ry = args
40
+ else
41
+ raise "Illegal number of arguments to rectangle"
42
+ end
43
+
29
44
  @output << %Q{<rect x="#{x}" y="#{y}" width="#{width}" height="#{height}"}
30
45
  @output << %Q{ rx="#{rx}" ry="#{ry}"} if rx && ry
46
+ write_style(style)
31
47
  @output << %Q{/>}
32
48
  end
33
49
 
34
50
  # Draw an circle given a center and two radii
35
- def ellipse(cx, cy, rx, ry)
36
- @output << %Q{<ellipse cx="#{cx}" cy="#{cy}" rx="#{rx}" ry="#{ry}"/>}
51
+ def ellipse(cx, cy, rx, ry, style={})
52
+ @output << %Q{<ellipse cx="#{cx}" cy="#{cy}" rx="#{rx}" ry="#{ry}"}
53
+ write_style(style)
54
+ @output << %Q{/>}
37
55
  end
38
56
 
39
57
  def polygon(*args)
@@ -89,6 +107,7 @@ private
89
107
  # Draws either a polygon or polyline according to the first parameter
90
108
  def polything(name, *args)
91
109
  return if args.empty?
110
+ style = (args.last.is_a?(Hash)) ? args.pop : {}
92
111
  coords = args.flatten
93
112
  raise "Illegal number of coordinates (should be even)" if coords.length.odd?
94
113
  @output << %Q{<#{name} points="}
@@ -98,6 +117,17 @@ private
98
117
  @output << "#{x},#{y}"
99
118
  @output << " " unless coords.empty?
100
119
  end
101
- @output << %{"/>}
120
+ write_style(style)
121
+ @output << '"/>'
122
+ end
123
+
124
+ # Writes styles to current output
125
+ def write_style(style)
126
+ return if style.nil? || style.empty?
127
+ @output << ' style="'
128
+ style.each_pair do |style, value|
129
+ @output << "#{style}:#{value};"
130
+ end
131
+ @output << '"'
102
132
  end
103
133
  end
@@ -51,6 +51,15 @@ describe Rasem::SVGImage do
51
51
  str.should =~ %r{y2="100"}
52
52
  end
53
53
 
54
+ it "should draw a line with style" do
55
+ img = Rasem::SVGImage.new("", 100, 100) do
56
+ line(0, 0, 10, 10, :fill=>"white")
57
+ end
58
+ str = img.output
59
+ str.should =~ %r{style=}
60
+ str.should =~ %r{fill:white}
61
+ end
62
+
54
63
  it "should draw a circle" do
55
64
  img = Rasem::SVGImage.new("", 100, 100) do
56
65
  circle(0, 0, 10)
@@ -61,7 +70,16 @@ describe Rasem::SVGImage do
61
70
  str.should =~ %r{cy="0"}
62
71
  str.should =~ %r{r="10"}
63
72
  end
64
-
73
+
74
+ it "should draw a circle with style" do
75
+ img = Rasem::SVGImage.new("", 100, 100) do
76
+ circle(0, 0, 10, :fill=>"white")
77
+ end
78
+ str = img.output
79
+ str.should =~ %r{style=}
80
+ str.should =~ %r{fill:white}
81
+ end
82
+
65
83
  it "should draw a rectangle" do
66
84
  img = Rasem::SVGImage.new("", 100, 100) do
67
85
  rectangle(0, 0, 100, 300)
@@ -72,6 +90,15 @@ describe Rasem::SVGImage do
72
90
  str.should =~ %r{height="300"}
73
91
  end
74
92
 
93
+ it "should draw a rectangle with style" do
94
+ img = Rasem::SVGImage.new("", 100, 100) do
95
+ rectangle(0, 0, 10, 10, :fill=>"white")
96
+ end
97
+ str = img.output
98
+ str.should =~ %r{style=}
99
+ str.should =~ %r{fill:white}
100
+ end
101
+
75
102
  it "should draw a symmetric round-rectangle" do
76
103
  img = Rasem::SVGImage.new("", 100, 100) do
77
104
  rectangle(0, 0, 100, 300, 20)
@@ -84,6 +111,15 @@ describe Rasem::SVGImage do
84
111
  str.should =~ %r{ry="20"}
85
112
  end
86
113
 
114
+ it "should draw a symmetric rounded-rectangle with style" do
115
+ img = Rasem::SVGImage.new("", 100, 100) do
116
+ rectangle(0, 0, 10, 10, 2, :fill=>"white")
117
+ end
118
+ str = img.output
119
+ str.should =~ %r{style=}
120
+ str.should =~ %r{fill:white}
121
+ end
122
+
87
123
  it "should draw a non-symmetric round-rectangle" do
88
124
  img = Rasem::SVGImage.new("", 100, 100) do
89
125
  rectangle(0, 0, 100, 300, 20, 5)
@@ -95,6 +131,15 @@ describe Rasem::SVGImage do
95
131
  str.should =~ %r{rx="20"}
96
132
  str.should =~ %r{ry="5"}
97
133
  end
134
+
135
+ it "should draw a non-symmetric rounded-rectangle with style" do
136
+ img = Rasem::SVGImage.new("", 100, 100) do
137
+ rectangle(0, 0, 10, 10, 2, 4, :fill=>"white")
138
+ end
139
+ str = img.output
140
+ str.should =~ %r{style=}
141
+ str.should =~ %r{fill:white}
142
+ end
98
143
 
99
144
  it "should draw an ellipse" do
100
145
  img = Rasem::SVGImage.new("", 100, 100) do
@@ -108,6 +153,15 @@ describe Rasem::SVGImage do
108
153
  str.should =~ %r{ry="300"}
109
154
  end
110
155
 
156
+ it "should draw an ellipse with style" do
157
+ img = Rasem::SVGImage.new("", 100, 100) do
158
+ ellipse(0, 0, 3, 10, :fill=>"white")
159
+ end
160
+ str = img.output
161
+ str.should =~ %r{style=}
162
+ str.should =~ %r{fill:white}
163
+ end
164
+
111
165
  it "should draw a polygon given an array of points" do
112
166
  img = Rasem::SVGImage.new("", 100, 100) do
113
167
  polygon([[0,0], [1,2], [3,4]])
@@ -117,6 +171,15 @@ describe Rasem::SVGImage do
117
171
  str.should =~ %r{points="0,0 1,2 3,4"}
118
172
  end
119
173
 
174
+ it "should draw a polygon with style" do
175
+ img = Rasem::SVGImage.new("", 100, 100) do
176
+ polygon([[0,0], [1,2], [3,4]], :fill=>"white")
177
+ end
178
+ str = img.output
179
+ str.should =~ %r{style=}
180
+ str.should =~ %r{fill:white}
181
+ end
182
+
120
183
  it "should draw a polyline given an array of points" do
121
184
  img = Rasem::SVGImage.new("", 100, 100) do
122
185
  polyline([[0,0], [1,2], [3,4]])
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.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: -2966740688363327135
113
+ hash: 3982115988403201538
114
114
  segments:
115
115
  - 0
116
116
  version: "0"