flurin-buttonize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +39 -0
  3. data/bin/buttonize +6 -0
  4. data/examples/buttons.rb +23 -0
  5. data/examples/default/big_blue_left.gif +0 -0
  6. data/examples/default/big_blue_middle.gif +0 -0
  7. data/examples/default/big_blue_right.gif +0 -0
  8. data/examples/default/big_green_left.gif +0 -0
  9. data/examples/default/big_green_middle.gif +0 -0
  10. data/examples/default/big_green_right.gif +0 -0
  11. data/examples/default/big_orange_left.gif +0 -0
  12. data/examples/default/big_orange_middle.gif +0 -0
  13. data/examples/default/big_orange_right.gif +0 -0
  14. data/examples/default/big_red_left.gif +0 -0
  15. data/examples/default/big_red_middle.gif +0 -0
  16. data/examples/default/big_red_right.gif +0 -0
  17. data/examples/default/small_blue_left.gif +0 -0
  18. data/examples/default/small_blue_middle.gif +0 -0
  19. data/examples/default/small_blue_right.gif +0 -0
  20. data/examples/default/small_green_left.gif +0 -0
  21. data/examples/default/small_green_middle.gif +0 -0
  22. data/examples/default/small_green_right.gif +0 -0
  23. data/examples/default/small_orange_left.gif +0 -0
  24. data/examples/default/small_orange_middle.gif +0 -0
  25. data/examples/default/small_orange_right.gif +0 -0
  26. data/examples/default/small_red_left.gif +0 -0
  27. data/examples/default/small_red_middle.gif +0 -0
  28. data/examples/default/small_red_right.gif +0 -0
  29. data/examples/default/styles.rb +23 -0
  30. data/examples/default/table_blue_left.gif +0 -0
  31. data/examples/default/table_blue_middle.gif +0 -0
  32. data/examples/default/table_blue_right.gif +0 -0
  33. data/examples/default/table_green_left.gif +0 -0
  34. data/examples/default/table_green_middle.gif +0 -0
  35. data/examples/default/table_green_right.gif +0 -0
  36. data/examples/default/table_orange_left.gif +0 -0
  37. data/examples/default/table_orange_middle.gif +0 -0
  38. data/examples/default/table_orange_right.gif +0 -0
  39. data/examples/default/table_red_left.gif +0 -0
  40. data/examples/default/table_red_middle.gif +0 -0
  41. data/examples/default/table_red_right.gif +0 -0
  42. data/lib/buttonize.rb +22 -0
  43. data/lib/buttonize/button.rb +90 -0
  44. data/lib/buttonize/button_set.rb +98 -0
  45. data/lib/buttonize/cli.rb +58 -0
  46. data/lib/buttonize/style_set.rb +54 -0
  47. metadata +117 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Flurin Egger, DigitPaint (http://www.digitpaint.nl)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,39 @@
1
+ Buttonize, your favourite button generator.
2
+ ===========================================
3
+
4
+ Buttonize is a little tool to generate sets of buttons by creating simple image templates (a left side, a repeatable middle
5
+ and a right side). You can use buttonize do create sets of buttons and sets of button styles. See the examples directory
6
+ for more information (examples/buttons.rb is a buttonset file and examples/default/styles.rb is a styleset file)
7
+
8
+
9
+ Installing
10
+ ============
11
+
12
+ Buttonize has the following dependencies:
13
+
14
+ * Ruby 1.8.x (not tested in 1.9.x)
15
+ * Rubygems
16
+ * RMagick
17
+ * Thor (for the commandline tool)
18
+
19
+ If all is well you can just install the gem by running:
20
+
21
+ sudo gem install flurin-buttonize --source=http://gems.github.com
22
+
23
+
24
+ Quickstart
25
+ ==========
26
+
27
+ For the impatient who just want to generate a blue button (and have all required stuff installed)!
28
+
29
+ Run:
30
+ buttonize button --style=big_blue "My first button"
31
+
32
+ and open
33
+ my_first_button.gif
34
+
35
+
36
+ Copyright & license
37
+ ===================
38
+
39
+ Copyright (c) 2009 Flurin Egger, DigitPaint, MIT Style License. (see MIT-LICENSE)
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.dirname(__FILE__) + "/../lib/buttonize"
4
+ require File.dirname(__FILE__) + "/../lib/buttonize/cli"
5
+
6
+ Buttonize::CLI.start
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + "/../lib/buttonize"
2
+
3
+ buttons = Buttonize::ButtonSet.define(:style_file => File.dirname(__FILE__) + "/default/styles.rb", :style_set => :default) do |set|
4
+
5
+ set.button "Test", :style => :small_green
6
+
7
+ set.group("big",:style => :big_green) do |s|
8
+ s.button "Confirm"
9
+ s.button "Export", "export"
10
+ s.button "Import", "import", :width => 65
11
+ end
12
+
13
+ set.group("big",:style => :big_blue) do |s|
14
+ s.button "Continue"
15
+ end
16
+
17
+ set.group("small", :style => :small_red) do |s|
18
+ s.button "Unsubscribe"
19
+ end
20
+ end
21
+
22
+ # This will not be ran if you use the buttonize commandline script
23
+ buttons.generate(:target_path => "~/temp/button_out")
@@ -0,0 +1,23 @@
1
+ Buttonize::StyleSet.define(:template_path => File.dirname(__FILE__)) do |set|
2
+ set.style(:big_blue) do |s|
3
+ s.font_size = 10
4
+ s.template_base = "big_blue"
5
+ s.paddings = [20,20]
6
+ end
7
+
8
+ set.style(:big_orange, :based_on => :big_blue, :template_base => "big_orange")
9
+ set.style(:big_red, :based_on => :big_blue, :template_base => "big_red")
10
+ set.style(:big_green, :based_on => :big_blue, :template_base => "big_green")
11
+
12
+ set.style(:small_blue, :font_size => 9, :template_base => "small_blue", :width => 65)
13
+ set.style(:small_orange, :based_on => :small_blue, :template_base => "small_orange")
14
+ set.style(:small_red, :based_on => :small_blue, :template_base => "small_red")
15
+ set.style(:small_green, :based_on => :small_blue, :template_base => "small_green")
16
+ end
17
+
18
+ # Styles = {
19
+ # :table_blue => {:fontsize => 8, :base => "table_blue", :paddings => [5,5], :font => File.dirname(__FILE__) + "/fonts/slkscrb.ttf", :antialias => false},
20
+ # :table_orange => {:fontsize => 8, :base => "table_orange", :paddings => [5,5], :font => File.dirname(__FILE__) + "/fonts/slkscrb.ttf", :antialias => false},
21
+ # :table_red => {:fontsize => 8, :base => "table_red", :paddings => [5,5], :font => File.dirname(__FILE__) + "/fonts/slkscrb.ttf", :antialias => false},
22
+ # :table_green => {:fontsize => 8, :base => "table_green", :paddings => [5,5], :font => File.dirname(__FILE__) + "/fonts/slkscrb.ttf", :antialias => false}
23
+ # }
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'rmagick'
3
+ include Magick
4
+ require 'fileutils'
5
+ include FileUtils
6
+
7
+ module Buttonize
8
+ def run_in_cli!
9
+ @run_in_cli = true
10
+ end
11
+
12
+ def cli?
13
+ @run_in_cli
14
+ end
15
+
16
+ module_function :run_in_cli!, :cli?
17
+ end
18
+
19
+ require File.dirname(__FILE__) + "/buttonize/button"
20
+ require File.dirname(__FILE__) + "/buttonize/style_set"
21
+ require File.dirname(__FILE__) + "/buttonize/button_set"
22
+
@@ -0,0 +1,90 @@
1
+ class Buttonize::Button
2
+
3
+ attr_reader :options
4
+
5
+ def initialize(options)
6
+ @options = {
7
+ :font_size => 9,
8
+ :paddings => [10,10],
9
+ :width => nil,
10
+ :template_base => "button",
11
+ :template_path => File.dirname(__FILE__) + "/../../examples/default",
12
+ :font => "Arial",#File.dirname(__FILE__) + "/fonts/verdanab.ttf",
13
+ :text_offset => {:x => 0, :y => 0},
14
+ :target_path => Dir.pwd,
15
+ :antialias => true
16
+ }.update(options)
17
+ @options[:template_images] ||= ["_left","_middle","_right"].map{|p| options[:template_base] + p + ".gif" }
18
+
19
+ # Validation
20
+ @options[:template_images].each{|i| raise "Missing template_image #{i}" unless File.exist?(File.join(options[:template_path],i)) }
21
+ end
22
+
23
+ def log(text)
24
+ puts text
25
+ end
26
+
27
+ def generate(text, filename = nil)
28
+ filename ||= self.text_to_filename(text)
29
+ filename = add_extension(filename,"gif")
30
+ paddings = options[:paddings]
31
+
32
+ # Load the images
33
+ left,mid,right = options[:template_images].map{|p| Image.read(File.join(options[:template_path],p)).first }
34
+
35
+ draw = Draw.new
36
+ draw.pointsize = options[:font_size]
37
+ draw.fill = "#fff"
38
+ draw.gravity = Magick::CenterGravity
39
+ draw.font = options[:font]
40
+ draw.font_weight = Magick::BoldWeight
41
+ draw.text_antialias = options[:antialias] || false
42
+
43
+ # Measure the text first
44
+ metrics = draw.get_type_metrics(text)
45
+ target_width = metrics.width + paddings[0] + paddings[1]
46
+
47
+ target_width = options[:width] if options[:width] && options[:width] > target_width
48
+
49
+ self.log "Generating image with text \"#{text}\" and width #{target_width}px and height #{left.rows}px"
50
+ dst = Image.new(target_width,left.rows) { self.background_color = "transparent"}
51
+
52
+ # Place left
53
+ dst.composite!(left,0,0,Magick::OverCompositeOp)
54
+
55
+ # Fill up the middle
56
+ pos = left.columns
57
+ while pos < (target_width - right.columns) do
58
+ if pos + mid.columns >= (target_width - right.columns)
59
+ leftover = target_width - pos - right.columns
60
+ dst.composite!(mid,(pos - (mid.columns - leftover)),0,Magick::OverCompositeOp)
61
+ pos = pos - (mid.columns - leftover) + mid.columns
62
+ else
63
+ dst.composite!(mid,pos,0,Magick::OverCompositeOp)
64
+ pos += mid.columns
65
+ end
66
+
67
+ end
68
+
69
+ # Place right
70
+ dst.composite!(right,pos,0,Magick::OverCompositeOp)
71
+ draw.annotate(dst,0,0,options[:text_offset][:x],options[:text_offset][:y],text)
72
+ dst.write(File.join(options[:target_path],filename))
73
+ dst.destroy!
74
+
75
+ filename
76
+ end
77
+
78
+ def text_to_filename(text)
79
+ text.to_s.downcase.gsub(/[^a-z0-9]/,"_").squeeze("_").gsub(/_$/,"")
80
+ end
81
+
82
+ def add_extension(filename,extension)
83
+ if filename =~ /\.#{extension}$/
84
+ filename
85
+ else
86
+ filename + "." + extension
87
+ end
88
+ end
89
+
90
+ end
@@ -0,0 +1,98 @@
1
+ module Buttonize
2
+ class ButtonSet
3
+
4
+ class << self
5
+ # Define a buttonset
6
+ #
7
+ # ==== Options
8
+ # :style_file:: The style definition file to load
9
+ # :style_set:: The styleset to use, defaults to :default
10
+ # :target_path:: The output directory
11
+ # --
12
+ def define(options={},&block)
13
+ @@set = self.new(options)
14
+ yield @@set
15
+ @@set
16
+ end
17
+
18
+ def current
19
+ @@set
20
+ end
21
+ end
22
+
23
+ attr_reader :groups, :buttons, :options
24
+ attr_accessor :path
25
+
26
+ def initialize(options)
27
+ defaults = {:style_set => :default}
28
+ @options = defaults.update(options)
29
+ @buttons = []
30
+ @groups = []
31
+ @path = ""
32
+ @parent = nil
33
+ end
34
+
35
+ def group(name,options={})
36
+ group = self.class.new({}.update(self.options).update(options))
37
+ group.path = File.join(self.path,name)
38
+ yield group
39
+ @groups << group
40
+ group
41
+ end
42
+
43
+ def button(text,*opts)
44
+ button = {:text => text, :options => {}}
45
+ button[:options] = opts.pop if opts.last.kind_of?(Hash)
46
+ button[:filename] = opts.pop if opts.last.kind_of?(String)
47
+ @buttons << button
48
+ button
49
+ end
50
+
51
+ # Generate all buttons recursively,
52
+ # all options set in define can be overruled here.
53
+ # --
54
+ def generate(options)
55
+ # Don't run this when we're in cli mode
56
+ return if Buttonize.cli?
57
+ generate_buttons(options)
58
+ end
59
+
60
+ protected
61
+
62
+ def generate_buttons(options)
63
+ current_options = self.options.merge(options)
64
+
65
+ require current_options[:style_file] if current_options[:style_file]
66
+ styleset = Buttonize::StyleSet.sets[current_options[:style_set]]
67
+
68
+ # 1. Generate my own buttons
69
+ self.buttons.each do |button|
70
+ button_options = button[:options].dup
71
+
72
+ # Determine target path
73
+ button_options[:target_path] = File.join(current_options[:target_path],self.path) unless button_options[:target_path]
74
+
75
+ # Merge with group options
76
+ button_options = current_options.merge(button_options)
77
+
78
+ # Merge with style options
79
+ style_name = button_options.delete(:style)
80
+ button_options = styleset.styles[style_name].merge(button_options) if styleset.styles.has_key?(style_name)
81
+
82
+ Button.new(button_options).generate(button[:text],button[:filename])
83
+ end
84
+
85
+ # 2. Generate buttons of my groups
86
+ self.groups.each do |group|
87
+ # Create target path
88
+ if File.directory?(current_options[:target_path]) && !File.directory?(File.join(current_options[:target_path],group.path))
89
+ FileUtils.mkdir_p(File.join(current_options[:target_path],group.path))
90
+ end
91
+
92
+ # Generate buttons
93
+ group.send(:generate_buttons,options)
94
+ end
95
+ end
96
+
97
+ end
98
+ end
@@ -0,0 +1,58 @@
1
+ require 'thor'
2
+
3
+ module Buttonize
4
+
5
+ class CLI < Thor
6
+
7
+ desc "generate BUTTONSET", "Generates a whole bunch of buttons from a ButtonSet file"
8
+ method_options :target_path => :optional, # The target where to put the generated buttons, defaults to PWD + "buttons"
9
+ :style_file => :optional, # The styleset file to load. Defaults to default built-in styles
10
+ :style_set => :optional # The styleset to use. Defaults to "default"
11
+ def generate(buttonset)
12
+ options = self.options.inject({}){|mem,h| mem[h[0].to_sym] = h[1]; mem}
13
+
14
+ target_path = options[:target_path]
15
+ if !target_path
16
+ target_path = File.join(Dir.pwd,"buttons")
17
+ FileUtils.mkdir_p(target_path) if !File.directory?(target_path)
18
+ end
19
+
20
+ options[:target_path] ||= target_path
21
+
22
+ Buttonize.run_in_cli!
23
+
24
+ # Load the buttonset
25
+ require buttonset
26
+ raise "Are you sure #{buttonset} is a buttonset file?" if !ButtonSet.current
27
+
28
+ puts "Generating buttons...."
29
+ ButtonSet.current.send(:generate_buttons,options)
30
+ end
31
+
32
+ desc "button TEXT [OUTFILE]", "Generates a single button with TEXT on it, it sanitizes the text to generate filename"
33
+ method_options :style_file => :optional, # The styleset file to load. Defaults to default built-in styles
34
+ :style_set => :optional, # The styleset to use. Defaults to "default"
35
+ :style => :required # The button style to use.
36
+ def button(text,outfile=nil)
37
+ options = {"style_file" => File.join(File.dirname(__FILE__),"../../examples/default/styles.rb"), "style_set" => :default}.update(self.options)
38
+ style = get_style(options["style_file"],options["style_set"],options["style"])
39
+ Buttonize::Button.new(style).generate(text,outfile)
40
+ end
41
+
42
+ protected
43
+
44
+ def get_style(file,set,style)
45
+ style_options = get_styleset(file,set).styles[style.to_sym]
46
+ raise "No such style '#{style}' defined in styleset '#{set}'" if style_options.nil?
47
+ style_options
48
+ end
49
+
50
+ def get_styleset(file,set)
51
+ require file
52
+ set = Buttonize::StyleSet.sets[set.to_sym]
53
+ raise "No such styleset '#{set}' defined in '#{file}'" if set.nil?
54
+ set
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,54 @@
1
+ class Buttonize::StyleSet
2
+
3
+ class Style < Hash
4
+ def method_missing(meth,*params)
5
+ if meth.to_s =~ /=$/
6
+ self[meth.to_s.sub(/=$/,"").to_sym] = params.first
7
+ elsif self.has_key?(meth)
8
+ self[meth]
9
+ else
10
+ super(meth,*params)
11
+ end
12
+ end
13
+ end
14
+
15
+ class << self
16
+ def define(*opts,&block)
17
+ options = {}
18
+ options = opts.pop if opts.last.kind_of?(Hash)
19
+ name = opts.pop if opts.last.kind_of?(String)
20
+ name ||= :default
21
+ set = self.sets[name] = self.new(name,options)
22
+ yield set
23
+ set
24
+ end
25
+
26
+ def sets
27
+ @@sets ||= {}
28
+ end
29
+ end
30
+
31
+ attr_reader :name, :styles, :options
32
+
33
+ def initialize(name,options={})
34
+ @name = name
35
+ @styles = {}
36
+ @options = options
37
+ end
38
+
39
+ def style(name,options={})
40
+ style = Style.new
41
+
42
+ style.update(self.options)
43
+ if base = options.delete(:based_on)
44
+ style.update(self.styles[base])
45
+ end
46
+ style.update(options)
47
+
48
+ yield(style) if block_given?
49
+
50
+ self.styles[name] = style
51
+ end
52
+
53
+
54
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flurin-buttonize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Flurin Egger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-23 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rmagick
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 2.5.1
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: wycats-thor
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: 0.9.8
32
+ version:
33
+ description:
34
+ email: f.p.egger@gmail.com
35
+ executables:
36
+ - buttonize
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - bin/buttonize
43
+ - examples/buttons.rb
44
+ - examples/default
45
+ - examples/default/big_blue_left.gif
46
+ - examples/default/big_blue_middle.gif
47
+ - examples/default/big_blue_right.gif
48
+ - examples/default/big_green_left.gif
49
+ - examples/default/big_green_middle.gif
50
+ - examples/default/big_green_right.gif
51
+ - examples/default/big_orange_left.gif
52
+ - examples/default/big_orange_middle.gif
53
+ - examples/default/big_orange_right.gif
54
+ - examples/default/big_red_left.gif
55
+ - examples/default/big_red_middle.gif
56
+ - examples/default/big_red_right.gif
57
+ - examples/default/small_blue_left.gif
58
+ - examples/default/small_blue_middle.gif
59
+ - examples/default/small_blue_right.gif
60
+ - examples/default/small_green_left.gif
61
+ - examples/default/small_green_middle.gif
62
+ - examples/default/small_green_right.gif
63
+ - examples/default/small_orange_left.gif
64
+ - examples/default/small_orange_middle.gif
65
+ - examples/default/small_orange_right.gif
66
+ - examples/default/small_red_left.gif
67
+ - examples/default/small_red_middle.gif
68
+ - examples/default/small_red_right.gif
69
+ - examples/default/styles.rb
70
+ - examples/default/table_blue_left.gif
71
+ - examples/default/table_blue_middle.gif
72
+ - examples/default/table_blue_right.gif
73
+ - examples/default/table_green_left.gif
74
+ - examples/default/table_green_middle.gif
75
+ - examples/default/table_green_right.gif
76
+ - examples/default/table_orange_left.gif
77
+ - examples/default/table_orange_middle.gif
78
+ - examples/default/table_orange_right.gif
79
+ - examples/default/table_red_left.gif
80
+ - examples/default/table_red_middle.gif
81
+ - examples/default/table_red_right.gif
82
+ - lib/buttonize
83
+ - lib/buttonize/button.rb
84
+ - lib/buttonize/button_set.rb
85
+ - lib/buttonize/cli.rb
86
+ - lib/buttonize/style_set.rb
87
+ - lib/buttonize.rb
88
+ - MIT-LICENSE
89
+ - README
90
+ has_rdoc: false
91
+ homepage: http://github.com/flurin/buttonize
92
+ post_install_message:
93
+ rdoc_options: []
94
+
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: "0"
102
+ version:
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: "0"
108
+ version:
109
+ requirements: []
110
+
111
+ rubyforge_project:
112
+ rubygems_version: 1.2.0
113
+ signing_key:
114
+ specification_version: 2
115
+ summary: A button generation tool for web applications.
116
+ test_files: []
117
+