1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud; |
4
|
|
|
|
5
|
|
|
use Imagine\Image\PointInterface; |
6
|
|
|
use SixtyNine\Cloud\Model\Box; |
7
|
|
|
use SixtyNine\Cloud\Placer\PlacerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Responsible to find a place for the word in the cloud |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class Usher |
14
|
|
|
{ |
15
|
|
|
const DEFAULT_MAX_TRIES = 100000; |
16
|
|
|
|
17
|
|
|
/** @var int */ |
18
|
|
|
protected $maxTries; |
19
|
|
|
|
20
|
|
|
/** @var \SixtyNine\Cloud\SimpleMask */ |
21
|
|
|
protected $mask; |
22
|
|
|
|
23
|
|
|
/** @var \SixtyNine\Cloud\Placer\PlacerInterface */ |
24
|
|
|
protected $placer; |
25
|
|
|
|
26
|
|
|
/** @var \SixtyNine\Cloud\FontMetrics */ |
27
|
|
|
protected $metrics; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param int $imgWidth |
31
|
|
|
* @param int $imgHeight |
32
|
|
|
* @param PlacerInterface $placer |
33
|
|
|
* @param FontMetrics $metrics |
34
|
|
|
* @param int $maxTries |
35
|
|
|
*/ |
36
|
4 |
|
public function __construct( |
37
|
|
|
$imgWidth, |
38
|
|
|
$imgHeight, |
39
|
|
|
PlacerInterface $placer, |
40
|
|
|
FontMetrics $metrics, |
41
|
|
|
$maxTries = self::DEFAULT_MAX_TRIES |
42
|
|
|
) { |
43
|
4 |
|
$this->mask = new SimpleMask(); |
44
|
4 |
|
$this->metrics = $metrics; |
45
|
4 |
|
$this->imgHeight = $imgHeight; |
|
|
|
|
46
|
4 |
|
$this->imgWidth = $imgWidth; |
|
|
|
|
47
|
4 |
|
$this->maxTries = $maxTries; |
48
|
4 |
|
$this->placer = $placer; |
49
|
4 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $word |
53
|
|
|
* @param string $font |
54
|
|
|
* @param int $size |
55
|
|
|
* @param int $angle |
56
|
|
|
* @return bool|Box |
57
|
|
|
*/ |
58
|
4 |
|
public function getPlace($word, $font, $size, $angle) |
59
|
|
|
{ |
60
|
4 |
|
$bounds = new Box(0, 0, $this->imgWidth, $this->imgHeight); |
61
|
4 |
|
$box = $this->metrics->calculateSize($word, $font, $size, $angle); |
62
|
4 |
|
$place = $this->searchPlace($bounds, $box); |
63
|
|
|
|
64
|
4 |
|
if ($place) { |
65
|
4 |
|
$this->mask->add($place->getPosition(), $box); |
66
|
4 |
|
return $place; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Search a free place for a new box. |
74
|
|
|
* @param \SixtyNine\Cloud\Model\Box $bounds |
75
|
|
|
* @param \SixtyNine\Cloud\Model\Box $box |
76
|
|
|
* @return bool|Box |
77
|
|
|
*/ |
78
|
4 |
|
protected function searchPlace(Box $bounds, Box $box) |
79
|
|
|
{ |
80
|
4 |
|
$place_found = false; |
81
|
4 |
|
$current = $this->placer->getFirstPlaceToTry(); |
82
|
4 |
|
$curTry = 1; |
83
|
|
|
|
84
|
4 |
|
while (!$place_found) { |
85
|
|
|
|
86
|
4 |
|
if (!$current) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
4 |
|
if ($curTry > $this->maxTries) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
$currentBox = $box->move($current->getX(), $current->getY()); |
95
|
4 |
|
$place_found = !$this->mask->overlaps($currentBox); |
96
|
|
|
|
97
|
4 |
|
if ($place_found) { |
98
|
4 |
|
break; |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
$current = $this->placer->getNextPlaceToTry($current); |
102
|
4 |
|
$curTry++; |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
$currentBox = $box->move($current->getX(), $current->getY()); |
106
|
4 |
|
return $currentBox->inside($bounds) ? $currentBox : false; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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: