From 6194cb478a59b5c60b01420ea1a17e88f66c2991 Mon Sep 17 00:00:00 2001 From: Scott Davis Date: Sat, 10 Dec 2011 12:49:34 -0500 Subject: [PATCH] fixed repeat-x issues closes #633" --- .../sprites/engines/chunky_png_engine.rb | 14 +--------- .../sass_extensions/sprites/layout_methods.rb | 26 +++++++++++++++++- .../sprites/public/images/repeat_x/four.png | Bin 0 -> 2802 bytes .../sprites/public/images/repeat_x/four.xcf | Bin 656 -> 0 bytes test/integrations/sprites_test.rb | 4 +-- test/test_helper.rb | 11 ++++++-- test/units/actions_test.rb | 2 +- test/units/sprites/engine_test.rb | 7 ++++- test/units/sprites/image_row_test.rb | 1 + test/units/sprites/layout_test.rb | 16 +++++++++++ 10 files changed, 61 insertions(+), 20 deletions(-) create mode 100644 test/fixtures/sprites/public/images/repeat_x/four.png delete mode 100644 test/fixtures/sprites/public/images/repeat_x/four.xcf 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 0e51a82e..19c210c7 100644 --- a/lib/compass/sass_extensions/sprites/engines/chunky_png_engine.rb +++ b/lib/compass/sass_extensions/sprites/engines/chunky_png_engine.rb @@ -13,19 +13,7 @@ module Compass @canvas = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT) images.each do |image| input_png = ChunkyPNG::Image.from_file(image.file) - if image.no_repeat? - 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 + canvas.replace! input_png, image.left, image.top end end diff --git a/lib/compass/sass_extensions/sprites/layout_methods.rb b/lib/compass/sass_extensions/sprites/layout_methods.rb index 151f9ee1..dea34db0 100644 --- a/lib/compass/sass_extensions/sprites/layout_methods.rb +++ b/lib/compass/sass_extensions/sprites/layout_methods.rb @@ -52,9 +52,33 @@ module Compass @width = width_for_vertical_layout calulate_vertical_postions @height = height_for_vertical_layout + if @images.any?(&:repeat_x?) + calculate_repeat_extra_width! + tile_images_that_repeat + 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 fitter = ::Compass::SassExtensions::Sprites::RowFitter.new(@images) @@ -104,7 +128,7 @@ module Compass def calulate_vertical_postions @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 last_image = @images[index-1] image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max diff --git a/test/fixtures/sprites/public/images/repeat_x/four.png b/test/fixtures/sprites/public/images/repeat_x/four.png new file mode 100644 index 0000000000000000000000000000000000000000..079a84e6d2f5f04d878c67a7e5f1b56394e9095d GIT binary patch literal 2802 zcmVEX>4Tx0C?J+Q+HUC_ZB|i_hk=OLfG)Jmu!ImA|tE_$Pihg5Rw34gb)%y#f69p zRumNxoJdu~g4GI0orvO~D7a@qiilc^Ra`jkAKa(4eR}Wh?fcjJyyu+f{LXpL4}cL8 zCXwc%Y5+M>g*-agACFH+#L2yY0u@N$1RxOR%fe>`#Q*^C19^CUbg)1C0k3ZW0swH; zE+i7i;s1lWP$pLZAdvvzA`<5d0gzGv$SzdK6adH=0I*ZDWC{S3003-xd_p1ssto|_ z^hrJi0NAOM+!p}Yq8zCR0F40vnJ7mj0zkU}U{!%qECRs70HCZuA}$2Lt^t5qwlYTo zfV~9(c8*w(4?ti5fSE!p%m5%b0suoE6U_r4Oaq`W(!b!TUvP!ENC5!A%azTSOVTqG zxRuZ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VfZcRX1&S&)1jiOPpx423?lIEROmG(H@JAFg?XogQlb;dIZPf{y+kr|S? zBlAsGMAqJ{&)IR=Ejg5&l$@hd4QZCNE7vf$D7Q~$D=U)?Nn}(WA6du22pZOfRS_cv~1-c(_QtNLti0-)8>m`6CO07JR*suu!$(^sg%jf zZm#rNxnmV!m1I@#YM0epR(~oNm0zrItf;Q|utvD%;#W>z)qM4NZQ9!2O1H}G>qzUQ z>u#*~S--DJy=p<#(1!30tsC);y-IHSJr>wyfLop*ExT zdYyk=%U1oZtGB+{Cfe4&-FJKQ4uc&PJKpb5^_C@dOYIJXG+^@gCvI%WcHjN%gI&kHifN$EH?V5MBa9S!3!a?Q1 zC*P)gd*e{(q0YnH!_D8Bf4B7r>qvPk(mKC&tSzH$pgp0z@92!9ogH2sN4~fJe(y2k zV|B+hk5`_cohUu=`Q(C=R&z?UQbnZ;IU-!xL z-sg{9@Vs#JBKKn3CAUkhJ+3`ResKNaNUvLO>t*-L?N>ambo5Q@JJIjcfBI^`)pOVQ z*DhV3dA;w(>>IakCfyvkCA#(acJ}QTcM9%I++BK)c(44v+WqPW`VZ=VwEnSWz-{38 zV8CF{!&wjS4he^z{*?dIhvCvk%tzHDMk9@nogW_?4H~`jWX_Y}r?RIL&&qyQ|9R_k ztLNYS;`>X_Sp3-V3;B!Bzpi_CX>@2HM@dakAa8CU zVIWOmV~41BLjV8(6iGxuR0!8&{Qv(y0}L=RAOYMQ0G4h81=<*r#Q*>R07*qoM6N<$ Ef;9S100000 literal 0 HcmV?d00001 diff --git a/test/fixtures/sprites/public/images/repeat_x/four.xcf b/test/fixtures/sprites/public/images/repeat_x/four.xcf deleted file mode 100644 index cadc920f81b306a8fde216b99fd8f0ef4ea1d39d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 656 zcmZ`%OHRWu5Oo5A(oz+~XTxTe=_07TLRp|LD)kDkW4jiKT_vt)IRYzg!|}>Y>Y!AF ziC*UUd7f=~tWB|jolu}mE*Zvo0P$Ny6yfg<(VKlSM!>j7P+^RC44mp7uzrbosBzg; zxT28O|vX)@B#7O|xA`&>fha+8hw ziPFO5u7spcX{lEnCeM>PI>AB**B%K!iX diff --git a/test/integrations/sprites_test.rb b/test/integrations/sprites_test.rb index 06964835..e1ef64a8 100644 --- a/test/integrations/sprites_test.rb +++ b/test/integrations/sprites_test.rb @@ -380,7 +380,7 @@ class SpritesTest < Test::Unit::TestCase SCSS assert_correct css, <<-CSS .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 { @@ -628,7 +628,7 @@ class SpritesTest < Test::Unit::TestCase SCSS assert_correct css, <<-CSS .ko-sprite, .ko-default_background, .ko-starbg26x27 { - background: url('/ko-sd6b4d44430.png') no-repeat; + background: url('/ko-sd46dfbab4f.png') no-repeat; } .ko-default_background { diff --git a/test/test_helper.rb b/test/test_helper.rb index 3b904712..cb8ce6d3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -23,6 +23,12 @@ require 'compass' require 'test/unit' +class String + def name + to_s + end +end + %w(command_line diff io rails test_case).each do |helper| require "helpers/#{helper}" end @@ -60,8 +66,9 @@ module SpriteHelper end def clean_up_sprites - init_sprite_helper - ::FileUtils.rm_r @images_tmp_path + init_sprite_helper + ::FileUtils.rm_r @images_tmp_path + rescue Errno::ENOENT => e end end \ No newline at end of file diff --git a/test/units/actions_test.rb b/test/units/actions_test.rb index 21b26414..c1cb379b 100644 --- a/test/units/actions_test.rb +++ b/test/units/actions_test.rb @@ -13,7 +13,7 @@ class ActionsTest < Test::Unit::TestCase end # When log4r is included, it sometimes breaks the Actions - def test_quiet_option + test "test_quiet_option" do b = BaseActionExtender.new b.logger = "" b.options[:quiet] = true diff --git a/test/units/sprites/engine_test.rb b/test/units/sprites/engine_test.rb index a172c518..571c1fac 100644 --- a/test/units/sprites/engine_test.rb +++ b/test/units/sprites/engine_test.rb @@ -1,14 +1,19 @@ require 'test_helper' class EngineTest < Test::Unit::TestCase - + include SpriteHelper def setup + create_sprite_temp sprite_filename = 'squares/ten-by-ten.png' @images = [ Compass::SassExtensions::Sprites::Image.new(nil, File.join(sprite_filename), {}) ] @engine = Compass::SassExtensions::Sprites::Engine.new(100, 100, @images) end + + def taredown + clean_up_sprites + end test "should have width of 100" do diff --git a/test/units/sprites/image_row_test.rb b/test/units/sprites/image_row_test.rb index 4c92b88a..6f40c819 100644 --- a/test/units/sprites/image_row_test.rb +++ b/test/units/sprites/image_row_test.rb @@ -3,6 +3,7 @@ require 'test_helper' class ImageRowTest < Test::Unit::TestCase include SpriteHelper def setup + clean_up_sprites create_sprite_temp file = StringIO.new("images_path = #{@images_src_path.inspect}\n") Compass.add_configuration(file, "sprite_config") diff --git a/test/units/sprites/layout_test.rb b/test/units/sprites/layout_test.rb index e82617ce..9c85f4c3 100644 --- a/test/units/sprites/layout_test.rb +++ b/test/units/sprites/layout_test.rb @@ -5,6 +5,7 @@ class LayoutTest < Test::Unit::TestCase def setup Hash.send(:include, Compass::SassExtensions::Functions::Sprites::VariableReader) + clean_up_sprites create_sprite_temp file = StringIO.new("images_path = #{@images_tmp_path.inspect}\n") Compass.add_configuration(file, "sprite_config") @@ -50,6 +51,21 @@ class LayoutTest < Test::Unit::TestCase sprite_map_test(opts) 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 it "should have a vertical layout" do