├── .circleci
└── config.yml
├── .gitignore
├── .rubocop.yml
├── CHANGELOG.md
├── CODE_OF_CONDUCT.md
├── Gemfile
├── Gemfile.lock
├── LICENSE.txt
├── README.md
├── Rakefile
├── ascii_to_svg.gemspec
├── bin
├── console
└── setup
├── lib
├── ascii_to_svg.rb
└── ascii_to_svg
│ └── version.rb
└── test
├── circle_ci.rb
├── copy.svg
├── example.rb
├── examples
├── 0-many-characters.svg
├── 0-many-characters.txt
├── 1-many-characters.svg
├── 1-many-characters.txt
├── 2-squuares.svg
├── 2-squuares.txt
├── 3-offset-2.svg
├── 3-offset-2.txt
├── 3-offset.svg
├── 3-offset.txt
├── 4-offset-minus.svg
├── 4-offset-minus.txt
├── 5-color.svg
├── 5-color.txt
├── 7-banner.svg
└── 7-banner.txt
├── t.rb
├── test-banner.rb
├── test-symbols.rb
├── test-uniquness.rb
└── test.rb
/.circleci/config.yml:
--------------------------------------------------------------------------------
1 | jobs:
2 | build:
3 | docker:
4 | - image: cimg/ruby:3.0.2
5 | steps:
6 | - checkout
7 | - run: bundle install
8 | - run: ruby './test/circle_ci.rb'
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /.bundle/
2 | /.yardoc
3 | /_yardoc/
4 | /coverage/
5 | /doc/
6 | /pkg/
7 | /spec/reports/
8 | /tmp/
9 |
--------------------------------------------------------------------------------
/.rubocop.yml:
--------------------------------------------------------------------------------
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
14 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ## [Unreleased]
2 |
3 | ## [0.1.0] - 2021-06-14
4 |
5 | - Initial release
6 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
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.
85 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
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"
11 |
12 | gem "digest", "~> 3.0.0"
13 |
--------------------------------------------------------------------------------
/Gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: .
3 | specs:
4 | ascii_to_svg (0.1.4)
5 |
6 | GEM
7 | remote: https://rubygems.org/
8 | specs:
9 | ast (2.4.2)
10 | digest (3.0.0)
11 | parallel (1.21.0)
12 | parser (3.1.0.0)
13 | ast (~> 2.4.1)
14 | rainbow (3.0.0)
15 | rake (13.0.6)
16 | regexp_parser (2.2.0)
17 | rexml (3.2.5)
18 | rubocop (1.24.1)
19 | parallel (~> 1.10)
20 | parser (>= 3.0.0.0)
21 | rainbow (>= 2.2.2, < 4.0)
22 | regexp_parser (>= 1.8, < 3.0)
23 | rexml
24 | rubocop-ast (>= 1.15.1, < 2.0)
25 | ruby-progressbar (~> 1.7)
26 | unicode-display_width (>= 1.4.0, < 3.0)
27 | rubocop-ast (1.15.1)
28 | parser (>= 3.0.1.1)
29 | ruby-progressbar (1.11.0)
30 | unicode-display_width (2.1.0)
31 |
32 | PLATFORMS
33 | x86_64-darwin-20
34 |
35 | DEPENDENCIES
36 | ascii_to_svg!
37 | digest (~> 3.0.0)
38 | rake (~> 13.0)
39 | rubocop (~> 1.7)
40 |
41 | BUNDLED WITH
42 | 2.2.33
43 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
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.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | This module generates beautiful svg images based on a input string.
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | ```ruby
18 | character_set = [ '-', '/', '|', "\", '#' ]
19 | ```
20 |
21 |
22 |
23 | ```ruby
24 | character_set = [ '-', '|', '#' ]
25 | ```
26 |
27 |
28 |
29 | ```ruby
30 | character_set = [ '-', '|', '#', 'o', '\' ]
31 | ```
32 |
33 |
34 |
35 | ```ruby
36 | character_set = [ '|', 'o', '.' ]
37 | options = {
38 | style__line__stroke__color: 'brown',
39 | style__ellipse__stroke__color: 'orange'
40 | }
41 | ```
42 |
43 |
44 |
45 | ```ruby
46 | character_set = [ '/', '\' ]
47 | options = {
48 | style__line__stroke__color: 'brown',
49 | style__ellipse__stroke__color: 'orange'
50 | }
51 | ```
52 |
53 |
54 |
55 | ```ruby
56 | character_set = [ 'x', '.' ]
57 | ```
58 |
59 |
60 |
61 | ```ruby
62 | character_set = [ '|', '.', '-' ]
63 | options = {
64 | style__canvas__fill__color: '#0A0C10',
65 | style__line__stroke__color: '#8D949D'
66 | }
67 | ```
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 | 1. [Examples](#examples)
77 | 2. [Quickstart](#quickstart)
78 | 3. [Functions](#functions)
79 | 4. [Naming](#naming)
80 | 5. [Symbols](#symbols-heading)
81 | 6. [Options](#options)
82 | 7. [Contributing](#contributing)
83 | 8. [Limitations](#limitations)
84 | 9. [Credits](#Credits)
85 | 10. [License](#license)
86 | 11. [Code of Conduct](#code-of-conduct)
87 | 12. [Support my Work](#support-my-work)
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 | 1. Install `gem`
97 | ```ruby
98 | gem install 'ascii_to_svg'
99 | ```
100 |
101 | 2. Run Code
102 | ```ruby
103 | require 'ascii_to_svg'
104 |
105 | # Generate Example String
106 | ascii = AsciiToSvg.example_string( [ 'x', 'o' ], 256 )
107 |
108 | # Generate SVG
109 | svg = AsciiToSvg.from_string( ascii, 16, {} )
110 |
111 | # => "
115 |
116 |
117 |
118 |
119 |
120 | Add this line to your application's Gemfile:
121 |
122 | ```ruby
123 | gem 'ascii_to_svg'
124 | ```
125 |
126 | And then execute:
127 |
128 | $ bundle install
129 |
130 | Or install it yourself as:
131 |
132 | $ gem install ascii_to_svg
133 |
134 |
135 | On Rubygems:
136 | - Gem: https://rubygems.org/gems/ascii_to_svg
137 | - Profile: https://rubygems.org/profiles/a6b8
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 | ### AsciiToSvg.options
146 | Output all Parameter
147 | ```ruby
148 | AsciiToSvg.options
149 | # => {:canvas=>{:size=>{:x=>500 ...
150 | ```
151 | ### AsciiToSvg.example_string()
152 |
153 | | **Type** | **Required** | **Description** | **Example** | **Description** |
154 | |------:|:------|:------|:------|:------|
155 | | **characters** | ```Array (of single alphabetic character)``` | No | ```[ 'x', 'o' ]``` | Set alphabetic characters for string |
156 | | **length** | ```Integer``` | No | ```512``` | Lengt of generated String | |
157 |
158 |
159 | ```ruby
160 | one = AsciiToSvg.example_string()
161 | two = AsciiToSvg.example_string( [ 'x', 'o' ], 512 )
162 | ```
163 |
164 | ### AsciiToSvg.from_string()
165 |
166 | | | **Type** | **Required** | **Description** |
167 | |------:|:------|:------|:------|
168 | | **ascii** | ```String``` | Yes | A String of one or more Characters |
169 | | **lenght** | ```Integer``` | Yes | Number of Characters in one Line (Row) |
170 | | **options** | ```Hash``` | No | Modify output. For detailed Information |
171 |
172 |
173 | ```ruby
174 | AsciiToSvg.from_string(
175 | ascii,
176 | length,
177 | options
178 | )
179 | ```
180 |
181 |
182 | ### AsciiToSvg.similar_svg()
183 |
184 | ```ruby
185 | original_str = AsciiToSvg.example_string( [ 'x', 'o' ], 256 )
186 | original_svg = AsciiToSvg.from_string( original_str, 16, {} )
187 |
188 | copy_str = original_str[ 0, original_str.length - 1 ]
189 | copy_svg = AsciiToSvg.from_string( copy_str, 16, {} )
190 |
191 | AsciiToSvg.similar_svg( original_svg, copy_svg )
192 | # => => {:hexdigest1=>"79d5e81d230214749672a1c10e103c46", :hexdigest2=>"f48cacecf2720633dd081c3421d84981", :unique=>false, :score=>0.002799275481640046}
193 | ```
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 | ```
202 | CANVAS
203 | -------------------------------------
204 | | ^
205 | | | Margin Top
206 | | GRID v
207 | | ----------------------------
208 | | | CELL
209 | | | ------------------------
210 | | | | | Offset x |
211 | | Margin | | Symbol |<-------->|
212 | | Left | | | |
213 | | <----> | |------------ |
214 | | | | ^ |
215 | | | | | Offset Y |
216 | | | | V |
217 | | | ------------------------
218 | ```
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 | | Nr | Sign | Image | SVG Element |
227 | | :-- | :-- | :-- | :-- |
228 | | 1 | "\\" | | Line |
229 | | 2 | "\|" | | Line |
230 | | 3 | "/" | | Line |
231 | | 4 | "-" | | Line |
232 | | 5 | "+" | | Line |
233 | | 6 | "x" | | Line |
234 | | 7 | "o" | | Ellipse |
235 | | 8 | "#" | | Rectangle |
236 | | 9 | "." | | |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 | ### Canvas
245 | Defines the full width and height.
246 | | Nr | Name | Key | Default | Type | Description |
247 | | :-- | :-- | :-- | :-- | :-- | :-- |
248 | | A.1. | Size X |:canvas__size__x | `500` | Integer | Set width of canvas |
249 | | A.2. | Margin Left |:canvas__margin__left | `0` | Integer | Set margin on the left side |
250 | | A.3. | Margin Top |:canvas__margin__top | `0` | Integer | Set margin on the top |
251 | | A.4. | Margin Right |:canvas__margin__right | `0` | Integer | Set margin on the right |
252 | | A.5. | Margin Bottom |:canvas__margin__bottom | `0` | Integer | Set margin on the bottom |
253 |
254 |
255 | ### Cell
256 | Defines the Area of one symbol including offset.
257 | | Nr | Name | Key | Default | Type | Description |
258 | | :-- | :-- | :-- | :-- | :-- | :-- |
259 | | B.1. | X Offset |:cell__x__offset | `0` | Integer | Define the X (width) offset to the next cell |
260 | | B.2. | Y Offset |:cell__y__offset | `0` | Integer | Define the Y (height) offset to the next cell |
261 |
262 |
263 | ### Symbols
264 | Defines which `char` will be interpreted as "`svg element`"
265 | | Nr | Name | Key | Default | Type | Image |
266 | | :-- | :-- | :-- | :-- | :-- | :-- |
267 | | C.1. | \ |:"symbols__\\\\" | `[ "\\" ]` | Array | |
268 | | C.2. | / | :"symbols__/" | `[ "/" ]` | Array | |
269 | | C.3. | X | :"symbols__X" | `[ "X", "x" ]` | Array | |
270 | | C.4. | - | :"symbols__-" | `[ "-" ]` | Array | |
271 | | C.5. | \| | :"symbols__\" | | `[ "\|", "1" ]` | Array | |
272 | | C.6. | O | :"symbols__O" | `[ "O", "o", "0" ]` | Array | |
273 | | C.7. | + | :"symbols__+" | `[ "+" ]` | Array | |
274 | | C.8. | # | :"symbols__#" | `[ "#" ]` | Array | |
275 |
276 |
277 | ### Style
278 | 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.
279 |
280 | [**Line**](#line)
281 | | Nr | Name | Key | Default | Type | Description |
282 | | :-- | :-- | :-- | :-- | :-- | :-- |
283 | | D.1. | Stroke Width |:style__line__stroke__width | `2.0` | Float | Define width of stroke, please notice linecap type for desired behavior |
284 | | 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" |
285 | | D.3. | Stroke Opacity |:style__line__stroke__opacity | `1.0` | Float | Define opacity of the stroke, use floating numbers. |
286 | | 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)|
287 |
288 | [**Ellipse**](#ellipse)
289 | | Nr | Name | Key | Default | Type | Description |
290 | | :-- | :-- | :-- | :-- | :-- | :-- |
291 | | D.5. | Stroke Width |:style__ellipse__stroke__width | `2.0` | Float | Define stroke width, use floating numbers |
292 | | 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". |
293 | | D.7. | Stroke Opacity |:style__ellipse__stroke__opacity | `1.0` | Float | Set stroke opacity, use floating numbers. |
294 | | 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)| |
295 | | D.9. | Fill |:style__ellipse__fill | `"none"` | String | Define color fill of |
296 |
297 | [**Rectangle**](#rectangle)
298 | | Nr | Name | Key | Default | Type | Description |
299 | | :-- | :-- | :-- | :-- | :-- | :-- |
300 | | 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". |
301 | | D.11. | Fill Opacity |:style__rectangle__fill__opacity | `1.0` | Float | Set infill opacity, use floating numbers. |
302 |
303 | [**Canvas**](#canvas)
304 | | Nr | Name | Key | Default | Type | Description |
305 | | :-- | :-- | :-- | :-- | :-- | :-- |
306 | | 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". |
307 | | D.13. | Fill Opacity |:style__canvas__fill__opacity | `1.0` | Float | Set canvas infill opacity, use floating numbers.|
308 |
309 |
310 |
311 |
312 |
313 |
314 | Bug reports and pull requests are welcome on GitHub at https://github.com/a6b8/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/a6b8/ascii_to_svg/blob/master/CODE_OF_CONDUCT.md).
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 | - Proof of Concept, not battle-tested.
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 | 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/a6b8/ascii_to_svg/blob/master/CODE_OF_CONDUCT.md).
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 | Please ⭐️ star this Project, every ⭐️ star makes us very happy!
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
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
9 |
--------------------------------------------------------------------------------
/ascii_to_svg.gemspec:
--------------------------------------------------------------------------------
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 a input string."
12 | spec.description = "Generates beautiful svg vecor images based on a input string"
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
38 |
--------------------------------------------------------------------------------
/bin/console:
--------------------------------------------------------------------------------
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__)
16 |
--------------------------------------------------------------------------------
/bin/setup:
--------------------------------------------------------------------------------
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
9 |
--------------------------------------------------------------------------------
/lib/ascii_to_svg.rb:
--------------------------------------------------------------------------------
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 | :"symbols__/",
179 | :"symbols__X",
180 | :"symbols__-",
181 | :"symbols__|",
182 | :"symbols__O",
183 | :"symbols__+",
184 | :"symbols__#",
185 | :style__line__stroke__width,
186 | :style__line__stroke__color,
187 | :style__line__stroke__opacity,
188 | :style__line__stroke__linecap,
189 | :style__ellipse__stroke__width,
190 | :style__ellipse__stroke__color,
191 | :style__ellipse__stroke__opacity,
192 | :style__ellipse__stroke__linecap,
193 | :style__ellipse__fill,
194 | :style__rectangle__fill__color,
195 | :style__rectangle__fill__opacity,
196 | :style__canvas__fill__color,
197 | :style__canvas__fill__opacity
198 | ]
199 |
200 | messages = []
201 | _options = Marshal.load( Marshal.dump( @TEMPLATE ) )
202 |
203 | vars.keys.each do | key |
204 | if allow_list.include?( key )
205 |
206 | keys = key.to_s.split( '__' ).map { | a | a.to_sym }
207 | case( keys.length )
208 | when 1
209 | _options[ keys[ 0 ] ] = vars[ key ]
210 | when 2
211 | _options[ keys[ 0 ] ][ keys[ 1 ] ] = vars[ key ]
212 | when 3
213 | _options[ keys[ 0 ] ][ keys[ 1 ] ][ keys[ 2 ] ] = vars[ key ]
214 | when 4
215 | _options[ keys[ 0 ] ][ keys[ 1 ] ][ keys[ 2 ] ][ keys[ 3 ] ] = vars[ key ]
216 | end
217 | else
218 | nearest = allow_list
219 | .map { | word | { score: self.str_difference( key, word ), word: word } }
220 | .min_by { | item | item[:score] }
221 |
222 | message = "\"#{key}\" is not a valid key, did you mean \"<--similar-->\"?"
223 | message = message.gsub( '<--similar-->', nearest[:word].to_s )
224 | messages.push( message )
225 | end
226 | end
227 |
228 | if messages.length != 0
229 | messages.length == 1 ? puts( 'Error found:' ) : puts( 'Errors found:' )
230 | messages.each { | m | puts( '- ' + m ) }
231 | end
232 | return validation ? messages.length == 0 : _options
233 | end
234 |
235 |
236 | def self.options_prepare( ascii, _length, vars )
237 | options = self.options_update( vars, false )
238 |
239 | options[:grid][:x][:length] = _length
240 |
241 | lines = ascii.chars.each_slice( options[:grid][:x][:length] ).map( &:join )
242 | options[:grid][:y][:length] = lines.length
243 |
244 | options[:grid][:x][:offset] = options[:canvas][:margin][:left] + options[:cell][:x][:offset] / 2
245 | options[:grid][:y][:offset] = options[:canvas][:margin][:top] + options[:cell][:x][:offset] / 2
246 |
247 | tmp = [ :left, :right ].map{ | k | options[:canvas][:margin][ k ] }.sum
248 | options[:grid][:size][:x] = options[:canvas][:size][:x] - tmp
249 |
250 | tmp = ( options[:cell][:x][:offset] * options[:grid][:x][:length] )
251 | options[:cell][:size][:x] = ( options[:grid][:size][:x] - tmp ) / options[:grid][:x][:length]
252 |
253 | tmp = [ :top, :bottom ].map{ | k | options[:canvas][:margin][ k ] }.sum
254 | options[:canvas][:size][:y] = ( ( options[:cell][:size][:x] + options[:cell][:x][:offset])* options[:grid][:y][:length] ) + tmp
255 | options[:grid][:size][:y] = options[:canvas][:size][:y] - tmp
256 |
257 | tmp = ( options[:cell][:y][:offset] * (options[:grid][:y][:length] ) )
258 | options[:cell][:size][:y] = ( options[:grid][:size][:y] - tmp ) / options[:grid][:y][:length]
259 |
260 | return [ options, lines ]
261 | end
262 |
263 |
264 | def self.cell_symbols( char, options )
265 | selector = nil
266 | options[:symbols].keys.each do | key |
267 | options[:symbols][ key ].include?( char ) ? selector = key : ''
268 | end
269 |
270 | results = []
271 | case( selector )
272 | when :"\\"
273 | results.push( [ 'line', :top__left, :bottom__right ] )
274 | when :"/"
275 | results.push( [ 'line', :top__right, :bottom__left ] )
276 | when :"X"
277 | results.push( [ 'line', :top__right, :bottom__left ] )
278 | results.push( [ 'line', :top__left, :bottom__right ] )
279 | when :"-"
280 | results.push( [ 'line', :center__left, :center__right ] )
281 | when :"|"
282 | results.push( [ 'line', :top__center, :bottom__center ] )
283 | when :"+"
284 | results.push( [ 'line', :center__left, :center__right ] )
285 | results.push( [ 'line', :top__center, :bottom__center ] )
286 | when :"O"
287 | results.push( [ 'ellipse', :center__center, nil ] )
288 | when :"#"
289 | results.push( [ 'rectangle', :top__left, nil ] )
290 | end
291 | return results
292 | end
293 |
294 |
295 | def self.cell_position( x, y, options )
296 | pos = {
297 | top:{
298 | left: nil,
299 | center: nil,
300 | right: nil
301 | },
302 | center: {
303 | left: nil,
304 | center: nil,
305 | right: nil,
306 | },
307 | bottom: {
308 | left: nil,
309 | center: nil,
310 | right: nil
311 | }
312 | }
313 |
314 | x = ( x * ( options[:cell][:size][:x] + options[:cell][:x][:offset] ) ) + options[:grid][:x][:offset]
315 | y = ( y * ( options[:cell][:size][:y] + options[:cell][:y][:offset] ) ) + options[:grid][:y][:offset]
316 |
317 | half_x = options[:cell][:size][:x] / 2
318 | half_y = options[:cell][:size][:y] / 2
319 |
320 | pos[:top][:left] = [ x, y ]
321 | pos[:top][:center] = [ x + half_x, y ]
322 | pos[:top][:right] = [ x + options[:cell][:size][:x], y ]
323 |
324 | pos[:center][:left] = [ x, y + half_y ]
325 | pos[:center][:center] = [ x + half_x, y + half_y ]
326 | pos[:center][:right] = [ x + options[:cell][:size][:x], y + half_y ]
327 |
328 | pos[:bottom][:left] = [ x, y + options[:cell][:size][:y] ]
329 | pos[:bottom][:center] = [ x + half_y, y + options[:cell][:size][:y] ]
330 | pos[:bottom][:right] = [ x + options[:cell][:size][:x], y + options[:cell][:size][:y] ]
331 |
332 | return pos
333 | end
334 |
335 |
336 | def self.cell_svg( symbols, position, options )
337 | str = ''
338 | symbols.each do | instruction |
339 | case instruction[ 0 ]
340 | when 'line'
341 | keys = instruction
342 | .drop( 1 )
343 | .map { | c |
344 | keys = c
345 | .to_s.split( '__' )
346 | .map { | c | c.to_sym }
347 | }
348 |
349 | x1, y1 = position[ keys[ 0 ][ 0 ] ][ keys[ 0 ][ 1 ] ]
350 | x2, y2 = position[ keys[ 1 ][ 0 ] ][ keys[ 1 ][ 1 ] ]
351 |
352 | s_width = options[:style][:line][:stroke][:width].to_s
353 | s_color = options[:style][:line][:stroke][:color].to_s
354 | s_opacity = options[:style][:line][:stroke][:opacity].to_s
355 | s_linecap = options[:style][:line][:stroke][:linecap].to_s
356 |
357 | str += " "
358 |
359 | when 'ellipse'
360 | keys = instruction
361 | .drop( 1 )
362 | .map { | c |
363 | keys = c
364 | .to_s.split( '__' )
365 | .map { | c | c }
366 | }
367 |
368 | cx, cy = position[ keys[ 0 ][ 0 ].to_sym ][ keys[ 0 ][ 1 ].to_sym ]
369 | rx = options[:cell][:size][:x] / 2
370 | ry = options[:cell][:size][:y] / 2
371 |
372 | s_width = options[:style][:ellipse][:stroke][:width]
373 | s_color = options[:style][:ellipse][:stroke][:color]
374 | s_opacity = options[:style][:ellipse][:stroke][:opacity]
375 | s_linecap = options[:style][:ellipse][:stroke][:linecap]
376 | e_fill = options[:style][:ellipse][:fill]
377 | str += " "
378 |
379 | when 'rectangle'
380 | keys = instruction
381 | .drop( 1 )
382 | .map { | c |
383 | keys = c
384 | .to_s.split( '__' )
385 | .map { | c | c }
386 | }
387 |
388 | x, y = position[ keys[ 0 ][ 0 ].to_sym ][ keys[ 0 ][ 1 ].to_sym ]
389 | r_fill = options[:style][:rectangle][:fill][:color]
390 | r_opacity = options[:style][:rectangle][:fill][:opacity]
391 |
392 | width = options[:cell][:size][:x]
393 | height = options[:cell][:size][:y]
394 |
395 | str += " "
396 | end
397 | end
398 | return str
399 | end
400 |
401 |
402 | def self.generate( lines, options )
403 | svg = ''
404 | width = options[:canvas][:size][:x]
405 | height = options[:canvas][:size][:y]
406 | fill = options[:style][:canvas][:fill][:color]
407 | fill_opacity = options[:style][:canvas][:fill][:opacity].to_s
408 |
409 | svg += "
410 | "
411 | svg += " "
412 | svg += lines.clone
413 | svg += " "
414 | return svg
415 | end
416 |
417 | end
418 |
--------------------------------------------------------------------------------
/lib/ascii_to_svg/version.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | module AsciiToSvg
4 | VERSION = "0.1.4"
5 | end
6 |
--------------------------------------------------------------------------------
/test/circle_ci.rb:
--------------------------------------------------------------------------------
1 | require './lib/ascii_to_svg'
2 |
3 | sets = [
4 | {
5 | test: 'Draw symbols',
6 | characters: [ '-', '/', '|', "\\", '+' ],
7 | times: 512,
8 | chars: 32,
9 | filename: '0-many-characters',
10 | params: {}
11 | },
12 | {
13 | test: 'Change default canvas size',
14 | characters: [ '-', '/', '|', "\\", '+', '#', '.' ],
15 | times: 512,
16 | chars: 16,
17 | filename: '1-many-characters',
18 | params: { canvas__size__x: 200 }
19 | },
20 | {
21 | test: 'Draw squares',
22 | characters: [ '#' ],
23 | times: 256,
24 | chars: 16,
25 | filename: '2-squuares',
26 | params: {}
27 | },
28 | {
29 | test: 'Draw squares with offset',
30 | characters: [ 'o' ],
31 | times: 256,
32 | chars: 16,
33 | filename: '3-offset',
34 | params: { cell__x__offset: 6, cell__y__offset: 6, }
35 | },
36 | {
37 | test: 'Draw squares with offset',
38 | characters: [ 'x' ],
39 | times: 256,
40 | chars: 16,
41 | filename: '3-offset-2',
42 | params: { canvas__size__x: 300, cell__x__offset: 8, cell__y__offset: 8, }
43 | },
44 | {
45 | test: 'Test minus cell offset values',
46 | characters: [ 'X', '#' ],
47 | times: 256,
48 | chars: 16,
49 | filename: '4-offset-minus',
50 | params: { cell__x__offset: -2, cell__y__offset: -2 }
51 | },
52 | {
53 | test: 'Color and Opacity Test',
54 | characters: [ '/', "#", "o" ],
55 | times: 256,
56 | chars: 16,
57 | filename: '5-color',
58 | params: {
59 | style__line__stroke__color: 'rgb(50,100,150)',
60 | style__ellipse__stroke__color: 'rgb(150,200,250)',
61 | style__rectangle__fill__color: 'rgb(250,300,50)',
62 | style__rectangle__fill__opacity: 0.2
63 | }
64 | },
65 | {
66 | test: 'Banner',
67 | characters: [ '-', '/', '|', "\\", '+' ],
68 | times: 128,
69 | chars: 16,
70 | filename: '7-banner',
71 | params: { cell__x__offset: -2, cell__y__offset: -2 }
72 | }
73 | ]
74 |
75 | puts 'COMPARE OUTPUT'
76 | results = []
77 | folder = Dir.pwd + '/test/examples/'
78 | sets.each.with_index do | set, index |
79 | p = folder + set[:filename] + '.txt'
80 | ascii = File.read( p )
81 | svg = AsciiToSvg.from_string( ascii, set[:chars], set[:params] )
82 |
83 | p = folder + set[:filename] + '.svg'
84 | original = File.read( p )
85 | result = AsciiToSvg.similar_svg( original, svg )
86 |
87 | space = ( 25 - set[:filename].length ).times.map { | a | '' }.join( ' ' )
88 | puts " [#{index}] #{set[:filename]}#{space}#{result[:unique]}"
89 | results.push( result[:unique] )
90 | end
91 |
92 | puts
93 | if results.all?
94 | puts " All tests passed."
95 | exit 0
96 | else
97 | puts " Not all tests passed."
98 | exit 1
99 | end
--------------------------------------------------------------------------------
/test/copy.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/test/example.rb:
--------------------------------------------------------------------------------
1 | require 'ascii_to_svg'
2 |
3 | # Generate Example String
4 | ascii = AsciiToSvg.example_string( ['x', 'o'], 256 )
5 |
6 | # Generate SVG
7 | svg = AsciiToSvg.from_string( ascii, 16, {} )
8 |
9 | # =>
--------------------------------------------------------------------------------
/test/examples/0-many-characters.txt:
--------------------------------------------------------------------------------
1 | \/-|/++/||--\\+-+|--+\+/|+/\||\+|//--//|-///+-\|\/|-|-\/||||/\\/\-/|+\++|++++|\/+|\|//-////+/\-+|/||+-|++\-+/+++++-/-\+/\|\-++||\|\/\/\|++\-\-//+|+-\/-\+|\-+\+|||||/++-\|/\-\|\/+---+\+\/+\+\+/-\+-//+-//\-|/|\\/-/|\+|+-\||//-/+\+\-//|+|-+|\|/+-/-|\\+-|+++/+\\/---|/+\+\//-|-/|+\+\|+/|+\+\||/+|/\+\++|-/|--\|/\-|-\/+/|+||+--|//-++//\/\\|+|--\/|-\\+\/-\|-\/-++++\|-/-+|||--\\--|//|/-+++||--|///-|/|\/-+++|--\|-+/-\|///+--/+\\/-/--\+\-+-+//\-/+\|++||+--/+/|\-|\|/\/+-\+|++|+|-\/++|\+\||++\-|+-//+------+\-/++/\|-|++-
--------------------------------------------------------------------------------
/test/examples/1-many-characters.txt:
--------------------------------------------------------------------------------
1 | +\/.\-|\\#|/|/-|.|/#|---.|-.#+#+|.-.-+-|.-+#-#+.#//-#//#\#\\||\/--|-.||||-\\--#+##\-\.+/|-/.+/-/|#.|..\+#.\\--/./|#||+#\|./\/+.\||-|--.|-+.+|-\/-.#-|+..++.#-\|+|./\.|/-|\-+##\.-|+\-.#\.+|\#-\++#\/|/+-|+#-//#+\./.|+.#+.+#+/|\\+-#-.-\/.+|#/##.+|..|/-+++|-||#|/+++///../#.##\|\+++||/-\\--|\/.++|-.-#+#|.+/..\#\||.//-.//\/#\.|+--+.+|.+#++#|\+/#+/-#+/||-\+-#.\/##+#\.+#\+\.+\-\/-|+#//-\\#\\.++|.\+\+\|-#-\.+//+./--|#+|.+.-|/|+\/|\++#-#-.+|+--/.-##-.+-+#|-#///#-|\-\\#|.#/#+/-|\/-//-#/\#-||\-+/|.+/-./#|-/\##\|#..-\++|
--------------------------------------------------------------------------------
/test/examples/2-squuares.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/test/examples/2-squuares.txt:
--------------------------------------------------------------------------------
1 | ################################################################################################################################################################################################################################################################
--------------------------------------------------------------------------------
/test/examples/3-offset-2.txt:
--------------------------------------------------------------------------------
1 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
--------------------------------------------------------------------------------
/test/examples/3-offset.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/test/examples/3-offset.txt:
--------------------------------------------------------------------------------
1 | oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
--------------------------------------------------------------------------------
/test/examples/4-offset-minus.txt:
--------------------------------------------------------------------------------
1 | ###X#XX#XXXX###XX#XXX#X#XX#X#X#XX##XX#XXXXX#X#XX#XXX##XXX###X##XXX#XXXXX#X#X####XXX#X##XX#XX#######X####XXXX##X#XXX##XXXX#X#X###X#XXX#X#X##XXXXX#XX##X####X#X#X##X#XX#X#XXX#XX######XX#XXX###XXXXX###XX#XXXXX###XX#XXXX####X##X##X##XX#XX#######XXX###XX#XX#X###
--------------------------------------------------------------------------------
/test/examples/5-color.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/test/examples/5-color.txt:
--------------------------------------------------------------------------------
1 | /#/ooo//////o/##/o/o#o/#ooo//ooo##o//#/oo/ooo#o/#//o#//#o//#o//#o#ooooo#ooo##o##/ooo#/##/o#/oo#//o/o///o###o//o///#oooo/o#o##oo##o#o#oo##o//o#/##/o#o####/o##/oo#/###/#/##oo//oo##//oo/#/o##oo/oo#o/o/o///#o/#////#//#/o#o/#//////#o/oooo/ooo#/o/#o//#//#o#o///#
--------------------------------------------------------------------------------
/test/examples/7-banner.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/test/examples/7-banner.txt:
--------------------------------------------------------------------------------
1 | /\\/-//+/|-|\/-|/|||\|\-|-\+/\+\-/+|+/+|++|||--|+\|++\/-||\++//+-\/\\+\+///\//\||\-||\\\-+\+-\+\-||--/-+||+|--\--||\||-\/||/+/-/
--------------------------------------------------------------------------------
/test/t.rb:
--------------------------------------------------------------------------------
1 | require '../lib/ascii_to_svg'
2 |
3 | AsciiToSvg.from_string( "#.#.#", 5, {})
--------------------------------------------------------------------------------
/test/test-banner.rb:
--------------------------------------------------------------------------------
1 | require '../lib/ascii_to_svg'
2 |
3 | sets = [
4 | {
5 | test: 'Draw symbols',
6 | characters: [ '-', '/', '|', "\\", '#' ],
7 | times: 160,
8 | chars: 32,
9 | path: '0.svg',
10 | params: {
11 | canvas__size__x: 600
12 | }
13 | },
14 | {
15 | test: 'Draw symbols',
16 | characters: [ 'o', '#' ],
17 | times: 160,
18 | chars: 32,
19 | path: '1.svg',
20 | params: {
21 | canvas__size__x: 600
22 | }
23 | },
24 | {
25 | test: 'Draw symbols',
26 | characters: [ '#', '/' ],
27 | times: 160,
28 | chars: 32,
29 | path: '2.svg',
30 | params: {
31 | canvas__size__x: 600
32 | }
33 | },
34 | {
35 | test: 'Draw symbols',
36 | characters: [ '-', '|', '#' ],
37 | times: 160,
38 | chars: 32,
39 | path: '3.svg',
40 | params: {
41 | canvas__size__x: 600
42 | }
43 | },
44 | {
45 | test: 'Draw symbols',
46 | characters: [ '-', '|', '#', 'o', '\\' ],
47 | times: 160,
48 | chars: 32,
49 | path: '4.svg',
50 | params: {
51 | canvas__size__x: 600
52 | }
53 | },
54 | {
55 | test: 'Draw symbols',
56 | characters: [ '|', 'o' ],
57 | times: 160,
58 | chars: 32,
59 | path: '5.svg',
60 | params: {
61 | canvas__size__x: 600
62 | }
63 | },
64 | {
65 | test: 'Draw symbols',
66 | characters: [ '|', 'o', '.' ],
67 | times: 160,
68 | chars: 32,
69 | path: '6.svg',
70 | params: {
71 | canvas__size__x: 600,
72 | style__line__stroke__color: 'brown',
73 | style__ellipse__stroke__color: 'orange'
74 | }
75 | },
76 | {
77 | test: 'Draw symbols',
78 | characters: [ '/', '\\' ],
79 | times: 160,
80 | chars: 32,
81 | path: '7.svg',
82 | params: {
83 | canvas__size__x: 600,
84 | style__line__stroke__color: 'brown',
85 | style__ellipse__stroke__color: 'orange'
86 | }
87 | },
88 | {
89 | test: 'Draw symbols',
90 | characters: [ '/', '\\' ],
91 | times: 160,
92 | chars: 32,
93 | path: '7.svg',
94 | params: {
95 | canvas__size__x: 600,
96 | style__line__stroke__color: 'brown',
97 | style__ellipse__stroke__color: 'orange'
98 | }
99 | },
100 | {
101 | test: 'Draw symbols',
102 | characters: [ '+', '-' ],
103 | times: 160,
104 | chars: 32,
105 | path: '9.svg',
106 | params: {
107 | canvas__size__x: 600,
108 | style__line__stroke__color: 'brown',
109 | style__ellipse__stroke__color: 'orange'
110 | }
111 | },
112 | {
113 | test: 'Draw symbols',
114 | characters: [ 'o' ],
115 | times: 160,
116 | chars: 32,
117 | path: '10.svg',
118 | params: {
119 | canvas__size__x: 600
120 | }
121 | },
122 | {
123 | test: 'Draw symbols',
124 | characters: [ '.', '#' ],
125 | times: 160,
126 | chars: 32,
127 | path: '11.svg',
128 | params: {
129 | canvas__size__x: 600
130 | }
131 | },
132 | {
133 | test: 'Draw symbols',
134 | characters: [ 'x', '.' ],
135 | times: 160,
136 | chars: 32,
137 | path: '12.svg',
138 | params: {
139 | canvas__size__x: 600
140 | }
141 | },
142 | {
143 | test: 'Draw symbols',
144 | characters: [ '|', '.', '-' ],
145 | times: 160,
146 | chars: 32,
147 | path: '13.svg',
148 | params: {
149 | canvas__size__x: 600,
150 | }
151 | },
152 | {
153 | test: 'Draw symbols',
154 | characters: [ '|', '.', '-' ],
155 | times: 160,
156 | chars: 32,
157 | path: '14.svg',
158 | params: {
159 | canvas__size__x: 600,
160 | style__canvas__fill__color: '#0A0C10',
161 | style__line__stroke__color: '#8D949D'
162 | }
163 | }
164 | ]
165 |
166 |
167 | puts 'TEST SVG'
168 | for index in 0..sets.length-1
169 | set = sets[ index ]
170 | puts " [#{index}]\t" + set[:test]
171 | ascii = AsciiToSvg.example_string( set[:characters], set[:times] )
172 | svg = AsciiToSvg.from_string( ascii, set[:chars], set[:params] )
173 | File.open( './banner/' + set[:path], "w" ) { | f | f.write( svg ) }
174 | end
--------------------------------------------------------------------------------
/test/test-symbols.rb:
--------------------------------------------------------------------------------
1 | require '../lib/ascii_to_svg'
2 |
3 | symbols = [
4 | [ '-', '0-minus.svg' ],
5 | [ '/', '1-tr-bl.svg' ],
6 | [ '|', '2-vertical.svg' ],
7 | [ "\\", '3-tl-br.svg' ],
8 | [ '+', '4-plus.svg' ],
9 | [ '#','5-rectangle.svg' ],
10 | [ '.', '6-empty.svg' ],
11 | [ 'x', '7-x.svg' ],
12 | [ '0', '8-ellipse.svg' ]
13 | ]
14 |
15 | symbols.each do | symbol |
16 | svg = AsciiToSvg.from_string( symbol[ 0 ], 1, {
17 | canvas__margin__left: 0,
18 | canvas__margin__top: 0,
19 | canvas__margin__right: 0,
20 | canvas__margin__bottom: 0,
21 | canvas__size__x: 30,
22 | style__line__stroke__color: 'purple',
23 | style__ellipse__stroke__color: 'purple',
24 | style__rectangle__fill__color: 'purple',
25 | }
26 | )
27 |
28 | File.open( './symbols/' + symbol[ 1 ], "w" ) { | f | f.write( svg ) }
29 | end
--------------------------------------------------------------------------------
/test/test-uniquness.rb:
--------------------------------------------------------------------------------
1 | require '../lib/ascii_to_svg'
2 |
3 |
4 | ascii = File.read( './files/ascii.txt' ).split( "\n" ).join()
5 | original = File.read( './files/original.svg' )
6 |
7 | svg = AsciiToSvg.from_string( ascii, 64, {
8 | canvas__margin__left: 120,
9 | canvas__margin__top: 120,
10 | canvas__margin__right: 120,
11 | canvas__margin__bottom: 120,
12 | canvas__size__x: 880,
13 | }
14 | )
15 |
16 | File.open( './copy.svg', "w" ) { | f | f.write( svg ) }
17 | puts AsciiToSvg.compare_svg( original, svg )
--------------------------------------------------------------------------------
/test/test.rb:
--------------------------------------------------------------------------------
1 | require '../lib/ascii_to_svg'
2 |
3 | sets = [
4 | {
5 | test: 'Draw symbols',
6 | characters: [ '-', '/', '|', "\\", '+' ],
7 | times: 512,
8 | chars: 32,
9 | path: '0-many-characters.svg',
10 | params: {}
11 | },
12 | {
13 | test: 'Change default canvas size',
14 | characters: [ '-', '/', '|', "\\", '+', '#', '.' ],
15 | times: 512,
16 | chars: 16,
17 | path: '1-many-characters.svg',
18 | params: { canvas__size__x: 200 }
19 | },
20 | {
21 | test: 'Draw squares',
22 | characters: [ '#' ],
23 | times: 256,
24 | chars: 16,
25 | path: '2-squuares.svg',
26 | params: {}
27 | },
28 | {
29 | test: 'Draw squares with offset',
30 | characters: [ 'o' ],
31 | times: 256,
32 | chars: 16,
33 | path: '3-offset.svg',
34 | params: { cell__x__offset: 6, cell__y__offset: 6, }
35 | },
36 | {
37 | test: 'Draw squares with offset',
38 | characters: [ 'x' ],
39 | times: 256,
40 | chars: 16,
41 | path: '3-offset-2.svg',
42 | params: { canvas__size__x: 300, cell__x__offset: 8, cell__y__offset: 8, }
43 | },
44 | {
45 | test: 'Test minus cell offset values',
46 | characters: [ 'X', '#' ],
47 | times: 256,
48 | chars: 16,
49 | path: '4-offset-minus.svg',
50 | params: { cell__x__offset: -2, cell__y__offset: -2 }
51 | },
52 | {
53 | test: 'Color and Opacity Test',
54 | characters: [ '/', "#", "o" ],
55 | times: 256,
56 | chars: 16,
57 | path: '5-color.svg',
58 | params: {
59 | style__line__stroke__color: 'rgb(50,100,150)',
60 | style__ellipse__stroke__color: 'rgb(150,200,250)',
61 | style__rectangle__fill__color: 'rgb(250,300,50)',
62 | style__rectangle__fill__opacity: 0.2
63 | }
64 | },
65 | {
66 | test: 'Banner',
67 | characters: [ '-', '/', '|', "\\", '+' ],
68 | times: 128,
69 | chars: 16,
70 | path: '7-banner.svg',
71 | params: { cell__x__offset: -2, cell__y__offset: -2 }
72 | }
73 | ]
74 |
75 | puts 'TEST SVG'
76 | for index in 0..sets.length-1
77 | set = sets[ index ]
78 | puts " [#{index}]\t" + set[:test]
79 | ascii = AsciiToSvg.example_string( set[:characters], set[:times] )
80 | svg = AsciiToSvg.from_string( ascii, set[:chars], set[:params] )
81 | File.open( './examples/' + set[:path], "w" ) { | f | f.write( svg ) }
82 | end
83 |
84 |
85 |
--------------------------------------------------------------------------------