Class: Buttonize::Button

Inherits:
Object
  • Object
show all
Defined in:
lib/buttonize/button.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Button

Returns a new instance of Button.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/buttonize/button.rb', line 5

def initialize(options)
  @options = {
    :width => nil, 
    :template_base => "button",
    :template_path => File.dirname(__FILE__) + "/../../examples/default",        
    :font => "Arial",#File.dirname(__FILE__) + "/fonts/verdanab.ttf", 
    :font_size => 9,       
    :font_antialias => true, 
    :text_color => "#fff",
    :text_offset => {:x => 0, :y => 0},
    :text_align => :center,
    :text_shadow => false,
    :text_shadow_offset => {:x => 1, :y => 1},
    :text_shadow_color => "#000",
    :padding => {:left => 10, :right => 10},       
    :target_path => Dir.pwd
  }.update(options)
  @options[:template_images] ||=  ["_left","_middle","_right"].map{|p| options[:template_base] + p + ".gif" }
  
  # Validation
  @options[:template_images].each{|i| raise "Missing template_image #{i}" unless File.exist?(File.join(options[:template_path],i)) }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/buttonize/button.rb', line 3

def options
  @options
end

Instance Method Details

#add_extension(filename, extension) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/buttonize/button.rb', line 98

def add_extension(filename,extension)
  if filename =~ /\.#{extension}$/
    filename
  else
    filename + "." + extension
  end
end

#generate(text, filename = nil) ⇒ Object



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
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
# File 'lib/buttonize/button.rb', line 32

def generate(text, filename = nil)
  filename ||= self.text_to_filename(text)
  filename = add_extension(filename,"gif")
  padding = options[:padding]

  # Load the images
  left,mid,right = options[:template_images].map{|p| Image.read(File.join(options[:template_path],p)).first }  

  draw = Draw.new
  draw.pointsize = options[:font_size]
  draw.gravity = self.gravity_from_alignment(options[:text_align])
  draw.font = options[:font]
  draw.font_weight = Magick::AnyWeight
  draw.text_antialias = options[:font_antialias] || false
  
  # Measure the text first
  metrics = draw.get_type_metrics(text)
  target_width = metrics.width + padding[:left] + padding[:right]

  target_width = options[:width] if options[:width] && options[:width] > target_width

  self.log "Generating image with text \"#{text}\" and width #{target_width}px and height #{left.rows}px"
  dst = Image.new(target_width,left.rows) { self.background_color = "transparent"}

  # Place left
  dst.composite!(left,0,0,Magick::OverCompositeOp)

  # Fill up the middle
  pos = left.columns
  while pos < (target_width - right.columns) do
    if pos + mid.columns >= (target_width - right.columns)
      leftover = target_width - pos - right.columns
      dst.composite!(mid,(pos - (mid.columns - leftover)),0,Magick::OverCompositeOp)
      pos = pos - (mid.columns - leftover) +  mid.columns
    else
      dst.composite!(mid,pos,0,Magick::OverCompositeOp)
      pos += mid.columns  
    end

  end

  # Place right
  dst.composite!(right,pos,0,Magick::OverCompositeOp)
  
  # Draw text
  left = options[:text_offset][:x] || 0
  left += padding[:left] unless options[:text_align] == :center
  
  if options[:text_shadow]
    draw.fill = options[:text_shadow_color]
    draw.annotate(dst,0,0,left+options[:text_shadow_offset][:x],options[:text_offset][:y]++options[:text_shadow_offset][:y],text)
  end
  draw.fill = options[:text_color]    
  draw.annotate(dst,0,0,left,options[:text_offset][:y],text)
  
  # Write file
  dst.write(File.join(options[:target_path],filename))
  dst.destroy!
  
  filename      
end

#gravity_from_alignment(alignment) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/buttonize/button.rb', line 106

def gravity_from_alignment(alignment)
  case alignment
  when :left then Magick::WestGravity
  when :right then Magick::EastGravity
  else Magick::CenterGravity      
  end
end

#log(text) ⇒ Object



28
29
30
# File 'lib/buttonize/button.rb', line 28

def log(text)
  puts text
end

#text_to_filename(text) ⇒ Object



94
95
96
# File 'lib/buttonize/button.rb', line 94

def text_to_filename(text)
  text.to_s.downcase.gsub(/[^a-z0-9]/,"_").squeeze("_").gsub(/_$/,"") 
end