Module: MemeCaptain::Draw

Defined in:
lib/meme_captain/draw.rb

Overview

Mix-in for Magick::Draw

Instance Method Summary collapse

Instance Method Details

#calc_pointsize(width, height, text, min_pointsize) ⇒ Object

Calculate the largest pointsize for text that will be in a width x height box.

Return [pointsize, metrics] where pointsize is the largest pointsize and metrics is the RMagick multiline type metrics of the best fit.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/meme_captain/draw.rb', line 11

def calc_pointsize(width, height, text, min_pointsize)
  current_pointsize = min_pointsize

  metrics = nil

  loop {
    self.pointsize = current_pointsize
    last_metrics = metrics
    metrics = get_multiline_type_metrics(text)

    if metrics.width + stroke_padding > width or
      metrics.height + stroke_padding > height
      if current_pointsize > min_pointsize
        current_pointsize -= 1
        metrics = last_metrics
      end
      break
    else
      current_pointsize += 1
    end
  }

  [current_pointsize, metrics]
end

#stroke_paddingObject

Return the number of pixels of padding to account for this object’s stroke width.



38
39
40
41
42
43
# File 'lib/meme_captain/draw.rb', line 38

def stroke_padding
  # Each side of the text needs stroke_width / 2 pixels of padding
  # because half of the stroke goes inside the text and half goes
  # outside. The / 2 and * 2 (each side) cancel.
  @stroke_width.to_i
end

#stroke_width=(stroke_width) ⇒ Object

Override and set instance variable because there is apparently no way to get the value of a Draw’s current stroke width.



47
48
49
50
# File 'lib/meme_captain/draw.rb', line 47

def stroke_width=(stroke_width)
  @stroke_width = stroke_width
  super
end