generation of png file
This commit is contained in:
parent
7077b76225
commit
c0c39c53d7
@ -1,20 +1,37 @@
|
|||||||
|
require 'chunky_png'
|
||||||
|
|
||||||
module Compass::SassExtensions::Functions::Sprites
|
module Compass::SassExtensions::Functions::Sprites
|
||||||
def sprite_image(uri)
|
def sprite_image(uri)
|
||||||
uri = uri.value
|
uri = uri.value
|
||||||
path, name = Compass::Sprites.path_and_name(uri)
|
path, name = Compass::Sprites.path_and_name(uri)
|
||||||
y = 0
|
|
||||||
last_spacing = 0
|
last_spacing = 0
|
||||||
default_spacing = number_from_var("#{name}-spacing")
|
default_spacing = number_from_var("#{name}-spacing")
|
||||||
|
width = 0
|
||||||
|
height = 0
|
||||||
images = Compass::Sprites.sprites(name)
|
images = Compass::Sprites.sprites(name)
|
||||||
|
|
||||||
|
# Calculation
|
||||||
images.each do |image|
|
images.each do |image|
|
||||||
current_spacing = number_from_var("#{name}-#{image[:name]}-spacing")
|
current_spacing = number_from_var("#{name}-#{image[:name]}-spacing")
|
||||||
if y > 0
|
if height > 0
|
||||||
y += [current_spacing, last_spacing, default_spacing].max
|
height += [current_spacing, last_spacing, default_spacing].max
|
||||||
end
|
end
|
||||||
image[:y] = y
|
image[:y] = height
|
||||||
y += image[:height]
|
height += image[:height]
|
||||||
last_spacing = current_spacing
|
last_spacing = current_spacing
|
||||||
|
width = image[:width] if image[:width] > width
|
||||||
end
|
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"))
|
image_url(Sass::Script::String.new("#{path}.png"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ module Compass
|
|||||||
width, height = Compass::SassExtensions::Functions::ImageSize::ImageProperties.new(file).size
|
width, height = Compass::SassExtensions::Functions::ImageSize::ImageProperties.new(file).size
|
||||||
images << {
|
images << {
|
||||||
:name => File.basename(file, '.png'),
|
:name => File.basename(file, '.png'),
|
||||||
:filename => File.basename(file),
|
:file => file,
|
||||||
:height => height,
|
:height => height,
|
||||||
:width => width
|
:width => width
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,22 @@ require "compass/sprites"
|
|||||||
describe Compass::Sprites do
|
describe Compass::Sprites do
|
||||||
|
|
||||||
before :each 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.configure_sass_plugin!
|
||||||
Compass::Sprites.reset
|
Compass::Sprites.reset
|
||||||
end
|
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)
|
def render(scss)
|
||||||
scss = %Q(@import "compass"; #{scss})
|
scss = %Q(@import "compass"; #{scss})
|
||||||
options = Compass.sass_engine_options
|
options = Compass.sass_engine_options
|
||||||
@ -41,6 +52,7 @@ describe Compass::Sprites do
|
|||||||
background-position: 0 -10px;
|
background-position: 0 -10px;
|
||||||
}
|
}
|
||||||
CSS
|
CSS
|
||||||
|
image_size('squares.png').should == [20, 30]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should generate sprite classes with dimensions" do
|
it "should generate sprite classes with dimensions" do
|
||||||
@ -66,6 +78,7 @@ describe Compass::Sprites do
|
|||||||
width: 20px;
|
width: 20px;
|
||||||
}
|
}
|
||||||
CSS
|
CSS
|
||||||
|
image_size('squares.png').should == [20, 30]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should provide sprite mixin" do
|
it "should provide sprite mixin" do
|
||||||
@ -95,6 +108,7 @@ describe Compass::Sprites do
|
|||||||
width: 20px;
|
width: 20px;
|
||||||
}
|
}
|
||||||
CSS
|
CSS
|
||||||
|
image_size('squares.png').should == [20, 30]
|
||||||
end
|
end
|
||||||
|
|
||||||
# CUSTOMIZATIONS:
|
# CUSTOMIZATIONS:
|
||||||
@ -109,6 +123,7 @@ describe Compass::Sprites do
|
|||||||
background: url('/squares.png') no-repeat;
|
background: url('/squares.png') no-repeat;
|
||||||
}
|
}
|
||||||
CSS
|
CSS
|
||||||
|
image_size('squares.png').should == [20, 30]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should calculate the spacing between images but not before first image" do
|
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;
|
background-position: 0 -43px;
|
||||||
}
|
}
|
||||||
CSS
|
CSS
|
||||||
|
image_size('squares.png').should == [20, 63]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should calculate the spacing between images" do
|
it "should calculate the spacing between images" do
|
||||||
@ -151,6 +167,7 @@ describe Compass::Sprites do
|
|||||||
background-position: 0 -43px;
|
background-position: 0 -43px;
|
||||||
}
|
}
|
||||||
CSS
|
CSS
|
||||||
|
image_size('squares.png').should == [20, 63]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should calculate the maximum spacing between images" do
|
it "should calculate the maximum spacing between images" do
|
||||||
@ -173,6 +190,7 @@ describe Compass::Sprites do
|
|||||||
background-position: 0 -54px;
|
background-position: 0 -54px;
|
||||||
}
|
}
|
||||||
CSS
|
CSS
|
||||||
|
image_size('squares.png').should == [20, 74]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should calculate the maximum spacing between images in reversed order" do
|
it "should calculate the maximum spacing between images in reversed order" do
|
||||||
@ -195,6 +213,7 @@ describe Compass::Sprites do
|
|||||||
background-position: 0 -54px;
|
background-position: 0 -54px;
|
||||||
}
|
}
|
||||||
CSS
|
CSS
|
||||||
|
image_size('squares.png').should == [20, 74]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should calculate the default spacing between images" do
|
it "should calculate the default spacing between images" do
|
||||||
@ -216,6 +235,7 @@ describe Compass::Sprites do
|
|||||||
background-position: 0 -32px;
|
background-position: 0 -32px;
|
||||||
}
|
}
|
||||||
CSS
|
CSS
|
||||||
|
image_size('squares.png').should == [20, 52]
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user