abstracted out engine for chunkypng

This commit is contained in:
Scott Davis 2011-02-24 01:02:31 -05:00
parent 1c71df3b41
commit 03f4c23c04
4 changed files with 171 additions and 154 deletions

View File

@ -1,7 +1,7 @@
PATH
remote: .
specs:
compass (0.11.beta.2.153582f)
compass (0.11.beta.2.1c71df3)
chunky_png (~> 0.12.0)
sass (>= 3.1.0.alpha.218)

View File

@ -1,6 +1,7 @@
require 'digest/md5'
require 'compass/sass_extensions/sprites/image'
require 'compass/sass_extensions/sprites/base'
require 'compass/sass_extensions/sprites/engines/chunky_png_engine'
module Compass::SassExtensions::Functions::Sprites
ZERO = Sass::Script::Number::new(0)
@ -14,63 +15,8 @@ module Compass::SassExtensions::Functions::Sprites
end
end
class SpriteMap < Compass::SassExtensions::Sprites::Base
# Calculates the overal image dimensions
# collects image sizes and input parameters for each sprite
def compute_image_metadata!
@width = 0
init_images
compute_image_positions!
@height = @images.last.top + @images.last.height
end
class SpriteMap < Compass::SassExtensions::Sprites::ChunkyPngEngine
def init_images
@images = image_names.collect do |relative_file|
image = Compass::SassExtensions::Sprites::Image.new(relative_file, options)
@width = [ @width, image.width + image.offset ].max
image
end
end
def compute_image_positions!
@images.each_with_index do |image, index|
image.left = image.position.unit_str == "%" ? (@width - image.width) * (image.position.value / 100) : image.position.value
next if index == 0
last_image = @images[index-1]
image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max
end
end
def image_for(name)
@images.detect { |img| img.name == name}
end
def require_png_library!
begin
require 'oily_png'
rescue LoadError
require 'chunky_png'
end
end
# Returns a PNG object
def construct_sprite
require_png_library!
output_png = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT)
images.each do |image|
input_png = ChunkyPNG::Image.from_file(image.file)
if image.repeat == "no-repeat"
output_png.replace input_png, image.left, image.top
else
x = image.left - (image.left / image.width).ceil * image.width
while x < width do
output_png.replace input_png, x, image.top
x += image.width
end
end
end
output_png
end
end
# Creates a SpriteMap object. A sprite map, when used in a property is the same

View File

@ -1,7 +1,23 @@
module Compass
module SassExtensions
module Sprites
class Base < Sass::Script::Literal
module Base
module ClassMethods
def from_uri(uri, context, kwargs)
path, name = Compass::Sprites.path_and_name(uri.value)
sprites = Compass::Sprites.discover_sprites(uri.value).map do |sprite|
sprite.gsub(Compass.configuration.images_path+"/", "")
end
new(sprites, path, name, context, kwargs)
end
end
#instance Methods
module InstanceMethods
def self.included(base)
base.extend(ClassMethods)
end
# Changing this string will invalidate all previously generated sprite images.
# We should do so only when the packing algorithm changes
SPRITE_VERSION = "1"
@ -9,13 +25,6 @@ module Compass
attr_accessor :image_names, :path, :name, :options
attr_accessor :images, :width, :height
def self.from_uri(uri, context, kwargs)
path, name = Compass::Sprites.path_and_name(uri.value)
sprites = Compass::Sprites.discover_sprites(uri.value).map do |sprite|
sprite.gsub(Compass.configuration.images_path+"/", "")
end
new(sprites, path, name, context, kwargs)
end
def initialize(image_names, path, name, context, options)
@image_names, @path, @name, @options = image_names, path, name, options
@ -32,6 +41,37 @@ module Compass
[width, height]
end
# Calculates the overal image dimensions
# collects image sizes and input parameters for each sprite
def compute_image_metadata!
@width = 0
init_images
compute_image_positions!
@height = @images.last.top + @images.last.height
end
def init_images
@images = image_names.collect do |relative_file|
image = Compass::SassExtensions::Sprites::Image.new(relative_file, options)
@width = [ @width, image.width + image.offset ].max
image
end
end
# Calculates the overal image dimensions
# collects image sizes and input parameters for each sprite
def compute_image_positions!
@images.each_with_index do |image, index|
image.left = image.position.unit_str == "%" ? (@width - image.width) * (image.position.value / 100) : image.position.value
next if index == 0
last_image = @images[index-1]
image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max
end
end
def image_for(name)
@images.detect { |img| img.name == name}
end
def sprite_names
image_names.map{|f| Compass::Sprites.sprite_name(f) }
end
@ -49,11 +89,6 @@ module Compass
File.join(Compass.configuration.images_path, "#{path}-#{uniqueness_hash}.png")
end
# Calculates the overal image dimensions
# collects image sizes and input parameters for each sprite
def compute_image_metadata!
end
# Generate a sprite image if necessary
def generate
if generation_required?
@ -115,7 +150,7 @@ module Compass
super
end
end
end
end
end
end

View File

@ -0,0 +1,36 @@
module Compass
module SassExtensions
module Sprites
class ChunkyPngEngine < Sass::Script::Literal
include Compass::SassExtensions::Sprites::Base::InstanceMethods
def require_png_library!
begin
require 'oily_png'
rescue LoadError
require 'chunky_png'
end
end
# Returns a PNG object
def construct_sprite
require_png_library!
output_png = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT)
images.each do |image|
input_png = ChunkyPNG::Image.from_file(image.file)
if image.repeat == "no-repeat"
output_png.replace input_png, image.left, image.top
else
x = image.left - (image.left / image.width).ceil * image.width
while x < width do
output_png.replace input_png, x, image.top
x += image.width
end
end
end
output_png
end
end
end
end
end