Class: Buttonize::Button
- Inherits:
-
Object
- Object
- Buttonize::Button
- Defined in:
- lib/buttonize/button.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #add_extension(filename, extension) ⇒ Object
- #generate(text, filename = nil) ⇒ Object
- #gravity_from_alignment(alignment) ⇒ Object
-
#initialize(options) ⇒ Button
constructor
A new instance of Button.
- #log(text) ⇒ Object
- #text_to_filename(text) ⇒ Object
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 |
# File 'lib/buttonize/button.rb', line 5 def initialize() = { :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, :padding => {:left => 10, :right => 10}, :target_path => Dir.pwd }.update() [:template_images] ||= ["_left","_middle","_right"].map{|p| [:template_base] + p + ".gif" } # Validation [:template_images].each{|i| raise "Missing template_image #{i}" unless File.exist?(File.join([:template_path],i)) } end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/buttonize/button.rb', line 3 def end |
Instance Method Details
#add_extension(filename, extension) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/buttonize/button.rb', line 90 def add_extension(filename,extension) if filename =~ /\.#{extension}$/ filename else filename + "." + extension end end |
#generate(text, filename = nil) ⇒ Object
29 30 31 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 |
# File 'lib/buttonize/button.rb', line 29 def generate(text, filename = nil) filename ||= self.text_to_filename(text) filename = add_extension(filename,"gif") padding = [:padding] # Load the images left,mid,right = [:template_images].map{|p| Image.read(File.join([:template_path],p)).first } draw = Draw.new draw.pointsize = [:font_size] draw.fill = [:text_color] draw.gravity = self.gravity_from_alignment([:text_align]) draw.font = [:font] draw.font_weight = Magick::AnyWeight draw.text_antialias = [:font_antialias] || false # Measure the text first metrics = draw.get_type_metrics(text) target_width = metrics.width + padding[:left] + padding[:right] target_width = [:width] if [:width] && [: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 = [:text_offset][:x] || 0 left += padding[:left] unless [:text_align] == :center draw.annotate(dst,0,0,left,[:text_offset][:y],text) # Write file dst.write(File.join([:target_path],filename)) dst.destroy! filename end |
#gravity_from_alignment(alignment) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/buttonize/button.rb', line 98 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
25 26 27 |
# File 'lib/buttonize/button.rb', line 25 def log(text) puts text end |
#text_to_filename(text) ⇒ Object
86 87 88 |
# File 'lib/buttonize/button.rb', line 86 def text_to_filename(text) text.to_s.downcase.gsub(/[^a-z0-9]/,"_").squeeze("_").gsub(/_$/,"") end |