sprite-factory 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,6 +10,7 @@ The library provides:
10
10
 
11
11
  * both a ruby API and a command line script
12
12
  * many customizable options
13
+ * support for multiple layout algorithms - horizontal, vertical or [packed](http://codeincomplete.com/posts/2011/5/7/bin_packing/)
13
14
  * support for any stylesheet syntax, including [CSS](http://www.w3.org/Style/CSS/) and [Sass](http://sass-lang.com/).
14
15
  * support for any image library, including [RMagick](http://rmagick.rubyforge.org/) and [ChunkyPNG](https://github.com/wvanbergen/chunky_png).
15
16
  * support for pngcrush'n the generated image file
@@ -64,6 +65,11 @@ When using a framework such as Rails, you would usually DRY this up with a helpe
64
65
  image_tag('s.gif', :class => name)
65
66
  end
66
67
 
68
+ >> NOTE: `s.gif` is the traditional name of a 1x1 pixel transparent .gif used as a
69
+ dummy `src` when the true image comes from a css background attribute. Technically,
70
+ for css sprites, you could just use a `div` with a class instead of an `img`, but
71
+ to keep the markup semantic it is common to use an `img` tag with a dummy `src=s.gif`.
72
+
67
73
  Customization
68
74
  =============
69
75
 
@@ -79,6 +85,7 @@ Much of the behavior can be customized by overriding the following options:
79
85
  - `:width` - fix width of each sprite to a specific size
80
86
  - `:height` - fix height of each sprite to a specific size
81
87
  - `:pngcrush` - pngcrush the generated output image (if pngcrush is available)
88
+ - `:nocss` - suppress generation of output css file (`run!` returns css content as a string instead)
82
89
 
83
90
  Options can be passed as command line arguments to the `sf` script:
84
91
 
@@ -91,6 +98,9 @@ Options can also be passed as the 2nd argument to the `#run!` method:
91
98
  You can see the results of many of these options by viewing the sample page that
92
99
  comes with the gem in `test/images/reference/index.html`.
93
100
 
101
+ >> NOTE: only the common options are available via the command line script (to keep it simple). Specifically,
102
+ the advanced `width`, `height`, `padding` and `nocss` options are only available via the Ruby interface.
103
+
94
104
  Layout
95
105
  ======
96
106
 
data/RELEASE_NOTES.md ADDED
@@ -0,0 +1,17 @@
1
+
2
+ July 9th 2011 - v1.3.0
3
+ ----------------------
4
+
5
+ * source image file extensions now treated in case INsensitive manner (e.g. we detect both .PNG, .png and .Png)
6
+ * added `:nocss => true` option to suppress generation of output css file (caller should use `run!` return value instead - it contains the generated css content as a string)
7
+
8
+ May 8th 2011 - v1.2.0
9
+ ---------------------
10
+
11
+ * added new `:layout => :packed` option to use bin-packing algorithm for generating rectangular sprite sheet
12
+ * added pngcrush support
13
+
14
+ April 29th 2011 - v1.0.0
15
+ ------------------------
16
+
17
+ * original version
@@ -2,7 +2,7 @@ module SpriteFactory
2
2
 
3
3
  #----------------------------------------------------------------------------
4
4
 
5
- VERSION = "1.2.0"
5
+ VERSION = "1.3.0"
6
6
  SUMMARY = "Automatic CSS sprite generator"
7
7
  DESCRIPTION = "Combines individual images from a directory into a single sprite image file and creates an appropriate CSS stylesheet"
8
8
  LIB = File.dirname(__FILE__)
@@ -69,5 +69,11 @@ module SpriteFactory
69
69
 
70
70
  #----------------------------------------------------------------------------
71
71
 
72
+ def self.find_files(*args)
73
+ Dir.glob(args, File::FNM_CASEFOLD).sort # we always do case IN-sensitive file lookups and sort the result
74
+ end
75
+
76
+ #----------------------------------------------------------------------------
77
+
72
78
  end
73
79
 
@@ -40,15 +40,23 @@ module SpriteFactory
40
40
  max = layout_images(images)
41
41
  header = summary(images, max)
42
42
 
43
+ report(header)
44
+
45
+ css = []
46
+ css << style_comment(header) # header comment
47
+ css << style(selector, css_path, images, &block) # generated styles
48
+ css << IO.read(custom_style_file) if File.exists?(custom_style_file) # custom styles
49
+ css = css.join("\n")
50
+
43
51
  create_sprite(images, max[:width], max[:height])
44
52
 
45
- css_file = File.open(output_style_file, "w+")
46
- css_file.puts style_comment(header)
47
- css_file.puts style(selector, css_path, images, &block)
48
- css_file.puts IO.read(custom_style_file) if File.exists?(custom_style_file)
49
- css_file.close
53
+ unless nocss?
54
+ css_file = File.open(output_style_file, "w+")
55
+ css_file.puts css
56
+ css_file.close
57
+ end
50
58
 
51
- report(header)
59
+ css # return generated CSS to caller in string form
52
60
 
53
61
  end
54
62
 
@@ -100,6 +108,10 @@ module SpriteFactory
100
108
  "#{output}.#{style_name}" if output and style_name
101
109
  end
102
110
 
111
+ def nocss?
112
+ config[:nocss] # set true if you dont want an output css file generated (e.g. you will take the #run! output and store it yourself)
113
+ end
114
+
103
115
  def custom_style_file
104
116
  File.join(input, File.basename(input) + ".#{style_name}")
105
117
  end
@@ -124,7 +136,7 @@ module SpriteFactory
124
136
  return [] if input.nil?
125
137
  valid_extensions = library::VALID_EXTENSIONS
126
138
  expansions = Array(valid_extensions).map{|ext| File.join(input, "**", "*.#{ext}")}
127
- Dir[*expansions].sort
139
+ SpriteFactory.find_files(*expansions)
128
140
  end
129
141
 
130
142
  #----------------------------------------------------------------------------
@@ -2,11 +2,11 @@
2
2
 
3
3
  Creating a sprite from following images:
4
4
 
5
- test/images/regular/regular1.png (64x64)
6
- test/images/regular/regular2.png (64x64)
7
- test/images/regular/regular3.png (64x64)
8
- test/images/regular/regular4.png (64x64)
9
- test/images/regular/regular5.png (64x64)
5
+ test/images/regular/regular1.PNG (64x64)
6
+ test/images/regular/regular2.PNG (64x64)
7
+ test/images/regular/regular3.PNG (64x64)
8
+ test/images/regular/regular4.PNG (64x64)
9
+ test/images/regular/regular5.PNG (64x64)
10
10
 
11
11
  Output files:
12
12
  test/images/regular.png
@@ -2,11 +2,11 @@
2
2
 
3
3
  Creating a sprite from following images:
4
4
 
5
- test/images/regular/regular1.png (64x64)
6
- test/images/regular/regular2.png (64x64)
7
- test/images/regular/regular3.png (64x64)
8
- test/images/regular/regular4.png (64x64)
9
- test/images/regular/regular5.png (64x64)
5
+ test/images/regular/regular1.PNG (64x64)
6
+ test/images/regular/regular2.PNG (64x64)
7
+ test/images/regular/regular3.PNG (64x64)
8
+ test/images/regular/regular4.PNG (64x64)
9
+ test/images/regular/regular5.PNG (64x64)
10
10
 
11
11
  Output files:
12
12
  test/images/regular.fixed.png
@@ -2,11 +2,11 @@
2
2
 
3
3
  Creating a sprite from following images:
4
4
 
5
- test/images/regular/regular1.png (64x64)
6
- test/images/regular/regular2.png (64x64)
7
- test/images/regular/regular3.png (64x64)
8
- test/images/regular/regular4.png (64x64)
9
- test/images/regular/regular5.png (64x64)
5
+ test/images/regular/regular1.PNG (64x64)
6
+ test/images/regular/regular2.PNG (64x64)
7
+ test/images/regular/regular3.PNG (64x64)
8
+ test/images/regular/regular4.PNG (64x64)
9
+ test/images/regular/regular5.PNG (64x64)
10
10
 
11
11
  Output files:
12
12
  test/images/regular.horizontal.png
@@ -2,11 +2,11 @@
2
2
 
3
3
  Creating a sprite from following images:
4
4
 
5
- test/images/regular/regular1.png (64x64)
6
- test/images/regular/regular2.png (64x64)
7
- test/images/regular/regular3.png (64x64)
8
- test/images/regular/regular4.png (64x64)
9
- test/images/regular/regular5.png (64x64)
5
+ test/images/regular/regular1.PNG (64x64)
6
+ test/images/regular/regular2.PNG (64x64)
7
+ test/images/regular/regular3.PNG (64x64)
8
+ test/images/regular/regular4.PNG (64x64)
9
+ test/images/regular/regular5.PNG (64x64)
10
10
 
11
11
  Output files:
12
12
  test/images/regular.packed.png
@@ -2,11 +2,11 @@
2
2
 
3
3
  Creating a sprite from following images:
4
4
 
5
- test/images/regular/regular1.png (64x64)
6
- test/images/regular/regular2.png (64x64)
7
- test/images/regular/regular3.png (64x64)
8
- test/images/regular/regular4.png (64x64)
9
- test/images/regular/regular5.png (64x64)
5
+ test/images/regular/regular1.PNG (64x64)
6
+ test/images/regular/regular2.PNG (64x64)
7
+ test/images/regular/regular3.PNG (64x64)
8
+ test/images/regular/regular4.PNG (64x64)
9
+ test/images/regular/regular5.PNG (64x64)
10
10
 
11
11
  Output files:
12
12
  test/images/regular.padded.png
@@ -1,11 +1,11 @@
1
1
  /*
2
2
  Creating a sprite from following images:
3
3
 
4
- test/images/regular/regular1.png (64x64)
5
- test/images/regular/regular2.png (64x64)
6
- test/images/regular/regular3.png (64x64)
7
- test/images/regular/regular4.png (64x64)
8
- test/images/regular/regular5.png (64x64)
4
+ test/images/regular/regular1.PNG (64x64)
5
+ test/images/regular/regular2.PNG (64x64)
6
+ test/images/regular/regular3.PNG (64x64)
7
+ test/images/regular/regular4.PNG (64x64)
8
+ test/images/regular/regular5.PNG (64x64)
9
9
 
10
10
  Output files:
11
11
  test/images/regular.sassy.png
@@ -2,11 +2,11 @@
2
2
 
3
3
  Creating a sprite from following images:
4
4
 
5
- test/images/regular/regular1.png (64x64)
6
- test/images/regular/regular2.png (64x64)
7
- test/images/regular/regular3.png (64x64)
8
- test/images/regular/regular4.png (64x64)
9
- test/images/regular/regular5.png (64x64)
5
+ test/images/regular/regular1.PNG (64x64)
6
+ test/images/regular/regular2.PNG (64x64)
7
+ test/images/regular/regular3.PNG (64x64)
8
+ test/images/regular/regular4.PNG (64x64)
9
+ test/images/regular/regular5.PNG (64x64)
10
10
 
11
11
  Output files:
12
12
  test/images/regular.vertical.png
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -108,5 +108,26 @@ module SpriteFactory
108
108
 
109
109
  #----------------------------------------------------------------------------
110
110
 
111
+ def test_generate_sprite_with_nocss
112
+ input = REGULAR_PATH
113
+ output = File.basename(REGULAR_PATH)
114
+ with_clean_output do
115
+
116
+ assert_equal(false, File.exists?(output_path(output + ".png")), "preconditions")
117
+ assert_equal(false, File.exists?(output_path(output + ".css")), "preconditions")
118
+
119
+ css = SpriteFactory.run!(REGULAR_PATH, {:nocss => true})
120
+
121
+ assert_equal(true, File.exists?(output_path(output + ".png")), "output sprite IMAGE should exist")
122
+ assert_equal(false, File.exists?(output_path(output + ".css")), "output sprite CSS should NOT exist")
123
+ assert_equal(IO.read(reference_path(output+".css")), css, "expected return value from #run! to provide generated CSS content")
124
+
125
+ assert_reference_image(output + ".png")
126
+
127
+ end
128
+ end
129
+
130
+ #----------------------------------------------------------------------------
131
+
111
132
  end
112
133
  end
data/test/test_case.rb CHANGED
@@ -15,8 +15,8 @@ module SpriteFactory
15
15
  FORMATS_PATH = 'test/images/formats'
16
16
  EMPTY_PATH = 'test/images/empty'
17
17
 
18
- REGULAR = Dir[File.join(REGULAR_PATH, '*.png')].sort
19
- IRREGULAR = Dir[File.join(IRREGULAR_PATH, '*.png')].sort
18
+ REGULAR = SpriteFactory.find_files(File.join(REGULAR_PATH, '*.png'))
19
+ IRREGULAR = SpriteFactory.find_files(File.join(IRREGULAR_PATH, '*.png'))
20
20
 
21
21
  REGULAR_INFO = [
22
22
  { :filename => REGULAR[0], :width => 64, :height => 64 },
@@ -65,10 +65,12 @@ module SpriteFactory
65
65
  end
66
66
 
67
67
  def clean_output
68
- Dir[File.join(IMAGES_PATH, "*.png"),
69
- File.join(IMAGES_PATH, "*.css"),
70
- File.join(IMAGES_PATH, "*.sass"),
71
- File.join(IMAGES_PATH, "*.scss")].each {|f| File.delete(f)}
68
+ SpriteFactory.find_files(File.join(IMAGES_PATH, "*.png"),
69
+ File.join(IMAGES_PATH, "*.css"),
70
+ File.join(IMAGES_PATH, "*.sass"),
71
+ File.join(IMAGES_PATH, "*.scss")).each do |f|
72
+ File.delete(f)
73
+ end
72
74
  end
73
75
 
74
76
  #----------------------------------------------------------------------------
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 1.2.0
9
+ version: 1.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jake Gordon
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-05-08 00:00:00 -07:00
17
+ date: 2011-07-09 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -56,6 +56,7 @@ files:
56
56
  - .gitignore
57
57
  - LICENSE
58
58
  - README.md
59
+ - RELEASE_NOTES.md
59
60
  - Rakefile
60
61
  - bin/sf
61
62
  - lib/sprite_factory.rb
@@ -119,11 +120,11 @@ files:
119
120
  - test/images/reference/regular.vertical.css
120
121
  - test/images/reference/regular.vertical.png
121
122
  - test/images/reference/s.gif
122
- - test/images/regular/regular1.png
123
- - test/images/regular/regular2.png
124
- - test/images/regular/regular3.png
125
- - test/images/regular/regular4.png
126
- - test/images/regular/regular5.png
123
+ - test/images/regular/regular1.PNG
124
+ - test/images/regular/regular2.PNG
125
+ - test/images/regular/regular3.PNG
126
+ - test/images/regular/regular4.PNG
127
+ - test/images/regular/regular5.PNG
127
128
  - test/integration_test.rb
128
129
  - test/layout/horizontal_test.rb
129
130
  - test/layout/packed_test.rb