-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpritePacker.php
More file actions
153 lines (144 loc) · 4.41 KB
/
SpritePacker.php
File metadata and controls
153 lines (144 loc) · 4.41 KB
1
2
3
4
5
6
7
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace SCG\SpriteMapper;
// Growing Bin Packer algorithm for positioning icons in a spritesheet
// using space as efficiently as possible.
//
// This code is a rewritten version of Jake Gordon's GrowingPacker algorithm.
// See <http://codeincomplete.com/posts/2011/5/7/bin_packing/> for more info.
// The algorithm has been adapted from Javascript to PHP and modified lightly.
//
class SpritePacker
{
public $pack;
// Note that $sprites needs to be converted to a StdObject temporarily
// as arrays cannot be properly passed by reference. This algorithm
// cannot work in PHP using arrays.
public function fit(&$sprites, $def_w=null, $def_h=null)
{
$len = count($sprites);
if (!isset($def_w)) {
$w = $len > 0 ? $sprites[0]->width : 0;
} else {
$w = $def_w;
}
if (!isset($def_h)) {
$h = $len > 0 ? $sprites[0]->height : 0;
} else {
$h = $def_h;
}
$this->pack = (object)array(
'x' => 0,
'y' => 0,
'w' => $w,
'h' => $h,
'used' => false,
);
foreach ($sprites as &$sprite) {
$node = $this->findNode($this->pack, $sprite->width, $sprite->height);
if ($node) {
$sprite->fit = $this->splitNode($node, $sprite->width, $sprite->height);
} else {
$sprite->fit = $this->growNode($sprite->width, $sprite->height);
}
}
}
public function findNode(&$pack, $w, $h)
{
if (@$pack->used) {
$node = $this->findNode($pack->right, $w, $h);
if ($node) {
return $node;
}
$node = $this->findNode($pack->down, $w, $h);
if ($node) {
return $node;
}
} else if (($w <= $pack->w) && ($h <= $pack->h)) {
return $pack;
} else {
return null;
}
}
public function splitNode(&$node, $w, $h)
{
$node->used = true;
$node->down = (object)array(
'x' => $node->x,
'y' => $node->y + $h,
'w' => $node->w,
'h' => $node->h - $h,
);
$node->right = (object)array(
'x' => $node->x + $w,
'y' => $node->y,
'w' => $node->w - $w,
'h' => $h,
);
return $node;
}
public function growNode($w, $h)
{
$canGrowDown = ($w <= $this->pack->w);
$canGrowRight = ($h <= $this->pack->h);
$shouldGrowRight = $canGrowRight && ($this->pack->h >= ($this->pack->w + $w));
$shouldGrowDown = $canGrowDown && ($this->pack->w >= ($this->pack->h + $h));
if ($shouldGrowRight) {
return $this->growRight($w, $h);
} else if ($shouldGrowDown) {
return $this->growDown($w, $h);
} else if ($canGrowRight) {
return $this->growRight($w, $h);
} else if ($canGrowDown) {
return $this->growDown($w, $h);
} else {
// if this happens, sort sizes first
return null;
}
}
public function growRight($w, $h)
{
$this->pack = (object)array(
'used' => true,
'x' => 0,
'y' => 0,
'w' => $this->pack->w + $w,
'h' => $this->pack->h,
'down' => $this->pack,
'right' => (object)array(
'x' => $this->pack->w,
'y' => 0,
'w' => $w,
'h' => $this->pack->h,
)
);
$node = $this->findNode($this->pack, $w, $h);
if ($node) {
return $this->splitNode($node, $w, $h);
} else {
return null;
}
}
public function growDown($w, $h)
{
$this->pack = (object)array(
'used' => true,
'x' => 0,
'y' => 0,
'w' => $this->pack->w,
'h' => $this->pack->h + $h,
'down' => (object)array(
'x' => 0,
'y' => $this->pack->h,
'w' => $this->pack->w,
'h' => $h,
),
'right' => $this->pack
);
$node = $this->findNode($this->pack, $w, $h);
if ($node) {
return $this->splitNode($node, $w, $h);
} else {
return null;
}
}
}