svgen 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4ac4edd0774ebc4189f3b37d4bdcb2cfa1cc9d9b
4
- data.tar.gz: 9241392aed9ead03e300d2a8dead528e111a52c0
3
+ metadata.gz: ba2bf72e26dc27ef68324a6cc8ed88bdaa06327d
4
+ data.tar.gz: 914b64c57e8d97d1082f19a09f298e2421542771
5
5
  SHA512:
6
- metadata.gz: 16975f7fd1044f56c0a1cb630175404f0c4b7b6455d2ea7b81e4a598e0554fe056ac0f97b3cfd3d48a0eb051567249a7c297aba47d26abff4f7f1580cd2d0f6a
7
- data.tar.gz: 1ac64515a7a7d4765f96c3ca6568ec583e7bbfb69a050fd936d72149fda20d1eb16e391e110bc2d0fe55b3b09d06022227a17adb1b6a60a0a25c5c39643a3b78
6
+ metadata.gz: dd05c6d01bf5f58db290ebde74d35edd0c17fbe1de358c11ddb3829ec9453355aabf788d0c63ebe04fe8b289768d4996d6bbe4a4df548a5cc3946d86972f9bd3
7
+ data.tar.gz: c4b524f9c66918f231d6874ea7035d2d84cdf9e208df9d2b58ce5c9e66639cca0e777f8753eaf9f7926d799330d8d95b1e64fd73e022493111052297afcb3460
data/README.md CHANGED
@@ -10,12 +10,15 @@ $ gem install svgen
10
10
 
11
11
  ## Usage
12
12
 
13
+ ### rect, circle, text
14
+
13
15
  ```rb
14
16
  require "svgen"
15
17
 
16
18
  svg = SVGen::SVG.new(width: 600, height: 400) do |svg|
17
19
  svg.rect(width: 300, height: 200, fill: "blue")
18
20
  svg.circle(cx: 100, cy: 100, r: 50, fill: "red")
21
+ svg.text("Sample Text", x: 20, y: 20)
19
22
  end
20
23
  svg.generate
21
24
  #=>
@@ -24,5 +27,29 @@ svg.generate
24
27
  # <svg width="600" height="300" xmlns="http://www.w3.org/2000/svg">
25
28
  # <rect width="300" height="200" fill="blue"/>
26
29
  # <circle cx="100" cy="100" r="50" fill="red"/>
30
+ # <text x="20" cy="20">Sample Text</text>
31
+ # </svg>
32
+ ```
33
+
34
+ ### group
35
+
36
+ ```rb
37
+ require "svgen"
38
+
39
+ svg = SVGen::SVG.new(width: 600, height: 400) do |svg|
40
+ svg.g(stroke: "red", "stroke-width" => 5) do |g|
41
+ g.rect(x: 50, y: 50, width: 100, height: 100)
42
+ g.circle(cx: 200, cy: 200, r: 50)
43
+ end
44
+ end
45
+ svg.generate
46
+ #=>
47
+ # <?xml version="1.0" encoding="UTF-8" standalone="no"?>
48
+ # <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
49
+ # <svg width="600" height="300" xmlns="http://www.w3.org/2000/svg">
50
+ # <g stroke="red" stroke-width="5">
51
+ # <rect x="50" y="50" width="100" height="100"/>
52
+ # <circle cx="200" cy="200" r="50"/>
53
+ # </g>
27
54
  # </svg>
28
55
  ```
@@ -4,6 +4,8 @@ module SVGen
4
4
  def initialize(attrs = {})
5
5
  @x = attrs[:x] || 0
6
6
  @y = attrs[:y] || 0
7
+ @stroke = attrs[:stroke]
8
+ @stroke_width = attrs[:"stroke-width"] || attrs["stroke-width"]
7
9
  end
8
10
 
9
11
  def generate(svg)
@@ -15,8 +17,11 @@ module SVGen
15
17
  def attributes
16
18
  attrs = {}
17
19
  instance_variables.each do |instance_variable|
18
- key = instance_variable.to_s.gsub(/@/) { "" }.to_sym
19
- attrs[key] = instance_variable_get(instance_variable)
20
+ value = instance_variable_get(instance_variable)
21
+ next if value.nil?
22
+ key = instance_variable.to_s.gsub(/@/) { "" }
23
+ key = key.gsub(/_/) { "-" }
24
+ attrs[key.to_sym] = value
20
25
  end
21
26
  attrs
22
27
  end
@@ -6,12 +6,21 @@ module SVGen
6
6
  @cx = attrs[:cx] || 0
7
7
  @cy = attrs[:cy] || 0
8
8
  @r = attrs[:r] || 10
9
- @fill = attrs[:fill] || "white"
9
+ @fill = attrs[:fill] || "rgba(0, 0, 0, 0)"
10
10
  end
11
11
 
12
12
  def generate(svg)
13
13
  svg.circle(attributes)
14
14
  end
15
+
16
+ protected
17
+
18
+ def attributes
19
+ attrs = super
20
+ attrs.delete(:x)
21
+ attrs.delete(:y)
22
+ attrs
23
+ end
15
24
  end
16
25
  end
17
26
  end
@@ -0,0 +1,28 @@
1
+ module SVGen
2
+ module Element
3
+ class Group < Base
4
+ include Nestable
5
+
6
+ def initialize(attrs = {}, &block)
7
+ @children = []
8
+ @stroke = attrs[:stroke]
9
+ @stroke_width = attrs[:"stroke-width"] || attrs["stroke-width"]
10
+ block.call(self) if block_given?
11
+ end
12
+
13
+ def generate(svg)
14
+ svg.g(attributes) do |g|
15
+ @children.each { |child| child.generate(g) }
16
+ end
17
+ end
18
+
19
+ protected
20
+
21
+ def attributes
22
+ attrs = super
23
+ attrs.delete(:children)
24
+ attrs
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ module SVGen
2
+ module Element
3
+ class Line < Base
4
+ def initialize(attrs = {})
5
+ super(attrs)
6
+ @x1 = attrs[:x1] || @x
7
+ @y1 = attrs[:y1] || @y
8
+ @x2 = attrs[:x2] || @x
9
+ @y2 = attrs[:y2] || @y
10
+ end
11
+
12
+ def generate(svg)
13
+ svg.line(attributes)
14
+ end
15
+
16
+ protected
17
+
18
+ def attributes
19
+ attrs = super
20
+ attrs.delete(:x)
21
+ attrs.delete(:y)
22
+ attrs
23
+ end
24
+ end
25
+ end
26
+ end
@@ -5,7 +5,7 @@ module SVGen
5
5
  super(attrs)
6
6
  @width = attrs[:width] || 100
7
7
  @height = attrs[:height] || 100
8
- @fill = attrs[:fill] || "white"
8
+ @fill = attrs[:fill] || "rgba(0, 0, 0, 0)"
9
9
  end
10
10
 
11
11
  def generate(svg)
@@ -0,0 +1,24 @@
1
+ module SVGen
2
+ module Nestable
3
+ def rect(attrs = {})
4
+ @children << Element::Rect.new(attrs)
5
+ end
6
+
7
+ def circle(attrs = {})
8
+ @children << Element::Circle.new(attrs)
9
+ end
10
+
11
+ def text(text, attrs = {})
12
+ @children << Element::Text.new(text, attrs)
13
+ end
14
+
15
+ def line(attrs = {})
16
+ @children << Element::Line.new(attrs)
17
+ end
18
+
19
+ def group(attrs = {}, &block)
20
+ @children << Element::Group.new(attrs, &block)
21
+ end
22
+ alias :g :group
23
+ end
24
+ end
data/lib/svgen/svg.rb CHANGED
@@ -2,23 +2,13 @@ require "builder"
2
2
 
3
3
  module SVGen
4
4
  class SVG
5
- def initialize(attrs = {})
5
+ include Nestable
6
+
7
+ def initialize(attrs = {}, &block)
6
8
  @children = []
7
9
  @width = attrs[:width] || 400
8
10
  @height = attrs[:height] || 300
9
- yield(self) if block_given?
10
- end
11
-
12
- def rect(attrs = {})
13
- @children << Element::Rect.new(attrs)
14
- end
15
-
16
- def circle(attrs = {})
17
- @children << Element::Circle.new(attrs)
18
- end
19
-
20
- def text(text, attrs = {})
21
- @children << Element::Text.new(text, attrs)
11
+ block.call(self) if block_given?
22
12
  end
23
13
 
24
14
  def generate
data/lib/svgen.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  lib = File.dirname(__FILE__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
+ require "svgen/nestable"
4
5
  require "svgen/svg"
5
6
  require "svgen/element/base"
6
7
  require "svgen/element/rect"
7
8
  require "svgen/element/circle"
8
9
  require "svgen/element/text"
10
+ require "svgen/element/line"
11
+ require "svgen/element/group"
@@ -1,5 +1,5 @@
1
1
  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
2
  <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
3
3
  <svg width="600" height="300" xmlns="http://www.w3.org/2000/svg">
4
- <circle x="0" y="0" cx="100" cy="100" r="50" fill="red"/>
4
+ <circle cx="100" cy="100" r="50" fill="red"/>
5
5
  </svg>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
+ <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
3
+ <svg width="600" height="300" xmlns="http://www.w3.org/2000/svg">
4
+ <g stroke="red" stroke-width="5">
5
+ <rect x="0" y="0" width="300" height="200" fill="red"/>
6
+ <circle cx="50" cy="50" r="10" fill="blue"/>
7
+ </g>
8
+ </svg>
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
+ <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
3
+ <svg width="600" height="300" xmlns="http://www.w3.org/2000/svg">
4
+ <line stroke="black" stroke-width="5" x1="10" y1="10" x2="50" y2="50"/>
5
+ </svg>
@@ -32,5 +32,22 @@ describe SVGen::SVG do
32
32
  svg.text("Sample Text", x: 20, y: 20)
33
33
  end
34
34
  end
35
+
36
+ it "returns SVG data with line" do
37
+ @sample = Pathname.new("spec/support/line.svg")
38
+ @svg = SVGen::SVG.new(width: 600, height: 300) do |svg|
39
+ svg.line(x1: 10, y1: 10, x2: 50, y2: 50, stroke: "black", :"stroke-width" => "5")
40
+ end
41
+ end
42
+
43
+ it "returns SVG data with group" do
44
+ @sample = Pathname.new("spec/support/group.svg")
45
+ @svg = SVGen::SVG.new(width: 600, height: 300) do |svg|
46
+ svg.g(stroke: "red", "stroke-width" => 5) do |g|
47
+ g.rect(width: "300", height: "200", fill: "red")
48
+ g.circle(cx: "50", cy: "50", r: "10", fill: "blue")
49
+ end
50
+ end
51
+ end
35
52
  end
36
53
  end
data/svgen.gemspec CHANGED
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "svgen"
6
- spec.version = "0.0.1"
6
+ spec.version = "0.0.2"
7
7
  spec.authors = ["Naoto Kaneko"]
8
8
  spec.email = ["naoty.k@gmail.com"]
9
9
  spec.summary = %q{SVG generator}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svgen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naoto Kaneko
@@ -81,12 +81,17 @@ files:
81
81
  - lib/svgen.rb
82
82
  - lib/svgen/element/base.rb
83
83
  - lib/svgen/element/circle.rb
84
+ - lib/svgen/element/group.rb
85
+ - lib/svgen/element/line.rb
84
86
  - lib/svgen/element/rect.rb
85
87
  - lib/svgen/element/text.rb
88
+ - lib/svgen/nestable.rb
86
89
  - lib/svgen/svg.rb
87
90
  - lib/svgen/version.rb
88
91
  - spec/spec_helper.rb
89
92
  - spec/support/circle.svg
93
+ - spec/support/group.svg
94
+ - spec/support/line.svg
90
95
  - spec/support/rect.svg
91
96
  - spec/support/sample.svg
92
97
  - spec/support/text.svg
@@ -119,6 +124,8 @@ summary: SVG generator
119
124
  test_files:
120
125
  - spec/spec_helper.rb
121
126
  - spec/support/circle.svg
127
+ - spec/support/group.svg
128
+ - spec/support/line.svg
122
129
  - spec/support/rect.svg
123
130
  - spec/support/sample.svg
124
131
  - spec/support/text.svg