ruby_svg_light 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2011, Morgan Prior
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ * Neither the name of the organization nor the
12
+ names of its contributors may be used to endorse or promote products
13
+ derived from this software without specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+
data/Rakefile CHANGED
@@ -2,10 +2,94 @@ require 'rspec/core/rake_task'
2
2
 
3
3
  file_list = FileList['spec/*_spec.rb']
4
4
 
5
- RSpec::Core::RakeTask.new('spec') do |t|
6
- t.pattern = file_list
7
- t.rspec_opts = ["--colour", "--format progress", "-p"]
5
+ namespace :test do
6
+ RSpec::Core::RakeTask.new('spec') do |t|
7
+ t.pattern = file_list
8
+ t.rspec_opts = ["--colour", "--format progress", "-p"]
9
+ end
8
10
  end
9
-
10
11
  desc 'Default: run specs.'
11
- task :default => 'spec'
12
+ task :default => 'test:spec'
13
+
14
+ namespace :deploy do
15
+
16
+ ## name and version taken from
17
+ ## https://github.com/mojombo/rakegem
18
+ def name
19
+ @name ||= Dir['*.gemspec'].first.split('.').first
20
+ end
21
+
22
+ def version
23
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
24
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
25
+ end
26
+
27
+ desc 'Run Spec'
28
+ html_path = "spec_results.html"
29
+ RSpec::Core::RakeTask.new('run_spec') do |t|
30
+ t.pattern = file_list
31
+ t.rspec_opts = ["--format html", "--out #{html_path}"]
32
+ end
33
+
34
+
35
+ desc 'Check that the spec passes'
36
+ task :spec_passes => :run_spec do
37
+ # find out how many errors were found
38
+ html = open(html_path).read
39
+ examples = html.match(/(\d+) examples/)[0].to_i rescue 0
40
+ failures = html.match(/(\d+) failures/)[0].to_i rescue 0
41
+ pending = html.match(/(\d+) pending/)[0].to_i rescue 0
42
+
43
+ if failures.zero?
44
+ puts "0 failures! #{examples} run, #{pending} pending"
45
+ else
46
+ $stderr.puts "\aSpecs did not pass deploy aborted!"
47
+ $stderr.puts "View spec results at #{File.expand_path(html_path)}"
48
+ $stderr.puts
49
+ $stderr.puts "#{failures} failures! #{examples} run, #{pending} pending"
50
+ exit -1
51
+ end
52
+ end
53
+
54
+ task :build_gem do
55
+ puts "Building Gem"
56
+ `gem build #{name}.gemspec`
57
+ unless $?.success?
58
+ $stderr.puts "Gem Build Failed"
59
+ exit -1
60
+ end
61
+
62
+ end
63
+
64
+ task :push_gem do
65
+ puts "Pushing Gem #{name}-#{version}"
66
+ `gem push #{name}-#{version}.gem`
67
+ unless $?.success?
68
+ $stderr.puts "Gem Push Failed"
69
+ exit -1
70
+ end
71
+ end
72
+
73
+ task :create_git_tag do
74
+ puts "Tagging #{name}-#{version}"
75
+ `git tag -a #{version} -m 'Tagging gem release #{version}'`
76
+ unless $?.success?
77
+ $stderr.puts "Tagging Git Failed"
78
+ exit -1
79
+ end
80
+ end
81
+
82
+ task :push_tag do
83
+ puts "Push Tag #{version}"
84
+ `git push origin #{version}`
85
+ unless $?.success?
86
+ $stderr.puts "Pushing Git Tag Failed"
87
+ exit -1
88
+ end
89
+ end
90
+
91
+ task :gem => [:spec_passes, :build_gem, :push_gem, :create_git_tag, :push_tag] do
92
+ puts "Deployed #{name} Version #{version} !"
93
+ end
94
+
95
+ end
@@ -0,0 +1,239 @@
1
+
2
+ module RubySVGLight
3
+ VERSION = '0.0.3'
4
+
5
+ class Document
6
+ attr_reader :body
7
+
8
+ def initialize( options={} )
9
+ @options = options
10
+ @options[:name] ||= ''
11
+ @options[:width] ||= :auto
12
+ @options[:height] ||= :auto
13
+ @options[:stroke] ||= '#000000'
14
+ @options[:stroke_width] ||= 1
15
+ @options[:fill] ||= 'white'
16
+ @options[:font_size] ||= '24px'
17
+
18
+ @width = 0
19
+ @height = 0
20
+ @body = []
21
+ end
22
+
23
+
24
+
25
+ def header
26
+ text = %{<?xml version="1.0" encoding="UTF-8" standalone="no"?>
27
+ <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
28
+
29
+ <svg
30
+ width="100%"
31
+ height="100%"
32
+ version="1.1"
33
+ xmlns="http://www.w3.org/2000/svg">
34
+ }
35
+ end
36
+
37
+ def footer
38
+ text = %{</svg>}
39
+ end
40
+
41
+
42
+
43
+ ## Drawing methodds
44
+ def circle(x=0, y=0, radius=10, opts={})
45
+ local_options = calc_local_options(opts)
46
+ text = %{<circle
47
+ cx="#{x}" cy="#{y}"
48
+ r="#{radius}"
49
+ stroke="#{local_options[:stroke]}"
50
+ stroke-width="#{local_options[:stroke_width]}"
51
+ fill="#{local_options[:fill]}"/>
52
+ }
53
+
54
+ update_size(x+radius, y+radius)
55
+ @body << text
56
+ end
57
+
58
+ def line(x,y,w,h, opts={})
59
+ local_options = calc_local_options(opts)
60
+ text = %{<line
61
+ x1="#{x}" y1="#{y}"
62
+ x2="#{x+w}" y2="#{y+h}"
63
+ stroke="#{local_options[:stroke]}"
64
+ stroke-width="#{local_options[:stroke_width]}"
65
+ />
66
+ }
67
+ #path d="m #{x},#{y} l #{x+w},#{y+h}"
68
+
69
+ update_size(x+w, y+h)
70
+ @body << text
71
+ end
72
+
73
+ def rectangle(x,y,width,height, opts={})
74
+ local_options = calc_local_options(opts)
75
+ text = %{<rect
76
+ x="#{x}" y="#{y}"
77
+ width="#{width}" height="#{height}"
78
+ fill="#{local_options[:fill]}"
79
+ stroke="#{local_options[:stroke]}"
80
+ stroke-width="#{local_options[:stroke_width]}"
81
+ />
82
+ }
83
+ update_size(x+width, y+height)
84
+ @body << text
85
+ end
86
+
87
+ def ellipse(x,y,rx,ry, opts={})
88
+ local_options = calc_local_options(opts)
89
+ text = %{<ellipse
90
+ cx="#{x}" cy="#{y}"
91
+ rx="#{rx}" ry="#{ry}"
92
+ fill="#{local_options[:fill]}"
93
+ stroke="#{local_options[:stroke]}"
94
+ stroke-width="#{local_options[:stroke_width]}"
95
+ />
96
+ }
97
+ update_size(x+rx, y+ry)
98
+ @body << text
99
+ end
100
+
101
+ def text(x,y, input_text, opts={})
102
+ centre_text = "text-anchor:middle; dominant-baseline:central;"
103
+ local_options = calc_local_options(opts)
104
+
105
+ text_vertical_centre = local_options[:text_centre] ||
106
+ local_options[:text_vertical_middle] ||
107
+ local_options[:text_vertical_centre] ||
108
+ local_options[:text_v_middle] ||
109
+ local_options[:text_v_centre]
110
+
111
+ text_horizontal_centre = local_options[:text_centre] ||
112
+ local_options[:text_horizontal_middle] ||
113
+ local_options[:text_horizontal_centre] ||
114
+ local_options[:text_h_middle] ||
115
+ local_options[:text_h_centre]
116
+
117
+
118
+ style = ""
119
+ style << "dominant-baseline: central; " if text_vertical_centre
120
+
121
+ style << "text-anchor: top; " if local_options[:text_horizontal_top]
122
+ style << "text-anchor: middle; " if text_horizontal_centre
123
+ style << "text-anchor: bottom; " if local_options[:text_horizontal_bottom]
124
+
125
+
126
+ #Stroke is outline
127
+ #fill is normal font. ie for text fill gets stroke colour
128
+ text = %{<text
129
+ x="#{x}" y="#{y}" }
130
+
131
+ text << %{
132
+ style="#{style}" } unless style == ""
133
+
134
+ text << %{
135
+ font-size="#{local_options[:font_size]}"
136
+ fill="#{local_options[:stroke]}"
137
+ font-family="Sans"
138
+ >#{input_text}
139
+ </text>
140
+ }
141
+ #Do not know height or width of text only the anchor
142
+ update_size(x, y)
143
+ @body << text
144
+ end
145
+
146
+ def options(opts={})
147
+ opts.each_pair do |key,value|
148
+ @options[key] = value
149
+ end
150
+
151
+ #return
152
+ @options
153
+ end
154
+
155
+
156
+ def height
157
+ if @options[:height] == :auto
158
+ @height
159
+ else
160
+ @options[:height]
161
+ end
162
+ end
163
+
164
+ def width
165
+ if @options[:width] == :auto
166
+ @width
167
+ else
168
+ @options[:width]
169
+ end
170
+ end
171
+
172
+
173
+ def components_only_to_s
174
+ text = ""
175
+ @body.each do |section|
176
+ text << section
177
+ end
178
+
179
+ #return
180
+ text
181
+ end
182
+
183
+ # To string includes header and footers
184
+ def to_s
185
+ text = ""
186
+ text << self.header
187
+ @body.each do |section|
188
+ text << section
189
+ end
190
+ text << self.footer
191
+
192
+ #return
193
+ text
194
+ end
195
+
196
+
197
+ # to_file includes header and footers for a complete svg file
198
+ def to_file( filename )
199
+ File.open(filename, 'w') do |f|
200
+ f.puts self.header
201
+ @body.each do |section|
202
+ f.puts section
203
+ end
204
+ f.puts self.footer
205
+ end
206
+ end
207
+
208
+
209
+ # Private method, for allowing overide options
210
+ def calc_local_options(opts={})
211
+ local_opts = @options.dup
212
+ opts.each_pair do |key,value|
213
+ local_opts[key] = value
214
+ end
215
+
216
+ #return
217
+ local_opts
218
+ end
219
+
220
+ #Internal helper function for updating dimensions
221
+ def update_size(x,y)
222
+ @width = x if x > @width
223
+ @height = y if y > @height
224
+ end
225
+
226
+ end
227
+ end
228
+
229
+
230
+
231
+ if $0 == __FILE__
232
+ filename = 'test.svg'
233
+
234
+ a = RubySVGLight::Document.new()
235
+ a.circle(20,10,10)
236
+ a.to_file( filename )
237
+
238
+ puts "Created #{filename}"
239
+ end
@@ -1,239 +1,10 @@
1
1
 
2
2
  module RubySVGLight
3
- VERSION = '0.0.3'
4
-
5
- class Document
6
- attr_reader :body
7
-
8
- def initialize( options={} )
9
- @options = options
10
- @options[:name] ||= ''
11
- @options[:width] ||= :auto
12
- @options[:height] ||= :auto
13
- @options[:stroke] ||= '#000000'
14
- @options[:stroke_width] ||= 1
15
- @options[:fill] ||= 'white'
16
- @options[:font_size] ||= '24px'
17
-
18
- @width = 0
19
- @height = 0
20
- @body = []
21
- end
22
-
23
-
24
-
25
- def header
26
- text = %{<?xml version="1.0" encoding="UTF-8" standalone="no"?>
27
- <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
28
-
29
- <svg
30
- width="100%"
31
- height="100%"
32
- version="1.1"
33
- xmlns="http://www.w3.org/2000/svg">
34
- }
35
- end
36
-
37
- def footer
38
- text = %{</svg>}
39
- end
40
-
41
-
42
-
43
- ## Drawing methodds
44
- def circle(x=0, y=0, radius=10, opts={})
45
- local_options = calc_local_options(opts)
46
- text = %{<circle
47
- cx="#{x}" cy="#{y}"
48
- r="#{radius}"
49
- stroke="#{local_options[:stroke]}"
50
- stroke-width="#{local_options[:stroke_width]}"
51
- fill="#{local_options[:fill]}"/>
52
- }
53
-
54
- update_size(x+radius, y+radius)
55
- @body << text
56
- end
57
-
58
- def line(x,y,w,h, opts={})
59
- local_options = calc_local_options(opts)
60
- text = %{<line
61
- x1="#{x}" y1="#{y}"
62
- x2="#{x+w}" y2="#{y+h}"
63
- stroke="#{local_options[:stroke]}"
64
- stroke-width="#{local_options[:stroke_width]}"
65
- />
66
- }
67
- #path d="m #{x},#{y} l #{x+w},#{y+h}"
68
-
69
- update_size(x+w, y+h)
70
- @body << text
71
- end
72
-
73
- def rectangle(x,y,width,height, opts={})
74
- local_options = calc_local_options(opts)
75
- text = %{<rect
76
- x="#{x}" y="#{y}"
77
- width="#{width}" height="#{height}"
78
- fill="#{local_options[:fill]}"
79
- stroke="#{local_options[:stroke]}"
80
- stroke-width="#{local_options[:stroke_width]}"
81
- />
82
- }
83
- update_size(x+width, y+height)
84
- @body << text
85
- end
86
-
87
- def ellipse(x,y,rx,ry, opts={})
88
- local_options = calc_local_options(opts)
89
- text = %{<ellipse
90
- cx="#{x}" cy="#{y}"
91
- rx="#{rx}" ry="#{ry}"
92
- fill="#{local_options[:fill]}"
93
- stroke="#{local_options[:stroke]}"
94
- stroke-width="#{local_options[:stroke_width]}"
95
- />
96
- }
97
- update_size(x+rx, y+ry)
98
- @body << text
99
- end
100
-
101
- def text(x,y, input_text, opts={})
102
- centre_text = "text-anchor:middle; dominant-baseline:central;"
103
- local_options = calc_local_options(opts)
104
-
105
- text_vertical_centre = local_options[:text_centre] ||
106
- local_options[:text_vertical_middle] ||
107
- local_options[:text_vertical_centre] ||
108
- local_options[:text_v_middle] ||
109
- local_options[:text_v_centre]
110
-
111
- text_horizontal_centre = local_options[:text_centre] ||
112
- local_options[:text_horizontal_middle] ||
113
- local_options[:text_horizontal_centre] ||
114
- local_options[:text_h_middle] ||
115
- local_options[:text_h_centre]
116
-
117
-
118
- style = ""
119
- style << "dominant-baseline: central; " if text_vertical_centre
120
-
121
- style << "text-anchor: top; " if local_options[:text_horizontal_top]
122
- style << "text-anchor: middle; " if text_horizontal_centre
123
- style << "text-anchor: bottom; " if local_options[:text_horizontal_bottom]
124
-
125
-
126
- #Stroke is outline
127
- #fill is normal font. ie for text fill gets stroke colour
128
- text = %{<text
129
- x="#{x}" y="#{y}" }
130
-
131
- text << %{
132
- style="#{style}" } unless style == ""
133
-
134
- text << %{
135
- font-size="#{local_options[:font_size]}"
136
- fill="#{local_options[:stroke]}"
137
- font-family="Sans"
138
- >#{input_text}
139
- </text>
140
- }
141
- #Do not know height or width of text only the anchor
142
- update_size(x, y)
143
- @body << text
144
- end
145
-
146
- def options(opts={})
147
- opts.each_pair do |key,value|
148
- @options[key] = value
149
- end
150
-
151
- #return
152
- @options
153
- end
154
-
155
-
156
- def height
157
- if @options[:height] == :auto
158
- @height
159
- else
160
- @options[:height]
161
- end
162
- end
163
-
164
- def width
165
- if @options[:width] == :auto
166
- @width
167
- else
168
- @options[:width]
169
- end
170
- end
171
-
172
-
173
- def components_only_to_s
174
- text = ""
175
- @body.each do |section|
176
- text << section
177
- end
178
-
179
- #return
180
- text
181
- end
182
-
183
- # To string includes header and footers
184
- def to_s
185
- text = ""
186
- text << self.header
187
- @body.each do |section|
188
- text << section
189
- end
190
- text << self.footer
191
-
192
- #return
193
- text
194
- end
195
-
196
-
197
- # to_file includes header and footers for a complete svg file
198
- def to_file( filename )
199
- File.open(filename, 'w') do |f|
200
- f.puts self.header
201
- @body.each do |section|
202
- f.puts section
203
- end
204
- f.puts self.footer
205
- end
206
- end
207
-
208
-
209
- # Private method, for allowing overide options
210
- def calc_local_options(opts={})
211
- local_opts = @options.dup
212
- opts.each_pair do |key,value|
213
- local_opts[key] = value
214
- end
215
-
216
- #return
217
- local_opts
218
- end
219
-
220
- #Internal helper function for updating dimensions
221
- def update_size(x,y)
222
- @width = x if x > @width
223
- @height = y if y > @height
224
- end
225
-
226
- end
3
+ VERSION = '0.1.0'
227
4
  end
228
5
 
229
-
230
-
231
- if $0 == __FILE__
232
- filename = 'test.svg'
233
-
234
- a = RubySVGLight::Document.new()
235
- a.circle(20,10,10)
236
- a.to_file( filename )
237
-
238
- puts "Created #{filename}"
6
+ begin
7
+ require 'ruby_svg_light/document'
8
+ rescue
9
+ require_relative 'ruby_svg_light/document'
239
10
  end