css_sprite 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [Richard Huang (flyerhzm@gmail.com)]
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.textile ADDED
@@ -0,0 +1,84 @@
1
+ h1. css_sprite
2
+
3
+ A rails plugin/gem to generate css sprite automatically.
4
+
5
+ **************************************************************************
6
+
7
+ h2. Install
8
+
9
+ install rmagick gem first:
10
+ <pre><code>
11
+ sudo gem install rmagick
12
+ </code></pre>
13
+
14
+ install it as a gem:
15
+ <pre><code>
16
+ sudo gem sources -a http://gemcutter.org
17
+ sudo gem install css_sprite
18
+ </code></pre>
19
+
20
+ or install it as a plugin:
21
+ <pre><code>
22
+ script/plugin install git@github.com:flyerhzm/css_sprite.git
23
+ </code></pre>
24
+
25
+ **************************************************************************
26
+
27
+ h2. Configuration
28
+
29
+ add <code>config/css_sprite.yml</code>, define about compositing what images.
30
+ <pre><code>
31
+ forum_icon_vertical.gif:
32
+ sources:
33
+ - good_topic.gif
34
+ - mid_topic.gif
35
+ - unread_topic.gif
36
+ - sticky_topic.gif
37
+ orient: vertical
38
+ span: 20
39
+ </code></pre>
40
+
41
+ first line defines the destination image filename.
42
+ <code>sources</code> is a list of source image filenames what want to composite. They are parsed by <code>Dir.glob</code>.
43
+ <code>orient</code> defines the composite gravity type, horizontal or vertical. Default is 'vertical'.
44
+ <code>span</code> defines the span between two images. Default is 0.
45
+
46
+ **************************************************************************
47
+
48
+ h2. Usage
49
+
50
+ if you use it as a gem, add a task <code>lib/tasks/css_sprite.rake</code> first:
51
+ <pre><code>
52
+ require 'css_sprite'
53
+ </code></pre>
54
+ if you use it as a plugin, ignore the step above.
55
+
56
+ then just run rake task:
57
+ <pre><code>
58
+ rake css_sprite_build
59
+ </code></pre>
60
+
61
+ the result css is generated at <code>tmp/css_sprite.css</code>
62
+ <pre><code>
63
+ .good_topic
64
+ backgound: url('/images/forum_icon_vertical.gif') no-repeat 0px 0px
65
+ width: 20px
66
+ height: 19px
67
+ .mid_topic
68
+ backgound: url('/images/forum_icon_vertical.gif') no-repeat 0px 39px
69
+ width: 20px
70
+ height: 19px
71
+ .unread_topic
72
+ backgound: url('/images/forum_icon_vertical.gif') no-repeat 0px 78px
73
+ width: 19px
74
+ height: 18px
75
+ .sticky_topic
76
+ backgound: url('/images/forum_icon_vertical.gif') no-repeat 0px 116px
77
+ width: 19px
78
+ height: 18px
79
+ </code></pre>
80
+
81
+ **************************************************************************
82
+
83
+
84
+ Copyright (c) 2009 [Richard Huang (flyerhzm@gmail.com)], released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+ require 'jeweler'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :spec
7
+
8
+ desc "Run all specs in spec directory"
9
+ Spec::Rake::SpecTask.new(:spec) do |t|
10
+ t.spec_files = FileList['spec/**/*_spec.rb']
11
+ end
12
+
13
+ Jeweler::Tasks.new do |gemspec|
14
+ gemspec.name = "css_sprite"
15
+ gemspec.summary = "css_sprite is a rails plugin/gem to generate css sprite image automatically."
16
+ gemspec.description = "css_sprite is a rails plugin/gem to generate css sprite image automatically."
17
+ gemspec.email = "flyerhzm@gmail.com"
18
+ gemspec.homepage = ""
19
+ gemspec.authors = ["Richard Huang"]
20
+ gemspec.files.exclude '.gitignore'
21
+ end
22
+ Jeweler::GemcutterTasks.new
23
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ # Include hook code here
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,76 @@
1
+ require 'RMagick'
2
+
3
+ class Sprite
4
+ CONFIG_PATH = RAILS_ROOT + '/config/'
5
+ IMAGE_PATH = RAILS_ROOT + '/public/images/'
6
+ TEMP_PATH = RAILS_ROOT + '/tmp/'
7
+
8
+ def initialize
9
+ @output = {}
10
+ end
11
+
12
+ def build
13
+ sprite_config = File.open(CONFIG_PATH + 'css_sprite.yml') {|f| YAML::load(f)}
14
+ sprite_config.each do |dest, configs|
15
+ output_image(dest, configs)
16
+ end
17
+
18
+ output_css
19
+ end
20
+
21
+ def output_image(dest, configs)
22
+ results = []
23
+ sources = configs['sources'].collect {|source| Dir.glob(IMAGE_PATH + source)}.flatten
24
+ span = configs['span'] || 0
25
+ dest_image = get_image(sources.shift)
26
+ results << image_properties(dest_image).merge(:x => 0, :y => 0)
27
+ sources.each do |source|
28
+ source_image = get_image(source)
29
+ if configs['orient'] == 'horizontal'
30
+ gravity = Magick::EastGravity
31
+ x = dest_image.columns + span
32
+ y = 0
33
+ else
34
+ gravity = Magick::SouthGravity
35
+ x = 0
36
+ y = dest_image.rows + span
37
+ end
38
+ results << image_properties(source_image).merge(:x => x, :y => y)
39
+ dest_image = composite_images(dest_image, source_image, x, y)
40
+ end
41
+ @output[dest] = results
42
+ dest_image.write(IMAGE_PATH + dest)
43
+ end
44
+
45
+ def output_css
46
+ File.open(TEMP_PATH + 'css_sprite.css', 'w') do |f|
47
+ @output.each do |dest, results|
48
+ results.each do |result|
49
+ f.puts ".#{result[:name]}"
50
+ f.puts "\tbackgound: url('/images/#{dest}') no-repeat #{result[:x]}px #{result[:y]}px"
51
+ f.puts "\twidth: #{result[:width]}px"
52
+ f.puts "\theight: #{result[:height]}px"
53
+ f.puts ""
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ def composite_images(dest_image, src_image, x, y)
60
+ width = [src_image.columns + x, dest_image.columns].max
61
+ height = [src_image.rows + y, dest_image.rows].max
62
+ image = Magick::Image.new(width, height)
63
+ image.composite!(dest_image, 0, 0, Magick::AddCompositeOp)
64
+ image.composite!(src_image, x, y, Magick::AddCompositeOp)
65
+ image
66
+ end
67
+
68
+ def get_image(image_filename)
69
+ image = Magick::Image::read(image_filename).first
70
+ end
71
+
72
+ def image_properties(image)
73
+ {:name => File.basename(image.filename).split('.')[0], :width => image.columns, :height => image.rows}
74
+ end
75
+
76
+ end
data/lib/css_sprite.rb ADDED
@@ -0,0 +1,4 @@
1
+ unless Rake::Task.task_defined? "css_sprite:build"
2
+ load File.join(File.dirname(__FILE__), '..', 'tasks', 'css_sprite_tasks.rake')
3
+ end
4
+
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe CssSprite do
4
+ before(:all) do
5
+ @sprite = CssSprite.new
6
+ end
7
+
8
+ context "get_image" do
9
+ it "should get a image" do
10
+ @sprite.get_image(File.join(File.dirname(__FILE__), 'resources/good_topic.gif')).class.should == Magick::Image
11
+ end
12
+ end
13
+
14
+ context "image_properties" do
15
+ it "should get image properties" do
16
+ image = @sprite.get_image(File.join(File.dirname(__FILE__), 'resources/good_topic.gif'))
17
+ @sprite.image_properties(image).should == {:name => 'good_topic', :width => 20, :height => 19}
18
+ end
19
+ end
20
+
21
+ context "composite_images" do
22
+ it "should composite two images into one horizontally" do
23
+ image1 = @sprite.get_image(File.join(File.dirname(__FILE__), 'resources/good_topic.gif'))
24
+ image2 = @sprite.get_image(File.join(File.dirname(__FILE__), 'resources/mid_topic.gif'))
25
+ image = @sprite.composite_images(image1, image2, image1.columns, 0)
26
+ @sprite.image_properties(image).should == {:name => nil, :width => 40, :height => 19}
27
+ end
28
+
29
+ it "should composite two images into one verically" do
30
+ image1 = @sprite.get_image(File.join(File.dirname(__FILE__), 'resources/good_topic.gif'))
31
+ image2 = @sprite.get_image(File.join(File.dirname(__FILE__), 'resources/mid_topic.gif'))
32
+ image = @sprite.composite_images(image1, image2, 0, image1.rows)
33
+ @sprite.image_properties(image).should == {:name => nil, :width => 20, :height => 38}
34
+ end
35
+ end
36
+ end
Binary file
Binary file
data/spec/spec.opts ADDED
@@ -0,0 +1,8 @@
1
+ --colour
2
+ --format
3
+ specdoc
4
+ --reverse
5
+ --timeout
6
+ 20
7
+ --loadby
8
+ mtime
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'spec/autorun'
3
+ require 'date'
4
+
5
+ RAILS_ROOT = '.'
6
+ require File.join(File.dirname(__FILE__), '/../lib/css_sprite.rb')
@@ -0,0 +1,8 @@
1
+ require File.join(File.dirname(__FILE__), '../lib/css_sprite/sprite.rb')
2
+
3
+ namespace :css_sprite do
4
+ desc "buid css sprite image"
5
+ task :build do
6
+ Sprite.new.build
7
+ end
8
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: css_sprite
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard Huang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-30 00:00:00 +08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: css_sprite is a rails plugin/gem to generate css sprite image automatically.
17
+ email: flyerhzm@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.textile
24
+ files:
25
+ - MIT-LICENSE
26
+ - README.textile
27
+ - Rakefile
28
+ - VERSION
29
+ - init.rb
30
+ - install.rb
31
+ - lib/css_sprite.rb
32
+ - lib/css_sprite/sprite.rb
33
+ - spec/css_sprite_spec.rb
34
+ - spec/resources/good_topic.gif
35
+ - spec/resources/mid_topic.gif
36
+ - spec/spec.opts
37
+ - spec/spec_helper.rb
38
+ - tasks/css_sprite_tasks.rake
39
+ - uninstall.rb
40
+ has_rdoc: true
41
+ homepage: ""
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.5
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: css_sprite is a rails plugin/gem to generate css sprite image automatically.
68
+ test_files:
69
+ - spec/spec_helper.rb
70
+ - spec/css_sprite_spec.rb