Class: Dragonfly::ImageMagick::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/image_magick/generator.rb

Constant Summary collapse

FONT_STYLES =
{
  'normal'  => 'normal',
  'italic'  => 'italic',
  'oblique' => 'oblique'
}
FONT_STRETCHES =
{
  'normal'          => 'normal',
  'semi-condensed'  => 'semi-condensed',
  'condensed'       => 'condensed',
  'extra-condensed' => 'extra-condensed',
  'ultra-condensed' => 'ultra-condensed',
  'semi-expanded'   => 'semi-expanded',
  'expanded'        => 'expanded',
  'extra-expanded'  => 'extra-expanded',
  'ultra-expanded'  => 'ultra-expanded'
}
FONT_WEIGHTS =
{
  'normal'  => 'normal',
  'bold'    => 'bold',
  'bolder'  => 'bolder',
  'lighter' => 'lighter',
  '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

#plain(width, height, colour, opts = {}) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/dragonfly/image_magick/generator.rb', line 42

def plain(width, height, colour, opts={})
  format = opts[:format] || 'png'
  [
    convert(nil, "-size #{width}x#{height} 'xc:#{colour}'", format),
    {:format => format.to_sym, :name => "plain.#{format}"}
  ]
end

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



50
51
52
53
54
55
# File 'lib/dragonfly/image_magick/generator.rb', line 50

def plasma(width, height, format='png')
  [
    convert(nil, "-size #{width}x#{height} plasma:fractal", format),
    {:format => format.to_sym, :name => "plasma.#{format}"}
  ]
end

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



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
# File 'lib/dragonfly/image_magick/generator.rb', line 57

def text(string, opts={})
  opts = HashWithCssStyleKeys[opts]
  args = []
  format = (opts[:format] || :png)
  background = opts[:background_color] || 'none'
  font_size = (opts[:font_size] || 12).to_i
  escaped_string = "\"#{string.gsub(/"/, '\"')}\""

  # Settings
  args.push("-gravity NorthWest")
  args.push("-antialias")
  args.push("-pointsize #{font_size}")
  args.push("-font '#{opts[:font]}'") if opts[:font]
  args.push("-family '#{opts[:font_family]}'") if opts[:font_family]
  args.push("-fill #{opts[:color]}") if opts[:color]
  args.push("-stroke #{opts[:stroke_color]}") if opts[:stroke_color]
  args.push("-style #{FONT_STYLES[opts[:font_style]]}") if opts[:font_style]
  args.push("-stretch #{FONT_STRETCHES[opts[:font_stretch]]}") if opts[:font_stretch]
  args.push("-weight #{FONT_WEIGHTS[opts[:font_weight]]}") if opts[:font_weight]
  args.push("-background #{background}")
  args.push("label:#{escaped_string}")

  # Padding
  pt, pr, pb, pl = parse_padding_string(opts[:padding]) if opts[:padding]
  padding_top    = (opts[:padding_top]    || pt || 0)
  padding_right  = (opts[:padding_right]  || pr || 0)
  padding_bottom = (opts[:padding_bottom] || pb || 0)
  padding_left   = (opts[:padding_left]   || pl || 0)

  tempfile = convert(nil, args.join(' '), format)

  if (padding_top || padding_right || padding_bottom || padding_left)
    attrs  = identify(tempfile)
    text_width  = attrs[:width].to_i
    text_height = attrs[:height].to_i
    width  = padding_left + text_width  + padding_right
    height = padding_top  + text_height + padding_bottom

    args = args.slice(0, args.length - 2)
    args.push("-size #{width}x#{height}")
    args.push("xc:#{background}")
    args.push("-annotate 0x0+#{padding_left}+#{padding_top} #{escaped_string}")
    run "#{convert_command} #{args.join(' ')} #{tempfile.path}"
  end

  [
    tempfile,
    {:format => format, :name => "text.#{format}"}
  ]
end