1 | <?php |
||
2 | namespace tinymeng\code\Gateways\barcode; |
||
3 | |||
4 | /** |
||
5 | *-------------------------------------------------------------------- |
||
6 | * |
||
7 | * Class for Label |
||
8 | * |
||
9 | *-------------------------------------------------------------------- |
||
10 | * Copyright (C) Jean-Sebastien Goupil |
||
11 | * http://www.barcodephp.com |
||
12 | */ |
||
13 | class BCGLabel { |
||
14 | const POSITION_TOP = 0; |
||
15 | const POSITION_RIGHT = 1; |
||
16 | const POSITION_BOTTOM = 2; |
||
17 | const POSITION_LEFT = 3; |
||
18 | |||
19 | const ALIGN_LEFT = 0; |
||
20 | const ALIGN_TOP = 0; |
||
21 | const ALIGN_CENTER = 1; |
||
22 | const ALIGN_RIGHT = 2; |
||
23 | const ALIGN_BOTTOM = 2; |
||
24 | |||
25 | private $font; |
||
26 | private $text; |
||
27 | private $position; |
||
28 | private $alignment; |
||
29 | private $offset; |
||
30 | private $spacing; |
||
31 | private $rotationAngle; |
||
32 | private $backgroundColor; |
||
33 | private $foregroundColor; |
||
34 | |||
35 | /** |
||
36 | * Constructor. |
||
37 | * |
||
38 | * @param string $text |
||
39 | * @param BCGFont $font |
||
40 | * @param int $position |
||
41 | * @param int $alignment |
||
42 | */ |
||
43 | public function __construct($text = '', $font = null, $position = self::POSITION_BOTTOM, $alignment = self::ALIGN_CENTER) { |
||
44 | $font = $font === null ? new BCGFontPhp(5) : $font; |
||
45 | $this->setFont($font); |
||
46 | $this->setText($text); |
||
47 | $this->setPosition($position); |
||
48 | $this->setAlignment($alignment); |
||
49 | $this->setSpacing(4); |
||
50 | $this->setOffset(0); |
||
51 | $this->setRotationAngle(0); |
||
52 | $this->setBackgroundColor(new BCGColor('white')); |
||
53 | $this->setForegroundColor(new BCGColor('black')); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Gets the text. |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | public function getText() { |
||
62 | return $this->font->getText(); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Sets the text. |
||
67 | * |
||
68 | * @param string $text |
||
69 | */ |
||
70 | public function setText($text) { |
||
71 | $this->text = $text; |
||
72 | $this->font->setText($this->text); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Gets the font. |
||
77 | * |
||
78 | * @return BCGFont |
||
79 | */ |
||
80 | public function getFont() { |
||
81 | return $this->font; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Sets the font. |
||
86 | * |
||
87 | * @param BCGFont $font |
||
88 | */ |
||
89 | public function setFont($font) { |
||
90 | if ($font === null) { |
||
91 | throw new BCGArgumentException('Font cannot be null.', 'font'); |
||
92 | } |
||
93 | |||
94 | $this->font = clone $font; |
||
95 | $this->font->setText($this->text); |
||
96 | $this->font->setRotationAngle($this->rotationAngle); |
||
97 | $this->font->setBackgroundColor($this->backgroundColor); |
||
98 | $this->font->setForegroundColor($this->foregroundColor); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Gets the text position for drawing. |
||
103 | * |
||
104 | * @return int |
||
105 | */ |
||
106 | public function getPosition() { |
||
107 | return $this->position; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Sets the text position for drawing. |
||
112 | * |
||
113 | * @param int $position |
||
114 | */ |
||
115 | public function setPosition($position) { |
||
116 | $position = intval($position); |
||
117 | if ($position !== self::POSITION_TOP && $position !== self::POSITION_RIGHT && $position !== self::POSITION_BOTTOM && $position !== self::POSITION_LEFT) { |
||
118 | throw new BCGArgumentException('The text position must be one of a valid constant.', 'position'); |
||
119 | } |
||
120 | |||
121 | $this->position = $position; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Gets the text alignment for drawing. |
||
126 | * |
||
127 | * @return int |
||
128 | */ |
||
129 | public function getAlignment() { |
||
130 | return $this->alignment; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Sets the text alignment for drawing. |
||
135 | * |
||
136 | * @param int $alignment |
||
137 | */ |
||
138 | public function setAlignment($alignment) { |
||
139 | $alignment = intval($alignment); |
||
140 | if ($alignment !== self::ALIGN_LEFT && $alignment !== self::ALIGN_TOP && $alignment !== self::ALIGN_CENTER && $alignment !== self::ALIGN_RIGHT && $alignment !== self::ALIGN_BOTTOM) { |
||
141 | throw new BCGArgumentException('The text alignment must be one of a valid constant.', 'alignment'); |
||
142 | } |
||
143 | |||
144 | $this->alignment = $alignment; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Gets the offset. |
||
149 | * |
||
150 | * @return int |
||
151 | */ |
||
152 | public function getOffset() { |
||
153 | return $this->offset; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Sets the offset. |
||
158 | * |
||
159 | * @param int $offset |
||
160 | */ |
||
161 | public function setOffset($offset) { |
||
162 | $this->offset = intval($offset); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Gets the spacing. |
||
167 | * |
||
168 | * @return int |
||
169 | */ |
||
170 | public function getSpacing() { |
||
171 | return $this->spacing; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Sets the spacing. |
||
176 | * |
||
177 | * @param int $spacing |
||
178 | */ |
||
179 | public function setSpacing($spacing) { |
||
180 | $this->spacing = max(0, intval($spacing)); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Gets the rotation angle in degree. |
||
185 | * |
||
186 | * @return int |
||
187 | */ |
||
188 | public function getRotationAngle() { |
||
189 | return $this->font->getRotationAngle(); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Sets the rotation angle in degree. |
||
194 | * |
||
195 | * @param int $rotationAngle |
||
196 | */ |
||
197 | public function setRotationAngle($rotationAngle) { |
||
198 | $this->rotationAngle = intval($rotationAngle); |
||
199 | $this->font->setRotationAngle($this->rotationAngle); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Gets the background color in case of rotation. |
||
204 | * |
||
205 | * @return BCGColor |
||
206 | */ |
||
207 | public function getBackgroundColor() { |
||
208 | return $this->backgroundColor; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Sets the background color in case of rotation. |
||
213 | * |
||
214 | * @param BCGColor $backgroundColor |
||
215 | */ |
||
216 | public /*internal*/ function setBackgroundColor($backgroundColor) { |
||
217 | $this->backgroundColor = $backgroundColor; |
||
218 | $this->font->setBackgroundColor($this->backgroundColor); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Gets the foreground color. |
||
223 | * |
||
224 | * @return BCGColor |
||
225 | */ |
||
226 | public function getForegroundColor() { |
||
227 | return $this->font->getForegroundColor(); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Sets the foreground color. |
||
232 | * |
||
233 | * @param BCGColor $foregroundColor |
||
234 | */ |
||
235 | public function setForegroundColor($foregroundColor) { |
||
236 | $this->foregroundColor = $foregroundColor; |
||
237 | $this->font->setForegroundColor($this->foregroundColor); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Gets the dimension taken by the label, including the spacing and offset. |
||
242 | * [0]: width |
||
243 | * [1]: height |
||
244 | * |
||
245 | * @return int[] |
||
246 | */ |
||
247 | public function getDimension() { |
||
248 | $w = 0; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
249 | $h = 0; |
||
0 ignored issues
–
show
|
|||
250 | |||
251 | $dimension = $this->font->getDimension(); |
||
252 | $w = $dimension[0]; |
||
253 | $h = $dimension[1]; |
||
254 | |||
255 | if ($this->position === self::POSITION_TOP || $this->position === self::POSITION_BOTTOM) { |
||
256 | $h += $this->spacing; |
||
257 | $w += max(0, $this->offset); |
||
258 | } else { |
||
259 | $w += $this->spacing; |
||
260 | $h += max(0, $this->offset); |
||
261 | } |
||
262 | |||
263 | return array($w, $h); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Draws the text. |
||
268 | * The coordinate passed are the positions of the barcode. |
||
269 | * $x1 and $y1 represent the top left corner. |
||
270 | * $x2 and $y2 represent the bottom right corner. |
||
271 | * |
||
272 | * @param resource $im |
||
273 | * @param int $x1 |
||
274 | * @param int $y1 |
||
275 | * @param int $x2 |
||
276 | * @param int $y2 |
||
277 | */ |
||
278 | public /*internal*/ function draw($im, $x1, $y1, $x2, $y2) { |
||
279 | $x = 0; |
||
280 | $y = 0; |
||
281 | |||
282 | $fontDimension = $this->font->getDimension(); |
||
283 | |||
284 | if ($this->position === self::POSITION_TOP || $this->position === self::POSITION_BOTTOM) { |
||
285 | if ($this->position === self::POSITION_TOP) { |
||
286 | $y = $y1 - $this->spacing - $fontDimension[1]; |
||
287 | } elseif ($this->position === self::POSITION_BOTTOM) { |
||
288 | $y = $y2 + $this->spacing; |
||
289 | } |
||
290 | |||
291 | if ($this->alignment === self::ALIGN_CENTER) { |
||
292 | $x = ($x2 - $x1) / 2 + $x1 - $fontDimension[0] / 2 + $this->offset; |
||
293 | } elseif ($this->alignment === self::ALIGN_LEFT) { |
||
294 | $x = $x1 + $this->offset; |
||
295 | } else { |
||
296 | $x = $x2 + $this->offset - $fontDimension[0]; |
||
297 | } |
||
298 | } else { |
||
299 | if ($this->position === self::POSITION_LEFT) { |
||
300 | $x = $x1 - $this->spacing - $fontDimension[0]; |
||
301 | } elseif ($this->position === self::POSITION_RIGHT) { |
||
302 | $x = $x2 + $this->spacing; |
||
303 | } |
||
304 | |||
305 | if ($this->alignment === self::ALIGN_CENTER) { |
||
306 | $y = ($y2 - $y1) / 2 + $y1 - $fontDimension[1] / 2 + $this->offset; |
||
307 | } elseif ($this->alignment === self::ALIGN_TOP) { |
||
308 | $y = $y1 + $this->offset; |
||
309 | } else { |
||
310 | $y = $y2 + $this->offset - $fontDimension[1]; |
||
311 | } |
||
312 | } |
||
313 | |||
314 | $this->font->setText($this->text); |
||
315 | $this->font->draw($im, $x, $y); |
||
316 | } |
||
317 | } |
||
318 | ?> |
||
0 ignored issues
–
show
It is not recommended to use PHP's closing tag
?> in files other than templates.
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore. A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever. ![]() |