rasem 0.0.0 → 0.1.0

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.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/rasem/svg_image.rb +103 -0
  3. data/spec/rasem_spec.rb +80 -5
  4. metadata +5 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.1.0
@@ -0,0 +1,103 @@
1
+ class Rasem::SVGImage
2
+
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
9
+
10
+ write_header(*args)
11
+ if block
12
+ self.instance_exec(&block)
13
+ self.close
14
+ end
15
+ end
16
+
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}"/>}
20
+ end
21
+
22
+ # 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
+ end
26
+
27
+ # Draw a rectangle or rounded rectangle
28
+ def rectangle(x, y, width, height, rx=nil, ry=rx)
29
+ @output << %Q{<rect x="#{x}" y="#{y}" width="#{width}" height="#{height}"}
30
+ @output << %Q{ rx="#{rx}" ry="#{ry}"} if rx && ry
31
+ @output << %Q{/>}
32
+ end
33
+
34
+ # 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}"/>}
37
+ end
38
+
39
+ def polygon(*args)
40
+ polything("polygon", *args)
41
+ end
42
+
43
+ def polyline(*args)
44
+ polything("polyline", *args)
45
+ end
46
+
47
+ # Closes the file. No more drawing is possible after this
48
+ def close
49
+ write_close
50
+ @closed = true
51
+ end
52
+
53
+ def output
54
+ @output.to_s
55
+ end
56
+
57
+ def closed?
58
+ @closed
59
+ end
60
+
61
+ private
62
+ # Creates an object for ouput out of an argument
63
+ def create_output(arg)
64
+ if arg.nil?
65
+ ""
66
+ elsif arg.respond_to?(:<<)
67
+ arg
68
+ else
69
+ raise "Illegal output object: #{arg.inspect}"
70
+ end
71
+ end
72
+
73
+ # Writes file header
74
+ def write_header(width, height)
75
+ @output << <<-HEADER
76
+ <?xml version="1.0" standalone="no"?>
77
+ <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
78
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
79
+ <svg width="#{width}" height="#{height}" version="1.1"
80
+ xmlns="http://www.w3.org/2000/svg">
81
+ HEADER
82
+ end
83
+
84
+ # Write the closing tag of the file
85
+ def write_close
86
+ @output << "</svg>"
87
+ end
88
+
89
+ # Draws either a polygon or polyline according to the first parameter
90
+ def polything(name, *args)
91
+ return if args.empty?
92
+ coords = args.flatten
93
+ raise "Illegal number of coordinates (should be even)" if coords.length.odd?
94
+ @output << %Q{<#{name} points="}
95
+ until coords.empty? do
96
+ x = coords.shift
97
+ y = coords.shift
98
+ @output << "#{x},#{y}"
99
+ @output << " " unless coords.empty?
100
+ end
101
+ @output << %{"/>}
102
+ end
103
+ end
@@ -4,8 +4,8 @@ describe Rasem::SVGImage do
4
4
  it "should initialize an empty image" do
5
5
  img = Rasem::SVGImage.new("", 100, 100)
6
6
  str = img.output
7
- str.should =~ /width="100"/
8
- str.should =~ /height="100"/
7
+ str.should =~ %r{width="100"}
8
+ str.should =~ %r{height="100"}
9
9
  end
10
10
 
11
11
  it "should close an image" do
@@ -26,7 +26,11 @@ describe Rasem::SVGImage do
26
26
  img.line(0, 0, 100, 100)
27
27
  img.close
28
28
  str = img.output
29
- str.should =~ %r{<line x1="0" y1="0" x2="100" y2="100"/>}
29
+ str.should =~ %r{<line}
30
+ str.should =~ %r{x1="0"}
31
+ str.should =~ %r{y1="0"}
32
+ str.should =~ %r{x2="100"}
33
+ str.should =~ %r{y2="100"}
30
34
  end
31
35
 
32
36
  it "should draw line using a block" do
@@ -34,7 +38,11 @@ describe Rasem::SVGImage do
34
38
  line(0, 0, 100, 100)
35
39
  end
36
40
  str = img.output
37
- str.should =~ %r{<line x1="0" y1="0" x2="100" y2="100"/>}
41
+ str.should =~ %r{<line}
42
+ str.should =~ %r{x1="0"}
43
+ str.should =~ %r{y1="0"}
44
+ str.should =~ %r{x2="100"}
45
+ str.should =~ %r{y2="100"}
38
46
  end
39
47
 
40
48
  it "should draw a circle" do
@@ -42,7 +50,74 @@ describe Rasem::SVGImage do
42
50
  circle(0, 0, 10)
43
51
  end
44
52
  str = img.output
45
- str.should =~ %r{<circle cx="0" cy="0" r="10"/>}
53
+ str.should =~ %r{<circle}
54
+ str.should =~ %r{cx="0"}
55
+ str.should =~ %r{cy="0"}
56
+ str.should =~ %r{r="10"}
57
+ end
58
+
59
+ it "should draw a rectangle" do
60
+ img = Rasem::SVGImage.new("", 100, 100) do
61
+ rectangle(0, 0, 100, 300)
62
+ end
63
+ str = img.output
64
+ str.should =~ %r{<rect}
65
+ str.should =~ %r{width="100"}
66
+ str.should =~ %r{height="300"}
67
+ end
68
+
69
+ it "should draw a symmetric round-rectangle" do
70
+ img = Rasem::SVGImage.new("", 100, 100) do
71
+ rectangle(0, 0, 100, 300, 20)
72
+ end
73
+ str = img.output
74
+ str.should =~ %r{<rect}
75
+ str.should =~ %r{width="100"}
76
+ str.should =~ %r{height="300"}
77
+ str.should =~ %r{rx="20"}
78
+ str.should =~ %r{ry="20"}
79
+ end
80
+
81
+ it "should draw a non-symmetric round-rectangle" do
82
+ img = Rasem::SVGImage.new("", 100, 100) do
83
+ rectangle(0, 0, 100, 300, 20, 5)
84
+ end
85
+ str = img.output
86
+ str.should =~ %r{<rect}
87
+ str.should =~ %r{width="100"}
88
+ str.should =~ %r{height="300"}
89
+ str.should =~ %r{rx="20"}
90
+ str.should =~ %r{ry="5"}
91
+ end
92
+
93
+ it "should draw an ellipse" do
94
+ img = Rasem::SVGImage.new("", 100, 100) do
95
+ ellipse(0, 0, 100, 300)
96
+ end
97
+ str = img.output
98
+ str.should =~ %r{<ellipse}
99
+ str.should =~ %r{cx="0"}
100
+ str.should =~ %r{cy="0"}
101
+ str.should =~ %r{rx="100"}
102
+ str.should =~ %r{ry="300"}
103
+ end
104
+
105
+ it "should draw a polygon given an array of points" do
106
+ img = Rasem::SVGImage.new("", 100, 100) do
107
+ polygon([[0,0], [1,2], [3,4]])
108
+ end
109
+ str = img.output
110
+ str.should =~ %r{<polygon}
111
+ str.should =~ %r{points="0,0 1,2 3,4"}
112
+ end
113
+
114
+ it "should draw a polyline given an array of points" do
115
+ img = Rasem::SVGImage.new("", 100, 100) do
116
+ polyline([[0,0], [1,2], [3,4]])
117
+ end
118
+ str = img.output
119
+ str.should =~ %r{<polyline}
120
+ str.should =~ %r{points="0,0 1,2 3,4"}
46
121
  end
47
122
 
48
123
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 0
9
- version: 0.0.0
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ahmed Eldawy
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-04-09 00:00:00 -05:00
17
+ date: 2011-04-10 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -93,6 +93,7 @@ files:
93
93
  - Rakefile
94
94
  - VERSION
95
95
  - lib/rasem.rb
96
+ - lib/rasem/svg_image.rb
96
97
  - spec/rasem_spec.rb
97
98
  - spec/spec_helper.rb
98
99
  has_rdoc: true
@@ -109,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
110
  requirements:
110
111
  - - ">="
111
112
  - !ruby/object:Gem::Version
112
- hash: -2449500993828401996
113
+ hash: -2310388374687668377
113
114
  segments:
114
115
  - 0
115
116
  version: "0"