jitterbug 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 Chris Sessions
1
+ Copyright (c) 2010 Flying Saucer
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.markdown CHANGED
@@ -40,17 +40,17 @@ Pass the `jitterbug` helper a string to convert into a header graphic. Optionall
40
40
 
41
41
  Install the gem:
42
42
 
43
- sudo gem install jitterbug
43
+ gem install jitterbug
44
44
 
45
45
  Drop any fonts into your project's font directory (by default `/lib/fonts`).
46
46
 
47
- ### Rails:
47
+ ### Rails 2:
48
48
 
49
49
  In your `config/environment.rb` file:
50
50
 
51
51
  Rails::Initializer.run do |config|
52
52
  ...
53
- config.gem "jitterbug", :source => 'http://gemcutter.org/'
53
+ config.gem "jitterbug"
54
54
  ...
55
55
  end
56
56
 
@@ -64,8 +64,6 @@ In your `app\controllers\application_controller.rb` file:
64
64
 
65
65
  ### Sinatra:
66
66
 
67
- Thanks to [audionerd](http://github.com/audionerd) for making Jitterbug work with Sinatra.
68
-
69
67
  require 'sinatra'
70
68
  require 'jitterbug'
71
69
 
@@ -80,10 +78,6 @@ Thanks to [audionerd](http://github.com/audionerd) for making Jitterbug work wit
80
78
 
81
79
  `Jitterbug::Config.env` is the a string representing the current environment (e.g.: `"development"` or `"production"`).
82
80
 
83
- ### Merb, Ramaze, others...
84
-
85
- Any takers?
86
-
87
81
  ## Available Options
88
82
 
89
83
  `:background` Background color for the generated header image (default `transparent`)
@@ -102,8 +96,6 @@ Any takers?
102
96
 
103
97
  `:img_path` Image path for generated header images (default `/content/jitterbug/`)
104
98
 
105
- `:kerning` Kerning value to apply to the generated header's text (default `0`)
106
-
107
99
  `:size` Font size for the generated header image (default `16`)
108
100
 
109
101
  `:style` Any additional inline styles to include in the generated tag
@@ -143,23 +135,27 @@ And relies on an external stylesheet like the following:
143
135
 
144
136
  ## Global Configuration
145
137
 
146
- You can optionally define a global configuration in `config/jitterbug.yml`. The following sample contains Jitterbug's built in defaults. Note that the asterisk default for the font causes Jitterbug to use the first font that it finds in the font_dir folder.
147
-
148
- development: &defaults
149
- background: transparent
150
- color: black
151
- font: *
152
- font_dir: /lib/fonts/
153
- format: png
154
- img_path: /content/jitterbug/
155
- size: 24
156
-
157
- test:
158
- <<: *defaults
159
- img_path: /tmp/jitterbug/
160
-
161
- production:
162
- <<: *defaults
138
+ You can optionally define a global configuration in `config/jitterbug.yml`. The following sample mirrors Jitterbug's built in defaults. Note that the asterisk default for the font causes Jitterbug to use the first font that it finds in the font_dir folder.
139
+
140
+ Jitterbug 0.6.0 uses a new configuration format, breaking compatibility with older versions. The initial root-level block defines the default values. You can include tag-specific cascading definitions. In the config below, `H1` tags are 24pt black text, `H2` tags are 16pt black text in the DinMedium font, and `H2` tags with class `small` specified are 8pt red text in the DinMedium font.
141
+
142
+ # Defaults
143
+ background: transparent
144
+ color: '#000000'
145
+ font: *
146
+ font_dir: /lib/fonts/
147
+ format: png
148
+ img_path: /content/jitterbug/
149
+ size: 24
150
+
151
+ # Tag-Specific
152
+ h2:
153
+ font: DinMedium
154
+ size: 16
155
+
156
+ h2.small:
157
+ color: red
158
+ size: 8
163
159
 
164
160
  ## Dependencies
165
161
 
@@ -170,3 +166,16 @@ Jitterbug uses [Imagemagick](http://www.imagemagick.org/script/index.php) to bui
170
166
  Jitterbug has been tested on OSX and Linux.
171
167
 
172
168
  The following font formats have successfully passed through the Jitterbug: OpenType (PostScript flavored), OpenType (TrueType flavored), PostScript (Type1), TrueType (Mac), and TrueType (PC). When processing Postscript fonts, Jitterbug (or rather Imagemagick) only uses the font outline file. Note that you should remove any spaces from your font filenames.
169
+
170
+ If you're using older Mac PostScript fonts you'll need to copy the font definitions from the resource fork to the data fork (see [here](http://wiki.github.com/sorccu/cufon/trouble-with-font-files) for more information). In OSX you'd do this:
171
+
172
+ cat "FontName/..namedfork/rsrc" > "FontName.dfont"
173
+
174
+ ## Contributors
175
+
176
+ Many thanks to the following contributors:
177
+
178
+ * Sinatra compatibility by [audionerd](http://github.com/audionerd)
179
+ * Apostrophe fix by [Neal White](http://www.cohesivecc.com/)
180
+
181
+ Jitterbug is written and maintained by [Flying Saucer](http://flying-saucer.net/)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.6.0
data/jitterbug.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{jitterbug}
8
- s.version = "0.5.1"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Chris Sessions", "Seth Engelhard"]
12
- s.date = %q{2009-11-17}
12
+ s.date = %q{2010-02-26}
13
13
  s.description = %q{Jitterbug provides on-demand text header images using the font of your choice. On its first request, Jitterbug creates the requested header graphic. Then, and on subsequent requests, it returns an html image tag pointing to the header graphic file.}
14
14
  s.email = %q{contact@flying-saucer.net}
15
15
  s.extra_rdoc_files = [
@@ -25,10 +25,6 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "jitterbug.gemspec",
27
27
  "lib/jitterbug.rb",
28
- "lib/jitterbug/config.rb",
29
- "lib/jitterbug/css.rb",
30
- "lib/jitterbug/fonts.rb",
31
- "lib/jitterbug/html.rb",
32
28
  "spec/jitterbug_spec.rb",
33
29
  "spec/spec.opts",
34
30
  "spec/spec_helper.rb"
data/lib/jitterbug.rb CHANGED
@@ -1,36 +1,121 @@
1
1
  require 'md5'
2
- %w(config css fonts html).each do |conf|
3
- require File.expand_path(File.join(File.dirname(__FILE__), 'jitterbug', conf))
4
- end
5
2
 
6
3
  module Jitterbug
7
4
 
8
5
  def self.included(base)
9
- Jitterbug::Config.read
6
+ Config.read_yaml
10
7
  end
11
8
 
12
9
  def jitterbug(label = '<jitterbug>', options = {})
13
- options = Jitterbug::Config.settings.merge(options)
14
- root = Jitterbug::Config.root
15
- hash = MD5.new("#{label}#{options.sort {|a, b| a[0].to_s <=> b[0].to_s}.to_s}")
16
- path = "#{root}/public/#{options[:img_path]}/#{hash}.#{options[:format]}".gsub('//', '/')
17
- unless File.exist?(path)
18
- caption = options[:width] ? "-size #{options[:width]}x caption:'#{label}'" : "label:'#{label}'"
19
- FileUtils.mkdir_p("#{root}/public/#{options[:img_path]}".gsub('//', '/'))
20
- `convert -background #{options[:background]} -fill "#{options[:color]}" \
21
- -font #{Jitterbug::Fonts.find(options[:font_dir], options[:font])} \
22
- -pointsize #{options[:size]} -blur 0x.3 #{caption} #{path}`
23
- # -kerning #{options[:kerning]} TODO add leading and kerning
24
- end
25
- img_src = "/#{options[:img_path]}/#{hash}.#{options[:format]}".gsub('//', '/')
26
- img_class = (['jitterbug'] << options[:class]).compact.join(' ')
27
-
28
- if options[:tag]
29
- Jitterbug::Html.tag(options[:tag], label, :class => img_class, :style => Jitterbug::Css.tag(img_src, options))
30
- elsif options[:fat]
31
- Jitterbug::Html.tag(options[:fat], label, :class => img_class, :style => Jitterbug::Css.fat(img_src, options))
32
- else
33
- Jitterbug::Html.img(img_src, :alt => label, :class => img_class, :style => options[:style])
10
+ opts = Config.settings(options)
11
+ image = Make.header(label, opts)
12
+ Html.render(image, label, opts)
13
+ end
14
+
15
+ module Config
16
+
17
+ @@settings = { :background => 'transparent',
18
+ :color => 'black',
19
+ :font => '*',
20
+ :font_dir => '/lib/fonts/',
21
+ :format => 'png',
22
+ :img_path => '/content/jitterbug/',
23
+ :size => 24,
24
+ :root => RAILS_ROOT,
25
+ :env => RAILS_ENV }
26
+
27
+ # TODO add :leading setting which requires >ImageMagick 6.5.5-8 to use -interline-spacing option
28
+ # TODO add :kerning setting
29
+
30
+ def self.read_yaml
31
+ config = File.join(@@settings[:root], 'config', 'jitterbug.yml')
32
+ if File.exist?(config)
33
+ YAML.load_file(config).each {|key, value| @@settings[key.to_sym] = value}
34
+ end
35
+ end
36
+
37
+ def self.settings(opts)
38
+ @@settings.merge(smash(opts[:tag])).
39
+ merge(smash("#{opts[:tag]}.#{opts[:class]}".to_sym)).
40
+ merge(opts)
41
+ end
42
+
43
+ def self.smash(sub)
44
+ if @@settings[sub].nil?
45
+ {}
46
+ else
47
+ @@settings[sub].inject({}) do |opts, (key, value)|
48
+ opts[(key.to_sym rescue key) || key] = value
49
+ opts
50
+ end
51
+ end
52
+ end
53
+
54
+ def self.root=(val)
55
+ @@settings[:root] = val
56
+ end
57
+
58
+ def self.env=(val)
59
+ @@settings[:env] = val
60
+ end
61
+ end
62
+
63
+ module Html
64
+
65
+ def self.render(img_src, label, opts = {})
66
+ img_class = (['jitterbug'] << opts[:class]).compact.join(' ')
67
+ if opts[:tag]
68
+ tag(opts[:tag], label, :class => img_class, :style => skinny_styles(img_src, opts))
69
+ elsif opts[:fat]
70
+ tag(opts[:fat], label, :class => img_class, :style => fat_styles(img_src, opts))
71
+ else
72
+ tag(:img, nil, :src => img_src, :alt => label, :class => img_class, :style => opts[:style])
73
+ end
74
+ end
75
+
76
+ def self.tag(type, content, opts)
77
+ attrs = opts.collect { |key, val| val.blank? ? nil : "#{key}=\"#{val}\"" }.compact.join(' ')
78
+ base = (attrs.present? ? "<#{type} #{attrs}" : "<#{type}")
79
+ base << (content ? ">#{content}</#{type}>" : " />")
80
+ end
81
+
82
+ def self.fat_styles(src, opts)
83
+ css = "display:block;text-indent:-9999px;margin:0;padding:0;background:url(#{src})no-repeat;"
84
+ css += "height:#{opts[:size]}px;" if opts[:width].nil?
85
+ "#{css}#{opts[:style].to_s}"
86
+ end
87
+
88
+ def self.skinny_styles(src, opts)
89
+ "background-image:url(#{src});#{opts[:style].to_s}"
90
+ end
91
+ end
92
+
93
+ module Make
94
+
95
+ def self.header(label, opts)
96
+ hash = MD5.new("#{label}#{opts.sort {|a, b| a[0].to_s <=> b[0].to_s}.to_s}")
97
+ image = "/#{opts[:img_path]}/#{hash}.#{opts[:format]}".gsub('//', '/')
98
+ path = "#{opts[:root]}/public/#{image}".gsub('//', '/')
99
+
100
+ if !File.exist?(path)
101
+ label.gsub!(/'/, "'\\\\\''")
102
+ caption = opts[:width] ? "-size #{opts[:width]}x caption:'#{label}'" : "label:'#{label}'"
103
+ FileUtils.mkdir_p("#{opts[:root]}/public/#{opts[:img_path]}".gsub('//', '/'))
104
+ `convert -background #{opts[:background]} -fill "#{opts[:color]}" \
105
+ -font #{find_font(opts)} -pointsize #{opts[:size]} -blur 0x.3 \
106
+ #{caption} #{path}`
107
+ end
108
+ image
109
+ end
110
+
111
+ def self.find_font(opts)
112
+ path = "#{opts[:root]}/#{opts[:font_dir]}/*#{opts[:font]}*".gsub('//', '/')
113
+ font = Dir.glob(path)
114
+ case font.size
115
+ when 0: raise "*** Jitterbug Error: Font '#{opts[:font]}' could not be found in #{opts[:font_dir]}"
116
+ when 1: return font.first
117
+ else raise "*** Jitterbug Error: Multiple fonts matched '#{opts[:font]}' in #{opts[:font_dir]}:\n + #{font.join("\n + ")}"
118
+ end
34
119
  end
35
120
  end
36
121
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jitterbug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Sessions
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-11-17 00:00:00 -08:00
13
+ date: 2010-02-26 00:00:00 -08:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -41,10 +41,6 @@ files:
41
41
  - VERSION
42
42
  - jitterbug.gemspec
43
43
  - lib/jitterbug.rb
44
- - lib/jitterbug/config.rb
45
- - lib/jitterbug/css.rb
46
- - lib/jitterbug/fonts.rb
47
- - lib/jitterbug/html.rb
48
44
  - spec/jitterbug_spec.rb
49
45
  - spec/spec.opts
50
46
  - spec/spec_helper.rb
@@ -1,43 +0,0 @@
1
- module Jitterbug
2
- module Config
3
-
4
- @@settings = { :background => 'transparent',
5
- :color => 'black',
6
- :font => '*',
7
- :font_dir => '/lib/fonts/',
8
- :format => 'png',
9
- :kerning => 0,
10
- :img_path => '/content/jitterbug/',
11
- :size => 24 }
12
-
13
- def self.read
14
- # TODO add :leading setting which requires >ImageMagick 6.5.5-8 to use -interline-spacing option
15
- config = "#{Jitterbug::Config.root}/config/jitterbug.yml"
16
- if File.exist?(config)
17
- YAML.load_file(config)[Jitterbug::Config.env].each {|key, value| @@settings[key.to_sym] = value}
18
- end
19
- end
20
-
21
- def self.settings
22
- @@settings
23
- end
24
-
25
- # accessor methods, with defaults for Rails
26
- def self.root
27
- @@root ||= RAILS_ROOT
28
- end
29
-
30
- def self.root=(value)
31
- @@root = value
32
- end
33
-
34
- def self.env
35
- @@environment ||= RAILS_ENV
36
- end
37
-
38
- def self.env=(value)
39
- @@environment = value
40
- end
41
-
42
- end
43
- end
data/lib/jitterbug/css.rb DELETED
@@ -1,15 +0,0 @@
1
- module Jitterbug
2
- module Css
3
-
4
- def self.fat(src, opts)
5
- css = "display:block;text-indent:-9999px;margin:0;padding:0;background:url(#{src})no-repeat;"
6
- css += "height:#{opts[:size]}px;" if opts[:width].nil?
7
- "#{css}#{opts[:style].to_s}"
8
- end
9
-
10
- def self.tag(src, opts)
11
- "background-image:url(#{src});#{opts[:style].to_s}"
12
- end
13
-
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- module Jitterbug
2
- module Fonts
3
-
4
- def self.find(_path, _font)
5
- path = "#{Jitterbug::Config.root}/#{_path}/*#{_font}*".gsub('//', '/')
6
- font = Dir.glob(path)
7
- case font.size
8
- when 0: raise "*** Jitterbug Error: Font '#{_font}' could not be found in #{_path}"
9
- when 1: return font.first
10
- else raise "*** Jitterbug Error: Multiple fonts matched '#{_font}' in #{_path}:\n + #{font.join("\n + ")}"
11
- end
12
- end
13
-
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- module Jitterbug
2
- module Html
3
-
4
- def self.img(src, opts = {})
5
- tag(:img, nil, opts.merge(:src => src))
6
- end
7
-
8
- def self.tag(type, content, opts = {})
9
- attrs = opts.collect { |key, val| val.blank? ? nil : "#{key}=\"#{val}\"" }.compact.join(' ')
10
- base = (attrs.present? ? "<#{type} #{attrs}" : "<#{type}")
11
- base << (content ? ">#{content}</#{type}>" : " />")
12
- end
13
-
14
- end
15
- end