Completed
Push — master ( ba71a7...d22a4f )
by Dan
03:09
created

Usher::addWordToMask()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.025

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 32
ccs 18
cts 20
cp 0.9
rs 8.439
c 1
b 0
f 0
cc 5
eloc 19
nc 5
nop 5
crap 5.025
1
<?php
2
3
namespace SixtyNine\Cloud\Usher;
4
5
use Imagine\Image\Point;
6
use SixtyNine\Cloud\Drawer\Drawer;
7
use SixtyNine\Cloud\Factory\Logger;
8
use SixtyNine\Cloud\FontMetrics;
9
use SixtyNine\DataTypes\Box;
10
use SixtyNine\Cloud\Placer\PlacerInterface;
11
12
/**
13
 * Responsible to find a place for the word in the cloud
14
 */
15
16
class Usher
17
{
18
    const DEFAULT_MAX_TRIES = 100000;
19
20
    /** @var int */
21
    protected $maxTries;
22
23
    /** @var \SixtyNine\Cloud\Usher\MaskInterface */
24
    protected $mask;
25
26
    /** @var \SixtyNine\Cloud\Placer\PlacerInterface */
27
    protected $placer;
28
29
    /** @var \SixtyNine\Cloud\FontMetrics */
30
    protected $metrics;
31
32
    /** @var Logger */
33
    protected $logger;
34
35
    /**
36
     * @param int $imgWidth
37
     * @param int $imgHeight
38
     * @param PlacerInterface $placer
39
     * @param FontMetrics $metrics
40
     * @param int $maxTries
41
     */
42 6
    public function __construct(
43
        $imgWidth,
44
        $imgHeight,
45
        PlacerInterface $placer,
46
        FontMetrics $metrics,
47
        $maxTries = self::DEFAULT_MAX_TRIES
48
    ) {
49 6
        $this->mask = new QuadTreeMask($imgWidth, $imgHeight);
50 6
        $this->metrics = $metrics;
51 6
        $this->imgHeight = $imgHeight;
0 ignored issues
show
Bug introduced by
The property imgHeight does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52 6
        $this->imgWidth = $imgWidth;
0 ignored issues
show
Bug introduced by
The property imgWidth does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
53 6
        $this->maxTries = $maxTries;
54 6
        $this->placer = $placer;
55 6
        $this->logger = Logger::getInstance();
56 6
    }
57
58
    /**
59
     * @param string $word
60
     * @param string $font
61
     * @param int $fontSize
62
     * @param int $angle
63
     * @param bool $precise
64
     * @return bool|Box
65
     */
66 6
    public function getPlace($word, $font, $fontSize, $angle, $precise = false)
67
    {
68 6
        $this->logger->log(
69 6
            sprintf(
70 6
                'Search place for "%s", font = %s(%s), angle = %s',
71 6
                $word,
72 6
                str_replace('.ttf', '', $font),
73 6
                $fontSize,
74 6
                $angle
75
            ),
76 6
            Logger::DEBUG
77
        );
78
79 6
        $bounds = new Box(0, 0, $this->imgWidth, $this->imgHeight);
80 6
        $size = $this->metrics->calculateSize($word, $font, $fontSize);
81 6
        $box = Drawer::getBoxForText(0, 0, $size->getWidth(), $size->getHeight(), $angle);
82
83 6
        $this->logger->log('  Text dimensions: ' . $size->getDimensions(), Logger::DEBUG);
84
85 6
        $place = $this->searchPlace($bounds, $box);
86
87 6
        if (!$place) {
88 1
            return false;
89
        }
90
91 6
        if ($precise) {
92 1
            $this->addWordToMask($word, $place, $font, $fontSize, $angle);
93 1
            return $place;
94
        }
95
96 5
        $this->mask->add(new Point(0, 0), $place);
97 5
        return $place;
98
    }
99
100
    /**
101
     * @param string $word
102
     * @param Box $place
103
     * @param string $font
104
     * @param int $size
105
     * @param int $angle
106
     */
107 1
    public function addWordToMask($word, Box $place, $font, $size, $angle)
108
    {
109 1
        $base = $this->metrics->calculateSize($word, $font, $size, $angle);
110 1
        foreach (str_split($word) as $letter) {
111 1
            $box = $this->metrics->calculateSize($letter, $font, $size);
112
113 1
            if ($angle === 0) {
114 1
                $newPos = new Point($place->getX(), $place->getY() + ($base->getHeight() - $box->getHeight()));
115 1
                $this->mask->add($newPos, $box, $angle);
0 ignored issues
show
Unused Code introduced by
The call to MaskInterface::add() has too many arguments starting with $angle.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
116 1
                $place = $place->move($box->getWidth(), 0);
117 1
                continue;
118
            }
119
120
            // Invert width and height
121 1
            $box = new Box(0, 0, $box->getHeight(), $box->getWidth());
122
123 1
            if ($place->getY() + ($base->getHeight() - $box->getHeight()) < 0) {
124
                continue;
125
            }
126
127 1
            if ($place->getX() + ($base->getWidth() - $box->getWidth()) < 0) {
128
                continue;
129
            }
130
131 1
            $newPos = new Point(
132 1
                $place->getX() + ($base->getWidth() - $box->getWidth()),
133 1
                $place->getY() + ($base->getHeight() - $box->getHeight())
134
            );
135 1
            $this->mask->add($newPos, $box);
136 1
            $place = $place->move(0, -$box->getHeight());
137
        }
138 1
    }
139
140
    /**
141
     * Search a free place for a new box.
142
     * @param \SixtyNine\DataTypes\Box $bounds
143
     * @param \SixtyNine\DataTypes\Box $box
144
     * @return bool|Box
145
     */
146 6
    protected function searchPlace(Box $bounds, Box $box)
147
    {
148
149 6
        $this->logger->log('  Search place for ' . $box, Logger::DEBUG);
150
151 6
        $placeFound = false;
152 6
        $current = $this->placer->getFirstPlaceToTry();
153 6
        $curTry = 1;
154 6
        $currentBox = null;
155
156 6
        while (!$placeFound) {
157
158 6
            if (!$current) {
159 1
                return false;
160
            }
161
162 6
            if ($curTry > $this->maxTries) {
163
                return false;
164
            }
165
166 6
            $currentBox = $box->move($current->getX(), $current->getY());
167
168 6
            $outOfBounds = !$currentBox->inside($bounds);
169
170 6
            if (!$outOfBounds) {
171 6
                $placeFound = !$this->mask->overlaps($currentBox);
172 6
                $placeFound = $placeFound && !$outOfBounds;
173
            }
174
175 6
            $this->logger->log(sprintf(
176 6
                '  Trying %s --> %s',
177 6
                $currentBox,
178 6
                $outOfBounds ? 'Out of bounds' : ($placeFound ? 'OK' : 'Collision')
179 6
            ), Logger::DEBUG);
180
181 6
            if ($placeFound) {
182 6
                break;
183
            }
184
185 6
            $current = $this->placer->getNextPlaceToTry($current);
186 6
            $curTry++;
187
        }
188
189 6
        return $currentBox->inside($bounds) ? $currentBox : false;
190
    }
191
192
    /**
193
     * @return MaskInterface
194
     */
195 5
    public function getMask()
196
    {
197 5
        return $this->mask;
198
    }
199
}
200