From a4861298a7354c8f2cd08970fd7af21a27aa319c Mon Sep 17 00:00:00 2001 From: Scott Davis Date: Thu, 24 Feb 2011 05:21:37 -0500 Subject: [PATCH] fixed rmagic --- lib/compass/sass_extensions/sprites/base.rb | 25 +++---------------- .../sprites/engines/rmagick_engine.rb | 19 +++++++++++--- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/lib/compass/sass_extensions/sprites/base.rb b/lib/compass/sass_extensions/sprites/base.rb index b94f27fc..b6e6db67 100644 --- a/lib/compass/sass_extensions/sprites/base.rb +++ b/lib/compass/sass_extensions/sprites/base.rb @@ -111,6 +111,7 @@ module Compass end def generation_required? + puts !File.exists?(filename) || outdated? !File.exists?(filename) || outdated? end @@ -129,7 +130,6 @@ module Compass @uniqueness_hash end - # saves the sprite for later retrieval def save!(output_png) saved = output_png.save filename Compass.configuration.run_callback(:sprite_saved, filename) @@ -138,31 +138,12 @@ module Compass # All the full-path filenames involved in this sprite def image_filenames - image_names.map do |image_name| - File.join(Compass.configuration.images_path, image_name) - end - end - - def save!(output_png) - saved = output_png.save filename - Compass.configuration.run_callback(:sprite_saved, filename) - saved - end - - # All the full-path filenames involved in this sprite - def image_filenames - image_names.map do |image_name| - File.join(Compass.configuration.images_path, image_name) - end + @images.map(&:file) end # Checks whether this sprite is outdated def outdated? - last_update = self.mtime - image_filenames.each do |image| - return true if File.mtime(image) > last_update - end - false + @images.map(&:mtime).any? { |mtime| mtime > self.mtime } end def mtime diff --git a/lib/compass/sass_extensions/sprites/engines/rmagick_engine.rb b/lib/compass/sass_extensions/sprites/engines/rmagick_engine.rb index 03085075..988d3b25 100644 --- a/lib/compass/sass_extensions/sprites/engines/rmagick_engine.rb +++ b/lib/compass/sass_extensions/sprites/engines/rmagick_engine.rb @@ -5,19 +5,30 @@ module Compass class ::Magick::Image alias :save :write end + + def composite_images(dest_image, src_image, x, y) + width = [src_image.columns + x, dest_image.columns].max + height = [src_image.rows + y, dest_image.rows].max + image = Magick::Image.new(width, height) {self.background_color = 'none'} + image.composite!(dest_image, 0, 0, Magick::CopyCompositeOp) + image.composite!(src_image, x, y, Magick::CopyCompositeOp) + image + end + # Returns a PNG object def construct_sprite output_png = Magick::Image.new(width, height) - output_png.background_color = 'transparent' - output_png.format = 'PNG' + output_png.background_color = 'none' + output_png.format = 'PNG24' images.each do |image| input_png = Magick::Image.read(image.file).first if image.repeat == "no-repeat" - output_png.composite!(input_png, image.left, image.top, Magick::CopyCompositeOp) + output_png = composite_images(output_png, input_png, image.left, image.top) else x = image.left - (image.left / image.width).ceil * image.width while x < width do - output_png.composite!(input_png, x, image.top, Magick::CopyCompositeOp) + output_png = composite_images(output_png, input_png, x, image.top) + #output_png.composite!(input_png, x, image.top, Magick::CopyCompositeOp) x += image.width end end