Completed
Push — master ( 023340...d49828 )
by Dan
03:04
created

CloudRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace SixtyNine\Cloud\Renderer;
4
5
6
use Imagine\Gd\Imagine;
7
use Imagine\Image\Box;
8
use Imagine\Image\Color;
9
use Imagine\Image\ImageInterface;
10
use Imagine\Image\Point;
11
use SixtyNine\Cloud\Drawer\Drawer;
12
use SixtyNine\Cloud\Factory\FontsFactory;
13
use SixtyNine\Cloud\Model\Cloud;
14
use SixtyNine\Cloud\Placer\PlacerInterface;
15
use SixtyNine\Cloud\Usher\MaskInterface;
16
17
class CloudRenderer
18
{
19
    /** @var Drawer */
20
    protected $drawer;
21
    /** @var Cloud */
22
    protected $cloud;
23
24
    /**
25
     * @param Cloud $cloud
26
     * @param FontsFactory $fontsFactory
27
     */
28 1
    public function __construct(Cloud $cloud, FontsFactory $fontsFactory)
29
    {
30 1
        $this->cloud = $cloud;
31 1
        $this->drawer = Drawer::create($fontsFactory)
32 1
            ->createImage($cloud->getWidth(), $cloud->getHeight(), $cloud->getBackgroundColor())
33 1
            ->setFont($cloud->getFont())
34
        ;
35 1
    }
36
37 1
    public function renderCloud()
38
    {
39
        /** @var \SixtyNine\Cloud\Model\CloudWord $word */
40 1
        foreach ($this->cloud->getWords() as $word) {
41
42 1
            if (!$word->getIsVisible()) {
43 1
                continue;
44
            }
45
46 1
            $pos = $word->getPosition();
47 1
            $box = $word->getBox();
48
49 1
            if ($word->getAngle() === 270) {
50 1
                $this->drawer->drawText($pos[0] + $box->getWidth(), $pos[1] + $box->getHeight() - $word->getSize(), $word->getText(), $word->getSize(), $word->getColor(), $word->getAngle());
51
            } else {
52 1
                $this->drawer->drawText($pos[0], $pos[1], $word->getText(), $word->getSize(), $word->getColor(), $word->getAngle());
53
            }
54
        }
55 1
    }
56
57
    /**
58
     * @param string $color
59
     */
60
    public function renderBoundingBoxes($color = '#ff0000')
61
    {
62
        /** @var \SixtyNine\Cloud\Model\CloudWord $word */
63
        foreach ($this->cloud->getWords() as $word) {
64
65
            if (!$word->getIsVisible()) {
66
                continue;
67
            }
68
69
            $box = $word->getBox();
70
71
            $this->drawer->drawBox($box->getX(), $box->getY(), $box->getWidth(), $box->getHeight(), $color);
72
        }
73
    }
74
75
    /**
76
     * @param PlacerInterface $placer
77
     * @param string $color
78
     * @param int $maxIterations
79
     */
80 1
    public function renderUsher(
81
        PlacerInterface $placer,
82
        $color = '#ff0000',
83
        $maxIterations = 5000
84
    ) {
85 1
        $i = 0;
86 1
        $cur = $placer->getFirstPlaceToTry();
87 1
        $color = new Color($color);
88
89 1
        while($cur) {
90
91 1
            $next = $placer->getNextPlaceToTry($cur);
92
93 1
            if ($next) {
94 1
                $this->getImage()->draw()->line($cur, $next, $color);
95
            }
96
97 1
            $i++;
98 1
            $cur = $next;
99
100 1
            if ($i >= $maxIterations) {
101 1
                break;
102
            }
103
        }
104 1
    }
105
106
    /**
107
     * @param MaskInterface $mask
108
     * @param string $color
109
     */
110
    public function renderMask(MaskInterface $mask, $color = '#ff0000')
111
    {
112
        $this->drawer->drawMask($mask, $color);
113
    }
114
115
    /**
116
     * @return ImageInterface
117
     */
118 1
    public function getImage()
119
    {
120 1
        return $this->drawer->getImage();
121
    }
122
}
123