ascii_to_svg 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a3e9081f24a286a36328672a54b82cc136b913892929d0b5e1b9fecbac987065
4
+ data.tar.gz: cae5de5779f943b44469be54f07a53a0eeac6a15761d0ac616a8e62b56886f1a
5
+ SHA512:
6
+ metadata.gz: 3267d68802fb3641067260252f9bfa6021979f21d81575ae04eee7cdead8b053e891db0cc93439289c0914e0b208cf00dab098bb27aba11a4ce6d96fba912aef
7
+ data.tar.gz: 6ba8287d253bb07be75c56d5b4fa11e420cea336e6bd6d7b7eef20494c5741d9688482ef73b2bf95088d0e5fda27c579852ded609b2329f72624a130156d2501
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-06-14
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at andreasbanholzer@me.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ascii_to_svg.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rubocop", "~> 1.7"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 a6b8
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,247 @@
1
+ # Ascii To Svg Generator for Ruby
2
+ This module generates beautiful svg images based on a string input.
3
+
4
+ ## Examples
5
+ e
6
+ <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/banners/0.svg">
7
+
8
+ ```ruby
9
+ character_set = [ '-', '/', '|', "\\", '#' ]
10
+ ```
11
+
12
+ <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/banners/3.svg">
13
+
14
+ ```ruby
15
+ character_set = [ '-', '|', '#' ]
16
+ ```
17
+
18
+ <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/banners/4.svg">
19
+
20
+ ```ruby
21
+ character_set = [ '-', '|', '#', 'o', '\\' ]
22
+ ```
23
+
24
+ <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/banners/6.svg">
25
+
26
+ ```ruby
27
+ character_set = [ '|', 'o', '.' ]
28
+ options = {
29
+ style__line__stroke__color: 'brown',
30
+ style__ellipse__stroke__color: 'orange'
31
+ }
32
+ ```
33
+
34
+ <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/banners/7.svg">
35
+
36
+ ```ruby
37
+ character_set = [ '/', '\\' ]
38
+ options = {
39
+ style__line__stroke__color: 'brown',
40
+ style__ellipse__stroke__color: 'orange'
41
+ }
42
+ ```
43
+
44
+ <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/banners/12.svg">
45
+
46
+ ```ruby
47
+ character_set = [ 'x', '.' ]
48
+ ```
49
+
50
+ <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/banners/14.svg">
51
+
52
+ ```ruby
53
+ character_set = [ '|', '.', '-' ]
54
+ options = {
55
+ style__canvas__fill__color: '#0A0C10',
56
+ style__line__stroke__color: '#8D949D'
57
+ }
58
+ ```
59
+
60
+ ## Quickstart
61
+ 1. Install `gem`
62
+ ```ruby
63
+ gem install 'ascii_to_svg'
64
+ ```
65
+
66
+ 2. Run Code
67
+ ```ruby
68
+ require 'ascii_to_svg'
69
+
70
+ # Generate Example String
71
+ ascii = AsciiToSvg.example_string( ['x', 'o'], 256 )
72
+
73
+ # Generate SVG
74
+ svg = AsciiToSvg.from_string( ascii, 16, {} )
75
+
76
+ # => "<svg xmlns=\"http://www.w3.org/2000/svg ...
77
+ ```
78
+
79
+ ## Functions
80
+ ### AsciiToSvg.options<br>
81
+ Output all Parameter
82
+ ```ruby
83
+ AsciiToSvg.options
84
+ # => {:canvas=>{:size=>{:x=>500 ...
85
+ ```
86
+ ### AsciiToSvg.example_string()
87
+
88
+ | **Type** | **Required** | **Description** | **Example** | **Description** |
89
+ |------:|:------|:------|:------|:------|
90
+ | **characters** | ```Array (of single alphabetic character)``` | No | ```[ 'x', 'o' ]``` | Set alphabetic characters for string |
91
+ | **length** | ```Integer``` | No | ```512``` | Lengt of generated String | |
92
+ <br>
93
+
94
+ ```ruby
95
+ one = AsciiToSvg.example_string()
96
+ two = AsciiToSvg.example_string( ['x', 'o' ], 512 )
97
+ ```
98
+
99
+ ### AsciiToSvg.from_string()
100
+
101
+ | | **Type** | **Required** | **Description** |
102
+ |------:|:------|:------|:------|
103
+ | **ascii** | ```String``` | Yes | A String of one or more Characters |
104
+ | **lenght** | ```Integer``` | Yes | Number of Characters in one Line (Row) |
105
+ | **options** | ```Hash``` | No | Modify output. For detailed Information |
106
+ <br>
107
+
108
+ ```ruby
109
+ AsciiToSvg.from_string(
110
+ ascii,
111
+ length,
112
+ options
113
+ )
114
+ ```
115
+
116
+
117
+ ### AsciiToSvg.similar_svg()
118
+
119
+ ```ruby
120
+ original_str = AsciiToSvg.example_string( ['x', 'o'], 256 )
121
+ original_svg = AsciiToSvg.from_string( original_str, 16, {} )
122
+
123
+ copy_str = original_str[ 0, original_str.length-1 ]
124
+ copy_svg = AsciiToSvg.from_string( copy_str, 16, {} )
125
+
126
+ AsciiToSvg.similar_svg( original_svg, copy_svg )
127
+ # => => {:hexdigest1=>"79d5e81d230214749672a1c10e103c46", :hexdigest2=>"f48cacecf2720633dd081c3421d84981", :unique=>false, :score=>0.002799275481640046}
128
+ ```
129
+
130
+ ## Naming
131
+ ```
132
+ Canvas
133
+ -------------------------------------
134
+ | ^
135
+ | | Margin Top
136
+ | Grid v
137
+ | ----------------------------
138
+ | | Cell
139
+ | | ------------------------
140
+ | | | | Offset x |
141
+ | Margin | | Symbol |<-------->|
142
+ | Left | | | |
143
+ | <----> | |------------ |
144
+ | | | ^ |
145
+ | | | | Offset Y |
146
+ | | | V |
147
+ | | ------------------------
148
+ ```
149
+
150
+
151
+
152
+
153
+
154
+ ## Signs
155
+
156
+ | Nr | Sign | Image | SVG Element |
157
+ | :-- | :-- | :-- | :-- |
158
+ | 1 | "\\" | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/3-tl-br.svg"> | Line |
159
+ | 2 | "\|" | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/2-vertical.svg"> | Line |
160
+ | 3 | "/" | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/1-tr-bl.svg"> | Line |
161
+ | 4 | "-" | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/0-minus.svg"> | Line |
162
+ | 5 | "+" | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/4-plus.svg"> | Line |
163
+ | 6 | "x" | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/7-x.svg"> | Line |
164
+ | 7 | "o" | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/8-ellipse.svg"> | Ellipse |
165
+ | 8 | "#" | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/5-rectangle.svg"> | Rectangle |
166
+ | 9 | "." | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/6-empty.svg"> | |
167
+
168
+
169
+
170
+ ## Options
171
+ ### Canvas
172
+ Defines the full width and height.
173
+ | Nr | Name | Key | Default | Type | Description |
174
+ | :-- | :-- | :-- | :-- | :-- | :-- |
175
+ | A.1. | Size X |:canvas__size__x | `500` | Integer | Set width of canvas |
176
+ | A.2. | Margin Left |:canvas__margin__left | `0` | Integer | Set margin on the left side |
177
+ | A.3. | Margin Top |:canvas__margin__top | `0` | Integer | Set margin on the top |
178
+ | A.4. | Margin Right |:canvas__margin__right | `0` | Integer | Set margin on the right |
179
+ | A.5. | Margin Bottom |:canvas__margin__bottom | `0` | Integer | Set margin on the bottom |
180
+
181
+
182
+ ### Cell
183
+ Defines the Area of one symbol including offset.
184
+ | Nr | Name | Key | Default | Type | Description |
185
+ | :-- | :-- | :-- | :-- | :-- | :-- |
186
+ | B.1. | X Offset |:cell__x__offset | `0` | Integer | Define the X (width) offset to the next cell |
187
+ | B.2. | Y Offset |:cell__y__offset | `0` | Integer | Define the Y (height) offset to the next cell |
188
+
189
+
190
+ ### Symbols
191
+ Defines which `char` will be interpreted as "`svg element`"
192
+ | Nr | Name | Key | Default | Type | Image |
193
+ | :-- | :-- | :-- | :-- | :-- | :-- |
194
+ | C.1. | \ |:options__\\\\ | `["\\"]` | Array | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/3-tl-br.svg"> |
195
+ | C.2. | / | :options__/ | `["/"]` | Array | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/1-tr-bl.svg"> |
196
+ | C.3. | X | :options__X | `["X", "x"]` | Array | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/7-x.svg"> |
197
+ | C.4. | - | :options__- | `["-"]` | Array | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/0-minus.svg"> |
198
+ | C.5. | \| | :options__\| | `["\|", "1"]` | Array | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/2-vertical.svg"> |
199
+ | C.6. | O | :options__O | `["O", "o", "0"]` | Array | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/8-ellipse.svg"> |
200
+ | C.7. | + | :options__+ | `["+"]` | Array | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/4-plus.svg"> |
201
+ | C.8. | # | :options__# | `["#"]` | Array | <img src="https://raw.githubusercontent.com/a6b8/a6b8/main/docs/ascii-to-svg-for-ruby/readme/symbols/5-rectangle.svg"> |
202
+
203
+
204
+ ### Style
205
+ Defines all Style Attributes. Styles can only changed by type of the svg element except "Canvas". Under "Symbol" you can find out which Symbol uses which svg Element.
206
+
207
+ [**Line**](#line)
208
+ | Nr | Name | Key | Default | Type | Description |
209
+ | :-- | :-- | :-- | :-- | :-- | :-- |
210
+ | D.1. | Stroke Width |:style__line__stroke__width | `2.0` | Float | Define width of stroke, please notice linecap type for desired behavior |
211
+ | D.2. | Stroke Color |:style__line__stroke__color | `"rgb(0,0,0)"` | String | Define color of stroke in RGB, you can also use HTML Color names or "none" |
212
+ | D.3. | Stroke Opacity |:style__line__stroke__opacity | `1.0` | Float | Define opacity of the stroke, use floating numbers. |
213
+ | D.4. | Stroke Linecap |:style__line__stroke__linecap | `"square"` | String | Defines behavior of line ("butt" / "round" / "sqaure"). Detailed explaination: [developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-linecap)|
214
+
215
+ [**Ellipse**](#ellipse)
216
+ | Nr | Name | Key | Default | Type | Description |
217
+ | :-- | :-- | :-- | :-- | :-- | :-- |
218
+ | D.5. | Stroke Width |:style__ellipse__stroke__width | `2.0` | Float | Define stroke width, use floating numbers |
219
+ | D.6. | Stroke Color |:style__ellipse__stroke__color | `"rgb(0,0,0)"` | String | Define stroke color in RGB, you can also use HTML Color names or "none". |
220
+ | D.7. | Stroke Opacity |:style__ellipse__stroke__opacity | `1.0` | Float | Set stroke opacity, use floating numbers. |
221
+ | D.8. | Stroke Linecap |:style__ellipse__stroke__linecap | `"square"` | String | Defines behavior of ellipse ("butt" / "round" / "sqaure"). Detailed explaination: [developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-linecap)| |
222
+ | D.9. | Fill |:style__ellipse__fill | `"none"` | String | Define color fill of |
223
+
224
+ [**Rectangle**](#rectangle)
225
+ | Nr | Name | Key | Default | Type | Description |
226
+ | :-- | :-- | :-- | :-- | :-- | :-- |
227
+ | D.10. | Fill Color |:style__rectangle__fill__color | `"rgb(0,0,0)"` | String | Define infill color in RGB, you can also use HTML Color names or "none". |
228
+ | D.11. | Fill Opacity |:style__rectangle__fill__opacity | `1.0` | Float | Set infill opacity, use floating numbers. |
229
+
230
+ [**Canvas**](#canvas)
231
+ | Nr | Name | Key | Default | Type | Description |
232
+ | :-- | :-- | :-- | :-- | :-- | :-- |
233
+ | D.12. | Fill Color |:style__canvas__fill__color | `"rgb(255,255,255)"` | String | Define canvas infill color in RGB, you can also use HTML Color names or "none". |
234
+ | D.13. | Fill Opacity |:style__canvas__fill__opacity | `1.0` | Float | Set canvas infill opacity, use floating numbers.|
235
+
236
+
237
+ ## Contributing
238
+
239
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ascii_to_svg. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ascii_to_svg/blob/master/CODE_OF_CONDUCT.md).
240
+
241
+ ## License
242
+
243
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
244
+
245
+ ## Code of Conduct
246
+
247
+ Everyone interacting in the AsciiToSvg project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ascii_to_svg/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ascii_to_svg/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ascii_to_svg"
7
+ spec.version = AsciiToSvg::VERSION
8
+ spec.authors = ["a6b8"]
9
+ spec.email = ["hello@13plu4.com"]
10
+
11
+ spec.summary = "Generative Art based on input Strings."
12
+ spec.description = "Generates beautiful svg vecor images based on a string input"
13
+ spec.homepage = "https://github.com/a6b8/ascii-to-svg-generator-for-ruby"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/a6b8/ascii-to-svg-generator-for-ruby"
21
+ spec.metadata["changelog_uri"] = "https://raw.githubusercontent.com/a6b8/ascii-to-svg-generator-for-ruby/main/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ascii_to_svg"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,410 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ascii_to_svg/version"
4
+ require 'digest'
5
+
6
+
7
+ module AsciiToSvg
8
+ class Error < StandardError; end
9
+
10
+ @TEMPLATE = {
11
+ canvas: {
12
+ size:{
13
+ x: 500,
14
+ y: nil,
15
+ },
16
+ margin: {
17
+ left: 0,
18
+ top: 0,
19
+ right: 0,
20
+ bottom: 0
21
+ }
22
+ },
23
+ grid: {
24
+ x: {
25
+ offset: nil,
26
+ length: nil
27
+ },
28
+ y: {
29
+ offset: nil,
30
+ length: nil
31
+ },
32
+ size: {
33
+ x: nil,
34
+ y: nil
35
+ }
36
+ },
37
+ cell: {
38
+ x: {
39
+ offset: 0
40
+ },
41
+ y: {
42
+ offset: 0
43
+ },
44
+ size: {
45
+ x: nil,
46
+ y: nil
47
+ }
48
+ },
49
+ symbols: {
50
+ "\\": [ "\\" ],
51
+ "/": [ "/" ],
52
+ "X": [ "X", "x" ],
53
+ "-": [ "-" ],
54
+ "|": [ "|", "1" ],
55
+ "O": [ "O", "o", "0" ],
56
+ "+": [ "+" ],
57
+ "#": [ "#" ]
58
+ },
59
+ style: {
60
+ line: {
61
+ stroke: {
62
+ width: 2.0,
63
+ color: 'rgb(0,0,0)',
64
+ opacity: 1.0,
65
+ linecap: 'square' # butt / round / square
66
+ }
67
+ },
68
+ ellipse: {
69
+ stroke: {
70
+ width: 2.0,
71
+ color: 'rgb(0,0,0)',
72
+ opacity: 1.0,
73
+ linecap: 'square'
74
+ },
75
+ fill: 'none'
76
+ },
77
+ rectangle: {
78
+ fill: {
79
+ color: 'rgb(0,0,0)',
80
+ opacity: 1.0
81
+ }
82
+ },
83
+ canvas: {
84
+ fill: {
85
+ color: 'rgb(255,255,255)',
86
+ opacity: 1.0
87
+ }
88
+ }
89
+ }
90
+ }
91
+
92
+ def self.get_options()
93
+ return @TEMPLATE
94
+ end
95
+
96
+
97
+ def self.example_string( characters=[ '-', '/', '|', "\\", '+' ], t=512 )
98
+ return t.times
99
+ .map { | s | characters[ rand( characters.length ) ] }
100
+ .join()
101
+ end
102
+
103
+
104
+ def self.from_string( ascii, _length, vars = {} )
105
+ if self.options_update( vars, true )
106
+ options, lines = self.options_prepare( ascii, _length, vars )
107
+ elements = ''
108
+ for y in 0..options[:grid][:y][:length] - 1
109
+ line = lines[ y ]
110
+ chars = line.split( '' )
111
+ for x in 0..options[:grid][:x][:length] - 1
112
+ char = line[ x ]
113
+ symbols = self.cell_symbols( char, options )
114
+ position = self.cell_position( x, y, options )
115
+ elements += self.cell_svg( symbols, position, options )
116
+ end
117
+ end
118
+ result = self.generate( elements, options )
119
+ else
120
+ end
121
+ end
122
+
123
+
124
+ def self.similar_svg( a, b )
125
+ compare = {}
126
+
127
+ [ [ :a, a ], [ :b, b ] ].each do | str |
128
+ str[ 1 ].gsub!( "\n", '' )
129
+ str[ 1 ].gsub!( ' ', '' )
130
+ compare[ str[ 0 ] ] = str[ 1 ]
131
+ end
132
+
133
+ result = {
134
+ hexdigest1: nil,
135
+ hexdigest2: nil,
136
+ unique: nil,
137
+ score: nil
138
+ }
139
+
140
+ result[:hexdigest1] = self.get_hexdigest( compare[:a] )
141
+ result[:hexdigest2] = self.get_hexdigest( compare[:b] )
142
+ result[:unique] = result[:hexdigest1] == result[:hexdigest2]
143
+ result[:score] = self.str_difference( compare[:a], compare[:b] )
144
+ return result
145
+ end
146
+
147
+
148
+ def self.get_hexdigest( str )
149
+ Digest::MD5.hexdigest str
150
+ end
151
+
152
+
153
+ private
154
+
155
+ def self.str_difference( a, b )
156
+ a = a.to_s.downcase.split( '_' ).join( '' )
157
+ b = b.to_s.downcase.split( '_' ).join( '' )
158
+ longer = [ a.size, b.size ].max
159
+ same = a
160
+ .each_char
161
+ .zip( b.each_char )
162
+ .select { | a, b | a == b }
163
+ .size
164
+ ( longer - same ) / a.size.to_f
165
+ end
166
+
167
+
168
+ def self.options_update( vars, validation )
169
+ allow_list = [
170
+ :canvas__size__x,
171
+ :canvas__margin__left,
172
+ :canvas__margin__top,
173
+ :canvas__margin__right,
174
+ :canvas__margin__bottom,
175
+ :cell__x__offset,
176
+ :cell__y__offset,
177
+ :symbols,
178
+ :style__line__stroke__width,
179
+ :style__line__stroke__color,
180
+ :style__line__stroke__opacity,
181
+ :style__line__stroke__linecap,
182
+ :style__ellipse__stroke__width,
183
+ :style__ellipse__stroke__color,
184
+ :style__ellipse__stroke__opacity,
185
+ :style__ellipse__stroke__linecap,
186
+ :style__ellipse__fill,
187
+ :style__rectangle__fill__color,
188
+ :style__rectangle__fill__opacity,
189
+ :style__canvas__fill__color,
190
+ :style__canvas__fill__opacity
191
+ ]
192
+
193
+ messages = []
194
+ _options = Marshal.load( Marshal.dump( @TEMPLATE ) )
195
+
196
+ vars.keys.each do | key |
197
+ if allow_list.include?( key )
198
+
199
+ keys = key.to_s.split( '__' ).map { | a | a.to_sym }
200
+ case( keys.length )
201
+ when 1
202
+ _options[ keys[ 0 ] ] = vars[ key ]
203
+ when 2
204
+ _options[ keys[ 0 ] ][ keys[ 1 ] ] = vars[ key ]
205
+ when 3
206
+ _options[ keys[ 0 ] ][ keys[ 1 ] ][ keys[ 2 ] ] = vars[ key ]
207
+ when 4
208
+ _options[ keys[ 0 ] ][ keys[ 1 ] ][ keys[ 2 ] ][ keys[ 3 ] ] = vars[ key ]
209
+ end
210
+ else
211
+ nearest = allow_list
212
+ .map { | word | { score: self.str_difference( key, word ), word: word } }
213
+ .min_by { | item | item[:score] }
214
+
215
+ message = "\"#{key}\" is not a valid key, did you mean \"<--similar-->\"?"
216
+ message = message.gsub( '<--similar-->', nearest[:word].to_s )
217
+ messages.push( message )
218
+ end
219
+ end
220
+
221
+ if messages.length != 0
222
+ messages.length == 1 ? puts( 'Error found:' ) : puts( 'Errors found:' )
223
+ messages.each { | m | puts( '- ' + m ) }
224
+ end
225
+ return validation ? messages.length == 0 : _options
226
+ end
227
+
228
+
229
+ def self.options_prepare( ascii, _length, vars )
230
+ options = self.options_update( vars, false )
231
+
232
+ options[:grid][:x][:length] = _length
233
+
234
+ lines = ascii.chars.each_slice( options[:grid][:x][:length] ).map( &:join )
235
+ options[:grid][:y][:length] = lines.length
236
+
237
+ options[:grid][:x][:offset] = options[:canvas][:margin][:left] + options[:cell][:x][:offset] / 2
238
+ options[:grid][:y][:offset] = options[:canvas][:margin][:top] + options[:cell][:x][:offset] / 2
239
+
240
+ tmp = [ :left, :right ].map{ | k | options[:canvas][:margin][ k ] }.sum
241
+ options[:grid][:size][:x] = options[:canvas][:size][:x] - tmp
242
+
243
+ tmp = ( options[:cell][:x][:offset] * options[:grid][:x][:length] )
244
+ options[:cell][:size][:x] = ( options[:grid][:size][:x] - tmp ) / options[:grid][:x][:length]
245
+
246
+ tmp = [ :top, :bottom ].map{ | k | options[:canvas][:margin][ k ] }.sum
247
+ options[:canvas][:size][:y] = ( ( options[:cell][:size][:x] + options[:cell][:x][:offset])* options[:grid][:y][:length] ) + tmp
248
+ options[:grid][:size][:y] = options[:canvas][:size][:y] - tmp
249
+
250
+ tmp = ( options[:cell][:y][:offset] * (options[:grid][:y][:length] ) )
251
+ options[:cell][:size][:y] = ( options[:grid][:size][:y] - tmp ) / options[:grid][:y][:length]
252
+
253
+ return [ options, lines ]
254
+ end
255
+
256
+
257
+ def self.cell_symbols( char, options )
258
+ selector = nil
259
+ options[:symbols].keys.each do | key |
260
+ options[:symbols][ key ].include?( char ) ? selector = key : ''
261
+ end
262
+
263
+ results = []
264
+ case( selector )
265
+ when :"\\"
266
+ results.push( [ 'line', :top__left, :bottom__right ] )
267
+ when :"/"
268
+ results.push( [ 'line', :top__right, :bottom__left ] )
269
+ when :"X"
270
+ results.push( [ 'line', :top__right, :bottom__left ] )
271
+ results.push( [ 'line', :top__left, :bottom__right ] )
272
+ when :"-"
273
+ results.push( [ 'line', :center__left, :center__right ] )
274
+ when :"|"
275
+ results.push( [ 'line', :top__center, :bottom__center ] )
276
+ when :"+"
277
+ results.push( [ 'line', :center__left, :center__right ] )
278
+ results.push( [ 'line', :top__center, :bottom__center ] )
279
+ when :"O"
280
+ results.push( [ 'ellipse', :center__center, nil ] )
281
+ when :"#"
282
+ results.push( [ 'rectangle', :top__left, nil ] )
283
+ end
284
+ return results
285
+ end
286
+
287
+
288
+ def self.cell_position( x, y, options )
289
+ pos = {
290
+ top:{
291
+ left: nil,
292
+ center: nil,
293
+ right: nil
294
+ },
295
+ center: {
296
+ left: nil,
297
+ center: nil,
298
+ right: nil,
299
+ },
300
+ bottom: {
301
+ left: nil,
302
+ center: nil,
303
+ right: nil
304
+ }
305
+ }
306
+
307
+ x = ( x * ( options[:cell][:size][:x] + options[:cell][:x][:offset] ) ) + options[:grid][:x][:offset]
308
+ y = ( y * ( options[:cell][:size][:y] + options[:cell][:y][:offset] ) ) + options[:grid][:y][:offset]
309
+
310
+ half_x = options[:cell][:size][:x] / 2
311
+ half_y = options[:cell][:size][:y] / 2
312
+
313
+ pos[:top][:left] = [ x, y ]
314
+ pos[:top][:center] = [ x + half_x, y ]
315
+ pos[:top][:right] = [ x + options[:cell][:size][:x], y ]
316
+
317
+ pos[:center][:left] = [ x, y + half_y ]
318
+ pos[:center][:center] = [ x + half_x, y + half_y ]
319
+ pos[:center][:right] = [ x + options[:cell][:size][:x], y + half_y ]
320
+
321
+ pos[:bottom][:left] = [ x, y + options[:cell][:size][:y] ]
322
+ pos[:bottom][:center] = [ x + half_y, y + options[:cell][:size][:y] ]
323
+ pos[:bottom][:right] = [ x + options[:cell][:size][:x], y + options[:cell][:size][:y] ]
324
+
325
+ return pos
326
+ end
327
+
328
+
329
+ def self.cell_svg( symbols, position, options )
330
+ str = ''
331
+ symbols.each do | instruction |
332
+ case instruction[ 0 ]
333
+ when 'line'
334
+ keys = instruction
335
+ .drop( 1 )
336
+ .map { | c |
337
+ keys = c
338
+ .to_s.split( '__' )
339
+ .map { | c | c.to_sym }
340
+ }
341
+
342
+ x1, y1 = position[ keys[ 0 ][ 0 ] ][ keys[ 0 ][ 1 ] ]
343
+ x2, y2 = position[ keys[ 1 ][ 0 ] ][ keys[ 1 ][ 1 ] ]
344
+
345
+ s_width = options[:style][:line][:stroke][:width].to_s
346
+ s_color = options[:style][:line][:stroke][:color].to_s
347
+ s_opacity = options[:style][:line][:stroke][:opacity].to_s
348
+ s_linecap = options[:style][:line][:stroke][:linecap].to_s
349
+
350
+ str += "<line x1=\"#{x1}\" y1=\"#{y1}\" x2=\"#{x2}\" y2=\"#{y2}\" style=\"stroke-width: #{s_width};stroke: #{s_color};stroke-opacity: #{s_opacity};stroke-linecap: #{s_linecap};\" transform=\"matrix(1,0,0,1,0,0)\" />"
351
+
352
+ when 'ellipse'
353
+ keys = instruction
354
+ .drop( 1 )
355
+ .map { | c |
356
+ keys = c
357
+ .to_s.split( '__' )
358
+ .map { | c | c }
359
+ }
360
+
361
+ cx, cy = position[ keys[ 0 ][ 0 ].to_sym ][ keys[ 0 ][ 1 ].to_sym ]
362
+ rx = options[:cell][:size][:x] / 2
363
+ ry = options[:cell][:size][:y] / 2
364
+
365
+ s_width = options[:style][:ellipse][:stroke][:width]
366
+ s_color = options[:style][:ellipse][:stroke][:color]
367
+ s_opacity = options[:style][:ellipse][:stroke][:opacity]
368
+ s_linecap = options[:style][:ellipse][:stroke][:linecap]
369
+ e_fill = options[:style][:ellipse][:fill]
370
+ str += "<ellipse cx=\"#{cx}\" cy=\"#{cy}\" rx=\"#{rx}\" ry=\"#{ry}\" style=\"stroke-width: #{s_width};stroke: #{s_color};stroke-opacity: #{s_opacity};stroke-linecap: #{s_linecap};; fill: #{e_fill}\" transform=\"matrix(1,0,0,1,0,0)\" />"
371
+
372
+ when 'rectangle'
373
+ keys = instruction
374
+ .drop( 1 )
375
+ .map { | c |
376
+ keys = c
377
+ .to_s.split( '__' )
378
+ .map { | c | c }
379
+ }
380
+
381
+ x, y = position[ keys[ 0 ][ 0 ].to_sym ][ keys[ 0 ][ 1 ].to_sym ]
382
+ r_fill = options[:style][:rectangle][:fill][:color]
383
+ r_opacity = options[:style][:rectangle][:fill][:opacity]
384
+
385
+ width = options[:cell][:size][:x]
386
+ height = options[:cell][:size][:y]
387
+
388
+ str += "<rect x=\"#{x}\" y=\"#{y}\" width=\"#{width}\" height=\"#{height}\" style=\"fill: #{r_fill}; fill-opacity: #{r_opacity}\" transform=\"matrix(1,0,0,1,0,0)\" />"
389
+ end
390
+ end
391
+ return str
392
+ end
393
+
394
+
395
+ def self.generate( lines, options )
396
+ svg = ''
397
+ width = options[:canvas][:size][:x]
398
+ height = options[:canvas][:size][:y]
399
+ fill = options[:style][:canvas][:fill][:color]
400
+ fill_opacity = options[:style][:canvas][:fill][:opacity].to_s
401
+
402
+ svg += "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:jfreesvg=\"http://www.jfree.org/jfreesvg/svg\" width=\"#{width}\" height=\"#{height}\" text-rendering=\"auto\" shape-rendering=\"auto\">
403
+ <defs></defs>"
404
+ svg += "<rect x=\"0\" y=\"0\" width=\"#{width}\" height=\"#{height}\" style=\"fill: #{fill}; fill-opacity: #{fill_opacity}\" transform=\"matrix(1,0,0,1,0,0)\" />"
405
+ svg += lines.clone
406
+ svg += "</svg>"
407
+ return svg
408
+ end
409
+
410
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AsciiToSvg
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ascii_to_svg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - a6b8
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Generates beautiful svg vecor images based on a string input
14
+ email:
15
+ - hello@13plu4.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - ascii_to_svg.gemspec
29
+ - bin/console
30
+ - bin/setup
31
+ - lib/ascii_to_svg.rb
32
+ - lib/ascii_to_svg/version.rb
33
+ homepage: https://github.com/a6b8/ascii-to-svg-generator-for-ruby
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ allowed_push_host: https://rubygems.org
38
+ homepage_uri: https://github.com/a6b8/ascii-to-svg-generator-for-ruby
39
+ source_code_uri: https://github.com/a6b8/ascii-to-svg-generator-for-ruby
40
+ changelog_uri: https://raw.githubusercontent.com/a6b8/ascii-to-svg-generator-for-ruby/main/CHANGELOG.md
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.4.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.2.3
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Generative Art based on input Strings.
60
+ test_files: []