8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/lash-sprites/packer/vertical_smart.rb', line 8
def self.pack(images, options = {})
width = 0
height = 0
images.sort! do |a, b|
if a.width == b.width
b.height <=> a.height
else
b.width <=> a.width
end
end
blocks = []
images.each do |img|
next unless img.exists?
smallest = nil
exact = nil
blocks.each do |block|
next unless block.fits?(img)
exact = block if (img.width == block.width || img.height == block.height) && (exact.nil? || block.area < exact.area)
smallest = block if smallest.nil? || block.area < smallest.area
break if smallest.area == img.area
end
if smallest
if exact
blocks.concat(split_block(blocks.delete(exact),img))
else
blocks.concat(split_block(blocks.delete(smallest),img))
end
else
if width == 0 && height == 0
b = Block.new(0, 0, img.width, img.height)
width = img.width
height = img.height
else
b = Block.new(0, height, width, img.height)
height += img.height
end
blocks.concat(split_block(b,img))
end
end
return {:width => width, :height => height}
end
|