quinn-ruby-svg 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.mkdn ADDED
@@ -0,0 +1,10 @@
1
+ ruby-svg
2
+ ========
3
+
4
+ This is the beginning of a library for creating SVG documents with Ruby.
5
+
6
+ Something I started on when I was bored, and didn't get very far before I was bored again.
7
+
8
+ Feel free to fork and continue work, please send me a pullrequest if you get something worthwhile going.
9
+
10
+ ~ elliottcable.name
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 0
3
+ :major: 0
4
+ :minor: 0
data/lib/ruby-svg.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'ruby-svg/primatives/Path'
2
+ require 'ruby-svg/primatives/Rectangle'
3
+ require 'ruby-svg/primatives/A'
4
+ # require 'ruby-svg/complex/Pie'
5
+
6
+ class SVG
7
+ def initialize(args = {})
8
+ args = { :encoding => 'UTF-8',
9
+ :doctype => 'svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"',
10
+ :namespace => 'svg',
11
+ :width => 500,
12
+ :height => 500 }.merge(args)
13
+ args.each do |k,v|
14
+ self.instance_variable_set(('@' + k.to_s).to_sym, v)
15
+ end
16
+ @content = Array.new
17
+ end
18
+
19
+ attr_accessor :style
20
+ attr_accessor :title
21
+ attr_accessor :desc
22
+
23
+ attr_reader :content
24
+ def << (element)
25
+ @content << element
26
+ end
27
+
28
+ def to_xml(args = {})
29
+ standalone = args[:standalone].nil? || args[:standalone].class == (TrueClass || FalseClass) ? true : args[:standalone]
30
+ indent = args[:indent].nil? || args[:indent].class != Fixnum ? 0 : args[:indent]
31
+ indent = 0 if standalone
32
+
33
+ out = Array.new
34
+ if standalone
35
+ out << SVGHelper::wrap("?xml version='1.0' encoding='#{@encoding}' standalone='no'?", indent)
36
+ out << SVGHelper::wrap("!DOCTYPE #{@doctype}", indent)
37
+ end
38
+
39
+ # Header
40
+ header = String.new
41
+ header << (standalone ? '' : @namespace + ':') + 'svg'
42
+ [:width, :height].each do |a|
43
+ header << " #{a.to_s}='#{instance_variable_get('@' + a.to_s)}'"
44
+ end
45
+ header << " version='1.1' xmlns='http://www.w3.org/2000/svg'" if standalone
46
+ header << " xmlns:xlink='http://www.w3.org/1999/xlink'"
47
+ out << SVGHelper::wrap(header, indent)
48
+ indent += 1
49
+
50
+ {:title => {}, :style => {:type => 'text/css'}, :desc => {}}.each do |tag,attribs|
51
+ it = instance_variable_get('@' + tag.to_s)
52
+ unless it.nil?
53
+ header = (standalone ? '' : @namespace + ':') + tag.to_s
54
+ attribs.each do |k,v|
55
+ header << " #{k}='#{v}'"
56
+ end
57
+ out << SVGHelper::wrap(header, indent)
58
+ indent += 1
59
+ lines = ''
60
+ it.each do |line|
61
+ lines << SVGHelper::indent(line, indent)
62
+ end
63
+ out << lines.sub(/([^\n])\z/, "\\1\n")
64
+ indent -= 1
65
+ out << SVGHelper::wrap('/' + tag.to_s, indent)
66
+ end
67
+ end
68
+
69
+ @content.each do |element|
70
+ out << element.to_xml(:standalone => standalone, :indent => indent)
71
+ end
72
+
73
+ indent -= 1
74
+ out << SVGHelper::wrap('/' + (standalone ? '' : @namespace + ':') + 'svg', indent).chomp
75
+ end
76
+ end
77
+
78
+ class SVGHelper
79
+ def self.wrap(c, indent = 0)
80
+ SVGHelper::indent("<#{c}>", indent) + "\n"
81
+ end
82
+
83
+ def self.indent(c, level)
84
+ (' ' * level) + c
85
+ end
86
+ end
@@ -0,0 +1,20 @@
1
+ class SVGPieChart
2
+
3
+ end
4
+
5
+ class SVGPieSlice
6
+ # x, y, radius, percent, used
7
+ def initialize(args = {})
8
+ args.each do |k,v|
9
+ self.instance_variable_set(('@' + k.to_s).to_sym, v)
10
+ end
11
+ @angle = (2 * Math::PI) * (@percent / 100.0)
12
+ @full = @percent + @used
13
+ end
14
+
15
+ def angle
16
+ (2 * Math::PI) * (@percent - / 100.0)
17
+ end
18
+
19
+
20
+ end
@@ -0,0 +1,33 @@
1
+ class SVGA
2
+ def initialize(args = {})
3
+ args = { :href => '#' }.merge(args)
4
+ args.each do |k,v|
5
+ self.instance_variable_set(('@' + k.to_s).to_sym, v)
6
+ end
7
+ @content = Array.new
8
+ end
9
+
10
+ attr_reader :content
11
+ def << (element)
12
+ @content << element
13
+ end
14
+
15
+ def to_xml(args = {})
16
+ standalone = args[:standalone].nil? || args[:standalone].class == (TrueClass || FalseClass) ? true : args[:standalone]
17
+ indent = args[:indent].nil? || args[:indent].class != Fixnum ? 0 : args[:indent]
18
+
19
+ element = (standalone ? '' : @namespace + ':') + 'a'
20
+
21
+ element << " xlink:href='#{@href}'"
22
+
23
+ out = SVGHelper::wrap(element, indent)
24
+ indent += 1
25
+
26
+ @content.each do |element|
27
+ out << element.to_xml(:standalone => standalone, :indent => indent)
28
+ end
29
+
30
+ indent -= 1
31
+ out << SVGHelper::wrap('/' + (standalone ? '' : @namespace + ':') + 'a', indent)
32
+ end
33
+ end
File without changes
@@ -0,0 +1,80 @@
1
+ class SVGPath
2
+ def initialize(args = {})
3
+ args.each do |k,v|
4
+ self.instance_variable_set(('@' + k.to_s).to_sym, v)
5
+ end
6
+ @descriptors = Array.new
7
+ end
8
+
9
+ attr_reader :descriptors
10
+
11
+ def <<(descriptor)
12
+ @descriptors << descriptor
13
+ end
14
+
15
+ def to_xml(args = {})
16
+ standalone = args[:standalone].nil? || args[:standalone].class == (TrueClass || FalseClass) ? true : args[:standalone]
17
+ indent = args[:indent].nil? || args[:indent].class != Fixnum ? 0 : args[:indent]
18
+
19
+ element = (standalone ? '' : @namespace + ':') + 'path'
20
+
21
+ descriptors = String.new
22
+ @descriptors.each do |descriptor|
23
+ descriptors << descriptor.to_s + " "
24
+ end
25
+ descriptors << "Z"
26
+
27
+ element << " d='#{descriptors}'"
28
+
29
+ [:id, :class, :style].each do |a|
30
+ element << " #{a.to_s}='#{instance_variable_get('@' + a.to_s)}'" unless a.nil?
31
+ end
32
+ out = SVGHelper::wrap(element + "/", indent)
33
+ end
34
+ end
35
+
36
+ class SVGPathMoveComponent
37
+ def initialize(args = {})
38
+ args = { :x => 0, :y => 0 }.merge(args)
39
+ args.each do |k,v|
40
+ self.instance_variable_set(('@' + k.to_s).to_sym, v)
41
+ end
42
+ end
43
+
44
+ def to_s
45
+ "M#{@x},#{@y}"
46
+ end
47
+ end
48
+ class SVGPathLineComponent
49
+ def initialize(args = {})
50
+ args = { :x => 0, :y => 0 }.merge(args)
51
+ args.each do |k,v|
52
+ self.instance_variable_set(('@' + k.to_s).to_sym, v)
53
+ end
54
+ end
55
+
56
+ def to_s
57
+ "L#{@x},#{@y}"
58
+ end
59
+ end
60
+ class SVGPathArcComponent
61
+ # :direction => :american
62
+ def initialize(args = {})
63
+ args = { :rx => 100, :ry => 100, :slant => 0, :long => false, :direction => :cartesian, :x => 100, :y => 100 }.merge(args)
64
+ args.each do |k,v|
65
+ self.instance_variable_set(('@' + k.to_s).to_sym, v)
66
+ end
67
+ end
68
+
69
+ def to_s
70
+ long = @long ? 1 : 0
71
+ direction = case @direction
72
+ when :cartesian
73
+ 0
74
+ when :american
75
+ 1
76
+ end
77
+
78
+ "A#{@rx},#{@ry} #{@slant} #{long},#{direction} #{@x},#{@y}"
79
+ end
80
+ end
@@ -0,0 +1,28 @@
1
+ class SVGPolygon
2
+ def initialize(args = {})
3
+ @points = ""
4
+ args = { :x => 0, :y => 0,
5
+ :width => 500, :height => 500 }.merge(args)
6
+ args.each do |k,v|
7
+ self.instance_variable_set(('@' + k.to_s).to_sym, v)
8
+ end
9
+ @content = Array.new
10
+ end
11
+
12
+ def vertex x,y
13
+ @points << "#{x}, #{y}"
14
+ end
15
+
16
+ alias_method :vertex, :point
17
+
18
+ def to_xml(args = {})
19
+ standalone = args[:standalone].nil? || args[:standalone].class == (TrueClass || FalseClass) ? true : args[:standalone]
20
+ indent = args[:indent].nil? || args[:indent].class != Fixnum ? 0 : args[:indent]
21
+
22
+ element = (standalone ? '' : @namespace + ':') + 'polygon'
23
+ [:x, :y, :points, :id, :class, :style].each do |a|
24
+ element << " #{a.to_s}='#{instance_variable_get('@' + a.to_s)}'" unless a.nil?
25
+ end
26
+ out = SVGHelper::wrap(element + "/", indent)
27
+ end
28
+ end
@@ -0,0 +1,21 @@
1
+ class SVGRectangle
2
+ def initialize(args = {})
3
+ args = { :x => 0, :y => 0,
4
+ :width => 500, :height => 500 }.merge(args)
5
+ args.each do |k,v|
6
+ self.instance_variable_set(('@' + k.to_s).to_sym, v)
7
+ end
8
+ @content = Array.new
9
+ end
10
+
11
+ def to_xml(args = {})
12
+ standalone = args[:standalone].nil? || args[:standalone].class == (TrueClass || FalseClass) ? true : args[:standalone]
13
+ indent = args[:indent].nil? || args[:indent].class != Fixnum ? 0 : args[:indent]
14
+
15
+ element = (standalone ? '' : @namespace + ':') + 'rect'
16
+ [:x, :y, :width, :height, :id, :class, :style].each do |a|
17
+ element << " #{a.to_s}='#{instance_variable_get('@' + a.to_s)}'" unless a.nil?
18
+ end
19
+ out = SVGHelper::wrap(element + "/", indent)
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quinn-ruby-svg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - quinn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-09 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: TODO
17
+ email: q.shanahan+service@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.mkdn
26
+ - VERSION.yml
27
+ - lib/ruby-svg
28
+ - lib/ruby-svg/complex
29
+ - lib/ruby-svg/complex/Pie.rb
30
+ - lib/ruby-svg/primatives
31
+ - lib/ruby-svg/primatives/A.rb
32
+ - lib/ruby-svg/primatives/Defs.rb
33
+ - lib/ruby-svg/primatives/Path.rb
34
+ - lib/ruby-svg/primatives/Polygon.rb
35
+ - lib/ruby-svg/primatives/Rectangle.rb
36
+ - lib/ruby-svg.rb
37
+ has_rdoc: false
38
+ homepage: http://github.com/quinn/ruby-svg
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: TODO
63
+ test_files: []
64
+