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