|
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 |
|
$cloudWord = new CloudWord(); |
|
201
|
|
|
$cloudWord |
|
202
|
3 |
|
->setCloud($cloud) |
|
203
|
3 |
|
->setPosition(array(0, 0)) |
|
204
|
3 |
|
->setSize($this->sizeGenerator->calculateFontSize($word->getCount(), $maxCount, $this->minFontSize, $this->maxFontSize)) |
|
205
|
3 |
|
->setAngle($word->getOrientation() === Word::DIR_VERTICAL ? 270 : 0) |
|
206
|
3 |
|
->setColor($word->getColor()) |
|
207
|
3 |
|
->setText($word->getText()) |
|
208
|
3 |
|
->setIsVisible(true) |
|
209
|
|
|
; |
|
210
|
3 |
|
$cloud->addWord($cloudWord); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
3 |
|
return $this; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param Cloud $cloud |
|
218
|
|
|
* @return \SixtyNine\Cloud\Usher\Usher |
|
219
|
|
|
*/ |
|
220
|
3 |
|
protected function placeWords(Cloud $cloud) |
|
221
|
|
|
{ |
|
222
|
3 |
|
$placer = $this->placerName |
|
223
|
1 |
|
? PlacerFactory::getInstance()->getPlacer($this->placerName, $cloud->getWidth(), $cloud->getHeight()) |
|
224
|
3 |
|
: PlacerFactory::getInstance()->getDefaultPlacer($cloud->getWidth(), $cloud->getHeight()) |
|
225
|
|
|
; |
|
226
|
|
|
|
|
227
|
3 |
|
$metrics = new FontMetrics($this->fontsFactory); |
|
228
|
3 |
|
$usher = new Usher($cloud->getWidth(), $cloud->getHeight(), $placer, $metrics); |
|
229
|
|
|
|
|
230
|
|
|
/** @var CloudWord $word */ |
|
231
|
3 |
|
foreach ($cloud->getWords() as $word) { |
|
232
|
|
|
|
|
233
|
3 |
|
$place = $usher->getPlace($word->getText(), $cloud->getFont(), $word->getSize(), $word->getAngle(), $this->precise); |
|
234
|
|
|
|
|
235
|
3 |
|
$word->setIsVisible((bool)$place); |
|
236
|
|
|
|
|
237
|
3 |
|
if ($place) { |
|
238
|
3 |
|
$word->setBox($place); |
|
239
|
3 |
|
$word->setPosition(array((int)$place->getX(), (int)$place->getY())); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
3 |
|
return $usher->getMask(); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @return MaskInterface |
|
248
|
|
|
*/ |
|
249
|
|
|
public function getMask() |
|
250
|
|
|
{ |
|
251
|
|
|
return $this->mask; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|