Completed
Push — master ( 49c7a9...f71cbe )
by Dan
02:30
created

CloudRenderer::renderUsher()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 13
cts 13
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 5
nop 4
crap 4
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\Factory\FontsFactory;
12
use SixtyNine\Cloud\Model\Cloud;
13
use SixtyNine\Cloud\Placer\PlacerInterface;
14
15
class CloudRenderer
16
{
17
    /**
18
     * @param Cloud $cloud
19
     * @param bool $drawBoundingBoxes
20
     * @return \Imagine\Gd\Image|\Imagine\Image\ImageInterface
21
     */
22 1
    public function render(Cloud $cloud, FontsFactory $fontsFactory, $drawBoundingBoxes = false)
23
    {
24 1
        $imagine = new Imagine();
25 1
        $size  = new Box($cloud->getWidth(), $cloud->getHeight());
26 1
        $image = $imagine->create(
27
            $size,
28 1
            new Color($cloud->getBackgroundColor())
29
        );
30
31
        /** @var \SixtyNine\Cloud\Model\CloudWord $word */
32 1
        foreach ($cloud->getWords() as $word) {
33
34 1
            if (!$word->getIsVisible()) {
35
                continue;
36
            }
37
38 1
            $font = $fontsFactory->getImagineFont($cloud->getFont(), $word->getSize(), $word->getColor());
39 1
            $angle = $word->getAngle();
40 1
            $pos = $word->getPosition();
41 1
            $box =$word->getBox();
42
43 1
            $image->draw()->text(
44 1
                $word->getText(),
45
                $font,
46 1
                new Point($pos[0], $pos[1]),
47
                $angle
48
            );
49
50 1
            if ($drawBoundingBoxes) {
51 1
                if ($word->getAngle() === 0) {
52
                    $points = array(
53 1
                        new Point($pos[0], $pos[1]),
54 1
                        new Point($pos[0] + $box[0], $pos[1]),
55 1
                        new Point($pos[0] + $box[0], $pos[1] + $box[1]),
56 1
                        new Point($pos[0], $pos[1] + $box[1]),
57
                    );
58
                } else {
59
                    $points = array(
60 1
                        new Point($pos[0], $pos[1] + $box[0]),
61 1
                        new Point($pos[0] - $box[0], $pos[1] + $box[0]),
62 1
                        new Point($pos[0] - $box[0], $pos[1] - $box[1] + $box[0]),
63 1
                        new Point($pos[0], $pos[1] - $box[1] + $box[0]),
64
                    );
65
                }
66 1
                $image->draw()->polygon($points, new Color(0xFF0000));
67
            }
68
        }
69
70 1
        return $image;
71
    }
72
73 1
    public function renderUsher(
74
        ImageInterface $image,
75
        PlacerInterface $placer,
76
        $color,
77
        $maxIterations = 5000
78
    ) {
79 1
        $i = 0;
80 1
        $cur = $placer->getFirstPlaceToTry();
81 1
        $color = new Color($color);
82
83 1
        while($cur) {
84
85 1
            $next = $placer->getNextPlaceToTry($cur);
86
87 1
            if ($next) {
88 1
                $image->draw()->line($cur, $next, $color);
89
            }
90
91 1
            $i++;
92 1
            $cur = $next;
93
94 1
            if ($i >= $maxIterations) {
95 1
                break;
96
            }
97
        }
98 1
    }
99
}
100