Skip to content

Rewrote PR #1303 and added a test #1310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/compass/sass_extensions/sprites/image_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ def add(image)
alias :<< :add

def height
images.map(&:height).max
images.map {|i| i.height + i.spacing}.max
end

def width
images.map(&:width).max
end

def total_width
images.inject(0) {|sum, img| sum + img.width }
images.inject(0) {|sum, img| sum + img.width + 2 * img.spacing}
end

def efficiency
1 - (total_width.to_f / max_width.to_f)
end

def will_fit?(image)
(total_width + image.width) <= max_width
(total_width + image.width + 2 * image.spacing) <= max_width
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/compass/sass_extensions/sprites/layout/smart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ def layout!
def calculate_positions!
fitter = ::Compass::SassExtensions::Sprites::RowFitter.new(@images)
current_y = 0
spacing = 0
fitter.fit!.each do |row|
current_x = 0
row.images.each_with_index do |image, index|
image.left = current_x
image.top = current_y
current_x += image.width
current_x += image.width + image.spacing + spacing
spacing = image.spacing
end
current_y += row.height
end
Expand Down
14 changes: 9 additions & 5 deletions lib/compass/sass_extensions/sprites/row_fitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,31 @@ def fit!(style = :scan)
end

def width
@width ||= @images.collect(&:width).max
@width ||= @images.collect(&:width).max + @images.collect(&:spacing).max
end

def height
@height ||= @rows.inject(0) {|sum, row| sum += row.height}
@height ||= @rows.inject(0) {|sum, row| sum += row.height} + @images[0].spacing
end

def efficiency
@rows.inject(0) { |sum, row| sum += row.efficiency } ** @rows.length
end

private
def new_row(image = nil)
row = Compass::SassExtensions::Sprites::ImageRow.new(width)
def new_row(image = nil)
if image
row = Compass::SassExtensions::Sprites::ImageRow.new(width + 2 * image.spacing)
else
row = Compass::SassExtensions::Sprites::ImageRow.new(width + 2 * @images[0].spacing)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would DRY this up a bit and calculate the width before calling ImageRow.new within the if statement.

end
row.add(image) if image
row
end

def fast_fit
row = new_row

@images.each do |image|
if !row.add(image)
@rows << row
Expand All @@ -58,7 +63,6 @@ def fast_fit

def scan_fit
fast_fit

moved_images = []

begin
Expand Down
44 changes: 44 additions & 0 deletions test/units/sprites/smart_layout_spacing_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'test_helper'
require 'compass/sass_extensions/sprites/layout'
require 'compass/sass_extensions/sprites/layout/smart'

class SmartLayoutSpacingTest < Test::Unit::TestCase
include SpriteHelper

def setup
file = StringIO.new("images_path = #{@images_src_path.inspect}\n")
Compass.add_configuration(file, "sprite_config")
end


def create_images_with_spacing(dims)
dims.collect { |width, height, spacing|
image = Compass::SassExtensions::Sprites::Image.new('blah', 'blah', {"spacing" => Sass::Script::Number.new(10, ['px'])})
image.stubs(:width => width, :height => height)
image
}
end

def spacing_dims
[
[ 100, 10, 10 ],
[ 80, 10, 20 ],
[ 50, 10, 20 ],
[ 35, 10, 10 ],
[ 20, 10, 10 ]
]
end

it 'should correctly space the images according to the spacing' do
images = create_images_with_spacing(spacing_dims)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the indenting


computed_images, width, height = Compass::SassExtensions::Sprites::Layout::Smart.new(images, nil).properties

computed_images.each do |img|
puts img.width.to_s + ": " + img.left.to_s + " " + img.top.to_s
end

assert_equal 110, width
assert_equal 90, height
end
end