|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud\Usher; |
|
4
|
|
|
|
|
5
|
|
|
use SixtyNine\Cloud\FontMetrics; |
|
6
|
|
|
use SixtyNine\DataTypes\Box; |
|
7
|
|
|
use Imagine\Image\Point; |
|
8
|
|
|
|
|
9
|
|
|
class PreciseMask extends QuadTreeMask implements MaskInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @param int $width |
|
13
|
|
|
* @param int $height |
|
14
|
|
|
* @param FontMetrics $metrics |
|
15
|
|
|
*/ |
|
16
|
|
|
public function __construct($width, $height, FontMetrics $metrics) { |
|
17
|
|
|
parent::__construct($width, $height); |
|
18
|
|
|
$this->metrics = $metrics; |
|
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $word |
|
23
|
|
|
* @param Box $place |
|
24
|
|
|
* @param string $font |
|
25
|
|
|
* @param int $size |
|
26
|
|
|
* @param int $angle |
|
27
|
|
|
*/ |
|
28
|
|
|
public function addWordToMask($word, Box $place, $font, $size, $angle) |
|
29
|
|
|
{ |
|
30
|
|
|
$base = $this->metrics->calculateSize($word, $font, $size, $angle); |
|
31
|
|
|
foreach (str_split($word) as $letter) { |
|
32
|
|
|
$box = $this->metrics->calculateSize($letter, $font, $size); |
|
33
|
|
|
|
|
34
|
|
|
if ($angle === 0) { |
|
35
|
|
|
$newPos = new Point($place->getX(), $place->getY() + ($base->getHeight() - $box->getHeight())); |
|
36
|
|
|
$this->add($newPos, $box, $angle); |
|
|
|
|
|
|
37
|
|
|
$place = $place->move($box->getWidth(), 0); |
|
38
|
|
|
continue; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// Invert width and height |
|
42
|
|
|
$box = new Box(0, 0, $box->getHeight(), $box->getWidth()); |
|
43
|
|
|
|
|
44
|
|
|
if ($place->getY() + ($base->getHeight() - $box->getHeight()) < 0) { |
|
45
|
|
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($place->getX() + ($base->getWidth() - $box->getWidth()) < 0) { |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$newPos = new Point( |
|
53
|
|
|
$place->getX() + ($base->getWidth() - $box->getWidth()), |
|
54
|
|
|
$place->getY() + ($base->getHeight() - $box->getHeight()) |
|
55
|
|
|
); |
|
56
|
|
|
$this->add($newPos, $box); |
|
57
|
|
|
$place = $place->move(0, -$box->getHeight()); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: