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