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