1 | <?php |
||
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 | 5 | public function __construct( |
|
57 | |||
58 | /** |
||
59 | * @param string $word |
||
60 | * @param string $font |
||
61 | * @param int $size |
||
62 | * @param int $angle |
||
63 | * @return bool|Box |
||
64 | */ |
||
65 | 5 | public function getPlace($word, $font, $size, $angle, $precise = false) |
|
66 | { |
||
67 | 5 | $this->logger->log( |
|
68 | sprintf( |
||
69 | 5 | 'Search place for "%s", font = %s(%s), angle = %s', |
|
70 | $word, |
||
71 | 5 | str_replace('.ttf', '', $font), |
|
72 | $size, |
||
73 | $angle |
||
74 | ), |
||
75 | 5 | Logger::DEBUG |
|
76 | ); |
||
77 | |||
78 | 5 | $bounds = new Box(0, 0, $this->imgWidth, $this->imgHeight); |
|
79 | 5 | $size = $this->metrics->calculateSize($word, $font, $size); |
|
80 | 5 | $box = Drawer::getBoxFoxText(0, 0, $size->getWidth(), $size->getHeight(), $angle); |
|
81 | |||
82 | 5 | $this->logger->log(' Text dimensions: ' . $size->getDimensions(), Logger::DEBUG); |
|
83 | |||
84 | 5 | $place = $this->searchPlace($bounds, $box); |
|
85 | |||
86 | 5 | if ($place) { |
|
87 | 5 | if ($precise) { |
|
88 | $this->addWordToMask($word, $place, $font, $size, $angle); |
||
89 | } else { |
||
90 | 5 | $this->mask->add(new Point(0, 0), $place, $angle); |
|
91 | } |
||
92 | 5 | return $place; |
|
93 | } |
||
94 | |||
95 | 1 | return false; |
|
96 | } |
||
97 | |||
98 | public function addWordToMask($word, Box $place, $font, $size, $angle) |
||
114 | |||
115 | /** |
||
116 | * Search a free place for a new box. |
||
117 | * @param \SixtyNine\Cloud\Model\Box $bounds |
||
118 | * @param \SixtyNine\Cloud\Model\Box $box |
||
119 | * @return bool|Box |
||
120 | */ |
||
121 | 5 | protected function searchPlace(Box $bounds, Box $box) |
|
122 | { |
||
123 | |||
124 | 5 | $this->logger->log(' Search place for ' . $box, Logger::DEBUG); |
|
125 | |||
126 | 5 | $placeFound = false; |
|
127 | 5 | $current = $this->placer->getFirstPlaceToTry(); |
|
128 | 5 | $curTry = 1; |
|
129 | |||
130 | 5 | while (!$placeFound) { |
|
131 | |||
132 | 5 | if (!$current) { |
|
133 | 1 | return false; |
|
134 | } |
||
135 | |||
136 | 5 | if ($curTry > $this->maxTries) { |
|
137 | return false; |
||
138 | } |
||
139 | |||
140 | 5 | $currentBox = $box->move($current->getX(), $current->getY()); |
|
141 | |||
142 | 5 | $outOfBounds = !$currentBox->inside($bounds); |
|
143 | |||
144 | 5 | if (!$outOfBounds) { |
|
145 | 5 | $placeFound = !$this->mask->overlaps($currentBox); |
|
146 | 5 | $placeFound = $placeFound && !$outOfBounds; |
|
147 | } |
||
148 | |||
149 | 5 | $this->logger->log(sprintf( |
|
150 | 5 | ' Trying %s --> %s', |
|
151 | $currentBox, |
||
152 | 5 | $outOfBounds ? 'Out of bounds' : ($placeFound ? 'OK' : 'Collision') |
|
153 | 5 | ), Logger::DEBUG); |
|
154 | |||
155 | 5 | if ($placeFound) { |
|
156 | 5 | break; |
|
157 | } |
||
158 | |||
159 | 5 | $current = $this->placer->getNextPlaceToTry($current); |
|
160 | 5 | $curTry++; |
|
161 | } |
||
162 | |||
163 | 5 | return $currentBox->inside($bounds) ? $currentBox : false; |
|
164 | } |
||
165 | |||
166 | 1 | public function getMask() |
|
170 | } |
||
171 |
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: