add support for alternate file sources for image size analysis
This commit is contained in:
parent
b1c34a24f2
commit
4dd1223682
@ -40,7 +40,8 @@ module Compass
|
||||
:preferred_syntax,
|
||||
:disable_warnings,
|
||||
:sprite_engine,
|
||||
:chunky_png_options
|
||||
:chunky_png_options,
|
||||
:images_file_source
|
||||
].flatten
|
||||
|
||||
ARRAY_ATTRIBUTES = [
|
||||
|
@ -24,12 +24,22 @@ module Compass::SassExtensions::Functions::ImageSize
|
||||
end
|
||||
|
||||
private
|
||||
def open_file_source
|
||||
source = Compass.configuration.images_file_source || lambda { |file| File.open(file, 'rb') }
|
||||
|
||||
io = source.call(@file)
|
||||
result = yield io
|
||||
io.close
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
def get_size_for_png
|
||||
File.open(@file, "rb") {|io| io.read}[0x10..0x18].unpack('NN')
|
||||
open_file_source {|io| io.read}[0x10..0x18].unpack('NN')
|
||||
end
|
||||
|
||||
def get_size_for_gif
|
||||
File.open(@file, "rb") {|io| io.read}[6..10].unpack('SS')
|
||||
open_file_source {|io| io.read}[6..10].unpack('SS')
|
||||
end
|
||||
|
||||
def get_size_for_jpg
|
||||
@ -37,10 +47,12 @@ module Compass::SassExtensions::Functions::ImageSize
|
||||
end
|
||||
|
||||
def get_size_for_jpeg
|
||||
jpeg = JPEG.new(@file)
|
||||
open_file_source do |io|
|
||||
jpeg = JPEG.new(io)
|
||||
[ jpeg.width, jpeg.height ]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@ -69,12 +81,8 @@ private
|
||||
class JPEG
|
||||
attr_reader :width, :height, :bits
|
||||
|
||||
def initialize(file)
|
||||
if file.kind_of? IO
|
||||
examine(file)
|
||||
else
|
||||
File.open(file, 'rb') { |io| examine(io) }
|
||||
end
|
||||
def initialize(io)
|
||||
examine(io)
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -167,7 +167,6 @@ class SassExtensionsTest < Test::Unit::TestCase
|
||||
assert_equal "url('data:font/truetype;base64,#{base64_string}') format('truetype')", evaluate("inline_font_files('bgrove.ttf', truetype)")
|
||||
end
|
||||
|
||||
|
||||
def test_image_size_should_respond_to_to_path
|
||||
object = mock()
|
||||
object.expects(:to_path).returns('foo.jpg')
|
||||
@ -176,6 +175,20 @@ class SassExtensionsTest < Test::Unit::TestCase
|
||||
Compass::SassExtensions::Functions::ImageSize::ImageProperties.new(object)
|
||||
end
|
||||
|
||||
def test_image_properites_should_be_able_to_use_file_like_object
|
||||
source = Class.new do
|
||||
def self.open(*args, &block)
|
||||
File.open(*args, &block)
|
||||
end
|
||||
end
|
||||
|
||||
Compass.configuration.images_file_source = lambda { |file| source.open(file) }
|
||||
|
||||
properties = Compass::SassExtensions::Functions::ImageSize::ImageProperties.new('test/fixtures/sprites/public/images/colors/blue.png')
|
||||
|
||||
assert_equal [ 10, 10 ], properties.size
|
||||
end
|
||||
|
||||
def test_reject
|
||||
assert_equal "b d", evaluate("reject(a b c d, a, c)")
|
||||
assert_equal "a b c d", evaluate("reject(a b c d, e)")
|
||||
|
Loading…
Reference in New Issue
Block a user