From 573d1a1b03804c97759fefbad623800d3cf89d88 Mon Sep 17 00:00:00 2001 From: Nico Hagenburger Date: Sun, 17 Oct 2010 16:18:04 +0200 Subject: [PATCH] generation of png file --- .../sass_extensions/functions/sprites.rb | 27 +++++++++++++++---- lib/compass/sprites.rb | 2 +- spec/sprites_spec.rb | 22 ++++++++++++++- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/compass/sass_extensions/functions/sprites.rb b/lib/compass/sass_extensions/functions/sprites.rb index c676ba11..8b6ae8e9 100644 --- a/lib/compass/sass_extensions/functions/sprites.rb +++ b/lib/compass/sass_extensions/functions/sprites.rb @@ -1,20 +1,37 @@ +require 'chunky_png' + module Compass::SassExtensions::Functions::Sprites def sprite_image(uri) uri = uri.value path, name = Compass::Sprites.path_and_name(uri) - y = 0 last_spacing = 0 default_spacing = number_from_var("#{name}-spacing") + width = 0 + height = 0 images = Compass::Sprites.sprites(name) + + # Calculation images.each do |image| current_spacing = number_from_var("#{name}-#{image[:name]}-spacing") - if y > 0 - y += [current_spacing, last_spacing, default_spacing].max + if height > 0 + height += [current_spacing, last_spacing, default_spacing].max end - image[:y] = y - y += image[:height] + image[:y] = height + height += image[:height] last_spacing = current_spacing + width = image[:width] if image[:width] > width end + + # Generation + output_png = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT) + images.each do |image| + input_png = ChunkyPNG::Image.from_file(image[:file]) + x = 0 + y = image[:y] + output_png.replace input_png, x, y + end + output_png.save File.join(File.join(Compass.configuration.images_path, "#{path}.png")) + image_url(Sass::Script::String.new("#{path}.png")) end diff --git a/lib/compass/sprites.rb b/lib/compass/sprites.rb index 6130c84e..09ee3c2f 100644 --- a/lib/compass/sprites.rb +++ b/lib/compass/sprites.rb @@ -33,7 +33,7 @@ module Compass width, height = Compass::SassExtensions::Functions::ImageSize::ImageProperties.new(file).size images << { :name => File.basename(file, '.png'), - :filename => File.basename(file), + :file => file, :height => height, :width => width } diff --git a/spec/sprites_spec.rb b/spec/sprites_spec.rb index dbc2df2e..df83aeea 100644 --- a/spec/sprites_spec.rb +++ b/spec/sprites_spec.rb @@ -4,11 +4,22 @@ require "compass/sprites" describe Compass::Sprites do before :each do - Compass.configuration.images_path = File.dirname(__FILE__) + "/test_project/public/images" + @images_src_path = File.join(File.dirname(__FILE__), 'test_project', 'public', 'images') + @images_tmp_path = File.join(File.dirname(__FILE__), 'test_project', 'public', 'images-tmp') + FileUtils.cp_r @images_src_path, @images_tmp_path + Compass.configuration.images_path = @images_tmp_path Compass.configure_sass_plugin! Compass::Sprites.reset end + after :each do + FileUtils.rm_r @images_tmp_path + end + + def image_size(file) + IO.read(File.join(@images_tmp_path, file))[0x10..0x18].unpack('NN') + end + def render(scss) scss = %Q(@import "compass"; #{scss}) options = Compass.sass_engine_options @@ -41,6 +52,7 @@ describe Compass::Sprites do background-position: 0 -10px; } CSS + image_size('squares.png').should == [20, 30] end it "should generate sprite classes with dimensions" do @@ -66,6 +78,7 @@ describe Compass::Sprites do width: 20px; } CSS + image_size('squares.png').should == [20, 30] end it "should provide sprite mixin" do @@ -95,6 +108,7 @@ describe Compass::Sprites do width: 20px; } CSS + image_size('squares.png').should == [20, 30] end # CUSTOMIZATIONS: @@ -109,6 +123,7 @@ describe Compass::Sprites do background: url('/squares.png') no-repeat; } CSS + image_size('squares.png').should == [20, 30] end it "should calculate the spacing between images but not before first image" do @@ -130,6 +145,7 @@ describe Compass::Sprites do background-position: 0 -43px; } CSS + image_size('squares.png').should == [20, 63] end it "should calculate the spacing between images" do @@ -151,6 +167,7 @@ describe Compass::Sprites do background-position: 0 -43px; } CSS + image_size('squares.png').should == [20, 63] end it "should calculate the maximum spacing between images" do @@ -173,6 +190,7 @@ describe Compass::Sprites do background-position: 0 -54px; } CSS + image_size('squares.png').should == [20, 74] end it "should calculate the maximum spacing between images in reversed order" do @@ -195,6 +213,7 @@ describe Compass::Sprites do background-position: 0 -54px; } CSS + image_size('squares.png').should == [20, 74] end it "should calculate the default spacing between images" do @@ -216,6 +235,7 @@ describe Compass::Sprites do background-position: 0 -32px; } CSS + image_size('squares.png').should == [20, 52] end end \ No newline at end of file