Ascii to Svg Generator for Ruby

This module generates beautiful svg images based on a input string.

Examples

e

character_set = [ '-', '/', '|', "\", '#' ]

character_set = [ '-', '|', '#' ]

character_set = [ '-', '|', '#', 'o', '\' ]

character_set = [ '|', 'o', '.' ]
options = {
    style__line__stroke__color: 'brown',
    style__ellipse__stroke__color: 'orange'
}

character_set = [ '/', '\' ]
options = {
    style__line__stroke__color: 'brown',
    style__ellipse__stroke__color: 'orange'
}

character_set = [ 'x', '.' ]

character_set = [ '|', '.', '-' ]
options = {
    style__canvas__fill__color: '#0A0C10',
    style__line__stroke__color: '#8D949D'
}

Quickstart

  1. Install gem

    gem install 'ascii_to_svg'
    
  2. Run Code

    require 'ascii_to_svg'
    
    # Generate Example String
    ascii = AsciiToSvg.example_string( [ 'x', 'o' ], 256 )
    
    # Generate SVG
    svg = AsciiToSvg.from_string( ascii, 16, {} )
    
    # => "<svg xmlns=\"http://www.w3.org/2000/svg ...
    

Functions

AsciiToSvg.options

Output all Parameter

    AsciiToSvg.options
    # => {:canvas=>{:size=>{:x=>500 ...

AsciiToSvg.example_string()

Type Required Description Example Description
characters Array (of single alphabetic character) No [ 'x', 'o' ] Set alphabetic characters for string
length Integer No 512 Lengt of generated String


    one = AsciiToSvg.example_string()
    two = AsciiToSvg.example_string( [ 'x', 'o' ], 512 )

AsciiToSvg.from_string()

Type Required Description
ascii String Yes A String of one or more Characters
lenght Integer Yes Number of Characters in one Line (Row)
options Hash No Modify output. For detailed Information


    AsciiToSvg.from_string(
        ascii, 
        length, 
        options
    )

AsciiToSvg.similar_svg()

    original_str = AsciiToSvg.example_string( [ 'x', 'o' ], 256 )
    original_svg = AsciiToSvg.from_string( original_str, 16, {} )

    copy_str = original_str[ 0, original_str.length - 1 ]
    copy_svg = AsciiToSvg.from_string( copy_str, 16, {} )

    AsciiToSvg.similar_svg( original_svg, copy_svg )
    # => => {:hexdigest1=>"79d5e81d230214749672a1c10e103c46", :hexdigest2=>"f48cacecf2720633dd081c3421d84981", :unique=>false, :score=>0.002799275481640046} 

Naming

Canvas
-------------------------------------
|                   ^
|                   | Margin Top
|        Grid       v
|        ----------------------------
|        |   Cell
|        |   ------------------------
|        |   |           | Offset x |
| Margin |   |   Symbol  |<-------->|
| Left   |   |           |          |
| <----> |   |------------          |
|        |   |        ^             |
|        |   |        | Offset Y    |
|        |   |        V             |
|        |   ------------------------

Signs

Nr Sign Image SVG Element
1 "\" Line
2 "\ "
3 "/" Line
4 "-" Line
5 "+" Line
6 "x" Line
7 "o" Ellipse
8 "#" Rectangle
9 "."

Options

Canvas

Defines the full width and height. | Nr | Name | Key | Default | Type | Description | | :-- | :-- | :-- | :-- | :-- | :-- | | A.1. | Size X |:canvas_sizex | 500 | Integer | Set width of canvas | | A.2. | Margin Left |:canvasmarginleft | 0 | Integer | Set margin on the left side | | A.3. | Margin Top |:canvasmargintop | 0 | Integer | Set margin on the top | | A.4. | Margin Right |:canvasmarginright | 0 | Integer | Set margin on the right | | A.5. | Margin Bottom |:canvasmargin_bottom | 0 | Integer | Set margin on the bottom |

Cell

Defines the Area of one symbol including offset. | Nr | Name | Key | Default | Type | Description | | :-- | :-- | :-- | :-- | :-- | :-- | | B.1. | X Offset |:cell_xoffset | 0 | Integer | Define the X (width) offset to the next cell | | B.2. | Y Offset |:celly_offset | 0 | Integer | Define the Y (height) offset to the next cell |

Symbols

Defines which char will be interpreted as "svg element" | Nr | Name | Key | Default | Type | Image | | :-- | :-- | :-- | :-- | :-- | :-- | | C.1. | \ |:"symbols_\\" | [ "\\" ] | Array | | | C.2. | / | :"symbols/" | [ "/" ] | Array | | | C.3. | X | :"symbolsX" | [ "X", "x" ] | Array | | | C.4. | - | :"symbols-" | [ "-" ] | Array | | | C.5. | | | :"symbols\" | | [ "\|", "1" ] | Array | | | C.6. | O | :"symbolsO" | [ "O", "o", "0" ] | Array | | | C.7. | + | :"symbols+" | [ "+" ] | Array | | | C.8. | # | :"symbols_#" | [ "#" ] | Array | |

Style

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.

Line | Nr | Name | Key | Default | Type | Description | | :-- | :-- | :-- | :-- | :-- | :-- | | D.1. | Stroke Width |:style_linestrokewidth | 2.0 | Float | Define width of stroke, please notice linecap type for desired behavior | | D.2. | Stroke Color |:stylelinestrokecolor | "rgb(0,0,0)" | String | Define color of stroke in RGB, you can also use HTML Color names or "none" | | D.3. | Stroke Opacity |:stylelinestrokeopacity | 1.0 | Float | Define opacity of the stroke, use floating numbers. | | D.4. | Stroke Linecap |:stylelinestroke_linecap | "square" | String | Defines behavior of line ("butt" / "round" / "sqaure"). Detailed explaination: developer.mozilla.org|

Ellipse | Nr | Name | Key | Default | Type | Description | | :-- | :-- | :-- | :-- | :-- | :-- | | D.5. | Stroke Width |:style_ellipsestrokewidth | 2.0 | Float | Define stroke width, use floating numbers | | D.6. | Stroke Color |:styleellipsestrokecolor | "rgb(0,0,0)" | String | Define stroke color in RGB, you can also use HTML Color names or "none". | | D.7. | Stroke Opacity |:styleellipsestrokeopacity | 1.0 | Float | Set stroke opacity, use floating numbers. | | D.8. | Stroke Linecap |:styleellipsestrokelinecap | "square" | String | Defines behavior of ellipse ("butt" / "round" / "sqaure"). Detailed explaination: developer.mozilla.org| | | D.9. | Fill |:styleellipse_fill | "none" | String | Define color fill of |

Rectangle | Nr | Name | Key | Default | Type | Description | | :-- | :-- | :-- | :-- | :-- | :-- | | D.10. | Fill Color |:style_rectanglefillcolor | "rgb(0,0,0)" | String | Define infill color in RGB, you can also use HTML Color names or "none". | | D.11. | Fill Opacity |:stylerectanglefill_opacity | 1.0 | Float | Set infill opacity, use floating numbers. |

Canvas | Nr | Name | Key | Default | Type | Description | | :-- | :-- | :-- | :-- | :-- | :-- | | D.12. | Fill Color |:style_canvasfillcolor | "rgb(255,255,255)" | String | Define canvas infill color in RGB, you can also use HTML Color names or "none". | | D.13. | Fill Opacity |:stylecanvasfill_opacity | 1.0 | Float | Set canvas infill opacity, use floating numbers.|

Contributing

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.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the AsciiToSvg project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.