start factoring out sprite images
This commit is contained in:
parent
c521908947
commit
f3957daef7
@ -1,4 +1,5 @@
|
|||||||
require 'digest/md5'
|
require 'digest/md5'
|
||||||
|
require 'compass/sass_extensions/sprites/image'
|
||||||
|
|
||||||
module Compass::SassExtensions::Functions::Sprites
|
module Compass::SassExtensions::Functions::Sprites
|
||||||
ZERO = Sass::Script::Number::new(0)
|
ZERO = Sass::Script::Number::new(0)
|
||||||
@ -54,65 +55,32 @@ module Compass::SassExtensions::Functions::Sprites
|
|||||||
# Calculates the overal image dimensions
|
# Calculates the overal image dimensions
|
||||||
# collects image sizes and input parameters for each sprite
|
# collects image sizes and input parameters for each sprite
|
||||||
def compute_image_metadata!
|
def compute_image_metadata!
|
||||||
@images = []
|
|
||||||
@width = 0
|
@width = 0
|
||||||
image_names.each do |relative_file|
|
|
||||||
file = File.join(Compass.configuration.images_path, relative_file)
|
@images = image_names.collect do |relative_file|
|
||||||
width, height = Compass::SassExtensions::Functions::ImageSize::ImageProperties.new(file).size
|
image = Compass::SassExtensions::Sprites::Image.new(relative_file, options)
|
||||||
sprite_name = Compass::Sprites.sprite_name(relative_file)
|
@width = [ @width, image.width + image.offset ].max
|
||||||
position = position_for(sprite_name)
|
image
|
||||||
offset = (position.unitless? || position.unit_str == "px") ? position.value : 0
|
|
||||||
@images << {
|
|
||||||
:name => sprite_name,
|
|
||||||
:file => file,
|
|
||||||
:relative_file => relative_file,
|
|
||||||
:height => height,
|
|
||||||
:width => width,
|
|
||||||
:repeat => repeat_for(sprite_name),
|
|
||||||
:spacing => spacing_for(sprite_name),
|
|
||||||
:position => position,
|
|
||||||
:digest => Digest::MD5.file(file).hexdigest
|
|
||||||
}
|
|
||||||
@width = [@width, width + offset].max
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@images.each_with_index do |image, index|
|
@images.each_with_index do |image, index|
|
||||||
if index == 0
|
if index == 0
|
||||||
image[:top] = 0
|
image.top = 0
|
||||||
else
|
else
|
||||||
last_image = @images[index-1]
|
last_image = @images[index-1]
|
||||||
image[:top] = last_image[:top] + last_image[:height] + [image[:spacing], last_image[:spacing]].max
|
image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max
|
||||||
end
|
end
|
||||||
if image[:position].unit_str == "%"
|
if image.position.unit_str == "%"
|
||||||
image[:left] = (@width - image[:width]) * (image[:position].value / 100)
|
image.left = (@width - image.width) * (image.position.value / 100)
|
||||||
else
|
else
|
||||||
image[:left] = image[:position].value
|
image.left = image.position.value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@height = @images.last[:top] + @images.last[:height]
|
@height = @images.last.top + @images.last.height
|
||||||
end
|
|
||||||
|
|
||||||
def position_for(name)
|
|
||||||
options.get_var("#{name}-position") || options.get_var("position") || Sass::Script::Number.new(0, ["px"])
|
|
||||||
end
|
|
||||||
|
|
||||||
def repeat_for(name)
|
|
||||||
if (var = options.get_var("#{name}-repeat"))
|
|
||||||
var.value
|
|
||||||
elsif (var = options.get_var("repeat"))
|
|
||||||
var.value
|
|
||||||
else
|
|
||||||
"no-repeat"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def spacing_for(name)
|
|
||||||
(options.get_var("#{name}-spacing") ||
|
|
||||||
options.get_var("spacing") ||
|
|
||||||
ZERO).value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def image_for(name)
|
def image_for(name)
|
||||||
@images.detect{|img| img[:name] == name}
|
@images.detect { |img| img.name == name}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Calculate the size of the sprite
|
# Calculate the size of the sprite
|
||||||
@ -146,14 +114,14 @@ module Compass::SassExtensions::Functions::Sprites
|
|||||||
require_png_library!
|
require_png_library!
|
||||||
output_png = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT)
|
output_png = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT)
|
||||||
images.each do |image|
|
images.each do |image|
|
||||||
input_png = ChunkyPNG::Image.from_file(image[:file])
|
input_png = ChunkyPNG::Image.from_file(image.file)
|
||||||
if image[:repeat] == "no-repeat"
|
if image.repeat == "no-repeat"
|
||||||
output_png.replace input_png, image[:left], image[:top]
|
output_png.replace input_png, image.left, image.top
|
||||||
else
|
else
|
||||||
x = image[:left] - (image[:left] / image[:width]).ceil * image[:width]
|
x = image.left - (image.left / image.width).ceil * image.width
|
||||||
while x < width do
|
while x < width do
|
||||||
output_png.replace input_png, x, image[:top]
|
output_png.replace input_png, x, image.top
|
||||||
x += image[:width]
|
x += image.width
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -172,7 +140,7 @@ module Compass::SassExtensions::Functions::Sprites
|
|||||||
sum << path
|
sum << path
|
||||||
images.each do |image|
|
images.each do |image|
|
||||||
[:relative_file, :height, :width, :repeat, :spacing, :position, :digest].each do |attr|
|
[:relative_file, :height, :width, :repeat, :spacing, :position, :digest].each do |attr|
|
||||||
sum << image[attr].to_s
|
sum << image.send(attr).to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
sum.hexdigest[0...10]
|
sum.hexdigest[0...10]
|
||||||
@ -335,10 +303,10 @@ module Compass::SassExtensions::Functions::Sprites
|
|||||||
if offset_x.unit_str == "%"
|
if offset_x.unit_str == "%"
|
||||||
x = offset_x # CE: Shouldn't this be a percentage of the total width?
|
x = offset_x # CE: Shouldn't this be a percentage of the total width?
|
||||||
else
|
else
|
||||||
x = offset_x.value - image[:left]
|
x = offset_x.value - image.left
|
||||||
x = Sass::Script::Number.new(x, x == 0 ? [] : ["px"])
|
x = Sass::Script::Number.new(x, x == 0 ? [] : ["px"])
|
||||||
end
|
end
|
||||||
y = offset_y.value - image[:top]
|
y = offset_y.value - image.top
|
||||||
y = Sass::Script::Number.new(y, y == 0 ? [] : ["px"])
|
y = Sass::Script::Number.new(y, y == 0 ? [] : ["px"])
|
||||||
Sass::Script::List.new([x, y],:space)
|
Sass::Script::List.new([x, y],:space)
|
||||||
end
|
end
|
||||||
|
@ -5,3 +5,17 @@ require 'rubygems'
|
|||||||
require 'compass'
|
require 'compass'
|
||||||
require 'rspec'
|
require 'rspec'
|
||||||
require 'rspec/autorun'
|
require 'rspec/autorun'
|
||||||
|
|
||||||
|
module CompassGlobalInclude
|
||||||
|
class << self
|
||||||
|
def included(klass)
|
||||||
|
klass.instance_eval do
|
||||||
|
let(:images_src_path) { File.join(File.dirname(__FILE__), 'test_project', 'public', 'images') }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
RSpec.configure do |config|
|
||||||
|
config.include(CompassGlobalInclude)
|
||||||
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user