From c0a0b638b1387d7ee484ffa4c6ae843f85573202 Mon Sep 17 00:00:00 2001 From: Scott Davis Date: Wed, 2 Mar 2011 13:55:25 -0500 Subject: [PATCH] tests are green and config option added --- Gemfile.lock | 2 +- lib/compass/configuration.rb | 3 +- lib/compass/configuration/defaults.rb | 5 +++ lib/compass/sass_extensions/sprites/base.rb | 24 ++++------- .../sprites/engines/chunky_png_engine.rb | 7 ++++ .../sprites/engines/rmagick_engine.rb | 41 ------------------- 6 files changed, 24 insertions(+), 58 deletions(-) delete mode 100644 lib/compass/sass_extensions/sprites/engines/rmagick_engine.rb diff --git a/Gemfile.lock b/Gemfile.lock index f078f652..f199dee7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - compass (0.11.beta.2.03f4c23) + compass (0.11.beta.2.a486129) chunky_png (~> 0.12.0) sass (>= 3.1.0.alpha.218) diff --git a/lib/compass/configuration.rb b/lib/compass/configuration.rb index 3fa3e9ef..c4e35336 100644 --- a/lib/compass/configuration.rb +++ b/lib/compass/configuration.rb @@ -37,7 +37,8 @@ module Compass :line_comments, :color_output, :preferred_syntax, - :disable_warnings + :disable_warnings, + :sprite_engine ].flatten end diff --git a/lib/compass/configuration/defaults.rb b/lib/compass/configuration/defaults.rb index db63bd3e..ec6ce392 100644 --- a/lib/compass/configuration/defaults.rb +++ b/lib/compass/configuration/defaults.rb @@ -128,6 +128,11 @@ module Compass def default_preferred_syntax :scss end + + def default_sprite_engine + :chunky_png + end + # helper functions def http_join(*segments) diff --git a/lib/compass/sass_extensions/sprites/base.rb b/lib/compass/sass_extensions/sprites/base.rb index b6e6db67..112db850 100644 --- a/lib/compass/sass_extensions/sprites/base.rb +++ b/lib/compass/sass_extensions/sprites/base.rb @@ -1,4 +1,5 @@ require 'compass/sass_extensions/sprites/image' +require 'compass/sass_extensions/sprites/engines/chunky_png_engine' module Compass module SassExtensions module Sprites @@ -12,21 +13,8 @@ module Compass new(sprites, path, name, context, kwargs) end - def require_engine! - begin - require 'rmagick' - require 'compass/sass_extensions/sprites/engines/rmagick_engine' - self.class.send(:include, ::Compass::SassExtensions::Sprites::RmagickEngine) - rescue LoadError - require 'compass/sass_extensions/sprites/engines/chunky_png_engine' - begin - require 'oily_png' - rescue LoadError - require 'chunky_png' - end - self.class.send(:include, ::Compass::SassExtensions::Sprites::ChunkyPngEngine) - end + self.class.send(:include, eval("::Compass::SassExtensions::Sprites::#{modulize}Engine")) end # Changing this string will invalidate all previously generated sprite images. @@ -111,7 +99,6 @@ module Compass end def generation_required? - puts !File.exists?(filename) || outdated? !File.exists?(filename) || outdated? end @@ -169,6 +156,13 @@ module Compass super end end + + private + + def modulize + @modulize ||= Compass::configuration.sprite_engine.to_s.scan(/([^_.]+)/).flatten.map {|chunk| "#{chunk[0].chr.upcase}#{chunk[1..-1]}" }.join + end + end end end diff --git a/lib/compass/sass_extensions/sprites/engines/chunky_png_engine.rb b/lib/compass/sass_extensions/sprites/engines/chunky_png_engine.rb index 3997aee6..f9a43e8a 100644 --- a/lib/compass/sass_extensions/sprites/engines/chunky_png_engine.rb +++ b/lib/compass/sass_extensions/sprites/engines/chunky_png_engine.rb @@ -1,7 +1,14 @@ +begin + require 'oily_png' +rescue LoadError + require 'chunky_png' +end + module Compass module SassExtensions module Sprites module ChunkyPngEngine + # Returns a PNG object def construct_sprite #require_png_library! diff --git a/lib/compass/sass_extensions/sprites/engines/rmagick_engine.rb b/lib/compass/sass_extensions/sprites/engines/rmagick_engine.rb deleted file mode 100644 index 988d3b25..00000000 --- a/lib/compass/sass_extensions/sprites/engines/rmagick_engine.rb +++ /dev/null @@ -1,41 +0,0 @@ -module Compass - module SassExtensions - module Sprites - module RmagickEngine - 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 = '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_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_images(output_png, input_png, x, image.top) - #output_png.composite!(input_png, x, image.top, Magick::CopyCompositeOp) - x += image.width - end - end - end - output_png - end - end - end - end -end \ No newline at end of file