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

rubocop:todo Metrics/ClassLength



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

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

rubocop:todo Metrics/MethodLength



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

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

    Word.new(
      word,
      word_options
    )
  }

  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