module MemeCaptain::Draw

Mix-in for Magick::Draw

Public Instance Methods

calc_pointsize(width, height, text, min_pointsize) click to toggle source

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.

# 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_padding() click to toggle source

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

# 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) click to toggle source

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

Calls superclass method
# File lib/meme_captain/draw.rb, line 47
def stroke_width=(stroke_width)
  @stroke_width = stroke_width
  super
end