Class: Dragonfly::Generation::RMagickGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/generation/r_magick_generator.rb

Constant Summary collapse

FONT_STYLES =
{
  'normal' => Magick::NormalStyle,
  'italic' => Magick::ItalicStyle,
  'oblique' => Magick::ObliqueStyle
}
FONT_STRETCHES =
{
  'normal'          => Magick::NormalStretch,
  'semi-condensed'  => Magick::SemiCondensedStretch,
  'condensed'       => Magick::CondensedStretch,
  'extra-condensed' => Magick::ExtraCondensedStretch,
  'ultra-condensed' => Magick::UltraCondensedStretch,
  'semi-expanded'   => Magick::SemiExpandedStretch,
  'expanded'        => Magick::ExpandedStretch,
  'extra-expanded'  => Magick::ExtraExpandedStretch,
  'ultra-expanded'  => Magick::UltraExpandedStretch
}
FONT_WEIGHTS =
{
  'normal'  => Magick::NormalWeight,
  'bold'    => Magick::BoldWeight,
  'bolder'  => Magick::BolderWeight,
  'lighter' => Magick::LighterWeight,
  '100'     => 100,
  '200'     => 200,
  '300'     => 300,
  '400'     => 400,
  '500'     => 500,
  '600'     => 600,
  '700'     => 700,
  '800'     => 800,
  '900'     => 900
}

Instance Method Summary collapse

Instance Method Details

#plasma(width, height, format = 'png') ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/dragonfly/generation/r_magick_generator.rb', line 45

def plasma(width, height, format='png')
  image = Magick::Image.read("plasma:fractal"){self.size = "#{width}x#{height}"}.first
  image.format = format.to_s
  content = use_filesystem ? write_to_tempfile(image) : image.to_blob
  image.destroy!
  [
    content,
    {:format => format.to_sym, :name => "plasma.#{format}"}
  ]
end

#text(text_string, opts = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dragonfly/generation/r_magick_generator.rb', line 56

def text(text_string, opts={})
  opts = HashWithCssStyleKeys[opts]

  draw = Magick::Draw.new
  draw.gravity = Magick::CenterGravity
  draw.text_antialias = true

  # Font size
  font_size = (opts[:font_size] || 12).to_i

  # Scale up the text for better quality -
  #  it will be reshrunk at the end
  s = scale_factor_for(font_size)

  # Settings
  draw.pointsize    = font_size * s
  draw.font         = opts[:font] if opts[:font]
  draw.font_family  = opts[:font_family] if opts[:font_family]
  draw.fill         = opts[:color] if opts[:color]
  draw.stroke       = opts[:stroke_color] if opts[:stroke_color]
  draw.font_style   = FONT_STYLES[opts[:font_style]] if opts[:font_style]
  draw.font_stretch = FONT_STRETCHES[opts[:font_stretch]] if opts[:font_stretch]
  draw.font_weight  = FONT_WEIGHTS[opts[:font_weight]] if opts[:font_weight]

  # Padding
  # NB the values are scaled up by the scale factor
  pt, pr, pb, pl = parse_padding_string(opts[:padding]) if opts[:padding]
  padding_top    = (opts[:padding_top]    || pt || 0) * s
  padding_right  = (opts[:padding_right]  || pr || 0) * s
  padding_bottom = (opts[:padding_bottom] || pb || 0) * s
  padding_left   = (opts[:padding_left]   || pl || 0) * s

  # Calculate (scaled up) dimensions
  metrics = draw.get_type_metrics(text_string)
  width, height = metrics.width, metrics.height

  scaled_up_width = padding_left + width + padding_right
  scaled_up_height = padding_top + height + padding_bottom

  # Draw the background
  image = Magick::Image.new(scaled_up_width, scaled_up_height){
    self.background_color = opts[:background_color] || 'transparent'
  }
  # Draw the text
  draw.annotate(image, width, height, padding_left, padding_top, text_string)

  # Scale back down again
  image.scale!(1/s)

  format = opts[:format] || :png
  image.format = format.to_s

  # Output image either as a string or a tempfile
  content = use_filesystem ? write_to_tempfile(image) : image.to_blob
  image.destroy!
  [
    content,
    {:format => format, :name => "text.#{format}"}
  ]
end