victor 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +62 -5
- data/lib/victor/attributes.rb +47 -47
- data/lib/victor/css.rb +49 -38
- data/lib/victor/svg.rb +78 -72
- data/lib/victor/version.rb +1 -1
- metadata +11 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8084c65a136f58070a350341405ef047bc7b3dfd282fec474223598ee7889f12
|
4
|
+
data.tar.gz: 2112d671dadbe7c6e7f1f4a78adad9c6cd24512ba3bdddcd641574e2fd218ad4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c3dc90b9f50060eeb447d1bbe5d54badcf5fff046ee91cea74cc487e0c58f37420e208d1d41b307bc80c4b67a3719f490012417a2deef76d02ba71f144599ab
|
7
|
+
data.tar.gz: 6665e0d722a7c4ffb70497ea884cf83223a5327fe3e68b498b1ea086b0fd9145377f41f7584a62c351886a6e2dfd4c1e834b68cc0243e5af71c24fbca7037318
|
data/README.md
CHANGED
@@ -1,16 +1,32 @@
|
|
1
1
|
Victor - Ruby SVG Image Builder
|
2
2
|
==================================================
|
3
3
|
|
4
|
-
[![Gem](https://
|
5
|
-
[![
|
6
|
-
[![Code
|
7
|
-
[![
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/victor.svg)](https://badge.fury.io/rb/victor)
|
5
|
+
[![Build Status](https://travis-ci.com/DannyBen/victor.svg?branch=master)](https://travis-ci.com/DannyBen/victor)
|
6
|
+
[![Code Quality](https://api.codacy.com/project/badge/Grade/a502c262875643eabb01a43f7f5131ff)](https://www.codacy.com/app/db/victor?utm_source=github.com&utm_medium=referral&utm_content=DannyBen/victor&utm_campaign=Badge_Grade)
|
7
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/85cc05c219d6d233ab78/maintainability)](https://codeclimate.com/github/DannyBen/victor/maintainability)
|
8
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/85cc05c219d6d233ab78/test_coverage)](https://codeclimate.com/github/DannyBen/victor/test_coverage)
|
8
9
|
|
9
10
|
---
|
10
11
|
|
11
12
|
Victor is a direct Ruby-to-SVG builder. All method calls are converted
|
12
13
|
directly to SVG elements.
|
13
14
|
|
15
|
+
![Demo](/animated.gif)
|
16
|
+
|
17
|
+
---
|
18
|
+
|
19
|
+
Table of Contents
|
20
|
+
--------------------------------------------------
|
21
|
+
|
22
|
+
* [Install](#install)
|
23
|
+
* [Examples](#examples)
|
24
|
+
* [Usage](#usage)
|
25
|
+
* [Composite SVG](#composite-svg)
|
26
|
+
* [Saving the Output](#saving-the-output)
|
27
|
+
* [SVG Templates](#svg-templates)
|
28
|
+
* [CSS](#css)
|
29
|
+
|
14
30
|
---
|
15
31
|
|
16
32
|
Install
|
@@ -155,6 +171,47 @@ svg.text "Victor", x: 40, y: 50, font_family: 'arial', font_weight: 'bold', font
|
|
155
171
|
# </text>
|
156
172
|
```
|
157
173
|
|
174
|
+
Composite SVG
|
175
|
+
--------------------------------------------------
|
176
|
+
Victor also supports the abiliy to combine several smaller SVG objects into
|
177
|
+
one using the `<<` operator. This operator expects to receive any object
|
178
|
+
that responds to `#to_s` (can be another `SVG` object).
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
require 'victor'
|
182
|
+
include Victor
|
183
|
+
|
184
|
+
# Create a reusable SVG object
|
185
|
+
frame = SVG.new
|
186
|
+
frame.rect x: 0, y: 0, width: 100, height: 100, fill: '#336'
|
187
|
+
frame.rect x: 10, y: 10, width: 80, height: 80, fill: '#fff'
|
188
|
+
|
189
|
+
# ... and another
|
190
|
+
troll = SVG.new
|
191
|
+
troll.circle cx: 50, cy: 60, r: 24, fill: 'yellow'
|
192
|
+
troll.polygon points: %w[24,50 50,14 76,54], fill: 'red'
|
193
|
+
|
194
|
+
# Combine objects into a single image
|
195
|
+
svg = SVG.new viewBox: '0 0 100 100'
|
196
|
+
svg << frame
|
197
|
+
svg << troll
|
198
|
+
|
199
|
+
# ... and save it
|
200
|
+
svg.save 'framed-troll'
|
201
|
+
```
|
202
|
+
|
203
|
+
Output:
|
204
|
+
|
205
|
+
[![troll](https://cdn.rawgit.com/DannyBen/victor/master/examples/13_composite_svg.svg)](https://cdn.rawgit.com/DannyBen/victor/master/examples/13_composite_svg.svg)
|
206
|
+
|
207
|
+
Another approach to a more modular SVG composition, would be to subclass
|
208
|
+
`Victor::SVG`.
|
209
|
+
|
210
|
+
See the [composite svg example](https://github.com/DannyBen/victor/tree/master/examples#13-composite-svg)
|
211
|
+
or the [subclassing example](https://github.com/DannyBen/victor/tree/master/examples#14-subclassing)
|
212
|
+
for more details.
|
213
|
+
|
214
|
+
|
158
215
|
Saving the Output
|
159
216
|
--------------------------------------------------
|
160
217
|
|
@@ -248,7 +305,7 @@ When the value of the CSS attribute is an array, Victor will simply use
|
|
248
305
|
the values of the array and prefix each of them with the key, so the above
|
249
306
|
will result in two `@import url(...)` rows.
|
250
307
|
|
251
|
-
See the [custom fonts example](https://github.com/DannyBen/victor/tree/master/examples#12-custom-fonts)
|
308
|
+
See the [custom fonts example](https://github.com/DannyBen/victor/tree/master/examples#12-custom-fonts).
|
252
309
|
|
253
310
|
|
254
311
|
---
|
data/lib/victor/attributes.rb
CHANGED
@@ -1,48 +1,48 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
class Attributes
|
6
|
-
attr_reader :attributes
|
7
|
-
|
8
|
-
def initialize(attributes={})
|
9
|
-
@attributes = attributes
|
10
|
-
end
|
11
|
-
|
12
|
-
def to_s
|
13
|
-
mapped = attributes.map do |key, value|
|
14
|
-
key = key.to_s.tr '_', '-'
|
15
|
-
|
16
|
-
if value.is_a? Hash
|
17
|
-
style = Attributes.new(value).to_style
|
18
|
-
"#{key}=\"#{style}\""
|
19
|
-
elsif value.is_a? Array
|
20
|
-
"#{key}=\"#{value.join ' '}\""
|
21
|
-
else
|
22
|
-
"#{key}=\"#{value}\""
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
mapped.join ' '
|
27
|
-
end
|
28
|
-
|
29
|
-
def to_style
|
30
|
-
mapped = attributes.map do |key, value|
|
31
|
-
key = key.to_s.tr '_', '-'
|
32
|
-
"#{key}:#{value}"
|
33
|
-
end
|
34
|
-
|
35
|
-
mapped.join '; '
|
36
|
-
end
|
37
|
-
|
38
|
-
def [](key)
|
39
|
-
attributes[key]
|
40
|
-
end
|
41
|
-
|
42
|
-
def []=(key, value)
|
43
|
-
attributes[key] = value
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
1
|
+
module Victor
|
2
|
+
|
3
|
+
# Handles conversion from a Hash of attributes, to an XML string or
|
4
|
+
# a CSS string.
|
5
|
+
class Attributes
|
6
|
+
attr_reader :attributes
|
7
|
+
|
8
|
+
def initialize(attributes={})
|
9
|
+
@attributes = attributes
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
mapped = attributes.map do |key, value|
|
14
|
+
key = key.to_s.tr '_', '-'
|
15
|
+
|
16
|
+
if value.is_a? Hash
|
17
|
+
style = Attributes.new(value).to_style
|
18
|
+
"#{key}=\"#{style}\""
|
19
|
+
elsif value.is_a? Array
|
20
|
+
"#{key}=\"#{value.join ' '}\""
|
21
|
+
else
|
22
|
+
"#{key}=\"#{value}\""
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
mapped.join ' '
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_style
|
30
|
+
mapped = attributes.map do |key, value|
|
31
|
+
key = key.to_s.tr '_', '-'
|
32
|
+
"#{key}:#{value}"
|
33
|
+
end
|
34
|
+
|
35
|
+
mapped.join '; '
|
36
|
+
end
|
37
|
+
|
38
|
+
def [](key)
|
39
|
+
attributes[key]
|
40
|
+
end
|
41
|
+
|
42
|
+
def []=(key, value)
|
43
|
+
attributes[key] = value
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
48
|
end
|
data/lib/victor/css.rb
CHANGED
@@ -1,39 +1,50 @@
|
|
1
|
-
module Victor
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
1
|
+
module Victor
|
2
|
+
|
3
|
+
# Converts a Hash to a CSS string
|
4
|
+
class CSS
|
5
|
+
attr_reader :attributes
|
6
|
+
|
7
|
+
def initialize(attributes={})
|
8
|
+
@attributes = attributes
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
convert_hash attributes
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def convert_hash(hash, indent=2)
|
18
|
+
return hash unless hash.is_a? Hash
|
19
|
+
|
20
|
+
result = []
|
21
|
+
hash.each do |key, value|
|
22
|
+
key = key.to_s.tr '_', '-'
|
23
|
+
result += css_block(key, value, indent)
|
24
|
+
end
|
25
|
+
|
26
|
+
result.join "\n"
|
27
|
+
end
|
28
|
+
|
29
|
+
def css_block(key, value, indent)
|
30
|
+
result = []
|
31
|
+
|
32
|
+
my_indent = " " * indent
|
33
|
+
|
34
|
+
if value.is_a? Hash
|
35
|
+
result.push "#{my_indent}#{key} {"
|
36
|
+
result.push convert_hash(value, indent+2)
|
37
|
+
result.push "#{my_indent}}"
|
38
|
+
elsif value.is_a? Array
|
39
|
+
value.each do |row|
|
40
|
+
result.push "#{my_indent}#{key} #{row};"
|
41
|
+
end
|
42
|
+
else
|
43
|
+
result.push "#{my_indent}#{key}: #{value};"
|
44
|
+
end
|
45
|
+
|
46
|
+
result
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
39
50
|
end
|
data/lib/victor/svg.rb
CHANGED
@@ -1,73 +1,79 @@
|
|
1
|
-
module Victor
|
2
|
-
|
3
|
-
class
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
svg_attributes
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
File.
|
62
|
-
end
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
1
|
+
module Victor
|
2
|
+
|
3
|
+
# This is the primary Victor class. It handles all the conversion from
|
4
|
+
# ruby to SVG using {#method_missing}
|
5
|
+
class SVG
|
6
|
+
attr_accessor :template, :css
|
7
|
+
attr_reader :content, :svg_attributes
|
8
|
+
|
9
|
+
def initialize(attributes={})
|
10
|
+
@template = attributes.delete(:template) || :default
|
11
|
+
@svg_attributes = Attributes.new attributes
|
12
|
+
svg_attributes[:width] ||= "100%"
|
13
|
+
svg_attributes[:height] ||= "100%"
|
14
|
+
@content = []
|
15
|
+
@css = {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(method_sym, *arguments, &block)
|
19
|
+
element method_sym, *arguments, &block
|
20
|
+
end
|
21
|
+
|
22
|
+
def <<(additional_content)
|
23
|
+
content.push additional_content.to_s
|
24
|
+
end
|
25
|
+
|
26
|
+
def build(&block)
|
27
|
+
self.instance_eval(&block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def element(name, value=nil, attributes={}, &_block)
|
31
|
+
if value.is_a? Hash
|
32
|
+
attributes = value
|
33
|
+
value = nil
|
34
|
+
end
|
35
|
+
|
36
|
+
attributes = Attributes.new attributes
|
37
|
+
|
38
|
+
if block_given? || value
|
39
|
+
content.push "<#{name} #{attributes}".strip + ">"
|
40
|
+
value ? content.push(value) : yield
|
41
|
+
content.push "</#{name}>"
|
42
|
+
else
|
43
|
+
content.push "<#{name} #{attributes}/>"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def render
|
48
|
+
svg_template % {
|
49
|
+
css: CSS.new(css),
|
50
|
+
attributes: svg_attributes,
|
51
|
+
content: content.join("\n")
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_s
|
56
|
+
content.join "\n"
|
57
|
+
end
|
58
|
+
|
59
|
+
def save(filename)
|
60
|
+
filename = "#{filename}.svg" unless filename =~ /\..{2,4}$/
|
61
|
+
File.write filename, render
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def svg_template
|
67
|
+
File.read template_path
|
68
|
+
end
|
69
|
+
|
70
|
+
def template_path
|
71
|
+
if template.is_a? Symbol
|
72
|
+
File.join File.dirname(__FILE__), 'templates', "#{template}.svg"
|
73
|
+
else
|
74
|
+
template
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
73
79
|
end
|
data/lib/victor/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: victor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: runfile
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0.
|
19
|
+
version: '0.10'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0.
|
26
|
+
version: '0.10'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: runfile-tasks
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,42 +44,42 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '3.
|
47
|
+
version: '3.6'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '3.
|
54
|
+
version: '3.6'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: simplecov
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0.
|
61
|
+
version: '0.16'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0.
|
68
|
+
version: '0.16'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: filewatcher
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0
|
75
|
+
version: '1.0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0
|
82
|
+
version: '1.0'
|
83
83
|
description: Build SVG images with ease
|
84
84
|
email: db@dannyben.com
|
85
85
|
executables: []
|
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
114
|
version: '0'
|
115
115
|
requirements: []
|
116
116
|
rubyforge_project:
|
117
|
-
rubygems_version: 2.
|
117
|
+
rubygems_version: 2.7.6
|
118
118
|
signing_key:
|
119
119
|
specification_version: 4
|
120
120
|
summary: SVG Builder
|