sprite 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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.md ADDED
@@ -0,0 +1,111 @@
1
+ # sprite #
2
+
3
+ `sprite` is a gem that helps generate css sprite images automagically. It's aim is to support all web frameworks (Merb/Rails/Sinatra)
4
+
5
+ it's a fork an extension of Richard Huang's excellent Rails plugin `css_sprite` (github.com/flyerhzm/css_sprite)
6
+
7
+ ****
8
+
9
+ ## Install ##
10
+
11
+ install rmagick gem first:
12
+
13
+ gem install rmagick
14
+
15
+ if you have any problems with the rmagick gem, install imagemagick via macports first:
16
+
17
+ sudo port install libxml2
18
+ sudo port install ImageMagick
19
+
20
+ or via installer: http://github.com/maddox/magick-installer/tree/master
21
+
22
+ install it as a gem:
23
+
24
+ gem sources -a http://gemcutter.org
25
+ gem install sass_sprite
26
+
27
+ ### if using merb ###
28
+
29
+ gem "sprite"
30
+
31
+ ### if using rails ###
32
+
33
+ script/plugin install git://github.com/merbjedi/sprite.git
34
+
35
+ or add to environment.rb
36
+
37
+ config.gem "sprite"
38
+
39
+ ***
40
+
41
+ ## Configuration ##
42
+
43
+ add `config/sprite.yml`, define about compositing what images.
44
+
45
+ forum_icon_vertical.gif: # destination image file
46
+ sources: # source image file list
47
+ - good_topic.gif
48
+ - mid_topic.gif
49
+ - unread_topic.gif
50
+ - sticky_topic.gif
51
+ orient: vertical # composite gravity, vertical or horizontal
52
+ span: 5 # span of space between two images
53
+
54
+ first line defines the destination image filename.
55
+ `sources` is a list of source image filenames what want to composite. They are parsed by <code>Dir.glob</code>.
56
+ `orient` defines the composite gravity type, horizontal or vertical. Default is 'vertical'.
57
+ `span` defines the span between two images. Default is 0.
58
+
59
+ you can define any number of destination image files.
60
+
61
+ ***
62
+
63
+ ## Usage ##
64
+
65
+ if you use it as a gem, add a task `lib/tasks/sprites.rake` first:
66
+
67
+ require 'sprites'
68
+
69
+ if you use it as a plugin, ignore the step above.
70
+
71
+ then just run rake task:
72
+
73
+ rake sprites:build
74
+
75
+ the result css is generated at `public/stylesheets/sprite.css`
76
+
77
+ .good_topic {
78
+ background: url('/images/forum_icon_vertical.gif') no-repeat 0px 0px;
79
+ width: 20px;
80
+ height: 19px;
81
+ }
82
+ .mid_topic {
83
+ background: url('/images/forum_icon_vertical.gif') no-repeat 0px 24px
84
+ width: 20px;
85
+ height: 19px;
86
+ }
87
+ .unread_topic {
88
+ background: url('/images/forum_icon_vertical.gif') no-repeat 0px 48px;
89
+ width: 19px;
90
+ height: 18px;
91
+ }
92
+ .sticky_topic {
93
+ background: url('/images/forum_icon_vertical.gif') no-repeat 0px 71px;
94
+ width: 19px;
95
+ height: 18px;
96
+ }
97
+
98
+
99
+ ***
100
+
101
+ ## Contributors ##
102
+
103
+ merbjedi - reorganized as a general purpose ruby plugin for merb/rails/sinatra/
104
+
105
+ josedelcorral - fix the style of generated css
106
+
107
+
108
+ ***
109
+
110
+
111
+ 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 = "sprite"
15
+ gemspec.summary = "generate your css sprites automagically"
16
+ gemspec.description = "sprite is a rails/merb plugin that generates sprites for css, sass"
17
+ gemspec.email = "merbjedi@gmail.com"
18
+ gemspec.homepage = "http://github.com/merbjedi/sprite"
19
+ gemspec.authors = ["Jacques Crocker", "Richard Huang"]
20
+ gemspec.files.exclude '.gitignore'
21
+ end
22
+ Jeweler::GemcutterTasks.new
23
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,24 @@
1
+ config:
2
+ base_directory: public/images
3
+ css_output: tmp/css_sprite.css
4
+
5
+ images:
6
+ - description: Stars with transparency
7
+ sources:
8
+ - sc-mcstars0.png
9
+ - sc-mcstars1.png
10
+ - sc-mcstars2.png
11
+ - sc-mcstars3.png
12
+ target: stars_png_with_transparency.png
13
+ align: horizontal
14
+ spaced_by: 50
15
+
16
+ - description: Stars as JPEG
17
+ sources:
18
+ - sc-0.jpg
19
+ - sc-1.jpg
20
+ - sc-2.jpg
21
+ - sc-3.jpg
22
+ target: stars_as_jpg.jpg
23
+ align: vertical
24
+ spaced_by: 50
data/lib/sprite.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'rake'
2
+ require 'sprite/sprite'
3
+ unless Rake::Task.task_defined? "sprite:build"
4
+ load File.join(File.dirname(__FILE__), '..', 'tasks', 'sprite_tasks.rake')
5
+ end
6
+
@@ -0,0 +1,84 @@
1
+ require 'RMagick'
2
+
3
+ class Sprite
4
+ CONFIG_PATH = RAILS_ROOT + '/config/'
5
+ DEFAULT_IMAGE_PATH = RAILS_ROOT + '/public/images/'
6
+ CSS_OUTPUT = RAILS_ROOT + '/tmp/css_sprite.css'
7
+
8
+ def initialize
9
+ @image_path = DEFAULT_IMAGE_PATH
10
+ @config_files = Dir.glob("#{CONFIG_PATH}/css_sprite*.yml")
11
+ end
12
+
13
+ def build
14
+ @config_files.each do |config_file|
15
+ @output = {}
16
+ sprite_config = File.open(config_file) {|f| YAML::load(f)}
17
+ @image_path = (sprite_config['config']['base_directory'])?RAILS_ROOT+"/"+sprite_config['config']['base_directory']+"/":DEFAULT_IMAGE_PATH
18
+ @css_output = (sprite_config['config']['css_output'])?RAILS_ROOT+"/"+sprite_config['config']['css_output']:CSS_OUTPUT
19
+ sprite_config['images'].each do |configuration|
20
+ output_image(configuration)
21
+ end
22
+ output_css(sprite_config)
23
+ end
24
+ end
25
+
26
+ def output_image(configuration)
27
+ results = []
28
+ sources = configuration['sources'].collect {|source| Dir.glob(@image_path+source)}.flatten
29
+ dest = configuration['target'] || sources[0].gsub(/\./,"_sprite.")
30
+ spaced_by = configuration['spaced_by'] || 0
31
+ dest_image = get_image(sources.shift)
32
+ results << image_properties(dest_image).merge(:x => 0, :y => 0)
33
+ sources.each do |source|
34
+ source_image = get_image(source)
35
+ if configuration['align'] == 'horizontal'
36
+ gravity = Magick::EastGravity
37
+ x = dest_image.columns + spaced_by
38
+ y = 0
39
+ else
40
+ gravity = Magick::SouthGravity
41
+ x = 0
42
+ y = dest_image.rows + spaced_by
43
+ end
44
+ results << image_properties(source_image).merge(:x => x, :y => y)
45
+ dest_image = composite_images(dest_image, source_image, x, y)
46
+ end
47
+ @output[dest] = results
48
+ dest_image.write(@image_path + dest)
49
+ end
50
+
51
+ def output_css(configuration)
52
+ File.open(@css_output, 'w') do |f|
53
+ @output.each do |dest, results|
54
+ results.each do |result|
55
+ f.puts ".#{result[:name]}"
56
+ f.puts "\tbackground: url('/images/#{dest}') no-repeat #{result[:x]}px #{result[:y]}px;"
57
+ f.puts "\twidth: #{result[:width]}px;"
58
+ f.puts "\theight: #{result[:height]}px;"
59
+ f.puts ""
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ def composite_images(dest_image, src_image, x, y)
66
+ width = [src_image.columns + x, dest_image.columns].max
67
+ height = [src_image.rows + y, dest_image.rows].max
68
+ image = Magick::Image.new(width, height)
69
+ image.opacity = Magick::MaxRGB
70
+
71
+ image.composite!(dest_image, 0, 0, Magick::OverCompositeOp)
72
+ image.composite!(src_image, x, y, Magick::OverCompositeOp)
73
+ image
74
+ end
75
+
76
+ def get_image(image_filename)
77
+ image = Magick::Image::read(image_filename).first
78
+ end
79
+
80
+ def image_properties(image)
81
+ {:name => File.basename(image.filename).split('.')[0], :width => image.columns, :height => image.rows}
82
+ end
83
+
84
+ 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/sprite.rb')
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Sprite do
4
+ before(:all) do
5
+ @sprite = Sprite.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
data/sprite.gemspec ADDED
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sprite}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jacques Crocker", "Richard Huang"]
12
+ s.date = %q{2009-11-13}
13
+ s.description = %q{sprite is a rails/merb plugin that generates sprites for css, sass}
14
+ s.email = %q{merbjedi@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ "MIT-LICENSE",
20
+ "README.md",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "config/css_sprite_example_config.yml",
24
+ "lib/sprite.rb",
25
+ "lib/sprite/sprite.rb",
26
+ "spec/resources/good_topic.gif",
27
+ "spec/resources/mid_topic.gif",
28
+ "spec/spec.opts",
29
+ "spec/spec_helper.rb",
30
+ "spec/sprite_spec.rb",
31
+ "sprite.gemspec",
32
+ "tasks/sprite_tasks.rake"
33
+ ]
34
+ s.homepage = %q{http://github.com/merbjedi/sprite}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{generate your css sprites automagically}
39
+ s.test_files = [
40
+ "spec/spec_helper.rb",
41
+ "spec/sprite_spec.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ else
50
+ end
51
+ else
52
+ end
53
+ end
54
+
@@ -0,0 +1,8 @@
1
+
2
+ namespace :sprite do
3
+ desc "buid css sprite image"
4
+ task :build do
5
+ require File.join(File.dirname(__FILE__), '../lib/sprite/sprite.rb')
6
+ Sprite.new.build
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jacques Crocker
8
+ - Richard Huang
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-11-13 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: sprite is a rails/merb plugin that generates sprites for css, sass
18
+ email: merbjedi@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README.md
25
+ files:
26
+ - MIT-LICENSE
27
+ - README.md
28
+ - Rakefile
29
+ - VERSION
30
+ - config/css_sprite_example_config.yml
31
+ - lib/sprite.rb
32
+ - lib/sprite/sprite.rb
33
+ - spec/resources/good_topic.gif
34
+ - spec/resources/mid_topic.gif
35
+ - spec/spec.opts
36
+ - spec/spec_helper.rb
37
+ - spec/sprite_spec.rb
38
+ - sprite.gemspec
39
+ - tasks/sprite_tasks.rake
40
+ has_rdoc: true
41
+ homepage: http://github.com/merbjedi/sprite
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: generate your css sprites automagically
68
+ test_files:
69
+ - spec/spec_helper.rb
70
+ - spec/sprite_spec.rb