Class: MagicCloud::Cloud

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

Overview

Main word-cloud class. Takes words with sizes, returns image

Constant Summary collapse

DEFAULT_FAMILY =
'Impact'

Instance Method Summary collapse

Constructor Details

#initialize(words, options = {}) ⇒ Cloud

Returns a new instance of Cloud.



16
17
18
19
20
21
22
# File 'lib/magic_cloud/cloud.rb', line 16

def initialize(words, options = {})
  @words = words.sort_by(&:last).reverse
  @options = options
  @scaler = make_scaler(words, options[:scale] || :log)
  @rotator = make_rotator(options[:rotate] || :square)
  @palette = make_palette(options[:palette] || :default)
end

Instance Method Details

#draw(width, height) ⇒ Object



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
# File 'lib/magic_cloud/cloud.rb', line 26

def draw(width, height)
  # FIXME: do it in init, for specs would be happy
  shapes = @words.each_with_index.map{|(word, size), i|
    Word.new(
      word,
      font_family: @options[:font_family] || DEFAULT_FAMILY,
      font_size: scaler.call(word, size, i),
      color: palette.call(word, i),
      rotate: rotator.call(word, i)
    )
  }

  Debug.reset!

  spriter = Spriter.new
  spriter.make_sprites!(shapes)

  layouter = Layouter.new(width, height)
  visible = layouter.layout!(shapes)

  canvas = Canvas.new(width, height, 'white')
  visible.each{|sh| sh.draw(canvas)}

  canvas.render
end