-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.rb
666 lines (566 loc) · 12.3 KB
/
draw.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
139
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
END{
if __FILE__ == $0
c = DrawContext.new
c.draw 'Wire Letters', wire: false
c.next_line
c.draw 'Wire Letters', latin: false
c.next_line
c.draw ' '
c.next_line
c.draw 'new Alphabet', latin: false
c.next_line
c.draw 'new Alphabet', wire: false
c.render? 'intro.png'
c = DrawContext.new
c.draw [*?a..?z].join()
c.render? 'Alphabet.png'
c = DrawContext.new
c.draw "A e o u i"
c.render? 'vowels.png'
c = DrawContext.new
c.draw "lh dT nc rs"
c.render? 'common_consonants.png'
c = DrawContext.new
c.draw "ZXJQ"
c.render? 'rare_consonants.png'
c = DrawContext.new
c.draw "m w p b"
c.next_line
c.draw " v f "
c.next_line
c.draw " y k g"
c.render? 'asymmetric_consonants.png'
c = DrawContext.new
c.draw "ndf mnpfks"
c.next_line
c.draw "ctv bcwvyrg"
c.render? 'confusions.png'
#def initialize dx: 100, dy: 100, pointsize: 50
c = DrawContext.new #dx: 30, dy: 30, pointsize: 30, pen: 3
showcase = -> text {
c.draw text, latin: false
c.draw ' '
c.draw text, wire: false
c.next_line
c.next_line
}
showcase.call 'laconic'
showcase.call 'europe'
showcase.call 'eternal'
showcase.call 'atoms'
showcase.call 'mind'
showcase.call 'rare'
showcase.call 'thought'
showcase.call 'terminology'
showcase.call 'enemies'
showcase.call 'losses'
c.render? 'showcase.png'
c = DrawContext.new dx: 300, dy: 300
c.no_grid!
draw_logo = -> {
symbols = 'nds'.chars
symbols.each_with_index { |x, i|
c.move
c.draw x, latin: false
c.next_line 0
}
symbols = 'yvw'.chars
symbols.each_with_index { |x, i|
c.move
c.draw x, latin: false
c.next_line 0
}
symbols = 'trc'.chars
symbols.each_with_index { |x, i|
c.move dx: c.cx
c.draw x, latin: false
c.next_line 0
}
symbols = 'fkp'.chars
symbols.each_with_index { |x, i|
c.move dx: c.cx
c.draw x, latin: false
c.next_line 0
}
}
c.use 'strokewidth 15'
c.use 'stroke navy'
draw_logo.call
c.use 'strokewidth 2'
c.use 'stroke orange'
draw_logo.call
c.use 'strokewidth 0.5'
c.use 'stroke yellow'
draw_logo.call
c.use 'trim'
c.use '+repage'
c.use 'bordercolor white -border 20'
c.render? 'symbol.png'
c = DrawContext.new
c.no_grid!
c.use '-strokewidth 20 -stroke aqua'
c.next_line 0
c.draw "x t q", latin: false
c.use '-strokewidth 5 -stroke navy'
c.next_line 0
c.draw "xryvtcq", latin: false
c.next_line 0.6
c.draw " w ", latin: false
c.use 'trim'
c.use '+repage'
c.use 'bordercolor white -border 20'
c.render? 'sequence.png'
# naming is hard
#c = DrawContext.new
#c.draw "new spinning wire script"
#c.draw "new twisted wire alphabet"
#c.draw "new twisted wire font"
#c.draw "new twisted wire script"
#c.render 'title.png'
end
}
# rename dx -> x (already)
class DrawContext
def draw_missing
draw_space
end
def draw_space
move dx: 1
end
# = alphabet
# == vowels
def draw_a
ratiox = 1
ratioy = 1
curve [
{ x: vx/2 * ratiox },
{ y: -vy*ratioy, x: vx/2 },
{ y: -vy, x: vx/2 },
]
curve [
{ y: -vy*ratioy, x: vx/2 },
{ x: vx - vx/2 * ratiox },
{ x: vx },
]
move_cursor
end
def draw_i
ratiox = 1
ratioy = 1
curve [
{ x: vx/2 * ratiox },
{ y: +vy*ratioy, x: vx/2 },
{ y: +vy, x: vx/2 },
]
curve [
{ y: +vy*ratioy, x: vx/2 },
{ x: vx - vx/2 * ratiox },
{ x: vx },
]
move_cursor
end
def draw_o
r = vx / 2
line dx: 2*r
move_cursor
# after cursor have moved 2*r distance by x:
right = Point[0 * @dx_scale, 0]
left = Point[-r*2 * @dx_scale, 0]
@rendering << "A 1,1, 0 0,0 #{@point + left} A 1,1 0 1,0 #{@point + right}" if @show_wire
#
# cursor is already in the place! (right point, relative 0,0)
end
def draw_e
# all points are relative to the initial point I'd have it => absolute numbers in actual exucution
ratiox = 1.5
ratioy = 0.5
curve [
{ y: -vy * ratioy },
{ y: -vy, x: vx * ratiox },
{ y: -vy },
]
curve [
{ x: -vx * ratiox, y: -vy },
{ y: -vy * ratioy },
{ },
]
move_cursor
end
def draw_u
ratiox = 1.5
ratioy = 0.5
curve [
{ y: +vy * ratioy },
{ y: +vy, x: vx * ratiox },
{ y: +vy },
]
curve [
{ x: -vx * ratiox, y: +vy },
{ y: +vy * ratioy },
{ },
]
move_cursor
end
# == rare consonants
def draw_x
line dx: cx, dy: cy
line dx: 0, dy: cy
line dx: cx, dy: 0
move_cursor
end
def draw_z
line dx: cx, dy: -cy
line dx: 0, dy: -cy
line dx: cx, dy: 0
move_cursor
end
def draw_q
line dx: 0, dy: cy
line dx: cx, dy: cy
line dx: cx, dy: 0
move_cursor
end
def draw_j
line dx: 0, dy: -cy
line dx: cx, dy: -cy
line dx: cx, dy: 0
move_cursor
end
# == simple symmetrical consonants
def draw_h
draw_l
end
def draw_l
line dx: cx
move_cursor
end
def draw_d
line dx: cx*0.5, dy: -cy
line dx: cx
move_cursor
end
def draw_t
line dx: cx*0.5, dy: cy
line dx: cx
move_cursor
end
def draw_n ratiox: 0.1, yy: 1
curve [
{ y: -cy * yy },
{ y: -cy * yy, x: 0.5*(1-ratiox)*cx },
{ x: 0.5*cx, y: -cy * yy },
]
curve [
{ y: -cy * yy, x: 0.5*cx*(1 + ratiox) },
{ y: -cy * yy, x: cx },
{ x: cx },
]
move_cursor
end
def draw_c
draw_n yy: -1
end
def draw_s ratiox: 0.5, ratio2x: 1.5, yy: 1
curve [
{ x: cx * ratiox },
{ y: -cy * yy, x: cx * ratio2x },
{ x: 0.5*cx, y: -cy * yy },
]
curve [
{ y: -cy * yy, x: cx * (1 - ratio2x) },
{ x: cx * (1 - ratiox) },
{ x: cx },
]
move_cursor
end
def draw_r
draw_s yy: -1
end
# == assymetrical composition (with line) letters
# === teeth letters
#
def draw_v ratiox: 1, ratioy: 0.7
curve [
{ x: 0.5*cx * ratiox, y: 0.5*cy * ratioy },
{ x: 0.5*cx * ratiox, y: cy - 0.5*cy * ratioy },
{ y: cy },
]
line dx: cx
move_cursor
end
def draw_f ratiox: 1, ratioy: 0.7
line dx: cx, dy: -cy
move_cursor
curve [
{ x: -0.5*cx * ratiox, y: 0.5*cy * ratioy },
{ x: -0.5*cx * ratiox, y: cy - 0.5*cy * ratioy },
{ y: cy },
]
move_cursor
end
# === lips letters (expected to be more round and not having two corners for sure - this may slow down as I assume)
#
def draw_p yy: 1, ratio: 0.1
curve [
{ y: -cy * ratio * yy },
{ y: -cy * yy },
{ x: cx, y: -cy * yy },
]
curve [
{ x: cx, y: -cy * yy },
{ x: cx, y: -cy * ratio * yy },
{ x: 0.25*cx },
]
line dx: cx
move_cursor
end
def draw_b
draw_p yy: -1
end
def draw_m yy: 1, ratio: 0.1
line dx: 0.75*cx
curve [
{ x: 0.75*cx, y: -cy * ratio * yy },
{ x: 0, y: -cy * ratio * yy },
{ x: 0, y: -cy * yy },
]
curve [
{ x: cx, y: -cy * yy },
{ x: cx, y: -cy * ratio * yy },
{ x: cx },
]
move_cursor
end
def draw_w
draw_m yy: -1
end
def draw_k yy: 1
line dx: cx, dy: -cy * yy
curve [
{ y: -cy * yy },
{ x: 0 },
{ x: cx },
]
move_cursor
end
def draw_g
draw_k yy: -1
end
def draw_y
curve [
{ x: cx },
{ y: cy, x: cx },
{ y: cy },
]
line dx: cx
move_cursor
end
# ===
# vowel dx
def vx
0.2
end
# vowel dy
def vy
2.0
end
def cx
1
end
def cy
1
end
def vowel_step ratio=1
line dx: vx * ratio
end
# wire - show this alhpabet, latin - show latin writing
def draw string, wire: true, latin: true
string.chars.each { |x|
name = "draw_#{to_name x}"
@used_starts << @point.dup
prev_point = @point
@show_wire = wire
if respond_to? name
send name
else
draw_missing
end
drawn_letter x, prev_point, @point if latin
@used_ends << @point.dup
}
end
def drawn_letter letter, a, b
mid = Point[(a.x + b.x) / 2, (a.y + b.y) / 2]
mid.x -= @pointsize * 0.25
@naming << %| -draw "text #{mid} '#{letter}'" |
end
def initialize dx: 100, dy: 100, pointsize: 50, pen: 5, gap: nil
@pen = pen
@dx_scale = dx
@dy_scale = dy
@pointsize = pointsize
@line = 0
# gap is like margin but is from baseline and not letters boundaries so it's size is bigger than the biggest letter dimension
@gap = gap || ([dx, dy].max * 2 + 10)
@used_starts = []
@used_ends = []
@rendering = []
@naming = []
next_line 0
end
def next_line line=@line+1
@line = line
@point = Point[@gap, @gap + @line*@gap]
move dx: 0, dy: 0
end
def move dx: 0, dy: 0
dx *= @dx_scale
dy *= @dy_scale
p = @point + [dx, dy]
@rendering << "M #{p}"
@point = p
end
def line dx: 0, dy: 0, absx: nil, absy: nil
dx *= @dx_scale
dy *= @dy_scale
dx = absx unless absx.nil?
dy = absy unless absy.nil?
p = @point + [dx, dy]
@rendering << "L #{p}" if @show_wire
@latest_point = p
end
def move_cursor
@point = @latest_point
end
def curve xs
(a, b, c) = xs
scale = -> point { Point[point.x * @dx_scale, point.y * @dy_scale] }
a = @point + scale[Point.from a]
b = @point + scale[Point.from b]
c = @point + scale[Point.from c]
@latest_point = c
@rendering << "C #{a} #{b} #{c}" if @show_wire
end
def arc dx: 0, dy: 0
line dx: dx, dy: dy
end
def to_name letter
return 'space' if letter == ' '
return letter.downcase
end
def render? file, **a
return if File.exist? file
render file, **a
end
def use thing
unless thing =~ /^\-|\+/
thing = "-#{thing}"
end
@rendering.push thing
end
def use_color color
use "stroke #{color}"
end
def no_grid!
@no_grid = true
end
def render file, names: true, latin: 'orange'
width = @used_ends.map { |x| x.x }.max + @gap
height = @used_ends.map { |x| x.y }.max + @gap
grid = []
grid.push *@used_starts.uniq.map { |point|
a = point.with x: 0
b = point.with x: width
%|-draw "polyline #{a} #{b}"|
}
grid = [] if @no_grid
# imagemagick does not like multiple M's at the start
while @rendering.count > 1 && @rendering[0] =~ /^M/ && @rendering[1] =~ /^M/
@rendering.shift
end
draw = ''
prev_i = 0
set_at_i = -10
@rendering_groups = @rendering.group_by.with_index { |x, i|
if x =~ /^\-|\+/
set_at_i = i
prev_i = i
else
if i == set_at_i + 1
prev_i += 1
end
prev_i
end
}
draw = ''
@rendering_groups.values.each { |xs|
if xs[0] =~ /^\-|\+/
xs.each { |x|
draw << ' '
draw << x
draw << ' '
}
else
draw << <<-END.strip.lines.map(&:strip).join(' ')
-draw "path '
#{ xs * ' ' }
'"
END
end
}
puts draw
#exit 0
#draw = <<-END.strip.lines.map(&:strip).join(' ')
# -draw "path '
# #{ @rendering * ' ' }
# '"
#END
draw = '' if @rendering.empty?
color = latin
naming = %'-font Courier -pointsize #@pointsize -strokewidth 0 -stroke #{color} -fill #{color} #{@naming * ' '}'
system <<-END.strip.lines.map(&:strip).join(' ')
convert
-size #{width}:#{height} xc:white
-fill none
-stroke gray
-strokewidth 1
#{grid * ' '}
-stroke navy
-strokewidth #{@pen}
#{draw}
#{names && naming}
#{file.inspect}
END
end
Point = Struct.new :x, :y do
def self.from given
return Point[given[:x] || 0, given[:y] || 0] if given.is_a? Hash
Point[*given]
end
def to_s
[x,y] * ?,
end
def + other
other = Point[*other] if other.kind_of? Array
Point[x + other.x, y + other.y]
end
def - other
other = Point[*other] if other.kind_of? Array
Point[x - other.x, y - other.y]
end
def / number
Point[x / number, y / number]
end
def with x: nil, y: nil
if x && y
raise ArgumentError
elsif x
Point[x, self.y]
elsif y
Point[self.x, y]
else
raise ArgumentError
end
end
end
end