1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud\Usher; |
4
|
|
|
|
5
|
|
|
use Imagine\Image\Point; |
6
|
|
|
use SixtyNine\Cloud\Drawer\Drawer; |
7
|
|
|
use SixtyNine\Cloud\Factory\Logger; |
8
|
|
|
use SixtyNine\Cloud\FontMetrics; |
9
|
|
|
use SixtyNine\DataTypes\Box; |
10
|
|
|
use SixtyNine\Cloud\Placer\PlacerInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Responsible to find a place for the word in the cloud |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
class Usher |
17
|
|
|
{ |
18
|
|
|
const DEFAULT_MAX_TRIES = 100000; |
19
|
|
|
|
20
|
|
|
/** @var int */ |
21
|
|
|
protected $maxTries; |
22
|
|
|
|
23
|
|
|
/** @var \SixtyNine\Cloud\Usher\MaskInterface */ |
24
|
|
|
protected $mask; |
25
|
|
|
|
26
|
|
|
/** @var \SixtyNine\Cloud\Placer\PlacerInterface */ |
27
|
|
|
protected $placer; |
28
|
|
|
|
29
|
|
|
/** @var \SixtyNine\Cloud\FontMetrics */ |
30
|
|
|
protected $metrics; |
31
|
|
|
|
32
|
|
|
/** @var Logger */ |
33
|
|
|
protected $logger; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param int $imgWidth |
37
|
|
|
* @param int $imgHeight |
38
|
|
|
* @param PlacerInterface $placer |
39
|
|
|
* @param FontMetrics $metrics |
40
|
|
|
* @param int $maxTries |
41
|
|
|
*/ |
42
|
6 |
|
public function __construct( |
43
|
|
|
$imgWidth, |
44
|
|
|
$imgHeight, |
45
|
|
|
PlacerInterface $placer, |
46
|
|
|
FontMetrics $metrics, |
47
|
|
|
$maxTries = self::DEFAULT_MAX_TRIES |
48
|
|
|
) { |
49
|
6 |
|
$this->mask = new QuadTreeMask($imgWidth, $imgHeight); |
50
|
6 |
|
$this->metrics = $metrics; |
51
|
6 |
|
$this->imgHeight = $imgHeight; |
|
|
|
|
52
|
6 |
|
$this->imgWidth = $imgWidth; |
|
|
|
|
53
|
6 |
|
$this->maxTries = $maxTries; |
54
|
6 |
|
$this->placer = $placer; |
55
|
6 |
|
$this->logger = Logger::getInstance(); |
56
|
6 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $word |
60
|
|
|
* @param string $font |
61
|
|
|
* @param int $fontSize |
62
|
|
|
* @param int $angle |
63
|
|
|
* @param bool $precise |
64
|
|
|
* @return bool|Box |
65
|
|
|
*/ |
66
|
6 |
|
public function getPlace($word, $font, $fontSize, $angle, $precise = false) |
67
|
|
|
{ |
68
|
6 |
|
$this->logger->log( |
69
|
|
|
sprintf( |
70
|
6 |
|
'Search place for "%s", font = %s(%s), angle = %s', |
71
|
|
|
$word, |
72
|
6 |
|
str_replace('.ttf', '', $font), |
73
|
|
|
$fontSize, |
74
|
|
|
$angle |
75
|
|
|
), |
76
|
6 |
|
Logger::DEBUG |
77
|
|
|
); |
78
|
|
|
|
79
|
6 |
|
$bounds = new Box(0, 0, $this->imgWidth, $this->imgHeight); |
80
|
6 |
|
$size = $this->metrics->calculateSize($word, $font, $fontSize); |
81
|
6 |
|
$box = Drawer::getBoxFoxText(0, 0, $size->getWidth(), $size->getHeight(), $angle); |
82
|
|
|
|
83
|
6 |
|
$this->logger->log(' Text dimensions: ' . $size->getDimensions(), Logger::DEBUG); |
84
|
|
|
|
85
|
6 |
|
$place = $this->searchPlace($bounds, $box); |
86
|
|
|
|
87
|
6 |
|
if ($place) { |
88
|
6 |
|
if ($precise) { |
89
|
1 |
|
$this->addWordToMask($word, $place, $font, $fontSize, $angle); |
90
|
|
|
} else { |
91
|
5 |
|
$this->mask->add(new Point(0, 0), $place); |
92
|
|
|
} |
93
|
6 |
|
return $place; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $word |
101
|
|
|
* @param Box $place |
102
|
|
|
* @param string $font |
103
|
|
|
* @param int $size |
104
|
|
|
* @param int $angle |
105
|
|
|
*/ |
106
|
1 |
|
public function addWordToMask($word, Box $place, $font, $size, $angle) |
107
|
|
|
{ |
108
|
1 |
|
$base = $this->metrics->calculateSize($word, $font, $size, $angle); |
109
|
1 |
|
foreach (str_split($word) as $letter) { |
110
|
1 |
|
$box = $this->metrics->calculateSize($letter, $font, $size); |
111
|
|
|
|
112
|
1 |
|
if ($angle === 0) { |
113
|
1 |
|
$newPos = new Point($place->getX(), $place->getY() + ($base->getHeight() - $box->getHeight())); |
114
|
1 |
|
$this->mask->add($newPos, $box, $angle); |
|
|
|
|
115
|
1 |
|
$place = $place->move($box->getWidth(), 0); |
116
|
|
|
} else { |
117
|
|
|
// Invert width and height |
118
|
1 |
|
$box = new Box(0, 0, $box->getHeight(), $box->getWidth()); |
119
|
|
|
|
120
|
1 |
|
if ($place->getY() + ($base->getHeight() - $box->getHeight()) < 0) { |
121
|
|
|
continue; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
if ($place->getX() + ($base->getWidth() - $box->getWidth()) < 0) { |
125
|
|
|
continue; |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
$newPos = new Point( |
129
|
1 |
|
$place->getX() + ($base->getWidth() - $box->getWidth()), |
130
|
1 |
|
$place->getY() + ($base->getHeight() - $box->getHeight()) |
131
|
|
|
); |
132
|
1 |
|
$this->mask->add($newPos, $box); |
133
|
1 |
|
$place = $place->move(0, -$box->getHeight()); |
134
|
|
|
} |
135
|
|
|
} |
136
|
1 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Search a free place for a new box. |
140
|
|
|
* @param \SixtyNine\DataTypes\Box $bounds |
141
|
|
|
* @param \SixtyNine\DataTypes\Box $box |
142
|
|
|
* @return bool|Box |
143
|
|
|
*/ |
144
|
6 |
|
protected function searchPlace(Box $bounds, Box $box) |
145
|
|
|
{ |
146
|
|
|
|
147
|
6 |
|
$this->logger->log(' Search place for ' . $box, Logger::DEBUG); |
148
|
|
|
|
149
|
6 |
|
$placeFound = false; |
150
|
6 |
|
$current = $this->placer->getFirstPlaceToTry(); |
151
|
6 |
|
$curTry = 1; |
152
|
6 |
|
$currentBox = null; |
153
|
|
|
|
154
|
6 |
|
while (!$placeFound) { |
155
|
|
|
|
156
|
6 |
|
if (!$current) { |
157
|
1 |
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
6 |
|
if ($curTry > $this->maxTries) { |
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
|
164
|
6 |
|
$currentBox = $box->move($current->getX(), $current->getY()); |
165
|
|
|
|
166
|
6 |
|
$outOfBounds = !$currentBox->inside($bounds); |
167
|
|
|
|
168
|
6 |
|
if (!$outOfBounds) { |
169
|
6 |
|
$placeFound = !$this->mask->overlaps($currentBox); |
170
|
6 |
|
$placeFound = $placeFound && !$outOfBounds; |
171
|
|
|
} |
172
|
|
|
|
173
|
6 |
|
$this->logger->log(sprintf( |
174
|
6 |
|
' Trying %s --> %s', |
175
|
|
|
$currentBox, |
176
|
6 |
|
$outOfBounds ? 'Out of bounds' : ($placeFound ? 'OK' : 'Collision') |
177
|
6 |
|
), Logger::DEBUG); |
178
|
|
|
|
179
|
6 |
|
if ($placeFound) { |
180
|
6 |
|
break; |
181
|
|
|
} |
182
|
|
|
|
183
|
6 |
|
$current = $this->placer->getNextPlaceToTry($current); |
184
|
6 |
|
$curTry++; |
185
|
|
|
} |
186
|
|
|
|
187
|
6 |
|
return $currentBox->inside($bounds) ? $currentBox : false; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return MaskInterface |
192
|
|
|
*/ |
193
|
5 |
|
public function getMask() |
194
|
|
|
{ |
195
|
5 |
|
return $this->mask; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: