class Compass::SassExtensions::Sprites::RowFitter

Attributes

images[R]
rows[R]

Public Class Methods

new(images) click to toggle source
# File lib/compass/sass_extensions/sprites/row_fitter.rb, line 12
def initialize(images)
  @images = images.sort do |a,b|
    if a.height == b.height
      b.width <=> a.width
    else
      a.height <=> b.height
    end
  end
  @rows = []
end

Public Instance Methods

efficiency() click to toggle source
# File lib/compass/sass_extensions/sprites/row_fitter.rb, line 36
def efficiency
  @rows.inject(0) { |sum, row| sum += row.efficiency } ** @rows.length
end
fit!(style = :scan) click to toggle source
# File lib/compass/sass_extensions/sprites/row_fitter.rb, line 23
def fit!(style = :scan)
  send("#{style}_fit")
  @rows
end
height() click to toggle source
# File lib/compass/sass_extensions/sprites/row_fitter.rb, line 32
def height
  @height ||= @rows.inject(0) {|sum, row| sum += row.height}
end
width() click to toggle source
# File lib/compass/sass_extensions/sprites/row_fitter.rb, line 28
def width
  @width ||= @images.collect(&:width).max
end

Private Instance Methods

fast_fit() click to toggle source
# File lib/compass/sass_extensions/sprites/row_fitter.rb, line 47
def fast_fit
  row = new_row
  @images.each do |image|
    if !row.add(image)
      @rows << row
      row = new_row(image)
    end
  end

  @rows << row
end
new_row(image = nil) click to toggle source
# File lib/compass/sass_extensions/sprites/row_fitter.rb, line 41
def new_row(image = nil)
  row = Compass::SassExtensions::Sprites::ImageRow.new(width)
  row.add(image) if image
  row
end
scan_fit() click to toggle source
# File lib/compass/sass_extensions/sprites/row_fitter.rb, line 59
def scan_fit
  fast_fit

  moved_images = []

  begin
    removed = false

    catch :done do
      @rows.each do |row|
        (@rows - [ row ]).each do |other_row|
          other_row.images.each do |image|
            if !moved_images.include?(image)
              if row.will_fit?(image)
                other_row.delete(image)
                row << image

                @rows.delete(other_row) if other_row.empty?
                removed = true

                moved_images << image
                throw :done
              end
            end
          end
        end
      end
    end
  end while removed
end