refactored engine to be a class and turned on best compression for chunky_png

This commit is contained in:
Scott Davis 2011-06-12 01:35:19 -04:00
parent 6f95c9d17a
commit b6cf4f5213

View File

@ -7,20 +7,19 @@ end
module Compass module Compass
module SassExtensions module SassExtensions
module Sprites module Sprites
module ChunkyPngEngine class ChunkyPngEngine < Compass::SassExtensions::Sprites::Engine
# Returns a PNG object
def construct_sprite def construct_sprite
output_png = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT) @canvas = 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 canvas.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
begin begin
output_png.replace! input_png, x, image.top canvas.replace! input_png, x, image.top
x += image.width x += image.width
rescue ChunkyPNG::OutOfBounds rescue ChunkyPNG::OutOfBounds
break; break;
@ -28,8 +27,15 @@ module Compass
end end
end end
end end
output_png end
def save(filename)
if canvas.nil?
construct_sprite
end
canvas.save(filename, :best_compression)
end end
end end
end end
end end