Class: Stitchifier

Inherits:
Object
  • Object
show all
Defined in:
lib/stitchify.rb

Constant Summary collapse

OPEN_BRACKET =
"\e[38;5;"
CLOSE_BRACKET =
"\e[0m"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(px = 10, ascii_width = nil) ⇒ Stitchifier

Returns a new instance of Stitchifier.



14
15
16
17
18
19
20
21
# File 'lib/stitchify.rb', line 14

def initialize(px = 10, ascii_width = nil)
    self.px = px
    self.pos_x = 0
    self.pos_y = 0
    self.width = 4 * px
    self.height = 4 * px
    self.ascii_width = ascii_width
end

Instance Attribute Details

#ascii_widthObject

Returns the value of attribute ascii_width.



12
13
14
# File 'lib/stitchify.rb', line 12

def ascii_width
  @ascii_width
end

#heightObject

Returns the value of attribute height.



12
13
14
# File 'lib/stitchify.rb', line 12

def height
  @height
end

#pos_xObject

Returns the value of attribute pos_x.



12
13
14
# File 'lib/stitchify.rb', line 12

def pos_x
  @pos_x
end

#pos_yObject

Returns the value of attribute pos_y.



12
13
14
# File 'lib/stitchify.rb', line 12

def pos_y
  @pos_y
end

#pxObject

Returns the value of attribute px.



12
13
14
# File 'lib/stitchify.rb', line 12

def px
  @px
end

#widthObject

Returns the value of attribute width.



12
13
14
# File 'lib/stitchify.rb', line 12

def width
  @width
end

Instance Method Details

#arrs_to_rasem(arrs, grid) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/stitchify.rb', line 66

def arrs_to_rasem(arrs, grid)
    Rasem::SVGImage.new(width: self.width, height: self.height) do
        for line_data in arrs
            line line_data[0], line_data[1], line_data[2], line_data[3], :stroke_width=>2, :fill=>line_data[4], :stroke=>line_data[4]
        end
        for line_data in grid
            line line_data[0], line_data[1], line_data[2], line_data[3], :stroke_width=>line_data[5]
        end
    end
end

#ascii_conditionsObject



55
56
57
58
59
60
# File 'lib/stitchify.rb', line 55

def ascii_conditions
    ret = {}
    ret[:color] = false
    ret[:width] = self.ascii_width unless self.ascii_width.nil?
    ret
end

#char_builder(char, hex_str) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/stitchify.rb', line 140

def char_builder(char, hex_str)
    output = []
    case char
    when '.'
        output << pos_slope_one(1.5 * px, 2.5 * px, px, hex_str)
        output << neg_slope_one(1.5 * px, 1.5 * px, px, hex_str)
    when '~'
        output << pos_slope_one((0 - px / 2), 2.5 * px, px, hex_str)
        output << neg_slope_one(px / 2,       1.5 * px, px, hex_str)
        output << pos_slope_one(1.5 * px,     2.5 * px, px, hex_str)
    when ':'
        output << pos_slope_one(1.5 * px, 2.5 * px, px, hex_str)
        output << neg_slope_one(1.5 * px, 1.5 * px, px, hex_str)
        output << pos_slope_one(1.5 * px, 1.5 * px, px, hex_str)
        output << neg_slope_one(1.5 * px, px / 2,   px, hex_str)
    when '+'
        output << vertical_line(1.5 * px, px / 2,   2 * px, hex_str)
        output << horizontal_line(px / 2, 1.5 * px, 2 * px, hex_str)
    when '='
        output << horizontal_line(px / 2, 1.5 * px, 2 * px, hex_str)
        output << horizontal_line(px / 2, 2.5 * px, 2 * px, hex_str)
    when 'o'
        output << pos_slope_one(px / 2,   1.5 * px, px, hex_str)
        output << neg_slope_one(1.5 * px, px / 2,   px, hex_str)
        output << pos_slope_one(1.5 * px, 2.5 * px, px, hex_str)
        output << neg_slope_one(px / 2,   1.5 * px, px, hex_str)
    when '*'
        output << pos_slope_one(px / 2,   2.5 * px , 2 * px, hex_str)
        output << neg_slope_one(px / 2,   px / 2,    2 * px, hex_str)
        output << vertical_line(1.5 * px, px / 2,    2 * px, hex_str)
    when 'x'
        output << pos_slope_one(px / 2,   2.5 * px , 2 * px, hex_str)
        output << neg_slope_one(px / 2,   px / 2,    2 * px, hex_str)
    when '^'
        output << pos_slope_one(px / 2,   1.5 * px, px, hex_str)
        output << neg_slope_one(1.5 * px, px / 2,   px, hex_str)
    when '%'
        output << pos_slope_one(px / 2,   1.5 * px, px, hex_str)
        output << neg_slope_one(px / 2,   px / 2,   px, hex_str)
        output << pos_slope_one(px / 2,   2.5 * px, 2 * px, hex_str)
        output << pos_slope_one(1.5 * px, 2.5 * px, px, hex_str)
        output << neg_slope_one(1.5 * px, 1.5 * px, px, hex_str)
    when '#'
        output << pos_slope_two(px / 2,   2.5 * px, px, hex_str)
        output << pos_slope_two(1.5 * px, 2.5 * px, px, hex_str)
        output << horizontal_line(px / 2, 1.5 * px, 2 * px, hex_str)
        output << horizontal_line(px / 2, 2.5 * px, 2 * px, hex_str)
    when '@'
        output << pos_slope_one(1.5 * px,   2.5 * px, px, hex_str)
        output << neg_slope_one(1.5 * px,   1.5 * px, px, hex_str)
        output << vertical_line(2.5 * px,   px / 2,   2 * px, hex_str)
        output << horizontal_line(1.5 * px, px / 2,   px, hex_str)
        output << pos_slope_one(px / 2,     1.5 * px, px, hex_str)
        output << neg_slope_one(px / 2,     1.5 * px, px, hex_str)
        output << horizontal_line(1.5 * px, 2.5 * px, px, hex_str)
    when '$'
        output << horizontal_line(px / 2, px / 2,   2 * px, hex_str)
        output << neg_slope_one(px / 2,   px / 2,   2 * px, hex_str)
        output << horizontal_line(px / 2, 2.5 * px, 2 * px, hex_str)
        output << vertical_line(1.5 * px, px / 2,   2 * px, hex_str)
    when 'M'
        output << vertical_line(px / 2,   px / 2,   2 * px, hex_str)
        output << neg_slope_one(px / 2,   px / 2,   px, hex_str)
        output << pos_slope_one(1.5 * px, 1.5 * px, px, hex_str)
        output << vertical_line(2.5 * px, px / 2,   2 * px, hex_str)
    when 'W'
        output << vertical_line(px / 2,   px / 2,   2 * px, hex_str)
        output << pos_slope_one(px / 2,   2.5 * px, px, hex_str)
        output << neg_slope_one(1.5 * px, 1.5 * px, px, hex_str)
        output << vertical_line(2.5 * px, px / 2,   2 * px, hex_str)
    when '|'
        output << vertical_line(2.5 * px, px / 2, 2 * px, hex_str)
    when '-'
        output << horizontal_line(px / 2, 2.5 * px, 2 * px, hex_str)
    end
    output
end

#clear_varsObject



48
49
50
51
52
53
# File 'lib/stitchify.rb', line 48

def clear_vars
    self.pos_x = 0
    self.pos_y = 0
    self.width = 0
    self.height = 0
end

#gridObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/stitchify.rb', line 83

def grid
    n = 10
    output = []
    (width / n).times do |i|
        x = 1
        x = 2 if (i % 10 == 0)
        output << [i * n, 0, i * n, height, 'black', x]
    end
    (height / n).times do |i|
        x = 1
        x = 2 if i % 10 == 0
        output << [0, i * n, width, i * n, 'black', x]
    end
    output
end

#horizontal_line(startX, startY, length, hex_str) ⇒ Object



248
249
250
251
252
253
254
255
256
# File 'lib/stitchify.rb', line 248

def horizontal_line(startX, startY, length, hex_str)
    [
        startX + pos_x,
        startY + pos_y,
        startX + pos_x + length,
        startY + pos_y,
        hex_str
    ]
end

#img_processor(img) ⇒ Object



62
63
64
# File 'lib/stitchify.rb', line 62

def img_processor(img)
    AsciiArt.new(img).to_ascii_art(ascii_conditions)
end

#line_builder(line) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/stitchify.rb', line 112

def line_builder(line)
    self.width = 0
    line_output = []

    l = line.split(OPEN_BRACKET)

    l.each do |segment|

        char_data = segment.split("m")

        # if the midpoint doesn't exist
        if char_data.length == 1 
            char_data[0].chars.each do |char|
                line_output = line_output + char_builder(char, '#000000')
                self.pos_x = self.pos_x + (2 * px)
                self.width = self.width + (2 * px)
            end
        else
            char = char_data[1].delete(CLOSE_BRACKET)
            line_output = line_output + char_builder(char, HexConverter.ansi_to_hex(char_data[0]))
            self.pos_x = self.pos_x + (2 * px)
            self.width = self.width + (2 * px)
        end
    end

    line_output
end

#neg_slope_half(startX, startY, height, hex_str) ⇒ Object



268
269
270
271
272
273
274
275
276
# File 'lib/stitchify.rb', line 268

def neg_slope_half(startX, startY, height, hex_str)
    [
        startX + pos_x,
        startY + pos_y,
        startX + pos_x + (2 * height),
        startY + pos_y + height,
        hex_str
    ]
end

#neg_slope_one(startX, startY, length, hex_str) ⇒ Object



228
229
230
231
232
233
234
235
236
# File 'lib/stitchify.rb', line 228

def neg_slope_one(startX, startY, length, hex_str)
    [
        startX + pos_x,
        startY + pos_y,
        startX + pos_x + length,
        startY + pos_y + length,
        hex_str
    ]
end

#neg_slope_two(startX, startY, width, hex_str) ⇒ Object



278
279
280
281
282
283
284
285
286
# File 'lib/stitchify.rb', line 278

def neg_slope_two(startX, startY, width, hex_str)
    [
        startX + pos_x,
        startY + pos_y,
        startX + pos_x + width,
        startY + pos_y + (2 * width),
        hex_str
    ]
end

#paragraph_builder(str) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/stitchify.rb', line 99

def paragraph_builder(str)
    self.height = 0
    arr = str.split("\n")
    output = []
    arr.each do |line|
        output = output + line_builder(line)
        self.pos_y = self.pos_y + (3 * px)
        self.pos_x = 0
        self.height = self.height + (3 * px)
    end
    output
end

#pos_slope_one(startX, startY, length, hex_str) ⇒ Object



218
219
220
221
222
223
224
225
226
# File 'lib/stitchify.rb', line 218

def pos_slope_one(startX, startY, length, hex_str)
    [
        startX + pos_x,
        startY + pos_y,
        startX + pos_x + length,
        startY + pos_y - length,
        hex_str
    ]
end

#pos_slope_two(startX, startY, width, hex_str) ⇒ Object



258
259
260
261
262
263
264
265
266
# File 'lib/stitchify.rb', line 258

def pos_slope_two(startX, startY, width, hex_str)
    [
        startX + pos_x,
        startY + pos_y,
        startX + pos_x + width,
        startY + pos_y - (2 * width),
        hex_str
    ]
end

#stitch(img, file = 'stitchify.svg') ⇒ Object



23
24
25
26
27
28
29
# File 'lib/stitchify.rb', line 23

def stitch(img, file = 'stitchify.svg')
    ascii = img_processor(img)
    arrs = paragraph_builder(ascii)
    rasem = arrs_to_rasem(arrs, grid)
    write(rasem, file)
    clear_vars
end

#stitch_string(str, file = 'stitchify.svg') ⇒ Object

“.~:+=o*x^%#@$MWn .~:+=o*x^%#@$MnW.~:+=o*x^% #@$MW”



41
42
43
44
45
46
# File 'lib/stitchify.rb', line 41

def stitch_string(str, file = 'stitchify.svg')
    arrs = paragraph_builder(str)
    rasem = arrs_to_rasem(arrs, grid)
    write(rasem, file)
    clear_vars
end

#stitch_to_output(img) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/stitchify.rb', line 31

def stitch_to_output(img)
    ascii = img_processor(img)
    arrs = paragraph_builder(ascii)
    ret = arrs_to_rasem(arrs, grid).to_s
    clear_vars
    ret
end

#vertical_line(startX, startY, length, hex_str) ⇒ Object



238
239
240
241
242
243
244
245
246
# File 'lib/stitchify.rb', line 238

def vertical_line(startX, startY, length, hex_str)
    [
        startX + pos_x,
        startY + pos_y,
        startX + pos_x,
        startY + pos_y + length,
        hex_str
    ]
end

#write(rasem, file) ⇒ Object



77
78
79
80
81
# File 'lib/stitchify.rb', line 77

def write(rasem, file)
    File.open(file, "w") do |f|
        rasem.write(f)
    end
end