fixed repeat-x issues closes #633"
This commit is contained in:
parent
f8377d15ac
commit
6194cb478a
@ -13,19 +13,7 @@ module Compass
|
|||||||
@canvas = 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.no_repeat?
|
canvas.replace! input_png, image.left, image.top
|
||||||
canvas.replace! input_png, image.left, image.top
|
|
||||||
else
|
|
||||||
x = image.left - (image.left / image.width).ceil * image.width
|
|
||||||
while x < width do
|
|
||||||
begin
|
|
||||||
canvas.replace! input_png, x, image.top
|
|
||||||
x += image.width
|
|
||||||
rescue ChunkyPNG::OutOfBounds
|
|
||||||
break;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -52,9 +52,33 @@ module Compass
|
|||||||
@width = width_for_vertical_layout
|
@width = width_for_vertical_layout
|
||||||
calulate_vertical_postions
|
calulate_vertical_postions
|
||||||
@height = height_for_vertical_layout
|
@height = height_for_vertical_layout
|
||||||
|
if @images.any?(&:repeat_x?)
|
||||||
|
calculate_repeat_extra_width!
|
||||||
|
tile_images_that_repeat
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def tile_images_that_repeat
|
||||||
|
@images.map {|img| img if img.repeat_x?}.compact.each do |image|
|
||||||
|
x = image.left - (image.left / image.width).ceil * image.width
|
||||||
|
while x < @width do
|
||||||
|
begin
|
||||||
|
img = image.dup
|
||||||
|
img.top = image.top
|
||||||
|
img.left = x.to_i
|
||||||
|
@images << img
|
||||||
|
x += image.width
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def calculate_repeat_extra_width!
|
||||||
|
m = @images.inject(1) {|m,img| img.repeat_x? ? m.lcm(img.width) : m}
|
||||||
|
remainder = @width % m
|
||||||
|
@width += (m - remainder) unless remainder.zero?
|
||||||
|
end
|
||||||
|
|
||||||
def calculate_smart_positions
|
def calculate_smart_positions
|
||||||
fitter = ::Compass::SassExtensions::Sprites::RowFitter.new(@images)
|
fitter = ::Compass::SassExtensions::Sprites::RowFitter.new(@images)
|
||||||
@ -104,7 +128,7 @@ module Compass
|
|||||||
|
|
||||||
def calulate_vertical_postions
|
def calulate_vertical_postions
|
||||||
@images.each_with_index do |image, index|
|
@images.each_with_index do |image, index|
|
||||||
image.left = image.position.unit_str == "%" ? (@width - image.width) * (image.position.value / 100.0) : image.position.value
|
image.left = (image.position.unit_str == "%" ? (@width - image.width) * (image.position.value / 100.0) : image.position.value).to_i
|
||||||
next if index == 0
|
next if index == 0
|
||||||
last_image = @images[index-1]
|
last_image = @images[index-1]
|
||||||
image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max
|
image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max
|
||||||
|
BIN
test/fixtures/sprites/public/images/repeat_x/four.png
vendored
Normal file
BIN
test/fixtures/sprites/public/images/repeat_x/four.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.7 KiB |
Binary file not shown.
@ -380,7 +380,7 @@ class SpritesTest < Test::Unit::TestCase
|
|||||||
SCSS
|
SCSS
|
||||||
assert_correct css, <<-CSS
|
assert_correct css, <<-CSS
|
||||||
.squares-sprite, .squares-ten-by-ten, .squares-twenty-by-twenty {
|
.squares-sprite, .squares-ten-by-ten, .squares-twenty-by-twenty {
|
||||||
background: url('/squares-s13833277b3.png') no-repeat;
|
background: url('/squares-s65c43cd573.png') no-repeat;
|
||||||
}
|
}
|
||||||
|
|
||||||
.squares-ten-by-ten {
|
.squares-ten-by-ten {
|
||||||
@ -628,7 +628,7 @@ class SpritesTest < Test::Unit::TestCase
|
|||||||
SCSS
|
SCSS
|
||||||
assert_correct css, <<-CSS
|
assert_correct css, <<-CSS
|
||||||
.ko-sprite, .ko-default_background, .ko-starbg26x27 {
|
.ko-sprite, .ko-default_background, .ko-starbg26x27 {
|
||||||
background: url('/ko-sd6b4d44430.png') no-repeat;
|
background: url('/ko-sd46dfbab4f.png') no-repeat;
|
||||||
}
|
}
|
||||||
|
|
||||||
.ko-default_background {
|
.ko-default_background {
|
||||||
|
@ -23,6 +23,12 @@ require 'compass'
|
|||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
|
|
||||||
|
|
||||||
|
class String
|
||||||
|
def name
|
||||||
|
to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
%w(command_line diff io rails test_case).each do |helper|
|
%w(command_line diff io rails test_case).each do |helper|
|
||||||
require "helpers/#{helper}"
|
require "helpers/#{helper}"
|
||||||
end
|
end
|
||||||
@ -60,8 +66,9 @@ module SpriteHelper
|
|||||||
end
|
end
|
||||||
|
|
||||||
def clean_up_sprites
|
def clean_up_sprites
|
||||||
init_sprite_helper
|
init_sprite_helper
|
||||||
::FileUtils.rm_r @images_tmp_path
|
::FileUtils.rm_r @images_tmp_path
|
||||||
|
rescue Errno::ENOENT => e
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
@ -13,7 +13,7 @@ class ActionsTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
# When log4r is included, it sometimes breaks the Actions
|
# When log4r is included, it sometimes breaks the Actions
|
||||||
def test_quiet_option
|
test "test_quiet_option" do
|
||||||
b = BaseActionExtender.new
|
b = BaseActionExtender.new
|
||||||
b.logger = ""
|
b.logger = ""
|
||||||
b.options[:quiet] = true
|
b.options[:quiet] = true
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class EngineTest < Test::Unit::TestCase
|
class EngineTest < Test::Unit::TestCase
|
||||||
|
include SpriteHelper
|
||||||
def setup
|
def setup
|
||||||
|
create_sprite_temp
|
||||||
sprite_filename = 'squares/ten-by-ten.png'
|
sprite_filename = 'squares/ten-by-ten.png'
|
||||||
@images = [
|
@images = [
|
||||||
Compass::SassExtensions::Sprites::Image.new(nil, File.join(sprite_filename), {})
|
Compass::SassExtensions::Sprites::Image.new(nil, File.join(sprite_filename), {})
|
||||||
]
|
]
|
||||||
@engine = Compass::SassExtensions::Sprites::Engine.new(100, 100, @images)
|
@engine = Compass::SassExtensions::Sprites::Engine.new(100, 100, @images)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def taredown
|
||||||
|
clean_up_sprites
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
test "should have width of 100" do
|
test "should have width of 100" do
|
||||||
|
@ -3,6 +3,7 @@ require 'test_helper'
|
|||||||
class ImageRowTest < Test::Unit::TestCase
|
class ImageRowTest < Test::Unit::TestCase
|
||||||
include SpriteHelper
|
include SpriteHelper
|
||||||
def setup
|
def setup
|
||||||
|
clean_up_sprites
|
||||||
create_sprite_temp
|
create_sprite_temp
|
||||||
file = StringIO.new("images_path = #{@images_src_path.inspect}\n")
|
file = StringIO.new("images_path = #{@images_src_path.inspect}\n")
|
||||||
Compass.add_configuration(file, "sprite_config")
|
Compass.add_configuration(file, "sprite_config")
|
||||||
|
@ -5,6 +5,7 @@ class LayoutTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
def setup
|
def setup
|
||||||
Hash.send(:include, Compass::SassExtensions::Functions::Sprites::VariableReader)
|
Hash.send(:include, Compass::SassExtensions::Functions::Sprites::VariableReader)
|
||||||
|
clean_up_sprites
|
||||||
create_sprite_temp
|
create_sprite_temp
|
||||||
file = StringIO.new("images_path = #{@images_tmp_path.inspect}\n")
|
file = StringIO.new("images_path = #{@images_tmp_path.inspect}\n")
|
||||||
Compass.add_configuration(file, "sprite_config")
|
Compass.add_configuration(file, "sprite_config")
|
||||||
@ -50,6 +51,21 @@ class LayoutTest < Test::Unit::TestCase
|
|||||||
sprite_map_test(opts)
|
sprite_map_test(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# REPEAT_X
|
||||||
|
|
||||||
|
test 'repeat-x layout single image' do
|
||||||
|
opts = {"repeat_x_three_repeat" => Sass::Script::String.new('repeat-x')}
|
||||||
|
map = sprite_map_test(@options.merge(opts), 'repeat_x/*.png')
|
||||||
|
assert_equal 6, map.width
|
||||||
|
assert_equal [0, 4, 7, 9, 14, 4, 4], map.images.map(&:top)
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'repeat-x layout multi image' do
|
||||||
|
opts = {"repeat_x_three_repeat" => Sass::Script::String.new('repeat-x'), "repeat_x_four_repeat" => Sass::Script::String.new('repeat-x')}
|
||||||
|
map = sprite_map_test(@options.merge(opts), 'repeat_x/*.png')
|
||||||
|
assert_equal 12, map.width
|
||||||
|
end
|
||||||
|
|
||||||
# VERTICAL LAYOUT
|
# VERTICAL LAYOUT
|
||||||
|
|
||||||
it "should have a vertical layout" do
|
it "should have a vertical layout" do
|
||||||
|
Loading…
Reference in New Issue
Block a user