Squaring helper

Skeleton for Turing::Image::*Squaring (mixin, actually).

Methods
Public Instance methods
generate(img, word, bg = nil)

contract method - generate the challenge

    # File lib/turing/image_plugins/__squaring_helper.rb, line 15
15:         def generate(img, word, bg = nil) # {{{
16:                 if bg.nil?
17:                         possible = Dir[File.join(@options[:bgdir], '*')]
18:                         bg = possible[rand(possible.size)]
19:                 else
20:                         unless FileTest.exists?(bg)
21:                                 raise ArgumentError, "Wrong background!"
22:                         end
23:                 end
24: 
25:                 img_tmp = GD2::Image.load(File.open(bg, 'r'))
26: 
27:                 if img_tmp.width < img.width || img_tmp.height < img.height
28:                         raise "Background has insufficient dimensions"
29:                 end
30: 
31:                 img.merge_from(img_tmp, 0, 0, 0, 0, img.width, img.height, 30.percent)
32: 
33:                 # XXX: is this equivalent to "img_tmp.destroy" ?
34:                 img_tmp = nil
35: 
36:                 fg = GD2::Color[0, 0, 0]
37: 
38:                 write_string(img, 'cour.ttf', fg, word, 35)
39: 
40:                 raise RuntimeError, "no squaring color selected!" if @squaring_color.nil?
41:                 fg = @squaring_color
42:                 
43:                 img.draw do |canvas|
44:                         canvas.color = fg
45:                         if rand(2).zero?
46:                                 delta = word.size > 8 ? 6 : 4
47:                                 0.step(img.width, delta) { |x| canvas.line(x, 0, x, img.height) }
48:                                 0.step(img.height, delta) { |y| canvas.line(0, y, img.width, y) }
49:                         else
50:                                 i = 0
51:                                 while i <= img.width
52:                                         canvas.line(i, 0, i, img.height)
53:                                         i += 3 + rand(4)
54:                                 end
55: 
56:                                 i = 0
57:                                 while i <= img.height
58:                                         canvas.line(0, i, img.width, i)
59:                                         i += 3 + rand(4)
60:                                 end
61:                         end
62:                 end
63:         end