1
|
|
|
<?php |
2
|
|
|
namespace SixtyNine\Cloud\Builder; |
3
|
|
|
|
4
|
|
|
use Imagine\Gd\Font; |
5
|
|
|
use Imagine\Image\Color; |
6
|
|
|
use Imagine\Image\Point; |
7
|
|
|
use SixtyNine\Cloud\Factory\FontsFactory; |
8
|
|
|
use SixtyNine\Cloud\Factory\PlacerFactory; |
9
|
|
|
use SixtyNine\Cloud\FontMetrics; |
10
|
|
|
use SixtyNine\Cloud\FontSize\FontSizeGeneratorInterface; |
11
|
|
|
use SixtyNine\Cloud\FontSize\LinearFontSizeGenerator; |
12
|
|
|
use SixtyNine\Cloud\Model\Cloud; |
13
|
|
|
use SixtyNine\Cloud\Model\CloudWord; |
14
|
|
|
use SixtyNine\Cloud\Model\Word; |
15
|
|
|
use SixtyNine\Cloud\Model\WordsList; |
16
|
|
|
use SixtyNine\Cloud\Usher\MaskInterface; |
17
|
|
|
use SixtyNine\Cloud\Usher\Usher; |
18
|
|
|
use Webmozart\Assert\Assert; |
19
|
|
|
|
20
|
|
|
class CloudBuilder |
21
|
|
|
{ |
22
|
|
|
/** @var WordsList */ |
23
|
|
|
protected $list; |
24
|
|
|
/** @var int */ |
25
|
|
|
protected $width = 800; |
26
|
|
|
/** @var int */ |
27
|
|
|
protected $height = 600; |
28
|
|
|
/** @var string */ |
29
|
|
|
protected $font; |
30
|
|
|
/** @var string */ |
31
|
|
|
protected $backgroundColor = '#ffffff'; |
32
|
|
|
/** @var int */ |
33
|
|
|
protected $backgroundOpacity = 100; |
34
|
|
|
/** @var FontSizeGeneratorInterface */ |
35
|
|
|
protected $sizeGenerator; |
36
|
|
|
/** @var string */ |
37
|
|
|
protected $placerName; |
38
|
|
|
/** @var int */ |
39
|
|
|
protected $minFontSize = 10; |
40
|
|
|
/** @var int */ |
41
|
|
|
protected $maxFontSize = 60; |
42
|
|
|
/** @var FontsFactory */ |
43
|
|
|
protected $fontsFactory; |
44
|
|
|
/** @var bool */ |
45
|
|
|
protected $precise = false; |
46
|
|
|
/** @var MaskInterface */ |
47
|
|
|
protected $mask; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param FontsFactory $fontsFactory |
51
|
|
|
*/ |
52
|
4 |
|
protected function __construct(FontsFactory $fontsFactory) |
53
|
|
|
{ |
54
|
4 |
|
$this->fontsFactory = $fontsFactory; |
55
|
4 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param \SixtyNine\Cloud\Factory\FontsFactory $fontsFactory |
59
|
|
|
* @return CloudBuilder |
60
|
|
|
*/ |
61
|
4 |
|
public static function create(FontsFactory $fontsFactory) |
62
|
|
|
{ |
63
|
4 |
|
return new self($fontsFactory); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param int $width |
68
|
|
|
* @param int $height |
69
|
|
|
* @return CloudBuilder |
70
|
|
|
*/ |
71
|
2 |
|
public function setDimension($width, $height) |
72
|
|
|
{ |
73
|
2 |
|
$this->width = $width; |
74
|
2 |
|
$this->height = $height; |
75
|
2 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $color |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
2 |
|
public function setBackgroundColor($color) |
83
|
|
|
{ |
84
|
2 |
|
$this->backgroundColor = $color; |
85
|
2 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param int $opacity |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function setBackgroundOpacity($opacity) |
93
|
|
|
{ |
94
|
|
|
$this->backgroundOpacity = $opacity; |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $name |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
4 |
|
public function setFont($name) |
103
|
|
|
{ |
104
|
4 |
|
$this->font = $name; |
105
|
4 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param FontSizeGeneratorInterface $generator |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function setSizeGenerator(FontSizeGeneratorInterface $generator) |
113
|
|
|
{ |
114
|
|
|
$this->sizeGenerator = $generator; |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param int $minSize |
120
|
|
|
* @param int $maxSize |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function setFontSizes($minSize, $maxSize) |
124
|
|
|
{ |
125
|
|
|
$this->minFontSize = $minSize; |
126
|
|
|
$this->maxFontSize = $maxSize; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $name |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
1 |
|
public function setPlacer($name) |
135
|
|
|
{ |
136
|
1 |
|
Assert::oneOf($name, PlacerFactory::getInstance()->getPlacersNames(), 'Placer not found: ' . $name); |
137
|
1 |
|
$this->placerName = $name; |
138
|
1 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function setPrecise() |
145
|
|
|
{ |
146
|
|
|
$this->precise = true; |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param WordsList $list |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
3 |
|
public function useList(WordsList $list) |
155
|
|
|
{ |
156
|
3 |
|
$this->list = $list; |
157
|
3 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return Cloud |
162
|
|
|
*/ |
163
|
4 |
|
public function build() |
164
|
|
|
{ |
165
|
4 |
|
Assert::notNull($this->font, 'Font not set'); |
166
|
|
|
|
167
|
4 |
|
$cloud = new Cloud(); |
168
|
|
|
$cloud |
169
|
4 |
|
->setFont($this->font) |
170
|
4 |
|
->setWidth($this->width) |
171
|
4 |
|
->setHeight($this->height) |
172
|
4 |
|
->setBackgroundColor($this->backgroundColor) |
173
|
4 |
|
->setBackgroundOpacity($this->backgroundOpacity) |
174
|
|
|
; |
175
|
|
|
|
176
|
4 |
|
if ($this->list) { |
177
|
3 |
|
$this->addWords($cloud, $this->list); |
178
|
3 |
|
$this->mask = $this->placeWords($cloud); |
179
|
|
|
} |
180
|
|
|
|
181
|
4 |
|
return $cloud; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param Cloud $cloud |
186
|
|
|
* @param WordsList $list |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
3 |
|
protected function addWords(Cloud $cloud, WordsList $list) |
190
|
|
|
{ |
191
|
3 |
|
$words = $list->getWordsOrdered(); |
192
|
3 |
|
$maxCount = $list->getWordsMaxCount(); |
193
|
|
|
|
194
|
3 |
|
if (!$this->sizeGenerator) { |
195
|
3 |
|
$this->sizeGenerator = new LinearFontSizeGenerator(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** @var Word $word */ |
199
|
3 |
|
foreach ($words as $word) { |
200
|
3 |
|
|
201
|
|
|
if (!$word->getCount()) { |
202
|
3 |
|
continue; |
203
|
3 |
|
} |
204
|
3 |
|
|
205
|
3 |
|
$cloudWord = new CloudWord(); |
206
|
3 |
|
$cloudWord |
207
|
3 |
|
->setCloud($cloud) |
208
|
3 |
|
->setPosition(array(0, 0)) |
209
|
|
|
->setSize($this->sizeGenerator->calculateFontSize($word->getCount(), $maxCount, $this->minFontSize, $this->maxFontSize)) |
210
|
3 |
|
->setAngle($word->getOrientation() === Word::DIR_VERTICAL ? 270 : 0) |
211
|
|
|
->setColor($word->getColor()) |
212
|
|
|
->setText($word->getText()) |
213
|
3 |
|
->setIsVisible(true) |
214
|
|
|
; |
215
|
|
|
$cloud->addWord($cloudWord); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
3 |
|
|
221
|
|
|
/** |
222
|
3 |
|
* @param Cloud $cloud |
223
|
1 |
|
* @return \SixtyNine\Cloud\Usher\Usher |
224
|
3 |
|
*/ |
225
|
|
|
protected function placeWords(Cloud $cloud) |
226
|
|
|
{ |
227
|
3 |
|
$placer = $this->placerName |
228
|
3 |
|
? PlacerFactory::getInstance()->getPlacer($this->placerName, $cloud->getWidth(), $cloud->getHeight()) |
229
|
|
|
: PlacerFactory::getInstance()->getDefaultPlacer($cloud->getWidth(), $cloud->getHeight()) |
230
|
|
|
; |
231
|
3 |
|
|
232
|
|
|
$metrics = new FontMetrics($this->fontsFactory); |
233
|
3 |
|
$usher = new Usher($cloud->getWidth(), $cloud->getHeight(), $placer, $metrics, $this->precise); |
234
|
|
|
|
235
|
3 |
|
/** @var CloudWord $word */ |
236
|
|
|
foreach ($cloud->getWords() as $word) { |
237
|
3 |
|
|
238
|
3 |
|
$place = $usher->getPlace($word->getText(), $cloud->getFont(), $word->getSize(), $word->getAngle()); |
239
|
3 |
|
|
240
|
|
|
$word->setIsVisible((bool)$place); |
241
|
|
|
|
242
|
|
|
if ($place) { |
243
|
3 |
|
$word->setBox($place); |
244
|
|
|
$word->setPosition(array((int)$place->getX(), (int)$place->getY())); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $usher->getMask(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return MaskInterface |
253
|
|
|
*/ |
254
|
|
|
public function getMask() |
255
|
|
|
{ |
256
|
|
|
return $this->mask; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|