1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-qrcode-component project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\QrCode; |
13
|
|
|
|
14
|
|
|
use Da\QrCode\Contracts\LabelInterface; |
15
|
|
|
use Da\QrCode\Exception\InvalidPathException; |
16
|
|
|
|
17
|
|
|
class Label implements LabelInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $text; |
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $fontSize; |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $font; |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $alignment; |
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $margins = [ |
39
|
|
|
't' => 0, |
40
|
|
|
'r' => 10, |
41
|
|
|
'b' => 10, |
42
|
|
|
'l' => 10, |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Label constructor. |
47
|
|
|
* |
48
|
|
|
* @param $text |
49
|
|
|
* @param string|null $font |
50
|
|
|
* @param int|null $fontSize |
51
|
|
|
* @param string|null $alignment |
52
|
|
|
* @param array $margins |
53
|
|
|
*/ |
54
|
|
|
public function __construct($text, $font = null, $fontSize = null, $alignment = null, array $margins = []) |
55
|
|
|
{ |
56
|
|
|
$this->text = $text; |
57
|
|
|
$this->font = $font ?: __DIR__ . '/../resources/fonts/noto_sans.otf'; |
58
|
|
|
$this->fontSize = $fontSize ?: 16; |
59
|
|
|
$this->alignment = $alignment ?: LabelInterface::ALIGN_CENTER; |
60
|
|
|
$this->margins = array_merge($this->margins, $margins); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
66
|
|
|
public function updateFontSize($size) |
67
|
|
|
{ |
68
|
|
|
$cloned = clone $this; |
69
|
|
|
$cloned->fontSize = $size; |
70
|
|
|
|
71
|
|
|
return $cloned; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
77
|
|
|
public function useFont($font) |
78
|
|
|
{ |
79
|
|
|
$path = realpath($font); |
80
|
|
|
if (!is_file($path)) { |
81
|
|
|
throw new InvalidPathException(sprintf('Invalid label font path "%s"', $path)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$cloned = clone $this; |
85
|
|
|
|
86
|
|
|
$cloned->font = $path; |
87
|
|
|
|
88
|
|
|
return $cloned; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function getFont() |
95
|
|
|
{ |
96
|
|
|
return $this->font; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritdoc |
101
|
|
|
*/ |
102
|
|
|
public function getText() |
103
|
|
|
{ |
104
|
|
|
return $this->text; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritdoc |
109
|
|
|
*/ |
110
|
|
|
public function getFontSize() |
111
|
|
|
{ |
112
|
|
|
return $this->fontSize; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
|
|
public function getAlignment() |
119
|
|
|
{ |
120
|
|
|
return $this->alignment; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritdoc |
125
|
|
|
*/ |
126
|
|
|
public function getMargins() |
127
|
|
|
{ |
128
|
|
|
return $this->margins; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|