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\FontSize\FontSizeGeneratorInterface; |
10
|
|
|
use SixtyNine\Cloud\FontSize\LinearFontSizeGenerator; |
11
|
|
|
use SixtyNine\Cloud\Model\Cloud; |
12
|
|
|
use SixtyNine\Cloud\Model\CloudWord; |
13
|
|
|
use SixtyNine\Cloud\Model\Word; |
14
|
|
|
use SixtyNine\Cloud\Model\WordsList; |
15
|
|
|
use SixtyNine\Cloud\Usher; |
16
|
|
|
|
17
|
|
|
class CloudBuilder |
18
|
|
|
{ |
19
|
|
|
/** @var WordsList */ |
20
|
|
|
protected $list; |
21
|
|
|
/** @var int */ |
22
|
|
|
protected $width = 800; |
23
|
|
|
/** @var int */ |
24
|
|
|
protected $height = 600; |
25
|
|
|
/** @var string */ |
26
|
|
|
protected $font; |
27
|
|
|
/** @var string */ |
28
|
|
|
protected $backgroundColor = '#000000'; |
29
|
|
|
/** @var FontSizeGeneratorInterface */ |
30
|
|
|
protected $sizeGenerator; |
31
|
|
|
/** @var string */ |
32
|
|
|
protected $placerName; |
33
|
|
|
/** @var int */ |
34
|
|
|
protected $minFontSize = 10; |
35
|
|
|
/** @var int */ |
36
|
|
|
protected $maxFontSize = 60; |
37
|
|
|
/** @var FontsFactory */ |
38
|
|
|
protected $fontsFactory; |
39
|
|
|
|
40
|
4 |
|
protected function __construct(FontsFactory $fontsFactory) |
41
|
|
|
{ |
42
|
4 |
|
$this->fontsFactory = $fontsFactory; |
43
|
4 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return CloudBuilder |
47
|
|
|
*/ |
48
|
4 |
|
public static function create(FontsFactory $fontsFactory) |
49
|
|
|
{ |
50
|
4 |
|
return new self($fontsFactory); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param int $width |
55
|
|
|
* @param int $height |
56
|
|
|
* @return CloudBuilder |
57
|
|
|
*/ |
58
|
2 |
|
public function setDimension($width, $height) |
59
|
|
|
{ |
60
|
2 |
|
$this->width = $width; |
61
|
2 |
|
$this->height = $height; |
62
|
2 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
public function setBackgroundColor($color) |
66
|
|
|
{ |
67
|
2 |
|
$this->backgroundColor = $color; |
68
|
2 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
public function setFont($name) |
72
|
|
|
{ |
73
|
4 |
|
$this->font = $name; |
74
|
4 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setSizeGenerator(FontSizeGeneratorInterface $generator) |
78
|
|
|
{ |
79
|
|
|
$this->sizeGenerator = $generator; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function setFontSizes($minSize, $maxSize) |
84
|
|
|
{ |
85
|
|
|
$this->minFontSize = $minSize; |
86
|
|
|
$this->maxFontSize = $maxSize; |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function setPlacer($name) |
91
|
|
|
{ |
92
|
1 |
|
if (!in_array($name, PlacerFactory::getInstance()->getPlacersNames())) { |
93
|
|
|
throw new \InvalidArgumentException('Placer not found: ' . $name); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$this->placerName = $name; |
97
|
1 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
public function useList(WordsList $list) |
101
|
|
|
{ |
102
|
3 |
|
$this->list = $list; |
103
|
3 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
4 |
|
public function build() |
107
|
|
|
{ |
108
|
4 |
|
if (!$this->font) { |
109
|
|
|
throw new \InvalidArgumentException('Font not set'); |
110
|
|
|
} |
111
|
|
|
|
112
|
4 |
|
$cloud = new Cloud(); |
113
|
|
|
$cloud |
114
|
4 |
|
->setFont($this->font) |
115
|
4 |
|
->setWidth($this->width) |
116
|
4 |
|
->setHeight($this->height) |
117
|
4 |
|
->setBackgroundColor($this->backgroundColor) |
118
|
|
|
; |
119
|
|
|
|
120
|
4 |
|
if ($this->list) { |
121
|
3 |
|
$this->addWords($cloud, $this->list); |
122
|
3 |
|
$this->placeWords($cloud); |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
return $cloud; |
126
|
|
|
} |
127
|
|
|
|
128
|
3 |
|
protected function addWords(Cloud $cloud, WordsList $list) |
129
|
|
|
{ |
130
|
3 |
|
$words = $list->getWordsOrdered(); |
131
|
3 |
|
$maxCount = $list->getWordsMaxCount(); |
132
|
|
|
|
133
|
3 |
|
if (!$this->sizeGenerator) { |
134
|
3 |
|
$this->sizeGenerator = new LinearFontSizeGenerator(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** @var Word $word */ |
138
|
3 |
|
foreach ($words as $word) { |
139
|
3 |
|
$cloudWord = new CloudWord(); |
140
|
|
|
$cloudWord |
141
|
3 |
|
->setCloud($cloud) |
142
|
3 |
|
->setPosition(array(0, 0)) |
143
|
3 |
|
->setSize($this->sizeGenerator->calculateFontSize($word->getCount(), $maxCount, $this->minFontSize, $this->maxFontSize)) |
144
|
3 |
|
->setAngle($word->getOrientation() === Word::DIR_VERTICAL ? 270 : 0) |
145
|
3 |
|
->setColor($word->getColor()) |
146
|
3 |
|
->setText($word->getText()) |
147
|
3 |
|
->setIsVisible(true) |
148
|
|
|
; |
149
|
3 |
|
$cloud->addWord($cloudWord); |
150
|
|
|
} |
151
|
|
|
|
152
|
3 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
3 |
|
protected function placeWords(Cloud $cloud) |
156
|
|
|
{ |
157
|
3 |
|
$placer = $this->placerName |
158
|
1 |
|
? PlacerFactory::getInstance()->getPlacer($this->placerName, $cloud->getWidth(), $cloud->getHeight()) |
159
|
3 |
|
: PlacerFactory::getInstance()->getDefaultPlacer($cloud->getWidth(), $cloud->getHeight()) |
160
|
|
|
; |
161
|
|
|
|
162
|
3 |
|
$usher = new Usher($cloud->getWidth(), $cloud->getHeight(), $placer); |
163
|
|
|
|
164
|
|
|
/** @var CloudWord $word */ |
165
|
3 |
|
foreach ($cloud->getWords() as $word) { |
166
|
3 |
|
$font = $this->fontsFactory->getImagineFont($cloud->getFont(), $word->getSize(), $word->getColor()); |
167
|
3 |
|
$box = $font->box($word->getText(), $word->getAngle()); |
168
|
3 |
|
$place = $usher->getPlace($box); |
169
|
|
|
|
170
|
|
|
$word |
171
|
3 |
|
->setIsVisible((bool)$place) |
172
|
3 |
|
->setBox(array($box->getWidth(), $box->getHeight())) |
173
|
|
|
; |
174
|
|
|
|
175
|
3 |
|
if ($place) { |
176
|
|
|
|
177
|
3 |
|
if ($word->getAngle() !== 0) { |
178
|
2 |
|
$place = new Point( |
179
|
2 |
|
$place->getX() + $box->getWidth(), |
180
|
2 |
|
$place->getY() + $box->getHeight() |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
3 |
|
$word->setPosition(array((int)$place->getX(), (int)$place->getY())); |
185
|
|
|
} |
186
|
|
|
} |
187
|
3 |
|
} |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|