rmagick changes
This commit is contained in:
parent
03f4c23c04
commit
bc13d6aed5
1
Gemfile
1
Gemfile
@ -15,3 +15,4 @@ gem "chunky_png", "~> 0.12.0"
|
||||
|
||||
gem "ruby-prof" unless RUBY_PLATFORM == "java"
|
||||
|
||||
#gem 'rmagick'
|
@ -1,7 +1,7 @@
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
compass (0.11.beta.2.1c71df3)
|
||||
compass (0.11.beta.2.03f4c23)
|
||||
chunky_png (~> 0.12.0)
|
||||
sass (>= 3.1.0.alpha.218)
|
||||
|
||||
|
@ -1,7 +1,5 @@
|
||||
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)
|
||||
@ -15,7 +13,7 @@ module Compass::SassExtensions::Functions::Sprites
|
||||
end
|
||||
end
|
||||
|
||||
class SpriteMap < Compass::SassExtensions::Sprites::ChunkyPngEngine
|
||||
class SpriteMap < Compass::SassExtensions::Sprites::Base
|
||||
|
||||
end
|
||||
|
||||
|
@ -1,21 +1,32 @@
|
||||
require 'compass/sass_extensions/sprites/image'
|
||||
module Compass
|
||||
module SassExtensions
|
||||
module Sprites
|
||||
module Base
|
||||
module ClassMethods
|
||||
def from_uri(uri, context, kwargs)
|
||||
class Base < Sass::Script::Literal
|
||||
|
||||
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
|
||||
end
|
||||
#instance Methods
|
||||
module InstanceMethods
|
||||
|
||||
def self.included(base)
|
||||
base.extend(ClassMethods)
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
# Changing this string will invalidate all previously generated sprite images.
|
||||
@ -27,6 +38,7 @@ module Compass
|
||||
|
||||
|
||||
def initialize(image_names, path, name, context, options)
|
||||
require_engine!
|
||||
@image_names, @path, @name, @options = image_names, path, name, options
|
||||
@images = nil
|
||||
@width = nil
|
||||
@ -131,6 +143,32 @@ module Compass
|
||||
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
|
||||
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
|
||||
end
|
||||
|
||||
def mtime
|
||||
File.mtime(filename)
|
||||
end
|
||||
|
||||
def inspect
|
||||
to_s
|
||||
end
|
||||
@ -154,4 +192,3 @@ module Compass
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,20 +1,10 @@
|
||||
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
|
||||
|
||||
module ChunkyPngEngine
|
||||
# Returns a PNG object
|
||||
def construct_sprite
|
||||
require_png_library!
|
||||
#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)
|
||||
|
@ -0,0 +1,30 @@
|
||||
module Compass
|
||||
module SassExtensions
|
||||
module Sprites
|
||||
module RmagickEngine
|
||||
class ::Magick::Image
|
||||
alias :save :write
|
||||
end
|
||||
# Returns a PNG object
|
||||
def construct_sprite
|
||||
output_png = Magick::Image.new(width, height)
|
||||
output_png.background_color = 'transparent'
|
||||
output_png.format = 'PNG'
|
||||
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)
|
||||
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)
|
||||
x += image.width
|
||||
end
|
||||
end
|
||||
end
|
||||
output_png
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user